preprocessor cleanup: __lint
[unleashed/tickless.git] / lib / libssl / ssl_srvr.c
blob16899de37f64466cb932d26029e01a91f6d93f98
1 /* $OpenBSD: ssl_srvr.c,v 1.11.4.1 2017/04/29 23:41:32 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
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
25 * are met:
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
51 * SUCH DAMAGE.
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
124 /* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
129 * license.
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
148 * OTHERWISE.
151 #include <stdio.h>
153 #include "ssl_locl.h"
155 #include <openssl/bn.h>
156 #include <openssl/buffer.h>
157 #include <openssl/curve25519.h>
158 #include <openssl/evp.h>
159 #include <openssl/dh.h>
160 #ifndef OPENSSL_NO_GOST
161 #include <openssl/gost.h>
162 #endif
163 #include <openssl/hmac.h>
164 #include <openssl/md5.h>
165 #include <openssl/objects.h>
166 #include <openssl/x509.h>
168 #include "bytestring.h"
171 ssl3_accept(SSL *s)
173 unsigned long alg_k;
174 void (*cb)(const SSL *ssl, int type, int val) = NULL;
175 int ret = -1;
176 int new_state, state, skip = 0;
178 ERR_clear_error();
179 errno = 0;
181 if (s->internal->info_callback != NULL)
182 cb = s->internal->info_callback;
183 else if (s->ctx->internal->info_callback != NULL)
184 cb = s->ctx->internal->info_callback;
186 /* init things to blank */
187 s->internal->in_handshake++;
188 if (!SSL_in_init(s) || SSL_in_before(s))
189 SSL_clear(s);
191 if (s->cert == NULL) {
192 SSLerror(s, SSL_R_NO_CERTIFICATE_SET);
193 ret = -1;
194 goto end;
197 for (;;) {
198 state = s->internal->state;
200 switch (s->internal->state) {
201 case SSL_ST_RENEGOTIATE:
202 s->internal->renegotiate = 1;
203 /* s->internal->state=SSL_ST_ACCEPT; */
205 case SSL_ST_BEFORE:
206 case SSL_ST_ACCEPT:
207 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
208 case SSL_ST_OK|SSL_ST_ACCEPT:
210 s->server = 1;
211 if (cb != NULL)
212 cb(s, SSL_CB_HANDSHAKE_START, 1);
214 if ((s->version >> 8) != 3) {
215 SSLerror(s, ERR_R_INTERNAL_ERROR);
216 ret = -1;
217 goto end;
219 s->internal->type = SSL_ST_ACCEPT;
221 if (!ssl3_setup_init_buffer(s)) {
222 ret = -1;
223 goto end;
225 if (!ssl3_setup_buffers(s)) {
226 ret = -1;
227 goto end;
230 s->internal->init_num = 0;
232 if (s->internal->state != SSL_ST_RENEGOTIATE) {
234 * Ok, we now need to push on a buffering BIO
235 * so that the output is sent in a way that
236 * TCP likes :-)
238 if (!ssl_init_wbio_buffer(s, 1)) {
239 ret = -1;
240 goto end;
243 if (!tls1_init_finished_mac(s)) {
244 ret = -1;
245 goto end;
248 s->internal->state = SSL3_ST_SR_CLNT_HELLO_A;
249 s->ctx->internal->stats.sess_accept++;
250 } else if (!S3I(s)->send_connection_binding) {
252 * Server attempting to renegotiate with
253 * client that doesn't support secure
254 * renegotiation.
256 SSLerror(s, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
257 ssl3_send_alert(s, SSL3_AL_FATAL,
258 SSL_AD_HANDSHAKE_FAILURE);
259 ret = -1;
260 goto end;
261 } else {
263 * s->internal->state == SSL_ST_RENEGOTIATE,
264 * we will just send a HelloRequest
266 s->ctx->internal->stats.sess_accept_renegotiate++;
267 s->internal->state = SSL3_ST_SW_HELLO_REQ_A;
269 break;
271 case SSL3_ST_SW_HELLO_REQ_A:
272 case SSL3_ST_SW_HELLO_REQ_B:
274 s->internal->shutdown = 0;
275 ret = ssl3_send_hello_request(s);
276 if (ret <= 0)
277 goto end;
278 S3I(s)->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
279 s->internal->state = SSL3_ST_SW_FLUSH;
280 s->internal->init_num = 0;
282 if (!tls1_init_finished_mac(s)) {
283 ret = -1;
284 goto end;
286 break;
288 case SSL3_ST_SW_HELLO_REQ_C:
289 s->internal->state = SSL_ST_OK;
290 break;
292 case SSL3_ST_SR_CLNT_HELLO_A:
293 case SSL3_ST_SR_CLNT_HELLO_B:
294 case SSL3_ST_SR_CLNT_HELLO_C:
296 s->internal->shutdown = 0;
297 if (s->internal->rwstate != SSL_X509_LOOKUP) {
298 ret = ssl3_get_client_hello(s);
299 if (ret <= 0)
300 goto end;
303 s->internal->renegotiate = 2;
304 s->internal->state = SSL3_ST_SW_SRVR_HELLO_A;
305 s->internal->init_num = 0;
306 break;
308 case SSL3_ST_SW_SRVR_HELLO_A:
309 case SSL3_ST_SW_SRVR_HELLO_B:
310 ret = ssl3_send_server_hello(s);
311 if (ret <= 0)
312 goto end;
313 if (s->internal->hit) {
314 if (s->internal->tlsext_ticket_expected)
315 s->internal->state = SSL3_ST_SW_SESSION_TICKET_A;
316 else
317 s->internal->state = SSL3_ST_SW_CHANGE_A;
319 else
320 s->internal->state = SSL3_ST_SW_CERT_A;
321 s->internal->init_num = 0;
322 break;
324 case SSL3_ST_SW_CERT_A:
325 case SSL3_ST_SW_CERT_B:
326 /* Check if it is anon DH or anon ECDH. */
327 if (!(S3I(s)->tmp.new_cipher->algorithm_auth &
328 SSL_aNULL)) {
329 ret = ssl3_send_server_certificate(s);
330 if (ret <= 0)
331 goto end;
332 if (s->internal->tlsext_status_expected)
333 s->internal->state = SSL3_ST_SW_CERT_STATUS_A;
334 else
335 s->internal->state = SSL3_ST_SW_KEY_EXCH_A;
336 } else {
337 skip = 1;
338 s->internal->state = SSL3_ST_SW_KEY_EXCH_A;
340 s->internal->init_num = 0;
341 break;
343 case SSL3_ST_SW_KEY_EXCH_A:
344 case SSL3_ST_SW_KEY_EXCH_B:
345 alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey;
348 * Only send if using a DH key exchange.
350 * For ECC ciphersuites, we send a ServerKeyExchange
351 * message only if the cipher suite is ECDHE. In other
352 * cases, the server certificate contains the server's
353 * public key for key exchange.
355 if (alg_k & (SSL_kDHE|SSL_kECDHE)) {
356 ret = ssl3_send_server_key_exchange(s);
357 if (ret <= 0)
358 goto end;
359 } else
360 skip = 1;
362 s->internal->state = SSL3_ST_SW_CERT_REQ_A;
363 s->internal->init_num = 0;
364 break;
366 case SSL3_ST_SW_CERT_REQ_A:
367 case SSL3_ST_SW_CERT_REQ_B:
369 * Determine whether or not we need to request a
370 * certificate.
372 * Do not request a certificate if:
374 * - We did not ask for it (SSL_VERIFY_PEER is unset).
376 * - SSL_VERIFY_CLIENT_ONCE is set and we are
377 * renegotiating.
379 * - We are using an anonymous ciphersuites
380 * (see section "Certificate request" in SSL 3 drafts
381 * and in RFC 2246) ... except when the application
382 * insists on verification (against the specs, but
383 * s3_clnt.c accepts this for SSL 3).
385 if (!(s->verify_mode & SSL_VERIFY_PEER) ||
386 ((s->session->peer != NULL) &&
387 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
388 ((S3I(s)->tmp.new_cipher->algorithm_auth &
389 SSL_aNULL) && !(s->verify_mode &
390 SSL_VERIFY_FAIL_IF_NO_PEER_CERT))) {
391 /* No cert request */
392 skip = 1;
393 S3I(s)->tmp.cert_request = 0;
394 s->internal->state = SSL3_ST_SW_SRVR_DONE_A;
395 if (S3I(s)->handshake_buffer) {
396 if (!tls1_digest_cached_records(s)) {
397 ret = -1;
398 goto end;
401 } else {
402 S3I(s)->tmp.cert_request = 1;
403 ret = ssl3_send_certificate_request(s);
404 if (ret <= 0)
405 goto end;
406 s->internal->state = SSL3_ST_SW_SRVR_DONE_A;
407 s->internal->init_num = 0;
409 break;
411 case SSL3_ST_SW_SRVR_DONE_A:
412 case SSL3_ST_SW_SRVR_DONE_B:
413 ret = ssl3_send_server_done(s);
414 if (ret <= 0)
415 goto end;
416 S3I(s)->tmp.next_state = SSL3_ST_SR_CERT_A;
417 s->internal->state = SSL3_ST_SW_FLUSH;
418 s->internal->init_num = 0;
419 break;
421 case SSL3_ST_SW_FLUSH:
424 * This code originally checked to see if
425 * any data was pending using BIO_CTRL_INFO
426 * and then flushed. This caused problems
427 * as documented in PR#1939. The proposed
428 * fix doesn't completely resolve this issue
429 * as buggy implementations of BIO_CTRL_PENDING
430 * still exist. So instead we just flush
431 * unconditionally.
434 s->internal->rwstate = SSL_WRITING;
435 if (BIO_flush(s->wbio) <= 0) {
436 ret = -1;
437 goto end;
439 s->internal->rwstate = SSL_NOTHING;
441 s->internal->state = S3I(s)->tmp.next_state;
442 break;
444 case SSL3_ST_SR_CERT_A:
445 case SSL3_ST_SR_CERT_B:
446 if (S3I(s)->tmp.cert_request) {
447 ret = ssl3_get_client_certificate(s);
448 if (ret <= 0)
449 goto end;
451 s->internal->init_num = 0;
452 s->internal->state = SSL3_ST_SR_KEY_EXCH_A;
453 break;
455 case SSL3_ST_SR_KEY_EXCH_A:
456 case SSL3_ST_SR_KEY_EXCH_B:
457 ret = ssl3_get_client_key_exchange(s);
458 if (ret <= 0)
459 goto end;
460 alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey;
461 if (ret == 2) {
463 * For the ECDH ciphersuites when
464 * the client sends its ECDH pub key in
465 * a certificate, the CertificateVerify
466 * message is not sent.
467 * Also for GOST ciphersuites when
468 * the client uses its key from the certificate
469 * for key exchange.
471 if (S3I(s)->next_proto_neg_seen)
472 s->internal->state = SSL3_ST_SR_NEXT_PROTO_A;
473 else
474 s->internal->state = SSL3_ST_SR_FINISHED_A;
475 s->internal->init_num = 0;
476 } else if (SSL_USE_SIGALGS(s) || (alg_k & SSL_kGOST)) {
477 s->internal->state = SSL3_ST_SR_CERT_VRFY_A;
478 s->internal->init_num = 0;
479 if (!s->session->peer)
480 break;
482 * For sigalgs freeze the handshake buffer
483 * at this point and digest cached records.
485 if (!S3I(s)->handshake_buffer) {
486 SSLerror(s, ERR_R_INTERNAL_ERROR);
487 ret = -1;
488 goto end;
490 s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
491 if (!tls1_digest_cached_records(s)) {
492 ret = -1;
493 goto end;
495 } else {
496 s->internal->state = SSL3_ST_SR_CERT_VRFY_A;
497 s->internal->init_num = 0;
500 * We need to get hashes here so if there is
501 * a client cert, it can be verified.
503 if (S3I(s)->handshake_buffer) {
504 if (!tls1_digest_cached_records(s)) {
505 ret = -1;
506 goto end;
509 if (!tls1_handshake_hash_value(s,
510 S3I(s)->tmp.cert_verify_md,
511 sizeof(S3I(s)->tmp.cert_verify_md),
512 NULL)) {
513 ret = -1;
514 goto end;
517 break;
519 case SSL3_ST_SR_CERT_VRFY_A:
520 case SSL3_ST_SR_CERT_VRFY_B:
521 s->s3->flags |= SSL3_FLAGS_CCS_OK;
523 /* we should decide if we expected this one */
524 ret = ssl3_get_cert_verify(s);
525 if (ret <= 0)
526 goto end;
528 if (S3I(s)->next_proto_neg_seen)
529 s->internal->state = SSL3_ST_SR_NEXT_PROTO_A;
530 else
531 s->internal->state = SSL3_ST_SR_FINISHED_A;
532 s->internal->init_num = 0;
533 break;
535 case SSL3_ST_SR_NEXT_PROTO_A:
536 case SSL3_ST_SR_NEXT_PROTO_B:
537 ret = ssl3_get_next_proto(s);
538 if (ret <= 0)
539 goto end;
540 s->internal->init_num = 0;
541 s->internal->state = SSL3_ST_SR_FINISHED_A;
542 break;
544 case SSL3_ST_SR_FINISHED_A:
545 case SSL3_ST_SR_FINISHED_B:
546 s->s3->flags |= SSL3_FLAGS_CCS_OK;
547 ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
548 SSL3_ST_SR_FINISHED_B);
549 if (ret <= 0)
550 goto end;
551 if (s->internal->hit)
552 s->internal->state = SSL_ST_OK;
553 else if (s->internal->tlsext_ticket_expected)
554 s->internal->state = SSL3_ST_SW_SESSION_TICKET_A;
555 else
556 s->internal->state = SSL3_ST_SW_CHANGE_A;
557 s->internal->init_num = 0;
558 break;
560 case SSL3_ST_SW_SESSION_TICKET_A:
561 case SSL3_ST_SW_SESSION_TICKET_B:
562 ret = ssl3_send_newsession_ticket(s);
563 if (ret <= 0)
564 goto end;
565 s->internal->state = SSL3_ST_SW_CHANGE_A;
566 s->internal->init_num = 0;
567 break;
569 case SSL3_ST_SW_CERT_STATUS_A:
570 case SSL3_ST_SW_CERT_STATUS_B:
571 ret = ssl3_send_cert_status(s);
572 if (ret <= 0)
573 goto end;
574 s->internal->state = SSL3_ST_SW_KEY_EXCH_A;
575 s->internal->init_num = 0;
576 break;
579 case SSL3_ST_SW_CHANGE_A:
580 case SSL3_ST_SW_CHANGE_B:
582 s->session->cipher = S3I(s)->tmp.new_cipher;
583 if (!tls1_setup_key_block(s)) {
584 ret = -1;
585 goto end;
588 ret = ssl3_send_change_cipher_spec(s,
589 SSL3_ST_SW_CHANGE_A, SSL3_ST_SW_CHANGE_B);
591 if (ret <= 0)
592 goto end;
593 s->internal->state = SSL3_ST_SW_FINISHED_A;
594 s->internal->init_num = 0;
596 if (!tls1_change_cipher_state(
597 s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
598 ret = -1;
599 goto end;
602 break;
604 case SSL3_ST_SW_FINISHED_A:
605 case SSL3_ST_SW_FINISHED_B:
606 ret = ssl3_send_finished(s,
607 SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B,
608 TLS_MD_SERVER_FINISH_CONST,
609 TLS_MD_SERVER_FINISH_CONST_SIZE);
610 if (ret <= 0)
611 goto end;
612 s->internal->state = SSL3_ST_SW_FLUSH;
613 if (s->internal->hit) {
614 if (S3I(s)->next_proto_neg_seen) {
615 s->s3->flags |= SSL3_FLAGS_CCS_OK;
616 S3I(s)->tmp.next_state =
617 SSL3_ST_SR_NEXT_PROTO_A;
618 } else
619 S3I(s)->tmp.next_state =
620 SSL3_ST_SR_FINISHED_A;
621 } else
622 S3I(s)->tmp.next_state = SSL_ST_OK;
623 s->internal->init_num = 0;
624 break;
626 case SSL_ST_OK:
627 /* clean a few things up */
628 tls1_cleanup_key_block(s);
630 BUF_MEM_free(s->internal->init_buf);
631 s->internal->init_buf = NULL;
633 /* remove buffering on output */
634 ssl_free_wbio_buffer(s);
636 s->internal->init_num = 0;
638 /* skipped if we just sent a HelloRequest */
639 if (s->internal->renegotiate == 2) {
640 s->internal->renegotiate = 0;
641 s->internal->new_session = 0;
643 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
645 s->ctx->internal->stats.sess_accept_good++;
646 /* s->server=1; */
647 s->internal->handshake_func = ssl3_accept;
649 if (cb != NULL)
650 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
653 ret = 1;
654 goto end;
655 /* break; */
657 default:
658 SSLerror(s, SSL_R_UNKNOWN_STATE);
659 ret = -1;
660 goto end;
661 /* break; */
664 if (!S3I(s)->tmp.reuse_message && !skip) {
665 if (s->internal->debug) {
666 if ((ret = BIO_flush(s->wbio)) <= 0)
667 goto end;
671 if ((cb != NULL) && (s->internal->state != state)) {
672 new_state = s->internal->state;
673 s->internal->state = state;
674 cb(s, SSL_CB_ACCEPT_LOOP, 1);
675 s->internal->state = new_state;
678 skip = 0;
680 end:
681 /* BIO_flush(s->wbio); */
683 s->internal->in_handshake--;
684 if (cb != NULL)
685 cb(s, SSL_CB_ACCEPT_EXIT, ret);
686 return (ret);
690 ssl3_send_hello_request(SSL *s)
692 CBB cbb, hello;
694 memset(&cbb, 0, sizeof(cbb));
696 if (s->internal->state == SSL3_ST_SW_HELLO_REQ_A) {
697 if (!ssl3_handshake_msg_start_cbb(s, &cbb, &hello,
698 SSL3_MT_HELLO_REQUEST))
699 goto err;
700 if (!ssl3_handshake_msg_finish_cbb(s, &cbb))
701 goto err;
703 s->internal->state = SSL3_ST_SW_HELLO_REQ_B;
706 /* SSL3_ST_SW_HELLO_REQ_B */
707 return (ssl3_handshake_write(s));
709 err:
710 CBB_cleanup(&cbb);
712 return (-1);
716 ssl3_get_client_hello(SSL *s)
718 CBS cbs, client_random, session_id, cookie, cipher_suites;
719 CBS compression_methods;
720 uint16_t client_version;
721 uint8_t comp_method;
722 int comp_null;
723 int i, j, ok, al, ret = -1, cookie_valid = 0;
724 long n;
725 unsigned long id;
726 unsigned char *p, *d;
727 SSL_CIPHER *c;
728 STACK_OF(SSL_CIPHER) *ciphers = NULL;
729 unsigned long alg_k;
730 const SSL_METHOD *method;
731 uint16_t shared_version;
732 unsigned char *end;
735 * We do this so that we will respond with our native type.
736 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
737 * This down switching should be handled by a different method.
738 * If we are SSLv3, we will respond with SSLv3, even if prompted with
739 * TLSv1.
741 if (s->internal->state == SSL3_ST_SR_CLNT_HELLO_A) {
742 s->internal->state = SSL3_ST_SR_CLNT_HELLO_B;
745 s->internal->first_packet = 1;
746 n = s->method->internal->ssl_get_message(s, SSL3_ST_SR_CLNT_HELLO_B,
747 SSL3_ST_SR_CLNT_HELLO_C, SSL3_MT_CLIENT_HELLO,
748 SSL3_RT_MAX_PLAIN_LENGTH, &ok);
749 if (!ok)
750 return ((int)n);
751 s->internal->first_packet = 0;
753 if (n < 0)
754 goto err;
756 d = p = (unsigned char *)s->internal->init_msg;
757 end = d + n;
759 CBS_init(&cbs, s->internal->init_msg, n);
762 * Use version from inside client hello, not from record header.
763 * (may differ: see RFC 2246, Appendix E, second paragraph)
765 if (!CBS_get_u16(&cbs, &client_version))
766 goto truncated;
768 if (ssl_max_shared_version(s, client_version, &shared_version) != 1) {
769 SSLerror(s, SSL_R_WRONG_VERSION_NUMBER);
770 if ((s->client_version >> 8) == SSL3_VERSION_MAJOR &&
771 !s->internal->enc_write_ctx && !s->internal->write_hash) {
773 * Similar to ssl3_get_record, send alert using remote
774 * version number.
776 s->version = s->client_version;
778 al = SSL_AD_PROTOCOL_VERSION;
779 goto f_err;
781 s->client_version = client_version;
782 s->version = shared_version;
784 if ((method = tls1_get_server_method(shared_version)) == NULL)
785 method = dtls1_get_server_method(shared_version);
786 if (method == NULL) {
787 SSLerror(s, ERR_R_INTERNAL_ERROR);
788 goto err;
790 s->method = method;
792 if (!CBS_get_bytes(&cbs, &client_random, SSL3_RANDOM_SIZE))
793 goto truncated;
794 if (!CBS_get_u8_length_prefixed(&cbs, &session_id))
795 goto truncated;
798 * If we require cookies (DTLS) and this ClientHello doesn't
799 * contain one, just return since we do not want to
800 * allocate any memory yet. So check cookie length...
802 if (SSL_IS_DTLS(s)) {
803 if (!CBS_get_u8_length_prefixed(&cbs, &cookie))
804 goto truncated;
805 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
806 if (CBS_len(&cookie) == 0)
807 return (1);
811 if (!CBS_write_bytes(&client_random, s->s3->client_random,
812 sizeof(s->s3->client_random), NULL))
813 goto err;
815 s->internal->hit = 0;
818 * Versions before 0.9.7 always allow clients to resume sessions in
819 * renegotiation. 0.9.7 and later allow this by default, but optionally
820 * ignore resumption requests with flag
821 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag
822 * rather than a change to default behavior so that applications
823 * relying on this for security won't even compile against older
824 * library versions).
826 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated()
827 * to request renegotiation but not a new session (s->internal->new_session
828 * remains unset): for servers, this essentially just means that the
829 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
830 * ignored.
832 if ((s->internal->new_session && (s->internal->options &
833 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
834 if (!ssl_get_new_session(s, 1))
835 goto err;
836 } else {
837 /* XXX - pass CBS through instead... */
838 i = ssl_get_prev_session(s,
839 (unsigned char *)CBS_data(&session_id),
840 CBS_len(&session_id), end);
841 if (i == 1) { /* previous session */
842 s->internal->hit = 1;
843 } else if (i == -1)
844 goto err;
845 else {
846 /* i == 0 */
847 if (!ssl_get_new_session(s, 1))
848 goto err;
852 if (SSL_IS_DTLS(s)) {
854 * The ClientHello may contain a cookie even if the HelloVerify
855 * message has not been sent - make sure that it does not cause
856 * an overflow.
858 if (CBS_len(&cookie) > sizeof(D1I(s)->rcvd_cookie)) {
859 al = SSL_AD_DECODE_ERROR;
860 SSLerror(s, SSL_R_COOKIE_MISMATCH);
861 goto f_err;
864 /* Verify the cookie if appropriate option is set. */
865 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
866 CBS_len(&cookie) > 0) {
867 size_t cookie_len;
869 /* XXX - rcvd_cookie seems to only be used here... */
870 if (!CBS_write_bytes(&cookie, D1I(s)->rcvd_cookie,
871 sizeof(D1I(s)->rcvd_cookie), &cookie_len))
872 goto err;
874 if (s->ctx->internal->app_verify_cookie_cb != NULL) {
875 if (s->ctx->internal->app_verify_cookie_cb(s,
876 D1I(s)->rcvd_cookie, cookie_len) == 0) {
877 al = SSL_AD_HANDSHAKE_FAILURE;
878 SSLerror(s, SSL_R_COOKIE_MISMATCH);
879 goto f_err;
881 /* else cookie verification succeeded */
882 /* XXX - can d1->cookie_len > sizeof(rcvd_cookie) ? */
883 } else if (timingsafe_memcmp(D1I(s)->rcvd_cookie,
884 D1I(s)->cookie, D1I(s)->cookie_len) != 0) {
885 /* default verification */
886 al = SSL_AD_HANDSHAKE_FAILURE;
887 SSLerror(s, SSL_R_COOKIE_MISMATCH);
888 goto f_err;
890 cookie_valid = 1;
894 if (!CBS_get_u16_length_prefixed(&cbs, &cipher_suites))
895 goto truncated;
897 /* XXX - This logic seems wrong... */
898 if (CBS_len(&cipher_suites) == 0 && CBS_len(&session_id) != 0) {
899 /* we need a cipher if we are not resuming a session */
900 al = SSL_AD_ILLEGAL_PARAMETER;
901 SSLerror(s, SSL_R_NO_CIPHERS_SPECIFIED);
902 goto f_err;
905 if (CBS_len(&cipher_suites) > 0) {
906 if ((ciphers = ssl_bytes_to_cipher_list(s,
907 CBS_data(&cipher_suites), CBS_len(&cipher_suites))) == NULL)
908 goto err;
911 /* If it is a hit, check that the cipher is in the list */
912 if (s->internal->hit && CBS_len(&cipher_suites) > 0) {
913 j = 0;
914 id = s->session->cipher->id;
916 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
917 c = sk_SSL_CIPHER_value(ciphers, i);
918 if (c->id == id) {
919 j = 1;
920 break;
923 if (j == 0) {
925 * We need to have the cipher in the cipher
926 * list if we are asked to reuse it
928 al = SSL_AD_ILLEGAL_PARAMETER;
929 SSLerror(s, SSL_R_REQUIRED_CIPHER_MISSING);
930 goto f_err;
934 if (!CBS_get_u8_length_prefixed(&cbs, &compression_methods))
935 goto truncated;
937 comp_null = 0;
938 while (CBS_len(&compression_methods) > 0) {
939 if (!CBS_get_u8(&compression_methods, &comp_method))
940 goto truncated;
941 if (comp_method == 0)
942 comp_null = 1;
944 if (comp_null == 0) {
945 al = SSL_AD_DECODE_ERROR;
946 SSLerror(s, SSL_R_NO_COMPRESSION_SPECIFIED);
947 goto f_err;
950 p = (unsigned char *)CBS_data(&cbs);
952 /* TLS extensions*/
953 if (!ssl_parse_clienthello_tlsext(s, &p, d, n, &al)) {
954 /* 'al' set by ssl_parse_clienthello_tlsext */
955 SSLerror(s, SSL_R_PARSE_TLSEXT);
956 goto f_err;
958 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
959 SSLerror(s, SSL_R_CLIENTHELLO_TLSEXT);
960 goto err;
964 * Check if we want to use external pre-shared secret for this
965 * handshake for not reused session only. We need to generate
966 * server_random before calling tls_session_secret_cb in order to allow
967 * SessionTicket processing to use it in key derivation.
969 arc4random_buf(s->s3->server_random, SSL3_RANDOM_SIZE);
971 if (!s->internal->hit && s->internal->tls_session_secret_cb) {
972 SSL_CIPHER *pref_cipher = NULL;
974 s->session->master_key_length = sizeof(s->session->master_key);
975 if (s->internal->tls_session_secret_cb(s, s->session->master_key,
976 &s->session->master_key_length, ciphers, &pref_cipher,
977 s->internal->tls_session_secret_cb_arg)) {
978 s->internal->hit = 1;
979 s->session->ciphers = ciphers;
980 s->session->verify_result = X509_V_OK;
982 ciphers = NULL;
984 /* check if some cipher was preferred by call back */
985 pref_cipher = pref_cipher ? pref_cipher :
986 ssl3_choose_cipher(s, s->session->ciphers,
987 SSL_get_ciphers(s));
988 if (pref_cipher == NULL) {
989 al = SSL_AD_HANDSHAKE_FAILURE;
990 SSLerror(s, SSL_R_NO_SHARED_CIPHER);
991 goto f_err;
994 s->session->cipher = pref_cipher;
996 sk_SSL_CIPHER_free(s->cipher_list);
997 sk_SSL_CIPHER_free(s->internal->cipher_list_by_id);
999 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1000 s->internal->cipher_list_by_id =
1001 sk_SSL_CIPHER_dup(s->session->ciphers);
1006 * Given s->session->ciphers and SSL_get_ciphers, we must
1007 * pick a cipher
1010 if (!s->internal->hit) {
1011 sk_SSL_CIPHER_free(s->session->ciphers);
1012 s->session->ciphers = ciphers;
1013 if (ciphers == NULL) {
1014 al = SSL_AD_ILLEGAL_PARAMETER;
1015 SSLerror(s, SSL_R_NO_CIPHERS_PASSED);
1016 goto f_err;
1018 ciphers = NULL;
1019 c = ssl3_choose_cipher(s, s->session->ciphers,
1020 SSL_get_ciphers(s));
1022 if (c == NULL) {
1023 al = SSL_AD_HANDSHAKE_FAILURE;
1024 SSLerror(s, SSL_R_NO_SHARED_CIPHER);
1025 goto f_err;
1027 S3I(s)->tmp.new_cipher = c;
1028 } else {
1029 S3I(s)->tmp.new_cipher = s->session->cipher;
1032 if (!tls1_handshake_hash_init(s))
1033 goto err;
1035 alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey;
1036 if (!(SSL_USE_SIGALGS(s) || (alg_k & SSL_kGOST)) ||
1037 !(s->verify_mode & SSL_VERIFY_PEER)) {
1038 if (!tls1_digest_cached_records(s)) {
1039 al = SSL_AD_INTERNAL_ERROR;
1040 goto f_err;
1045 * We now have the following setup.
1046 * client_random
1047 * cipher_list - our prefered list of ciphers
1048 * ciphers - the clients prefered list of ciphers
1049 * compression - basically ignored right now
1050 * ssl version is set - sslv3
1051 * s->session - The ssl session has been setup.
1052 * s->internal->hit - session reuse flag
1053 * s->tmp.new_cipher - the new cipher to use.
1056 /* Handles TLS extensions that we couldn't check earlier */
1057 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1058 SSLerror(s, SSL_R_CLIENTHELLO_TLSEXT);
1059 goto err;
1062 ret = cookie_valid ? 2 : 1;
1064 if (0) {
1065 truncated:
1066 al = SSL_AD_DECODE_ERROR;
1067 SSLerror(s, SSL_R_BAD_PACKET_LENGTH);
1068 f_err:
1069 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1071 err:
1072 sk_SSL_CIPHER_free(ciphers);
1074 return (ret);
1078 ssl3_send_server_hello(SSL *s)
1080 unsigned char *bufend;
1081 unsigned char *p, *d;
1082 CBB cbb, session_id;
1083 size_t outlen;
1084 int sl;
1086 memset(&cbb, 0, sizeof(cbb));
1088 bufend = (unsigned char *)s->internal->init_buf->data + SSL3_RT_MAX_PLAIN_LENGTH;
1090 if (s->internal->state == SSL3_ST_SW_SRVR_HELLO_A) {
1091 d = p = ssl3_handshake_msg_start(s, SSL3_MT_SERVER_HELLO);
1093 if (!CBB_init_fixed(&cbb, p, bufend - p))
1094 goto err;
1096 if (!CBB_add_u16(&cbb, s->version))
1097 goto err;
1098 if (!CBB_add_bytes(&cbb, s->s3->server_random,
1099 sizeof(s->s3->server_random)))
1100 goto err;
1103 * There are several cases for the session ID to send
1104 * back in the server hello:
1106 * - For session reuse from the session cache,
1107 * we send back the old session ID.
1108 * - If stateless session reuse (using a session ticket)
1109 * is successful, we send back the client's "session ID"
1110 * (which doesn't actually identify the session).
1111 * - If it is a new session, we send back the new
1112 * session ID.
1113 * - However, if we want the new session to be single-use,
1114 * we send back a 0-length session ID.
1116 * s->internal->hit is non-zero in either case of session reuse,
1117 * so the following won't overwrite an ID that we're supposed
1118 * to send back.
1120 if (!(s->ctx->internal->session_cache_mode & SSL_SESS_CACHE_SERVER)
1121 && !s->internal->hit)
1122 s->session->session_id_length = 0;
1124 sl = s->session->session_id_length;
1125 if (sl > (int)sizeof(s->session->session_id)) {
1126 SSLerror(s, ERR_R_INTERNAL_ERROR);
1127 goto err;
1130 if (!CBB_add_u8_length_prefixed(&cbb, &session_id))
1131 goto err;
1132 if (!CBB_add_bytes(&session_id, s->session->session_id, sl))
1133 goto err;
1135 /* Cipher suite. */
1136 if (!CBB_add_u16(&cbb,
1137 ssl3_cipher_get_value(S3I(s)->tmp.new_cipher)))
1138 goto err;
1140 /* Compression method. */
1141 if (!CBB_add_u8(&cbb, 0))
1142 goto err;
1144 if (!CBB_finish(&cbb, NULL, &outlen))
1145 goto err;
1147 if ((p = ssl_add_serverhello_tlsext(s, p + outlen,
1148 bufend)) == NULL) {
1149 SSLerror(s, ERR_R_INTERNAL_ERROR);
1150 goto err;
1153 ssl3_handshake_msg_finish(s, p - d);
1156 /* SSL3_ST_SW_SRVR_HELLO_B */
1157 return (ssl3_handshake_write(s));
1159 err:
1160 CBB_cleanup(&cbb);
1162 return (-1);
1166 ssl3_send_server_done(SSL *s)
1168 CBB cbb, done;
1170 memset(&cbb, 0, sizeof(cbb));
1172 if (s->internal->state == SSL3_ST_SW_SRVR_DONE_A) {
1173 if (!ssl3_handshake_msg_start_cbb(s, &cbb, &done,
1174 SSL3_MT_SERVER_DONE))
1175 goto err;
1176 if (!ssl3_handshake_msg_finish_cbb(s, &cbb))
1177 goto err;
1179 s->internal->state = SSL3_ST_SW_SRVR_DONE_B;
1182 /* SSL3_ST_SW_SRVR_DONE_B */
1183 return (ssl3_handshake_write(s));
1185 err:
1186 CBB_cleanup(&cbb);
1188 return (-1);
1192 ssl3_send_server_kex_dhe(SSL *s, CBB *cbb)
1194 CBB dh_p, dh_g, dh_Ys;
1195 DH *dh = NULL, *dhp;
1196 unsigned char *data;
1197 int al;
1199 if (s->cert->dh_tmp_auto != 0) {
1200 if ((dhp = ssl_get_auto_dh(s)) == NULL) {
1201 al = SSL_AD_INTERNAL_ERROR;
1202 SSLerror(s, ERR_R_INTERNAL_ERROR);
1203 goto f_err;
1205 } else
1206 dhp = s->cert->dh_tmp;
1208 if (dhp == NULL && s->cert->dh_tmp_cb != NULL)
1209 dhp = s->cert->dh_tmp_cb(s, 0,
1210 SSL_C_PKEYLENGTH(S3I(s)->tmp.new_cipher));
1212 if (dhp == NULL) {
1213 al = SSL_AD_HANDSHAKE_FAILURE;
1214 SSLerror(s, SSL_R_MISSING_TMP_DH_KEY);
1215 goto f_err;
1218 if (S3I(s)->tmp.dh != NULL) {
1219 SSLerror(s, ERR_R_INTERNAL_ERROR);
1220 goto err;
1223 if (s->cert->dh_tmp_auto != 0) {
1224 dh = dhp;
1225 } else if ((dh = DHparams_dup(dhp)) == NULL) {
1226 SSLerror(s, ERR_R_DH_LIB);
1227 goto err;
1229 S3I(s)->tmp.dh = dh;
1230 if (!DH_generate_key(dh)) {
1231 SSLerror(s, ERR_R_DH_LIB);
1232 goto err;
1236 * Serialize the DH parameters and public key.
1238 if (!CBB_add_u16_length_prefixed(cbb, &dh_p))
1239 goto err;
1240 if (!CBB_add_space(&dh_p, &data, BN_num_bytes(dh->p)))
1241 goto err;
1242 BN_bn2bin(dh->p, data);
1244 if (!CBB_add_u16_length_prefixed(cbb, &dh_g))
1245 goto err;
1246 if (!CBB_add_space(&dh_g, &data, BN_num_bytes(dh->g)))
1247 goto err;
1248 BN_bn2bin(dh->g, data);
1250 if (!CBB_add_u16_length_prefixed(cbb, &dh_Ys))
1251 goto err;
1252 if (!CBB_add_space(&dh_Ys, &data, BN_num_bytes(dh->pub_key)))
1253 goto err;
1254 BN_bn2bin(dh->pub_key, data);
1256 if (!CBB_flush(cbb))
1257 goto err;
1259 return (1);
1261 f_err:
1262 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1263 err:
1264 return (-1);
1267 static int
1268 ssl3_send_server_kex_ecdhe_ecp(SSL *s, int nid, CBB *cbb)
1270 CBB ecpoint;
1271 unsigned char *data;
1272 EC_KEY *ecdh = NULL, *ecdhp;
1273 const EC_GROUP *group;
1274 unsigned char *encodedPoint = NULL;
1275 int encodedlen = 0;
1276 int curve_id = 0;
1277 BN_CTX *bn_ctx = NULL;
1278 int al;
1280 ecdhp = s->cert->ecdh_tmp;
1281 if (s->cert->ecdh_tmp_auto != 0) {
1282 if (nid != NID_undef)
1283 ecdhp = EC_KEY_new_by_curve_name(nid);
1284 } else if (ecdhp == NULL && s->cert->ecdh_tmp_cb != NULL) {
1285 ecdhp = s->cert->ecdh_tmp_cb(s, 0,
1286 SSL_C_PKEYLENGTH(S3I(s)->tmp.new_cipher));
1288 if (ecdhp == NULL) {
1289 al = SSL_AD_HANDSHAKE_FAILURE;
1290 SSLerror(s, SSL_R_MISSING_TMP_ECDH_KEY);
1291 goto f_err;
1294 if (S3I(s)->tmp.ecdh != NULL) {
1295 SSLerror(s, ERR_R_INTERNAL_ERROR);
1296 goto err;
1299 /* Duplicate the ECDH structure. */
1300 if (s->cert->ecdh_tmp_auto != 0) {
1301 ecdh = ecdhp;
1302 } else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1303 SSLerror(s, ERR_R_ECDH_LIB);
1304 goto err;
1306 S3I(s)->tmp.ecdh = ecdh;
1308 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1309 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1310 (s->internal->options & SSL_OP_SINGLE_ECDH_USE)) {
1311 if (!EC_KEY_generate_key(ecdh)) {
1312 SSLerror(s, ERR_R_ECDH_LIB);
1313 goto err;
1317 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1318 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1319 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1320 SSLerror(s, ERR_R_ECDH_LIB);
1321 goto err;
1325 * Only named curves are supported in ECDH ephemeral key exchanges.
1326 * For supported named curves, curve_id is non-zero.
1328 if ((curve_id = tls1_ec_nid2curve_id(
1329 EC_GROUP_get_curve_name(group))) == 0) {
1330 SSLerror(s, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1331 goto err;
1335 * Encode the public key. First check the size of encoding and
1336 * allocate memory accordingly.
1338 encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1339 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
1341 encodedPoint = malloc(encodedlen);
1343 bn_ctx = BN_CTX_new();
1344 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1345 SSLerror(s, ERR_R_MALLOC_FAILURE);
1346 goto err;
1349 encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
1350 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encodedlen, bn_ctx);
1352 if (encodedlen == 0) {
1353 SSLerror(s, ERR_R_ECDH_LIB);
1354 goto err;
1357 BN_CTX_free(bn_ctx);
1358 bn_ctx = NULL;
1361 * Only named curves are supported in ECDH ephemeral key exchanges.
1362 * In this case the ServerKeyExchange message has:
1363 * [1 byte CurveType], [2 byte CurveName]
1364 * [1 byte length of encoded point], followed by
1365 * the actual encoded point itself.
1367 if (!CBB_add_u8(cbb, NAMED_CURVE_TYPE))
1368 goto err;
1369 if (!CBB_add_u16(cbb, curve_id))
1370 goto err;
1371 if (!CBB_add_u8_length_prefixed(cbb, &ecpoint))
1372 goto err;
1373 if (!CBB_add_space(&ecpoint, &data, encodedlen))
1374 goto err;
1376 memcpy(data, encodedPoint, encodedlen);
1378 free(encodedPoint);
1379 encodedPoint = NULL;
1381 if (!CBB_flush(cbb))
1382 goto err;
1384 return (1);
1386 f_err:
1387 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1388 err:
1389 free(encodedPoint);
1390 BN_CTX_free(bn_ctx);
1392 return (-1);
1395 static int
1396 ssl3_send_server_kex_ecdhe_ecx(SSL *s, int nid, CBB *cbb)
1398 uint8_t *public_key = NULL;
1399 int curve_id;
1400 CBB ecpoint;
1401 int ret = -1;
1403 /* Generate an X25519 key pair. */
1404 if (S3I(s)->tmp.x25519 != NULL) {
1405 SSLerror(s, ERR_R_INTERNAL_ERROR);
1406 goto err;
1408 if ((S3I(s)->tmp.x25519 = malloc(X25519_KEY_LENGTH)) == NULL)
1409 goto err;
1410 if ((public_key = malloc(X25519_KEY_LENGTH)) == NULL)
1411 goto err;
1412 X25519_keypair(public_key, S3I(s)->tmp.x25519);
1414 /* Serialize public key. */
1415 if ((curve_id = tls1_ec_nid2curve_id(nid)) == 0) {
1416 SSLerror(s, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1417 goto err;
1420 if (!CBB_add_u8(cbb, NAMED_CURVE_TYPE))
1421 goto err;
1422 if (!CBB_add_u16(cbb, curve_id))
1423 goto err;
1424 if (!CBB_add_u8_length_prefixed(cbb, &ecpoint))
1425 goto err;
1426 if (!CBB_add_bytes(&ecpoint, public_key, X25519_KEY_LENGTH))
1427 goto err;
1428 if (!CBB_flush(cbb))
1429 goto err;
1431 ret = 1;
1433 err:
1434 free(public_key);
1436 return (ret);
1439 static int
1440 ssl3_send_server_kex_ecdhe(SSL *s, CBB *cbb)
1442 int nid;
1444 nid = tls1_get_shared_curve(s);
1446 if (s->cert->ecdh_tmp_auto != 0 && nid == NID_X25519)
1447 return ssl3_send_server_kex_ecdhe_ecx(s, nid, cbb);
1449 return ssl3_send_server_kex_ecdhe_ecp(s, nid, cbb);
1453 ssl3_send_server_key_exchange(SSL *s)
1455 CBB cbb;
1456 unsigned char *params = NULL;
1457 size_t params_len;
1458 unsigned char *q;
1459 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1460 unsigned int u;
1461 EVP_PKEY *pkey;
1462 const EVP_MD *md = NULL;
1463 unsigned char *p, *d;
1464 int al, i, j, n, kn;
1465 unsigned long type;
1466 BUF_MEM *buf;
1467 EVP_MD_CTX md_ctx;
1469 memset(&cbb, 0, sizeof(cbb));
1471 EVP_MD_CTX_init(&md_ctx);
1472 if (s->internal->state == SSL3_ST_SW_KEY_EXCH_A) {
1473 type = S3I(s)->tmp.new_cipher->algorithm_mkey;
1475 buf = s->internal->init_buf;
1477 if (!CBB_init(&cbb, 0))
1478 goto err;
1480 if (type & SSL_kDHE) {
1481 if (ssl3_send_server_kex_dhe(s, &cbb) != 1)
1482 goto err;
1483 } else if (type & SSL_kECDHE) {
1484 if (ssl3_send_server_kex_ecdhe(s, &cbb) != 1)
1485 goto err;
1486 } else {
1487 al = SSL_AD_HANDSHAKE_FAILURE;
1488 SSLerror(s, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1489 goto f_err;
1492 if (!CBB_finish(&cbb, &params, &params_len))
1493 goto err;
1495 if (!(S3I(s)->tmp.new_cipher->algorithm_auth & SSL_aNULL)) {
1496 if ((pkey = ssl_get_sign_pkey(
1497 s, S3I(s)->tmp.new_cipher, &md)) == NULL) {
1498 al = SSL_AD_DECODE_ERROR;
1499 goto f_err;
1501 kn = EVP_PKEY_size(pkey);
1502 } else {
1503 pkey = NULL;
1504 kn = 0;
1507 if (!BUF_MEM_grow_clean(buf, ssl3_handshake_msg_hdr_len(s) +
1508 params_len + kn)) {
1509 SSLerror(s, ERR_LIB_BUF);
1510 goto err;
1513 d = p = ssl3_handshake_msg_start(s,
1514 SSL3_MT_SERVER_KEY_EXCHANGE);
1516 memcpy(p, params, params_len);
1518 free(params);
1519 params = NULL;
1521 n = params_len;
1522 p += params_len;
1524 /* not anonymous */
1525 if (pkey != NULL) {
1527 * n is the length of the params, they start at &(d[4])
1528 * and p points to the space at the end.
1530 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1531 q = md_buf;
1532 j = 0;
1533 if (!EVP_DigestInit_ex(&md_ctx, EVP_md5_sha1(),
1534 NULL))
1535 goto err;
1536 EVP_DigestUpdate(&md_ctx, s->s3->client_random,
1537 SSL3_RANDOM_SIZE);
1538 EVP_DigestUpdate(&md_ctx, s->s3->server_random,
1539 SSL3_RANDOM_SIZE);
1540 EVP_DigestUpdate(&md_ctx, d, n);
1541 EVP_DigestFinal_ex(&md_ctx, q,
1542 (unsigned int *)&i);
1543 q += i;
1544 j += i;
1545 if (RSA_sign(NID_md5_sha1, md_buf, j,
1546 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1547 SSLerror(s, ERR_R_RSA_LIB);
1548 goto err;
1550 s2n(u, p);
1551 n += u + 2;
1552 } else if (md) {
1553 /* Send signature algorithm. */
1554 if (SSL_USE_SIGALGS(s)) {
1555 if (!tls12_get_sigandhash(p, pkey, md)) {
1556 /* Should never happen */
1557 al = SSL_AD_INTERNAL_ERROR;
1558 SSLerror(s, ERR_R_INTERNAL_ERROR);
1559 goto f_err;
1561 p += 2;
1563 EVP_SignInit_ex(&md_ctx, md, NULL);
1564 EVP_SignUpdate(&md_ctx,
1565 s->s3->client_random,
1566 SSL3_RANDOM_SIZE);
1567 EVP_SignUpdate(&md_ctx,
1568 s->s3->server_random,
1569 SSL3_RANDOM_SIZE);
1570 EVP_SignUpdate(&md_ctx, d, n);
1571 if (!EVP_SignFinal(&md_ctx, &p[2],
1572 (unsigned int *)&i, pkey)) {
1573 SSLerror(s, ERR_R_EVP_LIB);
1574 goto err;
1576 s2n(i, p);
1577 n += i + 2;
1578 if (SSL_USE_SIGALGS(s))
1579 n += 2;
1580 } else {
1581 /* Is this error check actually needed? */
1582 al = SSL_AD_HANDSHAKE_FAILURE;
1583 SSLerror(s, SSL_R_UNKNOWN_PKEY_TYPE);
1584 goto f_err;
1588 ssl3_handshake_msg_finish(s, n);
1591 s->internal->state = SSL3_ST_SW_KEY_EXCH_B;
1593 EVP_MD_CTX_cleanup(&md_ctx);
1595 return (ssl3_handshake_write(s));
1597 f_err:
1598 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1599 err:
1600 free(params);
1601 EVP_MD_CTX_cleanup(&md_ctx);
1602 CBB_cleanup(&cbb);
1604 return (-1);
1608 ssl3_send_certificate_request(SSL *s)
1610 unsigned char *p, *d;
1611 int i, j, nl, off, n;
1612 STACK_OF(X509_NAME) *sk = NULL;
1613 X509_NAME *name;
1614 BUF_MEM *buf;
1616 if (s->internal->state == SSL3_ST_SW_CERT_REQ_A) {
1617 buf = s->internal->init_buf;
1619 d = p = ssl3_handshake_msg_start(s,
1620 SSL3_MT_CERTIFICATE_REQUEST);
1622 /* get the list of acceptable cert types */
1623 p++;
1624 n = ssl3_get_req_cert_type(s, p);
1625 d[0] = n;
1626 p += n;
1627 n++;
1629 if (SSL_USE_SIGALGS(s)) {
1630 nl = tls12_get_req_sig_algs(s, p + 2);
1631 s2n(nl, p);
1632 p += nl + 2;
1633 n += nl + 2;
1636 off = n;
1637 p += 2;
1638 n += 2;
1640 sk = SSL_get_client_CA_list(s);
1641 nl = 0;
1642 if (sk != NULL) {
1643 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1644 name = sk_X509_NAME_value(sk, i);
1645 j = i2d_X509_NAME(name, NULL);
1646 if (!BUF_MEM_grow_clean(buf,
1647 ssl3_handshake_msg_hdr_len(s) + n + j
1648 + 2)) {
1649 SSLerror(s, ERR_R_BUF_LIB);
1650 goto err;
1652 p = ssl3_handshake_msg_start(s,
1653 SSL3_MT_CERTIFICATE_REQUEST) + n;
1654 s2n(j, p);
1655 i2d_X509_NAME(name, &p);
1656 n += 2 + j;
1657 nl += 2 + j;
1660 /* else no CA names */
1661 p = ssl3_handshake_msg_start(s,
1662 SSL3_MT_CERTIFICATE_REQUEST) + off;
1663 s2n(nl, p);
1665 ssl3_handshake_msg_finish(s, n);
1667 s->internal->state = SSL3_ST_SW_CERT_REQ_B;
1670 /* SSL3_ST_SW_CERT_REQ_B */
1671 return (ssl3_handshake_write(s));
1672 err:
1673 return (-1);
1676 static int
1677 ssl3_get_client_kex_rsa(SSL *s, unsigned char *p, long n)
1679 unsigned char fakekey[SSL_MAX_MASTER_KEY_LENGTH];
1680 unsigned char *d;
1681 RSA *rsa = NULL;
1682 EVP_PKEY *pkey = NULL;
1683 int i, al;
1685 d = p;
1687 arc4random_buf(fakekey, sizeof(fakekey));
1688 fakekey[0] = s->client_version >> 8;
1689 fakekey[1] = s->client_version & 0xff;
1691 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1692 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA) ||
1693 (pkey->pkey.rsa == NULL)) {
1694 al = SSL_AD_HANDSHAKE_FAILURE;
1695 SSLerror(s, SSL_R_MISSING_RSA_CERTIFICATE);
1696 goto f_err;
1698 rsa = pkey->pkey.rsa;
1700 if (2 > n)
1701 goto truncated;
1702 n2s(p, i);
1703 if (n != i + 2) {
1704 SSLerror(s, SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1705 goto err;
1706 } else
1707 n = i;
1709 i = RSA_private_decrypt((int)n, p, p, rsa, RSA_PKCS1_PADDING);
1711 ERR_clear_error();
1713 al = -1;
1715 if (i != SSL_MAX_MASTER_KEY_LENGTH) {
1716 al = SSL_AD_DECODE_ERROR;
1717 /* SSLerror(s, SSL_R_BAD_RSA_DECRYPT); */
1720 if (p - d + 2 > n) /* needed in the SSL3 case */
1721 goto truncated;
1722 if ((al == -1) && !((p[0] == (s->client_version >> 8)) &&
1723 (p[1] == (s->client_version & 0xff)))) {
1725 * The premaster secret must contain the same version
1726 * number as the ClientHello to detect version rollback
1727 * attacks (strangely, the protocol does not offer such
1728 * protection for DH ciphersuites).
1729 * However, buggy clients exist that send the negotiated
1730 * protocol version instead if the server does not
1731 * support the requested protocol version.
1732 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
1733 * clients.
1735 if (!((s->internal->options & SSL_OP_TLS_ROLLBACK_BUG) &&
1736 (p[0] == (s->version >> 8)) &&
1737 (p[1] == (s->version & 0xff)))) {
1738 al = SSL_AD_DECODE_ERROR;
1739 /* SSLerror(s, SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
1742 * The Klima-Pokorny-Rosa extension of
1743 * Bleichenbacher's attack
1744 * (http://eprint.iacr.org/2003/052/) exploits
1745 * the version number check as a "bad version
1746 * oracle" -- an alert would reveal that the
1747 * plaintext corresponding to some ciphertext
1748 * made up by the adversary is properly
1749 * formatted except that the version number is
1750 * wrong.
1751 * To avoid such attacks, we should treat this
1752 * just like any other decryption error.
1757 if (al != -1) {
1759 * Some decryption failure -- use random value instead
1760 * as countermeasure against Bleichenbacher's attack
1761 * on PKCS #1 v1.5 RSA padding (see RFC 2246,
1762 * section 7.4.7.1).
1764 i = SSL_MAX_MASTER_KEY_LENGTH;
1765 p = fakekey;
1768 s->session->master_key_length =
1769 tls1_generate_master_secret(s,
1770 s->session->master_key, p, i);
1772 explicit_bzero(p, i);
1774 return (1);
1775 truncated:
1776 al = SSL_AD_DECODE_ERROR;
1777 SSLerror(s, SSL_R_BAD_PACKET_LENGTH);
1778 f_err:
1779 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1780 err:
1781 return (-1);
1784 static int
1785 ssl3_get_client_kex_dhe(SSL *s, unsigned char *p, long n)
1787 BIGNUM *bn = NULL;
1788 int key_size, al;
1789 CBS cbs, dh_Yc;
1790 DH *dh;
1792 if (n < 0)
1793 goto err;
1795 CBS_init(&cbs, p, n);
1797 if (!CBS_get_u16_length_prefixed(&cbs, &dh_Yc))
1798 goto truncated;
1800 if (CBS_len(&cbs) != 0)
1801 goto truncated;
1803 if (S3I(s)->tmp.dh == NULL) {
1804 al = SSL_AD_HANDSHAKE_FAILURE;
1805 SSLerror(s, SSL_R_MISSING_TMP_DH_KEY);
1806 goto f_err;
1808 dh = S3I(s)->tmp.dh;
1810 if ((bn = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL)) == NULL) {
1811 SSLerror(s, SSL_R_BN_LIB);
1812 goto err;
1815 key_size = DH_compute_key(p, bn, dh);
1816 if (key_size <= 0) {
1817 SSLerror(s, ERR_R_DH_LIB);
1818 BN_clear_free(bn);
1819 goto err;
1822 s->session->master_key_length =
1823 tls1_generate_master_secret(
1824 s, s->session->master_key, p, key_size);
1826 explicit_bzero(p, key_size);
1828 DH_free(S3I(s)->tmp.dh);
1829 S3I(s)->tmp.dh = NULL;
1831 BN_clear_free(bn);
1833 return (1);
1835 truncated:
1836 al = SSL_AD_DECODE_ERROR;
1837 SSLerror(s, SSL_R_BAD_PACKET_LENGTH);
1838 f_err:
1839 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1840 err:
1841 return (-1);
1844 static int
1845 ssl3_get_client_kex_ecdhe_ecp(SSL *s, unsigned char *p, long n)
1847 EC_KEY *srvr_ecdh = NULL;
1848 EVP_PKEY *clnt_pub_pkey = NULL;
1849 EC_POINT *clnt_ecpoint = NULL;
1850 BN_CTX *bn_ctx = NULL;
1851 int i, al;
1853 int ret = 1;
1854 int key_size;
1855 const EC_KEY *tkey;
1856 const EC_GROUP *group;
1857 const BIGNUM *priv_key;
1859 /* Initialize structures for server's ECDH key pair. */
1860 if ((srvr_ecdh = EC_KEY_new()) == NULL) {
1861 SSLerror(s, ERR_R_MALLOC_FAILURE);
1862 goto err;
1866 * Use the ephemeral values we saved when
1867 * generating the ServerKeyExchange message.
1869 tkey = S3I(s)->tmp.ecdh;
1871 group = EC_KEY_get0_group(tkey);
1872 priv_key = EC_KEY_get0_private_key(tkey);
1874 if (!EC_KEY_set_group(srvr_ecdh, group) ||
1875 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
1876 SSLerror(s, ERR_R_EC_LIB);
1877 goto err;
1880 /* Let's get client's public key */
1881 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
1882 SSLerror(s, ERR_R_MALLOC_FAILURE);
1883 goto err;
1886 if (n == 0L) {
1887 /* Client Publickey was in Client Certificate */
1888 if (((clnt_pub_pkey = X509_get_pubkey(
1889 s->session->peer)) == NULL) ||
1890 (clnt_pub_pkey->type != EVP_PKEY_EC)) {
1892 * XXX: For now, we do not support client
1893 * authentication using ECDH certificates
1894 * so this branch (n == 0L) of the code is
1895 * never executed. When that support is
1896 * added, we ought to ensure the key
1897 * received in the certificate is
1898 * authorized for key agreement.
1899 * ECDH_compute_key implicitly checks that
1900 * the two ECDH shares are for the same
1901 * group.
1903 al = SSL_AD_HANDSHAKE_FAILURE;
1904 SSLerror(s, SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
1905 goto f_err;
1908 if (EC_POINT_copy(clnt_ecpoint,
1909 EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec))
1910 == 0) {
1911 SSLerror(s, ERR_R_EC_LIB);
1912 goto err;
1914 ret = 2; /* Skip certificate verify processing */
1915 } else {
1917 * Get client's public key from encoded point
1918 * in the ClientKeyExchange message.
1920 if ((bn_ctx = BN_CTX_new()) == NULL) {
1921 SSLerror(s, ERR_R_MALLOC_FAILURE);
1922 goto err;
1925 /* Get encoded point length */
1926 i = *p;
1928 p += 1;
1929 if (n != 1 + i) {
1930 SSLerror(s, ERR_R_EC_LIB);
1931 goto err;
1933 if (EC_POINT_oct2point(group,
1934 clnt_ecpoint, p, i, bn_ctx) == 0) {
1935 SSLerror(s, ERR_R_EC_LIB);
1936 goto err;
1939 * p is pointing to somewhere in the buffer
1940 * currently, so set it to the start.
1942 p = (unsigned char *)s->internal->init_buf->data;
1945 /* Compute the shared pre-master secret */
1946 key_size = ECDH_size(srvr_ecdh);
1947 if (key_size <= 0) {
1948 SSLerror(s, ERR_R_ECDH_LIB);
1949 goto err;
1951 i = ECDH_compute_key(p, key_size, clnt_ecpoint, srvr_ecdh,
1952 NULL);
1953 if (i <= 0) {
1954 SSLerror(s, ERR_R_ECDH_LIB);
1955 goto err;
1958 EVP_PKEY_free(clnt_pub_pkey);
1959 EC_POINT_free(clnt_ecpoint);
1960 EC_KEY_free(srvr_ecdh);
1961 BN_CTX_free(bn_ctx);
1962 EC_KEY_free(S3I(s)->tmp.ecdh);
1963 S3I(s)->tmp.ecdh = NULL;
1965 /* Compute the master secret */
1966 s->session->master_key_length =
1967 tls1_generate_master_secret(
1968 s, s->session->master_key, p, i);
1970 explicit_bzero(p, i);
1971 return (ret);
1973 f_err:
1974 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1975 err:
1976 EVP_PKEY_free(clnt_pub_pkey);
1977 EC_POINT_free(clnt_ecpoint);
1978 EC_KEY_free(srvr_ecdh);
1979 BN_CTX_free(bn_ctx);
1980 return (-1);
1983 static int
1984 ssl3_get_client_kex_ecdhe_ecx(SSL *s, unsigned char *p, long n)
1986 uint8_t *shared_key = NULL;
1987 CBS cbs, ecpoint;
1988 int ret = -1;
1990 if (n < 0)
1991 goto err;
1993 CBS_init(&cbs, p, n);
1994 if (!CBS_get_u8_length_prefixed(&cbs, &ecpoint))
1995 goto err;
1996 if (CBS_len(&ecpoint) != X25519_KEY_LENGTH)
1997 goto err;
1999 if ((shared_key = malloc(X25519_KEY_LENGTH)) == NULL)
2000 goto err;
2001 if (!X25519(shared_key, S3I(s)->tmp.x25519, CBS_data(&ecpoint)))
2002 goto err;
2004 explicit_bzero(S3I(s)->tmp.x25519, X25519_KEY_LENGTH);
2005 free(S3I(s)->tmp.x25519);
2006 S3I(s)->tmp.x25519 = NULL;
2008 s->session->master_key_length =
2009 tls1_generate_master_secret(
2010 s, s->session->master_key, shared_key, X25519_KEY_LENGTH);
2012 ret = 1;
2014 err:
2015 if (shared_key != NULL)
2016 explicit_bzero(shared_key, X25519_KEY_LENGTH);
2017 free(shared_key);
2019 return (ret);
2022 static int
2023 ssl3_get_client_kex_ecdhe(SSL *s, unsigned char *p, long n)
2025 if (S3I(s)->tmp.x25519 != NULL)
2026 return ssl3_get_client_kex_ecdhe_ecx(s, p, n);
2028 return ssl3_get_client_kex_ecdhe_ecp(s, p, n);
2031 static int
2032 ssl3_get_client_kex_gost(SSL *s, unsigned char *p, long n)
2035 EVP_PKEY_CTX *pkey_ctx;
2036 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2037 unsigned char premaster_secret[32], *start;
2038 size_t outlen = 32, inlen;
2039 unsigned long alg_a;
2040 int Ttag, Tclass;
2041 long Tlen;
2042 int al;
2043 int ret = 0;
2045 /* Get our certificate private key*/
2046 alg_a = S3I(s)->tmp.new_cipher->algorithm_auth;
2047 if (alg_a & SSL_aGOST01)
2048 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2050 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2051 EVP_PKEY_decrypt_init(pkey_ctx);
2053 * If client certificate is present and is of the same type,
2054 * maybe use it for key exchange.
2055 * Don't mind errors from EVP_PKEY_derive_set_peer, because
2056 * it is completely valid to use a client certificate for
2057 * authorization only.
2059 client_pub_pkey = X509_get_pubkey(s->session->peer);
2060 if (client_pub_pkey) {
2061 if (EVP_PKEY_derive_set_peer(pkey_ctx,
2062 client_pub_pkey) <= 0)
2063 ERR_clear_error();
2065 if (2 > n)
2066 goto truncated;
2067 /* Decrypt session key */
2068 if (ASN1_get_object((const unsigned char **)&p, &Tlen, &Ttag,
2069 &Tclass, n) != V_ASN1_CONSTRUCTED ||
2070 Ttag != V_ASN1_SEQUENCE || Tclass != V_ASN1_UNIVERSAL) {
2071 SSLerror(s, SSL_R_DECRYPTION_FAILED);
2072 goto gerr;
2074 start = p;
2075 inlen = Tlen;
2076 if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen,
2077 start, inlen) <=0) {
2078 SSLerror(s, SSL_R_DECRYPTION_FAILED);
2079 goto gerr;
2081 /* Generate master secret */
2082 s->session->master_key_length =
2083 tls1_generate_master_secret(
2084 s, s->session->master_key, premaster_secret, 32);
2085 /* Check if pubkey from client certificate was used */
2086 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1,
2087 EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2088 ret = 2;
2089 else
2090 ret = 1;
2091 gerr:
2092 EVP_PKEY_free(client_pub_pkey);
2093 EVP_PKEY_CTX_free(pkey_ctx);
2094 if (ret)
2095 return (ret);
2096 else
2097 goto err;
2099 truncated:
2100 al = SSL_AD_DECODE_ERROR;
2101 SSLerror(s, SSL_R_BAD_PACKET_LENGTH);
2102 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2103 err:
2104 return (-1);
2108 ssl3_get_client_key_exchange(SSL *s)
2110 unsigned long alg_k;
2111 unsigned char *p;
2112 int al, ok;
2113 long n;
2115 /* 2048 maxlen is a guess. How long a key does that permit? */
2116 n = s->method->internal->ssl_get_message(s, SSL3_ST_SR_KEY_EXCH_A,
2117 SSL3_ST_SR_KEY_EXCH_B, SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
2118 if (!ok)
2119 return ((int)n);
2121 p = (unsigned char *)s->internal->init_msg;
2123 alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey;
2125 if (alg_k & SSL_kRSA) {
2126 if (ssl3_get_client_kex_rsa(s, p, n) != 1)
2127 goto err;
2128 } else if (alg_k & SSL_kDHE) {
2129 if (ssl3_get_client_kex_dhe(s, p, n) != 1)
2130 goto err;
2131 } else if (alg_k & SSL_kECDHE) {
2132 if (ssl3_get_client_kex_ecdhe(s, p, n) != 1)
2133 goto err;
2134 } else if (alg_k & SSL_kGOST) {
2135 if (ssl3_get_client_kex_gost(s, p, n) != 1)
2136 goto err;
2137 } else {
2138 al = SSL_AD_HANDSHAKE_FAILURE;
2139 SSLerror(s, SSL_R_UNKNOWN_CIPHER_TYPE);
2140 goto f_err;
2143 return (1);
2145 f_err:
2146 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2147 err:
2148 return (-1);
2152 ssl3_get_cert_verify(SSL *s)
2154 EVP_PKEY *pkey = NULL;
2155 unsigned char *p;
2156 int al, ok, ret = 0;
2157 long n;
2158 int type = 0, i, j;
2159 X509 *peer;
2160 const EVP_MD *md = NULL;
2161 EVP_MD_CTX mctx;
2162 EVP_MD_CTX_init(&mctx);
2164 n = s->method->internal->ssl_get_message(s, SSL3_ST_SR_CERT_VRFY_A,
2165 SSL3_ST_SR_CERT_VRFY_B, -1, SSL3_RT_MAX_PLAIN_LENGTH, &ok);
2166 if (!ok)
2167 return ((int)n);
2169 if (s->session->peer != NULL) {
2170 peer = s->session->peer;
2171 pkey = X509_get_pubkey(peer);
2172 type = X509_certificate_type(peer, pkey);
2173 } else {
2174 peer = NULL;
2175 pkey = NULL;
2178 if (S3I(s)->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) {
2179 S3I(s)->tmp.reuse_message = 1;
2180 if (peer != NULL) {
2181 al = SSL_AD_UNEXPECTED_MESSAGE;
2182 SSLerror(s, SSL_R_MISSING_VERIFY_MESSAGE);
2183 goto f_err;
2185 ret = 1;
2186 goto end;
2189 if (peer == NULL) {
2190 SSLerror(s, SSL_R_NO_CLIENT_CERT_RECEIVED);
2191 al = SSL_AD_UNEXPECTED_MESSAGE;
2192 goto f_err;
2195 if (!(type & EVP_PKT_SIGN)) {
2196 SSLerror(s, SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2197 al = SSL_AD_ILLEGAL_PARAMETER;
2198 goto f_err;
2201 if (S3I(s)->change_cipher_spec) {
2202 SSLerror(s, SSL_R_CCS_RECEIVED_EARLY);
2203 al = SSL_AD_UNEXPECTED_MESSAGE;
2204 goto f_err;
2207 /* we now have a signature that we need to verify */
2208 p = (unsigned char *)s->internal->init_msg;
2210 * Check for broken implementations of GOST ciphersuites.
2212 * If key is GOST and n is exactly 64, it is a bare
2213 * signature without length field.
2215 if (n == 64 && (pkey->type == NID_id_GostR3410_94 ||
2216 pkey->type == NID_id_GostR3410_2001) ) {
2217 i = 64;
2218 } else {
2219 if (SSL_USE_SIGALGS(s)) {
2220 int sigalg = tls12_get_sigid(pkey);
2221 /* Should never happen */
2222 if (sigalg == -1) {
2223 SSLerror(s, ERR_R_INTERNAL_ERROR);
2224 al = SSL_AD_INTERNAL_ERROR;
2225 goto f_err;
2227 if (2 > n)
2228 goto truncated;
2229 /* Check key type is consistent with signature */
2230 if (sigalg != (int)p[1]) {
2231 SSLerror(s, SSL_R_WRONG_SIGNATURE_TYPE);
2232 al = SSL_AD_DECODE_ERROR;
2233 goto f_err;
2235 md = tls12_get_hash(p[0]);
2236 if (md == NULL) {
2237 SSLerror(s, SSL_R_UNKNOWN_DIGEST);
2238 al = SSL_AD_DECODE_ERROR;
2239 goto f_err;
2241 p += 2;
2242 n -= 2;
2244 if (2 > n)
2245 goto truncated;
2246 n2s(p, i);
2247 n -= 2;
2248 if (i > n)
2249 goto truncated;
2251 j = EVP_PKEY_size(pkey);
2252 if ((i > j) || (n > j) || (n <= 0)) {
2253 SSLerror(s, SSL_R_WRONG_SIGNATURE_SIZE);
2254 al = SSL_AD_DECODE_ERROR;
2255 goto f_err;
2258 if (SSL_USE_SIGALGS(s)) {
2259 long hdatalen = 0;
2260 void *hdata;
2261 hdatalen = BIO_get_mem_data(S3I(s)->handshake_buffer, &hdata);
2262 if (hdatalen <= 0) {
2263 SSLerror(s, ERR_R_INTERNAL_ERROR);
2264 al = SSL_AD_INTERNAL_ERROR;
2265 goto f_err;
2267 if (!EVP_VerifyInit_ex(&mctx, md, NULL) ||
2268 !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) {
2269 SSLerror(s, ERR_R_EVP_LIB);
2270 al = SSL_AD_INTERNAL_ERROR;
2271 goto f_err;
2274 if (EVP_VerifyFinal(&mctx, p, i, pkey) <= 0) {
2275 al = SSL_AD_DECRYPT_ERROR;
2276 SSLerror(s, SSL_R_BAD_SIGNATURE);
2277 goto f_err;
2279 } else
2280 if (pkey->type == EVP_PKEY_RSA) {
2281 i = RSA_verify(NID_md5_sha1, S3I(s)->tmp.cert_verify_md,
2282 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i,
2283 pkey->pkey.rsa);
2284 if (i < 0) {
2285 al = SSL_AD_DECRYPT_ERROR;
2286 SSLerror(s, SSL_R_BAD_RSA_DECRYPT);
2287 goto f_err;
2289 if (i == 0) {
2290 al = SSL_AD_DECRYPT_ERROR;
2291 SSLerror(s, SSL_R_BAD_RSA_SIGNATURE);
2292 goto f_err;
2294 } else
2295 if (pkey->type == EVP_PKEY_DSA) {
2296 j = DSA_verify(pkey->save_type,
2297 &(S3I(s)->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2298 SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa);
2299 if (j <= 0) {
2300 /* bad signature */
2301 al = SSL_AD_DECRYPT_ERROR;
2302 SSLerror(s, SSL_R_BAD_DSA_SIGNATURE);
2303 goto f_err;
2305 } else
2306 if (pkey->type == EVP_PKEY_EC) {
2307 j = ECDSA_verify(pkey->save_type,
2308 &(S3I(s)->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2309 SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec);
2310 if (j <= 0) {
2311 /* bad signature */
2312 al = SSL_AD_DECRYPT_ERROR;
2313 SSLerror(s, SSL_R_BAD_ECDSA_SIGNATURE);
2314 goto f_err;
2316 } else
2317 #ifndef OPENSSL_NO_GOST
2318 if (pkey->type == NID_id_GostR3410_94 ||
2319 pkey->type == NID_id_GostR3410_2001) {
2320 long hdatalen = 0;
2321 void *hdata;
2322 unsigned char signature[128];
2323 unsigned int siglen = sizeof(signature);
2324 int nid;
2325 EVP_PKEY_CTX *pctx;
2327 hdatalen = BIO_get_mem_data(S3I(s)->handshake_buffer, &hdata);
2328 if (hdatalen <= 0) {
2329 SSLerror(s, ERR_R_INTERNAL_ERROR);
2330 al = SSL_AD_INTERNAL_ERROR;
2331 goto f_err;
2333 if (!EVP_PKEY_get_default_digest_nid(pkey, &nid) ||
2334 !(md = EVP_get_digestbynid(nid))) {
2335 SSLerror(s, ERR_R_EVP_LIB);
2336 al = SSL_AD_INTERNAL_ERROR;
2337 goto f_err;
2339 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2340 if (!pctx) {
2341 SSLerror(s, ERR_R_EVP_LIB);
2342 al = SSL_AD_INTERNAL_ERROR;
2343 goto f_err;
2345 if (!EVP_DigestInit_ex(&mctx, md, NULL) ||
2346 !EVP_DigestUpdate(&mctx, hdata, hdatalen) ||
2347 !EVP_DigestFinal(&mctx, signature, &siglen) ||
2348 (EVP_PKEY_verify_init(pctx) <= 0) ||
2349 (EVP_PKEY_CTX_set_signature_md(pctx, md) <= 0) ||
2350 (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_VERIFY,
2351 EVP_PKEY_CTRL_GOST_SIG_FORMAT,
2352 GOST_SIG_FORMAT_RS_LE,
2353 NULL) <= 0)) {
2354 SSLerror(s, ERR_R_EVP_LIB);
2355 al = SSL_AD_INTERNAL_ERROR;
2356 EVP_PKEY_CTX_free(pctx);
2357 goto f_err;
2360 if (EVP_PKEY_verify(pctx, p, i, signature, siglen) <= 0) {
2361 al = SSL_AD_DECRYPT_ERROR;
2362 SSLerror(s, SSL_R_BAD_SIGNATURE);
2363 EVP_PKEY_CTX_free(pctx);
2364 goto f_err;
2367 EVP_PKEY_CTX_free(pctx);
2368 } else
2369 #endif
2371 SSLerror(s, ERR_R_INTERNAL_ERROR);
2372 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
2373 goto f_err;
2377 ret = 1;
2378 if (0) {
2379 truncated:
2380 al = SSL_AD_DECODE_ERROR;
2381 SSLerror(s, SSL_R_BAD_PACKET_LENGTH);
2382 f_err:
2383 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2385 end:
2386 if (S3I(s)->handshake_buffer) {
2387 BIO_free(S3I(s)->handshake_buffer);
2388 S3I(s)->handshake_buffer = NULL;
2389 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
2391 EVP_MD_CTX_cleanup(&mctx);
2392 EVP_PKEY_free(pkey);
2393 return (ret);
2397 ssl3_get_client_certificate(SSL *s)
2399 CBS cbs, client_certs;
2400 int i, ok, al, ret = -1;
2401 X509 *x = NULL;
2402 long n;
2403 const unsigned char *q;
2404 STACK_OF(X509) *sk = NULL;
2406 n = s->method->internal->ssl_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B,
2407 -1, s->internal->max_cert_list, &ok);
2409 if (!ok)
2410 return ((int)n);
2412 if (S3I(s)->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
2413 if ((s->verify_mode & SSL_VERIFY_PEER) &&
2414 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2415 SSLerror(s, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2416 al = SSL_AD_HANDSHAKE_FAILURE;
2417 goto f_err;
2420 * If tls asked for a client cert,
2421 * the client must return a 0 list.
2423 if (S3I(s)->tmp.cert_request) {
2424 SSLerror(s, SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST
2426 al = SSL_AD_UNEXPECTED_MESSAGE;
2427 goto f_err;
2429 S3I(s)->tmp.reuse_message = 1;
2430 return (1);
2433 if (S3I(s)->tmp.message_type != SSL3_MT_CERTIFICATE) {
2434 al = SSL_AD_UNEXPECTED_MESSAGE;
2435 SSLerror(s, SSL_R_WRONG_MESSAGE_TYPE);
2436 goto f_err;
2439 if (n < 0)
2440 goto truncated;
2442 CBS_init(&cbs, s->internal->init_msg, n);
2444 if ((sk = sk_X509_new_null()) == NULL) {
2445 SSLerror(s, ERR_R_MALLOC_FAILURE);
2446 goto err;
2449 if (!CBS_get_u24_length_prefixed(&cbs, &client_certs) ||
2450 CBS_len(&cbs) != 0)
2451 goto truncated;
2453 while (CBS_len(&client_certs) > 0) {
2454 CBS cert;
2456 if (!CBS_get_u24_length_prefixed(&client_certs, &cert)) {
2457 al = SSL_AD_DECODE_ERROR;
2458 SSLerror(s, SSL_R_CERT_LENGTH_MISMATCH);
2459 goto f_err;
2462 q = CBS_data(&cert);
2463 x = d2i_X509(NULL, &q, CBS_len(&cert));
2464 if (x == NULL) {
2465 SSLerror(s, ERR_R_ASN1_LIB);
2466 goto err;
2468 if (q != CBS_data(&cert) + CBS_len(&cert)) {
2469 al = SSL_AD_DECODE_ERROR;
2470 SSLerror(s, SSL_R_CERT_LENGTH_MISMATCH);
2471 goto f_err;
2473 if (!sk_X509_push(sk, x)) {
2474 SSLerror(s, ERR_R_MALLOC_FAILURE);
2475 goto err;
2477 x = NULL;
2480 if (sk_X509_num(sk) <= 0) {
2482 * TLS does not mind 0 certs returned.
2483 * Fail for TLS only if we required a certificate.
2485 if ((s->verify_mode & SSL_VERIFY_PEER) &&
2486 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2487 SSLerror(s, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2488 al = SSL_AD_HANDSHAKE_FAILURE;
2489 goto f_err;
2491 /* No client certificate so digest cached records */
2492 if (S3I(s)->handshake_buffer && !tls1_digest_cached_records(s)) {
2493 al = SSL_AD_INTERNAL_ERROR;
2494 goto f_err;
2496 } else {
2497 i = ssl_verify_cert_chain(s, sk);
2498 if (i <= 0) {
2499 al = ssl_verify_alarm_type(s->verify_result);
2500 SSLerror(s, SSL_R_NO_CERTIFICATE_RETURNED);
2501 goto f_err;
2505 X509_free(s->session->peer);
2506 s->session->peer = sk_X509_shift(sk);
2507 s->session->verify_result = s->verify_result;
2510 * With the current implementation, sess_cert will always be NULL
2511 * when we arrive here
2513 if (SSI(s)->sess_cert == NULL) {
2514 SSI(s)->sess_cert = ssl_sess_cert_new();
2515 if (SSI(s)->sess_cert == NULL) {
2516 SSLerror(s, ERR_R_MALLOC_FAILURE);
2517 goto err;
2520 sk_X509_pop_free(SSI(s)->sess_cert->cert_chain, X509_free);
2521 SSI(s)->sess_cert->cert_chain = sk;
2524 * Inconsistency alert: cert_chain does *not* include the
2525 * peer's own certificate, while we do include it in s3_clnt.c
2528 sk = NULL;
2530 ret = 1;
2531 if (0) {
2532 truncated:
2533 al = SSL_AD_DECODE_ERROR;
2534 SSLerror(s, SSL_R_BAD_PACKET_LENGTH);
2535 f_err:
2536 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2538 err:
2539 X509_free(x);
2540 sk_X509_pop_free(sk, X509_free);
2542 return (ret);
2546 ssl3_send_server_certificate(SSL *s)
2548 CBB cbb, server_cert;
2549 X509 *x;
2552 * Server Certificate - RFC 5246, section 7.4.2.
2555 memset(&cbb, 0, sizeof(cbb));
2557 if (s->internal->state == SSL3_ST_SW_CERT_A) {
2558 if ((x = ssl_get_server_send_cert(s)) == NULL) {
2559 SSLerror(s, ERR_R_INTERNAL_ERROR);
2560 return (0);
2563 if (!ssl3_handshake_msg_start_cbb(s, &cbb, &server_cert,
2564 SSL3_MT_CERTIFICATE))
2565 goto err;
2566 if (!ssl3_output_cert_chain(s, &server_cert, x))
2567 goto err;
2568 if (!ssl3_handshake_msg_finish_cbb(s, &cbb))
2569 goto err;
2571 s->internal->state = SSL3_ST_SW_CERT_B;
2574 /* SSL3_ST_SW_CERT_B */
2575 return (ssl3_handshake_write(s));
2577 err:
2578 CBB_cleanup(&cbb);
2580 return (0);
2583 /* send a new session ticket (not necessarily for a new session) */
2585 ssl3_send_newsession_ticket(SSL *s)
2587 unsigned char *d, *p, *macstart;
2588 unsigned char *senc = NULL;
2589 const unsigned char *const_p;
2590 int len, slen_full, slen;
2591 SSL_SESSION *sess;
2592 unsigned int hlen;
2593 EVP_CIPHER_CTX ctx;
2594 HMAC_CTX hctx;
2595 SSL_CTX *tctx = s->initial_ctx;
2596 unsigned char iv[EVP_MAX_IV_LENGTH];
2597 unsigned char key_name[16];
2599 if (s->internal->state == SSL3_ST_SW_SESSION_TICKET_A) {
2600 /* get session encoding length */
2601 slen_full = i2d_SSL_SESSION(s->session, NULL);
2603 * Some length values are 16 bits, so forget it if session is
2604 * too long
2606 if (slen_full > 0xFF00)
2607 goto err;
2608 senc = malloc(slen_full);
2609 if (!senc)
2610 goto err;
2611 p = senc;
2612 i2d_SSL_SESSION(s->session, &p);
2615 * Create a fresh copy (not shared with other threads) to
2616 * clean up
2618 const_p = senc;
2619 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
2620 if (sess == NULL)
2621 goto err;
2623 /* ID is irrelevant for the ticket */
2624 sess->session_id_length = 0;
2626 slen = i2d_SSL_SESSION(sess, NULL);
2627 if (slen > slen_full) {
2628 /* shouldn't ever happen */
2629 goto err;
2631 p = senc;
2632 i2d_SSL_SESSION(sess, &p);
2633 SSL_SESSION_free(sess);
2636 * Grow buffer if need be: the length calculation is as
2637 * follows 1 (size of message name) + 3 (message length
2638 * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) +
2639 * 16 (key name) + max_iv_len (iv length) +
2640 * session_length + max_enc_block_size (max encrypted session
2641 * length) + max_md_size (HMAC).
2643 if (!BUF_MEM_grow(s->internal->init_buf, ssl3_handshake_msg_hdr_len(s) +
2644 22 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH +
2645 EVP_MAX_MD_SIZE + slen))
2646 goto err;
2648 d = p = ssl3_handshake_msg_start(s, SSL3_MT_NEWSESSION_TICKET);
2650 EVP_CIPHER_CTX_init(&ctx);
2651 HMAC_CTX_init(&hctx);
2654 * Initialize HMAC and cipher contexts. If callback present
2655 * it does all the work otherwise use generated values
2656 * from parent ctx.
2658 if (tctx->internal->tlsext_ticket_key_cb) {
2659 if (tctx->internal->tlsext_ticket_key_cb(s,
2660 key_name, iv, &ctx, &hctx, 1) < 0) {
2661 EVP_CIPHER_CTX_cleanup(&ctx);
2662 goto err;
2664 } else {
2665 arc4random_buf(iv, 16);
2666 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2667 tctx->internal->tlsext_tick_aes_key, iv);
2668 HMAC_Init_ex(&hctx, tctx->internal->tlsext_tick_hmac_key,
2669 16, tlsext_tick_md(), NULL);
2670 memcpy(key_name, tctx->internal->tlsext_tick_key_name, 16);
2674 * Ticket lifetime hint (advisory only):
2675 * We leave this unspecified for resumed session
2676 * (for simplicity), and guess that tickets for new
2677 * sessions will live as long as their sessions.
2679 l2n(s->internal->hit ? 0 : s->session->timeout, p);
2681 /* Skip ticket length for now */
2682 p += 2;
2683 /* Output key name */
2684 macstart = p;
2685 memcpy(p, key_name, 16);
2686 p += 16;
2687 /* output IV */
2688 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2689 p += EVP_CIPHER_CTX_iv_length(&ctx);
2690 /* Encrypt session data */
2691 EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
2692 p += len;
2693 EVP_EncryptFinal_ex(&ctx, p, &len);
2694 p += len;
2695 EVP_CIPHER_CTX_cleanup(&ctx);
2697 HMAC_Update(&hctx, macstart, p - macstart);
2698 HMAC_Final(&hctx, p, &hlen);
2699 HMAC_CTX_cleanup(&hctx);
2700 p += hlen;
2702 /* Now write out lengths: p points to end of data written */
2703 /* Total length */
2704 len = p - d;
2706 /* Skip ticket lifetime hint. */
2707 p = d + 4;
2708 s2n(len - 6, p); /* Message length */
2710 ssl3_handshake_msg_finish(s, len);
2712 s->internal->state = SSL3_ST_SW_SESSION_TICKET_B;
2714 explicit_bzero(senc, slen_full);
2715 free(senc);
2718 /* SSL3_ST_SW_SESSION_TICKET_B */
2719 return (ssl3_handshake_write(s));
2721 err:
2722 if (senc != NULL)
2723 explicit_bzero(senc, slen_full);
2724 free(senc);
2726 return (-1);
2730 ssl3_send_cert_status(SSL *s)
2732 CBB cbb, certstatus, ocspresp;
2734 memset(&cbb, 0, sizeof(cbb));
2736 if (s->internal->state == SSL3_ST_SW_CERT_STATUS_A) {
2737 if (!ssl3_handshake_msg_start_cbb(s, &cbb, &certstatus,
2738 SSL3_MT_CERTIFICATE_STATUS))
2739 goto err;
2740 if (!CBB_add_u8(&certstatus, s->tlsext_status_type))
2741 goto err;
2742 if (!CBB_add_u24_length_prefixed(&certstatus, &ocspresp))
2743 goto err;
2744 if (!CBB_add_bytes(&ocspresp, s->internal->tlsext_ocsp_resp,
2745 s->internal->tlsext_ocsp_resplen))
2746 goto err;
2747 if (!ssl3_handshake_msg_finish_cbb(s, &cbb))
2748 goto err;
2750 s->internal->state = SSL3_ST_SW_CERT_STATUS_B;
2753 /* SSL3_ST_SW_CERT_STATUS_B */
2754 return (ssl3_handshake_write(s));
2756 err:
2757 CBB_cleanup(&cbb);
2759 return (-1);
2763 * ssl3_get_next_proto reads a Next Protocol Negotiation handshake message.
2764 * It sets the next_proto member in s if found
2767 ssl3_get_next_proto(SSL *s)
2769 CBS cbs, proto, padding;
2770 int ok;
2771 long n;
2772 size_t len;
2775 * Clients cannot send a NextProtocol message if we didn't see the
2776 * extension in their ClientHello
2778 if (!S3I(s)->next_proto_neg_seen) {
2779 SSLerror(s, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
2780 return (-1);
2783 /* 514 maxlen is enough for the payload format below */
2784 n = s->method->internal->ssl_get_message(s, SSL3_ST_SR_NEXT_PROTO_A,
2785 SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO, 514, &ok);
2786 if (!ok)
2787 return ((int)n);
2790 * s->internal->state doesn't reflect whether ChangeCipherSpec has been received
2791 * in this handshake, but S3I(s)->change_cipher_spec does (will be reset
2792 * by ssl3_get_finished).
2794 if (!S3I(s)->change_cipher_spec) {
2795 SSLerror(s, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
2796 return (-1);
2799 if (n < 2)
2800 return (0);
2801 /* The body must be > 1 bytes long */
2803 CBS_init(&cbs, s->internal->init_msg, s->internal->init_num);
2806 * The payload looks like:
2807 * uint8 proto_len;
2808 * uint8 proto[proto_len];
2809 * uint8 padding_len;
2810 * uint8 padding[padding_len];
2812 if (!CBS_get_u8_length_prefixed(&cbs, &proto) ||
2813 !CBS_get_u8_length_prefixed(&cbs, &padding) ||
2814 CBS_len(&cbs) != 0)
2815 return 0;
2818 * XXX We should not NULL it, but this matches old behavior of not
2819 * freeing before malloc.
2821 s->internal->next_proto_negotiated = NULL;
2822 s->internal->next_proto_negotiated_len = 0;
2824 if (!CBS_stow(&proto, &s->internal->next_proto_negotiated, &len)) {
2825 SSLerror(s, ERR_R_MALLOC_FAILURE);
2826 return (0);
2828 s->internal->next_proto_negotiated_len = (uint8_t)len;
2830 return (1);