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.]
60 #include <openssl/objects.h>
61 #include <openssl/evp.h>
62 #include <openssl/hmac.h>
63 #include <openssl/ocsp.h>
66 const char tls1_version_str[] = "TLSv1" OPENSSL_VERSION_PTEXT;
68 #ifndef OPENSSL_NO_TLSEXT
69 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
70 const unsigned char *sess_id, int sesslen,
74 SSL3_ENC_METHOD TLSv1_enc_data = {
78 tls1_generate_master_secret,
79 tls1_change_cipher_state,
80 tls1_final_finish_mac,
81 TLS1_FINISH_MAC_LENGTH,
83 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
84 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
88 long tls1_default_timeout(void)
91 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
92 * http, the cache would over fill
97 IMPLEMENT_tls1_meth_func(tlsv1_base_method,
98 ssl_undefined_function,
99 ssl_undefined_function, ssl_bad_method)
105 s->method->ssl_clear(s);
109 void tls1_free(SSL *s)
114 void tls1_clear(SSL *s)
117 s->version = TLS1_VERSION;
121 long tls1_ctrl(SSL *s, int cmd, long larg, char *parg)
126 long tls1_callback_ctrl(SSL *s, int cmd, void *(*fp) ())
132 #ifndef OPENSSL_NO_TLSEXT
133 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p,
134 unsigned char *limit)
137 unsigned char *ret = p;
139 /* don't add extensions for SSLv3 unless doing secure renegotiation */
140 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding)
146 return NULL; /* this really never occurs, but ... */
148 if (s->tlsext_hostname != NULL) {
149 /* Add TLS extension servername to the Client Hello message */
150 unsigned long size_str;
154 * check for enough space.
155 * 4 for the servername type and entension length
156 * 2 for servernamelist length
157 * 1 for the hostname type
158 * 2 for hostname length
162 if ((lenmax = limit - ret - 9) < 0
164 strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
167 /* extension type and length */
168 s2n(TLSEXT_TYPE_server_name, ret);
169 s2n(size_str + 5, ret);
171 /* length of servername list */
172 s2n(size_str + 3, ret);
174 /* hostname type, length and hostname */
175 *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name;
177 memcpy(ret, s->tlsext_hostname, size_str);
182 /* Add RI if renegotiating */
183 if (s->new_session) {
186 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
187 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
191 if ((limit - p - 4 - el) < 0)
194 s2n(TLSEXT_TYPE_renegotiate, ret);
197 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
198 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
205 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
207 if (!s->new_session && s->session && s->session->tlsext_tick)
208 ticklen = s->session->tlsext_ticklen;
212 * Check for enough room 2 for extension type, 2 for len rest for
215 if (limit - ret - 4 - ticklen < 0)
217 s2n(TLSEXT_TYPE_session_ticket, ret);
220 memcpy(ret, s->session->tlsext_tick, ticklen);
225 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
226 s->version != DTLS1_VERSION) {
228 long extlen, idlen, itmp;
232 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
233 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
234 itmp = i2d_OCSP_RESPID(id, NULL);
240 if (s->tlsext_ocsp_exts) {
241 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
247 if ((long)(limit - ret - 7 - extlen - idlen) < 0)
249 s2n(TLSEXT_TYPE_status_request, ret);
250 if (extlen + idlen > 0xFFF0)
252 s2n(extlen + idlen + 5, ret);
253 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
255 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
256 /* save position of id len */
257 unsigned char *q = ret;
258 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
259 /* skip over id len */
261 itmp = i2d_OCSP_RESPID(id, &ret);
267 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
270 if ((extdatalen = ret - p - 2) == 0)
277 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p,
278 unsigned char *limit)
281 unsigned char *ret = p;
284 * don't add extensions for SSLv3, unless doing secure renegotiation
286 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
291 return NULL; /* this really never occurs, but ... */
293 if (!s->hit && s->servername_done == 1
294 && s->session->tlsext_hostname != NULL) {
295 if (limit - ret - 4 < 0)
298 s2n(TLSEXT_TYPE_server_name, ret);
302 if (s->s3->send_connection_binding) {
305 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
306 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
310 if ((limit - p - 4 - el) < 0)
313 s2n(TLSEXT_TYPE_renegotiate, ret);
316 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
317 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
324 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
325 if (limit - ret - 4 < 0)
327 s2n(TLSEXT_TYPE_session_ticket, ret);
331 if (s->tlsext_status_expected) {
332 if ((long)(limit - ret - 4) < 0)
334 s2n(TLSEXT_TYPE_status_request, ret);
338 if ((extdatalen = ret - p - 2) == 0)
345 # ifndef OPENSSL_NO_EC
347 * ssl_check_for_safari attempts to fingerprint Safari using OS X
348 * SecureTransport using the TLS extension block in |d|, of length |n|.
349 * Safari, since 10.6, sends exactly these extensions, in this order:
354 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
355 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
356 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
357 * 10.8..10.8.3 (which don't work).
359 static void ssl_check_for_safari(SSL *s, const unsigned char *data,
360 const unsigned char *d, int n)
362 unsigned short type, size;
363 static const unsigned char kSafariExtensionsBlock[] = {
364 0x00, 0x0a, /* elliptic_curves extension */
365 0x00, 0x08, /* 8 bytes */
366 0x00, 0x06, /* 6 bytes of curve ids */
367 0x00, 0x17, /* P-256 */
368 0x00, 0x18, /* P-384 */
369 0x00, 0x19, /* P-521 */
371 0x00, 0x0b, /* ec_point_formats */
372 0x00, 0x02, /* 2 bytes */
373 0x01, /* 1 point format */
374 0x00, /* uncompressed */
377 /* The following is only present in TLS 1.2 */
378 static const unsigned char kSafariTLS12ExtensionsBlock[] = {
379 0x00, 0x0d, /* signature_algorithms */
380 0x00, 0x0c, /* 12 bytes */
381 0x00, 0x0a, /* 10 bytes */
382 0x05, 0x01, /* SHA-384/RSA */
383 0x04, 0x01, /* SHA-256/RSA */
384 0x02, 0x01, /* SHA-1/RSA */
385 0x04, 0x03, /* SHA-256/ECDSA */
386 0x02, 0x03, /* SHA-1/ECDSA */
389 if (data >= (d + n - 2))
393 if (data > (d + n - 4))
398 if (type != TLSEXT_TYPE_server_name)
401 if (data + size > d + n)
405 if (TLS1_get_client_version(s) >= TLS1_2_VERSION) {
406 const size_t len1 = sizeof(kSafariExtensionsBlock);
407 const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
409 if (data + len1 + len2 != d + n)
411 if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
413 if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
416 const size_t len = sizeof(kSafariExtensionsBlock);
418 if (data + len != d + n)
420 if (memcmp(data, kSafariExtensionsBlock, len) != 0)
424 s->s3->is_probably_safari = 1;
426 # endif /* !OPENSSL_NO_EC */
428 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d,
434 unsigned char *data = *p;
435 int renegotiate_seen = 0;
437 s->servername_done = 0;
438 s->tlsext_status_type = -1;
440 # ifndef OPENSSL_NO_EC
441 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
442 ssl_check_for_safari(s, data, d, n);
443 # endif /* !OPENSSL_NO_EC */
445 if (data >= (d + n - 2))
450 if (data > (d + n - len))
453 while (data <= (d + n - 4)) {
457 if (data + size > (d + n))
460 if (s->tlsext_debug_cb)
461 s->tlsext_debug_cb(s, 0, type, data, size, s->tlsext_debug_arg);
463 * The servername extension is treated as follows:
465 * - Only the hostname type is supported with a maximum length of 255.
466 * - The servername is rejected if too long or if it contains zeros,
467 * in which case an fatal alert is generated.
468 * - The servername field is maintained together with the session cache.
469 * - When a session is resumed, the servername call back invoked in order
470 * to allow the application to position itself to the right context.
471 * - The servername is acknowledged if it is new for a session or when
472 * it is identical to a previously used for the same session.
473 * Applications can control the behaviour. They can at any time
474 * set a 'desirable' servername for a new SSL object. This can be the
475 * case for example with HTTPS when a Host: header field is received and
476 * a renegotiation is requested. In this case, a possible servername
477 * presented in the new client hello is only acknowledged if it matches
478 * the value of the Host: field.
479 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
480 * if they provide for changing an explicit servername context for the
481 * session, i.e. when the session has been established with a servername
483 * - On session reconnect, the servername extension may be absent.
487 if (type == TLSEXT_TYPE_server_name) {
488 unsigned char *sdata;
493 *al = SSL_AD_DECODE_ERROR;
499 *al = SSL_AD_DECODE_ERROR;
505 servname_type = *(sdata++);
510 *al = SSL_AD_DECODE_ERROR;
513 if (s->servername_done == 0)
514 switch (servname_type) {
515 case TLSEXT_NAMETYPE_host_name:
517 if (s->session->tlsext_hostname) {
518 *al = SSL_AD_DECODE_ERROR;
521 if (len > TLSEXT_MAXLEN_host_name) {
522 *al = TLS1_AD_UNRECOGNIZED_NAME;
525 if ((s->session->tlsext_hostname =
526 OPENSSL_malloc(len + 1)) == NULL) {
527 *al = TLS1_AD_INTERNAL_ERROR;
530 memcpy(s->session->tlsext_hostname, sdata, len);
531 s->session->tlsext_hostname[len] = '\0';
532 if (strlen(s->session->tlsext_hostname) != len) {
533 OPENSSL_free(s->session->tlsext_hostname);
534 s->session->tlsext_hostname = NULL;
535 *al = TLS1_AD_UNRECOGNIZED_NAME;
538 s->servername_done = 1;
541 s->servername_done = s->session->tlsext_hostname
542 && strlen(s->session->tlsext_hostname) == len
543 && strncmp(s->session->tlsext_hostname,
544 (char *)sdata, len) == 0;
555 *al = SSL_AD_DECODE_ERROR;
559 } else if (type == TLSEXT_TYPE_renegotiate) {
560 if (!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
562 renegotiate_seen = 1;
563 } else if (type == TLSEXT_TYPE_status_request &&
564 s->version != DTLS1_VERSION && s->ctx->tlsext_status_cb) {
567 *al = SSL_AD_DECODE_ERROR;
571 s->tlsext_status_type = *data++;
573 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
574 const unsigned char *sdata;
576 /* Read in responder_id_list */
580 *al = SSL_AD_DECODE_ERROR;
587 *al = SSL_AD_DECODE_ERROR;
594 *al = SSL_AD_DECODE_ERROR;
599 id = d2i_OCSP_RESPID(NULL, &sdata, idsize);
601 *al = SSL_AD_DECODE_ERROR;
605 OCSP_RESPID_free(id);
606 *al = SSL_AD_DECODE_ERROR;
609 if (!s->tlsext_ocsp_ids
610 && !(s->tlsext_ocsp_ids =
611 sk_OCSP_RESPID_new_null())) {
612 OCSP_RESPID_free(id);
613 *al = SSL_AD_INTERNAL_ERROR;
616 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
617 OCSP_RESPID_free(id);
618 *al = SSL_AD_INTERNAL_ERROR;
623 /* Read in request_extensions */
625 *al = SSL_AD_DECODE_ERROR;
631 *al = SSL_AD_DECODE_ERROR;
636 if (s->tlsext_ocsp_exts) {
637 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
638 X509_EXTENSION_free);
641 s->tlsext_ocsp_exts =
642 d2i_X509_EXTENSIONS(NULL, &sdata, dsize);
643 if (!s->tlsext_ocsp_exts || (data + dsize != sdata)) {
644 *al = SSL_AD_DECODE_ERROR;
650 * We don't know what to do with any other type * so ignore it.
653 s->tlsext_status_type = -1;
656 /* session ticket processed earlier */
664 /* Need RI if renegotiating */
666 if (!renegotiate_seen && s->new_session &&
667 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
668 *al = SSL_AD_HANDSHAKE_FAILURE;
669 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,
670 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
677 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d,
680 unsigned short length;
683 unsigned char *data = *p;
684 int tlsext_servername = 0;
685 int renegotiate_seen = 0;
687 if (data >= (d + n - 2))
691 if (data + length != d + n) {
692 *al = SSL_AD_DECODE_ERROR;
696 while (data <= (d + n - 4)) {
700 if (data + size > (d + n))
703 if (s->tlsext_debug_cb)
704 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
706 if (type == TLSEXT_TYPE_server_name) {
707 if (s->tlsext_hostname == NULL || size > 0) {
708 *al = TLS1_AD_UNRECOGNIZED_NAME;
711 tlsext_servername = 1;
712 } else if (type == TLSEXT_TYPE_session_ticket) {
713 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
715 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
718 s->tlsext_ticket_expected = 1;
719 } else if (type == TLSEXT_TYPE_status_request &&
720 s->version != DTLS1_VERSION) {
722 * MUST be empty and only sent if we've requested a status
725 if ((s->tlsext_status_type == -1) || (size > 0)) {
726 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
729 /* Set flag to expect CertificateStatus message */
730 s->tlsext_status_expected = 1;
731 } else if (type == TLSEXT_TYPE_renegotiate) {
732 if (!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
734 renegotiate_seen = 1;
740 *al = SSL_AD_DECODE_ERROR;
744 if (!s->hit && tlsext_servername == 1) {
745 if (s->tlsext_hostname) {
746 if (s->session->tlsext_hostname == NULL) {
747 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
748 if (!s->session->tlsext_hostname) {
749 *al = SSL_AD_UNRECOGNIZED_NAME;
753 *al = SSL_AD_DECODE_ERROR;
764 * Determine if we need to see RI. Strictly speaking if we want to avoid
765 * an attack we should *always* see RI even on initial server hello
766 * because the client doesn't see any renegotiation during an attack.
767 * However this would mean we could not connect to any server which
768 * doesn't support RI so for the immediate future tolerate RI absence on
769 * initial connect only.
771 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
772 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
773 *al = SSL_AD_HANDSHAKE_FAILURE;
774 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,
775 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
782 int ssl_check_clienthello_tlsext_early(SSL *s)
784 int ret = SSL_TLSEXT_ERR_NOACK;
785 int al = SSL_AD_UNRECOGNIZED_NAME;
787 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
789 s->ctx->tlsext_servername_callback(s, &al,
790 s->ctx->tlsext_servername_arg);
791 else if (s->initial_ctx != NULL
792 && s->initial_ctx->tlsext_servername_callback != 0)
794 s->initial_ctx->tlsext_servername_callback(s, &al,
796 initial_ctx->tlsext_servername_arg);
799 case SSL_TLSEXT_ERR_ALERT_FATAL:
800 ssl3_send_alert(s, SSL3_AL_FATAL, al);
803 case SSL_TLSEXT_ERR_ALERT_WARNING:
804 ssl3_send_alert(s, SSL3_AL_WARNING, al);
807 case SSL_TLSEXT_ERR_NOACK:
808 s->servername_done = 0;
815 int ssl_check_clienthello_tlsext_late(SSL *s)
817 int ret = SSL_TLSEXT_ERR_OK;
821 * If status request then ask callback what to do. Note: this must be
822 * called after servername callbacks in case the certificate has
823 * changed, and must be called after the cipher has been chosen because
824 * this may influence which certificate is sent
826 if (s->tlsext_status_type != -1 && s->ctx && s->ctx->tlsext_status_cb) {
829 certpkey = ssl_get_server_send_pkey(s);
830 /* If no certificate can't return certificate status */
831 if (certpkey == NULL) {
832 s->tlsext_status_expected = 0;
836 * Set current certificate to one we will use so SSL_get_certificate
837 * et al can pick it up.
839 s->cert->key = certpkey;
840 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
842 /* We don't want to send a status request response */
843 case SSL_TLSEXT_ERR_NOACK:
844 s->tlsext_status_expected = 0;
846 /* status request response should be sent */
847 case SSL_TLSEXT_ERR_OK:
848 if (s->tlsext_ocsp_resp)
849 s->tlsext_status_expected = 1;
851 s->tlsext_status_expected = 0;
853 /* something bad happened */
854 case SSL_TLSEXT_ERR_ALERT_FATAL:
855 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
856 al = SSL_AD_INTERNAL_ERROR;
860 s->tlsext_status_expected = 0;
864 case SSL_TLSEXT_ERR_ALERT_FATAL:
865 ssl3_send_alert(s, SSL3_AL_FATAL, al);
868 case SSL_TLSEXT_ERR_ALERT_WARNING:
869 ssl3_send_alert(s, SSL3_AL_WARNING, al);
877 int ssl_check_serverhello_tlsext(SSL *s)
879 int ret = SSL_TLSEXT_ERR_NOACK;
880 int al = SSL_AD_UNRECOGNIZED_NAME;
882 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
884 s->ctx->tlsext_servername_callback(s, &al,
885 s->ctx->tlsext_servername_arg);
886 else if (s->initial_ctx != NULL
887 && s->initial_ctx->tlsext_servername_callback != 0)
889 s->initial_ctx->tlsext_servername_callback(s, &al,
891 initial_ctx->tlsext_servername_arg);
894 * If we've requested certificate status and we wont get one tell the
897 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
898 && s->ctx->tlsext_status_cb) {
901 * Set resp to NULL, resplen to -1 so callback knows there is no
904 if (s->tlsext_ocsp_resp) {
905 OPENSSL_free(s->tlsext_ocsp_resp);
906 s->tlsext_ocsp_resp = NULL;
908 s->tlsext_ocsp_resplen = -1;
909 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
911 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
912 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
915 al = SSL_AD_INTERNAL_ERROR;
916 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
921 case SSL_TLSEXT_ERR_ALERT_FATAL:
922 ssl3_send_alert(s, SSL3_AL_FATAL, al);
925 case SSL_TLSEXT_ERR_ALERT_WARNING:
926 ssl3_send_alert(s, SSL3_AL_WARNING, al);
929 case SSL_TLSEXT_ERR_NOACK:
930 s->servername_done = 0;
937 * Since the server cache lookup is done early on in the processing of client
938 * hello and other operations depend on the result we need to handle any TLS
939 * session ticket extension at the same time.
942 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
943 const unsigned char *limit, SSL_SESSION **ret)
945 /* Point after session ID in client hello */
946 const unsigned char *p = session_id + len;
950 * If tickets disabled behave as if no ticket present to permit stateful
953 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
956 if ((s->version <= SSL3_VERSION) || !limit)
960 /* Skip past DTLS cookie */
961 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) {
967 /* Skip past cipher list */
972 /* Skip past compression algorithm list */
977 /* Now at start of extensions */
978 if ((p + 2) >= limit)
981 while ((p + 4) <= limit) {
982 unsigned short type, size;
985 if (p + size > limit)
987 if (type == TLSEXT_TYPE_session_ticket) {
989 * If zero length note client will accept a ticket and indicate
990 * cache miss to trigger full handshake
993 s->tlsext_ticket_expected = 1;
994 return 0; /* Cache miss */
996 return tls_decrypt_ticket(s, p, size, session_id, len, ret);
1003 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
1004 int eticklen, const unsigned char *sess_id,
1005 int sesslen, SSL_SESSION **psess)
1008 unsigned char *sdec;
1009 const unsigned char *p;
1010 int slen, mlen, renew_ticket = 0;
1011 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
1014 SSL_CTX *tctx = s->initial_ctx;
1015 /* Need at least keyname + iv + some encrypted data */
1018 /* Initialize session ticket encryption and HMAC contexts */
1019 HMAC_CTX_init(&hctx);
1020 EVP_CIPHER_CTX_init(&ctx);
1021 if (tctx->tlsext_ticket_key_cb) {
1022 unsigned char *nctick = (unsigned char *)etick;
1023 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
1032 /* Check key name matches */
1033 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
1035 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
1036 tlsext_tick_md(), NULL);
1037 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
1038 tctx->tlsext_tick_aes_key, etick + 16);
1041 * Attempt to process session ticket, first conduct sanity and integrity
1044 mlen = HMAC_size(&hctx);
1046 /* Check HMAC of encrypted ticket */
1047 HMAC_Update(&hctx, etick, eticklen);
1048 HMAC_Final(&hctx, tick_hmac, NULL);
1049 HMAC_CTX_cleanup(&hctx);
1050 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
1051 EVP_CIPHER_CTX_cleanup(&ctx);
1054 /* Attempt to decrypt session data */
1055 /* Move p after IV to start of encrypted ticket, update length */
1056 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
1057 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
1058 sdec = OPENSSL_malloc(eticklen);
1060 EVP_CIPHER_CTX_cleanup(&ctx);
1063 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
1064 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0) {
1065 EVP_CIPHER_CTX_cleanup(&ctx);
1070 EVP_CIPHER_CTX_cleanup(&ctx);
1073 sess = d2i_SSL_SESSION(NULL, &p, slen);
1077 * The session ID if non-empty is used by some clients to detect that
1078 * the ticket has been accepted. So we copy it to the session
1079 * structure. If it is empty set length to zero as required by
1083 memcpy(sess->session_id, sess_id, sesslen);
1084 sess->session_id_length = sesslen;
1086 s->tlsext_ticket_expected = renew_ticket;
1090 * If session decrypt failure indicate a cache miss and set state to send
1094 s->tlsext_ticket_expected = 1;