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