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