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