]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/statem/statem_clnt.c
Add some extra key_share tests
[thirdparty/openssl.git] / ssl / statem / statem_clnt.c
CommitLineData
846e33c7
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
8c74b5e5 3 *
846e33c7
RS
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8c74b5e5 8 */
846e33c7 9
ea262260
BM
10/* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12 *
0f113f3e 13 * Portions of the attached software ("Contribution") are developed by
ea262260
BM
14 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
15 *
16 * The Contribution is licensed pursuant to the OpenSSL open source
17 * license provided above.
18 *
ea262260
BM
19 * ECC cipher suite support in OpenSSL originally written by
20 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
21 *
22 */
ddac1974
NL
23/* ====================================================================
24 * Copyright 2005 Nokia. All rights reserved.
25 *
26 * The portions of the attached software ("Contribution") is developed by
27 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
28 * license.
29 *
30 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
31 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
32 * support (see RFC 4279) to OpenSSL.
33 *
34 * No patent licenses or other rights except those expressly stated in
35 * the OpenSSL open source license shall be deemed granted or received
36 * expressly, by implication, estoppel, or otherwise.
37 *
38 * No assurances are provided by Nokia that the Contribution does not
39 * infringe the patent or other intellectual property rights of any third
40 * party or that the license provides you with all the necessary rights
41 * to make use of the Contribution.
42 *
43 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
44 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
45 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
46 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
47 * OTHERWISE.
48 */
d02b48c6
RE
49
50#include <stdio.h>
8ba708e5 51#include "../ssl_locl.h"
61ae935a 52#include "statem_locl.h"
ec577822
BM
53#include <openssl/buffer.h>
54#include <openssl/rand.h>
55#include <openssl/objects.h>
56#include <openssl/evp.h>
dbad1690 57#include <openssl/md5.h>
3c27208f 58#include <openssl/dh.h>
d095b68d 59#include <openssl/bn.h>
3c27208f 60#include <openssl/engine.h>
f9b3bff6 61
e46f2334
MC
62static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt);
63
7ab09630 64static ossl_inline int cert_req_allowed(SSL *s);
a455d0f6 65static int key_exchange_expected(SSL *s);
0f113f3e 66static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
d45ba43d 67static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
ae2f7b37 68 WPACKET *pkt);
ea262260 69
61ae935a
MC
70/*
71 * Is a CertificateRequest message allowed at the moment or not?
72 *
73 * Return values are:
74 * 1: Yes
75 * 0: No
76 */
7ab09630 77static ossl_inline int cert_req_allowed(SSL *s)
61ae935a
MC
78{
79 /* TLS does not like anon-DH with client cert */
b7fa1f98 80 if ((s->version > SSL3_VERSION
a230b26e
EK
81 && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))
82 || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
61ae935a
MC
83 return 0;
84
85 return 1;
86}
87
88/*
a455d0f6 89 * Should we expect the ServerKeyExchange message or not?
61ae935a
MC
90 *
91 * Return values are:
92 * 1: Yes
93 * 0: No
94 */
a455d0f6 95static int key_exchange_expected(SSL *s)
61ae935a
MC
96{
97 long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
98
99 /*
100 * Can't skip server key exchange if this is an ephemeral
a455d0f6 101 * ciphersuite or for SRP
61ae935a 102 */
a455d0f6
MC
103 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
104 | SSL_kSRP)) {
105 return 1;
61ae935a
MC
106 }
107
a455d0f6 108 return 0;
61ae935a
MC
109}
110
0f1e51ea
MC
111/*
112 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
113 * handshake state transitions when a TLS1.3 client is reading messages from the
114 * server. The message type that the server has sent is provided in |mt|. The
115 * current state is in |s->statem.hand_state|.
116 *
94ed2c67
MC
117 * Return values are 1 for success (transition allowed) and 0 on error
118 * (transition not allowed)
0f1e51ea
MC
119 */
120static int ossl_statem_client13_read_transition(SSL *s, int mt)
121{
122 OSSL_STATEM *st = &s->statem;
123
94ed2c67
MC
124 /*
125 * TODO(TLS1.3): This is still based on the TLSv1.2 state machine. Over time
126 * we will update this to look more like real TLSv1.3
127 */
128
0f1e51ea
MC
129 /*
130 * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't
131 * yet negotiated TLSv1.3 at that point so that is handled by
132 * ossl_statem_client_read_transition()
133 */
134
135 switch (st->hand_state) {
136 default:
137 break;
138
139 case TLS_ST_CR_SRVR_HELLO:
e46f2334
MC
140 if (mt == SSL3_MT_ENCRYPTED_EXTENSIONS) {
141 st->hand_state = TLS_ST_CR_ENCRYPTED_EXTENSIONS;
142 return 1;
143 }
144 break;
145
146 case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
0f1e51ea 147 if (s->hit) {
92760c21
MC
148 if (mt == SSL3_MT_FINISHED) {
149 st->hand_state = TLS_ST_CR_FINISHED;
0f1e51ea
MC
150 return 1;
151 }
152 } else {
92760c21
MC
153 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
154 st->hand_state = TLS_ST_CR_CERT_REQ;
155 return 1;
f5ca0b04
MC
156 }
157 if (mt == SSL3_MT_CERTIFICATE) {
0f1e51ea
MC
158 st->hand_state = TLS_ST_CR_CERT;
159 return 1;
160 }
161 }
162 break;
163
92760c21
MC
164 case TLS_ST_CR_CERT_REQ:
165 if (mt == SSL3_MT_CERTIFICATE) {
166 st->hand_state = TLS_ST_CR_CERT;
167 return 1;
168 }
169 break;
170
0f1e51ea
MC
171 case TLS_ST_CR_CERT:
172 /*
173 * The CertificateStatus message is optional even if
174 * |tlsext_status_expected| is set
175 */
176 if (s->tlsext_status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
177 st->hand_state = TLS_ST_CR_CERT_STATUS;
178 return 1;
179 }
180 /* Fall through */
181
182 case TLS_ST_CR_CERT_STATUS:
0f1e51ea
MC
183 if (mt == SSL3_MT_FINISHED) {
184 st->hand_state = TLS_ST_CR_FINISHED;
185 return 1;
186 }
187 break;
92760c21 188
0f1e51ea
MC
189 }
190
0f1e51ea 191 /* No valid transition found */
0f1e51ea
MC
192 return 0;
193}
194
61ae935a 195/*
8481f583
MC
196 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
197 * handshake state transitions when the client is reading messages from the
198 * server. The message type that the server has sent is provided in |mt|. The
199 * current state is in |s->statem.hand_state|.
61ae935a 200 *
94ed2c67
MC
201 * Return values are 1 for success (transition allowed) and 0 on error
202 * (transition not allowed)
61ae935a 203 */
8481f583 204int ossl_statem_client_read_transition(SSL *s, int mt)
61ae935a 205{
d6f1a6e9 206 OSSL_STATEM *st = &s->statem;
a455d0f6 207 int ske_expected;
61ae935a 208
0f1e51ea
MC
209 /*
210 * Note that after a ClientHello we don't know what version we are going
211 * to negotiate yet, so we don't take this branch until later
212 */
f5ca0b04 213 if (SSL_IS_TLS13(s)) {
5abeaf35
MC
214 if (!ossl_statem_client13_read_transition(s, mt))
215 goto err;
216 return 1;
217 }
0f1e51ea 218
a230b26e 219 switch (st->hand_state) {
f3b3d7f0
RS
220 default:
221 break;
222
61ae935a
MC
223 case TLS_ST_CW_CLNT_HELLO:
224 if (mt == SSL3_MT_SERVER_HELLO) {
225 st->hand_state = TLS_ST_CR_SRVR_HELLO;
226 return 1;
227 }
228
229 if (SSL_IS_DTLS(s)) {
230 if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
231 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
232 return 1;
233 }
234 }
235 break;
236
237 case TLS_ST_CR_SRVR_HELLO:
238 if (s->hit) {
239 if (s->tlsext_ticket_expected) {
240 if (mt == SSL3_MT_NEWSESSION_TICKET) {
241 st->hand_state = TLS_ST_CR_SESSION_TICKET;
242 return 1;
243 }
244 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
245 st->hand_state = TLS_ST_CR_CHANGE;
246 return 1;
247 }
248 } else {
249 if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
250 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
251 return 1;
ad3819c2 252 } else if (s->version >= TLS1_VERSION
a230b26e
EK
253 && s->tls_session_secret_cb != NULL
254 && s->session->tlsext_tick != NULL
255 && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
ad3819c2
MC
256 /*
257 * Normally, we can tell if the server is resuming the session
258 * from the session ID. EAP-FAST (RFC 4851), however, relies on
259 * the next server message after the ServerHello to determine if
260 * the server is resuming.
261 */
262 s->hit = 1;
263 st->hand_state = TLS_ST_CR_CHANGE;
264 return 1;
61ae935a 265 } else if (!(s->s3->tmp.new_cipher->algorithm_auth
a230b26e 266 & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
61ae935a
MC
267 if (mt == SSL3_MT_CERTIFICATE) {
268 st->hand_state = TLS_ST_CR_CERT;
269 return 1;
270 }
271 } else {
a455d0f6 272 ske_expected = key_exchange_expected(s);
a455d0f6
MC
273 /* SKE is optional for some PSK ciphersuites */
274 if (ske_expected
a230b26e
EK
275 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
276 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
a455d0f6
MC
277 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
278 st->hand_state = TLS_ST_CR_KEY_EXCH;
279 return 1;
280 }
281 } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
a230b26e
EK
282 && cert_req_allowed(s)) {
283 st->hand_state = TLS_ST_CR_CERT_REQ;
284 return 1;
a455d0f6 285 } else if (mt == SSL3_MT_SERVER_DONE) {
a230b26e
EK
286 st->hand_state = TLS_ST_CR_SRVR_DONE;
287 return 1;
61ae935a
MC
288 }
289 }
290 }
291 break;
292
293 case TLS_ST_CR_CERT:
bb1aaab4
MC
294 /*
295 * The CertificateStatus message is optional even if
296 * |tlsext_status_expected| is set
297 */
298 if (s->tlsext_status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
299 st->hand_state = TLS_ST_CR_CERT_STATUS;
300 return 1;
a455d0f6
MC
301 }
302 /* Fall through */
303
304 case TLS_ST_CR_CERT_STATUS:
305 ske_expected = key_exchange_expected(s);
a455d0f6 306 /* SKE is optional for some PSK ciphersuites */
a230b26e
EK
307 if (ske_expected || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
308 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
61ae935a
MC
309 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
310 st->hand_state = TLS_ST_CR_KEY_EXCH;
311 return 1;
61ae935a 312 }
672f3337 313 goto err;
61ae935a 314 }
a455d0f6 315 /* Fall through */
61ae935a 316
a455d0f6
MC
317 case TLS_ST_CR_KEY_EXCH:
318 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
319 if (cert_req_allowed(s)) {
61ae935a
MC
320 st->hand_state = TLS_ST_CR_CERT_REQ;
321 return 1;
61ae935a 322 }
672f3337 323 goto err;
61ae935a 324 }
a455d0f6 325 /* Fall through */
61ae935a
MC
326
327 case TLS_ST_CR_CERT_REQ:
328 if (mt == SSL3_MT_SERVER_DONE) {
329 st->hand_state = TLS_ST_CR_SRVR_DONE;
330 return 1;
331 }
332 break;
333
334 case TLS_ST_CW_FINISHED:
c45d6b2b
DB
335 if (s->tlsext_ticket_expected) {
336 if (mt == SSL3_MT_NEWSESSION_TICKET) {
337 st->hand_state = TLS_ST_CR_SESSION_TICKET;
338 return 1;
339 }
61ae935a
MC
340 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
341 st->hand_state = TLS_ST_CR_CHANGE;
342 return 1;
343 }
344 break;
345
346 case TLS_ST_CR_SESSION_TICKET:
347 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
348 st->hand_state = TLS_ST_CR_CHANGE;
349 return 1;
350 }
351 break;
352
353 case TLS_ST_CR_CHANGE:
354 if (mt == SSL3_MT_FINISHED) {
355 st->hand_state = TLS_ST_CR_FINISHED;
356 return 1;
357 }
358 break;
61ae935a
MC
359 }
360
672f3337 361 err:
61ae935a 362 /* No valid transition found */
672f3337 363 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
340a2828 364 SSLerr(SSL_F_OSSL_STATEM_CLIENT_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE);
61ae935a
MC
365 return 0;
366}
367
368/*
0f1e51ea
MC
369 * ossl_statem_client13_write_transition() works out what handshake state to
370 * move to next when the TLSv1.3 client is writing messages to be sent to the
371 * server.
0f1e51ea
MC
372 */
373static WRITE_TRAN ossl_statem_client13_write_transition(SSL *s)
374{
375 OSSL_STATEM *st = &s->statem;
376
94ed2c67
MC
377 /*
378 * TODO(TLS1.3): This is still based on the TLSv1.2 state machine. Over time
379 * we will update this to look more like real TLSv1.3
380 */
381
0f1e51ea
MC
382 /*
383 * Note: There are no cases for TLS_ST_BEFORE or TLS_ST_CW_CLNT_HELLO,
384 * because we haven't negotiated TLSv1.3 yet at that point. They are
385 * handled by ossl_statem_client_write_transition().
386 */
387 switch (st->hand_state) {
388 default:
389 /* Shouldn't happen */
390 return WRITE_TRAN_ERROR;
391
92760c21 392 case TLS_ST_CR_FINISHED:
94ed2c67 393 st->hand_state = (s->s3->tmp.cert_req != 0) ? TLS_ST_CW_CERT
92760c21 394 : TLS_ST_CW_FINISHED;
0f1e51ea
MC
395 return WRITE_TRAN_CONTINUE;
396
397 case TLS_ST_CW_CERT:
398 /* If a non-empty Certificate we also send CertificateVerify */
94ed2c67 399 st->hand_state = (s->s3->tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY
92760c21 400 : TLS_ST_CW_FINISHED;
0f1e51ea
MC
401 return WRITE_TRAN_CONTINUE;
402
403 case TLS_ST_CW_CERT_VRFY:
0f1e51ea
MC
404 st->hand_state = TLS_ST_CW_FINISHED;
405 return WRITE_TRAN_CONTINUE;
406
407 case TLS_ST_CW_FINISHED:
94ed2c67
MC
408 st->hand_state = TLS_ST_OK;
409 ossl_statem_set_in_init(s, 0);
410 return WRITE_TRAN_CONTINUE;
0f1e51ea
MC
411 }
412}
413
414/*
415 * ossl_statem_client_write_transition() works out what handshake state to
416 * move to next when the client is writing messages to be sent to the server.
61ae935a 417 */
8481f583 418WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
61ae935a 419{
d6f1a6e9 420 OSSL_STATEM *st = &s->statem;
61ae935a 421
0f1e51ea
MC
422 /*
423 * Note that immediately before/after a ClientHello we don't know what
424 * version we are going to negotiate yet, so we don't take this branch until
425 * later
426 */
f5ca0b04 427 if (SSL_IS_TLS13(s))
0f1e51ea
MC
428 return ossl_statem_client13_write_transition(s);
429
a230b26e 430 switch (st->hand_state) {
f3b3d7f0
RS
431 default:
432 /* Shouldn't happen */
433 return WRITE_TRAN_ERROR;
434
a230b26e
EK
435 case TLS_ST_OK:
436 /* Renegotiation - fall through */
437 case TLS_ST_BEFORE:
438 st->hand_state = TLS_ST_CW_CLNT_HELLO;
439 return WRITE_TRAN_CONTINUE;
61ae935a 440
a230b26e
EK
441 case TLS_ST_CW_CLNT_HELLO:
442 /*
443 * No transition at the end of writing because we don't know what
444 * we will be sent
445 */
446 return WRITE_TRAN_FINISHED;
61ae935a 447
a230b26e
EK
448 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
449 st->hand_state = TLS_ST_CW_CLNT_HELLO;
450 return WRITE_TRAN_CONTINUE;
61ae935a 451
a230b26e
EK
452 case TLS_ST_CR_SRVR_DONE:
453 if (s->s3->tmp.cert_req)
454 st->hand_state = TLS_ST_CW_CERT;
455 else
61ae935a 456 st->hand_state = TLS_ST_CW_KEY_EXCH;
a230b26e 457 return WRITE_TRAN_CONTINUE;
61ae935a 458
a230b26e
EK
459 case TLS_ST_CW_CERT:
460 st->hand_state = TLS_ST_CW_KEY_EXCH;
461 return WRITE_TRAN_CONTINUE;
61ae935a 462
a230b26e
EK
463 case TLS_ST_CW_KEY_EXCH:
464 /*
465 * For TLS, cert_req is set to 2, so a cert chain of nothing is
466 * sent, but no verify packet is sent
467 */
468 /*
469 * XXX: For now, we do not support client authentication in ECDH
470 * cipher suites with ECDH (rather than ECDSA) certificates. We
471 * need to skip the certificate verify message when client's
472 * ECDH public key is sent inside the client certificate.
473 */
474 if (s->s3->tmp.cert_req == 1) {
475 st->hand_state = TLS_ST_CW_CERT_VRFY;
476 } else {
61ae935a 477 st->hand_state = TLS_ST_CW_CHANGE;
a230b26e
EK
478 }
479 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
480 st->hand_state = TLS_ST_CW_CHANGE;
481 }
482 return WRITE_TRAN_CONTINUE;
61ae935a 483
a230b26e
EK
484 case TLS_ST_CW_CERT_VRFY:
485 st->hand_state = TLS_ST_CW_CHANGE;
486 return WRITE_TRAN_CONTINUE;
487
488 case TLS_ST_CW_CHANGE:
61ae935a 489#if defined(OPENSSL_NO_NEXTPROTONEG)
a230b26e 490 st->hand_state = TLS_ST_CW_FINISHED;
61ae935a 491#else
a230b26e
EK
492 if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
493 st->hand_state = TLS_ST_CW_NEXT_PROTO;
494 else
495 st->hand_state = TLS_ST_CW_FINISHED;
61ae935a 496#endif
a230b26e 497 return WRITE_TRAN_CONTINUE;
61ae935a
MC
498
499#if !defined(OPENSSL_NO_NEXTPROTONEG)
a230b26e
EK
500 case TLS_ST_CW_NEXT_PROTO:
501 st->hand_state = TLS_ST_CW_FINISHED;
502 return WRITE_TRAN_CONTINUE;
61ae935a
MC
503#endif
504
a230b26e
EK
505 case TLS_ST_CW_FINISHED:
506 if (s->hit) {
507 st->hand_state = TLS_ST_OK;
508 ossl_statem_set_in_init(s, 0);
509 return WRITE_TRAN_CONTINUE;
510 } else {
511 return WRITE_TRAN_FINISHED;
512 }
61ae935a 513
a230b26e
EK
514 case TLS_ST_CR_FINISHED:
515 if (s->hit) {
516 st->hand_state = TLS_ST_CW_CHANGE;
517 return WRITE_TRAN_CONTINUE;
518 } else {
519 st->hand_state = TLS_ST_OK;
520 ossl_statem_set_in_init(s, 0);
521 return WRITE_TRAN_CONTINUE;
522 }
61ae935a
MC
523 }
524}
525
526/*
527 * Perform any pre work that needs to be done prior to sending a message from
528 * the client to the server.
529 */
8481f583 530WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
61ae935a 531{
d6f1a6e9 532 OSSL_STATEM *st = &s->statem;
61ae935a 533
a230b26e 534 switch (st->hand_state) {
f3b3d7f0
RS
535 default:
536 /* No pre work to be done */
537 break;
538
61ae935a
MC
539 case TLS_ST_CW_CLNT_HELLO:
540 s->shutdown = 0;
541 if (SSL_IS_DTLS(s)) {
542 /* every DTLS ClientHello resets Finished MAC */
2c4a056f
MC
543 if (!ssl3_init_finished_mac(s)) {
544 ossl_statem_set_error(s);
545 return WORK_ERROR;
546 }
61ae935a
MC
547 }
548 break;
549
61ae935a
MC
550 case TLS_ST_CW_CHANGE:
551 if (SSL_IS_DTLS(s)) {
552 if (s->hit) {
553 /*
554 * We're into the last flight so we don't retransmit these
555 * messages unless we need to.
556 */
557 st->use_timer = 0;
558 }
559#ifndef OPENSSL_NO_SCTP
560 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
561 return dtls_wait_for_dry(s);
562#endif
563 }
f3b3d7f0 564 break;
61ae935a
MC
565
566 case TLS_ST_OK:
567 return tls_finish_handshake(s, wst);
61ae935a
MC
568 }
569
570 return WORK_FINISHED_CONTINUE;
571}
572
573/*
574 * Perform any work that needs to be done after sending a message from the
575 * client to the server.
576 */
8481f583 577WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
61ae935a 578{
d6f1a6e9 579 OSSL_STATEM *st = &s->statem;
61ae935a
MC
580
581 s->init_num = 0;
582
a230b26e 583 switch (st->hand_state) {
f3b3d7f0
RS
584 default:
585 /* No post work to be done */
586 break;
587
61ae935a 588 case TLS_ST_CW_CLNT_HELLO:
46417569 589 if (wst == WORK_MORE_A && statem_flush(s) != 1)
61ae935a 590 return WORK_MORE_A;
46417569 591
61ae935a
MC
592 if (SSL_IS_DTLS(s)) {
593 /* Treat the next message as the first packet */
594 s->first_packet = 1;
595 }
596 break;
597
598 case TLS_ST_CW_KEY_EXCH:
599 if (tls_client_key_exchange_post_work(s) == 0)
600 return WORK_ERROR;
601 break;
602
603 case TLS_ST_CW_CHANGE:
604 s->session->cipher = s->s3->tmp.new_cipher;
605#ifdef OPENSSL_NO_COMP
606 s->session->compress_meth = 0;
607#else
608 if (s->s3->tmp.new_compression == NULL)
609 s->session->compress_meth = 0;
610 else
611 s->session->compress_meth = s->s3->tmp.new_compression->id;
612#endif
613 if (!s->method->ssl3_enc->setup_key_block(s))
614 return WORK_ERROR;
615
616 if (!s->method->ssl3_enc->change_cipher_state(s,
617 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
618 return WORK_ERROR;
619
620 if (SSL_IS_DTLS(s)) {
621#ifndef OPENSSL_NO_SCTP
622 if (s->hit) {
623 /*
624 * Change to new shared key of SCTP-Auth, will be ignored if
625 * no SCTP used.
626 */
627 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
628 0, NULL);
629 }
630#endif
631
632 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
633 }
634 break;
635
636 case TLS_ST_CW_FINISHED:
637#ifndef OPENSSL_NO_SCTP
638 if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
639 /*
640 * Change to new shared key of SCTP-Auth, will be ignored if
641 * no SCTP used.
642 */
643 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
644 0, NULL);
645 }
646#endif
647 if (statem_flush(s) != 1)
648 return WORK_MORE_B;
92760c21
MC
649
650 if (SSL_IS_TLS13(s)) {
651 if (!s->method->ssl3_enc->change_cipher_state(s,
652 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE))
653 return WORK_ERROR;
654 }
61ae935a 655 break;
61ae935a
MC
656 }
657
658 return WORK_FINISHED_CONTINUE;
659}
660
661/*
6392fb8e
MC
662 * Get the message construction function and message type for sending from the
663 * client
61ae935a
MC
664 *
665 * Valid return values are:
666 * 1: Success
667 * 0: Error
668 */
6392fb8e 669int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt,
a15c953f 670 confunc_f *confunc, int *mt)
61ae935a 671{
d6f1a6e9 672 OSSL_STATEM *st = &s->statem;
61ae935a 673
4a01c59f
MC
674 switch (st->hand_state) {
675 default:
676 /* Shouldn't happen */
677 return 0;
678
679 case TLS_ST_CW_CHANGE:
5923ad4b 680 if (SSL_IS_DTLS(s))
6392fb8e 681 *confunc = dtls_construct_change_cipher_spec;
4a01c59f 682 else
6392fb8e
MC
683 *confunc = tls_construct_change_cipher_spec;
684 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
4a01c59f
MC
685 break;
686
687 case TLS_ST_CW_CLNT_HELLO:
6392fb8e
MC
688 *confunc = tls_construct_client_hello;
689 *mt = SSL3_MT_CLIENT_HELLO;
4a01c59f
MC
690 break;
691
692 case TLS_ST_CW_CERT:
6392fb8e
MC
693 *confunc = tls_construct_client_certificate;
694 *mt = SSL3_MT_CERTIFICATE;
4a01c59f
MC
695 break;
696
697 case TLS_ST_CW_KEY_EXCH:
6392fb8e
MC
698 *confunc = tls_construct_client_key_exchange;
699 *mt = SSL3_MT_CLIENT_KEY_EXCHANGE;
4a01c59f
MC
700 break;
701
702 case TLS_ST_CW_CERT_VRFY:
6392fb8e
MC
703 *confunc = tls_construct_client_verify;
704 *mt = SSL3_MT_CERTIFICATE_VERIFY;
4a01c59f 705 break;
61ae935a
MC
706
707#if !defined(OPENSSL_NO_NEXTPROTONEG)
4a01c59f 708 case TLS_ST_CW_NEXT_PROTO:
6392fb8e
MC
709 *confunc = tls_construct_next_proto;
710 *mt = SSL3_MT_NEXT_PROTO;
4a01c59f 711 break;
61ae935a 712#endif
4a01c59f 713 case TLS_ST_CW_FINISHED:
6392fb8e
MC
714 *confunc = tls_construct_finished;
715 *mt = SSL3_MT_FINISHED;
4a01c59f
MC
716 break;
717 }
5923ad4b 718
5923ad4b 719 return 1;
61ae935a
MC
720}
721
722/*
723 * Returns the maximum allowed length for the current message that we are
724 * reading. Excludes the message header.
725 */
eda75751 726size_t ossl_statem_client_max_message_size(SSL *s)
61ae935a 727{
d6f1a6e9 728 OSSL_STATEM *st = &s->statem;
61ae935a 729
a230b26e 730 switch (st->hand_state) {
f3b3d7f0
RS
731 default:
732 /* Shouldn't happen */
733 return 0;
734
a230b26e
EK
735 case TLS_ST_CR_SRVR_HELLO:
736 return SERVER_HELLO_MAX_LENGTH;
61ae935a 737
a230b26e
EK
738 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
739 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
61ae935a 740
a230b26e
EK
741 case TLS_ST_CR_CERT:
742 return s->max_cert_list;
61ae935a 743
a230b26e
EK
744 case TLS_ST_CR_CERT_STATUS:
745 return SSL3_RT_MAX_PLAIN_LENGTH;
61ae935a 746
a230b26e
EK
747 case TLS_ST_CR_KEY_EXCH:
748 return SERVER_KEY_EXCH_MAX_LENGTH;
61ae935a 749
a230b26e
EK
750 case TLS_ST_CR_CERT_REQ:
751 /*
752 * Set to s->max_cert_list for compatibility with previous releases. In
753 * practice these messages can get quite long if servers are configured
754 * to provide a long list of acceptable CAs
755 */
756 return s->max_cert_list;
61ae935a 757
a230b26e
EK
758 case TLS_ST_CR_SRVR_DONE:
759 return SERVER_HELLO_DONE_MAX_LENGTH;
61ae935a 760
a230b26e
EK
761 case TLS_ST_CR_CHANGE:
762 if (s->version == DTLS1_BAD_VER)
763 return 3;
764 return CCS_MAX_LENGTH;
61ae935a 765
a230b26e
EK
766 case TLS_ST_CR_SESSION_TICKET:
767 return SSL3_RT_MAX_PLAIN_LENGTH;
61ae935a 768
a230b26e
EK
769 case TLS_ST_CR_FINISHED:
770 return FINISHED_MAX_LENGTH;
e46f2334
MC
771
772 case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
773 return ENCRYPTED_EXTENSIONS_MAX_LENGTH;
61ae935a 774 }
61ae935a
MC
775}
776
777/*
778 * Process a message that the client has been received from the server.
779 */
8481f583 780MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
61ae935a 781{
d6f1a6e9 782 OSSL_STATEM *st = &s->statem;
61ae935a 783
a230b26e 784 switch (st->hand_state) {
f3b3d7f0
RS
785 default:
786 /* Shouldn't happen */
787 return MSG_PROCESS_ERROR;
788
a230b26e
EK
789 case TLS_ST_CR_SRVR_HELLO:
790 return tls_process_server_hello(s, pkt);
61ae935a 791
a230b26e
EK
792 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
793 return dtls_process_hello_verify(s, pkt);
61ae935a 794
a230b26e
EK
795 case TLS_ST_CR_CERT:
796 return tls_process_server_certificate(s, pkt);
61ae935a 797
a230b26e
EK
798 case TLS_ST_CR_CERT_STATUS:
799 return tls_process_cert_status(s, pkt);
61ae935a 800
a230b26e
EK
801 case TLS_ST_CR_KEY_EXCH:
802 return tls_process_key_exchange(s, pkt);
61ae935a 803
a230b26e
EK
804 case TLS_ST_CR_CERT_REQ:
805 return tls_process_certificate_request(s, pkt);
61ae935a 806
a230b26e
EK
807 case TLS_ST_CR_SRVR_DONE:
808 return tls_process_server_done(s, pkt);
61ae935a 809
a230b26e
EK
810 case TLS_ST_CR_CHANGE:
811 return tls_process_change_cipher_spec(s, pkt);
61ae935a 812
a230b26e
EK
813 case TLS_ST_CR_SESSION_TICKET:
814 return tls_process_new_session_ticket(s, pkt);
61ae935a 815
a230b26e
EK
816 case TLS_ST_CR_FINISHED:
817 return tls_process_finished(s, pkt);
e46f2334
MC
818
819 case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
820 return tls_process_encrypted_extensions(s, pkt);
61ae935a 821 }
61ae935a
MC
822}
823
824/*
825 * Perform any further processing required following the receipt of a message
826 * from the server
827 */
8481f583 828WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
61ae935a 829{
d6f1a6e9 830 OSSL_STATEM *st = &s->statem;
61ae935a 831
a230b26e 832 switch (st->hand_state) {
f3b3d7f0
RS
833 default:
834 /* Shouldn't happen */
835 return WORK_ERROR;
836
05c4f1d5
MC
837 case TLS_ST_CR_CERT_REQ:
838 return tls_prepare_client_certificate(s, wst);
839
61ae935a
MC
840#ifndef OPENSSL_NO_SCTP
841 case TLS_ST_CR_SRVR_DONE:
842 /* We only get here if we are using SCTP and we are renegotiating */
843 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
844 s->s3->in_read_app_data = 2;
845 s->rwstate = SSL_READING;
846 BIO_clear_retry_flags(SSL_get_rbio(s));
847 BIO_set_retry_read(SSL_get_rbio(s));
fe3a3291 848 ossl_statem_set_sctp_read_sock(s, 1);
61ae935a
MC
849 return WORK_MORE_A;
850 }
fe3a3291 851 ossl_statem_set_sctp_read_sock(s, 0);
61ae935a
MC
852 return WORK_FINISHED_STOP;
853#endif
61ae935a 854 }
61ae935a
MC
855}
856
7cea05dc 857int tls_construct_client_hello(SSL *s, WPACKET *pkt)
0f113f3e 858{
2c7b4dbc 859 unsigned char *p;
ec60ccc1
MC
860 size_t sess_id_len;
861 int i, protverr;
2c7b4dbc 862 int al = SSL_AD_HANDSHAKE_FAILURE;
09b6c2ef 863#ifndef OPENSSL_NO_COMP
0f113f3e
MC
864 SSL_COMP *comp;
865#endif
b9908bf9 866 SSL_SESSION *sess = s->session;
0f113f3e 867
7cea05dc 868 if (!WPACKET_set_max_size(pkt, SSL3_RT_MAX_PLAIN_LENGTH)) {
2c7b4dbc
MC
869 /* Should not happen */
870 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
7cea05dc 871 return 0;
2c7b4dbc 872 }
0f113f3e 873
b9908bf9 874 /* Work out what SSL/TLS/DTLS version to use */
4fa52141
VD
875 protverr = ssl_set_client_hello_version(s);
876 if (protverr != 0) {
877 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr);
7cea05dc 878 return 0;
4fa52141 879 }
0f113f3e 880
a230b26e 881 if ((sess == NULL) || !ssl_version_supported(s, sess->ssl_version) ||
0f113f3e 882 /*
b9908bf9
MC
883 * In the case of EAP-FAST, we can have a pre-shared
884 * "ticket" without a session ID.
0f113f3e 885 */
b9908bf9
MC
886 (!sess->session_id_length && !sess->tlsext_tick) ||
887 (sess->not_resumable)) {
888 if (!ssl_get_new_session(s, 0))
7cea05dc 889 return 0;
b9908bf9
MC
890 }
891 /* else use the pre-loaded session */
0f113f3e 892
b9908bf9 893 p = s->s3->client_random;
0f113f3e 894
b9908bf9
MC
895 /*
896 * for DTLS if client_random is initialized, reuse it, we are
897 * required to use same upon reply to HelloVerify
898 */
899 if (SSL_IS_DTLS(s)) {
900 size_t idx;
901 i = 1;
902 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
903 if (p[idx]) {
904 i = 0;
905 break;
0f113f3e 906 }
0f113f3e 907 }
b9908bf9
MC
908 } else
909 i = 1;
0f113f3e 910
a230b26e 911 if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random)) <= 0)
7cea05dc 912 return 0;
b9908bf9 913
b9908bf9
MC
914 /*-
915 * version indicates the negotiated version: for example from
916 * an SSLv2/v3 compatible client hello). The client_version
917 * field is the maximum version we permit and it is also
918 * used in RSA encrypted premaster secrets. Some servers can
919 * choke if we initially report a higher version then
920 * renegotiate to a lower one in the premaster secret. This
921 * didn't happen with TLS 1.0 as most servers supported it
922 * but it can with TLS 1.1 or later if the server only supports
923 * 1.0.
924 *
925 * Possible scenario with previous logic:
926 * 1. Client hello indicates TLS 1.2
927 * 2. Server hello says TLS 1.0
928 * 3. RSA encrypted premaster secret uses 1.2.
8483a003 929 * 4. Handshake proceeds using TLS 1.0.
b9908bf9
MC
930 * 5. Server sends hello request to renegotiate.
931 * 6. Client hello indicates TLS v1.0 as we now
932 * know that is maximum server supports.
933 * 7. Server chokes on RSA encrypted premaster secret
934 * containing version 1.0.
935 *
936 * For interoperability it should be OK to always use the
937 * maximum version we support in client hello and then rely
938 * on the checking of version to ensure the servers isn't
939 * being inconsistent: for example initially negotiating with
940 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
941 * client_version in client hello and not resetting it to
942 * the negotiated version.
cd998837
MC
943 *
944 * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the
16bce0e0 945 * supported_versions extension for the real supported versions.
b9908bf9 946 */
7acb8b64 947 if (!WPACKET_put_bytes_u16(pkt, s->client_version)
7cea05dc 948 || !WPACKET_memcpy(pkt, s->s3->client_random, SSL3_RANDOM_SIZE)) {
2c7b4dbc 949 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
7cea05dc 950 return 0;
2c7b4dbc 951 }
b9908bf9
MC
952
953 /* Session ID */
954 if (s->new_session)
ec60ccc1 955 sess_id_len = 0;
b9908bf9 956 else
ec60ccc1
MC
957 sess_id_len = s->session->session_id_length;
958 if (sess_id_len > sizeof(s->session->session_id)
7cea05dc 959 || !WPACKET_start_sub_packet_u8(pkt)
ec60ccc1
MC
960 || (sess_id_len != 0 && !WPACKET_memcpy(pkt, s->session->session_id,
961 sess_id_len))
7cea05dc 962 || !WPACKET_close(pkt)) {
2c7b4dbc 963 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
7cea05dc 964 return 0;
b9908bf9 965 }
0f113f3e 966
b9908bf9
MC
967 /* cookie stuff for DTLS */
968 if (SSL_IS_DTLS(s)) {
2c7b4dbc 969 if (s->d1->cookie_len > sizeof(s->d1->cookie)
7cea05dc 970 || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie,
b2b3024e 971 s->d1->cookie_len)) {
b9908bf9 972 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
7cea05dc 973 return 0;
0f113f3e 974 }
b9908bf9
MC
975 }
976
977 /* Ciphers supported */
7cea05dc 978 if (!WPACKET_start_sub_packet_u16(pkt)) {
2c7b4dbc 979 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
7cea05dc 980 return 0;
2c7b4dbc
MC
981 }
982 /* ssl_cipher_list_to_bytes() raises SSLerr if appropriate */
7cea05dc
MC
983 if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), pkt))
984 return 0;
985 if (!WPACKET_close(pkt)) {
2c7b4dbc 986 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
7cea05dc 987 return 0;
b9908bf9 988 }
0f113f3e 989
b9908bf9 990 /* COMPRESSION */
7cea05dc 991 if (!WPACKET_start_sub_packet_u8(pkt)) {
2c7b4dbc 992 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
7cea05dc 993 return 0;
2c7b4dbc
MC
994 }
995#ifndef OPENSSL_NO_COMP
996 if (ssl_allow_compression(s) && s->ctx->comp_methods) {
997 int compnum = sk_SSL_COMP_num(s->ctx->comp_methods);
998 for (i = 0; i < compnum; i++) {
999 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
7cea05dc 1000 if (!WPACKET_put_bytes_u8(pkt, comp->id)) {
2c7b4dbc 1001 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
7cea05dc 1002 return 0;
2c7b4dbc
MC
1003 }
1004 }
b9908bf9 1005 }
09b6c2ef 1006#endif
2c7b4dbc 1007 /* Add the NULL method */
7cea05dc 1008 if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) {
2c7b4dbc 1009 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
7cea05dc 1010 return 0;
2c7b4dbc 1011 }
761772d7 1012
b9908bf9 1013 /* TLS extensions */
ab83e314 1014 if (!tls_construct_extensions(s, pkt, EXT_CLIENT_HELLO, &al)) {
b9908bf9
MC
1015 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1016 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
7cea05dc 1017 return 0;
b9908bf9 1018 }
0f113f3e 1019
b9908bf9 1020 return 1;
0f113f3e 1021}
d02b48c6 1022
be3583fa 1023MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
8ba708e5
MC
1024{
1025 int al;
cb150cbc 1026 size_t cookie_len;
8ba708e5
MC
1027 PACKET cookiepkt;
1028
1029 if (!PACKET_forward(pkt, 2)
a230b26e 1030 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
8ba708e5
MC
1031 al = SSL_AD_DECODE_ERROR;
1032 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1033 goto f_err;
1034 }
1035
1036 cookie_len = PACKET_remaining(&cookiepkt);
1037 if (cookie_len > sizeof(s->d1->cookie)) {
1038 al = SSL_AD_ILLEGAL_PARAMETER;
1039 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
1040 goto f_err;
1041 }
1042
1043 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
1044 al = SSL_AD_DECODE_ERROR;
1045 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1046 goto f_err;
1047 }
1048 s->d1->cookie_len = cookie_len;
1049
1050 return MSG_PROCESS_FINISHED_READING;
1051 f_err:
1052 ssl3_send_alert(s, SSL3_AL_FATAL, al);
fe3a3291 1053 ossl_statem_set_error(s);
8ba708e5
MC
1054 return MSG_PROCESS_ERROR;
1055}
1056
be3583fa 1057MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
b9908bf9
MC
1058{
1059 STACK_OF(SSL_CIPHER) *sk;
1060 const SSL_CIPHER *c;
332eb390 1061 PACKET session_id, extpkt;
b9908bf9 1062 size_t session_id_len;
b6981744 1063 const unsigned char *cipherchars;
b9908bf9
MC
1064 int i, al = SSL_AD_INTERNAL_ERROR;
1065 unsigned int compression;
4fa52141 1066 unsigned int sversion;
3434f40b 1067 unsigned int context;
4fa52141 1068 int protverr;
332eb390 1069 RAW_EXTENSION *extensions = NULL;
b9908bf9
MC
1070#ifndef OPENSSL_NO_COMP
1071 SSL_COMP *comp;
1072#endif
1073
4fa52141
VD
1074 if (!PACKET_get_net_2(pkt, &sversion)) {
1075 al = SSL_AD_DECODE_ERROR;
1076 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1077 goto f_err;
1078 }
50932c4a 1079
4fa52141
VD
1080 protverr = ssl_choose_client_version(s, sversion);
1081 if (protverr != 0) {
1082 al = SSL_AD_PROTOCOL_VERSION;
1083 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr);
1084 goto f_err;
0f113f3e 1085 }
0f113f3e
MC
1086
1087 /* load the server hello data */
1088 /* load the server random */
73999b62 1089 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
50932c4a 1090 al = SSL_AD_DECODE_ERROR;
b9908bf9 1091 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
50932c4a
MC
1092 goto f_err;
1093 }
0f113f3e
MC
1094
1095 s->hit = 0;
1096
fc5ce51d 1097 /* Get the session-id. */
71728dd8
MC
1098 if (!SSL_IS_TLS13(s)) {
1099 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
1100 al = SSL_AD_DECODE_ERROR;
1101 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1102 goto f_err;
1103 }
1104 session_id_len = PACKET_remaining(&session_id);
1105 if (session_id_len > sizeof s->session->session_id
1106 || session_id_len > SSL3_SESSION_ID_SIZE) {
1107 al = SSL_AD_ILLEGAL_PARAMETER;
1108 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1109 SSL_R_SSL3_SESSION_ID_TOO_LONG);
1110 goto f_err;
1111 }
1112 } else {
1113 session_id_len = 0;
0f113f3e 1114 }
e481f9b9 1115
73999b62 1116 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
f0659bdb 1117 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
fc5ce51d
EK
1118 al = SSL_AD_DECODE_ERROR;
1119 goto f_err;
1120 }
1121
0f113f3e 1122 /*
6e3d0153
EK
1123 * Check if we can resume the session based on external pre-shared secret.
1124 * EAP-FAST (RFC 4851) supports two types of session resumption.
1125 * Resumption based on server-side state works with session IDs.
1126 * Resumption based on pre-shared Protected Access Credentials (PACs)
1127 * works by overriding the SessionTicket extension at the application
1128 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1129 * servers would honour the session ID.) Therefore, the session ID alone
1130 * is not a reliable indicator of session resumption, so we first check if
1131 * we can resume, and later peek at the next handshake message to see if the
1132 * server wants to resume.
0f113f3e 1133 */
71728dd8
MC
1134 if (s->version >= TLS1_VERSION && !SSL_IS_TLS13(s)
1135 && s->tls_session_secret_cb && s->session->tlsext_tick) {
4a640fb6 1136 const SSL_CIPHER *pref_cipher = NULL;
8c1a5343
MC
1137 /*
1138 * s->session->master_key_length is a size_t, but this is an int for
1139 * backwards compat reasons
1140 */
1141 int master_key_length;
1142 master_key_length = sizeof(s->session->master_key);
0f113f3e 1143 if (s->tls_session_secret_cb(s, s->session->master_key,
8c1a5343 1144 &master_key_length,
0f113f3e 1145 NULL, &pref_cipher,
8c1a5343
MC
1146 s->tls_session_secret_cb_arg)
1147 && master_key_length > 0) {
1148 s->session->master_key_length = master_key_length;
0f113f3e 1149 s->session->cipher = pref_cipher ?
50932c4a 1150 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
6e3d0153 1151 } else {
b9908bf9 1152 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
6e3d0153
EK
1153 al = SSL_AD_INTERNAL_ERROR;
1154 goto f_err;
0f113f3e 1155 }
50932c4a
MC
1156 }
1157
fc5ce51d
EK
1158 if (session_id_len != 0 && session_id_len == s->session->session_id_length
1159 && memcmp(PACKET_data(&session_id), s->session->session_id,
1160 session_id_len) == 0) {
0f113f3e
MC
1161 if (s->sid_ctx_length != s->session->sid_ctx_length
1162 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1163 /* actually a client application bug */
1164 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1165 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
1166 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1167 goto f_err;
1168 }
1169 s->hit = 1;
6e3d0153 1170 } else {
0f113f3e 1171 /*
6e3d0153
EK
1172 * If we were trying for session-id reuse but the server
1173 * didn't echo the ID, make a new SSL_SESSION.
1174 * In the case of EAP-FAST and PAC, we do not send a session ID,
1175 * so the PAC-based session secret is always preserved. It'll be
1176 * overwritten if the server refuses resumption.
0f113f3e
MC
1177 */
1178 if (s->session->session_id_length > 0) {
4f6eaa59 1179 s->ctx->stats.sess_miss++;
0f113f3e
MC
1180 if (!ssl_get_new_session(s, 0)) {
1181 goto f_err;
1182 }
1183 }
50932c4a 1184
ccae4a15 1185 s->session->ssl_version = s->version;
fc5ce51d
EK
1186 s->session->session_id_length = session_id_len;
1187 /* session_id_len could be 0 */
1188 memcpy(s->session->session_id, PACKET_data(&session_id),
1189 session_id_len);
0f113f3e 1190 }
fc5ce51d 1191
ccae4a15
FI
1192 /* Session version and negotiated protocol version should match */
1193 if (s->version != s->session->ssl_version) {
1194 al = SSL_AD_PROTOCOL_VERSION;
1195
1196 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1197 SSL_R_SSL_SESSION_VERSION_MISMATCH);
1198 goto f_err;
1199 }
1200
50932c4a 1201 c = ssl_get_cipher_by_char(s, cipherchars);
0f113f3e
MC
1202 if (c == NULL) {
1203 /* unknown cipher */
1204 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1205 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
0f113f3e
MC
1206 goto f_err;
1207 }
0f113f3e 1208 /*
3eb2aff4
KR
1209 * Now that we know the version, update the check to see if it's an allowed
1210 * version.
1211 */
1212 s->s3->tmp.min_ver = s->version;
1213 s->s3->tmp.max_ver = s->version;
1214 /*
1215 * If it is a disabled cipher we either didn't send it in client hello,
1216 * or it's not allowed for the selected protocol. So we return an error.
0f113f3e
MC
1217 */
1218 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1219 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1220 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
0f113f3e
MC
1221 goto f_err;
1222 }
0f113f3e
MC
1223
1224 sk = ssl_get_ciphers_by_id(s);
1225 i = sk_SSL_CIPHER_find(sk, c);
1226 if (i < 0) {
1227 /* we did not say we would use this cipher */
1228 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1229 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
0f113f3e
MC
1230 goto f_err;
1231 }
1232
1233 /*
1234 * Depending on the session caching (internal/external), the cipher
1235 * and/or cipher_id values may not be set. Make sure that cipher_id is
1236 * set and use it for comparison.
1237 */
1238 if (s->session->cipher)
1239 s->session->cipher_id = s->session->cipher->id;
1240 if (s->hit && (s->session->cipher_id != c->id)) {
9e9858d1 1241 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1242 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
9e9858d1
RS
1243 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1244 goto f_err;
0f113f3e
MC
1245 }
1246 s->s3->tmp.new_cipher = c;
0f113f3e
MC
1247 /* lets get the compression algorithm */
1248 /* COMPRESSION */
71728dd8
MC
1249 if (!SSL_IS_TLS13(s)) {
1250 if (!PACKET_get_1(pkt, &compression)) {
1251 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1252 al = SSL_AD_DECODE_ERROR;
1253 goto f_err;
1254 }
1255 } else {
1256 compression = 0;
50932c4a 1257 }
71728dd8 1258
09b6c2ef 1259#ifdef OPENSSL_NO_COMP
fc5ce51d 1260 if (compression != 0) {
0f113f3e 1261 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1262 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
1263 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1264 goto f_err;
1265 }
1266 /*
1267 * If compression is disabled we'd better not try to resume a session
1268 * using compression.
1269 */
1270 if (s->session->compress_meth != 0) {
b9908bf9 1271 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
0f113f3e
MC
1272 goto f_err;
1273 }
09b6c2ef 1274#else
fc5ce51d 1275 if (s->hit && compression != s->session->compress_meth) {
0f113f3e 1276 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1277 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
1278 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1279 goto f_err;
1280 }
fc5ce51d 1281 if (compression == 0)
0f113f3e
MC
1282 comp = NULL;
1283 else if (!ssl_allow_compression(s)) {
1284 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1285 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
0f113f3e 1286 goto f_err;
fc5ce51d
EK
1287 } else {
1288 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1289 }
0f113f3e 1290
fc5ce51d 1291 if (compression != 0 && comp == NULL) {
0f113f3e 1292 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1293 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
1294 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1295 goto f_err;
1296 } else {
1297 s->s3->tmp.new_compression = comp;
1298 }
09b6c2ef 1299#endif
761772d7 1300
0f113f3e 1301 /* TLS extensions */
332eb390
MC
1302 if (PACKET_remaining(pkt) == 0) {
1303 PACKET_null_init(&extpkt);
1304 } else if (!PACKET_as_length_prefixed_2(pkt, &extpkt)) {
0f113f3e 1305 al = SSL_AD_DECODE_ERROR;
332eb390 1306 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_LENGTH);
0f113f3e
MC
1307 goto f_err;
1308 }
332eb390 1309
3434f40b
MC
1310 context = SSL_IS_TLS13(s) ? EXT_TLS1_3_SERVER_HELLO
1311 : EXT_TLS1_2_SERVER_HELLO;
1312 if (!tls_collect_extensions(s, &extpkt, context, &extensions, &al)
1313 || !tls_parse_all_extensions(s, context, extensions, &al))
332eb390
MC
1314 goto f_err;
1315
8723588e
MC
1316#ifndef OPENSSL_NO_SCTP
1317 if (SSL_IS_DTLS(s) && s->hit) {
1318 unsigned char sctpauthkey[64];
1319 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1320
1321 /*
1322 * Add new shared key for SCTP-Auth, will be ignored if
1323 * no SCTP used.
1324 */
141eb8c6
MC
1325 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1326 sizeof(DTLS1_SCTP_AUTH_LABEL));
8723588e
MC
1327
1328 if (SSL_export_keying_material(s, sctpauthkey,
a230b26e
EK
1329 sizeof(sctpauthkey),
1330 labelbuffer,
1331 sizeof(labelbuffer), NULL, 0, 0) <= 0)
8723588e
MC
1332 goto err;
1333
1334 BIO_ctrl(SSL_get_wbio(s),
1335 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1336 sizeof(sctpauthkey), sctpauthkey);
1337 }
1338#endif
1339
92760c21
MC
1340 /*
1341 * In TLSv1.3 we have some post-processing to change cipher state, otherwise
1342 * we're done with this message
1343 */
1344 if (SSL_IS_TLS13(s)
1345 && (!s->method->ssl3_enc->setup_key_block(s)
1346 || !s->method->ssl3_enc->change_cipher_state(s,
1347 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE)
1348 || !s->method->ssl3_enc->change_cipher_state(s,
1349 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_READ))) {
1350 al = SSL_AD_INTERNAL_ERROR;
1351 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_CANNOT_CHANGE_CIPHER);
1352 goto f_err;
1353 }
1354
b9908bf9 1355 return MSG_PROCESS_CONTINUE_READING;
0f113f3e
MC
1356 f_err:
1357 ssl3_send_alert(s, SSL3_AL_FATAL, al);
fe3a3291 1358 ossl_statem_set_error(s);
b9908bf9 1359 return MSG_PROCESS_ERROR;
0f113f3e 1360}
d02b48c6 1361
be3583fa 1362MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
b9908bf9
MC
1363{
1364 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
1365 unsigned long cert_list_len, cert_len;
1366 X509 *x = NULL;
b6981744 1367 const unsigned char *certstart, *certbytes;
b9908bf9
MC
1368 STACK_OF(X509) *sk = NULL;
1369 EVP_PKEY *pkey = NULL;
0f113f3e
MC
1370
1371 if ((sk = sk_X509_new_null()) == NULL) {
b9908bf9 1372 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
cc273a93 1373 goto err;
0f113f3e
MC
1374 }
1375
73999b62 1376 if (!PACKET_get_net_3(pkt, &cert_list_len)
a230b26e 1377 || PACKET_remaining(pkt) != cert_list_len) {
0f113f3e 1378 al = SSL_AD_DECODE_ERROR;
b9908bf9 1379 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1380 goto f_err;
1381 }
73999b62
MC
1382 while (PACKET_remaining(pkt)) {
1383 if (!PACKET_get_net_3(pkt, &cert_len)
a230b26e 1384 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
0f113f3e 1385 al = SSL_AD_DECODE_ERROR;
b9908bf9 1386 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
1387 SSL_R_CERT_LENGTH_MISMATCH);
1388 goto f_err;
1389 }
1390
df758a85
MC
1391 certstart = certbytes;
1392 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
0f113f3e
MC
1393 if (x == NULL) {
1394 al = SSL_AD_BAD_CERTIFICATE;
b9908bf9 1395 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
0f113f3e
MC
1396 goto f_err;
1397 }
df758a85 1398 if (certbytes != (certstart + cert_len)) {
0f113f3e 1399 al = SSL_AD_DECODE_ERROR;
b9908bf9 1400 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
1401 SSL_R_CERT_LENGTH_MISMATCH);
1402 goto f_err;
1403 }
1404 if (!sk_X509_push(sk, x)) {
b9908bf9 1405 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
cc273a93 1406 goto err;
0f113f3e
MC
1407 }
1408 x = NULL;
0f113f3e
MC
1409 }
1410
1411 i = ssl_verify_cert_chain(s, sk);
c8e2f98c
MC
1412 /*
1413 * The documented interface is that SSL_VERIFY_PEER should be set in order
1414 * for client side verification of the server certificate to take place.
1415 * However, historically the code has only checked that *any* flag is set
1416 * to cause server verification to take place. Use of the other flags makes
1417 * no sense in client mode. An attempt to clean up the semantics was
1418 * reverted because at least one application *only* set
1419 * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
1420 * server verification to take place, after the clean up it silently did
1421 * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
1422 * sent to them because they are void functions. Therefore, we now use the
1423 * (less clean) historic behaviour of performing validation if any flag is
1424 * set. The *documented* interface remains the same.
1425 */
1426 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
0f113f3e 1427 al = ssl_verify_alarm_type(s->verify_result);
b9908bf9 1428 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
1429 SSL_R_CERTIFICATE_VERIFY_FAILED);
1430 goto f_err;
1431 }
1432 ERR_clear_error(); /* but we keep s->verify_result */
1433 if (i > 1) {
b9908bf9 1434 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
0f113f3e
MC
1435 al = SSL_AD_HANDSHAKE_FAILURE;
1436 goto f_err;
1437 }
1438
c34b0f99 1439 s->session->peer_chain = sk;
0f113f3e
MC
1440 /*
1441 * Inconsistency alert: cert_chain does include the peer's certificate,
d4d78943 1442 * which we don't include in statem_srvr.c
0f113f3e
MC
1443 */
1444 x = sk_X509_value(sk, 0);
1445 sk = NULL;
1446 /*
1447 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1448 */
1449
8382fd3a 1450 pkey = X509_get0_pubkey(x);
0f113f3e 1451
55a9a16f 1452 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
0f113f3e
MC
1453 x = NULL;
1454 al = SSL3_AL_FATAL;
b9908bf9 1455 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
1456 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1457 goto f_err;
1458 }
1459
1460 i = ssl_cert_type(x, pkey);
55a9a16f 1461 if (i < 0) {
0f113f3e
MC
1462 x = NULL;
1463 al = SSL3_AL_FATAL;
b9908bf9 1464 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
1465 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1466 goto f_err;
1467 }
1468
55a9a16f 1469 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
e44380a9 1470 if (exp_idx >= 0 && i != exp_idx
a230b26e
EK
1471 && (exp_idx != SSL_PKEY_GOST_EC ||
1472 (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256
1473 && i != SSL_PKEY_GOST01))) {
55a9a16f
MC
1474 x = NULL;
1475 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1476 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
55a9a16f
MC
1477 SSL_R_WRONG_CERTIFICATE_TYPE);
1478 goto f_err;
0f113f3e 1479 }
a273c6ee 1480 s->session->peer_type = i;
55a9a16f
MC
1481
1482 X509_free(s->session->peer);
05f0fb9f 1483 X509_up_ref(x);
55a9a16f 1484 s->session->peer = x;
0f113f3e
MC
1485 s->session->verify_result = s->verify_result;
1486
1487 x = NULL;
b9908bf9 1488 ret = MSG_PROCESS_CONTINUE_READING;
66696478
RS
1489 goto done;
1490
0f113f3e 1491 f_err:
66696478 1492 ssl3_send_alert(s, SSL3_AL_FATAL, al);
cc273a93 1493 err:
fe3a3291 1494 ossl_statem_set_error(s);
66696478 1495 done:
0f113f3e
MC
1496 X509_free(x);
1497 sk_X509_pop_free(sk, X509_free);
b9908bf9 1498 return ret;
0f113f3e 1499}
d02b48c6 1500
7dc1c647 1501static int tls_process_ske_psk_preamble(SSL *s, PACKET *pkt, int *al)
02a74590
MC
1502{
1503#ifndef OPENSSL_NO_PSK
7dc1c647 1504 PACKET psk_identity_hint;
02a74590 1505
7dc1c647
MC
1506 /* PSK ciphersuites are preceded by an identity hint */
1507
1508 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
1509 *al = SSL_AD_DECODE_ERROR;
4fa88861 1510 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH);
7dc1c647
MC
1511 return 0;
1512 }
1513
1514 /*
1515 * Store PSK identity hint for later use, hint is used in
1516 * tls_construct_client_key_exchange. Assume that the maximum length of
1517 * a PSK identity hint can be as long as the maximum length of a PSK
1518 * identity.
1519 */
1520 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1521 *al = SSL_AD_HANDSHAKE_FAILURE;
4fa88861 1522 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG);
7dc1c647
MC
1523 return 0;
1524 }
02a74590 1525
7dc1c647
MC
1526 if (PACKET_remaining(&psk_identity_hint) == 0) {
1527 OPENSSL_free(s->session->psk_identity_hint);
1528 s->session->psk_identity_hint = NULL;
1529 } else if (!PACKET_strndup(&psk_identity_hint,
a230b26e 1530 &s->session->psk_identity_hint)) {
7dc1c647
MC
1531 *al = SSL_AD_INTERNAL_ERROR;
1532 return 0;
1533 }
1534
1535 return 1;
1536#else
4fa88861 1537 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
7dc1c647
MC
1538 *al = SSL_AD_INTERNAL_ERROR;
1539 return 0;
02a74590
MC
1540#endif
1541}
1542
25c6c10c
MC
1543static int tls_process_ske_srp(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1544{
1545#ifndef OPENSSL_NO_SRP
1546 PACKET prime, generator, salt, server_pub;
1547
1548 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1549 || !PACKET_get_length_prefixed_2(pkt, &generator)
1550 || !PACKET_get_length_prefixed_1(pkt, &salt)
1551 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
1552 *al = SSL_AD_DECODE_ERROR;
4fa88861 1553 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_LENGTH_MISMATCH);
25c6c10c
MC
1554 return 0;
1555 }
1556
348240c6 1557 /* TODO(size_t): Convert BN_bin2bn() calls */
25c6c10c
MC
1558 if ((s->srp_ctx.N =
1559 BN_bin2bn(PACKET_data(&prime),
348240c6 1560 (int)PACKET_remaining(&prime), NULL)) == NULL
25c6c10c
MC
1561 || (s->srp_ctx.g =
1562 BN_bin2bn(PACKET_data(&generator),
348240c6 1563 (int)PACKET_remaining(&generator), NULL)) == NULL
25c6c10c
MC
1564 || (s->srp_ctx.s =
1565 BN_bin2bn(PACKET_data(&salt),
348240c6 1566 (int)PACKET_remaining(&salt), NULL)) == NULL
25c6c10c
MC
1567 || (s->srp_ctx.B =
1568 BN_bin2bn(PACKET_data(&server_pub),
348240c6 1569 (int)PACKET_remaining(&server_pub), NULL)) == NULL) {
25c6c10c 1570 *al = SSL_AD_INTERNAL_ERROR;
4fa88861 1571 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_BN_LIB);
25c6c10c
MC
1572 return 0;
1573 }
1574
1575 if (!srp_verify_server_param(s, al)) {
1576 *al = SSL_AD_DECODE_ERROR;
4fa88861 1577 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_BAD_SRP_PARAMETERS);
25c6c10c
MC
1578 return 0;
1579 }
1580
1581 /* We must check if there is a certificate */
a230b26e 1582 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
25c6c10c
MC
1583 *pkey = X509_get0_pubkey(s->session->peer);
1584
1585 return 1;
1586#else
4fa88861 1587 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_INTERNAL_ERROR);
25c6c10c
MC
1588 *al = SSL_AD_INTERNAL_ERROR;
1589 return 0;
1590#endif
1591}
1592
e01a610d
MC
1593static int tls_process_ske_dhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1594{
1595#ifndef OPENSSL_NO_DH
1596 PACKET prime, generator, pub_key;
1597 EVP_PKEY *peer_tmp = NULL;
1598
1599 DH *dh = NULL;
1600 BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
1601
1602 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1603 || !PACKET_get_length_prefixed_2(pkt, &generator)
1604 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
1605 *al = SSL_AD_DECODE_ERROR;
4fa88861 1606 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_LENGTH_MISMATCH);
e01a610d
MC
1607 return 0;
1608 }
1609
1610 peer_tmp = EVP_PKEY_new();
1611 dh = DH_new();
1612
1613 if (peer_tmp == NULL || dh == NULL) {
1614 *al = SSL_AD_INTERNAL_ERROR;
4fa88861 1615 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_MALLOC_FAILURE);
e01a610d
MC
1616 goto err;
1617 }
1618
348240c6
MC
1619 /* TODO(size_t): Convert these calls */
1620 p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL);
1621 g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator),
1622 NULL);
1623 bnpub_key = BN_bin2bn(PACKET_data(&pub_key),
1624 (int)PACKET_remaining(&pub_key), NULL);
e01a610d
MC
1625 if (p == NULL || g == NULL || bnpub_key == NULL) {
1626 *al = SSL_AD_INTERNAL_ERROR;
4fa88861 1627 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
e01a610d
MC
1628 goto err;
1629 }
1630
1631 if (BN_is_zero(p) || BN_is_zero(g) || BN_is_zero(bnpub_key)) {
1632 *al = SSL_AD_DECODE_ERROR;
4fa88861 1633 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_BAD_DH_VALUE);
e01a610d
MC
1634 goto err;
1635 }
1636
1637 if (!DH_set0_pqg(dh, p, NULL, g)) {
1638 *al = SSL_AD_INTERNAL_ERROR;
4fa88861 1639 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
e01a610d
MC
1640 goto err;
1641 }
1642 p = g = NULL;
1643
1644 if (!DH_set0_key(dh, bnpub_key, NULL)) {
1645 *al = SSL_AD_INTERNAL_ERROR;
4fa88861 1646 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
e01a610d
MC
1647 goto err;
1648 }
1649 bnpub_key = NULL;
1650
1651 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1652 *al = SSL_AD_HANDSHAKE_FAILURE;
4fa88861 1653 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_DH_KEY_TOO_SMALL);
e01a610d
MC
1654 goto err;
1655 }
1656
1657 if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) {
1658 *al = SSL_AD_INTERNAL_ERROR;
4fa88861 1659 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_EVP_LIB);
e01a610d
MC
1660 goto err;
1661 }
1662
1663 s->s3->peer_tmp = peer_tmp;
1664
1665 /*
1666 * FIXME: This makes assumptions about which ciphersuites come with
1667 * public keys. We should have a less ad-hoc way of doing this
1668 */
a230b26e 1669 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
e01a610d
MC
1670 *pkey = X509_get0_pubkey(s->session->peer);
1671 /* else anonymous DH, so no certificate or pkey. */
1672
1673 return 1;
1674
1675 err:
1676 BN_free(p);
1677 BN_free(g);
1678 BN_free(bnpub_key);
1679 DH_free(dh);
1680 EVP_PKEY_free(peer_tmp);
1681
1682 return 0;
1683#else
4fa88861 1684 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_INTERNAL_ERROR);
e01a610d
MC
1685 *al = SSL_AD_INTERNAL_ERROR;
1686 return 0;
1687#endif
1688}
1689
ff74aeb1
MC
1690static int tls_process_ske_ecdhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1691{
1692#ifndef OPENSSL_NO_EC
1693 PACKET encoded_pt;
1694 const unsigned char *ecparams;
1695 int curve_nid;
ec24630a 1696 unsigned int curve_flags;
ff74aeb1
MC
1697 EVP_PKEY_CTX *pctx = NULL;
1698
1699 /*
1700 * Extract elliptic curve parameters and the server's ephemeral ECDH
1701 * public key. For now we only support named (not generic) curves and
1702 * ECParameters in this case is just three bytes.
1703 */
1704 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
1705 *al = SSL_AD_DECODE_ERROR;
4fa88861 1706 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_TOO_SHORT);
ff74aeb1
MC
1707 return 0;
1708 }
1709 /*
1710 * Check curve is one of our preferences, if not server has sent an
1711 * invalid curve. ECParameters is 3 bytes.
1712 */
1713 if (!tls1_check_curve(s, ecparams, 3)) {
1714 *al = SSL_AD_DECODE_ERROR;
4fa88861 1715 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_WRONG_CURVE);
ff74aeb1
MC
1716 return 0;
1717 }
1718
ec24630a
DSH
1719 curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2), &curve_flags);
1720
a230b26e 1721 if (curve_nid == 0) {
ff74aeb1 1722 *al = SSL_AD_INTERNAL_ERROR;
4fa88861 1723 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE,
ff74aeb1
MC
1724 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1725 return 0;
1726 }
1727
ec24630a
DSH
1728 if ((curve_flags & TLS_CURVE_TYPE) == TLS_CURVE_CUSTOM) {
1729 EVP_PKEY *key = EVP_PKEY_new();
1730
1731 if (key == NULL || !EVP_PKEY_set_type(key, curve_nid)) {
1732 *al = SSL_AD_INTERNAL_ERROR;
1733 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB);
1734 EVP_PKEY_free(key);
1735 return 0;
1736 }
1737 s->s3->peer_tmp = key;
1738 } else {
1739 /* Set up EVP_PKEY with named curve as parameters */
1740 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
1741 if (pctx == NULL
1742 || EVP_PKEY_paramgen_init(pctx) <= 0
1743 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0
1744 || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
1745 *al = SSL_AD_INTERNAL_ERROR;
1746 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB);
1747 EVP_PKEY_CTX_free(pctx);
1748 return 0;
1749 }
ff74aeb1 1750 EVP_PKEY_CTX_free(pctx);
ec24630a 1751 pctx = NULL;
ff74aeb1 1752 }
ff74aeb1
MC
1753
1754 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
1755 *al = SSL_AD_DECODE_ERROR;
4fa88861 1756 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_MISMATCH);
ff74aeb1
MC
1757 return 0;
1758 }
1759
ec24630a
DSH
1760 if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp,
1761 PACKET_data(&encoded_pt),
1762 PACKET_remaining(&encoded_pt))) {
ff74aeb1 1763 *al = SSL_AD_DECODE_ERROR;
4fa88861 1764 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_BAD_ECPOINT);
ff74aeb1
MC
1765 return 0;
1766 }
1767
1768 /*
1769 * The ECC/TLS specification does not mention the use of DSA to sign
1770 * ECParameters in the server key exchange message. We do support RSA
1771 * and ECDSA.
1772 */
1773 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aECDSA)
1774 *pkey = X509_get0_pubkey(s->session->peer);
1775 else if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aRSA)
1776 *pkey = X509_get0_pubkey(s->session->peer);
1777 /* else anonymous ECDH, so no certificate or pkey. */
1778
1779 return 1;
1780#else
4fa88861 1781 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_INTERNAL_ERROR);
ff74aeb1
MC
1782 *al = SSL_AD_INTERNAL_ERROR;
1783 return 0;
1784#endif
1785}
1786
be3583fa 1787MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
b9908bf9 1788{
7dc1c647 1789 int al = -1;
e1e588ac 1790 long alg_k;
b9908bf9 1791 EVP_PKEY *pkey = NULL;
73999b62 1792 PACKET save_param_start, signature;
b9908bf9 1793
b9908bf9
MC
1794 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1795
73999b62 1796 save_param_start = *pkt;
8d92c1f8 1797
3260adf1 1798#if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
61dd9f7a
DSH
1799 EVP_PKEY_free(s->s3->peer_tmp);
1800 s->s3->peer_tmp = NULL;
3260adf1 1801#endif
d02b48c6 1802
7689082b 1803 if (alg_k & SSL_PSK) {
7dc1c647
MC
1804 if (!tls_process_ske_psk_preamble(s, pkt, &al))
1805 goto err;
7689082b
DSH
1806 }
1807
1808 /* Nothing else to do for plain PSK or RSAPSK */
1809 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
25c6c10c
MC
1810 } else if (alg_k & SSL_kSRP) {
1811 if (!tls_process_ske_srp(s, pkt, &pkey, &al))
0f113f3e 1812 goto err;
e01a610d
MC
1813 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1814 if (!tls_process_ske_dhe(s, pkt, &pkey, &al))
1815 goto err;
ff74aeb1
MC
1816 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1817 if (!tls_process_ske_ecdhe(s, pkt, &pkey, &al))
1818 goto err;
0f113f3e
MC
1819 } else if (alg_k) {
1820 al = SSL_AD_UNEXPECTED_MESSAGE;
b9908bf9 1821 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
e1e588ac 1822 goto err;
0f113f3e 1823 }
0f113f3e 1824
0f113f3e
MC
1825 /* if it was signed, check the signature */
1826 if (pkey != NULL) {
32942870 1827 PACKET params;
be8dba2c
MC
1828 int maxsig;
1829 const EVP_MD *md = NULL;
e1e588ac
MC
1830 EVP_MD_CTX *md_ctx;
1831
32942870
EK
1832 /*
1833 * |pkt| now points to the beginning of the signature, so the difference
1834 * equals the length of the parameters.
1835 */
1836 if (!PACKET_get_sub_packet(&save_param_start, &params,
1837 PACKET_remaining(&save_param_start) -
73999b62 1838 PACKET_remaining(pkt))) {
32942870 1839 al = SSL_AD_INTERNAL_ERROR;
f0659bdb 1840 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
e1e588ac 1841 goto err;
32942870
EK
1842 }
1843
0f113f3e 1844 if (SSL_USE_SIGALGS(s)) {
b6981744 1845 const unsigned char *sigalgs;
0f113f3e 1846 int rv;
73999b62 1847 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
e1e588ac 1848 al = SSL_AD_DECODE_ERROR;
f0659bdb 1849 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
e1e588ac 1850 goto err;
0f113f3e 1851 }
32942870 1852 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
e1e588ac
MC
1853 if (rv == -1) {
1854 al = SSL_AD_INTERNAL_ERROR;
1855 goto err;
1856 } else if (rv == 0) {
1857 al = SSL_AD_DECODE_ERROR;
0f113f3e 1858 goto err;
0f113f3e 1859 }
a2f9200f 1860#ifdef SSL_DEBUG
0f113f3e
MC
1861 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1862#endif
3aeb9348 1863 } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) {
192e4bbb 1864 md = EVP_md5_sha1();
32942870 1865 } else {
0f113f3e 1866 md = EVP_sha1();
32942870 1867 }
0f113f3e 1868
73999b62
MC
1869 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1870 || PACKET_remaining(pkt) != 0) {
e1e588ac 1871 al = SSL_AD_DECODE_ERROR;
f0659bdb 1872 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
e1e588ac 1873 goto err;
0f113f3e 1874 }
be8dba2c
MC
1875 maxsig = EVP_PKEY_size(pkey);
1876 if (maxsig < 0) {
e1e588ac 1877 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 1878 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
e1e588ac 1879 goto err;
8098fc56 1880 }
0f113f3e
MC
1881
1882 /*
8098fc56 1883 * Check signature length
0f113f3e 1884 */
be8dba2c 1885 if (PACKET_remaining(&signature) > (size_t)maxsig) {
0f113f3e 1886 /* wrong packet length */
e1e588ac 1887 al = SSL_AD_DECODE_ERROR;
a230b26e
EK
1888 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1889 SSL_R_WRONG_SIGNATURE_LENGTH);
e1e588ac
MC
1890 goto err;
1891 }
1892
1893 md_ctx = EVP_MD_CTX_new();
1894 if (md_ctx == NULL) {
1895 al = SSL_AD_INTERNAL_ERROR;
1896 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1897 goto err;
0f113f3e 1898 }
e1e588ac 1899
6e59a892 1900 if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0
a230b26e
EK
1901 || EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]),
1902 SSL3_RANDOM_SIZE) <= 0
1903 || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]),
1904 SSL3_RANDOM_SIZE) <= 0
1905 || EVP_VerifyUpdate(md_ctx, PACKET_data(&params),
1906 PACKET_remaining(&params)) <= 0) {
e1e588ac 1907 EVP_MD_CTX_free(md_ctx);
192e4bbb
DSH
1908 al = SSL_AD_INTERNAL_ERROR;
1909 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
e1e588ac 1910 goto err;
192e4bbb 1911 }
348240c6 1912 /* TODO(size_t): Convert this call */
6e59a892 1913 if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature),
348240c6
MC
1914 (unsigned int)PACKET_remaining(&signature),
1915 pkey) <= 0) {
192e4bbb 1916 /* bad signature */
e1e588ac 1917 EVP_MD_CTX_free(md_ctx);
192e4bbb
DSH
1918 al = SSL_AD_DECRYPT_ERROR;
1919 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
e1e588ac 1920 goto err;
0f113f3e 1921 }
e1e588ac 1922 EVP_MD_CTX_free(md_ctx);
0f113f3e 1923 } else {
7689082b 1924 /* aNULL, aSRP or PSK do not need public keys */
e1e588ac 1925 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
a230b26e 1926 && !(alg_k & SSL_PSK)) {
0f113f3e 1927 /* Might be wrong key type, check it */
e1e588ac 1928 if (ssl3_check_cert_and_algorithm(s)) {
0f113f3e 1929 /* Otherwise this shouldn't happen */
e1e588ac 1930 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 1931 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
e1e588ac
MC
1932 } else {
1933 al = SSL_AD_DECODE_ERROR;
1934 }
0f113f3e
MC
1935 goto err;
1936 }
1937 /* still data left over */
73999b62 1938 if (PACKET_remaining(pkt) != 0) {
e1e588ac 1939 al = SSL_AD_DECODE_ERROR;
b9908bf9 1940 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
e1e588ac 1941 goto err;
0f113f3e
MC
1942 }
1943 }
e1e588ac 1944
b9908bf9 1945 return MSG_PROCESS_CONTINUE_READING;
0f113f3e 1946 err:
7dc1c647
MC
1947 if (al != -1)
1948 ssl3_send_alert(s, SSL3_AL_FATAL, al);
fe3a3291 1949 ossl_statem_set_error(s);
b9908bf9 1950 return MSG_PROCESS_ERROR;
0f113f3e 1951}
d02b48c6 1952
be3583fa 1953MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
b9908bf9
MC
1954{
1955 int ret = MSG_PROCESS_ERROR;
1956 unsigned int list_len, ctype_num, i, name_len;
1957 X509_NAME *xn = NULL;
b6981744
EK
1958 const unsigned char *data;
1959 const unsigned char *namestart, *namebytes;
b9908bf9 1960 STACK_OF(X509_NAME) *ca_sk = NULL;
0f113f3e
MC
1961
1962 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
b9908bf9 1963 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1964 goto err;
1965 }
1966
1967 /* get the certificate types */
73999b62 1968 if (!PACKET_get_1(pkt, &ctype_num)
a230b26e 1969 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
ac112332 1970 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 1971 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
ac112332
MC
1972 goto err;
1973 }
b548a1f1
RS
1974 OPENSSL_free(s->cert->ctypes);
1975 s->cert->ctypes = NULL;
0f113f3e
MC
1976 if (ctype_num > SSL3_CT_NUMBER) {
1977 /* If we exceed static buffer copy all to cert structure */
1978 s->cert->ctypes = OPENSSL_malloc(ctype_num);
1979 if (s->cert->ctypes == NULL) {
b9908bf9 1980 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1981 goto err;
1982 }
ac112332 1983 memcpy(s->cert->ctypes, data, ctype_num);
d736bc1a 1984 s->cert->ctype_num = ctype_num;
0f113f3e
MC
1985 ctype_num = SSL3_CT_NUMBER;
1986 }
1987 for (i = 0; i < ctype_num; i++)
ac112332
MC
1988 s->s3->tmp.ctype[i] = data[i];
1989
0f113f3e 1990 if (SSL_USE_SIGALGS(s)) {
73999b62 1991 if (!PACKET_get_net_2(pkt, &list_len)
a230b26e 1992 || !PACKET_get_bytes(pkt, &data, list_len)) {
0f113f3e 1993 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9
MC
1994 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1995 SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1996 goto err;
1997 }
ac112332 1998
0f113f3e
MC
1999 /* Clear certificate digests and validity flags */
2000 for (i = 0; i < SSL_PKEY_NUM; i++) {
d376e57d 2001 s->s3->tmp.md[i] = NULL;
6383d316 2002 s->s3->tmp.valid_flags[i] = 0;
0f113f3e 2003 }
ac112332 2004 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
0f113f3e 2005 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 2006 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
0f113f3e
MC
2007 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2008 goto err;
2009 }
2010 if (!tls1_process_sigalgs(s)) {
2011 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
b9908bf9 2012 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2013 goto err;
2014 }
a0f63828
DSH
2015 } else {
2016 ssl_set_default_md(s);
0f113f3e
MC
2017 }
2018
2019 /* get the CA RDNs */
73999b62 2020 if (!PACKET_get_net_2(pkt, &list_len)
a230b26e 2021 || PACKET_remaining(pkt) != list_len) {
0f113f3e 2022 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 2023 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
2024 goto err;
2025 }
2026
73999b62
MC
2027 while (PACKET_remaining(pkt)) {
2028 if (!PACKET_get_net_2(pkt, &name_len)
a230b26e 2029 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
0f113f3e 2030 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9
MC
2031 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2032 SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
2033 goto err;
2034 }
2035
ac112332 2036 namestart = namebytes;
0f113f3e 2037
ac112332
MC
2038 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
2039 name_len)) == NULL) {
3c33c6f6 2040 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 2041 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
3c33c6f6 2042 goto err;
0f113f3e
MC
2043 }
2044
ac112332 2045 if (namebytes != (namestart + name_len)) {
0f113f3e 2046 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 2047 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
0f113f3e
MC
2048 SSL_R_CA_DN_LENGTH_MISMATCH);
2049 goto err;
2050 }
2051 if (!sk_X509_NAME_push(ca_sk, xn)) {
b9908bf9 2052 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2053 goto err;
2054 }
6afef8b1 2055 xn = NULL;
0f113f3e
MC
2056 }
2057
0f113f3e
MC
2058 /* we should setup a certificate to return.... */
2059 s->s3->tmp.cert_req = 1;
2060 s->s3->tmp.ctype_num = ctype_num;
222561fe 2061 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
0f113f3e
MC
2062 s->s3->tmp.ca_names = ca_sk;
2063 ca_sk = NULL;
2064
05c4f1d5 2065 ret = MSG_PROCESS_CONTINUE_PROCESSING;
cc273a93 2066 goto done;
0f113f3e 2067 err:
fe3a3291 2068 ossl_statem_set_error(s);
cc273a93 2069 done:
6afef8b1 2070 X509_NAME_free(xn);
222561fe 2071 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
b9908bf9 2072 return ret;
0f113f3e
MC
2073}
2074
2075static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
dfeab068 2076{
0f113f3e 2077 return (X509_NAME_cmp(*a, *b));
dfeab068 2078}
dfeab068 2079
be3583fa 2080MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
b9908bf9
MC
2081{
2082 int al;
2083 unsigned int ticklen;
2084 unsigned long ticket_lifetime_hint;
ec60ccc1 2085 unsigned int sess_len;
b9908bf9 2086
73999b62 2087 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
a230b26e
EK
2088 || !PACKET_get_net_2(pkt, &ticklen)
2089 || PACKET_remaining(pkt) != ticklen) {
e711da71 2090 al = SSL_AD_DECODE_ERROR;
f0659bdb 2091 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
e711da71
EK
2092 goto f_err;
2093 }
2094
2095 /* Server is allowed to change its mind and send an empty ticket. */
2096 if (ticklen == 0)
c9de4a20 2097 return MSG_PROCESS_CONTINUE_READING;
e711da71 2098
98ece4ee
MC
2099 if (s->session->session_id_length > 0) {
2100 int i = s->session_ctx->session_cache_mode;
2101 SSL_SESSION *new_sess;
2102 /*
2103 * We reused an existing session, so we need to replace it with a new
2104 * one
2105 */
2106 if (i & SSL_SESS_CACHE_CLIENT) {
2107 /*
e4612d02 2108 * Remove the old session from the cache. We carry on if this fails
98ece4ee 2109 */
e4612d02 2110 SSL_CTX_remove_session(s->session_ctx, s->session);
98ece4ee
MC
2111 }
2112
2113 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2114 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 2115 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
98ece4ee
MC
2116 goto f_err;
2117 }
2118
2119 SSL_SESSION_free(s->session);
2120 s->session = new_sess;
2121 }
2122
b548a1f1
RS
2123 OPENSSL_free(s->session->tlsext_tick);
2124 s->session->tlsext_ticklen = 0;
e711da71 2125
0f113f3e 2126 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
a71edf3b 2127 if (s->session->tlsext_tick == NULL) {
b9908bf9 2128 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2129 goto err;
2130 }
73999b62 2131 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
561e12bb 2132 al = SSL_AD_DECODE_ERROR;
b9908bf9 2133 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
561e12bb
MC
2134 goto f_err;
2135 }
e711da71
EK
2136
2137 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
0f113f3e
MC
2138 s->session->tlsext_ticklen = ticklen;
2139 /*
2140 * There are two ways to detect a resumed ticket session. One is to set
2141 * an appropriate session ID and then the server must return a match in
2142 * ServerHello. This allows the normal client session ID matching to work
2143 * and we know much earlier that the ticket has been accepted. The
2144 * other way is to set zero length session ID when the ticket is
2145 * presented and rely on the handshake to determine session resumption.
2146 * We choose the former approach because this fits in with assumptions
2147 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2148 * SHA256 is disabled) hash of the ticket.
2149 */
ec60ccc1
MC
2150 /*
2151 * TODO(size_t): we use sess_len here because EVP_Digest expects an int
2152 * but s->session->session_id_length is a size_t
2153 */
d166ed8c 2154 if (!EVP_Digest(s->session->tlsext_tick, ticklen,
ec60ccc1 2155 s->session->session_id, &sess_len,
d166ed8c
DSH
2156 EVP_sha256(), NULL)) {
2157 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_EVP_LIB);
2158 goto err;
2159 }
ec60ccc1 2160 s->session->session_id_length = sess_len;
b9908bf9 2161 return MSG_PROCESS_CONTINUE_READING;
0f113f3e
MC
2162 f_err:
2163 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2164 err:
fe3a3291 2165 ossl_statem_set_error(s);
b9908bf9 2166 return MSG_PROCESS_ERROR;
0f113f3e 2167}
67c8e7f4 2168
be3583fa 2169MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
b9908bf9
MC
2170{
2171 int al;
8b0e934a 2172 size_t resplen;
b9908bf9 2173 unsigned int type;
b9908bf9 2174
73999b62 2175 if (!PACKET_get_1(pkt, &type)
a230b26e 2176 || type != TLSEXT_STATUSTYPE_ocsp) {
0f113f3e 2177 al = SSL_AD_DECODE_ERROR;
b9908bf9 2178 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
0f113f3e
MC
2179 goto f_err;
2180 }
56a26ce3
MC
2181 if (!PACKET_get_net_3_len(pkt, &resplen)
2182 || PACKET_remaining(pkt) != resplen) {
0f113f3e 2183 al = SSL_AD_DECODE_ERROR;
b9908bf9 2184 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
2185 goto f_err;
2186 }
ac63710a 2187 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
a71edf3b 2188 if (s->tlsext_ocsp_resp == NULL) {
0f113f3e 2189 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 2190 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2191 goto f_err;
2192 }
73999b62 2193 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
ac63710a 2194 al = SSL_AD_DECODE_ERROR;
b9908bf9 2195 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
ac63710a
MC
2196 goto f_err;
2197 }
0f113f3e 2198 s->tlsext_ocsp_resplen = resplen;
b9908bf9 2199 return MSG_PROCESS_CONTINUE_READING;
0f113f3e
MC
2200 f_err:
2201 ssl3_send_alert(s, SSL3_AL_FATAL, al);
fe3a3291 2202 ossl_statem_set_error(s);
b9908bf9 2203 return MSG_PROCESS_ERROR;
0f113f3e 2204}
d02b48c6 2205
7776a36c
MC
2206/*
2207 * Perform miscellaneous checks and processing after we have received the
2208 * server's initial flight. In TLS1.3 this is after the Server Finished message.
6530c490
MC
2209 * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0
2210 * on failure.
7776a36c
MC
2211 */
2212int tls_process_initial_server_flight(SSL *s, int *al)
b9908bf9 2213{
a455d0f6
MC
2214 /*
2215 * at this point we check that we have the required stuff from
2216 * the server
2217 */
2218 if (!ssl3_check_cert_and_algorithm(s)) {
7776a36c
MC
2219 *al = SSL_AD_HANDSHAKE_FAILURE;
2220 return 0;
a455d0f6
MC
2221 }
2222
bb1aaab4
MC
2223 /*
2224 * Call the ocsp status callback if needed. The |tlsext_ocsp_resp| and
2225 * |tlsext_ocsp_resplen| values will be set if we actually received a status
2226 * message, or NULL and -1 otherwise
2227 */
b1931d43 2228 if (s->tlsext_status_type != -1 && s->ctx->tlsext_status_cb != NULL) {
bb1aaab4
MC
2229 int ret;
2230 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2231 if (ret == 0) {
7776a36c
MC
2232 *al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2233 SSLerr(SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT,
bb1aaab4 2234 SSL_R_INVALID_STATUS_RESPONSE);
7776a36c 2235 return 0;
bb1aaab4
MC
2236 }
2237 if (ret < 0) {
7776a36c
MC
2238 *al = SSL_AD_INTERNAL_ERROR;
2239 SSLerr(SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT,
2240 ERR_R_MALLOC_FAILURE);
2241 return 0;
bb1aaab4
MC
2242 }
2243 }
ed29e82a
RP
2244#ifndef OPENSSL_NO_CT
2245 if (s->ct_validation_callback != NULL) {
43341433
VD
2246 /* Note we validate the SCTs whether or not we abort on error */
2247 if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
7776a36c
MC
2248 *al = SSL_AD_HANDSHAKE_FAILURE;
2249 return 0;
ed29e82a
RP
2250 }
2251 }
2252#endif
2253
7776a36c
MC
2254 return 1;
2255}
2256
2257MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
2258{
2259 int al = SSL_AD_INTERNAL_ERROR;
2260
2261 if (PACKET_remaining(pkt) > 0) {
2262 /* should contain no data */
2263 al = SSL_AD_DECODE_ERROR;
2264 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2265 goto err;
2266 }
2267#ifndef OPENSSL_NO_SRP
2268 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2269 if (SRP_Calc_A_param(s) <= 0) {
2270 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
2271 goto err;
2272 }
2273 }
2274#endif
2275
2276 /*
2277 * Error queue messages are generated directly by this function
2278 */
2279 if (!tls_process_initial_server_flight(s, &al))
2280 goto err;
2281
473483d4
MC
2282#ifndef OPENSSL_NO_SCTP
2283 /* Only applies to renegotiation */
2284 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
a230b26e 2285 && s->renegotiate != 0)
473483d4
MC
2286 return MSG_PROCESS_CONTINUE_PROCESSING;
2287 else
2288#endif
2289 return MSG_PROCESS_FINISHED_READING;
7776a36c
MC
2290
2291 err:
2292 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2293 ossl_statem_set_error(s);
2294 return MSG_PROCESS_ERROR;
0f113f3e 2295}
176f31dd 2296
f1ec23c0 2297static int tls_construct_cke_psk_preamble(SSL *s, WPACKET *pkt, int *al)
0f113f3e 2298{
7689082b 2299#ifndef OPENSSL_NO_PSK
13c0ec4a
MC
2300 int ret = 0;
2301 /*
2302 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2303 * \0-terminated identity. The last byte is for us for simulating
2304 * strnlen.
2305 */
2306 char identity[PSK_MAX_IDENTITY_LEN + 1];
2307 size_t identitylen = 0;
2308 unsigned char psk[PSK_MAX_PSK_LEN];
2309 unsigned char *tmppsk = NULL;
2310 char *tmpidentity = NULL;
2311 size_t psklen = 0;
2312
2313 if (s->psk_client_callback == NULL) {
05ec6a25 2314 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_CLIENT_CB);
13c0ec4a
MC
2315 *al = SSL_AD_INTERNAL_ERROR;
2316 goto err;
2317 }
d02b48c6 2318
13c0ec4a 2319 memset(identity, 0, sizeof(identity));
d02b48c6 2320
13c0ec4a
MC
2321 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2322 identity, sizeof(identity) - 1,
2323 psk, sizeof(psk));
7689082b 2324
13c0ec4a 2325 if (psklen > PSK_MAX_PSK_LEN) {
05ec6a25 2326 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
13c0ec4a
MC
2327 *al = SSL_AD_HANDSHAKE_FAILURE;
2328 goto err;
2329 } else if (psklen == 0) {
05ec6a25 2330 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
13c0ec4a
MC
2331 SSL_R_PSK_IDENTITY_NOT_FOUND);
2332 *al = SSL_AD_HANDSHAKE_FAILURE;
2333 goto err;
2334 }
7689082b 2335
13c0ec4a
MC
2336 identitylen = strlen(identity);
2337 if (identitylen > PSK_MAX_IDENTITY_LEN) {
05ec6a25 2338 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
13c0ec4a
MC
2339 *al = SSL_AD_HANDSHAKE_FAILURE;
2340 goto err;
2341 }
7689082b 2342
13c0ec4a
MC
2343 tmppsk = OPENSSL_memdup(psk, psklen);
2344 tmpidentity = OPENSSL_strdup(identity);
2345 if (tmppsk == NULL || tmpidentity == NULL) {
05ec6a25 2346 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
13c0ec4a
MC
2347 *al = SSL_AD_INTERNAL_ERROR;
2348 goto err;
2349 }
7689082b 2350
13c0ec4a
MC
2351 OPENSSL_free(s->s3->tmp.psk);
2352 s->s3->tmp.psk = tmppsk;
2353 s->s3->tmp.psklen = psklen;
2354 tmppsk = NULL;
2355 OPENSSL_free(s->session->psk_identity);
2356 s->session->psk_identity = tmpidentity;
2357 tmpidentity = NULL;
f1ec23c0 2358
b2b3024e 2359 if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) {
f1ec23c0
MC
2360 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2361 *al = SSL_AD_INTERNAL_ERROR;
2362 goto err;
2363 }
7689082b 2364
13c0ec4a 2365 ret = 1;
0bce0b02 2366
13c0ec4a
MC
2367 err:
2368 OPENSSL_cleanse(psk, psklen);
2369 OPENSSL_cleanse(identity, sizeof(identity));
2370 OPENSSL_clear_free(tmppsk, psklen);
2371 OPENSSL_clear_free(tmpidentity, identitylen);
d02b48c6 2372
13c0ec4a
MC
2373 return ret;
2374#else
05ec6a25 2375 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
13c0ec4a
MC
2376 *al = SSL_AD_INTERNAL_ERROR;
2377 return 0;
b9908bf9 2378#endif
13c0ec4a 2379}
b9908bf9 2380
f1ec23c0 2381static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt, int *al)
13c0ec4a 2382{
bc36ee62 2383#ifndef OPENSSL_NO_RSA
f1ec23c0 2384 unsigned char *encdata = NULL;
13c0ec4a
MC
2385 EVP_PKEY *pkey = NULL;
2386 EVP_PKEY_CTX *pctx = NULL;
2387 size_t enclen;
2388 unsigned char *pms = NULL;
2389 size_t pmslen = 0;
b9908bf9 2390
13c0ec4a
MC
2391 if (s->session->peer == NULL) {
2392 /*
2393 * We should always have a server certificate with SSL_kRSA.
2394 */
05ec6a25 2395 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
13c0ec4a
MC
2396 return 0;
2397 }
0f113f3e 2398
13c0ec4a
MC
2399 pkey = X509_get0_pubkey(s->session->peer);
2400 if (EVP_PKEY_get0_RSA(pkey) == NULL) {
05ec6a25 2401 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
13c0ec4a
MC
2402 return 0;
2403 }
0f113f3e 2404
13c0ec4a
MC
2405 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2406 pms = OPENSSL_malloc(pmslen);
2407 if (pms == NULL) {
05ec6a25 2408 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE);
13c0ec4a
MC
2409 *al = SSL_AD_INTERNAL_ERROR;
2410 return 0;
2411 }
0bce0b02 2412
13c0ec4a
MC
2413 pms[0] = s->client_version >> 8;
2414 pms[1] = s->client_version & 0xff;
348240c6
MC
2415 /* TODO(size_t): Convert this function */
2416 if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) {
13c0ec4a
MC
2417 goto err;
2418 }
0f113f3e 2419
13c0ec4a 2420 /* Fix buf for TLS and beyond */
f1ec23c0
MC
2421 if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {
2422 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2423 goto err;
2424 }
13c0ec4a
MC
2425 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2426 if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
2427 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
05ec6a25 2428 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB);
13c0ec4a
MC
2429 goto err;
2430 }
f1ec23c0
MC
2431 if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)
2432 || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {
05ec6a25 2433 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT);
13c0ec4a
MC
2434 goto err;
2435 }
13c0ec4a
MC
2436 EVP_PKEY_CTX_free(pctx);
2437 pctx = NULL;
0f113f3e 2438# ifdef PKCS1_CHECK
13c0ec4a
MC
2439 if (s->options & SSL_OP_PKCS1_CHECK_1)
2440 (*p)[1]++;
2441 if (s->options & SSL_OP_PKCS1_CHECK_2)
2442 tmp_buf[0] = 0x70;
0f113f3e 2443# endif
0f113f3e 2444
13c0ec4a 2445 /* Fix buf for TLS and beyond */
f1ec23c0
MC
2446 if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {
2447 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2448 goto err;
b9908bf9 2449 }
13c0ec4a
MC
2450
2451 s->s3->tmp.pms = pms;
2452 s->s3->tmp.pmslen = pmslen;
2453
2454 return 1;
2455 err:
2456 OPENSSL_clear_free(pms, pmslen);
2457 EVP_PKEY_CTX_free(pctx);
2458
2459 return 0;
2460#else
05ec6a25 2461 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
13c0ec4a
MC
2462 *al = SSL_AD_INTERNAL_ERROR;
2463 return 0;
f9b3bff6 2464#endif
13c0ec4a
MC
2465}
2466
f1ec23c0 2467static int tls_construct_cke_dhe(SSL *s, WPACKET *pkt, int *al)
a8c1c704
MC
2468{
2469#ifndef OPENSSL_NO_DH
2470 DH *dh_clnt = NULL;
2471 const BIGNUM *pub_key;
2472 EVP_PKEY *ckey = NULL, *skey = NULL;
f1ec23c0 2473 unsigned char *keybytes = NULL;
a8c1c704
MC
2474
2475 skey = s->s3->peer_tmp;
f1ec23c0
MC
2476 if (skey == NULL)
2477 goto err;
2478
0a699a07 2479 ckey = ssl_generate_pkey(skey);
b599ce3b
MC
2480 if (ckey == NULL)
2481 goto err;
2482
a8c1c704
MC
2483 dh_clnt = EVP_PKEY_get0_DH(ckey);
2484
0f1e51ea 2485 if (dh_clnt == NULL || ssl_derive(s, ckey, skey, 0) == 0)
f1ec23c0 2486 goto err;
a8c1c704
MC
2487
2488 /* send off the data */
2489 DH_get0_key(dh_clnt, &pub_key, NULL);
b2b3024e 2490 if (!WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(pub_key), &keybytes))
f1ec23c0
MC
2491 goto err;
2492
2493 BN_bn2bin(pub_key, keybytes);
a8c1c704
MC
2494 EVP_PKEY_free(ckey);
2495
2496 return 1;
f1ec23c0
MC
2497 err:
2498 EVP_PKEY_free(ckey);
2499#endif
05ec6a25 2500 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
a8c1c704
MC
2501 *al = SSL_AD_INTERNAL_ERROR;
2502 return 0;
a8c1c704
MC
2503}
2504
f1ec23c0 2505static int tls_construct_cke_ecdhe(SSL *s, WPACKET *pkt, int *al)
67ad5aab
MC
2506{
2507#ifndef OPENSSL_NO_EC
2508 unsigned char *encodedPoint = NULL;
348240c6 2509 size_t encoded_pt_len = 0;
67ad5aab 2510 EVP_PKEY *ckey = NULL, *skey = NULL;
f1ec23c0 2511 int ret = 0;
67ad5aab
MC
2512
2513 skey = s->s3->peer_tmp;
ec24630a 2514 if (skey == NULL) {
05ec6a25 2515 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
67ad5aab
MC
2516 return 0;
2517 }
2518
0a699a07 2519 ckey = ssl_generate_pkey(skey);
b599ce3b
MC
2520 if (ckey == NULL) {
2521 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_MALLOC_FAILURE);
2522 goto err;
2523 }
67ad5aab 2524
0f1e51ea 2525 if (ssl_derive(s, ckey, skey, 0) == 0) {
05ec6a25 2526 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EVP_LIB);
67ad5aab
MC
2527 goto err;
2528 }
2529
2530 /* Generate encoding of client key */
ec24630a 2531 encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(ckey, &encodedPoint);
67ad5aab
MC
2532
2533 if (encoded_pt_len == 0) {
05ec6a25 2534 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EC_LIB);
67ad5aab
MC
2535 goto err;
2536 }
2537
b2b3024e 2538 if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) {
f1ec23c0
MC
2539 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2540 goto err;
2541 }
67ad5aab 2542
f1ec23c0 2543 ret = 1;
67ad5aab 2544 err:
f1ec23c0 2545 OPENSSL_free(encodedPoint);
67ad5aab 2546 EVP_PKEY_free(ckey);
f1ec23c0 2547 return ret;
67ad5aab 2548#else
05ec6a25 2549 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
67ad5aab
MC
2550 *al = SSL_AD_INTERNAL_ERROR;
2551 return 0;
2552#endif
2553}
2554
f1ec23c0 2555static int tls_construct_cke_gost(SSL *s, WPACKET *pkt, int *al)
e00e0b3d
MC
2556{
2557#ifndef OPENSSL_NO_GOST
2558 /* GOST key exchange message creation */
2559 EVP_PKEY_CTX *pkey_ctx = NULL;
2560 X509 *peer_cert;
2561 size_t msglen;
2562 unsigned int md_len;
2563 unsigned char shared_ukm[32], tmp[256];
2564 EVP_MD_CTX *ukm_hash = NULL;
2565 int dgst_nid = NID_id_GostR3411_94;
2566 unsigned char *pms = NULL;
2567 size_t pmslen = 0;
2568
2569 if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
2570 dgst_nid = NID_id_GostR3411_2012_256;
2571
2572 /*
2573 * Get server sertificate PKEY and create ctx from it
2574 */
2575 peer_cert = s->session->peer;
2576 if (!peer_cert) {
2577 *al = SSL_AD_HANDSHAKE_FAILURE;
05ec6a25 2578 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST,
e00e0b3d
MC
2579 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2580 return 0;
2581 }
2582
2583 pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
2584 if (pkey_ctx == NULL) {
2585 *al = SSL_AD_INTERNAL_ERROR;
05ec6a25 2586 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);
e00e0b3d
MC
2587 return 0;
2588 }
2589 /*
2590 * If we have send a certificate, and certificate key
2591 * parameters match those of server certificate, use
2592 * certificate key for key exchange
2593 */
2594
2595 /* Otherwise, generate ephemeral key pair */
2596 pmslen = 32;
2597 pms = OPENSSL_malloc(pmslen);
2598 if (pms == NULL) {
2599 *al = SSL_AD_INTERNAL_ERROR;
05ec6a25 2600 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);
2f3930bc 2601 goto err;
e00e0b3d
MC
2602 }
2603
2604 if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
348240c6
MC
2605 /* Generate session key
2606 * TODO(size_t): Convert this function
2607 */
2608 || RAND_bytes(pms, (int)pmslen) <= 0) {
e00e0b3d 2609 *al = SSL_AD_INTERNAL_ERROR;
05ec6a25 2610 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
e00e0b3d
MC
2611 goto err;
2612 };
e00e0b3d
MC
2613 /*
2614 * Compute shared IV and store it in algorithm-specific context
2615 * data
2616 */
2617 ukm_hash = EVP_MD_CTX_new();
2618 if (ukm_hash == NULL
a230b26e
EK
2619 || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
2620 || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2621 SSL3_RANDOM_SIZE) <= 0
2622 || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2623 SSL3_RANDOM_SIZE) <= 0
2624 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
e00e0b3d 2625 *al = SSL_AD_INTERNAL_ERROR;
05ec6a25 2626 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
e00e0b3d
MC
2627 goto err;
2628 }
2629 EVP_MD_CTX_free(ukm_hash);
2630 ukm_hash = NULL;
2631 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
2632 EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {
2633 *al = SSL_AD_INTERNAL_ERROR;
05ec6a25 2634 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);
e00e0b3d
MC
2635 goto err;
2636 }
2637 /* Make GOST keytransport blob message */
2638 /*
2639 * Encapsulate it into sequence
2640 */
e00e0b3d
MC
2641 msglen = 255;
2642 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
2643 *al = SSL_AD_INTERNAL_ERROR;
05ec6a25 2644 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);
e00e0b3d
MC
2645 goto err;
2646 }
f1ec23c0 2647
08029dfa
MC
2648 if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
2649 || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))
b2b3024e 2650 || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {
f1ec23c0
MC
2651 *al = SSL_AD_INTERNAL_ERROR;
2652 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2653 goto err;
e00e0b3d 2654 }
f1ec23c0 2655
e00e0b3d
MC
2656 EVP_PKEY_CTX_free(pkey_ctx);
2657 s->s3->tmp.pms = pms;
2658 s->s3->tmp.pmslen = pmslen;
2659
2660 return 1;
2661 err:
2662 EVP_PKEY_CTX_free(pkey_ctx);
2663 OPENSSL_clear_free(pms, pmslen);
2664 EVP_MD_CTX_free(ukm_hash);
2665 return 0;
2666#else
05ec6a25 2667 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
e00e0b3d
MC
2668 *al = SSL_AD_INTERNAL_ERROR;
2669 return 0;
2670#endif
2671}
2672
f1ec23c0 2673static int tls_construct_cke_srp(SSL *s, WPACKET *pkt, int *al)
840a2bf8 2674{
8b9546c7 2675#ifndef OPENSSL_NO_SRP
f1ec23c0
MC
2676 unsigned char *abytes = NULL;
2677
2678 if (s->srp_ctx.A == NULL
b2b3024e
MC
2679 || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A),
2680 &abytes)) {
05ec6a25 2681 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR);
840a2bf8
MC
2682 return 0;
2683 }
f1ec23c0
MC
2684 BN_bn2bin(s->srp_ctx.A, abytes);
2685
840a2bf8
MC
2686 OPENSSL_free(s->session->srp_username);
2687 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2688 if (s->session->srp_username == NULL) {
05ec6a25 2689 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_MALLOC_FAILURE);
840a2bf8
MC
2690 return 0;
2691 }
2692
2693 return 1;
2694#else
05ec6a25 2695 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR);
840a2bf8
MC
2696 *al = SSL_AD_INTERNAL_ERROR;
2697 return 0;
2698#endif
2699}
2700
7cea05dc 2701int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt)
13c0ec4a 2702{
13c0ec4a
MC
2703 unsigned long alg_k;
2704 int al = -1;
2705
f1ec23c0 2706 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
13c0ec4a 2707
13c0ec4a 2708 if ((alg_k & SSL_PSK)
7cea05dc 2709 && !tls_construct_cke_psk_preamble(s, pkt, &al))
13c0ec4a
MC
2710 goto err;
2711
f1ec23c0 2712 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
7cea05dc 2713 if (!tls_construct_cke_rsa(s, pkt, &al))
13c0ec4a 2714 goto err;
a8c1c704 2715 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
7cea05dc 2716 if (!tls_construct_cke_dhe(s, pkt, &al))
b9908bf9 2717 goto err;
67ad5aab 2718 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
7cea05dc 2719 if (!tls_construct_cke_ecdhe(s, pkt, &al))
ce0c1f2b 2720 goto err;
e00e0b3d 2721 } else if (alg_k & SSL_kGOST) {
7cea05dc 2722 if (!tls_construct_cke_gost(s, pkt, &al))
a71edf3b 2723 goto err;
840a2bf8 2724 } else if (alg_k & SSL_kSRP) {
7cea05dc 2725 if (!tls_construct_cke_srp(s, pkt, &al))
69f68237 2726 goto err;
4a424545 2727 } else if (!(alg_k & SSL_kPSK)) {
b9908bf9
MC
2728 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2729 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2730 goto err;
2731 }
2732
b9908bf9 2733 return 1;
0f113f3e 2734 err:
13c0ec4a
MC
2735 if (al != -1)
2736 ssl3_send_alert(s, SSL3_AL_FATAL, al);
0bce0b02 2737 OPENSSL_clear_free(s->s3->tmp.pms, s->s3->tmp.pmslen);
76106e60 2738 s->s3->tmp.pms = NULL;
7689082b
DSH
2739#ifndef OPENSSL_NO_PSK
2740 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2741 s->s3->tmp.psk = NULL;
0f113f3e 2742#endif
b9908bf9
MC
2743 return 0;
2744}
2745
2746int tls_client_key_exchange_post_work(SSL *s)
2747{
2748 unsigned char *pms = NULL;
2749 size_t pmslen = 0;
2750
6f137370
MC
2751 pms = s->s3->tmp.pms;
2752 pmslen = s->s3->tmp.pmslen;
2753
b9908bf9
MC
2754#ifndef OPENSSL_NO_SRP
2755 /* Check for SRP */
2756 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2757 if (!srp_generate_client_master_secret(s)) {
2758 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2759 ERR_R_INTERNAL_ERROR);
2760 goto err;
2761 }
2762 return 1;
2763 }
2764#endif
b9908bf9
MC
2765
2766 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2767 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2768 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2769 goto err;
2770 }
2771 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2772 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2773 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
6f137370
MC
2774 /* ssl_generate_master_secret frees the pms even on error */
2775 pms = NULL;
2776 pmslen = 0;
b9908bf9
MC
2777 goto err;
2778 }
6f137370
MC
2779 pms = NULL;
2780 pmslen = 0;
473483d4
MC
2781
2782#ifndef OPENSSL_NO_SCTP
2783 if (SSL_IS_DTLS(s)) {
2784 unsigned char sctpauthkey[64];
2785 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2786
2787 /*
2788 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2789 * used.
2790 */
141eb8c6
MC
2791 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2792 sizeof(DTLS1_SCTP_AUTH_LABEL));
473483d4
MC
2793
2794 if (SSL_export_keying_material(s, sctpauthkey,
a230b26e
EK
2795 sizeof(sctpauthkey), labelbuffer,
2796 sizeof(labelbuffer), NULL, 0, 0) <= 0)
473483d4
MC
2797 goto err;
2798
2799 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2800 sizeof(sctpauthkey), sctpauthkey);
2801 }
2802#endif
2803
b9908bf9
MC
2804 return 1;
2805 err:
2806 OPENSSL_clear_free(pms, pmslen);
2807 s->s3->tmp.pms = NULL;
2808 return 0;
0f113f3e 2809}
d02b48c6 2810
7cea05dc 2811int tls_construct_client_verify(SSL *s, WPACKET *pkt)
0f113f3e 2812{
0f113f3e 2813 EVP_PKEY *pkey;
a0f63828 2814 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
5a008ff6 2815 EVP_MD_CTX *mctx = NULL;
0f113f3e 2816 unsigned u = 0;
a0f63828
DSH
2817 long hdatalen = 0;
2818 void *hdata;
6400f338 2819 unsigned char *sig = NULL;
6400f338 2820
bfb0641f 2821 mctx = EVP_MD_CTX_new();
6e59a892
RL
2822 if (mctx == NULL) {
2823 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2824 goto err;
2825 }
b9908bf9 2826 pkey = s->cert->key->privatekey;
a0f63828
DSH
2827
2828 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2829 if (hdatalen <= 0) {
5f3d93e4
MC
2830 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2831 goto err;
2832 }
0f1e51ea 2833
7cea05dc 2834 if (SSL_USE_SIGALGS(s)&& !tls12_get_sigandhash(pkt, pkey, md)) {
6400f338
MC
2835 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2836 goto err;
a0f63828 2837 }
855a54a9 2838#ifdef SSL_DEBUG
a0f63828 2839 fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md));
b9908bf9 2840#endif
6400f338
MC
2841 sig = OPENSSL_malloc(EVP_PKEY_size(pkey));
2842 if (sig == NULL) {
2843 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2844 goto err;
2845 }
6e59a892
RL
2846 if (!EVP_SignInit_ex(mctx, md, NULL)
2847 || !EVP_SignUpdate(mctx, hdata, hdatalen)
a0f63828 2848 || (s->version == SSL3_VERSION
6e59a892 2849 && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
348240c6 2850 (int)s->session->master_key_length,
a0f63828 2851 s->session->master_key))
6400f338 2852 || !EVP_SignFinal(mctx, sig, &u, pkey)) {
a0f63828
DSH
2853 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
2854 goto err;
2855 }
2a9b9654 2856#ifndef OPENSSL_NO_GOST
3aeb9348
DSH
2857 {
2858 int pktype = EVP_PKEY_id(pkey);
2859 if (pktype == NID_id_GostR3410_2001
2860 || pktype == NID_id_GostR3410_2012_256
2861 || pktype == NID_id_GostR3410_2012_512)
6400f338 2862 BUF_reverse(sig, NULL, u);
b9908bf9 2863 }
2a9b9654 2864#endif
a0f63828 2865
7cea05dc 2866 if (!WPACKET_sub_memcpy_u16(pkt, sig, u)) {
6400f338
MC
2867 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2868 goto err;
2869 }
2870
a0f63828
DSH
2871 /* Digest cached records and discard handshake buffer */
2872 if (!ssl3_digest_cached_records(s, 0))
2873 goto err;
6400f338 2874
6400f338 2875 OPENSSL_free(sig);
bfb0641f 2876 EVP_MD_CTX_free(mctx);
b9908bf9 2877 return 1;
0f113f3e 2878 err:
6400f338 2879 OPENSSL_free(sig);
bfb0641f 2880 EVP_MD_CTX_free(mctx);
6400f338 2881 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
b9908bf9 2882 return 0;
0f113f3e
MC
2883}
2884
2885/*
2886 * Check a certificate can be used for client authentication. Currently check
2887 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
2888 * certificates can be used and optionally checks suitability for Suite B.
0d609395
DSH
2889 */
2890static int ssl3_check_client_certificate(SSL *s)
0f113f3e 2891{
0f113f3e
MC
2892 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2893 return 0;
2894 /* If no suitable signature algorithm can't use certificate */
d376e57d 2895 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
0f113f3e
MC
2896 return 0;
2897 /*
2898 * If strict mode check suitability of chain before using it. This also
2899 * adjusts suite B digest if necessary.
2900 */
2901 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2902 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2903 return 0;
0f113f3e
MC
2904 return 1;
2905}
0d609395 2906
be3583fa 2907WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
0f113f3e
MC
2908{
2909 X509 *x509 = NULL;
2910 EVP_PKEY *pkey = NULL;
2911 int i;
2912
b9908bf9 2913 if (wst == WORK_MORE_A) {
0f113f3e
MC
2914 /* Let cert callback update client certificates if required */
2915 if (s->cert->cert_cb) {
2916 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2917 if (i < 0) {
2918 s->rwstate = SSL_X509_LOOKUP;
b9908bf9 2919 return WORK_MORE_A;
0f113f3e
MC
2920 }
2921 if (i == 0) {
2922 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
fe3a3291 2923 ossl_statem_set_error(s);
0f113f3e
MC
2924 return 0;
2925 }
2926 s->rwstate = SSL_NOTHING;
2927 }
2928 if (ssl3_check_client_certificate(s))
b9908bf9
MC
2929 return WORK_FINISHED_CONTINUE;
2930
2931 /* Fall through to WORK_MORE_B */
2932 wst = WORK_MORE_B;
0f113f3e
MC
2933 }
2934
2935 /* We need to get a client cert */
b9908bf9 2936 if (wst == WORK_MORE_B) {
0f113f3e
MC
2937 /*
2938 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2939 * return(-1); We then get retied later
2940 */
0f113f3e
MC
2941 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2942 if (i < 0) {
2943 s->rwstate = SSL_X509_LOOKUP;
b9908bf9 2944 return WORK_MORE_B;
0f113f3e
MC
2945 }
2946 s->rwstate = SSL_NOTHING;
2947 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
0f113f3e
MC
2948 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
2949 i = 0;
2950 } else if (i == 1) {
2951 i = 0;
b9908bf9 2952 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
0f113f3e
MC
2953 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2954 }
2955
222561fe 2956 X509_free(x509);
25aaa98a 2957 EVP_PKEY_free(pkey);
0f113f3e
MC
2958 if (i && !ssl3_check_client_certificate(s))
2959 i = 0;
2960 if (i == 0) {
2961 if (s->version == SSL3_VERSION) {
2962 s->s3->tmp.cert_req = 0;
2963 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
b9908bf9 2964 return WORK_FINISHED_CONTINUE;
0f113f3e
MC
2965 } else {
2966 s->s3->tmp.cert_req = 2;
124037fd 2967 if (!ssl3_digest_cached_records(s, 0)) {
dab18ab5 2968 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
fe3a3291 2969 ossl_statem_set_error(s);
dab18ab5
DSH
2970 return 0;
2971 }
0f113f3e
MC
2972 }
2973 }
2974
b9908bf9 2975 return WORK_FINISHED_CONTINUE;
0f113f3e
MC
2976 }
2977
b9908bf9
MC
2978 /* Shouldn't ever get here */
2979 return WORK_ERROR;
2980}
2981
7cea05dc 2982int tls_construct_client_certificate(SSL *s, WPACKET *pkt)
b9908bf9 2983{
7cea05dc 2984 if (!ssl3_output_cert_chain(s, pkt,
b90506e9
MC
2985 (s->s3->tmp.cert_req == 2) ? NULL
2986 : s->cert->key)) {
b9908bf9
MC
2987 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2988 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
b9908bf9 2989 return 0;
0f113f3e 2990 }
b9908bf9
MC
2991
2992 return 1;
0f113f3e
MC
2993}
2994
2995#define has_bits(i,m) (((i)&(m)) == (m))
d02b48c6 2996
36d16f8e 2997int ssl3_check_cert_and_algorithm(SSL *s)
0f113f3e 2998{
60f43e9e
RL
2999 int i;
3000#ifndef OPENSSL_NO_EC
3001 int idx;
3002#endif
0f113f3e
MC
3003 long alg_k, alg_a;
3004 EVP_PKEY *pkey = NULL;
26c79d56 3005 int al = SSL_AD_HANDSHAKE_FAILURE;
d02b48c6 3006
0f113f3e
MC
3007 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3008 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
d02b48c6 3009
0f113f3e 3010 /* we don't have a certificate */
55a9a16f 3011 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
0f113f3e 3012 return (1);
d02b48c6 3013
0f113f3e 3014 /* This is the passed certificate */
d02b48c6 3015
10bf4fc2 3016#ifndef OPENSSL_NO_EC
60f43e9e 3017 idx = s->session->peer_type;
0f113f3e 3018 if (idx == SSL_PKEY_ECC) {
a273c6ee 3019 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
0f113f3e
MC
3020 /* check failed */
3021 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3022 goto f_err;
3023 } else {
3024 return 1;
3025 }
3026 } else if (alg_a & SSL_aECDSA) {
3027 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3028 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3029 goto f_err;
0f113f3e
MC
3030 }
3031#endif
8382fd3a 3032 pkey = X509_get0_pubkey(s->session->peer);
a273c6ee 3033 i = X509_certificate_type(s->session->peer, pkey);
0f113f3e
MC
3034
3035 /* Check that we have a certificate if we require one */
3036 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3037 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3038 SSL_R_MISSING_RSA_SIGNING_CERT);
3039 goto f_err;
3040 }
bc36ee62 3041#ifndef OPENSSL_NO_DSA
0f113f3e
MC
3042 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3043 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3044 SSL_R_MISSING_DSA_SIGNING_CERT);
3045 goto f_err;
3046 }
d02b48c6 3047#endif
bc36ee62 3048#ifndef OPENSSL_NO_RSA
361a1191
KR
3049 if (alg_k & (SSL_kRSA | SSL_kRSAPSK) &&
3050 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3051 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3052 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3053 goto f_err;
0f113f3e 3054 }
79df9d62 3055#endif
bc36ee62 3056#ifndef OPENSSL_NO_DH
fb79abe3 3057 if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {
26c79d56
KR
3058 al = SSL_AD_INTERNAL_ERROR;
3059 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
0f113f3e 3060 goto f_err;
0f113f3e 3061 }
d02b48c6
RE
3062#endif
3063
0f113f3e
MC
3064 return (1);
3065 f_err:
26c79d56 3066 ssl3_send_alert(s, SSL3_AL_FATAL, al);
0f113f3e
MC
3067 return (0);
3068}
3069
e481f9b9 3070#ifndef OPENSSL_NO_NEXTPROTONEG
7cea05dc 3071int tls_construct_next_proto(SSL *s, WPACKET *pkt)
b9908bf9 3072{
15e6be6c
MC
3073 size_t len, padding_len;
3074 unsigned char *padding = NULL;
15e6be6c 3075
b9908bf9
MC
3076 len = s->next_proto_negotiated_len;
3077 padding_len = 32 - ((len + 2) % 32);
15e6be6c 3078
7cea05dc
MC
3079 if (!WPACKET_sub_memcpy_u8(pkt, s->next_proto_negotiated, len)
3080 || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {
15e6be6c
MC
3081 SSLerr(SSL_F_TLS_CONSTRUCT_NEXT_PROTO, ERR_R_INTERNAL_ERROR);
3082 goto err;
3083 }
3084
3085 memset(padding, 0, padding_len);
3086
b9908bf9 3087 return 1;
15e6be6c 3088 err:
15e6be6c
MC
3089 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3090 return 0;
b9908bf9 3091}
6434abbf 3092#endif
368888bc 3093
e46f2334
MC
3094static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt)
3095{
3096 int al = SSL_AD_INTERNAL_ERROR;
3097 PACKET extensions;
3434f40b 3098 RAW_EXTENSION *rawexts = NULL;
e46f2334 3099
e46f2334
MC
3100 if (!PACKET_as_length_prefixed_2(pkt, &extensions)) {
3101 al = SSL_AD_DECODE_ERROR;
3102 SSLerr(SSL_F_TLS_PROCESS_ENCRYPTED_EXTENSIONS, SSL_R_LENGTH_MISMATCH);
3103 goto err;
3104 }
3105
3434f40b
MC
3106 /*
3107 * TODO(TLS1.3): For now we are processing Encrypted Extensions and
3108 * Certificate extensions as part of this one message. Later we need to
3109 * split out the Certificate extensions into the Certificate message
3110 */
3111 if (!tls_collect_extensions(s, &extensions,
3112 EXT_TLS1_3_ENCRYPTED_EXTENSIONS
3113 | EXT_TLS1_3_CERTIFICATE, &rawexts, &al)
3114 || !tls_parse_all_extensions(s,
3115 EXT_TLS1_3_ENCRYPTED_EXTENSIONS
3116 | EXT_TLS1_3_CERTIFICATE,
3117 rawexts, &al))
3118 goto err;
3119
e46f2334
MC
3120 return MSG_PROCESS_CONTINUE_READING;
3121
3122 err:
3123 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3124 ossl_statem_set_error(s);
3125 return MSG_PROCESS_ERROR;
3126}
3127
368888bc 3128int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
0f113f3e
MC
3129{
3130 int i = 0;
368888bc 3131#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
3132 if (s->ctx->client_cert_engine) {
3133 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3134 SSL_get_client_CA_list(s),
3135 px509, ppkey, NULL, NULL, NULL);
3136 if (i != 0)
3137 return i;
3138 }
3139#endif
3140 if (s->ctx->client_cert_cb)
3141 i = s->ctx->client_cert_cb(s, px509, ppkey);
3142 return i;
3143}
d45ba43d 3144
ae2f7b37 3145int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, WPACKET *pkt)
d45ba43d 3146{
2c7b4dbc
MC
3147 int i;
3148 size_t totlen = 0, len, maxlen;
d45ba43d
MC
3149 int empty_reneg_info_scsv = !s->renegotiate;
3150 /* Set disabled masks for this session */
3151 ssl_set_client_disabled(s);
3152
3153 if (sk == NULL)
3154 return (0);
d45ba43d 3155
2c7b4dbc
MC
3156#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
3157# if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6
3158# error Max cipher length too short
3159# endif
3160 /*
3161 * Some servers hang if client hello > 256 bytes as hack workaround
3162 * chop number of supported ciphers to keep it well below this if we
3163 * use TLS v1.2
3164 */
3165 if (TLS1_get_version(s) >= TLS1_2_VERSION)
3166 maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
3167 else
3168#endif
3169 /* Maximum length that can be stored in 2 bytes. Length must be even */
3170 maxlen = 0xfffe;
3171
3172 if (empty_reneg_info_scsv)
3173 maxlen -= 2;
3174 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
3175 maxlen -= 2;
3176
3177 for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) {
3178 const SSL_CIPHER *c;
3179
d45ba43d
MC
3180 c = sk_SSL_CIPHER_value(sk, i);
3181 /* Skip disabled ciphers */
3182 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3183 continue;
2c7b4dbc
MC
3184
3185 if (!s->method->put_cipher_by_char(c, pkt, &len)) {
3186 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
3187 return 0;
3188 }
3189
3190 totlen += len;
d45ba43d 3191 }
2c7b4dbc
MC
3192
3193 if (totlen == 0) {
3194 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, SSL_R_NO_CIPHERS_AVAILABLE);
3195 return 0;
3196 }
3197
3198 if (totlen != 0) {
d45ba43d
MC
3199 if (empty_reneg_info_scsv) {
3200 static SSL_CIPHER scsv = {
3201 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3202 };
2c7b4dbc
MC
3203 if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
3204 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
3205 return 0;
3206 }
d45ba43d
MC
3207 }
3208 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3209 static SSL_CIPHER scsv = {
3210 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3211 };
2c7b4dbc
MC
3212 if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
3213 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
3214 return 0;
3215 }
d45ba43d
MC
3216 }
3217 }
3218
2c7b4dbc 3219 return 1;
d45ba43d 3220}