2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
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"
120 const char tls1_version_str[]="TLSv1" OPENSSL_VERSION_PTEXT;
122 #ifndef OPENSSL_NO_TLSEXT
123 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
124 const unsigned char *sess_id, int sesslen,
125 SSL_SESSION **psess);
128 SSL3_ENC_METHOD TLSv1_enc_data={
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,
140 tls1_export_keying_material,
143 long tls1_default_timeout(void)
145 /* 2 hours, the 24 hours mentioned in the TLSv1 spec
146 * is way too long for http, the cache would over fill */
152 if (!ssl3_new(s)) return(0);
153 s->method->ssl_clear(s);
157 void tls1_free(SSL *s)
159 #ifndef OPENSSL_NO_TLSEXT
160 if (s->tlsext_session_ticket)
162 OPENSSL_free(s->tlsext_session_ticket);
164 #endif /* OPENSSL_NO_TLSEXT */
168 void tls1_clear(SSL *s)
171 s->version = s->method->version;
174 #ifndef OPENSSL_NO_EC
176 static int nid_list[] =
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) */
205 static int pref_list[] =
207 NID_sect571r1, /* sect571r1 (14) */
208 NID_sect571k1, /* sect571k1 (13) */
209 NID_secp521r1, /* secp521r1 (25) */
210 NID_sect409k1, /* sect409k1 (11) */
211 NID_sect409r1, /* sect409r1 (12) */
212 NID_secp384r1, /* secp384r1 (24) */
213 NID_sect283k1, /* sect283k1 (9) */
214 NID_sect283r1, /* sect283r1 (10) */
215 NID_secp256k1, /* secp256k1 (22) */
216 NID_X9_62_prime256v1, /* secp256r1 (23) */
217 NID_sect239k1, /* sect239k1 (8) */
218 NID_sect233k1, /* sect233k1 (6) */
219 NID_sect233r1, /* sect233r1 (7) */
220 NID_secp224k1, /* secp224k1 (20) */
221 NID_secp224r1, /* secp224r1 (21) */
222 NID_sect193r1, /* sect193r1 (4) */
223 NID_sect193r2, /* sect193r2 (5) */
224 NID_secp192k1, /* secp192k1 (18) */
225 NID_X9_62_prime192v1, /* secp192r1 (19) */
226 NID_sect163k1, /* sect163k1 (1) */
227 NID_sect163r1, /* sect163r1 (2) */
228 NID_sect163r2, /* sect163r2 (3) */
229 NID_secp160k1, /* secp160k1 (15) */
230 NID_secp160r1, /* secp160r1 (16) */
231 NID_secp160r2, /* secp160r2 (17) */
234 int tls1_ec_curve_id2nid(int curve_id)
236 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
237 if ((curve_id < 1) || ((unsigned int)curve_id >
238 sizeof(nid_list)/sizeof(nid_list[0])))
240 return nid_list[curve_id-1];
243 int tls1_ec_nid2curve_id(int nid)
245 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
248 case NID_sect163k1: /* sect163k1 (1) */
250 case NID_sect163r1: /* sect163r1 (2) */
252 case NID_sect163r2: /* sect163r2 (3) */
254 case NID_sect193r1: /* sect193r1 (4) */
256 case NID_sect193r2: /* sect193r2 (5) */
258 case NID_sect233k1: /* sect233k1 (6) */
260 case NID_sect233r1: /* sect233r1 (7) */
262 case NID_sect239k1: /* sect239k1 (8) */
264 case NID_sect283k1: /* sect283k1 (9) */
266 case NID_sect283r1: /* sect283r1 (10) */
268 case NID_sect409k1: /* sect409k1 (11) */
270 case NID_sect409r1: /* sect409r1 (12) */
272 case NID_sect571k1: /* sect571k1 (13) */
274 case NID_sect571r1: /* sect571r1 (14) */
276 case NID_secp160k1: /* secp160k1 (15) */
278 case NID_secp160r1: /* secp160r1 (16) */
280 case NID_secp160r2: /* secp160r2 (17) */
282 case NID_secp192k1: /* secp192k1 (18) */
284 case NID_X9_62_prime192v1: /* secp192r1 (19) */
286 case NID_secp224k1: /* secp224k1 (20) */
288 case NID_secp224r1: /* secp224r1 (21) */
290 case NID_secp256k1: /* secp256k1 (22) */
292 case NID_X9_62_prime256v1: /* secp256r1 (23) */
294 case NID_secp384r1: /* secp384r1 (24) */
296 case NID_secp521r1: /* secp521r1 (25) */
302 #endif /* OPENSSL_NO_EC */
304 #ifndef OPENSSL_NO_TLSEXT
306 /* List of supported signature algorithms and hashes. Should make this
307 * customisable at some point, for now include everything we support.
310 #ifdef OPENSSL_NO_RSA
311 #define tlsext_sigalg_rsa(md) /* */
313 #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
316 #ifdef OPENSSL_NO_DSA
317 #define tlsext_sigalg_dsa(md) /* */
319 #define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
322 #ifdef OPENSSL_NO_ECDSA
323 #define tlsext_sigalg_ecdsa(md) /* */
325 #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
328 #define tlsext_sigalg(md) \
329 tlsext_sigalg_rsa(md) \
330 tlsext_sigalg_dsa(md) \
331 tlsext_sigalg_ecdsa(md)
333 static unsigned char tls12_sigalgs[] = {
334 #ifndef OPENSSL_NO_SHA512
335 tlsext_sigalg(TLSEXT_hash_sha512)
336 tlsext_sigalg(TLSEXT_hash_sha384)
338 #ifndef OPENSSL_NO_SHA256
339 tlsext_sigalg(TLSEXT_hash_sha256)
340 tlsext_sigalg(TLSEXT_hash_sha224)
342 #ifndef OPENSSL_NO_SHA
343 tlsext_sigalg(TLSEXT_hash_sha1)
347 int tls12_get_req_sig_algs(SSL *s, unsigned char *p)
349 size_t slen = sizeof(tls12_sigalgs);
351 memcpy(p, tls12_sigalgs, slen);
355 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
358 unsigned char *ret = p;
360 /* don't add extensions for SSLv3 unless doing secure renegotiation */
361 if (s->client_version == SSL3_VERSION
362 && !s->s3->send_connection_binding)
367 if (ret>=limit) return NULL; /* this really never occurs, but ... */
369 if (s->tlsext_hostname != NULL)
371 /* Add TLS extension servername to the Client Hello message */
372 unsigned long size_str;
375 /* check for enough space.
376 4 for the servername type and entension length
377 2 for servernamelist length
378 1 for the hostname type
379 2 for hostname length
383 if ((lenmax = limit - ret - 9) < 0
384 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
387 /* extension type and length */
388 s2n(TLSEXT_TYPE_server_name,ret);
391 /* length of servername list */
394 /* hostname type, length and hostname */
395 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
397 memcpy(ret, s->tlsext_hostname, size_str);
401 /* Add RI if renegotiating */
406 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
408 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
412 if((limit - p - 4 - el) < 0) return NULL;
414 s2n(TLSEXT_TYPE_renegotiate,ret);
417 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
419 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
426 #ifndef OPENSSL_NO_SRP
427 /* Add SRP username if there is one */
428 if (s->srp_ctx.login != NULL)
429 { /* Add TLS extension SRP username to the Client Hello message */
431 int login_len = strlen(s->srp_ctx.login);
432 if (login_len > 255 || login_len == 0)
434 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
438 /* check for enough space.
439 4 for the srp type type and entension length
440 1 for the srp user identity
441 + srp user identity length
443 if ((limit - ret - 5 - login_len) < 0) return NULL;
445 /* fill in the extension */
446 s2n(TLSEXT_TYPE_srp,ret);
447 s2n(login_len+1,ret);
448 (*ret++) = (unsigned char) login_len;
449 memcpy(ret, s->srp_ctx.login, login_len);
454 #ifndef OPENSSL_NO_EC
455 if (s->tlsext_ecpointformatlist != NULL &&
456 s->version != DTLS1_VERSION)
458 /* Add TLS extension ECPointFormats to the ClientHello message */
461 if ((lenmax = limit - ret - 5) < 0) return NULL;
462 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
463 if (s->tlsext_ecpointformatlist_length > 255)
465 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
469 s2n(TLSEXT_TYPE_ec_point_formats,ret);
470 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
471 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
472 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
473 ret+=s->tlsext_ecpointformatlist_length;
475 if (s->tlsext_ellipticcurvelist != NULL &&
476 s->version != DTLS1_VERSION)
478 /* Add TLS extension EllipticCurves to the ClientHello message */
481 if ((lenmax = limit - ret - 6) < 0) return NULL;
482 if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax) return NULL;
483 if (s->tlsext_ellipticcurvelist_length > 65532)
485 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
489 s2n(TLSEXT_TYPE_elliptic_curves,ret);
490 s2n(s->tlsext_ellipticcurvelist_length + 2, ret);
492 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
493 * elliptic_curve_list, but the examples use two bytes.
494 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
495 * resolves this to two bytes.
497 s2n(s->tlsext_ellipticcurvelist_length, ret);
498 memcpy(ret, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length);
499 ret+=s->tlsext_ellipticcurvelist_length;
501 #endif /* OPENSSL_NO_EC */
503 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
506 if (!s->new_session && s->session && s->session->tlsext_tick)
507 ticklen = s->session->tlsext_ticklen;
508 else if (s->session && s->tlsext_session_ticket &&
509 s->tlsext_session_ticket->data)
511 ticklen = s->tlsext_session_ticket->length;
512 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
513 if (!s->session->tlsext_tick)
515 memcpy(s->session->tlsext_tick,
516 s->tlsext_session_ticket->data,
518 s->session->tlsext_ticklen = ticklen;
522 if (ticklen == 0 && s->tlsext_session_ticket &&
523 s->tlsext_session_ticket->data == NULL)
525 /* Check for enough room 2 for extension type, 2 for len
528 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
529 s2n(TLSEXT_TYPE_session_ticket,ret);
533 memcpy(ret, s->session->tlsext_tick, ticklen);
539 if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
541 if ((size_t)(limit - ret) < sizeof(tls12_sigalgs) + 6)
543 s2n(TLSEXT_TYPE_signature_algorithms,ret);
544 s2n(sizeof(tls12_sigalgs) + 2, ret);
545 s2n(sizeof(tls12_sigalgs), ret);
546 memcpy(ret, tls12_sigalgs, sizeof(tls12_sigalgs));
547 ret += sizeof(tls12_sigalgs);
550 #ifdef TLSEXT_TYPE_opaque_prf_input
551 if (s->s3->client_opaque_prf_input != NULL &&
552 s->version != DTLS1_VERSION)
554 size_t col = s->s3->client_opaque_prf_input_len;
556 if ((long)(limit - ret - 6 - col < 0))
558 if (col > 0xFFFD) /* can't happen */
561 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
564 memcpy(ret, s->s3->client_opaque_prf_input, col);
569 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
570 s->version != DTLS1_VERSION)
573 long extlen, idlen, itmp;
577 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
579 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
580 itmp = i2d_OCSP_RESPID(id, NULL);
586 if (s->tlsext_ocsp_exts)
588 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
595 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
596 s2n(TLSEXT_TYPE_status_request, ret);
597 if (extlen + idlen > 0xFFF0)
599 s2n(extlen + idlen + 5, ret);
600 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
602 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
604 /* save position of id len */
605 unsigned char *q = ret;
606 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
607 /* skip over id len */
609 itmp = i2d_OCSP_RESPID(id, &ret);
615 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
618 #ifndef OPENSSL_NO_HEARTBEATS
619 /* Add Heartbeat extension */
620 s2n(TLSEXT_TYPE_heartbeat,ret);
623 * 1: peer may send requests
624 * 2: peer not allowed to send requests
626 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
627 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
629 *(ret++) = SSL_TLSEXT_HB_ENABLED;
632 #ifndef OPENSSL_NO_NEXTPROTONEG
633 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
635 /* The client advertises an emtpy extension to indicate its
636 * support for Next Protocol Negotiation */
637 if (limit - ret - 4 < 0)
639 s2n(TLSEXT_TYPE_next_proto_neg,ret);
644 #ifndef OPENSSL_NO_SRTP
645 if(SSL_get_srtp_profiles(s))
649 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
651 if((limit - p - 4 - el) < 0) return NULL;
653 s2n(TLSEXT_TYPE_use_srtp,ret);
656 if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
658 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
665 #ifdef TLSEXT_TYPE_padding
666 /* Add padding to workaround bugs in F5 terminators.
667 * See https://tools.ietf.org/html/draft-agl-tls-padding-02
669 * NB: because this code works out the length of all existing
670 * extensions it MUST always appear last.
673 int hlen = ret - (unsigned char *)s->init_buf->data;
674 /* The code in s23_clnt.c to build ClientHello messages includes the
675 * 5-byte record header in the buffer, while the code in s3_clnt.c does
677 if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
679 if (hlen > 0xff && hlen < 0x200)
687 s2n(TLSEXT_TYPE_padding, ret);
689 memset(ret, 0, hlen);
695 if ((extdatalen = ret-p-2)== 0)
702 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
705 unsigned char *ret = p;
706 #ifndef OPENSSL_NO_NEXTPROTONEG
707 int next_proto_neg_seen;
710 /* don't add extensions for SSLv3, unless doing secure renegotiation */
711 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
715 if (ret>=limit) return NULL; /* this really never occurs, but ... */
717 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
719 if ((long)(limit - ret - 4) < 0) return NULL;
721 s2n(TLSEXT_TYPE_server_name,ret);
725 if(s->s3->send_connection_binding)
729 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
731 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
735 if((limit - p - 4 - el) < 0) return NULL;
737 s2n(TLSEXT_TYPE_renegotiate,ret);
740 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
742 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
749 #ifndef OPENSSL_NO_EC
750 if (s->tlsext_ecpointformatlist != NULL &&
751 s->version != DTLS1_VERSION)
753 /* Add TLS extension ECPointFormats to the ServerHello message */
756 if ((lenmax = limit - ret - 5) < 0) return NULL;
757 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
758 if (s->tlsext_ecpointformatlist_length > 255)
760 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
764 s2n(TLSEXT_TYPE_ec_point_formats,ret);
765 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
766 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
767 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
768 ret+=s->tlsext_ecpointformatlist_length;
771 /* Currently the server should not respond with a SupportedCurves extension */
772 #endif /* OPENSSL_NO_EC */
774 if (s->tlsext_ticket_expected
775 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
777 if ((long)(limit - ret - 4) < 0) return NULL;
778 s2n(TLSEXT_TYPE_session_ticket,ret);
782 if (s->tlsext_status_expected)
784 if ((long)(limit - ret - 4) < 0) return NULL;
785 s2n(TLSEXT_TYPE_status_request,ret);
789 #ifdef TLSEXT_TYPE_opaque_prf_input
790 if (s->s3->server_opaque_prf_input != NULL &&
791 s->version != DTLS1_VERSION)
793 size_t sol = s->s3->server_opaque_prf_input_len;
795 if ((long)(limit - ret - 6 - sol) < 0)
797 if (sol > 0xFFFD) /* can't happen */
800 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
803 memcpy(ret, s->s3->server_opaque_prf_input, sol);
808 #ifndef OPENSSL_NO_SRTP
813 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
815 if((limit - p - 4 - el) < 0) return NULL;
817 s2n(TLSEXT_TYPE_use_srtp,ret);
820 if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
822 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
829 if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81)
830 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
831 { const unsigned char cryptopro_ext[36] = {
832 0xfd, 0xe8, /*65000*/
833 0x00, 0x20, /*32 bytes length*/
834 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
835 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
836 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
837 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17};
838 if (limit-ret<36) return NULL;
839 memcpy(ret,cryptopro_ext,36);
844 #ifndef OPENSSL_NO_HEARTBEATS
845 /* Add Heartbeat extension if we've received one */
846 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED)
848 s2n(TLSEXT_TYPE_heartbeat,ret);
851 * 1: peer may send requests
852 * 2: peer not allowed to send requests
854 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
855 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
857 *(ret++) = SSL_TLSEXT_HB_ENABLED;
862 #ifndef OPENSSL_NO_NEXTPROTONEG
863 next_proto_neg_seen = s->s3->next_proto_neg_seen;
864 s->s3->next_proto_neg_seen = 0;
865 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
867 const unsigned char *npa;
871 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
872 if (r == SSL_TLSEXT_ERR_OK)
874 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
875 s2n(TLSEXT_TYPE_next_proto_neg,ret);
877 memcpy(ret, npa, npalen);
879 s->s3->next_proto_neg_seen = 1;
884 if ((extdatalen = ret-p-2)== 0)
891 #ifndef OPENSSL_NO_EC
892 /* ssl_check_for_safari attempts to fingerprint Safari using OS X
893 * SecureTransport using the TLS extension block in |d|, of length |n|.
894 * Safari, since 10.6, sends exactly these extensions, in this order:
899 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
900 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
901 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
902 * 10.8..10.8.3 (which don't work).
904 static void ssl_check_for_safari(SSL *s, const unsigned char *data, const unsigned char *d, int n) {
905 unsigned short type, size;
906 static const unsigned char kSafariExtensionsBlock[] = {
907 0x00, 0x0a, /* elliptic_curves extension */
908 0x00, 0x08, /* 8 bytes */
909 0x00, 0x06, /* 6 bytes of curve ids */
910 0x00, 0x17, /* P-256 */
911 0x00, 0x18, /* P-384 */
912 0x00, 0x19, /* P-521 */
914 0x00, 0x0b, /* ec_point_formats */
915 0x00, 0x02, /* 2 bytes */
916 0x01, /* 1 point format */
917 0x00, /* uncompressed */
920 /* The following is only present in TLS 1.2 */
921 static const unsigned char kSafariTLS12ExtensionsBlock[] = {
922 0x00, 0x0d, /* signature_algorithms */
923 0x00, 0x0c, /* 12 bytes */
924 0x00, 0x0a, /* 10 bytes */
925 0x05, 0x01, /* SHA-384/RSA */
926 0x04, 0x01, /* SHA-256/RSA */
927 0x02, 0x01, /* SHA-1/RSA */
928 0x04, 0x03, /* SHA-256/ECDSA */
929 0x02, 0x03, /* SHA-1/ECDSA */
941 if (type != TLSEXT_TYPE_server_name)
948 if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
950 const size_t len1 = sizeof(kSafariExtensionsBlock);
951 const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
953 if (data + len1 + len2 != d+n)
955 if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
957 if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
962 const size_t len = sizeof(kSafariExtensionsBlock);
964 if (data + len != d+n)
966 if (memcmp(data, kSafariExtensionsBlock, len) != 0)
970 s->s3->is_probably_safari = 1;
972 #endif /* !OPENSSL_NO_EC */
974 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
979 unsigned char *data = *p;
980 int renegotiate_seen = 0;
983 s->servername_done = 0;
984 s->tlsext_status_type = -1;
985 #ifndef OPENSSL_NO_NEXTPROTONEG
986 s->s3->next_proto_neg_seen = 0;
989 #ifndef OPENSSL_NO_HEARTBEATS
990 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
991 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
994 #ifndef OPENSSL_NO_EC
995 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
996 ssl_check_for_safari(s, data, d, n);
997 #endif /* !OPENSSL_NO_EC */
1003 if (data > (d+n-len))
1006 while (data <= (d+n-4))
1011 if (data+size > (d+n))
1014 fprintf(stderr,"Received extension type %d size %d\n",type,size);
1016 if (s->tlsext_debug_cb)
1017 s->tlsext_debug_cb(s, 0, type, data, size,
1018 s->tlsext_debug_arg);
1019 /* The servername extension is treated as follows:
1021 - Only the hostname type is supported with a maximum length of 255.
1022 - The servername is rejected if too long or if it contains zeros,
1023 in which case an fatal alert is generated.
1024 - The servername field is maintained together with the session cache.
1025 - When a session is resumed, the servername call back invoked in order
1026 to allow the application to position itself to the right context.
1027 - The servername is acknowledged if it is new for a session or when
1028 it is identical to a previously used for the same session.
1029 Applications can control the behaviour. They can at any time
1030 set a 'desirable' servername for a new SSL object. This can be the
1031 case for example with HTTPS when a Host: header field is received and
1032 a renegotiation is requested. In this case, a possible servername
1033 presented in the new client hello is only acknowledged if it matches
1034 the value of the Host: field.
1035 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1036 if they provide for changing an explicit servername context for the session,
1037 i.e. when the session has been established with a servername extension.
1038 - On session reconnect, the servername extension may be absent.
1042 if (type == TLSEXT_TYPE_server_name)
1044 unsigned char *sdata;
1050 *al = SSL_AD_DECODE_ERROR;
1057 *al = SSL_AD_DECODE_ERROR;
1064 servname_type = *(sdata++);
1070 *al = SSL_AD_DECODE_ERROR;
1073 if (s->servername_done == 0)
1074 switch (servname_type)
1076 case TLSEXT_NAMETYPE_host_name:
1079 if(s->session->tlsext_hostname)
1081 *al = SSL_AD_DECODE_ERROR;
1084 if (len > TLSEXT_MAXLEN_host_name)
1086 *al = TLS1_AD_UNRECOGNIZED_NAME;
1089 if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
1091 *al = TLS1_AD_INTERNAL_ERROR;
1094 memcpy(s->session->tlsext_hostname, sdata, len);
1095 s->session->tlsext_hostname[len]='\0';
1096 if (strlen(s->session->tlsext_hostname) != len) {
1097 OPENSSL_free(s->session->tlsext_hostname);
1098 s->session->tlsext_hostname = NULL;
1099 *al = TLS1_AD_UNRECOGNIZED_NAME;
1102 s->servername_done = 1;
1106 s->servername_done = s->session->tlsext_hostname
1107 && strlen(s->session->tlsext_hostname) == len
1108 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
1120 *al = SSL_AD_DECODE_ERROR;
1125 #ifndef OPENSSL_NO_SRP
1126 else if (type == TLSEXT_TYPE_srp)
1128 if (size <= 0 || ((len = data[0])) != (size -1))
1130 *al = SSL_AD_DECODE_ERROR;
1133 if (s->srp_ctx.login != NULL)
1135 *al = SSL_AD_DECODE_ERROR;
1138 if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL)
1140 memcpy(s->srp_ctx.login, &data[1], len);
1141 s->srp_ctx.login[len]='\0';
1143 if (strlen(s->srp_ctx.login) != len)
1145 *al = SSL_AD_DECODE_ERROR;
1151 #ifndef OPENSSL_NO_EC
1152 else if (type == TLSEXT_TYPE_ec_point_formats &&
1153 s->version != DTLS1_VERSION)
1155 unsigned char *sdata = data;
1156 int ecpointformatlist_length = *(sdata++);
1158 if (ecpointformatlist_length != size - 1)
1160 *al = TLS1_AD_DECODE_ERROR;
1165 if(s->session->tlsext_ecpointformatlist)
1167 OPENSSL_free(s->session->tlsext_ecpointformatlist);
1168 s->session->tlsext_ecpointformatlist = NULL;
1170 s->session->tlsext_ecpointformatlist_length = 0;
1171 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
1173 *al = TLS1_AD_INTERNAL_ERROR;
1176 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
1177 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
1180 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
1181 sdata = s->session->tlsext_ecpointformatlist;
1182 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
1183 fprintf(stderr,"%i ",*(sdata++));
1184 fprintf(stderr,"\n");
1187 else if (type == TLSEXT_TYPE_elliptic_curves &&
1188 s->version != DTLS1_VERSION)
1190 unsigned char *sdata = data;
1191 int ellipticcurvelist_length = (*(sdata++) << 8);
1192 ellipticcurvelist_length += (*(sdata++));
1194 if (ellipticcurvelist_length != size - 2 ||
1195 ellipticcurvelist_length < 1)
1197 *al = TLS1_AD_DECODE_ERROR;
1202 if(s->session->tlsext_ellipticcurvelist)
1204 *al = TLS1_AD_DECODE_ERROR;
1207 s->session->tlsext_ellipticcurvelist_length = 0;
1208 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
1210 *al = TLS1_AD_INTERNAL_ERROR;
1213 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
1214 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
1217 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
1218 sdata = s->session->tlsext_ellipticcurvelist;
1219 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
1220 fprintf(stderr,"%i ",*(sdata++));
1221 fprintf(stderr,"\n");
1224 #endif /* OPENSSL_NO_EC */
1225 #ifdef TLSEXT_TYPE_opaque_prf_input
1226 else if (type == TLSEXT_TYPE_opaque_prf_input &&
1227 s->version != DTLS1_VERSION)
1229 unsigned char *sdata = data;
1233 *al = SSL_AD_DECODE_ERROR;
1236 n2s(sdata, s->s3->client_opaque_prf_input_len);
1237 if (s->s3->client_opaque_prf_input_len != size - 2)
1239 *al = SSL_AD_DECODE_ERROR;
1243 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
1244 OPENSSL_free(s->s3->client_opaque_prf_input);
1245 if (s->s3->client_opaque_prf_input_len == 0)
1246 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
1248 s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
1249 if (s->s3->client_opaque_prf_input == NULL)
1251 *al = TLS1_AD_INTERNAL_ERROR;
1256 else if (type == TLSEXT_TYPE_session_ticket)
1258 if (s->tls_session_ticket_ext_cb &&
1259 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
1261 *al = TLS1_AD_INTERNAL_ERROR;
1265 else if (type == TLSEXT_TYPE_renegotiate)
1267 if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
1269 renegotiate_seen = 1;
1271 else if (type == TLSEXT_TYPE_signature_algorithms)
1274 if (sigalg_seen || size < 2)
1276 *al = SSL_AD_DECODE_ERROR;
1282 if (dsize != size || dsize & 1)
1284 *al = SSL_AD_DECODE_ERROR;
1287 if (!tls1_process_sigalgs(s, data, dsize))
1289 *al = SSL_AD_DECODE_ERROR;
1293 else if (type == TLSEXT_TYPE_status_request &&
1294 s->version != DTLS1_VERSION)
1299 *al = SSL_AD_DECODE_ERROR;
1303 s->tlsext_status_type = *data++;
1305 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
1307 const unsigned char *sdata;
1309 /* Read in responder_id_list */
1314 *al = SSL_AD_DECODE_ERROR;
1323 *al = SSL_AD_DECODE_ERROR;
1327 dsize -= 2 + idsize;
1331 *al = SSL_AD_DECODE_ERROR;
1336 id = d2i_OCSP_RESPID(NULL,
1340 *al = SSL_AD_DECODE_ERROR;
1345 OCSP_RESPID_free(id);
1346 *al = SSL_AD_DECODE_ERROR;
1349 if (!s->tlsext_ocsp_ids
1350 && !(s->tlsext_ocsp_ids =
1351 sk_OCSP_RESPID_new_null()))
1353 OCSP_RESPID_free(id);
1354 *al = SSL_AD_INTERNAL_ERROR;
1357 if (!sk_OCSP_RESPID_push(
1358 s->tlsext_ocsp_ids, id))
1360 OCSP_RESPID_free(id);
1361 *al = SSL_AD_INTERNAL_ERROR;
1366 /* Read in request_extensions */
1369 *al = SSL_AD_DECODE_ERROR;
1376 *al = SSL_AD_DECODE_ERROR;
1382 if (s->tlsext_ocsp_exts)
1384 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
1385 X509_EXTENSION_free);
1388 s->tlsext_ocsp_exts =
1389 d2i_X509_EXTENSIONS(NULL,
1391 if (!s->tlsext_ocsp_exts
1392 || (data + dsize != sdata))
1394 *al = SSL_AD_DECODE_ERROR;
1399 /* We don't know what to do with any other type
1403 s->tlsext_status_type = -1;
1405 #ifndef OPENSSL_NO_HEARTBEATS
1406 else if (type == TLSEXT_TYPE_heartbeat)
1410 case 0x01: /* Client allows us to send HB requests */
1411 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
1413 case 0x02: /* Client doesn't accept HB requests */
1414 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
1415 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1417 default: *al = SSL_AD_ILLEGAL_PARAMETER;
1422 #ifndef OPENSSL_NO_NEXTPROTONEG
1423 else if (type == TLSEXT_TYPE_next_proto_neg &&
1424 s->s3->tmp.finish_md_len == 0)
1426 /* We shouldn't accept this extension on a
1429 * s->new_session will be set on renegotiation, but we
1430 * probably shouldn't rely that it couldn't be set on
1431 * the initial renegotation too in certain cases (when
1432 * there's some other reason to disallow resuming an
1433 * earlier session -- the current code won't be doing
1434 * anything like that, but this might change).
1436 * A valid sign that there's been a previous handshake
1437 * in this connection is if s->s3->tmp.finish_md_len >
1438 * 0. (We are talking about a check that will happen
1439 * in the Hello protocol round, well before a new
1440 * Finished message could have been computed.) */
1441 s->s3->next_proto_neg_seen = 1;
1445 /* session ticket processed earlier */
1446 #ifndef OPENSSL_NO_SRTP
1447 else if (type == TLSEXT_TYPE_use_srtp)
1449 if(ssl_parse_clienthello_use_srtp_ext(s, data, size,
1462 /* Need RI if renegotiating */
1464 if (!renegotiate_seen && s->renegotiate &&
1465 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1467 *al = SSL_AD_HANDSHAKE_FAILURE;
1468 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,
1469 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1476 #ifndef OPENSSL_NO_NEXTPROTONEG
1477 /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1478 * elements of zero length are allowed and the set of elements must exactly fill
1479 * the length of the block. */
1480 static char ssl_next_proto_validate(unsigned char *d, unsigned len)
1482 unsigned int off = 0;
1496 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
1498 unsigned short length;
1499 unsigned short type;
1500 unsigned short size;
1501 unsigned char *data = *p;
1502 int tlsext_servername = 0;
1503 int renegotiate_seen = 0;
1505 #ifndef OPENSSL_NO_NEXTPROTONEG
1506 s->s3->next_proto_neg_seen = 0;
1509 #ifndef OPENSSL_NO_HEARTBEATS
1510 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
1511 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
1514 if (data >= (d+n-2))
1518 if (data+length != d+n)
1520 *al = SSL_AD_DECODE_ERROR;
1524 while(data <= (d+n-4))
1529 if (data+size > (d+n))
1532 if (s->tlsext_debug_cb)
1533 s->tlsext_debug_cb(s, 1, type, data, size,
1534 s->tlsext_debug_arg);
1536 if (type == TLSEXT_TYPE_server_name)
1538 if (s->tlsext_hostname == NULL || size > 0)
1540 *al = TLS1_AD_UNRECOGNIZED_NAME;
1543 tlsext_servername = 1;
1546 #ifndef OPENSSL_NO_EC
1547 else if (type == TLSEXT_TYPE_ec_point_formats &&
1548 s->version != DTLS1_VERSION)
1550 unsigned char *sdata = data;
1551 int ecpointformatlist_length = *(sdata++);
1553 if (ecpointformatlist_length != size - 1 ||
1554 ecpointformatlist_length < 1)
1556 *al = TLS1_AD_DECODE_ERROR;
1559 s->session->tlsext_ecpointformatlist_length = 0;
1560 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
1561 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
1563 *al = TLS1_AD_INTERNAL_ERROR;
1566 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
1567 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
1569 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
1570 sdata = s->session->tlsext_ecpointformatlist;
1571 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
1572 fprintf(stderr,"%i ",*(sdata++));
1573 fprintf(stderr,"\n");
1576 #endif /* OPENSSL_NO_EC */
1578 else if (type == TLSEXT_TYPE_session_ticket)
1580 if (s->tls_session_ticket_ext_cb &&
1581 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
1583 *al = TLS1_AD_INTERNAL_ERROR;
1586 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
1589 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
1592 s->tlsext_ticket_expected = 1;
1594 #ifdef TLSEXT_TYPE_opaque_prf_input
1595 else if (type == TLSEXT_TYPE_opaque_prf_input &&
1596 s->version != DTLS1_VERSION)
1598 unsigned char *sdata = data;
1602 *al = SSL_AD_DECODE_ERROR;
1605 n2s(sdata, s->s3->server_opaque_prf_input_len);
1606 if (s->s3->server_opaque_prf_input_len != size - 2)
1608 *al = SSL_AD_DECODE_ERROR;
1612 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
1613 OPENSSL_free(s->s3->server_opaque_prf_input);
1614 if (s->s3->server_opaque_prf_input_len == 0)
1615 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
1617 s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
1619 if (s->s3->server_opaque_prf_input == NULL)
1621 *al = TLS1_AD_INTERNAL_ERROR;
1626 else if (type == TLSEXT_TYPE_status_request &&
1627 s->version != DTLS1_VERSION)
1629 /* MUST be empty and only sent if we've requested
1630 * a status request message.
1632 if ((s->tlsext_status_type == -1) || (size > 0))
1634 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
1637 /* Set flag to expect CertificateStatus message */
1638 s->tlsext_status_expected = 1;
1640 #ifndef OPENSSL_NO_NEXTPROTONEG
1641 else if (type == TLSEXT_TYPE_next_proto_neg &&
1642 s->s3->tmp.finish_md_len == 0)
1644 unsigned char *selected;
1645 unsigned char selected_len;
1647 /* We must have requested it. */
1648 if (s->ctx->next_proto_select_cb == NULL)
1650 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
1653 /* The data must be valid */
1654 if (!ssl_next_proto_validate(data, size))
1656 *al = TLS1_AD_DECODE_ERROR;
1659 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
1661 *al = TLS1_AD_INTERNAL_ERROR;
1664 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
1665 if (!s->next_proto_negotiated)
1667 *al = TLS1_AD_INTERNAL_ERROR;
1670 memcpy(s->next_proto_negotiated, selected, selected_len);
1671 s->next_proto_negotiated_len = selected_len;
1672 s->s3->next_proto_neg_seen = 1;
1675 else if (type == TLSEXT_TYPE_renegotiate)
1677 if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
1679 renegotiate_seen = 1;
1681 #ifndef OPENSSL_NO_HEARTBEATS
1682 else if (type == TLSEXT_TYPE_heartbeat)
1686 case 0x01: /* Server allows us to send HB requests */
1687 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
1689 case 0x02: /* Server doesn't accept HB requests */
1690 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
1691 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1693 default: *al = SSL_AD_ILLEGAL_PARAMETER;
1698 #ifndef OPENSSL_NO_SRTP
1699 else if (type == TLSEXT_TYPE_use_srtp)
1701 if(ssl_parse_serverhello_use_srtp_ext(s, data, size,
1712 *al = SSL_AD_DECODE_ERROR;
1716 if (!s->hit && tlsext_servername == 1)
1718 if (s->tlsext_hostname)
1720 if (s->session->tlsext_hostname == NULL)
1722 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
1723 if (!s->session->tlsext_hostname)
1725 *al = SSL_AD_UNRECOGNIZED_NAME;
1731 *al = SSL_AD_DECODE_ERROR;
1741 /* Determine if we need to see RI. Strictly speaking if we want to
1742 * avoid an attack we should *always* see RI even on initial server
1743 * hello because the client doesn't see any renegotiation during an
1744 * attack. However this would mean we could not connect to any server
1745 * which doesn't support RI so for the immediate future tolerate RI
1746 * absence on initial connect only.
1748 if (!renegotiate_seen
1749 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
1750 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1752 *al = SSL_AD_HANDSHAKE_FAILURE;
1753 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,
1754 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1762 int ssl_prepare_clienthello_tlsext(SSL *s)
1764 #ifndef OPENSSL_NO_EC
1765 /* If we are client and using an elliptic curve cryptography cipher suite, send the point formats
1766 * and elliptic curves we support.
1771 unsigned long alg_k, alg_a;
1772 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1774 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
1776 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1778 alg_k = c->algorithm_mkey;
1779 alg_a = c->algorithm_auth;
1780 if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe) || (alg_a & SSL_aECDSA)))
1786 using_ecc = using_ecc && (s->version >= TLS1_VERSION);
1789 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
1790 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
1792 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
1795 s->tlsext_ecpointformatlist_length = 3;
1796 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
1797 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
1798 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
1800 /* we support all named elliptic curves in draft-ietf-tls-ecc-12 */
1801 if (s->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->tlsext_ellipticcurvelist);
1802 s->tlsext_ellipticcurvelist_length = sizeof(pref_list)/sizeof(pref_list[0]) * 2;
1803 if ((s->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL)
1805 s->tlsext_ellipticcurvelist_length = 0;
1806 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
1809 for (i = 0, j = s->tlsext_ellipticcurvelist; (unsigned int)i <
1810 sizeof(pref_list)/sizeof(pref_list[0]); i++)
1812 int id = tls1_ec_nid2curve_id(pref_list[i]);
1816 #endif /* OPENSSL_NO_EC */
1818 #ifdef TLSEXT_TYPE_opaque_prf_input
1822 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
1824 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
1829 if (s->tlsext_opaque_prf_input != NULL)
1831 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
1832 OPENSSL_free(s->s3->client_opaque_prf_input);
1834 if (s->tlsext_opaque_prf_input_len == 0)
1835 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
1837 s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
1838 if (s->s3->client_opaque_prf_input == NULL)
1840 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
1843 s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
1847 /* at callback's request, insist on receiving an appropriate server opaque PRF input */
1848 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
1855 int ssl_prepare_serverhello_tlsext(SSL *s)
1857 #ifndef OPENSSL_NO_EC
1858 /* If we are server and using an ECC cipher suite, send the point formats we support
1859 * if the client sent us an ECPointsFormat extension. Note that the server is not
1860 * supposed to send an EllipticCurves extension.
1863 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1864 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1865 int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
1866 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1870 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
1871 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
1873 SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
1876 s->tlsext_ecpointformatlist_length = 3;
1877 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
1878 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
1879 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
1881 #endif /* OPENSSL_NO_EC */
1886 int ssl_check_clienthello_tlsext_early(SSL *s)
1888 int ret=SSL_TLSEXT_ERR_NOACK;
1889 int al = SSL_AD_UNRECOGNIZED_NAME;
1891 #ifndef OPENSSL_NO_EC
1892 /* The handling of the ECPointFormats extension is done elsewhere, namely in
1893 * ssl3_choose_cipher in s3_lib.c.
1895 /* The handling of the EllipticCurves extension is done elsewhere, namely in
1896 * ssl3_choose_cipher in s3_lib.c.
1900 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
1901 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
1902 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
1903 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
1905 #ifdef TLSEXT_TYPE_opaque_prf_input
1907 /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
1908 * but we might be sending an alert in response to the client hello,
1909 * so this has to happen here in
1910 * ssl_check_clienthello_tlsext_early(). */
1914 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
1916 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
1919 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1920 al = SSL_AD_INTERNAL_ERROR;
1925 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
1926 OPENSSL_free(s->s3->server_opaque_prf_input);
1927 s->s3->server_opaque_prf_input = NULL;
1929 if (s->tlsext_opaque_prf_input != NULL)
1931 if (s->s3->client_opaque_prf_input != NULL &&
1932 s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
1934 /* can only use this extension if we have a server opaque PRF input
1935 * of the same length as the client opaque PRF input! */
1937 if (s->tlsext_opaque_prf_input_len == 0)
1938 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
1940 s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
1941 if (s->s3->server_opaque_prf_input == NULL)
1943 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1944 al = SSL_AD_INTERNAL_ERROR;
1947 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
1951 if (r == 2 && s->s3->server_opaque_prf_input == NULL)
1953 /* The callback wants to enforce use of the extension,
1954 * but we can't do that with the client opaque PRF input;
1955 * abort the handshake.
1957 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1958 al = SSL_AD_HANDSHAKE_FAILURE;
1966 case SSL_TLSEXT_ERR_ALERT_FATAL:
1967 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1970 case SSL_TLSEXT_ERR_ALERT_WARNING:
1971 ssl3_send_alert(s,SSL3_AL_WARNING,al);
1974 case SSL_TLSEXT_ERR_NOACK:
1975 s->servername_done=0;
1981 int ssl_check_clienthello_tlsext_late(SSL *s)
1983 int ret = SSL_TLSEXT_ERR_OK;
1986 /* If status request then ask callback what to do.
1987 * Note: this must be called after servername callbacks in case
1988 * the certificate has changed, and must be called after the cipher
1989 * has been chosen because this may influence which certificate is sent
1991 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
1994 CERT_PKEY *certpkey;
1995 certpkey = ssl_get_server_send_pkey(s);
1996 /* If no certificate can't return certificate status */
1997 if (certpkey == NULL)
1999 s->tlsext_status_expected = 0;
2002 /* Set current certificate to one we will use so
2003 * SSL_get_certificate et al can pick it up.
2005 s->cert->key = certpkey;
2006 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2009 /* We don't want to send a status request response */
2010 case SSL_TLSEXT_ERR_NOACK:
2011 s->tlsext_status_expected = 0;
2013 /* status request response should be sent */
2014 case SSL_TLSEXT_ERR_OK:
2015 if (s->tlsext_ocsp_resp)
2016 s->tlsext_status_expected = 1;
2018 s->tlsext_status_expected = 0;
2020 /* something bad happened */
2021 case SSL_TLSEXT_ERR_ALERT_FATAL:
2022 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2023 al = SSL_AD_INTERNAL_ERROR;
2028 s->tlsext_status_expected = 0;
2033 case SSL_TLSEXT_ERR_ALERT_FATAL:
2034 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2037 case SSL_TLSEXT_ERR_ALERT_WARNING:
2038 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2046 int ssl_check_serverhello_tlsext(SSL *s)
2048 int ret=SSL_TLSEXT_ERR_NOACK;
2049 int al = SSL_AD_UNRECOGNIZED_NAME;
2051 #ifndef OPENSSL_NO_EC
2052 /* If we are client and using an elliptic curve cryptography cipher
2053 * suite, then if server returns an EC point formats lists extension
2054 * it must contain uncompressed.
2056 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2057 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2058 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
2059 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
2060 ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
2062 /* we are using an ECC cipher */
2064 unsigned char *list;
2065 int found_uncompressed = 0;
2066 list = s->session->tlsext_ecpointformatlist;
2067 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2069 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
2071 found_uncompressed = 1;
2075 if (!found_uncompressed)
2077 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2081 ret = SSL_TLSEXT_ERR_OK;
2082 #endif /* OPENSSL_NO_EC */
2084 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2085 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2086 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2087 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2089 #ifdef TLSEXT_TYPE_opaque_prf_input
2090 if (s->s3->server_opaque_prf_input_len > 0)
2092 /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
2093 * So first verify that we really have a value from the server too. */
2095 if (s->s3->server_opaque_prf_input == NULL)
2097 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2098 al = SSL_AD_HANDSHAKE_FAILURE;
2101 /* Anytime the server *has* sent an opaque PRF input, we need to check
2102 * that we have a client opaque PRF input of the same size. */
2103 if (s->s3->client_opaque_prf_input == NULL ||
2104 s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
2106 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2107 al = SSL_AD_ILLEGAL_PARAMETER;
2112 /* If we've requested certificate status and we wont get one
2115 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
2116 && s->ctx && s->ctx->tlsext_status_cb)
2119 /* Set resp to NULL, resplen to -1 so callback knows
2120 * there is no response.
2122 if (s->tlsext_ocsp_resp)
2124 OPENSSL_free(s->tlsext_ocsp_resp);
2125 s->tlsext_ocsp_resp = NULL;
2127 s->tlsext_ocsp_resplen = -1;
2128 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2131 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2132 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2136 al = SSL_AD_INTERNAL_ERROR;
2137 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2143 case SSL_TLSEXT_ERR_ALERT_FATAL:
2144 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2147 case SSL_TLSEXT_ERR_ALERT_WARNING:
2148 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2151 case SSL_TLSEXT_ERR_NOACK:
2152 s->servername_done=0;
2158 /* Since the server cache lookup is done early on in the processing of the
2159 * ClientHello, and other operations depend on the result, we need to handle
2160 * any TLS session ticket extension at the same time.
2162 * session_id: points at the session ID in the ClientHello. This code will
2163 * read past the end of this in order to parse out the session ticket
2164 * extension, if any.
2165 * len: the length of the session ID.
2166 * limit: a pointer to the first byte after the ClientHello.
2167 * ret: (output) on return, if a ticket was decrypted, then this is set to
2168 * point to the resulting session.
2170 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2171 * ciphersuite, in which case we have no use for session tickets and one will
2172 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2175 * -1: fatal error, either from parsing or decrypting the ticket.
2176 * 0: no ticket was found (or was ignored, based on settings).
2177 * 1: a zero length extension was found, indicating that the client supports
2178 * session tickets but doesn't currently have one to offer.
2179 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2180 * couldn't be decrypted because of a non-fatal error.
2181 * 3: a ticket was successfully decrypted and *ret was set.
2184 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2185 * a new session ticket to the client because the client indicated support
2186 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2187 * a session ticket or we couldn't use the one it gave us, or if
2188 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2189 * Otherwise, s->tlsext_ticket_expected is set to 0.
2191 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
2192 const unsigned char *limit, SSL_SESSION **ret)
2194 /* Point after session ID in client hello */
2195 const unsigned char *p = session_id + len;
2199 s->tlsext_ticket_expected = 0;
2201 /* If tickets disabled behave as if no ticket present
2202 * to permit stateful resumption.
2204 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2206 if ((s->version <= SSL3_VERSION) || !limit)
2210 /* Skip past DTLS cookie */
2211 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
2218 /* Skip past cipher list */
2223 /* Skip past compression algorithm list */
2228 /* Now at start of extensions */
2229 if ((p + 2) >= limit)
2232 while ((p + 4) <= limit)
2234 unsigned short type, size;
2237 if (p + size > limit)
2239 if (type == TLSEXT_TYPE_session_ticket)
2244 /* The client will accept a ticket but doesn't
2245 * currently have one. */
2246 s->tlsext_ticket_expected = 1;
2249 if (s->tls_session_secret_cb)
2251 /* Indicate that the ticket couldn't be
2252 * decrypted rather than generating the session
2253 * from ticket now, trigger abbreviated
2254 * handshake based on external mechanism to
2255 * calculate the master secret later. */
2258 r = tls_decrypt_ticket(s, p, size, session_id, len, ret);
2261 case 2: /* ticket couldn't be decrypted */
2262 s->tlsext_ticket_expected = 1;
2264 case 3: /* ticket was decrypted */
2266 case 4: /* ticket decrypted but need to renew */
2267 s->tlsext_ticket_expected = 1;
2269 default: /* fatal error */
2278 /* tls_decrypt_ticket attempts to decrypt a session ticket.
2280 * etick: points to the body of the session ticket extension.
2281 * eticklen: the length of the session tickets extenion.
2282 * sess_id: points at the session ID.
2283 * sesslen: the length of the session ID.
2284 * psess: (output) on return, if a ticket was decrypted, then this is set to
2285 * point to the resulting session.
2288 * -1: fatal error, either from parsing or decrypting the ticket.
2289 * 2: the ticket couldn't be decrypted.
2290 * 3: a ticket was successfully decrypted and *psess was set.
2291 * 4: same as 3, but the ticket needs to be renewed.
2293 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
2294 const unsigned char *sess_id, int sesslen,
2295 SSL_SESSION **psess)
2298 unsigned char *sdec;
2299 const unsigned char *p;
2300 int slen, mlen, renew_ticket = 0;
2301 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2304 SSL_CTX *tctx = s->initial_ctx;
2305 /* Need at least keyname + iv + some encrypted data */
2308 /* Initialize session ticket encryption and HMAC contexts */
2309 HMAC_CTX_init(&hctx);
2310 EVP_CIPHER_CTX_init(&ctx);
2311 if (tctx->tlsext_ticket_key_cb)
2313 unsigned char *nctick = (unsigned char *)etick;
2314 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2325 /* Check key name matches */
2326 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2328 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2329 tlsext_tick_md(), NULL);
2330 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2331 tctx->tlsext_tick_aes_key, etick + 16);
2333 /* Attempt to process session ticket, first conduct sanity and
2334 * integrity checks on ticket.
2336 mlen = HMAC_size(&hctx);
2339 EVP_CIPHER_CTX_cleanup(&ctx);
2343 /* Check HMAC of encrypted ticket */
2344 HMAC_Update(&hctx, etick, eticklen);
2345 HMAC_Final(&hctx, tick_hmac, NULL);
2346 HMAC_CTX_cleanup(&hctx);
2347 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
2349 /* Attempt to decrypt session data */
2350 /* Move p after IV to start of encrypted ticket, update length */
2351 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2352 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2353 sdec = OPENSSL_malloc(eticklen);
2356 EVP_CIPHER_CTX_cleanup(&ctx);
2359 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2360 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
2363 EVP_CIPHER_CTX_cleanup(&ctx);
2366 sess = d2i_SSL_SESSION(NULL, &p, slen);
2370 /* The session ID, if non-empty, is used by some clients to
2371 * detect that the ticket has been accepted. So we copy it to
2372 * the session structure. If it is empty set length to zero
2373 * as required by standard.
2376 memcpy(sess->session_id, sess_id, sesslen);
2377 sess->session_id_length = sesslen;
2385 /* For session parse failure, indicate that we need to send a new
2390 /* Tables to translate from NIDs to TLS v1.2 ids */
2398 static tls12_lookup tls12_md[] = {
2399 #ifndef OPENSSL_NO_MD5
2400 {NID_md5, TLSEXT_hash_md5},
2402 #ifndef OPENSSL_NO_SHA
2403 {NID_sha1, TLSEXT_hash_sha1},
2405 #ifndef OPENSSL_NO_SHA256
2406 {NID_sha224, TLSEXT_hash_sha224},
2407 {NID_sha256, TLSEXT_hash_sha256},
2409 #ifndef OPENSSL_NO_SHA512
2410 {NID_sha384, TLSEXT_hash_sha384},
2411 {NID_sha512, TLSEXT_hash_sha512}
2415 static tls12_lookup tls12_sig[] = {
2416 #ifndef OPENSSL_NO_RSA
2417 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
2419 #ifndef OPENSSL_NO_DSA
2420 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
2422 #ifndef OPENSSL_NO_ECDSA
2423 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2427 static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
2430 for (i = 0; i < tlen; i++)
2432 if (table[i].nid == nid)
2438 static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
2441 for (i = 0; i < tlen; i++)
2443 if (table[i].id == id)
2444 return table[i].nid;
2450 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
2455 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2456 sizeof(tls12_md)/sizeof(tls12_lookup));
2459 sig_id = tls12_get_sigid(pk);
2462 p[0] = (unsigned char)md_id;
2463 p[1] = (unsigned char)sig_id;
2467 int tls12_get_sigid(const EVP_PKEY *pk)
2469 return tls12_find_id(pk->type, tls12_sig,
2470 sizeof(tls12_sig)/sizeof(tls12_lookup));
2473 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
2477 #ifndef OPENSSL_NO_SHA
2478 case TLSEXT_hash_sha1:
2481 #ifndef OPENSSL_NO_SHA256
2482 case TLSEXT_hash_sha224:
2483 return EVP_sha224();
2485 case TLSEXT_hash_sha256:
2486 return EVP_sha256();
2488 #ifndef OPENSSL_NO_SHA512
2489 case TLSEXT_hash_sha384:
2490 return EVP_sha384();
2492 case TLSEXT_hash_sha512:
2493 return EVP_sha512();
2501 /* Set preferred digest for each key type */
2503 int tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize)
2508 /* Extension ignored for TLS versions below 1.2 */
2509 if (TLS1_get_version(s) < TLS1_2_VERSION)
2511 /* Should never happen */
2515 c->pkeys[SSL_PKEY_DSA_SIGN].digest = NULL;
2516 c->pkeys[SSL_PKEY_RSA_SIGN].digest = NULL;
2517 c->pkeys[SSL_PKEY_RSA_ENC].digest = NULL;
2518 c->pkeys[SSL_PKEY_ECC].digest = NULL;
2520 for (i = 0; i < dsize; i += 2)
2522 unsigned char hash_alg = data[i], sig_alg = data[i+1];
2526 #ifndef OPENSSL_NO_RSA
2527 case TLSEXT_signature_rsa:
2528 idx = SSL_PKEY_RSA_SIGN;
2531 #ifndef OPENSSL_NO_DSA
2532 case TLSEXT_signature_dsa:
2533 idx = SSL_PKEY_DSA_SIGN;
2536 #ifndef OPENSSL_NO_ECDSA
2537 case TLSEXT_signature_ecdsa:
2545 if (c->pkeys[idx].digest == NULL)
2547 md = tls12_get_hash(hash_alg);
2550 c->pkeys[idx].digest = md;
2551 if (idx == SSL_PKEY_RSA_SIGN)
2552 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2559 /* Set any remaining keys to default values. NOTE: if alg is not
2560 * supported it stays as NULL.
2562 #ifndef OPENSSL_NO_DSA
2563 if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest)
2564 c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
2566 #ifndef OPENSSL_NO_RSA
2567 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
2569 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
2570 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
2573 #ifndef OPENSSL_NO_ECDSA
2574 if (!c->pkeys[SSL_PKEY_ECC].digest)
2575 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
2582 #ifndef OPENSSL_NO_HEARTBEATS
2584 tls1_process_heartbeat(SSL *s)
2586 unsigned char *p = &s->s3->rrec.data[0], *pl;
2587 unsigned short hbtype;
2588 unsigned int payload;
2589 unsigned int padding = 16; /* Use minimum padding */
2591 /* Read type and payload length first */
2596 if (s->msg_callback)
2597 s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
2598 &s->s3->rrec.data[0], s->s3->rrec.length,
2599 s, s->msg_callback_arg);
2601 if (hbtype == TLS1_HB_REQUEST)
2603 unsigned char *buffer, *bp;
2606 /* Allocate memory for the response, size is 1 bytes
2607 * message type, plus 2 bytes payload length, plus
2608 * payload, plus padding
2610 buffer = OPENSSL_malloc(1 + 2 + payload + padding);
2613 /* Enter response type, length and copy payload */
2614 *bp++ = TLS1_HB_RESPONSE;
2616 memcpy(bp, pl, payload);
2618 /* Random padding */
2619 RAND_pseudo_bytes(bp, padding);
2621 r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer, 3 + payload + padding);
2623 if (r >= 0 && s->msg_callback)
2624 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
2625 buffer, 3 + payload + padding,
2626 s, s->msg_callback_arg);
2628 OPENSSL_free(buffer);
2633 else if (hbtype == TLS1_HB_RESPONSE)
2637 /* We only send sequence numbers (2 bytes unsigned int),
2638 * and 16 random bytes, so we just try to read the
2639 * sequence number */
2642 if (payload == 18 && seq == s->tlsext_hb_seq)
2645 s->tlsext_hb_pending = 0;
2653 tls1_heartbeat(SSL *s)
2655 unsigned char *buf, *p;
2657 unsigned int payload = 18; /* Sequence number + random bytes */
2658 unsigned int padding = 16; /* Use minimum padding */
2660 /* Only send if peer supports and accepts HB requests... */
2661 if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) ||
2662 s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS)
2664 SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT);
2668 /* ...and there is none in flight yet... */
2669 if (s->tlsext_hb_pending)
2671 SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PENDING);
2675 /* ...and no handshake in progress. */
2676 if (SSL_in_init(s) || s->in_handshake)
2678 SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_UNEXPECTED_MESSAGE);
2682 /* Check if padding is too long, payload and padding
2683 * must not exceed 2^14 - 3 = 16381 bytes in total.
2685 OPENSSL_assert(payload + padding <= 16381);
2687 /* Create HeartBeat message, we just use a sequence number
2688 * as payload to distuingish different messages and add
2689 * some random stuff.
2690 * - Message Type, 1 byte
2691 * - Payload Length, 2 bytes (unsigned int)
2692 * - Payload, the sequence number (2 bytes uint)
2693 * - Payload, random bytes (16 bytes uint)
2696 buf = OPENSSL_malloc(1 + 2 + payload + padding);
2699 *p++ = TLS1_HB_REQUEST;
2700 /* Payload length (18 bytes here) */
2702 /* Sequence number */
2703 s2n(s->tlsext_hb_seq, p);
2704 /* 16 random bytes */
2705 RAND_pseudo_bytes(p, 16);
2707 /* Random padding */
2708 RAND_pseudo_bytes(p, padding);
2710 ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding);
2713 if (s->msg_callback)
2714 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
2715 buf, 3 + payload + padding,
2716 s, s->msg_callback_arg);
2718 s->tlsext_hb_pending = 1;