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 "ssl_locl.h"
119 const char tls1_version_str[]="TLSv1" OPENSSL_VERSION_PTEXT;
121 #ifndef OPENSSL_NO_TLSEXT
122 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
123 const unsigned char *sess_id, int sesslen,
124 SSL_SESSION **psess);
127 SSL3_ENC_METHOD TLSv1_enc_data={
130 tls1_setup_key_block,
131 tls1_generate_master_secret,
132 tls1_change_cipher_state,
133 tls1_final_finish_mac,
134 TLS1_FINISH_MAC_LENGTH,
135 tls1_cert_verify_mac,
136 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
137 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
141 long tls1_default_timeout(void)
143 /* 2 hours, the 24 hours mentioned in the TLSv1 spec
144 * is way too long for http, the cache would over fill */
150 if (!ssl3_new(s)) return(0);
151 s->method->ssl_clear(s);
155 void tls1_free(SSL *s)
157 #ifndef OPENSSL_NO_TLSEXT
158 if (s->tlsext_session_ticket)
160 OPENSSL_free(s->tlsext_session_ticket);
162 #endif /* OPENSSL_NO_TLSEXT */
166 void tls1_clear(SSL *s)
169 s->version = s->method->version;
172 #ifndef OPENSSL_NO_EC
174 static int nid_list[] =
176 NID_sect163k1, /* sect163k1 (1) */
177 NID_sect163r1, /* sect163r1 (2) */
178 NID_sect163r2, /* sect163r2 (3) */
179 NID_sect193r1, /* sect193r1 (4) */
180 NID_sect193r2, /* sect193r2 (5) */
181 NID_sect233k1, /* sect233k1 (6) */
182 NID_sect233r1, /* sect233r1 (7) */
183 NID_sect239k1, /* sect239k1 (8) */
184 NID_sect283k1, /* sect283k1 (9) */
185 NID_sect283r1, /* sect283r1 (10) */
186 NID_sect409k1, /* sect409k1 (11) */
187 NID_sect409r1, /* sect409r1 (12) */
188 NID_sect571k1, /* sect571k1 (13) */
189 NID_sect571r1, /* sect571r1 (14) */
190 NID_secp160k1, /* secp160k1 (15) */
191 NID_secp160r1, /* secp160r1 (16) */
192 NID_secp160r2, /* secp160r2 (17) */
193 NID_secp192k1, /* secp192k1 (18) */
194 NID_X9_62_prime192v1, /* secp192r1 (19) */
195 NID_secp224k1, /* secp224k1 (20) */
196 NID_secp224r1, /* secp224r1 (21) */
197 NID_secp256k1, /* secp256k1 (22) */
198 NID_X9_62_prime256v1, /* secp256r1 (23) */
199 NID_secp384r1, /* secp384r1 (24) */
200 NID_secp521r1 /* secp521r1 (25) */
203 static int pref_list[] =
205 NID_sect571r1, /* sect571r1 (14) */
206 NID_sect571k1, /* sect571k1 (13) */
207 NID_secp521r1, /* secp521r1 (25) */
208 NID_sect409k1, /* sect409k1 (11) */
209 NID_sect409r1, /* sect409r1 (12) */
210 NID_secp384r1, /* secp384r1 (24) */
211 NID_sect283k1, /* sect283k1 (9) */
212 NID_sect283r1, /* sect283r1 (10) */
213 NID_secp256k1, /* secp256k1 (22) */
214 NID_X9_62_prime256v1, /* secp256r1 (23) */
215 NID_sect239k1, /* sect239k1 (8) */
216 NID_sect233k1, /* sect233k1 (6) */
217 NID_sect233r1, /* sect233r1 (7) */
218 NID_secp224k1, /* secp224k1 (20) */
219 NID_secp224r1, /* secp224r1 (21) */
220 NID_sect193r1, /* sect193r1 (4) */
221 NID_sect193r2, /* sect193r2 (5) */
222 NID_secp192k1, /* secp192k1 (18) */
223 NID_X9_62_prime192v1, /* secp192r1 (19) */
224 NID_sect163k1, /* sect163k1 (1) */
225 NID_sect163r1, /* sect163r1 (2) */
226 NID_sect163r2, /* sect163r2 (3) */
227 NID_secp160k1, /* secp160k1 (15) */
228 NID_secp160r1, /* secp160r1 (16) */
229 NID_secp160r2, /* secp160r2 (17) */
232 int tls1_ec_curve_id2nid(int curve_id)
234 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
235 if ((curve_id < 1) || ((unsigned int)curve_id >
236 sizeof(nid_list)/sizeof(nid_list[0])))
238 return nid_list[curve_id-1];
241 int tls1_ec_nid2curve_id(int nid)
243 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
246 case NID_sect163k1: /* sect163k1 (1) */
248 case NID_sect163r1: /* sect163r1 (2) */
250 case NID_sect163r2: /* sect163r2 (3) */
252 case NID_sect193r1: /* sect193r1 (4) */
254 case NID_sect193r2: /* sect193r2 (5) */
256 case NID_sect233k1: /* sect233k1 (6) */
258 case NID_sect233r1: /* sect233r1 (7) */
260 case NID_sect239k1: /* sect239k1 (8) */
262 case NID_sect283k1: /* sect283k1 (9) */
264 case NID_sect283r1: /* sect283r1 (10) */
266 case NID_sect409k1: /* sect409k1 (11) */
268 case NID_sect409r1: /* sect409r1 (12) */
270 case NID_sect571k1: /* sect571k1 (13) */
272 case NID_sect571r1: /* sect571r1 (14) */
274 case NID_secp160k1: /* secp160k1 (15) */
276 case NID_secp160r1: /* secp160r1 (16) */
278 case NID_secp160r2: /* secp160r2 (17) */
280 case NID_secp192k1: /* secp192k1 (18) */
282 case NID_X9_62_prime192v1: /* secp192r1 (19) */
284 case NID_secp224k1: /* secp224k1 (20) */
286 case NID_secp224r1: /* secp224r1 (21) */
288 case NID_secp256k1: /* secp256k1 (22) */
290 case NID_X9_62_prime256v1: /* secp256r1 (23) */
292 case NID_secp384r1: /* secp384r1 (24) */
294 case NID_secp521r1: /* secp521r1 (25) */
300 #endif /* OPENSSL_NO_EC */
302 #ifndef OPENSSL_NO_TLSEXT
304 /* List of supported signature algorithms and hashes. Should make this
305 * customisable at some point, for now include everything we support.
308 #ifdef OPENSSL_NO_RSA
309 #define tlsext_sigalg_rsa(md) /* */
311 #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
314 #ifdef OPENSSL_NO_DSA
315 #define tlsext_sigalg_dsa(md) /* */
317 #define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
320 #ifdef OPENSSL_NO_ECDSA
321 #define tlsext_sigalg_ecdsa(md) /* */
323 #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
326 #define tlsext_sigalg(md) \
327 tlsext_sigalg_rsa(md) \
328 tlsext_sigalg_dsa(md) \
329 tlsext_sigalg_ecdsa(md)
331 static unsigned char tls12_sigalgs[] = {
332 #ifndef OPENSSL_NO_SHA512
333 tlsext_sigalg(TLSEXT_hash_sha512)
334 tlsext_sigalg(TLSEXT_hash_sha384)
336 #ifndef OPENSSL_NO_SHA256
337 tlsext_sigalg(TLSEXT_hash_sha256)
338 tlsext_sigalg(TLSEXT_hash_sha224)
340 #ifndef OPENSSL_NO_SHA
341 tlsext_sigalg(TLSEXT_hash_sha1)
343 #ifndef OPENSSL_NO_MD5
344 tlsext_sigalg_rsa(TLSEXT_hash_md5)
348 int tls12_get_req_sig_algs(SSL *s, unsigned char *p)
350 size_t slen = sizeof(tls12_sigalgs);
352 /* If FIPS mode don't include MD5 which is last */
357 memcpy(p, tls12_sigalgs, slen);
361 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
364 unsigned char *ret = p;
366 /* don't add extensions for SSLv3 unless doing secure renegotiation */
367 if (s->client_version == SSL3_VERSION
368 && !s->s3->send_connection_binding)
373 if (ret>=limit) return NULL; /* this really never occurs, but ... */
375 if (s->tlsext_hostname != NULL)
377 /* Add TLS extension servername to the Client Hello message */
378 unsigned long size_str;
381 /* check for enough space.
382 4 for the servername type and entension length
383 2 for servernamelist length
384 1 for the hostname type
385 2 for hostname length
389 if ((lenmax = limit - ret - 9) < 0
390 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
393 /* extension type and length */
394 s2n(TLSEXT_TYPE_server_name,ret);
397 /* length of servername list */
400 /* hostname type, length and hostname */
401 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
403 memcpy(ret, s->tlsext_hostname, size_str);
407 /* Add RI if renegotiating */
412 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
414 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
418 if((limit - p - 4 - el) < 0) return NULL;
420 s2n(TLSEXT_TYPE_renegotiate,ret);
423 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
425 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
432 #ifndef OPENSSL_NO_SRP
433 #define MIN(x,y) (((x)<(y))?(x):(y))
434 /* we add SRP username the first time only if we have one! */
435 if (s->srp_ctx.login != NULL)
436 {/* Add TLS extension SRP username to the Client Hello message */
437 int login_len = MIN(strlen(s->srp_ctx.login) + 1, 255);
440 if ((lenmax = limit - ret - 5) < 0) return NULL;
441 if (login_len > lenmax) return NULL;
444 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
447 s2n(TLSEXT_TYPE_srp,ret);
448 s2n(login_len+1,ret);
450 (*ret++) = (unsigned char) MIN(strlen(s->srp_ctx.login), 254);
451 memcpy(ret, s->srp_ctx.login, MIN(strlen(s->srp_ctx.login), 254));
456 #ifndef OPENSSL_NO_EC
457 if (s->tlsext_ecpointformatlist != NULL &&
458 s->version != DTLS1_VERSION)
460 /* Add TLS extension ECPointFormats to the ClientHello message */
463 if ((lenmax = limit - ret - 5) < 0) return NULL;
464 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
465 if (s->tlsext_ecpointformatlist_length > 255)
467 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
471 s2n(TLSEXT_TYPE_ec_point_formats,ret);
472 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
473 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
474 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
475 ret+=s->tlsext_ecpointformatlist_length;
477 if (s->tlsext_ellipticcurvelist != NULL &&
478 s->version != DTLS1_VERSION)
480 /* Add TLS extension EllipticCurves to the ClientHello message */
483 if ((lenmax = limit - ret - 6) < 0) return NULL;
484 if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax) return NULL;
485 if (s->tlsext_ellipticcurvelist_length > 65532)
487 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
491 s2n(TLSEXT_TYPE_elliptic_curves,ret);
492 s2n(s->tlsext_ellipticcurvelist_length + 2, ret);
494 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
495 * elliptic_curve_list, but the examples use two bytes.
496 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
497 * resolves this to two bytes.
499 s2n(s->tlsext_ellipticcurvelist_length, ret);
500 memcpy(ret, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length);
501 ret+=s->tlsext_ellipticcurvelist_length;
503 #endif /* OPENSSL_NO_EC */
505 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
508 if (!s->new_session && s->session && s->session->tlsext_tick)
509 ticklen = s->session->tlsext_ticklen;
510 else if (s->session && s->tlsext_session_ticket &&
511 s->tlsext_session_ticket->data)
513 ticklen = s->tlsext_session_ticket->length;
514 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
515 if (!s->session->tlsext_tick)
517 memcpy(s->session->tlsext_tick,
518 s->tlsext_session_ticket->data,
520 s->session->tlsext_ticklen = ticklen;
524 if (ticklen == 0 && s->tlsext_session_ticket &&
525 s->tlsext_session_ticket->data == NULL)
527 /* Check for enough room 2 for extension type, 2 for len
530 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
531 s2n(TLSEXT_TYPE_session_ticket,ret);
535 memcpy(ret, s->session->tlsext_tick, ticklen);
541 if (TLS1_get_version(s) >= TLS1_2_VERSION)
543 if ((size_t)(limit - ret) < sizeof(tls12_sigalgs) + 6)
545 s2n(TLSEXT_TYPE_signature_algorithms,ret);
546 s2n(sizeof(tls12_sigalgs) + 2, ret);
547 s2n(sizeof(tls12_sigalgs), ret);
548 memcpy(ret, tls12_sigalgs, sizeof(tls12_sigalgs));
549 ret += sizeof(tls12_sigalgs);
552 #ifdef TLSEXT_TYPE_opaque_prf_input
553 if (s->s3->client_opaque_prf_input != NULL &&
554 s->version != DTLS1_VERSION)
556 size_t col = s->s3->client_opaque_prf_input_len;
558 if ((long)(limit - ret - 6 - col < 0))
560 if (col > 0xFFFD) /* can't happen */
563 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
566 memcpy(ret, s->s3->client_opaque_prf_input, col);
571 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
572 s->version != DTLS1_VERSION)
575 long extlen, idlen, itmp;
579 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
581 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
582 itmp = i2d_OCSP_RESPID(id, NULL);
588 if (s->tlsext_ocsp_exts)
590 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
597 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
598 s2n(TLSEXT_TYPE_status_request, ret);
599 if (extlen + idlen > 0xFFF0)
601 s2n(extlen + idlen + 5, ret);
602 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
604 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
606 /* save position of id len */
607 unsigned char *q = ret;
608 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
609 /* skip over id len */
611 itmp = i2d_OCSP_RESPID(id, &ret);
617 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
620 if ((extdatalen = ret-p-2)== 0)
627 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
630 unsigned char *ret = p;
632 /* don't add extensions for SSLv3, unless doing secure renegotiation */
633 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
637 if (ret>=limit) return NULL; /* this really never occurs, but ... */
639 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
641 if ((long)(limit - ret - 4) < 0) return NULL;
643 s2n(TLSEXT_TYPE_server_name,ret);
647 if(s->s3->send_connection_binding)
651 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
653 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
657 if((limit - p - 4 - el) < 0) return NULL;
659 s2n(TLSEXT_TYPE_renegotiate,ret);
662 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
664 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
671 #ifndef OPENSSL_NO_EC
672 if (s->tlsext_ecpointformatlist != NULL &&
673 s->version != DTLS1_VERSION)
675 /* Add TLS extension ECPointFormats to the ServerHello message */
678 if ((lenmax = limit - ret - 5) < 0) return NULL;
679 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
680 if (s->tlsext_ecpointformatlist_length > 255)
682 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
686 s2n(TLSEXT_TYPE_ec_point_formats,ret);
687 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
688 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
689 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
690 ret+=s->tlsext_ecpointformatlist_length;
693 /* Currently the server should not respond with a SupportedCurves extension */
694 #endif /* OPENSSL_NO_EC */
696 if (s->tlsext_ticket_expected
697 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
699 if ((long)(limit - ret - 4) < 0) return NULL;
700 s2n(TLSEXT_TYPE_session_ticket,ret);
704 if (s->tlsext_status_expected)
706 if ((long)(limit - ret - 4) < 0) return NULL;
707 s2n(TLSEXT_TYPE_status_request,ret);
711 #ifdef TLSEXT_TYPE_opaque_prf_input
712 if (s->s3->server_opaque_prf_input != NULL &&
713 s->version != DTLS1_VERSION)
715 size_t sol = s->s3->server_opaque_prf_input_len;
717 if ((long)(limit - ret - 6 - sol) < 0)
719 if (sol > 0xFFFD) /* can't happen */
722 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
725 memcpy(ret, s->s3->server_opaque_prf_input, sol);
729 if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81)
730 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
731 { const unsigned char cryptopro_ext[36] = {
732 0xfd, 0xe8, /*65000*/
733 0x00, 0x20, /*32 bytes length*/
734 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
735 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
736 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
737 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17};
738 if (limit-ret<36) return NULL;
739 memcpy(ret,cryptopro_ext,36);
744 if ((extdatalen = ret-p-2)== 0)
751 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
756 unsigned char *data = *p;
757 int renegotiate_seen = 0;
760 s->servername_done = 0;
761 s->tlsext_status_type = -1;
767 if (data > (d+n-len))
770 while (data <= (d+n-4))
775 if (data+size > (d+n))
778 fprintf(stderr,"Received extension type %d size %d\n",type,size);
780 if (s->tlsext_debug_cb)
781 s->tlsext_debug_cb(s, 0, type, data, size,
782 s->tlsext_debug_arg);
783 /* The servername extension is treated as follows:
785 - Only the hostname type is supported with a maximum length of 255.
786 - The servername is rejected if too long or if it contains zeros,
787 in which case an fatal alert is generated.
788 - The servername field is maintained together with the session cache.
789 - When a session is resumed, the servername call back invoked in order
790 to allow the application to position itself to the right context.
791 - The servername is acknowledged if it is new for a session or when
792 it is identical to a previously used for the same session.
793 Applications can control the behaviour. They can at any time
794 set a 'desirable' servername for a new SSL object. This can be the
795 case for example with HTTPS when a Host: header field is received and
796 a renegotiation is requested. In this case, a possible servername
797 presented in the new client hello is only acknowledged if it matches
798 the value of the Host: field.
799 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
800 if they provide for changing an explicit servername context for the session,
801 i.e. when the session has been established with a servername extension.
802 - On session reconnect, the servername extension may be absent.
806 if (type == TLSEXT_TYPE_server_name)
808 unsigned char *sdata;
814 *al = SSL_AD_DECODE_ERROR;
821 *al = SSL_AD_DECODE_ERROR;
828 servname_type = *(sdata++);
834 *al = SSL_AD_DECODE_ERROR;
837 if (s->servername_done == 0)
838 switch (servname_type)
840 case TLSEXT_NAMETYPE_host_name:
843 if(s->session->tlsext_hostname)
845 *al = SSL_AD_DECODE_ERROR;
848 if (len > TLSEXT_MAXLEN_host_name)
850 *al = TLS1_AD_UNRECOGNIZED_NAME;
853 if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
855 *al = TLS1_AD_INTERNAL_ERROR;
858 memcpy(s->session->tlsext_hostname, sdata, len);
859 s->session->tlsext_hostname[len]='\0';
860 if (strlen(s->session->tlsext_hostname) != len) {
861 OPENSSL_free(s->session->tlsext_hostname);
862 s->session->tlsext_hostname = NULL;
863 *al = TLS1_AD_UNRECOGNIZED_NAME;
866 s->servername_done = 1;
870 s->servername_done = s->session->tlsext_hostname
871 && strlen(s->session->tlsext_hostname) == len
872 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
884 *al = SSL_AD_DECODE_ERROR;
889 #ifndef OPENSSL_NO_SRP
890 else if (type == TLSEXT_TYPE_srp)
895 if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL)
897 memcpy(s->srp_ctx.login, &data[1], len);
898 s->srp_ctx.login[len]='\0';
903 #ifndef OPENSSL_NO_EC
904 else if (type == TLSEXT_TYPE_ec_point_formats &&
905 s->version != DTLS1_VERSION)
907 unsigned char *sdata = data;
908 int ecpointformatlist_length = *(sdata++);
910 if (ecpointformatlist_length != size - 1)
912 *al = TLS1_AD_DECODE_ERROR;
917 if(s->session->tlsext_ecpointformatlist)
919 OPENSSL_free(s->session->tlsext_ecpointformatlist);
920 s->session->tlsext_ecpointformatlist = NULL;
922 s->session->tlsext_ecpointformatlist_length = 0;
923 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
925 *al = TLS1_AD_INTERNAL_ERROR;
928 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
929 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
932 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
933 sdata = s->session->tlsext_ecpointformatlist;
934 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
935 fprintf(stderr,"%i ",*(sdata++));
936 fprintf(stderr,"\n");
939 else if (type == TLSEXT_TYPE_elliptic_curves &&
940 s->version != DTLS1_VERSION)
942 unsigned char *sdata = data;
943 int ellipticcurvelist_length = (*(sdata++) << 8);
944 ellipticcurvelist_length += (*(sdata++));
946 if (ellipticcurvelist_length != size - 2)
948 *al = TLS1_AD_DECODE_ERROR;
953 if(s->session->tlsext_ellipticcurvelist)
955 *al = TLS1_AD_DECODE_ERROR;
958 s->session->tlsext_ellipticcurvelist_length = 0;
959 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
961 *al = TLS1_AD_INTERNAL_ERROR;
964 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
965 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
968 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
969 sdata = s->session->tlsext_ellipticcurvelist;
970 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
971 fprintf(stderr,"%i ",*(sdata++));
972 fprintf(stderr,"\n");
975 #endif /* OPENSSL_NO_EC */
976 #ifdef TLSEXT_TYPE_opaque_prf_input
977 else if (type == TLSEXT_TYPE_opaque_prf_input &&
978 s->version != DTLS1_VERSION)
980 unsigned char *sdata = data;
984 *al = SSL_AD_DECODE_ERROR;
987 n2s(sdata, s->s3->client_opaque_prf_input_len);
988 if (s->s3->client_opaque_prf_input_len != size - 2)
990 *al = SSL_AD_DECODE_ERROR;
994 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
995 OPENSSL_free(s->s3->client_opaque_prf_input);
996 if (s->s3->client_opaque_prf_input_len == 0)
997 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
999 s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
1000 if (s->s3->client_opaque_prf_input == NULL)
1002 *al = TLS1_AD_INTERNAL_ERROR;
1007 else if (type == TLSEXT_TYPE_session_ticket)
1009 if (s->tls_session_ticket_ext_cb &&
1010 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
1012 *al = TLS1_AD_INTERNAL_ERROR;
1016 else if (type == TLSEXT_TYPE_renegotiate)
1018 if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
1020 renegotiate_seen = 1;
1022 else if (type == TLSEXT_TYPE_signature_algorithms)
1025 if (sigalg_seen || size < 2)
1027 *al = SSL_AD_DECODE_ERROR;
1033 if (dsize != size || dsize & 1)
1035 *al = SSL_AD_DECODE_ERROR;
1038 if (!tls1_process_sigalgs(s, data, dsize))
1040 *al = SSL_AD_DECODE_ERROR;
1044 else if (type == TLSEXT_TYPE_status_request &&
1045 s->version != DTLS1_VERSION && s->ctx->tlsext_status_cb)
1050 *al = SSL_AD_DECODE_ERROR;
1054 s->tlsext_status_type = *data++;
1056 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
1058 const unsigned char *sdata;
1060 /* Read in responder_id_list */
1065 *al = SSL_AD_DECODE_ERROR;
1074 *al = SSL_AD_DECODE_ERROR;
1078 dsize -= 2 + idsize;
1082 *al = SSL_AD_DECODE_ERROR;
1087 id = d2i_OCSP_RESPID(NULL,
1091 *al = SSL_AD_DECODE_ERROR;
1096 OCSP_RESPID_free(id);
1097 *al = SSL_AD_DECODE_ERROR;
1100 if (!s->tlsext_ocsp_ids
1101 && !(s->tlsext_ocsp_ids =
1102 sk_OCSP_RESPID_new_null()))
1104 OCSP_RESPID_free(id);
1105 *al = SSL_AD_INTERNAL_ERROR;
1108 if (!sk_OCSP_RESPID_push(
1109 s->tlsext_ocsp_ids, id))
1111 OCSP_RESPID_free(id);
1112 *al = SSL_AD_INTERNAL_ERROR;
1117 /* Read in request_extensions */
1120 *al = SSL_AD_DECODE_ERROR;
1127 *al = SSL_AD_DECODE_ERROR;
1133 s->tlsext_ocsp_exts =
1134 d2i_X509_EXTENSIONS(NULL,
1136 if (!s->tlsext_ocsp_exts
1137 || (data + dsize != sdata))
1139 *al = SSL_AD_DECODE_ERROR;
1144 /* We don't know what to do with any other type
1148 s->tlsext_status_type = -1;
1151 /* session ticket processed earlier */
1159 /* Need RI if renegotiating */
1161 if (!renegotiate_seen && s->renegotiate &&
1162 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1164 *al = SSL_AD_HANDSHAKE_FAILURE;
1165 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,
1166 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1173 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
1175 unsigned short length;
1176 unsigned short type;
1177 unsigned short size;
1178 unsigned char *data = *p;
1179 int tlsext_servername = 0;
1180 int renegotiate_seen = 0;
1182 if (data >= (d+n-2))
1186 if (data+length != d+n)
1188 *al = SSL_AD_DECODE_ERROR;
1192 while(data <= (d+n-4))
1197 if (data+size > (d+n))
1200 if (s->tlsext_debug_cb)
1201 s->tlsext_debug_cb(s, 1, type, data, size,
1202 s->tlsext_debug_arg);
1204 if (type == TLSEXT_TYPE_server_name)
1206 if (s->tlsext_hostname == NULL || size > 0)
1208 *al = TLS1_AD_UNRECOGNIZED_NAME;
1211 tlsext_servername = 1;
1214 #ifndef OPENSSL_NO_EC
1215 else if (type == TLSEXT_TYPE_ec_point_formats &&
1216 s->version != DTLS1_VERSION)
1218 unsigned char *sdata = data;
1219 int ecpointformatlist_length = *(sdata++);
1221 if (ecpointformatlist_length != size - 1)
1223 *al = TLS1_AD_DECODE_ERROR;
1226 s->session->tlsext_ecpointformatlist_length = 0;
1227 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
1228 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
1230 *al = TLS1_AD_INTERNAL_ERROR;
1233 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
1234 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
1236 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
1237 sdata = s->session->tlsext_ecpointformatlist;
1238 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
1239 fprintf(stderr,"%i ",*(sdata++));
1240 fprintf(stderr,"\n");
1243 #endif /* OPENSSL_NO_EC */
1245 else if (type == TLSEXT_TYPE_session_ticket)
1247 if (s->tls_session_ticket_ext_cb &&
1248 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
1250 *al = TLS1_AD_INTERNAL_ERROR;
1253 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
1256 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
1259 s->tlsext_ticket_expected = 1;
1261 #ifdef TLSEXT_TYPE_opaque_prf_input
1262 else if (type == TLSEXT_TYPE_opaque_prf_input &&
1263 s->version != DTLS1_VERSION)
1265 unsigned char *sdata = data;
1269 *al = SSL_AD_DECODE_ERROR;
1272 n2s(sdata, s->s3->server_opaque_prf_input_len);
1273 if (s->s3->server_opaque_prf_input_len != size - 2)
1275 *al = SSL_AD_DECODE_ERROR;
1279 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
1280 OPENSSL_free(s->s3->server_opaque_prf_input);
1281 if (s->s3->server_opaque_prf_input_len == 0)
1282 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
1284 s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
1286 if (s->s3->server_opaque_prf_input == NULL)
1288 *al = TLS1_AD_INTERNAL_ERROR;
1293 else if (type == TLSEXT_TYPE_status_request &&
1294 s->version != DTLS1_VERSION)
1296 /* MUST be empty and only sent if we've requested
1297 * a status request message.
1299 if ((s->tlsext_status_type == -1) || (size > 0))
1301 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
1304 /* Set flag to expect CertificateStatus message */
1305 s->tlsext_status_expected = 1;
1307 else if (type == TLSEXT_TYPE_renegotiate)
1309 if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
1311 renegotiate_seen = 1;
1318 *al = SSL_AD_DECODE_ERROR;
1322 if (!s->hit && tlsext_servername == 1)
1324 if (s->tlsext_hostname)
1326 if (s->session->tlsext_hostname == NULL)
1328 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
1329 if (!s->session->tlsext_hostname)
1331 *al = SSL_AD_UNRECOGNIZED_NAME;
1337 *al = SSL_AD_DECODE_ERROR;
1347 /* Determine if we need to see RI. Strictly speaking if we want to
1348 * avoid an attack we should *always* see RI even on initial server
1349 * hello because the client doesn't see any renegotiation during an
1350 * attack. However this would mean we could not connect to any server
1351 * which doesn't support RI so for the immediate future tolerate RI
1352 * absence on initial connect only.
1354 if (!renegotiate_seen
1355 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
1356 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1358 *al = SSL_AD_HANDSHAKE_FAILURE;
1359 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,
1360 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1368 int ssl_prepare_clienthello_tlsext(SSL *s)
1370 #ifndef OPENSSL_NO_EC
1371 /* If we are client and using an elliptic curve cryptography cipher suite, send the point formats
1372 * and elliptic curves we support.
1377 unsigned long alg_k, alg_a;
1378 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1380 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
1382 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1384 alg_k = c->algorithm_mkey;
1385 alg_a = c->algorithm_auth;
1386 if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe) || (alg_a & SSL_aECDSA)))
1392 using_ecc = using_ecc && (s->version >= TLS1_VERSION);
1395 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
1396 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
1398 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
1401 s->tlsext_ecpointformatlist_length = 3;
1402 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
1403 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
1404 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
1406 /* we support all named elliptic curves in draft-ietf-tls-ecc-12 */
1407 if (s->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->tlsext_ellipticcurvelist);
1408 s->tlsext_ellipticcurvelist_length = sizeof(pref_list)/sizeof(pref_list[0]) * 2;
1409 if ((s->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL)
1411 s->tlsext_ellipticcurvelist_length = 0;
1412 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
1415 for (i = 0, j = s->tlsext_ellipticcurvelist; (unsigned int)i <
1416 sizeof(pref_list)/sizeof(pref_list[0]); i++)
1418 int id = tls1_ec_nid2curve_id(pref_list[i]);
1422 #endif /* OPENSSL_NO_EC */
1424 #ifdef TLSEXT_TYPE_opaque_prf_input
1428 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
1430 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
1435 if (s->tlsext_opaque_prf_input != NULL)
1437 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
1438 OPENSSL_free(s->s3->client_opaque_prf_input);
1440 if (s->tlsext_opaque_prf_input_len == 0)
1441 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
1443 s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
1444 if (s->s3->client_opaque_prf_input == NULL)
1446 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
1449 s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
1453 /* at callback's request, insist on receiving an appropriate server opaque PRF input */
1454 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
1461 int ssl_prepare_serverhello_tlsext(SSL *s)
1463 #ifndef OPENSSL_NO_EC
1464 /* If we are server and using an ECC cipher suite, send the point formats we support
1465 * if the client sent us an ECPointsFormat extension. Note that the server is not
1466 * supposed to send an EllipticCurves extension.
1469 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1470 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1471 int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
1472 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1476 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
1477 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
1479 SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
1482 s->tlsext_ecpointformatlist_length = 3;
1483 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
1484 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
1485 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
1487 #endif /* OPENSSL_NO_EC */
1492 int ssl_check_clienthello_tlsext(SSL *s)
1494 int ret=SSL_TLSEXT_ERR_NOACK;
1495 int al = SSL_AD_UNRECOGNIZED_NAME;
1497 #ifndef OPENSSL_NO_EC
1498 /* The handling of the ECPointFormats extension is done elsewhere, namely in
1499 * ssl3_choose_cipher in s3_lib.c.
1501 /* The handling of the EllipticCurves extension is done elsewhere, namely in
1502 * ssl3_choose_cipher in s3_lib.c.
1506 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
1507 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
1508 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
1509 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
1511 /* If status request then ask callback what to do.
1512 * Note: this must be called after servername callbacks in case
1513 * the certificate has changed.
1515 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
1518 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1521 /* We don't want to send a status request response */
1522 case SSL_TLSEXT_ERR_NOACK:
1523 s->tlsext_status_expected = 0;
1525 /* status request response should be sent */
1526 case SSL_TLSEXT_ERR_OK:
1527 if (s->tlsext_ocsp_resp)
1528 s->tlsext_status_expected = 1;
1530 s->tlsext_status_expected = 0;
1532 /* something bad happened */
1533 case SSL_TLSEXT_ERR_ALERT_FATAL:
1534 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1535 al = SSL_AD_INTERNAL_ERROR;
1540 s->tlsext_status_expected = 0;
1542 #ifdef TLSEXT_TYPE_opaque_prf_input
1544 /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
1545 * but we might be sending an alert in response to the client hello,
1546 * so this has to happen here in ssl_check_clienthello_tlsext(). */
1550 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
1552 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
1555 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1556 al = SSL_AD_INTERNAL_ERROR;
1561 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
1562 OPENSSL_free(s->s3->server_opaque_prf_input);
1563 s->s3->server_opaque_prf_input = NULL;
1565 if (s->tlsext_opaque_prf_input != NULL)
1567 if (s->s3->client_opaque_prf_input != NULL &&
1568 s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
1570 /* can only use this extension if we have a server opaque PRF input
1571 * of the same length as the client opaque PRF input! */
1573 if (s->tlsext_opaque_prf_input_len == 0)
1574 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
1576 s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
1577 if (s->s3->server_opaque_prf_input == NULL)
1579 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1580 al = SSL_AD_INTERNAL_ERROR;
1583 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
1587 if (r == 2 && s->s3->server_opaque_prf_input == NULL)
1589 /* The callback wants to enforce use of the extension,
1590 * but we can't do that with the client opaque PRF input;
1591 * abort the handshake.
1593 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1594 al = SSL_AD_HANDSHAKE_FAILURE;
1602 case SSL_TLSEXT_ERR_ALERT_FATAL:
1603 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1606 case SSL_TLSEXT_ERR_ALERT_WARNING:
1607 ssl3_send_alert(s,SSL3_AL_WARNING,al);
1610 case SSL_TLSEXT_ERR_NOACK:
1611 s->servername_done=0;
1617 int ssl_check_serverhello_tlsext(SSL *s)
1619 int ret=SSL_TLSEXT_ERR_NOACK;
1620 int al = SSL_AD_UNRECOGNIZED_NAME;
1622 #ifndef OPENSSL_NO_EC
1623 /* If we are client and using an elliptic curve cryptography cipher
1624 * suite, then if server returns an EC point formats lists extension
1625 * it must contain uncompressed.
1627 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1628 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1629 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
1630 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
1631 ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
1633 /* we are using an ECC cipher */
1635 unsigned char *list;
1636 int found_uncompressed = 0;
1637 list = s->session->tlsext_ecpointformatlist;
1638 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
1640 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
1642 found_uncompressed = 1;
1646 if (!found_uncompressed)
1648 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1652 ret = SSL_TLSEXT_ERR_OK;
1653 #endif /* OPENSSL_NO_EC */
1655 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
1656 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
1657 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
1658 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
1660 #ifdef TLSEXT_TYPE_opaque_prf_input
1661 if (s->s3->server_opaque_prf_input_len > 0)
1663 /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
1664 * So first verify that we really have a value from the server too. */
1666 if (s->s3->server_opaque_prf_input == NULL)
1668 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1669 al = SSL_AD_HANDSHAKE_FAILURE;
1672 /* Anytime the server *has* sent an opaque PRF input, we need to check
1673 * that we have a client opaque PRF input of the same size. */
1674 if (s->s3->client_opaque_prf_input == NULL ||
1675 s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
1677 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1678 al = SSL_AD_ILLEGAL_PARAMETER;
1683 /* If we've requested certificate status and we wont get one
1686 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
1687 && s->ctx && s->ctx->tlsext_status_cb)
1690 /* Set resp to NULL, resplen to -1 so callback knows
1691 * there is no response.
1693 if (s->tlsext_ocsp_resp)
1695 OPENSSL_free(s->tlsext_ocsp_resp);
1696 s->tlsext_ocsp_resp = NULL;
1698 s->tlsext_ocsp_resplen = -1;
1699 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1702 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1703 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1707 al = SSL_AD_INTERNAL_ERROR;
1708 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1714 case SSL_TLSEXT_ERR_ALERT_FATAL:
1715 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1718 case SSL_TLSEXT_ERR_ALERT_WARNING:
1719 ssl3_send_alert(s,SSL3_AL_WARNING,al);
1722 case SSL_TLSEXT_ERR_NOACK:
1723 s->servername_done=0;
1729 /* Since the server cache lookup is done early on in the processing of client
1730 * hello and other operations depend on the result we need to handle any TLS
1731 * session ticket extension at the same time.
1734 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
1735 const unsigned char *limit, SSL_SESSION **ret)
1737 /* Point after session ID in client hello */
1738 const unsigned char *p = session_id + len;
1741 /* If tickets disabled behave as if no ticket present
1742 * to permit stateful resumption.
1744 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
1747 if ((s->version <= SSL3_VERSION) || !limit)
1751 /* Skip past DTLS cookie */
1752 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
1759 /* Skip past cipher list */
1764 /* Skip past compression algorithm list */
1769 /* Now at start of extensions */
1770 if ((p + 2) >= limit)
1773 while ((p + 4) <= limit)
1775 unsigned short type, size;
1778 if (p + size > limit)
1780 if (type == TLSEXT_TYPE_session_ticket)
1782 /* If tickets disabled indicate cache miss which will
1783 * trigger a full handshake
1785 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
1787 /* If zero length note client will accept a ticket
1788 * and indicate cache miss to trigger full handshake
1792 s->tlsext_ticket_expected = 1;
1793 return 0; /* Cache miss */
1795 if (s->tls_session_secret_cb)
1797 /* Indicate cache miss here and instead of
1798 * generating the session from ticket now,
1799 * trigger abbreviated handshake based on
1800 * external mechanism to calculate the master
1804 return tls_decrypt_ticket(s, p, size, session_id, len,
1812 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
1813 const unsigned char *sess_id, int sesslen,
1814 SSL_SESSION **psess)
1817 unsigned char *sdec;
1818 const unsigned char *p;
1819 int slen, mlen, renew_ticket = 0;
1820 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
1823 SSL_CTX *tctx = s->initial_ctx;
1824 /* Need at least keyname + iv + some encrypted data */
1827 /* Initialize session ticket encryption and HMAC contexts */
1828 HMAC_CTX_init(&hctx);
1829 EVP_CIPHER_CTX_init(&ctx);
1830 if (tctx->tlsext_ticket_key_cb)
1832 unsigned char *nctick = (unsigned char *)etick;
1833 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
1844 /* Check key name matches */
1845 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
1847 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
1848 tlsext_tick_md(), NULL);
1849 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
1850 tctx->tlsext_tick_aes_key, etick + 16);
1852 /* Attempt to process session ticket, first conduct sanity and
1853 * integrity checks on ticket.
1855 mlen = HMAC_size(&hctx);
1858 EVP_CIPHER_CTX_cleanup(&ctx);
1862 /* Check HMAC of encrypted ticket */
1863 HMAC_Update(&hctx, etick, eticklen);
1864 HMAC_Final(&hctx, tick_hmac, NULL);
1865 HMAC_CTX_cleanup(&hctx);
1866 if (memcmp(tick_hmac, etick + eticklen, mlen))
1868 /* Attempt to decrypt session data */
1869 /* Move p after IV to start of encrypted ticket, update length */
1870 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
1871 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
1872 sdec = OPENSSL_malloc(eticklen);
1875 EVP_CIPHER_CTX_cleanup(&ctx);
1878 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
1879 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
1882 EVP_CIPHER_CTX_cleanup(&ctx);
1885 sess = d2i_SSL_SESSION(NULL, &p, slen);
1889 /* The session ID if non-empty is used by some clients to
1890 * detect that the ticket has been accepted. So we copy it to
1891 * the session structure. If it is empty set length to zero
1892 * as required by standard.
1895 memcpy(sess->session_id, sess_id, sesslen);
1896 sess->session_id_length = sesslen;
1898 s->tlsext_ticket_expected = renew_ticket;
1901 /* If session decrypt failure indicate a cache miss and set state to
1905 s->tlsext_ticket_expected = 1;
1909 /* Tables to translate from NIDs to TLS v1.2 ids */
1917 static tls12_lookup tls12_md[] = {
1918 #ifndef OPENSSL_NO_MD5
1919 {NID_md5, TLSEXT_hash_md5},
1921 #ifndef OPENSSL_NO_SHA
1922 {NID_sha1, TLSEXT_hash_sha1},
1924 #ifndef OPENSSL_NO_SHA256
1925 {NID_sha224, TLSEXT_hash_sha224},
1926 {NID_sha256, TLSEXT_hash_sha256},
1928 #ifndef OPENSSL_NO_SHA512
1929 {NID_sha384, TLSEXT_hash_sha384},
1930 {NID_sha512, TLSEXT_hash_sha512}
1934 static tls12_lookup tls12_sig[] = {
1935 #ifndef OPENSSL_NO_RSA
1936 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
1938 #ifndef OPENSSL_NO_RSA
1939 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
1941 #ifndef OPENSSL_NO_ECDSA
1942 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
1946 static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
1949 for (i = 0; i < tlen; i++)
1951 if (table[i].nid == nid)
1957 static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
1960 for (i = 0; i < tlen; i++)
1962 if (table[i].id == id)
1963 return table[i].nid;
1969 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
1972 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
1973 sizeof(tls12_md)/sizeof(tls12_lookup));
1976 sig_id = tls12_get_sigid(pk);
1979 p[0] = (unsigned char)md_id;
1980 p[1] = (unsigned char)sig_id;
1984 int tls12_get_sigid(const EVP_PKEY *pk)
1986 return tls12_find_id(pk->type, tls12_sig,
1987 sizeof(tls12_sig)/sizeof(tls12_lookup));
1990 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
1994 #ifndef OPENSSL_NO_MD5
1995 case TLSEXT_hash_md5:
2002 #ifndef OPENSSL_NO_SHA
2003 case TLSEXT_hash_sha1:
2006 #ifndef OPENSSL_NO_SHA256
2007 case TLSEXT_hash_sha224:
2008 return EVP_sha224();
2010 case TLSEXT_hash_sha256:
2011 return EVP_sha256();
2013 #ifndef OPENSSL_NO_SHA512
2014 case TLSEXT_hash_sha384:
2015 return EVP_sha384();
2017 case TLSEXT_hash_sha512:
2018 return EVP_sha512();
2026 /* Set preferred digest for each key type */
2028 int tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize)
2033 /* Extension ignored for TLS versions below 1.2 */
2034 if (TLS1_get_version(s) < TLS1_2_VERSION)
2036 /* Should never happen */
2040 c->pkeys[SSL_PKEY_DSA_SIGN].digest = NULL;
2041 c->pkeys[SSL_PKEY_RSA_SIGN].digest = NULL;
2042 c->pkeys[SSL_PKEY_RSA_ENC].digest = NULL;
2043 c->pkeys[SSL_PKEY_ECC].digest = NULL;
2045 for (i = 0; i < dsize; i += 2)
2047 unsigned char hash_alg = data[i], sig_alg = data[i+1];
2051 #ifndef OPENSSL_NO_RSA
2052 case TLSEXT_signature_rsa:
2053 idx = SSL_PKEY_RSA_SIGN;
2056 #ifndef OPENSSL_NO_DSA
2057 case TLSEXT_signature_dsa:
2058 idx = SSL_PKEY_DSA_SIGN;
2061 #ifndef OPENSSL_NO_ECDSA
2062 case TLSEXT_signature_ecdsa:
2070 if (c->pkeys[idx].digest == NULL)
2072 md = tls12_get_hash(hash_alg);
2075 c->pkeys[idx].digest = md;
2076 if (idx == SSL_PKEY_RSA_SIGN)
2077 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2084 /* Set any remaining keys to default values. NOTE: if alg is not
2085 * supported it stays as NULL.
2087 #ifndef OPENSSL_NO_DSA
2088 if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest)
2089 c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_dss1();
2091 #ifndef OPENSSL_NO_RSA
2092 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
2094 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
2095 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
2098 #ifndef OPENSSL_NO_ECDSA
2099 if (!c->pkeys[SSL_PKEY_ECC].digest)
2100 c->pkeys[SSL_PKEY_ECC].digest = EVP_ecdsa();