]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
3c95ef22 | 2 | * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. |
aa8f3d76 | 3 | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
c80149d9 | 4 | * Copyright 2005 Nokia. All rights reserved. |
8e2f6b79 | 5 | * |
2c18d164 | 6 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
7 | * this file except in compliance with the License. You can obtain a copy |
8 | * in the file LICENSE in the source distribution or at | |
9 | * https://www.openssl.org/source/license.html | |
8e2f6b79 | 10 | */ |
846e33c7 | 11 | |
d02b48c6 | 12 | #include <stdio.h> |
706457b7 DMSP |
13 | #include "../ssl_local.h" |
14 | #include "statem_local.h" | |
15 | #include "internal/constant_time.h" | |
3faa07b5 | 16 | #include "internal/cryptlib.h" |
ec577822 BM |
17 | #include <openssl/buffer.h> |
18 | #include <openssl/rand.h> | |
19 | #include <openssl/objects.h> | |
20 | #include <openssl/evp.h> | |
21 | #include <openssl/x509.h> | |
3c27208f | 22 | #include <openssl/dh.h> |
d7e498ac | 23 | #include <openssl/rsa.h> |
d095b68d | 24 | #include <openssl/bn.h> |
dbad1690 | 25 | #include <openssl/md5.h> |
77359d22 | 26 | #include <openssl/trace.h> |
e7db9680 | 27 | #include <openssl/core_names.h> |
4e3ee452 | 28 | #include <openssl/asn1t.h> |
b67cb09f | 29 | #include <openssl/comp.h> |
f9b3bff6 | 30 | |
4ff1a526 MC |
31 | #define TICKET_NONCE_SIZE 8 |
32 | ||
4e3ee452 DB |
33 | typedef struct { |
34 | ASN1_TYPE *kxBlob; | |
35 | ASN1_TYPE *opaqueBlob; | |
36 | } GOST_KX_MESSAGE; | |
37 | ||
38 | DECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE) | |
39 | ||
40 | ASN1_SEQUENCE(GOST_KX_MESSAGE) = { | |
41 | ASN1_SIMPLE(GOST_KX_MESSAGE, kxBlob, ASN1_ANY), | |
42 | ASN1_OPT(GOST_KX_MESSAGE, opaqueBlob, ASN1_ANY), | |
43 | } ASN1_SEQUENCE_END(GOST_KX_MESSAGE) | |
44 | ||
45 | IMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE) | |
46 | ||
67ec6d2b MC |
47 | static CON_FUNC_RETURN tls_construct_encrypted_extensions(SSL_CONNECTION *s, |
48 | WPACKET *pkt); | |
d45ba43d | 49 | |
3c95ef22 TS |
50 | static ossl_inline int received_client_cert(const SSL_CONNECTION *sc) |
51 | { | |
52 | return sc->session->peer_rpk != NULL || sc->session->peer != NULL; | |
53 | } | |
54 | ||
61ae935a | 55 | /* |
0f1e51ea MC |
56 | * ossl_statem_server13_read_transition() encapsulates the logic for the allowed |
57 | * handshake state transitions when a TLSv1.3 server is reading messages from | |
58 | * the client. The message type that the client has sent is provided in |mt|. | |
59 | * The current state is in |s->statem.hand_state|. | |
60 | * | |
94ed2c67 MC |
61 | * Return values are 1 for success (transition allowed) and 0 on error |
62 | * (transition not allowed) | |
0f1e51ea | 63 | */ |
38b051a1 | 64 | static int ossl_statem_server13_read_transition(SSL_CONNECTION *s, int mt) |
0f1e51ea MC |
65 | { |
66 | OSSL_STATEM *st = &s->statem; | |
67 | ||
68 | /* | |
69 | * Note: There is no case for TLS_ST_BEFORE because at that stage we have | |
70 | * not negotiated TLSv1.3 yet, so that case is handled by | |
71 | * ossl_statem_server_read_transition() | |
72 | */ | |
73 | switch (st->hand_state) { | |
74 | default: | |
75 | break; | |
76 | ||
d7f8783f | 77 | case TLS_ST_EARLY_DATA: |
fc7129dc | 78 | if (s->hello_retry_request == SSL_HRR_PENDING) { |
d4504fe5 MC |
79 | if (mt == SSL3_MT_CLIENT_HELLO) { |
80 | st->hand_state = TLS_ST_SR_CLNT_HELLO; | |
81 | return 1; | |
82 | } | |
83 | break; | |
84 | } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) { | |
ef6c191b MC |
85 | if (mt == SSL3_MT_END_OF_EARLY_DATA) { |
86 | st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA; | |
87 | return 1; | |
88 | } | |
89 | break; | |
90 | } | |
91 | /* Fall through */ | |
92 | ||
93 | case TLS_ST_SR_END_OF_EARLY_DATA: | |
92760c21 | 94 | case TLS_ST_SW_FINISHED: |
555cbb32 | 95 | if (s->s3.tmp.cert_request) { |
0f1e51ea MC |
96 | if (mt == SSL3_MT_CERTIFICATE) { |
97 | st->hand_state = TLS_ST_SR_CERT; | |
98 | return 1; | |
99 | } | |
b67cb09f TS |
100 | #ifndef OPENSSL_NO_COMP_ALG |
101 | if (mt == SSL3_MT_COMPRESSED_CERTIFICATE | |
102 | && s->ext.compress_certificate_sent) { | |
103 | st->hand_state = TLS_ST_SR_COMP_CERT; | |
104 | return 1; | |
105 | } | |
106 | #endif | |
0f1e51ea | 107 | } else { |
92760c21 MC |
108 | if (mt == SSL3_MT_FINISHED) { |
109 | st->hand_state = TLS_ST_SR_FINISHED; | |
0f1e51ea MC |
110 | return 1; |
111 | } | |
112 | } | |
113 | break; | |
114 | ||
b67cb09f | 115 | case TLS_ST_SR_COMP_CERT: |
0f1e51ea | 116 | case TLS_ST_SR_CERT: |
3c95ef22 | 117 | if (!received_client_cert(s)) { |
92760c21 MC |
118 | if (mt == SSL3_MT_FINISHED) { |
119 | st->hand_state = TLS_ST_SR_FINISHED; | |
0f1e51ea MC |
120 | return 1; |
121 | } | |
122 | } else { | |
123 | if (mt == SSL3_MT_CERTIFICATE_VERIFY) { | |
124 | st->hand_state = TLS_ST_SR_CERT_VRFY; | |
125 | return 1; | |
126 | } | |
127 | } | |
128 | break; | |
129 | ||
130 | case TLS_ST_SR_CERT_VRFY: | |
0f1e51ea MC |
131 | if (mt == SSL3_MT_FINISHED) { |
132 | st->hand_state = TLS_ST_SR_FINISHED; | |
133 | return 1; | |
134 | } | |
135 | break; | |
8cdc8c51 MC |
136 | |
137 | case TLS_ST_OK: | |
10109364 MC |
138 | /* |
139 | * Its never ok to start processing handshake messages in the middle of | |
140 | * early data (i.e. before we've received the end of early data alert) | |
141 | */ | |
142 | if (s->early_data_state == SSL_EARLY_DATA_READING) | |
143 | break; | |
9d75dce3 | 144 | |
b67cb09f TS |
145 | if (s->post_handshake_auth == SSL_PHA_REQUESTED) { |
146 | if (mt == SSL3_MT_CERTIFICATE) { | |
147 | st->hand_state = TLS_ST_SR_CERT; | |
148 | return 1; | |
149 | } | |
150 | #ifndef OPENSSL_NO_COMP_ALG | |
151 | if (mt == SSL3_MT_COMPRESSED_CERTIFICATE | |
152 | && s->ext.compress_certificate_sent) { | |
153 | st->hand_state = TLS_ST_SR_COMP_CERT; | |
154 | return 1; | |
155 | } | |
156 | #endif | |
9d75dce3 TS |
157 | } |
158 | ||
8cdc8c51 MC |
159 | if (mt == SSL3_MT_KEY_UPDATE) { |
160 | st->hand_state = TLS_ST_SR_KEY_UPDATE; | |
161 | return 1; | |
162 | } | |
163 | break; | |
0f1e51ea MC |
164 | } |
165 | ||
166 | /* No valid transition found */ | |
0f1e51ea MC |
167 | return 0; |
168 | } | |
169 | ||
170 | /* | |
171 | * ossl_statem_server_read_transition() encapsulates the logic for the allowed | |
172 | * handshake state transitions when the server is reading messages from the | |
173 | * client. The message type that the client has sent is provided in |mt|. The | |
174 | * current state is in |s->statem.hand_state|. | |
61ae935a | 175 | * |
94ed2c67 MC |
176 | * Return values are 1 for success (transition allowed) and 0 on error |
177 | * (transition not allowed) | |
61ae935a | 178 | */ |
38b051a1 | 179 | int ossl_statem_server_read_transition(SSL_CONNECTION *s, int mt) |
61ae935a | 180 | { |
d6f1a6e9 | 181 | OSSL_STATEM *st = &s->statem; |
61ae935a | 182 | |
38b051a1 | 183 | if (SSL_CONNECTION_IS_TLS13(s)) { |
5abeaf35 MC |
184 | if (!ossl_statem_server13_read_transition(s, mt)) |
185 | goto err; | |
186 | return 1; | |
187 | } | |
0f1e51ea | 188 | |
e8aa8b6c | 189 | switch (st->hand_state) { |
f3b3d7f0 RS |
190 | default: |
191 | break; | |
192 | ||
61ae935a | 193 | case TLS_ST_BEFORE: |
0386aad1 | 194 | case TLS_ST_OK: |
61ae935a MC |
195 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: |
196 | if (mt == SSL3_MT_CLIENT_HELLO) { | |
197 | st->hand_state = TLS_ST_SR_CLNT_HELLO; | |
198 | return 1; | |
199 | } | |
200 | break; | |
201 | ||
202 | case TLS_ST_SW_SRVR_DONE: | |
203 | /* | |
204 | * If we get a CKE message after a ServerDone then either | |
205 | * 1) We didn't request a Certificate | |
206 | * OR | |
207 | * 2) If we did request one then | |
208 | * a) We allow no Certificate to be returned | |
209 | * AND | |
210 | * b) We are running SSL3 (in TLS1.0+ the client must return a 0 | |
211 | * list if we requested a certificate) | |
212 | */ | |
0f512756 | 213 | if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) { |
555cbb32 | 214 | if (s->s3.tmp.cert_request) { |
0f512756 | 215 | if (s->version == SSL3_VERSION) { |
23dd09b5 MC |
216 | if ((s->verify_mode & SSL_VERIFY_PEER) |
217 | && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { | |
0f512756 MC |
218 | /* |
219 | * This isn't an unexpected message as such - we're just | |
23dd09b5 MC |
220 | * not going to accept it because we require a client |
221 | * cert. | |
0f512756 | 222 | */ |
3ec8d113 | 223 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
3ec8d113 | 224 | SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
0f512756 MC |
225 | return 0; |
226 | } | |
227 | st->hand_state = TLS_ST_SR_KEY_EXCH; | |
228 | return 1; | |
229 | } | |
230 | } else { | |
231 | st->hand_state = TLS_ST_SR_KEY_EXCH; | |
232 | return 1; | |
233 | } | |
555cbb32 | 234 | } else if (s->s3.tmp.cert_request) { |
61ae935a MC |
235 | if (mt == SSL3_MT_CERTIFICATE) { |
236 | st->hand_state = TLS_ST_SR_CERT; | |
237 | return 1; | |
f100b031 | 238 | } |
61ae935a MC |
239 | } |
240 | break; | |
241 | ||
242 | case TLS_ST_SR_CERT: | |
243 | if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) { | |
244 | st->hand_state = TLS_ST_SR_KEY_EXCH; | |
245 | return 1; | |
246 | } | |
247 | break; | |
248 | ||
249 | case TLS_ST_SR_KEY_EXCH: | |
250 | /* | |
251 | * We should only process a CertificateVerify message if we have | |
252 | * received a Certificate from the client. If so then |s->session->peer| | |
253 | * will be non NULL. In some instances a CertificateVerify message is | |
254 | * not required even if the peer has sent a Certificate (e.g. such as in | |
a71a4966 | 255 | * the case of static DH). In that case |st->no_cert_verify| should be |
61ae935a MC |
256 | * set. |
257 | */ | |
3c95ef22 | 258 | if (!received_client_cert(s) || st->no_cert_verify) { |
61ae935a MC |
259 | if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
260 | /* | |
261 | * For the ECDH ciphersuites when the client sends its ECDH | |
262 | * pub key in a certificate, the CertificateVerify message is | |
263 | * not sent. Also for GOST ciphersuites when the client uses | |
264 | * its key from the certificate for key exchange. | |
265 | */ | |
266 | st->hand_state = TLS_ST_SR_CHANGE; | |
267 | return 1; | |
268 | } | |
269 | } else { | |
270 | if (mt == SSL3_MT_CERTIFICATE_VERIFY) { | |
271 | st->hand_state = TLS_ST_SR_CERT_VRFY; | |
272 | return 1; | |
273 | } | |
274 | } | |
275 | break; | |
276 | ||
277 | case TLS_ST_SR_CERT_VRFY: | |
278 | if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | |
279 | st->hand_state = TLS_ST_SR_CHANGE; | |
280 | return 1; | |
281 | } | |
282 | break; | |
283 | ||
284 | case TLS_ST_SR_CHANGE: | |
285 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
555cbb32 | 286 | if (s->s3.npn_seen) { |
61ae935a MC |
287 | if (mt == SSL3_MT_NEXT_PROTO) { |
288 | st->hand_state = TLS_ST_SR_NEXT_PROTO; | |
289 | return 1; | |
290 | } | |
291 | } else { | |
292 | #endif | |
293 | if (mt == SSL3_MT_FINISHED) { | |
294 | st->hand_state = TLS_ST_SR_FINISHED; | |
295 | return 1; | |
296 | } | |
297 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
298 | } | |
299 | #endif | |
300 | break; | |
301 | ||
302 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
303 | case TLS_ST_SR_NEXT_PROTO: | |
304 | if (mt == SSL3_MT_FINISHED) { | |
305 | st->hand_state = TLS_ST_SR_FINISHED; | |
306 | return 1; | |
307 | } | |
308 | break; | |
309 | #endif | |
310 | ||
311 | case TLS_ST_SW_FINISHED: | |
312 | if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | |
313 | st->hand_state = TLS_ST_SR_CHANGE; | |
314 | return 1; | |
315 | } | |
316 | break; | |
61ae935a MC |
317 | } |
318 | ||
5abeaf35 | 319 | err: |
61ae935a | 320 | /* No valid transition found */ |
38b051a1 | 321 | if (SSL_CONNECTION_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
f20404fc MC |
322 | BIO *rbio; |
323 | ||
324 | /* | |
325 | * CCS messages don't have a message sequence number so this is probably | |
326 | * because of an out-of-order CCS. We'll just drop it. | |
327 | */ | |
328 | s->init_num = 0; | |
329 | s->rwstate = SSL_READING; | |
38b051a1 | 330 | rbio = SSL_get_rbio(SSL_CONNECTION_GET_SSL(s)); |
f20404fc MC |
331 | BIO_clear_retry_flags(rbio); |
332 | BIO_set_retry_read(rbio); | |
333 | return 0; | |
334 | } | |
c48ffbcc | 335 | SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
61ae935a MC |
336 | return 0; |
337 | } | |
338 | ||
339 | /* | |
340 | * Should we send a ServerKeyExchange message? | |
341 | * | |
342 | * Valid return values are: | |
343 | * 1: Yes | |
344 | * 0: No | |
345 | */ | |
38b051a1 | 346 | static int send_server_key_exchange(SSL_CONNECTION *s) |
61ae935a | 347 | { |
555cbb32 | 348 | unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey; |
61ae935a MC |
349 | |
350 | /* | |
361a1191 | 351 | * only send a ServerKeyExchange if DH or fortezza but we have a |
61ae935a MC |
352 | * sign only certificate PSK: may send PSK identity hints For |
353 | * ECC ciphersuites, we send a serverKeyExchange message only if | |
354 | * the cipher suite is either ECDH-anon or ECDHE. In other cases, | |
355 | * the server certificate contains the server's public key for | |
356 | * key exchange. | |
357 | */ | |
a230b26e | 358 | if (alg_k & (SSL_kDHE | SSL_kECDHE) |
61ae935a MC |
359 | /* |
360 | * PSK: send ServerKeyExchange if PSK identity hint if | |
361 | * provided | |
362 | */ | |
363 | #ifndef OPENSSL_NO_PSK | |
364 | /* Only send SKE if we have identity hint for plain PSK */ | |
365 | || ((alg_k & (SSL_kPSK | SSL_kRSAPSK)) | |
366 | && s->cert->psk_identity_hint) | |
367 | /* For other PSK always send SKE */ | |
368 | || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK))) | |
369 | #endif | |
370 | #ifndef OPENSSL_NO_SRP | |
371 | /* SRP: send ServerKeyExchange */ | |
372 | || (alg_k & SSL_kSRP) | |
373 | #endif | |
a230b26e | 374 | ) { |
61ae935a MC |
375 | return 1; |
376 | } | |
377 | ||
378 | return 0; | |
379 | } | |
380 | ||
b67cb09f TS |
381 | /* |
382 | * Used to determine if we shoud send a CompressedCertificate message | |
383 | * | |
384 | * Returns the algorithm to use, TLSEXT_comp_cert_none means no compression | |
385 | */ | |
386 | static int get_compressed_certificate_alg(SSL_CONNECTION *sc) | |
387 | { | |
388 | #ifndef OPENSSL_NO_COMP_ALG | |
389 | int *alg = sc->ext.compress_certificate_from_peer; | |
390 | ||
391 | if (sc->s3.tmp.cert == NULL) | |
392 | return TLSEXT_comp_cert_none; | |
393 | ||
394 | for (; *alg != TLSEXT_comp_cert_none; alg++) { | |
395 | if (sc->s3.tmp.cert->comp_cert[*alg] != NULL) | |
396 | return *alg; | |
397 | } | |
398 | #endif | |
399 | return TLSEXT_comp_cert_none; | |
400 | } | |
401 | ||
61ae935a MC |
402 | /* |
403 | * Should we send a CertificateRequest message? | |
404 | * | |
405 | * Valid return values are: | |
406 | * 1: Yes | |
407 | * 0: No | |
408 | */ | |
38b051a1 | 409 | int send_certificate_request(SSL_CONNECTION *s) |
61ae935a MC |
410 | { |
411 | if ( | |
412 | /* don't request cert unless asked for it: */ | |
413 | s->verify_mode & SSL_VERIFY_PEER | |
9d75dce3 TS |
414 | /* |
415 | * don't request if post-handshake-only unless doing | |
416 | * post-handshake in TLSv1.3: | |
417 | */ | |
38b051a1 TM |
418 | && (!SSL_CONNECTION_IS_TLS13(s) |
419 | || !(s->verify_mode & SSL_VERIFY_POST_HANDSHAKE) | |
9d75dce3 | 420 | || s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) |
61ae935a MC |
421 | /* |
422 | * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert | |
9d75dce3 | 423 | * a second time: |
61ae935a | 424 | */ |
9d75dce3 | 425 | && (s->certreqs_sent < 1 || |
61ae935a MC |
426 | !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) |
427 | /* | |
428 | * never request cert in anonymous ciphersuites (see | |
429 | * section "Certificate request" in SSL 3 drafts and in | |
430 | * RFC 2246): | |
431 | */ | |
555cbb32 | 432 | && (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL) |
a230b26e EK |
433 | /* |
434 | * ... except when the application insists on | |
435 | * verification (against the specs, but statem_clnt.c accepts | |
436 | * this for SSL 3) | |
437 | */ | |
61ae935a MC |
438 | || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) |
439 | /* don't request certificate for SRP auth */ | |
555cbb32 | 440 | && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aSRP) |
61ae935a MC |
441 | /* |
442 | * With normal PSK Certificates and Certificate Requests | |
443 | * are omitted | |
444 | */ | |
555cbb32 | 445 | && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aPSK)) { |
61ae935a MC |
446 | return 1; |
447 | } | |
448 | ||
449 | return 0; | |
450 | } | |
451 | ||
3c95ef22 TS |
452 | static int do_compressed_cert(SSL_CONNECTION *sc) |
453 | { | |
454 | /* If we negotiated RPK, we won't attempt to compress it */ | |
455 | return sc->ext.server_cert_type == TLSEXT_cert_type_x509 | |
456 | && get_compressed_certificate_alg(sc) != TLSEXT_comp_cert_none; | |
457 | } | |
458 | ||
61ae935a | 459 | /* |
0f1e51ea MC |
460 | * ossl_statem_server13_write_transition() works out what handshake state to |
461 | * move to next when a TLSv1.3 server is writing messages to be sent to the | |
462 | * client. | |
0f1e51ea | 463 | */ |
38b051a1 | 464 | static WRITE_TRAN ossl_statem_server13_write_transition(SSL_CONNECTION *s) |
0f1e51ea MC |
465 | { |
466 | OSSL_STATEM *st = &s->statem; | |
467 | ||
468 | /* | |
469 | * No case for TLS_ST_BEFORE, because at that stage we have not negotiated | |
470 | * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition() | |
471 | */ | |
472 | ||
473 | switch (st->hand_state) { | |
474 | default: | |
475 | /* Shouldn't happen */ | |
c48ffbcc | 476 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
0f1e51ea MC |
477 | return WRITE_TRAN_ERROR; |
478 | ||
44c04a2e MC |
479 | case TLS_ST_OK: |
480 | if (s->key_update != SSL_KEY_UPDATE_NONE) { | |
481 | st->hand_state = TLS_ST_SW_KEY_UPDATE; | |
482 | return WRITE_TRAN_CONTINUE; | |
483 | } | |
9d75dce3 TS |
484 | if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { |
485 | st->hand_state = TLS_ST_SW_CERT_REQ; | |
486 | return WRITE_TRAN_CONTINUE; | |
487 | } | |
3bfacb5f BK |
488 | if (s->ext.extra_tickets_expected > 0) { |
489 | st->hand_state = TLS_ST_SW_SESSION_TICKET; | |
490 | return WRITE_TRAN_CONTINUE; | |
491 | } | |
8cdc8c51 MC |
492 | /* Try to read from the client instead */ |
493 | return WRITE_TRAN_FINISHED; | |
44c04a2e | 494 | |
0f1e51ea | 495 | case TLS_ST_SR_CLNT_HELLO: |
597c51bc | 496 | st->hand_state = TLS_ST_SW_SRVR_HELLO; |
d4504fe5 | 497 | return WRITE_TRAN_CONTINUE; |
7d061fce | 498 | |
0f1e51ea | 499 | case TLS_ST_SW_SRVR_HELLO: |
fc7129dc MC |
500 | if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0 |
501 | && s->hello_retry_request != SSL_HRR_COMPLETE) | |
db37d32c | 502 | st->hand_state = TLS_ST_SW_CHANGE; |
fc7129dc MC |
503 | else if (s->hello_retry_request == SSL_HRR_PENDING) |
504 | st->hand_state = TLS_ST_EARLY_DATA; | |
db37d32c MC |
505 | else |
506 | st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS; | |
507 | return WRITE_TRAN_CONTINUE; | |
508 | ||
509 | case TLS_ST_SW_CHANGE: | |
fc7129dc MC |
510 | if (s->hello_retry_request == SSL_HRR_PENDING) |
511 | st->hand_state = TLS_ST_EARLY_DATA; | |
512 | else | |
513 | st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS; | |
e46f2334 MC |
514 | return WRITE_TRAN_CONTINUE; |
515 | ||
516 | case TLS_ST_SW_ENCRYPTED_EXTENSIONS: | |
94ed2c67 | 517 | if (s->hit) |
92760c21 MC |
518 | st->hand_state = TLS_ST_SW_FINISHED; |
519 | else if (send_certificate_request(s)) | |
520 | st->hand_state = TLS_ST_SW_CERT_REQ; | |
3c95ef22 | 521 | else if (do_compressed_cert(s)) |
b67cb09f | 522 | st->hand_state = TLS_ST_SW_COMP_CERT; |
94ed2c67 | 523 | else |
0f1e51ea | 524 | st->hand_state = TLS_ST_SW_CERT; |
94ed2c67 | 525 | |
0f1e51ea MC |
526 | return WRITE_TRAN_CONTINUE; |
527 | ||
0f1e51ea | 528 | case TLS_ST_SW_CERT_REQ: |
9d75dce3 TS |
529 | if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { |
530 | s->post_handshake_auth = SSL_PHA_REQUESTED; | |
531 | st->hand_state = TLS_ST_OK; | |
3c95ef22 | 532 | } else if (do_compressed_cert(s)) { |
b67cb09f | 533 | st->hand_state = TLS_ST_SW_COMP_CERT; |
9d75dce3 TS |
534 | } else { |
535 | st->hand_state = TLS_ST_SW_CERT; | |
536 | } | |
0f1e51ea MC |
537 | return WRITE_TRAN_CONTINUE; |
538 | ||
b67cb09f | 539 | case TLS_ST_SW_COMP_CERT: |
92760c21 | 540 | case TLS_ST_SW_CERT: |
2c5dfdc3 MC |
541 | st->hand_state = TLS_ST_SW_CERT_VRFY; |
542 | return WRITE_TRAN_CONTINUE; | |
543 | ||
544 | case TLS_ST_SW_CERT_VRFY: | |
d805a57b | 545 | st->hand_state = TLS_ST_SW_FINISHED; |
0f1e51ea MC |
546 | return WRITE_TRAN_CONTINUE; |
547 | ||
548 | case TLS_ST_SW_FINISHED: | |
f7e393be MC |
549 | st->hand_state = TLS_ST_EARLY_DATA; |
550 | return WRITE_TRAN_CONTINUE; | |
94ed2c67 | 551 | |
d7f8783f MC |
552 | case TLS_ST_EARLY_DATA: |
553 | return WRITE_TRAN_FINISHED; | |
554 | ||
92760c21 | 555 | case TLS_ST_SR_FINISHED: |
30f05b19 MC |
556 | /* |
557 | * Technically we have finished the handshake at this point, but we're | |
9d0a8bb7 | 558 | * going to remain "in_init" for now and write out any session tickets |
30f05b19 | 559 | * immediately. |
30f05b19 | 560 | */ |
c0638ade MC |
561 | if (s->post_handshake_auth == SSL_PHA_REQUESTED) { |
562 | s->post_handshake_auth = SSL_PHA_EXT_RECEIVED; | |
61fb5923 | 563 | } else if (!s->ext.ticket_expected) { |
c0638ade | 564 | /* |
61fb5923 MC |
565 | * If we're not going to renew the ticket then we just finish the |
566 | * handshake at this point. | |
c0638ade MC |
567 | */ |
568 | st->hand_state = TLS_ST_OK; | |
9d0a8bb7 | 569 | return WRITE_TRAN_CONTINUE; |
c0638ade | 570 | } |
9d0a8bb7 MC |
571 | if (s->num_tickets > s->sent_tickets) |
572 | st->hand_state = TLS_ST_SW_SESSION_TICKET; | |
573 | else | |
574 | st->hand_state = TLS_ST_OK; | |
30f05b19 MC |
575 | return WRITE_TRAN_CONTINUE; |
576 | ||
8cdc8c51 | 577 | case TLS_ST_SR_KEY_UPDATE: |
44c04a2e | 578 | case TLS_ST_SW_KEY_UPDATE: |
36ff232c MC |
579 | st->hand_state = TLS_ST_OK; |
580 | return WRITE_TRAN_CONTINUE; | |
581 | ||
30f05b19 | 582 | case TLS_ST_SW_SESSION_TICKET: |
9d0a8bb7 MC |
583 | /* In a resumption we only ever send a maximum of one new ticket. |
584 | * Following an initial handshake we send the number of tickets we have | |
585 | * been configured for. | |
586 | */ | |
3bfacb5f BK |
587 | if (!SSL_IS_FIRST_HANDSHAKE(s) && s->ext.extra_tickets_expected > 0) { |
588 | return WRITE_TRAN_CONTINUE; | |
589 | } else if (s->hit || s->num_tickets <= s->sent_tickets) { | |
9d0a8bb7 MC |
590 | /* We've written enough tickets out. */ |
591 | st->hand_state = TLS_ST_OK; | |
592 | } | |
0f1e51ea MC |
593 | return WRITE_TRAN_CONTINUE; |
594 | } | |
595 | } | |
596 | ||
597 | /* | |
598 | * ossl_statem_server_write_transition() works out what handshake state to move | |
599 | * to next when the server is writing messages to be sent to the client. | |
61ae935a | 600 | */ |
38b051a1 | 601 | WRITE_TRAN ossl_statem_server_write_transition(SSL_CONNECTION *s) |
61ae935a | 602 | { |
d6f1a6e9 | 603 | OSSL_STATEM *st = &s->statem; |
61ae935a | 604 | |
0f1e51ea MC |
605 | /* |
606 | * Note that before the ClientHello we don't know what version we are going | |
607 | * to negotiate yet, so we don't take this branch until later | |
608 | */ | |
609 | ||
38b051a1 | 610 | if (SSL_CONNECTION_IS_TLS13(s)) |
0f1e51ea MC |
611 | return ossl_statem_server13_write_transition(s); |
612 | ||
e8aa8b6c | 613 | switch (st->hand_state) { |
f3b3d7f0 RS |
614 | default: |
615 | /* Shouldn't happen */ | |
c48ffbcc | 616 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f3b3d7f0 RS |
617 | return WRITE_TRAN_ERROR; |
618 | ||
0386aad1 MC |
619 | case TLS_ST_OK: |
620 | if (st->request_state == TLS_ST_SW_HELLO_REQ) { | |
621 | /* We must be trying to renegotiate */ | |
622 | st->hand_state = TLS_ST_SW_HELLO_REQ; | |
623 | st->request_state = TLS_ST_BEFORE; | |
624 | return WRITE_TRAN_CONTINUE; | |
625 | } | |
c7f47786 MC |
626 | /* Must be an incoming ClientHello */ |
627 | if (!tls_setup_handshake(s)) { | |
f63a17d6 | 628 | /* SSLfatal() already called */ |
c7f47786 MC |
629 | return WRITE_TRAN_ERROR; |
630 | } | |
0386aad1 MC |
631 | /* Fall through */ |
632 | ||
e8aa8b6c | 633 | case TLS_ST_BEFORE: |
a230b26e | 634 | /* Just go straight to trying to read from the client */ |
e8aa8b6c | 635 | return WRITE_TRAN_FINISHED; |
61ae935a | 636 | |
e8aa8b6c F |
637 | case TLS_ST_SW_HELLO_REQ: |
638 | st->hand_state = TLS_ST_OK; | |
e8aa8b6c | 639 | return WRITE_TRAN_CONTINUE; |
61ae935a | 640 | |
e8aa8b6c | 641 | case TLS_ST_SR_CLNT_HELLO: |
38b051a1 TM |
642 | if (SSL_CONNECTION_IS_DTLS(s) && !s->d1->cookie_verified |
643 | && (SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE)) { | |
e8aa8b6c | 644 | st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST; |
3faa07b5 MC |
645 | } else if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) { |
646 | /* We must have rejected the renegotiation */ | |
647 | st->hand_state = TLS_ST_OK; | |
648 | return WRITE_TRAN_CONTINUE; | |
649 | } else { | |
e8aa8b6c | 650 | st->hand_state = TLS_ST_SW_SRVR_HELLO; |
3faa07b5 | 651 | } |
e8aa8b6c | 652 | return WRITE_TRAN_CONTINUE; |
61ae935a | 653 | |
e8aa8b6c F |
654 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: |
655 | return WRITE_TRAN_FINISHED; | |
61ae935a | 656 | |
e8aa8b6c F |
657 | case TLS_ST_SW_SRVR_HELLO: |
658 | if (s->hit) { | |
aff8c126 | 659 | if (s->ext.ticket_expected) |
e8aa8b6c F |
660 | st->hand_state = TLS_ST_SW_SESSION_TICKET; |
661 | else | |
662 | st->hand_state = TLS_ST_SW_CHANGE; | |
663 | } else { | |
664 | /* Check if it is anon DH or anon ECDH, */ | |
665 | /* normal PSK or SRP */ | |
555cbb32 | 666 | if (!(s->s3.tmp.new_cipher->algorithm_auth & |
a230b26e | 667 | (SSL_aNULL | SSL_aSRP | SSL_aPSK))) { |
e8aa8b6c F |
668 | st->hand_state = TLS_ST_SW_CERT; |
669 | } else if (send_server_key_exchange(s)) { | |
61ae935a | 670 | st->hand_state = TLS_ST_SW_KEY_EXCH; |
e8aa8b6c | 671 | } else if (send_certificate_request(s)) { |
61ae935a | 672 | st->hand_state = TLS_ST_SW_CERT_REQ; |
e8aa8b6c F |
673 | } else { |
674 | st->hand_state = TLS_ST_SW_SRVR_DONE; | |
61ae935a | 675 | } |
e8aa8b6c F |
676 | } |
677 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 678 | |
e8aa8b6c | 679 | case TLS_ST_SW_CERT: |
aff8c126 | 680 | if (s->ext.status_expected) { |
e8aa8b6c | 681 | st->hand_state = TLS_ST_SW_CERT_STATUS; |
61ae935a | 682 | return WRITE_TRAN_CONTINUE; |
e8aa8b6c F |
683 | } |
684 | /* Fall through */ | |
61ae935a | 685 | |
e8aa8b6c F |
686 | case TLS_ST_SW_CERT_STATUS: |
687 | if (send_server_key_exchange(s)) { | |
688 | st->hand_state = TLS_ST_SW_KEY_EXCH; | |
61ae935a | 689 | return WRITE_TRAN_CONTINUE; |
e8aa8b6c F |
690 | } |
691 | /* Fall through */ | |
61ae935a | 692 | |
e8aa8b6c F |
693 | case TLS_ST_SW_KEY_EXCH: |
694 | if (send_certificate_request(s)) { | |
695 | st->hand_state = TLS_ST_SW_CERT_REQ; | |
61ae935a | 696 | return WRITE_TRAN_CONTINUE; |
e8aa8b6c F |
697 | } |
698 | /* Fall through */ | |
61ae935a | 699 | |
e8aa8b6c F |
700 | case TLS_ST_SW_CERT_REQ: |
701 | st->hand_state = TLS_ST_SW_SRVR_DONE; | |
702 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 703 | |
e8aa8b6c F |
704 | case TLS_ST_SW_SRVR_DONE: |
705 | return WRITE_TRAN_FINISHED; | |
706 | ||
707 | case TLS_ST_SR_FINISHED: | |
708 | if (s->hit) { | |
61ae935a | 709 | st->hand_state = TLS_ST_OK; |
61ae935a | 710 | return WRITE_TRAN_CONTINUE; |
aff8c126 | 711 | } else if (s->ext.ticket_expected) { |
e8aa8b6c F |
712 | st->hand_state = TLS_ST_SW_SESSION_TICKET; |
713 | } else { | |
714 | st->hand_state = TLS_ST_SW_CHANGE; | |
715 | } | |
716 | return WRITE_TRAN_CONTINUE; | |
717 | ||
718 | case TLS_ST_SW_SESSION_TICKET: | |
719 | st->hand_state = TLS_ST_SW_CHANGE; | |
720 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 721 | |
e8aa8b6c F |
722 | case TLS_ST_SW_CHANGE: |
723 | st->hand_state = TLS_ST_SW_FINISHED; | |
724 | return WRITE_TRAN_CONTINUE; | |
725 | ||
726 | case TLS_ST_SW_FINISHED: | |
727 | if (s->hit) { | |
728 | return WRITE_TRAN_FINISHED; | |
729 | } | |
730 | st->hand_state = TLS_ST_OK; | |
e8aa8b6c | 731 | return WRITE_TRAN_CONTINUE; |
61ae935a MC |
732 | } |
733 | } | |
734 | ||
735 | /* | |
736 | * Perform any pre work that needs to be done prior to sending a message from | |
737 | * the server to the client. | |
738 | */ | |
38b051a1 | 739 | WORK_STATE ossl_statem_server_pre_work(SSL_CONNECTION *s, WORK_STATE wst) |
61ae935a | 740 | { |
d6f1a6e9 | 741 | OSSL_STATEM *st = &s->statem; |
38b051a1 | 742 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
61ae935a | 743 | |
e8aa8b6c | 744 | switch (st->hand_state) { |
f3b3d7f0 RS |
745 | default: |
746 | /* No pre work to be done */ | |
747 | break; | |
748 | ||
61ae935a MC |
749 | case TLS_ST_SW_HELLO_REQ: |
750 | s->shutdown = 0; | |
38b051a1 | 751 | if (SSL_CONNECTION_IS_DTLS(s)) |
f5c7f5df | 752 | dtls1_clear_sent_buffer(s); |
61ae935a MC |
753 | break; |
754 | ||
755 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: | |
756 | s->shutdown = 0; | |
38b051a1 | 757 | if (SSL_CONNECTION_IS_DTLS(s)) { |
f5c7f5df | 758 | dtls1_clear_sent_buffer(s); |
61ae935a MC |
759 | /* We don't buffer this message so don't use the timer */ |
760 | st->use_timer = 0; | |
761 | } | |
762 | break; | |
763 | ||
764 | case TLS_ST_SW_SRVR_HELLO: | |
38b051a1 | 765 | if (SSL_CONNECTION_IS_DTLS(s)) { |
61ae935a | 766 | /* |
69687aa8 | 767 | * Messages we write from now on should be buffered and |
61ae935a MC |
768 | * retransmitted if necessary, so we need to use the timer now |
769 | */ | |
770 | st->use_timer = 1; | |
771 | } | |
772 | break; | |
773 | ||
774 | case TLS_ST_SW_SRVR_DONE: | |
775 | #ifndef OPENSSL_NO_SCTP | |
38b051a1 | 776 | if (SSL_CONNECTION_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(ssl))) { |
3ec8d113 | 777 | /* Calls SSLfatal() as required */ |
61ae935a | 778 | return dtls_wait_for_dry(s); |
3ec8d113 | 779 | } |
61ae935a MC |
780 | #endif |
781 | return WORK_FINISHED_CONTINUE; | |
782 | ||
783 | case TLS_ST_SW_SESSION_TICKET: | |
38b051a1 | 784 | if (SSL_CONNECTION_IS_TLS13(s) && s->sent_tickets == 0 |
3bfacb5f | 785 | && s->ext.extra_tickets_expected == 0) { |
30f05b19 MC |
786 | /* |
787 | * Actually this is the end of the handshake, but we're going | |
788 | * straight into writing the session ticket out. So we finish off | |
789 | * the handshake, but keep the various buffers active. | |
56d36288 | 790 | * |
3ec8d113 | 791 | * Calls SSLfatal as required. |
30f05b19 | 792 | */ |
2a8db717 | 793 | return tls_finish_handshake(s, wst, 0, 0); |
6250282f | 794 | } |
38b051a1 | 795 | if (SSL_CONNECTION_IS_DTLS(s)) { |
61ae935a MC |
796 | /* |
797 | * We're into the last flight. We don't retransmit the last flight | |
798 | * unless we need to, so we don't use the timer | |
799 | */ | |
800 | st->use_timer = 0; | |
801 | } | |
802 | break; | |
803 | ||
804 | case TLS_ST_SW_CHANGE: | |
38b051a1 | 805 | if (SSL_CONNECTION_IS_TLS13(s)) |
fc7129dc | 806 | break; |
2e3ec2e1 BK |
807 | /* Writes to s->session are only safe for initial handshakes */ |
808 | if (s->session->cipher == NULL) { | |
809 | s->session->cipher = s->s3.tmp.new_cipher; | |
810 | } else if (s->session->cipher != s->s3.tmp.new_cipher) { | |
c48ffbcc | 811 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2e3ec2e1 BK |
812 | return WORK_ERROR; |
813 | } | |
38b051a1 | 814 | if (!ssl->method->ssl3_enc->setup_key_block(s)) { |
f63a17d6 | 815 | /* SSLfatal() already called */ |
61ae935a MC |
816 | return WORK_ERROR; |
817 | } | |
38b051a1 | 818 | if (SSL_CONNECTION_IS_DTLS(s)) { |
61ae935a MC |
819 | /* |
820 | * We're into the last flight. We don't retransmit the last flight | |
821 | * unless we need to, so we don't use the timer. This might have | |
822 | * already been set to 0 if we sent a NewSessionTicket message, | |
823 | * but we'll set it again here in case we didn't. | |
824 | */ | |
825 | st->use_timer = 0; | |
826 | } | |
827 | return WORK_FINISHED_CONTINUE; | |
828 | ||
d7f8783f | 829 | case TLS_ST_EARLY_DATA: |
c36001c3 | 830 | if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING |
555cbb32 | 831 | && (s->s3.flags & TLS1_FLAGS_STATELESS) == 0) |
f7e393be MC |
832 | return WORK_FINISHED_CONTINUE; |
833 | /* Fall through */ | |
834 | ||
61ae935a | 835 | case TLS_ST_OK: |
3ec8d113 | 836 | /* Calls SSLfatal() as required */ |
2a8db717 | 837 | return tls_finish_handshake(s, wst, 1, 1); |
61ae935a MC |
838 | } |
839 | ||
840 | return WORK_FINISHED_CONTINUE; | |
841 | } | |
842 | ||
f273ff95 MC |
843 | static ossl_inline int conn_is_closed(void) |
844 | { | |
845 | switch (get_last_sys_error()) { | |
846 | #if defined(EPIPE) | |
847 | case EPIPE: | |
848 | return 1; | |
849 | #endif | |
850 | #if defined(ECONNRESET) | |
851 | case ECONNRESET: | |
852 | return 1; | |
853 | #endif | |
0b885f72 PM |
854 | #if defined(WSAECONNRESET) |
855 | case WSAECONNRESET: | |
856 | return 1; | |
857 | #endif | |
f273ff95 MC |
858 | default: |
859 | return 0; | |
860 | } | |
861 | } | |
862 | ||
61ae935a MC |
863 | /* |
864 | * Perform any work that needs to be done after sending a message from the | |
865 | * server to the client. | |
866 | */ | |
38b051a1 | 867 | WORK_STATE ossl_statem_server_post_work(SSL_CONNECTION *s, WORK_STATE wst) |
61ae935a | 868 | { |
d6f1a6e9 | 869 | OSSL_STATEM *st = &s->statem; |
38b051a1 | 870 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
61ae935a MC |
871 | |
872 | s->init_num = 0; | |
873 | ||
e8aa8b6c | 874 | switch (st->hand_state) { |
f3b3d7f0 RS |
875 | default: |
876 | /* No post work to be done */ | |
877 | break; | |
878 | ||
61ae935a MC |
879 | case TLS_ST_SW_HELLO_REQ: |
880 | if (statem_flush(s) != 1) | |
881 | return WORK_MORE_A; | |
2c4a056f | 882 | if (!ssl3_init_finished_mac(s)) { |
f63a17d6 | 883 | /* SSLfatal() already called */ |
2c4a056f MC |
884 | return WORK_ERROR; |
885 | } | |
61ae935a MC |
886 | break; |
887 | ||
888 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: | |
889 | if (statem_flush(s) != 1) | |
890 | return WORK_MORE_A; | |
891 | /* HelloVerifyRequest resets Finished MAC */ | |
2c4a056f | 892 | if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) { |
f63a17d6 | 893 | /* SSLfatal() already called */ |
2c4a056f MC |
894 | return WORK_ERROR; |
895 | } | |
61ae935a MC |
896 | /* |
897 | * The next message should be another ClientHello which we need to | |
898 | * treat like it was the first packet | |
899 | */ | |
900 | s->first_packet = 1; | |
901 | break; | |
902 | ||
903 | case TLS_ST_SW_SRVR_HELLO: | |
38b051a1 TM |
904 | if (SSL_CONNECTION_IS_TLS13(s) |
905 | && s->hello_retry_request == SSL_HRR_PENDING) { | |
75259b43 MC |
906 | if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0 |
907 | && statem_flush(s) != 1) | |
597c51bc MC |
908 | return WORK_MORE_A; |
909 | break; | |
910 | } | |
61ae935a | 911 | #ifndef OPENSSL_NO_SCTP |
38b051a1 | 912 | if (SSL_CONNECTION_IS_DTLS(s) && s->hit) { |
61ae935a MC |
913 | unsigned char sctpauthkey[64]; |
914 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; | |
09d62b33 | 915 | size_t labellen; |
61ae935a MC |
916 | |
917 | /* | |
918 | * Add new shared key for SCTP-Auth, will be ignored if no | |
919 | * SCTP used. | |
920 | */ | |
141eb8c6 MC |
921 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
922 | sizeof(DTLS1_SCTP_AUTH_LABEL)); | |
61ae935a | 923 | |
09d62b33 MT |
924 | /* Don't include the terminating zero. */ |
925 | labellen = sizeof(labelbuffer) - 1; | |
926 | if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG) | |
927 | labellen += 1; | |
928 | ||
38b051a1 | 929 | if (SSL_export_keying_material(ssl, sctpauthkey, |
a230b26e | 930 | sizeof(sctpauthkey), labelbuffer, |
09d62b33 | 931 | labellen, NULL, 0, |
a230b26e | 932 | 0) <= 0) { |
c48ffbcc | 933 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
61ae935a MC |
934 | return WORK_ERROR; |
935 | } | |
936 | ||
38b051a1 | 937 | BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, |
61ae935a MC |
938 | sizeof(sctpauthkey), sctpauthkey); |
939 | } | |
940 | #endif | |
38b051a1 | 941 | if (!SSL_CONNECTION_IS_TLS13(s) |
fc7129dc MC |
942 | || ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0 |
943 | && s->hello_retry_request != SSL_HRR_COMPLETE)) | |
db37d32c MC |
944 | break; |
945 | /* Fall through */ | |
946 | ||
947 | case TLS_ST_SW_CHANGE: | |
75259b43 MC |
948 | if (s->hello_retry_request == SSL_HRR_PENDING) { |
949 | if (!statem_flush(s)) | |
950 | return WORK_MORE_A; | |
fc7129dc | 951 | break; |
75259b43 | 952 | } |
de9e884b | 953 | |
38b051a1 TM |
954 | if (SSL_CONNECTION_IS_TLS13(s)) { |
955 | if (!ssl->method->ssl3_enc->setup_key_block(s) | |
956 | || !ssl->method->ssl3_enc->change_cipher_state(s, | |
3ec8d113 MC |
957 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) { |
958 | /* SSLfatal() already called */ | |
fe5e20fd | 959 | return WORK_ERROR; |
3ec8d113 | 960 | } |
fe5e20fd MC |
961 | |
962 | if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED | |
38b051a1 | 963 | && !ssl->method->ssl3_enc->change_cipher_state(s, |
3ec8d113 MC |
964 | SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ)) { |
965 | /* SSLfatal() already called */ | |
fe5e20fd | 966 | return WORK_ERROR; |
3ec8d113 | 967 | } |
de9e884b MC |
968 | /* |
969 | * We don't yet know whether the next record we are going to receive | |
970 | * is an unencrypted alert, an encrypted alert, or an encrypted | |
971 | * handshake message. We temporarily tolerate unencrypted alerts. | |
972 | */ | |
cffafb5f MC |
973 | if (s->rlayer.rrlmethod->set_plain_alerts != NULL) |
974 | s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 1); | |
db37d32c | 975 | break; |
92760c21 | 976 | } |
61ae935a | 977 | |
61ae935a | 978 | #ifndef OPENSSL_NO_SCTP |
38b051a1 | 979 | if (SSL_CONNECTION_IS_DTLS(s) && !s->hit) { |
61ae935a MC |
980 | /* |
981 | * Change to new shared key of SCTP-Auth, will be ignored if | |
982 | * no SCTP used. | |
983 | */ | |
38b051a1 | 984 | BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, |
61ae935a MC |
985 | 0, NULL); |
986 | } | |
987 | #endif | |
38b051a1 TM |
988 | if (!ssl->method->ssl3_enc->change_cipher_state(s, |
989 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) { | |
f63a17d6 | 990 | /* SSLfatal() already called */ |
61ae935a MC |
991 | return WORK_ERROR; |
992 | } | |
993 | ||
38b051a1 | 994 | if (SSL_CONNECTION_IS_DTLS(s)) |
b92fc4ae | 995 | dtls1_increment_epoch(s, SSL3_CC_WRITE); |
61ae935a MC |
996 | break; |
997 | ||
998 | case TLS_ST_SW_SRVR_DONE: | |
999 | if (statem_flush(s) != 1) | |
1000 | return WORK_MORE_A; | |
1001 | break; | |
1002 | ||
1003 | case TLS_ST_SW_FINISHED: | |
1004 | if (statem_flush(s) != 1) | |
1005 | return WORK_MORE_A; | |
1006 | #ifndef OPENSSL_NO_SCTP | |
38b051a1 | 1007 | if (SSL_CONNECTION_IS_DTLS(s) && s->hit) { |
61ae935a MC |
1008 | /* |
1009 | * Change to new shared key of SCTP-Auth, will be ignored if | |
1010 | * no SCTP used. | |
1011 | */ | |
38b051a1 | 1012 | BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, |
61ae935a MC |
1013 | 0, NULL); |
1014 | } | |
1015 | #endif | |
38b051a1 | 1016 | if (SSL_CONNECTION_IS_TLS13(s)) { |
d74014c4 BK |
1017 | /* TLS 1.3 gets the secret size from the handshake md */ |
1018 | size_t dummy; | |
38b051a1 | 1019 | if (!ssl->method->ssl3_enc->generate_master_secret(s, |
ec15acb6 | 1020 | s->master_secret, s->handshake_secret, 0, |
d74014c4 | 1021 | &dummy) |
38b051a1 | 1022 | || !ssl->method->ssl3_enc->change_cipher_state(s, |
92760c21 | 1023 | SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE)) |
f63a17d6 | 1024 | /* SSLfatal() already called */ |
92760c21 MC |
1025 | return WORK_ERROR; |
1026 | } | |
61ae935a | 1027 | break; |
30f05b19 | 1028 | |
9d75dce3 TS |
1029 | case TLS_ST_SW_CERT_REQ: |
1030 | if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { | |
1031 | if (statem_flush(s) != 1) | |
1032 | return WORK_MORE_A; | |
b67cb09f TS |
1033 | } else { |
1034 | if (!SSL_CONNECTION_IS_TLS13(s) | |
1035 | || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0) | |
1036 | s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none; | |
1037 | } | |
1038 | break; | |
1039 | ||
1040 | case TLS_ST_SW_ENCRYPTED_EXTENSIONS: | |
1041 | if (!s->hit && !send_certificate_request(s)) { | |
1042 | if (!SSL_CONNECTION_IS_TLS13(s) | |
1043 | || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0) | |
1044 | s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none; | |
9d75dce3 TS |
1045 | } |
1046 | break; | |
1047 | ||
44c04a2e | 1048 | case TLS_ST_SW_KEY_UPDATE: |
57389a32 MC |
1049 | if (statem_flush(s) != 1) |
1050 | return WORK_MORE_A; | |
3ec8d113 MC |
1051 | if (!tls13_update_key(s, 1)) { |
1052 | /* SSLfatal() already called */ | |
57389a32 | 1053 | return WORK_ERROR; |
3ec8d113 | 1054 | } |
57389a32 MC |
1055 | break; |
1056 | ||
30f05b19 | 1057 | case TLS_ST_SW_SESSION_TICKET: |
f273ff95 | 1058 | clear_sys_error(); |
38b051a1 TM |
1059 | if (SSL_CONNECTION_IS_TLS13(s) && statem_flush(s) != 1) { |
1060 | if (SSL_get_error(ssl, 0) == SSL_ERROR_SYSCALL | |
f273ff95 MC |
1061 | && conn_is_closed()) { |
1062 | /* | |
1063 | * We ignore connection closed errors in TLSv1.3 when sending a | |
1064 | * NewSessionTicket and behave as if we were successful. This is | |
1065 | * so that we are still able to read data sent to us by a client | |
1066 | * that closes soon after the end of the handshake without | |
1067 | * waiting to read our post-handshake NewSessionTickets. | |
1068 | */ | |
1069 | s->rwstate = SSL_NOTHING; | |
1070 | break; | |
1071 | } | |
1072 | ||
30f05b19 | 1073 | return WORK_MORE_A; |
f273ff95 | 1074 | } |
30f05b19 | 1075 | break; |
61ae935a MC |
1076 | } |
1077 | ||
1078 | return WORK_FINISHED_CONTINUE; | |
1079 | } | |
1080 | ||
1081 | /* | |
6392fb8e MC |
1082 | * Get the message construction function and message type for sending from the |
1083 | * server | |
61ae935a MC |
1084 | * |
1085 | * Valid return values are: | |
1086 | * 1: Success | |
1087 | * 0: Error | |
1088 | */ | |
38b051a1 | 1089 | int ossl_statem_server_construct_message(SSL_CONNECTION *s, |
a15c953f | 1090 | confunc_f *confunc, int *mt) |
61ae935a | 1091 | { |
d6f1a6e9 | 1092 | OSSL_STATEM *st = &s->statem; |
61ae935a | 1093 | |
4a01c59f MC |
1094 | switch (st->hand_state) { |
1095 | default: | |
1096 | /* Shouldn't happen */ | |
c48ffbcc | 1097 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE); |
4a01c59f MC |
1098 | return 0; |
1099 | ||
1100 | case TLS_ST_SW_CHANGE: | |
38b051a1 | 1101 | if (SSL_CONNECTION_IS_DTLS(s)) |
6392fb8e | 1102 | *confunc = dtls_construct_change_cipher_spec; |
4a01c59f | 1103 | else |
6392fb8e MC |
1104 | *confunc = tls_construct_change_cipher_spec; |
1105 | *mt = SSL3_MT_CHANGE_CIPHER_SPEC; | |
4a01c59f | 1106 | break; |
f3b3d7f0 | 1107 | |
4a01c59f | 1108 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: |
6392fb8e MC |
1109 | *confunc = dtls_construct_hello_verify_request; |
1110 | *mt = DTLS1_MT_HELLO_VERIFY_REQUEST; | |
4a01c59f | 1111 | break; |
61ae935a | 1112 | |
4a01c59f MC |
1113 | case TLS_ST_SW_HELLO_REQ: |
1114 | /* No construction function needed */ | |
6392fb8e MC |
1115 | *confunc = NULL; |
1116 | *mt = SSL3_MT_HELLO_REQUEST; | |
4a01c59f | 1117 | break; |
61ae935a | 1118 | |
4a01c59f | 1119 | case TLS_ST_SW_SRVR_HELLO: |
6392fb8e MC |
1120 | *confunc = tls_construct_server_hello; |
1121 | *mt = SSL3_MT_SERVER_HELLO; | |
4a01c59f | 1122 | break; |
61ae935a | 1123 | |
4a01c59f | 1124 | case TLS_ST_SW_CERT: |
6392fb8e MC |
1125 | *confunc = tls_construct_server_certificate; |
1126 | *mt = SSL3_MT_CERTIFICATE; | |
4a01c59f | 1127 | break; |
61ae935a | 1128 | |
b67cb09f TS |
1129 | #ifndef OPENSSL_NO_COMP_ALG |
1130 | case TLS_ST_SW_COMP_CERT: | |
1131 | *confunc = tls_construct_server_compressed_certificate; | |
1132 | *mt = SSL3_MT_COMPRESSED_CERTIFICATE; | |
1133 | break; | |
1134 | #endif | |
1135 | ||
2c5dfdc3 MC |
1136 | case TLS_ST_SW_CERT_VRFY: |
1137 | *confunc = tls_construct_cert_verify; | |
1138 | *mt = SSL3_MT_CERTIFICATE_VERIFY; | |
1139 | break; | |
1140 | ||
1141 | ||
4a01c59f | 1142 | case TLS_ST_SW_KEY_EXCH: |
6392fb8e MC |
1143 | *confunc = tls_construct_server_key_exchange; |
1144 | *mt = SSL3_MT_SERVER_KEY_EXCHANGE; | |
4a01c59f | 1145 | break; |
61ae935a | 1146 | |
4a01c59f | 1147 | case TLS_ST_SW_CERT_REQ: |
6392fb8e MC |
1148 | *confunc = tls_construct_certificate_request; |
1149 | *mt = SSL3_MT_CERTIFICATE_REQUEST; | |
4a01c59f | 1150 | break; |
61ae935a | 1151 | |
4a01c59f | 1152 | case TLS_ST_SW_SRVR_DONE: |
6392fb8e MC |
1153 | *confunc = tls_construct_server_done; |
1154 | *mt = SSL3_MT_SERVER_DONE; | |
4a01c59f | 1155 | break; |
61ae935a | 1156 | |
4a01c59f | 1157 | case TLS_ST_SW_SESSION_TICKET: |
6392fb8e MC |
1158 | *confunc = tls_construct_new_session_ticket; |
1159 | *mt = SSL3_MT_NEWSESSION_TICKET; | |
4a01c59f | 1160 | break; |
61ae935a | 1161 | |
4a01c59f | 1162 | case TLS_ST_SW_CERT_STATUS: |
6392fb8e MC |
1163 | *confunc = tls_construct_cert_status; |
1164 | *mt = SSL3_MT_CERTIFICATE_STATUS; | |
4a01c59f | 1165 | break; |
61ae935a | 1166 | |
4a01c59f | 1167 | case TLS_ST_SW_FINISHED: |
6392fb8e MC |
1168 | *confunc = tls_construct_finished; |
1169 | *mt = SSL3_MT_FINISHED; | |
4a01c59f | 1170 | break; |
e46f2334 | 1171 | |
f7e393be MC |
1172 | case TLS_ST_EARLY_DATA: |
1173 | *confunc = NULL; | |
1174 | *mt = SSL3_MT_DUMMY; | |
1175 | break; | |
1176 | ||
e46f2334 MC |
1177 | case TLS_ST_SW_ENCRYPTED_EXTENSIONS: |
1178 | *confunc = tls_construct_encrypted_extensions; | |
1179 | *mt = SSL3_MT_ENCRYPTED_EXTENSIONS; | |
1180 | break; | |
7d061fce | 1181 | |
44c04a2e MC |
1182 | case TLS_ST_SW_KEY_UPDATE: |
1183 | *confunc = tls_construct_key_update; | |
1184 | *mt = SSL3_MT_KEY_UPDATE; | |
1185 | break; | |
4a01c59f | 1186 | } |
61ae935a | 1187 | |
5923ad4b | 1188 | return 1; |
61ae935a MC |
1189 | } |
1190 | ||
8a18bc25 AG |
1191 | /* |
1192 | * Maximum size (excluding the Handshake header) of a ClientHello message, | |
1193 | * calculated as follows: | |
1194 | * | |
1195 | * 2 + # client_version | |
1196 | * 32 + # only valid length for random | |
1197 | * 1 + # length of session_id | |
1198 | * 32 + # maximum size for session_id | |
1199 | * 2 + # length of cipher suites | |
1200 | * 2^16-2 + # maximum length of cipher suites array | |
1201 | * 1 + # length of compression_methods | |
1202 | * 2^8-1 + # maximum length of compression methods | |
1203 | * 2 + # length of extensions | |
1204 | * 2^16-1 # maximum length of extensions | |
1205 | */ | |
1206 | #define CLIENT_HELLO_MAX_LENGTH 131396 | |
1207 | ||
61ae935a MC |
1208 | #define CLIENT_KEY_EXCH_MAX_LENGTH 2048 |
1209 | #define NEXT_PROTO_MAX_LENGTH 514 | |
1210 | ||
1211 | /* | |
1212 | * Returns the maximum allowed length for the current message that we are | |
1213 | * reading. Excludes the message header. | |
1214 | */ | |
38b051a1 | 1215 | size_t ossl_statem_server_max_message_size(SSL_CONNECTION *s) |
61ae935a | 1216 | { |
d6f1a6e9 | 1217 | OSSL_STATEM *st = &s->statem; |
61ae935a | 1218 | |
e8aa8b6c | 1219 | switch (st->hand_state) { |
f3b3d7f0 RS |
1220 | default: |
1221 | /* Shouldn't happen */ | |
1222 | return 0; | |
1223 | ||
61ae935a | 1224 | case TLS_ST_SR_CLNT_HELLO: |
8a18bc25 | 1225 | return CLIENT_HELLO_MAX_LENGTH; |
61ae935a | 1226 | |
ef6c191b MC |
1227 | case TLS_ST_SR_END_OF_EARLY_DATA: |
1228 | return END_OF_EARLY_DATA_MAX_LENGTH; | |
1229 | ||
b67cb09f | 1230 | case TLS_ST_SR_COMP_CERT: |
61ae935a MC |
1231 | case TLS_ST_SR_CERT: |
1232 | return s->max_cert_list; | |
1233 | ||
1234 | case TLS_ST_SR_KEY_EXCH: | |
1235 | return CLIENT_KEY_EXCH_MAX_LENGTH; | |
1236 | ||
1237 | case TLS_ST_SR_CERT_VRFY: | |
c6d14bfd | 1238 | return CERTIFICATE_VERIFY_MAX_LENGTH; |
61ae935a MC |
1239 | |
1240 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
1241 | case TLS_ST_SR_NEXT_PROTO: | |
1242 | return NEXT_PROTO_MAX_LENGTH; | |
1243 | #endif | |
1244 | ||
1245 | case TLS_ST_SR_CHANGE: | |
1246 | return CCS_MAX_LENGTH; | |
1247 | ||
1248 | case TLS_ST_SR_FINISHED: | |
1249 | return FINISHED_MAX_LENGTH; | |
8cdc8c51 MC |
1250 | |
1251 | case TLS_ST_SR_KEY_UPDATE: | |
1252 | return KEY_UPDATE_MAX_LENGTH; | |
61ae935a | 1253 | } |
61ae935a MC |
1254 | } |
1255 | ||
1256 | /* | |
1257 | * Process a message that the server has received from the client. | |
1258 | */ | |
38b051a1 TM |
1259 | MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL_CONNECTION *s, |
1260 | PACKET *pkt) | |
61ae935a | 1261 | { |
d6f1a6e9 | 1262 | OSSL_STATEM *st = &s->statem; |
61ae935a | 1263 | |
e8aa8b6c | 1264 | switch (st->hand_state) { |
f3b3d7f0 RS |
1265 | default: |
1266 | /* Shouldn't happen */ | |
c48ffbcc | 1267 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f3b3d7f0 RS |
1268 | return MSG_PROCESS_ERROR; |
1269 | ||
61ae935a MC |
1270 | case TLS_ST_SR_CLNT_HELLO: |
1271 | return tls_process_client_hello(s, pkt); | |
1272 | ||
ef6c191b MC |
1273 | case TLS_ST_SR_END_OF_EARLY_DATA: |
1274 | return tls_process_end_of_early_data(s, pkt); | |
1275 | ||
61ae935a MC |
1276 | case TLS_ST_SR_CERT: |
1277 | return tls_process_client_certificate(s, pkt); | |
1278 | ||
b67cb09f TS |
1279 | #ifndef OPENSSL_NO_COMP_ALG |
1280 | case TLS_ST_SR_COMP_CERT: | |
1281 | return tls_process_client_compressed_certificate(s, pkt); | |
1282 | #endif | |
1283 | ||
61ae935a MC |
1284 | case TLS_ST_SR_KEY_EXCH: |
1285 | return tls_process_client_key_exchange(s, pkt); | |
1286 | ||
1287 | case TLS_ST_SR_CERT_VRFY: | |
1288 | return tls_process_cert_verify(s, pkt); | |
1289 | ||
1290 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
1291 | case TLS_ST_SR_NEXT_PROTO: | |
1292 | return tls_process_next_proto(s, pkt); | |
1293 | #endif | |
1294 | ||
1295 | case TLS_ST_SR_CHANGE: | |
1296 | return tls_process_change_cipher_spec(s, pkt); | |
1297 | ||
1298 | case TLS_ST_SR_FINISHED: | |
1299 | return tls_process_finished(s, pkt); | |
8cdc8c51 MC |
1300 | |
1301 | case TLS_ST_SR_KEY_UPDATE: | |
1302 | return tls_process_key_update(s, pkt); | |
1303 | ||
61ae935a | 1304 | } |
61ae935a MC |
1305 | } |
1306 | ||
1307 | /* | |
1308 | * Perform any further processing required following the receipt of a message | |
1309 | * from the client | |
1310 | */ | |
38b051a1 TM |
1311 | WORK_STATE ossl_statem_server_post_process_message(SSL_CONNECTION *s, |
1312 | WORK_STATE wst) | |
61ae935a | 1313 | { |
d6f1a6e9 | 1314 | OSSL_STATEM *st = &s->statem; |
61ae935a | 1315 | |
e8aa8b6c | 1316 | switch (st->hand_state) { |
f3b3d7f0 RS |
1317 | default: |
1318 | /* Shouldn't happen */ | |
c48ffbcc | 1319 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f3b3d7f0 RS |
1320 | return WORK_ERROR; |
1321 | ||
61ae935a MC |
1322 | case TLS_ST_SR_CLNT_HELLO: |
1323 | return tls_post_process_client_hello(s, wst); | |
1324 | ||
1325 | case TLS_ST_SR_KEY_EXCH: | |
1326 | return tls_post_process_client_key_exchange(s, wst); | |
61ae935a | 1327 | } |
61ae935a MC |
1328 | } |
1329 | ||
edc032b5 | 1330 | #ifndef OPENSSL_NO_SRP |
29bfd5b7 | 1331 | /* Returns 1 on success, 0 for retryable error, -1 for fatal error */ |
38b051a1 | 1332 | static int ssl_check_srp_ext_ClientHello(SSL_CONNECTION *s) |
0f113f3e | 1333 | { |
29bfd5b7 MC |
1334 | int ret; |
1335 | int al = SSL_AD_UNRECOGNIZED_NAME; | |
0f113f3e | 1336 | |
555cbb32 | 1337 | if ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) && |
0f113f3e MC |
1338 | (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) { |
1339 | if (s->srp_ctx.login == NULL) { | |
1340 | /* | |
1341 | * RFC 5054 says SHOULD reject, we do so if There is no srp | |
1342 | * login name | |
1343 | */ | |
29bfd5b7 | 1344 | SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY, |
29bfd5b7 MC |
1345 | SSL_R_PSK_IDENTITY_NOT_FOUND); |
1346 | return -1; | |
0f113f3e | 1347 | } else { |
76cb077f | 1348 | ret = ssl_srp_server_param_with_username_intern(s, &al); |
29bfd5b7 MC |
1349 | if (ret < 0) |
1350 | return 0; | |
1351 | if (ret == SSL3_AL_FATAL) { | |
c48ffbcc | 1352 | SSLfatal(s, al, |
29bfd5b7 MC |
1353 | al == SSL_AD_UNKNOWN_PSK_IDENTITY |
1354 | ? SSL_R_PSK_IDENTITY_NOT_FOUND | |
1355 | : SSL_R_CLIENTHELLO_TLSEXT); | |
1356 | return -1; | |
1357 | } | |
0f113f3e MC |
1358 | } |
1359 | } | |
29bfd5b7 | 1360 | return 1; |
0f113f3e | 1361 | } |
edc032b5 BL |
1362 | #endif |
1363 | ||
c536b6be | 1364 | int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie, |
cb150cbc | 1365 | size_t cookie_len) |
8ba708e5 | 1366 | { |
8ba708e5 | 1367 | /* Always use DTLS 1.0 version: see RFC 6347 */ |
c536b6be MC |
1368 | if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION) |
1369 | || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len)) | |
1370 | return 0; | |
8ba708e5 | 1371 | |
c536b6be | 1372 | return 1; |
8ba708e5 MC |
1373 | } |
1374 | ||
67ec6d2b MC |
1375 | CON_FUNC_RETURN dtls_construct_hello_verify_request(SSL_CONNECTION *s, |
1376 | WPACKET *pkt) | |
8ba708e5 | 1377 | { |
cb150cbc | 1378 | unsigned int cookie_leni; |
38b051a1 TM |
1379 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1380 | ||
1381 | if (sctx->app_gen_cookie_cb == NULL | |
1382 | || sctx->app_gen_cookie_cb(SSL_CONNECTION_GET_SSL(s), s->d1->cookie, | |
1383 | &cookie_leni) == 0 | |
1384 | || cookie_leni > DTLS1_COOKIE_LENGTH) { | |
c48ffbcc | 1385 | SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_COOKIE_GEN_CALLBACK_FAILURE); |
67ec6d2b | 1386 | return CON_FUNC_ERROR; |
8ba708e5 | 1387 | } |
cb150cbc | 1388 | s->d1->cookie_len = cookie_leni; |
8ba708e5 | 1389 | |
4a01c59f | 1390 | if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie, |
38b051a1 | 1391 | s->d1->cookie_len)) { |
c48ffbcc | 1392 | SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 1393 | return CON_FUNC_ERROR; |
c536b6be | 1394 | } |
8ba708e5 | 1395 | |
67ec6d2b | 1396 | return CON_FUNC_SUCCESS; |
8ba708e5 MC |
1397 | } |
1398 | ||
805a2e9e MC |
1399 | /*- |
1400 | * ssl_check_for_safari attempts to fingerprint Safari using OS X | |
1401 | * SecureTransport using the TLS extension block in |hello|. | |
1402 | * Safari, since 10.6, sends exactly these extensions, in this order: | |
1403 | * SNI, | |
1404 | * elliptic_curves | |
1405 | * ec_point_formats | |
33564cb7 | 1406 | * signature_algorithms (for TLSv1.2 only) |
805a2e9e MC |
1407 | * |
1408 | * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8, | |
1409 | * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them. | |
1410 | * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from | |
1411 | * 10.8..10.8.3 (which don't work). | |
1412 | */ | |
38b051a1 TM |
1413 | static void ssl_check_for_safari(SSL_CONNECTION *s, |
1414 | const CLIENTHELLO_MSG *hello) | |
805a2e9e | 1415 | { |
805a2e9e MC |
1416 | static const unsigned char kSafariExtensionsBlock[] = { |
1417 | 0x00, 0x0a, /* elliptic_curves extension */ | |
1418 | 0x00, 0x08, /* 8 bytes */ | |
1419 | 0x00, 0x06, /* 6 bytes of curve ids */ | |
1420 | 0x00, 0x17, /* P-256 */ | |
1421 | 0x00, 0x18, /* P-384 */ | |
1422 | 0x00, 0x19, /* P-521 */ | |
1423 | ||
1424 | 0x00, 0x0b, /* ec_point_formats */ | |
1425 | 0x00, 0x02, /* 2 bytes */ | |
1426 | 0x01, /* 1 point format */ | |
1427 | 0x00, /* uncompressed */ | |
1428 | /* The following is only present in TLS 1.2 */ | |
1429 | 0x00, 0x0d, /* signature_algorithms */ | |
1430 | 0x00, 0x0c, /* 12 bytes */ | |
1431 | 0x00, 0x0a, /* 10 bytes */ | |
1432 | 0x05, 0x01, /* SHA-384/RSA */ | |
1433 | 0x04, 0x01, /* SHA-256/RSA */ | |
1434 | 0x02, 0x01, /* SHA-1/RSA */ | |
1435 | 0x04, 0x03, /* SHA-256/ECDSA */ | |
1436 | 0x02, 0x03, /* SHA-1/ECDSA */ | |
1437 | }; | |
805a2e9e MC |
1438 | /* Length of the common prefix (first two extensions). */ |
1439 | static const size_t kSafariCommonExtensionsLength = 18; | |
1266eefd MC |
1440 | unsigned int type; |
1441 | PACKET sni, tmppkt; | |
1442 | size_t ext_len; | |
805a2e9e MC |
1443 | |
1444 | tmppkt = hello->extensions; | |
1445 | ||
1446 | if (!PACKET_forward(&tmppkt, 2) | |
1447 | || !PACKET_get_net_2(&tmppkt, &type) | |
1448 | || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) { | |
1449 | return; | |
6b473aca MC |
1450 | } |
1451 | ||
805a2e9e MC |
1452 | if (type != TLSEXT_TYPE_server_name) |
1453 | return; | |
1454 | ||
38b051a1 TM |
1455 | ext_len = TLS1_get_client_version( |
1456 | SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION ? | |
1457 | sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength; | |
805a2e9e | 1458 | |
555cbb32 | 1459 | s->s3.is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock, |
805a2e9e | 1460 | ext_len); |
6b473aca MC |
1461 | } |
1462 | ||
55373bfd RS |
1463 | #define RENEG_OPTIONS_OK(options) \ |
1464 | ((options & SSL_OP_NO_RENEGOTIATION) == 0 \ | |
1465 | && (options & SSL_OP_ALLOW_CLIENT_RENEGOTIATION) != 0) | |
1466 | ||
38b051a1 | 1467 | MSG_PROCESS_RETURN tls_process_client_hello(SSL_CONNECTION *s, PACKET *pkt) |
e27f234a | 1468 | { |
e27f234a | 1469 | /* |cookie| will only be initialized for DTLS. */ |
1ab3836b | 1470 | PACKET session_id, compression, extensions, cookie; |
6e3ff632 | 1471 | static const unsigned char null_compression = 0; |
3faa07b5 | 1472 | CLIENTHELLO_MSG *clienthello = NULL; |
e27f234a | 1473 | |
c7f47786 MC |
1474 | /* Check if this is actually an unexpected renegotiation ClientHello */ |
1475 | if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) { | |
38b051a1 | 1476 | if (!ossl_assert(!SSL_CONNECTION_IS_TLS13(s))) { |
c48ffbcc | 1477 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
db0f35dd TS |
1478 | goto err; |
1479 | } | |
55373bfd | 1480 | if (!RENEG_OPTIONS_OK(s->options) |
555cbb32 | 1481 | || (!s->s3.send_connection_binding |
3faa07b5 MC |
1482 | && (s->options |
1483 | & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) == 0)) { | |
1484 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION); | |
1485 | return MSG_PROCESS_FINISHED_READING; | |
1486 | } | |
c7f47786 MC |
1487 | s->renegotiate = 1; |
1488 | s->new_session = 1; | |
1489 | } | |
1490 | ||
3faa07b5 MC |
1491 | clienthello = OPENSSL_zalloc(sizeof(*clienthello)); |
1492 | if (clienthello == NULL) { | |
c48ffbcc | 1493 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3faa07b5 MC |
1494 | goto err; |
1495 | } | |
1496 | ||
1ab3836b | 1497 | /* |
b1b4b543 | 1498 | * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure. |
1ab3836b | 1499 | */ |
6b1bb98f | 1500 | clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer); |
bbafa47b | 1501 | PACKET_null_init(&cookie); |
1ab3836b | 1502 | |
6b1bb98f | 1503 | if (clienthello->isv2) { |
9ceb2426 | 1504 | unsigned int mt; |
b1b4b543 | 1505 | |
fc7129dc MC |
1506 | if (!SSL_IS_FIRST_HANDSHAKE(s) |
1507 | || s->hello_retry_request != SSL_HRR_NONE) { | |
c48ffbcc | 1508 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
f63a17d6 | 1509 | goto err; |
7d061fce MC |
1510 | } |
1511 | ||
32ec4153 MC |
1512 | /*- |
1513 | * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2 | |
1514 | * header is sent directly on the wire, not wrapped as a TLS | |
1515 | * record. Our record layer just processes the message length and passes | |
1516 | * the rest right through. Its format is: | |
1517 | * Byte Content | |
1518 | * 0-1 msg_length - decoded by the record layer | |
1519 | * 2 msg_type - s->init_msg points here | |
1520 | * 3-4 version | |
1521 | * 5-6 cipher_spec_length | |
1522 | * 7-8 session_id_length | |
1523 | * 9-10 challenge_length | |
1524 | * ... ... | |
1525 | */ | |
1526 | ||
73999b62 | 1527 | if (!PACKET_get_1(pkt, &mt) |
a230b26e | 1528 | || mt != SSL2_MT_CLIENT_HELLO) { |
32ec4153 MC |
1529 | /* |
1530 | * Should never happen. We should have tested this in the record | |
1531 | * layer in order to have determined that this is a SSLv2 record | |
1532 | * in the first place | |
1533 | */ | |
c48ffbcc | 1534 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
d45ba43d | 1535 | goto err; |
32ec4153 | 1536 | } |
32ec4153 MC |
1537 | } |
1538 | ||
6b1bb98f | 1539 | if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) { |
c48ffbcc | 1540 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT); |
1ab3836b | 1541 | goto err; |
0f113f3e MC |
1542 | } |
1543 | ||
b3e2272c | 1544 | /* Parse the message and load client random. */ |
6b1bb98f | 1545 | if (clienthello->isv2) { |
32ec4153 MC |
1546 | /* |
1547 | * Handle an SSLv2 backwards compatible ClientHello | |
1548 | * Note, this is only for SSLv3+ using the backward compatible format. | |
e2994cf0 | 1549 | * Real SSLv2 is not supported, and is rejected below. |
32ec4153 | 1550 | */ |
1ab3836b | 1551 | unsigned int ciphersuite_len, session_id_len, challenge_len; |
b3e2272c | 1552 | PACKET challenge; |
0f113f3e | 1553 | |
1ab3836b | 1554 | if (!PACKET_get_net_2(pkt, &ciphersuite_len) |
a230b26e EK |
1555 | || !PACKET_get_net_2(pkt, &session_id_len) |
1556 | || !PACKET_get_net_2(pkt, &challenge_len)) { | |
c48ffbcc | 1557 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH); |
f63a17d6 | 1558 | goto err; |
5e9f0eeb | 1559 | } |
0f113f3e | 1560 | |
293b5ca4 | 1561 | if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
c48ffbcc | 1562 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 1563 | goto err; |
293b5ca4 AG |
1564 | } |
1565 | ||
6b1bb98f | 1566 | if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites, |
1ab3836b | 1567 | ciphersuite_len) |
6b1bb98f | 1568 | || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len) |
73999b62 | 1569 | || !PACKET_get_sub_packet(pkt, &challenge, challenge_len) |
b3e2272c | 1570 | /* No extensions. */ |
73999b62 | 1571 | || PACKET_remaining(pkt) != 0) { |
c48ffbcc | 1572 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH); |
f63a17d6 | 1573 | goto err; |
9ceb2426 | 1574 | } |
6b1bb98f | 1575 | clienthello->session_id_len = session_id_len; |
9ceb2426 | 1576 | |
fba7b84c | 1577 | /* Load the client random and compression list. We use SSL3_RANDOM_SIZE |
6b1bb98f | 1578 | * here rather than sizeof(clienthello->random) because that is the limit |
fba7b84c | 1579 | * for SSLv3 and it is fixed. It won't change even if |
6b1bb98f | 1580 | * sizeof(clienthello->random) does. |
fba7b84c MC |
1581 | */ |
1582 | challenge_len = challenge_len > SSL3_RANDOM_SIZE | |
1583 | ? SSL3_RANDOM_SIZE : challenge_len; | |
6b1bb98f | 1584 | memset(clienthello->random, 0, SSL3_RANDOM_SIZE); |
b3e2272c | 1585 | if (!PACKET_copy_bytes(&challenge, |
6b1bb98f | 1586 | clienthello->random + SSL3_RANDOM_SIZE - |
cb21df32 DB |
1587 | challenge_len, challenge_len) |
1588 | /* Advertise only null compression. */ | |
1589 | || !PACKET_buf_init(&compression, &null_compression, 1)) { | |
c48ffbcc | 1590 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 1591 | goto err; |
9ceb2426 | 1592 | } |
b3e2272c | 1593 | |
6b1bb98f | 1594 | PACKET_null_init(&clienthello->extensions); |
0f113f3e | 1595 | } else { |
b3e2272c | 1596 | /* Regular ClientHello. */ |
6b1bb98f | 1597 | if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE) |
e2994cf0 | 1598 | || !PACKET_get_length_prefixed_1(pkt, &session_id) |
6b1bb98f | 1599 | || !PACKET_copy_all(&session_id, clienthello->session_id, |
e2994cf0 | 1600 | SSL_MAX_SSL_SESSION_ID_LENGTH, |
6b1bb98f | 1601 | &clienthello->session_id_len)) { |
c48ffbcc | 1602 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 1603 | goto err; |
9ceb2426 | 1604 | } |
32ec4153 | 1605 | |
38b051a1 | 1606 | if (SSL_CONNECTION_IS_DTLS(s)) { |
73999b62 | 1607 | if (!PACKET_get_length_prefixed_1(pkt, &cookie)) { |
c48ffbcc | 1608 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 1609 | goto err; |
32ec4153 | 1610 | } |
6b1bb98f | 1611 | if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie, |
1ab3836b | 1612 | DTLS1_COOKIE_LENGTH, |
6b1bb98f | 1613 | &clienthello->dtls_cookie_len)) { |
c48ffbcc | 1614 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 1615 | goto err; |
1ab3836b | 1616 | } |
b3e2272c EK |
1617 | /* |
1618 | * If we require cookies and this ClientHello doesn't contain one, | |
1619 | * just return since we do not want to allocate any memory yet. | |
1620 | * So check cookie length... | |
1621 | */ | |
38b051a1 | 1622 | if (SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE) { |
01666a8c MC |
1623 | if (clienthello->dtls_cookie_len == 0) { |
1624 | OPENSSL_free(clienthello); | |
eb5fd03b | 1625 | return MSG_PROCESS_FINISHED_READING; |
01666a8c | 1626 | } |
b3e2272c | 1627 | } |
5e9f0eeb | 1628 | } |
0f113f3e | 1629 | |
6b1bb98f | 1630 | if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) { |
c48ffbcc | 1631 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 1632 | goto err; |
1ab3836b MC |
1633 | } |
1634 | ||
4bfe1432 | 1635 | if (!PACKET_get_length_prefixed_1(pkt, &compression)) { |
c48ffbcc | 1636 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 1637 | goto err; |
b3e2272c | 1638 | } |
1ab3836b | 1639 | |
b3e2272c | 1640 | /* Could be empty. */ |
1ab3836b | 1641 | if (PACKET_remaining(pkt) == 0) { |
6b1bb98f | 1642 | PACKET_null_init(&clienthello->extensions); |
1ab3836b | 1643 | } else { |
ef57a475 MC |
1644 | if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions) |
1645 | || PACKET_remaining(pkt) != 0) { | |
c48ffbcc | 1646 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 1647 | goto err; |
1ab3836b MC |
1648 | } |
1649 | } | |
1650 | } | |
1651 | ||
6b1bb98f | 1652 | if (!PACKET_copy_all(&compression, clienthello->compressions, |
e2994cf0 | 1653 | MAX_COMPRESSIONS_SIZE, |
6b1bb98f | 1654 | &clienthello->compressions_len)) { |
c48ffbcc | 1655 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 1656 | goto err; |
1ab3836b MC |
1657 | } |
1658 | ||
b1b4b543 | 1659 | /* Preserve the raw extensions PACKET for later use */ |
6b1bb98f | 1660 | extensions = clienthello->extensions; |
fe874d27 | 1661 | if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO, |
f63a17d6 | 1662 | &clienthello->pre_proc_exts, |
735d5b59 | 1663 | &clienthello->pre_proc_exts_len, 1)) { |
f63a17d6 MC |
1664 | /* SSLfatal already been called */ |
1665 | goto err; | |
1ab3836b | 1666 | } |
6b1bb98f | 1667 | s->clienthello = clienthello; |
1ab3836b | 1668 | |
6b1bb98f | 1669 | return MSG_PROCESS_CONTINUE_PROCESSING; |
6b1bb98f | 1670 | |
f63a17d6 | 1671 | err: |
fbaf2857 RS |
1672 | if (clienthello != NULL) |
1673 | OPENSSL_free(clienthello->pre_proc_exts); | |
6b1bb98f BK |
1674 | OPENSSL_free(clienthello); |
1675 | ||
1676 | return MSG_PROCESS_ERROR; | |
1677 | } | |
1678 | ||
38b051a1 | 1679 | static int tls_early_post_process_client_hello(SSL_CONNECTION *s) |
6b1bb98f BK |
1680 | { |
1681 | unsigned int j; | |
bf846a6d | 1682 | int i, al = SSL_AD_INTERNAL_ERROR; |
6b1bb98f BK |
1683 | int protverr; |
1684 | size_t loop; | |
1685 | unsigned long id; | |
1686 | #ifndef OPENSSL_NO_COMP | |
1687 | SSL_COMP *comp = NULL; | |
1688 | #endif | |
1689 | const SSL_CIPHER *c; | |
1690 | STACK_OF(SSL_CIPHER) *ciphers = NULL; | |
1691 | STACK_OF(SSL_CIPHER) *scsvs = NULL; | |
1692 | CLIENTHELLO_MSG *clienthello = s->clienthello; | |
f7f2a01d | 1693 | DOWNGRADE dgrd = DOWNGRADE_NONE; |
38b051a1 TM |
1694 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1695 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); | |
6b1bb98f | 1696 | |
1ab3836b | 1697 | /* Finished parsing the ClientHello, now we can start processing it */ |
a9c0d8be | 1698 | /* Give the ClientHello callback a crack at things */ |
38b051a1 | 1699 | if (sctx->client_hello_cb != NULL) { |
a9c0d8be | 1700 | /* A failure in the ClientHello callback terminates the connection. */ |
38b051a1 | 1701 | switch (sctx->client_hello_cb(ssl, &al, sctx->client_hello_cb_arg)) { |
f1b97da1 DB |
1702 | case SSL_CLIENT_HELLO_SUCCESS: |
1703 | break; | |
1704 | case SSL_CLIENT_HELLO_RETRY: | |
a9c0d8be | 1705 | s->rwstate = SSL_CLIENT_HELLO_CB; |
f1b97da1 DB |
1706 | return -1; |
1707 | case SSL_CLIENT_HELLO_ERROR: | |
1708 | default: | |
c48ffbcc | 1709 | SSLfatal(s, al, SSL_R_CALLBACK_FAILED); |
f1b97da1 | 1710 | goto err; |
6b1bb98f BK |
1711 | } |
1712 | } | |
1ab3836b MC |
1713 | |
1714 | /* Set up the client_random */ | |
555cbb32 | 1715 | memcpy(s->s3.client_random, clienthello->random, SSL3_RANDOM_SIZE); |
1ab3836b MC |
1716 | |
1717 | /* Choose the version */ | |
1718 | ||
6b1bb98f BK |
1719 | if (clienthello->isv2) { |
1720 | if (clienthello->legacy_version == SSL2_VERSION | |
1721 | || (clienthello->legacy_version & 0xff00) | |
b1b4b543 MC |
1722 | != (SSL3_VERSION_MAJOR << 8)) { |
1723 | /* | |
f63a17d6 | 1724 | * This is real SSLv2 or something completely unknown. We don't |
b1b4b543 MC |
1725 | * support it. |
1726 | */ | |
c48ffbcc | 1727 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNKNOWN_PROTOCOL); |
1ab3836b MC |
1728 | goto err; |
1729 | } | |
b1b4b543 | 1730 | /* SSLv3/TLS */ |
6b1bb98f | 1731 | s->client_version = clienthello->legacy_version; |
1ab3836b MC |
1732 | } |
1733 | /* | |
1734 | * Do SSL/TLS version negotiation if applicable. For DTLS we just check | |
1735 | * versions are potentially compatible. Version negotiation comes later. | |
1736 | */ | |
38b051a1 | 1737 | if (!SSL_CONNECTION_IS_DTLS(s)) { |
f7f2a01d | 1738 | protverr = ssl_choose_server_version(s, clienthello, &dgrd); |
38b051a1 | 1739 | } else if (ssl->method->version != DTLS_ANY_VERSION && |
6b1bb98f | 1740 | DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) { |
1ab3836b MC |
1741 | protverr = SSL_R_VERSION_TOO_LOW; |
1742 | } else { | |
1743 | protverr = 0; | |
1744 | } | |
1745 | ||
1746 | if (protverr) { | |
7d061fce | 1747 | if (SSL_IS_FIRST_HANDSHAKE(s)) { |
b1b4b543 | 1748 | /* like ssl3_get_record, send alert using remote version number */ |
6b1bb98f | 1749 | s->version = s->client_version = clienthello->legacy_version; |
1ab3836b | 1750 | } |
c48ffbcc | 1751 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr); |
6b1bb98f | 1752 | goto err; |
b3e2272c EK |
1753 | } |
1754 | ||
635b7d3f | 1755 | /* TLSv1.3 specifies that a ClientHello must end on a record boundary */ |
38b051a1 TM |
1756 | if (SSL_CONNECTION_IS_TLS13(s) |
1757 | && RECORD_LAYER_processed_read_pending(&s->rlayer)) { | |
c48ffbcc | 1758 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY); |
9e0ac6a2 MC |
1759 | goto err; |
1760 | } | |
1761 | ||
38b051a1 | 1762 | if (SSL_CONNECTION_IS_DTLS(s)) { |
1ed65871 | 1763 | /* Empty cookie was already handled above by returning early. */ |
38b051a1 TM |
1764 | if (SSL_get_options(ssl) & SSL_OP_COOKIE_EXCHANGE) { |
1765 | if (sctx->app_verify_cookie_cb != NULL) { | |
1766 | if (sctx->app_verify_cookie_cb(ssl, clienthello->dtls_cookie, | |
6b1bb98f | 1767 | clienthello->dtls_cookie_len) == 0) { |
f63a17d6 | 1768 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
f63a17d6 | 1769 | SSL_R_COOKIE_MISMATCH); |
6b1bb98f | 1770 | goto err; |
1ed65871 DB |
1771 | /* else cookie verification succeeded */ |
1772 | } | |
a230b26e | 1773 | /* default verification */ |
6b1bb98f BK |
1774 | } else if (s->d1->cookie_len != clienthello->dtls_cookie_len |
1775 | || memcmp(clienthello->dtls_cookie, s->d1->cookie, | |
1ab3836b | 1776 | s->d1->cookie_len) != 0) { |
c48ffbcc | 1777 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_COOKIE_MISMATCH); |
6b1bb98f | 1778 | goto err; |
1ed65871 DB |
1779 | } |
1780 | s->d1->cookie_verified = 1; | |
1781 | } | |
38b051a1 | 1782 | if (ssl->method->version == DTLS_ANY_VERSION) { |
f7f2a01d | 1783 | protverr = ssl_choose_server_version(s, clienthello, &dgrd); |
1ed65871 | 1784 | if (protverr != 0) { |
1ed65871 | 1785 | s->version = s->client_version; |
c48ffbcc | 1786 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr); |
6b1bb98f | 1787 | goto err; |
1ed65871 DB |
1788 | } |
1789 | } | |
1790 | } | |
1791 | ||
b3e2272c EK |
1792 | s->hit = 0; |
1793 | ||
0de6d66d | 1794 | if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites, |
f63a17d6 | 1795 | clienthello->isv2) || |
38b051a1 TM |
1796 | !ossl_bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, |
1797 | &scsvs, clienthello->isv2, 1)) { | |
f63a17d6 | 1798 | /* SSLfatal() already called */ |
0de6d66d MC |
1799 | goto err; |
1800 | } | |
1801 | ||
555cbb32 | 1802 | s->s3.send_connection_binding = 0; |
0de6d66d MC |
1803 | /* Check what signalling cipher-suite values were received. */ |
1804 | if (scsvs != NULL) { | |
1287dabd | 1805 | for (i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) { |
0de6d66d MC |
1806 | c = sk_SSL_CIPHER_value(scsvs, i); |
1807 | if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) { | |
1808 | if (s->renegotiate) { | |
1809 | /* SCSV is fatal if renegotiating */ | |
f63a17d6 | 1810 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
f63a17d6 | 1811 | SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING); |
0de6d66d MC |
1812 | goto err; |
1813 | } | |
555cbb32 | 1814 | s->s3.send_connection_binding = 1; |
0de6d66d MC |
1815 | } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV && |
1816 | !ssl_check_version_downgrade(s)) { | |
1817 | /* | |
1818 | * This SCSV indicates that the client previously tried | |
1819 | * a higher version. We should fail if the current version | |
1820 | * is an unexpected downgrade, as that indicates that the first | |
1821 | * connection may have been tampered with in order to trigger | |
1822 | * an insecure downgrade. | |
1823 | */ | |
f63a17d6 | 1824 | SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK, |
f63a17d6 | 1825 | SSL_R_INAPPROPRIATE_FALLBACK); |
0de6d66d MC |
1826 | goto err; |
1827 | } | |
1828 | } | |
1829 | } | |
1830 | ||
1831 | /* For TLSv1.3 we must select the ciphersuite *before* session resumption */ | |
38b051a1 | 1832 | if (SSL_CONNECTION_IS_TLS13(s)) { |
0de6d66d | 1833 | const SSL_CIPHER *cipher = |
38b051a1 | 1834 | ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(ssl)); |
0de6d66d MC |
1835 | |
1836 | if (cipher == NULL) { | |
c48ffbcc | 1837 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER); |
0de6d66d MC |
1838 | goto err; |
1839 | } | |
fc7129dc | 1840 | if (s->hello_retry_request == SSL_HRR_PENDING |
555cbb32 TS |
1841 | && (s->s3.tmp.new_cipher == NULL |
1842 | || s->s3.tmp.new_cipher->id != cipher->id)) { | |
0de6d66d MC |
1843 | /* |
1844 | * A previous HRR picked a different ciphersuite to the one we | |
1845 | * just selected. Something must have changed. | |
1846 | */ | |
c48ffbcc | 1847 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER); |
0de6d66d MC |
1848 | goto err; |
1849 | } | |
555cbb32 | 1850 | s->s3.tmp.new_cipher = cipher; |
0de6d66d MC |
1851 | } |
1852 | ||
1ab3836b | 1853 | /* We need to do this before getting the session */ |
70af3d8e | 1854 | if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret, |
fe874d27 | 1855 | SSL_EXT_CLIENT_HELLO, |
f63a17d6 MC |
1856 | clienthello->pre_proc_exts, NULL, 0)) { |
1857 | /* SSLfatal() already called */ | |
6b1bb98f | 1858 | goto err; |
1ab3836b MC |
1859 | } |
1860 | ||
b3e2272c EK |
1861 | /* |
1862 | * We don't allow resumption in a backwards compatible ClientHello. | |
407820c0 | 1863 | * In TLS1.1+, session_id MUST be empty. |
b3e2272c EK |
1864 | * |
1865 | * Versions before 0.9.7 always allow clients to resume sessions in | |
1866 | * renegotiation. 0.9.7 and later allow this by default, but optionally | |
1867 | * ignore resumption requests with flag | |
1868 | * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather | |
1869 | * than a change to default behavior so that applications relying on | |
1870 | * this for security won't even compile against older library versions). | |
1871 | * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to | |
1872 | * request renegotiation but not a new session (s->new_session remains | |
1873 | * unset): for servers, this essentially just means that the | |
1874 | * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be | |
1875 | * ignored. | |
1876 | */ | |
6b1bb98f | 1877 | if (clienthello->isv2 || |
b3e2272c EK |
1878 | (s->new_session && |
1879 | (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) { | |
f63a17d6 MC |
1880 | if (!ssl_get_new_session(s, 1)) { |
1881 | /* SSLfatal() already called */ | |
b3e2272c | 1882 | goto err; |
f63a17d6 | 1883 | } |
b3e2272c | 1884 | } else { |
f63a17d6 | 1885 | i = ssl_get_prev_session(s, clienthello); |
128ae276 | 1886 | if (i == 1) { |
b3e2272c EK |
1887 | /* previous session */ |
1888 | s->hit = 1; | |
1889 | } else if (i == -1) { | |
f63a17d6 | 1890 | /* SSLfatal() already called */ |
6b1bb98f | 1891 | goto err; |
32ec4153 | 1892 | } else { |
b3e2272c | 1893 | /* i == 0 */ |
f63a17d6 MC |
1894 | if (!ssl_get_new_session(s, 1)) { |
1895 | /* SSLfatal() already called */ | |
32ec4153 | 1896 | goto err; |
f63a17d6 | 1897 | } |
0f113f3e | 1898 | } |
b3e2272c | 1899 | } |
0f113f3e | 1900 | |
38b051a1 | 1901 | if (SSL_CONNECTION_IS_TLS13(s)) { |
a5816a5a MC |
1902 | memcpy(s->tmp_session_id, s->clienthello->session_id, |
1903 | s->clienthello->session_id_len); | |
1904 | s->tmp_session_id_len = s->clienthello->session_id_len; | |
1905 | } | |
1906 | ||
a055a881 | 1907 | /* |
0de6d66d MC |
1908 | * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check |
1909 | * ciphersuite compatibility with the session as part of resumption. | |
a055a881 | 1910 | */ |
38b051a1 | 1911 | if (!SSL_CONNECTION_IS_TLS13(s) && s->hit) { |
b3e2272c EK |
1912 | j = 0; |
1913 | id = s->session->cipher->id; | |
d02b48c6 | 1914 | |
77359d22 RL |
1915 | OSSL_TRACE_BEGIN(TLS_CIPHER) { |
1916 | BIO_printf(trc_out, "client sent %d ciphers\n", | |
1917 | sk_SSL_CIPHER_num(ciphers)); | |
1918 | } | |
b3e2272c EK |
1919 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
1920 | c = sk_SSL_CIPHER_value(ciphers, i); | |
77359d22 RL |
1921 | if (trc_out != NULL) |
1922 | BIO_printf(trc_out, "client [%2d of %2d]:%s\n", i, | |
1923 | sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c)); | |
b3e2272c EK |
1924 | if (c->id == id) { |
1925 | j = 1; | |
1926 | break; | |
32ec4153 | 1927 | } |
0f113f3e | 1928 | } |
b3e2272c | 1929 | if (j == 0) { |
ec30e856 | 1930 | /* |
b3e2272c EK |
1931 | * we need to have the cipher in the cipher list if we are asked |
1932 | * to reuse it | |
ec30e856 | 1933 | */ |
f63a17d6 | 1934 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
f63a17d6 | 1935 | SSL_R_REQUIRED_CIPHER_MISSING); |
77359d22 | 1936 | OSSL_TRACE_CANCEL(TLS_CIPHER); |
6b1bb98f | 1937 | goto err; |
32ec4153 | 1938 | } |
77359d22 | 1939 | OSSL_TRACE_END(TLS_CIPHER); |
b3e2272c | 1940 | } |
9ceb2426 | 1941 | |
6b1bb98f BK |
1942 | for (loop = 0; loop < clienthello->compressions_len; loop++) { |
1943 | if (clienthello->compressions[loop] == 0) | |
b3e2272c | 1944 | break; |
0f113f3e | 1945 | } |
32ec4153 | 1946 | |
6b1bb98f | 1947 | if (loop >= clienthello->compressions_len) { |
b3e2272c | 1948 | /* no compress */ |
c48ffbcc | 1949 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_COMPRESSION_SPECIFIED); |
6b1bb98f | 1950 | goto err; |
b3e2272c | 1951 | } |
f100b031 | 1952 | |
805a2e9e | 1953 | if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG) |
6b1bb98f | 1954 | ssl_check_for_safari(s, clienthello); |
805a2e9e | 1955 | |
0f113f3e | 1956 | /* TLS extensions */ |
fe874d27 | 1957 | if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO, |
f63a17d6 MC |
1958 | clienthello->pre_proc_exts, NULL, 0, 1)) { |
1959 | /* SSLfatal() already called */ | |
6b1bb98f | 1960 | goto err; |
0f113f3e MC |
1961 | } |
1962 | ||
1963 | /* | |
1964 | * Check if we want to use external pre-shared secret for this handshake | |
1965 | * for not reused session only. We need to generate server_random before | |
1966 | * calling tls_session_secret_cb in order to allow SessionTicket | |
1967 | * processing to use it in key derivation. | |
1968 | */ | |
1969 | { | |
1970 | unsigned char *pos; | |
555cbb32 | 1971 | pos = s->s3.server_random; |
f7f2a01d | 1972 | if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) { |
c48ffbcc | 1973 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6b1bb98f | 1974 | goto err; |
0f113f3e MC |
1975 | } |
1976 | } | |
1977 | ||
0de6d66d MC |
1978 | if (!s->hit |
1979 | && s->version >= TLS1_VERSION | |
38b051a1 TM |
1980 | && !SSL_CONNECTION_IS_TLS13(s) |
1981 | && !SSL_CONNECTION_IS_DTLS(s) | |
1982 | && s->ext.session_secret_cb != NULL) { | |
4a640fb6 | 1983 | const SSL_CIPHER *pref_cipher = NULL; |
8c1a5343 MC |
1984 | /* |
1985 | * s->session->master_key_length is a size_t, but this is an int for | |
1986 | * backwards compat reasons | |
1987 | */ | |
1988 | int master_key_length; | |
0f113f3e | 1989 | |
8c1a5343 | 1990 | master_key_length = sizeof(s->session->master_key); |
38b051a1 | 1991 | if (s->ext.session_secret_cb(ssl, s->session->master_key, |
8c1a5343 | 1992 | &master_key_length, ciphers, |
0f113f3e | 1993 | &pref_cipher, |
aff8c126 | 1994 | s->ext.session_secret_cb_arg) |
8c1a5343 MC |
1995 | && master_key_length > 0) { |
1996 | s->session->master_key_length = master_key_length; | |
0f113f3e | 1997 | s->hit = 1; |
eee2a6a7 | 1998 | s->peer_ciphers = ciphers; |
0f113f3e MC |
1999 | s->session->verify_result = X509_V_OK; |
2000 | ||
2001 | ciphers = NULL; | |
2002 | ||
2003 | /* check if some cipher was preferred by call back */ | |
3f4bf115 | 2004 | if (pref_cipher == NULL) |
eee2a6a7 | 2005 | pref_cipher = ssl3_choose_cipher(s, s->peer_ciphers, |
38b051a1 | 2006 | SSL_get_ciphers(ssl)); |
0f113f3e | 2007 | if (pref_cipher == NULL) { |
c48ffbcc | 2008 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER); |
6b1bb98f | 2009 | goto err; |
0f113f3e MC |
2010 | } |
2011 | ||
2012 | s->session->cipher = pref_cipher; | |
25aaa98a | 2013 | sk_SSL_CIPHER_free(s->cipher_list); |
eee2a6a7 | 2014 | s->cipher_list = sk_SSL_CIPHER_dup(s->peer_ciphers); |
25aaa98a | 2015 | sk_SSL_CIPHER_free(s->cipher_list_by_id); |
eee2a6a7 | 2016 | s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->peer_ciphers); |
0f113f3e MC |
2017 | } |
2018 | } | |
58ece833 | 2019 | |
0f113f3e MC |
2020 | /* |
2021 | * Worst case, we will use the NULL compression, but if we have other | |
b2ce0337 | 2022 | * options, we will now look for them. We have complen-1 compression |
0f113f3e MC |
2023 | * algorithms from the client, starting at q. |
2024 | */ | |
555cbb32 | 2025 | s->s3.tmp.new_compression = NULL; |
38b051a1 | 2026 | if (SSL_CONNECTION_IS_TLS13(s)) { |
1fe35494 MC |
2027 | /* |
2028 | * We already checked above that the NULL compression method appears in | |
2029 | * the list. Now we check there aren't any others (which is illegal in | |
2030 | * a TLSv1.3 ClientHello. | |
2031 | */ | |
2032 | if (clienthello->compressions_len != 1) { | |
f63a17d6 | 2033 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
f63a17d6 | 2034 | SSL_R_INVALID_COMPRESSION_ALGORITHM); |
1fe35494 MC |
2035 | goto err; |
2036 | } | |
2037 | } | |
09b6c2ef | 2038 | #ifndef OPENSSL_NO_COMP |
0f113f3e | 2039 | /* This only happens if we have a cache hit */ |
1fe35494 | 2040 | else if (s->session->compress_meth != 0) { |
0f113f3e | 2041 | int m, comp_id = s->session->compress_meth; |
9ceb2426 | 2042 | unsigned int k; |
0f113f3e MC |
2043 | /* Perform sanity checks on resumed compression algorithm */ |
2044 | /* Can't disable compression */ | |
2045 | if (!ssl_allow_compression(s)) { | |
f63a17d6 | 2046 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
f63a17d6 | 2047 | SSL_R_INCONSISTENT_COMPRESSION); |
6b1bb98f | 2048 | goto err; |
0f113f3e MC |
2049 | } |
2050 | /* Look for resumed compression method */ | |
38b051a1 TM |
2051 | for (m = 0; m < sk_SSL_COMP_num(sctx->comp_methods); m++) { |
2052 | comp = sk_SSL_COMP_value(sctx->comp_methods, m); | |
0f113f3e | 2053 | if (comp_id == comp->id) { |
555cbb32 | 2054 | s->s3.tmp.new_compression = comp; |
0f113f3e MC |
2055 | break; |
2056 | } | |
2057 | } | |
555cbb32 | 2058 | if (s->s3.tmp.new_compression == NULL) { |
f63a17d6 | 2059 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
f63a17d6 | 2060 | SSL_R_INVALID_COMPRESSION_ALGORITHM); |
6b1bb98f | 2061 | goto err; |
0f113f3e MC |
2062 | } |
2063 | /* Look for resumed method in compression list */ | |
6b1bb98f BK |
2064 | for (k = 0; k < clienthello->compressions_len; k++) { |
2065 | if (clienthello->compressions[k] == comp_id) | |
0f113f3e MC |
2066 | break; |
2067 | } | |
6b1bb98f | 2068 | if (k >= clienthello->compressions_len) { |
f63a17d6 | 2069 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
f63a17d6 | 2070 | SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING); |
6b1bb98f | 2071 | goto err; |
0f113f3e | 2072 | } |
c19602b5 | 2073 | } else if (s->hit) { |
0f113f3e | 2074 | comp = NULL; |
38b051a1 | 2075 | } else if (ssl_allow_compression(s) && sctx->comp_methods) { |
df6741c9 | 2076 | /* See if we have a match */ |
9ceb2426 MC |
2077 | int m, nn, v, done = 0; |
2078 | unsigned int o; | |
0f113f3e | 2079 | |
38b051a1 | 2080 | nn = sk_SSL_COMP_num(sctx->comp_methods); |
0f113f3e | 2081 | for (m = 0; m < nn; m++) { |
38b051a1 | 2082 | comp = sk_SSL_COMP_value(sctx->comp_methods, m); |
0f113f3e | 2083 | v = comp->id; |
6b1bb98f BK |
2084 | for (o = 0; o < clienthello->compressions_len; o++) { |
2085 | if (v == clienthello->compressions[o]) { | |
0f113f3e MC |
2086 | done = 1; |
2087 | break; | |
2088 | } | |
2089 | } | |
2090 | if (done) | |
2091 | break; | |
2092 | } | |
2093 | if (done) | |
555cbb32 | 2094 | s->s3.tmp.new_compression = comp; |
0f113f3e MC |
2095 | else |
2096 | comp = NULL; | |
2097 | } | |
e6f418bc | 2098 | #else |
0f113f3e MC |
2099 | /* |
2100 | * If compression is disabled we'd better not try to resume a session | |
2101 | * using compression. | |
2102 | */ | |
2103 | if (s->session->compress_meth != 0) { | |
c48ffbcc | 2104 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION); |
6b1bb98f | 2105 | goto err; |
0f113f3e | 2106 | } |
09b6c2ef | 2107 | #endif |
413c4f45 | 2108 | |
0f113f3e | 2109 | /* |
eee2a6a7 | 2110 | * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher |
0f113f3e | 2111 | */ |
d02b48c6 | 2112 | |
38b051a1 | 2113 | if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) { |
eee2a6a7 MC |
2114 | sk_SSL_CIPHER_free(s->peer_ciphers); |
2115 | s->peer_ciphers = ciphers; | |
0f113f3e | 2116 | if (ciphers == NULL) { |
c48ffbcc | 2117 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6b1bb98f | 2118 | goto err; |
0f113f3e MC |
2119 | } |
2120 | ciphers = NULL; | |
69b2d393 MC |
2121 | } |
2122 | ||
2123 | if (!s->hit) { | |
2124 | #ifdef OPENSSL_NO_COMP | |
2125 | s->session->compress_meth = 0; | |
2126 | #else | |
2127 | s->session->compress_meth = (comp == NULL) ? 0 : comp->id; | |
2128 | #endif | |
6f34d7bc BK |
2129 | if (!tls1_set_server_sigalgs(s)) { |
2130 | /* SSLfatal() already called */ | |
2131 | goto err; | |
2132 | } | |
e27f234a MC |
2133 | } |
2134 | ||
2135 | sk_SSL_CIPHER_free(ciphers); | |
6b1bb98f BK |
2136 | sk_SSL_CIPHER_free(scsvs); |
2137 | OPENSSL_free(clienthello->pre_proc_exts); | |
2138 | OPENSSL_free(s->clienthello); | |
2139 | s->clienthello = NULL; | |
2140 | return 1; | |
e27f234a | 2141 | err: |
e27f234a | 2142 | sk_SSL_CIPHER_free(ciphers); |
6b1bb98f BK |
2143 | sk_SSL_CIPHER_free(scsvs); |
2144 | OPENSSL_free(clienthello->pre_proc_exts); | |
2145 | OPENSSL_free(s->clienthello); | |
2146 | s->clienthello = NULL; | |
e27f234a | 2147 | |
6b1bb98f | 2148 | return 0; |
e27f234a MC |
2149 | } |
2150 | ||
24b8e4b2 MC |
2151 | /* |
2152 | * Call the status request callback if needed. Upon success, returns 1. | |
f63a17d6 | 2153 | * Upon failure, returns 0. |
24b8e4b2 | 2154 | */ |
38b051a1 | 2155 | static int tls_handle_status_request(SSL_CONNECTION *s) |
24b8e4b2 | 2156 | { |
38b051a1 TM |
2157 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
2158 | ||
aff8c126 | 2159 | s->ext.status_expected = 0; |
24b8e4b2 MC |
2160 | |
2161 | /* | |
2162 | * If status request then ask callback what to do. Note: this must be | |
2163 | * called after servername callbacks in case the certificate has changed, | |
2164 | * and must be called after the cipher has been chosen because this may | |
2165 | * influence which certificate is sent | |
2166 | */ | |
38b051a1 TM |
2167 | if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && sctx != NULL |
2168 | && sctx->ext.status_cb != NULL) { | |
24b8e4b2 | 2169 | int ret; |
1266eefd | 2170 | |
24b8e4b2 | 2171 | /* If no certificate can't return certificate status */ |
555cbb32 | 2172 | if (s->s3.tmp.cert != NULL) { |
24b8e4b2 MC |
2173 | /* |
2174 | * Set current certificate to one we will use so SSL_get_certificate | |
2175 | * et al can pick it up. | |
2176 | */ | |
555cbb32 | 2177 | s->cert->key = s->s3.tmp.cert; |
38b051a1 TM |
2178 | ret = sctx->ext.status_cb(SSL_CONNECTION_GET_SSL(s), |
2179 | sctx->ext.status_arg); | |
24b8e4b2 MC |
2180 | switch (ret) { |
2181 | /* We don't want to send a status request response */ | |
2182 | case SSL_TLSEXT_ERR_NOACK: | |
aff8c126 | 2183 | s->ext.status_expected = 0; |
24b8e4b2 MC |
2184 | break; |
2185 | /* status request response should be sent */ | |
2186 | case SSL_TLSEXT_ERR_OK: | |
aff8c126 RS |
2187 | if (s->ext.ocsp.resp) |
2188 | s->ext.status_expected = 1; | |
24b8e4b2 MC |
2189 | break; |
2190 | /* something bad happened */ | |
2191 | case SSL_TLSEXT_ERR_ALERT_FATAL: | |
2192 | default: | |
c48ffbcc | 2193 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CLIENTHELLO_TLSEXT); |
24b8e4b2 MC |
2194 | return 0; |
2195 | } | |
2196 | } | |
2197 | } | |
2198 | ||
2199 | return 1; | |
2200 | } | |
2201 | ||
5626f634 BK |
2202 | /* |
2203 | * Call the alpn_select callback if needed. Upon success, returns 1. | |
29bfd5b7 | 2204 | * Upon failure, returns 0. |
5626f634 | 2205 | */ |
38b051a1 | 2206 | int tls_handle_alpn(SSL_CONNECTION *s) |
5626f634 BK |
2207 | { |
2208 | const unsigned char *selected = NULL; | |
2209 | unsigned char selected_len = 0; | |
38b051a1 | 2210 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
5626f634 | 2211 | |
38b051a1 TM |
2212 | if (sctx->ext.alpn_select_cb != NULL && s->s3.alpn_proposed != NULL) { |
2213 | int r = sctx->ext.alpn_select_cb(SSL_CONNECTION_GET_SSL(s), | |
2214 | &selected, &selected_len, | |
2215 | s->s3.alpn_proposed, | |
2216 | (unsigned int)s->s3.alpn_proposed_len, | |
2217 | sctx->ext.alpn_select_cb_arg); | |
5626f634 BK |
2218 | |
2219 | if (r == SSL_TLSEXT_ERR_OK) { | |
555cbb32 TS |
2220 | OPENSSL_free(s->s3.alpn_selected); |
2221 | s->s3.alpn_selected = OPENSSL_memdup(selected, selected_len); | |
2222 | if (s->s3.alpn_selected == NULL) { | |
39a14059 | 2223 | s->s3.alpn_selected_len = 0; |
c48ffbcc | 2224 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
5626f634 BK |
2225 | return 0; |
2226 | } | |
555cbb32 | 2227 | s->s3.alpn_selected_len = selected_len; |
5626f634 BK |
2228 | #ifndef OPENSSL_NO_NEXTPROTONEG |
2229 | /* ALPN takes precedence over NPN. */ | |
555cbb32 | 2230 | s->s3.npn_seen = 0; |
5626f634 | 2231 | #endif |
630369d9 | 2232 | |
4be3a7c7 MC |
2233 | /* Check ALPN is consistent with session */ |
2234 | if (s->session->ext.alpn_selected == NULL | |
630369d9 MC |
2235 | || selected_len != s->session->ext.alpn_selected_len |
2236 | || memcmp(selected, s->session->ext.alpn_selected, | |
4be3a7c7 MC |
2237 | selected_len) != 0) { |
2238 | /* Not consistent so can't be used for early_data */ | |
630369d9 MC |
2239 | s->ext.early_data_ok = 0; |
2240 | ||
4be3a7c7 | 2241 | if (!s->hit) { |
9d5db9c9 MC |
2242 | /* |
2243 | * This is a new session and so alpn_selected should have | |
2244 | * been initialised to NULL. We should update it with the | |
2245 | * selected ALPN. | |
2246 | */ | |
2247 | if (!ossl_assert(s->session->ext.alpn_selected == NULL)) { | |
2248 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, | |
9d5db9c9 MC |
2249 | ERR_R_INTERNAL_ERROR); |
2250 | return 0; | |
2251 | } | |
4be3a7c7 MC |
2252 | s->session->ext.alpn_selected = OPENSSL_memdup(selected, |
2253 | selected_len); | |
2254 | if (s->session->ext.alpn_selected == NULL) { | |
f63a17d6 | 2255 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
f63a17d6 | 2256 | ERR_R_INTERNAL_ERROR); |
4be3a7c7 MC |
2257 | return 0; |
2258 | } | |
2259 | s->session->ext.alpn_selected_len = selected_len; | |
2260 | } | |
2261 | } | |
2262 | ||
5626f634 | 2263 | return 1; |
630369d9 | 2264 | } else if (r != SSL_TLSEXT_ERR_NOACK) { |
c48ffbcc | 2265 | SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL, |
f63a17d6 | 2266 | SSL_R_NO_APPLICATION_PROTOCOL); |
5626f634 BK |
2267 | return 0; |
2268 | } | |
630369d9 MC |
2269 | /* |
2270 | * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was | |
2271 | * present. | |
2272 | */ | |
5626f634 BK |
2273 | } |
2274 | ||
4be3a7c7 MC |
2275 | /* Check ALPN is consistent with session */ |
2276 | if (s->session->ext.alpn_selected != NULL) { | |
2277 | /* Not consistent so can't be used for early_data */ | |
630369d9 | 2278 | s->ext.early_data_ok = 0; |
4be3a7c7 | 2279 | } |
630369d9 | 2280 | |
5626f634 BK |
2281 | return 1; |
2282 | } | |
2283 | ||
38b051a1 | 2284 | WORK_STATE tls_post_process_client_hello(SSL_CONNECTION *s, WORK_STATE wst) |
e27f234a | 2285 | { |
4a640fb6 | 2286 | const SSL_CIPHER *cipher; |
38b051a1 | 2287 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
e27f234a MC |
2288 | |
2289 | if (wst == WORK_MORE_A) { | |
f63a17d6 | 2290 | int rv = tls_early_post_process_client_hello(s); |
6b1bb98f | 2291 | if (rv == 0) { |
f63a17d6 MC |
2292 | /* SSLfatal() was already called */ |
2293 | goto err; | |
6b1bb98f BK |
2294 | } |
2295 | if (rv < 0) | |
2296 | return WORK_MORE_A; | |
2297 | wst = WORK_MORE_B; | |
2298 | } | |
2299 | if (wst == WORK_MORE_B) { | |
38b051a1 | 2300 | if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) { |
e27f234a | 2301 | /* Let cert callback update server certificates if required */ |
6f34d7bc | 2302 | if (!s->hit && s->cert->cert_cb != NULL) { |
38b051a1 | 2303 | int rv = s->cert->cert_cb(ssl, s->cert->cert_cb_arg); |
6f34d7bc | 2304 | if (rv == 0) { |
c48ffbcc | 2305 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CERT_CB_ERROR); |
524006dd | 2306 | goto err; |
e27f234a | 2307 | } |
6f34d7bc BK |
2308 | if (rv < 0) { |
2309 | s->rwstate = SSL_X509_LOOKUP; | |
2310 | return WORK_MORE_B; | |
2311 | } | |
2312 | s->rwstate = SSL_NOTHING; | |
0f113f3e | 2313 | } |
e27f234a | 2314 | |
0de6d66d | 2315 | /* In TLSv1.3 we selected the ciphersuite before resumption */ |
38b051a1 | 2316 | if (!SSL_CONNECTION_IS_TLS13(s)) { |
0de6d66d | 2317 | cipher = |
38b051a1 TM |
2318 | ssl3_choose_cipher(s, s->peer_ciphers, |
2319 | SSL_get_ciphers(ssl)); | |
0de6d66d MC |
2320 | |
2321 | if (cipher == NULL) { | |
f63a17d6 | 2322 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
f63a17d6 MC |
2323 | SSL_R_NO_SHARED_CIPHER); |
2324 | goto err; | |
0de6d66d | 2325 | } |
555cbb32 | 2326 | s->s3.tmp.new_cipher = cipher; |
11c67eea | 2327 | } |
69b2d393 | 2328 | if (!s->hit) { |
f63a17d6 MC |
2329 | if (!tls_choose_sigalg(s, 1)) { |
2330 | /* SSLfatal already called */ | |
2331 | goto err; | |
2332 | } | |
69b2d393 MC |
2333 | /* check whether we should disable session resumption */ |
2334 | if (s->not_resumable_session_cb != NULL) | |
2335 | s->session->not_resumable = | |
38b051a1 | 2336 | s->not_resumable_session_cb(ssl, |
555cbb32 | 2337 | ((s->s3.tmp.new_cipher->algorithm_mkey |
8acc2799 | 2338 | & (SSL_kDHE | SSL_kECDHE)) != 0)); |
69b2d393 MC |
2339 | if (s->session->not_resumable) |
2340 | /* do not send a session ticket */ | |
2341 | s->ext.ticket_expected = 0; | |
2342 | } | |
e27f234a MC |
2343 | } else { |
2344 | /* Session-id reuse */ | |
555cbb32 | 2345 | s->s3.tmp.new_cipher = s->session->cipher; |
0f113f3e | 2346 | } |
0f113f3e | 2347 | |
e27f234a MC |
2348 | /*- |
2349 | * we now have the following setup. | |
2350 | * client_random | |
60250017 | 2351 | * cipher_list - our preferred list of ciphers |
2352 | * ciphers - the clients preferred list of ciphers | |
e27f234a MC |
2353 | * compression - basically ignored right now |
2354 | * ssl version is set - sslv3 | |
2355 | * s->session - The ssl session has been setup. | |
2356 | * s->hit - session reuse flag | |
555cbb32 | 2357 | * s->s3.tmp.new_cipher - the new cipher to use. |
e27f234a | 2358 | */ |
0f113f3e | 2359 | |
24b8e4b2 MC |
2360 | /* |
2361 | * Call status_request callback if needed. Has to be done after the | |
2362 | * certificate callbacks etc above. | |
2363 | */ | |
f63a17d6 MC |
2364 | if (!tls_handle_status_request(s)) { |
2365 | /* SSLfatal() already called */ | |
2366 | goto err; | |
e27f234a | 2367 | } |
5626f634 BK |
2368 | /* |
2369 | * Call alpn_select callback if needed. Has to be done after SNI and | |
630369d9 MC |
2370 | * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3 |
2371 | * we already did this because cipher negotiation happens earlier, and | |
2372 | * we must handle ALPN before we decide whether to accept early_data. | |
5626f634 | 2373 | */ |
38b051a1 | 2374 | if (!SSL_CONNECTION_IS_TLS13(s) && !tls_handle_alpn(s)) { |
f63a17d6 MC |
2375 | /* SSLfatal() already called */ |
2376 | goto err; | |
5626f634 | 2377 | } |
0f113f3e | 2378 | |
6b1bb98f | 2379 | wst = WORK_MORE_C; |
e27f234a MC |
2380 | } |
2381 | #ifndef OPENSSL_NO_SRP | |
6b1bb98f | 2382 | if (wst == WORK_MORE_C) { |
e27f234a | 2383 | int ret; |
29bfd5b7 | 2384 | if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) { |
e27f234a MC |
2385 | /* |
2386 | * callback indicates further work to be done | |
2387 | */ | |
2388 | s->rwstate = SSL_X509_LOOKUP; | |
6b1bb98f | 2389 | return WORK_MORE_C; |
e27f234a | 2390 | } |
29bfd5b7 MC |
2391 | if (ret < 0) { |
2392 | /* SSLfatal() already called */ | |
f63a17d6 | 2393 | goto err; |
0f113f3e MC |
2394 | } |
2395 | } | |
e27f234a | 2396 | #endif |
0f113f3e | 2397 | |
e27f234a | 2398 | return WORK_FINISHED_STOP; |
f63a17d6 | 2399 | err: |
e27f234a MC |
2400 | return WORK_ERROR; |
2401 | } | |
2402 | ||
67ec6d2b | 2403 | CON_FUNC_RETURN tls_construct_server_hello(SSL_CONNECTION *s, WPACKET *pkt) |
0f113f3e | 2404 | { |
f63a17d6 | 2405 | int compm; |
ec60ccc1 | 2406 | size_t sl, len; |
f2342b7a | 2407 | int version; |
a5816a5a | 2408 | unsigned char *session_id; |
38b051a1 TM |
2409 | int usetls13 = SSL_CONNECTION_IS_TLS13(s) |
2410 | || s->hello_retry_request == SSL_HRR_PENDING; | |
0f113f3e | 2411 | |
597c51bc | 2412 | version = usetls13 ? TLS1_2_VERSION : s->version; |
f2342b7a | 2413 | if (!WPACKET_put_bytes_u16(pkt, version) |
8157d44b MC |
2414 | /* |
2415 | * Random stuff. Filling of the server_random takes place in | |
2416 | * tls_process_client_hello() | |
2417 | */ | |
597c51bc | 2418 | || !WPACKET_memcpy(pkt, |
fc7129dc | 2419 | s->hello_retry_request == SSL_HRR_PENDING |
555cbb32 | 2420 | ? hrrrandom : s->s3.server_random, |
597c51bc | 2421 | SSL3_RANDOM_SIZE)) { |
c48ffbcc | 2422 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 2423 | return CON_FUNC_ERROR; |
8157d44b | 2424 | } |
0f113f3e | 2425 | |
e27f234a MC |
2426 | /*- |
2427 | * There are several cases for the session ID to send | |
2428 | * back in the server hello: | |
2429 | * - For session reuse from the session cache, | |
2430 | * we send back the old session ID. | |
2431 | * - If stateless session reuse (using a session ticket) | |
2432 | * is successful, we send back the client's "session ID" | |
2433 | * (which doesn't actually identify the session). | |
2434 | * - If it is a new session, we send back the new | |
2435 | * session ID. | |
2436 | * - However, if we want the new session to be single-use, | |
2437 | * we send back a 0-length session ID. | |
a5816a5a MC |
2438 | * - In TLSv1.3 we echo back the session id sent to us by the client |
2439 | * regardless | |
e27f234a MC |
2440 | * s->hit is non-zero in either case of session reuse, |
2441 | * so the following won't overwrite an ID that we're supposed | |
2442 | * to send back. | |
2443 | */ | |
2444 | if (s->session->not_resumable || | |
38b051a1 | 2445 | (!(SSL_CONNECTION_GET_CTX(s)->session_cache_mode & SSL_SESS_CACHE_SERVER) |
e27f234a MC |
2446 | && !s->hit)) |
2447 | s->session->session_id_length = 0; | |
2448 | ||
597c51bc | 2449 | if (usetls13) { |
a5816a5a MC |
2450 | sl = s->tmp_session_id_len; |
2451 | session_id = s->tmp_session_id; | |
2452 | } else { | |
2453 | sl = s->session->session_id_length; | |
2454 | session_id = s->session->session_id; | |
2455 | } | |
2456 | ||
ec60ccc1 | 2457 | if (sl > sizeof(s->session->session_id)) { |
c48ffbcc | 2458 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 2459 | return CON_FUNC_ERROR; |
e27f234a | 2460 | } |
0f113f3e | 2461 | |
8157d44b | 2462 | /* set up the compression method */ |
09b6c2ef | 2463 | #ifdef OPENSSL_NO_COMP |
8157d44b | 2464 | compm = 0; |
09b6c2ef | 2465 | #else |
555cbb32 | 2466 | if (usetls13 || s->s3.tmp.new_compression == NULL) |
8157d44b | 2467 | compm = 0; |
e27f234a | 2468 | else |
555cbb32 | 2469 | compm = s->s3.tmp.new_compression->id; |
09b6c2ef | 2470 | #endif |
e481f9b9 | 2471 | |
426dfc9f | 2472 | if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl) |
38b051a1 TM |
2473 | || !SSL_CONNECTION_GET_SSL(s)->method->put_cipher_by_char(s->s3.tmp.new_cipher, |
2474 | pkt, &len) | |
b4f001eb | 2475 | || !WPACKET_put_bytes_u8(pkt, compm)) { |
c48ffbcc | 2476 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 2477 | return CON_FUNC_ERROR; |
b4f001eb MC |
2478 | } |
2479 | ||
2480 | if (!tls_construct_extensions(s, pkt, | |
2481 | s->hello_retry_request == SSL_HRR_PENDING | |
2482 | ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | |
38b051a1 | 2483 | : (SSL_CONNECTION_IS_TLS13(s) |
b4f001eb MC |
2484 | ? SSL_EXT_TLS1_3_SERVER_HELLO |
2485 | : SSL_EXT_TLS1_2_SERVER_HELLO), | |
2486 | NULL, 0)) { | |
f63a17d6 | 2487 | /* SSLfatal() already called */ |
67ec6d2b | 2488 | return CON_FUNC_ERROR; |
0f113f3e | 2489 | } |
d02b48c6 | 2490 | |
fc7129dc | 2491 | if (s->hello_retry_request == SSL_HRR_PENDING) { |
597c51bc MC |
2492 | /* Ditch the session. We'll create a new one next time around */ |
2493 | SSL_SESSION_free(s->session); | |
2494 | s->session = NULL; | |
2495 | s->hit = 0; | |
2496 | ||
2497 | /* | |
2498 | * Re-initialise the Transcript Hash. We're going to prepopulate it with | |
2499 | * a synthetic message_hash in place of ClientHello1. | |
2500 | */ | |
43054d3d | 2501 | if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) { |
597c51bc | 2502 | /* SSLfatal() already called */ |
67ec6d2b | 2503 | return CON_FUNC_ERROR; |
597c51bc MC |
2504 | } |
2505 | } else if (!(s->verify_mode & SSL_VERIFY_PEER) | |
2506 | && !ssl3_digest_cached_records(s, 0)) { | |
f63a17d6 | 2507 | /* SSLfatal() already called */; |
67ec6d2b | 2508 | return CON_FUNC_ERROR; |
aff9929b MC |
2509 | } |
2510 | ||
67ec6d2b | 2511 | return CON_FUNC_SUCCESS; |
0f113f3e | 2512 | } |
d02b48c6 | 2513 | |
67ec6d2b | 2514 | CON_FUNC_RETURN tls_construct_server_done(SSL_CONNECTION *s, WPACKET *pkt) |
e27f234a | 2515 | { |
555cbb32 | 2516 | if (!s->s3.tmp.cert_request) { |
5923ad4b | 2517 | if (!ssl3_digest_cached_records(s, 0)) { |
f63a17d6 | 2518 | /* SSLfatal() already called */ |
67ec6d2b | 2519 | return CON_FUNC_ERROR; |
5923ad4b | 2520 | } |
e27f234a | 2521 | } |
67ec6d2b | 2522 | return CON_FUNC_SUCCESS; |
e27f234a MC |
2523 | } |
2524 | ||
67ec6d2b MC |
2525 | CON_FUNC_RETURN tls_construct_server_key_exchange(SSL_CONNECTION *s, |
2526 | WPACKET *pkt) | |
0f113f3e | 2527 | { |
e2b420fd | 2528 | EVP_PKEY *pkdh = NULL; |
0f113f3e | 2529 | unsigned char *encodedPoint = NULL; |
348240c6 | 2530 | size_t encodedlen = 0; |
0f113f3e | 2531 | int curve_id = 0; |
555cbb32 | 2532 | const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg; |
f63a17d6 | 2533 | int i; |
0f113f3e | 2534 | unsigned long type; |
18428097 | 2535 | BIGNUM *r[4]; |
bfb0641f | 2536 | EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); |
fe3066ee | 2537 | EVP_PKEY_CTX *pctx = NULL; |
c13d2a5b | 2538 | size_t paramlen, paramoffset; |
67ec6d2b MC |
2539 | int freer = 0; |
2540 | CON_FUNC_RETURN ret = CON_FUNC_ERROR; | |
38b051a1 | 2541 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
c13d2a5b | 2542 | |
5923ad4b | 2543 | if (!WPACKET_get_total_written(pkt, ¶moffset)) { |
c48ffbcc | 2544 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 2545 | goto err; |
c13d2a5b | 2546 | } |
0f113f3e | 2547 | |
6e59a892 | 2548 | if (md_ctx == NULL) { |
e077455e | 2549 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
f63a17d6 | 2550 | goto err; |
6e59a892 | 2551 | } |
0f113f3e | 2552 | |
555cbb32 | 2553 | type = s->s3.tmp.new_cipher->algorithm_mkey; |
e27f234a | 2554 | |
e27f234a | 2555 | r[0] = r[1] = r[2] = r[3] = NULL; |
85269210 | 2556 | #ifndef OPENSSL_NO_PSK |
e27f234a MC |
2557 | /* Plain PSK or RSAPSK nothing to do */ |
2558 | if (type & (SSL_kPSK | SSL_kRSAPSK)) { | |
2559 | } else | |
85269210 | 2560 | #endif /* !OPENSSL_NO_PSK */ |
e27f234a | 2561 | if (type & (SSL_kDHE | SSL_kDHEPSK)) { |
94d61512 | 2562 | CERT *cert = s->cert; |
e2b420fd | 2563 | EVP_PKEY *pkdhp = NULL; |
e2b420fd | 2564 | |
e27f234a | 2565 | if (s->cert->dh_tmp_auto) { |
091f6074 MC |
2566 | pkdh = ssl_get_auto_dh(s); |
2567 | if (pkdh == NULL) { | |
c48ffbcc | 2568 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 2569 | goto err; |
0f113f3e | 2570 | } |
e2b420fd DSH |
2571 | pkdhp = pkdh; |
2572 | } else { | |
2573 | pkdhp = cert->dh_tmp; | |
2574 | } | |
5b64ce89 | 2575 | #if !defined(OPENSSL_NO_DEPRECATED_3_0) |
e2b420fd | 2576 | if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) { |
38b051a1 TM |
2577 | pkdh = ssl_dh_to_pkey(s->cert->dh_tmp_cb(SSL_CONNECTION_GET_SSL(s), |
2578 | 0, 1024)); | |
e2b420fd | 2579 | if (pkdh == NULL) { |
c48ffbcc | 2580 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 2581 | goto err; |
e2b420fd DSH |
2582 | } |
2583 | pkdhp = pkdh; | |
2584 | } | |
1b2b4755 | 2585 | #endif |
e2b420fd | 2586 | if (pkdhp == NULL) { |
c48ffbcc | 2587 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY); |
f63a17d6 | 2588 | goto err; |
e27f234a MC |
2589 | } |
2590 | if (!ssl_security(s, SSL_SECOP_TMP_DH, | |
ed576acd | 2591 | EVP_PKEY_get_security_bits(pkdhp), 0, pkdhp)) { |
c48ffbcc | 2592 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL); |
f63a17d6 | 2593 | goto err; |
e27f234a | 2594 | } |
555cbb32 | 2595 | if (s->s3.tmp.pkey != NULL) { |
c48ffbcc | 2596 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
e27f234a MC |
2597 | goto err; |
2598 | } | |
0f113f3e | 2599 | |
0f00ed77 | 2600 | s->s3.tmp.pkey = ssl_generate_pkey(s, pkdhp); |
555cbb32 | 2601 | if (s->s3.tmp.pkey == NULL) { |
c48ffbcc | 2602 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
ffaef3f1 | 2603 | goto err; |
e27f234a | 2604 | } |
e2b420fd | 2605 | |
e2b420fd DSH |
2606 | EVP_PKEY_free(pkdh); |
2607 | pkdh = NULL; | |
2608 | ||
18428097 MC |
2609 | /* These BIGNUMs need to be freed when we're finished */ |
2610 | freer = 1; | |
2611 | if (!EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_P, | |
2612 | &r[0]) | |
2613 | || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_G, | |
2614 | &r[1]) | |
2615 | || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey, | |
2616 | OSSL_PKEY_PARAM_PUB_KEY, &r[2])) { | |
2617 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
2618 | goto err; | |
2619 | } | |
462f4f4b | 2620 | } else if (type & (SSL_kECDHE | SSL_kECDHEPSK)) { |
e27f234a | 2621 | |
555cbb32 | 2622 | if (s->s3.tmp.pkey != NULL) { |
c48ffbcc | 2623 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
e27f234a MC |
2624 | goto err; |
2625 | } | |
2626 | ||
57be4444 | 2627 | /* Get NID of appropriate shared curve */ |
8841154a | 2628 | curve_id = tls1_shared_group(s, -2); |
57be4444 | 2629 | if (curve_id == 0) { |
f63a17d6 | 2630 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
f63a17d6 | 2631 | SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); |
e27f234a MC |
2632 | goto err; |
2633 | } | |
aa6bd216 BK |
2634 | /* Cache the group used in the SSL_SESSION */ |
2635 | s->session->kex_group = curve_id; | |
880d9d86 | 2636 | /* Generate a new key for this curve */ |
aa6bd216 | 2637 | s->s3.tmp.pkey = ssl_generate_pkey_group(s, curve_id); |
555cbb32 | 2638 | if (s->s3.tmp.pkey == NULL) { |
f63a17d6 MC |
2639 | /* SSLfatal() already called */ |
2640 | goto err; | |
57be4444 DSH |
2641 | } |
2642 | ||
880d9d86 | 2643 | /* Encode the public key. */ |
5ac8fb58 MC |
2644 | encodedlen = EVP_PKEY_get1_encoded_public_key(s->s3.tmp.pkey, |
2645 | &encodedPoint); | |
e27f234a | 2646 | if (encodedlen == 0) { |
c48ffbcc | 2647 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB); |
e27f234a MC |
2648 | goto err; |
2649 | } | |
0f113f3e | 2650 | |
e27f234a MC |
2651 | /* |
2652 | * We'll generate the serverKeyExchange message explicitly so we | |
2653 | * can set these to NULLs | |
2654 | */ | |
2655 | r[0] = NULL; | |
2656 | r[1] = NULL; | |
2657 | r[2] = NULL; | |
2658 | r[3] = NULL; | |
2659 | } else | |
edc032b5 | 2660 | #ifndef OPENSSL_NO_SRP |
e27f234a MC |
2661 | if (type & SSL_kSRP) { |
2662 | if ((s->srp_ctx.N == NULL) || | |
2663 | (s->srp_ctx.g == NULL) || | |
2664 | (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) { | |
c48ffbcc | 2665 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_SRP_PARAM); |
e27f234a | 2666 | goto err; |
0f113f3e | 2667 | } |
e27f234a MC |
2668 | r[0] = s->srp_ctx.N; |
2669 | r[1] = s->srp_ctx.g; | |
2670 | r[2] = s->srp_ctx.s; | |
2671 | r[3] = s->srp_ctx.B; | |
2672 | } else | |
2673 | #endif | |
2674 | { | |
c48ffbcc | 2675 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); |
f63a17d6 | 2676 | goto err; |
e27f234a | 2677 | } |
0f113f3e | 2678 | |
555cbb32 TS |
2679 | if (((s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0) |
2680 | || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) { | |
f695571e DSH |
2681 | lu = NULL; |
2682 | } else if (lu == NULL) { | |
c48ffbcc | 2683 | SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 2684 | goto err; |
e27f234a | 2685 | } |
0f113f3e | 2686 | |
85269210 | 2687 | #ifndef OPENSSL_NO_PSK |
e27f234a | 2688 | if (type & SSL_PSK) { |
c13d2a5b MC |
2689 | size_t len = (s->cert->psk_identity_hint == NULL) |
2690 | ? 0 : strlen(s->cert->psk_identity_hint); | |
2691 | ||
2692 | /* | |
2693 | * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already | |
2694 | * checked this when we set the identity hint - but just in case | |
2695 | */ | |
2696 | if (len > PSK_MAX_IDENTITY_LEN | |
7cea05dc | 2697 | || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint, |
c13d2a5b | 2698 | len)) { |
c48ffbcc | 2699 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 2700 | goto err; |
85269210 | 2701 | } |
e27f234a | 2702 | } |
85269210 DSH |
2703 | #endif |
2704 | ||
e27f234a | 2705 | for (i = 0; i < 4 && r[i] != NULL; i++) { |
c13d2a5b MC |
2706 | unsigned char *binval; |
2707 | int res; | |
2708 | ||
edc032b5 | 2709 | #ifndef OPENSSL_NO_SRP |
e27f234a | 2710 | if ((i == 2) && (type & SSL_kSRP)) { |
7cea05dc | 2711 | res = WPACKET_start_sub_packet_u8(pkt); |
e27f234a | 2712 | } else |
78a01b3f | 2713 | #endif |
7cea05dc | 2714 | res = WPACKET_start_sub_packet_u16(pkt); |
c13d2a5b MC |
2715 | |
2716 | if (!res) { | |
c48ffbcc | 2717 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 2718 | goto err; |
c13d2a5b MC |
2719 | } |
2720 | ||
a230b26e | 2721 | /*- |
78a01b3f | 2722 | * for interoperability with some versions of the Microsoft TLS |
2723 | * stack, we need to zero pad the DHE pub key to the same length | |
2724 | * as the prime | |
2725 | */ | |
2726 | if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) { | |
c13d2a5b | 2727 | size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]); |
ff819477 | 2728 | |
c13d2a5b | 2729 | if (len > 0) { |
7cea05dc | 2730 | if (!WPACKET_allocate_bytes(pkt, len, &binval)) { |
c48ffbcc | 2731 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 2732 | goto err; |
c13d2a5b MC |
2733 | } |
2734 | memset(binval, 0, len); | |
78a01b3f | 2735 | } |
c13d2a5b | 2736 | } |
18428097 | 2737 | |
7cea05dc MC |
2738 | if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval) |
2739 | || !WPACKET_close(pkt)) { | |
c48ffbcc | 2740 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 2741 | goto err; |
c13d2a5b MC |
2742 | } |
2743 | ||
2744 | BN_bn2bin(r[i], binval); | |
e27f234a | 2745 | } |
d02b48c6 | 2746 | |
e27f234a MC |
2747 | if (type & (SSL_kECDHE | SSL_kECDHEPSK)) { |
2748 | /* | |
c13d2a5b MC |
2749 | * We only support named (not generic) curves. In this situation, the |
2750 | * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName] | |
2751 | * [1 byte length of encoded point], followed by the actual encoded | |
2752 | * point itself | |
e27f234a | 2753 | */ |
7cea05dc MC |
2754 | if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE) |
2755 | || !WPACKET_put_bytes_u8(pkt, 0) | |
2756 | || !WPACKET_put_bytes_u8(pkt, curve_id) | |
2757 | || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) { | |
c48ffbcc | 2758 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 2759 | goto err; |
c13d2a5b | 2760 | } |
e27f234a MC |
2761 | OPENSSL_free(encodedPoint); |
2762 | encodedPoint = NULL; | |
e27f234a | 2763 | } |
ea262260 | 2764 | |
e27f234a | 2765 | /* not anonymous */ |
f695571e | 2766 | if (lu != NULL) { |
555cbb32 | 2767 | EVP_PKEY *pkey = s->s3.tmp.cert->privatekey; |
72ceb6a6 DSH |
2768 | const EVP_MD *md; |
2769 | unsigned char *sigbytes1, *sigbytes2, *tbs; | |
bddbfae1 | 2770 | size_t siglen = 0, tbslen; |
f695571e | 2771 | |
38b051a1 | 2772 | if (pkey == NULL || !tls1_lookup_md(sctx, lu, &md)) { |
f695571e | 2773 | /* Should never happen */ |
c48ffbcc | 2774 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 2775 | goto err; |
f695571e | 2776 | } |
f695571e DSH |
2777 | /* Get length of the parameters we have written above */ |
2778 | if (!WPACKET_get_length(pkt, ¶mlen)) { | |
c48ffbcc | 2779 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 2780 | goto err; |
f695571e DSH |
2781 | } |
2782 | /* send signature algorithm */ | |
f63a17d6 | 2783 | if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) { |
c48ffbcc | 2784 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 MC |
2785 | goto err; |
2786 | } | |
bddbfae1 | 2787 | |
d8652be0 | 2788 | if (EVP_DigestSignInit_ex(md_ctx, &pctx, |
ed576acd | 2789 | md == NULL ? NULL : EVP_MD_get0_name(md), |
38b051a1 | 2790 | sctx->libctx, sctx->propq, pkey, |
d38b6ae9 | 2791 | NULL) <= 0) { |
c48ffbcc | 2792 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 2793 | goto err; |
f695571e DSH |
2794 | } |
2795 | if (lu->sig == EVP_PKEY_RSA_PSS) { | |
2796 | if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 | |
2797 | || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) { | |
c48ffbcc | 2798 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
f63a17d6 | 2799 | goto err; |
0f113f3e | 2800 | } |
f695571e | 2801 | } |
72ceb6a6 DSH |
2802 | tbslen = construct_key_exchange_tbs(s, &tbs, |
2803 | s->init_buf->data + paramoffset, | |
2804 | paramlen); | |
2805 | if (tbslen == 0) { | |
f63a17d6 MC |
2806 | /* SSLfatal() already called */ |
2807 | goto err; | |
72ceb6a6 | 2808 | } |
bddbfae1 MC |
2809 | |
2810 | if (EVP_DigestSign(md_ctx, NULL, &siglen, tbs, tbslen) <=0 | |
2811 | || !WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1) | |
2812 | || EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen) <= 0 | |
2813 | || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2) | |
2814 | || sigbytes1 != sigbytes2) { | |
2815 | OPENSSL_free(tbs); | |
c48ffbcc | 2816 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 2817 | goto err; |
77d514c5 | 2818 | } |
bddbfae1 | 2819 | OPENSSL_free(tbs); |
0f113f3e MC |
2820 | } |
2821 | ||
67ec6d2b | 2822 | ret = CON_FUNC_SUCCESS; |
0f113f3e | 2823 | err: |
e2b420fd | 2824 | EVP_PKEY_free(pkdh); |
b548a1f1 | 2825 | OPENSSL_free(encodedPoint); |
bfb0641f | 2826 | EVP_MD_CTX_free(md_ctx); |
18428097 MC |
2827 | if (freer) { |
2828 | BN_free(r[0]); | |
2829 | BN_free(r[1]); | |
2830 | BN_free(r[2]); | |
2831 | BN_free(r[3]); | |
2832 | } | |
2833 | return ret; | |
0f113f3e | 2834 | } |
d02b48c6 | 2835 | |
67ec6d2b MC |
2836 | CON_FUNC_RETURN tls_construct_certificate_request(SSL_CONNECTION *s, |
2837 | WPACKET *pkt) | |
0f113f3e | 2838 | { |
38b051a1 | 2839 | if (SSL_CONNECTION_IS_TLS13(s)) { |
9d75dce3 TS |
2840 | /* Send random context when doing post-handshake auth */ |
2841 | if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { | |
2842 | OPENSSL_free(s->pha_context); | |
2843 | s->pha_context_len = 32; | |
39a14059 MC |
2844 | if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL) { |
2845 | s->pha_context_len = 0; | |
2846 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
67ec6d2b | 2847 | return CON_FUNC_ERROR; |
39a14059 | 2848 | } |
38b051a1 TM |
2849 | if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx, |
2850 | s->pha_context, s->pha_context_len, 0) <= 0 | |
39a14059 MC |
2851 | || !WPACKET_sub_memcpy_u8(pkt, s->pha_context, |
2852 | s->pha_context_len)) { | |
c48ffbcc | 2853 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 2854 | return CON_FUNC_ERROR; |
9d75dce3 TS |
2855 | } |
2856 | /* reset the handshake hash back to just after the ClientFinished */ | |
2857 | if (!tls13_restore_handshake_digest_for_pha(s)) { | |
2858 | /* SSLfatal() already called */ | |
67ec6d2b | 2859 | return CON_FUNC_ERROR; |
9d75dce3 TS |
2860 | } |
2861 | } else { | |
2862 | if (!WPACKET_put_bytes_u8(pkt, 0)) { | |
c48ffbcc | 2863 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 2864 | return CON_FUNC_ERROR; |
9d75dce3 | 2865 | } |
03f44b97 | 2866 | } |
32f66107 | 2867 | |
fe874d27 MC |
2868 | if (!tls_construct_extensions(s, pkt, |
2869 | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL, | |
f63a17d6 MC |
2870 | 0)) { |
2871 | /* SSLfatal() already called */ | |
67ec6d2b | 2872 | return CON_FUNC_ERROR; |
03f44b97 | 2873 | } |
32f66107 DSH |
2874 | goto done; |
2875 | } | |
2876 | ||
2877 | /* get the list of acceptable cert types */ | |
2878 | if (!WPACKET_start_sub_packet_u8(pkt) | |
2879 | || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) { | |
c48ffbcc | 2880 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 2881 | return CON_FUNC_ERROR; |
28ff8ef3 | 2882 | } |
0f113f3e | 2883 | |
e27f234a | 2884 | if (SSL_USE_SIGALGS(s)) { |
98c792d1 | 2885 | const uint16_t *psigs; |
a9669ddc | 2886 | size_t nl = tls12_get_psigalgs(s, 1, &psigs); |
703bcee0 | 2887 | |
7cea05dc | 2888 | if (!WPACKET_start_sub_packet_u16(pkt) |
8f12296e | 2889 | || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH) |
7cea05dc MC |
2890 | || !tls12_copy_sigalgs(s, pkt, psigs, nl) |
2891 | || !WPACKET_close(pkt)) { | |
c48ffbcc | 2892 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 2893 | return CON_FUNC_ERROR; |
28ff8ef3 | 2894 | } |
e27f234a | 2895 | } |
0f113f3e | 2896 | |
98732979 | 2897 | if (!construct_ca_names(s, get_ca_names(s), pkt)) { |
f63a17d6 | 2898 | /* SSLfatal() already called */ |
67ec6d2b | 2899 | return CON_FUNC_ERROR; |
28ff8ef3 | 2900 | } |
e27f234a | 2901 | |
32f66107 | 2902 | done: |
9d75dce3 | 2903 | s->certreqs_sent++; |
555cbb32 | 2904 | s->s3.tmp.cert_request = 1; |
67ec6d2b | 2905 | return CON_FUNC_SUCCESS; |
0f113f3e | 2906 | } |
d02b48c6 | 2907 | |
38b051a1 | 2908 | static int tls_process_cke_psk_preamble(SSL_CONNECTION *s, PACKET *pkt) |
e27f234a | 2909 | { |
85269210 | 2910 | #ifndef OPENSSL_NO_PSK |
0907d710 MC |
2911 | unsigned char psk[PSK_MAX_PSK_LEN]; |
2912 | size_t psklen; | |
2913 | PACKET psk_identity; | |
efcdbcbe | 2914 | |
0907d710 | 2915 | if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) { |
c48ffbcc | 2916 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
0907d710 MC |
2917 | return 0; |
2918 | } | |
2919 | if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) { | |
c48ffbcc | 2920 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DATA_LENGTH_TOO_LONG); |
0907d710 MC |
2921 | return 0; |
2922 | } | |
2923 | if (s->psk_server_callback == NULL) { | |
c48ffbcc | 2924 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_SERVER_CB); |
0907d710 MC |
2925 | return 0; |
2926 | } | |
85269210 | 2927 | |
0907d710 | 2928 | if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) { |
c48ffbcc | 2929 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
0907d710 MC |
2930 | return 0; |
2931 | } | |
85269210 | 2932 | |
38b051a1 TM |
2933 | psklen = s->psk_server_callback(SSL_CONNECTION_GET_SSL(s), |
2934 | s->session->psk_identity, | |
a230b26e | 2935 | psk, sizeof(psk)); |
85269210 | 2936 | |
0907d710 | 2937 | if (psklen > PSK_MAX_PSK_LEN) { |
c48ffbcc | 2938 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
0907d710 MC |
2939 | return 0; |
2940 | } else if (psklen == 0) { | |
2941 | /* | |
2942 | * PSK related to the given identity not found | |
2943 | */ | |
c48ffbcc | 2944 | SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY, SSL_R_PSK_IDENTITY_NOT_FOUND); |
0907d710 MC |
2945 | return 0; |
2946 | } | |
85269210 | 2947 | |
555cbb32 TS |
2948 | OPENSSL_free(s->s3.tmp.psk); |
2949 | s->s3.tmp.psk = OPENSSL_memdup(psk, psklen); | |
0907d710 | 2950 | OPENSSL_cleanse(psk, psklen); |
85269210 | 2951 | |
555cbb32 | 2952 | if (s->s3.tmp.psk == NULL) { |
39a14059 | 2953 | s->s3.tmp.psklen = 0; |
e077455e | 2954 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
0907d710 | 2955 | return 0; |
85269210 | 2956 | } |
0907d710 | 2957 | |
555cbb32 | 2958 | s->s3.tmp.psklen = psklen; |
0907d710 MC |
2959 | |
2960 | return 1; | |
2961 | #else | |
2962 | /* Should never happen */ | |
c48ffbcc | 2963 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
0907d710 | 2964 | return 0; |
85269210 | 2965 | #endif |
0907d710 MC |
2966 | } |
2967 | ||
38b051a1 | 2968 | static int tls_process_cke_rsa(SSL_CONNECTION *s, PACKET *pkt) |
0907d710 | 2969 | { |
e7db9680 | 2970 | size_t outlen; |
0907d710 | 2971 | PACKET enc_premaster; |
e7db9680 | 2972 | EVP_PKEY *rsa = NULL; |
0907d710 MC |
2973 | unsigned char *rsa_decrypt = NULL; |
2974 | int ret = 0; | |
e7db9680 MC |
2975 | EVP_PKEY_CTX *ctx = NULL; |
2976 | OSSL_PARAM params[3], *p = params; | |
38b051a1 | 2977 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
0907d710 | 2978 | |
e7db9680 | 2979 | rsa = s->cert->pkeys[SSL_PKEY_RSA].privatekey; |
0907d710 | 2980 | if (rsa == NULL) { |
c48ffbcc | 2981 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_RSA_CERTIFICATE); |
0907d710 MC |
2982 | return 0; |
2983 | } | |
2984 | ||
2985 | /* SSLv3 and pre-standard DTLS omit the length bytes. */ | |
2986 | if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) { | |
2987 | enc_premaster = *pkt; | |
2988 | } else { | |
2989 | if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster) | |
2990 | || PACKET_remaining(pkt) != 0) { | |
c48ffbcc | 2991 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
0907d710 | 2992 | return 0; |
0f113f3e | 2993 | } |
0907d710 | 2994 | } |
0f113f3e | 2995 | |
e7db9680 MC |
2996 | outlen = SSL_MAX_MASTER_KEY_LENGTH; |
2997 | rsa_decrypt = OPENSSL_malloc(outlen); | |
0907d710 | 2998 | if (rsa_decrypt == NULL) { |
e077455e | 2999 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
0907d710 MC |
3000 | return 0; |
3001 | } | |
0f113f3e | 3002 | |
38b051a1 | 3003 | ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, rsa, sctx->propq); |
e7db9680 | 3004 | if (ctx == NULL) { |
e077455e | 3005 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
0907d710 | 3006 | goto err; |
f63a17d6 | 3007 | } |
0f113f3e | 3008 | |
0907d710 | 3009 | /* |
e7db9680 MC |
3010 | * We must not leak whether a decryption failure occurs because of |
3011 | * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246, | |
3012 | * section 7.4.7.1). We use the special padding type | |
e304aa87 | 3013 | * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automatically decrypt the |
e7db9680 MC |
3014 | * RSA, check the padding and check that the client version is as expected |
3015 | * in the premaster secret. If any of that fails then the function appears | |
3016 | * to return successfully but with a random result. The call below could | |
3017 | * still fail if the input is publicly invalid. | |
3018 | * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1 | |
0907d710 | 3019 | */ |
e7db9680 MC |
3020 | if (EVP_PKEY_decrypt_init(ctx) <= 0 |
3021 | || EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_WITH_TLS_PADDING) <= 0) { | |
c48ffbcc | 3022 | SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED); |
0907d710 | 3023 | goto err; |
f63a17d6 | 3024 | } |
20ca916d | 3025 | |
e7db9680 MC |
3026 | *p++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, |
3027 | (unsigned int *)&s->client_version); | |
3028 | if ((s->options & SSL_OP_TLS_ROLLBACK_BUG) != 0) | |
3029 | *p++ = OSSL_PARAM_construct_uint( | |
3030 | OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, | |
3031 | (unsigned int *)&s->version); | |
3032 | *p++ = OSSL_PARAM_construct_end(); | |
5b8fa431 | 3033 | |
e7db9680 MC |
3034 | if (!EVP_PKEY_CTX_set_params(ctx, params) |
3035 | || EVP_PKEY_decrypt(ctx, rsa_decrypt, &outlen, | |
3036 | PACKET_data(&enc_premaster), | |
3037 | PACKET_remaining(&enc_premaster)) <= 0) { | |
c48ffbcc | 3038 | SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED); |
0907d710 MC |
3039 | goto err; |
3040 | } | |
0f113f3e | 3041 | |
0907d710 | 3042 | /* |
e7db9680 MC |
3043 | * This test should never fail (otherwise we should have failed above) but |
3044 | * we double check anyway. | |
0907d710 | 3045 | */ |
e7db9680 MC |
3046 | if (outlen != SSL_MAX_MASTER_KEY_LENGTH) { |
3047 | OPENSSL_cleanse(rsa_decrypt, SSL_MAX_MASTER_KEY_LENGTH); | |
c48ffbcc | 3048 | SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED); |
e7db9680 | 3049 | goto err; |
0907d710 | 3050 | } |
0f113f3e | 3051 | |
e7db9680 MC |
3052 | /* Also cleanses rsa_decrypt (on success or failure) */ |
3053 | if (!ssl_generate_master_secret(s, rsa_decrypt, | |
3054 | SSL_MAX_MASTER_KEY_LENGTH, 0)) { | |
f63a17d6 | 3055 | /* SSLfatal() already called */ |
0907d710 MC |
3056 | goto err; |
3057 | } | |
0f113f3e | 3058 | |
0907d710 MC |
3059 | ret = 1; |
3060 | err: | |
3061 | OPENSSL_free(rsa_decrypt); | |
e7db9680 | 3062 | EVP_PKEY_CTX_free(ctx); |
0907d710 | 3063 | return ret; |
0907d710 MC |
3064 | } |
3065 | ||
38b051a1 | 3066 | static int tls_process_cke_dhe(SSL_CONNECTION *s, PACKET *pkt) |
642360f9 | 3067 | { |
642360f9 | 3068 | EVP_PKEY *skey = NULL; |
642360f9 | 3069 | unsigned int i; |
642360f9 MC |
3070 | const unsigned char *data; |
3071 | EVP_PKEY *ckey = NULL; | |
3072 | int ret = 0; | |
3073 | ||
31a7d80d | 3074 | if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) { |
c48ffbcc | 3075 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); |
642360f9 MC |
3076 | goto err; |
3077 | } | |
555cbb32 | 3078 | skey = s->s3.tmp.pkey; |
642360f9 | 3079 | if (skey == NULL) { |
c48ffbcc | 3080 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY); |
642360f9 MC |
3081 | goto err; |
3082 | } | |
3083 | ||
3084 | if (PACKET_remaining(pkt) == 0L) { | |
c48ffbcc | 3085 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_MISSING_TMP_DH_KEY); |
642360f9 MC |
3086 | goto err; |
3087 | } | |
3088 | if (!PACKET_get_bytes(pkt, &data, i)) { | |
3089 | /* We already checked we have enough data */ | |
c48ffbcc | 3090 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
642360f9 MC |
3091 | goto err; |
3092 | } | |
3093 | ckey = EVP_PKEY_new(); | |
3094 | if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) { | |
c48ffbcc | 3095 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED); |
642360f9 MC |
3096 | goto err; |
3097 | } | |
b6ff436f | 3098 | |
936d5657 | 3099 | if (!EVP_PKEY_set1_encoded_public_key(ckey, data, i)) { |
c48ffbcc | 3100 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
642360f9 MC |
3101 | goto err; |
3102 | } | |
3103 | ||
0f1e51ea | 3104 | if (ssl_derive(s, skey, ckey, 1) == 0) { |
f63a17d6 | 3105 | /* SSLfatal() already called */ |
642360f9 MC |
3106 | goto err; |
3107 | } | |
3108 | ||
3109 | ret = 1; | |
555cbb32 TS |
3110 | EVP_PKEY_free(s->s3.tmp.pkey); |
3111 | s->s3.tmp.pkey = NULL; | |
642360f9 MC |
3112 | err: |
3113 | EVP_PKEY_free(ckey); | |
3114 | return ret; | |
642360f9 MC |
3115 | } |
3116 | ||
38b051a1 | 3117 | static int tls_process_cke_ecdhe(SSL_CONNECTION *s, PACKET *pkt) |
19ed1ec1 | 3118 | { |
555cbb32 | 3119 | EVP_PKEY *skey = s->s3.tmp.pkey; |
19ed1ec1 MC |
3120 | EVP_PKEY *ckey = NULL; |
3121 | int ret = 0; | |
3122 | ||
3123 | if (PACKET_remaining(pkt) == 0L) { | |
3124 | /* We don't support ECDH client auth */ | |
c48ffbcc | 3125 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_TMP_ECDH_KEY); |
19ed1ec1 MC |
3126 | goto err; |
3127 | } else { | |
3128 | unsigned int i; | |
3129 | const unsigned char *data; | |
3130 | ||
3131 | /* | |
3132 | * Get client's public key from encoded point in the | |
3133 | * ClientKeyExchange message. | |
3134 | */ | |
3135 | ||
3136 | /* Get encoded point length */ | |
fb933982 DSH |
3137 | if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i) |
3138 | || PACKET_remaining(pkt) != 0) { | |
c48ffbcc | 3139 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
19ed1ec1 MC |
3140 | goto err; |
3141 | } | |
61bef9bd | 3142 | if (skey == NULL) { |
c48ffbcc | 3143 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_ECDH_KEY); |
61bef9bd MA |
3144 | goto err; |
3145 | } | |
3146 | ||
19ed1ec1 MC |
3147 | ckey = EVP_PKEY_new(); |
3148 | if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) { | |
c48ffbcc | 3149 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED); |
19ed1ec1 MC |
3150 | goto err; |
3151 | } | |
afce590b | 3152 | |
5ac8fb58 | 3153 | if (EVP_PKEY_set1_encoded_public_key(ckey, data, i) <= 0) { |
c48ffbcc | 3154 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB); |
19ed1ec1 MC |
3155 | goto err; |
3156 | } | |
3157 | } | |
3158 | ||
0f1e51ea | 3159 | if (ssl_derive(s, skey, ckey, 1) == 0) { |
f63a17d6 | 3160 | /* SSLfatal() already called */ |
19ed1ec1 MC |
3161 | goto err; |
3162 | } | |
3163 | ||
3164 | ret = 1; | |
555cbb32 TS |
3165 | EVP_PKEY_free(s->s3.tmp.pkey); |
3166 | s->s3.tmp.pkey = NULL; | |
19ed1ec1 MC |
3167 | err: |
3168 | EVP_PKEY_free(ckey); | |
3169 | ||
3170 | return ret; | |
19ed1ec1 MC |
3171 | } |
3172 | ||
38b051a1 | 3173 | static int tls_process_cke_srp(SSL_CONNECTION *s, PACKET *pkt) |
c437eef6 MC |
3174 | { |
3175 | #ifndef OPENSSL_NO_SRP | |
3176 | unsigned int i; | |
3177 | const unsigned char *data; | |
3178 | ||
3179 | if (!PACKET_get_net_2(pkt, &i) | |
a230b26e | 3180 | || !PACKET_get_bytes(pkt, &data, i)) { |
c48ffbcc | 3181 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRP_A_LENGTH); |
c437eef6 MC |
3182 | return 0; |
3183 | } | |
3184 | if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) { | |
c48ffbcc | 3185 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB); |
c437eef6 MC |
3186 | return 0; |
3187 | } | |
a230b26e | 3188 | if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) { |
c48ffbcc | 3189 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRP_PARAMETERS); |
c437eef6 MC |
3190 | return 0; |
3191 | } | |
3192 | OPENSSL_free(s->session->srp_username); | |
3193 | s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login); | |
3194 | if (s->session->srp_username == NULL) { | |
e077455e | 3195 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
c437eef6 MC |
3196 | return 0; |
3197 | } | |
3198 | ||
3199 | if (!srp_generate_server_master_secret(s)) { | |
f63a17d6 | 3200 | /* SSLfatal() already called */ |
c437eef6 MC |
3201 | return 0; |
3202 | } | |
3203 | ||
3204 | return 1; | |
3205 | #else | |
3206 | /* Should never happen */ | |
c48ffbcc | 3207 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
c437eef6 MC |
3208 | return 0; |
3209 | #endif | |
3210 | } | |
3211 | ||
38b051a1 | 3212 | static int tls_process_cke_gost(SSL_CONNECTION *s, PACKET *pkt) |
c437eef6 MC |
3213 | { |
3214 | #ifndef OPENSSL_NO_GOST | |
3215 | EVP_PKEY_CTX *pkey_ctx; | |
3216 | EVP_PKEY *client_pub_pkey = NULL, *pk = NULL; | |
3217 | unsigned char premaster_secret[32]; | |
3218 | const unsigned char *start; | |
3219 | size_t outlen = 32, inlen; | |
3220 | unsigned long alg_a; | |
4e3ee452 DB |
3221 | GOST_KX_MESSAGE *pKX = NULL; |
3222 | const unsigned char *ptr; | |
c437eef6 | 3223 | int ret = 0; |
38b051a1 | 3224 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
c437eef6 MC |
3225 | |
3226 | /* Get our certificate private key */ | |
555cbb32 | 3227 | alg_a = s->s3.tmp.new_cipher->algorithm_auth; |
c437eef6 MC |
3228 | if (alg_a & SSL_aGOST12) { |
3229 | /* | |
3230 | * New GOST ciphersuites have SSL_aGOST01 bit too | |
3231 | */ | |
3232 | pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey; | |
3233 | if (pk == NULL) { | |
3234 | pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey; | |
3235 | } | |
3236 | if (pk == NULL) { | |
3237 | pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey; | |
3238 | } | |
3239 | } else if (alg_a & SSL_aGOST01) { | |
3240 | pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey; | |
3241 | } | |
3242 | ||
38b051a1 | 3243 | pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq); |
c437eef6 | 3244 | if (pkey_ctx == NULL) { |
e077455e | 3245 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
c437eef6 MC |
3246 | return 0; |
3247 | } | |
3248 | if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) { | |
c48ffbcc | 3249 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
c437eef6 MC |
3250 | return 0; |
3251 | } | |
3252 | /* | |
3253 | * If client certificate is present and is of the same type, maybe | |
3254 | * use it for key exchange. Don't mind errors from | |
3255 | * EVP_PKEY_derive_set_peer, because it is completely valid to use a | |
3256 | * client certificate for authorization only. | |
3257 | */ | |
3c95ef22 | 3258 | client_pub_pkey = tls_get_peer_pkey(s); |
c437eef6 MC |
3259 | if (client_pub_pkey) { |
3260 | if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0) | |
3261 | ERR_clear_error(); | |
3262 | } | |
4e3ee452 DB |
3263 | |
3264 | ptr = PACKET_data(pkt); | |
3265 | /* Some implementations provide extra data in the opaqueBlob | |
3266 | * We have nothing to do with this blob so we just skip it */ | |
3267 | pKX = d2i_GOST_KX_MESSAGE(NULL, &ptr, PACKET_remaining(pkt)); | |
3268 | if (pKX == NULL | |
3269 | || pKX->kxBlob == NULL | |
3270 | || ASN1_TYPE_get(pKX->kxBlob) != V_ASN1_SEQUENCE) { | |
c48ffbcc | 3271 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED); |
4e3ee452 | 3272 | goto err; |
c437eef6 | 3273 | } |
4e3ee452 DB |
3274 | |
3275 | if (!PACKET_forward(pkt, ptr - PACKET_data(pkt))) { | |
c48ffbcc | 3276 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED); |
803cc8c7 | 3277 | goto err; |
4e3ee452 | 3278 | } |
803cc8c7 | 3279 | |
4e3ee452 | 3280 | if (PACKET_remaining(pkt) != 0) { |
c48ffbcc | 3281 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED); |
c437eef6 MC |
3282 | goto err; |
3283 | } | |
4e3ee452 DB |
3284 | |
3285 | inlen = pKX->kxBlob->value.sequence->length; | |
3286 | start = pKX->kxBlob->value.sequence->data; | |
803cc8c7 | 3287 | |
f63a17d6 MC |
3288 | if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start, |
3289 | inlen) <= 0) { | |
c48ffbcc | 3290 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED); |
c437eef6 MC |
3291 | goto err; |
3292 | } | |
3293 | /* Generate master secret */ | |
3294 | if (!ssl_generate_master_secret(s, premaster_secret, | |
3295 | sizeof(premaster_secret), 0)) { | |
f63a17d6 | 3296 | /* SSLfatal() already called */ |
c437eef6 MC |
3297 | goto err; |
3298 | } | |
3299 | /* Check if pubkey from client certificate was used */ | |
f63a17d6 MC |
3300 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, |
3301 | NULL) > 0) | |
c437eef6 MC |
3302 | s->statem.no_cert_verify = 1; |
3303 | ||
3304 | ret = 1; | |
3305 | err: | |
3306 | EVP_PKEY_CTX_free(pkey_ctx); | |
4e3ee452 | 3307 | GOST_KX_MESSAGE_free(pKX); |
c437eef6 MC |
3308 | return ret; |
3309 | #else | |
3310 | /* Should never happen */ | |
c48ffbcc | 3311 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
c437eef6 MC |
3312 | return 0; |
3313 | #endif | |
3314 | } | |
3315 | ||
38b051a1 | 3316 | static int tls_process_cke_gost18(SSL_CONNECTION *s, PACKET *pkt) |
5a5530a2 DB |
3317 | { |
3318 | #ifndef OPENSSL_NO_GOST | |
3319 | unsigned char rnd_dgst[32]; | |
3320 | EVP_PKEY_CTX *pkey_ctx = NULL; | |
3321 | EVP_PKEY *pk = NULL; | |
3322 | unsigned char premaster_secret[32]; | |
3323 | const unsigned char *start = NULL; | |
3324 | size_t outlen = 32, inlen = 0; | |
3325 | int ret = 0; | |
6dd4b77a | 3326 | int cipher_nid = ossl_gost18_cke_cipher_nid(s); |
38b051a1 | 3327 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
5a5530a2 DB |
3328 | |
3329 | if (cipher_nid == NID_undef) { | |
c48ffbcc | 3330 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
5a5530a2 DB |
3331 | return 0; |
3332 | } | |
3333 | ||
6dd4b77a | 3334 | if (ossl_gost_ukm(s, rnd_dgst) <= 0) { |
c48ffbcc | 3335 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
5a5530a2 DB |
3336 | goto err; |
3337 | } | |
3338 | ||
3339 | /* Get our certificate private key */ | |
3340 | pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey != NULL ? | |
3341 | s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey : | |
3342 | s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey; | |
3343 | if (pk == NULL) { | |
c48ffbcc | 3344 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE); |
5a5530a2 DB |
3345 | goto err; |
3346 | } | |
3347 | ||
38b051a1 | 3348 | pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq); |
5a5530a2 | 3349 | if (pkey_ctx == NULL) { |
e077455e | 3350 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
5a5530a2 DB |
3351 | goto err; |
3352 | } | |
3353 | if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) { | |
c48ffbcc | 3354 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
5a5530a2 DB |
3355 | goto err; |
3356 | } | |
3357 | ||
3358 | /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */ | |
3359 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT, | |
7b1264ba | 3360 | EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst) <= 0) { |
c48ffbcc | 3361 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); |
5a5530a2 DB |
3362 | goto err; |
3363 | } | |
3364 | ||
3365 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT, | |
7b1264ba | 3366 | EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL) <= 0) { |
c48ffbcc | 3367 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); |
5a5530a2 DB |
3368 | goto err; |
3369 | } | |
3370 | inlen = PACKET_remaining(pkt); | |
3371 | start = PACKET_data(pkt); | |
3372 | ||
3373 | if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) { | |
c48ffbcc | 3374 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED); |
5a5530a2 DB |
3375 | goto err; |
3376 | } | |
3377 | /* Generate master secret */ | |
3378 | if (!ssl_generate_master_secret(s, premaster_secret, | |
3379 | sizeof(premaster_secret), 0)) { | |
3380 | /* SSLfatal() already called */ | |
3381 | goto err; | |
3382 | } | |
3383 | ret = 1; | |
3384 | ||
3385 | err: | |
3386 | EVP_PKEY_CTX_free(pkey_ctx); | |
3387 | return ret; | |
3388 | #else | |
3389 | /* Should never happen */ | |
c48ffbcc | 3390 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
5a5530a2 DB |
3391 | return 0; |
3392 | #endif | |
3393 | } | |
3394 | ||
38b051a1 TM |
3395 | MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL_CONNECTION *s, |
3396 | PACKET *pkt) | |
0907d710 | 3397 | { |
0907d710 MC |
3398 | unsigned long alg_k; |
3399 | ||
555cbb32 | 3400 | alg_k = s->s3.tmp.new_cipher->algorithm_mkey; |
0907d710 MC |
3401 | |
3402 | /* For PSK parse and retrieve identity, obtain PSK key */ | |
f63a17d6 MC |
3403 | if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) { |
3404 | /* SSLfatal() already called */ | |
0907d710 | 3405 | goto err; |
f63a17d6 | 3406 | } |
0907d710 MC |
3407 | |
3408 | if (alg_k & SSL_kPSK) { | |
3409 | /* Identity extracted earlier: should be nothing left */ | |
3410 | if (PACKET_remaining(pkt) != 0) { | |
c48ffbcc | 3411 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
9059eb71 | 3412 | goto err; |
0907d710 MC |
3413 | } |
3414 | /* PSK handled by ssl_generate_master_secret */ | |
3415 | if (!ssl_generate_master_secret(s, NULL, 0, 0)) { | |
f63a17d6 | 3416 | /* SSLfatal() already called */ |
9059eb71 | 3417 | goto err; |
69f68237 | 3418 | } |
0907d710 | 3419 | } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) { |
f63a17d6 MC |
3420 | if (!tls_process_cke_rsa(s, pkt)) { |
3421 | /* SSLfatal() already called */ | |
0907d710 | 3422 | goto err; |
f63a17d6 | 3423 | } |
642360f9 | 3424 | } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
f63a17d6 MC |
3425 | if (!tls_process_cke_dhe(s, pkt)) { |
3426 | /* SSLfatal() already called */ | |
0f113f3e | 3427 | goto err; |
f63a17d6 | 3428 | } |
19ed1ec1 | 3429 | } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
f63a17d6 MC |
3430 | if (!tls_process_cke_ecdhe(s, pkt)) { |
3431 | /* SSLfatal() already called */ | |
19ed1ec1 | 3432 | goto err; |
f63a17d6 | 3433 | } |
c437eef6 | 3434 | } else if (alg_k & SSL_kSRP) { |
f63a17d6 MC |
3435 | if (!tls_process_cke_srp(s, pkt)) { |
3436 | /* SSLfatal() already called */ | |
0f113f3e | 3437 | goto err; |
f63a17d6 | 3438 | } |
c437eef6 | 3439 | } else if (alg_k & SSL_kGOST) { |
f63a17d6 MC |
3440 | if (!tls_process_cke_gost(s, pkt)) { |
3441 | /* SSLfatal() already called */ | |
0f113f3e | 3442 | goto err; |
f63a17d6 | 3443 | } |
5a5530a2 DB |
3444 | } else if (alg_k & SSL_kGOST18) { |
3445 | if (!tls_process_cke_gost18(s, pkt)) { | |
3446 | /* SSLfatal() already called */ | |
3447 | goto err; | |
3448 | } | |
c437eef6 | 3449 | } else { |
c48ffbcc | 3450 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE); |
9059eb71 | 3451 | goto err; |
0f113f3e MC |
3452 | } |
3453 | ||
e27f234a | 3454 | return MSG_PROCESS_CONTINUE_PROCESSING; |
0f113f3e | 3455 | err: |
85269210 | 3456 | #ifndef OPENSSL_NO_PSK |
555cbb32 TS |
3457 | OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen); |
3458 | s->s3.tmp.psk = NULL; | |
39a14059 | 3459 | s->s3.tmp.psklen = 0; |
58964a49 | 3460 | #endif |
e27f234a | 3461 | return MSG_PROCESS_ERROR; |
0f113f3e | 3462 | } |
d02b48c6 | 3463 | |
38b051a1 TM |
3464 | WORK_STATE tls_post_process_client_key_exchange(SSL_CONNECTION *s, |
3465 | WORK_STATE wst) | |
94836de2 | 3466 | { |
94836de2 | 3467 | #ifndef OPENSSL_NO_SCTP |
c130dd8e | 3468 | if (wst == WORK_MORE_A) { |
38b051a1 | 3469 | if (SSL_CONNECTION_IS_DTLS(s)) { |
c130dd8e MC |
3470 | unsigned char sctpauthkey[64]; |
3471 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; | |
09d62b33 | 3472 | size_t labellen; |
c130dd8e MC |
3473 | /* |
3474 | * Add new shared key for SCTP-Auth, will be ignored if no SCTP | |
3475 | * used. | |
3476 | */ | |
141eb8c6 MC |
3477 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
3478 | sizeof(DTLS1_SCTP_AUTH_LABEL)); | |
c130dd8e | 3479 | |
09d62b33 MT |
3480 | /* Don't include the terminating zero. */ |
3481 | labellen = sizeof(labelbuffer) - 1; | |
3482 | if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG) | |
3483 | labellen += 1; | |
3484 | ||
38b051a1 TM |
3485 | if (SSL_export_keying_material(SSL_CONNECTION_GET_SSL(s), |
3486 | sctpauthkey, | |
a230b26e | 3487 | sizeof(sctpauthkey), labelbuffer, |
09d62b33 | 3488 | labellen, NULL, 0, |
a230b26e | 3489 | 0) <= 0) { |
c48ffbcc | 3490 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
0fe2a0af | 3491 | return WORK_ERROR; |
c130dd8e | 3492 | } |
94836de2 | 3493 | |
846975f3 | 3494 | BIO_ctrl(s->wbio, BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, |
c130dd8e | 3495 | sizeof(sctpauthkey), sctpauthkey); |
94836de2 | 3496 | } |
94836de2 MC |
3497 | } |
3498 | #endif | |
3499 | ||
3c95ef22 | 3500 | if (s->statem.no_cert_verify || !received_client_cert(s)) { |
a230b26e EK |
3501 | /* |
3502 | * No certificate verify or no peer certificate so we no longer need | |
3503 | * the handshake_buffer | |
149c2ef5 MC |
3504 | */ |
3505 | if (!ssl3_digest_cached_records(s, 0)) { | |
f63a17d6 | 3506 | /* SSLfatal() already called */ |
149c2ef5 MC |
3507 | return WORK_ERROR; |
3508 | } | |
94836de2 | 3509 | return WORK_FINISHED_CONTINUE; |
28f4580c | 3510 | } else { |
555cbb32 | 3511 | if (!s->s3.handshake_buffer) { |
c48ffbcc | 3512 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
94836de2 MC |
3513 | return WORK_ERROR; |
3514 | } | |
3515 | /* | |
3516 | * For sigalgs freeze the handshake buffer. If we support | |
3517 | * extms we've done this already so this is a no-op | |
3518 | */ | |
3519 | if (!ssl3_digest_cached_records(s, 1)) { | |
f63a17d6 | 3520 | /* SSLfatal() already called */ |
94836de2 MC |
3521 | return WORK_ERROR; |
3522 | } | |
94836de2 MC |
3523 | } |
3524 | ||
3525 | return WORK_FINISHED_CONTINUE; | |
3526 | } | |
3527 | ||
3c95ef22 TS |
3528 | MSG_PROCESS_RETURN tls_process_client_rpk(SSL_CONNECTION *sc, PACKET *pkt) |
3529 | { | |
3530 | MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR; | |
3531 | SSL_SESSION *new_sess = NULL; | |
3532 | EVP_PKEY *peer_rpk = NULL; | |
3533 | ||
3534 | if (!tls_process_rpk(sc, pkt, &peer_rpk)) { | |
3535 | /* SSLfatal already called */ | |
3536 | goto err; | |
3537 | } | |
3538 | ||
3539 | if (peer_rpk == NULL) { | |
3540 | if ((sc->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) | |
3541 | && (sc->verify_mode & SSL_VERIFY_PEER)) { | |
3542 | SSLfatal(sc, SSL_AD_CERTIFICATE_REQUIRED, | |
3543 | SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); | |
3544 | goto err; | |
3545 | } | |
3546 | } else { | |
3547 | if (ssl_verify_rpk(sc, peer_rpk) <= 0) { | |
3548 | SSLfatal(sc, ssl_x509err2alert(sc->verify_result), | |
3549 | SSL_R_CERTIFICATE_VERIFY_FAILED); | |
3550 | goto err; | |
3551 | } | |
3552 | } | |
3553 | ||
3554 | /* | |
3555 | * Sessions must be immutable once they go into the session cache. Otherwise | |
3556 | * we can get multi-thread problems. Therefore we don't "update" sessions, | |
3557 | * we replace them with a duplicate. Here, we need to do this every time | |
3558 | * a new RPK (or certificate) is received via post-handshake authentication, | |
3559 | * as the session may have already gone into the session cache. | |
3560 | */ | |
3561 | ||
3562 | if (sc->post_handshake_auth == SSL_PHA_REQUESTED) { | |
3563 | if ((new_sess = ssl_session_dup(sc->session, 0)) == NULL) { | |
3564 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE); | |
3565 | goto err; | |
3566 | } | |
3567 | ||
3568 | SSL_SESSION_free(sc->session); | |
3569 | sc->session = new_sess; | |
3570 | } | |
3571 | ||
3572 | /* Ensure there is no peer/peer_chain */ | |
3573 | X509_free(sc->session->peer); | |
3574 | sc->session->peer = NULL; | |
3575 | sk_X509_pop_free(sc->session->peer_chain, X509_free); | |
3576 | sc->session->peer_chain = NULL; | |
3577 | /* Save RPK */ | |
3578 | EVP_PKEY_free(sc->session->peer_rpk); | |
3579 | sc->session->peer_rpk = peer_rpk; | |
3580 | peer_rpk = NULL; | |
3581 | ||
3582 | sc->session->verify_result = sc->verify_result; | |
3583 | ||
3584 | /* | |
3585 | * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE | |
3586 | * message | |
3587 | */ | |
3588 | if (SSL_CONNECTION_IS_TLS13(sc)) { | |
3589 | if (!ssl3_digest_cached_records(sc, 1)) { | |
3590 | /* SSLfatal() already called */ | |
3591 | goto err; | |
3592 | } | |
3593 | ||
3594 | /* Save the current hash state for when we receive the CertificateVerify */ | |
3595 | if (!ssl_handshake_hash(sc, sc->cert_verify_hash, | |
3596 | sizeof(sc->cert_verify_hash), | |
3597 | &sc->cert_verify_hash_len)) { | |
3598 | /* SSLfatal() already called */; | |
3599 | goto err; | |
3600 | } | |
3601 | ||
3602 | /* resend session tickets */ | |
3603 | sc->sent_tickets = 0; | |
3604 | } | |
3605 | ||
3606 | ret = MSG_PROCESS_CONTINUE_READING; | |
3607 | ||
3608 | err: | |
3609 | EVP_PKEY_free(peer_rpk); | |
3610 | return ret; | |
3611 | } | |
3612 | ||
38b051a1 TM |
3613 | MSG_PROCESS_RETURN tls_process_client_certificate(SSL_CONNECTION *s, |
3614 | PACKET *pkt) | |
e27f234a | 3615 | { |
f63a17d6 | 3616 | int i; |
eb5fd03b | 3617 | MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR; |
e27f234a | 3618 | X509 *x = NULL; |
9d75dce3 | 3619 | unsigned long l; |
b6981744 | 3620 | const unsigned char *certstart, *certbytes; |
e27f234a | 3621 | STACK_OF(X509) *sk = NULL; |
e96e0f8e | 3622 | PACKET spkt, context; |
d805a57b | 3623 | size_t chainidx; |
9d75dce3 | 3624 | SSL_SESSION *new_sess = NULL; |
38b051a1 | 3625 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
0f113f3e | 3626 | |
de9e884b MC |
3627 | /* |
3628 | * To get this far we must have read encrypted data from the client. We no | |
1853d20a | 3629 | * longer tolerate unencrypted alerts. This is ignored if less than TLSv1.3 |
de9e884b | 3630 | */ |
cffafb5f MC |
3631 | if (s->rlayer.rrlmethod->set_plain_alerts != NULL) |
3632 | s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 0); | |
de9e884b | 3633 | |
3c95ef22 TS |
3634 | if (s->ext.client_cert_type == TLSEXT_cert_type_rpk) |
3635 | return tls_process_client_rpk(s, pkt); | |
3636 | ||
3637 | if (s->ext.client_cert_type != TLSEXT_cert_type_x509) { | |
3638 | SSLfatal(s, SSL_AD_UNSUPPORTED_CERTIFICATE, | |
3639 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); | |
3640 | goto err; | |
3641 | } | |
3642 | ||
0f113f3e | 3643 | if ((sk = sk_X509_new_null()) == NULL) { |
e077455e | 3644 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
f63a17d6 | 3645 | goto err; |
0f113f3e MC |
3646 | } |
3647 | ||
38b051a1 TM |
3648 | if (SSL_CONNECTION_IS_TLS13(s) |
3649 | && (!PACKET_get_length_prefixed_1(pkt, &context) | |
3650 | || (s->pha_context == NULL && PACKET_remaining(&context) != 0) | |
3651 | || (s->pha_context != NULL | |
3652 | && !PACKET_equal(&context, s->pha_context, | |
3653 | s->pha_context_len)))) { | |
c48ffbcc | 3654 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT); |
9d75dce3 TS |
3655 | goto err; |
3656 | } | |
3657 | ||
3658 | if (!PACKET_get_length_prefixed_3(pkt, &spkt) | |
e96e0f8e | 3659 | || PACKET_remaining(pkt) != 0) { |
c48ffbcc | 3660 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 3661 | goto err; |
0f113f3e | 3662 | } |
0bc09ecd | 3663 | |
d805a57b | 3664 | for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) { |
0bc09ecd | 3665 | if (!PACKET_get_net_3(&spkt, &l) |
a230b26e | 3666 | || !PACKET_get_bytes(&spkt, &certbytes, l)) { |
c48ffbcc | 3667 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH); |
f63a17d6 | 3668 | goto err; |
0f113f3e MC |
3669 | } |
3670 | ||
0bc09ecd | 3671 | certstart = certbytes; |
38b051a1 | 3672 | x = X509_new_ex(sctx->libctx, sctx->propq); |
0f113f3e | 3673 | if (x == NULL) { |
e077455e | 3674 | SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_X509_LIB); |
6725682d SL |
3675 | goto err; |
3676 | } | |
3677 | if (d2i_X509(&x, (const unsigned char **)&certbytes, l) == NULL) { | |
c48ffbcc | 3678 | SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB); |
f63a17d6 | 3679 | goto err; |
0f113f3e | 3680 | } |
6725682d | 3681 | |
0bc09ecd | 3682 | if (certbytes != (certstart + l)) { |
c48ffbcc | 3683 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH); |
f63a17d6 | 3684 | goto err; |
0f113f3e | 3685 | } |
e96e0f8e | 3686 | |
38b051a1 | 3687 | if (SSL_CONNECTION_IS_TLS13(s)) { |
e96e0f8e MC |
3688 | RAW_EXTENSION *rawexts = NULL; |
3689 | PACKET extensions; | |
3690 | ||
3691 | if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) { | |
c48ffbcc | 3692 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH); |
f63a17d6 | 3693 | goto err; |
e96e0f8e | 3694 | } |
fe874d27 MC |
3695 | if (!tls_collect_extensions(s, &extensions, |
3696 | SSL_EXT_TLS1_3_CERTIFICATE, &rawexts, | |
f63a17d6 | 3697 | NULL, chainidx == 0) |
8e1634ec | 3698 | || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE, |
f63a17d6 | 3699 | rawexts, x, chainidx, |
8e1634ec | 3700 | PACKET_remaining(&spkt) == 0)) { |
5ee289ea | 3701 | OPENSSL_free(rawexts); |
f63a17d6 | 3702 | goto err; |
5ee289ea MC |
3703 | } |
3704 | OPENSSL_free(rawexts); | |
e96e0f8e MC |
3705 | } |
3706 | ||
0f113f3e | 3707 | if (!sk_X509_push(sk, x)) { |
e077455e | 3708 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
f63a17d6 | 3709 | goto err; |
0f113f3e MC |
3710 | } |
3711 | x = NULL; | |
0f113f3e MC |
3712 | } |
3713 | ||
3714 | if (sk_X509_num(sk) <= 0) { | |
3715 | /* TLS does not mind 0 certs returned */ | |
3716 | if (s->version == SSL3_VERSION) { | |
f63a17d6 | 3717 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
f63a17d6 MC |
3718 | SSL_R_NO_CERTIFICATES_RETURNED); |
3719 | goto err; | |
0f113f3e MC |
3720 | } |
3721 | /* Fail for TLS only if we required a certificate */ | |
3722 | else if ((s->verify_mode & SSL_VERIFY_PEER) && | |
3723 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { | |
f63a17d6 | 3724 | SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED, |
f63a17d6 MC |
3725 | SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
3726 | goto err; | |
0f113f3e MC |
3727 | } |
3728 | /* No client certificate so digest cached records */ | |
555cbb32 | 3729 | if (s->s3.handshake_buffer && !ssl3_digest_cached_records(s, 0)) { |
f63a17d6 MC |
3730 | /* SSLfatal() already called */ |
3731 | goto err; | |
0f113f3e MC |
3732 | } |
3733 | } else { | |
3734 | EVP_PKEY *pkey; | |
3735 | i = ssl_verify_cert_chain(s, sk); | |
3736 | if (i <= 0) { | |
c6d38183 | 3737 | SSLfatal(s, ssl_x509err2alert(s->verify_result), |
f63a17d6 MC |
3738 | SSL_R_CERTIFICATE_VERIFY_FAILED); |
3739 | goto err; | |
0f113f3e | 3740 | } |
8382fd3a | 3741 | pkey = X509_get0_pubkey(sk_X509_value(sk, 0)); |
0f113f3e | 3742 | if (pkey == NULL) { |
f63a17d6 | 3743 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
f63a17d6 MC |
3744 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
3745 | goto err; | |
0f113f3e | 3746 | } |
0f113f3e MC |
3747 | } |
3748 | ||
9d75dce3 TS |
3749 | /* |
3750 | * Sessions must be immutable once they go into the session cache. Otherwise | |
3751 | * we can get multi-thread problems. Therefore we don't "update" sessions, | |
3752 | * we replace them with a duplicate. Here, we need to do this every time | |
3753 | * a new certificate is received via post-handshake authentication, as the | |
3754 | * session may have already gone into the session cache. | |
3755 | */ | |
3756 | ||
3757 | if (s->post_handshake_auth == SSL_PHA_REQUESTED) { | |
9d75dce3 | 3758 | if ((new_sess = ssl_session_dup(s->session, 0)) == 0) { |
e077455e | 3759 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); |
9d75dce3 TS |
3760 | goto err; |
3761 | } | |
3762 | ||
9d75dce3 TS |
3763 | SSL_SESSION_free(s->session); |
3764 | s->session = new_sess; | |
3765 | } | |
3766 | ||
222561fe | 3767 | X509_free(s->session->peer); |
a8086e6b | 3768 | s->session->peer = sk_X509_shift(sk); |
0f113f3e MC |
3769 | s->session->verify_result = s->verify_result; |
3770 | ||
79b2a2f2 | 3771 | OSSL_STACK_OF_X509_free(s->session->peer_chain); |
c34b0f99 | 3772 | s->session->peer_chain = sk; |
0449702a | 3773 | sk = NULL; |
3c95ef22 TS |
3774 | /* Ensure there is no RPK */ |
3775 | EVP_PKEY_free(s->session->peer_rpk); | |
3776 | s->session->peer_rpk = NULL; | |
0f1e51ea MC |
3777 | |
3778 | /* | |
3779 | * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE | |
3780 | * message | |
3781 | */ | |
38b051a1 | 3782 | if (SSL_CONNECTION_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) { |
f63a17d6 MC |
3783 | /* SSLfatal() already called */ |
3784 | goto err; | |
0f1e51ea MC |
3785 | } |
3786 | ||
0f113f3e MC |
3787 | /* |
3788 | * Inconsistency alert: cert_chain does *not* include the peer's own | |
d4d78943 | 3789 | * certificate, while we do include it in statem_clnt.c |
0f113f3e | 3790 | */ |
2c5dfdc3 MC |
3791 | |
3792 | /* Save the current hash state for when we receive the CertificateVerify */ | |
38b051a1 | 3793 | if (SSL_CONNECTION_IS_TLS13(s)) { |
36ff232c MC |
3794 | if (!ssl_handshake_hash(s, s->cert_verify_hash, |
3795 | sizeof(s->cert_verify_hash), | |
3796 | &s->cert_verify_hash_len)) { | |
3797 | /* SSLfatal() already called */ | |
3798 | goto err; | |
3799 | } | |
3800 | ||
3801 | /* Resend session tickets */ | |
3802 | s->sent_tickets = 0; | |
2c5dfdc3 MC |
3803 | } |
3804 | ||
e27f234a | 3805 | ret = MSG_PROCESS_CONTINUE_READING; |
66696478 | 3806 | |
f63a17d6 | 3807 | err: |
222561fe | 3808 | X509_free(x); |
79b2a2f2 | 3809 | OSSL_STACK_OF_X509_free(sk); |
e27f234a | 3810 | return ret; |
0f113f3e | 3811 | } |
d02b48c6 | 3812 | |
b67cb09f TS |
3813 | #ifndef OPENSSL_NO_COMP_ALG |
3814 | MSG_PROCESS_RETURN tls_process_client_compressed_certificate(SSL_CONNECTION *sc, PACKET *pkt) | |
3815 | { | |
3816 | MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR; | |
3817 | PACKET tmppkt; | |
3818 | BUF_MEM *buf = BUF_MEM_new(); | |
3819 | ||
3820 | if (tls13_process_compressed_certificate(sc, pkt, &tmppkt, buf) != MSG_PROCESS_ERROR) | |
3821 | ret = tls_process_client_certificate(sc, &tmppkt); | |
3822 | ||
3823 | BUF_MEM_free(buf); | |
3824 | return ret; | |
3825 | } | |
3826 | #endif | |
3827 | ||
67ec6d2b | 3828 | CON_FUNC_RETURN tls_construct_server_certificate(SSL_CONNECTION *s, WPACKET *pkt) |
e27f234a | 3829 | { |
555cbb32 | 3830 | CERT_PKEY *cpk = s->s3.tmp.cert; |
e27f234a | 3831 | |
a497cf25 | 3832 | if (cpk == NULL) { |
c48ffbcc | 3833 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 3834 | return CON_FUNC_ERROR; |
e27f234a MC |
3835 | } |
3836 | ||
e96e0f8e MC |
3837 | /* |
3838 | * In TLSv1.3 the certificate chain is always preceded by a 0 length context | |
3839 | * for the server Certificate message | |
3840 | */ | |
38b051a1 | 3841 | if (SSL_CONNECTION_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) { |
c48ffbcc | 3842 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 3843 | return CON_FUNC_ERROR; |
f63a17d6 | 3844 | } |
3c95ef22 TS |
3845 | switch (s->ext.server_cert_type) { |
3846 | case TLSEXT_cert_type_rpk: | |
3847 | if (!tls_output_rpk(s, pkt, cpk)) { | |
3848 | /* SSLfatal() already called */ | |
3849 | return 0; | |
3850 | } | |
3851 | break; | |
3852 | case TLSEXT_cert_type_x509: | |
3853 | if (!ssl3_output_cert_chain(s, pkt, cpk, 0)) { | |
3854 | /* SSLfatal() already called */ | |
3855 | return 0; | |
3856 | } | |
3857 | break; | |
3858 | default: | |
3859 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
3860 | return 0; | |
e27f234a MC |
3861 | } |
3862 | ||
67ec6d2b | 3863 | return CON_FUNC_SUCCESS; |
e27f234a MC |
3864 | } |
3865 | ||
b67cb09f TS |
3866 | #ifndef OPENSSL_NO_COMP_ALG |
3867 | CON_FUNC_RETURN tls_construct_server_compressed_certificate(SSL_CONNECTION *sc, WPACKET *pkt) | |
3868 | { | |
3869 | int alg = get_compressed_certificate_alg(sc); | |
3870 | OSSL_COMP_CERT *cc = sc->s3.tmp.cert->comp_cert[alg]; | |
3871 | ||
3872 | if (!ossl_assert(cc != NULL)) { | |
3873 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
3874 | return 0; | |
3875 | } | |
3876 | /* | |
3877 | * Server can't compress on-demand | |
3878 | * Use pre-compressed certificate | |
3879 | */ | |
3880 | if (!WPACKET_put_bytes_u16(pkt, alg) | |
3881 | || !WPACKET_put_bytes_u24(pkt, cc->orig_len) | |
3882 | || !WPACKET_start_sub_packet_u24(pkt) | |
3883 | || !WPACKET_memcpy(pkt, cc->data, cc->len) | |
3884 | || !WPACKET_close(pkt)) | |
3885 | return 0; | |
3886 | ||
3887 | sc->s3.tmp.cert->cert_comp_used++; | |
3888 | return 1; | |
3889 | } | |
3890 | #endif | |
3891 | ||
38b051a1 TM |
3892 | static int create_ticket_prequel(SSL_CONNECTION *s, WPACKET *pkt, |
3893 | uint32_t age_add, unsigned char *tick_nonce) | |
6a11d5c5 | 3894 | { |
f0131dc0 | 3895 | uint32_t timeout = (uint32_t)ossl_time2seconds(s->session->timeout); |
0089cc7f | 3896 | |
6a11d5c5 | 3897 | /* |
0089cc7f | 3898 | * Ticket lifetime hint: |
6a11d5c5 | 3899 | * In TLSv1.3 we reset the "time" field above, and always specify the |
0089cc7f TS |
3900 | * timeout, limited to a 1 week period per RFC8446. |
3901 | * For TLSv1.2 this is advisory only and we leave this unspecified for | |
3902 | * resumed session (for simplicity). | |
6a11d5c5 | 3903 | */ |
0089cc7f TS |
3904 | #define ONE_WEEK_SEC (7 * 24 * 60 * 60) |
3905 | ||
38b051a1 | 3906 | if (SSL_CONNECTION_IS_TLS13(s)) { |
f0131dc0 P |
3907 | if (ossl_time_compare(s->session->timeout, |
3908 | ossl_seconds2time(ONE_WEEK_SEC)) > 0) | |
0089cc7f TS |
3909 | timeout = ONE_WEEK_SEC; |
3910 | } else if (s->hit) | |
3911 | timeout = 0; | |
3912 | ||
3913 | if (!WPACKET_put_bytes_u32(pkt, timeout)) { | |
c48ffbcc | 3914 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6a11d5c5 MC |
3915 | return 0; |
3916 | } | |
3917 | ||
38b051a1 | 3918 | if (SSL_CONNECTION_IS_TLS13(s)) { |
6a11d5c5 MC |
3919 | if (!WPACKET_put_bytes_u32(pkt, age_add) |
3920 | || !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) { | |
c48ffbcc | 3921 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6a11d5c5 MC |
3922 | return 0; |
3923 | } | |
3924 | } | |
3925 | ||
3926 | /* Start the sub-packet for the actual ticket data */ | |
3927 | if (!WPACKET_start_sub_packet_u16(pkt)) { | |
c48ffbcc | 3928 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6a11d5c5 MC |
3929 | return 0; |
3930 | } | |
3931 | ||
3932 | return 1; | |
3933 | } | |
3934 | ||
67ec6d2b MC |
3935 | static CON_FUNC_RETURN construct_stateless_ticket(SSL_CONNECTION *s, |
3936 | WPACKET *pkt, | |
3937 | uint32_t age_add, | |
3938 | unsigned char *tick_nonce) | |
e27f234a MC |
3939 | { |
3940 | unsigned char *senc = NULL; | |
83ae4661 | 3941 | EVP_CIPHER_CTX *ctx = NULL; |
a76ce286 | 3942 | SSL_HMAC *hctx = NULL; |
a00d75e1 | 3943 | unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2; |
e27f234a | 3944 | const unsigned char *const_p; |
a00d75e1 | 3945 | int len, slen_full, slen, lenfinal; |
e27f234a | 3946 | SSL_SESSION *sess; |
a76ce286 | 3947 | size_t hlen; |
222da979 | 3948 | SSL_CTX *tctx = s->session_ctx; |
e27f234a | 3949 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
d139723b | 3950 | unsigned char key_name[TLSEXT_KEYNAME_LENGTH]; |
67ec6d2b MC |
3951 | int iv_len; |
3952 | CON_FUNC_RETURN ok = CON_FUNC_ERROR; | |
a00d75e1 | 3953 | size_t macoffset, macendoffset; |
38b051a1 TM |
3954 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
3955 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); | |
df0fed9a | 3956 | |
e27f234a MC |
3957 | /* get session encoding length */ |
3958 | slen_full = i2d_SSL_SESSION(s->session, NULL); | |
3959 | /* | |
3960 | * Some length values are 16 bits, so forget it if session is too | |
3961 | * long | |
3962 | */ | |
3963 | if (slen_full == 0 || slen_full > 0xFF00) { | |
c48ffbcc | 3964 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f6370040 | 3965 | goto err; |
e27f234a MC |
3966 | } |
3967 | senc = OPENSSL_malloc(slen_full); | |
a71edf3b | 3968 | if (senc == NULL) { |
e077455e | 3969 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
f6370040 | 3970 | goto err; |
e27f234a | 3971 | } |
0f113f3e | 3972 | |
846ec07d | 3973 | ctx = EVP_CIPHER_CTX_new(); |
e077455e RL |
3974 | if (ctx == NULL) { |
3975 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); | |
3976 | goto err; | |
3977 | } | |
a76ce286 | 3978 | hctx = ssl_hmac_new(tctx); |
e077455e RL |
3979 | if (hctx == NULL) { |
3980 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); | |
83ae4661 MC |
3981 | goto err; |
3982 | } | |
0f113f3e | 3983 | |
e27f234a | 3984 | p = senc; |
f63a17d6 | 3985 | if (!i2d_SSL_SESSION(s->session, &p)) { |
c48ffbcc | 3986 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
e27f234a | 3987 | goto err; |
f63a17d6 | 3988 | } |
687eaf27 | 3989 | |
e27f234a MC |
3990 | /* |
3991 | * create a fresh copy (not shared with other threads) to clean up | |
3992 | */ | |
3993 | const_p = senc; | |
3c95ef22 TS |
3994 | sess = d2i_SSL_SESSION_ex(NULL, &const_p, slen_full, sctx->libctx, |
3995 | sctx->propq); | |
f63a17d6 | 3996 | if (sess == NULL) { |
c48ffbcc | 3997 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
e27f234a | 3998 | goto err; |
f63a17d6 | 3999 | } |
0f113f3e | 4000 | |
e27f234a | 4001 | slen = i2d_SSL_SESSION(sess, NULL); |
f63a17d6 MC |
4002 | if (slen == 0 || slen > slen_full) { |
4003 | /* shouldn't ever happen */ | |
c48ffbcc | 4004 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
e27f234a MC |
4005 | SSL_SESSION_free(sess); |
4006 | goto err; | |
4007 | } | |
4008 | p = senc; | |
4009 | if (!i2d_SSL_SESSION(sess, &p)) { | |
c48ffbcc | 4010 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
e27f234a MC |
4011 | SSL_SESSION_free(sess); |
4012 | goto err; | |
4013 | } | |
4014 | SSL_SESSION_free(sess); | |
0f113f3e | 4015 | |
e27f234a MC |
4016 | /* |
4017 | * Initialize HMAC and cipher contexts. If callback present it does | |
4018 | * all the work otherwise use generated values from parent ctx. | |
4019 | */ | |
a76ce286 P |
4020 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4021 | if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL) | |
4022 | #else | |
4023 | if (tctx->ext.ticket_key_evp_cb != NULL) | |
4024 | #endif | |
4025 | { | |
4026 | int ret = 0; | |
4027 | ||
4028 | if (tctx->ext.ticket_key_evp_cb != NULL) | |
38b051a1 | 4029 | ret = tctx->ext.ticket_key_evp_cb(ssl, key_name, iv, ctx, |
a76ce286 P |
4030 | ssl_hmac_get0_EVP_MAC_CTX(hctx), |
4031 | 1); | |
4032 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
4033 | else if (tctx->ext.ticket_key_cb != NULL) | |
4034 | /* if 0 is returned, write an empty ticket */ | |
38b051a1 | 4035 | ret = tctx->ext.ticket_key_cb(ssl, key_name, iv, ctx, |
a76ce286 P |
4036 | ssl_hmac_get0_HMAC_CTX(hctx), 1); |
4037 | #endif | |
5c753de6 TS |
4038 | |
4039 | if (ret == 0) { | |
3e93c5fe MC |
4040 | /* |
4041 | * In TLSv1.2 we construct a 0 length ticket. In TLSv1.3 a 0 | |
4042 | * length ticket is not allowed so we abort construction of the | |
4043 | * ticket | |
4044 | */ | |
4045 | if (SSL_CONNECTION_IS_TLS13(s)) { | |
67ec6d2b | 4046 | ok = CON_FUNC_DONT_SEND; |
3e93c5fe MC |
4047 | goto err; |
4048 | } | |
a00d75e1 | 4049 | /* Put timeout and length */ |
7cea05dc | 4050 | if (!WPACKET_put_bytes_u32(pkt, 0) |
4a01c59f | 4051 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
c48ffbcc | 4052 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
5c753de6 | 4053 | goto err; |
a00d75e1 | 4054 | } |
5c753de6 TS |
4055 | OPENSSL_free(senc); |
4056 | EVP_CIPHER_CTX_free(ctx); | |
a76ce286 | 4057 | ssl_hmac_free(hctx); |
67ec6d2b | 4058 | return CON_FUNC_SUCCESS; |
5c753de6 | 4059 | } |
f63a17d6 | 4060 | if (ret < 0) { |
c48ffbcc | 4061 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED); |
e27f234a | 4062 | goto err; |
f63a17d6 | 4063 | } |
ed576acd | 4064 | iv_len = EVP_CIPHER_CTX_get_iv_length(ctx); |
83ab43da DB |
4065 | if (iv_len < 0) { |
4066 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
4067 | goto err; | |
4068 | } | |
e27f234a | 4069 | } else { |
38b051a1 TM |
4070 | EVP_CIPHER *cipher = EVP_CIPHER_fetch(sctx->libctx, "AES-256-CBC", |
4071 | sctx->propq); | |
6f829f58 MC |
4072 | |
4073 | if (cipher == NULL) { | |
5a2d0ef3 RL |
4074 | /* Error is already recorded */ |
4075 | SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR); | |
6f829f58 MC |
4076 | goto err; |
4077 | } | |
d139723b | 4078 | |
ed576acd | 4079 | iv_len = EVP_CIPHER_get_iv_length(cipher); |
dfefa4c1 | 4080 | if (iv_len < 0 |
38b051a1 | 4081 | || RAND_bytes_ex(sctx->libctx, iv, iv_len, 0) <= 0 |
f63a17d6 | 4082 | || !EVP_EncryptInit_ex(ctx, cipher, NULL, |
4bfb96f2 | 4083 | tctx->ext.secure->tick_aes_key, iv) |
a76ce286 P |
4084 | || !ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key, |
4085 | sizeof(tctx->ext.secure->tick_hmac_key), | |
4086 | "SHA256")) { | |
6f829f58 | 4087 | EVP_CIPHER_free(cipher); |
c48ffbcc | 4088 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4f9fab6b | 4089 | goto err; |
f63a17d6 | 4090 | } |
6f829f58 | 4091 | EVP_CIPHER_free(cipher); |
aff8c126 RS |
4092 | memcpy(key_name, tctx->ext.tick_key_name, |
4093 | sizeof(tctx->ext.tick_key_name)); | |
0f113f3e MC |
4094 | } |
4095 | ||
6a11d5c5 MC |
4096 | if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) { |
4097 | /* SSLfatal() already called */ | |
4098 | goto err; | |
4099 | } | |
4100 | ||
4101 | if (!WPACKET_get_total_written(pkt, &macoffset) | |
a00d75e1 | 4102 | /* Output key name */ |
7cea05dc | 4103 | || !WPACKET_memcpy(pkt, key_name, sizeof(key_name)) |
a00d75e1 | 4104 | /* output IV */ |
7cea05dc MC |
4105 | || !WPACKET_memcpy(pkt, iv, iv_len) |
4106 | || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH, | |
a00d75e1 MC |
4107 | &encdata1) |
4108 | /* Encrypt session data */ | |
4109 | || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen) | |
7cea05dc | 4110 | || !WPACKET_allocate_bytes(pkt, len, &encdata2) |
a00d75e1 MC |
4111 | || encdata1 != encdata2 |
4112 | || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal) | |
7cea05dc | 4113 | || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2) |
a00d75e1 MC |
4114 | || encdata1 + len != encdata2 |
4115 | || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH | |
7cea05dc | 4116 | || !WPACKET_get_total_written(pkt, &macendoffset) |
a76ce286 P |
4117 | || !ssl_hmac_update(hctx, |
4118 | (unsigned char *)s->init_buf->data + macoffset, | |
4119 | macendoffset - macoffset) | |
7cea05dc | 4120 | || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1) |
a76ce286 | 4121 | || !ssl_hmac_final(hctx, macdata1, &hlen, EVP_MAX_MD_SIZE) |
a00d75e1 | 4122 | || hlen > EVP_MAX_MD_SIZE |
7cea05dc | 4123 | || !WPACKET_allocate_bytes(pkt, hlen, &macdata2) |
6a11d5c5 | 4124 | || macdata1 != macdata2) { |
c48ffbcc | 4125 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6a11d5c5 MC |
4126 | goto err; |
4127 | } | |
4128 | ||
4129 | /* Close the sub-packet created by create_ticket_prequel() */ | |
4130 | if (!WPACKET_close(pkt)) { | |
c48ffbcc | 4131 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
e27f234a | 4132 | goto err; |
a00d75e1 | 4133 | } |
6a11d5c5 | 4134 | |
67ec6d2b | 4135 | ok = CON_FUNC_SUCCESS; |
6a11d5c5 MC |
4136 | err: |
4137 | OPENSSL_free(senc); | |
4138 | EVP_CIPHER_CTX_free(ctx); | |
a76ce286 | 4139 | ssl_hmac_free(hctx); |
6a11d5c5 MC |
4140 | return ok; |
4141 | } | |
4142 | ||
38b051a1 TM |
4143 | static int construct_stateful_ticket(SSL_CONNECTION *s, WPACKET *pkt, |
4144 | uint32_t age_add, | |
6cc0b3c2 MC |
4145 | unsigned char *tick_nonce) |
4146 | { | |
4147 | if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) { | |
4148 | /* SSLfatal() already called */ | |
4149 | return 0; | |
4150 | } | |
4151 | ||
4152 | if (!WPACKET_memcpy(pkt, s->session->session_id, | |
4153 | s->session->session_id_length) | |
4154 | || !WPACKET_close(pkt)) { | |
c48ffbcc | 4155 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6cc0b3c2 MC |
4156 | return 0; |
4157 | } | |
4158 | ||
4159 | return 1; | |
4160 | } | |
4161 | ||
3e93c5fe MC |
4162 | static void tls_update_ticket_counts(SSL_CONNECTION *s) |
4163 | { | |
4164 | /* | |
4165 | * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets| | |
4166 | * gets reset to 0 if we send more tickets following a post-handshake | |
4167 | * auth, but |next_ticket_nonce| does not. If we're sending extra | |
4168 | * tickets, decrement the count of pending extra tickets. | |
4169 | */ | |
4170 | s->sent_tickets++; | |
4171 | s->next_ticket_nonce++; | |
4172 | if (s->ext.extra_tickets_expected > 0) | |
4173 | s->ext.extra_tickets_expected--; | |
4174 | } | |
4175 | ||
67ec6d2b | 4176 | CON_FUNC_RETURN tls_construct_new_session_ticket(SSL_CONNECTION *s, WPACKET *pkt) |
6a11d5c5 MC |
4177 | { |
4178 | SSL_CTX *tctx = s->session_ctx; | |
4179 | unsigned char tick_nonce[TICKET_NONCE_SIZE]; | |
4180 | union { | |
4181 | unsigned char age_add_c[sizeof(uint32_t)]; | |
4182 | uint32_t age_add; | |
4183 | } age_add_u; | |
67ec6d2b | 4184 | CON_FUNC_RETURN ret = CON_FUNC_ERROR; |
6a11d5c5 MC |
4185 | |
4186 | age_add_u.age_add = 0; | |
4187 | ||
38b051a1 | 4188 | if (SSL_CONNECTION_IS_TLS13(s)) { |
6a11d5c5 MC |
4189 | size_t i, hashlen; |
4190 | uint64_t nonce; | |
4191 | static const unsigned char nonce_label[] = "resumption"; | |
4192 | const EVP_MD *md = ssl_handshake_md(s); | |
ed576acd | 4193 | int hashleni = EVP_MD_get_size(md); |
6a11d5c5 MC |
4194 | |
4195 | /* Ensure cast to size_t is safe */ | |
4196 | if (!ossl_assert(hashleni >= 0)) { | |
c48ffbcc | 4197 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6a11d5c5 MC |
4198 | goto err; |
4199 | } | |
4200 | hashlen = (size_t)hashleni; | |
4201 | ||
6a11d5c5 MC |
4202 | /* |
4203 | * If we already sent one NewSessionTicket, or we resumed then | |
4204 | * s->session may already be in a cache and so we must not modify it. | |
4205 | * Instead we need to take a copy of it and modify that. | |
4206 | */ | |
4207 | if (s->sent_tickets != 0 || s->hit) { | |
4208 | SSL_SESSION *new_sess = ssl_session_dup(s->session, 0); | |
4209 | ||
4210 | if (new_sess == NULL) { | |
4211 | /* SSLfatal already called */ | |
4212 | goto err; | |
4213 | } | |
4214 | ||
4215 | SSL_SESSION_free(s->session); | |
4216 | s->session = new_sess; | |
4217 | } | |
4218 | ||
4219 | if (!ssl_generate_session_id(s, s->session)) { | |
4220 | /* SSLfatal() already called */ | |
4221 | goto err; | |
4222 | } | |
38b051a1 TM |
4223 | if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx, |
4224 | age_add_u.age_add_c, sizeof(age_add_u), 0) <= 0) { | |
c48ffbcc | 4225 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6a11d5c5 MC |
4226 | goto err; |
4227 | } | |
4228 | s->session->ext.tick_age_add = age_add_u.age_add; | |
4229 | ||
4230 | nonce = s->next_ticket_nonce; | |
4231 | for (i = TICKET_NONCE_SIZE; i > 0; i--) { | |
4232 | tick_nonce[i - 1] = (unsigned char)(nonce & 0xff); | |
4233 | nonce >>= 8; | |
4234 | } | |
4235 | ||
4236 | if (!tls13_hkdf_expand(s, md, s->resumption_master_secret, | |
4237 | nonce_label, | |
4238 | sizeof(nonce_label) - 1, | |
4239 | tick_nonce, | |
4240 | TICKET_NONCE_SIZE, | |
4241 | s->session->master_key, | |
0fb2815b | 4242 | hashlen, 1)) { |
6a11d5c5 MC |
4243 | /* SSLfatal() already called */ |
4244 | goto err; | |
4245 | } | |
4246 | s->session->master_key_length = hashlen; | |
4247 | ||
f0131dc0 | 4248 | s->session->time = ossl_time_now(); |
25959e04 | 4249 | ssl_session_calculate_timeout(s->session); |
555cbb32 | 4250 | if (s->s3.alpn_selected != NULL) { |
6a11d5c5 MC |
4251 | OPENSSL_free(s->session->ext.alpn_selected); |
4252 | s->session->ext.alpn_selected = | |
555cbb32 | 4253 | OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len); |
6a11d5c5 | 4254 | if (s->session->ext.alpn_selected == NULL) { |
39a14059 | 4255 | s->session->ext.alpn_selected_len = 0; |
e077455e | 4256 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
6a11d5c5 MC |
4257 | goto err; |
4258 | } | |
555cbb32 | 4259 | s->session->ext.alpn_selected_len = s->s3.alpn_selected_len; |
6a11d5c5 MC |
4260 | } |
4261 | s->session->ext.max_early_data = s->max_early_data; | |
4262 | } | |
4263 | ||
4264 | if (tctx->generate_ticket_cb != NULL && | |
38b051a1 TM |
4265 | tctx->generate_ticket_cb(SSL_CONNECTION_GET_SSL(s), |
4266 | tctx->ticket_cb_data) == 0) { | |
e49095f1 | 4267 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6a11d5c5 | 4268 | goto err; |
e49095f1 | 4269 | } |
e880d4e5 MC |
4270 | /* |
4271 | * If we are using anti-replay protection then we behave as if | |
4272 | * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there | |
4273 | * is no point in using full stateless tickets. | |
4274 | */ | |
38b051a1 | 4275 | if (SSL_CONNECTION_IS_TLS13(s) |
5d263fb7 MC |
4276 | && ((s->options & SSL_OP_NO_TICKET) != 0 |
4277 | || (s->max_early_data > 0 | |
4278 | && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) { | |
6cc0b3c2 MC |
4279 | if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) { |
4280 | /* SSLfatal() already called */ | |
4281 | goto err; | |
4282 | } | |
3e93c5fe | 4283 | } else { |
67ec6d2b | 4284 | CON_FUNC_RETURN tmpret; |
3e93c5fe MC |
4285 | |
4286 | tmpret = construct_stateless_ticket(s, pkt, age_add_u.age_add, | |
4287 | tick_nonce); | |
67ec6d2b MC |
4288 | if (tmpret != CON_FUNC_SUCCESS) { |
4289 | if (tmpret == CON_FUNC_DONT_SEND) { | |
4290 | /* Non-fatal. Abort construction but continue */ | |
4291 | ret = CON_FUNC_DONT_SEND; | |
3e93c5fe MC |
4292 | /* We count this as a success so update the counts anwyay */ |
4293 | tls_update_ticket_counts(s); | |
4294 | } | |
4295 | /* else SSLfatal() already called */ | |
4296 | goto err; | |
4297 | } | |
6a11d5c5 MC |
4298 | } |
4299 | ||
38b051a1 | 4300 | if (SSL_CONNECTION_IS_TLS13(s)) { |
16ff1342 MC |
4301 | if (!tls_construct_extensions(s, pkt, |
4302 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET, | |
4303 | NULL, 0)) { | |
4304 | /* SSLfatal() already called */ | |
4305 | goto err; | |
4306 | } | |
3e93c5fe | 4307 | tls_update_ticket_counts(s); |
36ff232c | 4308 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); |
f63a17d6 | 4309 | } |
e27f234a | 4310 | |
67ec6d2b | 4311 | ret = CON_FUNC_SUCCESS; |
687eaf27 | 4312 | err: |
3e93c5fe | 4313 | return ret; |
0f113f3e | 4314 | } |
67c8e7f4 | 4315 | |
f63e4288 MC |
4316 | /* |
4317 | * In TLSv1.3 this is called from the extensions code, otherwise it is used to | |
4318 | * create a separate message. Returns 1 on success or 0 on failure. | |
4319 | */ | |
38b051a1 | 4320 | int tls_construct_cert_status_body(SSL_CONNECTION *s, WPACKET *pkt) |
e27f234a | 4321 | { |
8cbfcc70 RS |
4322 | if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type) |
4323 | || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp, | |
4324 | s->ext.ocsp.resp_len)) { | |
c48ffbcc | 4325 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63e4288 MC |
4326 | return 0; |
4327 | } | |
4328 | ||
4329 | return 1; | |
4330 | } | |
4331 | ||
67ec6d2b | 4332 | CON_FUNC_RETURN tls_construct_cert_status(SSL_CONNECTION *s, WPACKET *pkt) |
f63e4288 MC |
4333 | { |
4334 | if (!tls_construct_cert_status_body(s, pkt)) { | |
3ec8d113 | 4335 | /* SSLfatal() already called */ |
67ec6d2b | 4336 | return CON_FUNC_ERROR; |
cc59ad10 | 4337 | } |
e27f234a | 4338 | |
67ec6d2b | 4339 | return CON_FUNC_SUCCESS; |
e27f234a MC |
4340 | } |
4341 | ||
e481f9b9 | 4342 | #ifndef OPENSSL_NO_NEXTPROTONEG |
e27f234a MC |
4343 | /* |
4344 | * tls_process_next_proto reads a Next Protocol Negotiation handshake message. | |
4345 | * It sets the next_proto member in s if found | |
4346 | */ | |
38b051a1 | 4347 | MSG_PROCESS_RETURN tls_process_next_proto(SSL_CONNECTION *s, PACKET *pkt) |
e27f234a | 4348 | { |
73999b62 | 4349 | PACKET next_proto, padding; |
e27f234a MC |
4350 | size_t next_proto_len; |
4351 | ||
50e735f9 MC |
4352 | /*- |
4353 | * The payload looks like: | |
4354 | * uint8 proto_len; | |
4355 | * uint8 proto[proto_len]; | |
4356 | * uint8 padding_len; | |
4357 | * uint8 padding[padding_len]; | |
4358 | */ | |
73999b62 MC |
4359 | if (!PACKET_get_length_prefixed_1(pkt, &next_proto) |
4360 | || !PACKET_get_length_prefixed_1(pkt, &padding) | |
4361 | || PACKET_remaining(pkt) > 0) { | |
c48ffbcc | 4362 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 4363 | return MSG_PROCESS_ERROR; |
cf9b0b6f | 4364 | } |
0f113f3e | 4365 | |
aff8c126 RS |
4366 | if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) { |
4367 | s->ext.npn_len = 0; | |
c48ffbcc | 4368 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 4369 | return MSG_PROCESS_ERROR; |
c3fc7eea MC |
4370 | } |
4371 | ||
aff8c126 | 4372 | s->ext.npn_len = (unsigned char)next_proto_len; |
0f113f3e | 4373 | |
e27f234a | 4374 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e | 4375 | } |
6434abbf | 4376 | #endif |
d45ba43d | 4377 | |
67ec6d2b MC |
4378 | static CON_FUNC_RETURN tls_construct_encrypted_extensions(SSL_CONNECTION *s, |
4379 | WPACKET *pkt) | |
e46f2334 | 4380 | { |
fe874d27 | 4381 | if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, |
f63a17d6 MC |
4382 | NULL, 0)) { |
4383 | /* SSLfatal() already called */ | |
67ec6d2b | 4384 | return CON_FUNC_ERROR; |
e46f2334 MC |
4385 | } |
4386 | ||
67ec6d2b | 4387 | return CON_FUNC_SUCCESS; |
e46f2334 MC |
4388 | } |
4389 | ||
38b051a1 | 4390 | MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL_CONNECTION *s, PACKET *pkt) |
ef6c191b | 4391 | { |
ef6c191b | 4392 | if (PACKET_remaining(pkt) != 0) { |
c48ffbcc | 4393 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
ef6c191b MC |
4394 | return MSG_PROCESS_ERROR; |
4395 | } | |
4396 | ||
4397 | if (s->early_data_state != SSL_EARLY_DATA_READING | |
4398 | && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) { | |
c48ffbcc | 4399 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 4400 | return MSG_PROCESS_ERROR; |
ef6c191b MC |
4401 | } |
4402 | ||
4403 | /* | |
4404 | * EndOfEarlyData signals a key change so the end of the message must be on | |
4405 | * a record boundary. | |
4406 | */ | |
4407 | if (RECORD_LAYER_processed_read_pending(&s->rlayer)) { | |
c48ffbcc | 4408 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY); |
f63a17d6 | 4409 | return MSG_PROCESS_ERROR; |
ef6c191b MC |
4410 | } |
4411 | ||
4412 | s->early_data_state = SSL_EARLY_DATA_FINISHED_READING; | |
38b051a1 | 4413 | if (!SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->change_cipher_state(s, |
ef6c191b | 4414 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) { |
f63a17d6 MC |
4415 | /* SSLfatal() already called */ |
4416 | return MSG_PROCESS_ERROR; | |
ef6c191b MC |
4417 | } |
4418 | ||
4419 | return MSG_PROCESS_CONTINUE_READING; | |
ef6c191b | 4420 | } |