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)
90 /* 2 hours, the 24 hours mentioned in the TLSv1 spec
91 * is way too long for http, the cache would over fill */
95 IMPLEMENT_tls1_meth_func(tlsv1_base_method,
96 ssl_undefined_function,
97 ssl_undefined_function,
102 if (!ssl3_new(s)) return(0);
103 s->method->ssl_clear(s);
107 void tls1_free(SSL *s)
112 void tls1_clear(SSL *s)
115 s->version=TLS1_VERSION;
119 long tls1_ctrl(SSL *s, int cmd, long larg, char *parg)
124 long tls1_callback_ctrl(SSL *s, int cmd, void *(*fp)())
130 #ifndef OPENSSL_NO_TLSEXT
131 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
134 unsigned char *ret = p;
136 /* don't add extensions for SSLv3 */
137 if (s->client_version == SSL3_VERSION)
142 if (ret>=limit) return NULL; /* this really never occurs, but ... */
144 if (s->tlsext_hostname != NULL)
146 /* Add TLS extension servername to the Client Hello message */
147 unsigned long size_str;
150 /* check for enough space.
151 4 for the servername type and entension length
152 2 for servernamelist length
153 1 for the hostname type
154 2 for hostname length
158 if ((lenmax = limit - ret - 9) < 0
159 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
162 /* extension type and length */
163 s2n(TLSEXT_TYPE_server_name,ret);
166 /* length of servername list */
169 /* hostname type, length and hostname */
170 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
172 memcpy(ret, s->tlsext_hostname, size_str);
177 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
180 if (s->session && s->session->tlsext_tick)
181 ticklen = s->session->tlsext_ticklen;
184 /* Check for enough room 2 for extension type, 2 for len
187 if (limit - ret - 4 - ticklen < 0)
189 s2n(TLSEXT_TYPE_session_ticket,ret);
193 memcpy(ret, s->session->tlsext_tick, ticklen);
198 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
201 long extlen, idlen, itmp;
205 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
207 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
208 itmp = i2d_OCSP_RESPID(id, NULL);
214 if (s->tlsext_ocsp_exts)
216 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
223 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
224 s2n(TLSEXT_TYPE_status_request, ret);
225 if (extlen + idlen > 0xFFF0)
227 s2n(extlen + idlen + 5, ret);
228 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
230 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
232 /* save position of id len */
233 unsigned char *q = ret;
234 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
235 /* skip over id len */
237 itmp = i2d_OCSP_RESPID(id, &ret);
243 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
246 if ((extdatalen = ret-p-2)== 0)
253 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
256 unsigned char *ret = p;
258 /* don't add extensions for SSLv3 */
259 if (s->version == SSL3_VERSION)
263 if (ret>=limit) return NULL; /* this really never occurs, but ... */
265 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
267 if (limit - ret - 4 < 0) return NULL;
269 s2n(TLSEXT_TYPE_server_name,ret);
273 if (s->tlsext_ticket_expected
274 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
276 if (limit - ret - 4 < 0) return NULL;
277 s2n(TLSEXT_TYPE_session_ticket,ret);
281 if (s->tlsext_status_expected)
283 if ((long)(limit - ret - 4) < 0) return NULL;
284 s2n(TLSEXT_TYPE_status_request,ret);
288 if ((extdatalen = ret-p-2)== 0)
295 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
300 unsigned char *data = *p;
301 s->servername_done = 0;
302 s->tlsext_status_type = -1;
308 if (data > (d+n-len))
311 while (data <= (d+n-4))
316 if (data+size > (d+n))
319 if (s->tlsext_debug_cb)
320 s->tlsext_debug_cb(s, 0, type, data, size,
321 s->tlsext_debug_arg);
322 /* The servername extension is treated as follows:
324 - Only the hostname type is supported with a maximum length of 255.
325 - The servername is rejected if too long or if it contains zeros,
326 in which case an fatal alert is generated.
327 - The servername field is maintained together with the session cache.
328 - When a session is resumed, the servername call back invoked in order
329 to allow the application to position itself to the right context.
330 - The servername is acknowledged if it is new for a session or when
331 it is identical to a previously used for the same session.
332 Applications can control the behaviour. They can at any time
333 set a 'desirable' servername for a new SSL object. This can be the
334 case for example with HTTPS when a Host: header field is received and
335 a renegotiation is requested. In this case, a possible servername
336 presented in the new client hello is only acknowledged if it matches
337 the value of the Host: field.
338 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
339 if they provide for changing an explicit servername context for the session,
340 i.e. when the session has been established with a servername extension.
341 - On session reconnect, the servername extension may be absent.
345 if (type == TLSEXT_TYPE_server_name)
347 unsigned char *sdata;
353 *al = SSL_AD_DECODE_ERROR;
360 *al = SSL_AD_DECODE_ERROR;
367 servname_type = *(sdata++);
373 *al = SSL_AD_DECODE_ERROR;
376 if (s->servername_done == 0)
377 switch (servname_type)
379 case TLSEXT_NAMETYPE_host_name:
380 if (s->session->tlsext_hostname == NULL)
382 if (len > TLSEXT_MAXLEN_host_name ||
383 ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL))
385 *al = TLS1_AD_UNRECOGNIZED_NAME;
388 memcpy(s->session->tlsext_hostname, sdata, len);
389 s->session->tlsext_hostname[len]='\0';
390 if (strlen(s->session->tlsext_hostname) != len) {
391 OPENSSL_free(s->session->tlsext_hostname);
392 s->session->tlsext_hostname = NULL;
393 *al = TLS1_AD_UNRECOGNIZED_NAME;
396 s->servername_done = 1;
400 s->servername_done = strlen(s->session->tlsext_hostname) == len
401 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
413 *al = SSL_AD_DECODE_ERROR;
418 else if (type == TLSEXT_TYPE_status_request
419 && s->ctx->tlsext_status_cb)
424 *al = SSL_AD_DECODE_ERROR;
428 s->tlsext_status_type = *data++;
430 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
432 const unsigned char *sdata;
434 /* Read in responder_id_list */
439 *al = SSL_AD_DECODE_ERROR;
448 *al = SSL_AD_DECODE_ERROR;
455 *al = SSL_AD_DECODE_ERROR;
460 id = d2i_OCSP_RESPID(NULL,
464 *al = SSL_AD_DECODE_ERROR;
469 OCSP_RESPID_free(id);
470 *al = SSL_AD_DECODE_ERROR;
473 if (!s->tlsext_ocsp_ids
474 && !(s->tlsext_ocsp_ids =
475 sk_OCSP_RESPID_new_null()))
477 OCSP_RESPID_free(id);
478 *al = SSL_AD_INTERNAL_ERROR;
481 if (!sk_OCSP_RESPID_push(
482 s->tlsext_ocsp_ids, id))
484 OCSP_RESPID_free(id);
485 *al = SSL_AD_INTERNAL_ERROR;
490 /* Read in request_extensions */
495 *al = SSL_AD_DECODE_ERROR;
501 s->tlsext_ocsp_exts =
502 d2i_X509_EXTENSIONS(NULL,
504 if (!s->tlsext_ocsp_exts
505 || (data + dsize != sdata))
507 *al = SSL_AD_DECODE_ERROR;
512 /* We don't know what to do with any other type
516 s->tlsext_status_type = -1;
518 /* session ticket processed earlier */
527 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
532 unsigned char *data = *p;
534 int tlsext_servername = 0;
541 while(data <= (d+n-4))
546 if (data+size > (d+n))
549 if (s->tlsext_debug_cb)
550 s->tlsext_debug_cb(s, 1, type, data, size,
551 s->tlsext_debug_arg);
553 if (type == TLSEXT_TYPE_server_name)
555 if (s->tlsext_hostname == NULL || size > 0)
557 *al = TLS1_AD_UNRECOGNIZED_NAME;
560 tlsext_servername = 1;
562 else if (type == TLSEXT_TYPE_session_ticket)
564 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
567 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
570 s->tlsext_ticket_expected = 1;
572 else if (type == TLSEXT_TYPE_status_request)
574 /* MUST be empty and only sent if we've requested
575 * a status request message.
577 if ((s->tlsext_status_type == -1) || (size > 0))
579 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
582 /* Set flag to expect CertificateStatus message */
583 s->tlsext_status_expected = 1;
591 *al = SSL_AD_DECODE_ERROR;
595 if (!s->hit && tlsext_servername == 1)
597 if (s->tlsext_hostname)
599 if (s->session->tlsext_hostname == NULL)
601 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
602 if (!s->session->tlsext_hostname)
604 *al = SSL_AD_UNRECOGNIZED_NAME;
610 *al = SSL_AD_DECODE_ERROR;
620 int ssl_check_clienthello_tlsext(SSL *s)
622 int ret=SSL_TLSEXT_ERR_NOACK;
623 int al = SSL_AD_UNRECOGNIZED_NAME;
625 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
626 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
627 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
628 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
630 /* If status request then ask callback what to do.
631 * Note: this must be called after servername callbacks in case
632 * the certificate has changed.
634 if ((s->tlsext_status_type != -1) && s->ctx->tlsext_status_cb)
637 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
640 /* We don't want to send a status request response */
641 case SSL_TLSEXT_ERR_NOACK:
642 s->tlsext_status_expected = 0;
644 /* status request response should be sent */
645 case SSL_TLSEXT_ERR_OK:
646 if (s->tlsext_ocsp_resp)
647 s->tlsext_status_expected = 1;
649 s->tlsext_status_expected = 0;
651 /* something bad happened */
652 case SSL_TLSEXT_ERR_ALERT_FATAL:
653 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
654 al = SSL_AD_INTERNAL_ERROR;
659 s->tlsext_status_expected = 0;
663 case SSL_TLSEXT_ERR_ALERT_FATAL:
664 ssl3_send_alert(s,SSL3_AL_FATAL,al);
667 case SSL_TLSEXT_ERR_ALERT_WARNING:
668 ssl3_send_alert(s,SSL3_AL_WARNING,al);
671 case SSL_TLSEXT_ERR_NOACK:
672 s->servername_done=0;
678 int ssl_check_serverhello_tlsext(SSL *s)
680 int ret=SSL_TLSEXT_ERR_NOACK;
681 int al = SSL_AD_UNRECOGNIZED_NAME;
683 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
684 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
685 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
686 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
688 /* If we've requested certificate status and we wont get one
691 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
692 && s->ctx->tlsext_status_cb)
695 /* Set resp to NULL, resplen to -1 so callback knows
696 * there is no response.
698 if (s->tlsext_ocsp_resp)
700 OPENSSL_free(s->tlsext_ocsp_resp);
701 s->tlsext_ocsp_resp = NULL;
703 s->tlsext_ocsp_resplen = -1;
704 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
707 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
708 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
712 al = SSL_AD_INTERNAL_ERROR;
713 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
719 case SSL_TLSEXT_ERR_ALERT_FATAL:
720 ssl3_send_alert(s,SSL3_AL_FATAL,al);
723 case SSL_TLSEXT_ERR_ALERT_WARNING:
724 ssl3_send_alert(s,SSL3_AL_WARNING,al);
727 case SSL_TLSEXT_ERR_NOACK:
728 s->servername_done=0;
734 /* Since the server cache lookup is done early on in the processing of client
735 * hello and other operations depend on the result we need to handle any TLS
736 * session ticket extension at the same time.
739 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
740 const unsigned char *limit, SSL_SESSION **ret)
742 /* Point after session ID in client hello */
743 const unsigned char *p = session_id + len;
746 /* If tickets disabled behave as if no ticket present
747 * to permit stateful resumption.
749 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
752 if ((s->version <= SSL3_VERSION) || !limit)
756 /* Skip past DTLS cookie */
757 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
764 /* Skip past cipher list */
769 /* Skip past compression algorithm list */
774 /* Now at start of extensions */
775 if ((p + 2) >= limit)
778 while ((p + 4) <= limit)
780 unsigned short type, size;
783 if (p + size > limit)
785 if (type == TLSEXT_TYPE_session_ticket)
787 /* If zero length note client will accept a ticket
788 * and indicate cache miss to trigger full handshake
792 s->tlsext_ticket_expected = 1;
793 return 0; /* Cache miss */
795 return tls_decrypt_ticket(s, p, size, session_id, len,
803 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
804 const unsigned char *sess_id, int sesslen,
809 const unsigned char *p;
810 int slen, mlen, renew_ticket = 0;
811 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
814 SSL_CTX *tctx = s->initial_ctx;
815 /* Need at least keyname + iv + some encrypted data */
818 /* Initialize session ticket encryption and HMAC contexts */
819 HMAC_CTX_init(&hctx);
820 EVP_CIPHER_CTX_init(&ctx);
821 if (tctx->tlsext_ticket_key_cb)
823 unsigned char *nctick = (unsigned char *)etick;
824 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
835 /* Check key name matches */
836 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
838 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
839 tlsext_tick_md(), NULL);
840 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
841 tctx->tlsext_tick_aes_key, etick + 16);
843 /* Attempt to process session ticket, first conduct sanity and
844 * integrity checks on ticket.
846 mlen = HMAC_size(&hctx);
848 /* Check HMAC of encrypted ticket */
849 HMAC_Update(&hctx, etick, eticklen);
850 HMAC_Final(&hctx, tick_hmac, NULL);
851 HMAC_CTX_cleanup(&hctx);
852 if (memcmp(tick_hmac, etick + eticklen, mlen))
854 /* Attempt to decrypt session data */
855 /* Move p after IV to start of encrypted ticket, update length */
856 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
857 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
858 sdec = OPENSSL_malloc(eticklen);
861 EVP_CIPHER_CTX_cleanup(&ctx);
864 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
865 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
868 EVP_CIPHER_CTX_cleanup(&ctx);
871 sess = d2i_SSL_SESSION(NULL, &p, slen);
875 /* The session ID if non-empty is used by some clients to
876 * detect that the ticket has been accepted. So we copy it to
877 * the session structure. If it is empty set length to zero
878 * as required by standard.
881 memcpy(sess->session_id, sess_id, sesslen);
882 sess->session_id_length = sesslen;
884 s->tlsext_ticket_expected = renew_ticket;
887 /* If session decrypt failure indicate a cache miss and set state to
891 s->tlsext_ticket_expected = 1;