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