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