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