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