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