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