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