]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/statem/statem_srvr.c
Fix some alert codes
[thirdparty/openssl.git] / ssl / statem / statem_srvr.c
CommitLineData
846e33c7
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
8e2f6b79 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
8e2f6b79 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 49
d02b48c6 50#include <stdio.h>
8ba708e5 51#include "../ssl_locl.h"
61ae935a 52#include "statem_locl.h"
68570797 53#include "internal/constant_time_locl.h"
ec577822
BM
54#include <openssl/buffer.h>
55#include <openssl/rand.h>
56#include <openssl/objects.h>
57#include <openssl/evp.h>
6434abbf 58#include <openssl/hmac.h>
ec577822 59#include <openssl/x509.h>
3c27208f 60#include <openssl/dh.h>
d095b68d 61#include <openssl/bn.h>
dbad1690 62#include <openssl/md5.h>
f9b3bff6 63
e46f2334 64static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt);
7d061fce 65static int tls_construct_hello_retry_request(SSL *s, WPACKET *pkt);
d45ba43d 66
61ae935a 67/*
0f1e51ea
MC
68 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
69 * handshake state transitions when a TLSv1.3 server is reading messages from
70 * the client. The message type that the client has sent is provided in |mt|.
71 * The current state is in |s->statem.hand_state|.
72 *
94ed2c67
MC
73 * Return values are 1 for success (transition allowed) and 0 on error
74 * (transition not allowed)
0f1e51ea
MC
75 */
76static int ossl_statem_server13_read_transition(SSL *s, int mt)
77{
78 OSSL_STATEM *st = &s->statem;
79
80 /*
81 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
82 * not negotiated TLSv1.3 yet, so that case is handled by
83 * ossl_statem_server_read_transition()
84 */
85 switch (st->hand_state) {
86 default:
87 break;
88
7d061fce
MC
89 case TLS_ST_SW_HELLO_RETRY_REQUEST:
90 if (mt == SSL3_MT_CLIENT_HELLO) {
91 st->hand_state = TLS_ST_SR_CLNT_HELLO;
92 return 1;
93 }
94 break;
95
d7f8783f 96 case TLS_ST_EARLY_DATA:
ef6c191b
MC
97 if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
98 if (mt == SSL3_MT_END_OF_EARLY_DATA) {
99 st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;
100 return 1;
101 }
102 break;
103 }
104 /* Fall through */
105
106 case TLS_ST_SR_END_OF_EARLY_DATA:
92760c21 107 case TLS_ST_SW_FINISHED:
0f1e51ea
MC
108 if (s->s3->tmp.cert_request) {
109 if (mt == SSL3_MT_CERTIFICATE) {
110 st->hand_state = TLS_ST_SR_CERT;
111 return 1;
112 }
113 } else {
92760c21
MC
114 if (mt == SSL3_MT_FINISHED) {
115 st->hand_state = TLS_ST_SR_FINISHED;
0f1e51ea
MC
116 return 1;
117 }
118 }
119 break;
120
121 case TLS_ST_SR_CERT:
122 if (s->session->peer == NULL) {
92760c21
MC
123 if (mt == SSL3_MT_FINISHED) {
124 st->hand_state = TLS_ST_SR_FINISHED;
0f1e51ea
MC
125 return 1;
126 }
127 } else {
128 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
129 st->hand_state = TLS_ST_SR_CERT_VRFY;
130 return 1;
131 }
132 }
133 break;
134
135 case TLS_ST_SR_CERT_VRFY:
0f1e51ea
MC
136 if (mt == SSL3_MT_FINISHED) {
137 st->hand_state = TLS_ST_SR_FINISHED;
138 return 1;
139 }
140 break;
8cdc8c51
MC
141
142 case TLS_ST_OK:
10109364
MC
143 /*
144 * Its never ok to start processing handshake messages in the middle of
145 * early data (i.e. before we've received the end of early data alert)
146 */
147 if (s->early_data_state == SSL_EARLY_DATA_READING)
148 break;
8cdc8c51
MC
149 if (mt == SSL3_MT_KEY_UPDATE) {
150 st->hand_state = TLS_ST_SR_KEY_UPDATE;
151 return 1;
152 }
153 break;
0f1e51ea
MC
154 }
155
156 /* No valid transition found */
0f1e51ea
MC
157 return 0;
158}
159
160/*
161 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
162 * handshake state transitions when the server is reading messages from the
163 * client. The message type that the client has sent is provided in |mt|. The
164 * current state is in |s->statem.hand_state|.
61ae935a 165 *
94ed2c67
MC
166 * Return values are 1 for success (transition allowed) and 0 on error
167 * (transition not allowed)
61ae935a 168 */
8481f583 169int ossl_statem_server_read_transition(SSL *s, int mt)
61ae935a 170{
d6f1a6e9 171 OSSL_STATEM *st = &s->statem;
61ae935a 172
f5ca0b04 173 if (SSL_IS_TLS13(s)) {
5abeaf35
MC
174 if (!ossl_statem_server13_read_transition(s, mt))
175 goto err;
176 return 1;
177 }
0f1e51ea 178
e8aa8b6c 179 switch (st->hand_state) {
f3b3d7f0
RS
180 default:
181 break;
182
61ae935a 183 case TLS_ST_BEFORE:
0386aad1 184 case TLS_ST_OK:
61ae935a
MC
185 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
186 if (mt == SSL3_MT_CLIENT_HELLO) {
187 st->hand_state = TLS_ST_SR_CLNT_HELLO;
188 return 1;
189 }
190 break;
191
192 case TLS_ST_SW_SRVR_DONE:
193 /*
194 * If we get a CKE message after a ServerDone then either
195 * 1) We didn't request a Certificate
196 * OR
197 * 2) If we did request one then
198 * a) We allow no Certificate to be returned
199 * AND
200 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
201 * list if we requested a certificate)
202 */
0f512756
MC
203 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
204 if (s->s3->tmp.cert_request) {
205 if (s->version == SSL3_VERSION) {
23dd09b5
MC
206 if ((s->verify_mode & SSL_VERIFY_PEER)
207 && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
0f512756
MC
208 /*
209 * This isn't an unexpected message as such - we're just
23dd09b5
MC
210 * not going to accept it because we require a client
211 * cert.
0f512756
MC
212 */
213 ssl3_send_alert(s, SSL3_AL_FATAL,
214 SSL3_AD_HANDSHAKE_FAILURE);
340a2828 215 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
0f512756
MC
216 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
217 return 0;
218 }
219 st->hand_state = TLS_ST_SR_KEY_EXCH;
220 return 1;
221 }
222 } else {
223 st->hand_state = TLS_ST_SR_KEY_EXCH;
224 return 1;
225 }
61ae935a
MC
226 } else if (s->s3->tmp.cert_request) {
227 if (mt == SSL3_MT_CERTIFICATE) {
228 st->hand_state = TLS_ST_SR_CERT;
229 return 1;
f100b031 230 }
61ae935a
MC
231 }
232 break;
233
234 case TLS_ST_SR_CERT:
235 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
236 st->hand_state = TLS_ST_SR_KEY_EXCH;
237 return 1;
238 }
239 break;
240
241 case TLS_ST_SR_KEY_EXCH:
242 /*
243 * We should only process a CertificateVerify message if we have
244 * received a Certificate from the client. If so then |s->session->peer|
245 * will be non NULL. In some instances a CertificateVerify message is
246 * not required even if the peer has sent a Certificate (e.g. such as in
a71a4966 247 * the case of static DH). In that case |st->no_cert_verify| should be
61ae935a
MC
248 * set.
249 */
a71a4966 250 if (s->session->peer == NULL || st->no_cert_verify) {
61ae935a
MC
251 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
252 /*
253 * For the ECDH ciphersuites when the client sends its ECDH
254 * pub key in a certificate, the CertificateVerify message is
255 * not sent. Also for GOST ciphersuites when the client uses
256 * its key from the certificate for key exchange.
257 */
258 st->hand_state = TLS_ST_SR_CHANGE;
259 return 1;
260 }
261 } else {
262 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
263 st->hand_state = TLS_ST_SR_CERT_VRFY;
264 return 1;
265 }
266 }
267 break;
268
269 case TLS_ST_SR_CERT_VRFY:
270 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
271 st->hand_state = TLS_ST_SR_CHANGE;
272 return 1;
273 }
274 break;
275
276 case TLS_ST_SR_CHANGE:
277#ifndef OPENSSL_NO_NEXTPROTONEG
aff8c126 278 if (s->s3->npn_seen) {
61ae935a
MC
279 if (mt == SSL3_MT_NEXT_PROTO) {
280 st->hand_state = TLS_ST_SR_NEXT_PROTO;
281 return 1;
282 }
283 } else {
284#endif
285 if (mt == SSL3_MT_FINISHED) {
286 st->hand_state = TLS_ST_SR_FINISHED;
287 return 1;
288 }
289#ifndef OPENSSL_NO_NEXTPROTONEG
290 }
291#endif
292 break;
293
294#ifndef OPENSSL_NO_NEXTPROTONEG
295 case TLS_ST_SR_NEXT_PROTO:
296 if (mt == SSL3_MT_FINISHED) {
297 st->hand_state = TLS_ST_SR_FINISHED;
298 return 1;
299 }
300 break;
301#endif
302
303 case TLS_ST_SW_FINISHED:
304 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
305 st->hand_state = TLS_ST_SR_CHANGE;
306 return 1;
307 }
308 break;
61ae935a
MC
309 }
310
5abeaf35 311 err:
61ae935a 312 /* No valid transition found */
672f3337 313 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
340a2828 314 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE);
61ae935a
MC
315 return 0;
316}
317
318/*
319 * Should we send a ServerKeyExchange message?
320 *
321 * Valid return values are:
322 * 1: Yes
323 * 0: No
324 */
bb3e20cf 325static int send_server_key_exchange(SSL *s)
61ae935a
MC
326{
327 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
328
329 /*
361a1191 330 * only send a ServerKeyExchange if DH or fortezza but we have a
61ae935a
MC
331 * sign only certificate PSK: may send PSK identity hints For
332 * ECC ciphersuites, we send a serverKeyExchange message only if
333 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
334 * the server certificate contains the server's public key for
335 * key exchange.
336 */
a230b26e 337 if (alg_k & (SSL_kDHE | SSL_kECDHE)
61ae935a
MC
338 /*
339 * PSK: send ServerKeyExchange if PSK identity hint if
340 * provided
341 */
342#ifndef OPENSSL_NO_PSK
343 /* Only send SKE if we have identity hint for plain PSK */
344 || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
345 && s->cert->psk_identity_hint)
346 /* For other PSK always send SKE */
347 || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
348#endif
349#ifndef OPENSSL_NO_SRP
350 /* SRP: send ServerKeyExchange */
351 || (alg_k & SSL_kSRP)
352#endif
a230b26e 353 ) {
61ae935a
MC
354 return 1;
355 }
356
357 return 0;
358}
359
360/*
361 * Should we send a CertificateRequest message?
362 *
363 * Valid return values are:
364 * 1: Yes
365 * 0: No
366 */
bb3e20cf 367static int send_certificate_request(SSL *s)
61ae935a
MC
368{
369 if (
370 /* don't request cert unless asked for it: */
371 s->verify_mode & SSL_VERIFY_PEER
372 /*
373 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
374 * during re-negotiation:
375 */
a03a9dbe 376 && (s->s3->tmp.finish_md_len == 0 ||
61ae935a
MC
377 !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
378 /*
379 * never request cert in anonymous ciphersuites (see
380 * section "Certificate request" in SSL 3 drafts and in
381 * RFC 2246):
382 */
383 && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
a230b26e
EK
384 /*
385 * ... except when the application insists on
386 * verification (against the specs, but statem_clnt.c accepts
387 * this for SSL 3)
388 */
61ae935a
MC
389 || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
390 /* don't request certificate for SRP auth */
391 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
392 /*
393 * With normal PSK Certificates and Certificate Requests
394 * are omitted
395 */
b7fa1f98 396 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
61ae935a
MC
397 return 1;
398 }
399
400 return 0;
401}
402
403/*
0f1e51ea
MC
404 * ossl_statem_server13_write_transition() works out what handshake state to
405 * move to next when a TLSv1.3 server is writing messages to be sent to the
406 * client.
0f1e51ea
MC
407 */
408static WRITE_TRAN ossl_statem_server13_write_transition(SSL *s)
409{
410 OSSL_STATEM *st = &s->statem;
411
412 /*
413 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
414 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
415 */
416
417 switch (st->hand_state) {
418 default:
419 /* Shouldn't happen */
420 return WRITE_TRAN_ERROR;
421
44c04a2e
MC
422 case TLS_ST_OK:
423 if (s->key_update != SSL_KEY_UPDATE_NONE) {
424 st->hand_state = TLS_ST_SW_KEY_UPDATE;
425 return WRITE_TRAN_CONTINUE;
426 }
8cdc8c51
MC
427 /* Try to read from the client instead */
428 return WRITE_TRAN_FINISHED;
44c04a2e 429
0f1e51ea 430 case TLS_ST_SR_CLNT_HELLO:
7d061fce
MC
431 if (s->hello_retry_request)
432 st->hand_state = TLS_ST_SW_HELLO_RETRY_REQUEST;
433 else
434 st->hand_state = TLS_ST_SW_SRVR_HELLO;
0f1e51ea
MC
435 return WRITE_TRAN_CONTINUE;
436
7d061fce
MC
437 case TLS_ST_SW_HELLO_RETRY_REQUEST:
438 return WRITE_TRAN_FINISHED;
439
0f1e51ea 440 case TLS_ST_SW_SRVR_HELLO:
e46f2334
MC
441 st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
442 return WRITE_TRAN_CONTINUE;
443
444 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
94ed2c67 445 if (s->hit)
92760c21
MC
446 st->hand_state = TLS_ST_SW_FINISHED;
447 else if (send_certificate_request(s))
448 st->hand_state = TLS_ST_SW_CERT_REQ;
94ed2c67 449 else
0f1e51ea 450 st->hand_state = TLS_ST_SW_CERT;
94ed2c67 451
0f1e51ea
MC
452 return WRITE_TRAN_CONTINUE;
453
0f1e51ea 454 case TLS_ST_SW_CERT_REQ:
92760c21 455 st->hand_state = TLS_ST_SW_CERT;
0f1e51ea
MC
456 return WRITE_TRAN_CONTINUE;
457
92760c21 458 case TLS_ST_SW_CERT:
2c5dfdc3
MC
459 st->hand_state = TLS_ST_SW_CERT_VRFY;
460 return WRITE_TRAN_CONTINUE;
461
462 case TLS_ST_SW_CERT_VRFY:
d805a57b 463 st->hand_state = TLS_ST_SW_FINISHED;
0f1e51ea
MC
464 return WRITE_TRAN_CONTINUE;
465
466 case TLS_ST_SW_FINISHED:
f7e393be
MC
467 st->hand_state = TLS_ST_EARLY_DATA;
468 return WRITE_TRAN_CONTINUE;
94ed2c67 469
d7f8783f
MC
470 case TLS_ST_EARLY_DATA:
471 return WRITE_TRAN_FINISHED;
472
92760c21 473 case TLS_ST_SR_FINISHED:
30f05b19
MC
474 /*
475 * Technically we have finished the handshake at this point, but we're
476 * going to remain "in_init" for now and write out the session ticket
477 * immediately.
478 * TODO(TLS1.3): Perhaps we need to be able to control this behaviour
479 * and give the application the opportunity to delay sending the
480 * session ticket?
481 */
482 st->hand_state = TLS_ST_SW_SESSION_TICKET;
483 return WRITE_TRAN_CONTINUE;
484
8cdc8c51 485 case TLS_ST_SR_KEY_UPDATE:
5bf47933
MC
486 if (s->key_update != SSL_KEY_UPDATE_NONE) {
487 st->hand_state = TLS_ST_SW_KEY_UPDATE;
488 return WRITE_TRAN_CONTINUE;
489 }
490 /* Fall through */
491
44c04a2e 492 case TLS_ST_SW_KEY_UPDATE:
30f05b19 493 case TLS_ST_SW_SESSION_TICKET:
0f1e51ea 494 st->hand_state = TLS_ST_OK;
0f1e51ea
MC
495 return WRITE_TRAN_CONTINUE;
496 }
497}
498
499/*
500 * ossl_statem_server_write_transition() works out what handshake state to move
501 * to next when the server is writing messages to be sent to the client.
61ae935a 502 */
8481f583 503WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
61ae935a 504{
d6f1a6e9 505 OSSL_STATEM *st = &s->statem;
61ae935a 506
0f1e51ea
MC
507 /*
508 * Note that before the ClientHello we don't know what version we are going
509 * to negotiate yet, so we don't take this branch until later
510 */
511
f5ca0b04 512 if (SSL_IS_TLS13(s))
0f1e51ea
MC
513 return ossl_statem_server13_write_transition(s);
514
e8aa8b6c 515 switch (st->hand_state) {
f3b3d7f0
RS
516 default:
517 /* Shouldn't happen */
518 return WRITE_TRAN_ERROR;
519
0386aad1
MC
520 case TLS_ST_OK:
521 if (st->request_state == TLS_ST_SW_HELLO_REQ) {
522 /* We must be trying to renegotiate */
523 st->hand_state = TLS_ST_SW_HELLO_REQ;
524 st->request_state = TLS_ST_BEFORE;
525 return WRITE_TRAN_CONTINUE;
526 }
c7f47786
MC
527 /* Must be an incoming ClientHello */
528 if (!tls_setup_handshake(s)) {
529 ossl_statem_set_error(s);
530 return WRITE_TRAN_ERROR;
531 }
0386aad1
MC
532 /* Fall through */
533
e8aa8b6c 534 case TLS_ST_BEFORE:
a230b26e 535 /* Just go straight to trying to read from the client */
e8aa8b6c 536 return WRITE_TRAN_FINISHED;
61ae935a 537
e8aa8b6c
F
538 case TLS_ST_SW_HELLO_REQ:
539 st->hand_state = TLS_ST_OK;
e8aa8b6c 540 return WRITE_TRAN_CONTINUE;
61ae935a 541
e8aa8b6c
F
542 case TLS_ST_SR_CLNT_HELLO:
543 if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
a230b26e 544 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
e8aa8b6c
F
545 st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
546 else
547 st->hand_state = TLS_ST_SW_SRVR_HELLO;
548 return WRITE_TRAN_CONTINUE;
61ae935a 549
e8aa8b6c
F
550 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
551 return WRITE_TRAN_FINISHED;
61ae935a 552
e8aa8b6c
F
553 case TLS_ST_SW_SRVR_HELLO:
554 if (s->hit) {
aff8c126 555 if (s->ext.ticket_expected)
e8aa8b6c
F
556 st->hand_state = TLS_ST_SW_SESSION_TICKET;
557 else
558 st->hand_state = TLS_ST_SW_CHANGE;
559 } else {
560 /* Check if it is anon DH or anon ECDH, */
561 /* normal PSK or SRP */
562 if (!(s->s3->tmp.new_cipher->algorithm_auth &
a230b26e 563 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
e8aa8b6c
F
564 st->hand_state = TLS_ST_SW_CERT;
565 } else if (send_server_key_exchange(s)) {
61ae935a 566 st->hand_state = TLS_ST_SW_KEY_EXCH;
e8aa8b6c 567 } else if (send_certificate_request(s)) {
61ae935a 568 st->hand_state = TLS_ST_SW_CERT_REQ;
e8aa8b6c
F
569 } else {
570 st->hand_state = TLS_ST_SW_SRVR_DONE;
61ae935a 571 }
e8aa8b6c
F
572 }
573 return WRITE_TRAN_CONTINUE;
61ae935a 574
e8aa8b6c 575 case TLS_ST_SW_CERT:
aff8c126 576 if (s->ext.status_expected) {
e8aa8b6c 577 st->hand_state = TLS_ST_SW_CERT_STATUS;
61ae935a 578 return WRITE_TRAN_CONTINUE;
e8aa8b6c
F
579 }
580 /* Fall through */
61ae935a 581
e8aa8b6c
F
582 case TLS_ST_SW_CERT_STATUS:
583 if (send_server_key_exchange(s)) {
584 st->hand_state = TLS_ST_SW_KEY_EXCH;
61ae935a 585 return WRITE_TRAN_CONTINUE;
e8aa8b6c
F
586 }
587 /* Fall through */
61ae935a 588
e8aa8b6c
F
589 case TLS_ST_SW_KEY_EXCH:
590 if (send_certificate_request(s)) {
591 st->hand_state = TLS_ST_SW_CERT_REQ;
61ae935a 592 return WRITE_TRAN_CONTINUE;
e8aa8b6c
F
593 }
594 /* Fall through */
61ae935a 595
e8aa8b6c
F
596 case TLS_ST_SW_CERT_REQ:
597 st->hand_state = TLS_ST_SW_SRVR_DONE;
598 return WRITE_TRAN_CONTINUE;
61ae935a 599
e8aa8b6c
F
600 case TLS_ST_SW_SRVR_DONE:
601 return WRITE_TRAN_FINISHED;
602
603 case TLS_ST_SR_FINISHED:
604 if (s->hit) {
61ae935a 605 st->hand_state = TLS_ST_OK;
61ae935a 606 return WRITE_TRAN_CONTINUE;
aff8c126 607 } else if (s->ext.ticket_expected) {
e8aa8b6c
F
608 st->hand_state = TLS_ST_SW_SESSION_TICKET;
609 } else {
610 st->hand_state = TLS_ST_SW_CHANGE;
611 }
612 return WRITE_TRAN_CONTINUE;
613
614 case TLS_ST_SW_SESSION_TICKET:
615 st->hand_state = TLS_ST_SW_CHANGE;
616 return WRITE_TRAN_CONTINUE;
61ae935a 617
e8aa8b6c
F
618 case TLS_ST_SW_CHANGE:
619 st->hand_state = TLS_ST_SW_FINISHED;
620 return WRITE_TRAN_CONTINUE;
621
622 case TLS_ST_SW_FINISHED:
623 if (s->hit) {
624 return WRITE_TRAN_FINISHED;
625 }
626 st->hand_state = TLS_ST_OK;
e8aa8b6c 627 return WRITE_TRAN_CONTINUE;
61ae935a
MC
628 }
629}
630
631/*
632 * Perform any pre work that needs to be done prior to sending a message from
633 * the server to the client.
634 */
8481f583 635WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
61ae935a 636{
d6f1a6e9 637 OSSL_STATEM *st = &s->statem;
61ae935a 638
e8aa8b6c 639 switch (st->hand_state) {
f3b3d7f0
RS
640 default:
641 /* No pre work to be done */
642 break;
643
61ae935a
MC
644 case TLS_ST_SW_HELLO_REQ:
645 s->shutdown = 0;
646 if (SSL_IS_DTLS(s))
f5c7f5df 647 dtls1_clear_sent_buffer(s);
61ae935a
MC
648 break;
649
650 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
651 s->shutdown = 0;
652 if (SSL_IS_DTLS(s)) {
f5c7f5df 653 dtls1_clear_sent_buffer(s);
61ae935a
MC
654 /* We don't buffer this message so don't use the timer */
655 st->use_timer = 0;
656 }
657 break;
658
659 case TLS_ST_SW_SRVR_HELLO:
660 if (SSL_IS_DTLS(s)) {
661 /*
69687aa8 662 * Messages we write from now on should be buffered and
61ae935a
MC
663 * retransmitted if necessary, so we need to use the timer now
664 */
665 st->use_timer = 1;
666 }
667 break;
668
669 case TLS_ST_SW_SRVR_DONE:
670#ifndef OPENSSL_NO_SCTP
671 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s)))
672 return dtls_wait_for_dry(s);
673#endif
674 return WORK_FINISHED_CONTINUE;
675
676 case TLS_ST_SW_SESSION_TICKET:
30f05b19
MC
677 if (SSL_IS_TLS13(s)) {
678 /*
679 * Actually this is the end of the handshake, but we're going
680 * straight into writing the session ticket out. So we finish off
681 * the handshake, but keep the various buffers active.
682 */
683 return tls_finish_handshake(s, wst, 0);
684 } if (SSL_IS_DTLS(s)) {
61ae935a
MC
685 /*
686 * We're into the last flight. We don't retransmit the last flight
687 * unless we need to, so we don't use the timer
688 */
689 st->use_timer = 0;
690 }
691 break;
692
693 case TLS_ST_SW_CHANGE:
694 s->session->cipher = s->s3->tmp.new_cipher;
695 if (!s->method->ssl3_enc->setup_key_block(s)) {
fe3a3291 696 ossl_statem_set_error(s);
61ae935a
MC
697 return WORK_ERROR;
698 }
699 if (SSL_IS_DTLS(s)) {
700 /*
701 * We're into the last flight. We don't retransmit the last flight
702 * unless we need to, so we don't use the timer. This might have
703 * already been set to 0 if we sent a NewSessionTicket message,
704 * but we'll set it again here in case we didn't.
705 */
706 st->use_timer = 0;
707 }
708 return WORK_FINISHED_CONTINUE;
709
d7f8783f 710 case TLS_ST_EARLY_DATA:
f7e393be
MC
711 if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING)
712 return WORK_FINISHED_CONTINUE;
713 /* Fall through */
714
61ae935a 715 case TLS_ST_OK:
30f05b19 716 return tls_finish_handshake(s, wst, 1);
61ae935a
MC
717 }
718
719 return WORK_FINISHED_CONTINUE;
720}
721
722/*
723 * Perform any work that needs to be done after sending a message from the
724 * server to the client.
725 */
8481f583 726WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
61ae935a 727{
d6f1a6e9 728 OSSL_STATEM *st = &s->statem;
61ae935a
MC
729
730 s->init_num = 0;
731
e8aa8b6c 732 switch (st->hand_state) {
f3b3d7f0
RS
733 default:
734 /* No post work to be done */
735 break;
736
7d061fce
MC
737 case TLS_ST_SW_HELLO_RETRY_REQUEST:
738 if (statem_flush(s) != 1)
739 return WORK_MORE_A;
740 break;
741
61ae935a
MC
742 case TLS_ST_SW_HELLO_REQ:
743 if (statem_flush(s) != 1)
744 return WORK_MORE_A;
2c4a056f
MC
745 if (!ssl3_init_finished_mac(s)) {
746 ossl_statem_set_error(s);
747 return WORK_ERROR;
748 }
61ae935a
MC
749 break;
750
751 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
752 if (statem_flush(s) != 1)
753 return WORK_MORE_A;
754 /* HelloVerifyRequest resets Finished MAC */
2c4a056f
MC
755 if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
756 ossl_statem_set_error(s);
757 return WORK_ERROR;
758 }
61ae935a
MC
759 /*
760 * The next message should be another ClientHello which we need to
761 * treat like it was the first packet
762 */
763 s->first_packet = 1;
764 break;
765
766 case TLS_ST_SW_SRVR_HELLO:
767#ifndef OPENSSL_NO_SCTP
768 if (SSL_IS_DTLS(s) && s->hit) {
769 unsigned char sctpauthkey[64];
770 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
771
772 /*
773 * Add new shared key for SCTP-Auth, will be ignored if no
774 * SCTP used.
775 */
141eb8c6
MC
776 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
777 sizeof(DTLS1_SCTP_AUTH_LABEL));
61ae935a
MC
778
779 if (SSL_export_keying_material(s, sctpauthkey,
a230b26e
EK
780 sizeof(sctpauthkey), labelbuffer,
781 sizeof(labelbuffer), NULL, 0,
782 0) <= 0) {
fe3a3291 783 ossl_statem_set_error(s);
61ae935a
MC
784 return WORK_ERROR;
785 }
786
787 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
788 sizeof(sctpauthkey), sctpauthkey);
789 }
790#endif
92760c21
MC
791 /*
792 * TODO(TLS1.3): This actually causes a problem. We don't yet know
793 * whether the next record we are going to receive is an unencrypted
794 * alert, or an encrypted handshake message. We're going to need
795 * something clever in the record layer for this.
796 */
797 if (SSL_IS_TLS13(s)) {
798 if (!s->method->ssl3_enc->setup_key_block(s)
799 || !s->method->ssl3_enc->change_cipher_state(s,
fe5e20fd
MC
800 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE))
801 return WORK_ERROR;
802
803 if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED
804 && !s->method->ssl3_enc->change_cipher_state(s,
92760c21 805 SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ))
fe5e20fd 806 return WORK_ERROR;
92760c21 807 }
61ae935a
MC
808 break;
809
810 case TLS_ST_SW_CHANGE:
811#ifndef OPENSSL_NO_SCTP
812 if (SSL_IS_DTLS(s) && !s->hit) {
813 /*
814 * Change to new shared key of SCTP-Auth, will be ignored if
815 * no SCTP used.
816 */
817 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
818 0, NULL);
819 }
820#endif
821 if (!s->method->ssl3_enc->change_cipher_state(s,
a230b26e
EK
822 SSL3_CHANGE_CIPHER_SERVER_WRITE))
823 {
fe3a3291 824 ossl_statem_set_error(s);
61ae935a
MC
825 return WORK_ERROR;
826 }
827
828 if (SSL_IS_DTLS(s))
829 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
830 break;
831
832 case TLS_ST_SW_SRVR_DONE:
833 if (statem_flush(s) != 1)
834 return WORK_MORE_A;
835 break;
836
837 case TLS_ST_SW_FINISHED:
838 if (statem_flush(s) != 1)
839 return WORK_MORE_A;
840#ifndef OPENSSL_NO_SCTP
841 if (SSL_IS_DTLS(s) && s->hit) {
842 /*
843 * Change to new shared key of SCTP-Auth, will be ignored if
844 * no SCTP used.
845 */
846 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
847 0, NULL);
848 }
849#endif
92760c21
MC
850 if (SSL_IS_TLS13(s)) {
851 if (!s->method->ssl3_enc->generate_master_secret(s,
ec15acb6 852 s->master_secret, s->handshake_secret, 0,
92760c21
MC
853 &s->session->master_key_length)
854 || !s->method->ssl3_enc->change_cipher_state(s,
855 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
856 return WORK_ERROR;
857 }
61ae935a 858 break;
30f05b19 859
44c04a2e 860 case TLS_ST_SW_KEY_UPDATE:
57389a32
MC
861 if (statem_flush(s) != 1)
862 return WORK_MORE_A;
57389a32
MC
863 if (!tls13_update_key(s, 1))
864 return WORK_ERROR;
865 break;
866
30f05b19
MC
867 case TLS_ST_SW_SESSION_TICKET:
868 if (SSL_IS_TLS13(s) && statem_flush(s) != 1)
869 return WORK_MORE_A;
870 break;
61ae935a
MC
871 }
872
873 return WORK_FINISHED_CONTINUE;
874}
875
876/*
6392fb8e
MC
877 * Get the message construction function and message type for sending from the
878 * server
61ae935a
MC
879 *
880 * Valid return values are:
881 * 1: Success
882 * 0: Error
883 */
6392fb8e 884int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
a15c953f 885 confunc_f *confunc, int *mt)
61ae935a 886{
d6f1a6e9 887 OSSL_STATEM *st = &s->statem;
61ae935a 888
4a01c59f
MC
889 switch (st->hand_state) {
890 default:
891 /* Shouldn't happen */
892 return 0;
893
894 case TLS_ST_SW_CHANGE:
5923ad4b 895 if (SSL_IS_DTLS(s))
6392fb8e 896 *confunc = dtls_construct_change_cipher_spec;
4a01c59f 897 else
6392fb8e
MC
898 *confunc = tls_construct_change_cipher_spec;
899 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
4a01c59f 900 break;
f3b3d7f0 901
4a01c59f 902 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
6392fb8e
MC
903 *confunc = dtls_construct_hello_verify_request;
904 *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
4a01c59f 905 break;
61ae935a 906
4a01c59f
MC
907 case TLS_ST_SW_HELLO_REQ:
908 /* No construction function needed */
6392fb8e
MC
909 *confunc = NULL;
910 *mt = SSL3_MT_HELLO_REQUEST;
4a01c59f 911 break;
61ae935a 912
4a01c59f 913 case TLS_ST_SW_SRVR_HELLO:
6392fb8e
MC
914 *confunc = tls_construct_server_hello;
915 *mt = SSL3_MT_SERVER_HELLO;
4a01c59f 916 break;
61ae935a 917
4a01c59f 918 case TLS_ST_SW_CERT:
6392fb8e
MC
919 *confunc = tls_construct_server_certificate;
920 *mt = SSL3_MT_CERTIFICATE;
4a01c59f 921 break;
61ae935a 922
2c5dfdc3
MC
923 case TLS_ST_SW_CERT_VRFY:
924 *confunc = tls_construct_cert_verify;
925 *mt = SSL3_MT_CERTIFICATE_VERIFY;
926 break;
927
928
4a01c59f 929 case TLS_ST_SW_KEY_EXCH:
6392fb8e
MC
930 *confunc = tls_construct_server_key_exchange;
931 *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
4a01c59f 932 break;
61ae935a 933
4a01c59f 934 case TLS_ST_SW_CERT_REQ:
6392fb8e
MC
935 *confunc = tls_construct_certificate_request;
936 *mt = SSL3_MT_CERTIFICATE_REQUEST;
4a01c59f 937 break;
61ae935a 938
4a01c59f 939 case TLS_ST_SW_SRVR_DONE:
6392fb8e
MC
940 *confunc = tls_construct_server_done;
941 *mt = SSL3_MT_SERVER_DONE;
4a01c59f 942 break;
61ae935a 943
4a01c59f 944 case TLS_ST_SW_SESSION_TICKET:
6392fb8e
MC
945 *confunc = tls_construct_new_session_ticket;
946 *mt = SSL3_MT_NEWSESSION_TICKET;
4a01c59f 947 break;
61ae935a 948
4a01c59f 949 case TLS_ST_SW_CERT_STATUS:
6392fb8e
MC
950 *confunc = tls_construct_cert_status;
951 *mt = SSL3_MT_CERTIFICATE_STATUS;
4a01c59f 952 break;
61ae935a 953
4a01c59f 954 case TLS_ST_SW_FINISHED:
6392fb8e
MC
955 *confunc = tls_construct_finished;
956 *mt = SSL3_MT_FINISHED;
4a01c59f 957 break;
e46f2334 958
f7e393be
MC
959 case TLS_ST_EARLY_DATA:
960 *confunc = NULL;
961 *mt = SSL3_MT_DUMMY;
962 break;
963
e46f2334
MC
964 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
965 *confunc = tls_construct_encrypted_extensions;
966 *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
967 break;
7d061fce
MC
968
969 case TLS_ST_SW_HELLO_RETRY_REQUEST:
970 *confunc = tls_construct_hello_retry_request;
971 *mt = SSL3_MT_HELLO_RETRY_REQUEST;
972 break;
44c04a2e
MC
973
974 case TLS_ST_SW_KEY_UPDATE:
975 *confunc = tls_construct_key_update;
976 *mt = SSL3_MT_KEY_UPDATE;
977 break;
4a01c59f 978 }
61ae935a 979
5923ad4b 980 return 1;
61ae935a
MC
981}
982
8a18bc25
AG
983/*
984 * Maximum size (excluding the Handshake header) of a ClientHello message,
985 * calculated as follows:
986 *
987 * 2 + # client_version
988 * 32 + # only valid length for random
989 * 1 + # length of session_id
990 * 32 + # maximum size for session_id
991 * 2 + # length of cipher suites
992 * 2^16-2 + # maximum length of cipher suites array
993 * 1 + # length of compression_methods
994 * 2^8-1 + # maximum length of compression methods
995 * 2 + # length of extensions
996 * 2^16-1 # maximum length of extensions
997 */
998#define CLIENT_HELLO_MAX_LENGTH 131396
999
61ae935a
MC
1000#define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1001#define NEXT_PROTO_MAX_LENGTH 514
1002
1003/*
1004 * Returns the maximum allowed length for the current message that we are
1005 * reading. Excludes the message header.
1006 */
eda75751 1007size_t ossl_statem_server_max_message_size(SSL *s)
61ae935a 1008{
d6f1a6e9 1009 OSSL_STATEM *st = &s->statem;
61ae935a 1010
e8aa8b6c 1011 switch (st->hand_state) {
f3b3d7f0
RS
1012 default:
1013 /* Shouldn't happen */
1014 return 0;
1015
61ae935a 1016 case TLS_ST_SR_CLNT_HELLO:
8a18bc25 1017 return CLIENT_HELLO_MAX_LENGTH;
61ae935a 1018
ef6c191b
MC
1019 case TLS_ST_SR_END_OF_EARLY_DATA:
1020 return END_OF_EARLY_DATA_MAX_LENGTH;
1021
61ae935a
MC
1022 case TLS_ST_SR_CERT:
1023 return s->max_cert_list;
1024
1025 case TLS_ST_SR_KEY_EXCH:
1026 return CLIENT_KEY_EXCH_MAX_LENGTH;
1027
1028 case TLS_ST_SR_CERT_VRFY:
1029 return SSL3_RT_MAX_PLAIN_LENGTH;
1030
1031#ifndef OPENSSL_NO_NEXTPROTONEG
1032 case TLS_ST_SR_NEXT_PROTO:
1033 return NEXT_PROTO_MAX_LENGTH;
1034#endif
1035
1036 case TLS_ST_SR_CHANGE:
1037 return CCS_MAX_LENGTH;
1038
1039 case TLS_ST_SR_FINISHED:
1040 return FINISHED_MAX_LENGTH;
8cdc8c51
MC
1041
1042 case TLS_ST_SR_KEY_UPDATE:
1043 return KEY_UPDATE_MAX_LENGTH;
61ae935a 1044 }
61ae935a
MC
1045}
1046
1047/*
1048 * Process a message that the server has received from the client.
1049 */
8481f583 1050MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
61ae935a 1051{
d6f1a6e9 1052 OSSL_STATEM *st = &s->statem;
61ae935a 1053
e8aa8b6c 1054 switch (st->hand_state) {
f3b3d7f0
RS
1055 default:
1056 /* Shouldn't happen */
1057 return MSG_PROCESS_ERROR;
1058
61ae935a
MC
1059 case TLS_ST_SR_CLNT_HELLO:
1060 return tls_process_client_hello(s, pkt);
1061
ef6c191b
MC
1062 case TLS_ST_SR_END_OF_EARLY_DATA:
1063 return tls_process_end_of_early_data(s, pkt);
1064
61ae935a
MC
1065 case TLS_ST_SR_CERT:
1066 return tls_process_client_certificate(s, pkt);
1067
1068 case TLS_ST_SR_KEY_EXCH:
1069 return tls_process_client_key_exchange(s, pkt);
1070
1071 case TLS_ST_SR_CERT_VRFY:
1072 return tls_process_cert_verify(s, pkt);
1073
1074#ifndef OPENSSL_NO_NEXTPROTONEG
1075 case TLS_ST_SR_NEXT_PROTO:
1076 return tls_process_next_proto(s, pkt);
1077#endif
1078
1079 case TLS_ST_SR_CHANGE:
1080 return tls_process_change_cipher_spec(s, pkt);
1081
1082 case TLS_ST_SR_FINISHED:
1083 return tls_process_finished(s, pkt);
8cdc8c51
MC
1084
1085 case TLS_ST_SR_KEY_UPDATE:
1086 return tls_process_key_update(s, pkt);
1087
61ae935a 1088 }
61ae935a
MC
1089}
1090
1091/*
1092 * Perform any further processing required following the receipt of a message
1093 * from the client
1094 */
8481f583 1095WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
61ae935a 1096{
d6f1a6e9 1097 OSSL_STATEM *st = &s->statem;
61ae935a 1098
e8aa8b6c 1099 switch (st->hand_state) {
f3b3d7f0
RS
1100 default:
1101 /* Shouldn't happen */
1102 return WORK_ERROR;
1103
61ae935a
MC
1104 case TLS_ST_SR_CLNT_HELLO:
1105 return tls_post_process_client_hello(s, wst);
1106
1107 case TLS_ST_SR_KEY_EXCH:
1108 return tls_post_process_client_key_exchange(s, wst);
61ae935a 1109 }
92760c21 1110 return WORK_FINISHED_CONTINUE;
61ae935a
MC
1111}
1112
edc032b5 1113#ifndef OPENSSL_NO_SRP
71fa4513 1114static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
0f113f3e
MC
1115{
1116 int ret = SSL_ERROR_NONE;
1117
1118 *al = SSL_AD_UNRECOGNIZED_NAME;
1119
1120 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
1121 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
1122 if (s->srp_ctx.login == NULL) {
1123 /*
1124 * RFC 5054 says SHOULD reject, we do so if There is no srp
1125 * login name
1126 */
1127 ret = SSL3_AL_FATAL;
1128 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1129 } else {
1130 ret = SSL_srp_server_param_with_username(s, al);
1131 }
1132 }
1133 return ret;
1134}
edc032b5
BL
1135#endif
1136
c536b6be 1137int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
cb150cbc 1138 size_t cookie_len)
8ba708e5 1139{
8ba708e5 1140 /* Always use DTLS 1.0 version: see RFC 6347 */
c536b6be
MC
1141 if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
1142 || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
1143 return 0;
8ba708e5 1144
c536b6be 1145 return 1;
8ba708e5
MC
1146}
1147
7cea05dc 1148int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
8ba708e5 1149{
cb150cbc 1150 unsigned int cookie_leni;
8ba708e5
MC
1151 if (s->ctx->app_gen_cookie_cb == NULL ||
1152 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
cb150cbc
MC
1153 &cookie_leni) == 0 ||
1154 cookie_leni > 255) {
f0659bdb 1155 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
8ba708e5 1156 SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
8ba708e5
MC
1157 return 0;
1158 }
cb150cbc 1159 s->d1->cookie_len = cookie_leni;
8ba708e5 1160
4a01c59f
MC
1161 if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
1162 s->d1->cookie_len)) {
c536b6be 1163 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST, ERR_R_INTERNAL_ERROR);
c536b6be
MC
1164 return 0;
1165 }
8ba708e5 1166
8ba708e5
MC
1167 return 1;
1168}
1169
805a2e9e
MC
1170#ifndef OPENSSL_NO_EC
1171/*-
1172 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1173 * SecureTransport using the TLS extension block in |hello|.
1174 * Safari, since 10.6, sends exactly these extensions, in this order:
1175 * SNI,
1176 * elliptic_curves
1177 * ec_point_formats
33564cb7 1178 * signature_algorithms (for TLSv1.2 only)
805a2e9e
MC
1179 *
1180 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1181 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1182 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1183 * 10.8..10.8.3 (which don't work).
1184 */
1185static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
1186{
805a2e9e
MC
1187 static const unsigned char kSafariExtensionsBlock[] = {
1188 0x00, 0x0a, /* elliptic_curves extension */
1189 0x00, 0x08, /* 8 bytes */
1190 0x00, 0x06, /* 6 bytes of curve ids */
1191 0x00, 0x17, /* P-256 */
1192 0x00, 0x18, /* P-384 */
1193 0x00, 0x19, /* P-521 */
1194
1195 0x00, 0x0b, /* ec_point_formats */
1196 0x00, 0x02, /* 2 bytes */
1197 0x01, /* 1 point format */
1198 0x00, /* uncompressed */
1199 /* The following is only present in TLS 1.2 */
1200 0x00, 0x0d, /* signature_algorithms */
1201 0x00, 0x0c, /* 12 bytes */
1202 0x00, 0x0a, /* 10 bytes */
1203 0x05, 0x01, /* SHA-384/RSA */
1204 0x04, 0x01, /* SHA-256/RSA */
1205 0x02, 0x01, /* SHA-1/RSA */
1206 0x04, 0x03, /* SHA-256/ECDSA */
1207 0x02, 0x03, /* SHA-1/ECDSA */
1208 };
805a2e9e
MC
1209 /* Length of the common prefix (first two extensions). */
1210 static const size_t kSafariCommonExtensionsLength = 18;
1266eefd
MC
1211 unsigned int type;
1212 PACKET sni, tmppkt;
1213 size_t ext_len;
805a2e9e
MC
1214
1215 tmppkt = hello->extensions;
1216
1217 if (!PACKET_forward(&tmppkt, 2)
1218 || !PACKET_get_net_2(&tmppkt, &type)
1219 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1220 return;
6b473aca
MC
1221 }
1222
805a2e9e
MC
1223 if (type != TLSEXT_TYPE_server_name)
1224 return;
1225
1226 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1227 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1228
1229 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1230 ext_len);
6b473aca 1231}
805a2e9e 1232#endif /* !OPENSSL_NO_EC */
6b473aca 1233
be3583fa 1234MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
e27f234a 1235{
6b1bb98f 1236 int al = SSL_AD_INTERNAL_ERROR;
e27f234a 1237 /* |cookie| will only be initialized for DTLS. */
1ab3836b 1238 PACKET session_id, compression, extensions, cookie;
6e3ff632 1239 static const unsigned char null_compression = 0;
6b1bb98f 1240 CLIENTHELLO_MSG *clienthello;
e27f234a 1241
6b1bb98f
BK
1242 clienthello = OPENSSL_zalloc(sizeof(*clienthello));
1243 if (clienthello == NULL) {
1244 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1245 goto err;
1246 }
c7f47786
MC
1247 /* Check if this is actually an unexpected renegotiation ClientHello */
1248 if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
1249 s->renegotiate = 1;
1250 s->new_session = 1;
1251 }
1252
1ab3836b 1253 /*
b1b4b543 1254 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1ab3836b 1255 */
6b1bb98f 1256 clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
bbafa47b 1257 PACKET_null_init(&cookie);
1ab3836b 1258
6b1bb98f 1259 if (clienthello->isv2) {
9ceb2426 1260 unsigned int mt;
b1b4b543 1261
7d061fce
MC
1262 if (!SSL_IS_FIRST_HANDSHAKE(s) || s->hello_retry_request) {
1263 al = SSL_AD_HANDSHAKE_FAILURE;
1264 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNEXPECTED_MESSAGE);
1265 goto f_err;
1266 }
1267
32ec4153
MC
1268 /*-
1269 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1270 * header is sent directly on the wire, not wrapped as a TLS
1271 * record. Our record layer just processes the message length and passes
1272 * the rest right through. Its format is:
1273 * Byte Content
1274 * 0-1 msg_length - decoded by the record layer
1275 * 2 msg_type - s->init_msg points here
1276 * 3-4 version
1277 * 5-6 cipher_spec_length
1278 * 7-8 session_id_length
1279 * 9-10 challenge_length
1280 * ... ...
1281 */
1282
73999b62 1283 if (!PACKET_get_1(pkt, &mt)
a230b26e 1284 || mt != SSL2_MT_CLIENT_HELLO) {
32ec4153
MC
1285 /*
1286 * Should never happen. We should have tested this in the record
1287 * layer in order to have determined that this is a SSLv2 record
1288 * in the first place
1289 */
e27f234a 1290 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
d45ba43d 1291 goto err;
32ec4153 1292 }
32ec4153
MC
1293 }
1294
6b1bb98f 1295 if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
1ab3836b
MC
1296 al = SSL_AD_DECODE_ERROR;
1297 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1298 goto err;
0f113f3e
MC
1299 }
1300
b3e2272c 1301 /* Parse the message and load client random. */
6b1bb98f 1302 if (clienthello->isv2) {
32ec4153
MC
1303 /*
1304 * Handle an SSLv2 backwards compatible ClientHello
1305 * Note, this is only for SSLv3+ using the backward compatible format.
e2994cf0 1306 * Real SSLv2 is not supported, and is rejected below.
32ec4153 1307 */
1ab3836b 1308 unsigned int ciphersuite_len, session_id_len, challenge_len;
b3e2272c 1309 PACKET challenge;
0f113f3e 1310
1ab3836b 1311 if (!PACKET_get_net_2(pkt, &ciphersuite_len)
a230b26e
EK
1312 || !PACKET_get_net_2(pkt, &session_id_len)
1313 || !PACKET_get_net_2(pkt, &challenge_len)) {
e27f234a
MC
1314 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1315 SSL_R_RECORD_LENGTH_MISMATCH);
6c3cca57
AE
1316 al = SSL_AD_DECODE_ERROR;
1317 goto f_err;
5e9f0eeb 1318 }
0f113f3e 1319
293b5ca4
AG
1320 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1321 al = SSL_AD_DECODE_ERROR;
1322 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1323 goto f_err;
1324 }
1325
6b1bb98f 1326 if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
1ab3836b 1327 ciphersuite_len)
6b1bb98f 1328 || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
73999b62 1329 || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
b3e2272c 1330 /* No extensions. */
73999b62 1331 || PACKET_remaining(pkt) != 0) {
f0659bdb
MC
1332 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1333 SSL_R_RECORD_LENGTH_MISMATCH);
9ceb2426
MC
1334 al = SSL_AD_DECODE_ERROR;
1335 goto f_err;
1336 }
6b1bb98f 1337 clienthello->session_id_len = session_id_len;
9ceb2426 1338
fba7b84c 1339 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
6b1bb98f 1340 * here rather than sizeof(clienthello->random) because that is the limit
fba7b84c 1341 * for SSLv3 and it is fixed. It won't change even if
6b1bb98f 1342 * sizeof(clienthello->random) does.
fba7b84c
MC
1343 */
1344 challenge_len = challenge_len > SSL3_RANDOM_SIZE
1345 ? SSL3_RANDOM_SIZE : challenge_len;
6b1bb98f 1346 memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
b3e2272c 1347 if (!PACKET_copy_bytes(&challenge,
6b1bb98f 1348 clienthello->random + SSL3_RANDOM_SIZE -
cb21df32
DB
1349 challenge_len, challenge_len)
1350 /* Advertise only null compression. */
1351 || !PACKET_buf_init(&compression, &null_compression, 1)) {
f0659bdb 1352 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
b3e2272c 1353 al = SSL_AD_INTERNAL_ERROR;
9ceb2426
MC
1354 goto f_err;
1355 }
b3e2272c 1356
6b1bb98f 1357 PACKET_null_init(&clienthello->extensions);
0f113f3e 1358 } else {
b3e2272c 1359 /* Regular ClientHello. */
6b1bb98f 1360 if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
e2994cf0 1361 || !PACKET_get_length_prefixed_1(pkt, &session_id)
6b1bb98f 1362 || !PACKET_copy_all(&session_id, clienthello->session_id,
e2994cf0 1363 SSL_MAX_SSL_SESSION_ID_LENGTH,
6b1bb98f 1364 &clienthello->session_id_len)) {
9ceb2426 1365 al = SSL_AD_DECODE_ERROR;
f0659bdb 1366 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
9ceb2426
MC
1367 goto f_err;
1368 }
32ec4153 1369
b3e2272c 1370 if (SSL_IS_DTLS(s)) {
73999b62 1371 if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
32ec4153 1372 al = SSL_AD_DECODE_ERROR;
f0659bdb 1373 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
32ec4153
MC
1374 goto f_err;
1375 }
6b1bb98f 1376 if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
1ab3836b 1377 DTLS1_COOKIE_LENGTH,
6b1bb98f 1378 &clienthello->dtls_cookie_len)) {
1ab3836b
MC
1379 al = SSL_AD_DECODE_ERROR;
1380 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1381 goto f_err;
1382 }
b3e2272c
EK
1383 /*
1384 * If we require cookies and this ClientHello doesn't contain one,
1385 * just return since we do not want to allocate any memory yet.
1386 * So check cookie length...
1387 */
1388 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
6b1bb98f 1389 if (clienthello->dtls_cookie_len == 0)
a230b26e 1390 return 1;
b3e2272c 1391 }
5e9f0eeb 1392 }
0f113f3e 1393
6b1bb98f 1394 if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
1ab3836b
MC
1395 al = SSL_AD_DECODE_ERROR;
1396 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1397 goto f_err;
1398 }
1399
4bfe1432 1400 if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
a230b26e
EK
1401 al = SSL_AD_DECODE_ERROR;
1402 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1403 goto f_err;
b3e2272c 1404 }
1ab3836b 1405
b3e2272c 1406 /* Could be empty. */
1ab3836b 1407 if (PACKET_remaining(pkt) == 0) {
6b1bb98f 1408 PACKET_null_init(&clienthello->extensions);
1ab3836b 1409 } else {
6b1bb98f 1410 if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)) {
1ab3836b
MC
1411 al = SSL_AD_DECODE_ERROR;
1412 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1413 goto f_err;
1414 }
1415 }
1416 }
1417
6b1bb98f 1418 if (!PACKET_copy_all(&compression, clienthello->compressions,
e2994cf0 1419 MAX_COMPRESSIONS_SIZE,
6b1bb98f 1420 &clienthello->compressions_len)) {
1ab3836b
MC
1421 al = SSL_AD_DECODE_ERROR;
1422 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1423 goto f_err;
1424 }
1425
b1b4b543 1426 /* Preserve the raw extensions PACKET for later use */
6b1bb98f 1427 extensions = clienthello->extensions;
fe874d27 1428 if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
6b1bb98f 1429 &clienthello->pre_proc_exts, &al,
735d5b59 1430 &clienthello->pre_proc_exts_len, 1)) {
1ab3836b
MC
1431 /* SSLerr already been called */
1432 goto f_err;
1433 }
6b1bb98f 1434 s->clienthello = clienthello;
1ab3836b 1435
6b1bb98f
BK
1436 return MSG_PROCESS_CONTINUE_PROCESSING;
1437 f_err:
1438 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1439 err:
1440 ossl_statem_set_error(s);
1441
1442 OPENSSL_free(clienthello->pre_proc_exts);
1443 OPENSSL_free(clienthello);
1444
1445 return MSG_PROCESS_ERROR;
1446}
1447
bf846a6d 1448static int tls_early_post_process_client_hello(SSL *s, int *pal)
6b1bb98f
BK
1449{
1450 unsigned int j;
bf846a6d 1451 int i, al = SSL_AD_INTERNAL_ERROR;
6b1bb98f
BK
1452 int protverr;
1453 size_t loop;
1454 unsigned long id;
1455#ifndef OPENSSL_NO_COMP
1456 SSL_COMP *comp = NULL;
1457#endif
1458 const SSL_CIPHER *c;
1459 STACK_OF(SSL_CIPHER) *ciphers = NULL;
1460 STACK_OF(SSL_CIPHER) *scsvs = NULL;
1461 CLIENTHELLO_MSG *clienthello = s->clienthello;
f7f2a01d 1462 DOWNGRADE dgrd = DOWNGRADE_NONE;
6b1bb98f 1463
1ab3836b 1464 /* Finished parsing the ClientHello, now we can start processing it */
6b1bb98f
BK
1465 /* Give the early callback a crack at things */
1466 if (s->ctx->early_cb != NULL) {
1467 int code;
1468 /* A failure in the early callback terminates the connection. */
bf846a6d 1469 code = s->ctx->early_cb(s, &al, s->ctx->early_cb_arg);
6b1bb98f
BK
1470 if (code == 0)
1471 goto err;
1472 if (code < 0) {
1473 s->rwstate = SSL_EARLY_WORK;
1474 return code;
1475 }
1476 }
1ab3836b
MC
1477
1478 /* Set up the client_random */
6b1bb98f 1479 memcpy(s->s3->client_random, clienthello->random, SSL3_RANDOM_SIZE);
1ab3836b
MC
1480
1481 /* Choose the version */
1482
6b1bb98f
BK
1483 if (clienthello->isv2) {
1484 if (clienthello->legacy_version == SSL2_VERSION
1485 || (clienthello->legacy_version & 0xff00)
b1b4b543
MC
1486 != (SSL3_VERSION_MAJOR << 8)) {
1487 /*
1488 * This is real SSLv2 or something complete unknown. We don't
1489 * support it.
1490 */
6b1bb98f 1491 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1ab3836b
MC
1492 goto err;
1493 }
b1b4b543 1494 /* SSLv3/TLS */
6b1bb98f 1495 s->client_version = clienthello->legacy_version;
1ab3836b
MC
1496 }
1497 /*
1498 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1499 * versions are potentially compatible. Version negotiation comes later.
1500 */
1501 if (!SSL_IS_DTLS(s)) {
f7f2a01d 1502 protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1ab3836b 1503 } else if (s->method->version != DTLS_ANY_VERSION &&
6b1bb98f 1504 DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) {
1ab3836b
MC
1505 protverr = SSL_R_VERSION_TOO_LOW;
1506 } else {
1507 protverr = 0;
1508 }
1509
1510 if (protverr) {
6b1bb98f 1511 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
7d061fce 1512 if (SSL_IS_FIRST_HANDSHAKE(s)) {
b1b4b543 1513 /* like ssl3_get_record, send alert using remote version number */
6b1bb98f 1514 s->version = s->client_version = clienthello->legacy_version;
1ab3836b 1515 }
bf846a6d 1516 al = SSL_AD_PROTOCOL_VERSION;
6b1bb98f 1517 goto err;
b3e2272c
EK
1518 }
1519
635b7d3f 1520 /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
9e0ac6a2 1521 if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
bf846a6d 1522 al = SSL_AD_UNEXPECTED_MESSAGE;
9e0ac6a2
MC
1523 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1524 SSL_R_NOT_ON_RECORD_BOUNDARY);
1525 goto err;
1526 }
1527
1ed65871
DB
1528 if (SSL_IS_DTLS(s)) {
1529 /* Empty cookie was already handled above by returning early. */
1530 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1531 if (s->ctx->app_verify_cookie_cb != NULL) {
6b1bb98f
BK
1532 if (s->ctx->app_verify_cookie_cb(s, clienthello->dtls_cookie,
1533 clienthello->dtls_cookie_len) == 0) {
bf846a6d 1534 al = SSL_AD_HANDSHAKE_FAILURE;
6b1bb98f 1535 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1ed65871 1536 SSL_R_COOKIE_MISMATCH);
6b1bb98f 1537 goto err;
1ed65871
DB
1538 /* else cookie verification succeeded */
1539 }
a230b26e 1540 /* default verification */
6b1bb98f
BK
1541 } else if (s->d1->cookie_len != clienthello->dtls_cookie_len
1542 || memcmp(clienthello->dtls_cookie, s->d1->cookie,
1ab3836b 1543 s->d1->cookie_len) != 0) {
bf846a6d 1544 al = SSL_AD_HANDSHAKE_FAILURE;
6b1bb98f
BK
1545 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1546 goto err;
1ed65871
DB
1547 }
1548 s->d1->cookie_verified = 1;
1549 }
1550 if (s->method->version == DTLS_ANY_VERSION) {
f7f2a01d 1551 protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1ed65871 1552 if (protverr != 0) {
6b1bb98f 1553 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
1ed65871 1554 s->version = s->client_version;
bf846a6d 1555 al = SSL_AD_PROTOCOL_VERSION;
6b1bb98f 1556 goto err;
1ed65871
DB
1557 }
1558 }
1559 }
1560
b3e2272c
EK
1561 s->hit = 0;
1562
1ab3836b 1563 /* We need to do this before getting the session */
70af3d8e 1564 if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
fe874d27 1565 SSL_EXT_CLIENT_HELLO,
bf846a6d 1566 clienthello->pre_proc_exts, NULL, 0, &al)) {
6b1bb98f
BK
1567 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1568 goto err;
1ab3836b
MC
1569 }
1570
b3e2272c
EK
1571 /*
1572 * We don't allow resumption in a backwards compatible ClientHello.
1573 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1574 *
1575 * Versions before 0.9.7 always allow clients to resume sessions in
1576 * renegotiation. 0.9.7 and later allow this by default, but optionally
1577 * ignore resumption requests with flag
1578 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1579 * than a change to default behavior so that applications relying on
1580 * this for security won't even compile against older library versions).
1581 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1582 * request renegotiation but not a new session (s->new_session remains
1583 * unset): for servers, this essentially just means that the
1584 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1585 * ignored.
1586 */
6b1bb98f 1587 if (clienthello->isv2 ||
b3e2272c
EK
1588 (s->new_session &&
1589 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1590 if (!ssl_get_new_session(s, 1))
1591 goto err;
1592 } else {
bf846a6d 1593 i = ssl_get_prev_session(s, clienthello, &al);
128ae276 1594 if (i == 1) {
b3e2272c
EK
1595 /* previous session */
1596 s->hit = 1;
1597 } else if (i == -1) {
6b1bb98f 1598 goto err;
32ec4153 1599 } else {
b3e2272c
EK
1600 /* i == 0 */
1601 if (!ssl_get_new_session(s, 1))
32ec4153 1602 goto err;
0f113f3e 1603 }
b3e2272c 1604 }
0f113f3e 1605
6b1bb98f 1606 if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,
bf846a6d 1607 clienthello->isv2, &al) ||
6b1bb98f 1608 !bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, &scsvs,
bf846a6d 1609 clienthello->isv2, &al)) {
6b1bb98f 1610 goto err;
b3e2272c 1611 }
5e9f0eeb 1612
90134d98
BK
1613 s->s3->send_connection_binding = 0;
1614 /* Check what signalling cipher-suite values were received. */
1615 if (scsvs != NULL) {
1616 for(i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
1617 c = sk_SSL_CIPHER_value(scsvs, i);
1618 if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
1619 if (s->renegotiate) {
1620 /* SCSV is fatal if renegotiating */
6b1bb98f 1621 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
90134d98 1622 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
bf846a6d 1623 al = SSL_AD_HANDSHAKE_FAILURE;
6b1bb98f 1624 goto err;
90134d98
BK
1625 }
1626 s->s3->send_connection_binding = 1;
1627 } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&
1628 !ssl_check_version_downgrade(s)) {
1629 /*
1630 * This SCSV indicates that the client previously tried
1631 * a higher version. We should fail if the current version
1632 * is an unexpected downgrade, as that indicates that the first
1633 * connection may have been tampered with in order to trigger
1634 * an insecure downgrade.
1635 */
6b1bb98f 1636 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
90134d98 1637 SSL_R_INAPPROPRIATE_FALLBACK);
bf846a6d 1638 al = SSL_AD_INAPPROPRIATE_FALLBACK;
6b1bb98f 1639 goto err;
90134d98
BK
1640 }
1641 }
1642 }
1643
b3e2272c
EK
1644 /* If it is a hit, check that the cipher is in the list */
1645 if (s->hit) {
1646 j = 0;
1647 id = s->session->cipher->id;
d02b48c6 1648
413c4f45 1649#ifdef CIPHER_DEBUG
a230b26e 1650 fprintf(stderr, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers));
413c4f45 1651#endif
b3e2272c
EK
1652 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1653 c = sk_SSL_CIPHER_value(ciphers, i);
413c4f45 1654#ifdef CIPHER_DEBUG
b3e2272c
EK
1655 fprintf(stderr, "client [%2d of %2d]:%s\n",
1656 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
88f2a4cf 1657#endif
b3e2272c
EK
1658 if (c->id == id) {
1659 j = 1;
1660 break;
32ec4153 1661 }
0f113f3e 1662 }
b3e2272c 1663 if (j == 0) {
ec30e856 1664 /*
b3e2272c
EK
1665 * we need to have the cipher in the cipher list if we are asked
1666 * to reuse it
ec30e856 1667 */
bf846a6d 1668 al = SSL_AD_ILLEGAL_PARAMETER;
6b1bb98f 1669 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
b3e2272c 1670 SSL_R_REQUIRED_CIPHER_MISSING);
6b1bb98f 1671 goto err;
32ec4153 1672 }
b3e2272c 1673 }
9ceb2426 1674
6b1bb98f
BK
1675 for (loop = 0; loop < clienthello->compressions_len; loop++) {
1676 if (clienthello->compressions[loop] == 0)
b3e2272c 1677 break;
0f113f3e 1678 }
32ec4153 1679
6b1bb98f 1680 if (loop >= clienthello->compressions_len) {
b3e2272c 1681 /* no compress */
bf846a6d 1682 al = SSL_AD_DECODE_ERROR;
6b1bb98f
BK
1683 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1684 goto err;
b3e2272c 1685 }
f100b031 1686
805a2e9e
MC
1687#ifndef OPENSSL_NO_EC
1688 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
6b1bb98f 1689 ssl_check_for_safari(s, clienthello);
805a2e9e
MC
1690#endif /* !OPENSSL_NO_EC */
1691
0f113f3e 1692 /* TLS extensions */
fe874d27 1693 if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,
735d5b59 1694 clienthello->pre_proc_exts, NULL, 0, &al, 1)) {
6b1bb98f
BK
1695 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1696 goto err;
0f113f3e
MC
1697 }
1698
1699 /*
1700 * Check if we want to use external pre-shared secret for this handshake
1701 * for not reused session only. We need to generate server_random before
1702 * calling tls_session_secret_cb in order to allow SessionTicket
1703 * processing to use it in key derivation.
1704 */
1705 {
1706 unsigned char *pos;
1707 pos = s->s3->server_random;
f7f2a01d 1708 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) {
6b1bb98f 1709 goto err;
0f113f3e
MC
1710 }
1711 }
1712
aff8c126 1713 if (!s->hit && s->version >= TLS1_VERSION && s->ext.session_secret_cb) {
4a640fb6 1714 const SSL_CIPHER *pref_cipher = NULL;
8c1a5343
MC
1715 /*
1716 * s->session->master_key_length is a size_t, but this is an int for
1717 * backwards compat reasons
1718 */
1719 int master_key_length;
0f113f3e 1720
8c1a5343 1721 master_key_length = sizeof(s->session->master_key);
aff8c126 1722 if (s->ext.session_secret_cb(s, s->session->master_key,
8c1a5343 1723 &master_key_length, ciphers,
0f113f3e 1724 &pref_cipher,
aff8c126 1725 s->ext.session_secret_cb_arg)
8c1a5343
MC
1726 && master_key_length > 0) {
1727 s->session->master_key_length = master_key_length;
0f113f3e
MC
1728 s->hit = 1;
1729 s->session->ciphers = ciphers;
1730 s->session->verify_result = X509_V_OK;
1731
1732 ciphers = NULL;
1733
1734 /* check if some cipher was preferred by call back */
3f4bf115
DSH
1735 if (pref_cipher == NULL)
1736 pref_cipher = ssl3_choose_cipher(s, s->session->ciphers,
1737 SSL_get_ciphers(s));
0f113f3e 1738 if (pref_cipher == NULL) {
bf846a6d 1739 al = SSL_AD_HANDSHAKE_FAILURE;
6b1bb98f
BK
1740 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1741 goto err;
0f113f3e
MC
1742 }
1743
1744 s->session->cipher = pref_cipher;
25aaa98a 1745 sk_SSL_CIPHER_free(s->cipher_list);
0f113f3e 1746 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
25aaa98a 1747 sk_SSL_CIPHER_free(s->cipher_list_by_id);
0f113f3e
MC
1748 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1749 }
1750 }
58ece833 1751
0f113f3e
MC
1752 /*
1753 * Worst case, we will use the NULL compression, but if we have other
b2ce0337 1754 * options, we will now look for them. We have complen-1 compression
0f113f3e
MC
1755 * algorithms from the client, starting at q.
1756 */
1757 s->s3->tmp.new_compression = NULL;
1fe35494
MC
1758 if (SSL_IS_TLS13(s)) {
1759 /*
1760 * We already checked above that the NULL compression method appears in
1761 * the list. Now we check there aren't any others (which is illegal in
1762 * a TLSv1.3 ClientHello.
1763 */
1764 if (clienthello->compressions_len != 1) {
1765 al = SSL_AD_ILLEGAL_PARAMETER;
1766 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1767 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1768 goto err;
1769 }
1770 }
09b6c2ef 1771#ifndef OPENSSL_NO_COMP
0f113f3e 1772 /* This only happens if we have a cache hit */
1fe35494 1773 else if (s->session->compress_meth != 0) {
0f113f3e 1774 int m, comp_id = s->session->compress_meth;
9ceb2426 1775 unsigned int k;
0f113f3e
MC
1776 /* Perform sanity checks on resumed compression algorithm */
1777 /* Can't disable compression */
1778 if (!ssl_allow_compression(s)) {
6b1bb98f 1779 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
0f113f3e 1780 SSL_R_INCONSISTENT_COMPRESSION);
6b1bb98f 1781 goto err;
0f113f3e
MC
1782 }
1783 /* Look for resumed compression method */
1784 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1785 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1786 if (comp_id == comp->id) {
1787 s->s3->tmp.new_compression = comp;
1788 break;
1789 }
1790 }
1791 if (s->s3->tmp.new_compression == NULL) {
6b1bb98f 1792 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
0f113f3e 1793 SSL_R_INVALID_COMPRESSION_ALGORITHM);
6b1bb98f 1794 goto err;
0f113f3e
MC
1795 }
1796 /* Look for resumed method in compression list */
6b1bb98f
BK
1797 for (k = 0; k < clienthello->compressions_len; k++) {
1798 if (clienthello->compressions[k] == comp_id)
0f113f3e
MC
1799 break;
1800 }
6b1bb98f 1801 if (k >= clienthello->compressions_len) {
bf846a6d 1802 al = SSL_AD_ILLEGAL_PARAMETER;
6b1bb98f 1803 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
8fdc99cb 1804 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
6b1bb98f 1805 goto err;
0f113f3e 1806 }
c19602b5 1807 } else if (s->hit) {
0f113f3e 1808 comp = NULL;
1fe35494 1809 } else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
df6741c9 1810 /* See if we have a match */
9ceb2426
MC
1811 int m, nn, v, done = 0;
1812 unsigned int o;
0f113f3e
MC
1813
1814 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1815 for (m = 0; m < nn; m++) {
1816 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1817 v = comp->id;
6b1bb98f
BK
1818 for (o = 0; o < clienthello->compressions_len; o++) {
1819 if (v == clienthello->compressions[o]) {
0f113f3e
MC
1820 done = 1;
1821 break;
1822 }
1823 }
1824 if (done)
1825 break;
1826 }
1827 if (done)
1828 s->s3->tmp.new_compression = comp;
1829 else
1830 comp = NULL;
1831 }
e6f418bc 1832#else
0f113f3e
MC
1833 /*
1834 * If compression is disabled we'd better not try to resume a session
1835 * using compression.
1836 */
1837 if (s->session->compress_meth != 0) {
6b1bb98f
BK
1838 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1839 goto err;
0f113f3e 1840 }
09b6c2ef 1841#endif
413c4f45 1842
0f113f3e
MC
1843 /*
1844 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1845 */
d02b48c6 1846
69b2d393 1847 if (!s->hit || s->hello_retry_request) {
25aaa98a 1848 sk_SSL_CIPHER_free(s->session->ciphers);
0f113f3e
MC
1849 s->session->ciphers = ciphers;
1850 if (ciphers == NULL) {
bf846a6d 1851 al = SSL_AD_INTERNAL_ERROR;
6b1bb98f
BK
1852 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1853 goto err;
0f113f3e
MC
1854 }
1855 ciphers = NULL;
69b2d393
MC
1856 }
1857
1858 if (!s->hit) {
1859#ifdef OPENSSL_NO_COMP
1860 s->session->compress_meth = 0;
1861#else
1862 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1863#endif
0f113f3e 1864 if (!tls1_set_server_sigalgs(s)) {
6b1bb98f 1865 SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
0f113f3e
MC
1866 goto err;
1867 }
e27f234a
MC
1868 }
1869
1870 sk_SSL_CIPHER_free(ciphers);
6b1bb98f
BK
1871 sk_SSL_CIPHER_free(scsvs);
1872 OPENSSL_free(clienthello->pre_proc_exts);
1873 OPENSSL_free(s->clienthello);
1874 s->clienthello = NULL;
1875 return 1;
e27f234a 1876 err:
fe3a3291 1877 ossl_statem_set_error(s);
bf846a6d 1878 *pal = al;
e27f234a
MC
1879
1880 sk_SSL_CIPHER_free(ciphers);
6b1bb98f
BK
1881 sk_SSL_CIPHER_free(scsvs);
1882 OPENSSL_free(clienthello->pre_proc_exts);
1883 OPENSSL_free(s->clienthello);
1884 s->clienthello = NULL;
e27f234a 1885
6b1bb98f 1886 return 0;
e27f234a
MC
1887}
1888
24b8e4b2
MC
1889/*
1890 * Call the status request callback if needed. Upon success, returns 1.
1266eefd 1891 * Upon failure, returns 0 and sets |*al| to the appropriate fatal alert.
24b8e4b2
MC
1892 */
1893static int tls_handle_status_request(SSL *s, int *al)
1894{
aff8c126 1895 s->ext.status_expected = 0;
24b8e4b2
MC
1896
1897 /*
1898 * If status request then ask callback what to do. Note: this must be
1899 * called after servername callbacks in case the certificate has changed,
1900 * and must be called after the cipher has been chosen because this may
1901 * influence which certificate is sent
1902 */
aff8c126
RS
1903 if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && s->ctx != NULL
1904 && s->ctx->ext.status_cb != NULL) {
24b8e4b2 1905 int ret;
1266eefd 1906
24b8e4b2 1907 /* If no certificate can't return certificate status */
a497cf25 1908 if (s->s3->tmp.cert != NULL) {
24b8e4b2
MC
1909 /*
1910 * Set current certificate to one we will use so SSL_get_certificate
1911 * et al can pick it up.
1912 */
a497cf25 1913 s->cert->key = s->s3->tmp.cert;
aff8c126 1914 ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
24b8e4b2
MC
1915 switch (ret) {
1916 /* We don't want to send a status request response */
1917 case SSL_TLSEXT_ERR_NOACK:
aff8c126 1918 s->ext.status_expected = 0;
24b8e4b2
MC
1919 break;
1920 /* status request response should be sent */
1921 case SSL_TLSEXT_ERR_OK:
aff8c126
RS
1922 if (s->ext.ocsp.resp)
1923 s->ext.status_expected = 1;
24b8e4b2
MC
1924 break;
1925 /* something bad happened */
1926 case SSL_TLSEXT_ERR_ALERT_FATAL:
1927 default:
1928 *al = SSL_AD_INTERNAL_ERROR;
1929 return 0;
1930 }
1931 }
1932 }
1933
1934 return 1;
1935}
1936
be3583fa 1937WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
e27f234a 1938{
d13dd4be 1939 int al = SSL_AD_HANDSHAKE_FAILURE;
4a640fb6 1940 const SSL_CIPHER *cipher;
e27f234a
MC
1941
1942 if (wst == WORK_MORE_A) {
6b1bb98f
BK
1943 int rv = tls_early_post_process_client_hello(s, &al);
1944 if (rv == 0) {
1945 /* SSLErr() was already called */
1946 goto f_err;
1947 }
1948 if (rv < 0)
1949 return WORK_MORE_A;
1950 wst = WORK_MORE_B;
1951 }
1952 if (wst == WORK_MORE_B) {
69b2d393 1953 if (!s->hit || s->hello_retry_request) {
e27f234a
MC
1954 /* Let cert callback update server certificates if required */
1955 if (s->cert->cert_cb) {
1956 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1957 if (rv == 0) {
1958 al = SSL_AD_INTERNAL_ERROR;
a230b26e
EK
1959 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1960 SSL_R_CERT_CB_ERROR);
e27f234a
MC
1961 goto f_err;
1962 }
1963 if (rv < 0) {
1964 s->rwstate = SSL_X509_LOOKUP;
6b1bb98f 1965 return WORK_MORE_B;
e27f234a
MC
1966 }
1967 s->rwstate = SSL_NOTHING;
0f113f3e 1968 }
a230b26e
EK
1969 cipher =
1970 ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
e27f234a
MC
1971
1972 if (cipher == NULL) {
a230b26e
EK
1973 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1974 SSL_R_NO_SHARED_CIPHER);
e27f234a 1975 goto f_err;
0f113f3e 1976 }
11c67eea
MC
1977 if (SSL_IS_TLS13(s) && s->s3->tmp.new_cipher != NULL
1978 && s->s3->tmp.new_cipher->id != cipher->id) {
1979 /*
1980 * A previous HRR picked a different ciphersuite to the one we
1981 * just selected. Something must have changed.
1982 */
1983 al = SSL_AD_ILLEGAL_PARAMETER;
1984 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO, SSL_R_BAD_CIPHER);
1985 goto f_err;
1986 }
e27f234a 1987 s->s3->tmp.new_cipher = cipher;
69b2d393
MC
1988 if (!s->hit) {
1989 if (!tls_choose_sigalg(s, &al))
1990 goto f_err;
1991 /* check whether we should disable session resumption */
1992 if (s->not_resumable_session_cb != NULL)
1993 s->session->not_resumable =
1994 s->not_resumable_session_cb(s, ((cipher->algorithm_mkey
1995 & (SSL_kDHE | SSL_kECDHE))
1996 != 0));
1997 if (s->session->not_resumable)
1998 /* do not send a session ticket */
1999 s->ext.ticket_expected = 0;
2000 }
e27f234a
MC
2001 } else {
2002 /* Session-id reuse */
2003 s->s3->tmp.new_cipher = s->session->cipher;
0f113f3e 2004 }
0f113f3e 2005
e27f234a
MC
2006 /*-
2007 * we now have the following setup.
2008 * client_random
60250017 2009 * cipher_list - our preferred list of ciphers
2010 * ciphers - the clients preferred list of ciphers
e27f234a
MC
2011 * compression - basically ignored right now
2012 * ssl version is set - sslv3
2013 * s->session - The ssl session has been setup.
2014 * s->hit - session reuse flag
2015 * s->s3->tmp.new_cipher- the new cipher to use.
2016 */
0f113f3e 2017
24b8e4b2
MC
2018 /*
2019 * Call status_request callback if needed. Has to be done after the
2020 * certificate callbacks etc above.
2021 */
2022 if (!tls_handle_status_request(s, &al)) {
2023 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
2024 SSL_R_CLIENTHELLO_TLSEXT);
2025 goto f_err;
e27f234a 2026 }
0f113f3e 2027
6b1bb98f 2028 wst = WORK_MORE_C;
e27f234a
MC
2029 }
2030#ifndef OPENSSL_NO_SRP
6b1bb98f 2031 if (wst == WORK_MORE_C) {
e27f234a
MC
2032 int ret;
2033 if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
2034 /*
2035 * callback indicates further work to be done
2036 */
2037 s->rwstate = SSL_X509_LOOKUP;
6b1bb98f 2038 return WORK_MORE_C;
e27f234a
MC
2039 }
2040 if (ret != SSL_ERROR_NONE) {
2041 /*
2042 * This is not really an error but the only means to for
2043 * a client to detect whether srp is supported.
2044 */
2045 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
2046 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
a230b26e 2047 SSL_R_CLIENTHELLO_TLSEXT);
7bb37cb5
E
2048 else
2049 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
2050 SSL_R_PSK_IDENTITY_NOT_FOUND);
e27f234a 2051 goto f_err;
0f113f3e
MC
2052 }
2053 }
e27f234a 2054#endif
0f113f3e 2055
e27f234a 2056 return WORK_FINISHED_STOP;
0f113f3e 2057 f_err:
e27f234a 2058 ssl3_send_alert(s, SSL3_AL_FATAL, al);
fe3a3291 2059 ossl_statem_set_error(s);
e27f234a
MC
2060 return WORK_ERROR;
2061}
2062
7cea05dc 2063int tls_construct_server_hello(SSL *s, WPACKET *pkt)
0f113f3e 2064{
ec60ccc1
MC
2065 int compm, al = SSL_AD_INTERNAL_ERROR;
2066 size_t sl, len;
f2342b7a 2067 int version;
0f113f3e 2068
b97667ce 2069 /* TODO(TLS1.3): Remove the DRAFT conditional before release */
f2342b7a
MC
2070 version = SSL_IS_TLS13(s) ? TLS1_3_VERSION_DRAFT : s->version;
2071 if (!WPACKET_put_bytes_u16(pkt, version)
8157d44b
MC
2072 /*
2073 * Random stuff. Filling of the server_random takes place in
2074 * tls_process_client_hello()
2075 */
7cea05dc 2076 || !WPACKET_memcpy(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
8157d44b
MC
2077 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
2078 goto err;
2079 }
0f113f3e 2080
e27f234a
MC
2081 /*-
2082 * There are several cases for the session ID to send
2083 * back in the server hello:
2084 * - For session reuse from the session cache,
2085 * we send back the old session ID.
2086 * - If stateless session reuse (using a session ticket)
2087 * is successful, we send back the client's "session ID"
2088 * (which doesn't actually identify the session).
2089 * - If it is a new session, we send back the new
2090 * session ID.
2091 * - However, if we want the new session to be single-use,
2092 * we send back a 0-length session ID.
2093 * s->hit is non-zero in either case of session reuse,
2094 * so the following won't overwrite an ID that we're supposed
2095 * to send back.
2096 */
2097 if (s->session->not_resumable ||
2098 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
2099 && !s->hit))
2100 s->session->session_id_length = 0;
2101
2102 sl = s->session->session_id_length;
ec60ccc1 2103 if (sl > sizeof(s->session->session_id)) {
e27f234a 2104 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
8157d44b 2105 goto err;
e27f234a 2106 }
0f113f3e 2107
8157d44b 2108 /* set up the compression method */
09b6c2ef 2109#ifdef OPENSSL_NO_COMP
8157d44b 2110 compm = 0;
09b6c2ef 2111#else
e27f234a 2112 if (s->s3->tmp.new_compression == NULL)
8157d44b 2113 compm = 0;
e27f234a 2114 else
8157d44b 2115 compm = s->s3->tmp.new_compression->id;
09b6c2ef 2116#endif
e481f9b9 2117
71728dd8
MC
2118 if ((!SSL_IS_TLS13(s)
2119 && !WPACKET_sub_memcpy_u8(pkt, s->session->session_id, sl))
7cea05dc 2120 || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt, &len)
71728dd8
MC
2121 || (!SSL_IS_TLS13(s)
2122 && !WPACKET_put_bytes_u8(pkt, compm))
7da160b0 2123 || !tls_construct_extensions(s, pkt,
3434f40b 2124 SSL_IS_TLS13(s)
fe874d27
MC
2125 ? SSL_EXT_TLS1_3_SERVER_HELLO
2126 : SSL_EXT_TLS1_2_SERVER_HELLO,
30aeba43 2127 NULL, 0, &al)) {
e27f234a 2128 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
8157d44b 2129 goto err;
0f113f3e 2130 }
d02b48c6 2131
aff9929b
MC
2132 if (!(s->verify_mode & SSL_VERIFY_PEER)
2133 && !ssl3_digest_cached_records(s, 0)) {
2134 al = SSL_AD_INTERNAL_ERROR;
2135 goto err;
2136 }
2137
e27f234a 2138 return 1;
8157d44b 2139 err:
7da160b0 2140 ssl3_send_alert(s, SSL3_AL_FATAL, al);
8157d44b 2141 return 0;
0f113f3e 2142}
d02b48c6 2143
7cea05dc 2144int tls_construct_server_done(SSL *s, WPACKET *pkt)
e27f234a 2145{
e27f234a 2146 if (!s->s3->tmp.cert_request) {
5923ad4b
MC
2147 if (!ssl3_digest_cached_records(s, 0)) {
2148 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2149 return 0;
2150 }
e27f234a 2151 }
e27f234a
MC
2152 return 1;
2153}
2154
7cea05dc 2155int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
0f113f3e 2156{
bc36ee62 2157#ifndef OPENSSL_NO_DH
e2b420fd 2158 EVP_PKEY *pkdh = NULL;
ea262260 2159#endif
10bf4fc2 2160#ifndef OPENSSL_NO_EC
0f113f3e 2161 unsigned char *encodedPoint = NULL;
348240c6 2162 size_t encodedlen = 0;
0f113f3e 2163 int curve_id = 0;
d02b48c6 2164#endif
f695571e 2165 const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
c13d2a5b 2166 int al = SSL_AD_INTERNAL_ERROR, i;
0f113f3e 2167 unsigned long type;
2ac6115d 2168 const BIGNUM *r[4];
bfb0641f 2169 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
fe3066ee 2170 EVP_PKEY_CTX *pctx = NULL;
c13d2a5b
MC
2171 size_t paramlen, paramoffset;
2172
5923ad4b 2173 if (!WPACKET_get_total_written(pkt, &paramoffset)) {
e4e1aa90 2174 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
c13d2a5b
MC
2175 goto f_err;
2176 }
0f113f3e 2177
6e59a892
RL
2178 if (md_ctx == NULL) {
2179 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
6e59a892
RL
2180 goto f_err;
2181 }
0f113f3e 2182
e27f234a 2183 type = s->s3->tmp.new_cipher->algorithm_mkey;
e27f234a 2184
e27f234a 2185 r[0] = r[1] = r[2] = r[3] = NULL;
85269210 2186#ifndef OPENSSL_NO_PSK
e27f234a
MC
2187 /* Plain PSK or RSAPSK nothing to do */
2188 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
2189 } else
85269210 2190#endif /* !OPENSSL_NO_PSK */
bc36ee62 2191#ifndef OPENSSL_NO_DH
e27f234a 2192 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
94d61512
BL
2193 CERT *cert = s->cert;
2194
e2b420fd
DSH
2195 EVP_PKEY *pkdhp = NULL;
2196 DH *dh;
2197
e27f234a 2198 if (s->cert->dh_tmp_auto) {
e2b420fd
DSH
2199 DH *dhp = ssl_get_auto_dh(s);
2200 pkdh = EVP_PKEY_new();
2201 if (pkdh == NULL || dhp == NULL) {
2202 DH_free(dhp);
e27f234a 2203 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
0f113f3e 2204 ERR_R_INTERNAL_ERROR);
e27f234a 2205 goto f_err;
0f113f3e 2206 }
e2b420fd
DSH
2207 EVP_PKEY_assign_DH(pkdh, dhp);
2208 pkdhp = pkdh;
2209 } else {
2210 pkdhp = cert->dh_tmp;
2211 }
2212 if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
2213 DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);
2214 pkdh = ssl_dh_to_pkey(dhp);
2215 if (pkdh == NULL) {
e2b420fd
DSH
2216 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2217 ERR_R_INTERNAL_ERROR);
2218 goto f_err;
2219 }
2220 pkdhp = pkdh;
2221 }
2222 if (pkdhp == NULL) {
e27f234a
MC
2223 al = SSL_AD_HANDSHAKE_FAILURE;
2224 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2225 SSL_R_MISSING_TMP_DH_KEY);
2226 goto f_err;
2227 }
2228 if (!ssl_security(s, SSL_SECOP_TMP_DH,
e2b420fd 2229 EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {
e27f234a
MC
2230 al = SSL_AD_HANDSHAKE_FAILURE;
2231 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2232 SSL_R_DH_KEY_TOO_SMALL);
2233 goto f_err;
2234 }
e2b420fd 2235 if (s->s3->tmp.pkey != NULL) {
e27f234a
MC
2236 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2237 ERR_R_INTERNAL_ERROR);
2238 goto err;
2239 }
0f113f3e 2240
0a699a07 2241 s->s3->tmp.pkey = ssl_generate_pkey(pkdhp);
e27f234a 2242
e2b420fd
DSH
2243 if (s->s3->tmp.pkey == NULL) {
2244 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
ffaef3f1 2245 goto err;
e27f234a 2246 }
e2b420fd
DSH
2247
2248 dh = EVP_PKEY_get0_DH(s->s3->tmp.pkey);
2249
2250 EVP_PKEY_free(pkdh);
2251 pkdh = NULL;
2252
0aeddcfa
MC
2253 DH_get0_pqg(dh, &r[0], NULL, &r[1]);
2254 DH_get0_key(dh, &r[2], NULL);
e27f234a 2255 } else
d02b48c6 2256#endif
10bf4fc2 2257#ifndef OPENSSL_NO_EC
e27f234a 2258 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
57be4444 2259 int nid;
e27f234a 2260
880d9d86 2261 if (s->s3->tmp.pkey != NULL) {
e27f234a
MC
2262 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2263 ERR_R_INTERNAL_ERROR);
2264 goto err;
2265 }
2266
57be4444 2267 /* Get NID of appropriate shared curve */
de4d764e 2268 nid = tls1_shared_group(s, -2);
57be4444
DSH
2269 curve_id = tls1_ec_nid2curve_id(nid);
2270 if (curve_id == 0) {
e27f234a
MC
2271 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2272 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
2273 goto err;
2274 }
0a699a07 2275 s->s3->tmp.pkey = ssl_generate_pkey_curve(curve_id);
880d9d86
DSH
2276 /* Generate a new key for this curve */
2277 if (s->s3->tmp.pkey == NULL) {
880d9d86 2278 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
57be4444
DSH
2279 goto f_err;
2280 }
2281
880d9d86 2282 /* Encode the public key. */
ec24630a
DSH
2283 encodedlen = EVP_PKEY_get1_tls_encodedpoint(s->s3->tmp.pkey,
2284 &encodedPoint);
e27f234a 2285 if (encodedlen == 0) {
cae41364 2286 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);
e27f234a
MC
2287 goto err;
2288 }
0f113f3e 2289
e27f234a
MC
2290 /*
2291 * We'll generate the serverKeyExchange message explicitly so we
2292 * can set these to NULLs
2293 */
2294 r[0] = NULL;
2295 r[1] = NULL;
2296 r[2] = NULL;
2297 r[3] = NULL;
2298 } else
10bf4fc2 2299#endif /* !OPENSSL_NO_EC */
edc032b5 2300#ifndef OPENSSL_NO_SRP
e27f234a
MC
2301 if (type & SSL_kSRP) {
2302 if ((s->srp_ctx.N == NULL) ||
2303 (s->srp_ctx.g == NULL) ||
2304 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
2305 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2306 SSL_R_MISSING_SRP_PARAM);
2307 goto err;
0f113f3e 2308 }
e27f234a
MC
2309 r[0] = s->srp_ctx.N;
2310 r[1] = s->srp_ctx.g;
2311 r[2] = s->srp_ctx.s;
2312 r[3] = s->srp_ctx.B;
2313 } else
2314#endif
2315 {
2316 al = SSL_AD_HANDSHAKE_FAILURE;
2317 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2318 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2319 goto f_err;
2320 }
0f113f3e 2321
f695571e
DSH
2322 if (((s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
2323 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
2324 lu = NULL;
2325 } else if (lu == NULL) {
2326 al = SSL_AD_DECODE_ERROR;
2327 goto f_err;
e27f234a 2328 }
0f113f3e 2329
85269210 2330#ifndef OPENSSL_NO_PSK
e27f234a 2331 if (type & SSL_PSK) {
c13d2a5b
MC
2332 size_t len = (s->cert->psk_identity_hint == NULL)
2333 ? 0 : strlen(s->cert->psk_identity_hint);
2334
2335 /*
2336 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2337 * checked this when we set the identity hint - but just in case
2338 */
2339 if (len > PSK_MAX_IDENTITY_LEN
7cea05dc 2340 || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
c13d2a5b
MC
2341 len)) {
2342 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2343 ERR_R_INTERNAL_ERROR);
2344 goto f_err;
85269210 2345 }
e27f234a 2346 }
85269210
DSH
2347#endif
2348
e27f234a 2349 for (i = 0; i < 4 && r[i] != NULL; i++) {
c13d2a5b
MC
2350 unsigned char *binval;
2351 int res;
2352
edc032b5 2353#ifndef OPENSSL_NO_SRP
e27f234a 2354 if ((i == 2) && (type & SSL_kSRP)) {
7cea05dc 2355 res = WPACKET_start_sub_packet_u8(pkt);
e27f234a 2356 } else
78a01b3f 2357#endif
7cea05dc 2358 res = WPACKET_start_sub_packet_u16(pkt);
c13d2a5b
MC
2359
2360 if (!res) {
2361 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2362 ERR_R_INTERNAL_ERROR);
2363 goto f_err;
2364 }
2365
78a01b3f 2366#ifndef OPENSSL_NO_DH
a230b26e 2367 /*-
78a01b3f 2368 * for interoperability with some versions of the Microsoft TLS
2369 * stack, we need to zero pad the DHE pub key to the same length
2370 * as the prime
2371 */
2372 if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
c13d2a5b 2373 size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
ff819477 2374
c13d2a5b 2375 if (len > 0) {
7cea05dc 2376 if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
c13d2a5b
MC
2377 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2378 ERR_R_INTERNAL_ERROR);
2379 goto f_err;
2380 }
2381 memset(binval, 0, len);
78a01b3f 2382 }
c13d2a5b 2383 }
edc032b5 2384#endif
7cea05dc
MC
2385 if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
2386 || !WPACKET_close(pkt)) {
c13d2a5b
MC
2387 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2388 ERR_R_INTERNAL_ERROR);
2389 goto f_err;
2390 }
2391
2392 BN_bn2bin(r[i], binval);
e27f234a 2393 }
d02b48c6 2394
10bf4fc2 2395#ifndef OPENSSL_NO_EC
e27f234a
MC
2396 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2397 /*
c13d2a5b
MC
2398 * We only support named (not generic) curves. In this situation, the
2399 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2400 * [1 byte length of encoded point], followed by the actual encoded
2401 * point itself
e27f234a 2402 */
7cea05dc
MC
2403 if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
2404 || !WPACKET_put_bytes_u8(pkt, 0)
2405 || !WPACKET_put_bytes_u8(pkt, curve_id)
2406 || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
c13d2a5b
MC
2407 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2408 ERR_R_INTERNAL_ERROR);
2409 goto f_err;
2410 }
e27f234a
MC
2411 OPENSSL_free(encodedPoint);
2412 encodedPoint = NULL;
e27f234a 2413 }
ea262260
BM
2414#endif
2415
e27f234a 2416 /* not anonymous */
f695571e 2417 if (lu != NULL) {
a497cf25 2418 EVP_PKEY *pkey = s->s3->tmp.cert->privatekey;
f695571e
DSH
2419 const EVP_MD *md = ssl_md(lu->hash_idx);
2420 unsigned char *sigbytes1, *sigbytes2;
2421 size_t siglen;
2422
2423 if (pkey == NULL || md == NULL) {
2424 /* Should never happen */
2425 al = SSL_AD_INTERNAL_ERROR;
2426 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2427 ERR_R_INTERNAL_ERROR);
2428 goto f_err;
2429 }
e27f234a
MC
2430 /*
2431 * n is the length of the params, they start at &(d[4]) and p
2432 * points to the space at the end.
2433 */
c13d2a5b 2434
f695571e
DSH
2435 /* Get length of the parameters we have written above */
2436 if (!WPACKET_get_length(pkt, &paramlen)) {
2437 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2438 ERR_R_INTERNAL_ERROR);
2439 goto f_err;
2440 }
2441 /* send signature algorithm */
2442 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg))
2443 return 0;
2444 /*
2445 * Create the signature. We don't know the actual length of the sig
2446 * until after we've created it, so we reserve enough bytes for it
2447 * up front, and then properly allocate them in the WPACKET
2448 * afterwards.
2449 */
2450 siglen = EVP_PKEY_size(pkey);
2451 if (!WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
2452 || EVP_DigestSignInit(md_ctx, &pctx, md, NULL, pkey) <= 0) {
2453 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2454 ERR_R_INTERNAL_ERROR);
2455 goto f_err;
2456 }
2457 if (lu->sig == EVP_PKEY_RSA_PSS) {
2458 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2459 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
c13d2a5b 2460 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
f695571e 2461 ERR_R_EVP_LIB);
5f3d93e4 2462 goto f_err;
0f113f3e 2463 }
f695571e
DSH
2464 }
2465 if (EVP_DigestSignUpdate(md_ctx, &(s->s3->client_random[0]),
2466 SSL3_RANDOM_SIZE) <= 0
2467 || EVP_DigestSignUpdate(md_ctx, &(s->s3->server_random[0]),
2468 SSL3_RANDOM_SIZE) <= 0
2469 || EVP_DigestSignUpdate(md_ctx,
2470 s->init_buf->data + paramoffset,
2471 paramlen) <= 0
2472 || EVP_DigestSignFinal(md_ctx, sigbytes1, &siglen) <= 0
2473 || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
2474 || sigbytes1 != sigbytes2) {
e27f234a 2475 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
f695571e 2476 ERR_R_INTERNAL_ERROR);
77d514c5
MC
2477 goto f_err;
2478 }
0f113f3e
MC
2479 }
2480
bfb0641f 2481 EVP_MD_CTX_free(md_ctx);
e27f234a 2482 return 1;
0f113f3e
MC
2483 f_err:
2484 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2485 err:
e2b420fd
DSH
2486#ifndef OPENSSL_NO_DH
2487 EVP_PKEY_free(pkdh);
2488#endif
556efe79 2489#ifndef OPENSSL_NO_EC
b548a1f1 2490 OPENSSL_free(encodedPoint);
ea262260 2491#endif
bfb0641f 2492 EVP_MD_CTX_free(md_ctx);
e27f234a 2493 return 0;
0f113f3e 2494}
d02b48c6 2495
7cea05dc 2496int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
0f113f3e 2497{
32f66107
DSH
2498 int al = SSL_AD_INTERNAL_ERROR;
2499
03f44b97
DSH
2500 if (SSL_IS_TLS13(s)) {
2501 /* TODO(TLS1.3) for now send empty request context */
2502 if (!WPACKET_put_bytes_u8(pkt, 0)) {
2503 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2504 ERR_R_INTERNAL_ERROR);
2505 goto err;
2506 }
32f66107 2507
fe874d27
MC
2508 if (!tls_construct_extensions(s, pkt,
2509 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
2510 0, &al)) {
03f44b97
DSH
2511 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2512 ERR_R_INTERNAL_ERROR);
2513 goto err;
2514 }
32f66107
DSH
2515 goto done;
2516 }
2517
2518 /* get the list of acceptable cert types */
2519 if (!WPACKET_start_sub_packet_u8(pkt)
2520 || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
2521 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2522 goto err;
28ff8ef3 2523 }
0f113f3e 2524
e27f234a 2525 if (SSL_USE_SIGALGS(s)) {
98c792d1 2526 const uint16_t *psigs;
a9669ddc 2527 size_t nl = tls12_get_psigalgs(s, 1, &psigs);
703bcee0 2528
7cea05dc 2529 if (!WPACKET_start_sub_packet_u16(pkt)
8f12296e 2530 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
7cea05dc
MC
2531 || !tls12_copy_sigalgs(s, pkt, psigs, nl)
2532 || !WPACKET_close(pkt)) {
28ff8ef3
MC
2533 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2534 ERR_R_INTERNAL_ERROR);
2535 goto err;
2536 }
e27f234a 2537 }
0f113f3e 2538
5d6cca05 2539 if (!construct_ca_names(s, pkt)) {
28ff8ef3
MC
2540 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2541 goto err;
2542 }
e27f234a 2543
32f66107 2544 done:
e27f234a 2545 s->s3->tmp.cert_request = 1;
e27f234a 2546 return 1;
0f113f3e 2547 err:
32f66107 2548 ssl3_send_alert(s, SSL3_AL_FATAL, al);
e27f234a 2549 return 0;
0f113f3e 2550}
d02b48c6 2551
0907d710 2552static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt, int *al)
e27f234a 2553{
85269210 2554#ifndef OPENSSL_NO_PSK
0907d710
MC
2555 unsigned char psk[PSK_MAX_PSK_LEN];
2556 size_t psklen;
2557 PACKET psk_identity;
efcdbcbe 2558
0907d710
MC
2559 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2560 *al = SSL_AD_DECODE_ERROR;
c76a4aea 2561 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH);
0907d710
MC
2562 return 0;
2563 }
2564 if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2565 *al = SSL_AD_DECODE_ERROR;
c76a4aea 2566 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG);
0907d710
MC
2567 return 0;
2568 }
2569 if (s->psk_server_callback == NULL) {
2570 *al = SSL_AD_INTERNAL_ERROR;
a230b26e 2571 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_SERVER_CB);
0907d710
MC
2572 return 0;
2573 }
85269210 2574
0907d710
MC
2575 if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2576 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2577 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
0907d710
MC
2578 return 0;
2579 }
85269210 2580
0907d710 2581 psklen = s->psk_server_callback(s, s->session->psk_identity,
a230b26e 2582 psk, sizeof(psk));
85269210 2583
0907d710
MC
2584 if (psklen > PSK_MAX_PSK_LEN) {
2585 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2586 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
0907d710
MC
2587 return 0;
2588 } else if (psklen == 0) {
2589 /*
2590 * PSK related to the given identity not found
2591 */
2592 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
c76a4aea 2593 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
0907d710
MC
2594 SSL_R_PSK_IDENTITY_NOT_FOUND);
2595 return 0;
2596 }
85269210 2597
0907d710
MC
2598 OPENSSL_free(s->s3->tmp.psk);
2599 s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
2600 OPENSSL_cleanse(psk, psklen);
85269210 2601
0907d710
MC
2602 if (s->s3->tmp.psk == NULL) {
2603 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2604 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
0907d710 2605 return 0;
85269210 2606 }
0907d710
MC
2607
2608 s->s3->tmp.psklen = psklen;
2609
2610 return 1;
2611#else
2612 /* Should never happen */
2613 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2614 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
0907d710 2615 return 0;
85269210 2616#endif
0907d710
MC
2617}
2618
0907d710
MC
2619static int tls_process_cke_rsa(SSL *s, PACKET *pkt, int *al)
2620{
bc36ee62 2621#ifndef OPENSSL_NO_RSA
0907d710
MC
2622 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2623 int decrypt_len;
2624 unsigned char decrypt_good, version_good;
2625 size_t j, padding_len;
2626 PACKET enc_premaster;
2627 RSA *rsa = NULL;
2628 unsigned char *rsa_decrypt = NULL;
2629 int ret = 0;
2630
d0ff28f8 2631 rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA].privatekey);
0907d710
MC
2632 if (rsa == NULL) {
2633 *al = SSL_AD_HANDSHAKE_FAILURE;
c76a4aea 2634 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_MISSING_RSA_CERTIFICATE);
0907d710
MC
2635 return 0;
2636 }
2637
2638 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2639 if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2640 enc_premaster = *pkt;
2641 } else {
2642 if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2643 || PACKET_remaining(pkt) != 0) {
2644 *al = SSL_AD_DECODE_ERROR;
c76a4aea 2645 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_LENGTH_MISMATCH);
0907d710 2646 return 0;
0f113f3e 2647 }
0907d710 2648 }
0f113f3e 2649
0907d710
MC
2650 /*
2651 * We want to be sure that the plaintext buffer size makes it safe to
2652 * iterate over the entire size of a premaster secret
2653 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2654 * their ciphertext cannot accommodate a premaster secret anyway.
2655 */
2656 if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
2657 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2658 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
0907d710
MC
2659 return 0;
2660 }
0f113f3e 2661
0907d710
MC
2662 rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
2663 if (rsa_decrypt == NULL) {
2664 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2665 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_MALLOC_FAILURE);
0907d710
MC
2666 return 0;
2667 }
0f113f3e 2668
0907d710
MC
2669 /*
2670 * We must not leak whether a decryption failure occurs because of
2671 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2672 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2673 * generates a random premaster secret for the case that the decrypt
2674 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2675 */
20ca916d 2676
a230b26e 2677 if (RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret)) <= 0)
0907d710 2678 goto err;
0f113f3e 2679
0907d710
MC
2680 /*
2681 * Decrypt with no padding. PKCS#1 padding will be removed as part of
2682 * the timing-sensitive code below.
2683 */
348240c6
MC
2684 /* TODO(size_t): Convert this function */
2685 decrypt_len = (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster),
2686 PACKET_data(&enc_premaster),
2687 rsa_decrypt, rsa, RSA_NO_PADDING);
0907d710
MC
2688 if (decrypt_len < 0)
2689 goto err;
20ca916d 2690
0907d710 2691 /* Check the padding. See RFC 3447, section 7.2.2. */
5b8fa431 2692
0907d710
MC
2693 /*
2694 * The smallest padded premaster is 11 bytes of overhead. Small keys
2695 * are publicly invalid, so this may return immediately. This ensures
2696 * PS is at least 8 bytes.
2697 */
2698 if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {
2699 *al = SSL_AD_DECRYPT_ERROR;
c76a4aea 2700 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_DECRYPTION_FAILED);
0907d710
MC
2701 goto err;
2702 }
0f113f3e 2703
0907d710
MC
2704 padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;
2705 decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) &
a230b26e 2706 constant_time_eq_int_8(rsa_decrypt[1], 2);
0907d710
MC
2707 for (j = 2; j < padding_len - 1; j++) {
2708 decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]);
2709 }
2710 decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]);
5b8fa431 2711
0907d710
MC
2712 /*
2713 * If the version in the decrypted pre-master secret is correct then
2714 * version_good will be 0xff, otherwise it'll be zero. The
2715 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2716 * (http://eprint.iacr.org/2003/052/) exploits the version number
2717 * check as a "bad version oracle". Thus version checks are done in
2718 * constant time and are treated like any other decryption error.
2719 */
2720 version_good =
2721 constant_time_eq_8(rsa_decrypt[padding_len],
2722 (unsigned)(s->client_version >> 8));
2723 version_good &=
2724 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2725 (unsigned)(s->client_version & 0xff));
0f113f3e 2726
0907d710
MC
2727 /*
2728 * The premaster secret must contain the same version number as the
2729 * ClientHello to detect version rollback attacks (strangely, the
2730 * protocol does not offer such protection for DH ciphersuites).
2731 * However, buggy clients exist that send the negotiated protocol
2732 * version instead if the server does not support the requested
2733 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2734 * clients.
2735 */
2736 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2737 unsigned char workaround_good;
2738 workaround_good = constant_time_eq_8(rsa_decrypt[padding_len],
2739 (unsigned)(s->version >> 8));
2740 workaround_good &=
5b8fa431 2741 constant_time_eq_8(rsa_decrypt[padding_len + 1],
0907d710
MC
2742 (unsigned)(s->version & 0xff));
2743 version_good |= workaround_good;
2744 }
0f113f3e 2745
0907d710
MC
2746 /*
2747 * Both decryption and version must be good for decrypt_good to
2748 * remain non-zero (0xff).
2749 */
2750 decrypt_good &= version_good;
0f113f3e 2751
0907d710
MC
2752 /*
2753 * Now copy rand_premaster_secret over from p using
2754 * decrypt_good_mask. If decryption failed, then p does not
2755 * contain valid plaintext, however, a check above guarantees
2756 * it is still sufficiently large to read from.
2757 */
2758 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2759 rsa_decrypt[padding_len + j] =
2760 constant_time_select_8(decrypt_good,
2761 rsa_decrypt[padding_len + j],
2762 rand_premaster_secret[j]);
2763 }
0f113f3e 2764
0907d710
MC
2765 if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len,
2766 sizeof(rand_premaster_secret), 0)) {
2767 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2768 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);
0907d710
MC
2769 goto err;
2770 }
0f113f3e 2771
0907d710
MC
2772 ret = 1;
2773 err:
2774 OPENSSL_free(rsa_decrypt);
2775 return ret;
2776#else
2777 /* Should never happen */
2778 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2779 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);
0907d710
MC
2780 return 0;
2781#endif
2782}
2783
642360f9
MC
2784static int tls_process_cke_dhe(SSL *s, PACKET *pkt, int *al)
2785{
2786#ifndef OPENSSL_NO_DH
2787 EVP_PKEY *skey = NULL;
2788 DH *cdh;
2789 unsigned int i;
2790 BIGNUM *pub_key;
2791 const unsigned char *data;
2792 EVP_PKEY *ckey = NULL;
2793 int ret = 0;
2794
31a7d80d 2795 if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
642360f9 2796 *al = SSL_AD_HANDSHAKE_FAILURE;
c76a4aea 2797 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE,
642360f9
MC
2798 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2799 goto err;
2800 }
642360f9
MC
2801 skey = s->s3->tmp.pkey;
2802 if (skey == NULL) {
2803 *al = SSL_AD_HANDSHAKE_FAILURE;
c76a4aea 2804 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_MISSING_TMP_DH_KEY);
642360f9
MC
2805 goto err;
2806 }
2807
2808 if (PACKET_remaining(pkt) == 0L) {
2809 *al = SSL_AD_HANDSHAKE_FAILURE;
c76a4aea 2810 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_MISSING_TMP_DH_KEY);
642360f9
MC
2811 goto err;
2812 }
2813 if (!PACKET_get_bytes(pkt, &data, i)) {
2814 /* We already checked we have enough data */
2815 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2816 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
642360f9
MC
2817 goto err;
2818 }
2819 ckey = EVP_PKEY_new();
2820 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
c76a4aea 2821 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_BN_LIB);
642360f9
MC
2822 goto err;
2823 }
2824 cdh = EVP_PKEY_get0_DH(ckey);
2825 pub_key = BN_bin2bn(data, i, NULL);
2826
2827 if (pub_key == NULL || !DH_set0_key(cdh, pub_key, NULL)) {
c76a4aea 2828 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
642360f9
MC
2829 if (pub_key != NULL)
2830 BN_free(pub_key);
2831 goto err;
2832 }
2833
0f1e51ea 2834 if (ssl_derive(s, skey, ckey, 1) == 0) {
642360f9 2835 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2836 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
642360f9
MC
2837 goto err;
2838 }
2839
2840 ret = 1;
2841 EVP_PKEY_free(s->s3->tmp.pkey);
2842 s->s3->tmp.pkey = NULL;
2843 err:
2844 EVP_PKEY_free(ckey);
2845 return ret;
2846#else
2847 /* Should never happen */
2848 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2849 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
642360f9
MC
2850 return 0;
2851#endif
2852}
2853
19ed1ec1
MC
2854static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt, int *al)
2855{
2856#ifndef OPENSSL_NO_EC
2857 EVP_PKEY *skey = s->s3->tmp.pkey;
2858 EVP_PKEY *ckey = NULL;
2859 int ret = 0;
2860
2861 if (PACKET_remaining(pkt) == 0L) {
2862 /* We don't support ECDH client auth */
2863 *al = SSL_AD_HANDSHAKE_FAILURE;
c76a4aea 2864 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, SSL_R_MISSING_TMP_ECDH_KEY);
19ed1ec1
MC
2865 goto err;
2866 } else {
2867 unsigned int i;
2868 const unsigned char *data;
2869
2870 /*
2871 * Get client's public key from encoded point in the
2872 * ClientKeyExchange message.
2873 */
2874
2875 /* Get encoded point length */
fb933982
DSH
2876 if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
2877 || PACKET_remaining(pkt) != 0) {
19ed1ec1 2878 *al = SSL_AD_DECODE_ERROR;
c76a4aea 2879 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, SSL_R_LENGTH_MISMATCH);
19ed1ec1
MC
2880 goto err;
2881 }
19ed1ec1
MC
2882 ckey = EVP_PKEY_new();
2883 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
c76a4aea 2884 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_EVP_LIB);
19ed1ec1
MC
2885 goto err;
2886 }
ec24630a 2887 if (EVP_PKEY_set1_tls_encodedpoint(ckey, data, i) == 0) {
fb933982 2888 *al = SSL_AD_HANDSHAKE_FAILURE;
c76a4aea 2889 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_EC_LIB);
19ed1ec1
MC
2890 goto err;
2891 }
2892 }
2893
0f1e51ea 2894 if (ssl_derive(s, skey, ckey, 1) == 0) {
19ed1ec1 2895 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2896 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
19ed1ec1
MC
2897 goto err;
2898 }
2899
2900 ret = 1;
2901 EVP_PKEY_free(s->s3->tmp.pkey);
2902 s->s3->tmp.pkey = NULL;
2903 err:
2904 EVP_PKEY_free(ckey);
2905
2906 return ret;
2907#else
2908 /* Should never happen */
2909 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2910 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
19ed1ec1
MC
2911 return 0;
2912#endif
2913}
2914
c437eef6
MC
2915static int tls_process_cke_srp(SSL *s, PACKET *pkt, int *al)
2916{
2917#ifndef OPENSSL_NO_SRP
2918 unsigned int i;
2919 const unsigned char *data;
2920
2921 if (!PACKET_get_net_2(pkt, &i)
a230b26e 2922 || !PACKET_get_bytes(pkt, &data, i)) {
c437eef6 2923 *al = SSL_AD_DECODE_ERROR;
c76a4aea 2924 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, SSL_R_BAD_SRP_A_LENGTH);
c437eef6
MC
2925 return 0;
2926 }
2927 if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
c76a4aea 2928 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_BN_LIB);
c437eef6
MC
2929 return 0;
2930 }
a230b26e 2931 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
c437eef6 2932 *al = SSL_AD_ILLEGAL_PARAMETER;
c76a4aea 2933 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, SSL_R_BAD_SRP_PARAMETERS);
c437eef6
MC
2934 return 0;
2935 }
2936 OPENSSL_free(s->session->srp_username);
2937 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2938 if (s->session->srp_username == NULL) {
c76a4aea 2939 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_MALLOC_FAILURE);
c437eef6
MC
2940 return 0;
2941 }
2942
2943 if (!srp_generate_server_master_secret(s)) {
c76a4aea 2944 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_INTERNAL_ERROR);
c437eef6
MC
2945 return 0;
2946 }
2947
2948 return 1;
2949#else
2950 /* Should never happen */
2951 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2952 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_INTERNAL_ERROR);
c437eef6
MC
2953 return 0;
2954#endif
2955}
2956
2957static int tls_process_cke_gost(SSL *s, PACKET *pkt, int *al)
2958{
2959#ifndef OPENSSL_NO_GOST
2960 EVP_PKEY_CTX *pkey_ctx;
2961 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2962 unsigned char premaster_secret[32];
2963 const unsigned char *start;
2964 size_t outlen = 32, inlen;
2965 unsigned long alg_a;
2966 int Ttag, Tclass;
2967 long Tlen;
348240c6 2968 size_t sess_key_len;
c437eef6
MC
2969 const unsigned char *data;
2970 int ret = 0;
2971
2972 /* Get our certificate private key */
2973 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2974 if (alg_a & SSL_aGOST12) {
2975 /*
2976 * New GOST ciphersuites have SSL_aGOST01 bit too
2977 */
2978 pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
2979 if (pk == NULL) {
2980 pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
2981 }
2982 if (pk == NULL) {
2983 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2984 }
2985 } else if (alg_a & SSL_aGOST01) {
2986 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2987 }
2988
2989 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2990 if (pkey_ctx == NULL) {
2991 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2992 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_MALLOC_FAILURE);
c437eef6
MC
2993 return 0;
2994 }
2995 if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
2996 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 2997 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
c437eef6
MC
2998 return 0;
2999 }
3000 /*
3001 * If client certificate is present and is of the same type, maybe
3002 * use it for key exchange. Don't mind errors from
3003 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3004 * client certificate for authorization only.
3005 */
3006 client_pub_pkey = X509_get0_pubkey(s->session->peer);
3007 if (client_pub_pkey) {
3008 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
3009 ERR_clear_error();
3010 }
3011 /* Decrypt session key */
3012 sess_key_len = PACKET_remaining(pkt);
3013 if (!PACKET_get_bytes(pkt, &data, sess_key_len)) {
3014 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 3015 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
c437eef6
MC
3016 goto err;
3017 }
348240c6 3018 /* TODO(size_t): Convert this function */
a230b26e 3019 if (ASN1_get_object((const unsigned char **)&data, &Tlen, &Ttag,
348240c6 3020 &Tclass, (long)sess_key_len) != V_ASN1_CONSTRUCTED
a230b26e 3021 || Ttag != V_ASN1_SEQUENCE || Tclass != V_ASN1_UNIVERSAL) {
c437eef6 3022 *al = SSL_AD_DECODE_ERROR;
c76a4aea 3023 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, SSL_R_DECRYPTION_FAILED);
c437eef6
MC
3024 goto err;
3025 }
3026 start = data;
3027 inlen = Tlen;
3028 if (EVP_PKEY_decrypt
3029 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
3030 *al = SSL_AD_DECODE_ERROR;
c76a4aea 3031 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, SSL_R_DECRYPTION_FAILED);
c437eef6
MC
3032 goto err;
3033 }
3034 /* Generate master secret */
3035 if (!ssl_generate_master_secret(s, premaster_secret,
3036 sizeof(premaster_secret), 0)) {
3037 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 3038 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
c437eef6
MC
3039 goto err;
3040 }
3041 /* Check if pubkey from client certificate was used */
3042 if (EVP_PKEY_CTX_ctrl
3043 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
3044 s->statem.no_cert_verify = 1;
3045
3046 ret = 1;
3047 err:
3048 EVP_PKEY_CTX_free(pkey_ctx);
3049 return ret;
3050#else
3051 /* Should never happen */
3052 *al = SSL_AD_INTERNAL_ERROR;
c76a4aea 3053 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
c437eef6
MC
3054 return 0;
3055#endif
3056}
3057
0907d710
MC
3058MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
3059{
3060 int al = -1;
3061 unsigned long alg_k;
3062
3063 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3064
3065 /* For PSK parse and retrieve identity, obtain PSK key */
3066 if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt, &al))
3067 goto err;
3068
3069 if (alg_k & SSL_kPSK) {
3070 /* Identity extracted earlier: should be nothing left */
3071 if (PACKET_remaining(pkt) != 0) {
3072 al = SSL_AD_HANDSHAKE_FAILURE;
a230b26e
EK
3073 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
3074 SSL_R_LENGTH_MISMATCH);
9059eb71 3075 goto err;
0907d710
MC
3076 }
3077 /* PSK handled by ssl_generate_master_secret */
3078 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
69f68237 3079 al = SSL_AD_INTERNAL_ERROR;
e27f234a 3080 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
9059eb71 3081 goto err;
69f68237 3082 }
0907d710
MC
3083 } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3084 if (!tls_process_cke_rsa(s, pkt, &al))
3085 goto err;
642360f9
MC
3086 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
3087 if (!tls_process_cke_dhe(s, pkt, &al))
0f113f3e 3088 goto err;
19ed1ec1
MC
3089 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
3090 if (!tls_process_cke_ecdhe(s, pkt, &al))
3091 goto err;
c437eef6
MC
3092 } else if (alg_k & SSL_kSRP) {
3093 if (!tls_process_cke_srp(s, pkt, &al))
0f113f3e 3094 goto err;
c437eef6
MC
3095 } else if (alg_k & SSL_kGOST) {
3096 if (!tls_process_cke_gost(s, pkt, &al))
0f113f3e 3097 goto err;
c437eef6 3098 } else {
0f113f3e 3099 al = SSL_AD_HANDSHAKE_FAILURE;
a230b26e
EK
3100 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
3101 SSL_R_UNKNOWN_CIPHER_TYPE);
9059eb71 3102 goto err;
0f113f3e
MC
3103 }
3104
e27f234a 3105 return MSG_PROCESS_CONTINUE_PROCESSING;
0f113f3e 3106 err:
0907d710
MC
3107 if (al != -1)
3108 ssl3_send_alert(s, SSL3_AL_FATAL, al);
85269210
DSH
3109#ifndef OPENSSL_NO_PSK
3110 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
3111 s->s3->tmp.psk = NULL;
58964a49 3112#endif
fe3a3291 3113 ossl_statem_set_error(s);
e27f234a 3114 return MSG_PROCESS_ERROR;
0f113f3e 3115}
d02b48c6 3116
be3583fa 3117WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
94836de2 3118{
94836de2 3119#ifndef OPENSSL_NO_SCTP
c130dd8e
MC
3120 if (wst == WORK_MORE_A) {
3121 if (SSL_IS_DTLS(s)) {
3122 unsigned char sctpauthkey[64];
3123 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3124 /*
3125 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3126 * used.
3127 */
141eb8c6
MC
3128 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3129 sizeof(DTLS1_SCTP_AUTH_LABEL));
c130dd8e
MC
3130
3131 if (SSL_export_keying_material(s, sctpauthkey,
a230b26e
EK
3132 sizeof(sctpauthkey), labelbuffer,
3133 sizeof(labelbuffer), NULL, 0,
3134 0) <= 0) {
fe3a3291 3135 ossl_statem_set_error(s);
0fe2a0af 3136 return WORK_ERROR;
c130dd8e 3137 }
94836de2 3138
c130dd8e
MC
3139 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3140 sizeof(sctpauthkey), sctpauthkey);
94836de2 3141 }
94836de2
MC
3142 }
3143#endif
3144
149c2ef5 3145 if (s->statem.no_cert_verify || !s->session->peer) {
a230b26e
EK
3146 /*
3147 * No certificate verify or no peer certificate so we no longer need
3148 * the handshake_buffer
149c2ef5
MC
3149 */
3150 if (!ssl3_digest_cached_records(s, 0)) {
3151 ossl_statem_set_error(s);
3152 return WORK_ERROR;
3153 }
94836de2 3154 return WORK_FINISHED_CONTINUE;
28f4580c 3155 } else {
94836de2
MC
3156 if (!s->s3->handshake_buffer) {
3157 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
3158 ERR_R_INTERNAL_ERROR);
fe3a3291 3159 ossl_statem_set_error(s);
94836de2
MC
3160 return WORK_ERROR;
3161 }
3162 /*
3163 * For sigalgs freeze the handshake buffer. If we support
3164 * extms we've done this already so this is a no-op
3165 */
3166 if (!ssl3_digest_cached_records(s, 1)) {
fe3a3291 3167 ossl_statem_set_error(s);
94836de2
MC
3168 return WORK_ERROR;
3169 }
94836de2
MC
3170 }
3171
3172 return WORK_FINISHED_CONTINUE;
3173}
3174
be3583fa 3175MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
e27f234a 3176{
20dbe585 3177 int i, al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR;
e27f234a
MC
3178 X509 *x = NULL;
3179 unsigned long l, llen;
b6981744 3180 const unsigned char *certstart, *certbytes;
e27f234a 3181 STACK_OF(X509) *sk = NULL;
e96e0f8e 3182 PACKET spkt, context;
d805a57b 3183 size_t chainidx;
0f113f3e
MC
3184
3185 if ((sk = sk_X509_new_null()) == NULL) {
e27f234a
MC
3186 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3187 goto f_err;
0f113f3e
MC
3188 }
3189
e96e0f8e
MC
3190 /* TODO(TLS1.3): For now we ignore the context. We need to verify this */
3191 if ((SSL_IS_TLS13(s) && !PACKET_get_length_prefixed_1(pkt, &context))
3192 || !PACKET_get_net_3(pkt, &llen)
3193 || !PACKET_get_sub_packet(pkt, &spkt, llen)
3194 || PACKET_remaining(pkt) != 0) {
0f113f3e 3195 al = SSL_AD_DECODE_ERROR;
e27f234a 3196 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
3197 goto f_err;
3198 }
0bc09ecd 3199
d805a57b 3200 for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
0bc09ecd 3201 if (!PACKET_get_net_3(&spkt, &l)
a230b26e 3202 || !PACKET_get_bytes(&spkt, &certbytes, l)) {
0f113f3e 3203 al = SSL_AD_DECODE_ERROR;
e27f234a 3204 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
0f113f3e
MC
3205 SSL_R_CERT_LENGTH_MISMATCH);
3206 goto f_err;
3207 }
3208
0bc09ecd
MC
3209 certstart = certbytes;
3210 x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
0f113f3e 3211 if (x == NULL) {
e27f234a
MC
3212 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3213 goto f_err;
0f113f3e 3214 }
0bc09ecd 3215 if (certbytes != (certstart + l)) {
0f113f3e 3216 al = SSL_AD_DECODE_ERROR;
e27f234a 3217 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
0f113f3e
MC
3218 SSL_R_CERT_LENGTH_MISMATCH);
3219 goto f_err;
3220 }
e96e0f8e
MC
3221
3222 if (SSL_IS_TLS13(s)) {
3223 RAW_EXTENSION *rawexts = NULL;
3224 PACKET extensions;
3225
3226 if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
3227 al = SSL_AD_DECODE_ERROR;
3228 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_BAD_LENGTH);
3229 goto f_err;
3230 }
fe874d27
MC
3231 if (!tls_collect_extensions(s, &extensions,
3232 SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
735d5b59 3233 &al, NULL, chainidx == 0)
8e1634ec
TT
3234 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
3235 rawexts, x, chainidx, &al,
3236 PACKET_remaining(&spkt) == 0)) {
5ee289ea 3237 OPENSSL_free(rawexts);
e96e0f8e 3238 goto f_err;
5ee289ea
MC
3239 }
3240 OPENSSL_free(rawexts);
e96e0f8e
MC
3241 }
3242
0f113f3e 3243 if (!sk_X509_push(sk, x)) {
e27f234a
MC
3244 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3245 goto f_err;
0f113f3e
MC
3246 }
3247 x = NULL;
0f113f3e
MC
3248 }
3249
3250 if (sk_X509_num(sk) <= 0) {
3251 /* TLS does not mind 0 certs returned */
3252 if (s->version == SSL3_VERSION) {
3253 al = SSL_AD_HANDSHAKE_FAILURE;
e27f234a 3254 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
0f113f3e
MC
3255 SSL_R_NO_CERTIFICATES_RETURNED);
3256 goto f_err;
3257 }
3258 /* Fail for TLS only if we required a certificate */
3259 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3260 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
e27f234a 3261 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
0f113f3e 3262 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
42c28b63 3263 al = SSL_AD_CERTIFICATE_REQUIRED;
0f113f3e
MC
3264 goto f_err;
3265 }
3266 /* No client certificate so digest cached records */
124037fd 3267 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
0f113f3e
MC
3268 goto f_err;
3269 }
3270 } else {
3271 EVP_PKEY *pkey;
3272 i = ssl_verify_cert_chain(s, sk);
3273 if (i <= 0) {
3274 al = ssl_verify_alarm_type(s->verify_result);
e27f234a 3275 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
0f113f3e
MC
3276 SSL_R_CERTIFICATE_VERIFY_FAILED);
3277 goto f_err;
3278 }
3279 if (i > 1) {
e27f234a 3280 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
0f113f3e
MC
3281 al = SSL_AD_HANDSHAKE_FAILURE;
3282 goto f_err;
3283 }
8382fd3a 3284 pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
0f113f3e
MC
3285 if (pkey == NULL) {
3286 al = SSL3_AD_HANDSHAKE_FAILURE;
e27f234a 3287 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
0f113f3e
MC
3288 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3289 goto f_err;
3290 }
0f113f3e
MC
3291 }
3292
222561fe 3293 X509_free(s->session->peer);
0f113f3e
MC
3294 s->session->peer = sk_X509_shift(sk);
3295 s->session->verify_result = s->verify_result;
3296
c34b0f99
DSH
3297 sk_X509_pop_free(s->session->peer_chain, X509_free);
3298 s->session->peer_chain = sk;
0f1e51ea
MC
3299
3300 /*
3301 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3302 * message
3303 */
94ed2c67 3304 if (SSL_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
0f1e51ea
MC
3305 al = SSL_AD_INTERNAL_ERROR;
3306 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3307 goto f_err;
3308 }
3309
0f113f3e
MC
3310 /*
3311 * Inconsistency alert: cert_chain does *not* include the peer's own
d4d78943 3312 * certificate, while we do include it in statem_clnt.c
0f113f3e 3313 */
0f113f3e 3314 sk = NULL;
2c5dfdc3
MC
3315
3316 /* Save the current hash state for when we receive the CertificateVerify */
3317 if (SSL_IS_TLS13(s)
3318 && !ssl_handshake_hash(s, s->cert_verify_hash,
3319 sizeof(s->cert_verify_hash),
3320 &s->cert_verify_hash_len)) {
3321 al = SSL_AD_INTERNAL_ERROR;
3322 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3323 goto f_err;
3324 }
3325
e27f234a 3326 ret = MSG_PROCESS_CONTINUE_READING;
66696478
RS
3327 goto done;
3328
0f113f3e 3329 f_err:
66696478 3330 ssl3_send_alert(s, SSL3_AL_FATAL, al);
fe3a3291 3331 ossl_statem_set_error(s);
66696478 3332 done:
222561fe
RS
3333 X509_free(x);
3334 sk_X509_pop_free(sk, X509_free);
e27f234a 3335 return ret;
0f113f3e 3336}
d02b48c6 3337
7cea05dc 3338int tls_construct_server_certificate(SSL *s, WPACKET *pkt)
e27f234a 3339{
a497cf25 3340 CERT_PKEY *cpk = s->s3->tmp.cert;
e96e0f8e 3341 int al = SSL_AD_INTERNAL_ERROR;
e27f234a 3342
a497cf25 3343 if (cpk == NULL) {
e27f234a 3344 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
e27f234a
MC
3345 return 0;
3346 }
3347
e96e0f8e
MC
3348 /*
3349 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3350 * for the server Certificate message
3351 */
3352 if ((SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0))
3353 || !ssl3_output_cert_chain(s, pkt, cpk, &al)) {
e27f234a 3354 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
e96e0f8e 3355 ssl3_send_alert(s, SSL3_AL_FATAL, al);
e27f234a
MC
3356 return 0;
3357 }
3358
3359 return 1;
3360}
3361
7cea05dc 3362int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
e27f234a
MC
3363{
3364 unsigned char *senc = NULL;
83ae4661 3365 EVP_CIPHER_CTX *ctx = NULL;
bf7c6817 3366 HMAC_CTX *hctx = NULL;
a00d75e1 3367 unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
e27f234a 3368 const unsigned char *const_p;
a00d75e1 3369 int len, slen_full, slen, lenfinal;
e27f234a
MC
3370 SSL_SESSION *sess;
3371 unsigned int hlen;
222da979 3372 SSL_CTX *tctx = s->session_ctx;
e27f234a 3373 unsigned char iv[EVP_MAX_IV_LENGTH];
d139723b 3374 unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
30f05b19 3375 int iv_len, al = SSL_AD_INTERNAL_ERROR;
a00d75e1 3376 size_t macoffset, macendoffset;
30f05b19
MC
3377 union {
3378 unsigned char age_add_c[sizeof(uint32_t)];
3379 uint32_t age_add;
3380 } age_add_u;
e27f234a 3381
fc24f0bf
MC
3382 if (SSL_IS_TLS13(s)) {
3383 if (RAND_bytes(age_add_u.age_add_c, sizeof(age_add_u)) <= 0)
3384 goto err;
3385 s->session->ext.tick_age_add = age_add_u.age_add;
2c604cb9 3386 s->session->time = (long)time(NULL);
f6370040
MC
3387 if (s->s3->alpn_selected != NULL) {
3388 OPENSSL_free(s->session->ext.alpn_selected);
3389 s->session->ext.alpn_selected =
3390 OPENSSL_memdup(s->s3->alpn_selected, s->s3->alpn_selected_len);
3391 if (s->session->ext.alpn_selected == NULL) {
3392 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3393 ERR_R_MALLOC_FAILURE);
3394 goto err;
3395 }
3396 s->session->ext.alpn_selected_len = s->s3->alpn_selected_len;
3397 }
3398 s->session->ext.max_early_data = s->max_early_data;
fc24f0bf
MC
3399 }
3400
e27f234a
MC
3401 /* get session encoding length */
3402 slen_full = i2d_SSL_SESSION(s->session, NULL);
3403 /*
3404 * Some length values are 16 bits, so forget it if session is too
3405 * long
3406 */
3407 if (slen_full == 0 || slen_full > 0xFF00) {
f6370040
MC
3408 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
3409 goto err;
e27f234a
MC
3410 }
3411 senc = OPENSSL_malloc(slen_full);
a71edf3b 3412 if (senc == NULL) {
f6370040
MC
3413 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
3414 goto err;
e27f234a 3415 }
0f113f3e 3416
846ec07d 3417 ctx = EVP_CIPHER_CTX_new();
bf7c6817 3418 hctx = HMAC_CTX_new();
83ae4661
MC
3419 if (ctx == NULL || hctx == NULL) {
3420 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
3421 goto err;
3422 }
0f113f3e 3423
e27f234a
MC
3424 p = senc;
3425 if (!i2d_SSL_SESSION(s->session, &p))
3426 goto err;
687eaf27 3427
e27f234a
MC
3428 /*
3429 * create a fresh copy (not shared with other threads) to clean up
3430 */
3431 const_p = senc;
3432 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3433 if (sess == NULL)
3434 goto err;
3435 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
0f113f3e 3436
e27f234a
MC
3437 slen = i2d_SSL_SESSION(sess, NULL);
3438 if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3439 SSL_SESSION_free(sess);
3440 goto err;
3441 }
3442 p = senc;
3443 if (!i2d_SSL_SESSION(sess, &p)) {
3444 SSL_SESSION_free(sess);
3445 goto err;
3446 }
3447 SSL_SESSION_free(sess);
0f113f3e 3448
e27f234a
MC
3449 /*
3450 * Initialize HMAC and cipher contexts. If callback present it does
3451 * all the work otherwise use generated values from parent ctx.
3452 */
aff8c126 3453 if (tctx->ext.ticket_key_cb) {
5c753de6 3454 /* if 0 is returned, write an empty ticket */
aff8c126 3455 int ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
5c753de6
TS
3456 hctx, 1);
3457
3458 if (ret == 0) {
a00d75e1
MC
3459
3460 /* Put timeout and length */
7cea05dc 3461 if (!WPACKET_put_bytes_u32(pkt, 0)
4a01c59f 3462 || !WPACKET_put_bytes_u16(pkt, 0)) {
a00d75e1
MC
3463 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3464 ERR_R_INTERNAL_ERROR);
5c753de6 3465 goto err;
a00d75e1 3466 }
5c753de6
TS
3467 OPENSSL_free(senc);
3468 EVP_CIPHER_CTX_free(ctx);
3469 HMAC_CTX_free(hctx);
3470 return 1;
3471 }
3472 if (ret < 0)
e27f234a 3473 goto err;
d139723b 3474 iv_len = EVP_CIPHER_CTX_iv_length(ctx);
e27f234a 3475 } else {
d139723b
KR
3476 const EVP_CIPHER *cipher = EVP_aes_256_cbc();
3477
3478 iv_len = EVP_CIPHER_iv_length(cipher);
3479 if (RAND_bytes(iv, iv_len) <= 0)
687eaf27 3480 goto err;
d139723b 3481 if (!EVP_EncryptInit_ex(ctx, cipher, NULL,
aff8c126 3482 tctx->ext.tick_aes_key, iv))
687eaf27 3483 goto err;
aff8c126
RS
3484 if (!HMAC_Init_ex(hctx, tctx->ext.tick_hmac_key,
3485 sizeof(tctx->ext.tick_hmac_key),
e27f234a 3486 EVP_sha256(), NULL))
4f9fab6b 3487 goto err;
aff8c126
RS
3488 memcpy(key_name, tctx->ext.tick_key_name,
3489 sizeof(tctx->ext.tick_key_name));
0f113f3e
MC
3490 }
3491
e27f234a 3492 /*
2c604cb9
MC
3493 * Ticket lifetime hint: For TLSv1.2 this is advisory only and we leave this
3494 * unspecified for resumed session (for simplicity).
3495 * In TLSv1.3 we reset the "time" field above, and always specify the
3496 * timeout.
e27f234a 3497 */
2c604cb9
MC
3498 if (!WPACKET_put_bytes_u32(pkt,
3499 (s->hit && !SSL_IS_TLS13(s))
3500 ? 0 : s->session->timeout)
30f05b19
MC
3501 || (SSL_IS_TLS13(s)
3502 && !WPACKET_put_bytes_u32(pkt, age_add_u.age_add))
a00d75e1 3503 /* Now the actual ticket data */
7cea05dc
MC
3504 || !WPACKET_start_sub_packet_u16(pkt)
3505 || !WPACKET_get_total_written(pkt, &macoffset)
a00d75e1 3506 /* Output key name */
7cea05dc 3507 || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
a00d75e1 3508 /* output IV */
7cea05dc
MC
3509 || !WPACKET_memcpy(pkt, iv, iv_len)
3510 || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
a00d75e1
MC
3511 &encdata1)
3512 /* Encrypt session data */
3513 || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
7cea05dc 3514 || !WPACKET_allocate_bytes(pkt, len, &encdata2)
a00d75e1
MC
3515 || encdata1 != encdata2
3516 || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
7cea05dc 3517 || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
a00d75e1
MC
3518 || encdata1 + len != encdata2
3519 || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
7cea05dc 3520 || !WPACKET_get_total_written(pkt, &macendoffset)
a00d75e1
MC
3521 || !HMAC_Update(hctx,
3522 (unsigned char *)s->init_buf->data + macoffset,
3523 macendoffset - macoffset)
7cea05dc 3524 || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
a00d75e1
MC
3525 || !HMAC_Final(hctx, macdata1, &hlen)
3526 || hlen > EVP_MAX_MD_SIZE
7cea05dc 3527 || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
a00d75e1 3528 || macdata1 != macdata2
30f05b19
MC
3529 || !WPACKET_close(pkt)
3530 || (SSL_IS_TLS13(s)
3531 && !tls_construct_extensions(s, pkt,
fe874d27 3532 SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
30f05b19 3533 NULL, 0, &al))) {
a00d75e1 3534 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
e27f234a 3535 goto err;
a00d75e1 3536 }
bcaad809
DSH
3537 EVP_CIPHER_CTX_free(ctx);
3538 HMAC_CTX_free(hctx);
e27f234a
MC
3539 OPENSSL_free(senc);
3540
3541 return 1;
687eaf27 3542 err:
f6370040 3543 ossl_statem_set_error(s);
b548a1f1 3544 OPENSSL_free(senc);
846ec07d 3545 EVP_CIPHER_CTX_free(ctx);
bf7c6817 3546 HMAC_CTX_free(hctx);
a00d75e1 3547 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
e27f234a 3548 return 0;
0f113f3e 3549}
67c8e7f4 3550
f63e4288
MC
3551/*
3552 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
3553 * create a separate message. Returns 1 on success or 0 on failure.
3554 */
3555int tls_construct_cert_status_body(SSL *s, WPACKET *pkt)
e27f234a 3556{
8cbfcc70
RS
3557 if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)
3558 || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,
3559 s->ext.ocsp.resp_len)) {
f63e4288
MC
3560 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY, ERR_R_INTERNAL_ERROR);
3561 return 0;
3562 }
3563
3564 return 1;
3565}
3566
3567int tls_construct_cert_status(SSL *s, WPACKET *pkt)
3568{
3569 if (!tls_construct_cert_status_body(s, pkt)) {
cc59ad10 3570 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
cc59ad10
MC
3571 return 0;
3572 }
e27f234a
MC
3573
3574 return 1;
3575}
3576
e481f9b9 3577#ifndef OPENSSL_NO_NEXTPROTONEG
e27f234a
MC
3578/*
3579 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
3580 * It sets the next_proto member in s if found
3581 */
be3583fa 3582MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
e27f234a 3583{
73999b62 3584 PACKET next_proto, padding;
e27f234a
MC
3585 size_t next_proto_len;
3586
50e735f9
MC
3587 /*-
3588 * The payload looks like:
3589 * uint8 proto_len;
3590 * uint8 proto[proto_len];
3591 * uint8 padding_len;
3592 * uint8 padding[padding_len];
3593 */
73999b62
MC
3594 if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
3595 || !PACKET_get_length_prefixed_1(pkt, &padding)
3596 || PACKET_remaining(pkt) > 0) {
e27f234a 3597 SSLerr(SSL_F_TLS_PROCESS_NEXT_PROTO, SSL_R_LENGTH_MISMATCH);
c3fc7eea 3598 goto err;
cf9b0b6f 3599 }
0f113f3e 3600
aff8c126
RS
3601 if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
3602 s->ext.npn_len = 0;
c3fc7eea
MC
3603 goto err;
3604 }
3605
aff8c126 3606 s->ext.npn_len = (unsigned char)next_proto_len;
0f113f3e 3607
e27f234a 3608 return MSG_PROCESS_CONTINUE_READING;
a230b26e 3609 err:
fe3a3291 3610 ossl_statem_set_error(s);
e27f234a 3611 return MSG_PROCESS_ERROR;
0f113f3e 3612}
6434abbf 3613#endif
d45ba43d 3614
e46f2334
MC
3615static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt)
3616{
3434f40b
MC
3617 int al;
3618
fe874d27 3619 if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
30aeba43 3620 NULL, 0, &al)) {
3434f40b 3621 ssl3_send_alert(s, SSL3_AL_FATAL, al);
e46f2334 3622 SSLerr(SSL_F_TLS_CONSTRUCT_ENCRYPTED_EXTENSIONS, ERR_R_INTERNAL_ERROR);
3434f40b 3623 ssl3_send_alert(s, SSL3_AL_FATAL, al);
e46f2334
MC
3624 return 0;
3625 }
3626
3627 return 1;
3628}
3629
7d061fce
MC
3630static int tls_construct_hello_retry_request(SSL *s, WPACKET *pkt)
3631{
429ff318 3632 int al = SSL_AD_INTERNAL_ERROR;
11c67eea 3633 size_t len = 0;
7d061fce
MC
3634
3635 /*
3636 * TODO(TLS1.3): Remove the DRAFT version before release
3637 * (should be s->version)
3638 */
3639 if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)
11c67eea 3640 || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt, &len)
fe874d27
MC
3641 || !tls_construct_extensions(s, pkt,
3642 SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
7d061fce 3643 NULL, 0, &al)) {
7d061fce 3644 SSLerr(SSL_F_TLS_CONSTRUCT_HELLO_RETRY_REQUEST, ERR_R_INTERNAL_ERROR);
11c67eea 3645 goto err;
7d061fce
MC
3646 }
3647
3648 /* Ditch the session. We'll create a new one next time around */
3649 SSL_SESSION_free(s->session);
3650 s->session = NULL;
3651 s->hit = 0;
3652
11c67eea
MC
3653 /*
3654 * Re-initialise the Transcript Hash. We're going to prepopulate it with
3655 * a synthetic message_hash in place of ClientHello1.
3656 */
3657 if (!create_synthetic_message_hash(s))
3658 goto err;
3659
7d061fce 3660 return 1;
11c67eea
MC
3661 err:
3662 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3663 return 0;
7d061fce 3664}
ef6c191b
MC
3665
3666MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL *s, PACKET *pkt)
3667{
3668 int al = SSL_AD_INTERNAL_ERROR;
3669
3670 if (PACKET_remaining(pkt) != 0) {
3671 al = SSL_AD_DECODE_ERROR;
3672 SSLerr(SSL_F_TLS_PROCESS_END_OF_EARLY_DATA, SSL_R_LENGTH_MISMATCH);
3673 ossl_statem_set_error(s);
3674 return MSG_PROCESS_ERROR;
3675 }
3676
3677 if (s->early_data_state != SSL_EARLY_DATA_READING
3678 && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) {
3679 SSLerr(SSL_F_TLS_PROCESS_END_OF_EARLY_DATA, ERR_R_INTERNAL_ERROR);
3680 goto err;
3681 }
3682
3683 /*
3684 * EndOfEarlyData signals a key change so the end of the message must be on
3685 * a record boundary.
3686 */
3687 if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
3688 al = SSL_AD_UNEXPECTED_MESSAGE;
3689 SSLerr(SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
3690 SSL_R_NOT_ON_RECORD_BOUNDARY);
3691 goto err;
3692 }
3693
3694 s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
3695 if (!s->method->ssl3_enc->change_cipher_state(s,
3696 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
3697 SSLerr(SSL_F_TLS_PROCESS_END_OF_EARLY_DATA, ERR_R_INTERNAL_ERROR);
3698 goto err;
3699 }
3700
3701 return MSG_PROCESS_CONTINUE_READING;
3702 err:
3703 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3704 ossl_statem_set_error(s);
3705 return MSG_PROCESS_ERROR;
3706}