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);
125 static int tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize);
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,
142 long tls1_default_timeout(void)
144 /* 2 hours, the 24 hours mentioned in the TLSv1 spec
145 * is way too long for http, the cache would over fill */
151 if (!ssl3_new(s)) return(0);
152 s->method->ssl_clear(s);
156 void tls1_free(SSL *s)
158 #ifndef OPENSSL_NO_TLSEXT
159 if (s->tlsext_session_ticket)
161 OPENSSL_free(s->tlsext_session_ticket);
163 #endif /* OPENSSL_NO_TLSEXT */
167 void tls1_clear(SSL *s)
170 s->version = s->method->version;
173 #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 int tls1_ec_curve_id2nid(int curve_id)
205 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
206 if ((curve_id < 1) || ((unsigned int)curve_id >
207 sizeof(nid_list)/sizeof(nid_list[0])))
209 return nid_list[curve_id-1];
212 int tls1_ec_nid2curve_id(int nid)
214 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
217 case NID_sect163k1: /* sect163k1 (1) */
219 case NID_sect163r1: /* sect163r1 (2) */
221 case NID_sect163r2: /* sect163r2 (3) */
223 case NID_sect193r1: /* sect193r1 (4) */
225 case NID_sect193r2: /* sect193r2 (5) */
227 case NID_sect233k1: /* sect233k1 (6) */
229 case NID_sect233r1: /* sect233r1 (7) */
231 case NID_sect239k1: /* sect239k1 (8) */
233 case NID_sect283k1: /* sect283k1 (9) */
235 case NID_sect283r1: /* sect283r1 (10) */
237 case NID_sect409k1: /* sect409k1 (11) */
239 case NID_sect409r1: /* sect409r1 (12) */
241 case NID_sect571k1: /* sect571k1 (13) */
243 case NID_sect571r1: /* sect571r1 (14) */
245 case NID_secp160k1: /* secp160k1 (15) */
247 case NID_secp160r1: /* secp160r1 (16) */
249 case NID_secp160r2: /* secp160r2 (17) */
251 case NID_secp192k1: /* secp192k1 (18) */
253 case NID_X9_62_prime192v1: /* secp192r1 (19) */
255 case NID_secp224k1: /* secp224k1 (20) */
257 case NID_secp224r1: /* secp224r1 (21) */
259 case NID_secp256k1: /* secp256k1 (22) */
261 case NID_X9_62_prime256v1: /* secp256r1 (23) */
263 case NID_secp384r1: /* secp384r1 (24) */
265 case NID_secp521r1: /* secp521r1 (25) */
271 #endif /* OPENSSL_NO_EC */
273 #ifndef OPENSSL_NO_TLSEXT
274 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
277 unsigned char *ret = p;
279 /* don't add extensions for SSLv3 unless doing secure renegotiation */
280 if (s->client_version == SSL3_VERSION
281 && !s->s3->send_connection_binding)
286 if (ret>=limit) return NULL; /* this really never occurs, but ... */
288 if (s->tlsext_hostname != NULL)
290 /* Add TLS extension servername to the Client Hello message */
291 unsigned long size_str;
294 /* check for enough space.
295 4 for the servername type and entension length
296 2 for servernamelist length
297 1 for the hostname type
298 2 for hostname length
302 if ((lenmax = limit - ret - 9) < 0
303 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
306 /* extension type and length */
307 s2n(TLSEXT_TYPE_server_name,ret);
310 /* length of servername list */
313 /* hostname type, length and hostname */
314 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
316 memcpy(ret, s->tlsext_hostname, size_str);
320 /* Add RI if renegotiating */
325 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
327 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
331 if((limit - p - 4 - el) < 0) return NULL;
333 s2n(TLSEXT_TYPE_renegotiate,ret);
336 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
338 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
345 #ifndef OPENSSL_NO_SRP
346 #define MIN(x,y) (((x)<(y))?(x):(y))
347 /* we add SRP username the first time only if we have one! */
348 if (s->srp_ctx.login != NULL)
349 {/* Add TLS extension SRP username to the Client Hello message */
350 int login_len = MIN(strlen(s->srp_ctx.login) + 1, 255);
353 if ((lenmax = limit - ret - 5) < 0) return NULL;
354 if (login_len > lenmax) return NULL;
357 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
360 s2n(TLSEXT_TYPE_srp,ret);
361 s2n(login_len+1,ret);
363 (*ret++) = (unsigned char) MIN(strlen(s->srp_ctx.login), 254);
364 memcpy(ret, s->srp_ctx.login, MIN(strlen(s->srp_ctx.login), 254));
369 #ifndef OPENSSL_NO_EC
370 if (s->tlsext_ecpointformatlist != NULL &&
371 s->version != DTLS1_VERSION)
373 /* Add TLS extension ECPointFormats to the ClientHello message */
376 if ((lenmax = limit - ret - 5) < 0) return NULL;
377 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
378 if (s->tlsext_ecpointformatlist_length > 255)
380 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
384 s2n(TLSEXT_TYPE_ec_point_formats,ret);
385 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
386 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
387 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
388 ret+=s->tlsext_ecpointformatlist_length;
390 if (s->tlsext_ellipticcurvelist != NULL &&
391 s->version != DTLS1_VERSION)
393 /* Add TLS extension EllipticCurves to the ClientHello message */
396 if ((lenmax = limit - ret - 6) < 0) return NULL;
397 if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax) return NULL;
398 if (s->tlsext_ellipticcurvelist_length > 65532)
400 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
404 s2n(TLSEXT_TYPE_elliptic_curves,ret);
405 s2n(s->tlsext_ellipticcurvelist_length + 2, ret);
407 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
408 * elliptic_curve_list, but the examples use two bytes.
409 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
410 * resolves this to two bytes.
412 s2n(s->tlsext_ellipticcurvelist_length, ret);
413 memcpy(ret, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length);
414 ret+=s->tlsext_ellipticcurvelist_length;
416 #endif /* OPENSSL_NO_EC */
418 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
421 if (!s->new_session && s->session && s->session->tlsext_tick)
422 ticklen = s->session->tlsext_ticklen;
423 else if (s->session && s->tlsext_session_ticket &&
424 s->tlsext_session_ticket->data)
426 ticklen = s->tlsext_session_ticket->length;
427 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
428 if (!s->session->tlsext_tick)
430 memcpy(s->session->tlsext_tick,
431 s->tlsext_session_ticket->data,
433 s->session->tlsext_ticklen = ticklen;
437 if (ticklen == 0 && s->tlsext_session_ticket &&
438 s->tlsext_session_ticket->data == NULL)
440 /* Check for enough room 2 for extension type, 2 for len
443 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
444 s2n(TLSEXT_TYPE_session_ticket,ret);
448 memcpy(ret, s->session->tlsext_tick, ticklen);
454 #ifdef TLSEXT_TYPE_opaque_prf_input
455 if (s->s3->client_opaque_prf_input != NULL &&
456 s->version != DTLS1_VERSION)
458 size_t col = s->s3->client_opaque_prf_input_len;
460 if ((long)(limit - ret - 6 - col < 0))
462 if (col > 0xFFFD) /* can't happen */
465 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
468 memcpy(ret, s->s3->client_opaque_prf_input, col);
473 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
474 s->version != DTLS1_VERSION)
477 long extlen, idlen, itmp;
481 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
483 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
484 itmp = i2d_OCSP_RESPID(id, NULL);
490 if (s->tlsext_ocsp_exts)
492 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
499 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
500 s2n(TLSEXT_TYPE_status_request, ret);
501 if (extlen + idlen > 0xFFF0)
503 s2n(extlen + idlen + 5, ret);
504 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
506 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
508 /* save position of id len */
509 unsigned char *q = ret;
510 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
511 /* skip over id len */
513 itmp = i2d_OCSP_RESPID(id, &ret);
519 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
522 #ifndef OPENSSL_NO_NEXTPROTONEG
523 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
525 /* The client advertises an emtpy extension to indicate its
526 * support for Next Protocol Negotiation */
527 if (limit - ret - 4 < 0)
529 s2n(TLSEXT_TYPE_next_proto_neg,ret);
534 if ((extdatalen = ret-p-2)== 0)
541 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
544 unsigned char *ret = p;
545 #ifndef OPENSSL_NO_NEXTPROTONEG
546 int next_proto_neg_seen;
549 /* don't add extensions for SSLv3, unless doing secure renegotiation */
550 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
554 if (ret>=limit) return NULL; /* this really never occurs, but ... */
556 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
558 if ((long)(limit - ret - 4) < 0) return NULL;
560 s2n(TLSEXT_TYPE_server_name,ret);
564 if(s->s3->send_connection_binding)
568 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
570 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
574 if((limit - p - 4 - el) < 0) return NULL;
576 s2n(TLSEXT_TYPE_renegotiate,ret);
579 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
581 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
588 #ifndef OPENSSL_NO_EC
589 if (s->tlsext_ecpointformatlist != NULL &&
590 s->version != DTLS1_VERSION)
592 /* Add TLS extension ECPointFormats to the ServerHello message */
595 if ((lenmax = limit - ret - 5) < 0) return NULL;
596 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
597 if (s->tlsext_ecpointformatlist_length > 255)
599 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
603 s2n(TLSEXT_TYPE_ec_point_formats,ret);
604 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
605 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
606 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
607 ret+=s->tlsext_ecpointformatlist_length;
610 /* Currently the server should not respond with a SupportedCurves extension */
611 #endif /* OPENSSL_NO_EC */
613 if (s->tlsext_ticket_expected
614 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
616 if ((long)(limit - ret - 4) < 0) return NULL;
617 s2n(TLSEXT_TYPE_session_ticket,ret);
621 if (s->tlsext_status_expected)
623 if ((long)(limit - ret - 4) < 0) return NULL;
624 s2n(TLSEXT_TYPE_status_request,ret);
628 #ifdef TLSEXT_TYPE_opaque_prf_input
629 if (s->s3->server_opaque_prf_input != NULL &&
630 s->version != DTLS1_VERSION)
632 size_t sol = s->s3->server_opaque_prf_input_len;
634 if ((long)(limit - ret - 6 - sol) < 0)
636 if (sol > 0xFFFD) /* can't happen */
639 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
642 memcpy(ret, s->s3->server_opaque_prf_input, sol);
646 if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81)
647 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
648 { const unsigned char cryptopro_ext[36] = {
649 0xfd, 0xe8, /*65000*/
650 0x00, 0x20, /*32 bytes length*/
651 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
652 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
653 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
654 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17};
655 if (limit-ret<36) return NULL;
656 memcpy(ret,cryptopro_ext,36);
661 #ifndef OPENSSL_NO_NEXTPROTONEG
662 next_proto_neg_seen = s->s3->next_proto_neg_seen;
663 s->s3->next_proto_neg_seen = 0;
664 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
666 const unsigned char *npa;
670 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
671 if (r == SSL_TLSEXT_ERR_OK)
673 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
674 s2n(TLSEXT_TYPE_next_proto_neg,ret);
676 memcpy(ret, npa, npalen);
678 s->s3->next_proto_neg_seen = 1;
683 if ((extdatalen = ret-p-2)== 0)
690 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
695 unsigned char *data = *p;
696 int renegotiate_seen = 0;
699 s->servername_done = 0;
700 s->tlsext_status_type = -1;
706 if (data > (d+n-len))
709 while (data <= (d+n-4))
714 if (data+size > (d+n))
717 fprintf(stderr,"Received extension type %d size %d\n",type,size);
719 if (s->tlsext_debug_cb)
720 s->tlsext_debug_cb(s, 0, type, data, size,
721 s->tlsext_debug_arg);
722 /* The servername extension is treated as follows:
724 - Only the hostname type is supported with a maximum length of 255.
725 - The servername is rejected if too long or if it contains zeros,
726 in which case an fatal alert is generated.
727 - The servername field is maintained together with the session cache.
728 - When a session is resumed, the servername call back invoked in order
729 to allow the application to position itself to the right context.
730 - The servername is acknowledged if it is new for a session or when
731 it is identical to a previously used for the same session.
732 Applications can control the behaviour. They can at any time
733 set a 'desirable' servername for a new SSL object. This can be the
734 case for example with HTTPS when a Host: header field is received and
735 a renegotiation is requested. In this case, a possible servername
736 presented in the new client hello is only acknowledged if it matches
737 the value of the Host: field.
738 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
739 if they provide for changing an explicit servername context for the session,
740 i.e. when the session has been established with a servername extension.
741 - On session reconnect, the servername extension may be absent.
745 if (type == TLSEXT_TYPE_server_name)
747 unsigned char *sdata;
753 *al = SSL_AD_DECODE_ERROR;
760 *al = SSL_AD_DECODE_ERROR;
767 servname_type = *(sdata++);
773 *al = SSL_AD_DECODE_ERROR;
776 if (s->servername_done == 0)
777 switch (servname_type)
779 case TLSEXT_NAMETYPE_host_name:
782 if(s->session->tlsext_hostname)
784 *al = SSL_AD_DECODE_ERROR;
787 if (len > TLSEXT_MAXLEN_host_name)
789 *al = TLS1_AD_UNRECOGNIZED_NAME;
792 if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
794 *al = TLS1_AD_INTERNAL_ERROR;
797 memcpy(s->session->tlsext_hostname, sdata, len);
798 s->session->tlsext_hostname[len]='\0';
799 if (strlen(s->session->tlsext_hostname) != len) {
800 OPENSSL_free(s->session->tlsext_hostname);
801 s->session->tlsext_hostname = NULL;
802 *al = TLS1_AD_UNRECOGNIZED_NAME;
805 s->servername_done = 1;
809 s->servername_done = s->session->tlsext_hostname
810 && strlen(s->session->tlsext_hostname) == len
811 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
823 *al = SSL_AD_DECODE_ERROR;
828 #ifndef OPENSSL_NO_SRP
829 else if (type == TLSEXT_TYPE_srp)
834 if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL)
836 memcpy(s->srp_ctx.login, &data[1], len);
837 s->srp_ctx.login[len]='\0';
842 #ifndef OPENSSL_NO_EC
843 else if (type == TLSEXT_TYPE_ec_point_formats &&
844 s->version != DTLS1_VERSION)
846 unsigned char *sdata = data;
847 int ecpointformatlist_length = *(sdata++);
849 if (ecpointformatlist_length != size - 1)
851 *al = TLS1_AD_DECODE_ERROR;
856 if(s->session->tlsext_ecpointformatlist)
858 OPENSSL_free(s->session->tlsext_ecpointformatlist);
859 s->session->tlsext_ecpointformatlist = NULL;
861 s->session->tlsext_ecpointformatlist_length = 0;
862 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
864 *al = TLS1_AD_INTERNAL_ERROR;
867 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
868 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
871 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
872 sdata = s->session->tlsext_ecpointformatlist;
873 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
874 fprintf(stderr,"%i ",*(sdata++));
875 fprintf(stderr,"\n");
878 else if (type == TLSEXT_TYPE_elliptic_curves &&
879 s->version != DTLS1_VERSION)
881 unsigned char *sdata = data;
882 int ellipticcurvelist_length = (*(sdata++) << 8);
883 ellipticcurvelist_length += (*(sdata++));
885 if (ellipticcurvelist_length != size - 2)
887 *al = TLS1_AD_DECODE_ERROR;
892 if(s->session->tlsext_ellipticcurvelist)
894 *al = TLS1_AD_DECODE_ERROR;
897 s->session->tlsext_ellipticcurvelist_length = 0;
898 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
900 *al = TLS1_AD_INTERNAL_ERROR;
903 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
904 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
907 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
908 sdata = s->session->tlsext_ellipticcurvelist;
909 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
910 fprintf(stderr,"%i ",*(sdata++));
911 fprintf(stderr,"\n");
914 #endif /* OPENSSL_NO_EC */
915 #ifdef TLSEXT_TYPE_opaque_prf_input
916 else if (type == TLSEXT_TYPE_opaque_prf_input &&
917 s->version != DTLS1_VERSION)
919 unsigned char *sdata = data;
923 *al = SSL_AD_DECODE_ERROR;
926 n2s(sdata, s->s3->client_opaque_prf_input_len);
927 if (s->s3->client_opaque_prf_input_len != size - 2)
929 *al = SSL_AD_DECODE_ERROR;
933 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
934 OPENSSL_free(s->s3->client_opaque_prf_input);
935 if (s->s3->client_opaque_prf_input_len == 0)
936 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
938 s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
939 if (s->s3->client_opaque_prf_input == NULL)
941 *al = TLS1_AD_INTERNAL_ERROR;
946 else if (type == TLSEXT_TYPE_session_ticket)
948 if (s->tls_session_ticket_ext_cb &&
949 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
951 *al = TLS1_AD_INTERNAL_ERROR;
955 else if (type == TLSEXT_TYPE_renegotiate)
957 if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
959 renegotiate_seen = 1;
961 else if (type == TLSEXT_TYPE_signature_algorithms)
964 if (sigalg_seen || size < 2)
966 *al = SSL_AD_DECODE_ERROR;
972 if (dsize != size || dsize & 1)
974 *al = SSL_AD_DECODE_ERROR;
977 if (!tls1_process_sigalgs(s, data, dsize))
979 *al = SSL_AD_DECODE_ERROR;
983 else if (type == TLSEXT_TYPE_status_request &&
984 s->version != DTLS1_VERSION && s->ctx->tlsext_status_cb)
989 *al = SSL_AD_DECODE_ERROR;
993 s->tlsext_status_type = *data++;
995 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
997 const unsigned char *sdata;
999 /* Read in responder_id_list */
1004 *al = SSL_AD_DECODE_ERROR;
1013 *al = SSL_AD_DECODE_ERROR;
1017 dsize -= 2 + idsize;
1021 *al = SSL_AD_DECODE_ERROR;
1026 id = d2i_OCSP_RESPID(NULL,
1030 *al = SSL_AD_DECODE_ERROR;
1035 OCSP_RESPID_free(id);
1036 *al = SSL_AD_DECODE_ERROR;
1039 if (!s->tlsext_ocsp_ids
1040 && !(s->tlsext_ocsp_ids =
1041 sk_OCSP_RESPID_new_null()))
1043 OCSP_RESPID_free(id);
1044 *al = SSL_AD_INTERNAL_ERROR;
1047 if (!sk_OCSP_RESPID_push(
1048 s->tlsext_ocsp_ids, id))
1050 OCSP_RESPID_free(id);
1051 *al = SSL_AD_INTERNAL_ERROR;
1056 /* Read in request_extensions */
1059 *al = SSL_AD_DECODE_ERROR;
1066 *al = SSL_AD_DECODE_ERROR;
1072 s->tlsext_ocsp_exts =
1073 d2i_X509_EXTENSIONS(NULL,
1075 if (!s->tlsext_ocsp_exts
1076 || (data + dsize != sdata))
1078 *al = SSL_AD_DECODE_ERROR;
1083 /* We don't know what to do with any other type
1087 s->tlsext_status_type = -1;
1089 #ifndef OPENSSL_NO_NEXTPROTONEG
1090 else if (type == TLSEXT_TYPE_next_proto_neg &&
1091 s->s3->tmp.finish_md_len == 0)
1093 /* We shouldn't accept this extension on a
1096 * s->new_session will be set on renegotiation, but we
1097 * probably shouldn't rely that it couldn't be set on
1098 * the initial renegotation too in certain cases (when
1099 * there's some other reason to disallow resuming an
1100 * earlier session -- the current code won't be doing
1101 * anything like that, but this might change).
1103 * A valid sign that there's been a previous handshake
1104 * in this connection is if s->s3->tmp.finish_md_len >
1105 * 0. (We are talking about a check that will happen
1106 * in the Hello protocol round, well before a new
1107 * Finished message could have been computed.) */
1108 s->s3->next_proto_neg_seen = 1;
1112 /* session ticket processed earlier */
1120 /* Need RI if renegotiating */
1122 if (!renegotiate_seen && s->renegotiate &&
1123 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1125 *al = SSL_AD_HANDSHAKE_FAILURE;
1126 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,
1127 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1134 #ifndef OPENSSL_NO_NEXTPROTONEG
1135 /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1136 * elements of zero length are allowed and the set of elements must exactly fill
1137 * the length of the block. */
1138 static int ssl_next_proto_validate(unsigned char *d, unsigned len)
1140 unsigned int off = 0;
1154 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
1156 unsigned short length;
1157 unsigned short type;
1158 unsigned short size;
1159 unsigned char *data = *p;
1160 int tlsext_servername = 0;
1161 int renegotiate_seen = 0;
1163 if (data >= (d+n-2))
1167 if (data+length != d+n)
1169 *al = SSL_AD_DECODE_ERROR;
1173 while(data <= (d+n-4))
1178 if (data+size > (d+n))
1181 if (s->tlsext_debug_cb)
1182 s->tlsext_debug_cb(s, 1, type, data, size,
1183 s->tlsext_debug_arg);
1185 if (type == TLSEXT_TYPE_server_name)
1187 if (s->tlsext_hostname == NULL || size > 0)
1189 *al = TLS1_AD_UNRECOGNIZED_NAME;
1192 tlsext_servername = 1;
1195 #ifndef OPENSSL_NO_EC
1196 else if (type == TLSEXT_TYPE_ec_point_formats &&
1197 s->version != DTLS1_VERSION)
1199 unsigned char *sdata = data;
1200 int ecpointformatlist_length = *(sdata++);
1202 if (ecpointformatlist_length != size - 1)
1204 *al = TLS1_AD_DECODE_ERROR;
1207 s->session->tlsext_ecpointformatlist_length = 0;
1208 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
1209 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
1211 *al = TLS1_AD_INTERNAL_ERROR;
1214 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
1215 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
1217 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
1218 sdata = s->session->tlsext_ecpointformatlist;
1219 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
1220 fprintf(stderr,"%i ",*(sdata++));
1221 fprintf(stderr,"\n");
1224 #endif /* OPENSSL_NO_EC */
1226 else if (type == TLSEXT_TYPE_session_ticket)
1228 if (s->tls_session_ticket_ext_cb &&
1229 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
1231 *al = TLS1_AD_INTERNAL_ERROR;
1234 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
1237 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
1240 s->tlsext_ticket_expected = 1;
1242 #ifdef TLSEXT_TYPE_opaque_prf_input
1243 else if (type == TLSEXT_TYPE_opaque_prf_input &&
1244 s->version != DTLS1_VERSION)
1246 unsigned char *sdata = data;
1250 *al = SSL_AD_DECODE_ERROR;
1253 n2s(sdata, s->s3->server_opaque_prf_input_len);
1254 if (s->s3->server_opaque_prf_input_len != size - 2)
1256 *al = SSL_AD_DECODE_ERROR;
1260 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
1261 OPENSSL_free(s->s3->server_opaque_prf_input);
1262 if (s->s3->server_opaque_prf_input_len == 0)
1263 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
1265 s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
1267 if (s->s3->server_opaque_prf_input == NULL)
1269 *al = TLS1_AD_INTERNAL_ERROR;
1274 else if (type == TLSEXT_TYPE_status_request &&
1275 s->version != DTLS1_VERSION)
1277 /* MUST be empty and only sent if we've requested
1278 * a status request message.
1280 if ((s->tlsext_status_type == -1) || (size > 0))
1282 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
1285 /* Set flag to expect CertificateStatus message */
1286 s->tlsext_status_expected = 1;
1288 #ifndef OPENSSL_NO_NEXTPROTONEG
1289 else if (type == TLSEXT_TYPE_next_proto_neg)
1291 unsigned char *selected;
1292 unsigned char selected_len;
1294 /* We must have requested it. */
1295 if ((s->ctx->next_proto_select_cb == NULL))
1297 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
1300 /* The data must be valid */
1301 if (!ssl_next_proto_validate(data, size))
1303 *al = TLS1_AD_DECODE_ERROR;
1306 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
1308 *al = TLS1_AD_INTERNAL_ERROR;
1311 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
1312 if (!s->next_proto_negotiated)
1314 *al = TLS1_AD_INTERNAL_ERROR;
1317 memcpy(s->next_proto_negotiated, selected, selected_len);
1318 s->next_proto_negotiated_len = selected_len;
1321 else if (type == TLSEXT_TYPE_renegotiate)
1323 if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
1325 renegotiate_seen = 1;
1332 *al = SSL_AD_DECODE_ERROR;
1336 if (!s->hit && tlsext_servername == 1)
1338 if (s->tlsext_hostname)
1340 if (s->session->tlsext_hostname == NULL)
1342 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
1343 if (!s->session->tlsext_hostname)
1345 *al = SSL_AD_UNRECOGNIZED_NAME;
1351 *al = SSL_AD_DECODE_ERROR;
1361 /* Determine if we need to see RI. Strictly speaking if we want to
1362 * avoid an attack we should *always* see RI even on initial server
1363 * hello because the client doesn't see any renegotiation during an
1364 * attack. However this would mean we could not connect to any server
1365 * which doesn't support RI so for the immediate future tolerate RI
1366 * absence on initial connect only.
1368 if (!renegotiate_seen
1369 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
1370 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1372 *al = SSL_AD_HANDSHAKE_FAILURE;
1373 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,
1374 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1382 int ssl_prepare_clienthello_tlsext(SSL *s)
1384 #ifndef OPENSSL_NO_EC
1385 /* If we are client and using an elliptic curve cryptography cipher suite, send the point formats
1386 * and elliptic curves we support.
1391 unsigned long alg_k, alg_a;
1392 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1394 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
1396 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1398 alg_k = c->algorithm_mkey;
1399 alg_a = c->algorithm_auth;
1400 if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe) || (alg_a & SSL_aECDSA)))
1406 using_ecc = using_ecc && (s->version >= TLS1_VERSION);
1409 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
1410 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
1412 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
1415 s->tlsext_ecpointformatlist_length = 3;
1416 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
1417 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
1418 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
1420 /* we support all named elliptic curves in draft-ietf-tls-ecc-12 */
1421 if (s->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->tlsext_ellipticcurvelist);
1422 s->tlsext_ellipticcurvelist_length = sizeof(nid_list)/sizeof(nid_list[0]) * 2;
1423 if ((s->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL)
1425 s->tlsext_ellipticcurvelist_length = 0;
1426 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
1429 for (i = 1, j = s->tlsext_ellipticcurvelist; (unsigned int)i <=
1430 sizeof(nid_list)/sizeof(nid_list[0]); i++)
1433 #endif /* OPENSSL_NO_EC */
1435 #ifdef TLSEXT_TYPE_opaque_prf_input
1439 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
1441 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
1446 if (s->tlsext_opaque_prf_input != NULL)
1448 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
1449 OPENSSL_free(s->s3->client_opaque_prf_input);
1451 if (s->tlsext_opaque_prf_input_len == 0)
1452 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
1454 s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
1455 if (s->s3->client_opaque_prf_input == NULL)
1457 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
1460 s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
1464 /* at callback's request, insist on receiving an appropriate server opaque PRF input */
1465 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
1472 int ssl_prepare_serverhello_tlsext(SSL *s)
1474 #ifndef OPENSSL_NO_EC
1475 /* If we are server and using an ECC cipher suite, send the point formats we support
1476 * if the client sent us an ECPointsFormat extension. Note that the server is not
1477 * supposed to send an EllipticCurves extension.
1480 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1481 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1482 int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
1483 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1487 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
1488 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
1490 SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
1493 s->tlsext_ecpointformatlist_length = 3;
1494 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
1495 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
1496 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
1498 #endif /* OPENSSL_NO_EC */
1503 int ssl_check_clienthello_tlsext(SSL *s)
1505 int ret=SSL_TLSEXT_ERR_NOACK;
1506 int al = SSL_AD_UNRECOGNIZED_NAME;
1508 #ifndef OPENSSL_NO_EC
1509 /* The handling of the ECPointFormats extension is done elsewhere, namely in
1510 * ssl3_choose_cipher in s3_lib.c.
1512 /* The handling of the EllipticCurves extension is done elsewhere, namely in
1513 * ssl3_choose_cipher in s3_lib.c.
1517 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
1518 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
1519 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
1520 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
1522 /* If status request then ask callback what to do.
1523 * Note: this must be called after servername callbacks in case
1524 * the certificate has changed.
1526 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
1529 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1532 /* We don't want to send a status request response */
1533 case SSL_TLSEXT_ERR_NOACK:
1534 s->tlsext_status_expected = 0;
1536 /* status request response should be sent */
1537 case SSL_TLSEXT_ERR_OK:
1538 if (s->tlsext_ocsp_resp)
1539 s->tlsext_status_expected = 1;
1541 s->tlsext_status_expected = 0;
1543 /* something bad happened */
1544 case SSL_TLSEXT_ERR_ALERT_FATAL:
1545 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1546 al = SSL_AD_INTERNAL_ERROR;
1551 s->tlsext_status_expected = 0;
1553 #ifdef TLSEXT_TYPE_opaque_prf_input
1555 /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
1556 * but we might be sending an alert in response to the client hello,
1557 * so this has to happen here in ssl_check_clienthello_tlsext(). */
1561 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
1563 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
1566 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1567 al = SSL_AD_INTERNAL_ERROR;
1572 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
1573 OPENSSL_free(s->s3->server_opaque_prf_input);
1574 s->s3->server_opaque_prf_input = NULL;
1576 if (s->tlsext_opaque_prf_input != NULL)
1578 if (s->s3->client_opaque_prf_input != NULL &&
1579 s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
1581 /* can only use this extension if we have a server opaque PRF input
1582 * of the same length as the client opaque PRF input! */
1584 if (s->tlsext_opaque_prf_input_len == 0)
1585 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
1587 s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
1588 if (s->s3->server_opaque_prf_input == NULL)
1590 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1591 al = SSL_AD_INTERNAL_ERROR;
1594 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
1598 if (r == 2 && s->s3->server_opaque_prf_input == NULL)
1600 /* The callback wants to enforce use of the extension,
1601 * but we can't do that with the client opaque PRF input;
1602 * abort the handshake.
1604 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1605 al = SSL_AD_HANDSHAKE_FAILURE;
1613 case SSL_TLSEXT_ERR_ALERT_FATAL:
1614 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1617 case SSL_TLSEXT_ERR_ALERT_WARNING:
1618 ssl3_send_alert(s,SSL3_AL_WARNING,al);
1621 case SSL_TLSEXT_ERR_NOACK:
1622 s->servername_done=0;
1628 int ssl_check_serverhello_tlsext(SSL *s)
1630 int ret=SSL_TLSEXT_ERR_NOACK;
1631 int al = SSL_AD_UNRECOGNIZED_NAME;
1633 #ifndef OPENSSL_NO_EC
1634 /* If we are client and using an elliptic curve cryptography cipher
1635 * suite, then if server returns an EC point formats lists extension
1636 * it must contain uncompressed.
1638 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1639 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1640 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
1641 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
1642 ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
1644 /* we are using an ECC cipher */
1646 unsigned char *list;
1647 int found_uncompressed = 0;
1648 list = s->session->tlsext_ecpointformatlist;
1649 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
1651 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
1653 found_uncompressed = 1;
1657 if (!found_uncompressed)
1659 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1663 ret = SSL_TLSEXT_ERR_OK;
1664 #endif /* OPENSSL_NO_EC */
1666 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
1667 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
1668 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
1669 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
1671 #ifdef TLSEXT_TYPE_opaque_prf_input
1672 if (s->s3->server_opaque_prf_input_len > 0)
1674 /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
1675 * So first verify that we really have a value from the server too. */
1677 if (s->s3->server_opaque_prf_input == NULL)
1679 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1680 al = SSL_AD_HANDSHAKE_FAILURE;
1683 /* Anytime the server *has* sent an opaque PRF input, we need to check
1684 * that we have a client opaque PRF input of the same size. */
1685 if (s->s3->client_opaque_prf_input == NULL ||
1686 s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
1688 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1689 al = SSL_AD_ILLEGAL_PARAMETER;
1694 /* If we've requested certificate status and we wont get one
1697 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
1698 && s->ctx && s->ctx->tlsext_status_cb)
1701 /* Set resp to NULL, resplen to -1 so callback knows
1702 * there is no response.
1704 if (s->tlsext_ocsp_resp)
1706 OPENSSL_free(s->tlsext_ocsp_resp);
1707 s->tlsext_ocsp_resp = NULL;
1709 s->tlsext_ocsp_resplen = -1;
1710 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1713 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1714 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1718 al = SSL_AD_INTERNAL_ERROR;
1719 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1725 case SSL_TLSEXT_ERR_ALERT_FATAL:
1726 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1729 case SSL_TLSEXT_ERR_ALERT_WARNING:
1730 ssl3_send_alert(s,SSL3_AL_WARNING,al);
1733 case SSL_TLSEXT_ERR_NOACK:
1734 s->servername_done=0;
1740 /* Since the server cache lookup is done early on in the processing of client
1741 * hello and other operations depend on the result we need to handle any TLS
1742 * session ticket extension at the same time.
1745 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
1746 const unsigned char *limit, SSL_SESSION **ret)
1748 /* Point after session ID in client hello */
1749 const unsigned char *p = session_id + len;
1752 /* If tickets disabled behave as if no ticket present
1753 * to permit stateful resumption.
1755 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
1758 if ((s->version <= SSL3_VERSION) || !limit)
1762 /* Skip past DTLS cookie */
1763 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
1770 /* Skip past cipher list */
1775 /* Skip past compression algorithm list */
1780 /* Now at start of extensions */
1781 if ((p + 2) >= limit)
1784 while ((p + 4) <= limit)
1786 unsigned short type, size;
1789 if (p + size > limit)
1791 if (type == TLSEXT_TYPE_session_ticket)
1793 /* If tickets disabled indicate cache miss which will
1794 * trigger a full handshake
1796 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
1798 /* If zero length note client will accept a ticket
1799 * and indicate cache miss to trigger full handshake
1803 s->tlsext_ticket_expected = 1;
1804 return 0; /* Cache miss */
1806 if (s->tls_session_secret_cb)
1808 /* Indicate cache miss here and instead of
1809 * generating the session from ticket now,
1810 * trigger abbreviated handshake based on
1811 * external mechanism to calculate the master
1815 return tls_decrypt_ticket(s, p, size, session_id, len,
1823 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
1824 const unsigned char *sess_id, int sesslen,
1825 SSL_SESSION **psess)
1828 unsigned char *sdec;
1829 const unsigned char *p;
1830 int slen, mlen, renew_ticket = 0;
1831 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
1834 SSL_CTX *tctx = s->initial_ctx;
1835 /* Need at least keyname + iv + some encrypted data */
1838 /* Initialize session ticket encryption and HMAC contexts */
1839 HMAC_CTX_init(&hctx);
1840 EVP_CIPHER_CTX_init(&ctx);
1841 if (tctx->tlsext_ticket_key_cb)
1843 unsigned char *nctick = (unsigned char *)etick;
1844 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
1855 /* Check key name matches */
1856 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
1858 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
1859 tlsext_tick_md(), NULL);
1860 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
1861 tctx->tlsext_tick_aes_key, etick + 16);
1863 /* Attempt to process session ticket, first conduct sanity and
1864 * integrity checks on ticket.
1866 mlen = HMAC_size(&hctx);
1869 EVP_CIPHER_CTX_cleanup(&ctx);
1873 /* Check HMAC of encrypted ticket */
1874 HMAC_Update(&hctx, etick, eticklen);
1875 HMAC_Final(&hctx, tick_hmac, NULL);
1876 HMAC_CTX_cleanup(&hctx);
1877 if (memcmp(tick_hmac, etick + eticklen, mlen))
1879 /* Attempt to decrypt session data */
1880 /* Move p after IV to start of encrypted ticket, update length */
1881 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
1882 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
1883 sdec = OPENSSL_malloc(eticklen);
1886 EVP_CIPHER_CTX_cleanup(&ctx);
1889 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
1890 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
1893 EVP_CIPHER_CTX_cleanup(&ctx);
1896 sess = d2i_SSL_SESSION(NULL, &p, slen);
1900 /* The session ID if non-empty is used by some clients to
1901 * detect that the ticket has been accepted. So we copy it to
1902 * the session structure. If it is empty set length to zero
1903 * as required by standard.
1906 memcpy(sess->session_id, sess_id, sesslen);
1907 sess->session_id_length = sesslen;
1909 s->tlsext_ticket_expected = renew_ticket;
1912 /* If session decrypt failure indicate a cache miss and set state to
1916 s->tlsext_ticket_expected = 1;
1920 /* Tables to translate from NIDs to TLS v1.2 ids */
1928 static tls12_lookup tls12_md[] = {
1929 #ifndef OPENSSL_NO_MD5
1930 {NID_md5, TLSEXT_hash_md5},
1932 #ifndef OPENSSL_NO_SHA
1933 {NID_sha1, TLSEXT_hash_sha1},
1935 #ifndef OPENSSL_NO_SHA256
1936 {NID_sha224, TLSEXT_hash_sha224},
1937 {NID_sha256, TLSEXT_hash_sha256},
1939 #ifndef OPENSSL_NO_SHA512
1940 {NID_sha384, TLSEXT_hash_sha384},
1941 {NID_sha512, TLSEXT_hash_sha512}
1945 static tls12_lookup tls12_sig[] = {
1946 #ifndef OPENSSL_NO_RSA
1947 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
1949 #ifndef OPENSSL_NO_RSA
1950 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
1952 #ifndef OPENSSL_NO_ECDSA
1953 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
1957 static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
1960 for (i = 0; i < tlen; i++)
1962 if (table[i].nid == nid)
1968 static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
1971 for (i = 0; i < tlen; i++)
1973 if (table[i].id == id)
1974 return table[i].nid;
1979 int tls12_get_sigandhash(unsigned char *p, EVP_PKEY *pk, const EVP_MD *md)
1982 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
1983 sizeof(tls12_md)/sizeof(tls12_lookup));
1986 sig_id = tls12_find_id(pk->type, tls12_sig,
1987 sizeof(tls12_sig)/sizeof(tls12_lookup));
1990 p[0] = (unsigned char)md_id;
1991 p[1] = (unsigned char)sig_id;
1995 /* Set preferred digest for each key type */
1997 int tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize)
2002 /* Extension ignored for TLS versions below 1.2 */
2003 if (s->version < TLS1_2_VERSION)
2006 c->pkeys[SSL_PKEY_DSA_SIGN].digest = NULL;
2007 c->pkeys[SSL_PKEY_RSA_SIGN].digest = NULL;
2008 c->pkeys[SSL_PKEY_RSA_ENC].digest = NULL;
2009 c->pkeys[SSL_PKEY_ECC].digest = NULL;
2011 for (i = 0; i < dsize; i += 2)
2013 unsigned char hash_alg = data[i], sig_alg = data[i+1];
2017 #ifndef OPENSSL_NO_RSA
2018 case TLSEXT_signature_rsa:
2019 idx = SSL_PKEY_RSA_SIGN;
2022 #ifndef OPENSSL_NO_DSA
2023 case TLSEXT_signature_dsa:
2024 idx = SSL_PKEY_DSA_SIGN;
2027 #ifndef OPENSSL_NO_ECDSA
2028 case TLSEXT_signature_ecdsa:
2036 if (c->pkeys[idx].digest)
2041 #ifndef OPENSSL_NO_MD5
2042 case TLSEXT_hash_md5:
2046 #ifndef OPENSSL_NO_SHA
2047 case TLSEXT_hash_sha1:
2051 #ifndef OPENSSL_NO_SHA256
2052 case TLSEXT_hash_sha224:
2056 case TLSEXT_hash_sha256:
2060 #ifndef OPENSSL_NO_SHA512
2061 case TLSEXT_hash_sha384:
2065 case TLSEXT_hash_sha512:
2074 c->pkeys[idx].digest = md;
2075 if (idx == SSL_PKEY_RSA_SIGN)
2076 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2080 /* Set any remaining keys to default values. NOTE: if alg is not
2081 * supported it stays as NULL.
2083 #ifndef OPENSSL_NO_DSA
2084 if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest)
2085 c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_dss1();
2087 #ifndef OPENSSL_NO_RSA
2088 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
2090 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
2091 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
2094 #ifndef OPENSSL_NO_ECDSA
2095 if (!c->pkeys[SSL_PKEY_ECC].digest)
2096 c->pkeys[SSL_PKEY_ECC].digest = EVP_ecdsa();