summaryrefslogtreecommitdiff
path: root/src/lib/libssl/s3_clnt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/s3_clnt.c')
-rw-r--r--src/lib/libssl/s3_clnt.c2669
1 files changed, 0 insertions, 2669 deletions
diff --git a/src/lib/libssl/s3_clnt.c b/src/lib/libssl/s3_clnt.c
deleted file mode 100644
index b739711732..0000000000
--- a/src/lib/libssl/s3_clnt.c
+++ /dev/null
@@ -1,2669 +0,0 @@
1/* $OpenBSD: s3_clnt.c,v 1.121 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 <limits.h>
152#include <stdint.h>
153#include <stdio.h>
154
155#include "ssl_locl.h"
156
157#include <openssl/bn.h>
158#include <openssl/buffer.h>
159#include <openssl/dh.h>
160#include <openssl/evp.h>
161#include <openssl/md5.h>
162#include <openssl/objects.h>
163
164#ifndef OPENSSL_NO_ENGINE
165#include <openssl/engine.h>
166#endif
167#ifndef OPENSSL_NO_GOST
168#include <openssl/gost.h>
169#endif
170
171#include "bytestring.h"
172
173#ifdef __OpenBSD__
174#include <sys/cdefs.h>
175__warn_references(SSLv3_client_method,
176 "SSLv3_client_method() enables the use of insecure protocols");
177#endif
178
179static const SSL_METHOD *ssl3_get_client_method(int ver);
180static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b);
181
182const SSL_METHOD SSLv3_client_method_data = {
183 .version = SSL3_VERSION,
184 .ssl_new = ssl3_new,
185 .ssl_clear = ssl3_clear,
186 .ssl_free = ssl3_free,
187 .ssl_accept = ssl_undefined_function,
188 .ssl_connect = ssl3_connect,
189 .ssl_read = ssl3_read,
190 .ssl_peek = ssl3_peek,
191 .ssl_write = ssl3_write,
192 .ssl_shutdown = ssl3_shutdown,
193 .ssl_renegotiate = ssl3_renegotiate,
194 .ssl_renegotiate_check = ssl3_renegotiate_check,
195 .ssl_get_message = ssl3_get_message,
196 .ssl_read_bytes = ssl3_read_bytes,
197 .ssl_write_bytes = ssl3_write_bytes,
198 .ssl_dispatch_alert = ssl3_dispatch_alert,
199 .ssl_ctrl = ssl3_ctrl,
200 .ssl_ctx_ctrl = ssl3_ctx_ctrl,
201 .get_cipher_by_char = ssl3_get_cipher_by_char,
202 .put_cipher_by_char = ssl3_put_cipher_by_char,
203 .ssl_pending = ssl3_pending,
204 .num_ciphers = ssl3_num_ciphers,
205 .get_cipher = ssl3_get_cipher,
206 .get_ssl_method = ssl3_get_client_method,
207 .get_timeout = ssl3_default_timeout,
208 .ssl3_enc = &SSLv3_enc_data,
209 .ssl_version = ssl_undefined_void_function,
210 .ssl_callback_ctrl = ssl3_callback_ctrl,
211 .ssl_ctx_callback_ctrl = ssl3_ctx_callback_ctrl,
212};
213
214const SSL_METHOD *
215SSLv3_client_method(void)
216{
217 return &SSLv3_client_method_data;
218}
219
220static const SSL_METHOD *
221ssl3_get_client_method(int ver)
222{
223 if (ver == SSL3_VERSION)
224 return (SSLv3_client_method());
225 return (NULL);
226}
227
228int
229ssl3_connect(SSL *s)
230{
231 void (*cb)(const SSL *ssl, int type, int val) = NULL;
232 int ret = -1;
233 int new_state, state, skip = 0;
234
235 ERR_clear_error();
236 errno = 0;
237
238 if (s->info_callback != NULL)
239 cb = s->info_callback;
240 else if (s->ctx->info_callback != NULL)
241 cb = s->ctx->info_callback;
242
243 s->in_handshake++;
244 if (!SSL_in_init(s) || SSL_in_before(s))
245 SSL_clear(s);
246
247 for (;;) {
248 state = s->state;
249
250 switch (s->state) {
251 case SSL_ST_RENEGOTIATE:
252 s->renegotiate = 1;
253 s->state = SSL_ST_CONNECT;
254 s->ctx->stats.sess_connect_renegotiate++;
255 /* break */
256 case SSL_ST_BEFORE:
257 case SSL_ST_CONNECT:
258 case SSL_ST_BEFORE|SSL_ST_CONNECT:
259 case SSL_ST_OK|SSL_ST_CONNECT:
260
261 s->server = 0;
262 if (cb != NULL)
263 cb(s, SSL_CB_HANDSHAKE_START, 1);
264
265 if ((s->version & 0xff00 ) != 0x0300) {
266 SSLerr(SSL_F_SSL3_CONNECT,
267 ERR_R_INTERNAL_ERROR);
268 ret = -1;
269 goto end;
270 }
271
272 /* s->version=SSL3_VERSION; */
273 s->type = SSL_ST_CONNECT;
274
275 if (!ssl3_setup_init_buffer(s)) {
276 ret = -1;
277 goto end;
278 }
279 if (!ssl3_setup_buffers(s)) {
280 ret = -1;
281 goto end;
282 }
283 if (!ssl_init_wbio_buffer(s, 0)) {
284 ret = -1;
285 goto end;
286 }
287
288 /* don't push the buffering BIO quite yet */
289
290 if (!ssl3_init_finished_mac(s)) {
291 ret = -1;
292 goto end;
293 }
294
295 s->state = SSL3_ST_CW_CLNT_HELLO_A;
296 s->ctx->stats.sess_connect++;
297 s->init_num = 0;
298 break;
299
300 case SSL3_ST_CW_CLNT_HELLO_A:
301 case SSL3_ST_CW_CLNT_HELLO_B:
302
303 s->shutdown = 0;
304 ret = ssl3_client_hello(s);
305 if (ret <= 0)
306 goto end;
307 s->state = SSL3_ST_CR_SRVR_HELLO_A;
308 s->init_num = 0;
309
310 /* turn on buffering for the next lot of output */
311 if (s->bbio != s->wbio)
312 s->wbio = BIO_push(s->bbio, s->wbio);
313
314 break;
315
316 case SSL3_ST_CR_SRVR_HELLO_A:
317 case SSL3_ST_CR_SRVR_HELLO_B:
318 ret = ssl3_get_server_hello(s);
319 if (ret <= 0)
320 goto end;
321
322 if (s->hit) {
323 s->state = SSL3_ST_CR_FINISHED_A;
324 if (s->tlsext_ticket_expected) {
325 /* receive renewed session ticket */
326 s->state = SSL3_ST_CR_SESSION_TICKET_A;
327 }
328 } else
329 s->state = SSL3_ST_CR_CERT_A;
330 s->init_num = 0;
331 break;
332
333 case SSL3_ST_CR_CERT_A:
334 case SSL3_ST_CR_CERT_B:
335 ret = ssl3_check_finished(s);
336 if (ret <= 0)
337 goto end;
338 if (ret == 2) {
339 s->hit = 1;
340 if (s->tlsext_ticket_expected)
341 s->state = SSL3_ST_CR_SESSION_TICKET_A;
342 else
343 s->state = SSL3_ST_CR_FINISHED_A;
344 s->init_num = 0;
345 break;
346 }
347 /* Check if it is anon DH/ECDH. */
348 if (!(s->s3->tmp.new_cipher->algorithm_auth &
349 SSL_aNULL)) {
350 ret = ssl3_get_server_certificate(s);
351 if (ret <= 0)
352 goto end;
353 if (s->tlsext_status_expected)
354 s->state = SSL3_ST_CR_CERT_STATUS_A;
355 else
356 s->state = SSL3_ST_CR_KEY_EXCH_A;
357 } else {
358 skip = 1;
359 s->state = SSL3_ST_CR_KEY_EXCH_A;
360 }
361 s->init_num = 0;
362 break;
363
364 case SSL3_ST_CR_KEY_EXCH_A:
365 case SSL3_ST_CR_KEY_EXCH_B:
366 ret = ssl3_get_key_exchange(s);
367 if (ret <= 0)
368 goto end;
369 s->state = SSL3_ST_CR_CERT_REQ_A;
370 s->init_num = 0;
371
372 /*
373 * At this point we check that we have the
374 * required stuff from the server.
375 */
376 if (!ssl3_check_cert_and_algorithm(s)) {
377 ret = -1;
378 goto end;
379 }
380 break;
381
382 case SSL3_ST_CR_CERT_REQ_A:
383 case SSL3_ST_CR_CERT_REQ_B:
384 ret = ssl3_get_certificate_request(s);
385 if (ret <= 0)
386 goto end;
387 s->state = SSL3_ST_CR_SRVR_DONE_A;
388 s->init_num = 0;
389 break;
390
391 case SSL3_ST_CR_SRVR_DONE_A:
392 case SSL3_ST_CR_SRVR_DONE_B:
393 ret = ssl3_get_server_done(s);
394 if (ret <= 0)
395 goto end;
396 if (s->s3->tmp.cert_req)
397 s->state = SSL3_ST_CW_CERT_A;
398 else
399 s->state = SSL3_ST_CW_KEY_EXCH_A;
400 s->init_num = 0;
401
402 break;
403
404 case SSL3_ST_CW_CERT_A:
405 case SSL3_ST_CW_CERT_B:
406 case SSL3_ST_CW_CERT_C:
407 case SSL3_ST_CW_CERT_D:
408 ret = ssl3_send_client_certificate(s);
409 if (ret <= 0)
410 goto end;
411 s->state = SSL3_ST_CW_KEY_EXCH_A;
412 s->init_num = 0;
413 break;
414
415 case SSL3_ST_CW_KEY_EXCH_A:
416 case SSL3_ST_CW_KEY_EXCH_B:
417 ret = ssl3_send_client_key_exchange(s);
418 if (ret <= 0)
419 goto end;
420 /*
421 * EAY EAY EAY need to check for DH fix cert
422 * sent back
423 */
424 /*
425 * For TLS, cert_req is set to 2, so a cert chain
426 * of nothing is sent, but no verify packet is sent
427 */
428 /*
429 * XXX: For now, we do not support client
430 * authentication in ECDH cipher suites with
431 * ECDH (rather than ECDSA) certificates.
432 * We need to skip the certificate verify
433 * message when client's ECDH public key is sent
434 * inside the client certificate.
435 */
436 if (s->s3->tmp.cert_req == 1) {
437 s->state = SSL3_ST_CW_CERT_VRFY_A;
438 } else {
439 s->state = SSL3_ST_CW_CHANGE_A;
440 s->s3->change_cipher_spec = 0;
441 }
442 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
443 s->state = SSL3_ST_CW_CHANGE_A;
444 s->s3->change_cipher_spec = 0;
445 }
446
447 s->init_num = 0;
448 break;
449
450 case SSL3_ST_CW_CERT_VRFY_A:
451 case SSL3_ST_CW_CERT_VRFY_B:
452 ret = ssl3_send_client_verify(s);
453 if (ret <= 0)
454 goto end;
455 s->state = SSL3_ST_CW_CHANGE_A;
456 s->init_num = 0;
457 s->s3->change_cipher_spec = 0;
458 break;
459
460 case SSL3_ST_CW_CHANGE_A:
461 case SSL3_ST_CW_CHANGE_B:
462 ret = ssl3_send_change_cipher_spec(s,
463 SSL3_ST_CW_CHANGE_A, SSL3_ST_CW_CHANGE_B);
464 if (ret <= 0)
465 goto end;
466
467 if (s->s3->next_proto_neg_seen)
468 s->state = SSL3_ST_CW_NEXT_PROTO_A;
469 else
470 s->state = SSL3_ST_CW_FINISHED_A;
471 s->init_num = 0;
472
473 s->session->cipher = s->s3->tmp.new_cipher;
474 if (!s->method->ssl3_enc->setup_key_block(s)) {
475 ret = -1;
476 goto end;
477 }
478
479 if (!s->method->ssl3_enc->change_cipher_state(s,
480 SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
481 ret = -1;
482 goto end;
483 }
484
485 break;
486
487 case SSL3_ST_CW_NEXT_PROTO_A:
488 case SSL3_ST_CW_NEXT_PROTO_B:
489 ret = ssl3_send_next_proto(s);
490 if (ret <= 0)
491 goto end;
492 s->state = SSL3_ST_CW_FINISHED_A;
493 break;
494
495 case SSL3_ST_CW_FINISHED_A:
496 case SSL3_ST_CW_FINISHED_B:
497 ret = ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A,
498 SSL3_ST_CW_FINISHED_B,
499 s->method->ssl3_enc->client_finished_label,
500 s->method->ssl3_enc->client_finished_label_len);
501 if (ret <= 0)
502 goto end;
503 s->s3->flags |= SSL3_FLAGS_CCS_OK;
504 s->state = SSL3_ST_CW_FLUSH;
505
506 /* clear flags */
507 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
508 if (s->hit) {
509 s->s3->tmp.next_state = SSL_ST_OK;
510 if (s->s3->flags &
511 SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
512 s->state = SSL_ST_OK;
513 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
514 s->s3->delay_buf_pop_ret = 0;
515 }
516 } else {
517 /* Allow NewSessionTicket if ticket expected */
518 if (s->tlsext_ticket_expected)
519 s->s3->tmp.next_state =
520 SSL3_ST_CR_SESSION_TICKET_A;
521 else
522
523 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
524 }
525 s->init_num = 0;
526 break;
527
528 case SSL3_ST_CR_SESSION_TICKET_A:
529 case SSL3_ST_CR_SESSION_TICKET_B:
530 ret = ssl3_get_new_session_ticket(s);
531 if (ret <= 0)
532 goto end;
533 s->state = SSL3_ST_CR_FINISHED_A;
534 s->init_num = 0;
535 break;
536
537 case SSL3_ST_CR_CERT_STATUS_A:
538 case SSL3_ST_CR_CERT_STATUS_B:
539 ret = ssl3_get_cert_status(s);
540 if (ret <= 0)
541 goto end;
542 s->state = SSL3_ST_CR_KEY_EXCH_A;
543 s->init_num = 0;
544 break;
545
546 case SSL3_ST_CR_FINISHED_A:
547 case SSL3_ST_CR_FINISHED_B:
548 s->s3->flags |= SSL3_FLAGS_CCS_OK;
549 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
550 SSL3_ST_CR_FINISHED_B);
551 if (ret <= 0)
552 goto end;
553
554 if (s->hit)
555 s->state = SSL3_ST_CW_CHANGE_A;
556 else
557 s->state = SSL_ST_OK;
558 s->init_num = 0;
559 break;
560
561 case SSL3_ST_CW_FLUSH:
562 s->rwstate = SSL_WRITING;
563 if (BIO_flush(s->wbio) <= 0) {
564 ret = -1;
565 goto end;
566 }
567 s->rwstate = SSL_NOTHING;
568 s->state = s->s3->tmp.next_state;
569 break;
570
571 case SSL_ST_OK:
572 /* clean a few things up */
573 ssl3_cleanup_key_block(s);
574
575 if (s->init_buf != NULL) {
576 BUF_MEM_free(s->init_buf);
577 s->init_buf = NULL;
578 }
579
580 /*
581 * If we are not 'joining' the last two packets,
582 * remove the buffering now
583 */
584 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
585 ssl_free_wbio_buffer(s);
586 /* else do it later in ssl3_write */
587
588 s->init_num = 0;
589 s->renegotiate = 0;
590 s->new_session = 0;
591
592 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
593 if (s->hit)
594 s->ctx->stats.sess_hit++;
595
596 ret = 1;
597 /* s->server=0; */
598 s->handshake_func = ssl3_connect;
599 s->ctx->stats.sess_connect_good++;
600
601 if (cb != NULL)
602 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
603
604 goto end;
605 /* break; */
606
607 default:
608 SSLerr(SSL_F_SSL3_CONNECT,
609 SSL_R_UNKNOWN_STATE);
610 ret = -1;
611 goto end;
612 /* break; */
613 }
614
615 /* did we do anything */
616 if (!s->s3->tmp.reuse_message && !skip) {
617 if (s->debug) {
618 if ((ret = BIO_flush(s->wbio)) <= 0)
619 goto end;
620 }
621
622 if ((cb != NULL) && (s->state != state)) {
623 new_state = s->state;
624 s->state = state;
625 cb(s, SSL_CB_CONNECT_LOOP, 1);
626 s->state = new_state;
627 }
628 }
629 skip = 0;
630 }
631
632end:
633 s->in_handshake--;
634 if (cb != NULL)
635 cb(s, SSL_CB_CONNECT_EXIT, ret);
636
637 return (ret);
638}
639
640
641int
642ssl3_client_hello(SSL *s)
643{
644 unsigned char *bufend, *p, *d;
645 int i;
646
647 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
648 SSL_SESSION *sess = s->session;
649
650 if ((sess == NULL) ||
651 (sess->ssl_version != s->version) ||
652 (!sess->session_id_length && !sess->tlsext_tick) ||
653 (sess->not_resumable)) {
654 if (!ssl_get_new_session(s, 0))
655 goto err;
656 }
657 /* else use the pre-loaded session */
658
659 arc4random_buf(s->s3->client_random, SSL3_RANDOM_SIZE);
660
661 d = p = ssl3_handshake_msg_start(s, SSL3_MT_CLIENT_HELLO);
662
663 /*
664 * Version indicates the negotiated version: for example from
665 * an SSLv2/v3 compatible client hello). The client_version
666 * field is the maximum version we permit and it is also
667 * used in RSA encrypted premaster secrets. Some servers can
668 * choke if we initially report a higher version then
669 * renegotiate to a lower one in the premaster secret. This
670 * didn't happen with TLS 1.0 as most servers supported it
671 * but it can with TLS 1.1 or later if the server only supports
672 * 1.0.
673 *
674 * Possible scenario with previous logic:
675 * 1. Client hello indicates TLS 1.2
676 * 2. Server hello says TLS 1.0
677 * 3. RSA encrypted premaster secret uses 1.2.
678 * 4. Handhaked proceeds using TLS 1.0.
679 * 5. Server sends hello request to renegotiate.
680 * 6. Client hello indicates TLS v1.0 as we now
681 * know that is maximum server supports.
682 * 7. Server chokes on RSA encrypted premaster secret
683 * containing version 1.0.
684 *
685 * For interoperability it should be OK to always use the
686 * maximum version we support in client hello and then rely
687 * on the checking of version to ensure the servers isn't
688 * being inconsistent: for example initially negotiating with
689 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
690 * client_version in client hello and not resetting it to
691 * the negotiated version.
692 */
693 *(p++) = s->client_version >> 8;
694 *(p++) = s->client_version & 0xff;
695
696 /* Random stuff */
697 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
698 p += SSL3_RANDOM_SIZE;
699
700 /* Session ID */
701 if (s->new_session)
702 i = 0;
703 else
704 i = s->session->session_id_length;
705 *(p++) = i;
706 if (i != 0) {
707 if (i > (int)sizeof(s->session->session_id)) {
708 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
709 ERR_R_INTERNAL_ERROR);
710 goto err;
711 }
712 memcpy(p, s->session->session_id, i);
713 p += i;
714 }
715
716 /* Ciphers supported */
717 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
718 if (i == 0) {
719 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
720 SSL_R_NO_CIPHERS_AVAILABLE);
721 goto err;
722 }
723 s2n(i, p);
724 p += i;
725
726 /* add in (no) COMPRESSION */
727 *(p++) = 1;
728 *(p++) = 0; /* Add the NULL method */
729
730 /* TLS extensions*/
731 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
732 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
733 SSL_R_CLIENTHELLO_TLSEXT);
734 goto err;
735 }
736 bufend = (unsigned char *)s->init_buf->data +
737 SSL3_RT_MAX_PLAIN_LENGTH;
738 if ((p = ssl_add_clienthello_tlsext(s, p, bufend)) == NULL) {
739 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
740 ERR_R_INTERNAL_ERROR);
741 goto err;
742 }
743
744 s->state = SSL3_ST_CW_CLNT_HELLO_B;
745
746 ssl3_handshake_msg_finish(s, p - d);
747 }
748
749 /* SSL3_ST_CW_CLNT_HELLO_B */
750 return (ssl3_handshake_write(s));
751
752err:
753 return (-1);
754}
755
756int
757ssl3_get_server_hello(SSL *s)
758{
759 STACK_OF(SSL_CIPHER) *sk;
760 const SSL_CIPHER *c;
761 unsigned char *p, *q, *d;
762 int i, al, ok;
763 unsigned int j;
764 uint16_t cipher_value;
765 long n;
766 unsigned long alg_k;
767
768 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
769 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, /* ?? */ &ok);
770
771 if (!ok)
772 return ((int)n);
773
774 if (SSL_IS_DTLS(s)) {
775 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
776 if (s->d1->send_cookie == 0) {
777 s->s3->tmp.reuse_message = 1;
778 return (1);
779 } else {
780 /* Already sent a cookie. */
781 al = SSL_AD_UNEXPECTED_MESSAGE;
782 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
783 SSL_R_BAD_MESSAGE_TYPE);
784 goto f_err;
785 }
786 }
787 }
788
789 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
790 al = SSL_AD_UNEXPECTED_MESSAGE;
791 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
792 SSL_R_BAD_MESSAGE_TYPE);
793 goto f_err;
794 }
795
796 d = p = (unsigned char *)s->init_msg;
797
798 if (2 > n)
799 goto truncated;
800 if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
801 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
802 s->version = (s->version&0xff00) | p[1];
803 al = SSL_AD_PROTOCOL_VERSION;
804 goto f_err;
805 }
806 p += 2;
807
808 /* load the server hello data */
809
810 if (p + SSL3_RANDOM_SIZE + 1 - d > n)
811 goto truncated;
812
813 /* load the server random */
814 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
815 p += SSL3_RANDOM_SIZE;
816
817 /* get the session-id */
818 j = *(p++);
819
820 if ((j > sizeof s->session->session_id) ||
821 (j > SSL3_SESSION_ID_SIZE)) {
822 al = SSL_AD_ILLEGAL_PARAMETER;
823 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
824 SSL_R_SSL3_SESSION_ID_TOO_LONG);
825 goto f_err;
826 }
827
828 if (p + j + 2 - d > n)
829 goto truncated;
830
831 /* Get the cipher value. */
832 q = p + j;
833 n2s(q, cipher_value);
834
835 /*
836 * Check if we want to resume the session based on external
837 * pre-shared secret
838 */
839 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
840 SSL_CIPHER *pref_cipher = NULL;
841 s->session->master_key_length = sizeof(s->session->master_key);
842 if (s->tls_session_secret_cb(s, s->session->master_key,
843 &s->session->master_key_length, NULL, &pref_cipher,
844 s->tls_session_secret_cb_arg)) {
845 s->session->cipher = pref_cipher ? pref_cipher :
846 ssl3_get_cipher_by_value(cipher_value);
847 s->s3->flags |= SSL3_FLAGS_CCS_OK;
848 }
849 }
850
851 if (j != 0 && j == s->session->session_id_length &&
852 timingsafe_memcmp(p, s->session->session_id, j) == 0) {
853 if (s->sid_ctx_length != s->session->sid_ctx_length ||
854 timingsafe_memcmp(s->session->sid_ctx,
855 s->sid_ctx, s->sid_ctx_length) != 0) {
856 /* actually a client application bug */
857 al = SSL_AD_ILLEGAL_PARAMETER;
858 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
859 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
860 goto f_err;
861 }
862 s->s3->flags |= SSL3_FLAGS_CCS_OK;
863 s->hit = 1;
864 } else {
865 /* a miss or crap from the other end */
866
867 /* If we were trying for session-id reuse, make a new
868 * SSL_SESSION so we don't stuff up other people */
869 s->hit = 0;
870 if (s->session->session_id_length > 0) {
871 if (!ssl_get_new_session(s, 0)) {
872 al = SSL_AD_INTERNAL_ERROR;
873 goto f_err;
874 }
875 }
876 s->session->session_id_length = j;
877 memcpy(s->session->session_id, p, j); /* j could be 0 */
878 }
879 p += j;
880
881 if ((c = ssl3_get_cipher_by_value(cipher_value)) == NULL) {
882 /* unknown cipher */
883 al = SSL_AD_ILLEGAL_PARAMETER;
884 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
885 SSL_R_UNKNOWN_CIPHER_RETURNED);
886 goto f_err;
887 }
888
889 /* TLS v1.2 only ciphersuites require v1.2 or later */
890 if ((c->algorithm_ssl & SSL_TLSV1_2) &&
891 (TLS1_get_version(s) < TLS1_2_VERSION)) {
892 al = SSL_AD_ILLEGAL_PARAMETER;
893 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
894 SSL_R_WRONG_CIPHER_RETURNED);
895 goto f_err;
896 }
897 p += SSL3_CIPHER_VALUE_SIZE;
898
899 sk = ssl_get_ciphers_by_id(s);
900 i = sk_SSL_CIPHER_find(sk, c);
901 if (i < 0) {
902 /* we did not say we would use this cipher */
903 al = SSL_AD_ILLEGAL_PARAMETER;
904 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
905 SSL_R_WRONG_CIPHER_RETURNED);
906 goto f_err;
907 }
908
909 /*
910 * Depending on the session caching (internal/external), the cipher
911 * and/or cipher_id values may not be set. Make sure that
912 * cipher_id is set and use it for comparison.
913 */
914 if (s->session->cipher)
915 s->session->cipher_id = s->session->cipher->id;
916 if (s->hit && (s->session->cipher_id != c->id)) {
917 al = SSL_AD_ILLEGAL_PARAMETER;
918 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
919 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
920 goto f_err;
921 }
922 s->s3->tmp.new_cipher = c;
923 /*
924 * Don't digest cached records if no sigalgs: we may need them for
925 * client authentication.
926 */
927 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
928 if (!(SSL_USE_SIGALGS(s) || (alg_k & SSL_kGOST)) &&
929 !ssl3_digest_cached_records(s)) {
930 al = SSL_AD_INTERNAL_ERROR;
931 goto f_err;
932 }
933 /* lets get the compression algorithm */
934 /* COMPRESSION */
935 if (p + 1 - d > n)
936 goto truncated;
937 if (*(p++) != 0) {
938 al = SSL_AD_ILLEGAL_PARAMETER;
939 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
940 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
941 goto f_err;
942 }
943
944 /* TLS extensions*/
945 if (s->version >= SSL3_VERSION) {
946 if (!ssl_parse_serverhello_tlsext(s, &p, d, n, &al)) {
947 /* 'al' set by ssl_parse_serverhello_tlsext */
948 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
949 SSL_R_PARSE_TLSEXT);
950 goto f_err;
951
952 }
953 if (ssl_check_serverhello_tlsext(s) <= 0) {
954 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
955 SSL_R_SERVERHELLO_TLSEXT);
956 goto err;
957 }
958 }
959
960 if (p != d + n)
961 goto truncated;
962
963 return (1);
964
965truncated:
966 /* wrong packet length */
967 al = SSL_AD_DECODE_ERROR;
968 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
969f_err:
970 ssl3_send_alert(s, SSL3_AL_FATAL, al);
971err:
972 return (-1);
973}
974
975int
976ssl3_get_server_certificate(SSL *s)
977{
978 int al, i, ok, ret = -1;
979 long n;
980 CBS cbs, cert_list;
981 X509 *x = NULL;
982 const unsigned char *q;
983 STACK_OF(X509) *sk = NULL;
984 SESS_CERT *sc;
985 EVP_PKEY *pkey = NULL;
986
987 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A,
988 SSL3_ST_CR_CERT_B, -1, s->max_cert_list, &ok);
989
990 if (!ok)
991 return ((int)n);
992
993 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
994 s->s3->tmp.reuse_message = 1;
995 return (1);
996 }
997
998 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
999 al = SSL_AD_UNEXPECTED_MESSAGE;
1000 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1001 SSL_R_BAD_MESSAGE_TYPE);
1002 goto f_err;
1003 }
1004
1005
1006 if ((sk = sk_X509_new_null()) == NULL) {
1007 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1008 ERR_R_MALLOC_FAILURE);
1009 goto err;
1010 }
1011
1012 if (n < 0)
1013 goto truncated;
1014
1015 CBS_init(&cbs, s->init_msg, n);
1016 if (CBS_len(&cbs) < 3)
1017 goto truncated;
1018
1019 if (!CBS_get_u24_length_prefixed(&cbs, &cert_list) ||
1020 CBS_len(&cbs) != 0) {
1021 al = SSL_AD_DECODE_ERROR;
1022 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1023 SSL_R_LENGTH_MISMATCH);
1024 goto f_err;
1025 }
1026
1027 while (CBS_len(&cert_list) > 0) {
1028 CBS cert;
1029
1030 if (CBS_len(&cert_list) < 3)
1031 goto truncated;
1032 if (!CBS_get_u24_length_prefixed(&cert_list, &cert)) {
1033 al = SSL_AD_DECODE_ERROR;
1034 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1035 SSL_R_CERT_LENGTH_MISMATCH);
1036 goto f_err;
1037 }
1038
1039 q = CBS_data(&cert);
1040 x = d2i_X509(NULL, &q, CBS_len(&cert));
1041 if (x == NULL) {
1042 al = SSL_AD_BAD_CERTIFICATE;
1043 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1044 ERR_R_ASN1_LIB);
1045 goto f_err;
1046 }
1047 if (q != CBS_data(&cert) + CBS_len(&cert)) {
1048 al = SSL_AD_DECODE_ERROR;
1049 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1050 SSL_R_CERT_LENGTH_MISMATCH);
1051 goto f_err;
1052 }
1053 if (!sk_X509_push(sk, x)) {
1054 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1055 ERR_R_MALLOC_FAILURE);
1056 goto err;
1057 }
1058 x = NULL;
1059 }
1060
1061 i = ssl_verify_cert_chain(s, sk);
1062 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)) {
1063 al = ssl_verify_alarm_type(s->verify_result);
1064 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1065 SSL_R_CERTIFICATE_VERIFY_FAILED);
1066 goto f_err;
1067
1068 }
1069 ERR_clear_error(); /* but we keep s->verify_result */
1070
1071 sc = ssl_sess_cert_new();
1072 if (sc == NULL)
1073 goto err;
1074 if (s->session->sess_cert)
1075 ssl_sess_cert_free(s->session->sess_cert);
1076 s->session->sess_cert = sc;
1077
1078 sc->cert_chain = sk;
1079 /*
1080 * Inconsistency alert: cert_chain does include the peer's
1081 * certificate, which we don't include in s3_srvr.c
1082 */
1083 x = sk_X509_value(sk, 0);
1084 sk = NULL;
1085 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1086
1087 pkey = X509_get_pubkey(x);
1088
1089 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1090 x = NULL;
1091 al = SSL3_AL_FATAL;
1092 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1093 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1094 goto f_err;
1095 }
1096
1097 i = ssl_cert_type(x, pkey);
1098 if (i < 0) {
1099 x = NULL;
1100 al = SSL3_AL_FATAL;
1101 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1102 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1103 goto f_err;
1104 }
1105
1106 sc->peer_cert_type = i;
1107 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1108 /*
1109 * Why would the following ever happen?
1110 * We just created sc a couple of lines ago.
1111 */
1112 if (sc->peer_pkeys[i].x509 != NULL)
1113 X509_free(sc->peer_pkeys[i].x509);
1114 sc->peer_pkeys[i].x509 = x;
1115 sc->peer_key = &(sc->peer_pkeys[i]);
1116
1117 if (s->session->peer != NULL)
1118 X509_free(s->session->peer);
1119 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1120 s->session->peer = x;
1121 s->session->verify_result = s->verify_result;
1122
1123 x = NULL;
1124 ret = 1;
1125
1126 if (0) {
1127truncated:
1128 /* wrong packet length */
1129 al = SSL_AD_DECODE_ERROR;
1130 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1131 SSL_R_BAD_PACKET_LENGTH);
1132f_err:
1133 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1134 }
1135err:
1136 EVP_PKEY_free(pkey);
1137 X509_free(x);
1138 sk_X509_pop_free(sk, X509_free);
1139 return (ret);
1140}
1141
1142int
1143ssl3_get_key_exchange(SSL *s)
1144{
1145 unsigned char *q, md_buf[EVP_MAX_MD_SIZE*2];
1146 EVP_MD_CTX md_ctx;
1147 unsigned char *param, *p;
1148 int al, i, j, param_len, ok;
1149 long n, alg_k, alg_a;
1150 EVP_PKEY *pkey = NULL;
1151 const EVP_MD *md = NULL;
1152 RSA *rsa = NULL;
1153 DH *dh = NULL;
1154 EC_KEY *ecdh = NULL;
1155 BN_CTX *bn_ctx = NULL;
1156 EC_POINT *srvr_ecpoint = NULL;
1157 int curve_nid = 0;
1158 int encoded_pt_len = 0;
1159
1160 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1161 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1162
1163 /*
1164 * Use same message size as in ssl3_get_certificate_request()
1165 * as ServerKeyExchange message may be skipped.
1166 */
1167 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1168 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list, &ok);
1169 if (!ok)
1170 return ((int)n);
1171
1172 EVP_MD_CTX_init(&md_ctx);
1173
1174 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1175 /*
1176 * Do not skip server key exchange if this cipher suite uses
1177 * ephemeral keys.
1178 */
1179 if (alg_k & (SSL_kDHE|SSL_kECDHE)) {
1180 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1181 SSL_R_UNEXPECTED_MESSAGE);
1182 al = SSL_AD_UNEXPECTED_MESSAGE;
1183 goto f_err;
1184 }
1185
1186 s->s3->tmp.reuse_message = 1;
1187 EVP_MD_CTX_cleanup(&md_ctx);
1188 return (1);
1189 }
1190
1191 if (s->session->sess_cert != NULL) {
1192 DH_free(s->session->sess_cert->peer_dh_tmp);
1193 s->session->sess_cert->peer_dh_tmp = NULL;
1194
1195 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1196 s->session->sess_cert->peer_ecdh_tmp = NULL;
1197 } else {
1198 s->session->sess_cert = ssl_sess_cert_new();
1199 if (s->session->sess_cert == NULL)
1200 goto err;
1201 }
1202
1203 param = p = (unsigned char *)s->init_msg;
1204 param_len = 0;
1205
1206 if (alg_k & SSL_kDHE) {
1207 if ((dh = DH_new()) == NULL) {
1208 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1209 ERR_R_DH_LIB);
1210 goto err;
1211 }
1212 if (2 > n)
1213 goto truncated;
1214 n2s(p, i);
1215 param_len = i + 2;
1216 if (param_len > n) {
1217 al = SSL_AD_DECODE_ERROR;
1218 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1219 SSL_R_BAD_DH_P_LENGTH);
1220 goto f_err;
1221 }
1222 if (!(dh->p = BN_bin2bn(p, i, NULL))) {
1223 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1224 ERR_R_BN_LIB);
1225 goto err;
1226 }
1227 p += i;
1228
1229 if (param_len + 2 > n)
1230 goto truncated;
1231 n2s(p, i);
1232 param_len += i + 2;
1233 if (param_len > n) {
1234 al = SSL_AD_DECODE_ERROR;
1235 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1236 SSL_R_BAD_DH_G_LENGTH);
1237 goto f_err;
1238 }
1239 if (!(dh->g = BN_bin2bn(p, i, NULL))) {
1240 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1241 ERR_R_BN_LIB);
1242 goto err;
1243 }
1244 p += i;
1245
1246 if (param_len + 2 > n)
1247 goto truncated;
1248 n2s(p, i);
1249 param_len += i + 2;
1250 if (param_len > n) {
1251 al = SSL_AD_DECODE_ERROR;
1252 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1253 SSL_R_BAD_DH_PUB_KEY_LENGTH);
1254 goto f_err;
1255 }
1256 if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {
1257 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1258 ERR_R_BN_LIB);
1259 goto err;
1260 }
1261 p += i;
1262 n -= param_len;
1263
1264 /*
1265 * Check the strength of the DH key just constructed.
1266 * Discard keys weaker than 1024 bits.
1267 */
1268
1269 if (DH_size(dh) < 1024 / 8) {
1270 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1271 SSL_R_BAD_DH_P_LENGTH);
1272 goto err;
1273 }
1274
1275 if (alg_a & SSL_aRSA)
1276 pkey = X509_get_pubkey(
1277 s->session->sess_cert->peer_pkeys[
1278 SSL_PKEY_RSA_ENC].x509);
1279 else if (alg_a & SSL_aDSS)
1280 pkey = X509_get_pubkey(
1281 s->session->sess_cert->peer_pkeys[
1282 SSL_PKEY_DSA_SIGN].x509);
1283 /* else anonymous DH, so no certificate or pkey. */
1284
1285 s->session->sess_cert->peer_dh_tmp = dh;
1286 dh = NULL;
1287 } else if (alg_k & SSL_kECDHE) {
1288 const EC_GROUP *group;
1289 EC_GROUP *ngroup;
1290
1291 if ((ecdh = EC_KEY_new()) == NULL) {
1292 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1293 ERR_R_MALLOC_FAILURE);
1294 goto err;
1295 }
1296
1297 /*
1298 * Extract elliptic curve parameters and the
1299 * server's ephemeral ECDH public key.
1300 * Keep accumulating lengths of various components in
1301 * param_len and make sure it never exceeds n.
1302 */
1303
1304 /*
1305 * XXX: For now we only support named (not generic) curves
1306 * and the ECParameters in this case is just three bytes.
1307 */
1308 param_len = 3;
1309 if (param_len > n) {
1310 al = SSL_AD_DECODE_ERROR;
1311 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1312 SSL_R_LENGTH_TOO_SHORT);
1313 goto f_err;
1314 }
1315
1316 /*
1317 * Check curve is one of our preferences, if not server has
1318 * sent an invalid curve.
1319 */
1320 if (tls1_check_curve(s, p, param_len) != 1) {
1321 al = SSL_AD_DECODE_ERROR;
1322 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1323 goto f_err;
1324 }
1325
1326 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1327 al = SSL_AD_INTERNAL_ERROR;
1328 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1329 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1330 goto f_err;
1331 }
1332
1333 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1334 if (ngroup == NULL) {
1335 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1336 ERR_R_EC_LIB);
1337 goto err;
1338 }
1339 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1340 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1341 ERR_R_EC_LIB);
1342 goto err;
1343 }
1344 EC_GROUP_free(ngroup);
1345
1346 group = EC_KEY_get0_group(ecdh);
1347
1348 p += 3;
1349
1350 /* Next, get the encoded ECPoint */
1351 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1352 ((bn_ctx = BN_CTX_new()) == NULL)) {
1353 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1354 ERR_R_MALLOC_FAILURE);
1355 goto err;
1356 }
1357
1358 if (param_len + 1 > n)
1359 goto truncated;
1360 encoded_pt_len = *p;
1361 /* length of encoded point */
1362 p += 1;
1363 param_len += (1 + encoded_pt_len);
1364 if ((param_len > n) || (EC_POINT_oct2point(group, srvr_ecpoint,
1365 p, encoded_pt_len, bn_ctx) == 0)) {
1366 al = SSL_AD_DECODE_ERROR;
1367 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1368 SSL_R_BAD_ECPOINT);
1369 goto f_err;
1370 }
1371
1372 n -= param_len;
1373 p += encoded_pt_len;
1374
1375 /*
1376 * The ECC/TLS specification does not mention the use
1377 * of DSA to sign ECParameters in the server key
1378 * exchange message. We do support RSA and ECDSA.
1379 */
1380 if (alg_a & SSL_aRSA)
1381 pkey = X509_get_pubkey(
1382 s->session->sess_cert->peer_pkeys[
1383 SSL_PKEY_RSA_ENC].x509);
1384 else if (alg_a & SSL_aECDSA)
1385 pkey = X509_get_pubkey(
1386 s->session->sess_cert->peer_pkeys[
1387 SSL_PKEY_ECC].x509);
1388 /* Else anonymous ECDH, so no certificate or pkey. */
1389 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1390 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1391 ecdh = NULL;
1392 BN_CTX_free(bn_ctx);
1393 bn_ctx = NULL;
1394 EC_POINT_free(srvr_ecpoint);
1395 srvr_ecpoint = NULL;
1396 } else if (alg_k) {
1397 al = SSL_AD_UNEXPECTED_MESSAGE;
1398 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1399 SSL_R_UNEXPECTED_MESSAGE);
1400 goto f_err;
1401 }
1402
1403 /* p points to the next byte, there are 'n' bytes left */
1404
1405 /* if it was signed, check the signature */
1406 if (pkey != NULL) {
1407 if (SSL_USE_SIGALGS(s)) {
1408 int sigalg = tls12_get_sigid(pkey);
1409 /* Should never happen */
1410 if (sigalg == -1) {
1411 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1412 ERR_R_INTERNAL_ERROR);
1413 goto err;
1414 }
1415 /*
1416 * Check key type is consistent
1417 * with signature
1418 */
1419 if (2 > n)
1420 goto truncated;
1421 if (sigalg != (int)p[1]) {
1422 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1423 SSL_R_WRONG_SIGNATURE_TYPE);
1424 al = SSL_AD_DECODE_ERROR;
1425 goto f_err;
1426 }
1427 md = tls12_get_hash(p[0]);
1428 if (md == NULL) {
1429 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1430 SSL_R_UNKNOWN_DIGEST);
1431 al = SSL_AD_DECODE_ERROR;
1432 goto f_err;
1433 }
1434 p += 2;
1435 n -= 2;
1436 } else
1437 md = EVP_sha1();
1438
1439 if (2 > n)
1440 goto truncated;
1441 n2s(p, i);
1442 n -= 2;
1443 j = EVP_PKEY_size(pkey);
1444
1445 if (i != n || n > j) {
1446 /* wrong packet length */
1447 al = SSL_AD_DECODE_ERROR;
1448 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1449 SSL_R_WRONG_SIGNATURE_LENGTH);
1450 goto f_err;
1451 }
1452
1453 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1454 int num;
1455
1456 j = 0;
1457 q = md_buf;
1458 for (num = 2; num > 0; num--) {
1459 if (!EVP_DigestInit_ex(&md_ctx,
1460 (num == 2) ? s->ctx->md5 : s->ctx->sha1,
1461 NULL)) {
1462 al = SSL_AD_INTERNAL_ERROR;
1463 goto f_err;
1464 }
1465 EVP_DigestUpdate(&md_ctx,
1466 s->s3->client_random,
1467 SSL3_RANDOM_SIZE);
1468 EVP_DigestUpdate(&md_ctx,
1469 s->s3->server_random,
1470 SSL3_RANDOM_SIZE);
1471 EVP_DigestUpdate(&md_ctx, param, param_len);
1472 EVP_DigestFinal_ex(&md_ctx, q,
1473 (unsigned int *)&i);
1474 q += i;
1475 j += i;
1476 }
1477 i = RSA_verify(NID_md5_sha1, md_buf, j,
1478 p, n, pkey->pkey.rsa);
1479 if (i < 0) {
1480 al = SSL_AD_DECRYPT_ERROR;
1481 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1482 SSL_R_BAD_RSA_DECRYPT);
1483 goto f_err;
1484 }
1485 if (i == 0) {
1486 /* bad signature */
1487 al = SSL_AD_DECRYPT_ERROR;
1488 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1489 SSL_R_BAD_SIGNATURE);
1490 goto f_err;
1491 }
1492 } else {
1493 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1494 EVP_VerifyUpdate(&md_ctx, s->s3->client_random,
1495 SSL3_RANDOM_SIZE);
1496 EVP_VerifyUpdate(&md_ctx, s->s3->server_random,
1497 SSL3_RANDOM_SIZE);
1498 EVP_VerifyUpdate(&md_ctx, param, param_len);
1499 if (EVP_VerifyFinal(&md_ctx, p,(int)n, pkey) <= 0) {
1500 /* bad signature */
1501 al = SSL_AD_DECRYPT_ERROR;
1502 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1503 SSL_R_BAD_SIGNATURE);
1504 goto f_err;
1505 }
1506 }
1507 } else {
1508 /* aNULL does not need public keys. */
1509 if (!(alg_a & SSL_aNULL)) {
1510 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1511 ERR_R_INTERNAL_ERROR);
1512 goto err;
1513 }
1514 /* still data left over */
1515 if (n != 0) {
1516 al = SSL_AD_DECODE_ERROR;
1517 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1518 SSL_R_EXTRA_DATA_IN_MESSAGE);
1519 goto f_err;
1520 }
1521 }
1522 EVP_PKEY_free(pkey);
1523 EVP_MD_CTX_cleanup(&md_ctx);
1524 return (1);
1525truncated:
1526 /* wrong packet length */
1527 al = SSL_AD_DECODE_ERROR;
1528 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_PACKET_LENGTH);
1529f_err:
1530 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1531err:
1532 EVP_PKEY_free(pkey);
1533 RSA_free(rsa);
1534 DH_free(dh);
1535 BN_CTX_free(bn_ctx);
1536 EC_POINT_free(srvr_ecpoint);
1537 EC_KEY_free(ecdh);
1538 EVP_MD_CTX_cleanup(&md_ctx);
1539 return (-1);
1540}
1541
1542int
1543ssl3_get_certificate_request(SSL *s)
1544{
1545 int ok, ret = 0;
1546 long n;
1547 uint8_t ctype_num;
1548 CBS cert_request, ctypes, rdn_list;
1549 X509_NAME *xn = NULL;
1550 const unsigned char *q;
1551 STACK_OF(X509_NAME) *ca_sk = NULL;
1552
1553 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1554 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list, &ok);
1555
1556 if (!ok)
1557 return ((int)n);
1558
1559 s->s3->tmp.cert_req = 0;
1560
1561 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1562 s->s3->tmp.reuse_message = 1;
1563 /*
1564 * If we get here we don't need any cached handshake records
1565 * as we wont be doing client auth.
1566 */
1567 if (s->s3->handshake_buffer) {
1568 if (!ssl3_digest_cached_records(s))
1569 goto err;
1570 }
1571 return (1);
1572 }
1573
1574 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1575 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1576 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1577 SSL_R_WRONG_MESSAGE_TYPE);
1578 goto err;
1579 }
1580
1581 /* TLS does not like anon-DH with client cert */
1582 if (s->version > SSL3_VERSION) {
1583 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
1584 ssl3_send_alert(s, SSL3_AL_FATAL,
1585 SSL_AD_UNEXPECTED_MESSAGE);
1586 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1587 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1588 goto err;
1589 }
1590 }
1591
1592 if (n < 0)
1593 goto truncated;
1594 CBS_init(&cert_request, s->init_msg, n);
1595
1596 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1597 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1598 ERR_R_MALLOC_FAILURE);
1599 goto err;
1600 }
1601
1602 /* get the certificate types */
1603 if (!CBS_get_u8(&cert_request, &ctype_num))
1604 goto truncated;
1605
1606 if (ctype_num > SSL3_CT_NUMBER)
1607 ctype_num = SSL3_CT_NUMBER;
1608 if (!CBS_get_bytes(&cert_request, &ctypes, ctype_num) ||
1609 !CBS_write_bytes(&ctypes, s->s3->tmp.ctype,
1610 sizeof(s->s3->tmp.ctype), NULL)) {
1611 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1612 SSL_R_DATA_LENGTH_TOO_LONG);
1613 goto err;
1614 }
1615
1616 if (SSL_USE_SIGALGS(s)) {
1617 CBS sigalgs;
1618
1619 if (CBS_len(&cert_request) < 2) {
1620 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1621 SSL_R_DATA_LENGTH_TOO_LONG);
1622 goto err;
1623 }
1624
1625 /* Check we have enough room for signature algorithms and
1626 * following length value.
1627 */
1628 if (!CBS_get_u16_length_prefixed(&cert_request, &sigalgs)) {
1629 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1630 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1631 SSL_R_DATA_LENGTH_TOO_LONG);
1632 goto err;
1633 }
1634 if ((CBS_len(&sigalgs) & 1) ||
1635 !tls1_process_sigalgs(s, CBS_data(&sigalgs),
1636 CBS_len(&sigalgs))) {
1637 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1638 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1639 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1640 goto err;
1641 }
1642 }
1643
1644 /* get the CA RDNs */
1645 if (CBS_len(&cert_request) < 2) {
1646 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1647 SSL_R_DATA_LENGTH_TOO_LONG);
1648 goto err;
1649 }
1650
1651 if (!CBS_get_u16_length_prefixed(&cert_request, &rdn_list) ||
1652 CBS_len(&cert_request) != 0) {
1653 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1654 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1655 SSL_R_LENGTH_MISMATCH);
1656 goto err;
1657 }
1658
1659 while (CBS_len(&rdn_list) > 0) {
1660 CBS rdn;
1661
1662 if (CBS_len(&rdn_list) < 2) {
1663 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1664 SSL_R_DATA_LENGTH_TOO_LONG);
1665 goto err;
1666 }
1667
1668 if (!CBS_get_u16_length_prefixed(&rdn_list, &rdn)) {
1669 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1670 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1671 SSL_R_CA_DN_TOO_LONG);
1672 goto err;
1673 }
1674
1675 q = CBS_data(&rdn);
1676 if ((xn = d2i_X509_NAME(NULL, &q, CBS_len(&rdn))) == NULL) {
1677 ssl3_send_alert(s, SSL3_AL_FATAL,
1678 SSL_AD_DECODE_ERROR);
1679 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1680 ERR_R_ASN1_LIB);
1681 goto err;
1682 }
1683
1684 if (q != CBS_data(&rdn) + CBS_len(&rdn)) {
1685 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1686 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1687 SSL_R_CA_DN_LENGTH_MISMATCH);
1688 goto err;
1689 }
1690 if (!sk_X509_NAME_push(ca_sk, xn)) {
1691 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1692 ERR_R_MALLOC_FAILURE);
1693 goto err;
1694 }
1695 }
1696
1697 /* we should setup a certificate to return.... */
1698 s->s3->tmp.cert_req = 1;
1699 s->s3->tmp.ctype_num = ctype_num;
1700 if (s->s3->tmp.ca_names != NULL)
1701 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1702 s->s3->tmp.ca_names = ca_sk;
1703 ca_sk = NULL;
1704
1705 ret = 1;
1706 if (0) {
1707truncated:
1708 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1709 SSL_R_BAD_PACKET_LENGTH);
1710 }
1711err:
1712 if (ca_sk != NULL)
1713 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1714 return (ret);
1715}
1716
1717static int
1718ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
1719{
1720 return (X509_NAME_cmp(*a, *b));
1721}
1722
1723int
1724ssl3_get_new_session_ticket(SSL *s)
1725{
1726 int ok, al, ret = 0;
1727 uint32_t lifetime_hint;
1728 long n;
1729 CBS cbs, session_ticket;
1730
1731 n = s->method->ssl_get_message(s, SSL3_ST_CR_SESSION_TICKET_A,
1732 SSL3_ST_CR_SESSION_TICKET_B, -1, 16384, &ok);
1733 if (!ok)
1734 return ((int)n);
1735
1736 if (s->s3->tmp.message_type == SSL3_MT_FINISHED) {
1737 s->s3->tmp.reuse_message = 1;
1738 return (1);
1739 }
1740 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET) {
1741 al = SSL_AD_UNEXPECTED_MESSAGE;
1742 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,
1743 SSL_R_BAD_MESSAGE_TYPE);
1744 goto f_err;
1745 }
1746
1747 if (n < 0) {
1748 al = SSL_AD_DECODE_ERROR;
1749 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,
1750 SSL_R_LENGTH_MISMATCH);
1751 goto f_err;
1752 }
1753
1754 CBS_init(&cbs, s->init_msg, n);
1755 if (!CBS_get_u32(&cbs, &lifetime_hint) ||
1756#if UINT32_MAX > LONG_MAX
1757 lifetime_hint > LONG_MAX ||
1758#endif
1759 !CBS_get_u16_length_prefixed(&cbs, &session_ticket) ||
1760 CBS_len(&cbs) != 0) {
1761 al = SSL_AD_DECODE_ERROR;
1762 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,
1763 SSL_R_LENGTH_MISMATCH);
1764 goto f_err;
1765 }
1766 s->session->tlsext_tick_lifetime_hint = (long)lifetime_hint;
1767
1768 if (!CBS_stow(&session_ticket, &s->session->tlsext_tick,
1769 &s->session->tlsext_ticklen)) {
1770 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,
1771 ERR_R_MALLOC_FAILURE);
1772 goto err;
1773 }
1774
1775 /*
1776 * There are two ways to detect a resumed ticket sesion.
1777 * One is to set an appropriate session ID and then the server
1778 * must return a match in ServerHello. This allows the normal
1779 * client session ID matching to work and we know much
1780 * earlier that the ticket has been accepted.
1781 *
1782 * The other way is to set zero length session ID when the
1783 * ticket is presented and rely on the handshake to determine
1784 * session resumption.
1785 *
1786 * We choose the former approach because this fits in with
1787 * assumptions elsewhere in OpenSSL. The session ID is set
1788 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1789 * ticket.
1790 */
1791 EVP_Digest(CBS_data(&session_ticket), CBS_len(&session_ticket),
1792 s->session->session_id, &s->session->session_id_length,
1793 EVP_sha256(), NULL);
1794 ret = 1;
1795 return (ret);
1796f_err:
1797 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1798err:
1799 return (-1);
1800}
1801
1802int
1803ssl3_get_cert_status(SSL *s)
1804{
1805 CBS cert_status, response;
1806 size_t stow_len;
1807 int ok, al;
1808 long n;
1809 uint8_t status_type;
1810
1811 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_STATUS_A,
1812 SSL3_ST_CR_CERT_STATUS_B, SSL3_MT_CERTIFICATE_STATUS,
1813 16384, &ok);
1814
1815 if (!ok)
1816 return ((int)n);
1817
1818 if (n < 0) {
1819 /* need at least status type + length */
1820 al = SSL_AD_DECODE_ERROR;
1821 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,
1822 SSL_R_LENGTH_MISMATCH);
1823 goto f_err;
1824 }
1825
1826 CBS_init(&cert_status, s->init_msg, n);
1827 if (!CBS_get_u8(&cert_status, &status_type) ||
1828 CBS_len(&cert_status) < 3) {
1829 /* need at least status type + length */
1830 al = SSL_AD_DECODE_ERROR;
1831 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,
1832 SSL_R_LENGTH_MISMATCH);
1833 goto f_err;
1834 }
1835
1836 if (status_type != TLSEXT_STATUSTYPE_ocsp) {
1837 al = SSL_AD_DECODE_ERROR;
1838 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,
1839 SSL_R_UNSUPPORTED_STATUS_TYPE);
1840 goto f_err;
1841 }
1842
1843 if (!CBS_get_u24_length_prefixed(&cert_status, &response) ||
1844 CBS_len(&cert_status) != 0) {
1845 al = SSL_AD_DECODE_ERROR;
1846 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,
1847 SSL_R_LENGTH_MISMATCH);
1848 goto f_err;
1849 }
1850
1851 if (!CBS_stow(&response, &s->tlsext_ocsp_resp,
1852 &stow_len) || stow_len > INT_MAX) {
1853 s->tlsext_ocsp_resplen = 0;
1854 al = SSL_AD_INTERNAL_ERROR;
1855 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,
1856 ERR_R_MALLOC_FAILURE);
1857 goto f_err;
1858 }
1859 s->tlsext_ocsp_resplen = (int)stow_len;
1860
1861 if (s->ctx->tlsext_status_cb) {
1862 int ret;
1863 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1864 if (ret == 0) {
1865 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1866 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,
1867 SSL_R_INVALID_STATUS_RESPONSE);
1868 goto f_err;
1869 }
1870 if (ret < 0) {
1871 al = SSL_AD_INTERNAL_ERROR;
1872 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,
1873 ERR_R_MALLOC_FAILURE);
1874 goto f_err;
1875 }
1876 }
1877 return (1);
1878f_err:
1879 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1880 return (-1);
1881}
1882
1883int
1884ssl3_get_server_done(SSL *s)
1885{
1886 int ok, ret = 0;
1887 long n;
1888
1889 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1890 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1891 30, /* should be very small, like 0 :-) */ &ok);
1892
1893 if (!ok)
1894 return ((int)n);
1895 if (n > 0) {
1896 /* should contain no data */
1897 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1898 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
1899 return (-1);
1900 }
1901 ret = 1;
1902 return (ret);
1903}
1904
1905int
1906ssl3_send_client_key_exchange(SSL *s)
1907{
1908 unsigned char *p, *q;
1909 int n;
1910 unsigned long alg_k;
1911 EVP_PKEY *pkey = NULL;
1912 EC_KEY *clnt_ecdh = NULL;
1913 const EC_POINT *srvr_ecpoint = NULL;
1914 EVP_PKEY *srvr_pub_pkey = NULL;
1915 unsigned char *encodedPoint = NULL;
1916 int encoded_pt_len = 0;
1917 BN_CTX *bn_ctx = NULL;
1918
1919 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
1920 p = ssl3_handshake_msg_start(s, SSL3_MT_CLIENT_KEY_EXCHANGE);
1921
1922 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1923
1924 if (s->session->sess_cert == NULL) {
1925 ssl3_send_alert(s, SSL3_AL_FATAL,
1926 SSL_AD_UNEXPECTED_MESSAGE);
1927 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1928 ERR_R_INTERNAL_ERROR);
1929 goto err;
1930 }
1931
1932 if (alg_k & SSL_kRSA) {
1933 RSA *rsa;
1934 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1935
1936 pkey = X509_get_pubkey(
1937 s->session->sess_cert->peer_pkeys[
1938 SSL_PKEY_RSA_ENC].x509);
1939 if ((pkey == NULL) ||
1940 (pkey->type != EVP_PKEY_RSA) ||
1941 (pkey->pkey.rsa == NULL)) {
1942 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1943 ERR_R_INTERNAL_ERROR);
1944 EVP_PKEY_free(pkey);
1945 goto err;
1946 }
1947 rsa = pkey->pkey.rsa;
1948 EVP_PKEY_free(pkey);
1949
1950 tmp_buf[0] = s->client_version >> 8;
1951 tmp_buf[1] = s->client_version & 0xff;
1952 arc4random_buf(&tmp_buf[2], sizeof(tmp_buf) - 2);
1953
1954 s->session->master_key_length = sizeof tmp_buf;
1955
1956 q = p;
1957 /* Fix buf for TLS and beyond */
1958 if (s->version > SSL3_VERSION)
1959 p += 2;
1960 n = RSA_public_encrypt(sizeof tmp_buf,
1961 tmp_buf, p, rsa, RSA_PKCS1_PADDING);
1962 if (n <= 0) {
1963 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1964 SSL_R_BAD_RSA_ENCRYPT);
1965 goto err;
1966 }
1967
1968 /* Fix buf for TLS and beyond */
1969 if (s->version > SSL3_VERSION) {
1970 s2n(n, q);
1971 n += 2;
1972 }
1973
1974 s->session->master_key_length =
1975 s->method->ssl3_enc->generate_master_secret(
1976 s, s->session->master_key, tmp_buf, sizeof tmp_buf);
1977 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
1978 } else if (alg_k & SSL_kDHE) {
1979 DH *dh_srvr, *dh_clnt;
1980
1981 /* Ensure that we have an ephemeral key for DHE. */
1982 if (s->session->sess_cert->peer_dh_tmp == NULL) {
1983 ssl3_send_alert(s, SSL3_AL_FATAL,
1984 SSL_AD_HANDSHAKE_FAILURE);
1985 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1986 SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1987 goto err;
1988 }
1989 dh_srvr = s->session->sess_cert->peer_dh_tmp;
1990
1991 /* Generate a new random key. */
1992 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
1993 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1994 ERR_R_DH_LIB);
1995 goto err;
1996 }
1997 if (!DH_generate_key(dh_clnt)) {
1998 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1999 ERR_R_DH_LIB);
2000 DH_free(dh_clnt);
2001 goto err;
2002 }
2003
2004 /*
2005 * Use the 'p' output buffer for the DH key, but
2006 * make sure to clear it out afterwards.
2007 */
2008 n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt);
2009
2010 if (n <= 0) {
2011 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2012 ERR_R_DH_LIB);
2013 DH_free(dh_clnt);
2014 goto err;
2015 }
2016
2017 /* Generate master key from the result. */
2018 s->session->master_key_length =
2019 s->method->ssl3_enc->generate_master_secret(s,
2020 s->session->master_key, p, n);
2021
2022 /* Clean up. */
2023 memset(p, 0, n);
2024
2025 /* Send off the data. */
2026 n = BN_num_bytes(dh_clnt->pub_key);
2027 s2n(n, p);
2028 BN_bn2bin(dh_clnt->pub_key, p);
2029 n += 2;
2030
2031 DH_free(dh_clnt);
2032
2033 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2034 } else if (alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe)) {
2035 const EC_GROUP *srvr_group = NULL;
2036 EC_KEY *tkey;
2037 int field_size = 0;
2038
2039
2040 /* Ensure that we have an ephemeral key for ECDHE. */
2041 if ((alg_k & SSL_kECDHE) &&
2042 s->session->sess_cert->peer_ecdh_tmp == NULL) {
2043 ssl3_send_alert(s, SSL3_AL_FATAL,
2044 SSL_AD_HANDSHAKE_FAILURE);
2045 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2046 ERR_R_INTERNAL_ERROR);
2047 goto err;
2048 }
2049 tkey = s->session->sess_cert->peer_ecdh_tmp;
2050
2051 if (alg_k & (SSL_kECDHr|SSL_kECDHe)) {
2052 /* Get the Server Public Key from Cert */
2053 srvr_pub_pkey = X509_get_pubkey(s->session-> \
2054 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2055 if (srvr_pub_pkey != NULL &&
2056 srvr_pub_pkey->type == EVP_PKEY_EC)
2057 tkey = srvr_pub_pkey->pkey.ec;
2058 }
2059
2060 if (tkey == NULL) {
2061 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2062 ERR_R_INTERNAL_ERROR);
2063 goto err;
2064 }
2065
2066 srvr_group = EC_KEY_get0_group(tkey);
2067 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2068
2069 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2070 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2071 ERR_R_INTERNAL_ERROR);
2072 goto err;
2073 }
2074
2075 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2076 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2077 ERR_R_MALLOC_FAILURE);
2078 goto err;
2079 }
2080
2081 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2082 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2083 ERR_R_EC_LIB);
2084 goto err;
2085 }
2086
2087 /* Generate a new ECDH key pair */
2088 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2089 SSLerr(
2090 SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2091 ERR_R_ECDH_LIB);
2092 goto err;
2093 }
2094
2095 /*
2096 * Use the 'p' output buffer for the ECDH key, but
2097 * make sure to clear it out afterwards.
2098 */
2099 field_size = EC_GROUP_get_degree(srvr_group);
2100 if (field_size <= 0) {
2101 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2102 ERR_R_ECDH_LIB);
2103 goto err;
2104 }
2105 n = ECDH_compute_key(p, (field_size + 7)/8,
2106 srvr_ecpoint, clnt_ecdh, NULL);
2107 if (n <= 0) {
2108 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2109 ERR_R_ECDH_LIB);
2110 goto err;
2111 }
2112
2113 /* generate master key from the result */
2114 s->session->master_key_length = s->method->ssl3_enc \
2115 -> generate_master_secret(s,
2116 s->session->master_key, p, n);
2117
2118 memset(p, 0, n); /* clean up */
2119
2120 /*
2121 * First check the size of encoding and
2122 * allocate memory accordingly.
2123 */
2124 encoded_pt_len = EC_POINT_point2oct(
2125 srvr_group,
2126 EC_KEY_get0_public_key(clnt_ecdh),
2127 POINT_CONVERSION_UNCOMPRESSED,
2128 NULL, 0, NULL);
2129
2130 encodedPoint = malloc(encoded_pt_len);
2131
2132 bn_ctx = BN_CTX_new();
2133 if ((encodedPoint == NULL) ||
2134 (bn_ctx == NULL)) {
2135 SSLerr(
2136 SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2137 ERR_R_MALLOC_FAILURE);
2138 goto err;
2139 }
2140
2141 /* Encode the public key */
2142 n = EC_POINT_point2oct(srvr_group,
2143 EC_KEY_get0_public_key(clnt_ecdh),
2144 POINT_CONVERSION_UNCOMPRESSED,
2145 encodedPoint, encoded_pt_len, bn_ctx);
2146
2147 *p = n; /* length of encoded point */
2148 /* Encoded point will be copied here */
2149 p += 1;
2150
2151 /* copy the point */
2152 memcpy((unsigned char *)p, encodedPoint, n);
2153 /* increment n to account for length field */
2154 n += 1;
2155
2156 /* Free allocated memory */
2157 BN_CTX_free(bn_ctx);
2158 free(encodedPoint);
2159 EC_KEY_free(clnt_ecdh);
2160 EVP_PKEY_free(srvr_pub_pkey);
2161 } else if (alg_k & SSL_kGOST) {
2162 /* GOST key exchange message creation */
2163 EVP_PKEY_CTX *pkey_ctx;
2164 X509 *peer_cert;
2165
2166 size_t msglen;
2167 unsigned int md_len;
2168 unsigned char premaster_secret[32], shared_ukm[32],
2169 tmp[256];
2170 EVP_MD_CTX *ukm_hash;
2171 EVP_PKEY *pub_key;
2172 int nid;
2173
2174 /* Get server sertificate PKEY and create ctx from it */
2175 peer_cert = s->session->sess_cert->peer_pkeys[SSL_PKEY_GOST01].x509;
2176 if (!peer_cert) {
2177 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2178 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2179 goto err;
2180 }
2181
2182 pkey_ctx = EVP_PKEY_CTX_new(
2183 pub_key = X509_get_pubkey(peer_cert),
2184 NULL);
2185 /*
2186 * If we have send a certificate, and certificate key
2187 * parameters match those of server certificate, use
2188 * certificate key for key exchange.
2189 * Otherwise, generate ephemeral key pair.
2190 */
2191
2192 EVP_PKEY_encrypt_init(pkey_ctx);
2193 /* Generate session key. */
2194 arc4random_buf(premaster_secret, 32);
2195 /*
2196 * If we have client certificate, use its secret
2197 * as peer key.
2198 */
2199 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2200 if (EVP_PKEY_derive_set_peer(pkey_ctx,
2201 s->cert->key->privatekey) <=0) {
2202 /*
2203 * If there was an error -
2204 * just ignore it. Ephemeral key
2205 * would be used
2206 */
2207 ERR_clear_error();
2208 }
2209 }
2210 /*
2211 * Compute shared IV and store it in algorithm-specific
2212 * context data
2213 */
2214 ukm_hash = EVP_MD_CTX_create();
2215 if (ukm_hash == NULL) {
2216 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2217 ERR_R_MALLOC_FAILURE);
2218 goto err;
2219 }
2220
2221 if (ssl_get_algorithm2(s) & SSL_HANDSHAKE_MAC_GOST94)
2222 nid = NID_id_GostR3411_94;
2223 else
2224 nid = NID_id_tc26_gost3411_2012_256;
2225 if (!EVP_DigestInit(ukm_hash, EVP_get_digestbynid(nid)))
2226 goto err;
2227 EVP_DigestUpdate(ukm_hash,
2228 s->s3->client_random, SSL3_RANDOM_SIZE);
2229 EVP_DigestUpdate(ukm_hash,
2230 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 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2236 SSL_R_LIBRARY_BUG);
2237 goto err;
2238 }
2239 /*
2240 * Make GOST keytransport blob message,
2241 * encapsulate it into sequence.
2242 */
2243 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2244 msglen = 255;
2245 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen,
2246 premaster_secret, 32) < 0) {
2247 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2248 SSL_R_LIBRARY_BUG);
2249 goto err;
2250 }
2251 if (msglen >= 0x80) {
2252 *(p++) = 0x81;
2253 *(p++) = msglen & 0xff;
2254 n = msglen + 3;
2255 } else {
2256 *(p++) = msglen & 0xff;
2257 n = msglen + 2;
2258 }
2259 memcpy(p, tmp, msglen);
2260 /* Check if pubkey from client certificate was used. */
2261 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1,
2262 EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2263 /* Set flag "skip certificate verify". */
2264 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2265 }
2266 EVP_PKEY_CTX_free(pkey_ctx);
2267 s->session->master_key_length =
2268 s->method->ssl3_enc->generate_master_secret(s,
2269 s->session->master_key, premaster_secret, 32);
2270 EVP_PKEY_free(pub_key);
2271
2272 } else {
2273 ssl3_send_alert(s, SSL3_AL_FATAL,
2274 SSL_AD_HANDSHAKE_FAILURE);
2275 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2276 ERR_R_INTERNAL_ERROR);
2277 goto err;
2278 }
2279
2280 s->state = SSL3_ST_CW_KEY_EXCH_B;
2281
2282 ssl3_handshake_msg_finish(s, n);
2283 }
2284
2285 /* SSL3_ST_CW_KEY_EXCH_B */
2286 return (ssl3_handshake_write(s));
2287
2288err:
2289 BN_CTX_free(bn_ctx);
2290 free(encodedPoint);
2291 EC_KEY_free(clnt_ecdh);
2292 EVP_PKEY_free(srvr_pub_pkey);
2293 return (-1);
2294}
2295
2296int
2297ssl3_send_client_verify(SSL *s)
2298{
2299 unsigned char *p;
2300 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
2301 EVP_PKEY *pkey;
2302 EVP_PKEY_CTX *pctx = NULL;
2303 EVP_MD_CTX mctx;
2304 unsigned u = 0;
2305 unsigned long n;
2306 int j;
2307
2308 EVP_MD_CTX_init(&mctx);
2309
2310 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2311 p = ssl3_handshake_msg_start(s, SSL3_MT_CERTIFICATE_VERIFY);
2312
2313 /*
2314 * Create context from key and test if sha1 is allowed as
2315 * digest.
2316 */
2317 pkey = s->cert->key->privatekey;
2318 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2319 EVP_PKEY_sign_init(pctx);
2320 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
2321 if (!SSL_USE_SIGALGS(s))
2322 s->method->ssl3_enc->cert_verify_mac(s,
2323 NID_sha1, &(data[MD5_DIGEST_LENGTH]));
2324 } else {
2325 ERR_clear_error();
2326 }
2327 /*
2328 * For TLS v1.2 send signature algorithm and signature
2329 * using agreed digest and cached handshake records.
2330 */
2331 if (SSL_USE_SIGALGS(s)) {
2332 long hdatalen = 0;
2333 void *hdata;
2334 const EVP_MD *md = s->cert->key->digest;
2335 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer,
2336 &hdata);
2337 if (hdatalen <= 0 ||
2338 !tls12_get_sigandhash(p, pkey, md)) {
2339 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2340 ERR_R_INTERNAL_ERROR);
2341 goto err;
2342 }
2343 p += 2;
2344 if (!EVP_SignInit_ex(&mctx, md, NULL) ||
2345 !EVP_SignUpdate(&mctx, hdata, hdatalen) ||
2346 !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
2347 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2348 ERR_R_EVP_LIB);
2349 goto err;
2350 }
2351 s2n(u, p);
2352 n = u + 4;
2353 if (!ssl3_digest_cached_records(s))
2354 goto err;
2355 } else if (pkey->type == EVP_PKEY_RSA) {
2356 s->method->ssl3_enc->cert_verify_mac(
2357 s, NID_md5, &(data[0]));
2358 if (RSA_sign(NID_md5_sha1, data,
2359 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, &(p[2]),
2360 &u, pkey->pkey.rsa) <= 0 ) {
2361 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2362 ERR_R_RSA_LIB);
2363 goto err;
2364 }
2365 s2n(u, p);
2366 n = u + 2;
2367 } else if (pkey->type == EVP_PKEY_DSA) {
2368 if (!DSA_sign(pkey->save_type,
2369 &(data[MD5_DIGEST_LENGTH]),
2370 SHA_DIGEST_LENGTH, &(p[2]),
2371 (unsigned int *)&j, pkey->pkey.dsa)) {
2372 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2373 ERR_R_DSA_LIB);
2374 goto err;
2375 }
2376 s2n(j, p);
2377 n = j + 2;
2378 } else if (pkey->type == EVP_PKEY_EC) {
2379 if (!ECDSA_sign(pkey->save_type,
2380 &(data[MD5_DIGEST_LENGTH]),
2381 SHA_DIGEST_LENGTH, &(p[2]),
2382 (unsigned int *)&j, pkey->pkey.ec)) {
2383 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2384 ERR_R_ECDSA_LIB);
2385 goto err;
2386 }
2387 s2n(j, p);
2388 n = j + 2;
2389#ifndef OPENSSL_NO_GOST
2390 } else if (pkey->type == NID_id_GostR3410_94 ||
2391 pkey->type == NID_id_GostR3410_2001) {
2392 unsigned char signbuf[128];
2393 long hdatalen = 0;
2394 void *hdata;
2395 const EVP_MD *md;
2396 int nid;
2397 size_t sigsize;
2398
2399 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2400 if (hdatalen <= 0) {
2401 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2402 ERR_R_INTERNAL_ERROR);
2403 goto err;
2404 }
2405 if (!EVP_PKEY_get_default_digest_nid(pkey, &nid) ||
2406 !(md = EVP_get_digestbynid(nid))) {
2407 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2408 ERR_R_EVP_LIB);
2409 goto err;
2410 }
2411 if (!EVP_DigestInit_ex(&mctx, md, NULL) ||
2412 !EVP_DigestUpdate(&mctx, hdata, hdatalen) ||
2413 !EVP_DigestFinal(&mctx, signbuf, &u) ||
2414 (EVP_PKEY_CTX_set_signature_md(pctx, md) <= 0) ||
2415 (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
2416 EVP_PKEY_CTRL_GOST_SIG_FORMAT,
2417 GOST_SIG_FORMAT_RS_LE,
2418 NULL) <= 0) ||
2419 (EVP_PKEY_sign(pctx, &(p[2]), &sigsize,
2420 signbuf, u) <= 0)) {
2421 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2422 ERR_R_EVP_LIB);
2423 goto err;
2424 }
2425 if (!ssl3_digest_cached_records(s))
2426 goto err;
2427 j = sigsize;
2428 s2n(j, p);
2429 n = j + 2;
2430#endif
2431 } else {
2432 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2433 ERR_R_INTERNAL_ERROR);
2434 goto err;
2435 }
2436
2437 s->state = SSL3_ST_CW_CERT_VRFY_B;
2438
2439 ssl3_handshake_msg_finish(s, n);
2440 }
2441
2442 EVP_MD_CTX_cleanup(&mctx);
2443 EVP_PKEY_CTX_free(pctx);
2444
2445 return (ssl3_handshake_write(s));
2446
2447err:
2448 EVP_MD_CTX_cleanup(&mctx);
2449 EVP_PKEY_CTX_free(pctx);
2450 return (-1);
2451}
2452
2453int
2454ssl3_send_client_certificate(SSL *s)
2455{
2456 X509 *x509 = NULL;
2457 EVP_PKEY *pkey = NULL;
2458 int i;
2459 unsigned long l;
2460
2461 if (s->state == SSL3_ST_CW_CERT_A) {
2462 if ((s->cert == NULL) || (s->cert->key->x509 == NULL) ||
2463 (s->cert->key->privatekey == NULL))
2464 s->state = SSL3_ST_CW_CERT_B;
2465 else
2466 s->state = SSL3_ST_CW_CERT_C;
2467 }
2468
2469 /* We need to get a client cert */
2470 if (s->state == SSL3_ST_CW_CERT_B) {
2471 /*
2472 * If we get an error, we need to
2473 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2474 * We then get retied later
2475 */
2476 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2477 if (i < 0) {
2478 s->rwstate = SSL_X509_LOOKUP;
2479 return (-1);
2480 }
2481 s->rwstate = SSL_NOTHING;
2482 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
2483 s->state = SSL3_ST_CW_CERT_B;
2484 if (!SSL_use_certificate(s, x509) ||
2485 !SSL_use_PrivateKey(s, pkey))
2486 i = 0;
2487 } else if (i == 1) {
2488 i = 0;
2489 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
2490 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2491 }
2492
2493 if (x509 != NULL)
2494 X509_free(x509);
2495 EVP_PKEY_free(pkey);
2496 if (i == 0) {
2497 if (s->version == SSL3_VERSION) {
2498 s->s3->tmp.cert_req = 0;
2499 ssl3_send_alert(s, SSL3_AL_WARNING,
2500 SSL_AD_NO_CERTIFICATE);
2501 return (1);
2502 } else {
2503 s->s3->tmp.cert_req = 2;
2504 }
2505 }
2506
2507 /* Ok, we have a cert */
2508 s->state = SSL3_ST_CW_CERT_C;
2509 }
2510
2511 if (s->state == SSL3_ST_CW_CERT_C) {
2512 s->state = SSL3_ST_CW_CERT_D;
2513 l = ssl3_output_cert_chain(s,
2514 (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key->x509);
2515 s->init_num = (int)l;
2516 s->init_off = 0;
2517 }
2518 /* SSL3_ST_CW_CERT_D */
2519 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2520}
2521
2522#define has_bits(i,m) (((i)&(m)) == (m))
2523
2524int
2525ssl3_check_cert_and_algorithm(SSL *s)
2526{
2527 int i, idx;
2528 long alg_k, alg_a;
2529 EVP_PKEY *pkey = NULL;
2530 SESS_CERT *sc;
2531 DH *dh;
2532
2533 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2534 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2535
2536 /* We don't have a certificate. */
2537 if (alg_a & SSL_aNULL)
2538 return (1);
2539
2540 sc = s->session->sess_cert;
2541 if (sc == NULL) {
2542 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2543 ERR_R_INTERNAL_ERROR);
2544 goto err;
2545 }
2546 dh = s->session->sess_cert->peer_dh_tmp;
2547
2548 /* This is the passed certificate. */
2549
2550 idx = sc->peer_cert_type;
2551 if (idx == SSL_PKEY_ECC) {
2552 if (ssl_check_srvr_ecc_cert_and_alg(
2553 sc->peer_pkeys[idx].x509, s) == 0) {
2554 /* check failed */
2555 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2556 SSL_R_BAD_ECC_CERT);
2557 goto f_err;
2558 } else {
2559 return (1);
2560 }
2561 }
2562 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
2563 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
2564 EVP_PKEY_free(pkey);
2565
2566 /* Check that we have a certificate if we require one. */
2567 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA|EVP_PKT_SIGN)) {
2568 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2569 SSL_R_MISSING_RSA_SIGNING_CERT);
2570 goto f_err;
2571 } else if ((alg_a & SSL_aDSS) &&
2572 !has_bits(i, EVP_PK_DSA|EVP_PKT_SIGN)) {
2573 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2574 SSL_R_MISSING_DSA_SIGNING_CERT);
2575 goto f_err;
2576 }
2577 if ((alg_k & SSL_kRSA) &&
2578 !has_bits(i, EVP_PK_RSA|EVP_PKT_ENC)) {
2579 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2580 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2581 goto f_err;
2582 }
2583 if ((alg_k & SSL_kDHE) &&
2584 !(has_bits(i, EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL))) {
2585 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2586 SSL_R_MISSING_DH_KEY);
2587 goto f_err;
2588 }
2589
2590 return (1);
2591f_err:
2592 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2593err:
2594 return (0);
2595}
2596
2597int
2598ssl3_send_next_proto(SSL *s)
2599{
2600 unsigned int len, padding_len;
2601 unsigned char *d, *p;
2602
2603 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2604 d = p = ssl3_handshake_msg_start(s, SSL3_MT_NEXT_PROTO);
2605
2606 len = s->next_proto_negotiated_len;
2607 padding_len = 32 - ((len + 2) % 32);
2608 *(p++) = len;
2609 memcpy(p, s->next_proto_negotiated, len);
2610 p += len;
2611 *(p++) = padding_len;
2612 memset(p, 0, padding_len);
2613 p += padding_len;
2614
2615 ssl3_handshake_msg_finish(s, p - d);
2616
2617 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2618 }
2619
2620 return (ssl3_handshake_write(s));
2621}
2622
2623/*
2624 * Check to see if handshake is full or resumed. Usually this is just a
2625 * case of checking to see if a cache hit has occurred. In the case of
2626 * session tickets we have to check the next message to be sure.
2627 */
2628
2629int
2630ssl3_check_finished(SSL *s)
2631{
2632 int ok;
2633 long n;
2634
2635 /* If we have no ticket it cannot be a resumed session. */
2636 if (!s->session->tlsext_tick)
2637 return (1);
2638 /* this function is called when we really expect a Certificate
2639 * message, so permit appropriate message length */
2640 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A,
2641 SSL3_ST_CR_CERT_B, -1, s->max_cert_list, &ok);
2642 if (!ok)
2643 return ((int)n);
2644 s->s3->tmp.reuse_message = 1;
2645 if ((s->s3->tmp.message_type == SSL3_MT_FINISHED) ||
2646 (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
2647 return (2);
2648
2649 return (1);
2650}
2651
2652int
2653ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
2654{
2655 int i = 0;
2656
2657#ifndef OPENSSL_NO_ENGINE
2658 if (s->ctx->client_cert_engine) {
2659 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
2660 SSL_get_client_CA_list(s),
2661 px509, ppkey, NULL, NULL, NULL);
2662 if (i != 0)
2663 return (i);
2664 }
2665#endif
2666 if (s->ctx->client_cert_cb)
2667 i = s->ctx->client_cert_cb(s, px509, ppkey);
2668 return (i);
2669}