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