preprocessor cleanup: __lint
[unleashed/tickless.git] / lib / libssl / ssl_clnt.c
blob1cdbf86c504492df3f7e49e23a2295ebda6e9084
1 /* $OpenBSD: ssl_clnt.c,v 1.11 2017/03/10 16:03:27 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 <limits.h>
152 #include <stdint.h>
153 #include <stdio.h>
155 #include "ssl_locl.h"
157 #include <openssl/bn.h>
158 #include <openssl/buffer.h>
159 #include <openssl/curve25519.h>
160 #include <openssl/dh.h>
161 #include <openssl/evp.h>
162 #include <openssl/md5.h>
163 #include <openssl/objects.h>
165 #ifndef OPENSSL_NO_ENGINE
166 #include <openssl/engine.h>
167 #endif
168 #ifndef OPENSSL_NO_GOST
169 #include <openssl/gost.h>
170 #endif
172 #include "bytestring.h"
174 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b);
177 ssl3_connect(SSL *s)
179 void (*cb)(const SSL *ssl, int type, int val) = NULL;
180 int ret = -1;
181 int new_state, state, skip = 0;
183 ERR_clear_error();
184 errno = 0;
186 if (s->internal->info_callback != NULL)
187 cb = s->internal->info_callback;
188 else if (s->ctx->internal->info_callback != NULL)
189 cb = s->ctx->internal->info_callback;
191 s->internal->in_handshake++;
192 if (!SSL_in_init(s) || SSL_in_before(s))
193 SSL_clear(s);
195 for (;;) {
196 state = s->internal->state;
198 switch (s->internal->state) {
199 case SSL_ST_RENEGOTIATE:
200 s->internal->renegotiate = 1;
201 s->internal->state = SSL_ST_CONNECT;
202 s->ctx->internal->stats.sess_connect_renegotiate++;
203 /* break */
204 case SSL_ST_BEFORE:
205 case SSL_ST_CONNECT:
206 case SSL_ST_BEFORE|SSL_ST_CONNECT:
207 case SSL_ST_OK|SSL_ST_CONNECT:
209 s->server = 0;
210 if (cb != NULL)
211 cb(s, SSL_CB_HANDSHAKE_START, 1);
213 if ((s->version & 0xff00 ) != 0x0300) {
214 SSLerror(s, ERR_R_INTERNAL_ERROR);
215 ret = -1;
216 goto end;
219 /* s->version=SSL3_VERSION; */
220 s->internal->type = SSL_ST_CONNECT;
222 if (!ssl3_setup_init_buffer(s)) {
223 ret = -1;
224 goto end;
226 if (!ssl3_setup_buffers(s)) {
227 ret = -1;
228 goto end;
230 if (!ssl_init_wbio_buffer(s, 0)) {
231 ret = -1;
232 goto end;
235 /* don't push the buffering BIO quite yet */
237 if (!tls1_init_finished_mac(s)) {
238 ret = -1;
239 goto end;
242 s->internal->state = SSL3_ST_CW_CLNT_HELLO_A;
243 s->ctx->internal->stats.sess_connect++;
244 s->internal->init_num = 0;
245 break;
247 case SSL3_ST_CW_CLNT_HELLO_A:
248 case SSL3_ST_CW_CLNT_HELLO_B:
250 s->internal->shutdown = 0;
251 ret = ssl3_client_hello(s);
252 if (ret <= 0)
253 goto end;
254 s->internal->state = SSL3_ST_CR_SRVR_HELLO_A;
255 s->internal->init_num = 0;
257 /* turn on buffering for the next lot of output */
258 if (s->bbio != s->wbio)
259 s->wbio = BIO_push(s->bbio, s->wbio);
261 break;
263 case SSL3_ST_CR_SRVR_HELLO_A:
264 case SSL3_ST_CR_SRVR_HELLO_B:
265 ret = ssl3_get_server_hello(s);
266 if (ret <= 0)
267 goto end;
269 if (s->internal->hit) {
270 s->internal->state = SSL3_ST_CR_FINISHED_A;
271 if (s->internal->tlsext_ticket_expected) {
272 /* receive renewed session ticket */
273 s->internal->state = SSL3_ST_CR_SESSION_TICKET_A;
275 } else
276 s->internal->state = SSL3_ST_CR_CERT_A;
277 s->internal->init_num = 0;
278 break;
280 case SSL3_ST_CR_CERT_A:
281 case SSL3_ST_CR_CERT_B:
282 ret = ssl3_check_finished(s);
283 if (ret <= 0)
284 goto end;
285 if (ret == 2) {
286 s->internal->hit = 1;
287 if (s->internal->tlsext_ticket_expected)
288 s->internal->state = SSL3_ST_CR_SESSION_TICKET_A;
289 else
290 s->internal->state = SSL3_ST_CR_FINISHED_A;
291 s->internal->init_num = 0;
292 break;
294 /* Check if it is anon DH/ECDH. */
295 if (!(S3I(s)->tmp.new_cipher->algorithm_auth &
296 SSL_aNULL)) {
297 ret = ssl3_get_server_certificate(s);
298 if (ret <= 0)
299 goto end;
300 if (s->internal->tlsext_status_expected)
301 s->internal->state = SSL3_ST_CR_CERT_STATUS_A;
302 else
303 s->internal->state = SSL3_ST_CR_KEY_EXCH_A;
304 } else {
305 skip = 1;
306 s->internal->state = SSL3_ST_CR_KEY_EXCH_A;
308 s->internal->init_num = 0;
309 break;
311 case SSL3_ST_CR_KEY_EXCH_A:
312 case SSL3_ST_CR_KEY_EXCH_B:
313 ret = ssl3_get_server_key_exchange(s);
314 if (ret <= 0)
315 goto end;
316 s->internal->state = SSL3_ST_CR_CERT_REQ_A;
317 s->internal->init_num = 0;
320 * At this point we check that we have the
321 * required stuff from the server.
323 if (!ssl3_check_cert_and_algorithm(s)) {
324 ret = -1;
325 goto end;
327 break;
329 case SSL3_ST_CR_CERT_REQ_A:
330 case SSL3_ST_CR_CERT_REQ_B:
331 ret = ssl3_get_certificate_request(s);
332 if (ret <= 0)
333 goto end;
334 s->internal->state = SSL3_ST_CR_SRVR_DONE_A;
335 s->internal->init_num = 0;
336 break;
338 case SSL3_ST_CR_SRVR_DONE_A:
339 case SSL3_ST_CR_SRVR_DONE_B:
340 ret = ssl3_get_server_done(s);
341 if (ret <= 0)
342 goto end;
343 if (S3I(s)->tmp.cert_req)
344 s->internal->state = SSL3_ST_CW_CERT_A;
345 else
346 s->internal->state = SSL3_ST_CW_KEY_EXCH_A;
347 s->internal->init_num = 0;
349 break;
351 case SSL3_ST_CW_CERT_A:
352 case SSL3_ST_CW_CERT_B:
353 case SSL3_ST_CW_CERT_C:
354 case SSL3_ST_CW_CERT_D:
355 ret = ssl3_send_client_certificate(s);
356 if (ret <= 0)
357 goto end;
358 s->internal->state = SSL3_ST_CW_KEY_EXCH_A;
359 s->internal->init_num = 0;
360 break;
362 case SSL3_ST_CW_KEY_EXCH_A:
363 case SSL3_ST_CW_KEY_EXCH_B:
364 ret = ssl3_send_client_key_exchange(s);
365 if (ret <= 0)
366 goto end;
368 * EAY EAY EAY need to check for DH fix cert
369 * sent back
372 * For TLS, cert_req is set to 2, so a cert chain
373 * of nothing is sent, but no verify packet is sent
376 * XXX: For now, we do not support client
377 * authentication in ECDH cipher suites with
378 * ECDH (rather than ECDSA) certificates.
379 * We need to skip the certificate verify
380 * message when client's ECDH public key is sent
381 * inside the client certificate.
383 if (S3I(s)->tmp.cert_req == 1) {
384 s->internal->state = SSL3_ST_CW_CERT_VRFY_A;
385 } else {
386 s->internal->state = SSL3_ST_CW_CHANGE_A;
387 S3I(s)->change_cipher_spec = 0;
389 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
390 s->internal->state = SSL3_ST_CW_CHANGE_A;
391 S3I(s)->change_cipher_spec = 0;
394 s->internal->init_num = 0;
395 break;
397 case SSL3_ST_CW_CERT_VRFY_A:
398 case SSL3_ST_CW_CERT_VRFY_B:
399 ret = ssl3_send_client_verify(s);
400 if (ret <= 0)
401 goto end;
402 s->internal->state = SSL3_ST_CW_CHANGE_A;
403 s->internal->init_num = 0;
404 S3I(s)->change_cipher_spec = 0;
405 break;
407 case SSL3_ST_CW_CHANGE_A:
408 case SSL3_ST_CW_CHANGE_B:
409 ret = ssl3_send_change_cipher_spec(s,
410 SSL3_ST_CW_CHANGE_A, SSL3_ST_CW_CHANGE_B);
411 if (ret <= 0)
412 goto end;
414 if (S3I(s)->next_proto_neg_seen)
415 s->internal->state = SSL3_ST_CW_NEXT_PROTO_A;
416 else
417 s->internal->state = SSL3_ST_CW_FINISHED_A;
418 s->internal->init_num = 0;
420 s->session->cipher = S3I(s)->tmp.new_cipher;
421 if (!tls1_setup_key_block(s)) {
422 ret = -1;
423 goto end;
426 if (!tls1_change_cipher_state(s,
427 SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
428 ret = -1;
429 goto end;
432 break;
434 case SSL3_ST_CW_NEXT_PROTO_A:
435 case SSL3_ST_CW_NEXT_PROTO_B:
436 ret = ssl3_send_next_proto(s);
437 if (ret <= 0)
438 goto end;
439 s->internal->state = SSL3_ST_CW_FINISHED_A;
440 break;
442 case SSL3_ST_CW_FINISHED_A:
443 case SSL3_ST_CW_FINISHED_B:
444 ret = ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A,
445 SSL3_ST_CW_FINISHED_B,
446 TLS_MD_CLIENT_FINISH_CONST,
447 TLS_MD_CLIENT_FINISH_CONST_SIZE);
448 if (ret <= 0)
449 goto end;
450 s->s3->flags |= SSL3_FLAGS_CCS_OK;
451 s->internal->state = SSL3_ST_CW_FLUSH;
453 /* clear flags */
454 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
455 if (s->internal->hit) {
456 S3I(s)->tmp.next_state = SSL_ST_OK;
457 if (s->s3->flags &
458 SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
459 s->internal->state = SSL_ST_OK;
460 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
461 S3I(s)->delay_buf_pop_ret = 0;
463 } else {
464 /* Allow NewSessionTicket if ticket expected */
465 if (s->internal->tlsext_ticket_expected)
466 S3I(s)->tmp.next_state =
467 SSL3_ST_CR_SESSION_TICKET_A;
468 else
470 S3I(s)->tmp.next_state = SSL3_ST_CR_FINISHED_A;
472 s->internal->init_num = 0;
473 break;
475 case SSL3_ST_CR_SESSION_TICKET_A:
476 case SSL3_ST_CR_SESSION_TICKET_B:
477 ret = ssl3_get_new_session_ticket(s);
478 if (ret <= 0)
479 goto end;
480 s->internal->state = SSL3_ST_CR_FINISHED_A;
481 s->internal->init_num = 0;
482 break;
484 case SSL3_ST_CR_CERT_STATUS_A:
485 case SSL3_ST_CR_CERT_STATUS_B:
486 ret = ssl3_get_cert_status(s);
487 if (ret <= 0)
488 goto end;
489 s->internal->state = SSL3_ST_CR_KEY_EXCH_A;
490 s->internal->init_num = 0;
491 break;
493 case SSL3_ST_CR_FINISHED_A:
494 case SSL3_ST_CR_FINISHED_B:
495 s->s3->flags |= SSL3_FLAGS_CCS_OK;
496 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
497 SSL3_ST_CR_FINISHED_B);
498 if (ret <= 0)
499 goto end;
501 if (s->internal->hit)
502 s->internal->state = SSL3_ST_CW_CHANGE_A;
503 else
504 s->internal->state = SSL_ST_OK;
505 s->internal->init_num = 0;
506 break;
508 case SSL3_ST_CW_FLUSH:
509 s->internal->rwstate = SSL_WRITING;
510 if (BIO_flush(s->wbio) <= 0) {
511 ret = -1;
512 goto end;
514 s->internal->rwstate = SSL_NOTHING;
515 s->internal->state = S3I(s)->tmp.next_state;
516 break;
518 case SSL_ST_OK:
519 /* clean a few things up */
520 tls1_cleanup_key_block(s);
522 BUF_MEM_free(s->internal->init_buf);
523 s->internal->init_buf = NULL;
526 * If we are not 'joining' the last two packets,
527 * remove the buffering now
529 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
530 ssl_free_wbio_buffer(s);
531 /* else do it later in ssl3_write */
533 s->internal->init_num = 0;
534 s->internal->renegotiate = 0;
535 s->internal->new_session = 0;
537 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
538 if (s->internal->hit)
539 s->ctx->internal->stats.sess_hit++;
541 ret = 1;
542 /* s->server=0; */
543 s->internal->handshake_func = ssl3_connect;
544 s->ctx->internal->stats.sess_connect_good++;
546 if (cb != NULL)
547 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
549 goto end;
550 /* break; */
552 default:
553 SSLerror(s, SSL_R_UNKNOWN_STATE);
554 ret = -1;
555 goto end;
556 /* break; */
559 /* did we do anything */
560 if (!S3I(s)->tmp.reuse_message && !skip) {
561 if (s->internal->debug) {
562 if ((ret = BIO_flush(s->wbio)) <= 0)
563 goto end;
566 if ((cb != NULL) && (s->internal->state != state)) {
567 new_state = s->internal->state;
568 s->internal->state = state;
569 cb(s, SSL_CB_CONNECT_LOOP, 1);
570 s->internal->state = new_state;
573 skip = 0;
576 end:
577 s->internal->in_handshake--;
578 if (cb != NULL)
579 cb(s, SSL_CB_CONNECT_EXIT, ret);
581 return (ret);
585 ssl3_client_hello(SSL *s)
587 unsigned char *bufend, *p, *d;
588 uint16_t max_version;
589 size_t outlen;
590 int i;
592 bufend = (unsigned char *)s->internal->init_buf->data + SSL3_RT_MAX_PLAIN_LENGTH;
594 if (s->internal->state == SSL3_ST_CW_CLNT_HELLO_A) {
595 SSL_SESSION *sess = s->session;
597 if (ssl_supported_version_range(s, NULL, &max_version) != 1) {
598 SSLerror(s, SSL_R_NO_PROTOCOLS_AVAILABLE);
599 return (-1);
601 s->client_version = s->version = max_version;
603 if ((sess == NULL) ||
604 (sess->ssl_version != s->version) ||
605 (!sess->session_id_length && !sess->tlsext_tick) ||
606 (sess->internal->not_resumable)) {
607 if (!ssl_get_new_session(s, 0))
608 goto err;
610 /* else use the pre-loaded session */
613 * If a DTLS ClientHello message is being resent after a
614 * HelloVerifyRequest, we must retain the original client
615 * random value.
617 if (!SSL_IS_DTLS(s) || D1I(s)->send_cookie == 0)
618 arc4random_buf(s->s3->client_random, SSL3_RANDOM_SIZE);
620 d = p = ssl3_handshake_msg_start(s, SSL3_MT_CLIENT_HELLO);
623 * Version indicates the negotiated version: for example from
624 * an SSLv2/v3 compatible client hello). The client_version
625 * field is the maximum version we permit and it is also
626 * used in RSA encrypted premaster secrets. Some servers can
627 * choke if we initially report a higher version then
628 * renegotiate to a lower one in the premaster secret. This
629 * didn't happen with TLS 1.0 as most servers supported it
630 * but it can with TLS 1.1 or later if the server only supports
631 * 1.0.
633 * Possible scenario with previous logic:
634 * 1. Client hello indicates TLS 1.2
635 * 2. Server hello says TLS 1.0
636 * 3. RSA encrypted premaster secret uses 1.2.
637 * 4. Handhaked proceeds using TLS 1.0.
638 * 5. Server sends hello request to renegotiate.
639 * 6. Client hello indicates TLS v1.0 as we now
640 * know that is maximum server supports.
641 * 7. Server chokes on RSA encrypted premaster secret
642 * containing version 1.0.
644 * For interoperability it should be OK to always use the
645 * maximum version we support in client hello and then rely
646 * on the checking of version to ensure the servers isn't
647 * being inconsistent: for example initially negotiating with
648 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
649 * client_version in client hello and not resetting it to
650 * the negotiated version.
653 *(p++) = s->client_version >> 8;
654 *(p++) = s->client_version & 0xff;
656 /* Random stuff */
657 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
658 p += SSL3_RANDOM_SIZE;
660 /* Session ID */
661 if (s->internal->new_session)
662 i = 0;
663 else
664 i = s->session->session_id_length;
665 *(p++) = i;
666 if (i != 0) {
667 if (i > (int)sizeof(s->session->session_id)) {
668 SSLerror(s, ERR_R_INTERNAL_ERROR);
669 goto err;
671 memcpy(p, s->session->session_id, i);
672 p += i;
675 /* DTLS Cookie. */
676 if (SSL_IS_DTLS(s)) {
677 if (D1I(s)->cookie_len > sizeof(D1I(s)->cookie)) {
678 SSLerror(s, ERR_R_INTERNAL_ERROR);
679 goto err;
681 *(p++) = D1I(s)->cookie_len;
682 memcpy(p, D1I(s)->cookie, D1I(s)->cookie_len);
683 p += D1I(s)->cookie_len;
686 /* Ciphers supported */
687 if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2],
688 bufend - &p[2], &outlen))
689 goto err;
690 if (outlen == 0) {
691 SSLerror(s, SSL_R_NO_CIPHERS_AVAILABLE);
692 goto err;
694 s2n(outlen, p);
695 p += outlen;
697 /* add in (no) COMPRESSION */
698 *(p++) = 1;
699 *(p++) = 0; /* Add the NULL method */
701 /* TLS extensions*/
702 if ((p = ssl_add_clienthello_tlsext(s, p, bufend)) == NULL) {
703 SSLerror(s, ERR_R_INTERNAL_ERROR);
704 goto err;
707 ssl3_handshake_msg_finish(s, p - d);
709 s->internal->state = SSL3_ST_CW_CLNT_HELLO_B;
712 /* SSL3_ST_CW_CLNT_HELLO_B */
713 return (ssl3_handshake_write(s));
715 err:
716 return (-1);
720 ssl3_get_server_hello(SSL *s)
722 CBS cbs, server_random, session_id;
723 uint16_t server_version, cipher_suite;
724 uint16_t min_version, max_version;
725 uint8_t compression_method;
726 STACK_OF(SSL_CIPHER) *sk;
727 const SSL_CIPHER *cipher;
728 const SSL_METHOD *method;
729 unsigned char *p;
730 unsigned long alg_k;
731 size_t outlen;
732 int i, al, ok;
733 long n;
735 s->internal->first_packet = 1;
736 n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
737 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, /* ?? */ &ok);
738 if (!ok)
739 return ((int)n);
740 s->internal->first_packet = 0;
742 if (n < 0)
743 goto truncated;
745 CBS_init(&cbs, s->internal->init_msg, n);
747 if (SSL_IS_DTLS(s)) {
748 if (S3I(s)->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
749 if (D1I(s)->send_cookie == 0) {
750 S3I(s)->tmp.reuse_message = 1;
751 return (1);
752 } else {
753 /* Already sent a cookie. */
754 al = SSL_AD_UNEXPECTED_MESSAGE;
755 SSLerror(s, SSL_R_BAD_MESSAGE_TYPE);
756 goto f_err;
761 if (S3I(s)->tmp.message_type != SSL3_MT_SERVER_HELLO) {
762 al = SSL_AD_UNEXPECTED_MESSAGE;
763 SSLerror(s, SSL_R_BAD_MESSAGE_TYPE);
764 goto f_err;
767 if (!CBS_get_u16(&cbs, &server_version))
768 goto truncated;
770 if (ssl_supported_version_range(s, &min_version, &max_version) != 1) {
771 SSLerror(s, SSL_R_NO_PROTOCOLS_AVAILABLE);
772 goto err;
775 if (server_version < min_version || server_version > max_version) {
776 SSLerror(s, SSL_R_WRONG_SSL_VERSION);
777 s->version = (s->version & 0xff00) | (server_version & 0xff);
778 al = SSL_AD_PROTOCOL_VERSION;
779 goto f_err;
781 s->version = server_version;
783 if ((method = tls1_get_client_method(server_version)) == NULL)
784 method = dtls1_get_client_method(server_version);
785 if (method == NULL) {
786 SSLerror(s, ERR_R_INTERNAL_ERROR);
787 goto err;
789 s->method = method;
791 /* Server random. */
792 if (!CBS_get_bytes(&cbs, &server_random, SSL3_RANDOM_SIZE))
793 goto truncated;
794 if (!CBS_write_bytes(&server_random, s->s3->server_random,
795 sizeof(s->s3->server_random), NULL))
796 goto err;
798 /* Session ID. */
799 if (!CBS_get_u8_length_prefixed(&cbs, &session_id))
800 goto truncated;
802 if ((CBS_len(&session_id) > sizeof(s->session->session_id)) ||
803 (CBS_len(&session_id) > SSL3_SESSION_ID_SIZE)) {
804 al = SSL_AD_ILLEGAL_PARAMETER;
805 SSLerror(s, SSL_R_SSL3_SESSION_ID_TOO_LONG);
806 goto f_err;
809 /* Cipher suite. */
810 if (!CBS_get_u16(&cbs, &cipher_suite))
811 goto truncated;
814 * Check if we want to resume the session based on external
815 * pre-shared secret.
817 if (s->internal->tls_session_secret_cb) {
818 SSL_CIPHER *pref_cipher = NULL;
819 s->session->master_key_length = sizeof(s->session->master_key);
820 if (s->internal->tls_session_secret_cb(s, s->session->master_key,
821 &s->session->master_key_length, NULL, &pref_cipher,
822 s->internal->tls_session_secret_cb_arg)) {
823 s->session->cipher = pref_cipher ? pref_cipher :
824 ssl3_get_cipher_by_value(cipher_suite);
825 s->s3->flags |= SSL3_FLAGS_CCS_OK;
829 if (s->session->session_id_length != 0 &&
830 CBS_mem_equal(&session_id, s->session->session_id,
831 s->session->session_id_length)) {
832 if (s->sid_ctx_length != s->session->sid_ctx_length ||
833 timingsafe_memcmp(s->session->sid_ctx,
834 s->sid_ctx, s->sid_ctx_length) != 0) {
835 /* actually a client application bug */
836 al = SSL_AD_ILLEGAL_PARAMETER;
837 SSLerror(s, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
838 goto f_err;
840 s->s3->flags |= SSL3_FLAGS_CCS_OK;
841 s->internal->hit = 1;
842 } else {
843 /* a miss or crap from the other end */
845 /* If we were trying for session-id reuse, make a new
846 * SSL_SESSION so we don't stuff up other people */
847 s->internal->hit = 0;
848 if (s->session->session_id_length > 0) {
849 if (!ssl_get_new_session(s, 0)) {
850 al = SSL_AD_INTERNAL_ERROR;
851 goto f_err;
856 * XXX - improve the handling for the case where there is a
857 * zero length session identifier.
859 if (!CBS_write_bytes(&session_id, s->session->session_id,
860 sizeof(s->session->session_id), &outlen))
861 goto err;
862 s->session->session_id_length = outlen;
864 s->session->ssl_version = s->version;
867 if ((cipher = ssl3_get_cipher_by_value(cipher_suite)) == NULL) {
868 al = SSL_AD_ILLEGAL_PARAMETER;
869 SSLerror(s, SSL_R_UNKNOWN_CIPHER_RETURNED);
870 goto f_err;
873 /* TLS v1.2 only ciphersuites require v1.2 or later. */
874 if ((cipher->algorithm_ssl & SSL_TLSV1_2) &&
875 (TLS1_get_version(s) < TLS1_2_VERSION)) {
876 al = SSL_AD_ILLEGAL_PARAMETER;
877 SSLerror(s, SSL_R_WRONG_CIPHER_RETURNED);
878 goto f_err;
881 sk = ssl_get_ciphers_by_id(s);
882 i = sk_SSL_CIPHER_find(sk, cipher);
883 if (i < 0) {
884 /* we did not say we would use this cipher */
885 al = SSL_AD_ILLEGAL_PARAMETER;
886 SSLerror(s, SSL_R_WRONG_CIPHER_RETURNED);
887 goto f_err;
891 * Depending on the session caching (internal/external), the cipher
892 * and/or cipher_id values may not be set. Make sure that
893 * cipher_id is set and use it for comparison.
895 if (s->session->cipher)
896 s->session->cipher_id = s->session->cipher->id;
897 if (s->internal->hit && (s->session->cipher_id != cipher->id)) {
898 al = SSL_AD_ILLEGAL_PARAMETER;
899 SSLerror(s, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
900 goto f_err;
902 S3I(s)->tmp.new_cipher = cipher;
904 if (!tls1_handshake_hash_init(s))
905 goto err;
908 * Don't digest cached records if no sigalgs: we may need them for
909 * client authentication.
911 alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey;
912 if (!(SSL_USE_SIGALGS(s) || (alg_k & SSL_kGOST)) &&
913 !tls1_digest_cached_records(s)) {
914 al = SSL_AD_INTERNAL_ERROR;
915 goto f_err;
918 if (!CBS_get_u8(&cbs, &compression_method))
919 goto truncated;
921 if (compression_method != 0) {
922 al = SSL_AD_ILLEGAL_PARAMETER;
923 SSLerror(s, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
924 goto f_err;
927 /* TLS extensions. */
928 p = (unsigned char *)CBS_data(&cbs);
929 if (!ssl_parse_serverhello_tlsext(s, &p, CBS_len(&cbs), &al)) {
930 /* 'al' set by ssl_parse_serverhello_tlsext */
931 SSLerror(s, SSL_R_PARSE_TLSEXT);
932 goto f_err;
934 if (ssl_check_serverhello_tlsext(s) <= 0) {
935 SSLerror(s, SSL_R_SERVERHELLO_TLSEXT);
936 goto err;
939 /* See if any data remains... */
940 if (p - CBS_data(&cbs) != CBS_len(&cbs))
941 goto truncated;
943 return (1);
945 truncated:
946 /* wrong packet length */
947 al = SSL_AD_DECODE_ERROR;
948 SSLerror(s, SSL_R_BAD_PACKET_LENGTH);
949 f_err:
950 ssl3_send_alert(s, SSL3_AL_FATAL, al);
951 err:
952 return (-1);
956 ssl3_get_server_certificate(SSL *s)
958 int al, i, ok, ret = -1;
959 long n;
960 CBS cbs, cert_list;
961 X509 *x = NULL;
962 const unsigned char *q;
963 STACK_OF(X509) *sk = NULL;
964 SESS_CERT *sc;
965 EVP_PKEY *pkey = NULL;
967 n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_CERT_A,
968 SSL3_ST_CR_CERT_B, -1, s->internal->max_cert_list, &ok);
970 if (!ok)
971 return ((int)n);
973 if (S3I(s)->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
974 S3I(s)->tmp.reuse_message = 1;
975 return (1);
978 if (S3I(s)->tmp.message_type != SSL3_MT_CERTIFICATE) {
979 al = SSL_AD_UNEXPECTED_MESSAGE;
980 SSLerror(s, SSL_R_BAD_MESSAGE_TYPE);
981 goto f_err;
985 if ((sk = sk_X509_new_null()) == NULL) {
986 SSLerror(s, ERR_R_MALLOC_FAILURE);
987 goto err;
990 if (n < 0)
991 goto truncated;
993 CBS_init(&cbs, s->internal->init_msg, n);
994 if (CBS_len(&cbs) < 3)
995 goto truncated;
997 if (!CBS_get_u24_length_prefixed(&cbs, &cert_list) ||
998 CBS_len(&cbs) != 0) {
999 al = SSL_AD_DECODE_ERROR;
1000 SSLerror(s, SSL_R_LENGTH_MISMATCH);
1001 goto f_err;
1004 while (CBS_len(&cert_list) > 0) {
1005 CBS cert;
1007 if (CBS_len(&cert_list) < 3)
1008 goto truncated;
1009 if (!CBS_get_u24_length_prefixed(&cert_list, &cert)) {
1010 al = SSL_AD_DECODE_ERROR;
1011 SSLerror(s, SSL_R_CERT_LENGTH_MISMATCH);
1012 goto f_err;
1015 q = CBS_data(&cert);
1016 x = d2i_X509(NULL, &q, CBS_len(&cert));
1017 if (x == NULL) {
1018 al = SSL_AD_BAD_CERTIFICATE;
1019 SSLerror(s, ERR_R_ASN1_LIB);
1020 goto f_err;
1022 if (q != CBS_data(&cert) + CBS_len(&cert)) {
1023 al = SSL_AD_DECODE_ERROR;
1024 SSLerror(s, SSL_R_CERT_LENGTH_MISMATCH);
1025 goto f_err;
1027 if (!sk_X509_push(sk, x)) {
1028 SSLerror(s, ERR_R_MALLOC_FAILURE);
1029 goto err;
1031 x = NULL;
1034 i = ssl_verify_cert_chain(s, sk);
1035 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)) {
1036 al = ssl_verify_alarm_type(s->verify_result);
1037 SSLerror(s, SSL_R_CERTIFICATE_VERIFY_FAILED);
1038 goto f_err;
1041 ERR_clear_error(); /* but we keep s->verify_result */
1043 sc = ssl_sess_cert_new();
1044 if (sc == NULL)
1045 goto err;
1046 ssl_sess_cert_free(SSI(s)->sess_cert);
1047 SSI(s)->sess_cert = sc;
1049 sc->cert_chain = sk;
1051 * Inconsistency alert: cert_chain does include the peer's
1052 * certificate, which we don't include in s3_srvr.c
1054 x = sk_X509_value(sk, 0);
1055 sk = NULL;
1056 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1058 pkey = X509_get_pubkey(x);
1060 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1061 x = NULL;
1062 al = SSL3_AL_FATAL;
1063 SSLerror(s, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1064 goto f_err;
1067 i = ssl_cert_type(x, pkey);
1068 if (i < 0) {
1069 x = NULL;
1070 al = SSL3_AL_FATAL;
1071 SSLerror(s, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1072 goto f_err;
1075 sc->peer_cert_type = i;
1076 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1078 * Why would the following ever happen?
1079 * We just created sc a couple of lines ago.
1081 X509_free(sc->peer_pkeys[i].x509);
1082 sc->peer_pkeys[i].x509 = x;
1083 sc->peer_key = &(sc->peer_pkeys[i]);
1085 X509_free(s->session->peer);
1086 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1087 s->session->peer = x;
1088 s->session->verify_result = s->verify_result;
1090 x = NULL;
1091 ret = 1;
1093 if (0) {
1094 truncated:
1095 /* wrong packet length */
1096 al = SSL_AD_DECODE_ERROR;
1097 SSLerror(s, SSL_R_BAD_PACKET_LENGTH);
1098 f_err:
1099 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1101 err:
1102 EVP_PKEY_free(pkey);
1103 X509_free(x);
1104 sk_X509_pop_free(sk, X509_free);
1106 return (ret);
1109 static int
1110 ssl3_get_server_kex_dhe(SSL *s, EVP_PKEY **pkey, unsigned char **pp, long *nn)
1112 CBS cbs, dhp, dhg, dhpk;
1113 BN_CTX *bn_ctx = NULL;
1114 SESS_CERT *sc = NULL;
1115 DH *dh = NULL;
1116 long alg_a;
1117 int al;
1119 alg_a = S3I(s)->tmp.new_cipher->algorithm_auth;
1120 sc = SSI(s)->sess_cert;
1122 if (*nn < 0)
1123 goto err;
1125 CBS_init(&cbs, *pp, *nn);
1127 if ((dh = DH_new()) == NULL) {
1128 SSLerror(s, ERR_R_DH_LIB);
1129 goto err;
1132 if (!CBS_get_u16_length_prefixed(&cbs, &dhp))
1133 goto truncated;
1134 if ((dh->p = BN_bin2bn(CBS_data(&dhp), CBS_len(&dhp), NULL)) == NULL) {
1135 SSLerror(s, ERR_R_BN_LIB);
1136 goto err;
1139 if (!CBS_get_u16_length_prefixed(&cbs, &dhg))
1140 goto truncated;
1141 if ((dh->g = BN_bin2bn(CBS_data(&dhg), CBS_len(&dhg), NULL)) == NULL) {
1142 SSLerror(s, ERR_R_BN_LIB);
1143 goto err;
1146 if (!CBS_get_u16_length_prefixed(&cbs, &dhpk))
1147 goto truncated;
1148 if ((dh->pub_key = BN_bin2bn(CBS_data(&dhpk), CBS_len(&dhpk),
1149 NULL)) == NULL) {
1150 SSLerror(s, ERR_R_BN_LIB);
1151 goto err;
1155 * Check the strength of the DH key just constructed.
1156 * Discard keys weaker than 1024 bits.
1158 if (DH_size(dh) < 1024 / 8) {
1159 SSLerror(s, SSL_R_BAD_DH_P_LENGTH);
1160 goto err;
1163 if (alg_a & SSL_aRSA)
1164 *pkey = X509_get_pubkey(sc->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1165 else if (alg_a & SSL_aDSS)
1166 *pkey = X509_get_pubkey(sc->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1167 else
1168 /* XXX - Anonymous DH, so no certificate or pkey. */
1169 *pkey = NULL;
1171 sc->peer_dh_tmp = dh;
1173 *nn = CBS_len(&cbs);
1174 *pp = (unsigned char *)CBS_data(&cbs);
1176 return (1);
1178 truncated:
1179 al = SSL_AD_DECODE_ERROR;
1180 SSLerror(s, SSL_R_BAD_PACKET_LENGTH);
1181 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1183 err:
1184 DH_free(dh);
1185 BN_CTX_free(bn_ctx);
1187 return (-1);
1190 static int
1191 ssl3_get_server_kex_ecdhe_ecp(SSL *s, SESS_CERT *sc, int nid, CBS *public)
1193 const EC_GROUP *group;
1194 EC_GROUP *ngroup = NULL;
1195 EC_POINT *point = NULL;
1196 BN_CTX *bn_ctx = NULL;
1197 EC_KEY *ecdh = NULL;
1198 int ret = -1;
1201 * Extract the server's ephemeral ECDH public key.
1204 if ((ecdh = EC_KEY_new()) == NULL) {
1205 SSLerror(s, ERR_R_MALLOC_FAILURE);
1206 goto err;
1209 if ((ngroup = EC_GROUP_new_by_curve_name(nid)) == NULL) {
1210 SSLerror(s, ERR_R_EC_LIB);
1211 goto err;
1213 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1214 SSLerror(s, ERR_R_EC_LIB);
1215 goto err;
1218 group = EC_KEY_get0_group(ecdh);
1220 if ((point = EC_POINT_new(group)) == NULL ||
1221 (bn_ctx = BN_CTX_new()) == NULL) {
1222 SSLerror(s, ERR_R_MALLOC_FAILURE);
1223 goto err;
1226 if (EC_POINT_oct2point(group, point, CBS_data(public),
1227 CBS_len(public), bn_ctx) == 0) {
1228 SSLerror(s, SSL_R_BAD_ECPOINT);
1229 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1230 goto err;
1233 EC_KEY_set_public_key(ecdh, point);
1234 sc->peer_ecdh_tmp = ecdh;
1235 ecdh = NULL;
1237 ret = 1;
1239 err:
1240 BN_CTX_free(bn_ctx);
1241 EC_GROUP_free(ngroup);
1242 EC_POINT_free(point);
1243 EC_KEY_free(ecdh);
1245 return (ret);
1248 static int
1249 ssl3_get_server_kex_ecdhe_ecx(SSL *s, SESS_CERT *sc, int nid, CBS *public)
1251 size_t outlen;
1253 if (nid != NID_X25519) {
1254 SSLerror(s, ERR_R_INTERNAL_ERROR);
1255 goto err;
1258 if (CBS_len(public) != X25519_KEY_LENGTH) {
1259 SSLerror(s, SSL_R_BAD_ECPOINT);
1260 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1261 goto err;
1264 if (!CBS_stow(public, &sc->peer_x25519_tmp, &outlen)) {
1265 SSLerror(s, ERR_R_MALLOC_FAILURE);
1266 goto err;
1269 return (1);
1271 err:
1272 return (-1);
1275 static int
1276 ssl3_get_server_kex_ecdhe(SSL *s, EVP_PKEY **pkey, unsigned char **pp, long *nn)
1278 CBS cbs, public;
1279 uint8_t curve_type;
1280 uint16_t curve_id;
1281 SESS_CERT *sc;
1282 long alg_a;
1283 int nid;
1284 int al;
1286 alg_a = S3I(s)->tmp.new_cipher->algorithm_auth;
1287 sc = SSI(s)->sess_cert;
1289 if (*nn < 0)
1290 goto err;
1292 CBS_init(&cbs, *pp, *nn);
1294 /* Only named curves are supported. */
1295 if (!CBS_get_u8(&cbs, &curve_type) ||
1296 curve_type != NAMED_CURVE_TYPE ||
1297 !CBS_get_u16(&cbs, &curve_id)) {
1298 al = SSL_AD_DECODE_ERROR;
1299 SSLerror(s, SSL_R_LENGTH_TOO_SHORT);
1300 goto f_err;
1304 * Check that the curve is one of our preferences - if it is not,
1305 * the server has sent us an invalid curve.
1307 if (tls1_check_curve(s, curve_id) != 1) {
1308 al = SSL_AD_DECODE_ERROR;
1309 SSLerror(s, SSL_R_WRONG_CURVE);
1310 goto f_err;
1313 if ((nid = tls1_ec_curve_id2nid(curve_id)) == 0) {
1314 al = SSL_AD_INTERNAL_ERROR;
1315 SSLerror(s, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1316 goto f_err;
1319 if (!CBS_get_u8_length_prefixed(&cbs, &public))
1320 goto truncated;
1322 if (nid == NID_X25519) {
1323 if (ssl3_get_server_kex_ecdhe_ecx(s, sc, nid, &public) != 1)
1324 goto err;
1325 } else {
1326 if (ssl3_get_server_kex_ecdhe_ecp(s, sc, nid, &public) != 1)
1327 goto err;
1331 * The ECC/TLS specification does not mention the use of DSA to sign
1332 * ECParameters in the server key exchange message. We do support RSA
1333 * and ECDSA.
1335 if (alg_a & SSL_aRSA)
1336 *pkey = X509_get_pubkey(sc->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1337 else if (alg_a & SSL_aECDSA)
1338 *pkey = X509_get_pubkey(sc->peer_pkeys[SSL_PKEY_ECC].x509);
1339 else
1340 /* XXX - Anonymous ECDH, so no certificate or pkey. */
1341 *pkey = NULL;
1343 *nn = CBS_len(&cbs);
1344 *pp = (unsigned char *)CBS_data(&cbs);
1346 return (1);
1348 truncated:
1349 al = SSL_AD_DECODE_ERROR;
1350 SSLerror(s, SSL_R_BAD_PACKET_LENGTH);
1352 f_err:
1353 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1355 err:
1356 return (-1);
1360 ssl3_get_server_key_exchange(SSL *s)
1362 unsigned char *q, md_buf[EVP_MAX_MD_SIZE*2];
1363 EVP_MD_CTX md_ctx;
1364 unsigned char *param, *p;
1365 int al, i, j, param_len, ok;
1366 long n, alg_k, alg_a;
1367 EVP_PKEY *pkey = NULL;
1368 const EVP_MD *md = NULL;
1369 RSA *rsa = NULL;
1371 alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey;
1372 alg_a = S3I(s)->tmp.new_cipher->algorithm_auth;
1375 * Use same message size as in ssl3_get_certificate_request()
1376 * as ServerKeyExchange message may be skipped.
1378 n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1379 SSL3_ST_CR_KEY_EXCH_B, -1, s->internal->max_cert_list, &ok);
1380 if (!ok)
1381 return ((int)n);
1383 EVP_MD_CTX_init(&md_ctx);
1385 if (S3I(s)->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1387 * Do not skip server key exchange if this cipher suite uses
1388 * ephemeral keys.
1390 if (alg_k & (SSL_kDHE|SSL_kECDHE)) {
1391 SSLerror(s, SSL_R_UNEXPECTED_MESSAGE);
1392 al = SSL_AD_UNEXPECTED_MESSAGE;
1393 goto f_err;
1396 S3I(s)->tmp.reuse_message = 1;
1397 EVP_MD_CTX_cleanup(&md_ctx);
1398 return (1);
1401 if (SSI(s)->sess_cert != NULL) {
1402 DH_free(SSI(s)->sess_cert->peer_dh_tmp);
1403 SSI(s)->sess_cert->peer_dh_tmp = NULL;
1405 EC_KEY_free(SSI(s)->sess_cert->peer_ecdh_tmp);
1406 SSI(s)->sess_cert->peer_ecdh_tmp = NULL;
1408 free(SSI(s)->sess_cert->peer_x25519_tmp);
1409 SSI(s)->sess_cert->peer_x25519_tmp = NULL;
1410 } else {
1411 SSI(s)->sess_cert = ssl_sess_cert_new();
1412 if (SSI(s)->sess_cert == NULL)
1413 goto err;
1416 param = p = (unsigned char *)s->internal->init_msg;
1417 param_len = n;
1419 if (alg_k & SSL_kDHE) {
1420 if (ssl3_get_server_kex_dhe(s, &pkey, &p, &n) != 1)
1421 goto err;
1422 } else if (alg_k & SSL_kECDHE) {
1423 if (ssl3_get_server_kex_ecdhe(s, &pkey, &p, &n) != 1)
1424 goto err;
1425 } else if (alg_k != 0) {
1426 al = SSL_AD_UNEXPECTED_MESSAGE;
1427 SSLerror(s, SSL_R_UNEXPECTED_MESSAGE);
1428 goto f_err;
1431 param_len = param_len - n;
1433 /* if it was signed, check the signature */
1434 if (pkey != NULL) {
1435 if (SSL_USE_SIGALGS(s)) {
1436 int sigalg = tls12_get_sigid(pkey);
1437 /* Should never happen */
1438 if (sigalg == -1) {
1439 SSLerror(s, ERR_R_INTERNAL_ERROR);
1440 goto err;
1443 * Check key type is consistent
1444 * with signature
1446 if (2 > n)
1447 goto truncated;
1448 if (sigalg != (int)p[1]) {
1449 SSLerror(s, SSL_R_WRONG_SIGNATURE_TYPE);
1450 al = SSL_AD_DECODE_ERROR;
1451 goto f_err;
1453 md = tls12_get_hash(p[0]);
1454 if (md == NULL) {
1455 SSLerror(s, SSL_R_UNKNOWN_DIGEST);
1456 al = SSL_AD_DECODE_ERROR;
1457 goto f_err;
1459 p += 2;
1460 n -= 2;
1461 } else
1462 md = EVP_sha1();
1464 if (2 > n)
1465 goto truncated;
1466 n2s(p, i);
1467 n -= 2;
1468 j = EVP_PKEY_size(pkey);
1470 if (i != n || n > j) {
1471 /* wrong packet length */
1472 al = SSL_AD_DECODE_ERROR;
1473 SSLerror(s, SSL_R_WRONG_SIGNATURE_LENGTH);
1474 goto f_err;
1477 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1478 j = 0;
1479 q = md_buf;
1480 if (!EVP_DigestInit_ex(&md_ctx, EVP_md5_sha1(), NULL)) {
1481 al = SSL_AD_INTERNAL_ERROR;
1482 goto f_err;
1484 EVP_DigestUpdate(&md_ctx, s->s3->client_random,
1485 SSL3_RANDOM_SIZE);
1486 EVP_DigestUpdate(&md_ctx, s->s3->server_random,
1487 SSL3_RANDOM_SIZE);
1488 EVP_DigestUpdate(&md_ctx, param, param_len);
1489 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
1490 q += i;
1491 j += i;
1492 i = RSA_verify(NID_md5_sha1, md_buf, j,
1493 p, n, pkey->pkey.rsa);
1494 if (i < 0) {
1495 al = SSL_AD_DECRYPT_ERROR;
1496 SSLerror(s, SSL_R_BAD_RSA_DECRYPT);
1497 goto f_err;
1499 if (i == 0) {
1500 /* bad signature */
1501 al = SSL_AD_DECRYPT_ERROR;
1502 SSLerror(s, SSL_R_BAD_SIGNATURE);
1503 goto f_err;
1505 } else {
1506 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1507 EVP_VerifyUpdate(&md_ctx, s->s3->client_random,
1508 SSL3_RANDOM_SIZE);
1509 EVP_VerifyUpdate(&md_ctx, s->s3->server_random,
1510 SSL3_RANDOM_SIZE);
1511 EVP_VerifyUpdate(&md_ctx, param, param_len);
1512 if (EVP_VerifyFinal(&md_ctx, p,(int)n, pkey) <= 0) {
1513 /* bad signature */
1514 al = SSL_AD_DECRYPT_ERROR;
1515 SSLerror(s, SSL_R_BAD_SIGNATURE);
1516 goto f_err;
1519 } else {
1520 /* aNULL does not need public keys. */
1521 if (!(alg_a & SSL_aNULL)) {
1522 SSLerror(s, ERR_R_INTERNAL_ERROR);
1523 goto err;
1525 /* still data left over */
1526 if (n != 0) {
1527 al = SSL_AD_DECODE_ERROR;
1528 SSLerror(s, SSL_R_EXTRA_DATA_IN_MESSAGE);
1529 goto f_err;
1533 EVP_PKEY_free(pkey);
1534 EVP_MD_CTX_cleanup(&md_ctx);
1536 return (1);
1538 truncated:
1539 /* wrong packet length */
1540 al = SSL_AD_DECODE_ERROR;
1541 SSLerror(s, SSL_R_BAD_PACKET_LENGTH);
1543 f_err:
1544 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1546 err:
1547 EVP_PKEY_free(pkey);
1548 RSA_free(rsa);
1549 EVP_MD_CTX_cleanup(&md_ctx);
1551 return (-1);
1555 ssl3_get_certificate_request(SSL *s)
1557 int ok, ret = 0;
1558 long n;
1559 uint8_t ctype_num;
1560 CBS cert_request, ctypes, rdn_list;
1561 X509_NAME *xn = NULL;
1562 const unsigned char *q;
1563 STACK_OF(X509_NAME) *ca_sk = NULL;
1565 n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1566 SSL3_ST_CR_CERT_REQ_B, -1, s->internal->max_cert_list, &ok);
1568 if (!ok)
1569 return ((int)n);
1571 S3I(s)->tmp.cert_req = 0;
1573 if (S3I(s)->tmp.message_type == SSL3_MT_SERVER_DONE) {
1574 S3I(s)->tmp.reuse_message = 1;
1576 * If we get here we don't need any cached handshake records
1577 * as we wont be doing client auth.
1579 if (S3I(s)->handshake_buffer) {
1580 if (!tls1_digest_cached_records(s))
1581 goto err;
1583 return (1);
1586 if (S3I(s)->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1587 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1588 SSLerror(s, SSL_R_WRONG_MESSAGE_TYPE);
1589 goto err;
1592 /* TLS does not like anon-DH with client cert */
1593 if (S3I(s)->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
1594 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1595 SSLerror(s, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1596 goto err;
1599 if (n < 0)
1600 goto truncated;
1601 CBS_init(&cert_request, s->internal->init_msg, n);
1603 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1604 SSLerror(s, ERR_R_MALLOC_FAILURE);
1605 goto err;
1608 /* get the certificate types */
1609 if (!CBS_get_u8(&cert_request, &ctype_num))
1610 goto truncated;
1612 if (ctype_num > SSL3_CT_NUMBER)
1613 ctype_num = SSL3_CT_NUMBER;
1614 if (!CBS_get_bytes(&cert_request, &ctypes, ctype_num) ||
1615 !CBS_write_bytes(&ctypes, (uint8_t *)S3I(s)->tmp.ctype,
1616 sizeof(S3I(s)->tmp.ctype), NULL)) {
1617 SSLerror(s, SSL_R_DATA_LENGTH_TOO_LONG);
1618 goto err;
1621 if (SSL_USE_SIGALGS(s)) {
1622 CBS sigalgs;
1624 if (CBS_len(&cert_request) < 2) {
1625 SSLerror(s, SSL_R_DATA_LENGTH_TOO_LONG);
1626 goto err;
1629 /* Check we have enough room for signature algorithms and
1630 * following length value.
1632 if (!CBS_get_u16_length_prefixed(&cert_request, &sigalgs)) {
1633 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1634 SSLerror(s, SSL_R_DATA_LENGTH_TOO_LONG);
1635 goto err;
1637 if ((CBS_len(&sigalgs) & 1) ||
1638 !tls1_process_sigalgs(s, CBS_data(&sigalgs),
1639 CBS_len(&sigalgs))) {
1640 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1641 SSLerror(s, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1642 goto err;
1646 /* get the CA RDNs */
1647 if (CBS_len(&cert_request) < 2) {
1648 SSLerror(s, SSL_R_DATA_LENGTH_TOO_LONG);
1649 goto err;
1652 if (!CBS_get_u16_length_prefixed(&cert_request, &rdn_list) ||
1653 CBS_len(&cert_request) != 0) {
1654 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1655 SSLerror(s, SSL_R_LENGTH_MISMATCH);
1656 goto err;
1659 while (CBS_len(&rdn_list) > 0) {
1660 CBS rdn;
1662 if (CBS_len(&rdn_list) < 2) {
1663 SSLerror(s, SSL_R_DATA_LENGTH_TOO_LONG);
1664 goto err;
1667 if (!CBS_get_u16_length_prefixed(&rdn_list, &rdn)) {
1668 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1669 SSLerror(s, SSL_R_CA_DN_TOO_LONG);
1670 goto err;
1673 q = CBS_data(&rdn);
1674 if ((xn = d2i_X509_NAME(NULL, &q, CBS_len(&rdn))) == NULL) {
1675 ssl3_send_alert(s, SSL3_AL_FATAL,
1676 SSL_AD_DECODE_ERROR);
1677 SSLerror(s, ERR_R_ASN1_LIB);
1678 goto err;
1681 if (q != CBS_data(&rdn) + CBS_len(&rdn)) {
1682 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1683 SSLerror(s, SSL_R_CA_DN_LENGTH_MISMATCH);
1684 goto err;
1686 if (!sk_X509_NAME_push(ca_sk, xn)) {
1687 SSLerror(s, ERR_R_MALLOC_FAILURE);
1688 goto err;
1690 xn = NULL; /* avoid free in err block */
1693 /* we should setup a certificate to return.... */
1694 S3I(s)->tmp.cert_req = 1;
1695 S3I(s)->tmp.ctype_num = ctype_num;
1696 sk_X509_NAME_pop_free(S3I(s)->tmp.ca_names, X509_NAME_free);
1697 S3I(s)->tmp.ca_names = ca_sk;
1698 ca_sk = NULL;
1700 ret = 1;
1701 if (0) {
1702 truncated:
1703 SSLerror(s, SSL_R_BAD_PACKET_LENGTH);
1705 err:
1706 X509_NAME_free(xn);
1707 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1708 return (ret);
1711 static int
1712 ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
1714 return (X509_NAME_cmp(*a, *b));
1718 ssl3_get_new_session_ticket(SSL *s)
1720 int ok, al, ret = 0;
1721 uint32_t lifetime_hint;
1722 long n;
1723 CBS cbs, session_ticket;
1725 n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_SESSION_TICKET_A,
1726 SSL3_ST_CR_SESSION_TICKET_B, -1, 16384, &ok);
1727 if (!ok)
1728 return ((int)n);
1730 if (S3I(s)->tmp.message_type == SSL3_MT_FINISHED) {
1731 S3I(s)->tmp.reuse_message = 1;
1732 return (1);
1734 if (S3I(s)->tmp.message_type != SSL3_MT_NEWSESSION_TICKET) {
1735 al = SSL_AD_UNEXPECTED_MESSAGE;
1736 SSLerror(s, SSL_R_BAD_MESSAGE_TYPE);
1737 goto f_err;
1740 if (n < 0) {
1741 al = SSL_AD_DECODE_ERROR;
1742 SSLerror(s, SSL_R_LENGTH_MISMATCH);
1743 goto f_err;
1746 CBS_init(&cbs, s->internal->init_msg, n);
1747 if (!CBS_get_u32(&cbs, &lifetime_hint) ||
1748 #if UINT32_MAX > LONG_MAX
1749 lifetime_hint > LONG_MAX ||
1750 #endif
1751 !CBS_get_u16_length_prefixed(&cbs, &session_ticket) ||
1752 CBS_len(&cbs) != 0) {
1753 al = SSL_AD_DECODE_ERROR;
1754 SSLerror(s, SSL_R_LENGTH_MISMATCH);
1755 goto f_err;
1757 s->session->tlsext_tick_lifetime_hint = (long)lifetime_hint;
1759 if (!CBS_stow(&session_ticket, &s->session->tlsext_tick,
1760 &s->session->tlsext_ticklen)) {
1761 SSLerror(s, ERR_R_MALLOC_FAILURE);
1762 goto err;
1766 * There are two ways to detect a resumed ticket sesion.
1767 * One is to set an appropriate session ID and then the server
1768 * must return a match in ServerHello. This allows the normal
1769 * client session ID matching to work and we know much
1770 * earlier that the ticket has been accepted.
1772 * The other way is to set zero length session ID when the
1773 * ticket is presented and rely on the handshake to determine
1774 * session resumption.
1776 * We choose the former approach because this fits in with
1777 * assumptions elsewhere in OpenSSL. The session ID is set
1778 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1779 * ticket.
1781 EVP_Digest(CBS_data(&session_ticket), CBS_len(&session_ticket),
1782 s->session->session_id, &s->session->session_id_length,
1783 EVP_sha256(), NULL);
1784 ret = 1;
1785 return (ret);
1786 f_err:
1787 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1788 err:
1789 return (-1);
1793 ssl3_get_cert_status(SSL *s)
1795 CBS cert_status, response;
1796 size_t stow_len;
1797 int ok, al;
1798 long n;
1799 uint8_t status_type;
1801 n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_CERT_STATUS_A,
1802 SSL3_ST_CR_CERT_STATUS_B, SSL3_MT_CERTIFICATE_STATUS,
1803 16384, &ok);
1805 if (!ok)
1806 return ((int)n);
1808 if (n < 0) {
1809 /* need at least status type + length */
1810 al = SSL_AD_DECODE_ERROR;
1811 SSLerror(s, SSL_R_LENGTH_MISMATCH);
1812 goto f_err;
1815 CBS_init(&cert_status, s->internal->init_msg, n);
1816 if (!CBS_get_u8(&cert_status, &status_type) ||
1817 CBS_len(&cert_status) < 3) {
1818 /* need at least status type + length */
1819 al = SSL_AD_DECODE_ERROR;
1820 SSLerror(s, SSL_R_LENGTH_MISMATCH);
1821 goto f_err;
1824 if (status_type != TLSEXT_STATUSTYPE_ocsp) {
1825 al = SSL_AD_DECODE_ERROR;
1826 SSLerror(s, SSL_R_UNSUPPORTED_STATUS_TYPE);
1827 goto f_err;
1830 if (!CBS_get_u24_length_prefixed(&cert_status, &response) ||
1831 CBS_len(&cert_status) != 0) {
1832 al = SSL_AD_DECODE_ERROR;
1833 SSLerror(s, SSL_R_LENGTH_MISMATCH);
1834 goto f_err;
1837 if (!CBS_stow(&response, &s->internal->tlsext_ocsp_resp,
1838 &stow_len) || stow_len > INT_MAX) {
1839 s->internal->tlsext_ocsp_resplen = 0;
1840 al = SSL_AD_INTERNAL_ERROR;
1841 SSLerror(s, ERR_R_MALLOC_FAILURE);
1842 goto f_err;
1844 s->internal->tlsext_ocsp_resplen = (int)stow_len;
1846 if (s->ctx->internal->tlsext_status_cb) {
1847 int ret;
1848 ret = s->ctx->internal->tlsext_status_cb(s,
1849 s->ctx->internal->tlsext_status_arg);
1850 if (ret == 0) {
1851 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1852 SSLerror(s, SSL_R_INVALID_STATUS_RESPONSE);
1853 goto f_err;
1855 if (ret < 0) {
1856 al = SSL_AD_INTERNAL_ERROR;
1857 SSLerror(s, ERR_R_MALLOC_FAILURE);
1858 goto f_err;
1861 return (1);
1862 f_err:
1863 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1864 return (-1);
1868 ssl3_get_server_done(SSL *s)
1870 int ok, ret = 0;
1871 long n;
1873 n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1874 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1875 30, /* should be very small, like 0 :-) */ &ok);
1877 if (!ok)
1878 return ((int)n);
1879 if (n > 0) {
1880 /* should contain no data */
1881 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1882 SSLerror(s, SSL_R_LENGTH_MISMATCH);
1883 return (-1);
1885 ret = 1;
1886 return (ret);
1889 static int
1890 ssl3_send_client_kex_rsa(SSL *s, SESS_CERT *sess_cert, CBB *cbb)
1892 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH];
1893 unsigned char *enc_pms = NULL;
1894 EVP_PKEY *pkey = NULL;
1895 int ret = -1;
1896 int enc_len;
1897 CBB epms;
1900 * RSA-Encrypted Premaster Secret Message - RFC 5246 section 7.4.7.1.
1903 pkey = X509_get_pubkey(sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1904 if (pkey == NULL || pkey->type != EVP_PKEY_RSA ||
1905 pkey->pkey.rsa == NULL) {
1906 SSLerror(s, ERR_R_INTERNAL_ERROR);
1907 goto err;
1910 pms[0] = s->client_version >> 8;
1911 pms[1] = s->client_version & 0xff;
1912 arc4random_buf(&pms[2], sizeof(pms) - 2);
1914 if ((enc_pms = malloc(RSA_size(pkey->pkey.rsa))) == NULL) {
1915 SSLerror(s, ERR_R_MALLOC_FAILURE);
1916 goto err;
1919 enc_len = RSA_public_encrypt(sizeof(pms), pms, enc_pms, pkey->pkey.rsa,
1920 RSA_PKCS1_PADDING);
1921 if (enc_len <= 0) {
1922 SSLerror(s, SSL_R_BAD_RSA_ENCRYPT);
1923 goto err;
1926 if (!CBB_add_u16_length_prefixed(cbb, &epms))
1927 goto err;
1928 if (!CBB_add_bytes(&epms, enc_pms, enc_len))
1929 goto err;
1930 if (!CBB_flush(cbb))
1931 goto err;
1933 s->session->master_key_length =
1934 tls1_generate_master_secret(s,
1935 s->session->master_key, pms, sizeof(pms));
1937 ret = 1;
1939 err:
1940 explicit_bzero(pms, sizeof(pms));
1941 EVP_PKEY_free(pkey);
1942 free(enc_pms);
1944 return (ret);
1947 static int
1948 ssl3_send_client_kex_dhe(SSL *s, SESS_CERT *sess_cert, CBB *cbb)
1950 DH *dh_srvr = NULL, *dh_clnt = NULL;
1951 unsigned char *key = NULL;
1952 int key_size = 0, key_len;
1953 unsigned char *data;
1954 int ret = -1;
1955 CBB dh_Yc;
1957 /* Ensure that we have an ephemeral key for DHE. */
1958 if (sess_cert->peer_dh_tmp == NULL) {
1959 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1960 SSLerror(s, SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1961 goto err;
1963 dh_srvr = sess_cert->peer_dh_tmp;
1965 /* Generate a new random key. */
1966 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
1967 SSLerror(s, ERR_R_DH_LIB);
1968 goto err;
1970 if (!DH_generate_key(dh_clnt)) {
1971 SSLerror(s, ERR_R_DH_LIB);
1972 goto err;
1974 key_size = DH_size(dh_clnt);
1975 if ((key = malloc(key_size)) == NULL) {
1976 SSLerror(s, ERR_R_MALLOC_FAILURE);
1977 goto err;
1979 key_len = DH_compute_key(key, dh_srvr->pub_key, dh_clnt);
1980 if (key_len <= 0) {
1981 SSLerror(s, ERR_R_DH_LIB);
1982 goto err;
1985 /* Generate master key from the result. */
1986 s->session->master_key_length =
1987 tls1_generate_master_secret(s,
1988 s->session->master_key, key, key_len);
1990 if (!CBB_add_u16_length_prefixed(cbb, &dh_Yc))
1991 goto err;
1992 if (!CBB_add_space(&dh_Yc, &data, BN_num_bytes(dh_clnt->pub_key)))
1993 goto err;
1994 BN_bn2bin(dh_clnt->pub_key, data);
1995 if (!CBB_flush(cbb))
1996 goto err;
1998 ret = 1;
2000 err:
2001 DH_free(dh_clnt);
2002 if (key != NULL)
2003 explicit_bzero(key, key_size);
2004 free(key);
2006 return (ret);
2009 static int
2010 ssl3_send_client_kex_ecdhe_ecp(SSL *s, SESS_CERT *sc, CBB *cbb)
2012 const EC_GROUP *group = NULL;
2013 const EC_POINT *point = NULL;
2014 EC_KEY *ecdh = NULL;
2015 BN_CTX *bn_ctx = NULL;
2016 unsigned char *key = NULL;
2017 unsigned char *data;
2018 size_t encoded_len;
2019 int key_size = 0, key_len;
2020 int ret = -1;
2021 CBB ecpoint;
2023 if ((group = EC_KEY_get0_group(sc->peer_ecdh_tmp)) == NULL ||
2024 (point = EC_KEY_get0_public_key(sc->peer_ecdh_tmp)) == NULL) {
2025 SSLerror(s, ERR_R_INTERNAL_ERROR);
2026 goto err;
2029 if ((ecdh = EC_KEY_new()) == NULL) {
2030 SSLerror(s, ERR_R_MALLOC_FAILURE);
2031 goto err;
2034 if (!EC_KEY_set_group(ecdh, group)) {
2035 SSLerror(s, ERR_R_EC_LIB);
2036 goto err;
2039 /* Generate a new ECDH key pair. */
2040 if (!(EC_KEY_generate_key(ecdh))) {
2041 SSLerror(s, ERR_R_ECDH_LIB);
2042 goto err;
2044 if ((key_size = ECDH_size(ecdh)) <= 0) {
2045 SSLerror(s, ERR_R_ECDH_LIB);
2046 goto err;
2048 if ((key = malloc(key_size)) == NULL) {
2049 SSLerror(s, ERR_R_MALLOC_FAILURE);
2051 key_len = ECDH_compute_key(key, key_size, point, ecdh, NULL);
2052 if (key_len <= 0) {
2053 SSLerror(s, ERR_R_ECDH_LIB);
2054 goto err;
2057 /* Generate master key from the result. */
2058 s->session->master_key_length =
2059 tls1_generate_master_secret(s,
2060 s->session->master_key, key, key_len);
2062 encoded_len = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
2063 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
2064 if (encoded_len == 0) {
2065 SSLerror(s, ERR_R_ECDH_LIB);
2066 goto err;
2069 if ((bn_ctx = BN_CTX_new()) == NULL) {
2070 SSLerror(s, ERR_R_MALLOC_FAILURE);
2071 goto err;
2074 /* Encode the public key. */
2075 if (!CBB_add_u8_length_prefixed(cbb, &ecpoint))
2076 goto err;
2077 if (!CBB_add_space(&ecpoint, &data, encoded_len))
2078 goto err;
2079 if (EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh),
2080 POINT_CONVERSION_UNCOMPRESSED, data, encoded_len,
2081 bn_ctx) == 0)
2082 goto err;
2083 if (!CBB_flush(cbb))
2084 goto err;
2086 ret = 1;
2088 err:
2089 if (key != NULL)
2090 explicit_bzero(key, key_size);
2091 free(key);
2093 BN_CTX_free(bn_ctx);
2094 EC_KEY_free(ecdh);
2096 return (ret);
2099 static int
2100 ssl3_send_client_kex_ecdhe_ecx(SSL *s, SESS_CERT *sc, CBB *cbb)
2102 uint8_t *public_key = NULL, *private_key = NULL, *shared_key = NULL;
2103 int ret = -1;
2104 CBB ecpoint;
2106 /* Generate X25519 key pair and derive shared key. */
2107 if ((public_key = malloc(X25519_KEY_LENGTH)) == NULL)
2108 goto err;
2109 if ((private_key = malloc(X25519_KEY_LENGTH)) == NULL)
2110 goto err;
2111 if ((shared_key = malloc(X25519_KEY_LENGTH)) == NULL)
2112 goto err;
2113 X25519_keypair(public_key, private_key);
2114 if (!X25519(shared_key, private_key, sc->peer_x25519_tmp))
2115 goto err;
2117 /* Serialize the public key. */
2118 if (!CBB_add_u8_length_prefixed(cbb, &ecpoint))
2119 goto err;
2120 if (!CBB_add_bytes(&ecpoint, public_key, X25519_KEY_LENGTH))
2121 goto err;
2122 if (!CBB_flush(cbb))
2123 goto err;
2125 /* Generate master key from the result. */
2126 s->session->master_key_length =
2127 tls1_generate_master_secret(s,
2128 s->session->master_key, shared_key, X25519_KEY_LENGTH);
2130 ret = 1;
2132 err:
2133 if (private_key != NULL)
2134 explicit_bzero(private_key, X25519_KEY_LENGTH);
2135 if (shared_key != NULL)
2136 explicit_bzero(shared_key, X25519_KEY_LENGTH);
2138 free(public_key);
2139 free(private_key);
2140 free(shared_key);
2142 return (ret);
2145 static int
2146 ssl3_send_client_kex_ecdhe(SSL *s, SESS_CERT *sc, CBB *cbb)
2148 if (sc->peer_x25519_tmp != NULL) {
2149 if (ssl3_send_client_kex_ecdhe_ecx(s, sc, cbb) != 1)
2150 goto err;
2151 } else if (sc->peer_ecdh_tmp != NULL) {
2152 if (ssl3_send_client_kex_ecdhe_ecp(s, sc, cbb) != 1)
2153 goto err;
2154 } else {
2155 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2156 SSLerror(s, ERR_R_INTERNAL_ERROR);
2157 goto err;
2160 return (1);
2162 err:
2163 return (-1);
2166 static int
2167 ssl3_send_client_kex_gost(SSL *s, SESS_CERT *sess_cert, CBB *cbb)
2169 unsigned char premaster_secret[32], shared_ukm[32], tmp[256];
2170 EVP_PKEY *pub_key = NULL;
2171 EVP_PKEY_CTX *pkey_ctx;
2172 X509 *peer_cert;
2173 size_t msglen;
2174 unsigned int md_len;
2175 EVP_MD_CTX *ukm_hash;
2176 int ret = -1;
2177 int nid;
2178 CBB gostblob;
2180 /* Get server sertificate PKEY and create ctx from it */
2181 peer_cert = sess_cert->peer_pkeys[SSL_PKEY_GOST01].x509;
2182 if (peer_cert == NULL) {
2183 SSLerror(s, SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2184 goto err;
2187 pub_key = X509_get_pubkey(peer_cert);
2188 pkey_ctx = EVP_PKEY_CTX_new(pub_key, NULL);
2191 * If we have send a certificate, and certificate key parameters match
2192 * those of server certificate, use certificate key for key exchange.
2193 * Otherwise, generate ephemeral key pair.
2195 EVP_PKEY_encrypt_init(pkey_ctx);
2197 /* Generate session key. */
2198 arc4random_buf(premaster_secret, 32);
2201 * If we have client certificate, use its secret as peer key.
2203 if (S3I(s)->tmp.cert_req && s->cert->key->privatekey) {
2204 if (EVP_PKEY_derive_set_peer(pkey_ctx,
2205 s->cert->key->privatekey) <=0) {
2207 * If there was an error - just ignore it.
2208 * Ephemeral key would be used.
2210 ERR_clear_error();
2215 * Compute shared IV and store it in algorithm-specific context data.
2217 ukm_hash = EVP_MD_CTX_create();
2218 if (ukm_hash == NULL) {
2219 SSLerror(s, ERR_R_MALLOC_FAILURE);
2220 goto err;
2223 if (ssl_get_algorithm2(s) & SSL_HANDSHAKE_MAC_GOST94)
2224 nid = NID_id_GostR3411_94;
2225 else
2226 nid = NID_id_tc26_gost3411_2012_256;
2227 if (!EVP_DigestInit(ukm_hash, EVP_get_digestbynid(nid)))
2228 goto err;
2229 EVP_DigestUpdate(ukm_hash, s->s3->client_random, SSL3_RANDOM_SIZE);
2230 EVP_DigestUpdate(ukm_hash, s->s3->server_random, SSL3_RANDOM_SIZE);
2231 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2232 EVP_MD_CTX_destroy(ukm_hash);
2233 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
2234 EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {
2235 SSLerror(s, SSL_R_LIBRARY_BUG);
2236 goto err;
2240 * Make GOST keytransport blob message, encapsulate it into sequence.
2242 msglen = 255;
2243 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret,
2244 32) < 0) {
2245 SSLerror(s, SSL_R_LIBRARY_BUG);
2246 goto err;
2249 if (!CBB_add_asn1(cbb, &gostblob, CBS_ASN1_SEQUENCE))
2250 goto err;
2251 if (!CBB_add_bytes(&gostblob, tmp, msglen))
2252 goto err;
2253 if (!CBB_flush(cbb))
2254 goto err;
2256 /* Check if pubkey from client certificate was used. */
2257 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
2258 NULL) > 0) {
2259 /* Set flag "skip certificate verify". */
2260 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2262 EVP_PKEY_CTX_free(pkey_ctx);
2263 s->session->master_key_length =
2264 tls1_generate_master_secret(s,
2265 s->session->master_key, premaster_secret, 32);
2267 ret = 1;
2269 err:
2270 explicit_bzero(premaster_secret, sizeof(premaster_secret));
2271 EVP_PKEY_free(pub_key);
2273 return (ret);
2277 ssl3_send_client_key_exchange(SSL *s)
2279 SESS_CERT *sess_cert;
2280 unsigned long alg_k;
2281 CBB cbb, kex;
2283 memset(&cbb, 0, sizeof(cbb));
2285 if (s->internal->state == SSL3_ST_CW_KEY_EXCH_A) {
2286 alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey;
2288 if ((sess_cert = SSI(s)->sess_cert) == NULL) {
2289 ssl3_send_alert(s, SSL3_AL_FATAL,
2290 SSL_AD_UNEXPECTED_MESSAGE);
2291 SSLerror(s, ERR_R_INTERNAL_ERROR);
2292 goto err;
2295 if (!ssl3_handshake_msg_start_cbb(s, &cbb, &kex,
2296 SSL3_MT_CLIENT_KEY_EXCHANGE))
2297 goto err;
2299 if (alg_k & SSL_kRSA) {
2300 if (ssl3_send_client_kex_rsa(s, sess_cert, &kex) != 1)
2301 goto err;
2302 } else if (alg_k & SSL_kDHE) {
2303 if (ssl3_send_client_kex_dhe(s, sess_cert, &kex) != 1)
2304 goto err;
2305 } else if (alg_k & SSL_kECDHE) {
2306 if (ssl3_send_client_kex_ecdhe(s, sess_cert, &kex) != 1)
2307 goto err;
2308 } else if (alg_k & SSL_kGOST) {
2309 if (ssl3_send_client_kex_gost(s, sess_cert, &kex) != 1)
2310 goto err;
2311 } else {
2312 ssl3_send_alert(s, SSL3_AL_FATAL,
2313 SSL_AD_HANDSHAKE_FAILURE);
2314 SSLerror(s, ERR_R_INTERNAL_ERROR);
2315 goto err;
2318 if (!ssl3_handshake_msg_finish_cbb(s, &cbb))
2319 goto err;
2321 s->internal->state = SSL3_ST_CW_KEY_EXCH_B;
2324 /* SSL3_ST_CW_KEY_EXCH_B */
2325 return (ssl3_handshake_write(s));
2327 err:
2328 CBB_cleanup(&cbb);
2330 return (-1);
2334 ssl3_send_client_verify(SSL *s)
2336 unsigned char *p;
2337 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
2338 EVP_PKEY *pkey;
2339 EVP_PKEY_CTX *pctx = NULL;
2340 EVP_MD_CTX mctx;
2341 unsigned u = 0;
2342 unsigned long n;
2343 int j;
2345 EVP_MD_CTX_init(&mctx);
2347 if (s->internal->state == SSL3_ST_CW_CERT_VRFY_A) {
2348 p = ssl3_handshake_msg_start(s, SSL3_MT_CERTIFICATE_VERIFY);
2351 * Create context from key and test if sha1 is allowed as
2352 * digest.
2354 pkey = s->cert->key->privatekey;
2355 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2356 EVP_PKEY_sign_init(pctx);
2358 /* XXX - is this needed? */
2359 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) <= 0)
2360 ERR_clear_error();
2362 if (!SSL_USE_SIGALGS(s)) {
2363 if (S3I(s)->handshake_buffer) {
2364 if (!tls1_digest_cached_records(s))
2365 goto err;
2367 if (!tls1_handshake_hash_value(s, data, sizeof(data),
2368 NULL))
2369 goto err;
2373 * For TLS v1.2 send signature algorithm and signature
2374 * using agreed digest and cached handshake records.
2376 if (SSL_USE_SIGALGS(s)) {
2377 long hdatalen = 0;
2378 void *hdata;
2379 const EVP_MD *md = s->cert->key->digest;
2380 hdatalen = BIO_get_mem_data(S3I(s)->handshake_buffer,
2381 &hdata);
2382 if (hdatalen <= 0 ||
2383 !tls12_get_sigandhash(p, pkey, md)) {
2384 SSLerror(s, ERR_R_INTERNAL_ERROR);
2385 goto err;
2387 p += 2;
2388 if (!EVP_SignInit_ex(&mctx, md, NULL) ||
2389 !EVP_SignUpdate(&mctx, hdata, hdatalen) ||
2390 !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
2391 SSLerror(s, ERR_R_EVP_LIB);
2392 goto err;
2394 s2n(u, p);
2395 n = u + 4;
2396 if (!tls1_digest_cached_records(s))
2397 goto err;
2398 } else if (pkey->type == EVP_PKEY_RSA) {
2399 if (RSA_sign(NID_md5_sha1, data,
2400 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, &(p[2]),
2401 &u, pkey->pkey.rsa) <= 0 ) {
2402 SSLerror(s, ERR_R_RSA_LIB);
2403 goto err;
2405 s2n(u, p);
2406 n = u + 2;
2407 } else if (pkey->type == EVP_PKEY_DSA) {
2408 if (!DSA_sign(pkey->save_type,
2409 &(data[MD5_DIGEST_LENGTH]),
2410 SHA_DIGEST_LENGTH, &(p[2]),
2411 (unsigned int *)&j, pkey->pkey.dsa)) {
2412 SSLerror(s, ERR_R_DSA_LIB);
2413 goto err;
2415 s2n(j, p);
2416 n = j + 2;
2417 } else if (pkey->type == EVP_PKEY_EC) {
2418 if (!ECDSA_sign(pkey->save_type,
2419 &(data[MD5_DIGEST_LENGTH]),
2420 SHA_DIGEST_LENGTH, &(p[2]),
2421 (unsigned int *)&j, pkey->pkey.ec)) {
2422 SSLerror(s, ERR_R_ECDSA_LIB);
2423 goto err;
2425 s2n(j, p);
2426 n = j + 2;
2427 #ifndef OPENSSL_NO_GOST
2428 } else if (pkey->type == NID_id_GostR3410_94 ||
2429 pkey->type == NID_id_GostR3410_2001) {
2430 unsigned char signbuf[128];
2431 long hdatalen = 0;
2432 void *hdata;
2433 const EVP_MD *md;
2434 int nid;
2435 size_t sigsize;
2437 hdatalen = BIO_get_mem_data(S3I(s)->handshake_buffer, &hdata);
2438 if (hdatalen <= 0) {
2439 SSLerror(s, ERR_R_INTERNAL_ERROR);
2440 goto err;
2442 if (!EVP_PKEY_get_default_digest_nid(pkey, &nid) ||
2443 !(md = EVP_get_digestbynid(nid))) {
2444 SSLerror(s, ERR_R_EVP_LIB);
2445 goto err;
2447 if (!EVP_DigestInit_ex(&mctx, md, NULL) ||
2448 !EVP_DigestUpdate(&mctx, hdata, hdatalen) ||
2449 !EVP_DigestFinal(&mctx, signbuf, &u) ||
2450 (EVP_PKEY_CTX_set_signature_md(pctx, md) <= 0) ||
2451 (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
2452 EVP_PKEY_CTRL_GOST_SIG_FORMAT,
2453 GOST_SIG_FORMAT_RS_LE,
2454 NULL) <= 0) ||
2455 (EVP_PKEY_sign(pctx, &(p[2]), &sigsize,
2456 signbuf, u) <= 0)) {
2457 SSLerror(s, ERR_R_EVP_LIB);
2458 goto err;
2460 if (!tls1_digest_cached_records(s))
2461 goto err;
2462 j = sigsize;
2463 s2n(j, p);
2464 n = j + 2;
2465 #endif
2466 } else {
2467 SSLerror(s, ERR_R_INTERNAL_ERROR);
2468 goto err;
2471 s->internal->state = SSL3_ST_CW_CERT_VRFY_B;
2473 ssl3_handshake_msg_finish(s, n);
2476 EVP_MD_CTX_cleanup(&mctx);
2477 EVP_PKEY_CTX_free(pctx);
2479 return (ssl3_handshake_write(s));
2481 err:
2482 EVP_MD_CTX_cleanup(&mctx);
2483 EVP_PKEY_CTX_free(pctx);
2484 return (-1);
2488 ssl3_send_client_certificate(SSL *s)
2490 EVP_PKEY *pkey = NULL;
2491 X509 *x509 = NULL;
2492 CBB cbb, client_cert;
2493 int i;
2495 memset(&cbb, 0, sizeof(cbb));
2497 if (s->internal->state == SSL3_ST_CW_CERT_A) {
2498 if ((s->cert == NULL) || (s->cert->key->x509 == NULL) ||
2499 (s->cert->key->privatekey == NULL))
2500 s->internal->state = SSL3_ST_CW_CERT_B;
2501 else
2502 s->internal->state = SSL3_ST_CW_CERT_C;
2505 /* We need to get a client cert */
2506 if (s->internal->state == SSL3_ST_CW_CERT_B) {
2508 * If we get an error, we need to
2509 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2510 * We then get retied later
2512 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2513 if (i < 0) {
2514 s->internal->rwstate = SSL_X509_LOOKUP;
2515 return (-1);
2517 s->internal->rwstate = SSL_NOTHING;
2518 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
2519 s->internal->state = SSL3_ST_CW_CERT_B;
2520 if (!SSL_use_certificate(s, x509) ||
2521 !SSL_use_PrivateKey(s, pkey))
2522 i = 0;
2523 } else if (i == 1) {
2524 i = 0;
2525 SSLerror(s, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2528 X509_free(x509);
2529 EVP_PKEY_free(pkey);
2530 if (i == 0)
2531 S3I(s)->tmp.cert_req = 2;
2533 /* Ok, we have a cert */
2534 s->internal->state = SSL3_ST_CW_CERT_C;
2537 if (s->internal->state == SSL3_ST_CW_CERT_C) {
2538 if (!ssl3_handshake_msg_start_cbb(s, &cbb, &client_cert,
2539 SSL3_MT_CERTIFICATE))
2540 goto err;
2541 if (!ssl3_output_cert_chain(s, &client_cert,
2542 (S3I(s)->tmp.cert_req == 2) ? NULL : s->cert->key->x509))
2543 goto err;
2544 if (!ssl3_handshake_msg_finish_cbb(s, &cbb))
2545 goto err;
2547 s->internal->state = SSL3_ST_CW_CERT_D;
2550 /* SSL3_ST_CW_CERT_D */
2551 return (ssl3_handshake_write(s));
2553 err:
2554 CBB_cleanup(&cbb);
2556 return (0);
2559 #define has_bits(i,m) (((i)&(m)) == (m))
2562 ssl3_check_cert_and_algorithm(SSL *s)
2564 int i, idx;
2565 long alg_k, alg_a;
2566 EVP_PKEY *pkey = NULL;
2567 SESS_CERT *sc;
2568 DH *dh;
2570 alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey;
2571 alg_a = S3I(s)->tmp.new_cipher->algorithm_auth;
2573 /* We don't have a certificate. */
2574 if (alg_a & SSL_aNULL)
2575 return (1);
2577 sc = SSI(s)->sess_cert;
2578 if (sc == NULL) {
2579 SSLerror(s, ERR_R_INTERNAL_ERROR);
2580 goto err;
2582 dh = SSI(s)->sess_cert->peer_dh_tmp;
2584 /* This is the passed certificate. */
2586 idx = sc->peer_cert_type;
2587 if (idx == SSL_PKEY_ECC) {
2588 if (ssl_check_srvr_ecc_cert_and_alg(
2589 sc->peer_pkeys[idx].x509, s) == 0) {
2590 /* check failed */
2591 SSLerror(s, SSL_R_BAD_ECC_CERT);
2592 goto f_err;
2593 } else {
2594 return (1);
2597 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
2598 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
2599 EVP_PKEY_free(pkey);
2601 /* Check that we have a certificate if we require one. */
2602 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA|EVP_PKT_SIGN)) {
2603 SSLerror(s, SSL_R_MISSING_RSA_SIGNING_CERT);
2604 goto f_err;
2605 } else if ((alg_a & SSL_aDSS) &&
2606 !has_bits(i, EVP_PK_DSA|EVP_PKT_SIGN)) {
2607 SSLerror(s, SSL_R_MISSING_DSA_SIGNING_CERT);
2608 goto f_err;
2610 if ((alg_k & SSL_kRSA) &&
2611 !has_bits(i, EVP_PK_RSA|EVP_PKT_ENC)) {
2612 SSLerror(s, SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2613 goto f_err;
2615 if ((alg_k & SSL_kDHE) &&
2616 !(has_bits(i, EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL))) {
2617 SSLerror(s, SSL_R_MISSING_DH_KEY);
2618 goto f_err;
2621 return (1);
2622 f_err:
2623 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2624 err:
2625 return (0);
2629 ssl3_send_next_proto(SSL *s)
2631 CBB cbb, nextproto, npn, padding;
2632 size_t pad_len;
2633 uint8_t *pad;
2635 memset(&cbb, 0, sizeof(cbb));
2637 if (s->internal->state == SSL3_ST_CW_NEXT_PROTO_A) {
2638 pad_len = 32 - ((s->internal->next_proto_negotiated_len + 2) % 32);
2640 if (!ssl3_handshake_msg_start_cbb(s, &cbb, &nextproto,
2641 SSL3_MT_NEXT_PROTO))
2642 goto err;
2643 if (!CBB_add_u8_length_prefixed(&nextproto, &npn))
2644 goto err;
2645 if (!CBB_add_bytes(&npn, s->internal->next_proto_negotiated,
2646 s->internal->next_proto_negotiated_len))
2647 goto err;
2648 if (!CBB_add_u8_length_prefixed(&nextproto, &padding))
2649 goto err;
2650 if (!CBB_add_space(&padding, &pad, pad_len))
2651 goto err;
2652 memset(pad, 0, pad_len);
2653 if (!ssl3_handshake_msg_finish_cbb(s, &cbb))
2654 goto err;
2656 s->internal->state = SSL3_ST_CW_NEXT_PROTO_B;
2659 return (ssl3_handshake_write(s));
2661 err:
2662 CBB_cleanup(&cbb);
2664 return (-1);
2668 * Check to see if handshake is full or resumed. Usually this is just a
2669 * case of checking to see if a cache hit has occurred. In the case of
2670 * session tickets we have to check the next message to be sure.
2674 ssl3_check_finished(SSL *s)
2676 int ok;
2677 long n;
2679 /* If we have no ticket it cannot be a resumed session. */
2680 if (!s->session->tlsext_tick)
2681 return (1);
2682 /* this function is called when we really expect a Certificate
2683 * message, so permit appropriate message length */
2684 n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_CERT_A,
2685 SSL3_ST_CR_CERT_B, -1, s->internal->max_cert_list, &ok);
2686 if (!ok)
2687 return ((int)n);
2688 S3I(s)->tmp.reuse_message = 1;
2689 if ((S3I(s)->tmp.message_type == SSL3_MT_FINISHED) ||
2690 (S3I(s)->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
2691 return (2);
2693 return (1);
2697 ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
2699 int i = 0;
2701 #ifndef OPENSSL_NO_ENGINE
2702 if (s->ctx->internal->client_cert_engine) {
2703 i = ENGINE_load_ssl_client_cert(
2704 s->ctx->internal->client_cert_engine, s,
2705 SSL_get_client_CA_list(s), px509, ppkey, NULL, NULL, NULL);
2706 if (i != 0)
2707 return (i);
2709 #endif
2710 if (s->ctx->internal->client_cert_cb)
2711 i = s->ctx->internal->client_cert_cb(s, px509, ppkey);
2712 return (i);