summaryrefslogtreecommitdiff
path: root/src/lib/libssl/s3_srvr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/s3_srvr.c')
-rw-r--r--src/lib/libssl/s3_srvr.c2778
1 files changed, 0 insertions, 2778 deletions
diff --git a/src/lib/libssl/s3_srvr.c b/src/lib/libssl/s3_srvr.c
deleted file mode 100644
index 7d2ec4d132..0000000000
--- a/src/lib/libssl/s3_srvr.c
+++ /dev/null
@@ -1,2778 +0,0 @@
1/* $OpenBSD: s3_srvr.c,v 1.112 2015/07/29 19:16:09 miod Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
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.
8 *
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).
15 *
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.
22 *
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)"
40 *
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.
52 *
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.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
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.
72 *
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/)"
77 *
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.
82 *
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.
86 *
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/)"
91 *
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 * ====================================================================
105 *
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).
109 *
110 */
111/* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 *
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
116 *
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
119 *
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
122 *
123 */
124/* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
126 *
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.
130 *
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.
134 *
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.
138 *
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.
143 *
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.
149 */
150
151#include <stdio.h>
152
153#include "ssl_locl.h"
154
155#include <openssl/bn.h>
156#include <openssl/buffer.h>
157#include <openssl/evp.h>
158#include <openssl/dh.h>
159#ifndef OPENSSL_NO_GOST
160#include <openssl/gost.h>
161#endif
162#include <openssl/hmac.h>
163#include <openssl/md5.h>
164#include <openssl/objects.h>
165#include <openssl/x509.h>
166
167#include "bytestring.h"
168
169#ifdef __OpenBSD__
170#include <sys/cdefs.h>
171__warn_references(SSLv3_server_method,
172 "SSLv3_server_method() enables the use of insecure protocols");
173#endif
174
175static const SSL_METHOD *ssl3_get_server_method(int ver);
176
177const SSL_METHOD SSLv3_server_method_data = {
178 .version = SSL3_VERSION,
179 .ssl_new = ssl3_new,
180 .ssl_clear = ssl3_clear,
181 .ssl_free = ssl3_free,
182 .ssl_accept = ssl3_accept,
183 .ssl_connect = ssl_undefined_function,
184 .ssl_read = ssl3_read,
185 .ssl_peek = ssl3_peek,
186 .ssl_write = ssl3_write,
187 .ssl_shutdown = ssl3_shutdown,
188 .ssl_renegotiate = ssl3_renegotiate,
189 .ssl_renegotiate_check = ssl3_renegotiate_check,
190 .ssl_get_message = ssl3_get_message,
191 .ssl_read_bytes = ssl3_read_bytes,
192 .ssl_write_bytes = ssl3_write_bytes,
193 .ssl_dispatch_alert = ssl3_dispatch_alert,
194 .ssl_ctrl = ssl3_ctrl,
195 .ssl_ctx_ctrl = ssl3_ctx_ctrl,
196 .get_cipher_by_char = ssl3_get_cipher_by_char,
197 .put_cipher_by_char = ssl3_put_cipher_by_char,
198 .ssl_pending = ssl3_pending,
199 .num_ciphers = ssl3_num_ciphers,
200 .get_cipher = ssl3_get_cipher,
201 .get_ssl_method = ssl3_get_server_method,
202 .get_timeout = ssl3_default_timeout,
203 .ssl3_enc = &SSLv3_enc_data,
204 .ssl_version = ssl_undefined_void_function,
205 .ssl_callback_ctrl = ssl3_callback_ctrl,
206 .ssl_ctx_callback_ctrl = ssl3_ctx_callback_ctrl,
207};
208
209const SSL_METHOD *
210SSLv3_server_method(void)
211{
212 return &SSLv3_server_method_data;
213}
214
215static const SSL_METHOD *
216ssl3_get_server_method(int ver)
217{
218 if (ver == SSL3_VERSION)
219 return (SSLv3_server_method());
220 return (NULL);
221}
222
223int
224ssl3_accept(SSL *s)
225{
226 unsigned long alg_k;
227 void (*cb)(const SSL *ssl, int type, int val) = NULL;
228 int ret = -1;
229 int new_state, state, skip = 0;
230
231 ERR_clear_error();
232 errno = 0;
233
234 if (s->info_callback != NULL)
235 cb = s->info_callback;
236 else if (s->ctx->info_callback != NULL)
237 cb = s->ctx->info_callback;
238
239 /* init things to blank */
240 s->in_handshake++;
241 if (!SSL_in_init(s) || SSL_in_before(s))
242 SSL_clear(s);
243
244 if (s->cert == NULL) {
245 SSLerr(SSL_F_SSL3_ACCEPT,
246 SSL_R_NO_CERTIFICATE_SET);
247 return (-1);
248 }
249
250 for (;;) {
251 state = s->state;
252
253 switch (s->state) {
254 case SSL_ST_RENEGOTIATE:
255 s->renegotiate = 1;
256 /* s->state=SSL_ST_ACCEPT; */
257
258 case SSL_ST_BEFORE:
259 case SSL_ST_ACCEPT:
260 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
261 case SSL_ST_OK|SSL_ST_ACCEPT:
262
263 s->server = 1;
264 if (cb != NULL)
265 cb(s, SSL_CB_HANDSHAKE_START, 1);
266
267 if ((s->version >> 8) != 3) {
268 SSLerr(SSL_F_SSL3_ACCEPT,
269 ERR_R_INTERNAL_ERROR);
270 return (-1);
271 }
272 s->type = SSL_ST_ACCEPT;
273
274 if (!ssl3_setup_init_buffer(s)) {
275 ret = -1;
276 goto end;
277 }
278 if (!ssl3_setup_buffers(s)) {
279 ret = -1;
280 goto end;
281 }
282
283 s->init_num = 0;
284
285 if (s->state != SSL_ST_RENEGOTIATE) {
286 /*
287 * Ok, we now need to push on a buffering BIO
288 * so that the output is sent in a way that
289 * TCP likes :-)
290 */
291 if (!ssl_init_wbio_buffer(s, 1)) {
292 ret = -1;
293 goto end;
294 }
295
296 if (!ssl3_init_finished_mac(s)) {
297 ret = -1;
298 goto end;
299 }
300
301 s->state = SSL3_ST_SR_CLNT_HELLO_A;
302 s->ctx->stats.sess_accept++;
303 } else if (!s->s3->send_connection_binding) {
304 /*
305 * Server attempting to renegotiate with
306 * client that doesn't support secure
307 * renegotiation.
308 */
309 SSLerr(SSL_F_SSL3_ACCEPT,
310 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
311 ssl3_send_alert(s, SSL3_AL_FATAL,
312 SSL_AD_HANDSHAKE_FAILURE);
313 ret = -1;
314 goto end;
315 } else {
316 /*
317 * s->state == SSL_ST_RENEGOTIATE,
318 * we will just send a HelloRequest
319 */
320 s->ctx->stats.sess_accept_renegotiate++;
321 s->state = SSL3_ST_SW_HELLO_REQ_A;
322 }
323 break;
324
325 case SSL3_ST_SW_HELLO_REQ_A:
326 case SSL3_ST_SW_HELLO_REQ_B:
327
328 s->shutdown = 0;
329 ret = ssl3_send_hello_request(s);
330 if (ret <= 0)
331 goto end;
332 s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
333 s->state = SSL3_ST_SW_FLUSH;
334 s->init_num = 0;
335
336 if (!ssl3_init_finished_mac(s)) {
337 ret = -1;
338 goto end;
339 }
340 break;
341
342 case SSL3_ST_SW_HELLO_REQ_C:
343 s->state = SSL_ST_OK;
344 break;
345
346 case SSL3_ST_SR_CLNT_HELLO_A:
347 case SSL3_ST_SR_CLNT_HELLO_B:
348 case SSL3_ST_SR_CLNT_HELLO_C:
349
350 s->shutdown = 0;
351 if (s->rwstate != SSL_X509_LOOKUP) {
352 ret = ssl3_get_client_hello(s);
353 if (ret <= 0)
354 goto end;
355 }
356
357 s->renegotiate = 2;
358 s->state = SSL3_ST_SW_SRVR_HELLO_A;
359 s->init_num = 0;
360 break;
361
362 case SSL3_ST_SW_SRVR_HELLO_A:
363 case SSL3_ST_SW_SRVR_HELLO_B:
364 ret = ssl3_send_server_hello(s);
365 if (ret <= 0)
366 goto end;
367 if (s->hit) {
368 if (s->tlsext_ticket_expected)
369 s->state = SSL3_ST_SW_SESSION_TICKET_A;
370 else
371 s->state = SSL3_ST_SW_CHANGE_A;
372 }
373 else
374 s->state = SSL3_ST_SW_CERT_A;
375 s->init_num = 0;
376 break;
377
378 case SSL3_ST_SW_CERT_A:
379 case SSL3_ST_SW_CERT_B:
380 /* Check if it is anon DH or anon ECDH. */
381 if (!(s->s3->tmp.new_cipher->algorithm_auth &
382 SSL_aNULL)) {
383 ret = ssl3_send_server_certificate(s);
384 if (ret <= 0)
385 goto end;
386 if (s->tlsext_status_expected)
387 s->state = SSL3_ST_SW_CERT_STATUS_A;
388 else
389 s->state = SSL3_ST_SW_KEY_EXCH_A;
390 } else {
391 skip = 1;
392 s->state = SSL3_ST_SW_KEY_EXCH_A;
393 }
394 s->init_num = 0;
395 break;
396
397 case SSL3_ST_SW_KEY_EXCH_A:
398 case SSL3_ST_SW_KEY_EXCH_B:
399 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
400
401 /*
402 * Only send if using a DH key exchange.
403 *
404 * For ECC ciphersuites, we send a ServerKeyExchange
405 * message only if the cipher suite is ECDHE. In other
406 * cases, the server certificate contains the server's
407 * public key for key exchange.
408 */
409 if (alg_k & (SSL_kDHE|SSL_kECDHE)) {
410 ret = ssl3_send_server_key_exchange(s);
411 if (ret <= 0)
412 goto end;
413 } else
414 skip = 1;
415
416 s->state = SSL3_ST_SW_CERT_REQ_A;
417 s->init_num = 0;
418 break;
419
420 case SSL3_ST_SW_CERT_REQ_A:
421 case SSL3_ST_SW_CERT_REQ_B:
422 /*
423 * Determine whether or not we need to request a
424 * certificate.
425 *
426 * Do not request a certificate if:
427 *
428 * - We did not ask for it (SSL_VERIFY_PEER is unset).
429 *
430 * - SSL_VERIFY_CLIENT_ONCE is set and we are
431 * renegotiating.
432 *
433 * - We are using an anonymous ciphersuites
434 * (see section "Certificate request" in SSL 3 drafts
435 * and in RFC 2246) ... except when the application
436 * insists on verification (against the specs, but
437 * s3_clnt.c accepts this for SSL 3).
438 */
439 if (!(s->verify_mode & SSL_VERIFY_PEER) ||
440 ((s->session->peer != NULL) &&
441 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
442 ((s->s3->tmp.new_cipher->algorithm_auth &
443 SSL_aNULL) && !(s->verify_mode &
444 SSL_VERIFY_FAIL_IF_NO_PEER_CERT))) {
445 /* No cert request */
446 skip = 1;
447 s->s3->tmp.cert_request = 0;
448 s->state = SSL3_ST_SW_SRVR_DONE_A;
449 if (s->s3->handshake_buffer)
450 if (!ssl3_digest_cached_records(s))
451 return (-1);
452 } else {
453 s->s3->tmp.cert_request = 1;
454 ret = ssl3_send_certificate_request(s);
455 if (ret <= 0)
456 goto end;
457 s->state = SSL3_ST_SW_SRVR_DONE_A;
458 s->init_num = 0;
459 }
460 break;
461
462 case SSL3_ST_SW_SRVR_DONE_A:
463 case SSL3_ST_SW_SRVR_DONE_B:
464 ret = ssl3_send_server_done(s);
465 if (ret <= 0)
466 goto end;
467 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
468 s->state = SSL3_ST_SW_FLUSH;
469 s->init_num = 0;
470 break;
471
472 case SSL3_ST_SW_FLUSH:
473
474 /*
475 * This code originally checked to see if
476 * any data was pending using BIO_CTRL_INFO
477 * and then flushed. This caused problems
478 * as documented in PR#1939. The proposed
479 * fix doesn't completely resolve this issue
480 * as buggy implementations of BIO_CTRL_PENDING
481 * still exist. So instead we just flush
482 * unconditionally.
483 */
484
485 s->rwstate = SSL_WRITING;
486 if (BIO_flush(s->wbio) <= 0) {
487 ret = -1;
488 goto end;
489 }
490 s->rwstate = SSL_NOTHING;
491
492 s->state = s->s3->tmp.next_state;
493 break;
494
495 case SSL3_ST_SR_CERT_A:
496 case SSL3_ST_SR_CERT_B:
497 if (s->s3->tmp.cert_request) {
498 ret = ssl3_get_client_certificate(s);
499 if (ret <= 0)
500 goto end;
501 }
502 s->init_num = 0;
503 s->state = SSL3_ST_SR_KEY_EXCH_A;
504 break;
505
506 case SSL3_ST_SR_KEY_EXCH_A:
507 case SSL3_ST_SR_KEY_EXCH_B:
508 ret = ssl3_get_client_key_exchange(s);
509 if (ret <= 0)
510 goto end;
511 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
512 if (ret == 2) {
513 /*
514 * For the ECDH ciphersuites when
515 * the client sends its ECDH pub key in
516 * a certificate, the CertificateVerify
517 * message is not sent.
518 * Also for GOST ciphersuites when
519 * the client uses its key from the certificate
520 * for key exchange.
521 */
522 if (s->s3->next_proto_neg_seen)
523 s->state = SSL3_ST_SR_NEXT_PROTO_A;
524 else
525 s->state = SSL3_ST_SR_FINISHED_A;
526 s->init_num = 0;
527 } else if (SSL_USE_SIGALGS(s) || (alg_k & SSL_kGOST)) {
528 s->state = SSL3_ST_SR_CERT_VRFY_A;
529 s->init_num = 0;
530 if (!s->session->peer)
531 break;
532 /*
533 * For sigalgs freeze the handshake buffer
534 * at this point and digest cached records.
535 */
536 if (!s->s3->handshake_buffer) {
537 SSLerr(SSL_F_SSL3_ACCEPT,
538 ERR_R_INTERNAL_ERROR);
539 return (-1);
540 }
541 s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
542 if (!ssl3_digest_cached_records(s))
543 return (-1);
544 } else {
545 int offset = 0;
546 int dgst_num;
547
548 s->state = SSL3_ST_SR_CERT_VRFY_A;
549 s->init_num = 0;
550
551 /*
552 * We need to get hashes here so if there is
553 * a client cert, it can be verified
554 * FIXME - digest processing for
555 * CertificateVerify should be generalized.
556 * But it is next step
557 */
558 if (s->s3->handshake_buffer)
559 if (!ssl3_digest_cached_records(s))
560 return (-1);
561 for (dgst_num = 0; dgst_num < SSL_MAX_DIGEST;
562 dgst_num++)
563 if (s->s3->handshake_dgst[dgst_num]) {
564 int dgst_size;
565
566 s->method->ssl3_enc->cert_verify_mac(s,
567 EVP_MD_CTX_type(
568 s->s3->handshake_dgst[dgst_num]),
569 &(s->s3->tmp.cert_verify_md[offset]));
570 dgst_size = EVP_MD_CTX_size(
571 s->s3->handshake_dgst[dgst_num]);
572 if (dgst_size < 0) {
573 ret = -1;
574 goto end;
575 }
576 offset += dgst_size;
577 }
578 }
579 break;
580
581 case SSL3_ST_SR_CERT_VRFY_A:
582 case SSL3_ST_SR_CERT_VRFY_B:
583 s->s3->flags |= SSL3_FLAGS_CCS_OK;
584
585 /* we should decide if we expected this one */
586 ret = ssl3_get_cert_verify(s);
587 if (ret <= 0)
588 goto end;
589
590 if (s->s3->next_proto_neg_seen)
591 s->state = SSL3_ST_SR_NEXT_PROTO_A;
592 else
593 s->state = SSL3_ST_SR_FINISHED_A;
594 s->init_num = 0;
595 break;
596
597 case SSL3_ST_SR_NEXT_PROTO_A:
598 case SSL3_ST_SR_NEXT_PROTO_B:
599 ret = ssl3_get_next_proto(s);
600 if (ret <= 0)
601 goto end;
602 s->init_num = 0;
603 s->state = SSL3_ST_SR_FINISHED_A;
604 break;
605
606 case SSL3_ST_SR_FINISHED_A:
607 case SSL3_ST_SR_FINISHED_B:
608 s->s3->flags |= SSL3_FLAGS_CCS_OK;
609 ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
610 SSL3_ST_SR_FINISHED_B);
611 if (ret <= 0)
612 goto end;
613 if (s->hit)
614 s->state = SSL_ST_OK;
615 else if (s->tlsext_ticket_expected)
616 s->state = SSL3_ST_SW_SESSION_TICKET_A;
617 else
618 s->state = SSL3_ST_SW_CHANGE_A;
619 s->init_num = 0;
620 break;
621
622 case SSL3_ST_SW_SESSION_TICKET_A:
623 case SSL3_ST_SW_SESSION_TICKET_B:
624 ret = ssl3_send_newsession_ticket(s);
625 if (ret <= 0)
626 goto end;
627 s->state = SSL3_ST_SW_CHANGE_A;
628 s->init_num = 0;
629 break;
630
631 case SSL3_ST_SW_CERT_STATUS_A:
632 case SSL3_ST_SW_CERT_STATUS_B:
633 ret = ssl3_send_cert_status(s);
634 if (ret <= 0)
635 goto end;
636 s->state = SSL3_ST_SW_KEY_EXCH_A;
637 s->init_num = 0;
638 break;
639
640
641 case SSL3_ST_SW_CHANGE_A:
642 case SSL3_ST_SW_CHANGE_B:
643
644 s->session->cipher = s->s3->tmp.new_cipher;
645 if (!s->method->ssl3_enc->setup_key_block(s)) {
646 ret = -1;
647 goto end;
648 }
649
650 ret = ssl3_send_change_cipher_spec(s,
651 SSL3_ST_SW_CHANGE_A, SSL3_ST_SW_CHANGE_B);
652
653 if (ret <= 0)
654 goto end;
655 s->state = SSL3_ST_SW_FINISHED_A;
656 s->init_num = 0;
657
658 if (!s->method->ssl3_enc->change_cipher_state(
659 s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
660 ret = -1;
661 goto end;
662 }
663
664 break;
665
666 case SSL3_ST_SW_FINISHED_A:
667 case SSL3_ST_SW_FINISHED_B:
668 ret = ssl3_send_finished(s,
669 SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B,
670 s->method->ssl3_enc->server_finished_label,
671 s->method->ssl3_enc->server_finished_label_len);
672 if (ret <= 0)
673 goto end;
674 s->state = SSL3_ST_SW_FLUSH;
675 if (s->hit) {
676 if (s->s3->next_proto_neg_seen) {
677 s->s3->flags |= SSL3_FLAGS_CCS_OK;
678 s->s3->tmp.next_state =
679 SSL3_ST_SR_NEXT_PROTO_A;
680 } else
681 s->s3->tmp.next_state =
682 SSL3_ST_SR_FINISHED_A;
683 } else
684 s->s3->tmp.next_state = SSL_ST_OK;
685 s->init_num = 0;
686 break;
687
688 case SSL_ST_OK:
689 /* clean a few things up */
690 ssl3_cleanup_key_block(s);
691
692 BUF_MEM_free(s->init_buf);
693 s->init_buf = NULL;
694
695 /* remove buffering on output */
696 ssl_free_wbio_buffer(s);
697
698 s->init_num = 0;
699
700 /* skipped if we just sent a HelloRequest */
701 if (s->renegotiate == 2) {
702 s->renegotiate = 0;
703 s->new_session = 0;
704
705 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
706
707 s->ctx->stats.sess_accept_good++;
708 /* s->server=1; */
709 s->handshake_func = ssl3_accept;
710
711 if (cb != NULL)
712 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
713 }
714
715 ret = 1;
716 goto end;
717 /* break; */
718
719 default:
720 SSLerr(SSL_F_SSL3_ACCEPT,
721 SSL_R_UNKNOWN_STATE);
722 ret = -1;
723 goto end;
724 /* break; */
725 }
726
727 if (!s->s3->tmp.reuse_message && !skip) {
728 if (s->debug) {
729 if ((ret = BIO_flush(s->wbio)) <= 0)
730 goto end;
731 }
732
733
734 if ((cb != NULL) && (s->state != state)) {
735 new_state = s->state;
736 s->state = state;
737 cb(s, SSL_CB_ACCEPT_LOOP, 1);
738 s->state = new_state;
739 }
740 }
741 skip = 0;
742 }
743end:
744 /* BIO_flush(s->wbio); */
745
746 s->in_handshake--;
747 if (cb != NULL)
748 cb(s, SSL_CB_ACCEPT_EXIT, ret);
749 return (ret);
750}
751
752int
753ssl3_send_hello_request(SSL *s)
754{
755 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
756 ssl3_handshake_msg_start(s, SSL3_MT_HELLO_REQUEST);
757 ssl3_handshake_msg_finish(s, 0);
758
759 s->state = SSL3_ST_SW_HELLO_REQ_B;
760 }
761
762 /* SSL3_ST_SW_HELLO_REQ_B */
763 return (ssl3_handshake_write(s));
764}
765
766int
767ssl3_get_client_hello(SSL *s)
768{
769 int i, j, ok, al, ret = -1;
770 unsigned int cookie_len;
771 long n;
772 unsigned long id;
773 unsigned char *p, *d;
774 SSL_CIPHER *c;
775 STACK_OF(SSL_CIPHER) *ciphers = NULL;
776 unsigned long alg_k;
777
778 /*
779 * We do this so that we will respond with our native type.
780 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
781 * This down switching should be handled by a different method.
782 * If we are SSLv3, we will respond with SSLv3, even if prompted with
783 * TLSv1.
784 */
785 if (s->state == SSL3_ST_SR_CLNT_HELLO_A) {
786 s->state = SSL3_ST_SR_CLNT_HELLO_B;
787 }
788 s->first_packet = 1;
789 n = s->method->ssl_get_message(s, SSL3_ST_SR_CLNT_HELLO_B,
790 SSL3_ST_SR_CLNT_HELLO_C, SSL3_MT_CLIENT_HELLO,
791 SSL3_RT_MAX_PLAIN_LENGTH, &ok);
792
793 if (!ok)
794 return ((int)n);
795 s->first_packet = 0;
796 d = p = (unsigned char *)s->init_msg;
797
798 if (2 > n)
799 goto truncated;
800 /*
801 * Use version from inside client hello, not from record header.
802 * (may differ: see RFC 2246, Appendix E, second paragraph)
803 */
804 s->client_version = (((int)p[0]) << 8)|(int)p[1];
805 p += 2;
806
807 if ((s->version == DTLS1_VERSION && s->client_version > s->version) ||
808 (s->version != DTLS1_VERSION && s->client_version < s->version)) {
809 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
810 SSL_R_WRONG_VERSION_NUMBER);
811 if ((s->client_version >> 8) == SSL3_VERSION_MAJOR &&
812 !s->enc_write_ctx && !s->write_hash) {
813 /*
814 * Similar to ssl3_get_record, send alert using remote
815 * version number
816 */
817 s->version = s->client_version;
818 }
819 al = SSL_AD_PROTOCOL_VERSION;
820 goto f_err;
821 }
822
823 /*
824 * If we require cookies and this ClientHello doesn't
825 * contain one, just return since we do not want to
826 * allocate any memory yet. So check cookie length...
827 */
828 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
829 unsigned int session_length, cookie_length;
830
831 session_length = *(p + SSL3_RANDOM_SIZE);
832 cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
833
834 if (cookie_length == 0)
835 return (1);
836 }
837
838 if (p + SSL3_RANDOM_SIZE + 1 - d > n)
839 goto truncated;
840
841 /* load the client random */
842 memcpy(s->s3->client_random, p, SSL3_RANDOM_SIZE);
843 p += SSL3_RANDOM_SIZE;
844
845 /* get the session-id */
846 j= *(p++);
847 if (p + j - d > n)
848 goto truncated;
849
850 s->hit = 0;
851 /*
852 * Versions before 0.9.7 always allow clients to resume sessions in
853 * renegotiation. 0.9.7 and later allow this by default, but optionally
854 * ignore resumption requests with flag
855 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag
856 * rather than a change to default behavior so that applications
857 * relying on this for security won't even compile against older
858 * library versions).
859 *
860 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated()
861 * to request renegotiation but not a new session (s->new_session
862 * remains unset): for servers, this essentially just means that the
863 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
864 * ignored.
865 */
866 if ((s->new_session && (s->options &
867 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
868 if (!ssl_get_new_session(s, 1))
869 goto err;
870 } else {
871 i = ssl_get_prev_session(s, p, j, d + n);
872 if (i == 1) { /* previous session */
873 s->hit = 1;
874 } else if (i == -1)
875 goto err;
876 else {
877 /* i == 0 */
878 if (!ssl_get_new_session(s, 1))
879 goto err;
880 }
881 }
882
883 p += j;
884
885 if (SSL_IS_DTLS(s)) {
886 /* cookie stuff */
887 if (p + 1 - d > n)
888 goto truncated;
889 cookie_len = *(p++);
890
891 /*
892 * The ClientHello may contain a cookie even if the
893 * HelloVerify message has not been sent--make sure that it
894 * does not cause an overflow.
895 */
896 if (cookie_len > sizeof(s->d1->rcvd_cookie)) {
897 /* too much data */
898 al = SSL_AD_DECODE_ERROR;
899 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
900 SSL_R_COOKIE_MISMATCH);
901 goto f_err;
902 }
903
904 if (p + cookie_len - d > n)
905 goto truncated;
906
907 /* verify the cookie if appropriate option is set. */
908 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
909 cookie_len > 0) {
910 memcpy(s->d1->rcvd_cookie, p, cookie_len);
911
912 if (s->ctx->app_verify_cookie_cb != NULL) {
913 if (s->ctx->app_verify_cookie_cb(s,
914 s->d1->rcvd_cookie, cookie_len) == 0) {
915 al = SSL_AD_HANDSHAKE_FAILURE;
916 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
917 SSL_R_COOKIE_MISMATCH);
918 goto f_err;
919 }
920 /* else cookie verification succeeded */
921 } else if (timingsafe_memcmp(s->d1->rcvd_cookie, s->d1->cookie,
922 s->d1->cookie_len) != 0) {
923 /* default verification */
924 al = SSL_AD_HANDSHAKE_FAILURE;
925 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
926 SSL_R_COOKIE_MISMATCH);
927 goto f_err;
928 }
929
930 ret = 2;
931 }
932
933 p += cookie_len;
934 }
935
936 if (p + 2 - d > n)
937 goto truncated;
938 n2s(p, i);
939 if ((i == 0) && (j != 0)) {
940 /* we need a cipher if we are not resuming a session */
941 al = SSL_AD_ILLEGAL_PARAMETER;
942 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
943 SSL_R_NO_CIPHERS_SPECIFIED);
944 goto f_err;
945 }
946 if (p + i - d > n)
947 goto truncated;
948 if (i > 0) {
949 if ((ciphers = ssl_bytes_to_cipher_list(s, p, i)) == NULL)
950 goto err;
951 }
952 p += i;
953
954 /* If it is a hit, check that the cipher is in the list */
955 if ((s->hit) && (i > 0)) {
956 j = 0;
957 id = s->session->cipher->id;
958
959 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
960 c = sk_SSL_CIPHER_value(ciphers, i);
961 if (c->id == id) {
962 j = 1;
963 break;
964 }
965 }
966 if (j == 0) {
967 /*
968 * We need to have the cipher in the cipher
969 * list if we are asked to reuse it
970 */
971 al = SSL_AD_ILLEGAL_PARAMETER;
972 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
973 SSL_R_REQUIRED_CIPHER_MISSING);
974 goto f_err;
975 }
976 }
977
978 /* compression */
979 if (p + 1 - d > n)
980 goto truncated;
981 i= *(p++);
982 if (p + i - d > n)
983 goto truncated;
984 for (j = 0; j < i; j++) {
985 if (p[j] == 0)
986 break;
987 }
988
989 p += i;
990 if (j >= i) {
991 /* no compress */
992 al = SSL_AD_DECODE_ERROR;
993 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
994 SSL_R_NO_COMPRESSION_SPECIFIED);
995 goto f_err;
996 }
997
998 /* TLS extensions*/
999 if (s->version >= SSL3_VERSION) {
1000 if (!ssl_parse_clienthello_tlsext(s, &p, d, n, &al)) {
1001 /* 'al' set by ssl_parse_clienthello_tlsext */
1002 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1003 SSL_R_PARSE_TLSEXT);
1004 goto f_err;
1005 }
1006 }
1007 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
1008 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1009 SSL_R_CLIENTHELLO_TLSEXT);
1010 goto err;
1011 }
1012
1013 /*
1014 * Check if we want to use external pre-shared secret for this
1015 * handshake for not reused session only. We need to generate
1016 * server_random before calling tls_session_secret_cb in order to allow
1017 * SessionTicket processing to use it in key derivation.
1018 */
1019 arc4random_buf(s->s3->server_random, SSL3_RANDOM_SIZE);
1020
1021 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
1022 SSL_CIPHER *pref_cipher = NULL;
1023
1024 s->session->master_key_length = sizeof(s->session->master_key);
1025 if (s->tls_session_secret_cb(s, s->session->master_key,
1026 &s->session->master_key_length, ciphers, &pref_cipher,
1027 s->tls_session_secret_cb_arg)) {
1028 s->hit = 1;
1029 s->session->ciphers = ciphers;
1030 s->session->verify_result = X509_V_OK;
1031
1032 ciphers = NULL;
1033
1034 /* check if some cipher was preferred by call back */
1035 pref_cipher = pref_cipher ? pref_cipher :
1036 ssl3_choose_cipher(s, s->session->ciphers,
1037 SSL_get_ciphers(s));
1038 if (pref_cipher == NULL) {
1039 al = SSL_AD_HANDSHAKE_FAILURE;
1040 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1041 SSL_R_NO_SHARED_CIPHER);
1042 goto f_err;
1043 }
1044
1045 s->session->cipher = pref_cipher;
1046
1047 if (s->cipher_list)
1048 sk_SSL_CIPHER_free(s->cipher_list);
1049
1050 if (s->cipher_list_by_id)
1051 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1052
1053 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1054 s->cipher_list_by_id =
1055 sk_SSL_CIPHER_dup(s->session->ciphers);
1056 }
1057 }
1058
1059 /*
1060 * Given s->session->ciphers and SSL_get_ciphers, we must
1061 * pick a cipher
1062 */
1063
1064 if (!s->hit) {
1065 if (s->session->ciphers != NULL)
1066 sk_SSL_CIPHER_free(s->session->ciphers);
1067 s->session->ciphers = ciphers;
1068 if (ciphers == NULL) {
1069 al = SSL_AD_ILLEGAL_PARAMETER;
1070 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1071 SSL_R_NO_CIPHERS_PASSED);
1072 goto f_err;
1073 }
1074 ciphers = NULL;
1075 c = ssl3_choose_cipher(s, s->session->ciphers,
1076 SSL_get_ciphers(s));
1077
1078 if (c == NULL) {
1079 al = SSL_AD_HANDSHAKE_FAILURE;
1080 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1081 SSL_R_NO_SHARED_CIPHER);
1082 goto f_err;
1083 }
1084 s->s3->tmp.new_cipher = c;
1085 } else {
1086 s->s3->tmp.new_cipher = s->session->cipher;
1087 }
1088
1089 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1090 if (!(SSL_USE_SIGALGS(s) || (alg_k & SSL_kGOST)) ||
1091 !(s->verify_mode & SSL_VERIFY_PEER)) {
1092 if (!ssl3_digest_cached_records(s)) {
1093 al = SSL_AD_INTERNAL_ERROR;
1094 goto f_err;
1095 }
1096 }
1097
1098 /*
1099 * We now have the following setup.
1100 * client_random
1101 * cipher_list - our prefered list of ciphers
1102 * ciphers - the clients prefered list of ciphers
1103 * compression - basically ignored right now
1104 * ssl version is set - sslv3
1105 * s->session - The ssl session has been setup.
1106 * s->hit - session reuse flag
1107 * s->tmp.new_cipher - the new cipher to use.
1108 */
1109
1110 /* Handles TLS extensions that we couldn't check earlier */
1111 if (s->version >= SSL3_VERSION) {
1112 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1113 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1114 SSL_R_CLIENTHELLO_TLSEXT);
1115 goto err;
1116 }
1117 }
1118
1119 if (ret < 0)
1120 ret = 1;
1121 if (0) {
1122truncated:
1123 al = SSL_AD_DECODE_ERROR;
1124 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_BAD_PACKET_LENGTH);
1125f_err:
1126 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1127 }
1128err:
1129 if (ciphers != NULL)
1130 sk_SSL_CIPHER_free(ciphers);
1131 return (ret);
1132}
1133
1134int
1135ssl3_send_server_hello(SSL *s)
1136{
1137 unsigned char *bufend;
1138 unsigned char *p, *d;
1139 int sl;
1140
1141 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1142 d = p = ssl3_handshake_msg_start(s, SSL3_MT_SERVER_HELLO);
1143
1144 *(p++) = s->version >> 8;
1145 *(p++) = s->version & 0xff;
1146
1147 /* Random stuff */
1148 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1149 p += SSL3_RANDOM_SIZE;
1150
1151 /*
1152 * There are several cases for the session ID to send
1153 * back in the server hello:
1154 *
1155 * - For session reuse from the session cache,
1156 * we send back the old session ID.
1157 * - If stateless session reuse (using a session ticket)
1158 * is successful, we send back the client's "session ID"
1159 * (which doesn't actually identify the session).
1160 * - If it is a new session, we send back the new
1161 * session ID.
1162 * - However, if we want the new session to be single-use,
1163 * we send back a 0-length session ID.
1164 *
1165 * s->hit is non-zero in either case of session reuse,
1166 * so the following won't overwrite an ID that we're supposed
1167 * to send back.
1168 */
1169 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1170 && !s->hit)
1171 s->session->session_id_length = 0;
1172
1173 sl = s->session->session_id_length;
1174 if (sl > (int)sizeof(s->session->session_id)) {
1175 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,
1176 ERR_R_INTERNAL_ERROR);
1177 return (-1);
1178 }
1179 *(p++) = sl;
1180 memcpy(p, s->session->session_id, sl);
1181 p += sl;
1182
1183 /* put the cipher */
1184 s2n(ssl3_cipher_get_value(s->s3->tmp.new_cipher), p);
1185
1186 /* put the compression method */
1187 *(p++) = 0;
1188
1189 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1190 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,
1191 SSL_R_SERVERHELLO_TLSEXT);
1192 return (-1);
1193 }
1194 bufend = (unsigned char *)s->init_buf->data +
1195 SSL3_RT_MAX_PLAIN_LENGTH;
1196 if ((p = ssl_add_serverhello_tlsext(s, p, bufend)) == NULL) {
1197 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,
1198 ERR_R_INTERNAL_ERROR);
1199 return (-1);
1200 }
1201
1202 ssl3_handshake_msg_finish(s, p - d);
1203 }
1204
1205 /* SSL3_ST_SW_SRVR_HELLO_B */
1206 return (ssl3_handshake_write(s));
1207}
1208
1209int
1210ssl3_send_server_done(SSL *s)
1211{
1212 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1213 ssl3_handshake_msg_start(s, SSL3_MT_SERVER_DONE);
1214 ssl3_handshake_msg_finish(s, 0);
1215
1216 s->state = SSL3_ST_SW_SRVR_DONE_B;
1217 }
1218
1219 /* SSL3_ST_SW_SRVR_DONE_B */
1220 return (ssl3_handshake_write(s));
1221}
1222
1223int
1224ssl3_send_server_key_exchange(SSL *s)
1225{
1226 unsigned char *q;
1227 int j, num;
1228 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1229 unsigned int u;
1230 DH *dh = NULL, *dhp;
1231 EC_KEY *ecdh = NULL, *ecdhp;
1232 unsigned char *encodedPoint = NULL;
1233 int encodedlen = 0;
1234 int curve_id = 0;
1235 BN_CTX *bn_ctx = NULL;
1236
1237 EVP_PKEY *pkey;
1238 const EVP_MD *md = NULL;
1239 unsigned char *p, *d;
1240 int al, i;
1241 unsigned long type;
1242 int n;
1243 CERT *cert;
1244 BIGNUM *r[4];
1245 int nr[4], kn;
1246 BUF_MEM *buf;
1247 EVP_MD_CTX md_ctx;
1248
1249 EVP_MD_CTX_init(&md_ctx);
1250 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1251 type = s->s3->tmp.new_cipher->algorithm_mkey;
1252 cert = s->cert;
1253
1254 buf = s->init_buf;
1255
1256 r[0] = r[1] = r[2] = r[3] = NULL;
1257 n = 0;
1258 if (type & SSL_kDHE) {
1259 if (s->cert->dh_tmp_auto != 0) {
1260 if ((dhp = ssl_get_auto_dh(s)) == NULL) {
1261 al = SSL_AD_INTERNAL_ERROR;
1262 SSLerr(
1263 SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1264 ERR_R_INTERNAL_ERROR);
1265 goto f_err;
1266 }
1267 } else
1268 dhp = cert->dh_tmp;
1269
1270 if (dhp == NULL && s->cert->dh_tmp_cb != NULL)
1271 dhp = s->cert->dh_tmp_cb(s, 0,
1272 SSL_C_PKEYLENGTH(s->s3->tmp.new_cipher));
1273
1274 if (dhp == NULL) {
1275 al = SSL_AD_HANDSHAKE_FAILURE;
1276 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1277 SSL_R_MISSING_TMP_DH_KEY);
1278 goto f_err;
1279 }
1280
1281 if (s->s3->tmp.dh != NULL) {
1282 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1283 ERR_R_INTERNAL_ERROR);
1284 goto err;
1285 }
1286
1287 if (s->cert->dh_tmp_auto != 0) {
1288 dh = dhp;
1289 } else if ((dh = DHparams_dup(dhp)) == NULL) {
1290 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1291 ERR_R_DH_LIB);
1292 goto err;
1293 }
1294 s->s3->tmp.dh = dh;
1295
1296 if ((dhp->pub_key == NULL || dhp->priv_key == NULL ||
1297 (s->options & SSL_OP_SINGLE_DH_USE))) {
1298 if (!DH_generate_key(dh)) {
1299 SSLerr(
1300 SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1301 ERR_R_DH_LIB);
1302 goto err;
1303 }
1304 } else {
1305 dh->pub_key = BN_dup(dhp->pub_key);
1306 dh->priv_key = BN_dup(dhp->priv_key);
1307 if ((dh->pub_key == NULL) ||
1308 (dh->priv_key == NULL)) {
1309 SSLerr(
1310 SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1311 ERR_R_DH_LIB);
1312 goto err;
1313 }
1314 }
1315 r[0] = dh->p;
1316 r[1] = dh->g;
1317 r[2] = dh->pub_key;
1318 } else
1319 if (type & SSL_kECDHE) {
1320 const EC_GROUP *group;
1321
1322 ecdhp = cert->ecdh_tmp;
1323 if (s->cert->ecdh_tmp_auto != 0) {
1324 int nid = tls1_get_shared_curve(s);
1325 if (nid != NID_undef)
1326 ecdhp = EC_KEY_new_by_curve_name(nid);
1327 } else if (ecdhp == NULL &&
1328 s->cert->ecdh_tmp_cb != NULL) {
1329 ecdhp = s->cert->ecdh_tmp_cb(s, 0,
1330 SSL_C_PKEYLENGTH(s->s3->tmp.new_cipher));
1331 }
1332 if (ecdhp == NULL) {
1333 al = SSL_AD_HANDSHAKE_FAILURE;
1334 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1335 SSL_R_MISSING_TMP_ECDH_KEY);
1336 goto f_err;
1337 }
1338
1339 if (s->s3->tmp.ecdh != NULL) {
1340 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1341 ERR_R_INTERNAL_ERROR);
1342 goto err;
1343 }
1344
1345 /* Duplicate the ECDH structure. */
1346 if (s->cert->ecdh_tmp_auto != 0) {
1347 ecdh = ecdhp;
1348 } else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1349 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1350 ERR_R_ECDH_LIB);
1351 goto err;
1352 }
1353 s->s3->tmp.ecdh = ecdh;
1354
1355 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1356 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1357 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1358 if (!EC_KEY_generate_key(ecdh)) {
1359 SSLerr(
1360 SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1361 ERR_R_ECDH_LIB);
1362 goto err;
1363 }
1364 }
1365
1366 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1367 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1368 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1369 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1370 ERR_R_ECDH_LIB);
1371 goto err;
1372 }
1373
1374 /*
1375 * XXX: For now, we only support ephemeral ECDH
1376 * keys over named (not generic) curves. For
1377 * supported named curves, curve_id is non-zero.
1378 */
1379 if ((curve_id = tls1_ec_nid2curve_id(
1380 EC_GROUP_get_curve_name(group))) == 0) {
1381 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1382 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1383 goto err;
1384 }
1385
1386 /*
1387 * Encode the public key.
1388 * First check the size of encoding and
1389 * allocate memory accordingly.
1390 */
1391 encodedlen = EC_POINT_point2oct(group,
1392 EC_KEY_get0_public_key(ecdh),
1393 POINT_CONVERSION_UNCOMPRESSED,
1394 NULL, 0, NULL);
1395
1396 encodedPoint = malloc(encodedlen);
1397
1398 bn_ctx = BN_CTX_new();
1399 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1400 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1401 ERR_R_MALLOC_FAILURE);
1402 goto err;
1403 }
1404
1405
1406 encodedlen = EC_POINT_point2oct(group,
1407 EC_KEY_get0_public_key(ecdh),
1408 POINT_CONVERSION_UNCOMPRESSED,
1409 encodedPoint, encodedlen, bn_ctx);
1410
1411 if (encodedlen == 0) {
1412 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1413 ERR_R_ECDH_LIB);
1414 goto err;
1415 }
1416
1417 BN_CTX_free(bn_ctx);
1418 bn_ctx = NULL;
1419
1420 /*
1421 * XXX: For now, we only support named (not
1422 * generic) curves in ECDH ephemeral key exchanges.
1423 * In this situation, we need four additional bytes
1424 * to encode the entire ServerECDHParams
1425 * structure.
1426 */
1427 n = 4 + encodedlen;
1428
1429 /*
1430 * We'll generate the serverKeyExchange message
1431 * explicitly so we can set these to NULLs
1432 */
1433 r[0] = NULL;
1434 r[1] = NULL;
1435 r[2] = NULL;
1436 r[3] = NULL;
1437 } else
1438 {
1439 al = SSL_AD_HANDSHAKE_FAILURE;
1440 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1441 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1442 goto f_err;
1443 }
1444 for (i = 0; i < 4 && r[i] != NULL; i++) {
1445 nr[i] = BN_num_bytes(r[i]);
1446 n += 2 + nr[i];
1447 }
1448
1449 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) {
1450 if ((pkey = ssl_get_sign_pkey(
1451 s, s->s3->tmp.new_cipher, &md)) == NULL) {
1452 al = SSL_AD_DECODE_ERROR;
1453 goto f_err;
1454 }
1455 kn = EVP_PKEY_size(pkey);
1456 } else {
1457 pkey = NULL;
1458 kn = 0;
1459 }
1460
1461 if (!BUF_MEM_grow_clean(buf, n + 4 + kn)) {
1462 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1463 ERR_LIB_BUF);
1464 goto err;
1465 }
1466 d = (unsigned char *)s->init_buf->data;
1467 p = &d[4];
1468
1469 for (i = 0; i < 4 && r[i] != NULL; i++) {
1470 s2n(nr[i], p);
1471 BN_bn2bin(r[i], p);
1472 p += nr[i];
1473 }
1474
1475 if (type & SSL_kECDHE) {
1476 /*
1477 * XXX: For now, we only support named (not generic)
1478 * curves.
1479 * In this situation, the serverKeyExchange message has:
1480 * [1 byte CurveType], [2 byte CurveName]
1481 * [1 byte length of encoded point], followed by
1482 * the actual encoded point itself
1483 */
1484 *p = NAMED_CURVE_TYPE;
1485 p += 1;
1486 *p = 0;
1487 p += 1;
1488 *p = curve_id;
1489 p += 1;
1490 *p = encodedlen;
1491 p += 1;
1492 memcpy((unsigned char*)p,
1493 (unsigned char *)encodedPoint, encodedlen);
1494 free(encodedPoint);
1495 encodedPoint = NULL;
1496 p += encodedlen;
1497 }
1498
1499
1500 /* not anonymous */
1501 if (pkey != NULL) {
1502 /*
1503 * n is the length of the params, they start at &(d[4])
1504 * and p points to the space at the end.
1505 */
1506 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1507 q = md_buf;
1508 j = 0;
1509 for (num = 2; num > 0; num--) {
1510 if (!EVP_DigestInit_ex(&md_ctx,
1511 (num == 2) ? s->ctx->md5 :
1512 s->ctx->sha1, NULL))
1513 goto err;
1514 EVP_DigestUpdate(&md_ctx,
1515 s->s3->client_random,
1516 SSL3_RANDOM_SIZE);
1517 EVP_DigestUpdate(&md_ctx,
1518 s->s3->server_random,
1519 SSL3_RANDOM_SIZE);
1520 EVP_DigestUpdate(&md_ctx, &d[4], n);
1521 EVP_DigestFinal_ex(&md_ctx, q,
1522 (unsigned int *)&i);
1523 q += i;
1524 j += i;
1525 }
1526 if (RSA_sign(NID_md5_sha1, md_buf, j,
1527 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1528 SSLerr(
1529 SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1530 ERR_LIB_RSA);
1531 goto err;
1532 }
1533 s2n(u, p);
1534 n += u + 2;
1535 } else if (md) {
1536 /* Send signature algorithm. */
1537 if (SSL_USE_SIGALGS(s)) {
1538 if (!tls12_get_sigandhash(p, pkey, md)) {
1539 /* Should never happen */
1540 al = SSL_AD_INTERNAL_ERROR;
1541 SSLerr(
1542 SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1543 ERR_R_INTERNAL_ERROR);
1544 goto f_err;
1545 }
1546 p += 2;
1547 }
1548 EVP_SignInit_ex(&md_ctx, md, NULL);
1549 EVP_SignUpdate(&md_ctx,
1550 s->s3->client_random,
1551 SSL3_RANDOM_SIZE);
1552 EVP_SignUpdate(&md_ctx,
1553 s->s3->server_random,
1554 SSL3_RANDOM_SIZE);
1555 EVP_SignUpdate(&md_ctx, &d[4], n);
1556 if (!EVP_SignFinal(&md_ctx, &p[2],
1557 (unsigned int *)&i, pkey)) {
1558 SSLerr(
1559 SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1560 ERR_LIB_EVP);
1561 goto err;
1562 }
1563 s2n(i, p);
1564 n += i + 2;
1565 if (SSL_USE_SIGALGS(s))
1566 n += 2;
1567 } else {
1568 /* Is this error check actually needed? */
1569 al = SSL_AD_HANDSHAKE_FAILURE;
1570 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1571 SSL_R_UNKNOWN_PKEY_TYPE);
1572 goto f_err;
1573 }
1574 }
1575
1576 *(d++) = SSL3_MT_SERVER_KEY_EXCHANGE;
1577 l2n3(n, d);
1578
1579 /* we should now have things packed up, so lets send it off */
1580 s->init_num = n + 4;
1581 s->init_off = 0;
1582 }
1583
1584 s->state = SSL3_ST_SW_KEY_EXCH_B;
1585 EVP_MD_CTX_cleanup(&md_ctx);
1586 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1587f_err:
1588 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1589err:
1590 free(encodedPoint);
1591 BN_CTX_free(bn_ctx);
1592 EVP_MD_CTX_cleanup(&md_ctx);
1593 return (-1);
1594}
1595
1596int
1597ssl3_send_certificate_request(SSL *s)
1598{
1599 unsigned char *p, *d;
1600 int i, j, nl, off, n;
1601 STACK_OF(X509_NAME) *sk = NULL;
1602 X509_NAME *name;
1603 BUF_MEM *buf;
1604
1605 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1606 buf = s->init_buf;
1607
1608 d = p = (unsigned char *)&(buf->data[4]);
1609
1610 /* get the list of acceptable cert types */
1611 p++;
1612 n = ssl3_get_req_cert_type(s, p);
1613 d[0] = n;
1614 p += n;
1615 n++;
1616
1617 if (SSL_USE_SIGALGS(s)) {
1618 nl = tls12_get_req_sig_algs(s, p + 2);
1619 s2n(nl, p);
1620 p += nl + 2;
1621 n += nl + 2;
1622 }
1623
1624 off = n;
1625 p += 2;
1626 n += 2;
1627
1628 sk = SSL_get_client_CA_list(s);
1629 nl = 0;
1630 if (sk != NULL) {
1631 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1632 name = sk_X509_NAME_value(sk, i);
1633 j = i2d_X509_NAME(name, NULL);
1634 if (!BUF_MEM_grow_clean(buf, 4 + n + j + 2)) {
1635 SSLerr(
1636 SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,
1637 ERR_R_BUF_LIB);
1638 goto err;
1639 }
1640 p = (unsigned char *)&(buf->data[4 + n]);
1641 s2n(j, p);
1642 i2d_X509_NAME(name, &p);
1643 n += 2 + j;
1644 nl += 2 + j;
1645 }
1646 }
1647 /* else no CA names */
1648 p = (unsigned char *)&(buf->data[4 + off]);
1649 s2n(nl, p);
1650
1651 d = (unsigned char *)buf->data;
1652 *(d++) = SSL3_MT_CERTIFICATE_REQUEST;
1653 l2n3(n, d);
1654
1655 /* we should now have things packed up, so lets send it off */
1656 s->init_num = n + 4;
1657 s->init_off = 0;
1658
1659 s->state = SSL3_ST_SW_CERT_REQ_B;
1660 }
1661
1662 /* SSL3_ST_SW_CERT_REQ_B */
1663 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1664err:
1665 return (-1);
1666}
1667
1668int
1669ssl3_get_client_key_exchange(SSL *s)
1670{
1671 int i, al, ok;
1672 long n;
1673 unsigned long alg_k;
1674 unsigned char *d, *p;
1675 RSA *rsa = NULL;
1676 EVP_PKEY *pkey = NULL;
1677 BIGNUM *pub = NULL;
1678 DH *dh_srvr;
1679
1680 EC_KEY *srvr_ecdh = NULL;
1681 EVP_PKEY *clnt_pub_pkey = NULL;
1682 EC_POINT *clnt_ecpoint = NULL;
1683 BN_CTX *bn_ctx = NULL;
1684
1685 /* 2048 maxlen is a guess. How long a key does that permit? */
1686 n = s->method->ssl_get_message(s, SSL3_ST_SR_KEY_EXCH_A,
1687 SSL3_ST_SR_KEY_EXCH_B, SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
1688 if (!ok)
1689 return ((int)n);
1690 d = p = (unsigned char *)s->init_msg;
1691
1692 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1693
1694 if (alg_k & SSL_kRSA) {
1695 char fakekey[SSL_MAX_MASTER_KEY_LENGTH];
1696
1697 arc4random_buf(fakekey, sizeof(fakekey));
1698 fakekey[0] = s->client_version >> 8;
1699 fakekey[1] = s->client_version & 0xff;
1700
1701 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1702 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA) ||
1703 (pkey->pkey.rsa == NULL)) {
1704 al = SSL_AD_HANDSHAKE_FAILURE;
1705 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1706 SSL_R_MISSING_RSA_CERTIFICATE);
1707 goto f_err;
1708 }
1709 rsa = pkey->pkey.rsa;
1710
1711 /* TLS and [incidentally] DTLS{0xFEFF} */
1712 if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER) {
1713 if (2 > n)
1714 goto truncated;
1715 n2s(p, i);
1716 if (n != i + 2) {
1717 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1718 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1719 goto err;
1720 } else
1721 n = i;
1722 }
1723
1724 i = RSA_private_decrypt((int)n, p, p, rsa, RSA_PKCS1_PADDING);
1725
1726 ERR_clear_error();
1727
1728 al = -1;
1729
1730 if (i != SSL_MAX_MASTER_KEY_LENGTH) {
1731 al = SSL_AD_DECODE_ERROR;
1732 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */
1733 }
1734
1735 if (p + 2 - d > n) /* needed in the SSL3 case */
1736 goto truncated;
1737 if ((al == -1) && !((p[0] == (s->client_version >> 8)) &&
1738 (p[1] == (s->client_version & 0xff)))) {
1739 /*
1740 * The premaster secret must contain the same version
1741 * number as the ClientHello to detect version rollback
1742 * attacks (strangely, the protocol does not offer such
1743 * protection for DH ciphersuites).
1744 * However, buggy clients exist that send the negotiated
1745 * protocol version instead if the server does not
1746 * support the requested protocol version.
1747 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
1748 * clients.
1749 */
1750 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
1751 (p[0] == (s->version >> 8)) &&
1752 (p[1] == (s->version & 0xff)))) {
1753 al = SSL_AD_DECODE_ERROR;
1754 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
1755
1756 /*
1757 * The Klima-Pokorny-Rosa extension of
1758 * Bleichenbacher's attack
1759 * (http://eprint.iacr.org/2003/052/) exploits
1760 * the version number check as a "bad version
1761 * oracle" -- an alert would reveal that the
1762 * plaintext corresponding to some ciphertext
1763 * made up by the adversary is properly
1764 * formatted except that the version number is
1765 * wrong.
1766 * To avoid such attacks, we should treat this
1767 * just like any other decryption error.
1768 */
1769 }
1770 }
1771
1772 if (al != -1) {
1773 /*
1774 * Some decryption failure -- use random value instead
1775 * as countermeasure against Bleichenbacher's attack
1776 * on PKCS #1 v1.5 RSA padding (see RFC 2246,
1777 * section 7.4.7.1).
1778 */
1779 i = SSL_MAX_MASTER_KEY_LENGTH;
1780 p = fakekey;
1781 }
1782
1783 s->session->master_key_length =
1784 s->method->ssl3_enc->generate_master_secret(s,
1785 s->session->master_key,
1786 p, i);
1787 OPENSSL_cleanse(p, i);
1788 } else if (alg_k & SSL_kDHE) {
1789 if (2 > n)
1790 goto truncated;
1791 n2s(p, i);
1792 if (n != i + 2) {
1793 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1794 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1795 goto err;
1796 }
1797
1798 if (n == 0L) {
1799 /* the parameters are in the cert */
1800 al = SSL_AD_HANDSHAKE_FAILURE;
1801 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1802 SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1803 goto f_err;
1804 } else {
1805 if (s->s3->tmp.dh == NULL) {
1806 al = SSL_AD_HANDSHAKE_FAILURE;
1807 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1808 SSL_R_MISSING_TMP_DH_KEY);
1809 goto f_err;
1810 } else
1811 dh_srvr = s->s3->tmp.dh;
1812 }
1813
1814 pub = BN_bin2bn(p, i, NULL);
1815 if (pub == NULL) {
1816 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1817 SSL_R_BN_LIB);
1818 goto err;
1819 }
1820
1821 i = DH_compute_key(p, pub, dh_srvr);
1822
1823 if (i <= 0) {
1824 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1825 ERR_R_DH_LIB);
1826 BN_clear_free(pub);
1827 goto err;
1828 }
1829
1830 DH_free(s->s3->tmp.dh);
1831 s->s3->tmp.dh = NULL;
1832
1833 BN_clear_free(pub);
1834 pub = NULL;
1835 s->session->master_key_length =
1836 s->method->ssl3_enc->generate_master_secret(
1837 s, s->session->master_key, p, i);
1838 OPENSSL_cleanse(p, i);
1839 } else
1840
1841 if (alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe)) {
1842 int ret = 1;
1843 int field_size = 0;
1844 const EC_KEY *tkey;
1845 const EC_GROUP *group;
1846 const BIGNUM *priv_key;
1847
1848 /* Initialize structures for server's ECDH key pair. */
1849 if ((srvr_ecdh = EC_KEY_new()) == NULL) {
1850 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1851 ERR_R_MALLOC_FAILURE);
1852 goto err;
1853 }
1854
1855 /* Let's get server private key and group information. */
1856 if (alg_k & (SSL_kECDHr|SSL_kECDHe)) {
1857 /* Use the certificate */
1858 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
1859 } else {
1860 /*
1861 * Use the ephermeral values we saved when
1862 * generating the ServerKeyExchange msg.
1863 */
1864 tkey = s->s3->tmp.ecdh;
1865 }
1866
1867 group = EC_KEY_get0_group(tkey);
1868 priv_key = EC_KEY_get0_private_key(tkey);
1869
1870 if (!EC_KEY_set_group(srvr_ecdh, group) ||
1871 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
1872 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1873 ERR_R_EC_LIB);
1874 goto err;
1875 }
1876
1877 /* Let's get client's public key */
1878 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
1879 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1880 ERR_R_MALLOC_FAILURE);
1881 goto err;
1882 }
1883
1884 if (n == 0L) {
1885 /* Client Publickey was in Client Certificate */
1886
1887 if (alg_k & SSL_kECDHE) {
1888 al = SSL_AD_HANDSHAKE_FAILURE;
1889 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1890 SSL_R_MISSING_TMP_ECDH_KEY);
1891 goto f_err;
1892 }
1893 if (((clnt_pub_pkey = X509_get_pubkey(
1894 s->session->peer)) == NULL) ||
1895 (clnt_pub_pkey->type != EVP_PKEY_EC)) {
1896 /*
1897 * XXX: For now, we do not support client
1898 * authentication using ECDH certificates
1899 * so this branch (n == 0L) of the code is
1900 * never executed. When that support is
1901 * added, we ought to ensure the key
1902 * received in the certificate is
1903 * authorized for key agreement.
1904 * ECDH_compute_key implicitly checks that
1905 * the two ECDH shares are for the same
1906 * group.
1907 */
1908 al = SSL_AD_HANDSHAKE_FAILURE;
1909 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1910 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
1911 goto f_err;
1912 }
1913
1914 if (EC_POINT_copy(clnt_ecpoint,
1915 EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec))
1916 == 0) {
1917 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1918 ERR_R_EC_LIB);
1919 goto err;
1920 }
1921 ret = 2; /* Skip certificate verify processing */
1922 } else {
1923 /*
1924 * Get client's public key from encoded point
1925 * in the ClientKeyExchange message.
1926 */
1927 if ((bn_ctx = BN_CTX_new()) == NULL) {
1928 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1929 ERR_R_MALLOC_FAILURE);
1930 goto err;
1931 }
1932
1933 /* Get encoded point length */
1934 i = *p;
1935
1936 p += 1;
1937 if (n != 1 + i) {
1938 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1939 ERR_R_EC_LIB);
1940 goto err;
1941 }
1942 if (EC_POINT_oct2point(group,
1943 clnt_ecpoint, p, i, bn_ctx) == 0) {
1944 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1945 ERR_R_EC_LIB);
1946 goto err;
1947 }
1948 /*
1949 * p is pointing to somewhere in the buffer
1950 * currently, so set it to the start.
1951 */
1952 p = (unsigned char *)s->init_buf->data;
1953 }
1954
1955 /* Compute the shared pre-master secret */
1956 field_size = EC_GROUP_get_degree(group);
1957 if (field_size <= 0) {
1958 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1959 ERR_R_ECDH_LIB);
1960 goto err;
1961 }
1962 i = ECDH_compute_key(p, (field_size + 7)/8, clnt_ecpoint,
1963 srvr_ecdh, NULL);
1964 if (i <= 0) {
1965 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1966 ERR_R_ECDH_LIB);
1967 goto err;
1968 }
1969
1970 EVP_PKEY_free(clnt_pub_pkey);
1971 EC_POINT_free(clnt_ecpoint);
1972 EC_KEY_free(srvr_ecdh);
1973 BN_CTX_free(bn_ctx);
1974 EC_KEY_free(s->s3->tmp.ecdh);
1975 s->s3->tmp.ecdh = NULL;
1976
1977
1978 /* Compute the master secret */
1979 s->session->master_key_length = s->method->ssl3_enc-> \
1980 generate_master_secret(s, s->session->master_key, p, i);
1981
1982 OPENSSL_cleanse(p, i);
1983 return (ret);
1984 } else
1985 if (alg_k & SSL_kGOST) {
1986 int ret = 0;
1987 EVP_PKEY_CTX *pkey_ctx;
1988 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
1989 unsigned char premaster_secret[32], *start;
1990 size_t outlen = 32, inlen;
1991 unsigned long alg_a;
1992 int Ttag, Tclass;
1993 long Tlen;
1994
1995 /* Get our certificate private key*/
1996 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1997 if (alg_a & SSL_aGOST01)
1998 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
1999
2000 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2001 EVP_PKEY_decrypt_init(pkey_ctx);
2002 /*
2003 * If client certificate is present and is of the same type,
2004 * maybe use it for key exchange.
2005 * Don't mind errors from EVP_PKEY_derive_set_peer, because
2006 * it is completely valid to use a client certificate for
2007 * authorization only.
2008 */
2009 client_pub_pkey = X509_get_pubkey(s->session->peer);
2010 if (client_pub_pkey) {
2011 if (EVP_PKEY_derive_set_peer(pkey_ctx,
2012 client_pub_pkey) <= 0)
2013 ERR_clear_error();
2014 }
2015 if (2 > n)
2016 goto truncated;
2017 /* Decrypt session key */
2018 if (ASN1_get_object((const unsigned char **)&p, &Tlen, &Ttag,
2019 &Tclass, n) != V_ASN1_CONSTRUCTED ||
2020 Ttag != V_ASN1_SEQUENCE || Tclass != V_ASN1_UNIVERSAL) {
2021 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2022 SSL_R_DECRYPTION_FAILED);
2023 goto gerr;
2024 }
2025 start = p;
2026 inlen = Tlen;
2027 if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen,
2028 start, inlen) <=0) {
2029 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2030 SSL_R_DECRYPTION_FAILED);
2031 goto gerr;
2032 }
2033 /* Generate master secret */
2034 s->session->master_key_length =
2035 s->method->ssl3_enc->generate_master_secret(
2036 s, s->session->master_key, premaster_secret, 32);
2037 /* Check if pubkey from client certificate was used */
2038 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1,
2039 EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2040 ret = 2;
2041 else
2042 ret = 1;
2043gerr:
2044 EVP_PKEY_free(client_pub_pkey);
2045 EVP_PKEY_CTX_free(pkey_ctx);
2046 if (ret)
2047 return (ret);
2048 else
2049 goto err;
2050 } else {
2051 al = SSL_AD_HANDSHAKE_FAILURE;
2052 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2053 SSL_R_UNKNOWN_CIPHER_TYPE);
2054 goto f_err;
2055 }
2056
2057 return (1);
2058truncated:
2059 al = SSL_AD_DECODE_ERROR;
2060 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BAD_PACKET_LENGTH);
2061f_err:
2062 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2063err:
2064 EVP_PKEY_free(clnt_pub_pkey);
2065 EC_POINT_free(clnt_ecpoint);
2066 EC_KEY_free(srvr_ecdh);
2067 BN_CTX_free(bn_ctx);
2068 return (-1);
2069}
2070
2071int
2072ssl3_get_cert_verify(SSL *s)
2073{
2074 EVP_PKEY *pkey = NULL;
2075 unsigned char *p;
2076 int al, ok, ret = 0;
2077 long n;
2078 int type = 0, i, j;
2079 X509 *peer;
2080 const EVP_MD *md = NULL;
2081 EVP_MD_CTX mctx;
2082 EVP_MD_CTX_init(&mctx);
2083
2084 n = s->method->ssl_get_message(s, SSL3_ST_SR_CERT_VRFY_A,
2085 SSL3_ST_SR_CERT_VRFY_B, -1, SSL3_RT_MAX_PLAIN_LENGTH, &ok);
2086 if (!ok)
2087 return ((int)n);
2088
2089 if (s->session->peer != NULL) {
2090 peer = s->session->peer;
2091 pkey = X509_get_pubkey(peer);
2092 type = X509_certificate_type(peer, pkey);
2093 } else {
2094 peer = NULL;
2095 pkey = NULL;
2096 }
2097
2098 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) {
2099 s->s3->tmp.reuse_message = 1;
2100 if (peer != NULL) {
2101 al = SSL_AD_UNEXPECTED_MESSAGE;
2102 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2103 SSL_R_MISSING_VERIFY_MESSAGE);
2104 goto f_err;
2105 }
2106 ret = 1;
2107 goto end;
2108 }
2109
2110 if (peer == NULL) {
2111 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2112 SSL_R_NO_CLIENT_CERT_RECEIVED);
2113 al = SSL_AD_UNEXPECTED_MESSAGE;
2114 goto f_err;
2115 }
2116
2117 if (!(type & EVP_PKT_SIGN)) {
2118 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2119 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2120 al = SSL_AD_ILLEGAL_PARAMETER;
2121 goto f_err;
2122 }
2123
2124 if (s->s3->change_cipher_spec) {
2125 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2126 SSL_R_CCS_RECEIVED_EARLY);
2127 al = SSL_AD_UNEXPECTED_MESSAGE;
2128 goto f_err;
2129 }
2130
2131 /* we now have a signature that we need to verify */
2132 p = (unsigned char *)s->init_msg;
2133 /*
2134 * Check for broken implementations of GOST ciphersuites.
2135 *
2136 * If key is GOST and n is exactly 64, it is a bare
2137 * signature without length field.
2138 */
2139 if (n == 64 && (pkey->type == NID_id_GostR3410_94 ||
2140 pkey->type == NID_id_GostR3410_2001) ) {
2141 i = 64;
2142 } else {
2143 if (SSL_USE_SIGALGS(s)) {
2144 int sigalg = tls12_get_sigid(pkey);
2145 /* Should never happen */
2146 if (sigalg == -1) {
2147 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2148 ERR_R_INTERNAL_ERROR);
2149 al = SSL_AD_INTERNAL_ERROR;
2150 goto f_err;
2151 }
2152 if (2 > n)
2153 goto truncated;
2154 /* Check key type is consistent with signature */
2155 if (sigalg != (int)p[1]) {
2156 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2157 SSL_R_WRONG_SIGNATURE_TYPE);
2158 al = SSL_AD_DECODE_ERROR;
2159 goto f_err;
2160 }
2161 md = tls12_get_hash(p[0]);
2162 if (md == NULL) {
2163 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2164 SSL_R_UNKNOWN_DIGEST);
2165 al = SSL_AD_DECODE_ERROR;
2166 goto f_err;
2167 }
2168 p += 2;
2169 n -= 2;
2170 }
2171 if (2 > n)
2172 goto truncated;
2173 n2s(p, i);
2174 n -= 2;
2175 if (i > n)
2176 goto truncated;
2177 }
2178 j = EVP_PKEY_size(pkey);
2179 if ((i > j) || (n > j) || (n <= 0)) {
2180 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2181 SSL_R_WRONG_SIGNATURE_SIZE);
2182 al = SSL_AD_DECODE_ERROR;
2183 goto f_err;
2184 }
2185
2186 if (SSL_USE_SIGALGS(s)) {
2187 long hdatalen = 0;
2188 void *hdata;
2189 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2190 if (hdatalen <= 0) {
2191 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2192 ERR_R_INTERNAL_ERROR);
2193 al = SSL_AD_INTERNAL_ERROR;
2194 goto f_err;
2195 }
2196 if (!EVP_VerifyInit_ex(&mctx, md, NULL) ||
2197 !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) {
2198 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2199 ERR_R_EVP_LIB);
2200 al = SSL_AD_INTERNAL_ERROR;
2201 goto f_err;
2202 }
2203
2204 if (EVP_VerifyFinal(&mctx, p, i, pkey) <= 0) {
2205 al = SSL_AD_DECRYPT_ERROR;
2206 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2207 SSL_R_BAD_SIGNATURE);
2208 goto f_err;
2209 }
2210 } else
2211 if (pkey->type == EVP_PKEY_RSA) {
2212 i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
2213 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i,
2214 pkey->pkey.rsa);
2215 if (i < 0) {
2216 al = SSL_AD_DECRYPT_ERROR;
2217 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2218 SSL_R_BAD_RSA_DECRYPT);
2219 goto f_err;
2220 }
2221 if (i == 0) {
2222 al = SSL_AD_DECRYPT_ERROR;
2223 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2224 SSL_R_BAD_RSA_SIGNATURE);
2225 goto f_err;
2226 }
2227 } else
2228 if (pkey->type == EVP_PKEY_DSA) {
2229 j = DSA_verify(pkey->save_type,
2230 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2231 SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa);
2232 if (j <= 0) {
2233 /* bad signature */
2234 al = SSL_AD_DECRYPT_ERROR;
2235 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2236 SSL_R_BAD_DSA_SIGNATURE);
2237 goto f_err;
2238 }
2239 } else
2240 if (pkey->type == EVP_PKEY_EC) {
2241 j = ECDSA_verify(pkey->save_type,
2242 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2243 SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec);
2244 if (j <= 0) {
2245 /* bad signature */
2246 al = SSL_AD_DECRYPT_ERROR;
2247 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2248 SSL_R_BAD_ECDSA_SIGNATURE);
2249 goto f_err;
2250 }
2251 } else
2252#ifndef OPENSSL_NO_GOST
2253 if (pkey->type == NID_id_GostR3410_94 ||
2254 pkey->type == NID_id_GostR3410_2001) {
2255 long hdatalen = 0;
2256 void *hdata;
2257 unsigned char signature[128];
2258 unsigned int siglen = sizeof(signature);
2259 int nid;
2260 EVP_PKEY_CTX *pctx;
2261
2262 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2263 if (hdatalen <= 0) {
2264 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2265 ERR_R_INTERNAL_ERROR);
2266 al = SSL_AD_INTERNAL_ERROR;
2267 goto f_err;
2268 }
2269 if (!EVP_PKEY_get_default_digest_nid(pkey, &nid) ||
2270 !(md = EVP_get_digestbynid(nid))) {
2271 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2272 ERR_R_EVP_LIB);
2273 al = SSL_AD_INTERNAL_ERROR;
2274 goto f_err;
2275 }
2276 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2277 if (!pctx) {
2278 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2279 ERR_R_EVP_LIB);
2280 al = SSL_AD_INTERNAL_ERROR;
2281 goto f_err;
2282 }
2283 if (!EVP_DigestInit_ex(&mctx, md, NULL) ||
2284 !EVP_DigestUpdate(&mctx, hdata, hdatalen) ||
2285 !EVP_DigestFinal(&mctx, signature, &siglen) ||
2286 (EVP_PKEY_verify_init(pctx) <= 0) ||
2287 (EVP_PKEY_CTX_set_signature_md(pctx, md) <= 0) ||
2288 (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_VERIFY,
2289 EVP_PKEY_CTRL_GOST_SIG_FORMAT,
2290 GOST_SIG_FORMAT_RS_LE,
2291 NULL) <= 0)) {
2292 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2293 ERR_R_EVP_LIB);
2294 al = SSL_AD_INTERNAL_ERROR;
2295 EVP_PKEY_CTX_free(pctx);
2296 goto f_err;
2297 }
2298
2299 if (EVP_PKEY_verify(pctx, p, i, signature, siglen) <= 0) {
2300 al = SSL_AD_DECRYPT_ERROR;
2301 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2302 SSL_R_BAD_SIGNATURE);
2303 EVP_PKEY_CTX_free(pctx);
2304 goto f_err;
2305 }
2306
2307 EVP_PKEY_CTX_free(pctx);
2308 } else
2309#endif
2310 {
2311 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2312 ERR_R_INTERNAL_ERROR);
2313 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
2314 goto f_err;
2315 }
2316
2317
2318 ret = 1;
2319 if (0) {
2320truncated:
2321 al = SSL_AD_DECODE_ERROR;
2322 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_PACKET_LENGTH);
2323f_err:
2324 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2325 }
2326end:
2327 if (s->s3->handshake_buffer) {
2328 BIO_free(s->s3->handshake_buffer);
2329 s->s3->handshake_buffer = NULL;
2330 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
2331 }
2332 EVP_MD_CTX_cleanup(&mctx);
2333 EVP_PKEY_free(pkey);
2334 return (ret);
2335}
2336
2337int
2338ssl3_get_client_certificate(SSL *s)
2339{
2340 CBS cbs, client_certs;
2341 int i, ok, al, ret = -1;
2342 X509 *x = NULL;
2343 long n;
2344 const unsigned char *q;
2345 STACK_OF(X509) *sk = NULL;
2346
2347 n = s->method->ssl_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B,
2348 -1, s->max_cert_list, &ok);
2349
2350 if (!ok)
2351 return ((int)n);
2352
2353 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
2354 if ((s->verify_mode & SSL_VERIFY_PEER) &&
2355 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2356 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2357 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2358 al = SSL_AD_HANDSHAKE_FAILURE;
2359 goto f_err;
2360 }
2361 /*
2362 * If tls asked for a client cert,
2363 * the client must return a 0 list.
2364 */
2365 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) {
2366 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2367 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST
2368 );
2369 al = SSL_AD_UNEXPECTED_MESSAGE;
2370 goto f_err;
2371 }
2372 s->s3->tmp.reuse_message = 1;
2373 return (1);
2374 }
2375
2376 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
2377 al = SSL_AD_UNEXPECTED_MESSAGE;
2378 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2379 SSL_R_WRONG_MESSAGE_TYPE);
2380 goto f_err;
2381 }
2382
2383 if (n < 0)
2384 goto truncated;
2385
2386 CBS_init(&cbs, s->init_msg, n);
2387
2388 if ((sk = sk_X509_new_null()) == NULL) {
2389 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2390 ERR_R_MALLOC_FAILURE);
2391 goto err;
2392 }
2393
2394 if (!CBS_get_u24_length_prefixed(&cbs, &client_certs) ||
2395 CBS_len(&cbs) != 0)
2396 goto truncated;
2397
2398 while (CBS_len(&client_certs) > 0) {
2399 CBS cert;
2400
2401 if (!CBS_get_u24_length_prefixed(&client_certs, &cert)) {
2402 al = SSL_AD_DECODE_ERROR;
2403 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2404 SSL_R_CERT_LENGTH_MISMATCH);
2405 goto f_err;
2406 }
2407
2408 q = CBS_data(&cert);
2409 x = d2i_X509(NULL, &q, CBS_len(&cert));
2410 if (x == NULL) {
2411 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2412 ERR_R_ASN1_LIB);
2413 goto err;
2414 }
2415 if (q != CBS_data(&cert) + CBS_len(&cert)) {
2416 al = SSL_AD_DECODE_ERROR;
2417 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2418 SSL_R_CERT_LENGTH_MISMATCH);
2419 goto f_err;
2420 }
2421 if (!sk_X509_push(sk, x)) {
2422 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2423 ERR_R_MALLOC_FAILURE);
2424 goto err;
2425 }
2426 x = NULL;
2427 }
2428
2429 if (sk_X509_num(sk) <= 0) {
2430 /* TLS does not mind 0 certs returned */
2431 if (s->version == SSL3_VERSION) {
2432 al = SSL_AD_HANDSHAKE_FAILURE;
2433 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2434 SSL_R_NO_CERTIFICATES_RETURNED);
2435 goto f_err;
2436 }
2437 /* Fail for TLS only if we required a certificate */
2438 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2439 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2440 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2441 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2442 al = SSL_AD_HANDSHAKE_FAILURE;
2443 goto f_err;
2444 }
2445 /* No client certificate so digest cached records */
2446 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) {
2447 al = SSL_AD_INTERNAL_ERROR;
2448 goto f_err;
2449 }
2450 } else {
2451 i = ssl_verify_cert_chain(s, sk);
2452 if (i <= 0) {
2453 al = ssl_verify_alarm_type(s->verify_result);
2454 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2455 SSL_R_NO_CERTIFICATE_RETURNED);
2456 goto f_err;
2457 }
2458 }
2459
2460 if (s->session->peer != NULL) /* This should not be needed */
2461 X509_free(s->session->peer);
2462 s->session->peer = sk_X509_shift(sk);
2463 s->session->verify_result = s->verify_result;
2464
2465 /*
2466 * With the current implementation, sess_cert will always be NULL
2467 * when we arrive here
2468 */
2469 if (s->session->sess_cert == NULL) {
2470 s->session->sess_cert = ssl_sess_cert_new();
2471 if (s->session->sess_cert == NULL) {
2472 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2473 ERR_R_MALLOC_FAILURE);
2474 goto err;
2475 }
2476 }
2477 if (s->session->sess_cert->cert_chain != NULL)
2478 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2479 s->session->sess_cert->cert_chain = sk;
2480
2481 /*
2482 * Inconsistency alert: cert_chain does *not* include the
2483 * peer's own certificate, while we do include it in s3_clnt.c
2484 */
2485
2486 sk = NULL;
2487
2488 ret = 1;
2489 if (0) {
2490truncated:
2491 al = SSL_AD_DECODE_ERROR;
2492 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2493 SSL_R_BAD_PACKET_LENGTH);
2494f_err:
2495 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2496 }
2497err:
2498 if (x != NULL)
2499 X509_free(x);
2500 if (sk != NULL)
2501 sk_X509_pop_free(sk, X509_free);
2502 return (ret);
2503}
2504
2505int
2506ssl3_send_server_certificate(SSL *s)
2507{
2508 unsigned long l;
2509 X509 *x;
2510
2511 if (s->state == SSL3_ST_SW_CERT_A) {
2512 x = ssl_get_server_send_cert(s);
2513 if (x == NULL) {
2514 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,
2515 ERR_R_INTERNAL_ERROR);
2516 return (0);
2517 }
2518
2519 l = ssl3_output_cert_chain(s, x);
2520 s->state = SSL3_ST_SW_CERT_B;
2521 s->init_num = (int)l;
2522 s->init_off = 0;
2523 }
2524
2525 /* SSL3_ST_SW_CERT_B */
2526 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2527}
2528
2529/* send a new session ticket (not necessarily for a new session) */
2530int
2531ssl3_send_newsession_ticket(SSL *s)
2532{
2533 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
2534 unsigned char *p, *senc, *macstart;
2535 const unsigned char *const_p;
2536 int len, slen_full, slen;
2537 SSL_SESSION *sess;
2538 unsigned int hlen;
2539 EVP_CIPHER_CTX ctx;
2540 HMAC_CTX hctx;
2541 SSL_CTX *tctx = s->initial_ctx;
2542 unsigned char iv[EVP_MAX_IV_LENGTH];
2543 unsigned char key_name[16];
2544
2545 /* get session encoding length */
2546 slen_full = i2d_SSL_SESSION(s->session, NULL);
2547 /*
2548 * Some length values are 16 bits, so forget it if session is
2549 * too long
2550 */
2551 if (slen_full > 0xFF00)
2552 return (-1);
2553 senc = malloc(slen_full);
2554 if (!senc)
2555 return (-1);
2556 p = senc;
2557 i2d_SSL_SESSION(s->session, &p);
2558
2559 /*
2560 * Create a fresh copy (not shared with other threads) to
2561 * clean up
2562 */
2563 const_p = senc;
2564 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
2565 if (sess == NULL) {
2566 free(senc);
2567 return (-1);
2568 }
2569
2570 /* ID is irrelevant for the ticket */
2571 sess->session_id_length = 0;
2572
2573 slen = i2d_SSL_SESSION(sess, NULL);
2574 if (slen > slen_full) {
2575 /* shouldn't ever happen */
2576 free(senc);
2577 return (-1);
2578 }
2579 p = senc;
2580 i2d_SSL_SESSION(sess, &p);
2581 SSL_SESSION_free(sess);
2582
2583 /*
2584 * Grow buffer if need be: the length calculation is as
2585 * follows 1 (size of message name) + 3 (message length
2586 * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) +
2587 * 16 (key name) + max_iv_len (iv length) +
2588 * session_length + max_enc_block_size (max encrypted session
2589 * length) + max_md_size (HMAC).
2590 */
2591 if (!BUF_MEM_grow(s->init_buf,
2592 26 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH +
2593 EVP_MAX_MD_SIZE + slen)) {
2594 free(senc);
2595 return (-1);
2596 }
2597
2598 p = (unsigned char *)s->init_buf->data;
2599 /* do the header */
2600 *(p++) = SSL3_MT_NEWSESSION_TICKET;
2601 /* Skip message length for now */
2602 p += 3;
2603 EVP_CIPHER_CTX_init(&ctx);
2604 HMAC_CTX_init(&hctx);
2605 /*
2606 * Initialize HMAC and cipher contexts. If callback present
2607 * it does all the work otherwise use generated values
2608 * from parent ctx.
2609 */
2610 if (tctx->tlsext_ticket_key_cb) {
2611 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
2612 &hctx, 1) < 0) {
2613 free(senc);
2614 EVP_CIPHER_CTX_cleanup(&ctx);
2615 return (-1);
2616 }
2617 } else {
2618 arc4random_buf(iv, 16);
2619 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2620 tctx->tlsext_tick_aes_key, iv);
2621 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2622 tlsext_tick_md(), NULL);
2623 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2624 }
2625
2626 /*
2627 * Ticket lifetime hint (advisory only):
2628 * We leave this unspecified for resumed session
2629 * (for simplicity), and guess that tickets for new
2630 * sessions will live as long as their sessions.
2631 */
2632 l2n(s->hit ? 0 : s->session->timeout, p);
2633
2634 /* Skip ticket length for now */
2635 p += 2;
2636 /* Output key name */
2637 macstart = p;
2638 memcpy(p, key_name, 16);
2639 p += 16;
2640 /* output IV */
2641 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2642 p += EVP_CIPHER_CTX_iv_length(&ctx);
2643 /* Encrypt session data */
2644 EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
2645 p += len;
2646 EVP_EncryptFinal(&ctx, p, &len);
2647 p += len;
2648 EVP_CIPHER_CTX_cleanup(&ctx);
2649
2650 HMAC_Update(&hctx, macstart, p - macstart);
2651 HMAC_Final(&hctx, p, &hlen);
2652 HMAC_CTX_cleanup(&hctx);
2653
2654 p += hlen;
2655 /* Now write out lengths: p points to end of data written */
2656 /* Total length */
2657 len = p - (unsigned char *)s->init_buf->data;
2658 p = (unsigned char *)s->init_buf->data + 1;
2659 l2n3(len - 4, p); /* Message length */
2660 p += 4;
2661 s2n(len - 10, p);
2662 /* Ticket length */
2663
2664 /* number of bytes to write */
2665 s->init_num = len;
2666 s->state = SSL3_ST_SW_SESSION_TICKET_B;
2667 s->init_off = 0;
2668 free(senc);
2669 }
2670
2671 /* SSL3_ST_SW_SESSION_TICKET_B */
2672 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2673}
2674
2675int
2676ssl3_send_cert_status(SSL *s)
2677{
2678 unsigned char *p;
2679
2680 if (s->state == SSL3_ST_SW_CERT_STATUS_A) {
2681 /*
2682 * Grow buffer if need be: the length calculation is as
2683 * follows 1 (message type) + 3 (message length) +
2684 * 1 (ocsp response type) + 3 (ocsp response length)
2685 * + (ocsp response)
2686 */
2687 if (!BUF_MEM_grow(s->init_buf, SSL3_HM_HEADER_LENGTH + 4 +
2688 s->tlsext_ocsp_resplen))
2689 return (-1);
2690
2691 p = ssl3_handshake_msg_start(s, SSL3_MT_CERTIFICATE_STATUS);
2692
2693 *(p++) = s->tlsext_status_type;
2694 l2n3(s->tlsext_ocsp_resplen, p);
2695 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
2696
2697 ssl3_handshake_msg_finish(s, s->tlsext_ocsp_resplen + 4);
2698
2699 s->state = SSL3_ST_SW_CERT_STATUS_B;
2700 }
2701
2702 /* SSL3_ST_SW_CERT_STATUS_B */
2703 return (ssl3_handshake_write(s));
2704}
2705
2706/*
2707 * ssl3_get_next_proto reads a Next Protocol Negotiation handshake message.
2708 * It sets the next_proto member in s if found
2709 */
2710int
2711ssl3_get_next_proto(SSL *s)
2712{
2713 CBS cbs, proto, padding;
2714 int ok;
2715 long n;
2716 size_t len;
2717
2718 /*
2719 * Clients cannot send a NextProtocol message if we didn't see the
2720 * extension in their ClientHello
2721 */
2722 if (!s->s3->next_proto_neg_seen) {
2723 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,
2724 SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
2725 return (-1);
2726 }
2727
2728 /* 514 maxlen is enough for the payload format below */
2729 n = s->method->ssl_get_message(s, SSL3_ST_SR_NEXT_PROTO_A,
2730 SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO, 514, &ok);
2731 if (!ok)
2732 return ((int)n);
2733
2734 /*
2735 * s->state doesn't reflect whether ChangeCipherSpec has been received
2736 * in this handshake, but s->s3->change_cipher_spec does (will be reset
2737 * by ssl3_get_finished).
2738 */
2739 if (!s->s3->change_cipher_spec) {
2740 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,
2741 SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
2742 return (-1);
2743 }
2744
2745 if (n < 2)
2746 return (0);
2747 /* The body must be > 1 bytes long */
2748
2749 CBS_init(&cbs, s->init_msg, s->init_num);
2750
2751 /*
2752 * The payload looks like:
2753 * uint8 proto_len;
2754 * uint8 proto[proto_len];
2755 * uint8 padding_len;
2756 * uint8 padding[padding_len];
2757 */
2758 if (!CBS_get_u8_length_prefixed(&cbs, &proto) ||
2759 !CBS_get_u8_length_prefixed(&cbs, &padding) ||
2760 CBS_len(&cbs) != 0)
2761 return 0;
2762
2763 /*
2764 * XXX We should not NULL it, but this matches old behavior of not
2765 * freeing before malloc.
2766 */
2767 s->next_proto_negotiated = NULL;
2768 s->next_proto_negotiated_len = 0;
2769
2770 if (!CBS_stow(&proto, &s->next_proto_negotiated, &len)) {
2771 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,
2772 ERR_R_MALLOC_FAILURE);
2773 return (0);
2774 }
2775 s->next_proto_negotiated_len = (uint8_t)len;
2776
2777 return (1);
2778}