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 *limit)
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 (limit - data <= 2)
393 if (limit - data < 4)
398 if (type != TLSEXT_TYPE_server_name)
401 if (limit - data < size)
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 (limit - data != (int)(len1 + len2))
411 if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
413 if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
416 const size_t len = sizeof(kSafariExtensionsBlock);
418 if (limit - data != (int)(len))
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,
429 unsigned char *limit, int *al)
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, limit);
443 # endif /* !OPENSSL_NO_EC */
448 if (limit - data < 2)
453 if (limit - data != len)
456 while (limit - data >= 4) {
460 if (limit - data < size)
462 if (s->tlsext_debug_cb)
463 s->tlsext_debug_cb(s, 0, type, data, size, s->tlsext_debug_arg);
465 * The servername extension is treated as follows:
467 * - Only the hostname type is supported with a maximum length of 255.
468 * - The servername is rejected if too long or if it contains zeros,
469 * in which case an fatal alert is generated.
470 * - The servername field is maintained together with the session cache.
471 * - When a session is resumed, the servername call back invoked in order
472 * to allow the application to position itself to the right context.
473 * - The servername is acknowledged if it is new for a session or when
474 * it is identical to a previously used for the same session.
475 * Applications can control the behaviour. They can at any time
476 * set a 'desirable' servername for a new SSL object. This can be the
477 * case for example with HTTPS when a Host: header field is received and
478 * a renegotiation is requested. In this case, a possible servername
479 * presented in the new client hello is only acknowledged if it matches
480 * the value of the Host: field.
481 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
482 * if they provide for changing an explicit servername context for the
483 * session, i.e. when the session has been established with a servername
485 * - On session reconnect, the servername extension may be absent.
489 if (type == TLSEXT_TYPE_server_name) {
490 unsigned char *sdata;
495 *al = SSL_AD_DECODE_ERROR;
501 *al = SSL_AD_DECODE_ERROR;
507 servname_type = *(sdata++);
512 *al = SSL_AD_DECODE_ERROR;
515 if (s->servername_done == 0)
516 switch (servname_type) {
517 case TLSEXT_NAMETYPE_host_name:
519 if (s->session->tlsext_hostname) {
520 *al = SSL_AD_DECODE_ERROR;
523 if (len > TLSEXT_MAXLEN_host_name) {
524 *al = TLS1_AD_UNRECOGNIZED_NAME;
527 if ((s->session->tlsext_hostname =
528 OPENSSL_malloc(len + 1)) == NULL) {
529 *al = TLS1_AD_INTERNAL_ERROR;
532 memcpy(s->session->tlsext_hostname, sdata, len);
533 s->session->tlsext_hostname[len] = '\0';
534 if (strlen(s->session->tlsext_hostname) != len) {
535 OPENSSL_free(s->session->tlsext_hostname);
536 s->session->tlsext_hostname = NULL;
537 *al = TLS1_AD_UNRECOGNIZED_NAME;
540 s->servername_done = 1;
543 s->servername_done = s->session->tlsext_hostname
544 && strlen(s->session->tlsext_hostname) == len
545 && strncmp(s->session->tlsext_hostname,
546 (char *)sdata, len) == 0;
557 *al = SSL_AD_DECODE_ERROR;
561 } else if (type == TLSEXT_TYPE_renegotiate) {
562 if (!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
564 renegotiate_seen = 1;
565 } else if (type == TLSEXT_TYPE_status_request &&
566 s->version != DTLS1_VERSION && s->ctx->tlsext_status_cb) {
569 *al = SSL_AD_DECODE_ERROR;
573 s->tlsext_status_type = *data++;
575 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
576 const unsigned char *sdata;
578 /* Read in responder_id_list */
582 *al = SSL_AD_DECODE_ERROR;
587 * We remove any OCSP_RESPIDs from a previous handshake
588 * to prevent unbounded memory growth - CVE-2016-6304
590 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids,
593 s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null();
594 if (s->tlsext_ocsp_ids == NULL) {
595 *al = SSL_AD_INTERNAL_ERROR;
599 s->tlsext_ocsp_ids = NULL;
606 *al = SSL_AD_DECODE_ERROR;
613 *al = SSL_AD_DECODE_ERROR;
618 id = d2i_OCSP_RESPID(NULL, &sdata, idsize);
620 *al = SSL_AD_DECODE_ERROR;
624 OCSP_RESPID_free(id);
625 *al = SSL_AD_DECODE_ERROR;
628 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
629 OCSP_RESPID_free(id);
630 *al = SSL_AD_INTERNAL_ERROR;
635 /* Read in request_extensions */
637 *al = SSL_AD_DECODE_ERROR;
643 *al = SSL_AD_DECODE_ERROR;
648 if (s->tlsext_ocsp_exts) {
649 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
650 X509_EXTENSION_free);
653 s->tlsext_ocsp_exts =
654 d2i_X509_EXTENSIONS(NULL, &sdata, dsize);
655 if (!s->tlsext_ocsp_exts || (data + dsize != sdata)) {
656 *al = SSL_AD_DECODE_ERROR;
662 * We don't know what to do with any other type * so ignore it.
665 s->tlsext_status_type = -1;
668 /* session ticket processed earlier */
676 /* Need RI if renegotiating */
678 if (!renegotiate_seen && s->new_session &&
679 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
680 *al = SSL_AD_HANDSHAKE_FAILURE;
681 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,
682 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
689 *al = SSL_AD_DECODE_ERROR;
693 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d,
696 unsigned short length;
699 unsigned char *data = *p;
700 int tlsext_servername = 0;
701 int renegotiate_seen = 0;
703 if ((d + n) - data <= 2)
707 if ((d + n) - data != length) {
708 *al = SSL_AD_DECODE_ERROR;
712 while ((d + n) - data >= 4) {
716 if ((d + n) - data < size)
719 if (s->tlsext_debug_cb)
720 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
722 if (type == TLSEXT_TYPE_server_name) {
723 if (s->tlsext_hostname == NULL || size > 0) {
724 *al = TLS1_AD_UNRECOGNIZED_NAME;
727 tlsext_servername = 1;
728 } else if (type == TLSEXT_TYPE_session_ticket) {
729 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
731 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
734 s->tlsext_ticket_expected = 1;
735 } else if (type == TLSEXT_TYPE_status_request &&
736 s->version != DTLS1_VERSION) {
738 * MUST be empty and only sent if we've requested a status
741 if ((s->tlsext_status_type == -1) || (size > 0)) {
742 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
745 /* Set flag to expect CertificateStatus message */
746 s->tlsext_status_expected = 1;
747 } else if (type == TLSEXT_TYPE_renegotiate) {
748 if (!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
750 renegotiate_seen = 1;
756 *al = SSL_AD_DECODE_ERROR;
760 if (!s->hit && tlsext_servername == 1) {
761 if (s->tlsext_hostname) {
762 if (s->session->tlsext_hostname == NULL) {
763 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
764 if (!s->session->tlsext_hostname) {
765 *al = SSL_AD_UNRECOGNIZED_NAME;
769 *al = SSL_AD_DECODE_ERROR;
780 * Determine if we need to see RI. Strictly speaking if we want to avoid
781 * an attack we should *always* see RI even on initial server hello
782 * because the client doesn't see any renegotiation during an attack.
783 * However this would mean we could not connect to any server which
784 * doesn't support RI so for the immediate future tolerate RI absence on
785 * initial connect only.
787 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
788 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
789 *al = SSL_AD_HANDSHAKE_FAILURE;
790 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,
791 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
798 int ssl_check_clienthello_tlsext_early(SSL *s)
800 int ret = SSL_TLSEXT_ERR_NOACK;
801 int al = SSL_AD_UNRECOGNIZED_NAME;
803 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
805 s->ctx->tlsext_servername_callback(s, &al,
806 s->ctx->tlsext_servername_arg);
807 else if (s->initial_ctx != NULL
808 && s->initial_ctx->tlsext_servername_callback != 0)
810 s->initial_ctx->tlsext_servername_callback(s, &al,
812 initial_ctx->tlsext_servername_arg);
815 case SSL_TLSEXT_ERR_ALERT_FATAL:
816 ssl3_send_alert(s, SSL3_AL_FATAL, al);
819 case SSL_TLSEXT_ERR_ALERT_WARNING:
820 ssl3_send_alert(s, SSL3_AL_WARNING, al);
823 case SSL_TLSEXT_ERR_NOACK:
824 s->servername_done = 0;
831 int ssl_check_clienthello_tlsext_late(SSL *s)
833 int ret = SSL_TLSEXT_ERR_OK;
837 * If status request then ask callback what to do. Note: this must be
838 * called after servername callbacks in case the certificate has
839 * changed, and must be called after the cipher has been chosen because
840 * this may influence which certificate is sent
842 if (s->tlsext_status_type != -1 && s->ctx && s->ctx->tlsext_status_cb) {
845 certpkey = ssl_get_server_send_pkey(s);
846 /* If no certificate can't return certificate status */
847 if (certpkey == NULL) {
848 s->tlsext_status_expected = 0;
852 * Set current certificate to one we will use so SSL_get_certificate
853 * et al can pick it up.
855 s->cert->key = certpkey;
856 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
858 /* We don't want to send a status request response */
859 case SSL_TLSEXT_ERR_NOACK:
860 s->tlsext_status_expected = 0;
862 /* status request response should be sent */
863 case SSL_TLSEXT_ERR_OK:
864 if (s->tlsext_ocsp_resp)
865 s->tlsext_status_expected = 1;
867 s->tlsext_status_expected = 0;
869 /* something bad happened */
870 case SSL_TLSEXT_ERR_ALERT_FATAL:
871 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
872 al = SSL_AD_INTERNAL_ERROR;
876 s->tlsext_status_expected = 0;
880 case SSL_TLSEXT_ERR_ALERT_FATAL:
881 ssl3_send_alert(s, SSL3_AL_FATAL, al);
884 case SSL_TLSEXT_ERR_ALERT_WARNING:
885 ssl3_send_alert(s, SSL3_AL_WARNING, al);
893 int ssl_check_serverhello_tlsext(SSL *s)
895 int ret = SSL_TLSEXT_ERR_NOACK;
896 int al = SSL_AD_UNRECOGNIZED_NAME;
898 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
900 s->ctx->tlsext_servername_callback(s, &al,
901 s->ctx->tlsext_servername_arg);
902 else if (s->initial_ctx != NULL
903 && s->initial_ctx->tlsext_servername_callback != 0)
905 s->initial_ctx->tlsext_servername_callback(s, &al,
907 initial_ctx->tlsext_servername_arg);
910 * If we've requested certificate status and we wont get one tell the
913 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
914 && s->ctx->tlsext_status_cb) {
917 * Set resp to NULL, resplen to -1 so callback knows there is no
920 if (s->tlsext_ocsp_resp) {
921 OPENSSL_free(s->tlsext_ocsp_resp);
922 s->tlsext_ocsp_resp = NULL;
924 s->tlsext_ocsp_resplen = -1;
925 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
927 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
928 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
931 al = SSL_AD_INTERNAL_ERROR;
932 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
937 case SSL_TLSEXT_ERR_ALERT_FATAL:
938 ssl3_send_alert(s, SSL3_AL_FATAL, al);
941 case SSL_TLSEXT_ERR_ALERT_WARNING:
942 ssl3_send_alert(s, SSL3_AL_WARNING, al);
945 case SSL_TLSEXT_ERR_NOACK:
946 s->servername_done = 0;
953 * Since the server cache lookup is done early on in the processing of client
954 * hello and other operations depend on the result we need to handle any TLS
955 * session ticket extension at the same time.
958 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
959 const unsigned char *limit, SSL_SESSION **ret)
961 /* Point after session ID in client hello */
962 const unsigned char *p = session_id + len;
966 * If tickets disabled behave as if no ticket present to permit stateful
969 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
972 if ((s->version <= SSL3_VERSION) || !limit)
976 /* Skip past DTLS cookie */
977 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) {
985 /* Skip past cipher list */
991 /* Skip past compression algorithm list */
997 /* Now at start of extensions */
1001 while (limit - p >= 4) {
1002 unsigned short type, size;
1005 if (limit - p < size)
1007 if (type == TLSEXT_TYPE_session_ticket) {
1009 * If zero length note client will accept a ticket and indicate
1010 * cache miss to trigger full handshake
1013 s->tlsext_ticket_expected = 1;
1014 return 0; /* Cache miss */
1016 return tls_decrypt_ticket(s, p, size, session_id, len, ret);
1023 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
1024 int eticklen, const unsigned char *sess_id,
1025 int sesslen, SSL_SESSION **psess)
1028 unsigned char *sdec;
1029 const unsigned char *p;
1030 int slen, mlen, renew_ticket = 0;
1031 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
1034 SSL_CTX *tctx = s->initial_ctx;
1036 /* Initialize session ticket encryption and HMAC contexts */
1037 HMAC_CTX_init(&hctx);
1038 EVP_CIPHER_CTX_init(&ctx);
1039 if (tctx->tlsext_ticket_key_cb) {
1040 unsigned char *nctick = (unsigned char *)etick;
1041 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
1050 /* Check key name matches */
1051 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
1053 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
1054 tlsext_tick_md(), NULL);
1055 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
1056 tctx->tlsext_tick_aes_key, etick + 16);
1059 * Attempt to process session ticket, first conduct sanity and integrity
1062 mlen = HMAC_size(&hctx);
1063 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
1064 if (eticklen <= 16 + EVP_CIPHER_CTX_iv_length(&ctx) + mlen) {
1065 HMAC_CTX_cleanup(&hctx);
1066 EVP_CIPHER_CTX_cleanup(&ctx);
1071 /* Check HMAC of encrypted ticket */
1072 HMAC_Update(&hctx, etick, eticklen);
1073 HMAC_Final(&hctx, tick_hmac, NULL);
1074 HMAC_CTX_cleanup(&hctx);
1075 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
1076 EVP_CIPHER_CTX_cleanup(&ctx);
1079 /* Attempt to decrypt session data */
1080 /* Move p after IV to start of encrypted ticket, update length */
1081 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
1082 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
1083 sdec = OPENSSL_malloc(eticklen);
1085 EVP_CIPHER_CTX_cleanup(&ctx);
1088 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
1089 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0) {
1090 EVP_CIPHER_CTX_cleanup(&ctx);
1095 EVP_CIPHER_CTX_cleanup(&ctx);
1098 sess = d2i_SSL_SESSION(NULL, &p, slen);
1102 * The session ID if non-empty is used by some clients to detect that
1103 * the ticket has been accepted. So we copy it to the session
1104 * structure. If it is empty set length to zero as required by
1108 memcpy(sess->session_id, sess_id, sesslen);
1109 sess->session_id_length = sesslen;
1111 s->tlsext_ticket_expected = renew_ticket;
1115 * If session decrypt failure indicate a cache miss and set state to send
1119 s->tlsext_ticket_expected = 1;