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