Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). */ |
| 108 | |
| 109 | #include <stdio.h> |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 110 | #include <stdlib.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 111 | #include <assert.h> |
| 112 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 113 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 114 | #include <openssl/evp.h> |
| 115 | #include <openssl/hmac.h> |
| 116 | #include <openssl/mem.h> |
| 117 | #include <openssl/obj.h> |
| 118 | #include <openssl/rand.h> |
| 119 | |
| 120 | #include "ssl_locl.h" |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 121 | |
| 122 | |
| 123 | static int tls_decrypt_ticket(SSL *s, const uint8_t *tick, int ticklen, |
| 124 | const uint8_t *sess_id, int sesslen, |
| 125 | SSL_SESSION **psess); |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 126 | static int ssl_check_clienthello_tlsext(SSL *s); |
| 127 | static int ssl_check_serverhello_tlsext(SSL *s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | |
David Benjamin | 338fcaf | 2014-12-11 01:20:52 -0500 | [diff] [blame] | 129 | const SSL3_ENC_METHOD TLSv1_enc_data = { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 130 | tls1_enc, |
| 131 | tls1_mac, |
| 132 | tls1_setup_key_block, |
| 133 | tls1_generate_master_secret, |
| 134 | tls1_change_cipher_state, |
| 135 | tls1_final_finish_mac, |
| 136 | TLS1_FINISH_MAC_LENGTH, |
| 137 | tls1_cert_verify_mac, |
| 138 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 139 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 140 | tls1_alert_code, |
| 141 | tls1_export_keying_material, |
| 142 | 0, |
| 143 | SSL3_HM_HEADER_LENGTH, |
| 144 | ssl3_set_handshake_header, |
| 145 | ssl3_handshake_write, |
| 146 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 147 | |
David Benjamin | 338fcaf | 2014-12-11 01:20:52 -0500 | [diff] [blame] | 148 | const SSL3_ENC_METHOD TLSv1_1_enc_data = { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 149 | tls1_enc, |
| 150 | tls1_mac, |
| 151 | tls1_setup_key_block, |
| 152 | tls1_generate_master_secret, |
| 153 | tls1_change_cipher_state, |
| 154 | tls1_final_finish_mac, |
| 155 | TLS1_FINISH_MAC_LENGTH, |
| 156 | tls1_cert_verify_mac, |
| 157 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 158 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 159 | tls1_alert_code, |
| 160 | tls1_export_keying_material, |
| 161 | SSL_ENC_FLAG_EXPLICIT_IV, |
| 162 | SSL3_HM_HEADER_LENGTH, |
| 163 | ssl3_set_handshake_header, |
| 164 | ssl3_handshake_write, |
| 165 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 166 | |
David Benjamin | 338fcaf | 2014-12-11 01:20:52 -0500 | [diff] [blame] | 167 | const SSL3_ENC_METHOD TLSv1_2_enc_data = { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 168 | tls1_enc, |
| 169 | tls1_mac, |
| 170 | tls1_setup_key_block, |
| 171 | tls1_generate_master_secret, |
| 172 | tls1_change_cipher_state, |
| 173 | tls1_final_finish_mac, |
| 174 | TLS1_FINISH_MAC_LENGTH, |
| 175 | tls1_cert_verify_mac, |
| 176 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 177 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 178 | tls1_alert_code, |
| 179 | tls1_export_keying_material, |
| 180 | SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF |
| 181 | |SSL_ENC_FLAG_TLS1_2_CIPHERS, |
| 182 | SSL3_HM_HEADER_LENGTH, |
| 183 | ssl3_set_handshake_header, |
| 184 | ssl3_handshake_write, |
| 185 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 186 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 187 | static int compare_uint16_t(const void *p1, const void *p2) { |
| 188 | uint16_t u1 = *((const uint16_t *)p1); |
| 189 | uint16_t u2 = *((const uint16_t *)p2); |
| 190 | if (u1 < u2) { |
| 191 | return -1; |
| 192 | } else if (u1 > u2) { |
| 193 | return 1; |
| 194 | } else { |
| 195 | return 0; |
| 196 | } |
| 197 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 198 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 199 | /* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be |
| 200 | * more than one extension of the same type in a ClientHello or ServerHello. |
| 201 | * This function does an initial scan over the extensions block to filter those |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 202 | * out. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 203 | static int tls1_check_duplicate_extensions(const CBS *cbs) { |
| 204 | CBS extensions = *cbs; |
| 205 | size_t num_extensions = 0, i = 0; |
| 206 | uint16_t *extension_types = NULL; |
| 207 | int ret = 0; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 208 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 209 | /* First pass: count the extensions. */ |
| 210 | while (CBS_len(&extensions) > 0) { |
| 211 | uint16_t type; |
| 212 | CBS extension; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 213 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 214 | if (!CBS_get_u16(&extensions, &type) || |
| 215 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 216 | goto done; |
| 217 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 218 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 219 | num_extensions++; |
| 220 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 221 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 222 | if (num_extensions == 0) { |
| 223 | return 1; |
| 224 | } |
David Benjamin | 9a37359 | 2014-07-25 04:27:53 -0400 | [diff] [blame] | 225 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 226 | extension_types = |
| 227 | (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions); |
| 228 | if (extension_types == NULL) { |
| 229 | OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions, |
| 230 | ERR_R_MALLOC_FAILURE); |
| 231 | goto done; |
| 232 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 233 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 234 | /* Second pass: gather the extension types. */ |
| 235 | extensions = *cbs; |
| 236 | for (i = 0; i < num_extensions; i++) { |
| 237 | CBS extension; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 238 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 239 | if (!CBS_get_u16(&extensions, &extension_types[i]) || |
| 240 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 241 | /* This should not happen. */ |
| 242 | goto done; |
| 243 | } |
| 244 | } |
| 245 | assert(CBS_len(&extensions) == 0); |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 246 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 247 | /* Sort the extensions and make sure there are no duplicates. */ |
| 248 | qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t); |
| 249 | for (i = 1; i < num_extensions; i++) { |
| 250 | if (extension_types[i - 1] == extension_types[i]) { |
| 251 | goto done; |
| 252 | } |
| 253 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 254 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 255 | ret = 1; |
| 256 | |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 257 | done: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 258 | if (extension_types) |
| 259 | OPENSSL_free(extension_types); |
| 260 | return ret; |
| 261 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 262 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 263 | char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) { |
| 264 | CBS client_hello, session_id, cipher_suites, compression_methods, extensions; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 265 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 266 | CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 267 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 268 | if (/* Skip client version. */ |
| 269 | !CBS_skip(&client_hello, 2) || |
| 270 | /* Skip client nonce. */ |
| 271 | !CBS_skip(&client_hello, 32) || |
| 272 | /* Extract session_id. */ |
| 273 | !CBS_get_u8_length_prefixed(&client_hello, &session_id)) { |
| 274 | return 0; |
| 275 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 276 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 277 | ctx->session_id = CBS_data(&session_id); |
| 278 | ctx->session_id_len = CBS_len(&session_id); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 279 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 280 | /* Skip past DTLS cookie */ |
| 281 | if (SSL_IS_DTLS(ctx->ssl)) { |
| 282 | CBS cookie; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 283 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 284 | if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) { |
| 285 | return 0; |
| 286 | } |
| 287 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 288 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 289 | /* Extract cipher_suites. */ |
| 290 | if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) || |
| 291 | CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) { |
| 292 | return 0; |
| 293 | } |
| 294 | ctx->cipher_suites = CBS_data(&cipher_suites); |
| 295 | ctx->cipher_suites_len = CBS_len(&cipher_suites); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 296 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 297 | /* Extract compression_methods. */ |
| 298 | if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) || |
| 299 | CBS_len(&compression_methods) < 1) { |
| 300 | return 0; |
| 301 | } |
| 302 | ctx->compression_methods = CBS_data(&compression_methods); |
| 303 | ctx->compression_methods_len = CBS_len(&compression_methods); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 304 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 305 | /* If the ClientHello ends here then it's valid, but doesn't have any |
| 306 | * extensions. (E.g. SSLv3.) */ |
| 307 | if (CBS_len(&client_hello) == 0) { |
| 308 | ctx->extensions = NULL; |
| 309 | ctx->extensions_len = 0; |
| 310 | return 1; |
| 311 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 312 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 313 | /* Extract extensions and check it is valid. */ |
| 314 | if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) || |
| 315 | !tls1_check_duplicate_extensions(&extensions) || |
| 316 | CBS_len(&client_hello) != 0) { |
| 317 | return 0; |
| 318 | } |
| 319 | ctx->extensions = CBS_data(&extensions); |
| 320 | ctx->extensions_len = CBS_len(&extensions); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 321 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 322 | return 1; |
| 323 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 324 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 325 | char SSL_early_callback_ctx_extension_get( |
| 326 | const struct ssl_early_callback_ctx *ctx, uint16_t extension_type, |
| 327 | const uint8_t **out_data, size_t *out_len) { |
| 328 | CBS extensions; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 329 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 330 | CBS_init(&extensions, ctx->extensions, ctx->extensions_len); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 331 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 332 | while (CBS_len(&extensions) != 0) { |
| 333 | uint16_t type; |
| 334 | CBS extension; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 335 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 336 | /* Decode the next extension. */ |
| 337 | if (!CBS_get_u16(&extensions, &type) || |
| 338 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 339 | return 0; |
| 340 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 341 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 342 | if (type == extension_type) { |
| 343 | *out_data = CBS_data(&extension); |
| 344 | *out_len = CBS_len(&extension); |
| 345 | return 1; |
| 346 | } |
| 347 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 348 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 349 | return 0; |
| 350 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 351 | |
David Benjamin | e518f65 | 2014-10-13 16:12:45 -0400 | [diff] [blame] | 352 | /* ECC curves from RFC4492 */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 353 | static const int nid_list[] = { |
| 354 | NID_sect163k1, /* sect163k1 (1) */ |
| 355 | NID_sect163r1, /* sect163r1 (2) */ |
| 356 | NID_sect163r2, /* sect163r2 (3) */ |
| 357 | NID_sect193r1, /* sect193r1 (4) */ |
| 358 | NID_sect193r2, /* sect193r2 (5) */ |
| 359 | NID_sect233k1, /* sect233k1 (6) */ |
| 360 | NID_sect233r1, /* sect233r1 (7) */ |
| 361 | NID_sect239k1, /* sect239k1 (8) */ |
| 362 | NID_sect283k1, /* sect283k1 (9) */ |
| 363 | NID_sect283r1, /* sect283r1 (10) */ |
| 364 | NID_sect409k1, /* sect409k1 (11) */ |
| 365 | NID_sect409r1, /* sect409r1 (12) */ |
| 366 | NID_sect571k1, /* sect571k1 (13) */ |
| 367 | NID_sect571r1, /* sect571r1 (14) */ |
| 368 | NID_secp160k1, /* secp160k1 (15) */ |
| 369 | NID_secp160r1, /* secp160r1 (16) */ |
| 370 | NID_secp160r2, /* secp160r2 (17) */ |
| 371 | NID_secp192k1, /* secp192k1 (18) */ |
| 372 | NID_X9_62_prime192v1, /* secp192r1 (19) */ |
| 373 | NID_secp224k1, /* secp224k1 (20) */ |
| 374 | NID_secp224r1, /* secp224r1 (21) */ |
| 375 | NID_secp256k1, /* secp256k1 (22) */ |
| 376 | NID_X9_62_prime256v1, /* secp256r1 (23) */ |
| 377 | NID_secp384r1, /* secp384r1 (24) */ |
| 378 | NID_secp521r1, /* secp521r1 (25) */ |
| 379 | NID_brainpoolP256r1, /* brainpoolP256r1 (26) */ |
| 380 | NID_brainpoolP384r1, /* brainpoolP384r1 (27) */ |
| 381 | NID_brainpoolP512r1 /* brainpool512r1 (28) */ |
| 382 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 383 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 384 | static const uint8_t ecformats_default[] = { |
| 385 | TLSEXT_ECPOINTFORMAT_uncompressed, |
| 386 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 387 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 388 | static const uint16_t eccurves_default[] = { |
| 389 | 23, /* secp256r1 (23) */ |
| 390 | 24, /* secp384r1 (24) */ |
| 391 | 25, /* secp521r1 (25) */ |
| 392 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 393 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 394 | int tls1_ec_curve_id2nid(uint16_t curve_id) { |
| 395 | if (curve_id < 1 || curve_id > sizeof(nid_list) / sizeof(nid_list[0])) { |
| 396 | return OBJ_undef; |
| 397 | } |
| 398 | return nid_list[curve_id - 1]; |
| 399 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 400 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 401 | uint16_t tls1_ec_nid2curve_id(int nid) { |
| 402 | size_t i; |
| 403 | for (i = 0; i < sizeof(nid_list) / sizeof(nid_list[0]); i++) { |
| 404 | /* nid_list[i] stores the NID corresponding to curve ID i+1. */ |
| 405 | if (nid == nid_list[i]) { |
| 406 | return i + 1; |
| 407 | } |
| 408 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 409 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 410 | /* Use 0 for non-existent curve ID. Note: this assumes that curve ID 0 will |
| 411 | * never be allocated. */ |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the |
| 416 | * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the |
| 417 | * peer's curve list. Otherwise, return the preferred list. */ |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 418 | static void tls1_get_curvelist(SSL *s, int get_peer_curves, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 419 | const uint16_t **out_curve_ids, |
| 420 | size_t *out_curve_ids_len) { |
| 421 | if (get_peer_curves) { |
| 422 | *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist; |
| 423 | *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length; |
| 424 | return; |
| 425 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 426 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 427 | *out_curve_ids = s->tlsext_ellipticcurvelist; |
| 428 | *out_curve_ids_len = s->tlsext_ellipticcurvelist_length; |
| 429 | if (!*out_curve_ids) { |
| 430 | *out_curve_ids = eccurves_default; |
| 431 | *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]); |
| 432 | } |
| 433 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 434 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 435 | int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) { |
| 436 | uint8_t curve_type; |
| 437 | uint16_t curve_id; |
| 438 | const uint16_t *curves; |
| 439 | size_t curves_len, i; |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 440 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 441 | /* Only support named curves. */ |
| 442 | if (!CBS_get_u8(cbs, &curve_type) || |
| 443 | curve_type != NAMED_CURVE_TYPE || |
| 444 | !CBS_get_u16(cbs, &curve_id)) { |
| 445 | return 0; |
| 446 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 447 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 448 | tls1_get_curvelist(s, 0, &curves, &curves_len); |
| 449 | for (i = 0; i < curves_len; i++) { |
| 450 | if (curve_id == curves[i]) { |
| 451 | *out_curve_id = curve_id; |
| 452 | return 1; |
| 453 | } |
| 454 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 455 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 456 | return 0; |
| 457 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 458 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 459 | int tls1_get_shared_curve(SSL *s) { |
| 460 | const uint16_t *pref, *supp; |
| 461 | size_t preflen, supplen, i, j; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 462 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 463 | /* Can't do anything on client side */ |
| 464 | if (s->server == 0) { |
| 465 | return NID_undef; |
| 466 | } |
| 467 | |
| 468 | /* Return first preference shared curve */ |
| 469 | tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &supp, |
| 470 | &supplen); |
| 471 | tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref, |
| 472 | &preflen); |
| 473 | |
| 474 | for (i = 0; i < preflen; i++) { |
| 475 | for (j = 0; j < supplen; j++) { |
| 476 | if (pref[i] == supp[j]) { |
| 477 | return tls1_ec_curve_id2nid(pref[i]); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | return NID_undef; |
| 483 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 484 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 485 | /* NOTE: tls1_ec_curve_id2nid and tls1_set_curves assume that |
| 486 | * |
| 487 | * (a) 0 is not a valid curve ID. |
| 488 | * |
| 489 | * (b) The largest curve ID is 31. |
| 490 | * |
| 491 | * Those implementations must be revised before adding support for curve IDs |
| 492 | * that break these assumptions. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 493 | OPENSSL_COMPILE_ASSERT((sizeof(nid_list) / sizeof(nid_list[0])) < 32, |
| 494 | small_curve_ids); |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 495 | |
| 496 | int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 497 | const int *curves, size_t ncurves) { |
| 498 | uint16_t *curve_ids; |
| 499 | size_t i; |
| 500 | |
| 501 | /* Bitmap of curves included to detect duplicates: only works |
| 502 | * while curve ids < 32. */ |
| 503 | uint32_t dup_list = 0; |
| 504 | curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t)); |
| 505 | if (curve_ids == NULL) { |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | for (i = 0; i < ncurves; i++) { |
| 510 | uint32_t idmask; |
| 511 | uint16_t id; |
| 512 | id = tls1_ec_nid2curve_id(curves[i]); |
| 513 | idmask = ((uint32_t)1) << id; |
| 514 | if (!id || (dup_list & idmask)) { |
| 515 | OPENSSL_free(curve_ids); |
| 516 | return 0; |
| 517 | } |
| 518 | dup_list |= idmask; |
| 519 | curve_ids[i] = id; |
| 520 | } |
| 521 | |
| 522 | if (*out_curve_ids) { |
| 523 | OPENSSL_free(*out_curve_ids); |
| 524 | } |
| 525 | *out_curve_ids = curve_ids; |
| 526 | *out_curve_ids_len = ncurves; |
| 527 | |
| 528 | return 1; |
| 529 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 530 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 531 | /* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the |
| 532 | * TLS curve ID and point format, respectively, for |ec|. It returns one on |
| 533 | * success and zero on failure. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 534 | static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id, |
| 535 | uint8_t *out_comp_id, EC_KEY *ec) { |
| 536 | int nid; |
| 537 | uint16_t id; |
| 538 | const EC_GROUP *grp; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 539 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 540 | if (ec == NULL) { |
| 541 | return 0; |
| 542 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 543 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 544 | grp = EC_KEY_get0_group(ec); |
| 545 | if (grp == NULL) { |
| 546 | return 0; |
| 547 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 548 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 549 | /* Determine curve ID */ |
| 550 | nid = EC_GROUP_get_curve_name(grp); |
| 551 | id = tls1_ec_nid2curve_id(nid); |
| 552 | if (!id) { |
| 553 | return 0; |
| 554 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 555 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 556 | /* Set the named curve ID. Arbitrary explicit curves are not supported. */ |
| 557 | *out_curve_id = id; |
| 558 | |
| 559 | if (out_comp_id) { |
| 560 | if (EC_KEY_get0_public_key(ec) == NULL) { |
| 561 | return 0; |
| 562 | } |
| 563 | if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) { |
| 564 | *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
| 565 | } else { |
| 566 | *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | return 1; |
| 571 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 572 | |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 573 | /* tls1_check_point_format returns one if |comp_id| is consistent with the |
| 574 | * peer's point format preferences. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 575 | static int tls1_check_point_format(SSL *s, uint8_t comp_id) { |
| 576 | uint8_t *p = s->s3->tmp.peer_ecpointformatlist; |
| 577 | size_t plen = s->s3->tmp.peer_ecpointformatlist_length; |
| 578 | size_t i; |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 579 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 580 | /* If point formats extension present check it, otherwise everything is |
| 581 | * supported (see RFC4492). */ |
| 582 | if (p == NULL) { |
| 583 | return 1; |
| 584 | } |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 585 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 586 | for (i = 0; i < plen; i++) { |
| 587 | if (comp_id == p[i]) { |
| 588 | return 1; |
| 589 | } |
| 590 | } |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 591 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | /* tls1_check_curve_id returns one if |curve_id| is consistent with both our |
| 596 | * and the peer's curve preferences. Note: if called as the client, only our |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 597 | * preferences are checked; the peer (the server) does not send preferences. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 598 | static int tls1_check_curve_id(SSL *s, uint16_t curve_id) { |
| 599 | const uint16_t *curves; |
| 600 | size_t curves_len, i, j; |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 601 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 602 | /* Check against our list, then the peer's list. */ |
| 603 | for (j = 0; j <= 1; j++) { |
| 604 | tls1_get_curvelist(s, j, &curves, &curves_len); |
| 605 | for (i = 0; i < curves_len; i++) { |
| 606 | if (curves[i] == curve_id) { |
| 607 | break; |
| 608 | } |
| 609 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 610 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 611 | if (i == curves_len) { |
| 612 | return 0; |
| 613 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 614 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 615 | /* Servers do not present a preference list so, if we are a client, only |
| 616 | * check our list. */ |
| 617 | if (!s->server) { |
| 618 | return 1; |
| 619 | } |
| 620 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 621 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 622 | return 1; |
| 623 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 624 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 625 | static void tls1_get_formatlist(SSL *s, const uint8_t **pformats, |
| 626 | size_t *pformatslen) { |
| 627 | /* If we have a custom point format list use it otherwise use default */ |
| 628 | if (s->tlsext_ecpointformatlist) { |
| 629 | *pformats = s->tlsext_ecpointformatlist; |
| 630 | *pformatslen = s->tlsext_ecpointformatlist_length; |
| 631 | } else { |
| 632 | *pformats = ecformats_default; |
| 633 | *pformatslen = sizeof(ecformats_default); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | int tls1_check_ec_cert(SSL *s, X509 *x) { |
| 638 | int ret = 0; |
| 639 | EVP_PKEY *pkey = X509_get_pubkey(x); |
| 640 | uint16_t curve_id; |
| 641 | uint8_t comp_id; |
| 642 | |
| 643 | if (!pkey || |
| 644 | pkey->type != EVP_PKEY_EC || |
| 645 | !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) || |
| 646 | !tls1_check_curve_id(s, curve_id) || |
| 647 | !tls1_check_point_format(s, comp_id)) { |
| 648 | goto done; |
| 649 | } |
| 650 | |
| 651 | ret = 1; |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 652 | |
| 653 | done: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 654 | if (pkey) { |
| 655 | EVP_PKEY_free(pkey); |
| 656 | } |
| 657 | return ret; |
| 658 | } |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 659 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 660 | int tls1_check_ec_tmp_key(SSL *s) { |
| 661 | uint16_t curve_id; |
| 662 | EC_KEY *ec = s->cert->ecdh_tmp; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 663 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 664 | if (s->cert->ecdh_tmp_auto) { |
| 665 | /* Need a shared curve */ |
| 666 | return tls1_get_shared_curve(s) != NID_undef; |
| 667 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 668 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 669 | if (!ec) { |
| 670 | if (s->cert->ecdh_tmp_cb) { |
| 671 | return 1; |
| 672 | } |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | return tls1_curve_params_from_ec_key(&curve_id, NULL, ec) && |
| 677 | tls1_check_curve_id(s, curve_id); |
| 678 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 679 | |
| 680 | /* List of supported signature algorithms and hashes. Should make this |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 681 | * customisable at some point, for now include everything we support. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 682 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 683 | #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 684 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 685 | #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 686 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 687 | #define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 688 | |
David Benjamin | cff6472 | 2014-08-19 19:54:46 -0400 | [diff] [blame] | 689 | static const uint8_t tls12_sigalgs[] = { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 690 | tlsext_sigalg(TLSEXT_hash_sha512) |
| 691 | tlsext_sigalg(TLSEXT_hash_sha384) |
| 692 | tlsext_sigalg(TLSEXT_hash_sha256) |
| 693 | tlsext_sigalg(TLSEXT_hash_sha224) |
| 694 | tlsext_sigalg(TLSEXT_hash_sha1) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 695 | }; |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 696 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 697 | size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) { |
| 698 | /* If server use client authentication sigalgs if not NULL */ |
| 699 | if (s->server && s->cert->client_sigalgs) { |
| 700 | *psigs = s->cert->client_sigalgs; |
| 701 | return s->cert->client_sigalgslen; |
| 702 | } else if (s->cert->conf_sigalgs) { |
| 703 | *psigs = s->cert->conf_sigalgs; |
| 704 | return s->cert->conf_sigalgslen; |
| 705 | } else { |
| 706 | *psigs = tls12_sigalgs; |
| 707 | return sizeof(tls12_sigalgs); |
| 708 | } |
| 709 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 710 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 711 | /* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It |
| 712 | * checks it is consistent with |s|'s sent supported signature algorithms and, |
| 713 | * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it |
| 714 | * returns 0 and writes an alert into |*out_alert|. */ |
| 715 | int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s, |
| 716 | CBS *cbs, EVP_PKEY *pkey) { |
| 717 | const uint8_t *sent_sigs; |
| 718 | size_t sent_sigslen, i; |
| 719 | int sigalg = tls12_get_sigid(pkey); |
| 720 | uint8_t hash, signature; |
| 721 | |
| 722 | /* Should never happen */ |
| 723 | if (sigalg == -1) { |
| 724 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR); |
| 725 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | if (!CBS_get_u8(cbs, &hash) || |
| 730 | !CBS_get_u8(cbs, &signature)) { |
| 731 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR); |
| 732 | *out_alert = SSL_AD_DECODE_ERROR; |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | /* Check key type is consistent with signature */ |
| 737 | if (sigalg != signature) { |
| 738 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE); |
| 739 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | if (pkey->type == EVP_PKEY_EC) { |
| 744 | uint16_t curve_id; |
| 745 | uint8_t comp_id; |
| 746 | /* Check compression and curve matches extensions */ |
| 747 | if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) { |
| 748 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | if (s->server && (!tls1_check_curve_id(s, curve_id) || |
| 753 | !tls1_check_point_format(s, comp_id))) { |
| 754 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE); |
| 755 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 756 | return 0; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /* Check signature matches a type we sent */ |
| 761 | sent_sigslen = tls12_get_psigalgs(s, &sent_sigs); |
| 762 | for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) { |
| 763 | if (hash == sent_sigs[0] && signature == sent_sigs[1]) { |
| 764 | break; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | /* Allow fallback to SHA-1. */ |
| 769 | if (i == sent_sigslen && hash != TLSEXT_hash_sha1) { |
| 770 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE); |
| 771 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | *out_md = tls12_get_hash(hash); |
| 776 | if (*out_md == NULL) { |
| 777 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST); |
| 778 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | return 1; |
| 783 | } |
| 784 | |
| 785 | /* Get a mask of disabled algorithms: an algorithm is disabled if it isn't |
| 786 | * supported or doesn't appear in supported signature algorithms. Unlike |
| 787 | * ssl_cipher_get_disabled this applies to a specific session and not global |
| 788 | * settings. */ |
| 789 | void ssl_set_client_disabled(SSL *s) { |
| 790 | CERT *c = s->cert; |
| 791 | const uint8_t *sigalgs; |
| 792 | size_t i, sigalgslen; |
| 793 | int have_rsa = 0, have_ecdsa = 0; |
| 794 | c->mask_a = 0; |
| 795 | c->mask_k = 0; |
| 796 | |
| 797 | /* Don't allow TLS 1.2 only ciphers if we don't suppport them */ |
| 798 | if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) { |
| 799 | c->mask_ssl = SSL_TLSV1_2; |
| 800 | } else { |
| 801 | c->mask_ssl = 0; |
| 802 | } |
| 803 | |
| 804 | /* Now go through all signature algorithms seeing if we support any for RSA, |
| 805 | * DSA, ECDSA. Do this for all versions not just TLS 1.2. */ |
| 806 | sigalgslen = tls12_get_psigalgs(s, &sigalgs); |
| 807 | for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) { |
| 808 | switch (sigalgs[1]) { |
| 809 | case TLSEXT_signature_rsa: |
| 810 | have_rsa = 1; |
| 811 | break; |
| 812 | |
| 813 | case TLSEXT_signature_ecdsa: |
| 814 | have_ecdsa = 1; |
| 815 | break; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | /* Disable auth if we don't include any appropriate signature algorithms. */ |
| 820 | if (!have_rsa) { |
| 821 | c->mask_a |= SSL_aRSA; |
| 822 | } |
| 823 | if (!have_ecdsa) { |
| 824 | c->mask_a |= SSL_aECDSA; |
| 825 | } |
| 826 | |
| 827 | /* with PSK there must be client callback set */ |
| 828 | if (!s->psk_client_callback) { |
| 829 | c->mask_a |= SSL_aPSK; |
| 830 | c->mask_k |= SSL_kPSK; |
| 831 | } |
| 832 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 833 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 834 | /* header_len is the length of the ClientHello header written so far, used to |
| 835 | * compute padding. It does not include the record header. Pass 0 if no padding |
| 836 | * is to be done. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 837 | uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit, |
| 838 | size_t header_len) { |
| 839 | int extdatalen = 0; |
| 840 | uint8_t *ret = buf; |
| 841 | uint8_t *orig = buf; |
| 842 | /* See if we support any ECC ciphersuites */ |
| 843 | int using_ecc = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 844 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 845 | if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) { |
| 846 | size_t i; |
| 847 | unsigned long alg_k, alg_a; |
| 848 | STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 849 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 850 | for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) { |
| 851 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 852 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 853 | alg_k = c->algorithm_mkey; |
| 854 | alg_a = c->algorithm_auth; |
| 855 | if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) { |
| 856 | using_ecc = 1; |
| 857 | break; |
| 858 | } |
| 859 | } |
| 860 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 861 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 862 | /* don't add extensions for SSLv3 unless doing secure renegotiation */ |
| 863 | if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) { |
| 864 | return orig; |
| 865 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 866 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 867 | ret += 2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 868 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 869 | if (ret >= limit) { |
| 870 | return NULL; /* should never occur. */ |
| 871 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 872 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 873 | if (s->tlsext_hostname != NULL) { |
| 874 | /* Add TLS extension servername to the Client Hello message */ |
| 875 | unsigned long size_str; |
| 876 | long lenmax; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 877 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 878 | /* check for enough space. |
| 879 | 4 for the servername type and entension length |
| 880 | 2 for servernamelist length |
| 881 | 1 for the hostname type |
| 882 | 2 for hostname length |
| 883 | + hostname length */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 884 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 885 | lenmax = limit - ret - 9; |
| 886 | size_str = strlen(s->tlsext_hostname); |
| 887 | if (lenmax < 0 || size_str > (unsigned long)lenmax) { |
| 888 | return NULL; |
| 889 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 890 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 891 | /* extension type and length */ |
| 892 | s2n(TLSEXT_TYPE_server_name, ret); |
| 893 | s2n(size_str + 5, ret); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 894 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 895 | /* length of servername list */ |
| 896 | s2n(size_str + 3, ret); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 897 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 898 | /* hostname type, length and hostname */ |
| 899 | *(ret++) = (uint8_t)TLSEXT_NAMETYPE_host_name; |
| 900 | s2n(size_str, ret); |
| 901 | memcpy(ret, s->tlsext_hostname, size_str); |
| 902 | ret += size_str; |
| 903 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 904 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 905 | /* Add RI if renegotiating */ |
| 906 | if (s->renegotiate) { |
| 907 | int el; |
David Benjamin | d1681e6 | 2014-11-20 14:54:14 -0500 | [diff] [blame] | 908 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 909 | if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) { |
| 910 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
| 911 | return NULL; |
| 912 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 913 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 914 | if ((limit - ret - 4 - el) < 0) { |
| 915 | return NULL; |
| 916 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 917 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 918 | s2n(TLSEXT_TYPE_renegotiate, ret); |
| 919 | s2n(el, ret); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 920 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 921 | if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) { |
| 922 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
| 923 | return NULL; |
| 924 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 925 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 926 | ret += el; |
| 927 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 928 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 929 | /* Add extended master secret. */ |
| 930 | if (s->version != SSL3_VERSION) { |
| 931 | if (limit - ret - 4 < 0) { |
| 932 | return NULL; |
| 933 | } |
| 934 | s2n(TLSEXT_TYPE_extended_master_secret, ret); |
| 935 | s2n(0, ret); |
| 936 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 937 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 938 | if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) { |
| 939 | int ticklen = 0; |
| 940 | if (!s->new_session && s->session && s->session->tlsext_tick) { |
| 941 | ticklen = s->session->tlsext_ticklen; |
| 942 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 943 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 944 | /* Check for enough room 2 for extension type, 2 for len rest for |
| 945 | * ticket. */ |
| 946 | if ((long)(limit - ret - 4 - ticklen) < 0) { |
| 947 | return NULL; |
| 948 | } |
| 949 | s2n(TLSEXT_TYPE_session_ticket, ret); |
| 950 | s2n(ticklen, ret); |
| 951 | if (ticklen) { |
| 952 | memcpy(ret, s->session->tlsext_tick, ticklen); |
| 953 | ret += ticklen; |
| 954 | } |
| 955 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 956 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 957 | if (SSL_USE_SIGALGS(s)) { |
| 958 | size_t salglen; |
| 959 | const uint8_t *salg; |
| 960 | salglen = tls12_get_psigalgs(s, &salg); |
| 961 | if ((size_t)(limit - ret) < salglen + 6) { |
| 962 | return NULL; |
| 963 | } |
| 964 | s2n(TLSEXT_TYPE_signature_algorithms, ret); |
| 965 | s2n(salglen + 2, ret); |
| 966 | s2n(salglen, ret); |
| 967 | memcpy(ret, salg, salglen); |
| 968 | ret += salglen; |
| 969 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 970 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 971 | if (s->ocsp_stapling_enabled) { |
| 972 | /* The status_request extension is excessively extensible at every layer. |
| 973 | * On the client, only support requesting OCSP responses with an empty |
| 974 | * responder_id_list and no extensions. */ |
| 975 | if (limit - ret - 4 - 1 - 2 - 2 < 0) { |
| 976 | return NULL; |
| 977 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 978 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 979 | s2n(TLSEXT_TYPE_status_request, ret); |
| 980 | s2n(1 + 2 + 2, ret); |
| 981 | /* status_type */ |
| 982 | *(ret++) = TLSEXT_STATUSTYPE_ocsp; |
| 983 | /* responder_id_list - empty */ |
| 984 | s2n(0, ret); |
| 985 | /* request_extensions - empty */ |
| 986 | s2n(0, ret); |
| 987 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 988 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 989 | if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) { |
| 990 | /* The client advertises an emtpy extension to indicate its support for |
| 991 | * Next Protocol Negotiation */ |
| 992 | if (limit - ret - 4 < 0) { |
| 993 | return NULL; |
| 994 | } |
| 995 | s2n(TLSEXT_TYPE_next_proto_neg, ret); |
| 996 | s2n(0, ret); |
| 997 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 998 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 999 | if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len) { |
| 1000 | /* The client advertises an empty extension to indicate its support for |
| 1001 | * certificate timestamps. */ |
| 1002 | if (limit - ret - 4 < 0) { |
| 1003 | return NULL; |
| 1004 | } |
| 1005 | s2n(TLSEXT_TYPE_certificate_timestamp, ret); |
| 1006 | s2n(0, ret); |
| 1007 | } |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1008 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1009 | if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) { |
| 1010 | if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) { |
| 1011 | return NULL; |
| 1012 | } |
| 1013 | s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret); |
| 1014 | s2n(2 + s->alpn_client_proto_list_len, ret); |
| 1015 | s2n(s->alpn_client_proto_list_len, ret); |
| 1016 | memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len); |
| 1017 | ret += s->alpn_client_proto_list_len; |
| 1018 | } |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1019 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1020 | if (s->tlsext_channel_id_enabled) { |
| 1021 | /* The client advertises an emtpy extension to indicate its support for |
| 1022 | * Channel ID. */ |
| 1023 | if (limit - ret - 4 < 0) { |
| 1024 | return NULL; |
| 1025 | } |
| 1026 | if (s->ctx->tlsext_channel_id_enabled_new) { |
| 1027 | s2n(TLSEXT_TYPE_channel_id_new, ret); |
| 1028 | } else { |
| 1029 | s2n(TLSEXT_TYPE_channel_id, ret); |
| 1030 | } |
| 1031 | s2n(0, ret); |
| 1032 | } |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1033 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1034 | if (SSL_get_srtp_profiles(s)) { |
| 1035 | int el; |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1036 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1037 | ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0); |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1038 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1039 | if ((limit - ret - 4 - el) < 0) { |
| 1040 | return NULL; |
| 1041 | } |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1042 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1043 | s2n(TLSEXT_TYPE_use_srtp, ret); |
| 1044 | s2n(el, ret); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1045 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1046 | if (!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) { |
| 1047 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1048 | return NULL; |
| 1049 | } |
| 1050 | ret += el; |
| 1051 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1052 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1053 | if (using_ecc) { |
| 1054 | /* Add TLS extension ECPointFormats to the ClientHello message */ |
| 1055 | long lenmax; |
| 1056 | const uint8_t *formats; |
| 1057 | const uint16_t *curves; |
| 1058 | size_t formats_len, curves_len, i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1059 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1060 | tls1_get_formatlist(s, &formats, &formats_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1061 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1062 | lenmax = limit - ret - 5; |
| 1063 | if (lenmax < 0) { |
| 1064 | return NULL; |
| 1065 | } |
| 1066 | if (formats_len > (size_t)lenmax) { |
| 1067 | return NULL; |
| 1068 | } |
| 1069 | if (formats_len > 255) { |
| 1070 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1071 | return NULL; |
| 1072 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1073 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1074 | s2n(TLSEXT_TYPE_ec_point_formats, ret); |
| 1075 | s2n(formats_len + 1, ret); |
| 1076 | *(ret++) = (uint8_t)formats_len; |
| 1077 | memcpy(ret, formats, formats_len); |
| 1078 | ret += formats_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1079 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1080 | /* Add TLS extension EllipticCurves to the ClientHello message */ |
| 1081 | tls1_get_curvelist(s, 0, &curves, &curves_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1082 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1083 | lenmax = limit - ret - 6; |
| 1084 | if (lenmax < 0) { |
| 1085 | return NULL; |
| 1086 | } |
| 1087 | if (curves_len * 2 > (size_t)lenmax) { |
| 1088 | return NULL; |
| 1089 | } |
| 1090 | if (curves_len * 2 > 65532) { |
| 1091 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1092 | return NULL; |
| 1093 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1094 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1095 | s2n(TLSEXT_TYPE_elliptic_curves, ret); |
| 1096 | s2n((curves_len * 2) + 2, ret); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1097 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1098 | s2n(curves_len * 2, ret); |
| 1099 | for (i = 0; i < curves_len; i++) { |
| 1100 | s2n(curves[i], ret); |
| 1101 | } |
| 1102 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1103 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1104 | if (header_len > 0) { |
| 1105 | size_t clienthello_minsize = 0; |
| 1106 | header_len += ret - orig; |
| 1107 | if (header_len > 0xff && header_len < 0x200) { |
| 1108 | /* Add padding to workaround bugs in F5 terminators. See |
| 1109 | * https://tools.ietf.org/html/draft-agl-tls-padding-03 |
| 1110 | * |
| 1111 | * NB: because this code works out the length of all existing extensions |
| 1112 | * it MUST always appear last. */ |
| 1113 | clienthello_minsize = 0x200; |
| 1114 | } |
| 1115 | if (s->fastradio_padding) { |
| 1116 | /* Pad the ClientHello record to 1024 bytes to fast forward the radio |
| 1117 | * into DCH (high data rate) state in 3G networks. Note that when |
| 1118 | * fastradio_padding is enabled, even if the header_len is less than 255 |
| 1119 | * bytes, the padding will be applied regardless. This is slightly |
| 1120 | * different from the TLS padding extension suggested in |
| 1121 | * https://tools.ietf.org/html/draft-agl-tls-padding-03 */ |
| 1122 | clienthello_minsize = 0x400; |
| 1123 | } |
| 1124 | if (header_len < clienthello_minsize) { |
| 1125 | size_t padding_len = clienthello_minsize - header_len; |
| 1126 | /* Extensions take at least four bytes to encode. Always include least |
| 1127 | * one byte of data if including the extension. WebSphere Application |
| 1128 | * Server 7.0 is intolerant to the last extension being zero-length. */ |
| 1129 | if (padding_len >= 4 + 1) { |
| 1130 | padding_len -= 4; |
| 1131 | } else { |
| 1132 | padding_len = 1; |
| 1133 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1134 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1135 | if (limit - ret - 4 - (long)padding_len < 0) { |
| 1136 | return NULL; |
| 1137 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1138 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1139 | s2n(TLSEXT_TYPE_padding, ret); |
| 1140 | s2n(padding_len, ret); |
| 1141 | memset(ret, 0, padding_len); |
| 1142 | ret += padding_len; |
| 1143 | } |
| 1144 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1145 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1146 | extdatalen = ret - orig - 2; |
| 1147 | if (extdatalen == 0) { |
| 1148 | return orig; |
| 1149 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1150 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1151 | s2n(extdatalen, orig); |
| 1152 | return ret; |
| 1153 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1154 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1155 | uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit) { |
| 1156 | int extdatalen = 0; |
| 1157 | uint8_t *orig = buf; |
| 1158 | uint8_t *ret = buf; |
| 1159 | int next_proto_neg_seen; |
| 1160 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 1161 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 1162 | int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA); |
| 1163 | using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1164 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1165 | /* don't add extensions for SSLv3, unless doing secure renegotiation */ |
| 1166 | if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) { |
| 1167 | return orig; |
| 1168 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1169 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1170 | ret += 2; |
| 1171 | if (ret >= limit) { |
| 1172 | return NULL; /* should never happen. */ |
| 1173 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1174 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1175 | if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL) { |
| 1176 | if ((long)(limit - ret - 4) < 0) { |
| 1177 | return NULL; |
| 1178 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1179 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1180 | s2n(TLSEXT_TYPE_server_name, ret); |
| 1181 | s2n(0, ret); |
| 1182 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1183 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1184 | if (s->s3->send_connection_binding) { |
| 1185 | int el; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1186 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1187 | if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) { |
| 1188 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1189 | return NULL; |
| 1190 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1191 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1192 | if ((limit - ret - 4 - el) < 0) { |
| 1193 | return NULL; |
| 1194 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1195 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1196 | s2n(TLSEXT_TYPE_renegotiate, ret); |
| 1197 | s2n(el, ret); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1198 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1199 | if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) { |
| 1200 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1201 | return NULL; |
| 1202 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1203 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1204 | ret += el; |
| 1205 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1206 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1207 | if (s->s3->tmp.extended_master_secret) { |
| 1208 | if ((long)(limit - ret - 4) < 0) { |
| 1209 | return NULL; |
| 1210 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1211 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1212 | s2n(TLSEXT_TYPE_extended_master_secret, ret); |
| 1213 | s2n(0, ret); |
| 1214 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1215 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1216 | if (using_ecc) { |
| 1217 | const uint8_t *plist; |
| 1218 | size_t plistlen; |
| 1219 | /* Add TLS extension ECPointFormats to the ServerHello message */ |
| 1220 | long lenmax; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1221 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1222 | tls1_get_formatlist(s, &plist, &plistlen); |
| 1223 | |
| 1224 | lenmax = limit - ret - 5; |
| 1225 | if (lenmax < 0) { |
| 1226 | return NULL; |
| 1227 | } |
| 1228 | if (plistlen > (size_t)lenmax) { |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | if (plistlen > 255) { |
| 1232 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1233 | return NULL; |
| 1234 | } |
| 1235 | |
| 1236 | s2n(TLSEXT_TYPE_ec_point_formats, ret); |
| 1237 | s2n(plistlen + 1, ret); |
| 1238 | *(ret++) = (uint8_t)plistlen; |
| 1239 | memcpy(ret, plist, plistlen); |
| 1240 | ret += plistlen; |
| 1241 | } |
| 1242 | /* Currently the server should not respond with a SupportedCurves extension */ |
| 1243 | |
| 1244 | if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) { |
| 1245 | if ((long)(limit - ret - 4) < 0) { |
| 1246 | return NULL; |
| 1247 | } |
| 1248 | s2n(TLSEXT_TYPE_session_ticket, ret); |
| 1249 | s2n(0, ret); |
| 1250 | } |
| 1251 | |
| 1252 | if (s->s3->tmp.certificate_status_expected) { |
| 1253 | if ((long)(limit - ret - 4) < 0) { |
| 1254 | return NULL; |
| 1255 | } |
| 1256 | s2n(TLSEXT_TYPE_status_request, ret); |
| 1257 | s2n(0, ret); |
| 1258 | } |
| 1259 | |
| 1260 | if (s->srtp_profile) { |
| 1261 | int el; |
| 1262 | |
| 1263 | ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0); |
| 1264 | |
| 1265 | if ((limit - ret - 4 - el) < 0) { |
| 1266 | return NULL; |
| 1267 | } |
| 1268 | |
| 1269 | s2n(TLSEXT_TYPE_use_srtp, ret); |
| 1270 | s2n(el, ret); |
| 1271 | |
| 1272 | if (!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) { |
| 1273 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1274 | return NULL; |
| 1275 | } |
| 1276 | ret += el; |
| 1277 | } |
| 1278 | |
| 1279 | next_proto_neg_seen = s->s3->next_proto_neg_seen; |
| 1280 | s->s3->next_proto_neg_seen = 0; |
| 1281 | if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) { |
| 1282 | const uint8_t *npa; |
| 1283 | unsigned int npalen; |
| 1284 | int r; |
| 1285 | |
| 1286 | r = s->ctx->next_protos_advertised_cb( |
| 1287 | s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg); |
| 1288 | if (r == SSL_TLSEXT_ERR_OK) { |
| 1289 | if ((long)(limit - ret - 4 - npalen) < 0) { |
| 1290 | return NULL; |
| 1291 | } |
| 1292 | s2n(TLSEXT_TYPE_next_proto_neg, ret); |
| 1293 | s2n(npalen, ret); |
| 1294 | memcpy(ret, npa, npalen); |
| 1295 | ret += npalen; |
| 1296 | s->s3->next_proto_neg_seen = 1; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | if (s->s3->alpn_selected) { |
| 1301 | const uint8_t *selected = s->s3->alpn_selected; |
| 1302 | size_t len = s->s3->alpn_selected_len; |
| 1303 | |
| 1304 | if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) { |
| 1305 | return NULL; |
| 1306 | } |
| 1307 | s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret); |
| 1308 | s2n(3 + len, ret); |
| 1309 | s2n(1 + len, ret); |
| 1310 | *ret++ = len; |
| 1311 | memcpy(ret, selected, len); |
| 1312 | ret += len; |
| 1313 | } |
| 1314 | |
| 1315 | /* If the client advertised support for Channel ID, and we have it |
| 1316 | * enabled, then we want to echo it back. */ |
| 1317 | if (s->s3->tlsext_channel_id_valid) { |
| 1318 | if (limit - ret - 4 < 0) { |
| 1319 | return NULL; |
| 1320 | } |
| 1321 | if (s->s3->tlsext_channel_id_new) { |
| 1322 | s2n(TLSEXT_TYPE_channel_id_new, ret); |
| 1323 | } else { |
| 1324 | s2n(TLSEXT_TYPE_channel_id, ret); |
| 1325 | } |
| 1326 | s2n(0, ret); |
| 1327 | } |
| 1328 | |
| 1329 | extdatalen = ret - orig - 2; |
| 1330 | if (extdatalen == 0) { |
| 1331 | return orig; |
| 1332 | } |
| 1333 | |
| 1334 | s2n(extdatalen, orig); |
| 1335 | return ret; |
| 1336 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1337 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1338 | /* tls1_alpn_handle_client_hello is called to process the ALPN extension in a |
| 1339 | * ClientHello. |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1340 | * cbs: the contents of the extension, not including the type and length. |
| 1341 | * out_alert: a pointer to the alert value to send in the event of a zero |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1342 | * return. |
| 1343 | * |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1344 | * returns: 1 on success. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1345 | static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert) { |
| 1346 | CBS protocol_name_list, protocol_name_list_copy; |
| 1347 | const uint8_t *selected; |
| 1348 | uint8_t selected_len; |
| 1349 | int r; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1350 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1351 | if (s->ctx->alpn_select_cb == NULL) { |
| 1352 | return 1; |
| 1353 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1354 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1355 | if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) || |
| 1356 | CBS_len(cbs) != 0 || CBS_len(&protocol_name_list) < 2) { |
| 1357 | goto parse_error; |
| 1358 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1359 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1360 | /* Validate the protocol list. */ |
| 1361 | protocol_name_list_copy = protocol_name_list; |
| 1362 | while (CBS_len(&protocol_name_list_copy) > 0) { |
| 1363 | CBS protocol_name; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1364 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1365 | if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name)) { |
| 1366 | goto parse_error; |
| 1367 | } |
| 1368 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1369 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1370 | r = s->ctx->alpn_select_cb( |
| 1371 | s, &selected, &selected_len, CBS_data(&protocol_name_list), |
| 1372 | CBS_len(&protocol_name_list), s->ctx->alpn_select_cb_arg); |
| 1373 | if (r == SSL_TLSEXT_ERR_OK) { |
| 1374 | if (s->s3->alpn_selected) { |
| 1375 | OPENSSL_free(s->s3->alpn_selected); |
| 1376 | } |
| 1377 | s->s3->alpn_selected = BUF_memdup(selected, selected_len); |
| 1378 | if (!s->s3->alpn_selected) { |
| 1379 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1380 | return 0; |
| 1381 | } |
| 1382 | s->s3->alpn_selected_len = selected_len; |
| 1383 | } |
| 1384 | |
| 1385 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1386 | |
| 1387 | parse_error: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1388 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1389 | return 0; |
| 1390 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1391 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1392 | static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) { |
| 1393 | int renegotiate_seen = 0; |
| 1394 | CBS extensions; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1395 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1396 | s->should_ack_sni = 0; |
| 1397 | s->s3->next_proto_neg_seen = 0; |
| 1398 | s->s3->tmp.certificate_status_expected = 0; |
| 1399 | s->s3->tmp.extended_master_secret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1400 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1401 | if (s->s3->alpn_selected) { |
| 1402 | OPENSSL_free(s->s3->alpn_selected); |
| 1403 | s->s3->alpn_selected = NULL; |
| 1404 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1405 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1406 | /* Clear any signature algorithms extension received */ |
| 1407 | if (s->cert->peer_sigalgs) { |
| 1408 | OPENSSL_free(s->cert->peer_sigalgs); |
| 1409 | s->cert->peer_sigalgs = NULL; |
| 1410 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1411 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1412 | /* Clear any shared signature algorithms */ |
| 1413 | if (s->cert->shared_sigalgs) { |
| 1414 | OPENSSL_free(s->cert->shared_sigalgs); |
| 1415 | s->cert->shared_sigalgs = NULL; |
| 1416 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1417 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1418 | /* Clear ECC extensions */ |
| 1419 | if (s->s3->tmp.peer_ecpointformatlist != 0) { |
| 1420 | OPENSSL_free(s->s3->tmp.peer_ecpointformatlist); |
| 1421 | s->s3->tmp.peer_ecpointformatlist = NULL; |
| 1422 | s->s3->tmp.peer_ecpointformatlist_length = 0; |
| 1423 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1424 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1425 | if (s->s3->tmp.peer_ellipticcurvelist != 0) { |
| 1426 | OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist); |
| 1427 | s->s3->tmp.peer_ellipticcurvelist = NULL; |
| 1428 | s->s3->tmp.peer_ellipticcurvelist_length = 0; |
| 1429 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1430 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1431 | /* There may be no extensions. */ |
| 1432 | if (CBS_len(cbs) == 0) { |
| 1433 | goto ri_check; |
| 1434 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1435 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1436 | /* Decode the extensions block and check it is valid. */ |
| 1437 | if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 1438 | !tls1_check_duplicate_extensions(&extensions)) { |
| 1439 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1440 | return 0; |
| 1441 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1442 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1443 | while (CBS_len(&extensions) != 0) { |
| 1444 | uint16_t type; |
| 1445 | CBS extension; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1446 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1447 | /* Decode the next extension. */ |
| 1448 | if (!CBS_get_u16(&extensions, &type) || |
| 1449 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 1450 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1451 | return 0; |
| 1452 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1453 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1454 | if (s->tlsext_debug_cb) { |
| 1455 | s->tlsext_debug_cb(s, 0, type, (uint8_t *)CBS_data(&extension), |
| 1456 | CBS_len(&extension), s->tlsext_debug_arg); |
| 1457 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1458 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1459 | /* The servername extension is treated as follows: |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1460 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1461 | - Only the hostname type is supported with a maximum length of 255. |
| 1462 | - The servername is rejected if too long or if it contains zeros, in |
| 1463 | which case an fatal alert is generated. |
| 1464 | - The servername field is maintained together with the session cache. |
| 1465 | - When a session is resumed, the servername call back invoked in order |
| 1466 | to allow the application to position itself to the right context. |
| 1467 | - The servername is acknowledged if it is new for a session or when |
| 1468 | it is identical to a previously used for the same session. |
| 1469 | Applications can control the behaviour. They can at any time |
| 1470 | set a 'desirable' servername for a new SSL object. This can be the |
| 1471 | case for example with HTTPS when a Host: header field is received and |
| 1472 | a renegotiation is requested. In this case, a possible servername |
| 1473 | presented in the new client hello is only acknowledged if it matches |
| 1474 | the value of the Host: field. |
| 1475 | - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
| 1476 | if they provide for changing an explicit servername context for the |
| 1477 | session, |
| 1478 | i.e. when the session has been established with a servername extension. |
| 1479 | - On session reconnect, the servername extension may be absent. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1480 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1481 | if (type == TLSEXT_TYPE_server_name) { |
| 1482 | CBS server_name_list; |
| 1483 | char have_seen_host_name = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1484 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1485 | if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) || |
| 1486 | CBS_len(&server_name_list) < 1 || CBS_len(&extension) != 0) { |
| 1487 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1488 | return 0; |
| 1489 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1490 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1491 | /* Decode each ServerName in the extension. */ |
| 1492 | while (CBS_len(&server_name_list) > 0) { |
| 1493 | uint8_t name_type; |
| 1494 | CBS host_name; |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1495 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1496 | /* Decode the NameType. */ |
| 1497 | if (!CBS_get_u8(&server_name_list, &name_type)) { |
| 1498 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1499 | return 0; |
| 1500 | } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 1501 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1502 | /* Only host_name is supported. */ |
| 1503 | if (name_type != TLSEXT_NAMETYPE_host_name) { |
| 1504 | continue; |
| 1505 | } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 1506 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1507 | if (have_seen_host_name) { |
| 1508 | /* The ServerNameList MUST NOT contain more than one name of the same |
| 1509 | * name_type. */ |
| 1510 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1511 | return 0; |
| 1512 | } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 1513 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1514 | have_seen_host_name = 1; |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 1515 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1516 | if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) || |
| 1517 | CBS_len(&host_name) < 1) { |
| 1518 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1519 | return 0; |
| 1520 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1521 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1522 | if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name || |
| 1523 | CBS_contains_zero_byte(&host_name)) { |
| 1524 | *out_alert = SSL_AD_UNRECOGNIZED_NAME; |
| 1525 | return 0; |
| 1526 | } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 1527 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1528 | if (!s->hit) { |
| 1529 | assert(s->session->tlsext_hostname == NULL); |
| 1530 | if (s->session->tlsext_hostname) { |
| 1531 | /* This should be impossible. */ |
| 1532 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1533 | return 0; |
| 1534 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1535 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1536 | /* Copy the hostname as a string. */ |
| 1537 | if (!CBS_strdup(&host_name, &s->session->tlsext_hostname)) { |
| 1538 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1539 | return 0; |
| 1540 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1541 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1542 | s->should_ack_sni = 1; |
| 1543 | } |
| 1544 | } |
| 1545 | } else if (type == TLSEXT_TYPE_ec_point_formats) { |
| 1546 | CBS ec_point_format_list; |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1547 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1548 | if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) || |
| 1549 | CBS_len(&extension) != 0) { |
| 1550 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1551 | return 0; |
| 1552 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1553 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1554 | if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist, |
| 1555 | &s->s3->tmp.peer_ecpointformatlist_length)) { |
| 1556 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1557 | return 0; |
| 1558 | } |
| 1559 | } else if (type == TLSEXT_TYPE_elliptic_curves) { |
| 1560 | CBS elliptic_curve_list; |
| 1561 | size_t i, num_curves; |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1562 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1563 | if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) || |
| 1564 | CBS_len(&elliptic_curve_list) == 0 || |
| 1565 | (CBS_len(&elliptic_curve_list) & 1) != 0 || |
| 1566 | CBS_len(&extension) != 0) { |
| 1567 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1568 | return 0; |
| 1569 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1570 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1571 | if (s->s3->tmp.peer_ellipticcurvelist) { |
| 1572 | OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist); |
| 1573 | s->s3->tmp.peer_ellipticcurvelist_length = 0; |
| 1574 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1575 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1576 | s->s3->tmp.peer_ellipticcurvelist = |
| 1577 | (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list)); |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1578 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1579 | if (s->s3->tmp.peer_ellipticcurvelist == NULL) { |
| 1580 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1581 | return 0; |
| 1582 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1583 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1584 | num_curves = CBS_len(&elliptic_curve_list) / 2; |
| 1585 | for (i = 0; i < num_curves; i++) { |
| 1586 | if (!CBS_get_u16(&elliptic_curve_list, |
| 1587 | &s->s3->tmp.peer_ellipticcurvelist[i])) { |
| 1588 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1589 | return 0; |
| 1590 | } |
| 1591 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1592 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1593 | if (CBS_len(&elliptic_curve_list) != 0) { |
| 1594 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1595 | return 0; |
| 1596 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1597 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1598 | s->s3->tmp.peer_ellipticcurvelist_length = num_curves; |
| 1599 | } else if (type == TLSEXT_TYPE_renegotiate) { |
| 1600 | if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert)) { |
| 1601 | return 0; |
| 1602 | } |
| 1603 | renegotiate_seen = 1; |
| 1604 | } else if (type == TLSEXT_TYPE_signature_algorithms) { |
| 1605 | CBS supported_signature_algorithms; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1606 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1607 | if (!CBS_get_u16_length_prefixed(&extension, |
| 1608 | &supported_signature_algorithms) || |
| 1609 | CBS_len(&extension) != 0) { |
| 1610 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1611 | return 0; |
| 1612 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1613 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1614 | /* Ensure the signature algorithms are non-empty. It contains a list of |
| 1615 | * SignatureAndHashAlgorithms which are two bytes each. */ |
| 1616 | if (CBS_len(&supported_signature_algorithms) == 0 || |
| 1617 | (CBS_len(&supported_signature_algorithms) % 2) != 0) { |
| 1618 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1619 | return 0; |
| 1620 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1621 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1622 | if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) { |
| 1623 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1624 | return 0; |
| 1625 | } |
| 1626 | /* If sigalgs received and no shared algorithms fatal error. */ |
| 1627 | if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs) { |
| 1628 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, |
| 1629 | SSL_R_NO_SHARED_SIGATURE_ALGORITHMS); |
| 1630 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1631 | return 0; |
| 1632 | } |
| 1633 | } else if (type == TLSEXT_TYPE_next_proto_neg && |
| 1634 | s->s3->tmp.finish_md_len == 0 && s->s3->alpn_selected == NULL) { |
| 1635 | /* The extension must be empty. */ |
| 1636 | if (CBS_len(&extension) != 0) { |
| 1637 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1638 | return 0; |
| 1639 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1640 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1641 | /* We shouldn't accept this extension on a renegotiation. |
| 1642 | * |
| 1643 | * s->new_session will be set on renegotiation, but we probably shouldn't |
| 1644 | * rely that it couldn't be set on the initial renegotation too in |
| 1645 | * certain cases (when there's some other reason to disallow resuming an |
| 1646 | * earlier session -- the current code won't be doing anything like that, |
| 1647 | * but this might change). |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1648 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1649 | * A valid sign that there's been a previous handshake in this connection |
| 1650 | * is if s->s3->tmp.finish_md_len > 0. (We are talking about a check |
| 1651 | * that will happen in the Hello protocol round, well before a new |
| 1652 | * Finished message could have been computed.) */ |
| 1653 | s->s3->next_proto_neg_seen = 1; |
| 1654 | } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation && |
| 1655 | s->ctx->alpn_select_cb && s->s3->tmp.finish_md_len == 0) { |
| 1656 | if (!tls1_alpn_handle_client_hello(s, &extension, out_alert)) { |
| 1657 | return 0; |
| 1658 | } |
| 1659 | /* ALPN takes precedence over NPN. */ |
| 1660 | s->s3->next_proto_neg_seen = 0; |
| 1661 | } else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled) { |
| 1662 | /* The extension must be empty. */ |
| 1663 | if (CBS_len(&extension) != 0) { |
| 1664 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1665 | return 0; |
| 1666 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1667 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1668 | s->s3->tlsext_channel_id_valid = 1; |
| 1669 | } else if (type == TLSEXT_TYPE_channel_id_new && |
| 1670 | s->tlsext_channel_id_enabled) { |
| 1671 | /* The extension must be empty. */ |
| 1672 | if (CBS_len(&extension) != 0) { |
| 1673 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1674 | return 0; |
| 1675 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1676 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1677 | s->s3->tlsext_channel_id_valid = 1; |
| 1678 | s->s3->tlsext_channel_id_new = 1; |
| 1679 | } else if (type == TLSEXT_TYPE_use_srtp) { |
| 1680 | if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert)) { |
| 1681 | return 0; |
| 1682 | } |
| 1683 | } else if (type == TLSEXT_TYPE_extended_master_secret && |
| 1684 | s->version != SSL3_VERSION) { |
| 1685 | if (CBS_len(&extension) != 0) { |
| 1686 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1687 | return 0; |
| 1688 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1689 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1690 | s->s3->tmp.extended_master_secret = 1; |
| 1691 | } |
| 1692 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1693 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1694 | ri_check: |
| 1695 | /* Need RI if renegotiating */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1696 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1697 | if (!renegotiate_seen && s->renegotiate && |
| 1698 | !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { |
| 1699 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 1700 | OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext, |
| 1701 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
| 1702 | return 0; |
| 1703 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1704 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1705 | return 1; |
| 1706 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1707 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1708 | int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) { |
| 1709 | int alert = -1; |
| 1710 | if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) { |
| 1711 | ssl3_send_alert(s, SSL3_AL_FATAL, alert); |
| 1712 | return 0; |
| 1713 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1714 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1715 | if (ssl_check_clienthello_tlsext(s) <= 0) { |
| 1716 | OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, |
| 1717 | SSL_R_CLIENTHELLO_TLSEXT); |
| 1718 | return 0; |
| 1719 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1720 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1721 | return 1; |
| 1722 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1723 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1724 | /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1725 | * elements of zero length are allowed and the set of elements must exactly |
| 1726 | * fill the length of the block. */ |
| 1727 | static char ssl_next_proto_validate(const CBS *cbs) { |
| 1728 | CBS copy = *cbs; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1729 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1730 | while (CBS_len(©) != 0) { |
| 1731 | CBS proto; |
| 1732 | if (!CBS_get_u8_length_prefixed(©, &proto) || CBS_len(&proto) == 0) { |
| 1733 | return 0; |
| 1734 | } |
| 1735 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1736 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1737 | return 1; |
| 1738 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1739 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1740 | static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) { |
| 1741 | int tlsext_servername = 0; |
| 1742 | int renegotiate_seen = 0; |
| 1743 | CBS extensions; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1744 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1745 | /* TODO(davidben): Move all of these to some per-handshake state that gets |
| 1746 | * systematically reset on a new handshake; perhaps allocate it fresh each |
| 1747 | * time so it's not even kept around post-handshake. */ |
| 1748 | s->s3->next_proto_neg_seen = 0; |
David Benjamin | 6444287 | 2014-07-21 17:43:45 -0400 | [diff] [blame] | 1749 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1750 | s->tlsext_ticket_expected = 0; |
| 1751 | s->s3->tmp.certificate_status_expected = 0; |
| 1752 | s->s3->tmp.extended_master_secret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1753 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1754 | if (s->s3->alpn_selected) { |
| 1755 | OPENSSL_free(s->s3->alpn_selected); |
| 1756 | s->s3->alpn_selected = NULL; |
| 1757 | } |
David Benjamin | a19fc25 | 2014-10-19 00:14:36 -0400 | [diff] [blame] | 1758 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1759 | /* Clear ECC extensions */ |
| 1760 | if (s->s3->tmp.peer_ecpointformatlist != 0) { |
| 1761 | OPENSSL_free(s->s3->tmp.peer_ecpointformatlist); |
| 1762 | s->s3->tmp.peer_ecpointformatlist = NULL; |
| 1763 | s->s3->tmp.peer_ecpointformatlist_length = 0; |
| 1764 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 1765 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1766 | /* There may be no extensions. */ |
| 1767 | if (CBS_len(cbs) == 0) { |
| 1768 | goto ri_check; |
| 1769 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1770 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1771 | /* Decode the extensions block and check it is valid. */ |
| 1772 | if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 1773 | !tls1_check_duplicate_extensions(&extensions)) { |
| 1774 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1775 | return 0; |
| 1776 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1777 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1778 | while (CBS_len(&extensions) != 0) { |
| 1779 | uint16_t type; |
| 1780 | CBS extension; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1781 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1782 | /* Decode the next extension. */ |
| 1783 | if (!CBS_get_u16(&extensions, &type) || |
| 1784 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 1785 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1786 | return 0; |
| 1787 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1788 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1789 | if (s->tlsext_debug_cb) { |
| 1790 | s->tlsext_debug_cb(s, 1, type, (uint8_t *)CBS_data(&extension), |
| 1791 | CBS_len(&extension), s->tlsext_debug_arg); |
| 1792 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1793 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1794 | if (type == TLSEXT_TYPE_server_name) { |
| 1795 | /* The extension must be empty. */ |
| 1796 | if (CBS_len(&extension) != 0) { |
| 1797 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1798 | return 0; |
| 1799 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 1800 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1801 | /* We must have sent it in ClientHello. */ |
| 1802 | if (s->tlsext_hostname == NULL) { |
| 1803 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 1804 | return 0; |
| 1805 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 1806 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1807 | tlsext_servername = 1; |
| 1808 | } else if (type == TLSEXT_TYPE_ec_point_formats) { |
| 1809 | CBS ec_point_format_list; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 1810 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1811 | if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) || |
| 1812 | CBS_len(&extension) != 0) { |
| 1813 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1814 | return 0; |
| 1815 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1816 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1817 | if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist, |
| 1818 | &s->s3->tmp.peer_ecpointformatlist_length)) { |
| 1819 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1820 | return 0; |
| 1821 | } |
| 1822 | } else if (type == TLSEXT_TYPE_session_ticket) { |
| 1823 | if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0) { |
| 1824 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 1825 | return 0; |
| 1826 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 1827 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1828 | s->tlsext_ticket_expected = 1; |
| 1829 | } else if (type == TLSEXT_TYPE_status_request) { |
| 1830 | /* The extension MUST be empty and may only sent if we've requested a |
| 1831 | * status request message. */ |
| 1832 | if (CBS_len(&extension) != 0) { |
| 1833 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1834 | return 0; |
| 1835 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 1836 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1837 | if (!s->ocsp_stapling_enabled) { |
| 1838 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 1839 | return 0; |
| 1840 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1841 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1842 | /* Set a flag to expect a CertificateStatus message */ |
| 1843 | s->s3->tmp.certificate_status_expected = 1; |
| 1844 | } else if (type == TLSEXT_TYPE_next_proto_neg && |
| 1845 | s->s3->tmp.finish_md_len == 0) { |
| 1846 | uint8_t *selected; |
| 1847 | uint8_t selected_len; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 1848 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1849 | /* We must have requested it. */ |
| 1850 | if (s->ctx->next_proto_select_cb == NULL) { |
| 1851 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 1852 | return 0; |
| 1853 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 1854 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1855 | /* The data must be valid. */ |
| 1856 | if (!ssl_next_proto_validate(&extension)) { |
| 1857 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1858 | return 0; |
| 1859 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1860 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1861 | if (s->ctx->next_proto_select_cb( |
| 1862 | s, &selected, &selected_len, CBS_data(&extension), |
| 1863 | CBS_len(&extension), |
| 1864 | s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) { |
| 1865 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1866 | return 0; |
| 1867 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1868 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1869 | s->next_proto_negotiated = BUF_memdup(selected, selected_len); |
| 1870 | if (s->next_proto_negotiated == NULL) { |
| 1871 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1872 | return 0; |
| 1873 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1874 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1875 | s->next_proto_negotiated_len = selected_len; |
| 1876 | s->s3->next_proto_neg_seen = 1; |
| 1877 | } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) { |
| 1878 | CBS protocol_name_list, protocol_name; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1879 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1880 | /* We must have requested it. */ |
| 1881 | if (s->alpn_client_proto_list == NULL) { |
| 1882 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 1883 | return 0; |
| 1884 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1885 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1886 | /* The extension data consists of a ProtocolNameList which must have |
| 1887 | * exactly one ProtocolName. Each of these is length-prefixed. */ |
| 1888 | if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) || |
| 1889 | CBS_len(&extension) != 0 || |
| 1890 | !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) || |
| 1891 | CBS_len(&protocol_name_list) != 0) { |
| 1892 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1893 | return 0; |
| 1894 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1895 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1896 | if (!CBS_stow(&protocol_name, &s->s3->alpn_selected, |
| 1897 | &s->s3->alpn_selected_len)) { |
| 1898 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1899 | return 0; |
| 1900 | } |
| 1901 | } else if (type == TLSEXT_TYPE_channel_id) { |
| 1902 | if (CBS_len(&extension) != 0) { |
| 1903 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1904 | return 0; |
| 1905 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1906 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1907 | s->s3->tlsext_channel_id_valid = 1; |
| 1908 | } else if (type == TLSEXT_TYPE_channel_id_new) { |
| 1909 | if (CBS_len(&extension) != 0) { |
| 1910 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1911 | return 0; |
| 1912 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1913 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1914 | s->s3->tlsext_channel_id_valid = 1; |
| 1915 | s->s3->tlsext_channel_id_new = 1; |
| 1916 | } else if (type == TLSEXT_TYPE_certificate_timestamp) { |
| 1917 | if (CBS_len(&extension) == 0) { |
| 1918 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1919 | return 0; |
| 1920 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1921 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1922 | /* Session resumption uses the original session information. */ |
| 1923 | if (!s->hit && |
| 1924 | !CBS_stow(&extension, &s->session->tlsext_signed_cert_timestamp_list, |
| 1925 | &s->session->tlsext_signed_cert_timestamp_list_length)) { |
| 1926 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1927 | return 0; |
| 1928 | } |
| 1929 | } else if (type == TLSEXT_TYPE_renegotiate) { |
| 1930 | if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert)) { |
| 1931 | return 0; |
| 1932 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1933 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1934 | renegotiate_seen = 1; |
| 1935 | } else if (type == TLSEXT_TYPE_use_srtp) { |
| 1936 | if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert)) { |
| 1937 | return 0; |
| 1938 | } |
| 1939 | } else if (type == TLSEXT_TYPE_extended_master_secret) { |
| 1940 | if (/* It is invalid for the server to select EMS and |
| 1941 | SSLv3. */ |
| 1942 | s->version == SSL3_VERSION || CBS_len(&extension) != 0) { |
| 1943 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1944 | return 0; |
| 1945 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1946 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1947 | s->s3->tmp.extended_master_secret = 1; |
| 1948 | } |
| 1949 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1950 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1951 | if (!s->hit && tlsext_servername == 1 && s->tlsext_hostname) { |
| 1952 | if (s->session->tlsext_hostname == NULL) { |
| 1953 | s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname); |
| 1954 | if (!s->session->tlsext_hostname) { |
| 1955 | *out_alert = SSL_AD_UNRECOGNIZED_NAME; |
| 1956 | return 0; |
| 1957 | } |
| 1958 | } else { |
| 1959 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1960 | return 0; |
| 1961 | } |
| 1962 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1963 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1964 | ri_check: |
| 1965 | /* Determine if we need to see RI. Strictly speaking if we want to avoid an |
| 1966 | * attack we should *always* see RI even on initial server hello because the |
| 1967 | * client doesn't see any renegotiation during an attack. However this would |
| 1968 | * mean we could not connect to any server which doesn't support RI so for |
| 1969 | * the immediate future tolerate RI absence on initial connect only. */ |
| 1970 | if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) && |
| 1971 | !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { |
| 1972 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 1973 | OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, |
| 1974 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
| 1975 | return 0; |
| 1976 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1977 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1978 | return 1; |
| 1979 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1980 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1981 | int ssl_prepare_clienthello_tlsext(SSL *s) { return 1; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1982 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1983 | int ssl_prepare_serverhello_tlsext(SSL *s) { return 1; } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 1984 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1985 | static int ssl_check_clienthello_tlsext(SSL *s) { |
| 1986 | int ret = SSL_TLSEXT_ERR_NOACK; |
| 1987 | int al = SSL_AD_UNRECOGNIZED_NAME; |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 1988 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1989 | /* The handling of the ECPointFormats extension is done elsewhere, namely in |
| 1990 | * ssl3_choose_cipher in s3_lib.c. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1991 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1992 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) { |
| 1993 | ret = s->ctx->tlsext_servername_callback(s, &al, |
| 1994 | s->ctx->tlsext_servername_arg); |
| 1995 | } else if (s->initial_ctx != NULL && |
| 1996 | s->initial_ctx->tlsext_servername_callback != 0) { |
| 1997 | ret = s->initial_ctx->tlsext_servername_callback( |
| 1998 | s, &al, s->initial_ctx->tlsext_servername_arg); |
| 1999 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2000 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2001 | switch (ret) { |
| 2002 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2003 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2004 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2005 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2006 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2007 | ssl3_send_alert(s, SSL3_AL_WARNING, al); |
| 2008 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2009 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2010 | case SSL_TLSEXT_ERR_NOACK: |
| 2011 | s->should_ack_sni = 0; |
| 2012 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2013 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2014 | default: |
| 2015 | return 1; |
| 2016 | } |
| 2017 | } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 2018 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2019 | static int ssl_check_serverhello_tlsext(SSL *s) { |
| 2020 | int ret = SSL_TLSEXT_ERR_NOACK; |
| 2021 | int al = SSL_AD_UNRECOGNIZED_NAME; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2022 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2023 | /* If we are client and using an elliptic curve cryptography cipher suite, |
| 2024 | * then if server returns an EC point formats lists extension it must contain |
| 2025 | * uncompressed. */ |
| 2026 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 2027 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 2028 | if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) && |
| 2029 | !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed)) { |
| 2030 | OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext, |
| 2031 | SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST); |
| 2032 | return -1; |
| 2033 | } |
| 2034 | ret = SSL_TLSEXT_ERR_OK; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2035 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2036 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) { |
| 2037 | ret = s->ctx->tlsext_servername_callback(s, &al, |
| 2038 | s->ctx->tlsext_servername_arg); |
| 2039 | } else if (s->initial_ctx != NULL && |
| 2040 | s->initial_ctx->tlsext_servername_callback != 0) { |
| 2041 | ret = s->initial_ctx->tlsext_servername_callback( |
| 2042 | s, &al, s->initial_ctx->tlsext_servername_arg); |
| 2043 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2044 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2045 | switch (ret) { |
| 2046 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2047 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2048 | return -1; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2049 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2050 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2051 | ssl3_send_alert(s, SSL3_AL_WARNING, al); |
| 2052 | return 1; |
| 2053 | |
| 2054 | default: |
| 2055 | return 1; |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) { |
| 2060 | int alert = -1; |
| 2061 | if (s->version < SSL3_VERSION) { |
| 2062 | return 1; |
| 2063 | } |
| 2064 | |
| 2065 | if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) { |
| 2066 | ssl3_send_alert(s, SSL3_AL_FATAL, alert); |
| 2067 | return 0; |
| 2068 | } |
| 2069 | |
| 2070 | if (ssl_check_serverhello_tlsext(s) <= 0) { |
| 2071 | OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, |
| 2072 | SSL_R_SERVERHELLO_TLSEXT); |
| 2073 | return 0; |
| 2074 | } |
| 2075 | |
| 2076 | return 1; |
| 2077 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2078 | |
| 2079 | /* Since the server cache lookup is done early on in the processing of the |
| 2080 | * ClientHello, and other operations depend on the result, we need to handle |
| 2081 | * any TLS session ticket extension at the same time. |
| 2082 | * |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2083 | * ctx: contains the early callback context, which is the result of a |
| 2084 | * shallow parse of the ClientHello. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2085 | * ret: (output) on return, if a ticket was decrypted, then this is set to |
| 2086 | * point to the resulting session. |
| 2087 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2088 | * Returns: |
| 2089 | * -1: fatal error, either from parsing or decrypting the ticket. |
| 2090 | * 0: no ticket was found (or was ignored, based on settings). |
| 2091 | * 1: a zero length extension was found, indicating that the client supports |
| 2092 | * session tickets but doesn't currently have one to offer. |
David Benjamin | d1681e6 | 2014-11-20 14:54:14 -0500 | [diff] [blame] | 2093 | * 2: a ticket was offered but couldn't be decrypted because of a non-fatal |
| 2094 | * error. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2095 | * 3: a ticket was successfully decrypted and *ret was set. |
| 2096 | * |
| 2097 | * Side effects: |
| 2098 | * Sets s->tlsext_ticket_expected to 1 if the server will have to issue |
| 2099 | * a new session ticket to the client because the client indicated support |
David Benjamin | d1681e6 | 2014-11-20 14:54:14 -0500 | [diff] [blame] | 2100 | * but the client either doesn't have a session ticket or we couldn't use |
| 2101 | * the one it gave us, or if s->ctx->tlsext_ticket_key_cb asked to renew |
| 2102 | * the client's ticket. Otherwise, s->tlsext_ticket_expected is set to 0. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2103 | */ |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2104 | int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2105 | SSL_SESSION **ret) { |
| 2106 | *ret = NULL; |
| 2107 | s->tlsext_ticket_expected = 0; |
| 2108 | const uint8_t *data; |
| 2109 | size_t len; |
| 2110 | int r; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2111 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2112 | /* If tickets disabled behave as if no ticket present to permit stateful |
| 2113 | * resumption. */ |
| 2114 | if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || |
| 2115 | (s->version <= SSL3_VERSION && !ctx->extensions) || |
| 2116 | !SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket, |
| 2117 | &data, &len)) { |
| 2118 | return 0; |
| 2119 | } |
| 2120 | |
| 2121 | if (len == 0) { |
| 2122 | /* The client will accept a ticket but doesn't currently have one. */ |
| 2123 | s->tlsext_ticket_expected = 1; |
| 2124 | return 1; |
| 2125 | } |
| 2126 | |
| 2127 | r = tls_decrypt_ticket(s, data, len, ctx->session_id, ctx->session_id_len, |
| 2128 | ret); |
| 2129 | switch (r) { |
| 2130 | case 2: /* ticket couldn't be decrypted */ |
| 2131 | s->tlsext_ticket_expected = 1; |
| 2132 | return 2; |
| 2133 | |
| 2134 | case 3: /* ticket was decrypted */ |
| 2135 | return r; |
| 2136 | |
| 2137 | case 4: /* ticket decrypted but need to renew */ |
| 2138 | s->tlsext_ticket_expected = 1; |
| 2139 | return 3; |
| 2140 | |
| 2141 | default: /* fatal error */ |
| 2142 | return -1; |
| 2143 | } |
| 2144 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2145 | |
| 2146 | /* tls_decrypt_ticket attempts to decrypt a session ticket. |
| 2147 | * |
| 2148 | * etick: points to the body of the session ticket extension. |
| 2149 | * eticklen: the length of the session tickets extenion. |
| 2150 | * sess_id: points at the session ID. |
| 2151 | * sesslen: the length of the session ID. |
| 2152 | * psess: (output) on return, if a ticket was decrypted, then this is set to |
| 2153 | * point to the resulting session. |
| 2154 | * |
| 2155 | * Returns: |
| 2156 | * -1: fatal error, either from parsing or decrypting the ticket. |
| 2157 | * 2: the ticket couldn't be decrypted. |
| 2158 | * 3: a ticket was successfully decrypted and *psess was set. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2159 | * 4: same as 3, but the ticket needs to be renewed. */ |
| 2160 | static int tls_decrypt_ticket(SSL *s, const uint8_t *etick, int eticklen, |
| 2161 | const uint8_t *sess_id, int sesslen, |
| 2162 | SSL_SESSION **psess) { |
| 2163 | SSL_SESSION *sess; |
| 2164 | uint8_t *sdec; |
| 2165 | const uint8_t *p; |
| 2166 | int slen, mlen, renew_ticket = 0; |
| 2167 | uint8_t tick_hmac[EVP_MAX_MD_SIZE]; |
| 2168 | HMAC_CTX hctx; |
| 2169 | EVP_CIPHER_CTX ctx; |
| 2170 | SSL_CTX *tctx = s->initial_ctx; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2171 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2172 | /* Need at least keyname + iv + some encrypted data */ |
| 2173 | if (eticklen < 48) { |
| 2174 | return 2; |
| 2175 | } |
| 2176 | |
| 2177 | /* Initialize session ticket encryption and HMAC contexts */ |
| 2178 | HMAC_CTX_init(&hctx); |
| 2179 | EVP_CIPHER_CTX_init(&ctx); |
| 2180 | if (tctx->tlsext_ticket_key_cb) { |
| 2181 | uint8_t *nctick = (uint8_t *)etick; |
| 2182 | int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, &ctx, &hctx, 0); |
| 2183 | if (rv < 0) { |
| 2184 | return -1; |
| 2185 | } |
| 2186 | if (rv == 0) { |
| 2187 | return 2; |
| 2188 | } |
| 2189 | if (rv == 2) { |
| 2190 | renew_ticket = 1; |
| 2191 | } |
| 2192 | } else { |
| 2193 | /* Check key name matches */ |
| 2194 | if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) { |
| 2195 | return 2; |
| 2196 | } |
| 2197 | if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(), |
| 2198 | NULL) || |
| 2199 | !EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, |
| 2200 | tctx->tlsext_tick_aes_key, etick + 16)) { |
| 2201 | HMAC_CTX_cleanup(&hctx); |
| 2202 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2203 | return -1; |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | /* Attempt to process session ticket, first conduct sanity and integrity |
| 2208 | * checks on ticket. */ |
| 2209 | mlen = HMAC_size(&hctx); |
| 2210 | if (mlen < 0) { |
| 2211 | HMAC_CTX_cleanup(&hctx); |
| 2212 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2213 | return -1; |
| 2214 | } |
| 2215 | eticklen -= mlen; |
| 2216 | /* Check HMAC of encrypted ticket */ |
| 2217 | HMAC_Update(&hctx, etick, eticklen); |
| 2218 | HMAC_Final(&hctx, tick_hmac, NULL); |
| 2219 | HMAC_CTX_cleanup(&hctx); |
| 2220 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { |
| 2221 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2222 | return 2; |
| 2223 | } |
| 2224 | |
| 2225 | /* Attempt to decrypt session data */ |
| 2226 | /* Move p after IV to start of encrypted ticket, update length */ |
| 2227 | p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx); |
| 2228 | eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx); |
| 2229 | sdec = OPENSSL_malloc(eticklen); |
| 2230 | if (!sdec) { |
| 2231 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2232 | return -1; |
| 2233 | } |
| 2234 | EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen); |
| 2235 | if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0) { |
| 2236 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2237 | OPENSSL_free(sdec); |
| 2238 | return 2; |
| 2239 | } |
| 2240 | slen += mlen; |
| 2241 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2242 | p = sdec; |
| 2243 | |
| 2244 | sess = d2i_SSL_SESSION(NULL, &p, slen); |
| 2245 | OPENSSL_free(sdec); |
| 2246 | if (sess) { |
| 2247 | /* The session ID, if non-empty, is used by some clients to detect that the |
| 2248 | * ticket has been accepted. So we copy it to the session structure. If it |
| 2249 | * is empty set length to zero as required by standard. */ |
| 2250 | if (sesslen) { |
| 2251 | memcpy(sess->session_id, sess_id, sesslen); |
| 2252 | } |
| 2253 | sess->session_id_length = sesslen; |
| 2254 | *psess = sess; |
| 2255 | if (renew_ticket) { |
| 2256 | return 4; |
| 2257 | } |
| 2258 | return 3; |
| 2259 | } |
| 2260 | |
| 2261 | ERR_clear_error(); |
| 2262 | /* For session parse failure, indicate that we need to send a new ticket. */ |
| 2263 | return 2; |
| 2264 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2265 | |
| 2266 | /* Tables to translate from NIDs to TLS v1.2 ids */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2267 | typedef struct { |
| 2268 | int nid; |
| 2269 | int id; |
| 2270 | } tls12_lookup; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2271 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2272 | static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5}, |
| 2273 | {NID_sha1, TLSEXT_hash_sha1}, |
| 2274 | {NID_sha224, TLSEXT_hash_sha224}, |
| 2275 | {NID_sha256, TLSEXT_hash_sha256}, |
| 2276 | {NID_sha384, TLSEXT_hash_sha384}, |
| 2277 | {NID_sha512, TLSEXT_hash_sha512}}; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2278 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2279 | static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa}, |
| 2280 | {EVP_PKEY_EC, TLSEXT_signature_ecdsa}}; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2281 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2282 | static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) { |
| 2283 | size_t i; |
| 2284 | for (i = 0; i < tlen; i++) { |
| 2285 | if (table[i].nid == nid) { |
| 2286 | return table[i].id; |
| 2287 | } |
| 2288 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2289 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2290 | return -1; |
| 2291 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2292 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2293 | static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) { |
| 2294 | size_t i; |
| 2295 | for (i = 0; i < tlen; i++) { |
| 2296 | if (table[i].id == id) { |
| 2297 | return table[i].nid; |
| 2298 | } |
| 2299 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2300 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2301 | return NID_undef; |
| 2302 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2303 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2304 | int tls12_get_sigandhash(uint8_t *p, const EVP_PKEY *pk, const EVP_MD *md) { |
| 2305 | int sig_id, md_id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2306 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2307 | if (!md) { |
| 2308 | return 0; |
| 2309 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2310 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2311 | md_id = tls12_find_id(EVP_MD_type(md), tls12_md, |
| 2312 | sizeof(tls12_md) / sizeof(tls12_lookup)); |
| 2313 | if (md_id == -1) { |
| 2314 | return 0; |
| 2315 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2316 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2317 | sig_id = tls12_get_sigid(pk); |
| 2318 | if (sig_id == -1) { |
| 2319 | return 0; |
| 2320 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2321 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2322 | p[0] = (uint8_t)md_id; |
| 2323 | p[1] = (uint8_t)sig_id; |
| 2324 | return 1; |
| 2325 | } |
| 2326 | |
| 2327 | int tls12_get_sigid(const EVP_PKEY *pk) { |
| 2328 | return tls12_find_id(pk->type, tls12_sig, |
| 2329 | sizeof(tls12_sig) / sizeof(tls12_lookup)); |
| 2330 | } |
| 2331 | |
| 2332 | const EVP_MD *tls12_get_hash(uint8_t hash_alg) { |
| 2333 | switch (hash_alg) { |
| 2334 | case TLSEXT_hash_md5: |
| 2335 | return EVP_md5(); |
| 2336 | |
| 2337 | case TLSEXT_hash_sha1: |
| 2338 | return EVP_sha1(); |
| 2339 | |
| 2340 | case TLSEXT_hash_sha224: |
| 2341 | return EVP_sha224(); |
| 2342 | |
| 2343 | case TLSEXT_hash_sha256: |
| 2344 | return EVP_sha256(); |
| 2345 | |
| 2346 | case TLSEXT_hash_sha384: |
| 2347 | return EVP_sha384(); |
| 2348 | |
| 2349 | case TLSEXT_hash_sha512: |
| 2350 | return EVP_sha512(); |
| 2351 | |
| 2352 | default: |
| 2353 | return NULL; |
| 2354 | } |
| 2355 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2356 | |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 2357 | /* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature |
| 2358 | * algorithm |sig_alg|. It returns -1 if the type is unknown. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2359 | static int tls12_get_pkey_type(uint8_t sig_alg) { |
| 2360 | switch (sig_alg) { |
| 2361 | case TLSEXT_signature_rsa: |
| 2362 | return EVP_PKEY_RSA; |
| 2363 | |
| 2364 | case TLSEXT_signature_ecdsa: |
| 2365 | return EVP_PKEY_EC; |
| 2366 | |
| 2367 | default: |
| 2368 | return -1; |
| 2369 | } |
| 2370 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2371 | |
| 2372 | /* Convert TLS 1.2 signature algorithm extension values into NIDs */ |
| 2373 | static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2374 | int *psignhash_nid, const uint8_t *data) { |
| 2375 | int sign_nid = 0, hash_nid = 0; |
| 2376 | if (!phash_nid && !psign_nid && !psignhash_nid) { |
| 2377 | return; |
| 2378 | } |
| 2379 | |
| 2380 | if (phash_nid || psignhash_nid) { |
| 2381 | hash_nid = tls12_find_nid(data[0], tls12_md, |
| 2382 | sizeof(tls12_md) / sizeof(tls12_lookup)); |
| 2383 | if (phash_nid) { |
| 2384 | *phash_nid = hash_nid; |
| 2385 | } |
| 2386 | } |
| 2387 | |
| 2388 | if (psign_nid || psignhash_nid) { |
| 2389 | sign_nid = tls12_find_nid(data[1], tls12_sig, |
| 2390 | sizeof(tls12_sig) / sizeof(tls12_lookup)); |
| 2391 | if (psign_nid) { |
| 2392 | *psign_nid = sign_nid; |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | if (psignhash_nid) { |
| 2397 | if (sign_nid && hash_nid) { |
| 2398 | OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid); |
| 2399 | } else { |
| 2400 | *psignhash_nid = NID_undef; |
| 2401 | } |
| 2402 | } |
| 2403 | } |
| 2404 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2405 | /* Given preference and allowed sigalgs set shared sigalgs */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2406 | static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref, |
| 2407 | size_t preflen, const uint8_t *allow, |
| 2408 | size_t allowlen) { |
| 2409 | const uint8_t *ptmp, *atmp; |
| 2410 | size_t i, j, nmatch = 0; |
| 2411 | |
| 2412 | for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) { |
| 2413 | /* Skip disabled hashes or signature algorithms */ |
| 2414 | if (tls12_get_hash(ptmp[0]) == NULL || |
| 2415 | tls12_get_pkey_type(ptmp[1]) == -1) { |
| 2416 | continue; |
| 2417 | } |
| 2418 | |
| 2419 | for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) { |
| 2420 | if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) { |
| 2421 | nmatch++; |
| 2422 | if (shsig) { |
| 2423 | shsig->rhash = ptmp[0]; |
| 2424 | shsig->rsign = ptmp[1]; |
| 2425 | tls1_lookup_sigalg(&shsig->hash_nid, &shsig->sign_nid, |
| 2426 | &shsig->signandhash_nid, ptmp); |
| 2427 | shsig++; |
| 2428 | } |
| 2429 | |
| 2430 | break; |
| 2431 | } |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | return nmatch; |
| 2436 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2437 | |
| 2438 | /* Set shared signature algorithms for SSL structures */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2439 | static int tls1_set_shared_sigalgs(SSL *s) { |
| 2440 | const uint8_t *pref, *allow, *conf; |
| 2441 | size_t preflen, allowlen, conflen; |
| 2442 | size_t nmatch; |
| 2443 | TLS_SIGALGS *salgs = NULL; |
| 2444 | CERT *c = s->cert; |
| 2445 | |
| 2446 | if (c->shared_sigalgs) { |
| 2447 | OPENSSL_free(c->shared_sigalgs); |
| 2448 | c->shared_sigalgs = NULL; |
| 2449 | } |
| 2450 | |
| 2451 | /* If client use client signature algorithms if not NULL */ |
| 2452 | if (!s->server && c->client_sigalgs) { |
| 2453 | conf = c->client_sigalgs; |
| 2454 | conflen = c->client_sigalgslen; |
| 2455 | } else if (c->conf_sigalgs) { |
| 2456 | conf = c->conf_sigalgs; |
| 2457 | conflen = c->conf_sigalgslen; |
| 2458 | } else { |
| 2459 | conflen = tls12_get_psigalgs(s, &conf); |
| 2460 | } |
| 2461 | |
| 2462 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
| 2463 | pref = conf; |
| 2464 | preflen = conflen; |
| 2465 | allow = c->peer_sigalgs; |
| 2466 | allowlen = c->peer_sigalgslen; |
| 2467 | } else { |
| 2468 | allow = conf; |
| 2469 | allowlen = conflen; |
| 2470 | pref = c->peer_sigalgs; |
| 2471 | preflen = c->peer_sigalgslen; |
| 2472 | } |
| 2473 | |
| 2474 | nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen); |
| 2475 | if (!nmatch) { |
| 2476 | return 1; |
| 2477 | } |
| 2478 | |
| 2479 | salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS)); |
| 2480 | if (!salgs) { |
| 2481 | return 0; |
| 2482 | } |
| 2483 | |
| 2484 | nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen); |
| 2485 | c->shared_sigalgs = salgs; |
| 2486 | c->shared_sigalgslen = nmatch; |
| 2487 | return 1; |
| 2488 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2489 | |
| 2490 | /* Set preferred digest for each key type */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2491 | int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) { |
| 2492 | CERT *c = s->cert; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2493 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2494 | /* Extension ignored for inappropriate versions */ |
| 2495 | if (!SSL_USE_SIGALGS(s)) { |
| 2496 | return 1; |
| 2497 | } |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 2498 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2499 | /* Length must be even */ |
| 2500 | if (CBS_len(sigalgs) % 2 != 0) { |
| 2501 | return 0; |
| 2502 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2503 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2504 | /* Should never happen */ |
| 2505 | if (!c) { |
| 2506 | return 0; |
| 2507 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2508 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2509 | if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen)) { |
| 2510 | return 0; |
| 2511 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2512 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2513 | tls1_set_shared_sigalgs(s); |
| 2514 | return 1; |
| 2515 | } |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 2516 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2517 | const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey) { |
| 2518 | CERT *c = s->cert; |
| 2519 | int type = EVP_PKEY_id(pkey); |
| 2520 | size_t i; |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 2521 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2522 | /* Select the first shared digest supported by our key. */ |
| 2523 | for (i = 0; i < c->shared_sigalgslen; i++) { |
| 2524 | const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash); |
| 2525 | if (md == NULL || |
| 2526 | tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type || |
| 2527 | !EVP_PKEY_supports_digest(pkey, md)) { |
| 2528 | continue; |
| 2529 | } |
| 2530 | return md; |
| 2531 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2532 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2533 | /* If no suitable digest may be found, default to SHA-1. */ |
| 2534 | return EVP_sha1(); |
| 2535 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2536 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2537 | int SSL_get_sigalgs(SSL *s, int idx, int *psign, int *phash, int *psignhash, |
| 2538 | uint8_t *rsig, uint8_t *rhash) { |
| 2539 | const uint8_t *psig = s->cert->peer_sigalgs; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2540 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2541 | if (psig == NULL) { |
| 2542 | return 0; |
| 2543 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2544 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2545 | if (idx >= 0) { |
| 2546 | idx <<= 1; |
| 2547 | if (idx >= (int)s->cert->peer_sigalgslen) { |
| 2548 | return 0; |
| 2549 | } |
| 2550 | psig += idx; |
| 2551 | if (rhash) { |
| 2552 | *rhash = psig[0]; |
| 2553 | } |
| 2554 | if (rsig) { |
| 2555 | *rsig = psig[1]; |
| 2556 | } |
| 2557 | tls1_lookup_sigalg(phash, psign, psignhash, psig); |
| 2558 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2559 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2560 | return s->cert->peer_sigalgslen / 2; |
| 2561 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2562 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2563 | int SSL_get_shared_sigalgs(SSL *s, int idx, int *psign, int *phash, |
| 2564 | int *psignhash, uint8_t *rsig, uint8_t *rhash) { |
| 2565 | TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2566 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2567 | if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen) { |
| 2568 | return 0; |
| 2569 | } |
| 2570 | |
| 2571 | shsigalgs += idx; |
| 2572 | if (phash) { |
| 2573 | *phash = shsigalgs->hash_nid; |
| 2574 | } |
| 2575 | if (psign) { |
| 2576 | *psign = shsigalgs->sign_nid; |
| 2577 | } |
| 2578 | if (psignhash) { |
| 2579 | *psignhash = shsigalgs->signandhash_nid; |
| 2580 | } |
| 2581 | if (rsig) { |
| 2582 | *rsig = shsigalgs->rsign; |
| 2583 | } |
| 2584 | if (rhash) { |
| 2585 | *rhash = shsigalgs->rhash; |
| 2586 | } |
| 2587 | |
| 2588 | return s->cert->shared_sigalgslen; |
| 2589 | } |
| 2590 | |
| 2591 | /* tls1_channel_id_hash calculates the signed data for a Channel ID on the |
| 2592 | * given SSL connection and writes it to |md|. */ |
| 2593 | int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) { |
| 2594 | EVP_MD_CTX ctx; |
| 2595 | uint8_t temp_digest[EVP_MAX_MD_SIZE]; |
| 2596 | unsigned temp_digest_len; |
| 2597 | int i; |
| 2598 | static const char kClientIDMagic[] = "TLS Channel ID signature"; |
| 2599 | |
| 2600 | if (s->s3->handshake_buffer && |
| 2601 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 2602 | return 0; |
| 2603 | } |
| 2604 | |
| 2605 | EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic)); |
| 2606 | |
| 2607 | if (s->hit && s->s3->tlsext_channel_id_new) { |
| 2608 | static const char kResumptionMagic[] = "Resumption"; |
| 2609 | EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic)); |
| 2610 | if (s->session->original_handshake_hash_len == 0) { |
| 2611 | return 0; |
| 2612 | } |
| 2613 | EVP_DigestUpdate(md, s->session->original_handshake_hash, |
| 2614 | s->session->original_handshake_hash_len); |
| 2615 | } |
| 2616 | |
| 2617 | EVP_MD_CTX_init(&ctx); |
| 2618 | for (i = 0; i < SSL_MAX_DIGEST; i++) { |
| 2619 | if (s->s3->handshake_dgst[i] == NULL) { |
| 2620 | continue; |
| 2621 | } |
| 2622 | EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]); |
| 2623 | EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len); |
| 2624 | EVP_DigestUpdate(md, temp_digest, temp_digest_len); |
| 2625 | } |
| 2626 | EVP_MD_CTX_cleanup(&ctx); |
| 2627 | |
| 2628 | return 1; |
| 2629 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2630 | |
| 2631 | /* tls1_record_handshake_hashes_for_channel_id records the current handshake |
| 2632 | * hashes in |s->session| so that Channel ID resumptions can sign that data. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2633 | int tls1_record_handshake_hashes_for_channel_id(SSL *s) { |
| 2634 | int digest_len; |
| 2635 | /* This function should never be called for a resumed session because the |
| 2636 | * handshake hashes that we wish to record are for the original, full |
| 2637 | * handshake. */ |
| 2638 | if (s->hit) { |
| 2639 | return -1; |
| 2640 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2641 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2642 | /* It only makes sense to call this function if Channel IDs have been |
| 2643 | * negotiated. */ |
| 2644 | if (!s->s3->tlsext_channel_id_new) { |
| 2645 | return -1; |
| 2646 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2647 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2648 | digest_len = |
| 2649 | tls1_handshake_digest(s, s->session->original_handshake_hash, |
| 2650 | sizeof(s->session->original_handshake_hash)); |
| 2651 | if (digest_len < 0) { |
| 2652 | return -1; |
| 2653 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2654 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2655 | s->session->original_handshake_hash_len = digest_len; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2656 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2657 | return 1; |
| 2658 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2659 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2660 | int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, |
| 2661 | int client) { |
| 2662 | uint8_t *sigalgs, *sptr; |
| 2663 | int rhash, rsign; |
| 2664 | size_t i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2665 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2666 | if (salglen & 1) { |
| 2667 | return 0; |
| 2668 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2669 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2670 | sigalgs = OPENSSL_malloc(salglen); |
| 2671 | if (sigalgs == NULL) { |
| 2672 | return 0; |
| 2673 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2674 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2675 | for (i = 0, sptr = sigalgs; i < salglen; i += 2) { |
| 2676 | rhash = tls12_find_id(*psig_nids++, tls12_md, |
| 2677 | sizeof(tls12_md) / sizeof(tls12_lookup)); |
| 2678 | rsign = tls12_find_id(*psig_nids++, tls12_sig, |
| 2679 | sizeof(tls12_sig) / sizeof(tls12_lookup)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2680 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2681 | if (rhash == -1 || rsign == -1) { |
| 2682 | goto err; |
| 2683 | } |
| 2684 | *sptr++ = rhash; |
| 2685 | *sptr++ = rsign; |
| 2686 | } |
| 2687 | |
| 2688 | if (client) { |
| 2689 | if (c->client_sigalgs) { |
| 2690 | OPENSSL_free(c->client_sigalgs); |
| 2691 | } |
| 2692 | c->client_sigalgs = sigalgs; |
| 2693 | c->client_sigalgslen = salglen; |
| 2694 | } else { |
| 2695 | if (c->conf_sigalgs) { |
| 2696 | OPENSSL_free(c->conf_sigalgs); |
| 2697 | } |
| 2698 | c->conf_sigalgs = sigalgs; |
| 2699 | c->conf_sigalgslen = salglen; |
| 2700 | } |
| 2701 | |
| 2702 | return 1; |
| 2703 | |
| 2704 | err: |
| 2705 | OPENSSL_free(sigalgs); |
| 2706 | return 0; |
| 2707 | } |