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