Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 3 | * |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
Bodo Möller | f1fd454 | 2006-01-03 03:27:19 +0000 | [diff] [blame] | 8 | */ |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 9 | |
| 10 | #include <stdio.h> |
Emilia Kasper | aa474d1 | 2016-02-19 17:24:44 +0100 | [diff] [blame] | 11 | #include <stdlib.h> |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 12 | #include <openssl/objects.h> |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 13 | #include <openssl/evp.h> |
| 14 | #include <openssl/hmac.h> |
Dr. Stephen Henson | 67c8e7f | 2007-09-26 21:56:59 +0000 | [diff] [blame] | 15 | #include <openssl/ocsp.h> |
Matt Caswell | 5951e84 | 2016-04-20 16:38:29 +0100 | [diff] [blame] | 16 | #include <openssl/conf.h> |
| 17 | #include <openssl/x509v3.h> |
Rich Salz | 3c27208 | 2016-03-18 14:30:20 -0400 | [diff] [blame] | 18 | #include <openssl/dh.h> |
| 19 | #include <openssl/bn.h> |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 20 | #include "ssl_locl.h" |
Rich Salz | 3c27208 | 2016-03-18 14:30:20 -0400 | [diff] [blame] | 21 | #include <openssl/ct.h> |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 22 | |
Matt Caswell | d736bc1 | 2016-10-04 21:22:19 +0100 | [diff] [blame] | 23 | static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, size_t ticklen, |
Matt Caswell | ec60ccc | 2016-10-04 20:31:19 +0100 | [diff] [blame] | 24 | const unsigned char *sess_id, size_t sesslen, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 25 | SSL_SESSION **psess); |
Emilia Kasper | aa474d1 | 2016-02-19 17:24:44 +0100 | [diff] [blame] | 26 | static int ssl_check_serverhello_tlsext(SSL *s); |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 27 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 28 | SSL3_ENC_METHOD const TLSv1_enc_data = { |
| 29 | tls1_enc, |
| 30 | tls1_mac, |
| 31 | tls1_setup_key_block, |
| 32 | tls1_generate_master_secret, |
| 33 | tls1_change_cipher_state, |
| 34 | tls1_final_finish_mac, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 35 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 36 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 37 | tls1_alert_code, |
| 38 | tls1_export_keying_material, |
| 39 | 0, |
Matt Caswell | a29fa98 | 2016-09-29 22:40:15 +0100 | [diff] [blame] | 40 | ssl3_set_handshake_header, |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 41 | tls_close_construct_packet, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 42 | ssl3_handshake_write |
| 43 | }; |
Dr. Stephen Henson | 173e72e | 2013-03-11 15:34:28 +0000 | [diff] [blame] | 44 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 45 | SSL3_ENC_METHOD const TLSv1_1_enc_data = { |
| 46 | tls1_enc, |
| 47 | tls1_mac, |
| 48 | tls1_setup_key_block, |
| 49 | tls1_generate_master_secret, |
| 50 | tls1_change_cipher_state, |
| 51 | tls1_final_finish_mac, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 52 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 53 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 54 | tls1_alert_code, |
| 55 | tls1_export_keying_material, |
| 56 | SSL_ENC_FLAG_EXPLICIT_IV, |
Matt Caswell | a29fa98 | 2016-09-29 22:40:15 +0100 | [diff] [blame] | 57 | ssl3_set_handshake_header, |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 58 | tls_close_construct_packet, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 59 | ssl3_handshake_write |
| 60 | }; |
Dr. Stephen Henson | 173e72e | 2013-03-11 15:34:28 +0000 | [diff] [blame] | 61 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 62 | SSL3_ENC_METHOD const TLSv1_2_enc_data = { |
| 63 | tls1_enc, |
| 64 | tls1_mac, |
| 65 | tls1_setup_key_block, |
| 66 | tls1_generate_master_secret, |
| 67 | tls1_change_cipher_state, |
| 68 | tls1_final_finish_mac, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 69 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 70 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 71 | tls1_alert_code, |
| 72 | tls1_export_keying_material, |
| 73 | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF |
| 74 | | SSL_ENC_FLAG_TLS1_2_CIPHERS, |
Matt Caswell | a29fa98 | 2016-09-29 22:40:15 +0100 | [diff] [blame] | 75 | ssl3_set_handshake_header, |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 76 | tls_close_construct_packet, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 77 | ssl3_handshake_write |
| 78 | }; |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 79 | |
Matt Caswell | 582a17d | 2016-10-21 17:39:33 +0100 | [diff] [blame] | 80 | SSL3_ENC_METHOD const TLSv1_3_enc_data = { |
Matt Caswell | bebc0c7 | 2016-11-17 18:00:17 +0000 | [diff] [blame] | 81 | tls13_enc, |
Matt Caswell | 582a17d | 2016-10-21 17:39:33 +0100 | [diff] [blame] | 82 | tls1_mac, |
Matt Caswell | 92760c2 | 2016-11-09 14:06:12 +0000 | [diff] [blame] | 83 | tls13_setup_key_block, |
| 84 | tls13_generate_master_secret, |
| 85 | tls13_change_cipher_state, |
| 86 | tls13_final_finish_mac, |
Matt Caswell | 582a17d | 2016-10-21 17:39:33 +0100 | [diff] [blame] | 87 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 88 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 89 | tls1_alert_code, |
| 90 | tls1_export_keying_material, |
Matt Caswell | bebc0c7 | 2016-11-17 18:00:17 +0000 | [diff] [blame] | 91 | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF, |
Matt Caswell | 582a17d | 2016-10-21 17:39:33 +0100 | [diff] [blame] | 92 | ssl3_set_handshake_header, |
| 93 | tls_close_construct_packet, |
| 94 | ssl3_handshake_write |
| 95 | }; |
| 96 | |
Dr. Stephen Henson | f3b656b | 2005-08-05 23:56:11 +0000 | [diff] [blame] | 97 | long tls1_default_timeout(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 98 | { |
| 99 | /* |
| 100 | * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for |
| 101 | * http, the cache would over fill |
| 102 | */ |
| 103 | return (60 * 60 * 2); |
| 104 | } |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 105 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 106 | int tls1_new(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 107 | { |
| 108 | if (!ssl3_new(s)) |
| 109 | return (0); |
| 110 | s->method->ssl_clear(s); |
| 111 | return (1); |
| 112 | } |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 113 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 114 | void tls1_free(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 115 | { |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 116 | OPENSSL_free(s->tlsext_session_ticket); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 117 | ssl3_free(s); |
| 118 | } |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 119 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 120 | void tls1_clear(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 121 | { |
| 122 | ssl3_clear(s); |
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 | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 127 | } |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 128 | |
Dr. Stephen Henson | 525de5d | 2007-08-12 23:59:05 +0000 | [diff] [blame] | 129 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | eda3766 | 2011-05-30 17:58:13 +0000 | [diff] [blame] | 130 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 131 | typedef struct { |
| 132 | int nid; /* Curve NID */ |
| 133 | int secbits; /* Bits of security (from SP800-57) */ |
| 134 | unsigned int flags; /* Flags: currently just field type */ |
| 135 | } tls_curve_info; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 136 | |
Dr. Stephen Henson | 2dc1aee | 2016-02-13 15:27:43 +0000 | [diff] [blame] | 137 | /* |
| 138 | * Table of curve information. |
Rich Salz | ddb4c04 | 2016-01-03 13:24:32 -0500 | [diff] [blame] | 139 | * 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] | 140 | * table: the index of each entry is one less than the TLS curve id. |
| 141 | */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 142 | static const tls_curve_info nid_list[] = { |
| 143 | {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */ |
| 144 | {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */ |
| 145 | {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */ |
| 146 | {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */ |
| 147 | {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */ |
| 148 | {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */ |
| 149 | {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */ |
| 150 | {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */ |
| 151 | {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */ |
| 152 | {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */ |
| 153 | {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */ |
| 154 | {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */ |
| 155 | {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */ |
| 156 | {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */ |
| 157 | {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */ |
| 158 | {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */ |
| 159 | {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */ |
| 160 | {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */ |
| 161 | {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */ |
| 162 | {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */ |
| 163 | {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */ |
| 164 | {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */ |
| 165 | {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */ |
| 166 | {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */ |
| 167 | {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */ |
| 168 | {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */ |
| 169 | {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */ |
| 170 | {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */ |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 171 | {NID_X25519, 128, TLS_CURVE_CUSTOM}, /* X25519 (29) */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 172 | }; |
Dr. Stephen Henson | eda3766 | 2011-05-30 17:58:13 +0000 | [diff] [blame] | 173 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 174 | static const unsigned char ecformats_default[] = { |
| 175 | TLSEXT_ECPOINTFORMAT_uncompressed, |
| 176 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime, |
| 177 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 |
| 178 | }; |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 179 | |
Kurt Roeckx | fe6ef24 | 2015-12-04 22:30:36 +0100 | [diff] [blame] | 180 | /* The default curves */ |
| 181 | static const unsigned char eccurves_default[] = { |
Dr. Stephen Henson | 1db3107 | 2016-02-25 17:46:14 +0000 | [diff] [blame] | 182 | 0, 29, /* X25519 (29) */ |
Emilia Kasper | de57d23 | 2015-05-20 15:47:51 +0200 | [diff] [blame] | 183 | 0, 23, /* secp256r1 (23) */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 184 | 0, 25, /* secp521r1 (25) */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 185 | 0, 24, /* secp384r1 (24) */ |
Emilia Kasper | de57d23 | 2015-05-20 15:47:51 +0200 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | static const unsigned char eccurves_all[] = { |
Dr. Stephen Henson | 1db3107 | 2016-02-25 17:46:14 +0000 | [diff] [blame] | 189 | 0, 29, /* X25519 (29) */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 190 | 0, 23, /* secp256r1 (23) */ |
Emilia Kasper | de57d23 | 2015-05-20 15:47:51 +0200 | [diff] [blame] | 191 | 0, 25, /* secp521r1 (25) */ |
Emilia Kasper | de57d23 | 2015-05-20 15:47:51 +0200 | [diff] [blame] | 192 | 0, 24, /* secp384r1 (24) */ |
| 193 | 0, 26, /* brainpoolP256r1 (26) */ |
Rich Salz | ddb4c04 | 2016-01-03 13:24:32 -0500 | [diff] [blame] | 194 | 0, 27, /* brainpoolP384r1 (27) */ |
| 195 | 0, 28, /* brainpool512r1 (28) */ |
| 196 | |
| 197 | /* |
| 198 | * Remaining curves disabled by default but still permitted if set |
| 199 | * via an explicit callback or parameters. |
| 200 | */ |
Emilia Kasper | de57d23 | 2015-05-20 15:47:51 +0200 | [diff] [blame] | 201 | 0, 22, /* secp256k1 (22) */ |
Emilia Kasper | de57d23 | 2015-05-20 15:47:51 +0200 | [diff] [blame] | 202 | 0, 14, /* sect571r1 (14) */ |
| 203 | 0, 13, /* sect571k1 (13) */ |
| 204 | 0, 11, /* sect409k1 (11) */ |
| 205 | 0, 12, /* sect409r1 (12) */ |
| 206 | 0, 9, /* sect283k1 (9) */ |
| 207 | 0, 10, /* sect283r1 (10) */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 208 | 0, 20, /* secp224k1 (20) */ |
| 209 | 0, 21, /* secp224r1 (21) */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 210 | 0, 18, /* secp192k1 (18) */ |
| 211 | 0, 19, /* secp192r1 (19) */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 212 | 0, 15, /* secp160k1 (15) */ |
| 213 | 0, 16, /* secp160r1 (16) */ |
| 214 | 0, 17, /* secp160r2 (17) */ |
Emilia Kasper | de57d23 | 2015-05-20 15:47:51 +0200 | [diff] [blame] | 215 | 0, 8, /* sect239k1 (8) */ |
| 216 | 0, 6, /* sect233k1 (6) */ |
| 217 | 0, 7, /* sect233r1 (7) */ |
| 218 | 0, 4, /* sect193r1 (4) */ |
| 219 | 0, 5, /* sect193r2 (5) */ |
| 220 | 0, 1, /* sect163k1 (1) */ |
| 221 | 0, 2, /* sect163r1 (2) */ |
| 222 | 0, 3, /* sect163r2 (3) */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 223 | }; |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 224 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 225 | static const unsigned char suiteb_curves[] = { |
| 226 | 0, TLSEXT_curve_P_256, |
| 227 | 0, TLSEXT_curve_P_384 |
| 228 | }; |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 229 | |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 230 | int tls1_ec_curve_id2nid(int curve_id, unsigned int *pflags) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 231 | { |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 232 | const tls_curve_info *cinfo; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 233 | /* ECC curves from RFC 4492 and RFC 7027 */ |
Dr. Stephen Henson | b6eb982 | 2015-05-02 18:30:00 +0100 | [diff] [blame] | 234 | if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list))) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 235 | return 0; |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 236 | cinfo = nid_list + curve_id - 1; |
| 237 | if (pflags) |
| 238 | *pflags = cinfo->flags; |
| 239 | return cinfo->nid; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 240 | } |
Dr. Stephen Henson | 525de5d | 2007-08-12 23:59:05 +0000 | [diff] [blame] | 241 | |
| 242 | int tls1_ec_nid2curve_id(int nid) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 243 | { |
Dr. Stephen Henson | 2fa2d15 | 2016-02-13 15:28:25 +0000 | [diff] [blame] | 244 | size_t i; |
| 245 | for (i = 0; i < OSSL_NELEM(nid_list); i++) { |
| 246 | if (nid_list[i].nid == nid) |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 247 | return (int)(i + 1); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 248 | } |
Dr. Stephen Henson | 2fa2d15 | 2016-02-13 15:28:25 +0000 | [diff] [blame] | 249 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Emilia Kasper | 740580c | 2014-12-01 16:55:55 +0100 | [diff] [blame] | 252 | /* |
| 253 | * Get curves list, if "sess" is set return client curves otherwise |
| 254 | * preferred list. |
| 255 | * Sets |num_curves| to the number of curves in the list, i.e., |
| 256 | * the length of |pcurves| is 2 * num_curves. |
| 257 | * Returns 1 on success and 0 if the client curves list has invalid format. |
| 258 | * The latter indicates an internal error: we should not be accepting such |
| 259 | * lists in the first place. |
| 260 | * TODO(emilia): we should really be storing the curves list in explicitly |
| 261 | * parsed form instead. (However, this would affect binary compatibility |
| 262 | * so cannot happen in the 1.0.x series.) |
Dr. Stephen Henson | fd2b65c | 2012-04-04 14:41:01 +0000 | [diff] [blame] | 263 | */ |
Matt Caswell | 6b473ac | 2016-11-24 16:59:48 +0000 | [diff] [blame^] | 264 | int tls1_get_curvelist(SSL *s, int sess, const unsigned char **pcurves, |
| 265 | size_t *num_curves) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 266 | { |
| 267 | size_t pcurveslen = 0; |
| 268 | if (sess) { |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 269 | *pcurves = s->session->tlsext_supportedgroupslist; |
| 270 | pcurveslen = s->session->tlsext_supportedgroupslist_length; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 271 | } else { |
| 272 | /* For Suite B mode only include P-256, P-384 */ |
| 273 | switch (tls1_suiteb(s)) { |
| 274 | case SSL_CERT_FLAG_SUITEB_128_LOS: |
| 275 | *pcurves = suiteb_curves; |
| 276 | pcurveslen = sizeof(suiteb_curves); |
| 277 | break; |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 278 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 279 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: |
| 280 | *pcurves = suiteb_curves; |
| 281 | pcurveslen = 2; |
| 282 | break; |
Dr. Stephen Henson | b34aa49 | 2012-12-10 02:02:16 +0000 | [diff] [blame] | 283 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 284 | case SSL_CERT_FLAG_SUITEB_192_LOS: |
| 285 | *pcurves = suiteb_curves + 2; |
| 286 | pcurveslen = 2; |
| 287 | break; |
| 288 | default: |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 289 | *pcurves = s->tlsext_supportedgroupslist; |
| 290 | pcurveslen = s->tlsext_supportedgroupslist_length; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 291 | } |
| 292 | if (!*pcurves) { |
Kurt Roeckx | fe6ef24 | 2015-12-04 22:30:36 +0100 | [diff] [blame] | 293 | *pcurves = eccurves_default; |
| 294 | pcurveslen = sizeof(eccurves_default); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
Emilia Kasper | 740580c | 2014-12-01 16:55:55 +0100 | [diff] [blame] | 297 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 298 | /* We do not allow odd length arrays to enter the system. */ |
| 299 | if (pcurveslen & 1) { |
| 300 | SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR); |
| 301 | *num_curves = 0; |
| 302 | return 0; |
| 303 | } else { |
| 304 | *num_curves = pcurveslen / 2; |
| 305 | return 1; |
| 306 | } |
| 307 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 308 | |
| 309 | /* See if curve is allowed by security callback */ |
Matt Caswell | 6b473ac | 2016-11-24 16:59:48 +0000 | [diff] [blame^] | 310 | int tls_curve_allowed(SSL *s, const unsigned char *curve, int op) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 311 | { |
| 312 | const tls_curve_info *cinfo; |
| 313 | if (curve[0]) |
| 314 | return 1; |
Dr. Stephen Henson | b6eb982 | 2015-05-02 18:30:00 +0100 | [diff] [blame] | 315 | if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list))) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 316 | return 0; |
| 317 | cinfo = &nid_list[curve[1] - 1]; |
| 318 | # ifdef OPENSSL_NO_EC2M |
| 319 | if (cinfo->flags & TLS_CURVE_CHAR2) |
| 320 | return 0; |
| 321 | # endif |
| 322 | return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve); |
| 323 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 324 | |
Dr. Stephen Henson | d18b716 | 2012-07-24 13:47:40 +0000 | [diff] [blame] | 325 | /* Check a curve is one of our preferences */ |
| 326 | 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] | 327 | { |
| 328 | const unsigned char *curves; |
| 329 | size_t num_curves, i; |
| 330 | unsigned int suiteb_flags = tls1_suiteb(s); |
| 331 | if (len != 3 || p[0] != NAMED_CURVE_TYPE) |
| 332 | return 0; |
| 333 | /* Check curve matches Suite B preferences */ |
| 334 | if (suiteb_flags) { |
| 335 | unsigned long cid = s->s3->tmp.new_cipher->id; |
| 336 | if (p[1]) |
| 337 | return 0; |
| 338 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) { |
| 339 | if (p[2] != TLSEXT_curve_P_256) |
| 340 | return 0; |
| 341 | } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) { |
| 342 | if (p[2] != TLSEXT_curve_P_384) |
| 343 | return 0; |
| 344 | } else /* Should never happen */ |
| 345 | return 0; |
| 346 | } |
| 347 | if (!tls1_get_curvelist(s, 0, &curves, &num_curves)) |
| 348 | return 0; |
| 349 | for (i = 0; i < num_curves; i++, curves += 2) { |
| 350 | if (p[1] == curves[0] && p[2] == curves[1]) |
| 351 | return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK); |
| 352 | } |
| 353 | return 0; |
| 354 | } |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 355 | |
Tim Hudson | 1d97c84 | 2014-12-28 12:48:40 +1000 | [diff] [blame] | 356 | /*- |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 357 | * For nmatch >= 0, return the NID of the |nmatch|th shared group or NID_undef |
Kurt Roeckx | 6977e8e | 2015-12-04 22:25:11 +0100 | [diff] [blame] | 358 | * if there is no match. |
| 359 | * For nmatch == -1, return number of matches |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 360 | * For nmatch == -2, return the NID of the group to use for |
Emilia Kasper | 376e2ca | 2014-12-04 15:00:11 +0100 | [diff] [blame] | 361 | * an EC tmp key, or NID_undef if there is no match. |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 362 | */ |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 363 | int tls1_shared_group(SSL *s, int nmatch) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 364 | { |
| 365 | const unsigned char *pref, *supp; |
| 366 | size_t num_pref, num_supp, i, j; |
| 367 | int k; |
| 368 | /* Can't do anything on client side */ |
| 369 | if (s->server == 0) |
| 370 | return -1; |
| 371 | if (nmatch == -2) { |
| 372 | if (tls1_suiteb(s)) { |
| 373 | /* |
| 374 | * For Suite B ciphersuite determines curve: we already know |
| 375 | * these are acceptable due to previous checks. |
| 376 | */ |
| 377 | unsigned long cid = s->s3->tmp.new_cipher->id; |
| 378 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
| 379 | return NID_X9_62_prime256v1; /* P-256 */ |
| 380 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
| 381 | return NID_secp384r1; /* P-384 */ |
| 382 | /* Should never happen */ |
| 383 | return NID_undef; |
| 384 | } |
| 385 | /* If not Suite B just return first preference shared curve */ |
| 386 | nmatch = 0; |
| 387 | } |
| 388 | /* |
| 389 | * Avoid truncation. tls1_get_curvelist takes an int |
| 390 | * but s->options is a long... |
| 391 | */ |
| 392 | if (!tls1_get_curvelist |
| 393 | (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp, |
| 394 | &num_supp)) |
| 395 | /* In practice, NID_undef == 0 but let's be precise. */ |
| 396 | return nmatch == -1 ? 0 : NID_undef; |
| 397 | if (!tls1_get_curvelist |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 398 | (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref, &num_pref)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 399 | return nmatch == -1 ? 0 : NID_undef; |
Kurt Roeckx | 3c06513 | 2015-05-30 19:20:12 +0200 | [diff] [blame] | 400 | |
| 401 | /* |
| 402 | * If the client didn't send the elliptic_curves extension all of them |
| 403 | * are allowed. |
| 404 | */ |
| 405 | if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) { |
| 406 | supp = eccurves_all; |
| 407 | num_supp = sizeof(eccurves_all) / 2; |
| 408 | } else if (num_pref == 0 && |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 409 | (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) { |
Kurt Roeckx | 3c06513 | 2015-05-30 19:20:12 +0200 | [diff] [blame] | 410 | pref = eccurves_all; |
| 411 | num_pref = sizeof(eccurves_all) / 2; |
| 412 | } |
| 413 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 414 | k = 0; |
| 415 | for (i = 0; i < num_pref; i++, pref += 2) { |
| 416 | const unsigned char *tsupp = supp; |
| 417 | for (j = 0; j < num_supp; j++, tsupp += 2) { |
| 418 | if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) { |
| 419 | if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED)) |
| 420 | continue; |
| 421 | if (nmatch == k) { |
| 422 | int id = (pref[0] << 8) | pref[1]; |
Dr. Stephen Henson | ec24630 | 2016-08-11 15:41:49 +0100 | [diff] [blame] | 423 | return tls1_ec_curve_id2nid(id, NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 424 | } |
| 425 | k++; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | if (nmatch == -1) |
| 430 | return k; |
| 431 | /* Out of range (nmatch > k). */ |
| 432 | return NID_undef; |
| 433 | } |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 434 | |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 435 | int tls1_set_groups(unsigned char **pext, size_t *pextlen, |
| 436 | int *groups, size_t ngroups) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 437 | { |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 438 | unsigned char *glist, *p; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 439 | size_t i; |
| 440 | /* |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 441 | * Bitmap of groups included to detect duplicates: only works while group |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 442 | * ids < 32 |
| 443 | */ |
| 444 | unsigned long dup_list = 0; |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 445 | glist = OPENSSL_malloc(ngroups * 2); |
| 446 | if (glist == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 447 | return 0; |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 448 | for (i = 0, p = glist; i < ngroups; i++) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 449 | unsigned long idmask; |
| 450 | int id; |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 451 | /* TODO(TLS1.3): Convert for DH groups */ |
| 452 | id = tls1_ec_nid2curve_id(groups[i]); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 453 | idmask = 1L << id; |
| 454 | if (!id || (dup_list & idmask)) { |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 455 | OPENSSL_free(glist); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | dup_list |= idmask; |
| 459 | s2n(id, p); |
| 460 | } |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 461 | OPENSSL_free(*pext); |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 462 | *pext = glist; |
| 463 | *pextlen = ngroups * 2; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 464 | return 1; |
| 465 | } |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 466 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 467 | # define MAX_CURVELIST 28 |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 468 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 469 | typedef struct { |
| 470 | size_t nidcnt; |
| 471 | int nid_arr[MAX_CURVELIST]; |
| 472 | } nid_cb_st; |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 473 | |
| 474 | static int nid_cb(const char *elem, int len, void *arg) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 475 | { |
| 476 | nid_cb_st *narg = arg; |
| 477 | size_t i; |
| 478 | int nid; |
| 479 | char etmp[20]; |
Kurt Roeckx | 2747d73 | 2015-01-24 14:46:50 +0100 | [diff] [blame] | 480 | if (elem == NULL) |
| 481 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 482 | if (narg->nidcnt == MAX_CURVELIST) |
| 483 | return 0; |
| 484 | if (len > (int)(sizeof(etmp) - 1)) |
| 485 | return 0; |
| 486 | memcpy(etmp, elem, len); |
| 487 | etmp[len] = 0; |
| 488 | nid = EC_curve_nist2nid(etmp); |
| 489 | if (nid == NID_undef) |
| 490 | nid = OBJ_sn2nid(etmp); |
| 491 | if (nid == NID_undef) |
| 492 | nid = OBJ_ln2nid(etmp); |
| 493 | if (nid == NID_undef) |
| 494 | return 0; |
| 495 | for (i = 0; i < narg->nidcnt; i++) |
| 496 | if (narg->nid_arr[i] == nid) |
| 497 | return 0; |
| 498 | narg->nid_arr[narg->nidcnt++] = nid; |
| 499 | return 1; |
| 500 | } |
| 501 | |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 502 | /* Set groups based on a colon separate list */ |
| 503 | int tls1_set_groups_list(unsigned char **pext, size_t *pextlen, const char *str) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 504 | { |
| 505 | nid_cb_st ncb; |
| 506 | ncb.nidcnt = 0; |
| 507 | if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb)) |
| 508 | return 0; |
| 509 | if (pext == NULL) |
| 510 | return 1; |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 511 | return tls1_set_groups(pext, pextlen, ncb.nid_arr, ncb.nidcnt); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Dr. Stephen Henson | fd2b65c | 2012-04-04 14:41:01 +0000 | [diff] [blame] | 514 | /* For an EC key set TLS id and required compression based on parameters */ |
| 515 | static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 516 | EC_KEY *ec) |
| 517 | { |
Dr. Stephen Henson | 2235b7f | 2016-02-13 15:26:15 +0000 | [diff] [blame] | 518 | int id; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 519 | const EC_GROUP *grp; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 520 | if (!ec) |
| 521 | return 0; |
| 522 | /* Determine if it is a prime field */ |
| 523 | grp = EC_KEY_get0_group(ec); |
| 524 | if (!grp) |
| 525 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 526 | /* Determine curve ID */ |
| 527 | id = EC_GROUP_get_curve_name(grp); |
| 528 | id = tls1_ec_nid2curve_id(id); |
Dr. Stephen Henson | 2235b7f | 2016-02-13 15:26:15 +0000 | [diff] [blame] | 529 | /* If no id return error: we don't support arbitrary explicit curves */ |
| 530 | if (id == 0) |
| 531 | return 0; |
| 532 | curve_id[0] = 0; |
| 533 | curve_id[1] = (unsigned char)id; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 534 | if (comp_id) { |
| 535 | if (EC_KEY_get0_public_key(ec) == NULL) |
| 536 | return 0; |
Dr. Stephen Henson | 2235b7f | 2016-02-13 15:26:15 +0000 | [diff] [blame] | 537 | if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) { |
| 538 | *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; |
| 539 | } else { |
| 540 | if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 541 | *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
| 542 | else |
| 543 | *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; |
Dr. Stephen Henson | 2235b7f | 2016-02-13 15:26:15 +0000 | [diff] [blame] | 544 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 545 | } |
| 546 | return 1; |
| 547 | } |
| 548 | |
Dr. Stephen Henson | fd2b65c | 2012-04-04 14:41:01 +0000 | [diff] [blame] | 549 | /* Check an EC key is compatible with extensions */ |
| 550 | static int tls1_check_ec_key(SSL *s, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 551 | unsigned char *curve_id, unsigned char *comp_id) |
| 552 | { |
| 553 | const unsigned char *pformats, *pcurves; |
| 554 | size_t num_formats, num_curves, i; |
| 555 | int j; |
| 556 | /* |
| 557 | * If point formats extension present check it, otherwise everything is |
| 558 | * supported (see RFC4492). |
| 559 | */ |
| 560 | if (comp_id && s->session->tlsext_ecpointformatlist) { |
| 561 | pformats = s->session->tlsext_ecpointformatlist; |
| 562 | num_formats = s->session->tlsext_ecpointformatlist_length; |
| 563 | for (i = 0; i < num_formats; i++, pformats++) { |
| 564 | if (*comp_id == *pformats) |
| 565 | break; |
| 566 | } |
| 567 | if (i == num_formats) |
| 568 | return 0; |
| 569 | } |
| 570 | if (!curve_id) |
| 571 | return 1; |
| 572 | /* Check curve is consistent with client and server preferences */ |
| 573 | for (j = 0; j <= 1; j++) { |
| 574 | if (!tls1_get_curvelist(s, j, &pcurves, &num_curves)) |
| 575 | return 0; |
Matt Caswell | b79d241 | 2015-03-20 15:10:16 +0000 | [diff] [blame] | 576 | if (j == 1 && num_curves == 0) { |
| 577 | /* |
| 578 | * If we've not received any curves then skip this check. |
| 579 | * RFC 4492 does not require the supported elliptic curves extension |
| 580 | * so if it is not sent we can just choose any curve. |
| 581 | * It is invalid to send an empty list in the elliptic curves |
| 582 | * extension, so num_curves == 0 always means no extension. |
| 583 | */ |
| 584 | break; |
| 585 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 586 | for (i = 0; i < num_curves; i++, pcurves += 2) { |
| 587 | if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1]) |
| 588 | break; |
| 589 | } |
| 590 | if (i == num_curves) |
| 591 | return 0; |
| 592 | /* For clients can only check sent curve list */ |
| 593 | if (!s->server) |
| 594 | break; |
| 595 | } |
| 596 | return 1; |
| 597 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 598 | |
Dr. Stephen Henson | 5087afa | 2012-11-26 18:38:10 +0000 | [diff] [blame] | 599 | static void tls1_get_formatlist(SSL *s, const unsigned char **pformats, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 600 | size_t *num_formats) |
| 601 | { |
| 602 | /* |
| 603 | * If we have a custom point format list use it otherwise use default |
| 604 | */ |
| 605 | if (s->tlsext_ecpointformatlist) { |
| 606 | *pformats = s->tlsext_ecpointformatlist; |
| 607 | *num_formats = s->tlsext_ecpointformatlist_length; |
| 608 | } else { |
| 609 | *pformats = ecformats_default; |
| 610 | /* For Suite B we don't support char2 fields */ |
| 611 | if (tls1_suiteb(s)) |
| 612 | *num_formats = sizeof(ecformats_default) - 1; |
| 613 | else |
| 614 | *num_formats = sizeof(ecformats_default); |
| 615 | } |
| 616 | } |
Dr. Stephen Henson | 5087afa | 2012-11-26 18:38:10 +0000 | [diff] [blame] | 617 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 618 | /* |
| 619 | * Check cert parameters compatible with extensions: currently just checks EC |
| 620 | * certificates have compatible curves and compression. |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 621 | */ |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 622 | 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] | 623 | { |
| 624 | unsigned char comp_id, curve_id[2]; |
| 625 | EVP_PKEY *pkey; |
| 626 | int rv; |
Dr. Stephen Henson | 8382fd3 | 2015-12-20 00:32:36 +0000 | [diff] [blame] | 627 | pkey = X509_get0_pubkey(x); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 628 | if (!pkey) |
| 629 | return 0; |
| 630 | /* If not EC nothing to do */ |
Dr. Stephen Henson | 3aeb934 | 2016-01-19 00:21:12 +0000 | [diff] [blame] | 631 | if (EVP_PKEY_id(pkey) != EVP_PKEY_EC) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 632 | return 1; |
Dr. Stephen Henson | 3aeb934 | 2016-01-19 00:21:12 +0000 | [diff] [blame] | 633 | rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 634 | if (!rv) |
| 635 | return 0; |
| 636 | /* |
| 637 | * Can't check curve_id for client certs as we don't have a supported |
| 638 | * curves extension. |
| 639 | */ |
| 640 | rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id); |
| 641 | if (!rv) |
| 642 | return 0; |
| 643 | /* |
| 644 | * Special case for suite B. We *MUST* sign using SHA256+P-256 or |
| 645 | * SHA384+P-384, adjust digest if necessary. |
| 646 | */ |
| 647 | if (set_ee_md && tls1_suiteb(s)) { |
| 648 | int check_md; |
| 649 | size_t i; |
| 650 | CERT *c = s->cert; |
| 651 | if (curve_id[0]) |
| 652 | return 0; |
| 653 | /* Check to see we have necessary signing algorithm */ |
| 654 | if (curve_id[1] == TLSEXT_curve_P_256) |
| 655 | check_md = NID_ecdsa_with_SHA256; |
| 656 | else if (curve_id[1] == TLSEXT_curve_P_384) |
| 657 | check_md = NID_ecdsa_with_SHA384; |
| 658 | else |
| 659 | return 0; /* Should never happen */ |
| 660 | for (i = 0; i < c->shared_sigalgslen; i++) |
| 661 | if (check_md == c->shared_sigalgs[i].signandhash_nid) |
| 662 | break; |
| 663 | if (i == c->shared_sigalgslen) |
| 664 | return 0; |
| 665 | if (set_ee_md == 2) { |
| 666 | if (check_md == NID_ecdsa_with_SHA256) |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 667 | s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256(); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 668 | else |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 669 | s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384(); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | return rv; |
| 673 | } |
| 674 | |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 675 | # ifndef OPENSSL_NO_EC |
Kurt Roeckx | 6977e8e | 2015-12-04 22:25:11 +0100 | [diff] [blame] | 676 | /* |
FdaSilvaYY | 8483a00 | 2016-03-10 21:34:48 +0100 | [diff] [blame] | 677 | * tls1_check_ec_tmp_key - Check EC temporary key compatibility |
Kurt Roeckx | 6977e8e | 2015-12-04 22:25:11 +0100 | [diff] [blame] | 678 | * @s: SSL connection |
| 679 | * @cid: Cipher ID we're considering using |
| 680 | * |
| 681 | * Checks that the kECDHE cipher suite we're considering using |
| 682 | * is compatible with the client extensions. |
| 683 | * |
| 684 | * Returns 0 when the cipher can't be used or 1 when it can. |
| 685 | */ |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 686 | int tls1_check_ec_tmp_key(SSL *s, unsigned long cid) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 687 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 688 | /* |
| 689 | * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other |
| 690 | * curves permitted. |
| 691 | */ |
| 692 | if (tls1_suiteb(s)) { |
Kurt Roeckx | 6977e8e | 2015-12-04 22:25:11 +0100 | [diff] [blame] | 693 | unsigned char curve_id[2]; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 694 | /* Curve to check determined by ciphersuite */ |
| 695 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
| 696 | curve_id[1] = TLSEXT_curve_P_256; |
| 697 | else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
| 698 | curve_id[1] = TLSEXT_curve_P_384; |
| 699 | else |
| 700 | return 0; |
| 701 | curve_id[0] = 0; |
| 702 | /* Check this curve is acceptable */ |
| 703 | if (!tls1_check_ec_key(s, curve_id, NULL)) |
| 704 | return 0; |
Kurt Roeckx | fe6ef24 | 2015-12-04 22:30:36 +0100 | [diff] [blame] | 705 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 706 | } |
Kurt Roeckx | fe6ef24 | 2015-12-04 22:30:36 +0100 | [diff] [blame] | 707 | /* Need a shared curve */ |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 708 | if (tls1_shared_group(s, 0)) |
Kurt Roeckx | fe6ef24 | 2015-12-04 22:30:36 +0100 | [diff] [blame] | 709 | return 1; |
Kurt Roeckx | 6977e8e | 2015-12-04 22:25:11 +0100 | [diff] [blame] | 710 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 711 | } |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 712 | # endif /* OPENSSL_NO_EC */ |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 713 | |
Dr. Stephen Henson | 14536c8 | 2013-08-17 17:40:08 +0100 | [diff] [blame] | 714 | #else |
| 715 | |
| 716 | 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] | 717 | { |
| 718 | return 1; |
| 719 | } |
Dr. Stephen Henson | 14536c8 | 2013-08-17 17:40:08 +0100 | [diff] [blame] | 720 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 721 | #endif /* OPENSSL_NO_EC */ |
Bodo Möller | f1fd454 | 2006-01-03 03:27:19 +0000 | [diff] [blame] | 722 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 723 | /* |
| 724 | * List of supported signature algorithms and hashes. Should make this |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 725 | * customisable at some point, for now include everything we support. |
| 726 | */ |
| 727 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 728 | #ifdef OPENSSL_NO_RSA |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 729 | # define tlsext_sigalg_rsa(md) /* */ |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 730 | #else |
| 731 | # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa, |
| 732 | #endif |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 733 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 734 | #ifdef OPENSSL_NO_DSA |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 735 | # define tlsext_sigalg_dsa(md) /* */ |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 736 | #else |
| 737 | # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa, |
| 738 | #endif |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 739 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 740 | #ifdef OPENSSL_NO_EC |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 741 | # define tlsext_sigalg_ecdsa(md)/* */ |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 742 | #else |
| 743 | # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa, |
| 744 | #endif |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 745 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 746 | #define tlsext_sigalg(md) \ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 747 | tlsext_sigalg_rsa(md) \ |
| 748 | tlsext_sigalg_dsa(md) \ |
| 749 | tlsext_sigalg_ecdsa(md) |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 750 | |
Cristian Rodríguez | d97ed21 | 2014-04-20 18:41:15 -0300 | [diff] [blame] | 751 | static const unsigned char tls12_sigalgs[] = { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 752 | tlsext_sigalg(TLSEXT_hash_sha512) |
| 753 | tlsext_sigalg(TLSEXT_hash_sha384) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 754 | tlsext_sigalg(TLSEXT_hash_sha256) |
| 755 | tlsext_sigalg(TLSEXT_hash_sha224) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 756 | tlsext_sigalg(TLSEXT_hash_sha1) |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 757 | #ifndef OPENSSL_NO_GOST |
| 758 | TLSEXT_hash_gostr3411, TLSEXT_signature_gostr34102001, |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 759 | TLSEXT_hash_gostr34112012_256, TLSEXT_signature_gostr34102012_256, |
| 760 | TLSEXT_hash_gostr34112012_512, TLSEXT_signature_gostr34102012_512 |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 761 | #endif |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 762 | }; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 763 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 764 | #ifndef OPENSSL_NO_EC |
Cristian Rodríguez | d97ed21 | 2014-04-20 18:41:15 -0300 | [diff] [blame] | 765 | static const unsigned char suiteb_sigalgs[] = { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 766 | tlsext_sigalg_ecdsa(TLSEXT_hash_sha256) |
| 767 | tlsext_sigalg_ecdsa(TLSEXT_hash_sha384) |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 768 | }; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 769 | #endif |
Dr. Stephen Henson | b7bfe69 | 2012-07-18 14:09:46 +0000 | [diff] [blame] | 770 | size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 771 | { |
| 772 | /* |
| 773 | * If Suite B mode use Suite B sigalgs only, ignore any other |
| 774 | * preferences. |
| 775 | */ |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 776 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 777 | switch (tls1_suiteb(s)) { |
| 778 | case SSL_CERT_FLAG_SUITEB_128_LOS: |
| 779 | *psigs = suiteb_sigalgs; |
| 780 | return sizeof(suiteb_sigalgs); |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 781 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 782 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: |
| 783 | *psigs = suiteb_sigalgs; |
| 784 | return 2; |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 785 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 786 | case SSL_CERT_FLAG_SUITEB_192_LOS: |
| 787 | *psigs = suiteb_sigalgs + 2; |
| 788 | return 2; |
| 789 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 790 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 791 | /* If server use client authentication sigalgs if not NULL */ |
| 792 | if (s->server && s->cert->client_sigalgs) { |
| 793 | *psigs = s->cert->client_sigalgs; |
| 794 | return s->cert->client_sigalgslen; |
| 795 | } else if (s->cert->conf_sigalgs) { |
| 796 | *psigs = s->cert->conf_sigalgs; |
| 797 | return s->cert->conf_sigalgslen; |
| 798 | } else { |
| 799 | *psigs = tls12_sigalgs; |
| 800 | return sizeof(tls12_sigalgs); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | * Check signature algorithm is consistent with sent supported signature |
Dr. Stephen Henson | ec4a50b | 2012-07-24 18:11:27 +0000 | [diff] [blame] | 806 | * algorithms and if so return relevant digest. |
| 807 | */ |
| 808 | int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 809 | const unsigned char *sig, EVP_PKEY *pkey) |
| 810 | { |
| 811 | const unsigned char *sent_sigs; |
| 812 | size_t sent_sigslen, i; |
| 813 | int sigalg = tls12_get_sigid(pkey); |
| 814 | /* Should never happen */ |
| 815 | if (sigalg == -1) |
| 816 | return -1; |
| 817 | /* Check key type is consistent with signature */ |
| 818 | if (sigalg != (int)sig[1]) { |
| 819 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
| 820 | return 0; |
| 821 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 822 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | 3aeb934 | 2016-01-19 00:21:12 +0000 | [diff] [blame] | 823 | if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 824 | unsigned char curve_id[2], comp_id; |
| 825 | /* Check compression and curve matches extensions */ |
Dr. Stephen Henson | 3aeb934 | 2016-01-19 00:21:12 +0000 | [diff] [blame] | 826 | if (!tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey))) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 827 | return 0; |
| 828 | if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) { |
| 829 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); |
| 830 | return 0; |
| 831 | } |
| 832 | /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */ |
| 833 | if (tls1_suiteb(s)) { |
| 834 | if (curve_id[0]) |
| 835 | return 0; |
| 836 | if (curve_id[1] == TLSEXT_curve_P_256) { |
| 837 | if (sig[0] != TLSEXT_hash_sha256) { |
| 838 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, |
| 839 | SSL_R_ILLEGAL_SUITEB_DIGEST); |
| 840 | return 0; |
| 841 | } |
| 842 | } else if (curve_id[1] == TLSEXT_curve_P_384) { |
| 843 | if (sig[0] != TLSEXT_hash_sha384) { |
| 844 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, |
| 845 | SSL_R_ILLEGAL_SUITEB_DIGEST); |
| 846 | return 0; |
| 847 | } |
| 848 | } else |
| 849 | return 0; |
| 850 | } |
| 851 | } else if (tls1_suiteb(s)) |
| 852 | return 0; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 853 | #endif |
Dr. Stephen Henson | 2ea8035 | 2012-08-15 15:15:05 +0000 | [diff] [blame] | 854 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 855 | /* Check signature matches a type we sent */ |
| 856 | sent_sigslen = tls12_get_psigalgs(s, &sent_sigs); |
| 857 | for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) { |
| 858 | if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1]) |
| 859 | break; |
| 860 | } |
| 861 | /* Allow fallback to SHA1 if not strict mode */ |
| 862 | if (i == sent_sigslen |
| 863 | && (sig[0] != TLSEXT_hash_sha1 |
| 864 | || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { |
| 865 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
| 866 | return 0; |
| 867 | } |
| 868 | *pmd = tls12_get_hash(sig[0]); |
| 869 | if (*pmd == NULL) { |
| 870 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST); |
| 871 | return 0; |
| 872 | } |
| 873 | /* Make sure security callback allows algorithm */ |
| 874 | if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK, |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 875 | EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd), (void *)sig)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 876 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
| 877 | return 0; |
| 878 | } |
| 879 | /* |
| 880 | * Store the digest used so applications can retrieve it if they wish. |
| 881 | */ |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 882 | s->s3->tmp.peer_md = *pmd; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 883 | return 1; |
| 884 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 885 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 886 | /* |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 887 | * Set a mask of disabled algorithms: an algorithm is disabled if it isn't |
| 888 | * supported, doesn't appear in supported signature algorithms, isn't supported |
| 889 | * by the enabled protocol versions or by the security level. |
| 890 | * |
| 891 | * This function should only be used for checking which ciphers are supported |
| 892 | * by the client. |
| 893 | * |
| 894 | * 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] | 895 | */ |
| 896 | void ssl_set_client_disabled(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 897 | { |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 898 | s->s3->tmp.mask_a = 0; |
| 899 | s->s3->tmp.mask_k = 0; |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 900 | ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK); |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 901 | ssl_get_client_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver); |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 902 | #ifndef OPENSSL_NO_PSK |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 903 | /* with PSK there must be client callback set */ |
| 904 | if (!s->psk_client_callback) { |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 905 | s->s3->tmp.mask_a |= SSL_aPSK; |
Dr. Stephen Henson | fe5eef3 | 2015-06-28 17:01:07 +0100 | [diff] [blame] | 906 | s->s3->tmp.mask_k |= SSL_PSK; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 907 | } |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 908 | #endif /* OPENSSL_NO_PSK */ |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 909 | #ifndef OPENSSL_NO_SRP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 910 | if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) { |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 911 | s->s3->tmp.mask_a |= SSL_aSRP; |
| 912 | s->s3->tmp.mask_k |= SSL_kSRP; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 913 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 914 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 915 | } |
Dr. Stephen Henson | fc101f8 | 2011-05-11 16:33:28 +0000 | [diff] [blame] | 916 | |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 917 | /* |
| 918 | * ssl_cipher_disabled - check that a cipher is disabled or not |
| 919 | * @s: SSL connection that you want to use the cipher on |
| 920 | * @c: cipher to check |
| 921 | * @op: Security check that you want to do |
| 922 | * |
| 923 | * Returns 1 when it's disabled, 0 when enabled. |
| 924 | */ |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 925 | int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 926 | { |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 927 | if (c->algorithm_mkey & s->s3->tmp.mask_k |
Dr. Stephen Henson | 4d69f9e | 2015-05-18 23:29:57 +0100 | [diff] [blame] | 928 | || c->algorithm_auth & s->s3->tmp.mask_a) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 929 | return 1; |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 930 | if (s->s3->tmp.max_ver == 0) |
| 931 | return 1; |
| 932 | if (!SSL_IS_DTLS(s) && ((c->min_tls > s->s3->tmp.max_ver) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 933 | || (c->max_tls < s->s3->tmp.min_ver))) |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 934 | return 1; |
| 935 | 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] | 936 | || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver))) |
Kurt Roeckx | 3eb2aff | 2016-02-07 20:17:07 +0100 | [diff] [blame] | 937 | return 1; |
| 938 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 939 | return !ssl_security(s, op, c->strength_bits, 0, (void *)c); |
| 940 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 941 | |
| 942 | static int tls_use_ticket(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 943 | { |
Matt Caswell | f5ca0b0 | 2016-11-21 12:10:35 +0000 | [diff] [blame] | 944 | if ((s->options & SSL_OP_NO_TICKET) || SSL_IS_TLS13(s)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 945 | return 0; |
| 946 | return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL); |
| 947 | } |
Dr. Stephen Henson | 8b8e5be | 2014-01-14 14:55:21 +0000 | [diff] [blame] | 948 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 949 | static int compare_uint(const void *p1, const void *p2) |
| 950 | { |
Emilia Kasper | aa474d1 | 2016-02-19 17:24:44 +0100 | [diff] [blame] | 951 | unsigned int u1 = *((const unsigned int *)p1); |
| 952 | unsigned int u2 = *((const unsigned int *)p2); |
| 953 | if (u1 < u2) |
| 954 | return -1; |
| 955 | else if (u1 > u2) |
| 956 | return 1; |
| 957 | else |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | /* |
| 962 | * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be |
| 963 | * more than one extension of the same type in a ClientHello or ServerHello. |
| 964 | * This function does an initial scan over the extensions block to filter those |
| 965 | * out. It returns 1 if all extensions are unique, and 0 if the extensions |
| 966 | * contain duplicates, could not be successfully parsed, or an internal error |
| 967 | * occurred. |
| 968 | */ |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 969 | static int tls1_check_duplicate_extensions(const PACKET *packet) |
| 970 | { |
Emilia Kasper | aa474d1 | 2016-02-19 17:24:44 +0100 | [diff] [blame] | 971 | PACKET extensions = *packet; |
| 972 | size_t num_extensions = 0, i = 0; |
| 973 | unsigned int *extension_types = NULL; |
| 974 | int ret = 0; |
| 975 | |
| 976 | /* First pass: count the extensions. */ |
| 977 | while (PACKET_remaining(&extensions) > 0) { |
| 978 | unsigned int type; |
| 979 | PACKET extension; |
| 980 | if (!PACKET_get_net_2(&extensions, &type) || |
| 981 | !PACKET_get_length_prefixed_2(&extensions, &extension)) { |
| 982 | goto done; |
| 983 | } |
| 984 | num_extensions++; |
| 985 | } |
| 986 | |
| 987 | if (num_extensions <= 1) |
| 988 | return 1; |
| 989 | |
| 990 | extension_types = OPENSSL_malloc(sizeof(unsigned int) * num_extensions); |
| 991 | if (extension_types == NULL) { |
| 992 | SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_MALLOC_FAILURE); |
| 993 | goto done; |
| 994 | } |
| 995 | |
| 996 | /* Second pass: gather the extension types. */ |
| 997 | extensions = *packet; |
| 998 | for (i = 0; i < num_extensions; i++) { |
| 999 | PACKET extension; |
| 1000 | if (!PACKET_get_net_2(&extensions, &extension_types[i]) || |
| 1001 | !PACKET_get_length_prefixed_2(&extensions, &extension)) { |
| 1002 | /* This should not happen. */ |
| 1003 | SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR); |
| 1004 | goto done; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | if (PACKET_remaining(&extensions) != 0) { |
| 1009 | SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR); |
| 1010 | goto done; |
| 1011 | } |
| 1012 | /* Sort the extensions and make sure there are no duplicates. */ |
| 1013 | qsort(extension_types, num_extensions, sizeof(unsigned int), compare_uint); |
| 1014 | for (i = 1; i < num_extensions; i++) { |
| 1015 | if (extension_types[i - 1] == extension_types[i]) |
| 1016 | goto done; |
| 1017 | } |
| 1018 | ret = 1; |
| 1019 | done: |
| 1020 | OPENSSL_free(extension_types); |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
Matt Caswell | ae2f7b3 | 2016-09-05 17:34:04 +0100 | [diff] [blame] | 1024 | int ssl_add_clienthello_tlsext(SSL *s, WPACKET *pkt, int *al) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1025 | { |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1026 | #ifndef OPENSSL_NO_EC |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1027 | const unsigned char *pcurves = NULL; |
| 1028 | size_t num_curves = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1029 | int using_ecc = 0; |
Matt Caswell | c805f61 | 2016-11-21 13:24:50 +0000 | [diff] [blame] | 1030 | int min_version, max_version, reason; |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1031 | |
| 1032 | /* See if we support any ECC ciphersuites */ |
Matt Caswell | c805f61 | 2016-11-21 13:24:50 +0000 | [diff] [blame] | 1033 | if ((s->version >= TLS1_VERSION && s->version <= TLS1_3_VERSION) |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1034 | || SSL_IS_DTLS(s)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1035 | int i; |
| 1036 | unsigned long alg_k, alg_a; |
| 1037 | STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s); |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 1038 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1039 | for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) { |
Dr. Stephen Henson | 4a640fb | 2015-12-23 00:47:28 +0000 | [diff] [blame] | 1040 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); |
Dr. Stephen Henson | d0595f1 | 2012-03-28 15:05:04 +0000 | [diff] [blame] | 1041 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1042 | alg_k = c->algorithm_mkey; |
| 1043 | alg_a = c->algorithm_auth; |
Dr. Stephen Henson | ce0c1f2 | 2016-02-11 18:19:27 +0000 | [diff] [blame] | 1044 | if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) |
Matt Caswell | c805f61 | 2016-11-21 13:24:50 +0000 | [diff] [blame] | 1045 | || (alg_a & SSL_aECDSA) |
| 1046 | || c->min_tls >= TLS1_3_VERSION) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1047 | using_ecc = 1; |
| 1048 | break; |
| 1049 | } |
Dr. Stephen Henson | 5a3d8ee | 2014-11-03 17:47:11 +0000 | [diff] [blame] | 1050 | } |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1051 | } |
| 1052 | #else |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 1053 | if (SSL_IS_TLS13(s)) { |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1054 | /* Shouldn't happen! */ |
| 1055 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1056 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1057 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1058 | #endif |
Dr. Stephen Henson | 5a3d8ee | 2014-11-03 17:47:11 +0000 | [diff] [blame] | 1059 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1060 | /* Add RI if renegotiating */ |
| 1061 | if (s->renegotiate) { |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1062 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate) |
Matt Caswell | 2f97192 | 2016-09-26 15:31:20 +0100 | [diff] [blame] | 1063 | || !WPACKET_start_sub_packet_u16(pkt) |
| 1064 | || !WPACKET_sub_memcpy_u8(pkt, s->s3->previous_client_finished, |
| 1065 | s->s3->previous_client_finished_len) |
| 1066 | || !WPACKET_close(pkt)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1067 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1068 | return 0; |
Dr. Stephen Henson | 860c3dd | 2009-11-11 14:51:19 +0000 | [diff] [blame] | 1069 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1070 | } |
| 1071 | /* Only add RI for SSLv3 */ |
| 1072 | if (s->client_version == SSL3_VERSION) |
| 1073 | goto done; |
Bodo Möller | 36ca4ba | 2006-03-11 23:46:37 +0000 | [diff] [blame] | 1074 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1075 | if (s->tlsext_hostname != NULL) { |
| 1076 | /* Add TLS extension servername to the Client Hello message */ |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1077 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1078 | /* Sub-packet for server_name extension */ |
Matt Caswell | de45185 | 2016-09-09 00:13:41 +0100 | [diff] [blame] | 1079 | || !WPACKET_start_sub_packet_u16(pkt) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1080 | /* Sub-packet for servername list (always 1 hostname)*/ |
Matt Caswell | de45185 | 2016-09-09 00:13:41 +0100 | [diff] [blame] | 1081 | || !WPACKET_start_sub_packet_u16(pkt) |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1082 | || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name) |
Matt Caswell | b2b3024 | 2016-09-13 11:32:52 +0100 | [diff] [blame] | 1083 | || !WPACKET_sub_memcpy_u16(pkt, s->tlsext_hostname, |
| 1084 | strlen(s->tlsext_hostname)) |
Matt Caswell | 0217dd1 | 2016-09-06 15:09:51 +0100 | [diff] [blame] | 1085 | || !WPACKET_close(pkt) |
| 1086 | || !WPACKET_close(pkt)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1087 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1088 | return 0; |
| 1089 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1090 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1091 | #ifndef OPENSSL_NO_SRP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1092 | /* Add SRP username if there is one */ |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1093 | if (s->srp_ctx.login != NULL) { |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1094 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1095 | /* Sub-packet for SRP extension */ |
Matt Caswell | de45185 | 2016-09-09 00:13:41 +0100 | [diff] [blame] | 1096 | || !WPACKET_start_sub_packet_u16(pkt) |
| 1097 | || !WPACKET_start_sub_packet_u8(pkt) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1098 | /* login must not be zero...internal error if so */ |
Matt Caswell | de45185 | 2016-09-09 00:13:41 +0100 | [diff] [blame] | 1099 | || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH) |
Matt Caswell | 0217dd1 | 2016-09-06 15:09:51 +0100 | [diff] [blame] | 1100 | || !WPACKET_memcpy(pkt, s->srp_ctx.login, |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1101 | strlen(s->srp_ctx.login)) |
Matt Caswell | 0217dd1 | 2016-09-06 15:09:51 +0100 | [diff] [blame] | 1102 | || !WPACKET_close(pkt) |
| 1103 | || !WPACKET_close(pkt)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1104 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1105 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1106 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1107 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1108 | #endif |
Ben Laurie | 333f926 | 2011-11-15 22:59:20 +0000 | [diff] [blame] | 1109 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1110 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1111 | if (using_ecc) { |
| 1112 | /* |
| 1113 | * Add TLS extension ECPointFormats to the ClientHello message |
| 1114 | */ |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1115 | const unsigned char *pformats, *pcurvestmp; |
| 1116 | size_t num_formats; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1117 | size_t i; |
Ben Laurie | 333f926 | 2011-11-15 22:59:20 +0000 | [diff] [blame] | 1118 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1119 | tls1_get_formatlist(s, &pformats, &num_formats); |
Ben Laurie | 333f926 | 2011-11-15 22:59:20 +0000 | [diff] [blame] | 1120 | |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1121 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1122 | /* Sub-packet for formats extension */ |
Matt Caswell | de45185 | 2016-09-09 00:13:41 +0100 | [diff] [blame] | 1123 | || !WPACKET_start_sub_packet_u16(pkt) |
Matt Caswell | b2b3024 | 2016-09-13 11:32:52 +0100 | [diff] [blame] | 1124 | || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats) |
Matt Caswell | 0217dd1 | 2016-09-06 15:09:51 +0100 | [diff] [blame] | 1125 | || !WPACKET_close(pkt)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1126 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1127 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1128 | } |
Dr. Stephen Henson | 0e1dba9 | 2007-10-26 12:06:36 +0000 | [diff] [blame] | 1129 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1130 | /* |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 1131 | * Add TLS extension supported_groups to the ClientHello message |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1132 | */ |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 1133 | /* TODO(TLS1.3): Add support for DHE groups */ |
| 1134 | pcurves = s->tlsext_supportedgroupslist; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1135 | if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1136 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1137 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1138 | } |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1139 | pcurvestmp = pcurves; |
Dr. Stephen Henson | 4817504 | 2011-12-31 22:59:57 +0000 | [diff] [blame] | 1140 | |
Matt Caswell | de4d764 | 2016-11-09 14:51:06 +0000 | [diff] [blame] | 1141 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups) |
| 1142 | /* Sub-packet for supported_groups extension */ |
Matt Caswell | de45185 | 2016-09-09 00:13:41 +0100 | [diff] [blame] | 1143 | || !WPACKET_start_sub_packet_u16(pkt) |
| 1144 | || !WPACKET_start_sub_packet_u16(pkt)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1145 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1146 | return 0; |
| 1147 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1148 | /* Copy curve ID if supported */ |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1149 | for (i = 0; i < num_curves; i++, pcurvestmp += 2) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1150 | if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) { |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1151 | if (!WPACKET_put_bytes_u8(pkt, pcurvestmp[0]) |
| 1152 | || !WPACKET_put_bytes_u8(pkt, pcurvestmp[1])) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1153 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, |
| 1154 | ERR_R_INTERNAL_ERROR); |
| 1155 | return 0; |
| 1156 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1157 | } |
| 1158 | } |
Matt Caswell | 0217dd1 | 2016-09-06 15:09:51 +0100 | [diff] [blame] | 1159 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1160 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1161 | return 0; |
| 1162 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1163 | } |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1164 | #endif /* OPENSSL_NO_EC */ |
Adam Langley | 6f017a8 | 2013-04-15 18:07:47 -0400 | [diff] [blame] | 1165 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1166 | if (tls_use_ticket(s)) { |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1167 | size_t ticklen; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1168 | if (!s->new_session && s->session && s->session->tlsext_tick) |
| 1169 | ticklen = s->session->tlsext_ticklen; |
| 1170 | else if (s->session && s->tlsext_session_ticket && |
| 1171 | s->tlsext_session_ticket->data) { |
| 1172 | ticklen = s->tlsext_session_ticket->length; |
| 1173 | s->session->tlsext_tick = OPENSSL_malloc(ticklen); |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1174 | if (s->session->tlsext_tick == NULL) { |
| 1175 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1176 | return 0; |
| 1177 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1178 | memcpy(s->session->tlsext_tick, |
| 1179 | s->tlsext_session_ticket->data, ticklen); |
| 1180 | s->session->tlsext_ticklen = ticklen; |
| 1181 | } else |
| 1182 | ticklen = 0; |
| 1183 | if (ticklen == 0 && s->tlsext_session_ticket && |
| 1184 | s->tlsext_session_ticket->data == NULL) |
| 1185 | goto skip_ext; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1186 | |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1187 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket) |
Matt Caswell | b2b3024 | 2016-09-13 11:32:52 +0100 | [diff] [blame] | 1188 | || !WPACKET_sub_memcpy_u16(pkt, s->session->tlsext_tick, |
| 1189 | ticklen)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1190 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1191 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | skip_ext: |
Adam Langley | 6f017a8 | 2013-04-15 18:07:47 -0400 | [diff] [blame] | 1195 | |
David Benjamin | f7aa318 | 2016-03-05 19:49:20 -0500 | [diff] [blame] | 1196 | if (SSL_CLIENT_USE_SIGALGS(s)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1197 | size_t salglen; |
| 1198 | const unsigned char *salg; |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1199 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1200 | salglen = tls12_get_psigalgs(s, &salg); |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1201 | |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1202 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1203 | /* Sub-packet for sig-algs extension */ |
Matt Caswell | de45185 | 2016-09-09 00:13:41 +0100 | [diff] [blame] | 1204 | || !WPACKET_start_sub_packet_u16(pkt) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1205 | /* Sub-packet for the actual list */ |
Matt Caswell | de45185 | 2016-09-09 00:13:41 +0100 | [diff] [blame] | 1206 | || !WPACKET_start_sub_packet_u16(pkt) |
Matt Caswell | 0217dd1 | 2016-09-06 15:09:51 +0100 | [diff] [blame] | 1207 | || !tls12_copy_sigalgs(s, pkt, salg, salglen) |
| 1208 | || !WPACKET_close(pkt) |
| 1209 | || !WPACKET_close(pkt)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1210 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1211 | return 0; |
| 1212 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1213 | } |
Matt Caswell | 3e41ac3 | 2016-03-21 16:54:53 +0000 | [diff] [blame] | 1214 | #ifndef OPENSSL_NO_OCSP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1215 | if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) { |
| 1216 | int i; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1217 | |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1218 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1219 | /* Sub-packet for status request extension */ |
Matt Caswell | de45185 | 2016-09-09 00:13:41 +0100 | [diff] [blame] | 1220 | || !WPACKET_start_sub_packet_u16(pkt) |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1221 | || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1222 | /* Sub-packet for the ids */ |
Matt Caswell | de45185 | 2016-09-09 00:13:41 +0100 | [diff] [blame] | 1223 | || !WPACKET_start_sub_packet_u16(pkt)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1224 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1225 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1226 | } |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1227 | for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) { |
| 1228 | unsigned char *idbytes; |
| 1229 | int idlen; |
| 1230 | OCSP_RESPID *id; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1231 | |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1232 | id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i); |
| 1233 | idlen = i2d_OCSP_RESPID(id, NULL); |
| 1234 | if (idlen <= 0 |
| 1235 | /* Sub-packet for an individual id */ |
Matt Caswell | dd8710d | 2016-09-13 23:59:11 +0100 | [diff] [blame] | 1236 | || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes) |
Matt Caswell | b2b3024 | 2016-09-13 11:32:52 +0100 | [diff] [blame] | 1237 | || i2d_OCSP_RESPID(id, &idbytes) != idlen) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1238 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1239 | return 0; |
| 1240 | } |
| 1241 | } |
Matt Caswell | 0217dd1 | 2016-09-06 15:09:51 +0100 | [diff] [blame] | 1242 | if (!WPACKET_close(pkt) |
Matt Caswell | de45185 | 2016-09-09 00:13:41 +0100 | [diff] [blame] | 1243 | || !WPACKET_start_sub_packet_u16(pkt)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1244 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1245 | return 0; |
| 1246 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1247 | if (s->tlsext_ocsp_exts) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1248 | unsigned char *extbytes; |
| 1249 | int extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1250 | |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1251 | if (extlen < 0) { |
| 1252 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1253 | return 0; |
| 1254 | } |
Matt Caswell | 0217dd1 | 2016-09-06 15:09:51 +0100 | [diff] [blame] | 1255 | if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1256 | || i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &extbytes) |
| 1257 | != extlen) { |
| 1258 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1259 | return 0; |
| 1260 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1261 | } |
Matt Caswell | 0217dd1 | 2016-09-06 15:09:51 +0100 | [diff] [blame] | 1262 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1263 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1264 | return 0; |
| 1265 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1266 | } |
Matt Caswell | 3e41ac3 | 2016-03-21 16:54:53 +0000 | [diff] [blame] | 1267 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1268 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1269 | #ifndef OPENSSL_NO_NEXTPROTONEG |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1270 | if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) { |
| 1271 | /* |
FdaSilvaYY | 8483a00 | 2016-03-10 21:34:48 +0100 | [diff] [blame] | 1272 | * The client advertises an empty extension to indicate its support |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1273 | * for Next Protocol Negotiation |
| 1274 | */ |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1275 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg) |
| 1276 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1277 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1278 | return 0; |
| 1279 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1280 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1281 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1282 | |
Todd Short | 817cd0d | 2016-03-05 08:47:55 -0500 | [diff] [blame] | 1283 | /* |
| 1284 | * finish_md_len is non-zero during a renegotiation, so |
| 1285 | * this avoids sending ALPN during the renegotiation |
| 1286 | * (see longer comment below) |
| 1287 | */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1288 | if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) { |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1289 | if (!WPACKET_put_bytes_u16(pkt, |
| 1290 | TLSEXT_TYPE_application_layer_protocol_negotiation) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1291 | /* Sub-packet ALPN extension */ |
Matt Caswell | de45185 | 2016-09-09 00:13:41 +0100 | [diff] [blame] | 1292 | || !WPACKET_start_sub_packet_u16(pkt) |
Matt Caswell | b2b3024 | 2016-09-13 11:32:52 +0100 | [diff] [blame] | 1293 | || !WPACKET_sub_memcpy_u16(pkt, s->alpn_client_proto_list, |
| 1294 | s->alpn_client_proto_list_len) |
Matt Caswell | 0217dd1 | 2016-09-06 15:09:51 +0100 | [diff] [blame] | 1295 | || !WPACKET_close(pkt)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1296 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1297 | return 0; |
| 1298 | } |
Todd Short | 817cd0d | 2016-03-05 08:47:55 -0500 | [diff] [blame] | 1299 | s->s3->alpn_sent = 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1300 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1301 | #ifndef OPENSSL_NO_SRTP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1302 | if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) { |
Matt Caswell | 7facdbd | 2016-09-28 13:33:41 +0100 | [diff] [blame] | 1303 | STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(s); |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1304 | SRTP_PROTECTION_PROFILE *prof; |
| 1305 | int i, ct; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1306 | |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1307 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1308 | /* Sub-packet for SRTP extension */ |
Matt Caswell | de45185 | 2016-09-09 00:13:41 +0100 | [diff] [blame] | 1309 | || !WPACKET_start_sub_packet_u16(pkt) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1310 | /* Sub-packet for the protection profile list */ |
Matt Caswell | de45185 | 2016-09-09 00:13:41 +0100 | [diff] [blame] | 1311 | || !WPACKET_start_sub_packet_u16(pkt)) { |
Matt Caswell | 69f6823 | 2015-03-06 14:37:17 +0000 | [diff] [blame] | 1312 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1313 | return 0; |
Matt Caswell | 69f6823 | 2015-03-06 14:37:17 +0000 | [diff] [blame] | 1314 | } |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1315 | ct = sk_SRTP_PROTECTION_PROFILE_num(clnt); |
| 1316 | for (i = 0; i < ct; i++) { |
| 1317 | prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i); |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1318 | if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1319 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1320 | return 0; |
| 1321 | } |
| 1322 | } |
Matt Caswell | 7facdbd | 2016-09-28 13:33:41 +0100 | [diff] [blame] | 1323 | if (!WPACKET_close(pkt) |
| 1324 | /* Add an empty use_mki value */ |
| 1325 | || !WPACKET_put_bytes_u8(pkt, 0) |
| 1326 | || !WPACKET_close(pkt)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1327 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1328 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1329 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1330 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1331 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1332 | custom_ext_init(&s->cert->cli_ext); |
| 1333 | /* Add custom TLS Extensions to ClientHello */ |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1334 | if (!custom_ext_add(s, 0, pkt, al)) { |
| 1335 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1336 | return 0; |
| 1337 | } |
| 1338 | |
David Woodhouse | cde6145 | 2016-10-14 00:26:38 +0100 | [diff] [blame] | 1339 | if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)) { |
| 1340 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac) |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1341 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
David Woodhouse | cde6145 | 2016-10-14 00:26:38 +0100 | [diff] [blame] | 1342 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1343 | return 0; |
| 1344 | } |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1345 | } |
| 1346 | |
Rob Percival | ed29e82 | 2016-03-03 16:19:23 +0000 | [diff] [blame] | 1347 | #ifndef OPENSSL_NO_CT |
| 1348 | if (s->ct_validation_callback != NULL) { |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1349 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp) |
| 1350 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1351 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1352 | return 0; |
| 1353 | } |
Rob Percival | ed29e82 | 2016-03-03 16:19:23 +0000 | [diff] [blame] | 1354 | } |
| 1355 | #endif |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1356 | |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1357 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret) |
| 1358 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1359 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1360 | return 0; |
| 1361 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1362 | |
Matt Caswell | c805f61 | 2016-11-21 13:24:50 +0000 | [diff] [blame] | 1363 | reason = ssl_get_client_min_max_version(s, &min_version, &max_version); |
| 1364 | if (reason != 0) { |
| 1365 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, reason); |
| 1366 | return 0; |
| 1367 | } |
| 1368 | |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1369 | /* TLS1.3 specific extensions */ |
Matt Caswell | c805f61 | 2016-11-21 13:24:50 +0000 | [diff] [blame] | 1370 | if (!SSL_IS_DTLS(s) && max_version >= TLS1_3_VERSION) { |
| 1371 | int currv; |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1372 | size_t i, sharessent = 0; |
| 1373 | |
| 1374 | /* TODO(TLS1.3): Should we add this extension for versions < TLS1.3? */ |
| 1375 | /* supported_versions extension */ |
Matt Caswell | b5b253b | 2016-10-21 23:40:52 +0100 | [diff] [blame] | 1376 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions) |
| 1377 | || !WPACKET_start_sub_packet_u16(pkt) |
| 1378 | || !WPACKET_start_sub_packet_u8(pkt)) { |
| 1379 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1380 | return 0; |
| 1381 | } |
Matt Caswell | c805f61 | 2016-11-21 13:24:50 +0000 | [diff] [blame] | 1382 | |
Matt Caswell | f2342b7 | 2016-11-09 14:43:05 +0000 | [diff] [blame] | 1383 | /* |
| 1384 | * TODO(TLS1.3): There is some discussion on the TLS list as to wheter |
| 1385 | * we should include versions <TLS1.2. For the moment we do. To be |
| 1386 | * reviewed later. |
| 1387 | */ |
Matt Caswell | b5b253b | 2016-10-21 23:40:52 +0100 | [diff] [blame] | 1388 | for (currv = max_version; currv >= min_version; currv--) { |
| 1389 | /* TODO(TLS1.3): Remove this first if clause prior to release!! */ |
| 1390 | if (currv == TLS1_3_VERSION) { |
| 1391 | if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)) { |
| 1392 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, |
| 1393 | ERR_R_INTERNAL_ERROR); |
| 1394 | return 0; |
| 1395 | } |
| 1396 | } else if (!WPACKET_put_bytes_u16(pkt, currv)) { |
| 1397 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1398 | return 0; |
| 1399 | } |
| 1400 | } |
| 1401 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
| 1402 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1403 | return 0; |
| 1404 | } |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1405 | |
| 1406 | |
| 1407 | /* key_share extension */ |
| 1408 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share) |
| 1409 | /* Extension data sub-packet */ |
| 1410 | || !WPACKET_start_sub_packet_u16(pkt) |
| 1411 | /* KeyShare list sub-packet */ |
| 1412 | || !WPACKET_start_sub_packet_u16(pkt)) { |
| 1413 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
| 1417 | /* |
| 1418 | * TODO(TLS1.3): Make the number of key_shares sent configurable. For |
| 1419 | * now, just send one |
| 1420 | */ |
| 1421 | for (i = 0; i < num_curves && sharessent < 1; i++, pcurves += 2) { |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 1422 | unsigned char *encodedPoint = NULL; |
| 1423 | unsigned int curve_id = 0; |
| 1424 | EVP_PKEY *key_share_key = NULL; |
| 1425 | size_t encodedlen; |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1426 | |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 1427 | if (!tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) |
| 1428 | continue; |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1429 | |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 1430 | if (s->s3->tmp.pkey != NULL) { |
| 1431 | /* Shouldn't happen! */ |
| 1432 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, |
| 1433 | ERR_R_INTERNAL_ERROR); |
| 1434 | return 0; |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1435 | } |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 1436 | |
| 1437 | /* Generate a key for this key_share */ |
| 1438 | curve_id = (pcurves[0] << 8) | pcurves[1]; |
| 1439 | key_share_key = ssl_generate_pkey_curve(curve_id); |
| 1440 | if (key_share_key == NULL) { |
| 1441 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_EVP_LIB); |
| 1442 | return 0; |
| 1443 | } |
| 1444 | |
| 1445 | /* Encode the public key. */ |
| 1446 | encodedlen = EVP_PKEY_get1_tls_encodedpoint(key_share_key, |
| 1447 | &encodedPoint); |
| 1448 | if (encodedlen == 0) { |
| 1449 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_EC_LIB); |
| 1450 | EVP_PKEY_free(key_share_key); |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
| 1454 | /* Create KeyShareEntry */ |
| 1455 | if (!WPACKET_put_bytes_u16(pkt, curve_id) |
| 1456 | || !WPACKET_sub_memcpy_u16(pkt, encodedPoint, encodedlen)) { |
| 1457 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, |
| 1458 | ERR_R_INTERNAL_ERROR); |
| 1459 | EVP_PKEY_free(key_share_key); |
| 1460 | OPENSSL_free(encodedPoint); |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | /* |
| 1465 | * TODO(TLS1.3): When changing to send more than one key_share we're |
| 1466 | * going to need to be able to save more than one EVP_PKEY. For now |
| 1467 | * we reuse the existing tmp.pkey |
| 1468 | */ |
| 1469 | s->s3->group_id = curve_id; |
| 1470 | s->s3->tmp.pkey = key_share_key; |
| 1471 | sharessent++; |
| 1472 | OPENSSL_free(encodedPoint); |
Matt Caswell | d2c27a2 | 2016-10-31 14:49:52 +0000 | [diff] [blame] | 1473 | } |
| 1474 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
| 1475 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1476 | return 0; |
| 1477 | } |
Matt Caswell | b5b253b | 2016-10-21 23:40:52 +0100 | [diff] [blame] | 1478 | } |
| 1479 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1480 | /* |
| 1481 | * Add padding to workaround bugs in F5 terminators. See |
| 1482 | * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this |
| 1483 | * code works out the length of all existing extensions it MUST always |
| 1484 | * appear last. |
| 1485 | */ |
| 1486 | if (s->options & SSL_OP_TLSEXT_PADDING) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1487 | unsigned char *padbytes; |
| 1488 | size_t hlen; |
| 1489 | |
Matt Caswell | ae2f7b3 | 2016-09-05 17:34:04 +0100 | [diff] [blame] | 1490 | if (!WPACKET_get_total_written(pkt, &hlen)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1491 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1492 | return 0; |
| 1493 | } |
Matt Caswell | a3680c8 | 2015-03-31 13:57:46 +0100 | [diff] [blame] | 1494 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1495 | if (hlen > 0xff && hlen < 0x200) { |
| 1496 | hlen = 0x200 - hlen; |
| 1497 | if (hlen >= 4) |
| 1498 | hlen -= 4; |
| 1499 | else |
| 1500 | hlen = 0; |
| 1501 | |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 1502 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding) |
Matt Caswell | b2b3024 | 2016-09-13 11:32:52 +0100 | [diff] [blame] | 1503 | || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) { |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1504 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1505 | return 0; |
| 1506 | } |
| 1507 | memset(padbytes, 0, hlen); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | done: |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 1512 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 1515 | /* |
| 1516 | * Add the key_share extension. |
| 1517 | * |
| 1518 | * Returns 1 on success or 0 on failure. |
| 1519 | */ |
| 1520 | static int add_client_key_share_ext(SSL *s, WPACKET *pkt, int *al) |
| 1521 | { |
| 1522 | unsigned char *encodedPoint; |
| 1523 | size_t encoded_pt_len = 0; |
| 1524 | EVP_PKEY *ckey = s->s3->peer_tmp, *skey = NULL; |
| 1525 | |
| 1526 | if (ckey == NULL) { |
| 1527 | SSLerr(SSL_F_ADD_CLIENT_KEY_SHARE_EXT, ERR_R_INTERNAL_ERROR); |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
| 1531 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share) |
| 1532 | || !WPACKET_start_sub_packet_u16(pkt) |
| 1533 | || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)) { |
| 1534 | SSLerr(SSL_F_ADD_CLIENT_KEY_SHARE_EXT, ERR_R_INTERNAL_ERROR); |
| 1535 | return 0; |
| 1536 | } |
| 1537 | |
| 1538 | skey = ssl_generate_pkey(ckey); |
Matt Caswell | 884a790 | 2016-11-23 22:12:56 +0000 | [diff] [blame] | 1539 | if (skey == NULL) { |
| 1540 | SSLerr(SSL_F_ADD_CLIENT_KEY_SHARE_EXT, ERR_R_MALLOC_FAILURE); |
| 1541 | return 0; |
| 1542 | } |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 1543 | |
| 1544 | /* Generate encoding of server key */ |
| 1545 | encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(skey, &encodedPoint); |
| 1546 | if (encoded_pt_len == 0) { |
| 1547 | SSLerr(SSL_F_ADD_CLIENT_KEY_SHARE_EXT, ERR_R_EC_LIB); |
| 1548 | EVP_PKEY_free(skey); |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
| 1552 | if (!WPACKET_sub_memcpy_u16(pkt, encodedPoint, encoded_pt_len) |
| 1553 | || !WPACKET_close(pkt)) { |
| 1554 | SSLerr(SSL_F_ADD_CLIENT_KEY_SHARE_EXT, ERR_R_INTERNAL_ERROR); |
| 1555 | EVP_PKEY_free(skey); |
| 1556 | OPENSSL_free(encodedPoint); |
| 1557 | return 0; |
| 1558 | } |
| 1559 | OPENSSL_free(encodedPoint); |
| 1560 | |
| 1561 | /* This causes the crypto state to be updated based on the derived keys */ |
| 1562 | s->s3->tmp.pkey = skey; |
| 1563 | if (ssl_derive(s, skey, ckey, 1) == 0) { |
| 1564 | *al = SSL_AD_INTERNAL_ERROR; |
| 1565 | SSLerr(SSL_F_ADD_CLIENT_KEY_SHARE_EXT, ERR_R_INTERNAL_ERROR); |
| 1566 | return 0; |
| 1567 | } |
| 1568 | |
| 1569 | return 1; |
| 1570 | } |
| 1571 | |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1572 | int ssl_add_serverhello_tlsext(SSL *s, WPACKET *pkt, int *al) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1573 | { |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1574 | #ifndef OPENSSL_NO_NEXTPROTONEG |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1575 | int next_proto_neg_seen; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1576 | #endif |
| 1577 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1578 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 1579 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
Dr. Stephen Henson | ce0c1f2 | 2016-02-11 18:19:27 +0000 | [diff] [blame] | 1580 | int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1581 | using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL); |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1582 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1583 | |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1584 | if (!WPACKET_start_sub_packet_u16(pkt) |
| 1585 | || !WPACKET_set_flags(pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)) { |
| 1586 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1587 | return 0; |
| 1588 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1589 | |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1590 | if (s->s3->send_connection_binding && |
| 1591 | !ssl_add_serverhello_renegotiate_ext(s, pkt)) { |
| 1592 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1593 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | /* Only add RI for SSLv3 */ |
| 1597 | if (s->version == SSL3_VERSION) |
| 1598 | goto done; |
| 1599 | |
| 1600 | if (!s->hit && s->servername_done == 1 |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1601 | && s->session->tlsext_hostname != NULL) { |
| 1602 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name) |
| 1603 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
| 1604 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1605 | return 0; |
| 1606 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1607 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1608 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1609 | if (using_ecc) { |
| 1610 | const unsigned char *plist; |
| 1611 | size_t plistlen; |
| 1612 | /* |
| 1613 | * Add TLS extension ECPointFormats to the ServerHello message |
| 1614 | */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1615 | tls1_get_formatlist(s, &plist, &plistlen); |
| 1616 | |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1617 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats) |
| 1618 | || !WPACKET_start_sub_packet_u16(pkt) |
| 1619 | || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen) |
| 1620 | || !WPACKET_close(pkt)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1621 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1622 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1623 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1624 | } |
| 1625 | /* |
| 1626 | * Currently the server should not respond with a SupportedCurves |
| 1627 | * extension |
| 1628 | */ |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1629 | #endif /* OPENSSL_NO_EC */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1630 | |
| 1631 | if (s->tlsext_ticket_expected && tls_use_ticket(s)) { |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1632 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket) |
| 1633 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
| 1634 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1635 | return 0; |
| 1636 | } |
Todd Short | 5c753de | 2016-05-12 18:16:52 -0400 | [diff] [blame] | 1637 | } else { |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1638 | /* |
| 1639 | * if we don't add the above TLSEXT, we can't add a session ticket |
| 1640 | * later |
| 1641 | */ |
Todd Short | 5c753de | 2016-05-12 18:16:52 -0400 | [diff] [blame] | 1642 | s->tlsext_ticket_expected = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | if (s->tlsext_status_expected) { |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1646 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request) |
| 1647 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
| 1648 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1649 | return 0; |
| 1650 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1651 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1652 | #ifndef OPENSSL_NO_SRTP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1653 | if (SSL_IS_DTLS(s) && s->srtp_profile) { |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1654 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp) |
| 1655 | || !WPACKET_start_sub_packet_u16(pkt) |
| 1656 | || !WPACKET_put_bytes_u16(pkt, 2) |
| 1657 | || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id) |
| 1658 | || !WPACKET_put_bytes_u8(pkt, 0) |
| 1659 | || !WPACKET_close(pkt)) { |
Matt Caswell | 69f6823 | 2015-03-06 14:37:17 +0000 | [diff] [blame] | 1660 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1661 | return 0; |
Matt Caswell | 69f6823 | 2015-03-06 14:37:17 +0000 | [diff] [blame] | 1662 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1663 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1664 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1665 | |
| 1666 | if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80 |
| 1667 | || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81) |
| 1668 | && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) { |
| 1669 | const unsigned char cryptopro_ext[36] = { |
| 1670 | 0xfd, 0xe8, /* 65000 */ |
| 1671 | 0x00, 0x20, /* 32 bytes length */ |
| 1672 | 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85, |
| 1673 | 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06, |
| 1674 | 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08, |
| 1675 | 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17 |
| 1676 | }; |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1677 | if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) { |
| 1678 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1679 | return 0; |
| 1680 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1681 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1682 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1683 | #ifndef OPENSSL_NO_NEXTPROTONEG |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1684 | next_proto_neg_seen = s->s3->next_proto_neg_seen; |
| 1685 | s->s3->next_proto_neg_seen = 0; |
| 1686 | if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) { |
| 1687 | const unsigned char *npa; |
| 1688 | unsigned int npalen; |
| 1689 | int r; |
| 1690 | |
| 1691 | r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, |
| 1692 | s-> |
| 1693 | ctx->next_protos_advertised_cb_arg); |
| 1694 | if (r == SSL_TLSEXT_ERR_OK) { |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1695 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg) |
| 1696 | || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) { |
| 1697 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1698 | return 0; |
| 1699 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1700 | s->s3->next_proto_neg_seen = 1; |
| 1701 | } |
| 1702 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1703 | #endif |
Matt Caswell | bcec335 | 2016-11-01 13:24:02 +0000 | [diff] [blame] | 1704 | |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 1705 | if (SSL_IS_TLS13(s) && !s->hit && !add_client_key_share_ext(s, pkt, al)) |
| 1706 | return 0; |
Matt Caswell | bcec335 | 2016-11-01 13:24:02 +0000 | [diff] [blame] | 1707 | |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1708 | if (!custom_ext_add(s, 1, pkt, al)) { |
| 1709 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1710 | return 0; |
| 1711 | } |
| 1712 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1713 | if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) { |
| 1714 | /* |
| 1715 | * Don't use encrypt_then_mac if AEAD or RC4 might want to disable |
| 1716 | * for other cases too. |
| 1717 | */ |
| 1718 | if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 1719 | || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4 |
| 1720 | || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT |
| 1721 | || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1722 | s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC; |
| 1723 | else { |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1724 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac) |
| 1725 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
| 1726 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1727 | return 0; |
| 1728 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1729 | } |
| 1730 | } |
Dr. Stephen Henson | e7f0d92 | 2015-12-04 19:48:15 +0000 | [diff] [blame] | 1731 | if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) { |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1732 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret) |
| 1733 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
| 1734 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1735 | return 0; |
| 1736 | } |
Dr. Stephen Henson | ddc06b3 | 2015-01-23 02:45:13 +0000 | [diff] [blame] | 1737 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1738 | |
Todd Short | 817cd0d | 2016-03-05 08:47:55 -0500 | [diff] [blame] | 1739 | if (s->s3->alpn_selected != NULL) { |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1740 | if (!WPACKET_put_bytes_u16(pkt, |
| 1741 | TLSEXT_TYPE_application_layer_protocol_negotiation) |
| 1742 | || !WPACKET_start_sub_packet_u16(pkt) |
| 1743 | || !WPACKET_start_sub_packet_u16(pkt) |
| 1744 | || !WPACKET_sub_memcpy_u8(pkt, s->s3->alpn_selected, |
| 1745 | s->s3->alpn_selected_len) |
| 1746 | || !WPACKET_close(pkt) |
| 1747 | || !WPACKET_close(pkt)) { |
| 1748 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1749 | return 0; |
| 1750 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | done: |
Matt Caswell | 8157d44 | 2016-09-28 11:13:48 +0100 | [diff] [blame] | 1754 | if (!WPACKET_close(pkt)) { |
| 1755 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 1756 | return 0; |
| 1757 | } |
| 1758 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | /* |
Todd Short | 817cd0d | 2016-03-05 08:47:55 -0500 | [diff] [blame] | 1762 | * Process the ALPN extension in a ClientHello. |
Todd Short | 817cd0d | 2016-03-05 08:47:55 -0500 | [diff] [blame] | 1763 | * al: a pointer to the alert value to send in the event of a failure. |
Emilia Kasper | 70c2288 | 2016-07-04 20:32:28 +0200 | [diff] [blame] | 1764 | * returns 1 on success, 0 on error. |
Todd Short | 817cd0d | 2016-03-05 08:47:55 -0500 | [diff] [blame] | 1765 | */ |
Emilia Kasper | 70c2288 | 2016-07-04 20:32:28 +0200 | [diff] [blame] | 1766 | static int tls1_alpn_handle_client_hello_late(SSL *s, int *al) |
Todd Short | 817cd0d | 2016-03-05 08:47:55 -0500 | [diff] [blame] | 1767 | { |
| 1768 | const unsigned char *selected = NULL; |
| 1769 | unsigned char selected_len = 0; |
| 1770 | |
| 1771 | if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) { |
| 1772 | int r = s->ctx->alpn_select_cb(s, &selected, &selected_len, |
| 1773 | s->s3->alpn_proposed, |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 1774 | (unsigned int)s->s3->alpn_proposed_len, |
Todd Short | 817cd0d | 2016-03-05 08:47:55 -0500 | [diff] [blame] | 1775 | s->ctx->alpn_select_cb_arg); |
| 1776 | |
| 1777 | if (r == SSL_TLSEXT_ERR_OK) { |
| 1778 | OPENSSL_free(s->s3->alpn_selected); |
| 1779 | s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len); |
| 1780 | if (s->s3->alpn_selected == NULL) { |
| 1781 | *al = SSL_AD_INTERNAL_ERROR; |
Todd Short | 817cd0d | 2016-03-05 08:47:55 -0500 | [diff] [blame] | 1782 | return 0; |
| 1783 | } |
| 1784 | s->s3->alpn_selected_len = selected_len; |
Todd Short | 0351baa | 2016-03-12 09:14:05 -0500 | [diff] [blame] | 1785 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1786 | /* ALPN takes precedence over NPN. */ |
| 1787 | s->s3->next_proto_neg_seen = 0; |
| 1788 | #endif |
Todd Short | 817cd0d | 2016-03-05 08:47:55 -0500 | [diff] [blame] | 1789 | } else { |
| 1790 | *al = SSL_AD_NO_APPLICATION_PROTOCOL; |
Emilia Kasper | 0621786 | 2015-09-22 15:20:26 +0200 | [diff] [blame] | 1791 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1792 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1793 | } |
Adam Langley | 6f017a8 | 2013-04-15 18:07:47 -0400 | [diff] [blame] | 1794 | |
Emilia Kasper | 0621786 | 2015-09-22 15:20:26 +0200 | [diff] [blame] | 1795 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1796 | } |
Adam Langley | 6f017a8 | 2013-04-15 18:07:47 -0400 | [diff] [blame] | 1797 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1798 | #ifndef OPENSSL_NO_NEXTPROTONEG |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1799 | /* |
| 1800 | * ssl_next_proto_validate validates a Next Protocol Negotiation block. No |
| 1801 | * elements of zero length are allowed and the set of elements must exactly |
| 1802 | * fill the length of the block. |
| 1803 | */ |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1804 | static char ssl_next_proto_validate(PACKET *pkt) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1805 | { |
Kazuki Yamaguchi | b04f947 | 2015-12-13 00:51:06 +0900 | [diff] [blame] | 1806 | PACKET tmp_protocol; |
Ben Laurie | ee2ffc2 | 2010-07-28 10:06:55 +0000 | [diff] [blame] | 1807 | |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1808 | while (PACKET_remaining(pkt)) { |
Kazuki Yamaguchi | b04f947 | 2015-12-13 00:51:06 +0900 | [diff] [blame] | 1809 | if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1810 | || PACKET_remaining(&tmp_protocol) == 0) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1811 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1812 | } |
Ben Laurie | ee2ffc2 | 2010-07-28 10:06:55 +0000 | [diff] [blame] | 1813 | |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1814 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1815 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1816 | #endif |
Ben Laurie | ee2ffc2 | 2010-07-28 10:06:55 +0000 | [diff] [blame] | 1817 | |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1818 | static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1819 | { |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1820 | unsigned int length, type, size; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1821 | int tlsext_servername = 0; |
| 1822 | int renegotiate_seen = 0; |
Bodo Möller | ed3883d | 2006-01-02 23:14:37 +0000 | [diff] [blame] | 1823 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1824 | #ifndef OPENSSL_NO_NEXTPROTONEG |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1825 | s->s3->next_proto_neg_seen = 0; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1826 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1827 | s->tlsext_ticket_expected = 0; |
Bodo Möller | 6f31dd7 | 2011-11-24 21:07:01 +0000 | [diff] [blame] | 1828 | |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 1829 | OPENSSL_free(s->s3->alpn_selected); |
| 1830 | s->s3->alpn_selected = NULL; |
Adam Langley | 6f017a8 | 2013-04-15 18:07:47 -0400 | [diff] [blame] | 1831 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1832 | s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC; |
Dr. Stephen Henson | 4817504 | 2011-12-31 22:59:57 +0000 | [diff] [blame] | 1833 | |
Dr. Stephen Henson | e7f0d92 | 2015-12-04 19:48:15 +0000 | [diff] [blame] | 1834 | s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS; |
| 1835 | |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1836 | if (!PACKET_get_net_2(pkt, &length)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1837 | goto ri_check; |
Dr. Stephen Henson | 5e3ff62 | 2013-03-22 17:12:33 +0000 | [diff] [blame] | 1838 | |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1839 | if (PACKET_remaining(pkt) != length) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1840 | *al = SSL_AD_DECODE_ERROR; |
| 1841 | return 0; |
| 1842 | } |
Bodo Möller | ed3883d | 2006-01-02 23:14:37 +0000 | [diff] [blame] | 1843 | |
Emilia Kasper | aa474d1 | 2016-02-19 17:24:44 +0100 | [diff] [blame] | 1844 | if (!tls1_check_duplicate_extensions(pkt)) { |
| 1845 | *al = SSL_AD_DECODE_ERROR; |
| 1846 | return 0; |
| 1847 | } |
| 1848 | |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1849 | while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) { |
Emilia Kasper | b698174 | 2016-02-01 15:26:18 +0100 | [diff] [blame] | 1850 | const unsigned char *data; |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1851 | PACKET spkt; |
Bodo Möller | ed3883d | 2006-01-02 23:14:37 +0000 | [diff] [blame] | 1852 | |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1853 | if (!PACKET_get_sub_packet(pkt, &spkt, size) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1854 | || !PACKET_peek_bytes(&spkt, &data, size)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1855 | goto ri_check; |
Bodo Möller | ed3883d | 2006-01-02 23:14:37 +0000 | [diff] [blame] | 1856 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1857 | if (s->tlsext_debug_cb) |
| 1858 | s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg); |
Bodo Möller | ed3883d | 2006-01-02 23:14:37 +0000 | [diff] [blame] | 1859 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1860 | if (type == TLSEXT_TYPE_renegotiate) { |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1861 | if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1862 | return 0; |
| 1863 | renegotiate_seen = 1; |
| 1864 | } else if (s->version == SSL3_VERSION) { |
| 1865 | } else if (type == TLSEXT_TYPE_server_name) { |
| 1866 | if (s->tlsext_hostname == NULL || size > 0) { |
| 1867 | *al = TLS1_AD_UNRECOGNIZED_NAME; |
| 1868 | return 0; |
| 1869 | } |
| 1870 | tlsext_servername = 1; |
| 1871 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1872 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1873 | else if (type == TLSEXT_TYPE_ec_point_formats) { |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1874 | unsigned int ecpointformatlist_length; |
| 1875 | if (!PACKET_get_1(&spkt, &ecpointformatlist_length) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1876 | || ecpointformatlist_length != size - 1) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1877 | *al = TLS1_AD_DECODE_ERROR; |
| 1878 | return 0; |
| 1879 | } |
| 1880 | if (!s->hit) { |
| 1881 | s->session->tlsext_ecpointformatlist_length = 0; |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 1882 | OPENSSL_free(s->session->tlsext_ecpointformatlist); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1883 | if ((s->session->tlsext_ecpointformatlist = |
| 1884 | OPENSSL_malloc(ecpointformatlist_length)) == NULL) { |
| 1885 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1886 | return 0; |
| 1887 | } |
| 1888 | s->session->tlsext_ecpointformatlist_length = |
| 1889 | ecpointformatlist_length; |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1890 | if (!PACKET_copy_bytes(&spkt, |
| 1891 | s->session->tlsext_ecpointformatlist, |
| 1892 | ecpointformatlist_length)) { |
| 1893 | *al = TLS1_AD_DECODE_ERROR; |
| 1894 | return 0; |
| 1895 | } |
| 1896 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1897 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1898 | } |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1899 | #endif /* OPENSSL_NO_EC */ |
Dr. Stephen Henson | 5a3d8ee | 2014-11-03 17:47:11 +0000 | [diff] [blame] | 1900 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1901 | else if (type == TLSEXT_TYPE_session_ticket) { |
| 1902 | if (s->tls_session_ticket_ext_cb && |
| 1903 | !s->tls_session_ticket_ext_cb(s, data, size, |
| 1904 | s->tls_session_ticket_ext_cb_arg)) |
| 1905 | { |
| 1906 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1907 | return 0; |
| 1908 | } |
| 1909 | if (!tls_use_ticket(s) || (size > 0)) { |
| 1910 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
| 1911 | return 0; |
| 1912 | } |
| 1913 | s->tlsext_ticket_expected = 1; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1914 | } else if (type == TLSEXT_TYPE_status_request) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1915 | /* |
| 1916 | * MUST be empty and only sent if we've requested a status |
| 1917 | * request message. |
| 1918 | */ |
| 1919 | if ((s->tlsext_status_type == -1) || (size > 0)) { |
| 1920 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
| 1921 | return 0; |
| 1922 | } |
| 1923 | /* Set flag to expect CertificateStatus message */ |
| 1924 | s->tlsext_status_expected = 1; |
| 1925 | } |
Rob Percival | ed29e82 | 2016-03-03 16:19:23 +0000 | [diff] [blame] | 1926 | #ifndef OPENSSL_NO_CT |
| 1927 | /* |
| 1928 | * Only take it if we asked for it - i.e if there is no CT validation |
| 1929 | * callback set, then a custom extension MAY be processing it, so we |
| 1930 | * need to let control continue to flow to that. |
| 1931 | */ |
| 1932 | else if (type == TLSEXT_TYPE_signed_certificate_timestamp && |
| 1933 | s->ct_validation_callback != NULL) { |
| 1934 | /* Simply copy it off for later processing */ |
| 1935 | if (s->tlsext_scts != NULL) { |
| 1936 | OPENSSL_free(s->tlsext_scts); |
| 1937 | s->tlsext_scts = NULL; |
| 1938 | } |
| 1939 | s->tlsext_scts_len = size; |
| 1940 | if (size > 0) { |
| 1941 | s->tlsext_scts = OPENSSL_malloc(size); |
| 1942 | if (s->tlsext_scts == NULL) { |
| 1943 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1944 | return 0; |
| 1945 | } |
| 1946 | memcpy(s->tlsext_scts, data, size); |
| 1947 | } |
| 1948 | } |
| 1949 | #endif |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1950 | #ifndef OPENSSL_NO_NEXTPROTONEG |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1951 | else if (type == TLSEXT_TYPE_next_proto_neg && |
| 1952 | s->s3->tmp.finish_md_len == 0) { |
| 1953 | unsigned char *selected; |
| 1954 | unsigned char selected_len; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1955 | /* We must have requested it. */ |
| 1956 | if (s->ctx->next_proto_select_cb == NULL) { |
| 1957 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
| 1958 | return 0; |
| 1959 | } |
| 1960 | /* The data must be valid */ |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 1961 | if (!ssl_next_proto_validate(&spkt)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1962 | *al = TLS1_AD_DECODE_ERROR; |
| 1963 | return 0; |
| 1964 | } |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 1965 | if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, |
| 1966 | size, |
| 1967 | s-> |
| 1968 | ctx->next_proto_select_cb_arg) != |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1969 | SSL_TLSEXT_ERR_OK) { |
| 1970 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1971 | return 0; |
| 1972 | } |
Matt Caswell | c31dbed | 2016-09-09 10:53:39 +0100 | [diff] [blame] | 1973 | /* |
| 1974 | * Could be non-NULL if server has sent multiple NPN extensions in |
| 1975 | * a single Serverhello |
| 1976 | */ |
| 1977 | OPENSSL_free(s->next_proto_negotiated); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1978 | s->next_proto_negotiated = OPENSSL_malloc(selected_len); |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 1979 | if (s->next_proto_negotiated == NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1980 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1981 | return 0; |
| 1982 | } |
| 1983 | memcpy(s->next_proto_negotiated, selected, selected_len); |
| 1984 | s->next_proto_negotiated_len = selected_len; |
| 1985 | s->s3->next_proto_neg_seen = 1; |
| 1986 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 1987 | #endif |
Bodo Möller | 761772d | 2007-09-21 06:54:24 +0000 | [diff] [blame] | 1988 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1989 | else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) { |
| 1990 | unsigned len; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1991 | /* We must have requested it. */ |
Todd Short | 817cd0d | 2016-03-05 08:47:55 -0500 | [diff] [blame] | 1992 | if (!s->s3->alpn_sent) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1993 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
| 1994 | return 0; |
| 1995 | } |
Matt Caswell | 50e735f | 2015-01-05 11:30:03 +0000 | [diff] [blame] | 1996 | /*- |
| 1997 | * The extension data consists of: |
| 1998 | * uint16 list_length |
| 1999 | * uint8 proto_length; |
| 2000 | * uint8 proto[proto_length]; |
| 2001 | */ |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 2002 | if (!PACKET_get_net_2(&spkt, &len) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2003 | || PACKET_remaining(&spkt) != len || !PACKET_get_1(&spkt, &len) |
| 2004 | || PACKET_remaining(&spkt) != len) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2005 | *al = TLS1_AD_DECODE_ERROR; |
| 2006 | return 0; |
| 2007 | } |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 2008 | OPENSSL_free(s->s3->alpn_selected); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2009 | s->s3->alpn_selected = OPENSSL_malloc(len); |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 2010 | if (s->s3->alpn_selected == NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2011 | *al = TLS1_AD_INTERNAL_ERROR; |
| 2012 | return 0; |
| 2013 | } |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 2014 | if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) { |
| 2015 | *al = TLS1_AD_DECODE_ERROR; |
| 2016 | return 0; |
| 2017 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2018 | s->s3->alpn_selected_len = len; |
| 2019 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 2020 | #ifndef OPENSSL_NO_SRTP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2021 | else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) { |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 2022 | if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2023 | return 0; |
| 2024 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 2025 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2026 | else if (type == TLSEXT_TYPE_encrypt_then_mac) { |
| 2027 | /* Ignore if inappropriate ciphersuite */ |
David Woodhouse | cde6145 | 2016-10-14 00:26:38 +0100 | [diff] [blame] | 2028 | if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) && |
| 2029 | s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2030 | && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4) |
| 2031 | s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC; |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 2032 | } else if (type == TLSEXT_TYPE_extended_master_secret && |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 2033 | (SSL_IS_DTLS(s) || !SSL_IS_TLS13(s))) { |
Dr. Stephen Henson | e7f0d92 | 2015-12-04 19:48:15 +0000 | [diff] [blame] | 2034 | s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS; |
Dr. Stephen Henson | ddc06b3 | 2015-01-23 02:45:13 +0000 | [diff] [blame] | 2035 | if (!s->hit) |
| 2036 | s->session->flags |= SSL_SESS_FLAG_EXTMS; |
Matt Caswell | d7c42d7 | 2016-11-01 14:09:19 +0000 | [diff] [blame] | 2037 | } else if (type == TLSEXT_TYPE_key_share |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 2038 | && SSL_IS_TLS13(s)) { |
Matt Caswell | d7c42d7 | 2016-11-01 14:09:19 +0000 | [diff] [blame] | 2039 | unsigned int group_id; |
| 2040 | PACKET encoded_pt; |
| 2041 | EVP_PKEY *ckey = s->s3->tmp.pkey, *skey = NULL; |
| 2042 | |
| 2043 | /* Sanity check */ |
| 2044 | if (ckey == NULL) { |
| 2045 | *al = SSL_AD_INTERNAL_ERROR; |
| 2046 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 2047 | return 0; |
| 2048 | } |
| 2049 | |
| 2050 | if (!PACKET_get_net_2(&spkt, &group_id)) { |
| 2051 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 2052 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, |
| 2053 | SSL_R_LENGTH_MISMATCH); |
| 2054 | return 0; |
| 2055 | } |
| 2056 | |
| 2057 | if (group_id != s->s3->group_id) { |
| 2058 | /* |
| 2059 | * This isn't for the group that we sent in the original |
| 2060 | * key_share! |
| 2061 | */ |
| 2062 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 2063 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, |
| 2064 | SSL_R_BAD_KEY_SHARE); |
| 2065 | return 0; |
| 2066 | } |
| 2067 | |
Matt Caswell | 323f212 | 2016-11-04 09:49:16 +0000 | [diff] [blame] | 2068 | if (!PACKET_as_length_prefixed_2(&spkt, &encoded_pt) |
| 2069 | || PACKET_remaining(&encoded_pt) == 0) { |
Matt Caswell | d7c42d7 | 2016-11-01 14:09:19 +0000 | [diff] [blame] | 2070 | *al = SSL_AD_DECODE_ERROR; |
| 2071 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, |
| 2072 | SSL_R_LENGTH_MISMATCH); |
| 2073 | return 0; |
| 2074 | } |
| 2075 | |
Matt Caswell | 94ed2c6 | 2016-11-14 14:53:31 +0000 | [diff] [blame] | 2076 | skey = ssl_generate_pkey(ckey); |
Matt Caswell | 884a790 | 2016-11-23 22:12:56 +0000 | [diff] [blame] | 2077 | if (skey == NULL) { |
| 2078 | *al = SSL_AD_INTERNAL_ERROR; |
| 2079 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, ERR_R_MALLOC_FAILURE); |
| 2080 | return 0; |
| 2081 | } |
Matt Caswell | d7c42d7 | 2016-11-01 14:09:19 +0000 | [diff] [blame] | 2082 | if (!EVP_PKEY_set1_tls_encodedpoint(skey, PACKET_data(&encoded_pt), |
| 2083 | PACKET_remaining(&encoded_pt))) { |
| 2084 | *al = SSL_AD_DECODE_ERROR; |
| 2085 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_BAD_ECPOINT); |
| 2086 | return 0; |
| 2087 | } |
| 2088 | |
Matt Caswell | 0f1e51e | 2016-11-02 15:03:56 +0000 | [diff] [blame] | 2089 | if (ssl_derive(s, ckey, skey, 1) == 0) { |
| 2090 | *al = SSL_AD_INTERNAL_ERROR; |
| 2091 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 2092 | EVP_PKEY_free(skey); |
| 2093 | return 0; |
| 2094 | } |
Matt Caswell | d7c42d7 | 2016-11-01 14:09:19 +0000 | [diff] [blame] | 2095 | EVP_PKEY_free(skey); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2096 | /* |
| 2097 | * If this extension type was not otherwise handled, but matches a |
| 2098 | * custom_cli_ext_record, then send it to the c callback |
| 2099 | */ |
Matt Caswell | d7c42d7 | 2016-11-01 14:09:19 +0000 | [diff] [blame] | 2100 | } else if (custom_ext_parse(s, 0, type, data, size, al) <= 0) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2101 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2102 | } |
Adam Langley | 6f017a8 | 2013-04-15 18:07:47 -0400 | [diff] [blame] | 2103 | |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 2104 | if (PACKET_remaining(pkt) != 0) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2105 | *al = SSL_AD_DECODE_ERROR; |
| 2106 | return 0; |
| 2107 | } |
Bodo Möller | ed3883d | 2006-01-02 23:14:37 +0000 | [diff] [blame] | 2108 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2109 | if (!s->hit && tlsext_servername == 1) { |
| 2110 | if (s->tlsext_hostname) { |
| 2111 | if (s->session->tlsext_hostname == NULL) { |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2112 | s->session->tlsext_hostname = |
| 2113 | OPENSSL_strdup(s->tlsext_hostname); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2114 | if (!s->session->tlsext_hostname) { |
| 2115 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2116 | return 0; |
| 2117 | } |
| 2118 | } else { |
| 2119 | *al = SSL_AD_DECODE_ERROR; |
| 2120 | return 0; |
| 2121 | } |
| 2122 | } |
| 2123 | } |
Bodo Möller | ed3883d | 2006-01-02 23:14:37 +0000 | [diff] [blame] | 2124 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2125 | ri_check: |
Dr. Stephen Henson | c27c9cb | 2009-12-14 13:56:04 +0000 | [diff] [blame] | 2126 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2127 | /* |
| 2128 | * Determine if we need to see RI. Strictly speaking if we want to avoid |
| 2129 | * an attack we should *always* see RI even on initial server hello |
| 2130 | * because the client doesn't see any renegotiation during an attack. |
| 2131 | * However this would mean we could not connect to any server which |
Matt Caswell | e7653f3 | 2016-06-14 10:41:00 +0100 | [diff] [blame] | 2132 | * doesn't support RI so for the immediate future tolerate RI absence |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2133 | */ |
| 2134 | if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) |
| 2135 | && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { |
| 2136 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 2137 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, |
| 2138 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
| 2139 | return 0; |
| 2140 | } |
Dr. Stephen Henson | c27c9cb | 2009-12-14 13:56:04 +0000 | [diff] [blame] | 2141 | |
Dr. Stephen Henson | e7f0d92 | 2015-12-04 19:48:15 +0000 | [diff] [blame] | 2142 | if (s->hit) { |
| 2143 | /* |
| 2144 | * Check extended master secret extension is consistent with |
| 2145 | * original session. |
| 2146 | */ |
| 2147 | if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) != |
| 2148 | !(s->session->flags & SSL_SESS_FLAG_EXTMS)) { |
| 2149 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 2150 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS); |
| 2151 | return 0; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2152 | } |
Dr. Stephen Henson | e7f0d92 | 2015-12-04 19:48:15 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2155 | return 1; |
| 2156 | } |
Bodo Möller | b2172f4 | 2006-04-03 11:56:30 +0000 | [diff] [blame] | 2157 | |
Bodo Möller | 36ca4ba | 2006-03-11 23:46:37 +0000 | [diff] [blame] | 2158 | int ssl_prepare_clienthello_tlsext(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2159 | { |
Todd Short | 817cd0d | 2016-03-05 08:47:55 -0500 | [diff] [blame] | 2160 | s->s3->alpn_sent = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2161 | return 1; |
| 2162 | } |
Bodo Möller | 36ca4ba | 2006-03-11 23:46:37 +0000 | [diff] [blame] | 2163 | |
| 2164 | int ssl_prepare_serverhello_tlsext(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2165 | { |
| 2166 | return 1; |
| 2167 | } |
Bodo Möller | 36ca4ba | 2006-03-11 23:46:37 +0000 | [diff] [blame] | 2168 | |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 2169 | /* Initialise digests to default values */ |
Dr. Stephen Henson | a0f6382 | 2015-11-24 00:08:35 +0000 | [diff] [blame] | 2170 | void ssl_set_default_md(SSL *s) |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 2171 | { |
| 2172 | const EVP_MD **pmd = s->s3->tmp.md; |
| 2173 | #ifndef OPENSSL_NO_DSA |
Dr. Stephen Henson | 152fbc2 | 2015-11-29 16:27:08 +0000 | [diff] [blame] | 2174 | pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX); |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 2175 | #endif |
| 2176 | #ifndef OPENSSL_NO_RSA |
Dr. Stephen Henson | d18d31a | 2015-08-29 22:11:05 +0100 | [diff] [blame] | 2177 | if (SSL_USE_SIGALGS(s)) |
Dr. Stephen Henson | 152fbc2 | 2015-11-29 16:27:08 +0000 | [diff] [blame] | 2178 | pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX); |
Dr. Stephen Henson | d18d31a | 2015-08-29 22:11:05 +0100 | [diff] [blame] | 2179 | else |
Dr. Stephen Henson | 152fbc2 | 2015-11-29 16:27:08 +0000 | [diff] [blame] | 2180 | pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX); |
Dr. Stephen Henson | d18d31a | 2015-08-29 22:11:05 +0100 | [diff] [blame] | 2181 | pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN]; |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 2182 | #endif |
| 2183 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | 152fbc2 | 2015-11-29 16:27:08 +0000 | [diff] [blame] | 2184 | pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX); |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 2185 | #endif |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 2186 | #ifndef OPENSSL_NO_GOST |
Dr. Stephen Henson | 152fbc2 | 2015-11-29 16:27:08 +0000 | [diff] [blame] | 2187 | pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX); |
| 2188 | pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX); |
| 2189 | pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX); |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 2190 | #endif |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 2191 | } |
Bodo Möller | 36ca4ba | 2006-03-11 23:46:37 +0000 | [diff] [blame] | 2192 | |
Dr. Stephen Henson | e469af8 | 2014-11-17 16:52:59 +0000 | [diff] [blame] | 2193 | int tls1_set_server_sigalgs(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2194 | { |
| 2195 | int al; |
| 2196 | size_t i; |
FdaSilvaYY | 8483a00 | 2016-03-10 21:34:48 +0100 | [diff] [blame] | 2197 | |
| 2198 | /* Clear any shared signature algorithms */ |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 2199 | OPENSSL_free(s->cert->shared_sigalgs); |
| 2200 | s->cert->shared_sigalgs = NULL; |
| 2201 | s->cert->shared_sigalgslen = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2202 | /* Clear certificate digests and validity flags */ |
| 2203 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 2204 | s->s3->tmp.md[i] = NULL; |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 2205 | s->s3->tmp.valid_flags[i] = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2206 | } |
Dr. Stephen Henson | e469af8 | 2014-11-17 16:52:59 +0000 | [diff] [blame] | 2207 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2208 | /* If sigalgs received process it. */ |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 2209 | if (s->s3->tmp.peer_sigalgs) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2210 | if (!tls1_process_sigalgs(s)) { |
| 2211 | SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE); |
| 2212 | al = SSL_AD_INTERNAL_ERROR; |
| 2213 | goto err; |
| 2214 | } |
| 2215 | /* Fatal error is no shared signature algorithms */ |
| 2216 | if (!s->cert->shared_sigalgs) { |
| 2217 | SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, |
FdaSilvaYY | f430ba3 | 2016-06-19 19:39:01 +0200 | [diff] [blame] | 2218 | SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2219 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 2220 | goto err; |
| 2221 | } |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 2222 | } else { |
| 2223 | ssl_set_default_md(s); |
| 2224 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2225 | return 1; |
| 2226 | err: |
| 2227 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2228 | return 0; |
| 2229 | } |
Dr. Stephen Henson | e469af8 | 2014-11-17 16:52:59 +0000 | [diff] [blame] | 2230 | |
Emilia Kasper | 70c2288 | 2016-07-04 20:32:28 +0200 | [diff] [blame] | 2231 | /* |
| 2232 | * Upon success, returns 1. |
| 2233 | * Upon failure, returns 0 and sets |al| to the appropriate fatal alert. |
| 2234 | */ |
| 2235 | int ssl_check_clienthello_tlsext_late(SSL *s, int *al) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2236 | { |
Emilia Kasper | 70c2288 | 2016-07-04 20:32:28 +0200 | [diff] [blame] | 2237 | s->tlsext_status_expected = 0; |
Ben Laurie | 2daceb0 | 2012-09-11 12:57:46 +0000 | [diff] [blame] | 2238 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2239 | /* |
| 2240 | * If status request then ask callback what to do. Note: this must be |
| 2241 | * called after servername callbacks in case the certificate has changed, |
| 2242 | * and must be called after the cipher has been chosen because this may |
| 2243 | * influence which certificate is sent |
| 2244 | */ |
| 2245 | if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) { |
Emilia Kasper | 70c2288 | 2016-07-04 20:32:28 +0200 | [diff] [blame] | 2246 | int ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2247 | CERT_PKEY *certpkey; |
| 2248 | certpkey = ssl_get_server_send_pkey(s); |
| 2249 | /* If no certificate can't return certificate status */ |
Emilia Kasper | 70c2288 | 2016-07-04 20:32:28 +0200 | [diff] [blame] | 2250 | if (certpkey != NULL) { |
| 2251 | /* |
| 2252 | * Set current certificate to one we will use so SSL_get_certificate |
| 2253 | * et al can pick it up. |
| 2254 | */ |
| 2255 | s->cert->key = certpkey; |
| 2256 | ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); |
| 2257 | switch (ret) { |
| 2258 | /* We don't want to send a status request response */ |
| 2259 | case SSL_TLSEXT_ERR_NOACK: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2260 | s->tlsext_status_expected = 0; |
Emilia Kasper | 70c2288 | 2016-07-04 20:32:28 +0200 | [diff] [blame] | 2261 | break; |
| 2262 | /* status request response should be sent */ |
| 2263 | case SSL_TLSEXT_ERR_OK: |
| 2264 | if (s->tlsext_ocsp_resp) |
| 2265 | s->tlsext_status_expected = 1; |
| 2266 | break; |
| 2267 | /* something bad happened */ |
| 2268 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2269 | default: |
| 2270 | *al = SSL_AD_INTERNAL_ERROR; |
| 2271 | return 0; |
| 2272 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2273 | } |
Todd Short | 817cd0d | 2016-03-05 08:47:55 -0500 | [diff] [blame] | 2274 | } |
| 2275 | |
Emilia Kasper | 70c2288 | 2016-07-04 20:32:28 +0200 | [diff] [blame] | 2276 | if (!tls1_alpn_handle_client_hello_late(s, al)) { |
| 2277 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2278 | } |
Emilia Kasper | 70c2288 | 2016-07-04 20:32:28 +0200 | [diff] [blame] | 2279 | |
| 2280 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2281 | } |
Ben Laurie | 2daceb0 | 2012-09-11 12:57:46 +0000 | [diff] [blame] | 2282 | |
Bodo Möller | 36ca4ba | 2006-03-11 23:46:37 +0000 | [diff] [blame] | 2283 | int ssl_check_serverhello_tlsext(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2284 | { |
| 2285 | int ret = SSL_TLSEXT_ERR_NOACK; |
| 2286 | int al = SSL_AD_UNRECOGNIZED_NAME; |
Bodo Möller | 36ca4ba | 2006-03-11 23:46:37 +0000 | [diff] [blame] | 2287 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 2288 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2289 | /* |
| 2290 | * If we are client and using an elliptic curve cryptography cipher |
| 2291 | * suite, then if server returns an EC point formats lists extension it |
| 2292 | * must contain uncompressed. |
| 2293 | */ |
| 2294 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 2295 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 2296 | if ((s->tlsext_ecpointformatlist != NULL) |
| 2297 | && (s->tlsext_ecpointformatlist_length > 0) |
| 2298 | && (s->session->tlsext_ecpointformatlist != NULL) |
| 2299 | && (s->session->tlsext_ecpointformatlist_length > 0) |
Dr. Stephen Henson | ce0c1f2 | 2016-02-11 18:19:27 +0000 | [diff] [blame] | 2300 | && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2301 | /* we are using an ECC cipher */ |
| 2302 | size_t i; |
| 2303 | unsigned char *list; |
| 2304 | int found_uncompressed = 0; |
| 2305 | list = s->session->tlsext_ecpointformatlist; |
| 2306 | for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) { |
| 2307 | if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) { |
| 2308 | found_uncompressed = 1; |
| 2309 | break; |
| 2310 | } |
| 2311 | } |
| 2312 | if (!found_uncompressed) { |
| 2313 | SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT, |
| 2314 | SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST); |
| 2315 | return -1; |
| 2316 | } |
| 2317 | } |
| 2318 | ret = SSL_TLSEXT_ERR_OK; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2319 | #endif /* OPENSSL_NO_EC */ |
Bodo Möller | 36ca4ba | 2006-03-11 23:46:37 +0000 | [diff] [blame] | 2320 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2321 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) |
| 2322 | ret = |
| 2323 | s->ctx->tlsext_servername_callback(s, &al, |
| 2324 | s->ctx->tlsext_servername_arg); |
| 2325 | else if (s->initial_ctx != NULL |
| 2326 | && s->initial_ctx->tlsext_servername_callback != 0) |
| 2327 | ret = |
| 2328 | s->initial_ctx->tlsext_servername_callback(s, &al, |
| 2329 | s-> |
| 2330 | initial_ctx->tlsext_servername_arg); |
Bodo Möller | 241520e | 2006-01-11 06:10:40 +0000 | [diff] [blame] | 2331 | |
Matt Caswell | b1931d4 | 2015-12-10 10:44:30 +0000 | [diff] [blame] | 2332 | /* |
| 2333 | * Ensure we get sensible values passed to tlsext_status_cb in the event |
| 2334 | * that we don't receive a status message |
| 2335 | */ |
Matt Caswell | bb1aaab | 2015-11-05 14:31:11 +0000 | [diff] [blame] | 2336 | OPENSSL_free(s->tlsext_ocsp_resp); |
| 2337 | s->tlsext_ocsp_resp = NULL; |
Matt Caswell | 8b0e934 | 2016-10-06 19:17:54 +0100 | [diff] [blame] | 2338 | s->tlsext_ocsp_resplen = 0; |
Bodo Möller | 58ece83 | 2006-01-13 09:21:10 +0000 | [diff] [blame] | 2339 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2340 | switch (ret) { |
| 2341 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2342 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2343 | return -1; |
Bodo Möller | 3327372 | 2006-03-30 02:44:56 +0000 | [diff] [blame] | 2344 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2345 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2346 | ssl3_send_alert(s, SSL3_AL_WARNING, al); |
| 2347 | return 1; |
Dr. Stephen Henson | 09e4e4b | 2012-04-24 12:22:23 +0000 | [diff] [blame] | 2348 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2349 | case SSL_TLSEXT_ERR_NOACK: |
| 2350 | s->servername_done = 0; |
| 2351 | default: |
| 2352 | return 1; |
| 2353 | } |
| 2354 | } |
| 2355 | |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 2356 | int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2357 | { |
| 2358 | int al = -1; |
| 2359 | if (s->version < SSL3_VERSION) |
| 2360 | return 1; |
Matt Caswell | 50932c4 | 2015-08-04 17:36:02 +0100 | [diff] [blame] | 2361 | if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2362 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2363 | return 0; |
| 2364 | } |
| 2365 | |
| 2366 | if (ssl_check_serverhello_tlsext(s) <= 0) { |
| 2367 | SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT); |
| 2368 | return 0; |
| 2369 | } |
| 2370 | return 1; |
Dr. Stephen Henson | 09e4e4b | 2012-04-24 12:22:23 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Matt Caswell | 6438632 | 2016-10-31 16:36:30 +0000 | [diff] [blame] | 2373 | /* |
| 2374 | * Given a list of extensions that we collected earlier, find one of a given |
| 2375 | * type and return it. |
| 2376 | * |
| 2377 | * |exts| is the set of extensions previously collected. |
| 2378 | * |numexts| is the number of extensions that we have. |
| 2379 | * |type| the type of the extension that we are looking for. |
| 2380 | * |
| 2381 | * Returns a pointer to the found RAW_EXTENSION data, or NULL if not found. |
| 2382 | */ |
Matt Caswell | cd99883 | 2016-10-23 00:41:11 +0100 | [diff] [blame] | 2383 | RAW_EXTENSION *tls_get_extension_by_type(RAW_EXTENSION *exts, size_t numexts, |
| 2384 | unsigned int type) |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 2385 | { |
| 2386 | size_t loop; |
| 2387 | |
| 2388 | for (loop = 0; loop < numexts; loop++) { |
| 2389 | if (exts[loop].type == type) |
| 2390 | return &exts[loop]; |
| 2391 | } |
| 2392 | |
| 2393 | return NULL; |
| 2394 | } |
| 2395 | |
Tim Hudson | 1d97c84 | 2014-12-28 12:48:40 +1000 | [diff] [blame] | 2396 | /*- |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 2397 | * Gets the ticket information supplied by the client if any. |
Dr. Stephen Henson | e7f0d92 | 2015-12-04 19:48:15 +0000 | [diff] [blame] | 2398 | * |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 2399 | * hello: The parsed ClientHello data |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 2400 | * ret: (output) on return, if a ticket was decrypted, then this is set to |
| 2401 | * point to the resulting session. |
| 2402 | * |
| 2403 | * If s->tls_session_secret_cb is set then we are expecting a pre-shared key |
| 2404 | * ciphersuite, in which case we have no use for session tickets and one will |
| 2405 | * never be decrypted, nor will s->tlsext_ticket_expected be set to 1. |
| 2406 | * |
| 2407 | * Returns: |
| 2408 | * -1: fatal error, either from parsing or decrypting the ticket. |
| 2409 | * 0: no ticket was found (or was ignored, based on settings). |
| 2410 | * 1: a zero length extension was found, indicating that the client supports |
| 2411 | * session tickets but doesn't currently have one to offer. |
| 2412 | * 2: either s->tls_session_secret_cb was set, or a ticket was offered but |
| 2413 | * couldn't be decrypted because of a non-fatal error. |
| 2414 | * 3: a ticket was successfully decrypted and *ret was set. |
| 2415 | * |
| 2416 | * Side effects: |
| 2417 | * Sets s->tlsext_ticket_expected to 1 if the server will have to issue |
| 2418 | * a new session ticket to the client because the client indicated support |
| 2419 | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have |
| 2420 | * a session ticket or we couldn't use the one it gave us, or if |
| 2421 | * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket. |
| 2422 | * Otherwise, s->tlsext_ticket_expected is set to 0. |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 2423 | */ |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 2424 | int tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello, |
| 2425 | SSL_SESSION **ret) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2426 | { |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 2427 | int retv; |
| 2428 | const unsigned char *etick; |
| 2429 | size_t size; |
| 2430 | RAW_EXTENSION *ticketext; |
Dr. Stephen Henson | e7f0d92 | 2015-12-04 19:48:15 +0000 | [diff] [blame] | 2431 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2432 | *ret = NULL; |
| 2433 | s->tlsext_ticket_expected = 0; |
Dr. Stephen Henson | e8da6a1 | 2008-09-03 22:17:11 +0000 | [diff] [blame] | 2434 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2435 | /* |
Matt Caswell | 9362c93 | 2016-11-08 16:10:21 +0000 | [diff] [blame] | 2436 | * If tickets disabled or not supported by the protocol version |
| 2437 | * (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] | 2438 | * resumption. |
| 2439 | */ |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 2440 | if (s->version <= SSL3_VERSION || !tls_use_ticket(s)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2441 | return 0; |
Matt Caswell | 9ceb242 | 2015-04-16 10:06:25 +0100 | [diff] [blame] | 2442 | |
Matt Caswell | cd99883 | 2016-10-23 00:41:11 +0100 | [diff] [blame] | 2443 | ticketext = tls_get_extension_by_type(hello->pre_proc_exts, |
| 2444 | hello->num_extensions, |
| 2445 | TLSEXT_TYPE_session_ticket); |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 2446 | if (ticketext == NULL) |
| 2447 | return 0; |
| 2448 | |
Matt Caswell | 6b473ac | 2016-11-24 16:59:48 +0000 | [diff] [blame^] | 2449 | ticketext->parsed = 1; |
| 2450 | |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 2451 | size = PACKET_remaining(&ticketext->data); |
| 2452 | if (size == 0) { |
| 2453 | /* |
| 2454 | * The client will accept a ticket but doesn't currently have |
| 2455 | * one. |
| 2456 | */ |
| 2457 | s->tlsext_ticket_expected = 1; |
| 2458 | return 1; |
Matt Caswell | 9ceb242 | 2015-04-16 10:06:25 +0100 | [diff] [blame] | 2459 | } |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 2460 | if (s->tls_session_secret_cb) { |
| 2461 | /* |
| 2462 | * Indicate that the ticket couldn't be decrypted rather than |
| 2463 | * generating the session from ticket now, trigger |
| 2464 | * abbreviated handshake based on external mechanism to |
| 2465 | * calculate the master secret later. |
| 2466 | */ |
| 2467 | return 2; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2468 | } |
Matt Caswell | 1ab3836 | 2016-10-22 17:24:37 +0100 | [diff] [blame] | 2469 | if (!PACKET_get_bytes(&ticketext->data, &etick, size)) { |
| 2470 | /* Shouldn't ever happen */ |
| 2471 | return -1; |
| 2472 | } |
| 2473 | retv = tls_decrypt_ticket(s, etick, size, hello->session_id, |
| 2474 | hello->session_id_len, ret); |
| 2475 | switch (retv) { |
| 2476 | case 2: /* ticket couldn't be decrypted */ |
| 2477 | s->tlsext_ticket_expected = 1; |
| 2478 | return 2; |
| 2479 | |
| 2480 | case 3: /* ticket was decrypted */ |
| 2481 | return 3; |
| 2482 | |
| 2483 | case 4: /* ticket decrypted but need to renew */ |
| 2484 | s->tlsext_ticket_expected = 1; |
| 2485 | return 3; |
| 2486 | |
| 2487 | default: /* fatal error */ |
| 2488 | return -1; |
| 2489 | } |
| 2490 | } |
| 2491 | |
Tim Hudson | 1d97c84 | 2014-12-28 12:48:40 +1000 | [diff] [blame] | 2492 | /*- |
| 2493 | * tls_decrypt_ticket attempts to decrypt a session ticket. |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 2494 | * |
| 2495 | * etick: points to the body of the session ticket extension. |
FdaSilvaYY | 8483a00 | 2016-03-10 21:34:48 +0100 | [diff] [blame] | 2496 | * eticklen: the length of the session tickets extension. |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 2497 | * sess_id: points at the session ID. |
| 2498 | * sesslen: the length of the session ID. |
| 2499 | * psess: (output) on return, if a ticket was decrypted, then this is set to |
| 2500 | * point to the resulting session. |
| 2501 | * |
| 2502 | * Returns: |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 2503 | * -2: fatal error, malloc failure. |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 2504 | * -1: fatal error, either from parsing or decrypting the ticket. |
| 2505 | * 2: the ticket couldn't be decrypted. |
| 2506 | * 3: a ticket was successfully decrypted and *psess was set. |
| 2507 | * 4: same as 3, but the ticket needs to be renewed. |
| 2508 | */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2509 | static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, |
Matt Caswell | d736bc1 | 2016-10-04 21:22:19 +0100 | [diff] [blame] | 2510 | size_t eticklen, const unsigned char *sess_id, |
Matt Caswell | ec60ccc | 2016-10-04 20:31:19 +0100 | [diff] [blame] | 2511 | size_t sesslen, SSL_SESSION **psess) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2512 | { |
| 2513 | SSL_SESSION *sess; |
| 2514 | unsigned char *sdec; |
| 2515 | const unsigned char *p; |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 2516 | int slen, renew_ticket = 0, ret = -1, declen; |
| 2517 | size_t mlen; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2518 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 2519 | HMAC_CTX *hctx = NULL; |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 2520 | EVP_CIPHER_CTX *ctx; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2521 | SSL_CTX *tctx = s->initial_ctx; |
Dr. Stephen Henson | e97763c | 2016-08-22 17:20:01 +0100 | [diff] [blame] | 2522 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2523 | /* Initialize session ticket encryption and HMAC contexts */ |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 2524 | hctx = HMAC_CTX_new(); |
| 2525 | if (hctx == NULL) |
| 2526 | return -2; |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 2527 | ctx = EVP_CIPHER_CTX_new(); |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 2528 | if (ctx == NULL) { |
| 2529 | ret = -2; |
| 2530 | goto err; |
| 2531 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2532 | if (tctx->tlsext_ticket_key_cb) { |
| 2533 | unsigned char *nctick = (unsigned char *)etick; |
| 2534 | int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 2535 | ctx, hctx, 0); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2536 | if (rv < 0) |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 2537 | goto err; |
| 2538 | if (rv == 0) { |
| 2539 | ret = 2; |
| 2540 | goto err; |
| 2541 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2542 | if (rv == 2) |
| 2543 | renew_ticket = 1; |
| 2544 | } else { |
| 2545 | /* Check key name matches */ |
TJ Saunders | 4e2e1ec | 2016-02-27 19:36:00 +0100 | [diff] [blame] | 2546 | if (memcmp(etick, tctx->tlsext_tick_key_name, |
| 2547 | sizeof(tctx->tlsext_tick_key_name)) != 0) { |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 2548 | ret = 2; |
| 2549 | goto err; |
| 2550 | } |
TJ Saunders | 4e2e1ec | 2016-02-27 19:36:00 +0100 | [diff] [blame] | 2551 | if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key, |
| 2552 | sizeof(tctx->tlsext_tick_hmac_key), |
Matt Caswell | 5f3d93e | 2015-11-06 16:31:21 +0000 | [diff] [blame] | 2553 | EVP_sha256(), NULL) <= 0 |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2554 | || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, |
| 2555 | tctx->tlsext_tick_aes_key, |
| 2556 | etick + sizeof(tctx->tlsext_tick_key_name)) <= |
| 2557 | 0) { |
Matt Caswell | 5f3d93e | 2015-11-06 16:31:21 +0000 | [diff] [blame] | 2558 | goto err; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2559 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2560 | } |
| 2561 | /* |
| 2562 | * Attempt to process session ticket, first conduct sanity and integrity |
| 2563 | * checks on ticket. |
| 2564 | */ |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 2565 | mlen = HMAC_size(hctx); |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 2566 | if (mlen == 0) { |
Matt Caswell | 5f3d93e | 2015-11-06 16:31:21 +0000 | [diff] [blame] | 2567 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2568 | } |
Dr. Stephen Henson | e97763c | 2016-08-22 17:20:01 +0100 | [diff] [blame] | 2569 | /* Sanity check ticket length: must exceed keyname + IV + HMAC */ |
| 2570 | if (eticklen <= |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 2571 | TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) { |
Dr. Stephen Henson | e97763c | 2016-08-22 17:20:01 +0100 | [diff] [blame] | 2572 | ret = 2; |
| 2573 | goto err; |
| 2574 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2575 | eticklen -= mlen; |
| 2576 | /* Check HMAC of encrypted ticket */ |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 2577 | if (HMAC_Update(hctx, etick, eticklen) <= 0 |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2578 | || HMAC_Final(hctx, tick_hmac, NULL) <= 0) { |
Matt Caswell | 5f3d93e | 2015-11-06 16:31:21 +0000 | [diff] [blame] | 2579 | goto err; |
| 2580 | } |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 2581 | HMAC_CTX_free(hctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2582 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 2583 | EVP_CIPHER_CTX_free(ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2584 | return 2; |
| 2585 | } |
| 2586 | /* Attempt to decrypt session data */ |
| 2587 | /* Move p after IV to start of encrypted ticket, update length */ |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 2588 | p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx); |
| 2589 | eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2590 | sdec = OPENSSL_malloc(eticklen); |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 2591 | if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, |
| 2592 | (int)eticklen) <= 0) { |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 2593 | EVP_CIPHER_CTX_free(ctx); |
Matt Caswell | d1247df | 2016-03-15 11:38:56 +0000 | [diff] [blame] | 2594 | OPENSSL_free(sdec); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2595 | return -1; |
| 2596 | } |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 2597 | if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) { |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 2598 | EVP_CIPHER_CTX_free(ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2599 | OPENSSL_free(sdec); |
| 2600 | return 2; |
| 2601 | } |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 2602 | slen += declen; |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 2603 | EVP_CIPHER_CTX_free(ctx); |
| 2604 | ctx = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2605 | p = sdec; |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 2606 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2607 | sess = d2i_SSL_SESSION(NULL, &p, slen); |
| 2608 | OPENSSL_free(sdec); |
| 2609 | if (sess) { |
| 2610 | /* |
| 2611 | * The session ID, if non-empty, is used by some clients to detect |
| 2612 | * that the ticket has been accepted. So we copy it to the session |
| 2613 | * structure. If it is empty set length to zero as required by |
| 2614 | * standard. |
| 2615 | */ |
| 2616 | if (sesslen) |
| 2617 | memcpy(sess->session_id, sess_id, sesslen); |
| 2618 | sess->session_id_length = sesslen; |
| 2619 | *psess = sess; |
| 2620 | if (renew_ticket) |
| 2621 | return 4; |
| 2622 | else |
| 2623 | return 3; |
| 2624 | } |
| 2625 | ERR_clear_error(); |
| 2626 | /* |
| 2627 | * For session parse failure, indicate that we need to send a new ticket. |
| 2628 | */ |
| 2629 | return 2; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2630 | err: |
Richard Levitte | 846ec07 | 2015-12-13 22:08:41 +0100 | [diff] [blame] | 2631 | EVP_CIPHER_CTX_free(ctx); |
Richard Levitte | bf7c681 | 2015-11-30 13:44:28 +0100 | [diff] [blame] | 2632 | HMAC_CTX_free(hctx); |
Matt Caswell | 35b1a43 | 2016-02-13 23:22:45 +0000 | [diff] [blame] | 2633 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2634 | } |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 2635 | |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 2636 | /* Tables to translate from NIDs to TLS v1.2 ids */ |
| 2637 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2638 | typedef struct { |
| 2639 | int nid; |
| 2640 | int id; |
| 2641 | } tls12_lookup; |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 2642 | |
Cristian Rodríguez | d97ed21 | 2014-04-20 18:41:15 -0300 | [diff] [blame] | 2643 | static const tls12_lookup tls12_md[] = { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2644 | {NID_md5, TLSEXT_hash_md5}, |
| 2645 | {NID_sha1, TLSEXT_hash_sha1}, |
| 2646 | {NID_sha224, TLSEXT_hash_sha224}, |
| 2647 | {NID_sha256, TLSEXT_hash_sha256}, |
| 2648 | {NID_sha384, TLSEXT_hash_sha384}, |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 2649 | {NID_sha512, TLSEXT_hash_sha512}, |
| 2650 | {NID_id_GostR3411_94, TLSEXT_hash_gostr3411}, |
| 2651 | {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256}, |
| 2652 | {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512}, |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 2653 | }; |
| 2654 | |
Cristian Rodríguez | d97ed21 | 2014-04-20 18:41:15 -0300 | [diff] [blame] | 2655 | static const tls12_lookup tls12_sig[] = { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2656 | {EVP_PKEY_RSA, TLSEXT_signature_rsa}, |
| 2657 | {EVP_PKEY_DSA, TLSEXT_signature_dsa}, |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 2658 | {EVP_PKEY_EC, TLSEXT_signature_ecdsa}, |
| 2659 | {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001}, |
| 2660 | {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256}, |
| 2661 | {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512} |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 2662 | }; |
| 2663 | |
Cristian Rodríguez | d97ed21 | 2014-04-20 18:41:15 -0300 | [diff] [blame] | 2664 | static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2665 | { |
| 2666 | size_t i; |
| 2667 | for (i = 0; i < tlen; i++) { |
| 2668 | if (table[i].nid == nid) |
| 2669 | return table[i].id; |
| 2670 | } |
| 2671 | return -1; |
| 2672 | } |
Dr. Stephen Henson | e7f8ff4 | 2012-03-06 14:28:21 +0000 | [diff] [blame] | 2673 | |
Cristian Rodríguez | d97ed21 | 2014-04-20 18:41:15 -0300 | [diff] [blame] | 2674 | static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2675 | { |
| 2676 | size_t i; |
| 2677 | for (i = 0; i < tlen; i++) { |
| 2678 | if ((table[i].id) == id) |
| 2679 | return table[i].nid; |
| 2680 | } |
| 2681 | return NID_undef; |
| 2682 | } |
Dr. Stephen Henson | a2f9200 | 2011-05-09 15:44:01 +0000 | [diff] [blame] | 2683 | |
Matt Caswell | 6400f33 | 2016-09-14 11:10:37 +0100 | [diff] [blame] | 2684 | int tls12_get_sigandhash(WPACKET *pkt, const EVP_PKEY *pk, const EVP_MD *md) |
| 2685 | { |
| 2686 | int sig_id, md_id; |
Matt Caswell | 418a18a | 2016-09-20 10:16:15 +0100 | [diff] [blame] | 2687 | |
| 2688 | if (md == NULL) |
Matt Caswell | 6400f33 | 2016-09-14 11:10:37 +0100 | [diff] [blame] | 2689 | return 0; |
| 2690 | md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md)); |
| 2691 | if (md_id == -1) |
| 2692 | return 0; |
| 2693 | sig_id = tls12_get_sigid(pk); |
| 2694 | if (sig_id == -1) |
| 2695 | return 0; |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 2696 | if (!WPACKET_put_bytes_u8(pkt, md_id) || !WPACKET_put_bytes_u8(pkt, sig_id)) |
Matt Caswell | 6400f33 | 2016-09-14 11:10:37 +0100 | [diff] [blame] | 2697 | return 0; |
| 2698 | |
| 2699 | return 1; |
| 2700 | } |
| 2701 | |
Dr. Stephen Henson | a2f9200 | 2011-05-09 15:44:01 +0000 | [diff] [blame] | 2702 | int tls12_get_sigid(const EVP_PKEY *pk) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2703 | { |
Dr. Stephen Henson | 3aeb934 | 2016-01-19 00:21:12 +0000 | [diff] [blame] | 2704 | return tls12_find_id(EVP_PKEY_id(pk), tls12_sig, OSSL_NELEM(tls12_sig)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2705 | } |
Dr. Stephen Henson | a2f9200 | 2011-05-09 15:44:01 +0000 | [diff] [blame] | 2706 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2707 | typedef struct { |
| 2708 | int nid; |
| 2709 | int secbits; |
Dr. Stephen Henson | 7afd231 | 2015-11-29 16:54:27 +0000 | [diff] [blame] | 2710 | int md_idx; |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 2711 | unsigned char tlsext_hash; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2712 | } tls12_hash_info; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2713 | |
| 2714 | static const tls12_hash_info tls12_md_info[] = { |
Dr. Stephen Henson | 7afd231 | 2015-11-29 16:54:27 +0000 | [diff] [blame] | 2715 | {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5}, |
| 2716 | {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1}, |
| 2717 | {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224}, |
| 2718 | {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256}, |
| 2719 | {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384}, |
| 2720 | {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512}, |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2721 | {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411}, |
| 2722 | {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX, |
| 2723 | TLSEXT_hash_gostr34112012_256}, |
| 2724 | {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX, |
| 2725 | TLSEXT_hash_gostr34112012_512}, |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2726 | }; |
| 2727 | |
| 2728 | static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2729 | { |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 2730 | unsigned int i; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2731 | if (hash_alg == 0) |
| 2732 | return NULL; |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 2733 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2734 | for (i = 0; i < OSSL_NELEM(tls12_md_info); i++) { |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 2735 | if (tls12_md_info[i].tlsext_hash == hash_alg) |
| 2736 | return tls12_md_info + i; |
| 2737 | } |
| 2738 | |
| 2739 | return NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2740 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2741 | |
Dr. Stephen Henson | a2f9200 | 2011-05-09 15:44:01 +0000 | [diff] [blame] | 2742 | const EVP_MD *tls12_get_hash(unsigned char hash_alg) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2743 | { |
| 2744 | const tls12_hash_info *inf; |
| 2745 | if (hash_alg == TLSEXT_hash_md5 && FIPS_mode()) |
| 2746 | return NULL; |
| 2747 | inf = tls12_get_hash_info(hash_alg); |
Dr. Stephen Henson | 7afd231 | 2015-11-29 16:54:27 +0000 | [diff] [blame] | 2748 | if (!inf) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2749 | return NULL; |
Dr. Stephen Henson | 7afd231 | 2015-11-29 16:54:27 +0000 | [diff] [blame] | 2750 | return ssl_md(inf->md_idx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2751 | } |
Dr. Stephen Henson | a2f9200 | 2011-05-09 15:44:01 +0000 | [diff] [blame] | 2752 | |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 2753 | static int tls12_get_pkey_idx(unsigned char sig_alg) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2754 | { |
| 2755 | switch (sig_alg) { |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 2756 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2757 | case TLSEXT_signature_rsa: |
| 2758 | return SSL_PKEY_RSA_SIGN; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 2759 | #endif |
| 2760 | #ifndef OPENSSL_NO_DSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2761 | case TLSEXT_signature_dsa: |
| 2762 | return SSL_PKEY_DSA_SIGN; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 2763 | #endif |
| 2764 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2765 | case TLSEXT_signature_ecdsa: |
| 2766 | return SSL_PKEY_ECC; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 2767 | #endif |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2768 | #ifndef OPENSSL_NO_GOST |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 2769 | case TLSEXT_signature_gostr34102001: |
| 2770 | return SSL_PKEY_GOST01; |
| 2771 | |
| 2772 | case TLSEXT_signature_gostr34102012_256: |
| 2773 | return SSL_PKEY_GOST12_256; |
| 2774 | |
| 2775 | case TLSEXT_signature_gostr34102012_512: |
| 2776 | return SSL_PKEY_GOST12_512; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2777 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2778 | } |
| 2779 | return -1; |
| 2780 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 2781 | |
| 2782 | /* Convert TLS 1.2 signature algorithm extension values into NIDs */ |
| 2783 | static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2784 | int *psignhash_nid, const unsigned char *data) |
| 2785 | { |
Matt Caswell | 330dcb0 | 2015-11-11 10:44:07 +0000 | [diff] [blame] | 2786 | int sign_nid = NID_undef, hash_nid = NID_undef; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2787 | if (!phash_nid && !psign_nid && !psignhash_nid) |
| 2788 | return; |
| 2789 | if (phash_nid || psignhash_nid) { |
Dr. Stephen Henson | b6eb982 | 2015-05-02 18:30:00 +0100 | [diff] [blame] | 2790 | hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2791 | if (phash_nid) |
| 2792 | *phash_nid = hash_nid; |
| 2793 | } |
| 2794 | if (psign_nid || psignhash_nid) { |
Dr. Stephen Henson | b6eb982 | 2015-05-02 18:30:00 +0100 | [diff] [blame] | 2795 | sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2796 | if (psign_nid) |
| 2797 | *psign_nid = sign_nid; |
| 2798 | } |
| 2799 | if (psignhash_nid) { |
Matt Caswell | 330dcb0 | 2015-11-11 10:44:07 +0000 | [diff] [blame] | 2800 | if (sign_nid == NID_undef || hash_nid == NID_undef |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 2801 | || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid) <= 0) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2802 | *psignhash_nid = NID_undef; |
| 2803 | } |
| 2804 | } |
| 2805 | |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2806 | /* Check to see if a signature algorithm is allowed */ |
| 2807 | static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2808 | { |
| 2809 | /* See if we have an entry in the hash table and it is enabled */ |
| 2810 | const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]); |
Dr. Stephen Henson | 7afd231 | 2015-11-29 16:54:27 +0000 | [diff] [blame] | 2811 | if (hinf == NULL || ssl_md(hinf->md_idx) == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2812 | return 0; |
| 2813 | /* See if public key algorithm allowed */ |
| 2814 | if (tls12_get_pkey_idx(ptmp[1]) == -1) |
| 2815 | return 0; |
| 2816 | /* Finally see if security callback allows it */ |
| 2817 | return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp); |
| 2818 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2819 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2820 | /* |
| 2821 | * Get a mask of disabled public key algorithms based on supported signature |
| 2822 | * algorithms. For example if no signature algorithm supports RSA then RSA is |
| 2823 | * disabled. |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2824 | */ |
| 2825 | |
Dr. Stephen Henson | 90d9e49 | 2015-11-05 16:14:17 +0000 | [diff] [blame] | 2826 | 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] | 2827 | { |
| 2828 | const unsigned char *sigalgs; |
| 2829 | size_t i, sigalgslen; |
| 2830 | int have_rsa = 0, have_dsa = 0, have_ecdsa = 0; |
| 2831 | /* |
| 2832 | * Now go through all signature algorithms seeing if we support any for |
| 2833 | * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep |
| 2834 | * down calls to security callback only check if we have to. |
| 2835 | */ |
| 2836 | sigalgslen = tls12_get_psigalgs(s, &sigalgs); |
| 2837 | for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) { |
| 2838 | switch (sigalgs[1]) { |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 2839 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2840 | case TLSEXT_signature_rsa: |
| 2841 | if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs)) |
| 2842 | have_rsa = 1; |
| 2843 | break; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 2844 | #endif |
| 2845 | #ifndef OPENSSL_NO_DSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2846 | case TLSEXT_signature_dsa: |
| 2847 | if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs)) |
| 2848 | have_dsa = 1; |
| 2849 | break; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 2850 | #endif |
| 2851 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2852 | case TLSEXT_signature_ecdsa: |
| 2853 | if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs)) |
| 2854 | have_ecdsa = 1; |
| 2855 | break; |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 2856 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2857 | } |
| 2858 | } |
| 2859 | if (!have_rsa) |
| 2860 | *pmask_a |= SSL_aRSA; |
| 2861 | if (!have_dsa) |
| 2862 | *pmask_a |= SSL_aDSS; |
| 2863 | if (!have_ecdsa) |
| 2864 | *pmask_a |= SSL_aECDSA; |
| 2865 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 2866 | |
Matt Caswell | ae2f7b3 | 2016-09-05 17:34:04 +0100 | [diff] [blame] | 2867 | int tls12_copy_sigalgs(SSL *s, WPACKET *pkt, |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 2868 | const unsigned char *psig, size_t psiglen) |
| 2869 | { |
| 2870 | size_t i; |
Matt Caswell | c0f9e23 | 2016-09-13 09:40:38 +0100 | [diff] [blame] | 2871 | |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 2872 | for (i = 0; i < psiglen; i += 2, psig += 2) { |
| 2873 | if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) { |
Matt Caswell | 08029df | 2016-09-20 14:47:44 +0100 | [diff] [blame] | 2874 | if (!WPACKET_put_bytes_u8(pkt, psig[0]) |
| 2875 | || !WPACKET_put_bytes_u8(pkt, psig[1])) |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 2876 | return 0; |
| 2877 | } |
| 2878 | } |
| 2879 | return 1; |
| 2880 | } |
| 2881 | |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 2882 | /* Given preference and allowed sigalgs set shared sigalgs */ |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 2883 | static size_t tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig, |
| 2884 | const unsigned char *pref, size_t preflen, |
| 2885 | const unsigned char *allow, size_t allowlen) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2886 | { |
| 2887 | const unsigned char *ptmp, *atmp; |
| 2888 | size_t i, j, nmatch = 0; |
| 2889 | for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) { |
| 2890 | /* Skip disabled hashes or signature algorithms */ |
| 2891 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp)) |
| 2892 | continue; |
| 2893 | for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) { |
| 2894 | if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) { |
| 2895 | nmatch++; |
| 2896 | if (shsig) { |
| 2897 | shsig->rhash = ptmp[0]; |
| 2898 | shsig->rsign = ptmp[1]; |
| 2899 | tls1_lookup_sigalg(&shsig->hash_nid, |
| 2900 | &shsig->sign_nid, |
| 2901 | &shsig->signandhash_nid, ptmp); |
| 2902 | shsig++; |
| 2903 | } |
| 2904 | break; |
| 2905 | } |
| 2906 | } |
| 2907 | } |
| 2908 | return nmatch; |
| 2909 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 2910 | |
| 2911 | /* Set shared signature algorithms for SSL structures */ |
| 2912 | static int tls1_set_shared_sigalgs(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2913 | { |
| 2914 | const unsigned char *pref, *allow, *conf; |
| 2915 | size_t preflen, allowlen, conflen; |
| 2916 | size_t nmatch; |
| 2917 | TLS_SIGALGS *salgs = NULL; |
| 2918 | CERT *c = s->cert; |
| 2919 | unsigned int is_suiteb = tls1_suiteb(s); |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 2920 | |
| 2921 | OPENSSL_free(c->shared_sigalgs); |
| 2922 | c->shared_sigalgs = NULL; |
| 2923 | c->shared_sigalgslen = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2924 | /* If client use client signature algorithms if not NULL */ |
| 2925 | if (!s->server && c->client_sigalgs && !is_suiteb) { |
| 2926 | conf = c->client_sigalgs; |
| 2927 | conflen = c->client_sigalgslen; |
| 2928 | } else if (c->conf_sigalgs && !is_suiteb) { |
| 2929 | conf = c->conf_sigalgs; |
| 2930 | conflen = c->conf_sigalgslen; |
| 2931 | } else |
| 2932 | conflen = tls12_get_psigalgs(s, &conf); |
| 2933 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) { |
| 2934 | pref = conf; |
| 2935 | preflen = conflen; |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 2936 | allow = s->s3->tmp.peer_sigalgs; |
| 2937 | allowlen = s->s3->tmp.peer_sigalgslen; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2938 | } else { |
| 2939 | allow = conf; |
| 2940 | allowlen = conflen; |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 2941 | pref = s->s3->tmp.peer_sigalgs; |
| 2942 | preflen = s->s3->tmp.peer_sigalgslen; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2943 | } |
| 2944 | nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen); |
Dr. Stephen Henson | 34e3edb | 2015-03-03 13:20:57 +0000 | [diff] [blame] | 2945 | if (nmatch) { |
| 2946 | salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS)); |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 2947 | if (salgs == NULL) |
Dr. Stephen Henson | 34e3edb | 2015-03-03 13:20:57 +0000 | [diff] [blame] | 2948 | return 0; |
| 2949 | nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen); |
| 2950 | } else { |
| 2951 | salgs = NULL; |
| 2952 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2953 | c->shared_sigalgs = salgs; |
| 2954 | c->shared_sigalgslen = nmatch; |
| 2955 | return 1; |
| 2956 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 2957 | |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 2958 | /* Set preferred digest for each key type */ |
| 2959 | |
Matt Caswell | d736bc1 | 2016-10-04 21:22:19 +0100 | [diff] [blame] | 2960 | int tls1_save_sigalgs(SSL *s, const unsigned char *data, size_t dsize) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2961 | { |
| 2962 | CERT *c = s->cert; |
| 2963 | /* Extension ignored for inappropriate versions */ |
| 2964 | if (!SSL_USE_SIGALGS(s)) |
| 2965 | return 1; |
| 2966 | /* Should never happen */ |
| 2967 | if (!c) |
| 2968 | return 0; |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 2969 | |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 2970 | OPENSSL_free(s->s3->tmp.peer_sigalgs); |
| 2971 | s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize); |
| 2972 | if (s->s3->tmp.peer_sigalgs == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2973 | return 0; |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 2974 | s->s3->tmp.peer_sigalgslen = dsize; |
| 2975 | memcpy(s->s3->tmp.peer_sigalgs, data, dsize); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2976 | return 1; |
| 2977 | } |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 2978 | |
Dr. Stephen Henson | c800c27 | 2014-10-09 20:37:27 +0100 | [diff] [blame] | 2979 | int tls1_process_sigalgs(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2980 | { |
| 2981 | int idx; |
| 2982 | size_t i; |
| 2983 | const EVP_MD *md; |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 2984 | const EVP_MD **pmd = s->s3->tmp.md; |
Dr. Stephen Henson | f7d5348 | 2015-07-14 23:19:11 +0100 | [diff] [blame] | 2985 | uint32_t *pvalid = s->s3->tmp.valid_flags; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2986 | CERT *c = s->cert; |
| 2987 | TLS_SIGALGS *sigptr; |
| 2988 | if (!tls1_set_shared_sigalgs(s)) |
| 2989 | return 0; |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 2990 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2991 | for (i = 0, sigptr = c->shared_sigalgs; |
| 2992 | i < c->shared_sigalgslen; i++, sigptr++) { |
| 2993 | idx = tls12_get_pkey_idx(sigptr->rsign); |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 2994 | if (idx > 0 && pmd[idx] == NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2995 | md = tls12_get_hash(sigptr->rhash); |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 2996 | pmd[idx] = md; |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 2997 | pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2998 | if (idx == SSL_PKEY_RSA_SIGN) { |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 2999 | pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN; |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 3000 | pmd[SSL_PKEY_RSA_ENC] = md; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3001 | } |
| 3002 | } |
Dr. Stephen Henson | 6b7be58 | 2011-05-06 13:00:07 +0000 | [diff] [blame] | 3003 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3004 | } |
| 3005 | /* |
| 3006 | * In strict mode leave unset digests as NULL to indicate we can't use |
| 3007 | * the certificate for signing. |
| 3008 | */ |
| 3009 | if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { |
| 3010 | /* |
| 3011 | * Set any remaining keys to default values. NOTE: if alg is not |
| 3012 | * supported it stays as NULL. |
| 3013 | */ |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 3014 | #ifndef OPENSSL_NO_DSA |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 3015 | if (pmd[SSL_PKEY_DSA_SIGN] == NULL) |
| 3016 | pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1(); |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 3017 | #endif |
| 3018 | #ifndef OPENSSL_NO_RSA |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 3019 | if (pmd[SSL_PKEY_RSA_SIGN] == NULL) { |
| 3020 | pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1(); |
| 3021 | pmd[SSL_PKEY_RSA_ENC] = EVP_sha1(); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3022 | } |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 3023 | #endif |
| 3024 | #ifndef OPENSSL_NO_EC |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 3025 | if (pmd[SSL_PKEY_ECC] == NULL) |
| 3026 | pmd[SSL_PKEY_ECC] = EVP_sha1(); |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 3027 | #endif |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 3028 | #ifndef OPENSSL_NO_GOST |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 3029 | if (pmd[SSL_PKEY_GOST01] == NULL) |
| 3030 | pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94); |
| 3031 | if (pmd[SSL_PKEY_GOST12_256] == NULL) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 3032 | pmd[SSL_PKEY_GOST12_256] = |
| 3033 | EVP_get_digestbynid(NID_id_GostR3411_2012_256); |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 3034 | if (pmd[SSL_PKEY_GOST12_512] == NULL) |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 3035 | pmd[SSL_PKEY_GOST12_512] = |
| 3036 | EVP_get_digestbynid(NID_id_GostR3411_2012_512); |
| 3037 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3038 | } |
| 3039 | return 1; |
| 3040 | } |
Dr. Stephen Henson | 4817504 | 2011-12-31 22:59:57 +0000 | [diff] [blame] | 3041 | |
Dr. Stephen Henson | e7f8ff4 | 2012-03-06 14:28:21 +0000 | [diff] [blame] | 3042 | int SSL_get_sigalgs(SSL *s, int idx, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3043 | int *psign, int *phash, int *psignhash, |
| 3044 | unsigned char *rsig, unsigned char *rhash) |
| 3045 | { |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 3046 | const unsigned char *psig = s->s3->tmp.peer_sigalgs; |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 3047 | size_t numsigalgs = s->s3->tmp.peer_sigalgslen / 2; |
| 3048 | if (psig == NULL || numsigalgs > INT_MAX) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3049 | return 0; |
| 3050 | if (idx >= 0) { |
| 3051 | idx <<= 1; |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 3052 | if (idx >= (int)s->s3->tmp.peer_sigalgslen) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3053 | return 0; |
| 3054 | psig += idx; |
| 3055 | if (rhash) |
| 3056 | *rhash = psig[0]; |
| 3057 | if (rsig) |
| 3058 | *rsig = psig[1]; |
| 3059 | tls1_lookup_sigalg(phash, psign, psignhash, psig); |
| 3060 | } |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 3061 | return (int)numsigalgs; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3062 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 3063 | |
| 3064 | int SSL_get_shared_sigalgs(SSL *s, int idx, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3065 | int *psign, int *phash, int *psignhash, |
| 3066 | unsigned char *rsig, unsigned char *rhash) |
| 3067 | { |
| 3068 | TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs; |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 3069 | if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen |
| 3070 | || s->cert->shared_sigalgslen > INT_MAX) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3071 | return 0; |
| 3072 | shsigalgs += idx; |
| 3073 | if (phash) |
| 3074 | *phash = shsigalgs->hash_nid; |
| 3075 | if (psign) |
| 3076 | *psign = shsigalgs->sign_nid; |
| 3077 | if (psignhash) |
| 3078 | *psignhash = shsigalgs->signandhash_nid; |
| 3079 | if (rsig) |
| 3080 | *rsig = shsigalgs->rsign; |
| 3081 | if (rhash) |
| 3082 | *rhash = shsigalgs->rhash; |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 3083 | return (int)s->cert->shared_sigalgslen; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3084 | } |
Dr. Stephen Henson | e7f8ff4 | 2012-03-06 14:28:21 +0000 | [diff] [blame] | 3085 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 3086 | #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3087 | |
| 3088 | typedef struct { |
| 3089 | size_t sigalgcnt; |
| 3090 | int sigalgs[MAX_SIGALGLEN]; |
| 3091 | } sig_cb_st; |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 3092 | |
Dr. Stephen Henson | 431f458 | 2015-07-23 14:57:42 +0100 | [diff] [blame] | 3093 | static void get_sigorhash(int *psig, int *phash, const char *str) |
| 3094 | { |
| 3095 | if (strcmp(str, "RSA") == 0) { |
| 3096 | *psig = EVP_PKEY_RSA; |
| 3097 | } else if (strcmp(str, "DSA") == 0) { |
| 3098 | *psig = EVP_PKEY_DSA; |
| 3099 | } else if (strcmp(str, "ECDSA") == 0) { |
| 3100 | *psig = EVP_PKEY_EC; |
| 3101 | } else { |
| 3102 | *phash = OBJ_sn2nid(str); |
| 3103 | if (*phash == NID_undef) |
| 3104 | *phash = OBJ_ln2nid(str); |
| 3105 | } |
| 3106 | } |
| 3107 | |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 3108 | static int sig_cb(const char *elem, int len, void *arg) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3109 | { |
| 3110 | sig_cb_st *sarg = arg; |
| 3111 | size_t i; |
| 3112 | char etmp[20], *p; |
Dr. Stephen Henson | 431f458 | 2015-07-23 14:57:42 +0100 | [diff] [blame] | 3113 | int sig_alg = NID_undef, hash_alg = NID_undef; |
Kurt Roeckx | 2747d73 | 2015-01-24 14:46:50 +0100 | [diff] [blame] | 3114 | if (elem == NULL) |
| 3115 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3116 | if (sarg->sigalgcnt == MAX_SIGALGLEN) |
| 3117 | return 0; |
| 3118 | if (len > (int)(sizeof(etmp) - 1)) |
| 3119 | return 0; |
| 3120 | memcpy(etmp, elem, len); |
| 3121 | etmp[len] = 0; |
| 3122 | p = strchr(etmp, '+'); |
| 3123 | if (!p) |
| 3124 | return 0; |
| 3125 | *p = 0; |
| 3126 | p++; |
| 3127 | if (!*p) |
| 3128 | return 0; |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 3129 | |
Dr. Stephen Henson | 431f458 | 2015-07-23 14:57:42 +0100 | [diff] [blame] | 3130 | get_sigorhash(&sig_alg, &hash_alg, etmp); |
| 3131 | get_sigorhash(&sig_alg, &hash_alg, p); |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 3132 | |
Dr. Stephen Henson | 431f458 | 2015-07-23 14:57:42 +0100 | [diff] [blame] | 3133 | if (sig_alg == NID_undef || hash_alg == NID_undef) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3134 | return 0; |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 3135 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3136 | for (i = 0; i < sarg->sigalgcnt; i += 2) { |
| 3137 | if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg) |
| 3138 | return 0; |
| 3139 | } |
| 3140 | sarg->sigalgs[sarg->sigalgcnt++] = hash_alg; |
| 3141 | sarg->sigalgs[sarg->sigalgcnt++] = sig_alg; |
| 3142 | return 1; |
| 3143 | } |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 3144 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3145 | /* |
FdaSilvaYY | 9d22666 | 2016-06-28 22:39:55 +0200 | [diff] [blame] | 3146 | * Set supported signature algorithms based on a colon separated list of the |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3147 | * form sig+hash e.g. RSA+SHA512:DSA+SHA512 |
| 3148 | */ |
Dr. Stephen Henson | 3dbc46d | 2012-07-03 12:51:14 +0000 | [diff] [blame] | 3149 | int tls1_set_sigalgs_list(CERT *c, const char *str, int client) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3150 | { |
| 3151 | sig_cb_st sig; |
| 3152 | sig.sigalgcnt = 0; |
| 3153 | if (!CONF_parse_list(str, ':', 1, sig_cb, &sig)) |
| 3154 | return 0; |
| 3155 | if (c == NULL) |
| 3156 | return 1; |
| 3157 | return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client); |
| 3158 | } |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 3159 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 3160 | 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] | 3161 | { |
| 3162 | unsigned char *sigalgs, *sptr; |
| 3163 | int rhash, rsign; |
| 3164 | size_t i; |
| 3165 | if (salglen & 1) |
| 3166 | return 0; |
| 3167 | sigalgs = OPENSSL_malloc(salglen); |
| 3168 | if (sigalgs == NULL) |
| 3169 | return 0; |
| 3170 | for (i = 0, sptr = sigalgs; i < salglen; i += 2) { |
Dr. Stephen Henson | b6eb982 | 2015-05-02 18:30:00 +0100 | [diff] [blame] | 3171 | rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md)); |
| 3172 | rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig)); |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 3173 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3174 | if (rhash == -1 || rsign == -1) |
| 3175 | goto err; |
| 3176 | *sptr++ = rhash; |
| 3177 | *sptr++ = rsign; |
| 3178 | } |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 3179 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3180 | if (client) { |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 3181 | OPENSSL_free(c->client_sigalgs); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3182 | c->client_sigalgs = sigalgs; |
| 3183 | c->client_sigalgslen = salglen; |
| 3184 | } else { |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 3185 | OPENSSL_free(c->conf_sigalgs); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3186 | c->conf_sigalgs = sigalgs; |
| 3187 | c->conf_sigalgslen = salglen; |
| 3188 | } |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 3189 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3190 | return 1; |
Dr. Stephen Henson | 0f229cc | 2012-06-22 14:03:31 +0000 | [diff] [blame] | 3191 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3192 | err: |
| 3193 | OPENSSL_free(sigalgs); |
| 3194 | return 0; |
| 3195 | } |
Dr. Stephen Henson | 4453cd8 | 2012-06-25 14:32:30 +0000 | [diff] [blame] | 3196 | |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 3197 | 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] | 3198 | { |
| 3199 | int sig_nid; |
| 3200 | size_t i; |
| 3201 | if (default_nid == -1) |
| 3202 | return 1; |
| 3203 | sig_nid = X509_get_signature_nid(x); |
| 3204 | if (default_nid) |
| 3205 | return sig_nid == default_nid ? 1 : 0; |
| 3206 | for (i = 0; i < c->shared_sigalgslen; i++) |
| 3207 | if (sig_nid == c->shared_sigalgs[i].signandhash_nid) |
| 3208 | return 1; |
| 3209 | return 0; |
| 3210 | } |
| 3211 | |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 3212 | /* Check to see if a certificate issuer name matches list of CA names */ |
| 3213 | 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] | 3214 | { |
| 3215 | X509_NAME *nm; |
| 3216 | int i; |
| 3217 | nm = X509_get_issuer_name(x); |
| 3218 | for (i = 0; i < sk_X509_NAME_num(names); i++) { |
| 3219 | if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i))) |
| 3220 | return 1; |
| 3221 | } |
| 3222 | return 0; |
| 3223 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 3224 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3225 | /* |
| 3226 | * Check certificate chain is consistent with TLS extensions and is usable by |
| 3227 | * server. This servers two purposes: it allows users to check chains before |
| 3228 | * passing them to the server and it allows the server to check chains before |
| 3229 | * attempting to use them. |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 3230 | */ |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 3231 | |
| 3232 | /* Flags which need to be set for a certificate when stict mode not set */ |
| 3233 | |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 3234 | #define CERT_PKEY_VALID_FLAGS \ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3235 | (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM) |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 3236 | /* Strict mode flags */ |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame] | 3237 | #define CERT_PKEY_STRICT_FLAGS \ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3238 | (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \ |
| 3239 | | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE) |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 3240 | |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 3241 | 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] | 3242 | int idx) |
| 3243 | { |
| 3244 | int i; |
| 3245 | int rv = 0; |
| 3246 | int check_flags = 0, strict_mode; |
| 3247 | CERT_PKEY *cpk = NULL; |
| 3248 | CERT *c = s->cert; |
Dr. Stephen Henson | f7d5348 | 2015-07-14 23:19:11 +0100 | [diff] [blame] | 3249 | uint32_t *pvalid; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3250 | unsigned int suiteb_flags = tls1_suiteb(s); |
| 3251 | /* idx == -1 means checking server chains */ |
| 3252 | if (idx != -1) { |
| 3253 | /* idx == -2 means checking client certificate chains */ |
| 3254 | if (idx == -2) { |
| 3255 | cpk = c->key; |
Matt Caswell | 348240c | 2016-10-19 15:11:24 +0100 | [diff] [blame] | 3256 | idx = (int)(cpk - c->pkeys); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3257 | } else |
| 3258 | cpk = c->pkeys + idx; |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 3259 | pvalid = s->s3->tmp.valid_flags + idx; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3260 | x = cpk->x509; |
| 3261 | pk = cpk->privatekey; |
| 3262 | chain = cpk->chain; |
| 3263 | strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT; |
| 3264 | /* If no cert or key, forget it */ |
| 3265 | if (!x || !pk) |
| 3266 | goto end; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3267 | } else { |
| 3268 | if (!x || !pk) |
Matt Caswell | d813f9e | 2015-03-11 17:01:38 +0000 | [diff] [blame] | 3269 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3270 | idx = ssl_cert_type(x, pk); |
| 3271 | if (idx == -1) |
Matt Caswell | d813f9e | 2015-03-11 17:01:38 +0000 | [diff] [blame] | 3272 | return 0; |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 3273 | pvalid = s->s3->tmp.valid_flags + idx; |
| 3274 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3275 | if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) |
| 3276 | check_flags = CERT_PKEY_STRICT_FLAGS; |
| 3277 | else |
| 3278 | check_flags = CERT_PKEY_VALID_FLAGS; |
| 3279 | strict_mode = 1; |
| 3280 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 3281 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3282 | if (suiteb_flags) { |
| 3283 | int ok; |
| 3284 | if (check_flags) |
| 3285 | check_flags |= CERT_PKEY_SUITEB; |
| 3286 | ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags); |
| 3287 | if (ok == X509_V_OK) |
| 3288 | rv |= CERT_PKEY_SUITEB; |
| 3289 | else if (!check_flags) |
| 3290 | goto end; |
| 3291 | } |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 3292 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3293 | /* |
| 3294 | * Check all signature algorithms are consistent with signature |
| 3295 | * algorithms extension if TLS 1.2 or later and strict mode. |
| 3296 | */ |
| 3297 | if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) { |
| 3298 | int default_nid; |
| 3299 | unsigned char rsign = 0; |
Dr. Stephen Henson | 76106e6 | 2015-05-12 17:17:37 +0100 | [diff] [blame] | 3300 | if (s->s3->tmp.peer_sigalgs) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3301 | default_nid = 0; |
| 3302 | /* If no sigalgs extension use defaults from RFC5246 */ |
| 3303 | else { |
| 3304 | switch (idx) { |
| 3305 | case SSL_PKEY_RSA_ENC: |
| 3306 | case SSL_PKEY_RSA_SIGN: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3307 | rsign = TLSEXT_signature_rsa; |
| 3308 | default_nid = NID_sha1WithRSAEncryption; |
| 3309 | break; |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 3310 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3311 | case SSL_PKEY_DSA_SIGN: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3312 | rsign = TLSEXT_signature_dsa; |
| 3313 | default_nid = NID_dsaWithSHA1; |
| 3314 | break; |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 3315 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3316 | case SSL_PKEY_ECC: |
| 3317 | rsign = TLSEXT_signature_ecdsa; |
| 3318 | default_nid = NID_ecdsa_with_SHA1; |
| 3319 | break; |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 3320 | |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 3321 | case SSL_PKEY_GOST01: |
| 3322 | rsign = TLSEXT_signature_gostr34102001; |
| 3323 | default_nid = NID_id_GostR3411_94_with_GostR3410_2001; |
| 3324 | break; |
| 3325 | |
| 3326 | case SSL_PKEY_GOST12_256: |
| 3327 | rsign = TLSEXT_signature_gostr34102012_256; |
| 3328 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256; |
| 3329 | break; |
| 3330 | |
| 3331 | case SSL_PKEY_GOST12_512: |
| 3332 | rsign = TLSEXT_signature_gostr34102012_512; |
| 3333 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512; |
| 3334 | break; |
| 3335 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3336 | default: |
| 3337 | default_nid = -1; |
| 3338 | break; |
| 3339 | } |
| 3340 | } |
| 3341 | /* |
| 3342 | * If peer sent no signature algorithms extension and we have set |
| 3343 | * preferred signature algorithms check we support sha1. |
| 3344 | */ |
| 3345 | if (default_nid > 0 && c->conf_sigalgs) { |
| 3346 | size_t j; |
| 3347 | const unsigned char *p = c->conf_sigalgs; |
| 3348 | for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) { |
| 3349 | if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign) |
| 3350 | break; |
| 3351 | } |
| 3352 | if (j == c->conf_sigalgslen) { |
| 3353 | if (check_flags) |
| 3354 | goto skip_sigs; |
| 3355 | else |
| 3356 | goto end; |
| 3357 | } |
| 3358 | } |
| 3359 | /* Check signature algorithm of each cert in chain */ |
| 3360 | if (!tls1_check_sig_alg(c, x, default_nid)) { |
| 3361 | if (!check_flags) |
| 3362 | goto end; |
| 3363 | } else |
| 3364 | rv |= CERT_PKEY_EE_SIGNATURE; |
| 3365 | rv |= CERT_PKEY_CA_SIGNATURE; |
| 3366 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 3367 | if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) { |
| 3368 | if (check_flags) { |
| 3369 | rv &= ~CERT_PKEY_CA_SIGNATURE; |
| 3370 | break; |
| 3371 | } else |
| 3372 | goto end; |
| 3373 | } |
| 3374 | } |
| 3375 | } |
| 3376 | /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */ |
| 3377 | else if (check_flags) |
| 3378 | rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE; |
| 3379 | skip_sigs: |
| 3380 | /* Check cert parameters are consistent */ |
| 3381 | if (tls1_check_cert_param(s, x, check_flags ? 1 : 2)) |
| 3382 | rv |= CERT_PKEY_EE_PARAM; |
| 3383 | else if (!check_flags) |
| 3384 | goto end; |
| 3385 | if (!s->server) |
| 3386 | rv |= CERT_PKEY_CA_PARAM; |
| 3387 | /* In strict mode check rest of chain too */ |
| 3388 | else if (strict_mode) { |
| 3389 | rv |= CERT_PKEY_CA_PARAM; |
| 3390 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 3391 | X509 *ca = sk_X509_value(chain, i); |
| 3392 | if (!tls1_check_cert_param(s, ca, 0)) { |
| 3393 | if (check_flags) { |
| 3394 | rv &= ~CERT_PKEY_CA_PARAM; |
| 3395 | break; |
| 3396 | } else |
| 3397 | goto end; |
| 3398 | } |
| 3399 | } |
| 3400 | } |
| 3401 | if (!s->server && strict_mode) { |
| 3402 | STACK_OF(X509_NAME) *ca_dn; |
| 3403 | int check_type = 0; |
Dr. Stephen Henson | 3aeb934 | 2016-01-19 00:21:12 +0000 | [diff] [blame] | 3404 | switch (EVP_PKEY_id(pk)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3405 | case EVP_PKEY_RSA: |
| 3406 | check_type = TLS_CT_RSA_SIGN; |
| 3407 | break; |
| 3408 | case EVP_PKEY_DSA: |
| 3409 | check_type = TLS_CT_DSS_SIGN; |
| 3410 | break; |
| 3411 | case EVP_PKEY_EC: |
| 3412 | check_type = TLS_CT_ECDSA_SIGN; |
| 3413 | break; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3414 | } |
| 3415 | if (check_type) { |
| 3416 | const unsigned char *ctypes; |
| 3417 | int ctypelen; |
| 3418 | if (c->ctypes) { |
| 3419 | ctypes = c->ctypes; |
| 3420 | ctypelen = (int)c->ctype_num; |
| 3421 | } else { |
| 3422 | ctypes = (unsigned char *)s->s3->tmp.ctype; |
| 3423 | ctypelen = s->s3->tmp.ctype_num; |
| 3424 | } |
| 3425 | for (i = 0; i < ctypelen; i++) { |
| 3426 | if (ctypes[i] == check_type) { |
| 3427 | rv |= CERT_PKEY_CERT_TYPE; |
| 3428 | break; |
| 3429 | } |
| 3430 | } |
| 3431 | if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags) |
| 3432 | goto end; |
| 3433 | } else |
| 3434 | rv |= CERT_PKEY_CERT_TYPE; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 3435 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3436 | ca_dn = s->s3->tmp.ca_names; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 3437 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3438 | if (!sk_X509_NAME_num(ca_dn)) |
| 3439 | rv |= CERT_PKEY_ISSUER_NAME; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 3440 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3441 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { |
| 3442 | if (ssl_check_ca_name(ca_dn, x)) |
| 3443 | rv |= CERT_PKEY_ISSUER_NAME; |
| 3444 | } |
| 3445 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { |
| 3446 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 3447 | X509 *xtmp = sk_X509_value(chain, i); |
| 3448 | if (ssl_check_ca_name(ca_dn, xtmp)) { |
| 3449 | rv |= CERT_PKEY_ISSUER_NAME; |
| 3450 | break; |
| 3451 | } |
| 3452 | } |
| 3453 | } |
| 3454 | if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME)) |
| 3455 | goto end; |
| 3456 | } else |
| 3457 | rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 3458 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3459 | if (!check_flags || (rv & check_flags) == check_flags) |
| 3460 | rv |= CERT_PKEY_VALID; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 3461 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3462 | end: |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 3463 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3464 | if (TLS1_get_version(s) >= TLS1_2_VERSION) { |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 3465 | if (*pvalid & CERT_PKEY_EXPLICIT_SIGN) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3466 | rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
Dr. Stephen Henson | d376e57 | 2015-05-12 18:56:39 +0100 | [diff] [blame] | 3467 | else if (s->s3->tmp.md[idx] != NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3468 | rv |= CERT_PKEY_SIGN; |
| 3469 | } else |
| 3470 | rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN; |
Dr. Stephen Henson | 6dbb621 | 2012-07-27 13:39:23 +0000 | [diff] [blame] | 3471 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3472 | /* |
| 3473 | * When checking a CERT_PKEY structure all flags are irrelevant if the |
| 3474 | * chain is invalid. |
| 3475 | */ |
| 3476 | if (!check_flags) { |
| 3477 | if (rv & CERT_PKEY_VALID) |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 3478 | *pvalid = rv; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3479 | else { |
| 3480 | /* Preserve explicit sign flag, clear rest */ |
Dr. Stephen Henson | 6383d31 | 2015-05-12 22:17:34 +0100 | [diff] [blame] | 3481 | *pvalid &= CERT_PKEY_EXPLICIT_SIGN; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3482 | return 0; |
| 3483 | } |
| 3484 | } |
| 3485 | return rv; |
| 3486 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 3487 | |
| 3488 | /* Set validity of certificates in an SSL structure */ |
| 3489 | void tls1_set_cert_validity(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3490 | { |
Matt Caswell | 17dd65e | 2015-03-24 15:10:15 +0000 | [diff] [blame] | 3491 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC); |
| 3492 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN); |
| 3493 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN); |
Matt Caswell | 17dd65e | 2015-03-24 15:10:15 +0000 | [diff] [blame] | 3494 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC); |
Dmitry Belyavsky | e44380a | 2015-11-17 15:32:30 +0000 | [diff] [blame] | 3495 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01); |
| 3496 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256); |
| 3497 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
Dr. Stephen Henson | 18d7158 | 2012-06-29 14:24:42 +0000 | [diff] [blame] | 3500 | /* User level utiity function to check a chain is suitable */ |
| 3501 | 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] | 3502 | { |
| 3503 | return tls1_check_chain(s, x, pk, chain, -1); |
| 3504 | } |
Dr. Stephen Henson | d61ff83 | 2012-06-28 12:45:49 +0000 | [diff] [blame] | 3505 | |
Dr. Stephen Henson | 09599b5 | 2014-01-22 16:22:48 +0000 | [diff] [blame] | 3506 | #ifndef OPENSSL_NO_DH |
| 3507 | DH *ssl_get_auto_dh(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3508 | { |
| 3509 | int dh_secbits = 80; |
| 3510 | if (s->cert->dh_tmp_auto == 2) |
| 3511 | return DH_get_1024_160(); |
Dr. Stephen Henson | adc5506 | 2015-06-28 17:01:52 +0100 | [diff] [blame] | 3512 | if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3513 | if (s->s3->tmp.new_cipher->strength_bits == 256) |
| 3514 | dh_secbits = 128; |
| 3515 | else |
| 3516 | dh_secbits = 80; |
| 3517 | } else { |
| 3518 | CERT_PKEY *cpk = ssl_get_server_send_pkey(s); |
| 3519 | dh_secbits = EVP_PKEY_security_bits(cpk->privatekey); |
| 3520 | } |
Dr. Stephen Henson | 09599b5 | 2014-01-22 16:22:48 +0000 | [diff] [blame] | 3521 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3522 | if (dh_secbits >= 128) { |
| 3523 | DH *dhp = DH_new(); |
Matt Caswell | 0aeddcf | 2016-04-06 17:49:48 +0100 | [diff] [blame] | 3524 | BIGNUM *p, *g; |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 3525 | if (dhp == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3526 | return NULL; |
Matt Caswell | 0aeddcf | 2016-04-06 17:49:48 +0100 | [diff] [blame] | 3527 | g = BN_new(); |
| 3528 | if (g != NULL) |
| 3529 | BN_set_word(g, 2); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3530 | if (dh_secbits >= 192) |
Rich Salz | 9021a5d | 2016-04-18 07:43:54 -0400 | [diff] [blame] | 3531 | p = BN_get_rfc3526_prime_8192(NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3532 | else |
Rich Salz | 9021a5d | 2016-04-18 07:43:54 -0400 | [diff] [blame] | 3533 | p = BN_get_rfc3526_prime_3072(NULL); |
Matt Caswell | 0aeddcf | 2016-04-06 17:49:48 +0100 | [diff] [blame] | 3534 | if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3535 | DH_free(dhp); |
Matt Caswell | 0aeddcf | 2016-04-06 17:49:48 +0100 | [diff] [blame] | 3536 | BN_free(p); |
| 3537 | BN_free(g); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3538 | return NULL; |
| 3539 | } |
| 3540 | return dhp; |
| 3541 | } |
| 3542 | if (dh_secbits >= 112) |
| 3543 | return DH_get_2048_224(); |
| 3544 | return DH_get_1024_160(); |
| 3545 | } |
Dr. Stephen Henson | 09599b5 | 2014-01-22 16:22:48 +0000 | [diff] [blame] | 3546 | #endif |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 3547 | |
| 3548 | 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] | 3549 | { |
Dr. Stephen Henson | 72245f3 | 2015-12-30 13:34:53 +0000 | [diff] [blame] | 3550 | int secbits = -1; |
Dr. Stephen Henson | 8382fd3 | 2015-12-20 00:32:36 +0000 | [diff] [blame] | 3551 | EVP_PKEY *pkey = X509_get0_pubkey(x); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3552 | if (pkey) { |
Dr. Stephen Henson | 72245f3 | 2015-12-30 13:34:53 +0000 | [diff] [blame] | 3553 | /* |
| 3554 | * If no parameters this will return -1 and fail using the default |
| 3555 | * security callback for any non-zero security level. This will |
| 3556 | * reject keys which omit parameters but this only affects DSA and |
| 3557 | * omission of parameters is never (?) done in practice. |
| 3558 | */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3559 | secbits = EVP_PKEY_security_bits(pkey); |
Dr. Stephen Henson | 72245f3 | 2015-12-30 13:34:53 +0000 | [diff] [blame] | 3560 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3561 | if (s) |
| 3562 | return ssl_security(s, op, secbits, 0, x); |
| 3563 | else |
| 3564 | return ssl_ctx_security(ctx, op, secbits, 0, x); |
| 3565 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 3566 | |
| 3567 | 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] | 3568 | { |
| 3569 | /* Lookup signature algorithm digest */ |
| 3570 | int secbits = -1, md_nid = NID_undef, sig_nid; |
Dr. Stephen Henson | 221c7b5 | 2016-02-11 15:25:11 +0000 | [diff] [blame] | 3571 | /* Don't check signature if self signed */ |
| 3572 | if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0) |
| 3573 | return 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3574 | sig_nid = X509_get_signature_nid(x); |
| 3575 | if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) { |
| 3576 | const EVP_MD *md; |
| 3577 | if (md_nid && (md = EVP_get_digestbynid(md_nid))) |
| 3578 | secbits = EVP_MD_size(md) * 4; |
| 3579 | } |
| 3580 | if (s) |
| 3581 | return ssl_security(s, op, secbits, md_nid, x); |
| 3582 | else |
| 3583 | return ssl_ctx_security(ctx, op, secbits, md_nid, x); |
| 3584 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 3585 | |
| 3586 | 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] | 3587 | { |
| 3588 | if (vfy) |
| 3589 | vfy = SSL_SECOP_PEER; |
| 3590 | if (is_ee) { |
| 3591 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy)) |
| 3592 | return SSL_R_EE_KEY_TOO_SMALL; |
| 3593 | } else { |
| 3594 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy)) |
| 3595 | return SSL_R_CA_KEY_TOO_SMALL; |
| 3596 | } |
| 3597 | if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy)) |
| 3598 | return SSL_R_CA_MD_TOO_WEAK; |
| 3599 | return 1; |
| 3600 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 3601 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3602 | /* |
| 3603 | * Check security of a chain, if sk includes the end entity certificate then |
| 3604 | * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending |
| 3605 | * 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] | 3606 | */ |
| 3607 | |
| 3608 | 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] | 3609 | { |
| 3610 | int rv, start_idx, i; |
| 3611 | if (x == NULL) { |
| 3612 | x = sk_X509_value(sk, 0); |
| 3613 | start_idx = 1; |
| 3614 | } else |
| 3615 | start_idx = 0; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 3616 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3617 | rv = ssl_security_cert(s, NULL, x, vfy, 1); |
| 3618 | if (rv != 1) |
| 3619 | return rv; |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 3620 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3621 | for (i = start_idx; i < sk_X509_num(sk); i++) { |
| 3622 | x = sk_X509_value(sk, i); |
| 3623 | rv = ssl_security_cert(s, NULL, x, vfy, 0); |
| 3624 | if (rv != 1) |
| 3625 | return rv; |
| 3626 | } |
| 3627 | return 1; |
| 3628 | } |