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