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