Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 3 | * |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
Bodo Möller | f1fd454 | 2006-01-03 03:27:19 +0000 | [diff] [blame] | 8 | */ |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 9 | |
| 10 | #include <stdio.h> |
Emilia Kasper | aa474d1 | 2016-02-19 17:24:44 +0100 | [diff] [blame] | 11 | #include <stdlib.h> |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 12 | #include <openssl/objects.h> |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 13 | #include <openssl/evp.h> |
| 14 | #include <openssl/hmac.h> |
Dr. Stephen Henson | 67c8e7f | 2007-09-26 21:56:59 +0000 | [diff] [blame] | 15 | #include <openssl/ocsp.h> |
Matt Caswell | 5951e84 | 2016-04-20 16:38:29 +0100 | [diff] [blame] | 16 | #include <openssl/conf.h> |
| 17 | #include <openssl/x509v3.h> |
Rich Salz | 3c27208 | 2016-03-18 14:30:20 -0400 | [diff] [blame] | 18 | #include <openssl/dh.h> |
| 19 | #include <openssl/bn.h> |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 20 | #include "ssl_locl.h" |
Rich Salz | 3c27208 | 2016-03-18 14:30:20 -0400 | [diff] [blame] | 21 | #include <openssl/ct.h> |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 22 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 23 | SSL3_ENC_METHOD const TLSv1_enc_data = { |
| 24 | tls1_enc, |
| 25 | tls1_mac, |
| 26 | tls1_setup_key_block, |
| 27 | tls1_generate_master_secret, |
| 28 | tls1_change_cipher_state, |
| 29 | tls1_final_finish_mac, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 30 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 31 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 32 | tls1_alert_code, |
| 33 | tls1_export_keying_material, |
| 34 | 0, |
Matt Caswell | a29fa98 | 2016-09-29 22:40:15 +0100 | [diff] [blame] | 35 | ssl3_set_handshake_header, |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 36 | tls_close_construct_packet, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 37 | ssl3_handshake_write |
| 38 | }; |
Dr. Stephen Henson | 173e72e | 2013-03-11 15:34:28 +0000 | [diff] [blame] | 39 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 40 | SSL3_ENC_METHOD const TLSv1_1_enc_data = { |
| 41 | tls1_enc, |
| 42 | tls1_mac, |
| 43 | tls1_setup_key_block, |
| 44 | tls1_generate_master_secret, |
| 45 | tls1_change_cipher_state, |
| 46 | tls1_final_finish_mac, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 47 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 48 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 49 | tls1_alert_code, |
| 50 | tls1_export_keying_material, |
| 51 | SSL_ENC_FLAG_EXPLICIT_IV, |
Matt Caswell | a29fa98 | 2016-09-29 22:40:15 +0100 | [diff] [blame] | 52 | ssl3_set_handshake_header, |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 53 | tls_close_construct_packet, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 54 | ssl3_handshake_write |
| 55 | }; |
Dr. Stephen Henson | 173e72e | 2013-03-11 15:34:28 +0000 | [diff] [blame] | 56 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 57 | SSL3_ENC_METHOD const TLSv1_2_enc_data = { |
| 58 | tls1_enc, |
| 59 | tls1_mac, |
| 60 | tls1_setup_key_block, |
| 61 | tls1_generate_master_secret, |
| 62 | tls1_change_cipher_state, |
| 63 | tls1_final_finish_mac, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 64 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 65 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 66 | tls1_alert_code, |
| 67 | tls1_export_keying_material, |
| 68 | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF |
| 69 | | SSL_ENC_FLAG_TLS1_2_CIPHERS, |
Matt Caswell | a29fa98 | 2016-09-29 22:40:15 +0100 | [diff] [blame] | 70 | ssl3_set_handshake_header, |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 71 | tls_close_construct_packet, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 72 | ssl3_handshake_write |
| 73 | }; |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 74 | |
Matt Caswell | 582a17d | 2016-10-21 17:39:33 +0100 | [diff] [blame] | 75 | SSL3_ENC_METHOD const TLSv1_3_enc_data = { |
Matt Caswell | bebc0c7 | 2016-11-17 18:00:17 +0000 | [diff] [blame] | 76 | tls13_enc, |
Matt Caswell | 582a17d | 2016-10-21 17:39:33 +0100 | [diff] [blame] | 77 | tls1_mac, |
Matt Caswell | 92760c2 | 2016-11-09 14:06:12 +0000 | [diff] [blame] | 78 | tls13_setup_key_block, |
| 79 | tls13_generate_master_secret, |
| 80 | tls13_change_cipher_state, |
| 81 | tls13_final_finish_mac, |
Matt Caswell | 582a17d | 2016-10-21 17:39:33 +0100 | [diff] [blame] | 82 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 83 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
Matt Caswell | 0490431 | 2016-12-30 11:26:39 +0000 | [diff] [blame] | 84 | tls13_alert_code, |
Matt Caswell | 582a17d | 2016-10-21 17:39:33 +0100 | [diff] [blame] | 85 | tls1_export_keying_material, |
Matt Caswell | bebc0c7 | 2016-11-17 18:00:17 +0000 | [diff] [blame] | 86 | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF, |
Matt Caswell | 582a17d | 2016-10-21 17:39:33 +0100 | [diff] [blame] | 87 | ssl3_set_handshake_header, |
| 88 | tls_close_construct_packet, |
| 89 | ssl3_handshake_write |
| 90 | }; |
| 91 | |
Dr. Stephen Henson | f3b656b | 2005-08-05 23:56:11 +0000 | [diff] [blame] | 92 | long tls1_default_timeout(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 93 | { |
| 94 | /* |
| 95 | * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for |
| 96 | * http, the cache would over fill |
| 97 | */ |
| 98 | return (60 * 60 * 2); |
| 99 | } |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 100 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 101 | int tls1_new(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 102 | { |
| 103 | if (!ssl3_new(s)) |
| 104 | return (0); |
| 105 | s->method->ssl_clear(s); |
| 106 | return (1); |
| 107 | } |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 108 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 109 | void tls1_free(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 110 | { |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 111 | OPENSSL_free(s->ext.session_ticket); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 112 | ssl3_free(s); |
| 113 | } |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 114 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 115 | void tls1_clear(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 116 | { |
| 117 | ssl3_clear(s); |
Viktor Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 118 | if (s->method->version == TLS_ANY_VERSION) |
| 119 | s->version = TLS_MAX_VERSION; |
| 120 | else |
| 121 | s->version = s->method->version; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 122 | } |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 123 | |
Dr. Stephen Henson | 525de5d | 2007-08-12 23:59:05 +0000 | [diff] [blame] | 124 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | eda3766 | 2011-05-30 17:58:13 +0000 | [diff] [blame] | 125 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 126 | typedef struct { |
| 127 | int nid; /* Curve NID */ |
| 128 | int secbits; /* Bits of security (from SP800-57) */ |
| 129 | unsigned int flags; /* Flags: currently just field type */ |
| 130 | } tls_curve_info; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 131 | |
Dr. Stephen Henson | 2dc1aee | 2016-02-13 15:27:43 +0000 | [diff] [blame] | 132 | /* |
| 133 | * Table of curve information. |
Rich Salz | ddb4c04 | 2016-01-03 13:24:32 -0500 | [diff] [blame] | 134 | * Do not delete entries or reorder this array! It is used as a lookup |
Dr. Stephen Henson | 2dc1aee | 2016-02-13 15:27:43 +0000 | [diff] [blame] | 135 | * table: the index of each entry is one less than the TLS curve id. |
| 136 | */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 137 | static const tls_curve_info nid_list[] = { |
| 138 | {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */ |
| 139 | {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */ |
| 140 | {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */ |
| 141 | {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */ |
| 142 | {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */ |
| 143 | {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */ |
| 144 | {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */ |
| 145 | {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */ |
| 146 | {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */ |
| 147 | {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */ |
| 148 | {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */ |
| 149 | {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */ |
| 150 | {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */ |
| 151 | {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */ |
| 152 | {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */ |
| 153 | {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */ |
| 154 | {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */ |
| 155 | {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */ |
| 156 | {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */ |
| 157 | {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */ |
| 158 | {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */ |
| 159 | {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */ |
| 160 | {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */ |
| 161 | {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */ |
| 162 | {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */ |
| 163 | {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */ |
| 164 | {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */ |
| 165 | {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */ |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 166 | {NID_X25519, 128, TLS_CURVE_CUSTOM}, /* X25519 (29) */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 167 | }; |
Dr. Stephen Henson | eda3766 | 2011-05-30 17:58:13 +0000 | [diff] [blame] | 168 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 169 | static const unsigned char ecformats_default[] = { |
| 170 | TLSEXT_ECPOINTFORMAT_uncompressed, |
| 171 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime, |
| 172 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 |
| 173 | }; |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 174 | |
Kurt Roeckx | fe6ef24 | 2015-12-04 22:30:36 +0100 | [diff] [blame] | 175 | /* The default curves */ |
| 176 | static const unsigned char eccurves_default[] = { |
Dr. Stephen Henson | 1db3107 | 2016-02-25 17:46:14 +0000 | [diff] [blame] | 177 | 0, 29, /* X25519 (29) */ |
Emilia Kasper | de57d23 | 2015-05-20 15:47:51 +0200 | [diff] [blame] | 178 | 0, 23, /* secp256r1 (23) */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 179 | 0, 25, /* secp521r1 (25) */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 180 | 0, 24, /* secp384r1 (24) */ |
Emilia Kasper | de57d23 | 2015-05-20 15:47:51 +0200 | [diff] [blame] | 181 | }; |
| 182 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 183 | static const unsigned char suiteb_curves[] = { |
| 184 | 0, TLSEXT_curve_P_256, |
| 185 | 0, TLSEXT_curve_P_384 |
| 186 | }; |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 187 | |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 188 | int tls1_ec_curve_id2nid(int curve_id, unsigned int *pflags) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 189 | { |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 190 | const tls_curve_info *cinfo; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 191 | /* ECC curves from RFC 4492 and RFC 7027 */ |
Dr. Stephen Henson | b6eb982 | 2015-05-02 18:30:00 +0100 | [diff] [blame] | 192 | if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list))) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 193 | return 0; |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 194 | cinfo = nid_list + curve_id - 1; |
| 195 | if (pflags) |
| 196 | *pflags = cinfo->flags; |
| 197 | return cinfo->nid; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 198 | } |
Dr. Stephen Henson | 525de5d | 2007-08-12 23:59:05 +0000 | [diff] [blame] | 199 | |
| 200 | int tls1_ec_nid2curve_id(int nid) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 201 | { |
Dr. Stephen Henson | 2fa2d15 | 2016-02-13 15:28:25 +0000 | [diff] [blame] | 202 | size_t i; |
| 203 | for (i = 0; i < OSSL_NELEM(nid_list); i++) { |
| 204 | if (nid_list[i].nid == nid) |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 205 | return (int)(i + 1); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 206 | } |
Dr. Stephen Henson | 2fa2d15 | 2016-02-13 15:28:25 +0000 | [diff] [blame] | 207 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Emilia Kasper | 740580c | 2014-12-01 16:55:55 +0100 | [diff] [blame] | 210 | /* |
| 211 | * Get curves list, if "sess" is set return client curves otherwise |
| 212 | * preferred list. |
| 213 | * Sets |num_curves| to the number of curves in the list, i.e., |
| 214 | * the length of |pcurves| is 2 * num_curves. |
| 215 | * Returns 1 on success and 0 if the client curves list has invalid format. |
| 216 | * The latter indicates an internal error: we should not be accepting such |
| 217 | * lists in the first place. |
| 218 | * TODO(emilia): we should really be storing the curves list in explicitly |
| 219 | * parsed form instead. (However, this would affect binary compatibility |
| 220 | * so cannot happen in the 1.0.x series.) |
Dr. Stephen Henson | fd2b65c | 2012-04-04 14:41:01 +0000 | [diff] [blame] | 221 | */ |
Matt Caswell | 6b473ac | 2016-11-24 16:59:48 +0000 | [diff] [blame] | 222 | int tls1_get_curvelist(SSL *s, int sess, const unsigned char **pcurves, |
| 223 | size_t *num_curves) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 224 | { |
| 225 | size_t pcurveslen = 0; |
Rich Salz | 3e37351 | 2016-09-19 13:09:58 -0400 | [diff] [blame] | 226 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 227 | if (sess) { |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 228 | *pcurves = s->session->ext.supportedgroups; |
| 229 | pcurveslen = s->session->ext.supportedgroups_len; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 230 | } else { |
| 231 | /* For Suite B mode only include P-256, P-384 */ |
| 232 | switch (tls1_suiteb(s)) { |
| 233 | case SSL_CERT_FLAG_SUITEB_128_LOS: |
| 234 | *pcurves = suiteb_curves; |
| 235 | pcurveslen = sizeof(suiteb_curves); |
| 236 | break; |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 237 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 238 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: |
| 239 | *pcurves = suiteb_curves; |
| 240 | pcurveslen = 2; |
| 241 | break; |
Dr. Stephen Henson | b34aa49 | 2012-12-10 02:02:16 +0000 | [diff] [blame] | 242 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 243 | case SSL_CERT_FLAG_SUITEB_192_LOS: |
| 244 | *pcurves = suiteb_curves + 2; |
| 245 | pcurveslen = 2; |
| 246 | break; |
| 247 | default: |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 248 | *pcurves = s->ext.supportedgroups; |
| 249 | pcurveslen = s->ext.supportedgroups_len; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 250 | } |
| 251 | if (!*pcurves) { |
Kurt Roeckx | fe6ef24 | 2015-12-04 22:30:36 +0100 | [diff] [blame] | 252 | *pcurves = eccurves_default; |
| 253 | pcurveslen = sizeof(eccurves_default); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
Emilia Kasper | 740580c | 2014-12-01 16:55:55 +0100 | [diff] [blame] | 256 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 257 | /* We do not allow odd length arrays to enter the system. */ |
| 258 | if (pcurveslen & 1) { |
| 259 | SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR); |
| 260 | *num_curves = 0; |
| 261 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 262 | } |
Rich Salz | 3e37351 | 2016-09-19 13:09:58 -0400 | [diff] [blame] | 263 | *num_curves = pcurveslen / 2; |
| 264 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 265 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 266 | |
| 267 | /* See if curve is allowed by security callback */ |
Matt Caswell | 6b473ac | 2016-11-24 16:59:48 +0000 | [diff] [blame] | 268 | int tls_curve_allowed(SSL *s, const unsigned char *curve, int op) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 269 | { |
| 270 | const tls_curve_info *cinfo; |
| 271 | if (curve[0]) |
| 272 | return 1; |
Dr. Stephen Henson | b6eb982 | 2015-05-02 18:30:00 +0100 | [diff] [blame] | 273 | if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list))) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 274 | return 0; |
| 275 | cinfo = &nid_list[curve[1] - 1]; |
| 276 | # ifdef OPENSSL_NO_EC2M |
| 277 | if (cinfo->flags & TLS_CURVE_CHAR2) |
| 278 | return 0; |
| 279 | # endif |
| 280 | return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve); |
| 281 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 282 | |
Dr. Stephen Henson | d18b716 | 2012-07-24 13:47:40 +0000 | [diff] [blame] | 283 | /* Check a curve is one of our preferences */ |
| 284 | int tls1_check_curve(SSL *s, const unsigned char *p, size_t len) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 285 | { |
| 286 | const unsigned char *curves; |
| 287 | size_t num_curves, i; |
| 288 | unsigned int suiteb_flags = tls1_suiteb(s); |
| 289 | if (len != 3 || p[0] != NAMED_CURVE_TYPE) |
| 290 | return 0; |
| 291 | /* Check curve matches Suite B preferences */ |
| 292 | if (suiteb_flags) { |
| 293 | unsigned long cid = s->s3->tmp.new_cipher->id; |
| 294 | if (p[1]) |
| 295 | return 0; |
| 296 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) { |
| 297 | if (p[2] != TLSEXT_curve_P_256) |
| 298 | return 0; |
| 299 | } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) { |
| 300 | if (p[2] != TLSEXT_curve_P_384) |
| 301 | return 0; |
| 302 | } else /* Should never happen */ |
| 303 | return 0; |
| 304 | } |
| 305 | if (!tls1_get_curvelist(s, 0, &curves, &num_curves)) |
| 306 | return 0; |
| 307 | for (i = 0; i < num_curves; i++, curves += 2) { |
| 308 | if (p[1] == curves[0] && p[2] == curves[1]) |
| 309 | return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK); |
| 310 | } |
| 311 | return 0; |
| 312 | } |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 313 | |
Tim Hudson | 1d97c84 | 2014-12-28 12:48:40 +1000 | [diff] [blame] | 314 | /*- |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 315 | * For nmatch >= 0, return the NID of the |nmatch|th shared group or NID_undef |
Kurt Roeckx | 6977e8e | 2015-12-04 22:25:11 +0100 | [diff] [blame] | 316 | * if there is no match. |
| 317 | * For nmatch == -1, return number of matches |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 318 | * For nmatch == -2, return the NID of the group to use for |
Emilia Kasper | 376e2ca | 2014-12-04 15:00:11 +0100 | [diff] [blame] | 319 | * an EC tmp key, or NID_undef if there is no match. |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 320 | */ |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 321 | int tls1_shared_group(SSL *s, int nmatch) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 322 | { |
| 323 | const unsigned char *pref, *supp; |
| 324 | size_t num_pref, num_supp, i, j; |
| 325 | int k; |
Rich Salz | 3e37351 | 2016-09-19 13:09:58 -0400 | [diff] [blame] | 326 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 327 | /* Can't do anything on client side */ |
| 328 | if (s->server == 0) |
| 329 | return -1; |
| 330 | if (nmatch == -2) { |
| 331 | if (tls1_suiteb(s)) { |
| 332 | /* |
| 333 | * For Suite B ciphersuite determines curve: we already know |
| 334 | * these are acceptable due to previous checks. |
| 335 | */ |
| 336 | unsigned long cid = s->s3->tmp.new_cipher->id; |
Rich Salz | 3e37351 | 2016-09-19 13:09:58 -0400 | [diff] [blame] | 337 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 338 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
| 339 | return NID_X9_62_prime256v1; /* P-256 */ |
| 340 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
| 341 | return NID_secp384r1; /* P-384 */ |
| 342 | /* Should never happen */ |
| 343 | return NID_undef; |
| 344 | } |
| 345 | /* If not Suite B just return first preference shared curve */ |
| 346 | nmatch = 0; |
| 347 | } |
| 348 | /* |
| 349 | * Avoid truncation. tls1_get_curvelist takes an int |
| 350 | * but s->options is a long... |
| 351 | */ |
Rich Salz | 3e37351 | 2016-09-19 13:09:58 -0400 | [diff] [blame] | 352 | if (!tls1_get_curvelist(s, |
| 353 | (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, |
| 354 | &supp, &num_supp)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 355 | /* In practice, NID_undef == 0 but let's be precise. */ |
| 356 | return nmatch == -1 ? 0 : NID_undef; |
Rich Salz | 3e37351 | 2016-09-19 13:09:58 -0400 | [diff] [blame] | 357 | if (!tls1_get_curvelist(s, |
| 358 | (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0, |
| 359 | &pref, &num_pref)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 360 | return nmatch == -1 ? 0 : NID_undef; |
Kurt Roeckx | 3c06513 | 2015-05-30 19:20:12 +0200 | [diff] [blame] | 361 | |
Rich Salz | 3e37351 | 2016-09-19 13:09:58 -0400 | [diff] [blame] | 362 | for (k = 0, i = 0; i < num_pref; i++, pref += 2) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 363 | const unsigned char *tsupp = supp; |
Rich Salz | 3e37351 | 2016-09-19 13:09:58 -0400 | [diff] [blame] | 364 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 365 | for (j = 0; j < num_supp; j++, tsupp += 2) { |
| 366 | if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) { |
| 367 | if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED)) |
| 368 | continue; |
| 369 | if (nmatch == k) { |
| 370 | int id = (pref[0] << 8) | pref[1]; |
Rich Salz | 3e37351 | 2016-09-19 13:09:58 -0400 | [diff] [blame] | 371 | |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 372 | return tls1_ec_curve_id2nid(id, NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 373 | } |
| 374 | k++; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | if (nmatch == -1) |
| 379 | return k; |
| 380 | /* Out of range (nmatch > k). */ |
| 381 | return NID_undef; |
| 382 | } |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 383 | |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 384 | int tls1_set_groups(unsigned char **pext, size_t *pextlen, |
| 385 | int *groups, size_t ngroups) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 386 | { |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 387 | unsigned char *glist, *p; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 388 | size_t i; |
| 389 | /* |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 390 | * Bitmap of groups included to detect duplicates: only works while group |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 391 | * ids < 32 |
| 392 | */ |
| 393 | unsigned long dup_list = 0; |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 394 | glist = OPENSSL_malloc(ngroups * 2); |
| 395 | if (glist == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 396 | return 0; |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 397 | for (i = 0, p = glist; i < ngroups; i++) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 398 | unsigned long idmask; |
| 399 | int id; |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 400 | /* TODO(TLS1.3): Convert for DH groups */ |
| 401 | id = tls1_ec_nid2curve_id(groups[i]); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 402 | idmask = 1L << id; |
| 403 | if (!id || (dup_list & idmask)) { |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 404 | OPENSSL_free(glist); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | dup_list |= idmask; |
| 408 | s2n(id, p); |
| 409 | } |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 410 | OPENSSL_free(*pext); |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 411 | *pext = glist; |
| 412 | *pextlen = ngroups * 2; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 413 | return 1; |
| 414 | } |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 415 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 416 | # define MAX_CURVELIST 28 |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 417 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 418 | typedef struct { |
| 419 | size_t nidcnt; |
| 420 | int nid_arr[MAX_CURVELIST]; |
| 421 | } nid_cb_st; |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 422 | |
| 423 | static int nid_cb(const char *elem, int len, void *arg) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 424 | { |
| 425 | nid_cb_st *narg = arg; |
| 426 | size_t i; |
| 427 | int nid; |
| 428 | char etmp[20]; |
Kurt Roeckx | 2747d73 | 2015-01-24 14:46:50 +0100 | [diff] [blame] | 429 | if (elem == NULL) |
| 430 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 431 | if (narg->nidcnt == MAX_CURVELIST) |
| 432 | return 0; |
| 433 | if (len > (int)(sizeof(etmp) - 1)) |
| 434 | return 0; |
| 435 | memcpy(etmp, elem, len); |
| 436 | etmp[len] = 0; |
| 437 | nid = EC_curve_nist2nid(etmp); |
| 438 | if (nid == NID_undef) |
| 439 | nid = OBJ_sn2nid(etmp); |
| 440 | if (nid == NID_undef) |
| 441 | nid = OBJ_ln2nid(etmp); |
| 442 | if (nid == NID_undef) |
| 443 | return 0; |
| 444 | for (i = 0; i < narg->nidcnt; i++) |
| 445 | if (narg->nid_arr[i] == nid) |
| 446 | return 0; |
| 447 | narg->nid_arr[narg->nidcnt++] = nid; |
| 448 | return 1; |
| 449 | } |
| 450 | |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 451 | /* Set groups based on a colon separate list */ |
| 452 | int tls1_set_groups_list(unsigned char **pext, size_t *pextlen, const char *str) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 453 | { |
| 454 | nid_cb_st ncb; |
| 455 | ncb.nidcnt = 0; |
| 456 | if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb)) |
| 457 | return 0; |
| 458 | if (pext == NULL) |
| 459 | return 1; |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 460 | return tls1_set_groups(pext, pextlen, ncb.nid_arr, ncb.nidcnt); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Dr. Stephen Henson | fd2b65c | 2012-04-04 14:41:01 +0000 | [diff] [blame] | 463 | /* For an EC key set TLS id and required compression based on parameters */ |
| 464 | static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 465 | EC_KEY *ec) |
| 466 | { |
Dr. Stephen Henson | 2235b7f | 2016-02-13 15:26:15 +0000 | [diff] [blame] | 467 | int id; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 468 | const EC_GROUP *grp; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 469 | if (!ec) |
| 470 | return 0; |
| 471 | /* Determine if it is a prime field */ |
| 472 | grp = EC_KEY_get0_group(ec); |
| 473 | if (!grp) |
| 474 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 475 | /* Determine curve ID */ |
| 476 | id = EC_GROUP_get_curve_name(grp); |
| 477 | id = tls1_ec_nid2curve_id(id); |
Dr. Stephen Henson | 2235b7f | 2016-02-13 15:26:15 +0000 | [diff] [blame] | 478 | /* If no id return error: we don't support arbitrary explicit curves */ |
| 479 | if (id == 0) |
| 480 | return 0; |
| 481 | curve_id[0] = 0; |
| 482 | curve_id[1] = (unsigned char)id; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 483 | if (comp_id) { |
| 484 | if (EC_KEY_get0_public_key(ec) == NULL) |
| 485 | return 0; |
Dr. Stephen Henson | 2235b7f | 2016-02-13 15:26:15 +0000 | [diff] [blame] | 486 | if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) { |
| 487 | *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; |
| 488 | } else { |
| 489 | if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 490 | *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
| 491 | else |
| 492 | *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; |
Dr. Stephen Henson | 2235b7f | 2016-02-13 15:26:15 +0000 | [diff] [blame] | 493 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 494 | } |
| 495 | return 1; |
| 496 | } |
| 497 | |
Dr. Stephen Henson | fd2b65c | 2012-04-04 14:41:01 +0000 | [diff] [blame] | 498 | /* Check an EC key is compatible with extensions */ |
| 499 | static int tls1_check_ec_key(SSL *s, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 500 | unsigned char *curve_id, unsigned char *comp_id) |
| 501 | { |
| 502 | const unsigned char *pformats, *pcurves; |
| 503 | size_t num_formats, num_curves, i; |
| 504 | int j; |
| 505 | /* |
| 506 | * If point formats extension present check it, otherwise everything is |
| 507 | * supported (see RFC4492). |
| 508 | */ |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 509 | if (comp_id && s->session->ext.ecpointformats) { |
| 510 | pformats = s->session->ext.ecpointformats; |
| 511 | num_formats = s->session->ext.ecpointformats_len; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 512 | for (i = 0; i < num_formats; i++, pformats++) { |
| 513 | if (*comp_id == *pformats) |
| 514 | break; |
| 515 | } |
| 516 | if (i == num_formats) |
| 517 | return 0; |
| 518 | } |
| 519 | if (!curve_id) |
| 520 | return 1; |
| 521 | /* Check curve is consistent with client and server preferences */ |
| 522 | for (j = 0; j <= 1; j++) { |
| 523 | if (!tls1_get_curvelist(s, j, &pcurves, &num_curves)) |
| 524 | return 0; |
Matt Caswell | b79d241 | 2015-03-20 15:10:16 +0000 | [diff] [blame] | 525 | if (j == 1 && num_curves == 0) { |
| 526 | /* |
| 527 | * If we've not received any curves then skip this check. |
| 528 | * RFC 4492 does not require the supported elliptic curves extension |
| 529 | * so if it is not sent we can just choose any curve. |
| 530 | * It is invalid to send an empty list in the elliptic curves |
| 531 | * extension, so num_curves == 0 always means no extension. |
| 532 | */ |
| 533 | break; |
| 534 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 535 | for (i = 0; i < num_curves; i++, pcurves += 2) { |
| 536 | if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1]) |
| 537 | break; |
| 538 | } |
| 539 | if (i == num_curves) |
| 540 | return 0; |
| 541 | /* For clients can only check sent curve list */ |
| 542 | if (!s->server) |
| 543 | break; |
| 544 | } |
| 545 | return 1; |
| 546 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 547 | |
Matt Caswell | 7da160b | 2016-11-25 10:22:02 +0000 | [diff] [blame] | 548 | void tls1_get_formatlist(SSL *s, const unsigned char **pformats, |
| 549 | size_t *num_formats) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 550 | { |
| 551 | /* |
| 552 | * If we have a custom point format list use it otherwise use default |
| 553 | */ |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 554 | if (s->ext.ecpointformats) { |
| 555 | *pformats = s->ext.ecpointformats; |
| 556 | *num_formats = s->ext.ecpointformats_len; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 557 | } else { |
| 558 | *pformats = ecformats_default; |
| 559 | /* For Suite B we don't support char2 fields */ |
| 560 | if (tls1_suiteb(s)) |
| 561 | *num_formats = sizeof(ecformats_default) - 1; |
| 562 | else |
| 563 | *num_formats = sizeof(ecformats_default); |
| 564 | } |
| 565 | } |
Dr. Stephen Henson | 5087afa | 2012-11-26 18:38:10 +0000 | [diff] [blame] | 566 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 567 | /* |
| 568 | * Check cert parameters compatible with extensions: currently just checks EC |
| 569 | * certificates have compatible curves and compression. |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 570 | */ |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 571 | static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 572 | { |
| 573 | unsigned char comp_id, curve_id[2]; |
| 574 | EVP_PKEY *pkey; |
| 575 | int rv; |
Dr. Stephen Henson | 8382fd3 | 2015-12-20 00:32:36 +0000 | [diff] [blame] | 576 | pkey = X509_get0_pubkey(x); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 577 | if (!pkey) |
| 578 | return 0; |
| 579 | /* If not EC nothing to do */ |
Dr. Stephen Henson | 3aeb934 | 2016-01-19 00:21:12 +0000 | [diff] [blame] | 580 | if (EVP_PKEY_id(pkey) != EVP_PKEY_EC) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 581 | return 1; |
Dr. Stephen Henson | 3aeb934 | 2016-01-19 00:21:12 +0000 | [diff] [blame] | 582 | rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 583 | if (!rv) |
| 584 | return 0; |
| 585 | /* |
| 586 | * Can't check curve_id for client certs as we don't have a supported |
| 587 | * curves extension. |
| 588 | */ |
| 589 | rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id); |
| 590 | if (!rv) |
| 591 | return 0; |
| 592 | /* |
| 593 | * Special case for suite B. We *MUST* sign using SHA256+P-256 or |
| 594 | * SHA384+P-384, adjust digest if necessary. |
| 595 | */ |
| 596 | if (set_ee_md && tls1_suiteb(s)) { |
| 597 | int check_md; |
| 598 | size_t i; |
| 599 | CERT *c = s->cert; |
| 600 | if (curve_id[0]) |
| 601 | return 0; |
| 602 | /* Check to see we have necessary signing algorithm */ |
| 603 | if (curve_id[1] == TLSEXT_curve_P_256) |
| 604 | check_md = NID_ecdsa_with_SHA256; |
| 605 | else if (curve_id[1] == TLSEXT_curve_P_384) |
| 606 | check_md = NID_ecdsa_with_SHA384; |
| 607 | else |
| 608 | return 0; /* Should never happen */ |
| 609 | for (i = 0; i < c->shared_sigalgslen; i++) |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 610 | if (check_md == c->shared_sigalgs[i]->sigandhash) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 611 | break; |
| 612 | if (i == c->shared_sigalgslen) |
| 613 | return 0; |
| 614 | if (set_ee_md == 2) { |
| 615 | if (check_md == NID_ecdsa_with_SHA256) |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 616 | s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256(); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 617 | else |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 618 | s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384(); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | return rv; |
| 622 | } |
| 623 | |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 624 | # ifndef OPENSSL_NO_EC |
Kurt Roeckx | 6977e8e | 2015-12-04 22:25:11 +0100 | [diff] [blame] | 625 | /* |
FdaSilvaYY | 8483a00 | 2016-03-10 21:34:48 +0100 | [diff] [blame] | 626 | * tls1_check_ec_tmp_key - Check EC temporary key compatibility |
Kurt Roeckx | 6977e8e | 2015-12-04 22:25:11 +0100 | [diff] [blame] | 627 | * @s: SSL connection |
| 628 | * @cid: Cipher ID we're considering using |
| 629 | * |
| 630 | * Checks that the kECDHE cipher suite we're considering using |
| 631 | * is compatible with the client extensions. |
| 632 | * |
| 633 | * Returns 0 when the cipher can't be used or 1 when it can. |
| 634 | */ |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 635 | int tls1_check_ec_tmp_key(SSL *s, unsigned long cid) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 636 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 637 | /* |
| 638 | * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other |
| 639 | * curves permitted. |
| 640 | */ |
| 641 | if (tls1_suiteb(s)) { |
Kurt Roeckx | 6977e8e | 2015-12-04 22:25:11 +0100 | [diff] [blame] | 642 | unsigned char curve_id[2]; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 643 | /* Curve to check determined by ciphersuite */ |
| 644 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
| 645 | curve_id[1] = TLSEXT_curve_P_256; |
| 646 | else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
| 647 | curve_id[1] = TLSEXT_curve_P_384; |
| 648 | else |
| 649 | return 0; |
| 650 | curve_id[0] = 0; |
| 651 | /* Check this curve is acceptable */ |
| 652 | if (!tls1_check_ec_key(s, curve_id, NULL)) |
| 653 | return 0; |
Kurt Roeckx | fe6ef24 | 2015-12-04 22:30:36 +0100 | [diff] [blame] | 654 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 655 | } |
Kurt Roeckx | fe6ef24 | 2015-12-04 22:30:36 +0100 | [diff] [blame] | 656 | /* Need a shared curve */ |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 657 | if (tls1_shared_group(s, 0)) |
Kurt Roeckx | fe6ef24 | 2015-12-04 22:30:36 +0100 | [diff] [blame] | 658 | return 1; |
Kurt Roeckx | 6977e8e | 2015-12-04 22:25:11 +0100 | [diff] [blame] | 659 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 660 | } |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 661 | # endif /* OPENSSL_NO_EC */ |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 662 | |
Dr. Stephen Henson | 14536c8 | 2013-08-17 17:40:08 +0100 | [diff] [blame] | 663 | #else |
| 664 | |
| 665 | static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 666 | { |
| 667 | return 1; |
| 668 | } |
Dr. Stephen Henson | 14536c8 | 2013-08-17 17:40:08 +0100 | [diff] [blame] | 669 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 670 | #endif /* OPENSSL_NO_EC */ |
Bodo Möller | f1fd454 | 2006-01-03 03:27:19 +0000 | [diff] [blame] | 671 | |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 672 | /* Default sigalg schemes */ |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 673 | static const uint16_t tls12_sigalgs[] = { |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 674 | #ifndef OPENSSL_NO_EC |
| 675 | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
| 676 | TLSEXT_SIGALG_ecdsa_secp384r1_sha384, |
| 677 | TLSEXT_SIGALG_ecdsa_secp521r1_sha512, |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 678 | #endif |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 679 | |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 680 | TLSEXT_SIGALG_rsa_pss_sha256, |
| 681 | TLSEXT_SIGALG_rsa_pss_sha384, |
| 682 | TLSEXT_SIGALG_rsa_pss_sha512, |
| 683 | |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 684 | TLSEXT_SIGALG_rsa_pkcs1_sha256, |
| 685 | TLSEXT_SIGALG_rsa_pkcs1_sha384, |
| 686 | TLSEXT_SIGALG_rsa_pkcs1_sha512, |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 687 | |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 688 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 42ab223 | 2017-01-10 13:45:24 +0000 | [diff] [blame] | 689 | TLSEXT_SIGALG_ecdsa_sha1, |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 690 | #endif |
Matt Caswell | 42ab223 | 2017-01-10 13:45:24 +0000 | [diff] [blame] | 691 | TLSEXT_SIGALG_rsa_pkcs1_sha1, |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 692 | #ifndef OPENSSL_NO_DSA |
Matt Caswell | 42ab223 | 2017-01-10 13:45:24 +0000 | [diff] [blame] | 693 | TLSEXT_SIGALG_dsa_sha1, |
| 694 | |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 695 | TLSEXT_SIGALG_dsa_sha256, |
| 696 | TLSEXT_SIGALG_dsa_sha384, |
| 697 | TLSEXT_SIGALG_dsa_sha512 |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 698 | #endif |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 699 | }; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 700 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 701 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 702 | static const uint16_t suiteb_sigalgs[] = { |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 703 | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
| 704 | TLSEXT_SIGALG_ecdsa_secp384r1_sha384 |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 705 | }; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 706 | #endif |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 707 | |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 708 | static const SIGALG_LOOKUP sigalg_lookup_tbl[] = { |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 709 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 710 | {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 711 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 712 | NID_ecdsa_with_SHA256, NID_X9_62_prime256v1}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 713 | {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 714 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 715 | NID_ecdsa_with_SHA384, NID_secp384r1}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 716 | {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 717 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 718 | NID_ecdsa_with_SHA512, NID_secp521r1}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 719 | {NULL, TLSEXT_SIGALG_ecdsa_sha1, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 720 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 721 | NID_ecdsa_with_SHA1, NID_undef}, |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 722 | #endif |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 723 | {"rsa_pss_sha256", TLSEXT_SIGALG_rsa_pss_sha256, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 724 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
| 725 | NID_undef, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 726 | {"rsa_pss_sha384", TLSEXT_SIGALG_rsa_pss_sha384, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 727 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
| 728 | NID_undef, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 729 | {"rsa_pss_sha512", TLSEXT_SIGALG_rsa_pss_sha512, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 730 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
| 731 | NID_undef, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 732 | {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256, |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 733 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 734 | NID_sha256WithRSAEncryption, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 735 | {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384, |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 736 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 737 | NID_sha384WithRSAEncryption, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 738 | {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512, |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 739 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 740 | NID_sha512WithRSAEncryption, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 741 | {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1, |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 742 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 743 | NID_sha1WithRSAEncryption, NID_undef}, |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 744 | #ifndef OPENSSL_NO_DSA |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 745 | {NULL, TLSEXT_SIGALG_dsa_sha256, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 746 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
| 747 | NID_dsa_with_SHA256, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 748 | {NULL, TLSEXT_SIGALG_dsa_sha384, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 749 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
| 750 | NID_undef, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 751 | {NULL, TLSEXT_SIGALG_dsa_sha512, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 752 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
| 753 | NID_undef, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 754 | {NULL, TLSEXT_SIGALG_dsa_sha1, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 755 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
| 756 | NID_dsaWithSHA1, NID_undef}, |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 757 | #endif |
| 758 | #ifndef OPENSSL_NO_GOST |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 759 | {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 760 | NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, |
| 761 | NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, |
| 762 | NID_undef, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 763 | {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 764 | NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, |
| 765 | NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, |
| 766 | NID_undef, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 767 | {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 768 | NID_id_GostR3411_94, SSL_MD_GOST94_IDX, |
| 769 | NID_id_GostR3410_2001, SSL_PKEY_GOST01, |
| 770 | NID_undef, NID_undef} |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 771 | #endif |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 772 | }; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 773 | /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */ |
| 774 | static const SIGALG_LOOKUP legacy_rsa_sigalg = { |
| 775 | "rsa_pkcs1_md5_sha1", 0, |
| 776 | NID_md5_sha1, SSL_MD_MD5_SHA1_IDX, |
| 777 | EVP_PKEY_RSA, SSL_PKEY_RSA, |
| 778 | NID_undef, NID_undef |
| 779 | }; |
| 780 | |
| 781 | /* |
| 782 | * Default signature algorithm values used if signature algorithms not present. |
| 783 | * From RFC5246. Note: order must match certificate index order. |
| 784 | */ |
| 785 | static const uint16_t tls_default_sigalg[] = { |
| 786 | TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */ |
| 787 | TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */ |
| 788 | TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */ |
| 789 | TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */ |
| 790 | TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, /* SSL_PKEY_GOST12_256 */ |
| 791 | TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512 /* SSL_PKEY_GOST12_512 */ |
| 792 | }; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 793 | |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 794 | /* Lookup TLS signature algorithm */ |
| 795 | static const SIGALG_LOOKUP *tls1_lookup_sigalg(uint16_t sigalg) |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 796 | { |
| 797 | size_t i; |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 798 | const SIGALG_LOOKUP *s; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 799 | |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 800 | for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); |
| 801 | i++, s++) { |
| 802 | if (s->sigalg == sigalg) |
| 803 | return s; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 804 | } |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 805 | return NULL; |
| 806 | } |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 807 | /* |
| 808 | * Return a signature algorithm for TLS < 1.2 where the signature type |
| 809 | * is fixed by the certificate type. |
| 810 | */ |
| 811 | static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx) |
| 812 | { |
| 813 | if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg)) |
| 814 | return NULL; |
| 815 | if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) { |
| 816 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(tls_default_sigalg[idx]); |
| 817 | |
| 818 | if (lu == NULL || ssl_md(lu->hash_idx) == NULL) { |
| 819 | return NULL; |
| 820 | } |
| 821 | return lu; |
| 822 | } |
| 823 | return &legacy_rsa_sigalg; |
| 824 | } |
| 825 | /* Set peer sigalg based key type */ |
| 826 | int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey) |
| 827 | { |
| 828 | int idx = ssl_cert_type(NULL, pkey); |
| 829 | |
| 830 | const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, idx); |
| 831 | if (lu == NULL) |
| 832 | return 0; |
| 833 | s->s3->tmp.peer_sigalg = lu; |
| 834 | return 1; |
| 835 | } |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 836 | |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 837 | static int tls_sigalg_get_sig(uint16_t sigalg) |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 838 | { |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 839 | const SIGALG_LOOKUP *r = tls1_lookup_sigalg(sigalg); |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 840 | |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 841 | return r != NULL ? r->sig : 0; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 842 | } |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 843 | |
| 844 | size_t tls12_get_psigalgs(SSL *s, int sent, const uint16_t **psigs) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 845 | { |
| 846 | /* |
| 847 | * If Suite B mode use Suite B sigalgs only, ignore any other |
| 848 | * preferences. |
| 849 | */ |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 850 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 851 | switch (tls1_suiteb(s)) { |
| 852 | case SSL_CERT_FLAG_SUITEB_128_LOS: |
| 853 | *psigs = suiteb_sigalgs; |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 854 | return OSSL_NELEM(suiteb_sigalgs); |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 855 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 856 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: |
| 857 | *psigs = suiteb_sigalgs; |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 858 | return 1; |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 859 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 860 | case SSL_CERT_FLAG_SUITEB_192_LOS: |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 861 | *psigs = suiteb_sigalgs + 1; |
| 862 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 863 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 864 | #endif |
Dr. Stephen Henson | a9669dd | 2017-01-25 19:12:48 +0000 | [diff] [blame] | 865 | /* |
| 866 | * We use client_sigalgs (if not NULL) if we're a server |
| 867 | * and sending a certificate request or if we're a client and |
| 868 | * determining which shared algorithm to use. |
| 869 | */ |
| 870 | if ((s->server == sent) && s->cert->client_sigalgs != NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 871 | *psigs = s->cert->client_sigalgs; |
| 872 | return s->cert->client_sigalgslen; |
| 873 | } else if (s->cert->conf_sigalgs) { |
| 874 | *psigs = s->cert->conf_sigalgs; |
| 875 | return s->cert->conf_sigalgslen; |
| 876 | } else { |
| 877 | *psigs = tls12_sigalgs; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 878 | return OSSL_NELEM(tls12_sigalgs); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * Check signature algorithm is consistent with sent supported signature |
Dr. Stephen Henson | b2eb699 | 2017-01-25 16:22:13 +0000 | [diff] [blame] | 884 | * algorithms and if so set relevant digest and signature scheme in |
| 885 | * s. |
Dr. Stephen Henson | ec4a50b | 2012-07-24 18:11:27 +0000 | [diff] [blame] | 886 | */ |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 887 | int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 888 | { |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 889 | const uint16_t *sent_sigs; |
Dr. Stephen Henson | 5554fac | 2017-01-25 16:46:02 +0000 | [diff] [blame] | 890 | const EVP_MD *md = NULL; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 891 | char sigalgstr[2]; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 892 | size_t sent_sigslen, i; |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 893 | int pkeyid = EVP_PKEY_id(pkey); |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 894 | const SIGALG_LOOKUP *lu; |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 895 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 896 | /* Should never happen */ |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 897 | if (pkeyid == -1) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 898 | return -1; |
Dr. Stephen Henson | 5a8916d | 2017-02-17 16:08:19 +0000 | [diff] [blame^] | 899 | if (SSL_IS_TLS13(s)) { |
| 900 | /* Disallow DSA for TLS 1.3 */ |
| 901 | if (pkeyid == EVP_PKEY_DSA) { |
| 902 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
| 903 | return 0; |
| 904 | } |
| 905 | /* Only allow PSS for TLS 1.3 */ |
| 906 | if (pkeyid == EVP_PKEY_RSA) |
| 907 | pkeyid = EVP_PKEY_RSA_PSS; |
| 908 | } |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 909 | lu = tls1_lookup_sigalg(sig); |
| 910 | /* |
| 911 | * Check sigalgs is known and key type is consistent with signature: |
| 912 | * RSA keys can be used for RSA-PSS |
| 913 | */ |
| 914 | if (lu == NULL || (pkeyid != lu->sig |
| 915 | && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 916 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
| 917 | return 0; |
| 918 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 919 | #ifndef OPENSSL_NO_EC |
Matt Caswell | fe3066e | 2017-01-03 10:01:39 +0000 | [diff] [blame] | 920 | if (pkeyid == EVP_PKEY_EC) { |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 921 | EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey); |
Dr. Stephen Henson | f1adb00 | 2017-01-30 22:02:11 +0000 | [diff] [blame] | 922 | int curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 923 | |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 924 | if (SSL_IS_TLS13(s)) { |
| 925 | /* For TLS 1.3 check curve matches signature algorithm */ |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 926 | |
Dr. Stephen Henson | a34a9df | 2017-02-14 14:27:15 +0000 | [diff] [blame] | 927 | if (lu->curve != NID_undef && curve != lu->curve) { |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 928 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 929 | return 0; |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 930 | } |
| 931 | } else { |
| 932 | unsigned char curve_id[2], comp_id; |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 933 | |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 934 | /* Check compression and curve matches extensions */ |
| 935 | if (!tls1_set_ec_id(curve_id, &comp_id, ec)) |
| 936 | return 0; |
| 937 | if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) { |
| 938 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); |
| 939 | return 0; |
| 940 | } |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 941 | if (tls1_suiteb(s)) { |
Dr. Stephen Henson | f1adb00 | 2017-01-30 22:02:11 +0000 | [diff] [blame] | 942 | /* Check sigalg matches a permissible Suite B value */ |
| 943 | if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256 |
| 944 | && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) { |
| 945 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, |
| 946 | SSL_R_WRONG_SIGNATURE_TYPE); |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 947 | return 0; |
Dr. Stephen Henson | f1adb00 | 2017-01-30 22:02:11 +0000 | [diff] [blame] | 948 | } |
| 949 | /* |
| 950 | * Suite B also requires P-256+SHA256 and P-384+SHA384: |
| 951 | * this matches the TLS 1.3 requirements so we can just |
| 952 | * check the curve is the expected TLS 1.3 value. |
| 953 | * If this fails an inappropriate digest is being used. |
| 954 | */ |
| 955 | if (curve != lu->curve) { |
| 956 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, |
| 957 | SSL_R_ILLEGAL_SUITEB_DIGEST); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 958 | return 0; |
| 959 | } |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 960 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 961 | } |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 962 | } else if (tls1_suiteb(s)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 963 | return 0; |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 964 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 965 | #endif |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 966 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 967 | /* Check signature matches a type we sent */ |
Dr. Stephen Henson | a9669dd | 2017-01-25 19:12:48 +0000 | [diff] [blame] | 968 | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 969 | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 970 | if (sig == *sent_sigs) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 971 | break; |
| 972 | } |
| 973 | /* Allow fallback to SHA1 if not strict mode */ |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 974 | if (i == sent_sigslen && (lu->hash != NID_sha1 |
| 975 | || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 976 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
| 977 | return 0; |
| 978 | } |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 979 | md = ssl_md(lu->hash_idx); |
Dr. Stephen Henson | 5554fac | 2017-01-25 16:46:02 +0000 | [diff] [blame] | 980 | if (md == NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 981 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST); |
| 982 | return 0; |
| 983 | } |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 984 | /* |
| 985 | * Make sure security callback allows algorithm. For historical reasons we |
| 986 | * have to pass the sigalg as a two byte char array. |
| 987 | */ |
| 988 | sigalgstr[0] = (sig >> 8) & 0xff; |
| 989 | sigalgstr[1] = sig & 0xff; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 990 | if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK, |
Dr. Stephen Henson | 5554fac | 2017-01-25 16:46:02 +0000 | [diff] [blame] | 991 | EVP_MD_size(md) * 4, EVP_MD_type(md), |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 992 | (void *)sigalgstr)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 993 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
| 994 | return 0; |
| 995 | } |
Dr. Stephen Henson | 6cbebb5 | 2017-01-30 18:10:17 +0000 | [diff] [blame] | 996 | /* Store the sigalg the peer uses */ |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 997 | s->s3->tmp.peer_sigalg = lu; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 998 | return 1; |
| 999 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1000 | |
Dr. Stephen Henson | 42ef7ae | 2017-01-25 23:28:57 +0000 | [diff] [blame] | 1001 | int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid) |
| 1002 | { |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 1003 | if (s->s3->tmp.peer_sigalg == NULL) |
Dr. Stephen Henson | 42ef7ae | 2017-01-25 23:28:57 +0000 | [diff] [blame] | 1004 | return 0; |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 1005 | *pnid = s->s3->tmp.peer_sigalg->sig; |
Dr. Stephen Henson | 42ef7ae | 2017-01-25 23:28:57 +0000 | [diff] [blame] | 1006 | return 1; |
| 1007 | } |
| 1008 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1009 | /* |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 1010 | * Set a mask of disabled algorithms: an algorithm is disabled if it isn't |
| 1011 | * supported, doesn't appear in supported signature algorithms, isn't supported |
| 1012 | * by the enabled protocol versions or by the security level. |
| 1013 | * |
| 1014 | * This function should only be used for checking which ciphers are supported |
| 1015 | * by the client. |
| 1016 | * |
| 1017 | * Call ssl_cipher_disabled() to check that it's enabled or not. |
Dr. Stephen Henson | b7bfe69 | 2012-07-18 14:09:46 +0000 | [diff] [blame] | 1018 | */ |
| 1019 | void ssl_set_client_disabled(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1020 | { |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 1021 | s->s3->tmp.mask_a = 0; |
| 1022 | s->s3->tmp.mask_k = 0; |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 1023 | ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK); |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 1024 | ssl_get_client_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver); |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1025 | #ifndef OPENSSL_NO_PSK |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1026 | /* with PSK there must be client callback set */ |
| 1027 | if (!s->psk_client_callback) { |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 1028 | s->s3->tmp.mask_a |= SSL_aPSK; |
Dr. Stephen Henson | fe5eef3 | 2015-06-28 17:01:07 +0100 | [diff] [blame] | 1029 | s->s3->tmp.mask_k |= SSL_PSK; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1030 | } |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1031 | #endif /* OPENSSL_NO_PSK */ |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1032 | #ifndef OPENSSL_NO_SRP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1033 | if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) { |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 1034 | s->s3->tmp.mask_a |= SSL_aSRP; |
| 1035 | s->s3->tmp.mask_k |= SSL_kSRP; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1036 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1037 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1038 | } |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 1039 | |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 1040 | /* |
| 1041 | * ssl_cipher_disabled - check that a cipher is disabled or not |
| 1042 | * @s: SSL connection that you want to use the cipher on |
| 1043 | * @c: cipher to check |
| 1044 | * @op: Security check that you want to do |
| 1045 | * |
| 1046 | * Returns 1 when it's disabled, 0 when enabled. |
| 1047 | */ |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1048 | int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1049 | { |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 1050 | if (c->algorithm_mkey & s->s3->tmp.mask_k |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 1051 | || c->algorithm_auth & s->s3->tmp.mask_a) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1052 | return 1; |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 1053 | if (s->s3->tmp.max_ver == 0) |
| 1054 | return 1; |
| 1055 | if (!SSL_IS_DTLS(s) && ((c->min_tls > s->s3->tmp.max_ver) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1056 | || (c->max_tls < s->s3->tmp.min_ver))) |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 1057 | return 1; |
| 1058 | if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1059 | || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver))) |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 1060 | return 1; |
| 1061 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1062 | return !ssl_security(s, op, c->strength_bits, 0, (void *)c); |
| 1063 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1064 | |
Matt Caswell | 7da160b | 2016-11-25 10:22:02 +0000 | [diff] [blame] | 1065 | int tls_use_ticket(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1066 | { |
Matt Caswell | 0819129 | 2017-01-19 11:23:06 +0000 | [diff] [blame] | 1067 | if ((s->options & SSL_OP_NO_TICKET)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1068 | return 0; |
| 1069 | return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL); |
| 1070 | } |
Dr. Stephen Henson | 8b8e5be | 2014-01-14 14:55:21 +0000 | [diff] [blame] | 1071 | |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 1072 | /* Initialise digests to default values */ |
Dr. Stephen Henson | a0f6382 | 2015-11-24 00:08:35 +0000 | [diff] [blame] | 1073 | void ssl_set_default_md(SSL *s) |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 1074 | { |
| 1075 | const EVP_MD **pmd = s->s3->tmp.md; |
| 1076 | #ifndef OPENSSL_NO_DSA |
Dr. Stephen Henson | 152fbc2 | 2015-11-29 16:27:08 +0000 | [diff] [blame] | 1077 | pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX); |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 1078 | #endif |
| 1079 | #ifndef OPENSSL_NO_RSA |
Dr. Stephen Henson | d18d31a | 2015-08-29 22:11:05 +0100 | [diff] [blame] | 1080 | if (SSL_USE_SIGALGS(s)) |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 1081 | pmd[SSL_PKEY_RSA] = ssl_md(SSL_MD_SHA1_IDX); |
Dr. Stephen Henson | d18d31a | 2015-08-29 22:11:05 +0100 | [diff] [blame] | 1082 | else |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 1083 | pmd[SSL_PKEY_RSA] = ssl_md(SSL_MD_MD5_SHA1_IDX); |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 1084 | #endif |
| 1085 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | 152fbc2 | 2015-11-29 16:27:08 +0000 | [diff] [blame] | 1086 | pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX); |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 1087 | #endif |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1088 | #ifndef OPENSSL_NO_GOST |
Dr. Stephen Henson | 152fbc2 | 2015-11-29 16:27:08 +0000 | [diff] [blame] | 1089 | pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX); |
| 1090 | pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX); |
| 1091 | pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX); |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1092 | #endif |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 1093 | } |
Bodo Möller | 36ca4ba | 2006-03-11 23:46:37 +0000 | [diff] [blame] | 1094 | |
Dr. Stephen Henson | e469af8 | 2014-11-17 16:52:59 +0000 | [diff] [blame] | 1095 | int tls1_set_server_sigalgs(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1096 | { |
| 1097 | int al; |
| 1098 | size_t i; |
FdaSilvaYY | 8483a00 | 2016-03-10 21:34:48 +0100 | [diff] [blame] | 1099 | |
| 1100 | /* Clear any shared signature algorithms */ |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 1101 | OPENSSL_free(s->cert->shared_sigalgs); |
| 1102 | s->cert->shared_sigalgs = NULL; |
| 1103 | s->cert->shared_sigalgslen = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1104 | /* Clear certificate digests and validity flags */ |
| 1105 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 1106 | s->s3->tmp.md[i] = NULL; |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 1107 | s->s3->tmp.valid_flags[i] = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1108 | } |
Dr. Stephen Henson | e469af8 | 2014-11-17 16:52:59 +0000 | [diff] [blame] | 1109 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1110 | /* If sigalgs received process it. */ |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 1111 | if (s->s3->tmp.peer_sigalgs) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1112 | if (!tls1_process_sigalgs(s)) { |
| 1113 | SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE); |
| 1114 | al = SSL_AD_INTERNAL_ERROR; |
| 1115 | goto err; |
| 1116 | } |
| 1117 | /* Fatal error is no shared signature algorithms */ |
| 1118 | if (!s->cert->shared_sigalgs) { |
| 1119 | SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, |
FdaSilvaYY | f430ba3 | 2016-06-19 19:39:01 +0200 | [diff] [blame] | 1120 | SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1121 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1122 | goto err; |
| 1123 | } |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 1124 | } else { |
| 1125 | ssl_set_default_md(s); |
| 1126 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1127 | return 1; |
| 1128 | err: |
| 1129 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 1130 | return 0; |
| 1131 | } |
Dr. Stephen Henson | e469af8 | 2014-11-17 16:52:59 +0000 | [diff] [blame] | 1132 | |
Tim Hudson | 1d97c84 | 2014-12-28 12:48:40 +1000 | [diff] [blame] | 1133 | /*- |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1134 | * Gets the ticket information supplied by the client if any. |
Dr. Stephen Henson | e7f0d92 | 2015-12-04 19:48:15 +0000 | [diff] [blame] | 1135 | * |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1136 | * hello: The parsed ClientHello data |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 1137 | * ret: (output) on return, if a ticket was decrypted, then this is set to |
| 1138 | * point to the resulting session. |
| 1139 | * |
| 1140 | * If s->tls_session_secret_cb is set then we are expecting a pre-shared key |
| 1141 | * ciphersuite, in which case we have no use for session tickets and one will |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1142 | * never be decrypted, nor will s->ext.ticket_expected be set to 1. |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 1143 | * |
| 1144 | * Returns: |
| 1145 | * -1: fatal error, either from parsing or decrypting the ticket. |
| 1146 | * 0: no ticket was found (or was ignored, based on settings). |
| 1147 | * 1: a zero length extension was found, indicating that the client supports |
| 1148 | * session tickets but doesn't currently have one to offer. |
| 1149 | * 2: either s->tls_session_secret_cb was set, or a ticket was offered but |
| 1150 | * couldn't be decrypted because of a non-fatal error. |
| 1151 | * 3: a ticket was successfully decrypted and *ret was set. |
| 1152 | * |
| 1153 | * Side effects: |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1154 | * Sets s->ext.ticket_expected to 1 if the server will have to issue |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 1155 | * a new session ticket to the client because the client indicated support |
| 1156 | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have |
| 1157 | * a session ticket or we couldn't use the one it gave us, or if |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1158 | * s->ctx->ext.ticket_key_cb asked to renew the client's ticket. |
| 1159 | * Otherwise, s->ext.ticket_expected is set to 0. |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 1160 | */ |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1161 | TICKET_RETURN tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello, |
| 1162 | SSL_SESSION **ret) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1163 | { |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1164 | int retv; |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1165 | size_t size; |
| 1166 | RAW_EXTENSION *ticketext; |
Dr. Stephen Henson | e7f0d92 | 2015-12-04 19:48:15 +0000 | [diff] [blame] | 1167 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1168 | *ret = NULL; |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1169 | s->ext.ticket_expected = 0; |
Dr. Stephen Henson | e8da6a1 | 2008-09-03 22:17:11 +0000 | [diff] [blame] | 1170 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1171 | /* |
Matt Caswell | 9362c93 | 2016-11-08 16:10:21 +0000 | [diff] [blame] | 1172 | * If tickets disabled or not supported by the protocol version |
| 1173 | * (e.g. TLSv1.3) behave as if no ticket present to permit stateful |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1174 | * resumption. |
| 1175 | */ |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1176 | if (s->version <= SSL3_VERSION || !tls_use_ticket(s)) |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1177 | return TICKET_NONE; |
Matt Caswell | 9ceb242 | 2015-04-16 10:06:25 +0100 | [diff] [blame] | 1178 | |
Matt Caswell | 70af3d8 | 2016-11-28 09:31:59 +0000 | [diff] [blame] | 1179 | ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket]; |
| 1180 | if (!ticketext->present) |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1181 | return TICKET_NONE; |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1182 | |
| 1183 | size = PACKET_remaining(&ticketext->data); |
| 1184 | if (size == 0) { |
| 1185 | /* |
| 1186 | * The client will accept a ticket but doesn't currently have |
| 1187 | * one. |
| 1188 | */ |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1189 | s->ext.ticket_expected = 1; |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1190 | return TICKET_EMPTY; |
Matt Caswell | 9ceb242 | 2015-04-16 10:06:25 +0100 | [diff] [blame] | 1191 | } |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1192 | if (s->ext.session_secret_cb) { |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1193 | /* |
| 1194 | * Indicate that the ticket couldn't be decrypted rather than |
| 1195 | * generating the session from ticket now, trigger |
| 1196 | * abbreviated handshake based on external mechanism to |
| 1197 | * calculate the master secret later. |
| 1198 | */ |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1199 | return TICKET_NO_DECRYPT; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1200 | } |
Matt Caswell | 70af3d8 | 2016-11-28 09:31:59 +0000 | [diff] [blame] | 1201 | |
| 1202 | retv = tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size, |
| 1203 | hello->session_id, hello->session_id_len, ret); |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1204 | switch (retv) { |
Matt Caswell | 61c3264 | 2017-01-27 12:11:23 +0000 | [diff] [blame] | 1205 | case TICKET_NO_DECRYPT: |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1206 | s->ext.ticket_expected = 1; |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1207 | return TICKET_NO_DECRYPT; |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1208 | |
Matt Caswell | 61c3264 | 2017-01-27 12:11:23 +0000 | [diff] [blame] | 1209 | case TICKET_SUCCESS: |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1210 | return TICKET_SUCCESS; |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1211 | |
Matt Caswell | 61c3264 | 2017-01-27 12:11:23 +0000 | [diff] [blame] | 1212 | case TICKET_SUCCESS_RENEW: |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1213 | s->ext.ticket_expected = 1; |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1214 | return TICKET_SUCCESS; |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1215 | |
Matt Caswell | 61c3264 | 2017-01-27 12:11:23 +0000 | [diff] [blame] | 1216 | default: |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1217 | return TICKET_FATAL_ERR_OTHER; |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | |
Tim Hudson | 1d97c84 | 2014-12-28 12:48:40 +1000 | [diff] [blame] | 1221 | /*- |
| 1222 | * tls_decrypt_ticket attempts to decrypt a session ticket. |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 1223 | * |
| 1224 | * etick: points to the body of the session ticket extension. |
FdaSilvaYY | 8483a00 | 2016-03-10 21:34:48 +0100 | [diff] [blame] | 1225 | * eticklen: the length of the session tickets extension. |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 1226 | * sess_id: points at the session ID. |
| 1227 | * sesslen: the length of the session ID. |
| 1228 | * psess: (output) on return, if a ticket was decrypted, then this is set to |
| 1229 | * point to the resulting session. |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 1230 | */ |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1231 | TICKET_RETURN tls_decrypt_ticket(SSL *s, const unsigned char *etick, |
| 1232 | size_t eticklen, const unsigned char *sess_id, |
| 1233 | size_t sesslen, SSL_SESSION **psess) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1234 | { |
| 1235 | SSL_SESSION *sess; |
| 1236 | unsigned char *sdec; |
| 1237 | const unsigned char *p; |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1238 | int slen, renew_ticket = 0, declen; |
| 1239 | TICKET_RETURN ret = TICKET_FATAL_ERR_OTHER; |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1240 | size_t mlen; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1241 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 1242 | HMAC_CTX *hctx = NULL; |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1243 | EVP_CIPHER_CTX *ctx; |
Todd Short | 222da97 | 2017-01-31 15:32:50 -0500 | [diff] [blame] | 1244 | SSL_CTX *tctx = s->session_ctx; |
Dr. Stephen Henson | e97763c | 2016-08-22 17:20:01 +0100 | [diff] [blame] | 1245 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1246 | /* Initialize session ticket encryption and HMAC contexts */ |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 1247 | hctx = HMAC_CTX_new(); |
| 1248 | if (hctx == NULL) |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1249 | return TICKET_FATAL_ERR_MALLOC; |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1250 | ctx = EVP_CIPHER_CTX_new(); |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 1251 | if (ctx == NULL) { |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1252 | ret = TICKET_FATAL_ERR_MALLOC; |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 1253 | goto err; |
| 1254 | } |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1255 | if (tctx->ext.ticket_key_cb) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1256 | unsigned char *nctick = (unsigned char *)etick; |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1257 | int rv = tctx->ext.ticket_key_cb(s, nctick, nctick + 16, |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1258 | ctx, hctx, 0); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1259 | if (rv < 0) |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 1260 | goto err; |
| 1261 | if (rv == 0) { |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1262 | ret = TICKET_NO_DECRYPT; |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 1263 | goto err; |
| 1264 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1265 | if (rv == 2) |
| 1266 | renew_ticket = 1; |
| 1267 | } else { |
| 1268 | /* Check key name matches */ |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1269 | if (memcmp(etick, tctx->ext.tick_key_name, |
| 1270 | sizeof(tctx->ext.tick_key_name)) != 0) { |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1271 | ret = TICKET_NO_DECRYPT; |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 1272 | goto err; |
| 1273 | } |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1274 | if (HMAC_Init_ex(hctx, tctx->ext.tick_hmac_key, |
| 1275 | sizeof(tctx->ext.tick_hmac_key), |
Matt Caswell | 5f3d93e | 2015-11-06 16:31:21 +0000 | [diff] [blame] | 1276 | EVP_sha256(), NULL) <= 0 |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1277 | || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1278 | tctx->ext.tick_aes_key, |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1279 | etick |
| 1280 | + sizeof(tctx->ext.tick_key_name)) <= 0) { |
Matt Caswell | 5f3d93e | 2015-11-06 16:31:21 +0000 | [diff] [blame] | 1281 | goto err; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1282 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1283 | } |
| 1284 | /* |
| 1285 | * Attempt to process session ticket, first conduct sanity and integrity |
| 1286 | * checks on ticket. |
| 1287 | */ |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 1288 | mlen = HMAC_size(hctx); |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1289 | if (mlen == 0) { |
Matt Caswell | 5f3d93e | 2015-11-06 16:31:21 +0000 | [diff] [blame] | 1290 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1291 | } |
Dr. Stephen Henson | e97763c | 2016-08-22 17:20:01 +0100 | [diff] [blame] | 1292 | /* Sanity check ticket length: must exceed keyname + IV + HMAC */ |
| 1293 | if (eticklen <= |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1294 | TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) { |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1295 | ret = TICKET_NO_DECRYPT; |
Dr. Stephen Henson | e97763c | 2016-08-22 17:20:01 +0100 | [diff] [blame] | 1296 | goto err; |
| 1297 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1298 | eticklen -= mlen; |
| 1299 | /* Check HMAC of encrypted ticket */ |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 1300 | if (HMAC_Update(hctx, etick, eticklen) <= 0 |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1301 | || HMAC_Final(hctx, tick_hmac, NULL) <= 0) { |
Matt Caswell | 5f3d93e | 2015-11-06 16:31:21 +0000 | [diff] [blame] | 1302 | goto err; |
| 1303 | } |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 1304 | HMAC_CTX_free(hctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1305 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1306 | EVP_CIPHER_CTX_free(ctx); |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1307 | return TICKET_NO_DECRYPT; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1308 | } |
| 1309 | /* Attempt to decrypt session data */ |
| 1310 | /* Move p after IV to start of encrypted ticket, update length */ |
Bernd Edlinger | 57b0d65 | 2017-02-13 18:36:13 +0100 | [diff] [blame] | 1311 | p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx); |
| 1312 | eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1313 | sdec = OPENSSL_malloc(eticklen); |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1314 | if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, |
| 1315 | (int)eticklen) <= 0) { |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1316 | EVP_CIPHER_CTX_free(ctx); |
Matt Caswell | d1247df | 2016-03-15 11:38:56 +0000 | [diff] [blame] | 1317 | OPENSSL_free(sdec); |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1318 | return TICKET_FATAL_ERR_OTHER; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1319 | } |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1320 | if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) { |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1321 | EVP_CIPHER_CTX_free(ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1322 | OPENSSL_free(sdec); |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1323 | return TICKET_NO_DECRYPT; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1324 | } |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1325 | slen += declen; |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1326 | EVP_CIPHER_CTX_free(ctx); |
| 1327 | ctx = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1328 | p = sdec; |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 1329 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1330 | sess = d2i_SSL_SESSION(NULL, &p, slen); |
| 1331 | OPENSSL_free(sdec); |
| 1332 | if (sess) { |
| 1333 | /* |
| 1334 | * The session ID, if non-empty, is used by some clients to detect |
| 1335 | * that the ticket has been accepted. So we copy it to the session |
| 1336 | * structure. If it is empty set length to zero as required by |
| 1337 | * standard. |
| 1338 | */ |
| 1339 | if (sesslen) |
| 1340 | memcpy(sess->session_id, sess_id, sesslen); |
| 1341 | sess->session_id_length = sesslen; |
| 1342 | *psess = sess; |
| 1343 | if (renew_ticket) |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1344 | return TICKET_SUCCESS_RENEW; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1345 | else |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1346 | return TICKET_SUCCESS; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1347 | } |
| 1348 | ERR_clear_error(); |
| 1349 | /* |
| 1350 | * For session parse failure, indicate that we need to send a new ticket. |
| 1351 | */ |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1352 | return TICKET_NO_DECRYPT; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1353 | err: |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1354 | EVP_CIPHER_CTX_free(ctx); |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 1355 | HMAC_CTX_free(hctx); |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 1356 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1357 | } |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 1358 | |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1359 | static int tls12_get_pkey_idx(int sig_nid) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1360 | { |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1361 | switch (sig_nid) { |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1362 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1363 | case EVP_PKEY_RSA: |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 1364 | return SSL_PKEY_RSA; |
Dr. Stephen Henson | b2eb699 | 2017-01-25 16:22:13 +0000 | [diff] [blame] | 1365 | /* |
| 1366 | * For now return RSA key for PSS. When we support PSS only keys |
| 1367 | * this will need to be updated. |
| 1368 | */ |
| 1369 | case EVP_PKEY_RSA_PSS: |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 1370 | return SSL_PKEY_RSA; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1371 | #endif |
| 1372 | #ifndef OPENSSL_NO_DSA |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1373 | case EVP_PKEY_DSA: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1374 | return SSL_PKEY_DSA_SIGN; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1375 | #endif |
| 1376 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1377 | case EVP_PKEY_EC: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1378 | return SSL_PKEY_ECC; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1379 | #endif |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1380 | #ifndef OPENSSL_NO_GOST |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1381 | case NID_id_GostR3410_2001: |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1382 | return SSL_PKEY_GOST01; |
| 1383 | |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1384 | case NID_id_GostR3410_2012_256: |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1385 | return SSL_PKEY_GOST12_256; |
| 1386 | |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1387 | case NID_id_GostR3410_2012_512: |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1388 | return SSL_PKEY_GOST12_512; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1389 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1390 | } |
| 1391 | return -1; |
| 1392 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 1393 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1394 | /* Check to see if a signature algorithm is allowed */ |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1395 | static int tls12_sigalg_allowed(SSL *s, int op, uint16_t ptmp) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1396 | { |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1397 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(ptmp); |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1398 | unsigned char sigalgstr[2]; |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1399 | int secbits; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1400 | |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1401 | /* See if sigalgs is recognised and if hash is enabled */ |
| 1402 | if (lu == NULL || ssl_md(lu->hash_idx) == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1403 | return 0; |
| 1404 | /* See if public key algorithm allowed */ |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1405 | if (tls12_get_pkey_idx(lu->sig) == -1) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1406 | return 0; |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1407 | /* Security bits: half digest bits */ |
| 1408 | secbits = EVP_MD_size(ssl_md(lu->hash_idx)) * 4; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1409 | /* Finally see if security callback allows it */ |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1410 | sigalgstr[0] = (ptmp >> 8) & 0xff; |
| 1411 | sigalgstr[1] = ptmp & 0xff; |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1412 | return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1413 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1414 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1415 | /* |
| 1416 | * Get a mask of disabled public key algorithms based on supported signature |
| 1417 | * algorithms. For example if no signature algorithm supports RSA then RSA is |
| 1418 | * disabled. |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1419 | */ |
| 1420 | |
Dr. Stephen Henson | 90d9e49 | 2015-11-05 16:14:17 +0000 | [diff] [blame] | 1421 | void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1422 | { |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1423 | const uint16_t *sigalgs; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1424 | size_t i, sigalgslen; |
| 1425 | int have_rsa = 0, have_dsa = 0, have_ecdsa = 0; |
| 1426 | /* |
| 1427 | * Now go through all signature algorithms seeing if we support any for |
| 1428 | * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep |
| 1429 | * down calls to security callback only check if we have to. |
| 1430 | */ |
Dr. Stephen Henson | a9669dd | 2017-01-25 19:12:48 +0000 | [diff] [blame] | 1431 | sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs); |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1432 | for (i = 0; i < sigalgslen; i ++, sigalgs++) { |
| 1433 | switch (tls_sigalg_get_sig(*sigalgs)) { |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1434 | #ifndef OPENSSL_NO_RSA |
Dr. Stephen Henson | b2eb699 | 2017-01-25 16:22:13 +0000 | [diff] [blame] | 1435 | /* Any RSA-PSS signature algorithms also mean we allow RSA */ |
| 1436 | case EVP_PKEY_RSA_PSS: |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1437 | case EVP_PKEY_RSA: |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1438 | if (!have_rsa && tls12_sigalg_allowed(s, op, *sigalgs)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1439 | have_rsa = 1; |
| 1440 | break; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1441 | #endif |
| 1442 | #ifndef OPENSSL_NO_DSA |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1443 | case EVP_PKEY_DSA: |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1444 | if (!have_dsa && tls12_sigalg_allowed(s, op, *sigalgs)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1445 | have_dsa = 1; |
| 1446 | break; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1447 | #endif |
| 1448 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1449 | case EVP_PKEY_EC: |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1450 | if (!have_ecdsa && tls12_sigalg_allowed(s, op, *sigalgs)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1451 | have_ecdsa = 1; |
| 1452 | break; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1453 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1454 | } |
| 1455 | } |
| 1456 | if (!have_rsa) |
| 1457 | *pmask_a |= SSL_aRSA; |
| 1458 | if (!have_dsa) |
| 1459 | *pmask_a |= SSL_aDSS; |
| 1460 | if (!have_ecdsa) |
| 1461 | *pmask_a |= SSL_aECDSA; |
| 1462 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1463 | |
Matt Caswell | ae2f7b3 | 2016-09-05 17:34:04 +0100 | [diff] [blame] | 1464 | int tls12_copy_sigalgs(SSL *s, WPACKET *pkt, |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1465 | const uint16_t *psig, size_t psiglen) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1466 | { |
| 1467 | size_t i; |
Matt Caswell | c0f9e23 | 2016-09-13 09:40:38 +0100 | [diff] [blame] | 1468 | |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1469 | for (i = 0; i < psiglen; i++, psig++) { |
| 1470 | if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, *psig)) { |
| 1471 | if (!WPACKET_put_bytes_u16(pkt, *psig)) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1472 | return 0; |
| 1473 | } |
| 1474 | } |
| 1475 | return 1; |
| 1476 | } |
| 1477 | |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 1478 | /* Given preference and allowed sigalgs set shared sigalgs */ |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1479 | static size_t tls12_shared_sigalgs(SSL *s, const SIGALG_LOOKUP **shsig, |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1480 | const uint16_t *pref, size_t preflen, |
| 1481 | const uint16_t *allow, size_t allowlen) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1482 | { |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1483 | const uint16_t *ptmp, *atmp; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1484 | size_t i, j, nmatch = 0; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1485 | for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1486 | /* Skip disabled hashes or signature algorithms */ |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1487 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, *ptmp)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1488 | continue; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1489 | for (j = 0, atmp = allow; j < allowlen; j++, atmp++) { |
| 1490 | if (*ptmp == *atmp) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1491 | nmatch++; |
| 1492 | if (shsig) { |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1493 | *shsig = tls1_lookup_sigalg(*ptmp); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1494 | shsig++; |
| 1495 | } |
| 1496 | break; |
| 1497 | } |
| 1498 | } |
| 1499 | } |
| 1500 | return nmatch; |
| 1501 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 1502 | |
| 1503 | /* Set shared signature algorithms for SSL structures */ |
| 1504 | static int tls1_set_shared_sigalgs(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1505 | { |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1506 | const uint16_t *pref, *allow, *conf; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1507 | size_t preflen, allowlen, conflen; |
| 1508 | size_t nmatch; |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1509 | const SIGALG_LOOKUP **salgs = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1510 | CERT *c = s->cert; |
| 1511 | unsigned int is_suiteb = tls1_suiteb(s); |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 1512 | |
| 1513 | OPENSSL_free(c->shared_sigalgs); |
| 1514 | c->shared_sigalgs = NULL; |
| 1515 | c->shared_sigalgslen = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1516 | /* If client use client signature algorithms if not NULL */ |
| 1517 | if (!s->server && c->client_sigalgs && !is_suiteb) { |
| 1518 | conf = c->client_sigalgs; |
| 1519 | conflen = c->client_sigalgslen; |
| 1520 | } else if (c->conf_sigalgs && !is_suiteb) { |
| 1521 | conf = c->conf_sigalgs; |
| 1522 | conflen = c->conf_sigalgslen; |
| 1523 | } else |
Dr. Stephen Henson | a9669dd | 2017-01-25 19:12:48 +0000 | [diff] [blame] | 1524 | conflen = tls12_get_psigalgs(s, 0, &conf); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1525 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) { |
| 1526 | pref = conf; |
| 1527 | preflen = conflen; |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 1528 | allow = s->s3->tmp.peer_sigalgs; |
| 1529 | allowlen = s->s3->tmp.peer_sigalgslen; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1530 | } else { |
| 1531 | allow = conf; |
| 1532 | allowlen = conflen; |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 1533 | pref = s->s3->tmp.peer_sigalgs; |
| 1534 | preflen = s->s3->tmp.peer_sigalgslen; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1535 | } |
| 1536 | nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen); |
Dr. Stephen Henson | 34e3edb | 2015-03-03 13:20:57 +0000 | [diff] [blame] | 1537 | if (nmatch) { |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1538 | salgs = OPENSSL_malloc(nmatch * sizeof(*salgs)); |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 1539 | if (salgs == NULL) |
Dr. Stephen Henson | 34e3edb | 2015-03-03 13:20:57 +0000 | [diff] [blame] | 1540 | return 0; |
| 1541 | nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen); |
| 1542 | } else { |
| 1543 | salgs = NULL; |
| 1544 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1545 | c->shared_sigalgs = salgs; |
| 1546 | c->shared_sigalgslen = nmatch; |
| 1547 | return 1; |
| 1548 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 1549 | |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 1550 | /* Set preferred digest for each key type */ |
| 1551 | |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1552 | int tls1_save_sigalgs(SSL *s, PACKET *pkt) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1553 | { |
| 1554 | CERT *c = s->cert; |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1555 | unsigned int stmp; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1556 | size_t size, i; |
| 1557 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1558 | /* Extension ignored for inappropriate versions */ |
| 1559 | if (!SSL_USE_SIGALGS(s)) |
| 1560 | return 1; |
| 1561 | /* Should never happen */ |
| 1562 | if (!c) |
| 1563 | return 0; |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 1564 | |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1565 | size = PACKET_remaining(pkt); |
| 1566 | |
| 1567 | /* Invalid data length */ |
| 1568 | if ((size & 1) != 0) |
| 1569 | return 0; |
| 1570 | |
| 1571 | size >>= 1; |
| 1572 | |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 1573 | OPENSSL_free(s->s3->tmp.peer_sigalgs); |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1574 | s->s3->tmp.peer_sigalgs = OPENSSL_malloc(size |
| 1575 | * sizeof(*s->s3->tmp.peer_sigalgs)); |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 1576 | if (s->s3->tmp.peer_sigalgs == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1577 | return 0; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1578 | s->s3->tmp.peer_sigalgslen = size; |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1579 | for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++) |
| 1580 | s->s3->tmp.peer_sigalgs[i] = stmp; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1581 | |
| 1582 | if (i != size) |
| 1583 | return 0; |
| 1584 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1585 | return 1; |
| 1586 | } |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 1587 | |
Dr. Stephen Henson | c800c27 | 2014-10-09 20:37:27 +0100 | [diff] [blame] | 1588 | int tls1_process_sigalgs(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1589 | { |
| 1590 | int idx; |
| 1591 | size_t i; |
| 1592 | const EVP_MD *md; |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 1593 | const EVP_MD **pmd = s->s3->tmp.md; |
Dr. Stephen Henson | f7d5348 | 2015-07-14 23:19:11 +0100 | [diff] [blame] | 1594 | uint32_t *pvalid = s->s3->tmp.valid_flags; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1595 | CERT *c = s->cert; |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1596 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1597 | if (!tls1_set_shared_sigalgs(s)) |
| 1598 | return 0; |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 1599 | |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1600 | for (i = 0; i < c->shared_sigalgslen; i++) { |
| 1601 | const SIGALG_LOOKUP *sigptr = c->shared_sigalgs[i]; |
| 1602 | |
Matt Caswell | 523fb32 | 2016-12-30 14:08:19 +0000 | [diff] [blame] | 1603 | /* Ignore PKCS1 based sig algs in TLSv1.3 */ |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1604 | if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA) |
Matt Caswell | 523fb32 | 2016-12-30 14:08:19 +0000 | [diff] [blame] | 1605 | continue; |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1606 | idx = tls12_get_pkey_idx(sigptr->sig); |
Dr. Stephen Henson | 8fd19b2 | 2017-02-10 04:22:18 +0000 | [diff] [blame] | 1607 | if (idx >= 0 && pmd[idx] == NULL) { |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1608 | md = ssl_md(sigptr->hash_idx); |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 1609 | pmd[idx] = md; |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 1610 | pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1611 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1612 | } |
| 1613 | /* |
Matt Caswell | 523fb32 | 2016-12-30 14:08:19 +0000 | [diff] [blame] | 1614 | * In strict mode or TLS1.3 leave unset digests as NULL to indicate we can't |
| 1615 | * use the certificate for signing. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1616 | */ |
Matt Caswell | 523fb32 | 2016-12-30 14:08:19 +0000 | [diff] [blame] | 1617 | if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) |
| 1618 | && !SSL_IS_TLS13(s)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1619 | /* |
| 1620 | * Set any remaining keys to default values. NOTE: if alg is not |
| 1621 | * supported it stays as NULL. |
| 1622 | */ |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1623 | #ifndef OPENSSL_NO_DSA |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 1624 | if (pmd[SSL_PKEY_DSA_SIGN] == NULL) |
| 1625 | pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1(); |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1626 | #endif |
| 1627 | #ifndef OPENSSL_NO_RSA |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 1628 | if (pmd[SSL_PKEY_RSA] == NULL) { |
| 1629 | pmd[SSL_PKEY_RSA] = EVP_sha1(); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1630 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1631 | #endif |
| 1632 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 1633 | if (pmd[SSL_PKEY_ECC] == NULL) |
| 1634 | pmd[SSL_PKEY_ECC] = EVP_sha1(); |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1635 | #endif |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1636 | #ifndef OPENSSL_NO_GOST |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1637 | if (pmd[SSL_PKEY_GOST01] == NULL) |
| 1638 | pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94); |
| 1639 | if (pmd[SSL_PKEY_GOST12_256] == NULL) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1640 | pmd[SSL_PKEY_GOST12_256] = |
| 1641 | EVP_get_digestbynid(NID_id_GostR3411_2012_256); |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1642 | if (pmd[SSL_PKEY_GOST12_512] == NULL) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1643 | pmd[SSL_PKEY_GOST12_512] = |
| 1644 | EVP_get_digestbynid(NID_id_GostR3411_2012_512); |
| 1645 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1646 | } |
| 1647 | return 1; |
| 1648 | } |
Dr. Stephen Henson | 4817504 | 2011-12-31 22:59:57 +0000 | [diff] [blame] | 1649 | |
Dr. Stephen Henson | e7f8ff4 | 2012-03-06 14:28:21 +0000 | [diff] [blame] | 1650 | int SSL_get_sigalgs(SSL *s, int idx, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1651 | int *psign, int *phash, int *psignhash, |
| 1652 | unsigned char *rsig, unsigned char *rhash) |
| 1653 | { |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1654 | uint16_t *psig = s->s3->tmp.peer_sigalgs; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1655 | size_t numsigalgs = s->s3->tmp.peer_sigalgslen; |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1656 | if (psig == NULL || numsigalgs > INT_MAX) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1657 | return 0; |
| 1658 | if (idx >= 0) { |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1659 | const SIGALG_LOOKUP *lu; |
| 1660 | |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1661 | if (idx >= (int)numsigalgs) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1662 | return 0; |
| 1663 | psig += idx; |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1664 | if (rhash != NULL) |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1665 | *rhash = (unsigned char)((*psig >> 8) & 0xff); |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1666 | if (rsig != NULL) |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1667 | *rsig = (unsigned char)(*psig & 0xff); |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1668 | lu = tls1_lookup_sigalg(*psig); |
| 1669 | if (psign != NULL) |
| 1670 | *psign = lu != NULL ? lu->sig : NID_undef; |
| 1671 | if (phash != NULL) |
| 1672 | *phash = lu != NULL ? lu->hash : NID_undef; |
| 1673 | if (psignhash != NULL) |
| 1674 | *psignhash = lu != NULL ? lu->sigandhash : NID_undef; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1675 | } |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1676 | return (int)numsigalgs; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1677 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 1678 | |
| 1679 | int SSL_get_shared_sigalgs(SSL *s, int idx, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1680 | int *psign, int *phash, int *psignhash, |
| 1681 | unsigned char *rsig, unsigned char *rhash) |
| 1682 | { |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1683 | const SIGALG_LOOKUP *shsigalgs; |
| 1684 | if (s->cert->shared_sigalgs == NULL |
Peter Wu | 6d047e0 | 2017-02-02 12:11:10 +0100 | [diff] [blame] | 1685 | || idx < 0 |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1686 | || idx >= (int)s->cert->shared_sigalgslen |
| 1687 | || s->cert->shared_sigalgslen > INT_MAX) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1688 | return 0; |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1689 | shsigalgs = s->cert->shared_sigalgs[idx]; |
| 1690 | if (phash != NULL) |
| 1691 | *phash = shsigalgs->hash; |
| 1692 | if (psign != NULL) |
| 1693 | *psign = shsigalgs->sig; |
| 1694 | if (psignhash != NULL) |
| 1695 | *psignhash = shsigalgs->sigandhash; |
| 1696 | if (rsig != NULL) |
| 1697 | *rsig = (unsigned char)(shsigalgs->sigalg & 0xff); |
| 1698 | if (rhash != NULL) |
| 1699 | *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff); |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1700 | return (int)s->cert->shared_sigalgslen; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1701 | } |
Dr. Stephen Henson | e7f8ff4 | 2012-03-06 14:28:21 +0000 | [diff] [blame] | 1702 | |
Dr. Stephen Henson | 787ebca | 2017-01-29 15:12:58 +0000 | [diff] [blame] | 1703 | /* Maximum possible number of unique entries in sigalgs array */ |
| 1704 | #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1705 | |
| 1706 | typedef struct { |
| 1707 | size_t sigalgcnt; |
Dr. Stephen Henson | 787ebca | 2017-01-29 15:12:58 +0000 | [diff] [blame] | 1708 | int sigalgs[TLS_MAX_SIGALGCNT]; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1709 | } sig_cb_st; |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1710 | |
Dr. Stephen Henson | 431f458 | 2015-07-23 14:57:42 +0100 | [diff] [blame] | 1711 | static void get_sigorhash(int *psig, int *phash, const char *str) |
| 1712 | { |
| 1713 | if (strcmp(str, "RSA") == 0) { |
| 1714 | *psig = EVP_PKEY_RSA; |
Dr. Stephen Henson | b2eb699 | 2017-01-25 16:22:13 +0000 | [diff] [blame] | 1715 | } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) { |
| 1716 | *psig = EVP_PKEY_RSA_PSS; |
Dr. Stephen Henson | 431f458 | 2015-07-23 14:57:42 +0100 | [diff] [blame] | 1717 | } else if (strcmp(str, "DSA") == 0) { |
| 1718 | *psig = EVP_PKEY_DSA; |
| 1719 | } else if (strcmp(str, "ECDSA") == 0) { |
| 1720 | *psig = EVP_PKEY_EC; |
| 1721 | } else { |
| 1722 | *phash = OBJ_sn2nid(str); |
| 1723 | if (*phash == NID_undef) |
| 1724 | *phash = OBJ_ln2nid(str); |
| 1725 | } |
| 1726 | } |
Dr. Stephen Henson | 787ebca | 2017-01-29 15:12:58 +0000 | [diff] [blame] | 1727 | /* Maximum length of a signature algorithm string component */ |
| 1728 | #define TLS_MAX_SIGSTRING_LEN 40 |
Dr. Stephen Henson | 431f458 | 2015-07-23 14:57:42 +0100 | [diff] [blame] | 1729 | |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1730 | static int sig_cb(const char *elem, int len, void *arg) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1731 | { |
| 1732 | sig_cb_st *sarg = arg; |
| 1733 | size_t i; |
Dr. Stephen Henson | 787ebca | 2017-01-29 15:12:58 +0000 | [diff] [blame] | 1734 | char etmp[TLS_MAX_SIGSTRING_LEN], *p; |
Dr. Stephen Henson | 431f458 | 2015-07-23 14:57:42 +0100 | [diff] [blame] | 1735 | int sig_alg = NID_undef, hash_alg = NID_undef; |
Kurt Roeckx | 2747d73 | 2015-01-24 14:46:50 +0100 | [diff] [blame] | 1736 | if (elem == NULL) |
| 1737 | return 0; |
Dr. Stephen Henson | 787ebca | 2017-01-29 15:12:58 +0000 | [diff] [blame] | 1738 | if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1739 | return 0; |
| 1740 | if (len > (int)(sizeof(etmp) - 1)) |
| 1741 | return 0; |
| 1742 | memcpy(etmp, elem, len); |
| 1743 | etmp[len] = 0; |
| 1744 | p = strchr(etmp, '+'); |
Dr. Stephen Henson | 8a43a42 | 2017-01-26 14:40:59 +0000 | [diff] [blame] | 1745 | /* See if we have a match for TLS 1.3 names */ |
| 1746 | if (p == NULL) { |
| 1747 | const SIGALG_LOOKUP *s; |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1748 | |
Dr. Stephen Henson | 8a43a42 | 2017-01-26 14:40:59 +0000 | [diff] [blame] | 1749 | for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); |
| 1750 | i++, s++) { |
| 1751 | if (s->name != NULL && strcmp(etmp, s->name) == 0) { |
| 1752 | sig_alg = s->sig; |
| 1753 | hash_alg = s->hash; |
| 1754 | break; |
| 1755 | } |
| 1756 | } |
| 1757 | } else { |
| 1758 | *p = 0; |
| 1759 | p++; |
| 1760 | if (*p == 0) |
| 1761 | return 0; |
| 1762 | get_sigorhash(&sig_alg, &hash_alg, etmp); |
| 1763 | get_sigorhash(&sig_alg, &hash_alg, p); |
| 1764 | } |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1765 | |
Dr. Stephen Henson | 431f458 | 2015-07-23 14:57:42 +0100 | [diff] [blame] | 1766 | if (sig_alg == NID_undef || hash_alg == NID_undef) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1767 | return 0; |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1768 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1769 | for (i = 0; i < sarg->sigalgcnt; i += 2) { |
| 1770 | if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg) |
| 1771 | return 0; |
| 1772 | } |
| 1773 | sarg->sigalgs[sarg->sigalgcnt++] = hash_alg; |
| 1774 | sarg->sigalgs[sarg->sigalgcnt++] = sig_alg; |
| 1775 | return 1; |
| 1776 | } |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1777 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1778 | /* |
FdaSilvaYY | 9d22666 | 2016-06-28 22:39:55 +0200 | [diff] [blame] | 1779 | * Set supported signature algorithms based on a colon separated list of the |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1780 | * form sig+hash e.g. RSA+SHA512:DSA+SHA512 |
| 1781 | */ |
Dr. Stephen Henson | 3dbc46d | 2012-07-03 12:51:14 +0000 | [diff] [blame] | 1782 | int tls1_set_sigalgs_list(CERT *c, const char *str, int client) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1783 | { |
| 1784 | sig_cb_st sig; |
| 1785 | sig.sigalgcnt = 0; |
| 1786 | if (!CONF_parse_list(str, ':', 1, sig_cb, &sig)) |
| 1787 | return 0; |
| 1788 | if (c == NULL) |
| 1789 | return 1; |
| 1790 | return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client); |
| 1791 | } |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1792 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1793 | int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1794 | { |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1795 | uint16_t *sigalgs, *sptr; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1796 | size_t i; |
Matt Caswell | 63c1df0 | 2016-12-14 16:50:14 +0000 | [diff] [blame] | 1797 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1798 | if (salglen & 1) |
| 1799 | return 0; |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 1800 | sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1801 | if (sigalgs == NULL) |
| 1802 | return 0; |
| 1803 | for (i = 0, sptr = sigalgs; i < salglen; i += 2) { |
Matt Caswell | 63c1df0 | 2016-12-14 16:50:14 +0000 | [diff] [blame] | 1804 | size_t j; |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 1805 | const SIGALG_LOOKUP *curr; |
Matt Caswell | 63c1df0 | 2016-12-14 16:50:14 +0000 | [diff] [blame] | 1806 | int md_id = *psig_nids++; |
| 1807 | int sig_id = *psig_nids++; |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1808 | |
Matt Caswell | 63c1df0 | 2016-12-14 16:50:14 +0000 | [diff] [blame] | 1809 | for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl); |
| 1810 | j++, curr++) { |
Matt Caswell | fe3066e | 2017-01-03 10:01:39 +0000 | [diff] [blame] | 1811 | if (curr->hash == md_id && curr->sig == sig_id) { |
Matt Caswell | 63c1df0 | 2016-12-14 16:50:14 +0000 | [diff] [blame] | 1812 | *sptr++ = curr->sigalg; |
| 1813 | break; |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | if (j == OSSL_NELEM(sigalg_lookup_tbl)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1818 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1819 | } |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1820 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1821 | if (client) { |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 1822 | OPENSSL_free(c->client_sigalgs); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1823 | c->client_sigalgs = sigalgs; |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 1824 | c->client_sigalgslen = salglen / 2; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1825 | } else { |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 1826 | OPENSSL_free(c->conf_sigalgs); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1827 | c->conf_sigalgs = sigalgs; |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 1828 | c->conf_sigalgslen = salglen / 2; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1829 | } |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1830 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1831 | return 1; |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1832 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1833 | err: |
| 1834 | OPENSSL_free(sigalgs); |
| 1835 | return 0; |
| 1836 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 1837 | |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1838 | static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1839 | { |
| 1840 | int sig_nid; |
| 1841 | size_t i; |
| 1842 | if (default_nid == -1) |
| 1843 | return 1; |
| 1844 | sig_nid = X509_get_signature_nid(x); |
| 1845 | if (default_nid) |
| 1846 | return sig_nid == default_nid ? 1 : 0; |
| 1847 | for (i = 0; i < c->shared_sigalgslen; i++) |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1848 | if (sig_nid == c->shared_sigalgs[i]->sigandhash) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1849 | return 1; |
| 1850 | return 0; |
| 1851 | } |
| 1852 | |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 1853 | /* Check to see if a certificate issuer name matches list of CA names */ |
| 1854 | static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1855 | { |
| 1856 | X509_NAME *nm; |
| 1857 | int i; |
| 1858 | nm = X509_get_issuer_name(x); |
| 1859 | for (i = 0; i < sk_X509_NAME_num(names); i++) { |
| 1860 | if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i))) |
| 1861 | return 1; |
| 1862 | } |
| 1863 | return 0; |
| 1864 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1865 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1866 | /* |
| 1867 | * Check certificate chain is consistent with TLS extensions and is usable by |
| 1868 | * server. This servers two purposes: it allows users to check chains before |
| 1869 | * passing them to the server and it allows the server to check chains before |
| 1870 | * attempting to use them. |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1871 | */ |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 1872 | |
| 1873 | /* Flags which need to be set for a certificate when stict mode not set */ |
| 1874 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1875 | #define CERT_PKEY_VALID_FLAGS \ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1876 | (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM) |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 1877 | /* Strict mode flags */ |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1878 | #define CERT_PKEY_STRICT_FLAGS \ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1879 | (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \ |
| 1880 | | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE) |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 1881 | |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1882 | int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1883 | int idx) |
| 1884 | { |
| 1885 | int i; |
| 1886 | int rv = 0; |
| 1887 | int check_flags = 0, strict_mode; |
| 1888 | CERT_PKEY *cpk = NULL; |
| 1889 | CERT *c = s->cert; |
Dr. Stephen Henson | f7d5348 | 2015-07-14 23:19:11 +0100 | [diff] [blame] | 1890 | uint32_t *pvalid; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1891 | unsigned int suiteb_flags = tls1_suiteb(s); |
| 1892 | /* idx == -1 means checking server chains */ |
| 1893 | if (idx != -1) { |
| 1894 | /* idx == -2 means checking client certificate chains */ |
| 1895 | if (idx == -2) { |
| 1896 | cpk = c->key; |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1897 | idx = (int)(cpk - c->pkeys); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1898 | } else |
| 1899 | cpk = c->pkeys + idx; |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 1900 | pvalid = s->s3->tmp.valid_flags + idx; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1901 | x = cpk->x509; |
| 1902 | pk = cpk->privatekey; |
| 1903 | chain = cpk->chain; |
| 1904 | strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT; |
| 1905 | /* If no cert or key, forget it */ |
| 1906 | if (!x || !pk) |
| 1907 | goto end; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1908 | } else { |
| 1909 | if (!x || !pk) |
Matt Caswell | d813f9e | 2015-03-11 17:01:38 +0000 | [diff] [blame] | 1910 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1911 | idx = ssl_cert_type(x, pk); |
| 1912 | if (idx == -1) |
Matt Caswell | d813f9e | 2015-03-11 17:01:38 +0000 | [diff] [blame] | 1913 | return 0; |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 1914 | pvalid = s->s3->tmp.valid_flags + idx; |
| 1915 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1916 | if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) |
| 1917 | check_flags = CERT_PKEY_STRICT_FLAGS; |
| 1918 | else |
| 1919 | check_flags = CERT_PKEY_VALID_FLAGS; |
| 1920 | strict_mode = 1; |
| 1921 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1922 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1923 | if (suiteb_flags) { |
| 1924 | int ok; |
| 1925 | if (check_flags) |
| 1926 | check_flags |= CERT_PKEY_SUITEB; |
| 1927 | ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags); |
| 1928 | if (ok == X509_V_OK) |
| 1929 | rv |= CERT_PKEY_SUITEB; |
| 1930 | else if (!check_flags) |
| 1931 | goto end; |
| 1932 | } |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 1933 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1934 | /* |
| 1935 | * Check all signature algorithms are consistent with signature |
| 1936 | * algorithms extension if TLS 1.2 or later and strict mode. |
| 1937 | */ |
| 1938 | if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) { |
| 1939 | int default_nid; |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1940 | int rsign = 0; |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 1941 | if (s->s3->tmp.peer_sigalgs) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1942 | default_nid = 0; |
| 1943 | /* If no sigalgs extension use defaults from RFC5246 */ |
| 1944 | else { |
| 1945 | switch (idx) { |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 1946 | case SSL_PKEY_RSA: |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1947 | rsign = EVP_PKEY_RSA; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1948 | default_nid = NID_sha1WithRSAEncryption; |
| 1949 | break; |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1950 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1951 | case SSL_PKEY_DSA_SIGN: |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1952 | rsign = EVP_PKEY_DSA; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1953 | default_nid = NID_dsaWithSHA1; |
| 1954 | break; |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1955 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1956 | case SSL_PKEY_ECC: |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1957 | rsign = EVP_PKEY_EC; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1958 | default_nid = NID_ecdsa_with_SHA1; |
| 1959 | break; |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1960 | |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1961 | case SSL_PKEY_GOST01: |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1962 | rsign = NID_id_GostR3410_2001; |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1963 | default_nid = NID_id_GostR3411_94_with_GostR3410_2001; |
| 1964 | break; |
| 1965 | |
| 1966 | case SSL_PKEY_GOST12_256: |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1967 | rsign = NID_id_GostR3410_2012_256; |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1968 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256; |
| 1969 | break; |
| 1970 | |
| 1971 | case SSL_PKEY_GOST12_512: |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1972 | rsign = NID_id_GostR3410_2012_512; |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1973 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512; |
| 1974 | break; |
| 1975 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1976 | default: |
| 1977 | default_nid = -1; |
| 1978 | break; |
| 1979 | } |
| 1980 | } |
| 1981 | /* |
| 1982 | * If peer sent no signature algorithms extension and we have set |
| 1983 | * preferred signature algorithms check we support sha1. |
| 1984 | */ |
| 1985 | if (default_nid > 0 && c->conf_sigalgs) { |
| 1986 | size_t j; |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1987 | const uint16_t *p = c->conf_sigalgs; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1988 | for (j = 0; j < c->conf_sigalgslen; j++, p++) { |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1989 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*p); |
| 1990 | |
| 1991 | if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1992 | break; |
| 1993 | } |
| 1994 | if (j == c->conf_sigalgslen) { |
| 1995 | if (check_flags) |
| 1996 | goto skip_sigs; |
| 1997 | else |
| 1998 | goto end; |
| 1999 | } |
| 2000 | } |
| 2001 | /* Check signature algorithm of each cert in chain */ |
| 2002 | if (!tls1_check_sig_alg(c, x, default_nid)) { |
| 2003 | if (!check_flags) |
| 2004 | goto end; |
| 2005 | } else |
| 2006 | rv |= CERT_PKEY_EE_SIGNATURE; |
| 2007 | rv |= CERT_PKEY_CA_SIGNATURE; |
| 2008 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 2009 | if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) { |
| 2010 | if (check_flags) { |
| 2011 | rv &= ~CERT_PKEY_CA_SIGNATURE; |
| 2012 | break; |
| 2013 | } else |
| 2014 | goto end; |
| 2015 | } |
| 2016 | } |
| 2017 | } |
| 2018 | /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */ |
| 2019 | else if (check_flags) |
| 2020 | rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE; |
| 2021 | skip_sigs: |
| 2022 | /* Check cert parameters are consistent */ |
| 2023 | if (tls1_check_cert_param(s, x, check_flags ? 1 : 2)) |
| 2024 | rv |= CERT_PKEY_EE_PARAM; |
| 2025 | else if (!check_flags) |
| 2026 | goto end; |
| 2027 | if (!s->server) |
| 2028 | rv |= CERT_PKEY_CA_PARAM; |
| 2029 | /* In strict mode check rest of chain too */ |
| 2030 | else if (strict_mode) { |
| 2031 | rv |= CERT_PKEY_CA_PARAM; |
| 2032 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 2033 | X509 *ca = sk_X509_value(chain, i); |
| 2034 | if (!tls1_check_cert_param(s, ca, 0)) { |
| 2035 | if (check_flags) { |
| 2036 | rv &= ~CERT_PKEY_CA_PARAM; |
| 2037 | break; |
| 2038 | } else |
| 2039 | goto end; |
| 2040 | } |
| 2041 | } |
| 2042 | } |
| 2043 | if (!s->server && strict_mode) { |
| 2044 | STACK_OF(X509_NAME) *ca_dn; |
| 2045 | int check_type = 0; |
Dr. Stephen Henson | 3aeb934 | 2016-01-19 00:21:12 +0000 | [diff] [blame] | 2046 | switch (EVP_PKEY_id(pk)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2047 | case EVP_PKEY_RSA: |
| 2048 | check_type = TLS_CT_RSA_SIGN; |
| 2049 | break; |
| 2050 | case EVP_PKEY_DSA: |
| 2051 | check_type = TLS_CT_DSS_SIGN; |
| 2052 | break; |
| 2053 | case EVP_PKEY_EC: |
| 2054 | check_type = TLS_CT_ECDSA_SIGN; |
| 2055 | break; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2056 | } |
| 2057 | if (check_type) { |
| 2058 | const unsigned char *ctypes; |
| 2059 | int ctypelen; |
| 2060 | if (c->ctypes) { |
| 2061 | ctypes = c->ctypes; |
| 2062 | ctypelen = (int)c->ctype_num; |
| 2063 | } else { |
| 2064 | ctypes = (unsigned char *)s->s3->tmp.ctype; |
| 2065 | ctypelen = s->s3->tmp.ctype_num; |
| 2066 | } |
| 2067 | for (i = 0; i < ctypelen; i++) { |
| 2068 | if (ctypes[i] == check_type) { |
| 2069 | rv |= CERT_PKEY_CERT_TYPE; |
| 2070 | break; |
| 2071 | } |
| 2072 | } |
| 2073 | if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags) |
| 2074 | goto end; |
| 2075 | } else |
| 2076 | rv |= CERT_PKEY_CERT_TYPE; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 2077 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2078 | ca_dn = s->s3->tmp.ca_names; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 2079 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2080 | if (!sk_X509_NAME_num(ca_dn)) |
| 2081 | rv |= CERT_PKEY_ISSUER_NAME; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 2082 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2083 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { |
| 2084 | if (ssl_check_ca_name(ca_dn, x)) |
| 2085 | rv |= CERT_PKEY_ISSUER_NAME; |
| 2086 | } |
| 2087 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { |
| 2088 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 2089 | X509 *xtmp = sk_X509_value(chain, i); |
| 2090 | if (ssl_check_ca_name(ca_dn, xtmp)) { |
| 2091 | rv |= CERT_PKEY_ISSUER_NAME; |
| 2092 | break; |
| 2093 | } |
| 2094 | } |
| 2095 | } |
| 2096 | if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME)) |
| 2097 | goto end; |
| 2098 | } else |
| 2099 | rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 2100 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2101 | if (!check_flags || (rv & check_flags) == check_flags) |
| 2102 | rv |= CERT_PKEY_VALID; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 2103 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2104 | end: |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 2105 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2106 | if (TLS1_get_version(s) >= TLS1_2_VERSION) { |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 2107 | if (*pvalid & CERT_PKEY_EXPLICIT_SIGN) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2108 | rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 2109 | else if (s->s3->tmp.md[idx] != NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2110 | rv |= CERT_PKEY_SIGN; |
| 2111 | } else |
| 2112 | rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 2113 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2114 | /* |
| 2115 | * When checking a CERT_PKEY structure all flags are irrelevant if the |
| 2116 | * chain is invalid. |
| 2117 | */ |
| 2118 | if (!check_flags) { |
| 2119 | if (rv & CERT_PKEY_VALID) |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 2120 | *pvalid = rv; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2121 | else { |
| 2122 | /* Preserve explicit sign flag, clear rest */ |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 2123 | *pvalid &= CERT_PKEY_EXPLICIT_SIGN; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2124 | return 0; |
| 2125 | } |
| 2126 | } |
| 2127 | return rv; |
| 2128 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 2129 | |
| 2130 | /* Set validity of certificates in an SSL structure */ |
| 2131 | void tls1_set_cert_validity(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2132 | { |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 2133 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA); |
Matt Caswell | 17dd65e | 2015-03-24 15:10:15 +0000 | [diff] [blame] | 2134 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN); |
Matt Caswell | 17dd65e | 2015-03-24 15:10:15 +0000 | [diff] [blame] | 2135 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC); |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 2136 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01); |
| 2137 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256); |
| 2138 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Dr. Stephen Henson | 18d7158 | 2012-06-29 14:24:42 +0000 | [diff] [blame] | 2141 | /* User level utiity function to check a chain is suitable */ |
| 2142 | int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2143 | { |
| 2144 | return tls1_check_chain(s, x, pk, chain, -1); |
| 2145 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 2146 | |
Dr. Stephen Henson | 09599b5 | 2014-01-22 16:22:48 +0000 | [diff] [blame] | 2147 | #ifndef OPENSSL_NO_DH |
| 2148 | DH *ssl_get_auto_dh(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2149 | { |
| 2150 | int dh_secbits = 80; |
| 2151 | if (s->cert->dh_tmp_auto == 2) |
| 2152 | return DH_get_1024_160(); |
Dr. Stephen Henson | adc5506 | 2015-06-28 17:01:52 +0100 | [diff] [blame] | 2153 | if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2154 | if (s->s3->tmp.new_cipher->strength_bits == 256) |
| 2155 | dh_secbits = 128; |
| 2156 | else |
| 2157 | dh_secbits = 80; |
| 2158 | } else { |
Dr. Stephen Henson | a497cf2 | 2017-02-14 00:35:26 +0000 | [diff] [blame] | 2159 | if (s->s3->tmp.cert == NULL) |
Dr. Stephen Henson | f365a3e | 2017-02-13 16:32:06 +0000 | [diff] [blame] | 2160 | return NULL; |
Dr. Stephen Henson | a497cf2 | 2017-02-14 00:35:26 +0000 | [diff] [blame] | 2161 | dh_secbits = EVP_PKEY_security_bits(s->s3->tmp.cert->privatekey); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2162 | } |
Dr. Stephen Henson | 09599b5 | 2014-01-22 16:22:48 +0000 | [diff] [blame] | 2163 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2164 | if (dh_secbits >= 128) { |
| 2165 | DH *dhp = DH_new(); |
Matt Caswell | 0aeddcf | 2016-04-06 17:49:48 +0100 | [diff] [blame] | 2166 | BIGNUM *p, *g; |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 2167 | if (dhp == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2168 | return NULL; |
Matt Caswell | 0aeddcf | 2016-04-06 17:49:48 +0100 | [diff] [blame] | 2169 | g = BN_new(); |
| 2170 | if (g != NULL) |
| 2171 | BN_set_word(g, 2); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2172 | if (dh_secbits >= 192) |
Rich Salz | 9021a5d | 2016-04-18 07:43:54 -0400 | [diff] [blame] | 2173 | p = BN_get_rfc3526_prime_8192(NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2174 | else |
Rich Salz | 9021a5d | 2016-04-18 07:43:54 -0400 | [diff] [blame] | 2175 | p = BN_get_rfc3526_prime_3072(NULL); |
Matt Caswell | 0aeddcf | 2016-04-06 17:49:48 +0100 | [diff] [blame] | 2176 | if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2177 | DH_free(dhp); |
Matt Caswell | 0aeddcf | 2016-04-06 17:49:48 +0100 | [diff] [blame] | 2178 | BN_free(p); |
| 2179 | BN_free(g); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2180 | return NULL; |
| 2181 | } |
| 2182 | return dhp; |
| 2183 | } |
| 2184 | if (dh_secbits >= 112) |
| 2185 | return DH_get_2048_224(); |
| 2186 | return DH_get_1024_160(); |
| 2187 | } |
Dr. Stephen Henson | 09599b5 | 2014-01-22 16:22:48 +0000 | [diff] [blame] | 2188 | #endif |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2189 | |
| 2190 | static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2191 | { |
Dr. Stephen Henson | 72245f3 | 2015-12-30 13:34:53 +0000 | [diff] [blame] | 2192 | int secbits = -1; |
Dr. Stephen Henson | 8382fd3 | 2015-12-20 00:32:36 +0000 | [diff] [blame] | 2193 | EVP_PKEY *pkey = X509_get0_pubkey(x); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2194 | if (pkey) { |
Dr. Stephen Henson | 72245f3 | 2015-12-30 13:34:53 +0000 | [diff] [blame] | 2195 | /* |
| 2196 | * If no parameters this will return -1 and fail using the default |
| 2197 | * security callback for any non-zero security level. This will |
| 2198 | * reject keys which omit parameters but this only affects DSA and |
| 2199 | * omission of parameters is never (?) done in practice. |
| 2200 | */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2201 | secbits = EVP_PKEY_security_bits(pkey); |
Dr. Stephen Henson | 72245f3 | 2015-12-30 13:34:53 +0000 | [diff] [blame] | 2202 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2203 | if (s) |
| 2204 | return ssl_security(s, op, secbits, 0, x); |
| 2205 | else |
| 2206 | return ssl_ctx_security(ctx, op, secbits, 0, x); |
| 2207 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2208 | |
| 2209 | static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2210 | { |
| 2211 | /* Lookup signature algorithm digest */ |
| 2212 | int secbits = -1, md_nid = NID_undef, sig_nid; |
Dr. Stephen Henson | 221c7b5 | 2016-02-11 15:25:11 +0000 | [diff] [blame] | 2213 | /* Don't check signature if self signed */ |
| 2214 | if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0) |
| 2215 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2216 | sig_nid = X509_get_signature_nid(x); |
| 2217 | if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) { |
| 2218 | const EVP_MD *md; |
| 2219 | if (md_nid && (md = EVP_get_digestbynid(md_nid))) |
| 2220 | secbits = EVP_MD_size(md) * 4; |
| 2221 | } |
| 2222 | if (s) |
| 2223 | return ssl_security(s, op, secbits, md_nid, x); |
| 2224 | else |
| 2225 | return ssl_ctx_security(ctx, op, secbits, md_nid, x); |
| 2226 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2227 | |
| 2228 | int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2229 | { |
| 2230 | if (vfy) |
| 2231 | vfy = SSL_SECOP_PEER; |
| 2232 | if (is_ee) { |
| 2233 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy)) |
| 2234 | return SSL_R_EE_KEY_TOO_SMALL; |
| 2235 | } else { |
| 2236 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy)) |
| 2237 | return SSL_R_CA_KEY_TOO_SMALL; |
| 2238 | } |
| 2239 | if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy)) |
| 2240 | return SSL_R_CA_MD_TOO_WEAK; |
| 2241 | return 1; |
| 2242 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2243 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2244 | /* |
| 2245 | * Check security of a chain, if sk includes the end entity certificate then |
| 2246 | * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending |
| 2247 | * one to the peer. Return values: 1 if ok otherwise error code to use |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2248 | */ |
| 2249 | |
| 2250 | int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2251 | { |
| 2252 | int rv, start_idx, i; |
| 2253 | if (x == NULL) { |
| 2254 | x = sk_X509_value(sk, 0); |
| 2255 | start_idx = 1; |
| 2256 | } else |
| 2257 | start_idx = 0; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2258 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2259 | rv = ssl_security_cert(s, NULL, x, vfy, 1); |
| 2260 | if (rv != 1) |
| 2261 | return rv; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2262 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2263 | for (i = start_idx; i < sk_X509_num(sk); i++) { |
| 2264 | x = sk_X509_value(sk, i); |
| 2265 | rv = ssl_security_cert(s, NULL, x, vfy, 0); |
| 2266 | if (rv != 1) |
| 2267 | return rv; |
| 2268 | } |
| 2269 | return 1; |
| 2270 | } |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2271 | |
| 2272 | /* |
| 2273 | * Choose an appropriate signature algorithm based on available certificates |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2274 | * Sets chosen certificate and signature algorithm. |
| 2275 | * |
| 2276 | * For servers if we fail to find a required certificate it is a fatal error |
| 2277 | * and an appropriate error code is set and the TLS alert set in *al. |
| 2278 | * |
| 2279 | * For clients al is set to NULL. If a certificate is not suitable it is not |
| 2280 | * a fatal error: we will either try another certificate or not present one |
| 2281 | * to the server. In this case no error is set. |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2282 | */ |
Dr. Stephen Henson | 4a419f6 | 2017-02-13 15:50:43 +0000 | [diff] [blame] | 2283 | int tls_choose_sigalg(SSL *s, int *al) |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2284 | { |
Dr. Stephen Henson | 7b3a4d6 | 2017-02-16 01:29:14 +0000 | [diff] [blame] | 2285 | int idx = -1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2286 | const SIGALG_LOOKUP *lu = NULL; |
| 2287 | |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2288 | s->s3->tmp.cert = NULL; |
| 2289 | s->s3->tmp.sigalg = NULL; |
| 2290 | |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2291 | if (SSL_IS_TLS13(s)) { |
| 2292 | size_t i; |
Richard Levitte | 21f198e | 2017-02-03 13:56:54 +0100 | [diff] [blame] | 2293 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2294 | int curve = -1; |
Richard Levitte | 21f198e | 2017-02-03 13:56:54 +0100 | [diff] [blame] | 2295 | #endif |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2296 | |
| 2297 | /* Look for a certificate matching shared sigaglgs */ |
| 2298 | for (i = 0; i < s->cert->shared_sigalgslen; i++) { |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2299 | lu = s->cert->shared_sigalgs[i]; |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2300 | |
Dr. Stephen Henson | 5a8916d | 2017-02-17 16:08:19 +0000 | [diff] [blame^] | 2301 | /* Skip DSA and RSA if not PSS */ |
| 2302 | if (lu->sig == EVP_PKEY_DSA || lu->sig == EVP_PKEY_RSA) |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2303 | continue; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2304 | if (ssl_md(lu->hash_idx) == NULL) |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2305 | continue; |
| 2306 | idx = lu->sig_idx; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2307 | if (!ssl_has_cert(s, idx)) |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2308 | continue; |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2309 | if (lu->sig == EVP_PKEY_EC) { |
Richard Levitte | 21f198e | 2017-02-03 13:56:54 +0100 | [diff] [blame] | 2310 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2311 | if (curve == -1) { |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2312 | EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[idx].privatekey); |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2313 | |
| 2314 | curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); |
| 2315 | } |
Dr. Stephen Henson | a34a9df | 2017-02-14 14:27:15 +0000 | [diff] [blame] | 2316 | if (lu->curve != NID_undef && curve != lu->curve) |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2317 | continue; |
Richard Levitte | 21f198e | 2017-02-03 13:56:54 +0100 | [diff] [blame] | 2318 | #else |
| 2319 | continue; |
| 2320 | #endif |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2321 | } |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2322 | break; |
| 2323 | } |
| 2324 | if (i == s->cert->shared_sigalgslen) { |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2325 | if (al == NULL) |
| 2326 | return 1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2327 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 2328 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, |
| 2329 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
| 2330 | return 0; |
| 2331 | } |
| 2332 | } else { |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2333 | if (s->server) { |
| 2334 | /* Find index corresponding to ciphersuite */ |
| 2335 | idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); |
| 2336 | /* If no certificate for ciphersuite return */ |
| 2337 | if (idx == -1) |
| 2338 | return 1; |
| 2339 | if (idx == SSL_PKEY_GOST_EC) { |
| 2340 | /* Work out which GOST certificate is avaiable */ |
| 2341 | if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) { |
| 2342 | idx = SSL_PKEY_GOST12_512; |
| 2343 | } else if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) { |
| 2344 | idx = SSL_PKEY_GOST12_256; |
| 2345 | } else if (ssl_has_cert(s, SSL_PKEY_GOST01)) { |
| 2346 | idx = SSL_PKEY_GOST01; |
| 2347 | } else { |
| 2348 | if (al == NULL) |
| 2349 | return 1; |
| 2350 | *al = SSL_AD_INTERNAL_ERROR; |
| 2351 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); |
| 2352 | return 0; |
| 2353 | } |
| 2354 | } else if (!ssl_has_cert(s, idx)) { |
| 2355 | if (al == NULL) |
| 2356 | return 1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2357 | *al = SSL_AD_INTERNAL_ERROR; |
| 2358 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); |
| 2359 | return 0; |
| 2360 | } |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2361 | } else { |
| 2362 | /* Find index for client certificate */ |
| 2363 | idx = s->cert->key - s->cert->pkeys; |
| 2364 | if (!ssl_has_cert(s, idx)) |
| 2365 | return 1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
| 2368 | if (SSL_USE_SIGALGS(s)) { |
| 2369 | if (s->s3->tmp.peer_sigalgs != NULL) { |
| 2370 | size_t i; |
| 2371 | |
| 2372 | /* |
| 2373 | * Find highest preference signature algorithm matching |
| 2374 | * cert type |
| 2375 | */ |
| 2376 | for (i = 0; i < s->cert->shared_sigalgslen; i++) { |
| 2377 | lu = s->cert->shared_sigalgs[i]; |
| 2378 | if (lu->sig_idx == idx) |
| 2379 | break; |
| 2380 | if (idx == SSL_PKEY_RSA && lu->sig == EVP_PKEY_RSA_PSS) |
| 2381 | break; |
| 2382 | } |
| 2383 | if (i == s->cert->shared_sigalgslen) { |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2384 | if (al == NULL) |
| 2385 | return 1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2386 | *al = SSL_AD_INTERNAL_ERROR; |
| 2387 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); |
| 2388 | return 0; |
| 2389 | } |
| 2390 | } else { |
| 2391 | /* |
| 2392 | * If we have no sigalg use defaults |
| 2393 | */ |
| 2394 | const uint16_t *sent_sigs; |
| 2395 | size_t sent_sigslen, i; |
| 2396 | |
| 2397 | if ((lu = tls1_get_legacy_sigalg(s, idx)) == NULL) { |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2398 | if (al == NULL) |
| 2399 | return 1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2400 | *al = SSL_AD_INTERNAL_ERROR; |
| 2401 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); |
| 2402 | return 0; |
| 2403 | } |
| 2404 | |
| 2405 | /* Check signature matches a type we sent */ |
| 2406 | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
| 2407 | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { |
| 2408 | if (lu->sigalg == *sent_sigs) |
| 2409 | break; |
| 2410 | } |
| 2411 | if (i == sent_sigslen) { |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2412 | if (al == NULL) |
| 2413 | return 1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2414 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
| 2415 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 2416 | return 0; |
| 2417 | } |
| 2418 | } |
| 2419 | } else { |
| 2420 | if ((lu = tls1_get_legacy_sigalg(s, idx)) == NULL) { |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2421 | if (al == NULL) |
| 2422 | return 1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2423 | *al = SSL_AD_INTERNAL_ERROR; |
| 2424 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); |
| 2425 | return 0; |
| 2426 | } |
| 2427 | } |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2428 | } |
Dr. Stephen Henson | 7b3a4d6 | 2017-02-16 01:29:14 +0000 | [diff] [blame] | 2429 | if (idx == -1) { |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2430 | if (al != NULL) { |
| 2431 | *al = SSL_AD_INTERNAL_ERROR; |
| 2432 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); |
| 2433 | } |
Dr. Stephen Henson | 7b3a4d6 | 2017-02-16 01:29:14 +0000 | [diff] [blame] | 2434 | return 0; |
| 2435 | } |
Dr. Stephen Henson | a497cf2 | 2017-02-14 00:35:26 +0000 | [diff] [blame] | 2436 | s->s3->tmp.cert = &s->cert->pkeys[idx]; |
Dr. Stephen Henson | 59088e4 | 2017-02-15 15:28:56 +0000 | [diff] [blame] | 2437 | s->cert->key = s->s3->tmp.cert; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2438 | s->s3->tmp.sigalg = lu; |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2439 | return 1; |
| 2440 | } |