2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
13 * Portions of the attached software ("Contribution") are developed by
14 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
16 * The Contribution is licensed pursuant to the OpenSSL open source
17 * license provided above.
19 * ECC cipher suite support in OpenSSL originally written by
20 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
23 /* ====================================================================
24 * Copyright 2005 Nokia. All rights reserved.
26 * The portions of the attached software ("Contribution") is developed by
27 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
30 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
31 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
32 * support (see RFC 4279) to OpenSSL.
34 * No patent licenses or other rights except those expressly stated in
35 * the OpenSSL open source license shall be deemed granted or received
36 * expressly, by implication, estoppel, or otherwise.
38 * No assurances are provided by Nokia that the Contribution does not
39 * infringe the patent or other intellectual property rights of any third
40 * party or that the license provides you with all the necessary rights
41 * to make use of the Contribution.
43 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
44 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
45 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
46 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
51 #include "../ssl_locl.h"
52 #include "statem_locl.h"
53 #include "internal/constant_time_locl.h"
54 #include <openssl/buffer.h>
55 #include <openssl/rand.h>
56 #include <openssl/objects.h>
57 #include <openssl/evp.h>
58 #include <openssl/hmac.h>
59 #include <openssl/x509.h>
60 #include <openssl/dh.h>
61 #include <openssl/bn.h>
62 #include <openssl/md5.h>
64 static STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,
65 PACKET *cipher_suites,
67 **skp, int sslv2format,
71 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
72 * handshake state transitions when a TLSv1.3 server is reading messages from
73 * the client. The message type that the client has sent is provided in |mt|.
74 * The current state is in |s->statem.hand_state|.
76 * Return values are 1 for success (transition allowed) and 0 on error
77 * (transition not allowed)
79 static int ossl_statem_server13_read_transition(SSL *s, int mt)
81 OSSL_STATEM *st = &s->statem;
84 * TODO(TLS1.3): This is still based on the TLSv1.2 state machine. Over time
85 * we will update this to look more like real TLSv1.3
89 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
90 * not negotiated TLSv1.3 yet, so that case is handled by
91 * ossl_statem_server_read_transition()
93 switch (st->hand_state) {
97 case TLS_ST_SW_FINISHED:
98 if (s->s3->tmp.cert_request) {
99 if (mt == SSL3_MT_CERTIFICATE) {
100 st->hand_state = TLS_ST_SR_CERT;
104 if (mt == SSL3_MT_FINISHED) {
105 st->hand_state = TLS_ST_SR_FINISHED;
112 if (s->session->peer == NULL) {
113 if (mt == SSL3_MT_FINISHED) {
114 st->hand_state = TLS_ST_SR_FINISHED;
118 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
119 st->hand_state = TLS_ST_SR_CERT_VRFY;
125 case TLS_ST_SR_CERT_VRFY:
126 if (mt == SSL3_MT_FINISHED) {
127 st->hand_state = TLS_ST_SR_FINISHED;
133 /* No valid transition found */
134 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
135 SSLerr(SSL_F_OSSL_STATEM_SERVER13_READ_TRANSITION,
136 SSL_R_UNEXPECTED_MESSAGE);
141 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
142 * handshake state transitions when the server is reading messages from the
143 * client. The message type that the client has sent is provided in |mt|. The
144 * current state is in |s->statem.hand_state|.
146 * Return values are 1 for success (transition allowed) and 0 on error
147 * (transition not allowed)
149 int ossl_statem_server_read_transition(SSL *s, int mt)
151 OSSL_STATEM *st = &s->statem;
153 if (s->method->version == TLS1_3_VERSION)
154 return ossl_statem_server13_read_transition(s, mt);
156 switch (st->hand_state) {
161 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
162 if (mt == SSL3_MT_CLIENT_HELLO) {
163 st->hand_state = TLS_ST_SR_CLNT_HELLO;
168 case TLS_ST_SW_SRVR_DONE:
170 * If we get a CKE message after a ServerDone then either
171 * 1) We didn't request a Certificate
173 * 2) If we did request one then
174 * a) We allow no Certificate to be returned
176 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
177 * list if we requested a certificate)
179 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
180 if (s->s3->tmp.cert_request) {
181 if (s->version == SSL3_VERSION) {
182 if ((s->verify_mode & SSL_VERIFY_PEER)
183 && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
185 * This isn't an unexpected message as such - we're just
186 * not going to accept it because we require a client
189 ssl3_send_alert(s, SSL3_AL_FATAL,
190 SSL3_AD_HANDSHAKE_FAILURE);
191 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
192 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
195 st->hand_state = TLS_ST_SR_KEY_EXCH;
199 st->hand_state = TLS_ST_SR_KEY_EXCH;
202 } else if (s->s3->tmp.cert_request) {
203 if (mt == SSL3_MT_CERTIFICATE) {
204 st->hand_state = TLS_ST_SR_CERT;
211 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
212 st->hand_state = TLS_ST_SR_KEY_EXCH;
217 case TLS_ST_SR_KEY_EXCH:
219 * We should only process a CertificateVerify message if we have
220 * received a Certificate from the client. If so then |s->session->peer|
221 * will be non NULL. In some instances a CertificateVerify message is
222 * not required even if the peer has sent a Certificate (e.g. such as in
223 * the case of static DH). In that case |st->no_cert_verify| should be
226 if (s->session->peer == NULL || st->no_cert_verify) {
227 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
229 * For the ECDH ciphersuites when the client sends its ECDH
230 * pub key in a certificate, the CertificateVerify message is
231 * not sent. Also for GOST ciphersuites when the client uses
232 * its key from the certificate for key exchange.
234 st->hand_state = TLS_ST_SR_CHANGE;
238 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
239 st->hand_state = TLS_ST_SR_CERT_VRFY;
245 case TLS_ST_SR_CERT_VRFY:
246 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
247 st->hand_state = TLS_ST_SR_CHANGE;
252 case TLS_ST_SR_CHANGE:
253 #ifndef OPENSSL_NO_NEXTPROTONEG
254 if (s->s3->next_proto_neg_seen) {
255 if (mt == SSL3_MT_NEXT_PROTO) {
256 st->hand_state = TLS_ST_SR_NEXT_PROTO;
261 if (mt == SSL3_MT_FINISHED) {
262 st->hand_state = TLS_ST_SR_FINISHED;
265 #ifndef OPENSSL_NO_NEXTPROTONEG
270 #ifndef OPENSSL_NO_NEXTPROTONEG
271 case TLS_ST_SR_NEXT_PROTO:
272 if (mt == SSL3_MT_FINISHED) {
273 st->hand_state = TLS_ST_SR_FINISHED;
279 case TLS_ST_SW_FINISHED:
280 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
281 st->hand_state = TLS_ST_SR_CHANGE;
287 /* No valid transition found */
288 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
289 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE);
294 * Should we send a ServerKeyExchange message?
296 * Valid return values are:
300 static int send_server_key_exchange(SSL *s)
302 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
305 * only send a ServerKeyExchange if DH or fortezza but we have a
306 * sign only certificate PSK: may send PSK identity hints For
307 * ECC ciphersuites, we send a serverKeyExchange message only if
308 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
309 * the server certificate contains the server's public key for
312 if (alg_k & (SSL_kDHE | SSL_kECDHE)
314 * PSK: send ServerKeyExchange if PSK identity hint if
317 #ifndef OPENSSL_NO_PSK
318 /* Only send SKE if we have identity hint for plain PSK */
319 || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
320 && s->cert->psk_identity_hint)
321 /* For other PSK always send SKE */
322 || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
324 #ifndef OPENSSL_NO_SRP
325 /* SRP: send ServerKeyExchange */
326 || (alg_k & SSL_kSRP)
336 * Should we send a CertificateRequest message?
338 * Valid return values are:
342 static int send_certificate_request(SSL *s)
345 /* don't request cert unless asked for it: */
346 s->verify_mode & SSL_VERIFY_PEER
348 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
349 * during re-negotiation:
351 && ((s->session->peer == NULL) ||
352 !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
354 * never request cert in anonymous ciphersuites (see
355 * section "Certificate request" in SSL 3 drafts and in
358 && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
360 * ... except when the application insists on
361 * verification (against the specs, but statem_clnt.c accepts
364 || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
365 /* don't request certificate for SRP auth */
366 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
368 * With normal PSK Certificates and Certificate Requests
371 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
379 * ossl_statem_server13_write_transition() works out what handshake state to
380 * move to next when a TLSv1.3 server is writing messages to be sent to the
383 static WRITE_TRAN ossl_statem_server13_write_transition(SSL *s)
385 OSSL_STATEM *st = &s->statem;
388 * TODO(TLS1.3): This is still based on the TLSv1.2 state machine. Over time
389 * we will update this to look more like real TLSv1.3
393 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
394 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
397 switch (st->hand_state) {
399 /* Shouldn't happen */
400 return WRITE_TRAN_ERROR;
402 case TLS_ST_SR_CLNT_HELLO:
403 st->hand_state = TLS_ST_SW_SRVR_HELLO;
404 return WRITE_TRAN_CONTINUE;
406 case TLS_ST_SW_SRVR_HELLO:
408 st->hand_state = TLS_ST_SW_FINISHED;
409 else if (send_certificate_request(s))
410 st->hand_state = TLS_ST_SW_CERT_REQ;
412 st->hand_state = TLS_ST_SW_CERT;
414 return WRITE_TRAN_CONTINUE;
416 case TLS_ST_SW_CERT_REQ:
417 st->hand_state = TLS_ST_SW_CERT;
418 return WRITE_TRAN_CONTINUE;
421 if (s->tlsext_status_expected)
422 st->hand_state = TLS_ST_SW_CERT_STATUS;
424 st->hand_state = TLS_ST_SW_FINISHED;
425 return WRITE_TRAN_CONTINUE;
427 case TLS_ST_SW_CERT_STATUS:
428 st->hand_state = TLS_ST_SW_FINISHED;
429 return WRITE_TRAN_CONTINUE;
431 case TLS_ST_SW_FINISHED:
432 return WRITE_TRAN_FINISHED;
434 case TLS_ST_SR_FINISHED:
435 st->hand_state = TLS_ST_OK;
436 ossl_statem_set_in_init(s, 0);
437 return WRITE_TRAN_CONTINUE;
442 * ossl_statem_server_write_transition() works out what handshake state to move
443 * to next when the server is writing messages to be sent to the client.
445 WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
447 OSSL_STATEM *st = &s->statem;
450 * Note that before the ClientHello we don't know what version we are going
451 * to negotiate yet, so we don't take this branch until later
454 if (s->method->version == TLS1_3_VERSION)
455 return ossl_statem_server13_write_transition(s);
457 switch (st->hand_state) {
459 /* Shouldn't happen */
460 return WRITE_TRAN_ERROR;
463 /* Just go straight to trying to read from the client */
464 return WRITE_TRAN_FINISHED;
467 /* We must be trying to renegotiate */
468 st->hand_state = TLS_ST_SW_HELLO_REQ;
469 return WRITE_TRAN_CONTINUE;
471 case TLS_ST_SW_HELLO_REQ:
472 st->hand_state = TLS_ST_OK;
473 ossl_statem_set_in_init(s, 0);
474 return WRITE_TRAN_CONTINUE;
476 case TLS_ST_SR_CLNT_HELLO:
477 if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
478 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
479 st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
481 st->hand_state = TLS_ST_SW_SRVR_HELLO;
482 return WRITE_TRAN_CONTINUE;
484 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
485 return WRITE_TRAN_FINISHED;
487 case TLS_ST_SW_SRVR_HELLO:
489 if (s->tlsext_ticket_expected)
490 st->hand_state = TLS_ST_SW_SESSION_TICKET;
492 st->hand_state = TLS_ST_SW_CHANGE;
494 /* Check if it is anon DH or anon ECDH, */
495 /* normal PSK or SRP */
496 if (!(s->s3->tmp.new_cipher->algorithm_auth &
497 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
498 st->hand_state = TLS_ST_SW_CERT;
499 } else if (send_server_key_exchange(s)) {
500 st->hand_state = TLS_ST_SW_KEY_EXCH;
501 } else if (send_certificate_request(s)) {
502 st->hand_state = TLS_ST_SW_CERT_REQ;
504 st->hand_state = TLS_ST_SW_SRVR_DONE;
507 return WRITE_TRAN_CONTINUE;
510 if (s->tlsext_status_expected) {
511 st->hand_state = TLS_ST_SW_CERT_STATUS;
512 return WRITE_TRAN_CONTINUE;
516 case TLS_ST_SW_CERT_STATUS:
517 if (send_server_key_exchange(s)) {
518 st->hand_state = TLS_ST_SW_KEY_EXCH;
519 return WRITE_TRAN_CONTINUE;
523 case TLS_ST_SW_KEY_EXCH:
524 if (send_certificate_request(s)) {
525 st->hand_state = TLS_ST_SW_CERT_REQ;
526 return WRITE_TRAN_CONTINUE;
530 case TLS_ST_SW_CERT_REQ:
531 st->hand_state = TLS_ST_SW_SRVR_DONE;
532 return WRITE_TRAN_CONTINUE;
534 case TLS_ST_SW_SRVR_DONE:
535 return WRITE_TRAN_FINISHED;
537 case TLS_ST_SR_FINISHED:
539 st->hand_state = TLS_ST_OK;
540 ossl_statem_set_in_init(s, 0);
541 return WRITE_TRAN_CONTINUE;
542 } else if (s->tlsext_ticket_expected) {
543 st->hand_state = TLS_ST_SW_SESSION_TICKET;
545 st->hand_state = TLS_ST_SW_CHANGE;
547 return WRITE_TRAN_CONTINUE;
549 case TLS_ST_SW_SESSION_TICKET:
550 st->hand_state = TLS_ST_SW_CHANGE;
551 return WRITE_TRAN_CONTINUE;
553 case TLS_ST_SW_CHANGE:
554 st->hand_state = TLS_ST_SW_FINISHED;
555 return WRITE_TRAN_CONTINUE;
557 case TLS_ST_SW_FINISHED:
559 return WRITE_TRAN_FINISHED;
561 st->hand_state = TLS_ST_OK;
562 ossl_statem_set_in_init(s, 0);
563 return WRITE_TRAN_CONTINUE;
568 * Perform any pre work that needs to be done prior to sending a message from
569 * the server to the client.
571 WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
573 OSSL_STATEM *st = &s->statem;
575 switch (st->hand_state) {
577 /* No pre work to be done */
580 case TLS_ST_SW_HELLO_REQ:
583 dtls1_clear_sent_buffer(s);
586 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
588 if (SSL_IS_DTLS(s)) {
589 dtls1_clear_sent_buffer(s);
590 /* We don't buffer this message so don't use the timer */
595 case TLS_ST_SW_SRVR_HELLO:
596 if (SSL_IS_DTLS(s)) {
598 * Messages we write from now on should be bufferred and
599 * retransmitted if necessary, so we need to use the timer now
605 case TLS_ST_SW_SRVR_DONE:
606 #ifndef OPENSSL_NO_SCTP
607 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s)))
608 return dtls_wait_for_dry(s);
610 return WORK_FINISHED_CONTINUE;
612 case TLS_ST_SW_SESSION_TICKET:
613 if (SSL_IS_DTLS(s)) {
615 * We're into the last flight. We don't retransmit the last flight
616 * unless we need to, so we don't use the timer
622 case TLS_ST_SW_CHANGE:
623 s->session->cipher = s->s3->tmp.new_cipher;
624 if (!s->method->ssl3_enc->setup_key_block(s)) {
625 ossl_statem_set_error(s);
628 if (SSL_IS_DTLS(s)) {
630 * We're into the last flight. We don't retransmit the last flight
631 * unless we need to, so we don't use the timer. This might have
632 * already been set to 0 if we sent a NewSessionTicket message,
633 * but we'll set it again here in case we didn't.
637 return WORK_FINISHED_CONTINUE;
640 return tls_finish_handshake(s, wst);
643 return WORK_FINISHED_CONTINUE;
647 * Perform any work that needs to be done after sending a message from the
648 * server to the client.
650 WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
652 OSSL_STATEM *st = &s->statem;
656 switch (st->hand_state) {
658 /* No post work to be done */
661 case TLS_ST_SW_HELLO_REQ:
662 if (statem_flush(s) != 1)
664 if (!ssl3_init_finished_mac(s)) {
665 ossl_statem_set_error(s);
670 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
671 if (statem_flush(s) != 1)
673 /* HelloVerifyRequest resets Finished MAC */
674 if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
675 ossl_statem_set_error(s);
679 * The next message should be another ClientHello which we need to
680 * treat like it was the first packet
685 case TLS_ST_SW_SRVR_HELLO:
686 #ifndef OPENSSL_NO_SCTP
687 if (SSL_IS_DTLS(s) && s->hit) {
688 unsigned char sctpauthkey[64];
689 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
692 * Add new shared key for SCTP-Auth, will be ignored if no
695 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
696 sizeof(DTLS1_SCTP_AUTH_LABEL));
698 if (SSL_export_keying_material(s, sctpauthkey,
699 sizeof(sctpauthkey), labelbuffer,
700 sizeof(labelbuffer), NULL, 0,
702 ossl_statem_set_error(s);
706 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
707 sizeof(sctpauthkey), sctpauthkey);
711 * TODO(TLS1.3): This actually causes a problem. We don't yet know
712 * whether the next record we are going to receive is an unencrypted
713 * alert, or an encrypted handshake message. We're going to need
714 * something clever in the record layer for this.
716 if (SSL_IS_TLS13(s)) {
717 if (!s->method->ssl3_enc->setup_key_block(s)
718 || !s->method->ssl3_enc->change_cipher_state(s,
719 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)
720 || !s->method->ssl3_enc->change_cipher_state(s,
721 SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ))
726 case TLS_ST_SW_CHANGE:
727 #ifndef OPENSSL_NO_SCTP
728 if (SSL_IS_DTLS(s) && !s->hit) {
730 * Change to new shared key of SCTP-Auth, will be ignored if
733 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
737 if (!s->method->ssl3_enc->change_cipher_state(s,
738 SSL3_CHANGE_CIPHER_SERVER_WRITE))
740 ossl_statem_set_error(s);
745 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
748 case TLS_ST_SW_SRVR_DONE:
749 if (statem_flush(s) != 1)
753 case TLS_ST_SW_FINISHED:
754 if (statem_flush(s) != 1)
756 #ifndef OPENSSL_NO_SCTP
757 if (SSL_IS_DTLS(s) && s->hit) {
759 * Change to new shared key of SCTP-Auth, will be ignored if
762 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
766 if (SSL_IS_TLS13(s)) {
767 if (!s->method->ssl3_enc->generate_master_secret(s,
768 s->session->master_key, s->handshake_secret, 0,
769 &s->session->master_key_length)
770 || !s->method->ssl3_enc->change_cipher_state(s,
771 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
777 return WORK_FINISHED_CONTINUE;
781 * Get the message construction function and message type for sending from the
784 * Valid return values are:
788 int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
789 confunc_f *confunc, int *mt)
791 OSSL_STATEM *st = &s->statem;
793 switch (st->hand_state) {
795 /* Shouldn't happen */
798 case TLS_ST_SW_CHANGE:
800 *confunc = dtls_construct_change_cipher_spec;
802 *confunc = tls_construct_change_cipher_spec;
803 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
806 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
807 *confunc = dtls_construct_hello_verify_request;
808 *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
811 case TLS_ST_SW_HELLO_REQ:
812 /* No construction function needed */
814 *mt = SSL3_MT_HELLO_REQUEST;
817 case TLS_ST_SW_SRVR_HELLO:
818 *confunc = tls_construct_server_hello;
819 *mt = SSL3_MT_SERVER_HELLO;
823 *confunc = tls_construct_server_certificate;
824 *mt = SSL3_MT_CERTIFICATE;
827 case TLS_ST_SW_KEY_EXCH:
828 *confunc = tls_construct_server_key_exchange;
829 *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
832 case TLS_ST_SW_CERT_REQ:
833 *confunc = tls_construct_certificate_request;
834 *mt = SSL3_MT_CERTIFICATE_REQUEST;
837 case TLS_ST_SW_SRVR_DONE:
838 *confunc = tls_construct_server_done;
839 *mt = SSL3_MT_SERVER_DONE;
842 case TLS_ST_SW_SESSION_TICKET:
843 *confunc = tls_construct_new_session_ticket;
844 *mt = SSL3_MT_NEWSESSION_TICKET;
847 case TLS_ST_SW_CERT_STATUS:
848 *confunc = tls_construct_cert_status;
849 *mt = SSL3_MT_CERTIFICATE_STATUS;
852 case TLS_ST_SW_FINISHED:
853 *confunc = tls_construct_finished;
854 *mt = SSL3_MT_FINISHED;
862 * Maximum size (excluding the Handshake header) of a ClientHello message,
863 * calculated as follows:
865 * 2 + # client_version
866 * 32 + # only valid length for random
867 * 1 + # length of session_id
868 * 32 + # maximum size for session_id
869 * 2 + # length of cipher suites
870 * 2^16-2 + # maximum length of cipher suites array
871 * 1 + # length of compression_methods
872 * 2^8-1 + # maximum length of compression methods
873 * 2 + # length of extensions
874 * 2^16-1 # maximum length of extensions
876 #define CLIENT_HELLO_MAX_LENGTH 131396
878 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
879 #define NEXT_PROTO_MAX_LENGTH 514
882 * Returns the maximum allowed length for the current message that we are
883 * reading. Excludes the message header.
885 size_t ossl_statem_server_max_message_size(SSL *s)
887 OSSL_STATEM *st = &s->statem;
889 switch (st->hand_state) {
891 /* Shouldn't happen */
894 case TLS_ST_SR_CLNT_HELLO:
895 return CLIENT_HELLO_MAX_LENGTH;
898 return s->max_cert_list;
900 case TLS_ST_SR_KEY_EXCH:
901 return CLIENT_KEY_EXCH_MAX_LENGTH;
903 case TLS_ST_SR_CERT_VRFY:
904 return SSL3_RT_MAX_PLAIN_LENGTH;
906 #ifndef OPENSSL_NO_NEXTPROTONEG
907 case TLS_ST_SR_NEXT_PROTO:
908 return NEXT_PROTO_MAX_LENGTH;
911 case TLS_ST_SR_CHANGE:
912 return CCS_MAX_LENGTH;
914 case TLS_ST_SR_FINISHED:
915 return FINISHED_MAX_LENGTH;
920 * Process a message that the server has received from the client.
922 MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
924 OSSL_STATEM *st = &s->statem;
926 switch (st->hand_state) {
928 /* Shouldn't happen */
929 return MSG_PROCESS_ERROR;
931 case TLS_ST_SR_CLNT_HELLO:
932 return tls_process_client_hello(s, pkt);
935 return tls_process_client_certificate(s, pkt);
937 case TLS_ST_SR_KEY_EXCH:
938 return tls_process_client_key_exchange(s, pkt);
940 case TLS_ST_SR_CERT_VRFY:
941 return tls_process_cert_verify(s, pkt);
943 #ifndef OPENSSL_NO_NEXTPROTONEG
944 case TLS_ST_SR_NEXT_PROTO:
945 return tls_process_next_proto(s, pkt);
948 case TLS_ST_SR_CHANGE:
949 return tls_process_change_cipher_spec(s, pkt);
951 case TLS_ST_SR_FINISHED:
952 return tls_process_finished(s, pkt);
957 * Perform any further processing required following the receipt of a message
960 WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
962 OSSL_STATEM *st = &s->statem;
964 switch (st->hand_state) {
966 /* Shouldn't happen */
969 case TLS_ST_SR_CLNT_HELLO:
970 return tls_post_process_client_hello(s, wst);
972 case TLS_ST_SR_KEY_EXCH:
973 return tls_post_process_client_key_exchange(s, wst);
975 case TLS_ST_SR_CERT_VRFY:
976 #ifndef OPENSSL_NO_SCTP
977 if ( /* Is this SCTP? */
978 BIO_dgram_is_sctp(SSL_get_wbio(s))
979 /* Are we renegotiating? */
980 && s->renegotiate && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
981 s->s3->in_read_app_data = 2;
982 s->rwstate = SSL_READING;
983 BIO_clear_retry_flags(SSL_get_rbio(s));
984 BIO_set_retry_read(SSL_get_rbio(s));
985 ossl_statem_set_sctp_read_sock(s, 1);
988 ossl_statem_set_sctp_read_sock(s, 0);
991 return WORK_FINISHED_CONTINUE;
993 return WORK_FINISHED_CONTINUE;
996 #ifndef OPENSSL_NO_SRP
997 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
999 int ret = SSL_ERROR_NONE;
1001 *al = SSL_AD_UNRECOGNIZED_NAME;
1003 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
1004 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
1005 if (s->srp_ctx.login == NULL) {
1007 * RFC 5054 says SHOULD reject, we do so if There is no srp
1010 ret = SSL3_AL_FATAL;
1011 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1013 ret = SSL_srp_server_param_with_username(s, al);
1020 int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
1023 /* Always use DTLS 1.0 version: see RFC 6347 */
1024 if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
1025 || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
1031 int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
1033 unsigned int cookie_leni;
1034 if (s->ctx->app_gen_cookie_cb == NULL ||
1035 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
1036 &cookie_leni) == 0 ||
1037 cookie_leni > 255) {
1038 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
1039 SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
1042 s->d1->cookie_len = cookie_leni;
1044 if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
1045 s->d1->cookie_len)) {
1046 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST, ERR_R_INTERNAL_ERROR);
1053 MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
1055 int i, al = SSL_AD_INTERNAL_ERROR;
1059 const SSL_CIPHER *c;
1060 #ifndef OPENSSL_NO_COMP
1061 SSL_COMP *comp = NULL;
1063 STACK_OF(SSL_CIPHER) *ciphers = NULL;
1065 /* |cookie| will only be initialized for DTLS. */
1066 PACKET session_id, compression, extensions, cookie;
1067 static const unsigned char null_compression = 0;
1068 CLIENTHELLO_MSG clienthello;
1071 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1073 memset(&clienthello, 0, sizeof(clienthello));
1074 clienthello.isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
1075 PACKET_null_init(&cookie);
1077 if (clienthello.isv2) {
1081 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1082 * header is sent directly on the wire, not wrapped as a TLS
1083 * record. Our record layer just processes the message length and passes
1084 * the rest right through. Its format is:
1086 * 0-1 msg_length - decoded by the record layer
1087 * 2 msg_type - s->init_msg points here
1089 * 5-6 cipher_spec_length
1090 * 7-8 session_id_length
1091 * 9-10 challenge_length
1095 if (!PACKET_get_1(pkt, &mt)
1096 || mt != SSL2_MT_CLIENT_HELLO) {
1098 * Should never happen. We should have tested this in the record
1099 * layer in order to have determined that this is a SSLv2 record
1100 * in the first place
1102 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1107 if (!PACKET_get_net_2(pkt, &clienthello.legacy_version)) {
1108 al = SSL_AD_DECODE_ERROR;
1109 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1113 /* Parse the message and load client random. */
1114 if (clienthello.isv2) {
1116 * Handle an SSLv2 backwards compatible ClientHello
1117 * Note, this is only for SSLv3+ using the backward compatible format.
1118 * Real SSLv2 is not supported, and is rejected below.
1120 unsigned int ciphersuite_len, session_id_len, challenge_len;
1123 if (!PACKET_get_net_2(pkt, &ciphersuite_len)
1124 || !PACKET_get_net_2(pkt, &session_id_len)
1125 || !PACKET_get_net_2(pkt, &challenge_len)) {
1126 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1127 SSL_R_RECORD_LENGTH_MISMATCH);
1128 al = SSL_AD_DECODE_ERROR;
1132 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1133 al = SSL_AD_DECODE_ERROR;
1134 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1138 if (!PACKET_get_sub_packet(pkt, &clienthello.ciphersuites,
1140 || !PACKET_copy_bytes(pkt, clienthello.session_id, session_id_len)
1141 || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
1142 /* No extensions. */
1143 || PACKET_remaining(pkt) != 0) {
1144 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1145 SSL_R_RECORD_LENGTH_MISMATCH);
1146 al = SSL_AD_DECODE_ERROR;
1149 clienthello.session_id_len = session_id_len;
1151 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1152 * here rather than sizeof(clienthello.random) because that is the limit
1153 * for SSLv3 and it is fixed. It won't change even if
1154 * sizeof(clienthello.random) does.
1156 challenge_len = challenge_len > SSL3_RANDOM_SIZE
1157 ? SSL3_RANDOM_SIZE : challenge_len;
1158 memset(clienthello.random, 0, SSL3_RANDOM_SIZE);
1159 if (!PACKET_copy_bytes(&challenge,
1160 clienthello.random + SSL3_RANDOM_SIZE -
1161 challenge_len, challenge_len)
1162 /* Advertise only null compression. */
1163 || !PACKET_buf_init(&compression, &null_compression, 1)) {
1164 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1165 al = SSL_AD_INTERNAL_ERROR;
1169 PACKET_null_init(&clienthello.extensions);
1171 /* Regular ClientHello. */
1172 if (!PACKET_copy_bytes(pkt, clienthello.random, SSL3_RANDOM_SIZE)
1173 || !PACKET_get_length_prefixed_1(pkt, &session_id)
1174 || !PACKET_copy_all(&session_id, clienthello.session_id,
1175 SSL_MAX_SSL_SESSION_ID_LENGTH,
1176 &clienthello.session_id_len)) {
1177 al = SSL_AD_DECODE_ERROR;
1178 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1182 if (SSL_IS_DTLS(s)) {
1183 if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1184 al = SSL_AD_DECODE_ERROR;
1185 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1188 if (!PACKET_copy_all(&cookie, clienthello.dtls_cookie,
1189 DTLS1_COOKIE_LENGTH,
1190 &clienthello.dtls_cookie_len)) {
1191 al = SSL_AD_DECODE_ERROR;
1192 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1196 * If we require cookies and this ClientHello doesn't contain one,
1197 * just return since we do not want to allocate any memory yet.
1198 * So check cookie length...
1200 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1201 if (clienthello.dtls_cookie_len == 0)
1206 if (!PACKET_get_length_prefixed_2(pkt, &clienthello.ciphersuites)) {
1207 al = SSL_AD_DECODE_ERROR;
1208 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1212 if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
1213 al = SSL_AD_DECODE_ERROR;
1214 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1218 /* Could be empty. */
1219 if (PACKET_remaining(pkt) == 0) {
1220 PACKET_null_init(&clienthello.extensions);
1222 if (!PACKET_get_length_prefixed_2(pkt, &clienthello.extensions)) {
1223 al = SSL_AD_DECODE_ERROR;
1224 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1230 if (!PACKET_copy_all(&compression, clienthello.compressions,
1231 MAX_COMPRESSIONS_SIZE,
1232 &clienthello.compressions_len)) {
1233 al = SSL_AD_DECODE_ERROR;
1234 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1238 /* Preserve the raw extensions PACKET for later use */
1239 extensions = clienthello.extensions;
1240 if (!tls_collect_extensions(&extensions, &clienthello.pre_proc_exts,
1241 &clienthello.num_extensions, &al)) {
1242 /* SSLerr already been called */
1246 /* Finished parsing the ClientHello, now we can start processing it */
1248 /* Set up the client_random */
1249 memcpy(s->s3->client_random, clienthello.random, SSL3_RANDOM_SIZE);
1251 /* Choose the version */
1253 if (clienthello.isv2) {
1254 if (clienthello.legacy_version == SSL2_VERSION
1255 || (clienthello.legacy_version & 0xff00)
1256 != (SSL3_VERSION_MAJOR << 8)) {
1258 * This is real SSLv2 or something complete unknown. We don't
1261 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1265 s->client_version = clienthello.legacy_version;
1268 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1269 * versions are potentially compatible. Version negotiation comes later.
1271 if (!SSL_IS_DTLS(s)) {
1272 protverr = ssl_choose_server_version(s, &clienthello);
1273 } else if (s->method->version != DTLS_ANY_VERSION &&
1274 DTLS_VERSION_LT((int)clienthello.legacy_version, s->version)) {
1275 protverr = SSL_R_VERSION_TOO_LOW;
1281 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, protverr);
1282 if ((!s->enc_write_ctx && !s->write_hash)) {
1283 /* like ssl3_get_record, send alert using remote version number */
1284 s->version = s->client_version = clienthello.legacy_version;
1286 al = SSL_AD_PROTOCOL_VERSION;
1290 if (SSL_IS_DTLS(s)) {
1291 /* Empty cookie was already handled above by returning early. */
1292 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1293 if (s->ctx->app_verify_cookie_cb != NULL) {
1294 if (s->ctx->app_verify_cookie_cb(s, clienthello.dtls_cookie,
1295 clienthello.dtls_cookie_len) == 0) {
1296 al = SSL_AD_HANDSHAKE_FAILURE;
1297 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1298 SSL_R_COOKIE_MISMATCH);
1300 /* else cookie verification succeeded */
1302 /* default verification */
1303 } else if (s->d1->cookie_len != clienthello.dtls_cookie_len
1304 || memcmp(clienthello.dtls_cookie, s->d1->cookie,
1305 s->d1->cookie_len) != 0) {
1306 al = SSL_AD_HANDSHAKE_FAILURE;
1307 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1310 s->d1->cookie_verified = 1;
1312 if (s->method->version == DTLS_ANY_VERSION) {
1313 protverr = ssl_choose_server_version(s, &clienthello);
1314 if (protverr != 0) {
1315 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, protverr);
1316 s->version = s->client_version;
1317 al = SSL_AD_PROTOCOL_VERSION;
1325 /* We need to do this before getting the session */
1326 if (!tls_check_client_ems_support(s, &clienthello)) {
1327 /* Only fails if the extension is malformed */
1328 al = SSL_AD_DECODE_ERROR;
1329 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1334 * We don't allow resumption in a backwards compatible ClientHello.
1335 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1337 * Versions before 0.9.7 always allow clients to resume sessions in
1338 * renegotiation. 0.9.7 and later allow this by default, but optionally
1339 * ignore resumption requests with flag
1340 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1341 * than a change to default behavior so that applications relying on
1342 * this for security won't even compile against older library versions).
1343 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1344 * request renegotiation but not a new session (s->new_session remains
1345 * unset): for servers, this essentially just means that the
1346 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1349 if (clienthello.isv2 ||
1351 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1352 if (!ssl_get_new_session(s, 1))
1355 i = ssl_get_prev_session(s, &clienthello);
1357 * Only resume if the session's version matches the negotiated
1359 * RFC 5246 does not provide much useful advice on resumption
1360 * with a different protocol version. It doesn't forbid it but
1361 * the sanity of such behaviour would be questionable.
1362 * In practice, clients do not accept a version mismatch and
1363 * will abort the handshake with an error.
1365 if (i == 1 && s->version == s->session->ssl_version) {
1366 /* previous session */
1368 } else if (i == -1) {
1372 if (!ssl_get_new_session(s, 1))
1377 if (ssl_bytes_to_cipher_list(s, &clienthello.ciphersuites, &ciphers,
1378 clienthello.isv2, &al) == NULL) {
1382 /* If it is a hit, check that the cipher is in the list */
1385 id = s->session->cipher->id;
1388 fprintf(stderr, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers));
1390 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1391 c = sk_SSL_CIPHER_value(ciphers, i);
1393 fprintf(stderr, "client [%2d of %2d]:%s\n",
1394 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1403 * we need to have the cipher in the cipher list if we are asked
1406 al = SSL_AD_ILLEGAL_PARAMETER;
1407 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1408 SSL_R_REQUIRED_CIPHER_MISSING);
1413 for (loop = 0; loop < clienthello.compressions_len; loop++) {
1414 if (clienthello.compressions[loop] == 0)
1418 if (loop >= clienthello.compressions_len) {
1420 al = SSL_AD_DECODE_ERROR;
1421 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1425 /* TLS extensions */
1426 if (!ssl_parse_clienthello_tlsext(s, &clienthello)) {
1427 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1431 /* Check we've got a key_share for TLSv1.3 */
1432 if (SSL_IS_TLS13(s) && s->s3->peer_tmp == NULL && !s->hit) {
1433 /* No suitable share */
1434 /* TODO(TLS1.3): Send a HelloRetryRequest */
1435 al = SSL_AD_HANDSHAKE_FAILURE;
1436 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_SUITABLE_KEY_SHARE);
1441 * Check if we want to use external pre-shared secret for this handshake
1442 * for not reused session only. We need to generate server_random before
1443 * calling tls_session_secret_cb in order to allow SessionTicket
1444 * processing to use it in key derivation.
1448 pos = s->s3->server_random;
1449 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1454 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
1455 const SSL_CIPHER *pref_cipher = NULL;
1457 * s->session->master_key_length is a size_t, but this is an int for
1458 * backwards compat reasons
1460 int master_key_length;
1462 master_key_length = sizeof(s->session->master_key);
1463 if (s->tls_session_secret_cb(s, s->session->master_key,
1464 &master_key_length, ciphers,
1466 s->tls_session_secret_cb_arg)
1467 && master_key_length > 0) {
1468 s->session->master_key_length = master_key_length;
1470 s->session->ciphers = ciphers;
1471 s->session->verify_result = X509_V_OK;
1475 /* check if some cipher was preferred by call back */
1477 pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
1482 if (pref_cipher == NULL) {
1483 al = SSL_AD_HANDSHAKE_FAILURE;
1484 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1488 s->session->cipher = pref_cipher;
1489 sk_SSL_CIPHER_free(s->cipher_list);
1490 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1491 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1492 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1497 * Worst case, we will use the NULL compression, but if we have other
1498 * options, we will now look for them. We have complen-1 compression
1499 * algorithms from the client, starting at q.
1501 s->s3->tmp.new_compression = NULL;
1502 #ifndef OPENSSL_NO_COMP
1503 /* This only happens if we have a cache hit */
1504 if (s->session->compress_meth != 0) {
1505 int m, comp_id = s->session->compress_meth;
1507 /* Perform sanity checks on resumed compression algorithm */
1508 /* Can't disable compression */
1509 if (!ssl_allow_compression(s)) {
1510 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1511 SSL_R_INCONSISTENT_COMPRESSION);
1514 /* Look for resumed compression method */
1515 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1516 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1517 if (comp_id == comp->id) {
1518 s->s3->tmp.new_compression = comp;
1522 if (s->s3->tmp.new_compression == NULL) {
1523 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1524 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1527 /* Look for resumed method in compression list */
1528 for (k = 0; k < clienthello.compressions_len; k++) {
1529 if (clienthello.compressions[k] == comp_id)
1532 if (k >= clienthello.compressions_len) {
1533 al = SSL_AD_ILLEGAL_PARAMETER;
1534 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1535 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
1540 else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1541 /* See if we have a match */
1542 int m, nn, v, done = 0;
1545 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1546 for (m = 0; m < nn; m++) {
1547 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1549 for (o = 0; o < clienthello.compressions_len; o++) {
1550 if (v == clienthello.compressions[o]) {
1559 s->s3->tmp.new_compression = comp;
1565 * If compression is disabled we'd better not try to resume a session
1566 * using compression.
1568 if (s->session->compress_meth != 0) {
1569 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1575 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1579 #ifdef OPENSSL_NO_COMP
1580 s->session->compress_meth = 0;
1582 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1584 sk_SSL_CIPHER_free(s->session->ciphers);
1585 s->session->ciphers = ciphers;
1586 if (ciphers == NULL) {
1587 al = SSL_AD_INTERNAL_ERROR;
1588 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1592 if (!tls1_set_server_sigalgs(s)) {
1593 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1598 sk_SSL_CIPHER_free(ciphers);
1599 OPENSSL_free(clienthello.pre_proc_exts);
1600 return MSG_PROCESS_CONTINUE_PROCESSING;
1602 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1604 ossl_statem_set_error(s);
1606 sk_SSL_CIPHER_free(ciphers);
1607 OPENSSL_free(clienthello.pre_proc_exts);
1609 return MSG_PROCESS_ERROR;
1612 WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
1614 int al = SSL_AD_HANDSHAKE_FAILURE;
1615 const SSL_CIPHER *cipher;
1617 if (wst == WORK_MORE_A) {
1619 /* Let cert callback update server certificates if required */
1620 if (s->cert->cert_cb) {
1621 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1623 al = SSL_AD_INTERNAL_ERROR;
1624 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1625 SSL_R_CERT_CB_ERROR);
1629 s->rwstate = SSL_X509_LOOKUP;
1632 s->rwstate = SSL_NOTHING;
1635 ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1637 if (cipher == NULL) {
1638 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1639 SSL_R_NO_SHARED_CIPHER);
1642 s->s3->tmp.new_cipher = cipher;
1643 /* check whether we should disable session resumption */
1644 if (s->not_resumable_session_cb != NULL)
1645 s->session->not_resumable = s->not_resumable_session_cb(s,
1646 ((cipher->algorithm_mkey & (SSL_kDHE | SSL_kECDHE)) != 0));
1647 if (s->session->not_resumable)
1648 /* do not send a session ticket */
1649 s->tlsext_ticket_expected = 0;
1651 /* Session-id reuse */
1652 s->s3->tmp.new_cipher = s->session->cipher;
1655 if (!(s->verify_mode & SSL_VERIFY_PEER)) {
1656 if (!ssl3_digest_cached_records(s, 0)) {
1657 al = SSL_AD_INTERNAL_ERROR;
1663 * we now have the following setup.
1665 * cipher_list - our preferred list of ciphers
1666 * ciphers - the clients preferred list of ciphers
1667 * compression - basically ignored right now
1668 * ssl version is set - sslv3
1669 * s->session - The ssl session has been setup.
1670 * s->hit - session reuse flag
1671 * s->s3->tmp.new_cipher- the new cipher to use.
1674 /* Handles TLS extensions that we couldn't check earlier */
1675 if (s->version >= SSL3_VERSION) {
1676 if (!ssl_check_clienthello_tlsext_late(s, &al)) {
1677 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1678 SSL_R_CLIENTHELLO_TLSEXT);
1685 #ifndef OPENSSL_NO_SRP
1686 if (wst == WORK_MORE_B) {
1688 if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
1690 * callback indicates further work to be done
1692 s->rwstate = SSL_X509_LOOKUP;
1695 if (ret != SSL_ERROR_NONE) {
1697 * This is not really an error but the only means to for
1698 * a client to detect whether srp is supported.
1700 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
1701 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1702 SSL_R_CLIENTHELLO_TLSEXT);
1704 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1705 SSL_R_PSK_IDENTITY_NOT_FOUND);
1712 return WORK_FINISHED_STOP;
1714 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1715 ossl_statem_set_error(s);
1719 int tls_construct_server_hello(SSL *s, WPACKET *pkt)
1721 int compm, al = SSL_AD_INTERNAL_ERROR;
1725 /* TODO(TLS1.3): Remove the DRAFT conditional before release */
1726 version = SSL_IS_TLS13(s) ? TLS1_3_VERSION_DRAFT : s->version;
1727 if (!WPACKET_put_bytes_u16(pkt, version)
1729 * Random stuff. Filling of the server_random takes place in
1730 * tls_process_client_hello()
1732 || !WPACKET_memcpy(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1733 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1738 * There are several cases for the session ID to send
1739 * back in the server hello:
1740 * - For session reuse from the session cache,
1741 * we send back the old session ID.
1742 * - If stateless session reuse (using a session ticket)
1743 * is successful, we send back the client's "session ID"
1744 * (which doesn't actually identify the session).
1745 * - If it is a new session, we send back the new
1747 * - However, if we want the new session to be single-use,
1748 * we send back a 0-length session ID.
1749 * s->hit is non-zero in either case of session reuse,
1750 * so the following won't overwrite an ID that we're supposed
1753 if (s->session->not_resumable ||
1754 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1756 s->session->session_id_length = 0;
1758 sl = s->session->session_id_length;
1759 if (sl > sizeof(s->session->session_id)) {
1760 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1764 /* set up the compression method */
1765 #ifdef OPENSSL_NO_COMP
1768 if (s->s3->tmp.new_compression == NULL)
1771 compm = s->s3->tmp.new_compression->id;
1774 if (!WPACKET_sub_memcpy_u8(pkt, s->session->session_id, sl)
1775 || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt, &len)
1776 || !WPACKET_put_bytes_u8(pkt, compm)
1777 || !ssl_prepare_serverhello_tlsext(s)
1778 || !ssl_add_serverhello_tlsext(s, pkt, &al)) {
1779 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1785 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1789 int tls_construct_server_done(SSL *s, WPACKET *pkt)
1791 if (!s->s3->tmp.cert_request) {
1792 if (!ssl3_digest_cached_records(s, 0)) {
1793 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1800 int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
1802 #ifndef OPENSSL_NO_DH
1803 EVP_PKEY *pkdh = NULL;
1805 #ifndef OPENSSL_NO_EC
1806 unsigned char *encodedPoint = NULL;
1807 size_t encodedlen = 0;
1811 const EVP_MD *md = NULL;
1812 int al = SSL_AD_INTERNAL_ERROR, i;
1815 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
1816 size_t paramlen, paramoffset;
1818 if (!WPACKET_get_total_written(pkt, ¶moffset)) {
1819 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1823 if (md_ctx == NULL) {
1824 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1828 type = s->s3->tmp.new_cipher->algorithm_mkey;
1830 r[0] = r[1] = r[2] = r[3] = NULL;
1831 #ifndef OPENSSL_NO_PSK
1832 /* Plain PSK or RSAPSK nothing to do */
1833 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
1835 #endif /* !OPENSSL_NO_PSK */
1836 #ifndef OPENSSL_NO_DH
1837 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
1838 CERT *cert = s->cert;
1840 EVP_PKEY *pkdhp = NULL;
1843 if (s->cert->dh_tmp_auto) {
1844 DH *dhp = ssl_get_auto_dh(s);
1845 pkdh = EVP_PKEY_new();
1846 if (pkdh == NULL || dhp == NULL) {
1848 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1849 ERR_R_INTERNAL_ERROR);
1852 EVP_PKEY_assign_DH(pkdh, dhp);
1855 pkdhp = cert->dh_tmp;
1857 if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
1858 DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);
1859 pkdh = ssl_dh_to_pkey(dhp);
1861 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1862 ERR_R_INTERNAL_ERROR);
1867 if (pkdhp == NULL) {
1868 al = SSL_AD_HANDSHAKE_FAILURE;
1869 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1870 SSL_R_MISSING_TMP_DH_KEY);
1873 if (!ssl_security(s, SSL_SECOP_TMP_DH,
1874 EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {
1875 al = SSL_AD_HANDSHAKE_FAILURE;
1876 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1877 SSL_R_DH_KEY_TOO_SMALL);
1880 if (s->s3->tmp.pkey != NULL) {
1881 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1882 ERR_R_INTERNAL_ERROR);
1886 s->s3->tmp.pkey = ssl_generate_pkey(pkdhp);
1888 if (s->s3->tmp.pkey == NULL) {
1889 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
1893 dh = EVP_PKEY_get0_DH(s->s3->tmp.pkey);
1895 EVP_PKEY_free(pkdh);
1898 DH_get0_pqg(dh, &r[0], NULL, &r[1]);
1899 DH_get0_key(dh, &r[2], NULL);
1902 #ifndef OPENSSL_NO_EC
1903 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
1906 if (s->s3->tmp.pkey != NULL) {
1907 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1908 ERR_R_INTERNAL_ERROR);
1912 /* Get NID of appropriate shared curve */
1913 nid = tls1_shared_group(s, -2);
1914 curve_id = tls1_ec_nid2curve_id(nid);
1915 if (curve_id == 0) {
1916 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1917 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1920 s->s3->tmp.pkey = ssl_generate_pkey_curve(curve_id);
1921 /* Generate a new key for this curve */
1922 if (s->s3->tmp.pkey == NULL) {
1923 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
1927 /* Encode the public key. */
1928 encodedlen = EVP_PKEY_get1_tls_encodedpoint(s->s3->tmp.pkey,
1930 if (encodedlen == 0) {
1931 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);
1936 * We'll generate the serverKeyExchange message explicitly so we
1937 * can set these to NULLs
1944 #endif /* !OPENSSL_NO_EC */
1945 #ifndef OPENSSL_NO_SRP
1946 if (type & SSL_kSRP) {
1947 if ((s->srp_ctx.N == NULL) ||
1948 (s->srp_ctx.g == NULL) ||
1949 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
1950 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1951 SSL_R_MISSING_SRP_PARAM);
1954 r[0] = s->srp_ctx.N;
1955 r[1] = s->srp_ctx.g;
1956 r[2] = s->srp_ctx.s;
1957 r[3] = s->srp_ctx.B;
1961 al = SSL_AD_HANDSHAKE_FAILURE;
1962 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1963 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1967 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
1968 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) {
1969 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md))
1971 al = SSL_AD_DECODE_ERROR;
1978 #ifndef OPENSSL_NO_PSK
1979 if (type & SSL_PSK) {
1980 size_t len = (s->cert->psk_identity_hint == NULL)
1981 ? 0 : strlen(s->cert->psk_identity_hint);
1984 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
1985 * checked this when we set the identity hint - but just in case
1987 if (len > PSK_MAX_IDENTITY_LEN
1988 || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
1990 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
1991 ERR_R_INTERNAL_ERROR);
1997 for (i = 0; i < 4 && r[i] != NULL; i++) {
1998 unsigned char *binval;
2001 #ifndef OPENSSL_NO_SRP
2002 if ((i == 2) && (type & SSL_kSRP)) {
2003 res = WPACKET_start_sub_packet_u8(pkt);
2006 res = WPACKET_start_sub_packet_u16(pkt);
2009 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2010 ERR_R_INTERNAL_ERROR);
2014 #ifndef OPENSSL_NO_DH
2016 * for interoperability with some versions of the Microsoft TLS
2017 * stack, we need to zero pad the DHE pub key to the same length
2020 if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
2021 size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
2024 if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
2025 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2026 ERR_R_INTERNAL_ERROR);
2029 memset(binval, 0, len);
2033 if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
2034 || !WPACKET_close(pkt)) {
2035 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2036 ERR_R_INTERNAL_ERROR);
2040 BN_bn2bin(r[i], binval);
2043 #ifndef OPENSSL_NO_EC
2044 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2046 * We only support named (not generic) curves. In this situation, the
2047 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2048 * [1 byte length of encoded point], followed by the actual encoded
2051 if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
2052 || !WPACKET_put_bytes_u8(pkt, 0)
2053 || !WPACKET_put_bytes_u8(pkt, curve_id)
2054 || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
2055 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2056 ERR_R_INTERNAL_ERROR);
2059 OPENSSL_free(encodedPoint);
2060 encodedPoint = NULL;
2067 * n is the length of the params, they start at &(d[4]) and p
2068 * points to the space at the end.
2071 unsigned char *sigbytes1, *sigbytes2;
2072 unsigned int siglen;
2074 /* Get length of the parameters we have written above */
2075 if (!WPACKET_get_length(pkt, ¶mlen)) {
2076 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2077 ERR_R_INTERNAL_ERROR);
2080 /* send signature algorithm */
2081 if (SSL_USE_SIGALGS(s)) {
2082 if (!tls12_get_sigandhash(pkt, pkey, md)) {
2083 /* Should never happen */
2084 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2085 ERR_R_INTERNAL_ERROR);
2090 fprintf(stderr, "Using hash %s\n", EVP_MD_name(md));
2093 * Create the signature. We don't know the actual length of the sig
2094 * until after we've created it, so we reserve enough bytes for it
2095 * up front, and then properly allocate them in the WPACKET
2098 if (!WPACKET_sub_reserve_bytes_u16(pkt, EVP_PKEY_size(pkey),
2100 || EVP_SignInit_ex(md_ctx, md, NULL) <= 0
2101 || EVP_SignUpdate(md_ctx, &(s->s3->client_random[0]),
2102 SSL3_RANDOM_SIZE) <= 0
2103 || EVP_SignUpdate(md_ctx, &(s->s3->server_random[0]),
2104 SSL3_RANDOM_SIZE) <= 0
2105 || EVP_SignUpdate(md_ctx, s->init_buf->data + paramoffset,
2107 || EVP_SignFinal(md_ctx, sigbytes1, &siglen, pkey) <= 0
2108 || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
2109 || sigbytes1 != sigbytes2) {
2110 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2111 ERR_R_INTERNAL_ERROR);
2115 /* Is this error check actually needed? */
2116 al = SSL_AD_HANDSHAKE_FAILURE;
2117 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2118 SSL_R_UNKNOWN_PKEY_TYPE);
2123 EVP_MD_CTX_free(md_ctx);
2126 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2128 #ifndef OPENSSL_NO_DH
2129 EVP_PKEY_free(pkdh);
2131 #ifndef OPENSSL_NO_EC
2132 OPENSSL_free(encodedPoint);
2134 EVP_MD_CTX_free(md_ctx);
2138 int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
2141 STACK_OF(X509_NAME) *sk = NULL;
2143 /* get the list of acceptable cert types */
2144 if (!WPACKET_start_sub_packet_u8(pkt)
2145 || !ssl3_get_req_cert_type(s, pkt)
2146 || !WPACKET_close(pkt)) {
2147 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2151 if (SSL_USE_SIGALGS(s)) {
2152 const unsigned char *psigs;
2153 size_t nl = tls12_get_psigalgs(s, &psigs);
2154 if (!WPACKET_start_sub_packet_u16(pkt)
2155 || !tls12_copy_sigalgs(s, pkt, psigs, nl)
2156 || !WPACKET_close(pkt)) {
2157 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2158 ERR_R_INTERNAL_ERROR);
2163 /* Start sub-packet for client CA list */
2164 if (!WPACKET_start_sub_packet_u16(pkt)) {
2165 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2169 sk = SSL_get_client_CA_list(s);
2171 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2172 unsigned char *namebytes;
2173 X509_NAME *name = sk_X509_NAME_value(sk, i);
2177 || (namelen = i2d_X509_NAME(name, NULL)) < 0
2178 || !WPACKET_sub_allocate_bytes_u16(pkt, namelen,
2180 || i2d_X509_NAME(name, &namebytes) != namelen) {
2181 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2182 ERR_R_INTERNAL_ERROR);
2187 /* else no CA names */
2189 if (!WPACKET_close(pkt)) {
2190 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2194 s->s3->tmp.cert_request = 1;
2198 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2202 static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt, int *al)
2204 #ifndef OPENSSL_NO_PSK
2205 unsigned char psk[PSK_MAX_PSK_LEN];
2207 PACKET psk_identity;
2209 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2210 *al = SSL_AD_DECODE_ERROR;
2211 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH);
2214 if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2215 *al = SSL_AD_DECODE_ERROR;
2216 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG);
2219 if (s->psk_server_callback == NULL) {
2220 *al = SSL_AD_INTERNAL_ERROR;
2221 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_SERVER_CB);
2225 if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2226 *al = SSL_AD_INTERNAL_ERROR;
2227 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2231 psklen = s->psk_server_callback(s, s->session->psk_identity,
2234 if (psklen > PSK_MAX_PSK_LEN) {
2235 *al = SSL_AD_INTERNAL_ERROR;
2236 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2238 } else if (psklen == 0) {
2240 * PSK related to the given identity not found
2242 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2243 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2244 SSL_R_PSK_IDENTITY_NOT_FOUND);
2248 OPENSSL_free(s->s3->tmp.psk);
2249 s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
2250 OPENSSL_cleanse(psk, psklen);
2252 if (s->s3->tmp.psk == NULL) {
2253 *al = SSL_AD_INTERNAL_ERROR;
2254 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
2258 s->s3->tmp.psklen = psklen;
2262 /* Should never happen */
2263 *al = SSL_AD_INTERNAL_ERROR;
2264 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2269 static int tls_process_cke_rsa(SSL *s, PACKET *pkt, int *al)
2271 #ifndef OPENSSL_NO_RSA
2272 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2274 unsigned char decrypt_good, version_good;
2275 size_t j, padding_len;
2276 PACKET enc_premaster;
2278 unsigned char *rsa_decrypt = NULL;
2281 rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey);
2283 *al = SSL_AD_HANDSHAKE_FAILURE;
2284 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_MISSING_RSA_CERTIFICATE);
2288 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2289 if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2290 enc_premaster = *pkt;
2292 if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2293 || PACKET_remaining(pkt) != 0) {
2294 *al = SSL_AD_DECODE_ERROR;
2295 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_LENGTH_MISMATCH);
2301 * We want to be sure that the plaintext buffer size makes it safe to
2302 * iterate over the entire size of a premaster secret
2303 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2304 * their ciphertext cannot accommodate a premaster secret anyway.
2306 if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
2307 *al = SSL_AD_INTERNAL_ERROR;
2308 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
2312 rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
2313 if (rsa_decrypt == NULL) {
2314 *al = SSL_AD_INTERNAL_ERROR;
2315 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_MALLOC_FAILURE);
2320 * We must not leak whether a decryption failure occurs because of
2321 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2322 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2323 * generates a random premaster secret for the case that the decrypt
2324 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2327 if (RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret)) <= 0)
2331 * Decrypt with no padding. PKCS#1 padding will be removed as part of
2332 * the timing-sensitive code below.
2334 /* TODO(size_t): Convert this function */
2335 decrypt_len = (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster),
2336 PACKET_data(&enc_premaster),
2337 rsa_decrypt, rsa, RSA_NO_PADDING);
2338 if (decrypt_len < 0)
2341 /* Check the padding. See RFC 3447, section 7.2.2. */
2344 * The smallest padded premaster is 11 bytes of overhead. Small keys
2345 * are publicly invalid, so this may return immediately. This ensures
2346 * PS is at least 8 bytes.
2348 if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {
2349 *al = SSL_AD_DECRYPT_ERROR;
2350 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_DECRYPTION_FAILED);
2354 padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;
2355 decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) &
2356 constant_time_eq_int_8(rsa_decrypt[1], 2);
2357 for (j = 2; j < padding_len - 1; j++) {
2358 decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]);
2360 decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]);
2363 * If the version in the decrypted pre-master secret is correct then
2364 * version_good will be 0xff, otherwise it'll be zero. The
2365 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2366 * (http://eprint.iacr.org/2003/052/) exploits the version number
2367 * check as a "bad version oracle". Thus version checks are done in
2368 * constant time and are treated like any other decryption error.
2371 constant_time_eq_8(rsa_decrypt[padding_len],
2372 (unsigned)(s->client_version >> 8));
2374 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2375 (unsigned)(s->client_version & 0xff));
2378 * The premaster secret must contain the same version number as the
2379 * ClientHello to detect version rollback attacks (strangely, the
2380 * protocol does not offer such protection for DH ciphersuites).
2381 * However, buggy clients exist that send the negotiated protocol
2382 * version instead if the server does not support the requested
2383 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2386 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2387 unsigned char workaround_good;
2388 workaround_good = constant_time_eq_8(rsa_decrypt[padding_len],
2389 (unsigned)(s->version >> 8));
2391 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2392 (unsigned)(s->version & 0xff));
2393 version_good |= workaround_good;
2397 * Both decryption and version must be good for decrypt_good to
2398 * remain non-zero (0xff).
2400 decrypt_good &= version_good;
2403 * Now copy rand_premaster_secret over from p using
2404 * decrypt_good_mask. If decryption failed, then p does not
2405 * contain valid plaintext, however, a check above guarantees
2406 * it is still sufficiently large to read from.
2408 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2409 rsa_decrypt[padding_len + j] =
2410 constant_time_select_8(decrypt_good,
2411 rsa_decrypt[padding_len + j],
2412 rand_premaster_secret[j]);
2415 if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len,
2416 sizeof(rand_premaster_secret), 0)) {
2417 *al = SSL_AD_INTERNAL_ERROR;
2418 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);
2424 OPENSSL_free(rsa_decrypt);
2427 /* Should never happen */
2428 *al = SSL_AD_INTERNAL_ERROR;
2429 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);
2434 static int tls_process_cke_dhe(SSL *s, PACKET *pkt, int *al)
2436 #ifndef OPENSSL_NO_DH
2437 EVP_PKEY *skey = NULL;
2441 const unsigned char *data;
2442 EVP_PKEY *ckey = NULL;
2445 if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
2446 *al = SSL_AD_HANDSHAKE_FAILURE;
2447 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE,
2448 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2451 skey = s->s3->tmp.pkey;
2453 *al = SSL_AD_HANDSHAKE_FAILURE;
2454 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_MISSING_TMP_DH_KEY);
2458 if (PACKET_remaining(pkt) == 0L) {
2459 *al = SSL_AD_HANDSHAKE_FAILURE;
2460 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_MISSING_TMP_DH_KEY);
2463 if (!PACKET_get_bytes(pkt, &data, i)) {
2464 /* We already checked we have enough data */
2465 *al = SSL_AD_INTERNAL_ERROR;
2466 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2469 ckey = EVP_PKEY_new();
2470 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
2471 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_BN_LIB);
2474 cdh = EVP_PKEY_get0_DH(ckey);
2475 pub_key = BN_bin2bn(data, i, NULL);
2477 if (pub_key == NULL || !DH_set0_key(cdh, pub_key, NULL)) {
2478 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2479 if (pub_key != NULL)
2484 if (ssl_derive(s, skey, ckey, 1) == 0) {
2485 *al = SSL_AD_INTERNAL_ERROR;
2486 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2491 EVP_PKEY_free(s->s3->tmp.pkey);
2492 s->s3->tmp.pkey = NULL;
2494 EVP_PKEY_free(ckey);
2497 /* Should never happen */
2498 *al = SSL_AD_INTERNAL_ERROR;
2499 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2504 static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt, int *al)
2506 #ifndef OPENSSL_NO_EC
2507 EVP_PKEY *skey = s->s3->tmp.pkey;
2508 EVP_PKEY *ckey = NULL;
2511 if (PACKET_remaining(pkt) == 0L) {
2512 /* We don't support ECDH client auth */
2513 *al = SSL_AD_HANDSHAKE_FAILURE;
2514 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, SSL_R_MISSING_TMP_ECDH_KEY);
2518 const unsigned char *data;
2521 * Get client's public key from encoded point in the
2522 * ClientKeyExchange message.
2525 /* Get encoded point length */
2526 if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
2527 || PACKET_remaining(pkt) != 0) {
2528 *al = SSL_AD_DECODE_ERROR;
2529 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, SSL_R_LENGTH_MISMATCH);
2532 ckey = EVP_PKEY_new();
2533 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
2534 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_EVP_LIB);
2537 if (EVP_PKEY_set1_tls_encodedpoint(ckey, data, i) == 0) {
2538 *al = SSL_AD_HANDSHAKE_FAILURE;
2539 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_EC_LIB);
2544 if (ssl_derive(s, skey, ckey, 1) == 0) {
2545 *al = SSL_AD_INTERNAL_ERROR;
2546 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2551 EVP_PKEY_free(s->s3->tmp.pkey);
2552 s->s3->tmp.pkey = NULL;
2554 EVP_PKEY_free(ckey);
2558 /* Should never happen */
2559 *al = SSL_AD_INTERNAL_ERROR;
2560 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2565 static int tls_process_cke_srp(SSL *s, PACKET *pkt, int *al)
2567 #ifndef OPENSSL_NO_SRP
2569 const unsigned char *data;
2571 if (!PACKET_get_net_2(pkt, &i)
2572 || !PACKET_get_bytes(pkt, &data, i)) {
2573 *al = SSL_AD_DECODE_ERROR;
2574 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, SSL_R_BAD_SRP_A_LENGTH);
2577 if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
2578 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_BN_LIB);
2581 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
2582 *al = SSL_AD_ILLEGAL_PARAMETER;
2583 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, SSL_R_BAD_SRP_PARAMETERS);
2586 OPENSSL_free(s->session->srp_username);
2587 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2588 if (s->session->srp_username == NULL) {
2589 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_MALLOC_FAILURE);
2593 if (!srp_generate_server_master_secret(s)) {
2594 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_INTERNAL_ERROR);
2600 /* Should never happen */
2601 *al = SSL_AD_INTERNAL_ERROR;
2602 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_INTERNAL_ERROR);
2607 static int tls_process_cke_gost(SSL *s, PACKET *pkt, int *al)
2609 #ifndef OPENSSL_NO_GOST
2610 EVP_PKEY_CTX *pkey_ctx;
2611 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2612 unsigned char premaster_secret[32];
2613 const unsigned char *start;
2614 size_t outlen = 32, inlen;
2615 unsigned long alg_a;
2618 size_t sess_key_len;
2619 const unsigned char *data;
2622 /* Get our certificate private key */
2623 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2624 if (alg_a & SSL_aGOST12) {
2626 * New GOST ciphersuites have SSL_aGOST01 bit too
2628 pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
2630 pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
2633 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2635 } else if (alg_a & SSL_aGOST01) {
2636 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2639 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2640 if (pkey_ctx == NULL) {
2641 *al = SSL_AD_INTERNAL_ERROR;
2642 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_MALLOC_FAILURE);
2645 if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
2646 *al = SSL_AD_INTERNAL_ERROR;
2647 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2651 * If client certificate is present and is of the same type, maybe
2652 * use it for key exchange. Don't mind errors from
2653 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2654 * client certificate for authorization only.
2656 client_pub_pkey = X509_get0_pubkey(s->session->peer);
2657 if (client_pub_pkey) {
2658 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2661 /* Decrypt session key */
2662 sess_key_len = PACKET_remaining(pkt);
2663 if (!PACKET_get_bytes(pkt, &data, sess_key_len)) {
2664 *al = SSL_AD_INTERNAL_ERROR;
2665 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2668 /* TODO(size_t): Convert this function */
2669 if (ASN1_get_object((const unsigned char **)&data, &Tlen, &Ttag,
2670 &Tclass, (long)sess_key_len) != V_ASN1_CONSTRUCTED
2671 || Ttag != V_ASN1_SEQUENCE || Tclass != V_ASN1_UNIVERSAL) {
2672 *al = SSL_AD_DECODE_ERROR;
2673 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, SSL_R_DECRYPTION_FAILED);
2678 if (EVP_PKEY_decrypt
2679 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2680 *al = SSL_AD_DECODE_ERROR;
2681 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, SSL_R_DECRYPTION_FAILED);
2684 /* Generate master secret */
2685 if (!ssl_generate_master_secret(s, premaster_secret,
2686 sizeof(premaster_secret), 0)) {
2687 *al = SSL_AD_INTERNAL_ERROR;
2688 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2691 /* Check if pubkey from client certificate was used */
2692 if (EVP_PKEY_CTX_ctrl
2693 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2694 s->statem.no_cert_verify = 1;
2698 EVP_PKEY_CTX_free(pkey_ctx);
2701 /* Should never happen */
2702 *al = SSL_AD_INTERNAL_ERROR;
2703 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2708 MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
2711 unsigned long alg_k;
2713 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2715 /* For PSK parse and retrieve identity, obtain PSK key */
2716 if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt, &al))
2719 if (alg_k & SSL_kPSK) {
2720 /* Identity extracted earlier: should be nothing left */
2721 if (PACKET_remaining(pkt) != 0) {
2722 al = SSL_AD_HANDSHAKE_FAILURE;
2723 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2724 SSL_R_LENGTH_MISMATCH);
2727 /* PSK handled by ssl_generate_master_secret */
2728 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
2729 al = SSL_AD_INTERNAL_ERROR;
2730 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2733 } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2734 if (!tls_process_cke_rsa(s, pkt, &al))
2736 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2737 if (!tls_process_cke_dhe(s, pkt, &al))
2739 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2740 if (!tls_process_cke_ecdhe(s, pkt, &al))
2742 } else if (alg_k & SSL_kSRP) {
2743 if (!tls_process_cke_srp(s, pkt, &al))
2745 } else if (alg_k & SSL_kGOST) {
2746 if (!tls_process_cke_gost(s, pkt, &al))
2749 al = SSL_AD_HANDSHAKE_FAILURE;
2750 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2751 SSL_R_UNKNOWN_CIPHER_TYPE);
2755 return MSG_PROCESS_CONTINUE_PROCESSING;
2758 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2759 #ifndef OPENSSL_NO_PSK
2760 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2761 s->s3->tmp.psk = NULL;
2763 ossl_statem_set_error(s);
2764 return MSG_PROCESS_ERROR;
2767 WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
2769 #ifndef OPENSSL_NO_SCTP
2770 if (wst == WORK_MORE_A) {
2771 if (SSL_IS_DTLS(s)) {
2772 unsigned char sctpauthkey[64];
2773 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2775 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2778 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2779 sizeof(DTLS1_SCTP_AUTH_LABEL));
2781 if (SSL_export_keying_material(s, sctpauthkey,
2782 sizeof(sctpauthkey), labelbuffer,
2783 sizeof(labelbuffer), NULL, 0,
2785 ossl_statem_set_error(s);
2789 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2790 sizeof(sctpauthkey), sctpauthkey);
2795 if ((wst == WORK_MORE_B)
2797 && BIO_dgram_is_sctp(SSL_get_wbio(s))
2798 /* Are we renegotiating? */
2800 /* Are we going to skip the CertificateVerify? */
2801 && (s->session->peer == NULL || s->statem.no_cert_verify)
2802 && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
2803 s->s3->in_read_app_data = 2;
2804 s->rwstate = SSL_READING;
2805 BIO_clear_retry_flags(SSL_get_rbio(s));
2806 BIO_set_retry_read(SSL_get_rbio(s));
2807 ossl_statem_set_sctp_read_sock(s, 1);
2810 ossl_statem_set_sctp_read_sock(s, 0);
2814 if (s->statem.no_cert_verify || !s->session->peer) {
2816 * No certificate verify or no peer certificate so we no longer need
2817 * the handshake_buffer
2819 if (!ssl3_digest_cached_records(s, 0)) {
2820 ossl_statem_set_error(s);
2823 return WORK_FINISHED_CONTINUE;
2825 if (!s->s3->handshake_buffer) {
2826 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
2827 ERR_R_INTERNAL_ERROR);
2828 ossl_statem_set_error(s);
2832 * For sigalgs freeze the handshake buffer. If we support
2833 * extms we've done this already so this is a no-op
2835 if (!ssl3_digest_cached_records(s, 1)) {
2836 ossl_statem_set_error(s);
2841 return WORK_FINISHED_CONTINUE;
2844 MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt)
2846 EVP_PKEY *pkey = NULL;
2847 const unsigned char *sig, *data;
2848 #ifndef OPENSSL_NO_GOST
2849 unsigned char *gost_data = NULL;
2851 int al, ret = MSG_PROCESS_ERROR;
2855 const EVP_MD *md = NULL;
2859 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
2862 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
2863 al = SSL_AD_INTERNAL_ERROR;
2867 peer = s->session->peer;
2868 pkey = X509_get0_pubkey(peer);
2869 type = X509_certificate_type(peer, pkey);
2871 if (!(type & EVP_PKT_SIGN)) {
2872 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY,
2873 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2874 al = SSL_AD_ILLEGAL_PARAMETER;
2878 /* Check for broken implementations of GOST ciphersuites */
2880 * If key is GOST and n is exactly 64, it is bare signature without
2881 * length field (CryptoPro implementations at least till CSP 4.0)
2883 #ifndef OPENSSL_NO_GOST
2884 if (PACKET_remaining(pkt) == 64
2885 && EVP_PKEY_id(pkey) == NID_id_GostR3410_2001) {
2890 if (SSL_USE_SIGALGS(s)) {
2893 if (!PACKET_get_bytes(pkt, &sig, 2)) {
2894 al = SSL_AD_DECODE_ERROR;
2897 rv = tls12_check_peer_sigalg(&md, s, sig, pkey);
2899 al = SSL_AD_INTERNAL_ERROR;
2901 } else if (rv == 0) {
2902 al = SSL_AD_DECODE_ERROR;
2906 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
2909 /* Use default digest for this key type */
2910 int idx = ssl_cert_type(NULL, pkey);
2912 md = s->s3->tmp.md[idx];
2914 al = SSL_AD_INTERNAL_ERROR;
2919 if (!PACKET_get_net_2(pkt, &len)) {
2920 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2921 al = SSL_AD_DECODE_ERROR;
2925 j = EVP_PKEY_size(pkey);
2926 if (((int)len > j) || ((int)PACKET_remaining(pkt) > j)
2927 || (PACKET_remaining(pkt) == 0)) {
2928 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
2929 al = SSL_AD_DECODE_ERROR;
2932 if (!PACKET_get_bytes(pkt, &data, len)) {
2933 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2934 al = SSL_AD_DECODE_ERROR;
2938 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2939 if (hdatalen <= 0) {
2940 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
2941 al = SSL_AD_INTERNAL_ERROR;
2946 fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md));
2948 if (!EVP_VerifyInit_ex(mctx, md, NULL)
2949 || !EVP_VerifyUpdate(mctx, hdata, hdatalen)) {
2950 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
2951 al = SSL_AD_INTERNAL_ERROR;
2954 #ifndef OPENSSL_NO_GOST
2956 int pktype = EVP_PKEY_id(pkey);
2957 if (pktype == NID_id_GostR3410_2001
2958 || pktype == NID_id_GostR3410_2012_256
2959 || pktype == NID_id_GostR3410_2012_512) {
2960 if ((gost_data = OPENSSL_malloc(len)) == NULL) {
2961 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
2962 al = SSL_AD_INTERNAL_ERROR;
2965 BUF_reverse(gost_data, data, len);
2971 if (s->version == SSL3_VERSION
2972 && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
2973 (int)s->session->master_key_length,
2974 s->session->master_key)) {
2975 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
2976 al = SSL_AD_INTERNAL_ERROR;
2980 if (EVP_VerifyFinal(mctx, data, len, pkey) <= 0) {
2981 al = SSL_AD_DECRYPT_ERROR;
2982 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
2986 ret = MSG_PROCESS_CONTINUE_PROCESSING;
2989 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2990 ossl_statem_set_error(s);
2992 BIO_free(s->s3->handshake_buffer);
2993 s->s3->handshake_buffer = NULL;
2994 EVP_MD_CTX_free(mctx);
2995 #ifndef OPENSSL_NO_GOST
2996 OPENSSL_free(gost_data);
3001 MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
3003 int i, al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR;
3005 unsigned long l, llen;
3006 const unsigned char *certstart, *certbytes;
3007 STACK_OF(X509) *sk = NULL;
3010 if ((sk = sk_X509_new_null()) == NULL) {
3011 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3015 if (!PACKET_get_net_3(pkt, &llen)
3016 || !PACKET_get_sub_packet(pkt, &spkt, llen)
3017 || PACKET_remaining(pkt) != 0) {
3018 al = SSL_AD_DECODE_ERROR;
3019 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3023 while (PACKET_remaining(&spkt) > 0) {
3024 if (!PACKET_get_net_3(&spkt, &l)
3025 || !PACKET_get_bytes(&spkt, &certbytes, l)) {
3026 al = SSL_AD_DECODE_ERROR;
3027 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3028 SSL_R_CERT_LENGTH_MISMATCH);
3032 certstart = certbytes;
3033 x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
3035 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3038 if (certbytes != (certstart + l)) {
3039 al = SSL_AD_DECODE_ERROR;
3040 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3041 SSL_R_CERT_LENGTH_MISMATCH);
3044 if (!sk_X509_push(sk, x)) {
3045 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3051 if (sk_X509_num(sk) <= 0) {
3052 /* TLS does not mind 0 certs returned */
3053 if (s->version == SSL3_VERSION) {
3054 al = SSL_AD_HANDSHAKE_FAILURE;
3055 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3056 SSL_R_NO_CERTIFICATES_RETURNED);
3059 /* Fail for TLS only if we required a certificate */
3060 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3061 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3062 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3063 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3064 al = SSL_AD_HANDSHAKE_FAILURE;
3067 /* No client certificate so digest cached records */
3068 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3073 i = ssl_verify_cert_chain(s, sk);
3075 al = ssl_verify_alarm_type(s->verify_result);
3076 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3077 SSL_R_CERTIFICATE_VERIFY_FAILED);
3081 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
3082 al = SSL_AD_HANDSHAKE_FAILURE;
3085 pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
3087 al = SSL3_AD_HANDSHAKE_FAILURE;
3088 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3089 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3094 X509_free(s->session->peer);
3095 s->session->peer = sk_X509_shift(sk);
3096 s->session->verify_result = s->verify_result;
3098 sk_X509_pop_free(s->session->peer_chain, X509_free);
3099 s->session->peer_chain = sk;
3102 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3105 if (SSL_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
3106 al = SSL_AD_INTERNAL_ERROR;
3107 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3112 * Inconsistency alert: cert_chain does *not* include the peer's own
3113 * certificate, while we do include it in statem_clnt.c
3116 ret = MSG_PROCESS_CONTINUE_READING;
3120 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3121 ossl_statem_set_error(s);
3124 sk_X509_pop_free(sk, X509_free);
3128 int tls_construct_server_certificate(SSL *s, WPACKET *pkt)
3132 cpk = ssl_get_server_send_pkey(s);
3134 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3138 if (!ssl3_output_cert_chain(s, pkt, cpk)) {
3139 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3146 int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
3148 unsigned char *senc = NULL;
3149 EVP_CIPHER_CTX *ctx = NULL;
3150 HMAC_CTX *hctx = NULL;
3151 unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
3152 const unsigned char *const_p;
3153 int len, slen_full, slen, lenfinal;
3156 SSL_CTX *tctx = s->initial_ctx;
3157 unsigned char iv[EVP_MAX_IV_LENGTH];
3158 unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
3160 size_t macoffset, macendoffset;
3162 /* get session encoding length */
3163 slen_full = i2d_SSL_SESSION(s->session, NULL);
3165 * Some length values are 16 bits, so forget it if session is too
3168 if (slen_full == 0 || slen_full > 0xFF00) {
3169 ossl_statem_set_error(s);
3172 senc = OPENSSL_malloc(slen_full);
3174 ossl_statem_set_error(s);
3178 ctx = EVP_CIPHER_CTX_new();
3179 hctx = HMAC_CTX_new();
3180 if (ctx == NULL || hctx == NULL) {
3181 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
3186 if (!i2d_SSL_SESSION(s->session, &p))
3190 * create a fresh copy (not shared with other threads) to clean up
3193 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3196 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3198 slen = i2d_SSL_SESSION(sess, NULL);
3199 if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3200 SSL_SESSION_free(sess);
3204 if (!i2d_SSL_SESSION(sess, &p)) {
3205 SSL_SESSION_free(sess);
3208 SSL_SESSION_free(sess);
3211 * Initialize HMAC and cipher contexts. If callback present it does
3212 * all the work otherwise use generated values from parent ctx.
3214 if (tctx->tlsext_ticket_key_cb) {
3215 /* if 0 is returned, write an empty ticket */
3216 int ret = tctx->tlsext_ticket_key_cb(s, key_name, iv, ctx,
3221 /* Put timeout and length */
3222 if (!WPACKET_put_bytes_u32(pkt, 0)
3223 || !WPACKET_put_bytes_u16(pkt, 0)) {
3224 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3225 ERR_R_INTERNAL_ERROR);
3229 EVP_CIPHER_CTX_free(ctx);
3230 HMAC_CTX_free(hctx);
3235 iv_len = EVP_CIPHER_CTX_iv_length(ctx);
3237 const EVP_CIPHER *cipher = EVP_aes_256_cbc();
3239 iv_len = EVP_CIPHER_iv_length(cipher);
3240 if (RAND_bytes(iv, iv_len) <= 0)
3242 if (!EVP_EncryptInit_ex(ctx, cipher, NULL,
3243 tctx->tlsext_tick_aes_key, iv))
3245 if (!HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key,
3246 sizeof(tctx->tlsext_tick_hmac_key),
3247 EVP_sha256(), NULL))
3249 memcpy(key_name, tctx->tlsext_tick_key_name,
3250 sizeof(tctx->tlsext_tick_key_name));
3254 * Ticket lifetime hint (advisory only): We leave this unspecified
3255 * for resumed session (for simplicity), and guess that tickets for
3256 * new sessions will live as long as their sessions.
3258 if (!WPACKET_put_bytes_u32(pkt, s->hit ? 0 : s->session->timeout)
3259 /* Now the actual ticket data */
3260 || !WPACKET_start_sub_packet_u16(pkt)
3261 || !WPACKET_get_total_written(pkt, &macoffset)
3262 /* Output key name */
3263 || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
3265 || !WPACKET_memcpy(pkt, iv, iv_len)
3266 || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
3268 /* Encrypt session data */
3269 || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
3270 || !WPACKET_allocate_bytes(pkt, len, &encdata2)
3271 || encdata1 != encdata2
3272 || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
3273 || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
3274 || encdata1 + len != encdata2
3275 || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
3276 || !WPACKET_get_total_written(pkt, &macendoffset)
3277 || !HMAC_Update(hctx,
3278 (unsigned char *)s->init_buf->data + macoffset,
3279 macendoffset - macoffset)
3280 || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
3281 || !HMAC_Final(hctx, macdata1, &hlen)
3282 || hlen > EVP_MAX_MD_SIZE
3283 || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
3284 || macdata1 != macdata2
3285 || !WPACKET_close(pkt)) {
3286 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
3289 EVP_CIPHER_CTX_free(ctx);
3290 HMAC_CTX_free(hctx);
3296 EVP_CIPHER_CTX_free(ctx);
3297 HMAC_CTX_free(hctx);
3298 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3302 int tls_construct_cert_status(SSL *s, WPACKET *pkt)
3304 if (!WPACKET_put_bytes_u8(pkt, s->tlsext_status_type)
3305 || !WPACKET_sub_memcpy_u24(pkt, s->tlsext_ocsp_resp,
3306 s->tlsext_ocsp_resplen)) {
3307 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_STATUS, ERR_R_INTERNAL_ERROR);
3308 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3315 #ifndef OPENSSL_NO_NEXTPROTONEG
3317 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
3318 * It sets the next_proto member in s if found
3320 MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
3322 PACKET next_proto, padding;
3323 size_t next_proto_len;
3326 * The payload looks like:
3328 * uint8 proto[proto_len];
3329 * uint8 padding_len;
3330 * uint8 padding[padding_len];
3332 if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
3333 || !PACKET_get_length_prefixed_1(pkt, &padding)
3334 || PACKET_remaining(pkt) > 0) {
3335 SSLerr(SSL_F_TLS_PROCESS_NEXT_PROTO, SSL_R_LENGTH_MISMATCH);
3339 if (!PACKET_memdup(&next_proto, &s->next_proto_negotiated, &next_proto_len)) {
3340 s->next_proto_negotiated_len = 0;
3344 s->next_proto_negotiated_len = (unsigned char)next_proto_len;
3346 return MSG_PROCESS_CONTINUE_READING;
3348 ossl_statem_set_error(s);
3349 return MSG_PROCESS_ERROR;
3353 #define SSLV2_CIPHER_LEN 3
3355 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,
3356 PACKET *cipher_suites,
3357 STACK_OF(SSL_CIPHER) **skp,
3358 int sslv2format, int *al)
3360 const SSL_CIPHER *c;
3361 STACK_OF(SSL_CIPHER) *sk;
3363 /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
3364 unsigned char cipher[SSLV2_CIPHER_LEN];
3366 s->s3->send_connection_binding = 0;
3368 n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
3370 if (PACKET_remaining(cipher_suites) == 0) {
3371 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, SSL_R_NO_CIPHERS_SPECIFIED);
3372 *al = SSL_AD_ILLEGAL_PARAMETER;
3376 if (PACKET_remaining(cipher_suites) % n != 0) {
3377 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3378 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
3379 *al = SSL_AD_DECODE_ERROR;
3383 if ((skp == NULL) || (*skp == NULL)) {
3384 sk = sk_SSL_CIPHER_new_null(); /* change perhaps later */
3386 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3387 *al = SSL_AD_INTERNAL_ERROR;
3392 sk_SSL_CIPHER_zero(sk);
3395 if (!PACKET_memdup(cipher_suites, &s->s3->tmp.ciphers_raw,
3396 &s->s3->tmp.ciphers_rawlen)) {
3397 *al = SSL_AD_INTERNAL_ERROR;
3401 while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
3403 * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
3404 * first byte set to zero, while true SSLv2 ciphers have a non-zero
3405 * first byte. We don't support any true SSLv2 ciphers, so skip them.
3407 if (sslv2format && cipher[0] != '\0')
3410 /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
3411 if ((cipher[n - 2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
3412 (cipher[n - 1] == (SSL3_CK_SCSV & 0xff))) {
3413 /* SCSV fatal if renegotiating */
3414 if (s->renegotiate) {
3415 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3416 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
3417 *al = SSL_AD_HANDSHAKE_FAILURE;
3420 s->s3->send_connection_binding = 1;
3424 /* Check for TLS_FALLBACK_SCSV */
3425 if ((cipher[n - 2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
3426 (cipher[n - 1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) {
3428 * The SCSV indicates that the client previously tried a higher
3429 * version. Fail if the current version is an unexpected
3432 if (!ssl_check_version_downgrade(s)) {
3433 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3434 SSL_R_INAPPROPRIATE_FALLBACK);
3435 *al = SSL_AD_INAPPROPRIATE_FALLBACK;
3441 /* For SSLv2-compat, ignore leading 0-byte. */
3442 c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher);
3444 if (!sk_SSL_CIPHER_push(sk, c)) {
3445 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3446 *al = SSL_AD_INTERNAL_ERROR;
3451 if (PACKET_remaining(cipher_suites) > 0) {
3452 *al = SSL_AD_INTERNAL_ERROR;
3453 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_INTERNAL_ERROR);
3461 if ((skp == NULL) || (*skp == NULL))
3462 sk_SSL_CIPHER_free(sk);