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