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 | 9195ddc | 2017-02-18 03:42:15 +0000 | [diff] [blame] | 571 | static int tls1_check_cert_param(SSL *s, X509 *x, int check_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 |
Dr. Stephen Henson | 9195ddc | 2017-02-18 03:42:15 +0000 | [diff] [blame] | 594 | * SHA384+P-384. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 595 | */ |
Dr. Stephen Henson | 9195ddc | 2017-02-18 03:42:15 +0000 | [diff] [blame] | 596 | if (check_ee_md && tls1_suiteb(s)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 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; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 614 | } |
| 615 | return rv; |
| 616 | } |
| 617 | |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 618 | # ifndef OPENSSL_NO_EC |
Kurt Roeckx | 6977e8e | 2015-12-04 22:25:11 +0100 | [diff] [blame] | 619 | /* |
FdaSilvaYY | 8483a00 | 2016-03-10 21:34:48 +0100 | [diff] [blame] | 620 | * tls1_check_ec_tmp_key - Check EC temporary key compatibility |
Kurt Roeckx | 6977e8e | 2015-12-04 22:25:11 +0100 | [diff] [blame] | 621 | * @s: SSL connection |
| 622 | * @cid: Cipher ID we're considering using |
| 623 | * |
| 624 | * Checks that the kECDHE cipher suite we're considering using |
| 625 | * is compatible with the client extensions. |
| 626 | * |
| 627 | * Returns 0 when the cipher can't be used or 1 when it can. |
| 628 | */ |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 629 | int tls1_check_ec_tmp_key(SSL *s, unsigned long cid) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 630 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 631 | /* |
| 632 | * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other |
| 633 | * curves permitted. |
| 634 | */ |
| 635 | if (tls1_suiteb(s)) { |
Kurt Roeckx | 6977e8e | 2015-12-04 22:25:11 +0100 | [diff] [blame] | 636 | unsigned char curve_id[2]; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 637 | /* Curve to check determined by ciphersuite */ |
| 638 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
| 639 | curve_id[1] = TLSEXT_curve_P_256; |
| 640 | else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
| 641 | curve_id[1] = TLSEXT_curve_P_384; |
| 642 | else |
| 643 | return 0; |
| 644 | curve_id[0] = 0; |
| 645 | /* Check this curve is acceptable */ |
| 646 | if (!tls1_check_ec_key(s, curve_id, NULL)) |
| 647 | return 0; |
Kurt Roeckx | fe6ef24 | 2015-12-04 22:30:36 +0100 | [diff] [blame] | 648 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 649 | } |
Kurt Roeckx | fe6ef24 | 2015-12-04 22:30:36 +0100 | [diff] [blame] | 650 | /* Need a shared curve */ |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 651 | if (tls1_shared_group(s, 0)) |
Kurt Roeckx | fe6ef24 | 2015-12-04 22:30:36 +0100 | [diff] [blame] | 652 | return 1; |
Kurt Roeckx | 6977e8e | 2015-12-04 22:25:11 +0100 | [diff] [blame] | 653 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 654 | } |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 655 | # endif /* OPENSSL_NO_EC */ |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 656 | |
Dr. Stephen Henson | 14536c8 | 2013-08-17 17:40:08 +0100 | [diff] [blame] | 657 | #else |
| 658 | |
| 659 | 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] | 660 | { |
| 661 | return 1; |
| 662 | } |
Dr. Stephen Henson | 14536c8 | 2013-08-17 17:40:08 +0100 | [diff] [blame] | 663 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 664 | #endif /* OPENSSL_NO_EC */ |
Bodo Möller | f1fd454 | 2006-01-03 03:27:19 +0000 | [diff] [blame] | 665 | |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 666 | /* Default sigalg schemes */ |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 667 | static const uint16_t tls12_sigalgs[] = { |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 668 | #ifndef OPENSSL_NO_EC |
| 669 | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
| 670 | TLSEXT_SIGALG_ecdsa_secp384r1_sha384, |
| 671 | TLSEXT_SIGALG_ecdsa_secp521r1_sha512, |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 672 | #endif |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 673 | |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 674 | TLSEXT_SIGALG_rsa_pss_sha256, |
| 675 | TLSEXT_SIGALG_rsa_pss_sha384, |
| 676 | TLSEXT_SIGALG_rsa_pss_sha512, |
| 677 | |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 678 | TLSEXT_SIGALG_rsa_pkcs1_sha256, |
| 679 | TLSEXT_SIGALG_rsa_pkcs1_sha384, |
| 680 | TLSEXT_SIGALG_rsa_pkcs1_sha512, |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 681 | |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 682 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 42ab223 | 2017-01-10 13:45:24 +0000 | [diff] [blame] | 683 | TLSEXT_SIGALG_ecdsa_sha1, |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 684 | #endif |
Matt Caswell | 42ab223 | 2017-01-10 13:45:24 +0000 | [diff] [blame] | 685 | TLSEXT_SIGALG_rsa_pkcs1_sha1, |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 686 | #ifndef OPENSSL_NO_DSA |
Matt Caswell | 42ab223 | 2017-01-10 13:45:24 +0000 | [diff] [blame] | 687 | TLSEXT_SIGALG_dsa_sha1, |
| 688 | |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 689 | TLSEXT_SIGALG_dsa_sha256, |
| 690 | TLSEXT_SIGALG_dsa_sha384, |
| 691 | TLSEXT_SIGALG_dsa_sha512 |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 692 | #endif |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 693 | }; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 694 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 695 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 696 | static const uint16_t suiteb_sigalgs[] = { |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 697 | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
| 698 | TLSEXT_SIGALG_ecdsa_secp384r1_sha384 |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 699 | }; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 700 | #endif |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 701 | |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 702 | static const SIGALG_LOOKUP sigalg_lookup_tbl[] = { |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 703 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 704 | {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 705 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 706 | NID_ecdsa_with_SHA256, NID_X9_62_prime256v1}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 707 | {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 708 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 709 | NID_ecdsa_with_SHA384, NID_secp384r1}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 710 | {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 711 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 712 | NID_ecdsa_with_SHA512, NID_secp521r1}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 713 | {NULL, TLSEXT_SIGALG_ecdsa_sha1, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 714 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
| 715 | NID_ecdsa_with_SHA1, NID_undef}, |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 716 | #endif |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 717 | {"rsa_pss_sha256", TLSEXT_SIGALG_rsa_pss_sha256, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 718 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
| 719 | NID_undef, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 720 | {"rsa_pss_sha384", TLSEXT_SIGALG_rsa_pss_sha384, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 721 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
| 722 | NID_undef, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 723 | {"rsa_pss_sha512", TLSEXT_SIGALG_rsa_pss_sha512, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 724 | NID_sha512, SSL_MD_SHA512_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_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256, |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 727 | 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] | 728 | NID_sha256WithRSAEncryption, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 729 | {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384, |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 730 | 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] | 731 | NID_sha384WithRSAEncryption, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 732 | {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512, |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 733 | 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] | 734 | NID_sha512WithRSAEncryption, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 735 | {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1, |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 736 | 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] | 737 | NID_sha1WithRSAEncryption, NID_undef}, |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 738 | #ifndef OPENSSL_NO_DSA |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 739 | {NULL, TLSEXT_SIGALG_dsa_sha256, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 740 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
| 741 | NID_dsa_with_SHA256, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 742 | {NULL, TLSEXT_SIGALG_dsa_sha384, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 743 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
| 744 | NID_undef, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 745 | {NULL, TLSEXT_SIGALG_dsa_sha512, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 746 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
| 747 | NID_undef, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 748 | {NULL, TLSEXT_SIGALG_dsa_sha1, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 749 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
| 750 | NID_dsaWithSHA1, NID_undef}, |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 751 | #endif |
| 752 | #ifndef OPENSSL_NO_GOST |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 753 | {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 754 | NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, |
| 755 | NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, |
| 756 | NID_undef, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 757 | {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 758 | NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, |
| 759 | NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, |
| 760 | NID_undef, NID_undef}, |
Dr. Stephen Henson | edbfba1 | 2017-01-26 14:23:05 +0000 | [diff] [blame] | 761 | {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411, |
Dr. Stephen Henson | 17ae384 | 2017-01-30 16:05:23 +0000 | [diff] [blame] | 762 | NID_id_GostR3411_94, SSL_MD_GOST94_IDX, |
| 763 | NID_id_GostR3410_2001, SSL_PKEY_GOST01, |
| 764 | NID_undef, NID_undef} |
Matt Caswell | 5eeb6c6 | 2017-01-10 14:38:09 +0000 | [diff] [blame] | 765 | #endif |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 766 | }; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 767 | /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */ |
| 768 | static const SIGALG_LOOKUP legacy_rsa_sigalg = { |
| 769 | "rsa_pkcs1_md5_sha1", 0, |
| 770 | NID_md5_sha1, SSL_MD_MD5_SHA1_IDX, |
| 771 | EVP_PKEY_RSA, SSL_PKEY_RSA, |
| 772 | NID_undef, NID_undef |
| 773 | }; |
| 774 | |
| 775 | /* |
| 776 | * Default signature algorithm values used if signature algorithms not present. |
| 777 | * From RFC5246. Note: order must match certificate index order. |
| 778 | */ |
| 779 | static const uint16_t tls_default_sigalg[] = { |
| 780 | TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */ |
| 781 | TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */ |
| 782 | TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */ |
| 783 | TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */ |
| 784 | TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, /* SSL_PKEY_GOST12_256 */ |
| 785 | TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512 /* SSL_PKEY_GOST12_512 */ |
| 786 | }; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 787 | |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 788 | /* Lookup TLS signature algorithm */ |
| 789 | static const SIGALG_LOOKUP *tls1_lookup_sigalg(uint16_t sigalg) |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 790 | { |
| 791 | size_t i; |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 792 | const SIGALG_LOOKUP *s; |
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 | for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); |
| 795 | i++, s++) { |
| 796 | if (s->sigalg == sigalg) |
| 797 | return s; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 798 | } |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 799 | return NULL; |
| 800 | } |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 801 | /* |
| 802 | * Return a signature algorithm for TLS < 1.2 where the signature type |
| 803 | * is fixed by the certificate type. |
| 804 | */ |
| 805 | static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx) |
| 806 | { |
| 807 | if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg)) |
| 808 | return NULL; |
| 809 | if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) { |
| 810 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(tls_default_sigalg[idx]); |
| 811 | |
| 812 | if (lu == NULL || ssl_md(lu->hash_idx) == NULL) { |
| 813 | return NULL; |
| 814 | } |
| 815 | return lu; |
| 816 | } |
| 817 | return &legacy_rsa_sigalg; |
| 818 | } |
| 819 | /* Set peer sigalg based key type */ |
| 820 | int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey) |
| 821 | { |
| 822 | int idx = ssl_cert_type(NULL, pkey); |
| 823 | |
| 824 | const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, idx); |
| 825 | if (lu == NULL) |
| 826 | return 0; |
| 827 | s->s3->tmp.peer_sigalg = lu; |
| 828 | return 1; |
| 829 | } |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 830 | |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 831 | 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] | 832 | { |
| 833 | /* |
| 834 | * If Suite B mode use Suite B sigalgs only, ignore any other |
| 835 | * preferences. |
| 836 | */ |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 837 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 838 | switch (tls1_suiteb(s)) { |
| 839 | case SSL_CERT_FLAG_SUITEB_128_LOS: |
| 840 | *psigs = suiteb_sigalgs; |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 841 | return OSSL_NELEM(suiteb_sigalgs); |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 842 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 843 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: |
| 844 | *psigs = suiteb_sigalgs; |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 845 | return 1; |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 846 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 847 | case SSL_CERT_FLAG_SUITEB_192_LOS: |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 848 | *psigs = suiteb_sigalgs + 1; |
| 849 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 850 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 851 | #endif |
Dr. Stephen Henson | a9669dd | 2017-01-25 19:12:48 +0000 | [diff] [blame] | 852 | /* |
| 853 | * We use client_sigalgs (if not NULL) if we're a server |
| 854 | * and sending a certificate request or if we're a client and |
| 855 | * determining which shared algorithm to use. |
| 856 | */ |
| 857 | if ((s->server == sent) && s->cert->client_sigalgs != NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 858 | *psigs = s->cert->client_sigalgs; |
| 859 | return s->cert->client_sigalgslen; |
| 860 | } else if (s->cert->conf_sigalgs) { |
| 861 | *psigs = s->cert->conf_sigalgs; |
| 862 | return s->cert->conf_sigalgslen; |
| 863 | } else { |
| 864 | *psigs = tls12_sigalgs; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 865 | return OSSL_NELEM(tls12_sigalgs); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Check signature algorithm is consistent with sent supported signature |
Dr. Stephen Henson | b2eb699 | 2017-01-25 16:22:13 +0000 | [diff] [blame] | 871 | * algorithms and if so set relevant digest and signature scheme in |
| 872 | * s. |
Dr. Stephen Henson | ec4a50b | 2012-07-24 18:11:27 +0000 | [diff] [blame] | 873 | */ |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 874 | 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] | 875 | { |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 876 | const uint16_t *sent_sigs; |
Dr. Stephen Henson | 5554fac | 2017-01-25 16:46:02 +0000 | [diff] [blame] | 877 | const EVP_MD *md = NULL; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 878 | char sigalgstr[2]; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 879 | size_t sent_sigslen, i; |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 880 | int pkeyid = EVP_PKEY_id(pkey); |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 881 | const SIGALG_LOOKUP *lu; |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 882 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 883 | /* Should never happen */ |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 884 | if (pkeyid == -1) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 885 | return -1; |
Dr. Stephen Henson | 5a8916d | 2017-02-17 16:08:19 +0000 | [diff] [blame] | 886 | if (SSL_IS_TLS13(s)) { |
| 887 | /* Disallow DSA for TLS 1.3 */ |
| 888 | if (pkeyid == EVP_PKEY_DSA) { |
| 889 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
| 890 | return 0; |
| 891 | } |
| 892 | /* Only allow PSS for TLS 1.3 */ |
| 893 | if (pkeyid == EVP_PKEY_RSA) |
| 894 | pkeyid = EVP_PKEY_RSA_PSS; |
| 895 | } |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 896 | lu = tls1_lookup_sigalg(sig); |
| 897 | /* |
Dr. Stephen Henson | 095a982 | 2017-02-25 00:25:04 +0000 | [diff] [blame] | 898 | * Check sigalgs is known. Disallow SHA1 with TLS 1.3. Check key type is |
| 899 | * consistent with signature: RSA keys can be used for RSA-PSS |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 900 | */ |
Dr. Stephen Henson | 095a982 | 2017-02-25 00:25:04 +0000 | [diff] [blame] | 901 | if (lu == NULL || (SSL_IS_TLS13(s) && lu->hash == NID_sha1) |
| 902 | || (pkeyid != lu->sig |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 903 | && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 904 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
| 905 | return 0; |
| 906 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 907 | #ifndef OPENSSL_NO_EC |
Matt Caswell | fe3066e | 2017-01-03 10:01:39 +0000 | [diff] [blame] | 908 | if (pkeyid == EVP_PKEY_EC) { |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 909 | EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey); |
Dr. Stephen Henson | f1adb00 | 2017-01-30 22:02:11 +0000 | [diff] [blame] | 910 | 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] | 911 | |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 912 | if (SSL_IS_TLS13(s)) { |
Dr. Stephen Henson | e892e32 | 2017-02-24 20:43:02 +0000 | [diff] [blame] | 913 | if (EC_KEY_get_conv_form(ec) != POINT_CONVERSION_UNCOMPRESSED) { |
| 914 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, |
| 915 | SSL_R_ILLEGAL_POINT_COMPRESSION); |
| 916 | return 0; |
| 917 | } |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 918 | /* For TLS 1.3 check curve matches signature algorithm */ |
Dr. Stephen Henson | a34a9df | 2017-02-14 14:27:15 +0000 | [diff] [blame] | 919 | if (lu->curve != NID_undef && curve != lu->curve) { |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 920 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 921 | return 0; |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 922 | } |
| 923 | } else { |
| 924 | unsigned char curve_id[2], comp_id; |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 925 | |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 926 | /* Check compression and curve matches extensions */ |
| 927 | if (!tls1_set_ec_id(curve_id, &comp_id, ec)) |
| 928 | return 0; |
| 929 | if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) { |
| 930 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); |
| 931 | return 0; |
| 932 | } |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 933 | if (tls1_suiteb(s)) { |
Dr. Stephen Henson | f1adb00 | 2017-01-30 22:02:11 +0000 | [diff] [blame] | 934 | /* Check sigalg matches a permissible Suite B value */ |
| 935 | if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256 |
| 936 | && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) { |
| 937 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, |
| 938 | SSL_R_WRONG_SIGNATURE_TYPE); |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 939 | return 0; |
Dr. Stephen Henson | f1adb00 | 2017-01-30 22:02:11 +0000 | [diff] [blame] | 940 | } |
| 941 | /* |
| 942 | * Suite B also requires P-256+SHA256 and P-384+SHA384: |
| 943 | * this matches the TLS 1.3 requirements so we can just |
| 944 | * check the curve is the expected TLS 1.3 value. |
| 945 | * If this fails an inappropriate digest is being used. |
| 946 | */ |
| 947 | if (curve != lu->curve) { |
| 948 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, |
| 949 | SSL_R_ILLEGAL_SUITEB_DIGEST); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 950 | return 0; |
| 951 | } |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 952 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 953 | } |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 954 | } else if (tls1_suiteb(s)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 955 | return 0; |
Dr. Stephen Henson | 8f88cb5 | 2017-01-30 14:45:20 +0000 | [diff] [blame] | 956 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 957 | #endif |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 958 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 959 | /* Check signature matches a type we sent */ |
Dr. Stephen Henson | a9669dd | 2017-01-25 19:12:48 +0000 | [diff] [blame] | 960 | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 961 | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 962 | if (sig == *sent_sigs) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 963 | break; |
| 964 | } |
| 965 | /* Allow fallback to SHA1 if not strict mode */ |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 966 | if (i == sent_sigslen && (lu->hash != NID_sha1 |
| 967 | || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 968 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
| 969 | return 0; |
| 970 | } |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 971 | md = ssl_md(lu->hash_idx); |
Dr. Stephen Henson | 5554fac | 2017-01-25 16:46:02 +0000 | [diff] [blame] | 972 | if (md == NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 973 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST); |
| 974 | return 0; |
| 975 | } |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 976 | /* |
| 977 | * Make sure security callback allows algorithm. For historical reasons we |
| 978 | * have to pass the sigalg as a two byte char array. |
| 979 | */ |
| 980 | sigalgstr[0] = (sig >> 8) & 0xff; |
| 981 | sigalgstr[1] = sig & 0xff; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 982 | if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK, |
Dr. Stephen Henson | 5554fac | 2017-01-25 16:46:02 +0000 | [diff] [blame] | 983 | EVP_MD_size(md) * 4, EVP_MD_type(md), |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 984 | (void *)sigalgstr)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 985 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
| 986 | return 0; |
| 987 | } |
Dr. Stephen Henson | 6cbebb5 | 2017-01-30 18:10:17 +0000 | [diff] [blame] | 988 | /* Store the sigalg the peer uses */ |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 989 | s->s3->tmp.peer_sigalg = lu; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 990 | return 1; |
| 991 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 992 | |
Dr. Stephen Henson | 42ef7ae | 2017-01-25 23:28:57 +0000 | [diff] [blame] | 993 | int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid) |
| 994 | { |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 995 | if (s->s3->tmp.peer_sigalg == NULL) |
Dr. Stephen Henson | 42ef7ae | 2017-01-25 23:28:57 +0000 | [diff] [blame] | 996 | return 0; |
Dr. Stephen Henson | f742cda | 2017-01-30 13:48:39 +0000 | [diff] [blame] | 997 | *pnid = s->s3->tmp.peer_sigalg->sig; |
Dr. Stephen Henson | 42ef7ae | 2017-01-25 23:28:57 +0000 | [diff] [blame] | 998 | return 1; |
| 999 | } |
| 1000 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1001 | /* |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 1002 | * Set a mask of disabled algorithms: an algorithm is disabled if it isn't |
| 1003 | * supported, doesn't appear in supported signature algorithms, isn't supported |
| 1004 | * by the enabled protocol versions or by the security level. |
| 1005 | * |
| 1006 | * This function should only be used for checking which ciphers are supported |
| 1007 | * by the client. |
| 1008 | * |
| 1009 | * 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] | 1010 | */ |
| 1011 | void ssl_set_client_disabled(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1012 | { |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 1013 | s->s3->tmp.mask_a = 0; |
| 1014 | s->s3->tmp.mask_k = 0; |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 1015 | 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] | 1016 | 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] | 1017 | #ifndef OPENSSL_NO_PSK |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1018 | /* with PSK there must be client callback set */ |
| 1019 | if (!s->psk_client_callback) { |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 1020 | s->s3->tmp.mask_a |= SSL_aPSK; |
Dr. Stephen Henson | fe5eef3 | 2015-06-28 17:01:07 +0100 | [diff] [blame] | 1021 | s->s3->tmp.mask_k |= SSL_PSK; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1022 | } |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1023 | #endif /* OPENSSL_NO_PSK */ |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1024 | #ifndef OPENSSL_NO_SRP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1025 | if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) { |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 1026 | s->s3->tmp.mask_a |= SSL_aSRP; |
| 1027 | s->s3->tmp.mask_k |= SSL_kSRP; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1028 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1029 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1030 | } |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 1031 | |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 1032 | /* |
| 1033 | * ssl_cipher_disabled - check that a cipher is disabled or not |
| 1034 | * @s: SSL connection that you want to use the cipher on |
| 1035 | * @c: cipher to check |
| 1036 | * @op: Security check that you want to do |
| 1037 | * |
| 1038 | * Returns 1 when it's disabled, 0 when enabled. |
| 1039 | */ |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1040 | int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1041 | { |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 1042 | if (c->algorithm_mkey & s->s3->tmp.mask_k |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 1043 | || c->algorithm_auth & s->s3->tmp.mask_a) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1044 | return 1; |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 1045 | if (s->s3->tmp.max_ver == 0) |
| 1046 | return 1; |
| 1047 | 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] | 1048 | || (c->max_tls < s->s3->tmp.min_ver))) |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 1049 | return 1; |
| 1050 | 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] | 1051 | || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver))) |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 1052 | return 1; |
| 1053 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1054 | return !ssl_security(s, op, c->strength_bits, 0, (void *)c); |
| 1055 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1056 | |
Matt Caswell | 7da160b | 2016-11-25 10:22:02 +0000 | [diff] [blame] | 1057 | int tls_use_ticket(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1058 | { |
Matt Caswell | 0819129 | 2017-01-19 11:23:06 +0000 | [diff] [blame] | 1059 | if ((s->options & SSL_OP_NO_TICKET)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1060 | return 0; |
| 1061 | return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL); |
| 1062 | } |
Dr. Stephen Henson | 8b8e5be | 2014-01-14 14:55:21 +0000 | [diff] [blame] | 1063 | |
Dr. Stephen Henson | e469af8 | 2014-11-17 16:52:59 +0000 | [diff] [blame] | 1064 | int tls1_set_server_sigalgs(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1065 | { |
| 1066 | int al; |
| 1067 | size_t i; |
FdaSilvaYY | 8483a00 | 2016-03-10 21:34:48 +0100 | [diff] [blame] | 1068 | |
| 1069 | /* Clear any shared signature algorithms */ |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 1070 | OPENSSL_free(s->cert->shared_sigalgs); |
| 1071 | s->cert->shared_sigalgs = NULL; |
| 1072 | s->cert->shared_sigalgslen = 0; |
Dr. Stephen Henson | 9195ddc | 2017-02-18 03:42:15 +0000 | [diff] [blame] | 1073 | /* Clear certificate validity flags */ |
| 1074 | for (i = 0; i < SSL_PKEY_NUM; i++) |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 1075 | s->s3->tmp.valid_flags[i] = 0; |
Dr. Stephen Henson | a8bb912 | 2017-02-19 18:47:16 +0000 | [diff] [blame] | 1076 | /* |
| 1077 | * If peer sent no signature algorithms check to see if we support |
| 1078 | * the default algorithm for each certificate type |
| 1079 | */ |
| 1080 | if (s->s3->tmp.peer_sigalgs == NULL) { |
| 1081 | const uint16_t *sent_sigs; |
| 1082 | size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
Dr. Stephen Henson | e469af8 | 2014-11-17 16:52:59 +0000 | [diff] [blame] | 1083 | |
Dr. Stephen Henson | a8bb912 | 2017-02-19 18:47:16 +0000 | [diff] [blame] | 1084 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 1085 | const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i); |
| 1086 | size_t j; |
| 1087 | |
| 1088 | if (lu == NULL) |
| 1089 | continue; |
| 1090 | /* Check default matches a type we sent */ |
| 1091 | for (j = 0; j < sent_sigslen; j++) { |
| 1092 | if (lu->sigalg == sent_sigs[j]) { |
| 1093 | s->s3->tmp.valid_flags[i] = CERT_PKEY_SIGN; |
| 1094 | break; |
| 1095 | } |
| 1096 | } |
| 1097 | } |
Dr. Stephen Henson | 9195ddc | 2017-02-18 03:42:15 +0000 | [diff] [blame] | 1098 | return 1; |
Dr. Stephen Henson | a8bb912 | 2017-02-19 18:47:16 +0000 | [diff] [blame] | 1099 | } |
Dr. Stephen Henson | 9195ddc | 2017-02-18 03:42:15 +0000 | [diff] [blame] | 1100 | |
| 1101 | if (!tls1_process_sigalgs(s)) { |
| 1102 | SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE); |
| 1103 | al = SSL_AD_INTERNAL_ERROR; |
| 1104 | goto err; |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 1105 | } |
Dr. Stephen Henson | 9195ddc | 2017-02-18 03:42:15 +0000 | [diff] [blame] | 1106 | if (s->cert->shared_sigalgs != NULL) |
| 1107 | return 1; |
| 1108 | /* Fatal error is no shared signature algorithms */ |
| 1109 | SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS); |
| 1110 | al = SSL_AD_ILLEGAL_PARAMETER; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1111 | err: |
| 1112 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 1113 | return 0; |
| 1114 | } |
Dr. Stephen Henson | e469af8 | 2014-11-17 16:52:59 +0000 | [diff] [blame] | 1115 | |
Tim Hudson | 1d97c84 | 2014-12-28 12:48:40 +1000 | [diff] [blame] | 1116 | /*- |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1117 | * Gets the ticket information supplied by the client if any. |
Dr. Stephen Henson | e7f0d92 | 2015-12-04 19:48:15 +0000 | [diff] [blame] | 1118 | * |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1119 | * hello: The parsed ClientHello data |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 1120 | * ret: (output) on return, if a ticket was decrypted, then this is set to |
| 1121 | * point to the resulting session. |
| 1122 | * |
| 1123 | * If s->tls_session_secret_cb is set then we are expecting a pre-shared key |
| 1124 | * 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] | 1125 | * 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] | 1126 | * |
| 1127 | * Returns: |
| 1128 | * -1: fatal error, either from parsing or decrypting the ticket. |
| 1129 | * 0: no ticket was found (or was ignored, based on settings). |
| 1130 | * 1: a zero length extension was found, indicating that the client supports |
| 1131 | * session tickets but doesn't currently have one to offer. |
| 1132 | * 2: either s->tls_session_secret_cb was set, or a ticket was offered but |
| 1133 | * couldn't be decrypted because of a non-fatal error. |
| 1134 | * 3: a ticket was successfully decrypted and *ret was set. |
| 1135 | * |
| 1136 | * Side effects: |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1137 | * 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] | 1138 | * a new session ticket to the client because the client indicated support |
| 1139 | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have |
| 1140 | * 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] | 1141 | * s->ctx->ext.ticket_key_cb asked to renew the client's ticket. |
| 1142 | * Otherwise, s->ext.ticket_expected is set to 0. |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 1143 | */ |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1144 | TICKET_RETURN tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello, |
| 1145 | SSL_SESSION **ret) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1146 | { |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1147 | int retv; |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1148 | size_t size; |
| 1149 | RAW_EXTENSION *ticketext; |
Dr. Stephen Henson | e7f0d92 | 2015-12-04 19:48:15 +0000 | [diff] [blame] | 1150 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1151 | *ret = NULL; |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1152 | s->ext.ticket_expected = 0; |
Dr. Stephen Henson | e8da6a1 | 2008-09-03 22:17:11 +0000 | [diff] [blame] | 1153 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1154 | /* |
Matt Caswell | 9362c93 | 2016-11-08 16:10:21 +0000 | [diff] [blame] | 1155 | * If tickets disabled or not supported by the protocol version |
| 1156 | * (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] | 1157 | * resumption. |
| 1158 | */ |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1159 | if (s->version <= SSL3_VERSION || !tls_use_ticket(s)) |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1160 | return TICKET_NONE; |
Matt Caswell | 9ceb242 | 2015-04-16 10:06:25 +0100 | [diff] [blame] | 1161 | |
Matt Caswell | 70af3d8 | 2016-11-28 09:31:59 +0000 | [diff] [blame] | 1162 | ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket]; |
| 1163 | if (!ticketext->present) |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1164 | return TICKET_NONE; |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1165 | |
| 1166 | size = PACKET_remaining(&ticketext->data); |
| 1167 | if (size == 0) { |
| 1168 | /* |
| 1169 | * The client will accept a ticket but doesn't currently have |
| 1170 | * one. |
| 1171 | */ |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1172 | s->ext.ticket_expected = 1; |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1173 | return TICKET_EMPTY; |
Matt Caswell | 9ceb242 | 2015-04-16 10:06:25 +0100 | [diff] [blame] | 1174 | } |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1175 | if (s->ext.session_secret_cb) { |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1176 | /* |
| 1177 | * Indicate that the ticket couldn't be decrypted rather than |
| 1178 | * generating the session from ticket now, trigger |
| 1179 | * abbreviated handshake based on external mechanism to |
| 1180 | * calculate the master secret later. |
| 1181 | */ |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1182 | return TICKET_NO_DECRYPT; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1183 | } |
Matt Caswell | 70af3d8 | 2016-11-28 09:31:59 +0000 | [diff] [blame] | 1184 | |
| 1185 | retv = tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size, |
| 1186 | hello->session_id, hello->session_id_len, ret); |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1187 | switch (retv) { |
Matt Caswell | 61c3264 | 2017-01-27 12:11:23 +0000 | [diff] [blame] | 1188 | case TICKET_NO_DECRYPT: |
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_NO_DECRYPT; |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1191 | |
Matt Caswell | 61c3264 | 2017-01-27 12:11:23 +0000 | [diff] [blame] | 1192 | case TICKET_SUCCESS: |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1193 | return TICKET_SUCCESS; |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1194 | |
Matt Caswell | 61c3264 | 2017-01-27 12:11:23 +0000 | [diff] [blame] | 1195 | case TICKET_SUCCESS_RENEW: |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1196 | s->ext.ticket_expected = 1; |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1197 | return TICKET_SUCCESS; |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1198 | |
Matt Caswell | 61c3264 | 2017-01-27 12:11:23 +0000 | [diff] [blame] | 1199 | default: |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1200 | return TICKET_FATAL_ERR_OTHER; |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | |
Tim Hudson | 1d97c84 | 2014-12-28 12:48:40 +1000 | [diff] [blame] | 1204 | /*- |
| 1205 | * tls_decrypt_ticket attempts to decrypt a session ticket. |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 1206 | * |
| 1207 | * etick: points to the body of the session ticket extension. |
FdaSilvaYY | 8483a00 | 2016-03-10 21:34:48 +0100 | [diff] [blame] | 1208 | * eticklen: the length of the session tickets extension. |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 1209 | * sess_id: points at the session ID. |
| 1210 | * sesslen: the length of the session ID. |
| 1211 | * psess: (output) on return, if a ticket was decrypted, then this is set to |
| 1212 | * point to the resulting session. |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 1213 | */ |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1214 | TICKET_RETURN tls_decrypt_ticket(SSL *s, const unsigned char *etick, |
| 1215 | size_t eticklen, const unsigned char *sess_id, |
| 1216 | size_t sesslen, SSL_SESSION **psess) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1217 | { |
| 1218 | SSL_SESSION *sess; |
| 1219 | unsigned char *sdec; |
| 1220 | const unsigned char *p; |
Matt Caswell | ddf6ec0 | 2017-01-20 16:01:27 +0000 | [diff] [blame] | 1221 | int slen, renew_ticket = 0, declen; |
| 1222 | TICKET_RETURN ret = TICKET_FATAL_ERR_OTHER; |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1223 | size_t mlen; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1224 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 1225 | HMAC_CTX *hctx = NULL; |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1226 | EVP_CIPHER_CTX *ctx; |
Todd Short | 222da97 | 2017-01-31 15:32:50 -0500 | [diff] [blame] | 1227 | SSL_CTX *tctx = s->session_ctx; |
Dr. Stephen Henson | e97763c | 2016-08-22 17:20:01 +0100 | [diff] [blame] | 1228 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1229 | /* Initialize session ticket encryption and HMAC contexts */ |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 1230 | hctx = HMAC_CTX_new(); |
| 1231 | if (hctx == NULL) |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1232 | return TICKET_FATAL_ERR_MALLOC; |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1233 | ctx = EVP_CIPHER_CTX_new(); |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 1234 | if (ctx == NULL) { |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1235 | ret = TICKET_FATAL_ERR_MALLOC; |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 1236 | goto err; |
| 1237 | } |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1238 | if (tctx->ext.ticket_key_cb) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1239 | unsigned char *nctick = (unsigned char *)etick; |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1240 | int rv = tctx->ext.ticket_key_cb(s, nctick, nctick + 16, |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1241 | ctx, hctx, 0); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1242 | if (rv < 0) |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 1243 | goto err; |
| 1244 | if (rv == 0) { |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1245 | ret = TICKET_NO_DECRYPT; |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 1246 | goto err; |
| 1247 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1248 | if (rv == 2) |
| 1249 | renew_ticket = 1; |
| 1250 | } else { |
| 1251 | /* Check key name matches */ |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1252 | if (memcmp(etick, tctx->ext.tick_key_name, |
| 1253 | sizeof(tctx->ext.tick_key_name)) != 0) { |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1254 | ret = TICKET_NO_DECRYPT; |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 1255 | goto err; |
| 1256 | } |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1257 | if (HMAC_Init_ex(hctx, tctx->ext.tick_hmac_key, |
| 1258 | sizeof(tctx->ext.tick_hmac_key), |
Matt Caswell | 5f3d93e | 2015-11-06 16:31:21 +0000 | [diff] [blame] | 1259 | EVP_sha256(), NULL) <= 0 |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1260 | || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, |
Rich Salz | aff8c12 | 2016-12-08 14:18:40 -0500 | [diff] [blame] | 1261 | tctx->ext.tick_aes_key, |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1262 | etick |
| 1263 | + sizeof(tctx->ext.tick_key_name)) <= 0) { |
Matt Caswell | 5f3d93e | 2015-11-06 16:31:21 +0000 | [diff] [blame] | 1264 | goto err; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1265 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1266 | } |
| 1267 | /* |
| 1268 | * Attempt to process session ticket, first conduct sanity and integrity |
| 1269 | * checks on ticket. |
| 1270 | */ |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 1271 | mlen = HMAC_size(hctx); |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1272 | if (mlen == 0) { |
Matt Caswell | 5f3d93e | 2015-11-06 16:31:21 +0000 | [diff] [blame] | 1273 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1274 | } |
Dr. Stephen Henson | e97763c | 2016-08-22 17:20:01 +0100 | [diff] [blame] | 1275 | /* Sanity check ticket length: must exceed keyname + IV + HMAC */ |
| 1276 | if (eticklen <= |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1277 | TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) { |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1278 | ret = TICKET_NO_DECRYPT; |
Dr. Stephen Henson | e97763c | 2016-08-22 17:20:01 +0100 | [diff] [blame] | 1279 | goto err; |
| 1280 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1281 | eticklen -= mlen; |
| 1282 | /* Check HMAC of encrypted ticket */ |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 1283 | if (HMAC_Update(hctx, etick, eticklen) <= 0 |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1284 | || HMAC_Final(hctx, tick_hmac, NULL) <= 0) { |
Matt Caswell | 5f3d93e | 2015-11-06 16:31:21 +0000 | [diff] [blame] | 1285 | goto err; |
| 1286 | } |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 1287 | HMAC_CTX_free(hctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1288 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1289 | EVP_CIPHER_CTX_free(ctx); |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1290 | return TICKET_NO_DECRYPT; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1291 | } |
| 1292 | /* Attempt to decrypt session data */ |
| 1293 | /* Move p after IV to start of encrypted ticket, update length */ |
Bernd Edlinger | 57b0d65 | 2017-02-13 18:36:13 +0100 | [diff] [blame] | 1294 | p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx); |
| 1295 | eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1296 | sdec = OPENSSL_malloc(eticklen); |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1297 | if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, |
| 1298 | (int)eticklen) <= 0) { |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1299 | EVP_CIPHER_CTX_free(ctx); |
Matt Caswell | d1247df | 2016-03-15 11:38:56 +0000 | [diff] [blame] | 1300 | OPENSSL_free(sdec); |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1301 | return TICKET_FATAL_ERR_OTHER; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1302 | } |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1303 | if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) { |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1304 | EVP_CIPHER_CTX_free(ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1305 | OPENSSL_free(sdec); |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1306 | return TICKET_NO_DECRYPT; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1307 | } |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1308 | slen += declen; |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1309 | EVP_CIPHER_CTX_free(ctx); |
| 1310 | ctx = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1311 | p = sdec; |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 1312 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1313 | sess = d2i_SSL_SESSION(NULL, &p, slen); |
| 1314 | OPENSSL_free(sdec); |
| 1315 | if (sess) { |
Bernd Edlinger | 79020b2 | 2017-02-22 11:59:44 +0100 | [diff] [blame] | 1316 | /* Some additional consistency checks */ |
| 1317 | if (p != sdec + slen || sess->session_id_length != 0) { |
| 1318 | SSL_SESSION_free(sess); |
| 1319 | return 2; |
| 1320 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1321 | /* |
| 1322 | * The session ID, if non-empty, is used by some clients to detect |
| 1323 | * that the ticket has been accepted. So we copy it to the session |
| 1324 | * structure. If it is empty set length to zero as required by |
| 1325 | * standard. |
| 1326 | */ |
| 1327 | if (sesslen) |
| 1328 | memcpy(sess->session_id, sess_id, sesslen); |
| 1329 | sess->session_id_length = sesslen; |
| 1330 | *psess = sess; |
| 1331 | if (renew_ticket) |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1332 | return TICKET_SUCCESS_RENEW; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1333 | else |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1334 | return TICKET_SUCCESS; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1335 | } |
| 1336 | ERR_clear_error(); |
| 1337 | /* |
| 1338 | * For session parse failure, indicate that we need to send a new ticket. |
| 1339 | */ |
Matt Caswell | 1053a6e | 2017-01-18 16:28:23 +0000 | [diff] [blame] | 1340 | return TICKET_NO_DECRYPT; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1341 | err: |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 1342 | EVP_CIPHER_CTX_free(ctx); |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 1343 | HMAC_CTX_free(hctx); |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 1344 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1345 | } |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 1346 | |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1347 | static int tls12_get_pkey_idx(int sig_nid) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1348 | { |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1349 | switch (sig_nid) { |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1350 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1351 | case EVP_PKEY_RSA: |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 1352 | return SSL_PKEY_RSA; |
Dr. Stephen Henson | b2eb699 | 2017-01-25 16:22:13 +0000 | [diff] [blame] | 1353 | /* |
| 1354 | * For now return RSA key for PSS. When we support PSS only keys |
| 1355 | * this will need to be updated. |
| 1356 | */ |
| 1357 | case EVP_PKEY_RSA_PSS: |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 1358 | return SSL_PKEY_RSA; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1359 | #endif |
| 1360 | #ifndef OPENSSL_NO_DSA |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1361 | case EVP_PKEY_DSA: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1362 | return SSL_PKEY_DSA_SIGN; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1363 | #endif |
| 1364 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1365 | case EVP_PKEY_EC: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1366 | return SSL_PKEY_ECC; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1367 | #endif |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1368 | #ifndef OPENSSL_NO_GOST |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1369 | case NID_id_GostR3410_2001: |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1370 | return SSL_PKEY_GOST01; |
| 1371 | |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1372 | case NID_id_GostR3410_2012_256: |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1373 | return SSL_PKEY_GOST12_256; |
| 1374 | |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1375 | case NID_id_GostR3410_2012_512: |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1376 | return SSL_PKEY_GOST12_512; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1377 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1378 | } |
| 1379 | return -1; |
| 1380 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 1381 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1382 | /* Check to see if a signature algorithm is allowed */ |
Dr. Stephen Henson | b0e9ab9 | 2017-03-03 03:10:13 +0000 | [diff] [blame^] | 1383 | static int tls12_sigalg_allowed(SSL *s, int op, const SIGALG_LOOKUP *lu) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1384 | { |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1385 | unsigned char sigalgstr[2]; |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1386 | int secbits; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1387 | |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1388 | /* See if sigalgs is recognised and if hash is enabled */ |
| 1389 | if (lu == NULL || ssl_md(lu->hash_idx) == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1390 | return 0; |
Dr. Stephen Henson | 224b4e3 | 2017-03-01 17:15:43 +0000 | [diff] [blame] | 1391 | /* DSA is not allowed in TLS 1.3 */ |
| 1392 | if (SSL_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA) |
| 1393 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1394 | /* See if public key algorithm allowed */ |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1395 | if (tls12_get_pkey_idx(lu->sig) == -1) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1396 | return 0; |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1397 | /* Security bits: half digest bits */ |
| 1398 | secbits = EVP_MD_size(ssl_md(lu->hash_idx)) * 4; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1399 | /* Finally see if security callback allows it */ |
Dr. Stephen Henson | b0e9ab9 | 2017-03-03 03:10:13 +0000 | [diff] [blame^] | 1400 | sigalgstr[0] = (lu->sigalg >> 8) & 0xff; |
| 1401 | sigalgstr[1] = lu->sigalg & 0xff; |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1402 | return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1403 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1404 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1405 | /* |
| 1406 | * Get a mask of disabled public key algorithms based on supported signature |
| 1407 | * algorithms. For example if no signature algorithm supports RSA then RSA is |
| 1408 | * disabled. |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1409 | */ |
| 1410 | |
Dr. Stephen Henson | 90d9e49 | 2015-11-05 16:14:17 +0000 | [diff] [blame] | 1411 | 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] | 1412 | { |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1413 | const uint16_t *sigalgs; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1414 | size_t i, sigalgslen; |
| 1415 | int have_rsa = 0, have_dsa = 0, have_ecdsa = 0; |
| 1416 | /* |
| 1417 | * Now go through all signature algorithms seeing if we support any for |
| 1418 | * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep |
| 1419 | * down calls to security callback only check if we have to. |
| 1420 | */ |
Dr. Stephen Henson | a9669dd | 2017-01-25 19:12:48 +0000 | [diff] [blame] | 1421 | sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs); |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1422 | for (i = 0; i < sigalgslen; i ++, sigalgs++) { |
Dr. Stephen Henson | b0e9ab9 | 2017-03-03 03:10:13 +0000 | [diff] [blame^] | 1423 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*sigalgs); |
| 1424 | |
| 1425 | if (lu == NULL) |
| 1426 | continue; |
| 1427 | switch (lu->sig) { |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1428 | #ifndef OPENSSL_NO_RSA |
Dr. Stephen Henson | b2eb699 | 2017-01-25 16:22:13 +0000 | [diff] [blame] | 1429 | /* Any RSA-PSS signature algorithms also mean we allow RSA */ |
| 1430 | case EVP_PKEY_RSA_PSS: |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1431 | case EVP_PKEY_RSA: |
Dr. Stephen Henson | b0e9ab9 | 2017-03-03 03:10:13 +0000 | [diff] [blame^] | 1432 | if (!have_rsa && tls12_sigalg_allowed(s, op, lu)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1433 | have_rsa = 1; |
| 1434 | break; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1435 | #endif |
| 1436 | #ifndef OPENSSL_NO_DSA |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1437 | case EVP_PKEY_DSA: |
Dr. Stephen Henson | b0e9ab9 | 2017-03-03 03:10:13 +0000 | [diff] [blame^] | 1438 | if (!have_dsa && tls12_sigalg_allowed(s, op, lu)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1439 | have_dsa = 1; |
| 1440 | break; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1441 | #endif |
| 1442 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1443 | case EVP_PKEY_EC: |
Dr. Stephen Henson | b0e9ab9 | 2017-03-03 03:10:13 +0000 | [diff] [blame^] | 1444 | if (!have_ecdsa && tls12_sigalg_allowed(s, op, lu)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1445 | have_ecdsa = 1; |
| 1446 | break; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1447 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | if (!have_rsa) |
| 1451 | *pmask_a |= SSL_aRSA; |
| 1452 | if (!have_dsa) |
| 1453 | *pmask_a |= SSL_aDSS; |
| 1454 | if (!have_ecdsa) |
| 1455 | *pmask_a |= SSL_aECDSA; |
| 1456 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1457 | |
Matt Caswell | ae2f7b3 | 2016-09-05 17:34:04 +0100 | [diff] [blame] | 1458 | int tls12_copy_sigalgs(SSL *s, WPACKET *pkt, |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1459 | const uint16_t *psig, size_t psiglen) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1460 | { |
| 1461 | size_t i; |
Dr. Stephen Henson | b0e9ab9 | 2017-03-03 03:10:13 +0000 | [diff] [blame^] | 1462 | int rv = 0; |
Matt Caswell | c0f9e23 | 2016-09-13 09:40:38 +0100 | [diff] [blame] | 1463 | |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1464 | for (i = 0; i < psiglen; i++, psig++) { |
Dr. Stephen Henson | b0e9ab9 | 2017-03-03 03:10:13 +0000 | [diff] [blame^] | 1465 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*psig); |
| 1466 | |
| 1467 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu)) |
| 1468 | continue; |
| 1469 | if (!WPACKET_put_bytes_u16(pkt, *psig)) |
| 1470 | return 0; |
| 1471 | /* |
| 1472 | * If TLS 1.3 must have at least one valid TLS 1.3 message |
| 1473 | * signing algorithm: i.e. neither RSA nor SHA1 |
| 1474 | */ |
| 1475 | if (rv == 0 && (!SSL_IS_TLS13(s) |
| 1476 | || (lu->sig != EVP_PKEY_RSA && lu->hash != NID_sha1))) |
| 1477 | rv = 1; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1478 | } |
Dr. Stephen Henson | b0e9ab9 | 2017-03-03 03:10:13 +0000 | [diff] [blame^] | 1479 | return rv; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1480 | } |
| 1481 | |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 1482 | /* Given preference and allowed sigalgs set shared sigalgs */ |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1483 | 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] | 1484 | const uint16_t *pref, size_t preflen, |
| 1485 | const uint16_t *allow, size_t allowlen) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1486 | { |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1487 | const uint16_t *ptmp, *atmp; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1488 | size_t i, j, nmatch = 0; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1489 | for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) { |
Dr. Stephen Henson | b0e9ab9 | 2017-03-03 03:10:13 +0000 | [diff] [blame^] | 1490 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*ptmp); |
| 1491 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1492 | /* Skip disabled hashes or signature algorithms */ |
Dr. Stephen Henson | b0e9ab9 | 2017-03-03 03:10:13 +0000 | [diff] [blame^] | 1493 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1494 | continue; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1495 | for (j = 0, atmp = allow; j < allowlen; j++, atmp++) { |
| 1496 | if (*ptmp == *atmp) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1497 | nmatch++; |
Dr. Stephen Henson | b0e9ab9 | 2017-03-03 03:10:13 +0000 | [diff] [blame^] | 1498 | if (shsig) |
| 1499 | *shsig++ = lu; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1500 | break; |
| 1501 | } |
| 1502 | } |
| 1503 | } |
| 1504 | return nmatch; |
| 1505 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 1506 | |
| 1507 | /* Set shared signature algorithms for SSL structures */ |
| 1508 | static int tls1_set_shared_sigalgs(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1509 | { |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1510 | const uint16_t *pref, *allow, *conf; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1511 | size_t preflen, allowlen, conflen; |
| 1512 | size_t nmatch; |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1513 | const SIGALG_LOOKUP **salgs = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1514 | CERT *c = s->cert; |
| 1515 | unsigned int is_suiteb = tls1_suiteb(s); |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 1516 | |
| 1517 | OPENSSL_free(c->shared_sigalgs); |
| 1518 | c->shared_sigalgs = NULL; |
| 1519 | c->shared_sigalgslen = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1520 | /* If client use client signature algorithms if not NULL */ |
| 1521 | if (!s->server && c->client_sigalgs && !is_suiteb) { |
| 1522 | conf = c->client_sigalgs; |
| 1523 | conflen = c->client_sigalgslen; |
| 1524 | } else if (c->conf_sigalgs && !is_suiteb) { |
| 1525 | conf = c->conf_sigalgs; |
| 1526 | conflen = c->conf_sigalgslen; |
| 1527 | } else |
Dr. Stephen Henson | a9669dd | 2017-01-25 19:12:48 +0000 | [diff] [blame] | 1528 | conflen = tls12_get_psigalgs(s, 0, &conf); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1529 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) { |
| 1530 | pref = conf; |
| 1531 | preflen = conflen; |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 1532 | allow = s->s3->tmp.peer_sigalgs; |
| 1533 | allowlen = s->s3->tmp.peer_sigalgslen; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1534 | } else { |
| 1535 | allow = conf; |
| 1536 | allowlen = conflen; |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 1537 | pref = s->s3->tmp.peer_sigalgs; |
| 1538 | preflen = s->s3->tmp.peer_sigalgslen; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1539 | } |
| 1540 | nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen); |
Dr. Stephen Henson | 34e3edb | 2015-03-03 13:20:57 +0000 | [diff] [blame] | 1541 | if (nmatch) { |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1542 | salgs = OPENSSL_malloc(nmatch * sizeof(*salgs)); |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 1543 | if (salgs == NULL) |
Dr. Stephen Henson | 34e3edb | 2015-03-03 13:20:57 +0000 | [diff] [blame] | 1544 | return 0; |
| 1545 | nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen); |
| 1546 | } else { |
| 1547 | salgs = NULL; |
| 1548 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1549 | c->shared_sigalgs = salgs; |
| 1550 | c->shared_sigalgslen = nmatch; |
| 1551 | return 1; |
| 1552 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 1553 | |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 1554 | /* Set preferred digest for each key type */ |
| 1555 | |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1556 | int tls1_save_sigalgs(SSL *s, PACKET *pkt) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1557 | { |
| 1558 | CERT *c = s->cert; |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1559 | unsigned int stmp; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1560 | size_t size, i; |
| 1561 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1562 | /* Extension ignored for inappropriate versions */ |
| 1563 | if (!SSL_USE_SIGALGS(s)) |
| 1564 | return 1; |
| 1565 | /* Should never happen */ |
| 1566 | if (!c) |
| 1567 | return 0; |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 1568 | |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1569 | size = PACKET_remaining(pkt); |
| 1570 | |
| 1571 | /* Invalid data length */ |
Dr. Stephen Henson | 8f12296 | 2017-03-03 02:44:18 +0000 | [diff] [blame] | 1572 | if (size == 0 || (size & 1) != 0) |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1573 | return 0; |
| 1574 | |
| 1575 | size >>= 1; |
| 1576 | |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 1577 | OPENSSL_free(s->s3->tmp.peer_sigalgs); |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1578 | s->s3->tmp.peer_sigalgs = OPENSSL_malloc(size |
| 1579 | * sizeof(*s->s3->tmp.peer_sigalgs)); |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 1580 | if (s->s3->tmp.peer_sigalgs == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1581 | return 0; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1582 | s->s3->tmp.peer_sigalgslen = size; |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1583 | for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++) |
| 1584 | s->s3->tmp.peer_sigalgs[i] = stmp; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1585 | |
| 1586 | if (i != size) |
| 1587 | return 0; |
| 1588 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1589 | return 1; |
| 1590 | } |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 1591 | |
Dr. Stephen Henson | c800c27 | 2014-10-09 20:37:27 +0100 | [diff] [blame] | 1592 | int tls1_process_sigalgs(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1593 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1594 | size_t i; |
Dr. Stephen Henson | f7d5348 | 2015-07-14 23:19:11 +0100 | [diff] [blame] | 1595 | uint32_t *pvalid = s->s3->tmp.valid_flags; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1596 | CERT *c = s->cert; |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1597 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1598 | if (!tls1_set_shared_sigalgs(s)) |
| 1599 | return 0; |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 1600 | |
Dr. Stephen Henson | 9195ddc | 2017-02-18 03:42:15 +0000 | [diff] [blame] | 1601 | for (i = 0; i < SSL_PKEY_NUM; i++) |
| 1602 | pvalid[i] = 0; |
| 1603 | |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1604 | for (i = 0; i < c->shared_sigalgslen; i++) { |
| 1605 | const SIGALG_LOOKUP *sigptr = c->shared_sigalgs[i]; |
Dr. Stephen Henson | 9195ddc | 2017-02-18 03:42:15 +0000 | [diff] [blame] | 1606 | int idx = sigptr->sig_idx; |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1607 | |
Matt Caswell | 523fb32 | 2016-12-30 14:08:19 +0000 | [diff] [blame] | 1608 | /* Ignore PKCS1 based sig algs in TLSv1.3 */ |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1609 | if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA) |
Matt Caswell | 523fb32 | 2016-12-30 14:08:19 +0000 | [diff] [blame] | 1610 | continue; |
Dr. Stephen Henson | 9195ddc | 2017-02-18 03:42:15 +0000 | [diff] [blame] | 1611 | /* If not disabled indicate we can explicitly sign */ |
| 1612 | if (pvalid[idx] == 0 && tls12_get_pkey_idx(sigptr->sig) != -1) |
Dr. Stephen Henson | a8bb912 | 2017-02-19 18:47:16 +0000 | [diff] [blame] | 1613 | pvalid[sigptr->sig_idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1614 | } |
| 1615 | return 1; |
| 1616 | } |
Dr. Stephen Henson | 4817504 | 2011-12-31 22:59:57 +0000 | [diff] [blame] | 1617 | |
Dr. Stephen Henson | e7f8ff4 | 2012-03-06 14:28:21 +0000 | [diff] [blame] | 1618 | int SSL_get_sigalgs(SSL *s, int idx, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1619 | int *psign, int *phash, int *psignhash, |
| 1620 | unsigned char *rsig, unsigned char *rhash) |
| 1621 | { |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1622 | uint16_t *psig = s->s3->tmp.peer_sigalgs; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1623 | size_t numsigalgs = s->s3->tmp.peer_sigalgslen; |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1624 | if (psig == NULL || numsigalgs > INT_MAX) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1625 | return 0; |
| 1626 | if (idx >= 0) { |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1627 | const SIGALG_LOOKUP *lu; |
| 1628 | |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1629 | if (idx >= (int)numsigalgs) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1630 | return 0; |
| 1631 | psig += idx; |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1632 | if (rhash != NULL) |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1633 | *rhash = (unsigned char)((*psig >> 8) & 0xff); |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1634 | if (rsig != NULL) |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1635 | *rsig = (unsigned char)(*psig & 0xff); |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1636 | lu = tls1_lookup_sigalg(*psig); |
| 1637 | if (psign != NULL) |
| 1638 | *psign = lu != NULL ? lu->sig : NID_undef; |
| 1639 | if (phash != NULL) |
| 1640 | *phash = lu != NULL ? lu->hash : NID_undef; |
| 1641 | if (psignhash != NULL) |
| 1642 | *psignhash = lu != NULL ? lu->sigandhash : NID_undef; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1643 | } |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1644 | return (int)numsigalgs; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1645 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 1646 | |
| 1647 | int SSL_get_shared_sigalgs(SSL *s, int idx, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1648 | int *psign, int *phash, int *psignhash, |
| 1649 | unsigned char *rsig, unsigned char *rhash) |
| 1650 | { |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1651 | const SIGALG_LOOKUP *shsigalgs; |
| 1652 | if (s->cert->shared_sigalgs == NULL |
Peter Wu | 6d047e0 | 2017-02-02 12:11:10 +0100 | [diff] [blame] | 1653 | || idx < 0 |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1654 | || idx >= (int)s->cert->shared_sigalgslen |
| 1655 | || s->cert->shared_sigalgslen > INT_MAX) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1656 | return 0; |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1657 | shsigalgs = s->cert->shared_sigalgs[idx]; |
| 1658 | if (phash != NULL) |
| 1659 | *phash = shsigalgs->hash; |
| 1660 | if (psign != NULL) |
| 1661 | *psign = shsigalgs->sig; |
| 1662 | if (psignhash != NULL) |
| 1663 | *psignhash = shsigalgs->sigandhash; |
| 1664 | if (rsig != NULL) |
| 1665 | *rsig = (unsigned char)(shsigalgs->sigalg & 0xff); |
| 1666 | if (rhash != NULL) |
| 1667 | *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff); |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1668 | return (int)s->cert->shared_sigalgslen; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1669 | } |
Dr. Stephen Henson | e7f8ff4 | 2012-03-06 14:28:21 +0000 | [diff] [blame] | 1670 | |
Dr. Stephen Henson | 787ebca | 2017-01-29 15:12:58 +0000 | [diff] [blame] | 1671 | /* Maximum possible number of unique entries in sigalgs array */ |
| 1672 | #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1673 | |
| 1674 | typedef struct { |
| 1675 | size_t sigalgcnt; |
Dr. Stephen Henson | 787ebca | 2017-01-29 15:12:58 +0000 | [diff] [blame] | 1676 | int sigalgs[TLS_MAX_SIGALGCNT]; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1677 | } sig_cb_st; |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1678 | |
Dr. Stephen Henson | 431f458 | 2015-07-23 14:57:42 +0100 | [diff] [blame] | 1679 | static void get_sigorhash(int *psig, int *phash, const char *str) |
| 1680 | { |
| 1681 | if (strcmp(str, "RSA") == 0) { |
| 1682 | *psig = EVP_PKEY_RSA; |
Dr. Stephen Henson | b2eb699 | 2017-01-25 16:22:13 +0000 | [diff] [blame] | 1683 | } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) { |
| 1684 | *psig = EVP_PKEY_RSA_PSS; |
Dr. Stephen Henson | 431f458 | 2015-07-23 14:57:42 +0100 | [diff] [blame] | 1685 | } else if (strcmp(str, "DSA") == 0) { |
| 1686 | *psig = EVP_PKEY_DSA; |
| 1687 | } else if (strcmp(str, "ECDSA") == 0) { |
| 1688 | *psig = EVP_PKEY_EC; |
| 1689 | } else { |
| 1690 | *phash = OBJ_sn2nid(str); |
| 1691 | if (*phash == NID_undef) |
| 1692 | *phash = OBJ_ln2nid(str); |
| 1693 | } |
| 1694 | } |
Dr. Stephen Henson | 787ebca | 2017-01-29 15:12:58 +0000 | [diff] [blame] | 1695 | /* Maximum length of a signature algorithm string component */ |
| 1696 | #define TLS_MAX_SIGSTRING_LEN 40 |
Dr. Stephen Henson | 431f458 | 2015-07-23 14:57:42 +0100 | [diff] [blame] | 1697 | |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1698 | static int sig_cb(const char *elem, int len, void *arg) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1699 | { |
| 1700 | sig_cb_st *sarg = arg; |
| 1701 | size_t i; |
Dr. Stephen Henson | 787ebca | 2017-01-29 15:12:58 +0000 | [diff] [blame] | 1702 | char etmp[TLS_MAX_SIGSTRING_LEN], *p; |
Dr. Stephen Henson | 431f458 | 2015-07-23 14:57:42 +0100 | [diff] [blame] | 1703 | int sig_alg = NID_undef, hash_alg = NID_undef; |
Kurt Roeckx | 2747d73 | 2015-01-24 14:46:50 +0100 | [diff] [blame] | 1704 | if (elem == NULL) |
| 1705 | return 0; |
Dr. Stephen Henson | 787ebca | 2017-01-29 15:12:58 +0000 | [diff] [blame] | 1706 | if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1707 | return 0; |
| 1708 | if (len > (int)(sizeof(etmp) - 1)) |
| 1709 | return 0; |
| 1710 | memcpy(etmp, elem, len); |
| 1711 | etmp[len] = 0; |
| 1712 | p = strchr(etmp, '+'); |
Dr. Stephen Henson | 8a43a42 | 2017-01-26 14:40:59 +0000 | [diff] [blame] | 1713 | /* See if we have a match for TLS 1.3 names */ |
| 1714 | if (p == NULL) { |
| 1715 | const SIGALG_LOOKUP *s; |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1716 | |
Dr. Stephen Henson | 8a43a42 | 2017-01-26 14:40:59 +0000 | [diff] [blame] | 1717 | for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); |
| 1718 | i++, s++) { |
| 1719 | if (s->name != NULL && strcmp(etmp, s->name) == 0) { |
| 1720 | sig_alg = s->sig; |
| 1721 | hash_alg = s->hash; |
| 1722 | break; |
| 1723 | } |
| 1724 | } |
| 1725 | } else { |
| 1726 | *p = 0; |
| 1727 | p++; |
| 1728 | if (*p == 0) |
| 1729 | return 0; |
| 1730 | get_sigorhash(&sig_alg, &hash_alg, etmp); |
| 1731 | get_sigorhash(&sig_alg, &hash_alg, p); |
| 1732 | } |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1733 | |
Dr. Stephen Henson | 431f458 | 2015-07-23 14:57:42 +0100 | [diff] [blame] | 1734 | if (sig_alg == NID_undef || hash_alg == NID_undef) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1735 | return 0; |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1736 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1737 | for (i = 0; i < sarg->sigalgcnt; i += 2) { |
| 1738 | if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg) |
| 1739 | return 0; |
| 1740 | } |
| 1741 | sarg->sigalgs[sarg->sigalgcnt++] = hash_alg; |
| 1742 | sarg->sigalgs[sarg->sigalgcnt++] = sig_alg; |
| 1743 | return 1; |
| 1744 | } |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1745 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1746 | /* |
FdaSilvaYY | 9d22666 | 2016-06-28 22:39:55 +0200 | [diff] [blame] | 1747 | * Set supported signature algorithms based on a colon separated list of the |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1748 | * form sig+hash e.g. RSA+SHA512:DSA+SHA512 |
| 1749 | */ |
Dr. Stephen Henson | 3dbc46d | 2012-07-03 12:51:14 +0000 | [diff] [blame] | 1750 | int tls1_set_sigalgs_list(CERT *c, const char *str, int client) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1751 | { |
| 1752 | sig_cb_st sig; |
| 1753 | sig.sigalgcnt = 0; |
| 1754 | if (!CONF_parse_list(str, ':', 1, sig_cb, &sig)) |
| 1755 | return 0; |
| 1756 | if (c == NULL) |
| 1757 | return 1; |
| 1758 | return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client); |
| 1759 | } |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1760 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1761 | 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] | 1762 | { |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1763 | uint16_t *sigalgs, *sptr; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1764 | size_t i; |
Matt Caswell | 63c1df0 | 2016-12-14 16:50:14 +0000 | [diff] [blame] | 1765 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1766 | if (salglen & 1) |
| 1767 | return 0; |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 1768 | sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1769 | if (sigalgs == NULL) |
| 1770 | return 0; |
| 1771 | for (i = 0, sptr = sigalgs; i < salglen; i += 2) { |
Matt Caswell | 63c1df0 | 2016-12-14 16:50:14 +0000 | [diff] [blame] | 1772 | size_t j; |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 1773 | const SIGALG_LOOKUP *curr; |
Matt Caswell | 63c1df0 | 2016-12-14 16:50:14 +0000 | [diff] [blame] | 1774 | int md_id = *psig_nids++; |
| 1775 | int sig_id = *psig_nids++; |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1776 | |
Matt Caswell | 63c1df0 | 2016-12-14 16:50:14 +0000 | [diff] [blame] | 1777 | for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl); |
| 1778 | j++, curr++) { |
Matt Caswell | fe3066e | 2017-01-03 10:01:39 +0000 | [diff] [blame] | 1779 | if (curr->hash == md_id && curr->sig == sig_id) { |
Matt Caswell | 63c1df0 | 2016-12-14 16:50:14 +0000 | [diff] [blame] | 1780 | *sptr++ = curr->sigalg; |
| 1781 | break; |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | if (j == OSSL_NELEM(sigalg_lookup_tbl)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1786 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1787 | } |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1788 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1789 | if (client) { |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 1790 | OPENSSL_free(c->client_sigalgs); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1791 | c->client_sigalgs = sigalgs; |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 1792 | c->client_sigalgslen = salglen / 2; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1793 | } else { |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 1794 | OPENSSL_free(c->conf_sigalgs); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1795 | c->conf_sigalgs = sigalgs; |
Matt Caswell | 7a531ee | 2017-01-10 14:23:02 +0000 | [diff] [blame] | 1796 | c->conf_sigalgslen = salglen / 2; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1797 | } |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1798 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1799 | return 1; |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 1800 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1801 | err: |
| 1802 | OPENSSL_free(sigalgs); |
| 1803 | return 0; |
| 1804 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 1805 | |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1806 | 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] | 1807 | { |
| 1808 | int sig_nid; |
| 1809 | size_t i; |
| 1810 | if (default_nid == -1) |
| 1811 | return 1; |
| 1812 | sig_nid = X509_get_signature_nid(x); |
| 1813 | if (default_nid) |
| 1814 | return sig_nid == default_nid ? 1 : 0; |
| 1815 | for (i = 0; i < c->shared_sigalgslen; i++) |
Dr. Stephen Henson | 4d43ee2 | 2017-01-26 15:24:35 +0000 | [diff] [blame] | 1816 | if (sig_nid == c->shared_sigalgs[i]->sigandhash) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1817 | return 1; |
| 1818 | return 0; |
| 1819 | } |
| 1820 | |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 1821 | /* Check to see if a certificate issuer name matches list of CA names */ |
| 1822 | 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] | 1823 | { |
| 1824 | X509_NAME *nm; |
| 1825 | int i; |
| 1826 | nm = X509_get_issuer_name(x); |
| 1827 | for (i = 0; i < sk_X509_NAME_num(names); i++) { |
| 1828 | if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i))) |
| 1829 | return 1; |
| 1830 | } |
| 1831 | return 0; |
| 1832 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1833 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1834 | /* |
| 1835 | * Check certificate chain is consistent with TLS extensions and is usable by |
| 1836 | * server. This servers two purposes: it allows users to check chains before |
| 1837 | * passing them to the server and it allows the server to check chains before |
| 1838 | * attempting to use them. |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1839 | */ |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 1840 | |
| 1841 | /* Flags which need to be set for a certificate when stict mode not set */ |
| 1842 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1843 | #define CERT_PKEY_VALID_FLAGS \ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1844 | (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM) |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 1845 | /* Strict mode flags */ |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1846 | #define CERT_PKEY_STRICT_FLAGS \ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1847 | (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \ |
| 1848 | | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE) |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 1849 | |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1850 | 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] | 1851 | int idx) |
| 1852 | { |
| 1853 | int i; |
| 1854 | int rv = 0; |
| 1855 | int check_flags = 0, strict_mode; |
| 1856 | CERT_PKEY *cpk = NULL; |
| 1857 | CERT *c = s->cert; |
Dr. Stephen Henson | f7d5348 | 2015-07-14 23:19:11 +0100 | [diff] [blame] | 1858 | uint32_t *pvalid; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1859 | unsigned int suiteb_flags = tls1_suiteb(s); |
| 1860 | /* idx == -1 means checking server chains */ |
| 1861 | if (idx != -1) { |
| 1862 | /* idx == -2 means checking client certificate chains */ |
| 1863 | if (idx == -2) { |
| 1864 | cpk = c->key; |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1865 | idx = (int)(cpk - c->pkeys); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1866 | } else |
| 1867 | cpk = c->pkeys + idx; |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 1868 | pvalid = s->s3->tmp.valid_flags + idx; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1869 | x = cpk->x509; |
| 1870 | pk = cpk->privatekey; |
| 1871 | chain = cpk->chain; |
| 1872 | strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT; |
| 1873 | /* If no cert or key, forget it */ |
| 1874 | if (!x || !pk) |
| 1875 | goto end; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1876 | } else { |
| 1877 | if (!x || !pk) |
Matt Caswell | d813f9e | 2015-03-11 17:01:38 +0000 | [diff] [blame] | 1878 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1879 | idx = ssl_cert_type(x, pk); |
| 1880 | if (idx == -1) |
Matt Caswell | d813f9e | 2015-03-11 17:01:38 +0000 | [diff] [blame] | 1881 | return 0; |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 1882 | pvalid = s->s3->tmp.valid_flags + idx; |
| 1883 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1884 | if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) |
| 1885 | check_flags = CERT_PKEY_STRICT_FLAGS; |
| 1886 | else |
| 1887 | check_flags = CERT_PKEY_VALID_FLAGS; |
| 1888 | strict_mode = 1; |
| 1889 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1890 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1891 | if (suiteb_flags) { |
| 1892 | int ok; |
| 1893 | if (check_flags) |
| 1894 | check_flags |= CERT_PKEY_SUITEB; |
| 1895 | ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags); |
| 1896 | if (ok == X509_V_OK) |
| 1897 | rv |= CERT_PKEY_SUITEB; |
| 1898 | else if (!check_flags) |
| 1899 | goto end; |
| 1900 | } |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 1901 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1902 | /* |
| 1903 | * Check all signature algorithms are consistent with signature |
| 1904 | * algorithms extension if TLS 1.2 or later and strict mode. |
| 1905 | */ |
| 1906 | if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) { |
| 1907 | int default_nid; |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1908 | int rsign = 0; |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 1909 | if (s->s3->tmp.peer_sigalgs) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1910 | default_nid = 0; |
| 1911 | /* If no sigalgs extension use defaults from RFC5246 */ |
| 1912 | else { |
| 1913 | switch (idx) { |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 1914 | case SSL_PKEY_RSA: |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1915 | rsign = EVP_PKEY_RSA; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1916 | default_nid = NID_sha1WithRSAEncryption; |
| 1917 | break; |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1918 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1919 | case SSL_PKEY_DSA_SIGN: |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1920 | rsign = EVP_PKEY_DSA; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1921 | default_nid = NID_dsaWithSHA1; |
| 1922 | break; |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1923 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1924 | case SSL_PKEY_ECC: |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1925 | rsign = EVP_PKEY_EC; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1926 | default_nid = NID_ecdsa_with_SHA1; |
| 1927 | break; |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 1928 | |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1929 | case SSL_PKEY_GOST01: |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1930 | rsign = NID_id_GostR3410_2001; |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1931 | default_nid = NID_id_GostR3411_94_with_GostR3410_2001; |
| 1932 | break; |
| 1933 | |
| 1934 | case SSL_PKEY_GOST12_256: |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1935 | rsign = NID_id_GostR3410_2012_256; |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1936 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256; |
| 1937 | break; |
| 1938 | |
| 1939 | case SSL_PKEY_GOST12_512: |
Matt Caswell | 536199e | 2016-12-14 16:37:48 +0000 | [diff] [blame] | 1940 | rsign = NID_id_GostR3410_2012_512; |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1941 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512; |
| 1942 | break; |
| 1943 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1944 | default: |
| 1945 | default_nid = -1; |
| 1946 | break; |
| 1947 | } |
| 1948 | } |
| 1949 | /* |
| 1950 | * If peer sent no signature algorithms extension and we have set |
| 1951 | * preferred signature algorithms check we support sha1. |
| 1952 | */ |
| 1953 | if (default_nid > 0 && c->conf_sigalgs) { |
| 1954 | size_t j; |
Dr. Stephen Henson | 98c792d | 2017-01-25 14:33:55 +0000 | [diff] [blame] | 1955 | const uint16_t *p = c->conf_sigalgs; |
Matt Caswell | 703bcee | 2016-12-14 14:31:21 +0000 | [diff] [blame] | 1956 | for (j = 0; j < c->conf_sigalgslen; j++, p++) { |
Dr. Stephen Henson | 44b6318 | 2017-01-30 17:27:35 +0000 | [diff] [blame] | 1957 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*p); |
| 1958 | |
| 1959 | if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1960 | break; |
| 1961 | } |
| 1962 | if (j == c->conf_sigalgslen) { |
| 1963 | if (check_flags) |
| 1964 | goto skip_sigs; |
| 1965 | else |
| 1966 | goto end; |
| 1967 | } |
| 1968 | } |
| 1969 | /* Check signature algorithm of each cert in chain */ |
| 1970 | if (!tls1_check_sig_alg(c, x, default_nid)) { |
| 1971 | if (!check_flags) |
| 1972 | goto end; |
| 1973 | } else |
| 1974 | rv |= CERT_PKEY_EE_SIGNATURE; |
| 1975 | rv |= CERT_PKEY_CA_SIGNATURE; |
| 1976 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1977 | if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) { |
| 1978 | if (check_flags) { |
| 1979 | rv &= ~CERT_PKEY_CA_SIGNATURE; |
| 1980 | break; |
| 1981 | } else |
| 1982 | goto end; |
| 1983 | } |
| 1984 | } |
| 1985 | } |
| 1986 | /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */ |
| 1987 | else if (check_flags) |
| 1988 | rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE; |
| 1989 | skip_sigs: |
| 1990 | /* Check cert parameters are consistent */ |
Dr. Stephen Henson | 9195ddc | 2017-02-18 03:42:15 +0000 | [diff] [blame] | 1991 | if (tls1_check_cert_param(s, x, 1)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1992 | rv |= CERT_PKEY_EE_PARAM; |
| 1993 | else if (!check_flags) |
| 1994 | goto end; |
| 1995 | if (!s->server) |
| 1996 | rv |= CERT_PKEY_CA_PARAM; |
| 1997 | /* In strict mode check rest of chain too */ |
| 1998 | else if (strict_mode) { |
| 1999 | rv |= CERT_PKEY_CA_PARAM; |
| 2000 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 2001 | X509 *ca = sk_X509_value(chain, i); |
| 2002 | if (!tls1_check_cert_param(s, ca, 0)) { |
| 2003 | if (check_flags) { |
| 2004 | rv &= ~CERT_PKEY_CA_PARAM; |
| 2005 | break; |
| 2006 | } else |
| 2007 | goto end; |
| 2008 | } |
| 2009 | } |
| 2010 | } |
| 2011 | if (!s->server && strict_mode) { |
| 2012 | STACK_OF(X509_NAME) *ca_dn; |
| 2013 | int check_type = 0; |
Dr. Stephen Henson | 3aeb934 | 2016-01-19 00:21:12 +0000 | [diff] [blame] | 2014 | switch (EVP_PKEY_id(pk)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2015 | case EVP_PKEY_RSA: |
| 2016 | check_type = TLS_CT_RSA_SIGN; |
| 2017 | break; |
| 2018 | case EVP_PKEY_DSA: |
| 2019 | check_type = TLS_CT_DSS_SIGN; |
| 2020 | break; |
| 2021 | case EVP_PKEY_EC: |
| 2022 | check_type = TLS_CT_ECDSA_SIGN; |
| 2023 | break; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2024 | } |
| 2025 | if (check_type) { |
Dr. Stephen Henson | 75c13e7 | 2017-02-23 22:12:28 +0000 | [diff] [blame] | 2026 | const uint8_t *ctypes = s->s3->tmp.ctype; |
| 2027 | size_t j; |
| 2028 | |
| 2029 | for (j = 0; j < s->s3->tmp.ctype_len; j++, ctypes++) { |
| 2030 | if (*ctypes == check_type) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2031 | rv |= CERT_PKEY_CERT_TYPE; |
| 2032 | break; |
| 2033 | } |
| 2034 | } |
| 2035 | if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags) |
| 2036 | goto end; |
Dr. Stephen Henson | 75c13e7 | 2017-02-23 22:12:28 +0000 | [diff] [blame] | 2037 | } else { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2038 | rv |= CERT_PKEY_CERT_TYPE; |
Dr. Stephen Henson | 75c13e7 | 2017-02-23 22:12:28 +0000 | [diff] [blame] | 2039 | } |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 2040 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2041 | ca_dn = s->s3->tmp.ca_names; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 2042 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2043 | if (!sk_X509_NAME_num(ca_dn)) |
| 2044 | rv |= CERT_PKEY_ISSUER_NAME; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 2045 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2046 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { |
| 2047 | if (ssl_check_ca_name(ca_dn, x)) |
| 2048 | rv |= CERT_PKEY_ISSUER_NAME; |
| 2049 | } |
| 2050 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { |
| 2051 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 2052 | X509 *xtmp = sk_X509_value(chain, i); |
| 2053 | if (ssl_check_ca_name(ca_dn, xtmp)) { |
| 2054 | rv |= CERT_PKEY_ISSUER_NAME; |
| 2055 | break; |
| 2056 | } |
| 2057 | } |
| 2058 | } |
| 2059 | if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME)) |
| 2060 | goto end; |
| 2061 | } else |
| 2062 | rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 2063 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2064 | if (!check_flags || (rv & check_flags) == check_flags) |
| 2065 | rv |= CERT_PKEY_VALID; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 2066 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2067 | end: |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 2068 | |
Dr. Stephen Henson | a8bb912 | 2017-02-19 18:47:16 +0000 | [diff] [blame] | 2069 | if (TLS1_get_version(s) >= TLS1_2_VERSION) |
| 2070 | rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN); |
| 2071 | else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2072 | rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 2073 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2074 | /* |
| 2075 | * When checking a CERT_PKEY structure all flags are irrelevant if the |
| 2076 | * chain is invalid. |
| 2077 | */ |
| 2078 | if (!check_flags) { |
Dr. Stephen Henson | a8bb912 | 2017-02-19 18:47:16 +0000 | [diff] [blame] | 2079 | if (rv & CERT_PKEY_VALID) { |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 2080 | *pvalid = rv; |
Dr. Stephen Henson | a8bb912 | 2017-02-19 18:47:16 +0000 | [diff] [blame] | 2081 | } else { |
| 2082 | /* Preserve sign and explicit sign flag, clear rest */ |
| 2083 | *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2084 | return 0; |
| 2085 | } |
| 2086 | } |
| 2087 | return rv; |
| 2088 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 2089 | |
| 2090 | /* Set validity of certificates in an SSL structure */ |
| 2091 | void tls1_set_cert_validity(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2092 | { |
Dr. Stephen Henson | d0ff28f | 2017-02-10 04:23:53 +0000 | [diff] [blame] | 2093 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA); |
Matt Caswell | 17dd65e | 2015-03-24 15:10:15 +0000 | [diff] [blame] | 2094 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN); |
Matt Caswell | 17dd65e | 2015-03-24 15:10:15 +0000 | [diff] [blame] | 2095 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC); |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 2096 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01); |
| 2097 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256); |
| 2098 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
Dr. Stephen Henson | 18d7158 | 2012-06-29 14:24:42 +0000 | [diff] [blame] | 2101 | /* User level utiity function to check a chain is suitable */ |
| 2102 | 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] | 2103 | { |
| 2104 | return tls1_check_chain(s, x, pk, chain, -1); |
| 2105 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 2106 | |
Dr. Stephen Henson | 09599b5 | 2014-01-22 16:22:48 +0000 | [diff] [blame] | 2107 | #ifndef OPENSSL_NO_DH |
| 2108 | DH *ssl_get_auto_dh(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2109 | { |
| 2110 | int dh_secbits = 80; |
| 2111 | if (s->cert->dh_tmp_auto == 2) |
| 2112 | return DH_get_1024_160(); |
Dr. Stephen Henson | adc5506 | 2015-06-28 17:01:52 +0100 | [diff] [blame] | 2113 | if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2114 | if (s->s3->tmp.new_cipher->strength_bits == 256) |
| 2115 | dh_secbits = 128; |
| 2116 | else |
| 2117 | dh_secbits = 80; |
| 2118 | } else { |
Dr. Stephen Henson | a497cf2 | 2017-02-14 00:35:26 +0000 | [diff] [blame] | 2119 | if (s->s3->tmp.cert == NULL) |
Dr. Stephen Henson | f365a3e | 2017-02-13 16:32:06 +0000 | [diff] [blame] | 2120 | return NULL; |
Dr. Stephen Henson | a497cf2 | 2017-02-14 00:35:26 +0000 | [diff] [blame] | 2121 | dh_secbits = EVP_PKEY_security_bits(s->s3->tmp.cert->privatekey); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2122 | } |
Dr. Stephen Henson | 09599b5 | 2014-01-22 16:22:48 +0000 | [diff] [blame] | 2123 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2124 | if (dh_secbits >= 128) { |
| 2125 | DH *dhp = DH_new(); |
Matt Caswell | 0aeddcf | 2016-04-06 17:49:48 +0100 | [diff] [blame] | 2126 | BIGNUM *p, *g; |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 2127 | if (dhp == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2128 | return NULL; |
Matt Caswell | 0aeddcf | 2016-04-06 17:49:48 +0100 | [diff] [blame] | 2129 | g = BN_new(); |
| 2130 | if (g != NULL) |
| 2131 | BN_set_word(g, 2); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2132 | if (dh_secbits >= 192) |
Rich Salz | 9021a5d | 2016-04-18 07:43:54 -0400 | [diff] [blame] | 2133 | p = BN_get_rfc3526_prime_8192(NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2134 | else |
Rich Salz | 9021a5d | 2016-04-18 07:43:54 -0400 | [diff] [blame] | 2135 | p = BN_get_rfc3526_prime_3072(NULL); |
Matt Caswell | 0aeddcf | 2016-04-06 17:49:48 +0100 | [diff] [blame] | 2136 | if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2137 | DH_free(dhp); |
Matt Caswell | 0aeddcf | 2016-04-06 17:49:48 +0100 | [diff] [blame] | 2138 | BN_free(p); |
| 2139 | BN_free(g); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2140 | return NULL; |
| 2141 | } |
| 2142 | return dhp; |
| 2143 | } |
| 2144 | if (dh_secbits >= 112) |
| 2145 | return DH_get_2048_224(); |
| 2146 | return DH_get_1024_160(); |
| 2147 | } |
Dr. Stephen Henson | 09599b5 | 2014-01-22 16:22:48 +0000 | [diff] [blame] | 2148 | #endif |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2149 | |
| 2150 | 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] | 2151 | { |
Dr. Stephen Henson | 72245f3 | 2015-12-30 13:34:53 +0000 | [diff] [blame] | 2152 | int secbits = -1; |
Dr. Stephen Henson | 8382fd3 | 2015-12-20 00:32:36 +0000 | [diff] [blame] | 2153 | EVP_PKEY *pkey = X509_get0_pubkey(x); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2154 | if (pkey) { |
Dr. Stephen Henson | 72245f3 | 2015-12-30 13:34:53 +0000 | [diff] [blame] | 2155 | /* |
| 2156 | * If no parameters this will return -1 and fail using the default |
| 2157 | * security callback for any non-zero security level. This will |
| 2158 | * reject keys which omit parameters but this only affects DSA and |
| 2159 | * omission of parameters is never (?) done in practice. |
| 2160 | */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2161 | secbits = EVP_PKEY_security_bits(pkey); |
Dr. Stephen Henson | 72245f3 | 2015-12-30 13:34:53 +0000 | [diff] [blame] | 2162 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2163 | if (s) |
| 2164 | return ssl_security(s, op, secbits, 0, x); |
| 2165 | else |
| 2166 | return ssl_ctx_security(ctx, op, secbits, 0, x); |
| 2167 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2168 | |
| 2169 | 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] | 2170 | { |
| 2171 | /* Lookup signature algorithm digest */ |
| 2172 | int secbits = -1, md_nid = NID_undef, sig_nid; |
Dr. Stephen Henson | 221c7b5 | 2016-02-11 15:25:11 +0000 | [diff] [blame] | 2173 | /* Don't check signature if self signed */ |
| 2174 | if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0) |
| 2175 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2176 | sig_nid = X509_get_signature_nid(x); |
| 2177 | if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) { |
| 2178 | const EVP_MD *md; |
| 2179 | if (md_nid && (md = EVP_get_digestbynid(md_nid))) |
| 2180 | secbits = EVP_MD_size(md) * 4; |
| 2181 | } |
| 2182 | if (s) |
| 2183 | return ssl_security(s, op, secbits, md_nid, x); |
| 2184 | else |
| 2185 | return ssl_ctx_security(ctx, op, secbits, md_nid, x); |
| 2186 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2187 | |
| 2188 | 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] | 2189 | { |
| 2190 | if (vfy) |
| 2191 | vfy = SSL_SECOP_PEER; |
| 2192 | if (is_ee) { |
| 2193 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy)) |
| 2194 | return SSL_R_EE_KEY_TOO_SMALL; |
| 2195 | } else { |
| 2196 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy)) |
| 2197 | return SSL_R_CA_KEY_TOO_SMALL; |
| 2198 | } |
| 2199 | if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy)) |
| 2200 | return SSL_R_CA_MD_TOO_WEAK; |
| 2201 | return 1; |
| 2202 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2203 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2204 | /* |
| 2205 | * Check security of a chain, if sk includes the end entity certificate then |
| 2206 | * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending |
| 2207 | * 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] | 2208 | */ |
| 2209 | |
| 2210 | 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] | 2211 | { |
| 2212 | int rv, start_idx, i; |
| 2213 | if (x == NULL) { |
| 2214 | x = sk_X509_value(sk, 0); |
| 2215 | start_idx = 1; |
| 2216 | } else |
| 2217 | start_idx = 0; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2218 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2219 | rv = ssl_security_cert(s, NULL, x, vfy, 1); |
| 2220 | if (rv != 1) |
| 2221 | return rv; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2222 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2223 | for (i = start_idx; i < sk_X509_num(sk); i++) { |
| 2224 | x = sk_X509_value(sk, i); |
| 2225 | rv = ssl_security_cert(s, NULL, x, vfy, 0); |
| 2226 | if (rv != 1) |
| 2227 | return rv; |
| 2228 | } |
| 2229 | return 1; |
| 2230 | } |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2231 | |
| 2232 | /* |
| 2233 | * Choose an appropriate signature algorithm based on available certificates |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2234 | * Sets chosen certificate and signature algorithm. |
| 2235 | * |
| 2236 | * For servers if we fail to find a required certificate it is a fatal error |
| 2237 | * and an appropriate error code is set and the TLS alert set in *al. |
| 2238 | * |
| 2239 | * For clients al is set to NULL. If a certificate is not suitable it is not |
| 2240 | * a fatal error: we will either try another certificate or not present one |
| 2241 | * to the server. In this case no error is set. |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2242 | */ |
Dr. Stephen Henson | 4a419f6 | 2017-02-13 15:50:43 +0000 | [diff] [blame] | 2243 | int tls_choose_sigalg(SSL *s, int *al) |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2244 | { |
Dr. Stephen Henson | 7b3a4d6 | 2017-02-16 01:29:14 +0000 | [diff] [blame] | 2245 | int idx = -1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2246 | const SIGALG_LOOKUP *lu = NULL; |
| 2247 | |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2248 | s->s3->tmp.cert = NULL; |
| 2249 | s->s3->tmp.sigalg = NULL; |
| 2250 | |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2251 | if (SSL_IS_TLS13(s)) { |
| 2252 | size_t i; |
Richard Levitte | 21f198e | 2017-02-03 13:56:54 +0100 | [diff] [blame] | 2253 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | e892e32 | 2017-02-24 20:43:02 +0000 | [diff] [blame] | 2254 | int curve = -1, skip_ec = 0; |
Richard Levitte | 21f198e | 2017-02-03 13:56:54 +0100 | [diff] [blame] | 2255 | #endif |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2256 | |
| 2257 | /* Look for a certificate matching shared sigaglgs */ |
| 2258 | for (i = 0; i < s->cert->shared_sigalgslen; i++) { |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2259 | lu = s->cert->shared_sigalgs[i]; |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2260 | |
Dr. Stephen Henson | 095a982 | 2017-02-25 00:25:04 +0000 | [diff] [blame] | 2261 | /* Skip SHA1, DSA and RSA if not PSS */ |
| 2262 | if (lu->hash == NID_sha1 || lu->sig == EVP_PKEY_DSA |
| 2263 | || lu->sig == EVP_PKEY_RSA) |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2264 | continue; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2265 | if (ssl_md(lu->hash_idx) == NULL) |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2266 | continue; |
| 2267 | idx = lu->sig_idx; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2268 | if (!ssl_has_cert(s, idx)) |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2269 | continue; |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2270 | if (lu->sig == EVP_PKEY_EC) { |
Richard Levitte | 21f198e | 2017-02-03 13:56:54 +0100 | [diff] [blame] | 2271 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2272 | if (curve == -1) { |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2273 | 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] | 2274 | |
| 2275 | curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); |
Dr. Stephen Henson | e892e32 | 2017-02-24 20:43:02 +0000 | [diff] [blame] | 2276 | if (EC_KEY_get_conv_form(ec) |
| 2277 | != POINT_CONVERSION_UNCOMPRESSED) |
| 2278 | skip_ec = 1; |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2279 | } |
Dr. Stephen Henson | e892e32 | 2017-02-24 20:43:02 +0000 | [diff] [blame] | 2280 | if (skip_ec || (lu->curve != NID_undef && curve != lu->curve)) |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2281 | continue; |
Richard Levitte | 21f198e | 2017-02-03 13:56:54 +0100 | [diff] [blame] | 2282 | #else |
| 2283 | continue; |
| 2284 | #endif |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2285 | } |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2286 | break; |
| 2287 | } |
| 2288 | if (i == s->cert->shared_sigalgslen) { |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2289 | if (al == NULL) |
| 2290 | return 1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2291 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 2292 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, |
| 2293 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
| 2294 | return 0; |
| 2295 | } |
| 2296 | } else { |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2297 | if (s->server) { |
| 2298 | /* Find index corresponding to ciphersuite */ |
| 2299 | idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); |
| 2300 | /* If no certificate for ciphersuite return */ |
| 2301 | if (idx == -1) |
| 2302 | return 1; |
| 2303 | if (idx == SSL_PKEY_GOST_EC) { |
| 2304 | /* Work out which GOST certificate is avaiable */ |
| 2305 | if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) { |
| 2306 | idx = SSL_PKEY_GOST12_512; |
| 2307 | } else if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) { |
| 2308 | idx = SSL_PKEY_GOST12_256; |
| 2309 | } else if (ssl_has_cert(s, SSL_PKEY_GOST01)) { |
| 2310 | idx = SSL_PKEY_GOST01; |
| 2311 | } else { |
| 2312 | if (al == NULL) |
| 2313 | return 1; |
| 2314 | *al = SSL_AD_INTERNAL_ERROR; |
| 2315 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); |
| 2316 | return 0; |
| 2317 | } |
| 2318 | } else if (!ssl_has_cert(s, idx)) { |
| 2319 | if (al == NULL) |
| 2320 | return 1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2321 | *al = SSL_AD_INTERNAL_ERROR; |
| 2322 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); |
| 2323 | return 0; |
| 2324 | } |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2325 | } else { |
| 2326 | /* Find index for client certificate */ |
| 2327 | idx = s->cert->key - s->cert->pkeys; |
| 2328 | if (!ssl_has_cert(s, idx)) |
| 2329 | return 1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2330 | } |
| 2331 | |
| 2332 | if (SSL_USE_SIGALGS(s)) { |
| 2333 | if (s->s3->tmp.peer_sigalgs != NULL) { |
| 2334 | size_t i; |
Dr. Stephen Henson | 599b586 | 2017-02-24 16:39:57 +0000 | [diff] [blame] | 2335 | #ifndef OPENSSL_NO_EC |
| 2336 | int curve; |
| 2337 | |
| 2338 | /* For Suite B need to match signature algorithm to curve */ |
| 2339 | if (tls1_suiteb(s)) { |
| 2340 | EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[idx].privatekey); |
| 2341 | curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); |
| 2342 | } else { |
| 2343 | curve = -1; |
| 2344 | } |
| 2345 | #endif |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2346 | |
| 2347 | /* |
| 2348 | * Find highest preference signature algorithm matching |
| 2349 | * cert type |
| 2350 | */ |
| 2351 | for (i = 0; i < s->cert->shared_sigalgslen; i++) { |
| 2352 | lu = s->cert->shared_sigalgs[i]; |
Dr. Stephen Henson | 599b586 | 2017-02-24 16:39:57 +0000 | [diff] [blame] | 2353 | #ifdef OPENSSL_NO_EC |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2354 | if (lu->sig_idx == idx) |
| 2355 | break; |
Dr. Stephen Henson | 599b586 | 2017-02-24 16:39:57 +0000 | [diff] [blame] | 2356 | #else |
| 2357 | if (lu->sig_idx == idx |
| 2358 | && (curve == -1 || lu->curve == curve)) |
| 2359 | break; |
| 2360 | #endif |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2361 | if (idx == SSL_PKEY_RSA && lu->sig == EVP_PKEY_RSA_PSS) |
| 2362 | break; |
| 2363 | } |
| 2364 | if (i == s->cert->shared_sigalgslen) { |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2365 | if (al == NULL) |
| 2366 | return 1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2367 | *al = SSL_AD_INTERNAL_ERROR; |
| 2368 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); |
| 2369 | return 0; |
| 2370 | } |
| 2371 | } else { |
| 2372 | /* |
| 2373 | * If we have no sigalg use defaults |
| 2374 | */ |
| 2375 | const uint16_t *sent_sigs; |
| 2376 | size_t sent_sigslen, i; |
| 2377 | |
| 2378 | if ((lu = tls1_get_legacy_sigalg(s, idx)) == NULL) { |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2379 | if (al == NULL) |
| 2380 | return 1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2381 | *al = SSL_AD_INTERNAL_ERROR; |
| 2382 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); |
| 2383 | return 0; |
| 2384 | } |
| 2385 | |
| 2386 | /* Check signature matches a type we sent */ |
| 2387 | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
| 2388 | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { |
| 2389 | if (lu->sigalg == *sent_sigs) |
| 2390 | break; |
| 2391 | } |
| 2392 | if (i == sent_sigslen) { |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2393 | if (al == NULL) |
| 2394 | return 1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2395 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
| 2396 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 2397 | return 0; |
| 2398 | } |
| 2399 | } |
| 2400 | } else { |
| 2401 | if ((lu = tls1_get_legacy_sigalg(s, idx)) == NULL) { |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2402 | if (al == NULL) |
| 2403 | return 1; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2404 | *al = SSL_AD_INTERNAL_ERROR; |
| 2405 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); |
| 2406 | return 0; |
| 2407 | } |
| 2408 | } |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2409 | } |
Dr. Stephen Henson | 7b3a4d6 | 2017-02-16 01:29:14 +0000 | [diff] [blame] | 2410 | if (idx == -1) { |
Dr. Stephen Henson | 717a265 | 2017-02-15 16:19:43 +0000 | [diff] [blame] | 2411 | if (al != NULL) { |
| 2412 | *al = SSL_AD_INTERNAL_ERROR; |
| 2413 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); |
| 2414 | } |
Dr. Stephen Henson | 7b3a4d6 | 2017-02-16 01:29:14 +0000 | [diff] [blame] | 2415 | return 0; |
| 2416 | } |
Dr. Stephen Henson | a497cf2 | 2017-02-14 00:35:26 +0000 | [diff] [blame] | 2417 | s->s3->tmp.cert = &s->cert->pkeys[idx]; |
Dr. Stephen Henson | 59088e4 | 2017-02-15 15:28:56 +0000 | [diff] [blame] | 2418 | s->cert->key = s->s3->tmp.cert; |
Dr. Stephen Henson | 0972bc5 | 2017-02-13 16:04:07 +0000 | [diff] [blame] | 2419 | s->s3->tmp.sigalg = lu; |
Dr. Stephen Henson | 93a77f9 | 2017-01-31 17:45:00 +0000 | [diff] [blame] | 2420 | return 1; |
| 2421 | } |