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