]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
3c95ef22 | 2 | * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. |
aa8f3d76 | 3 | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
c80149d9 | 4 | * Copyright 2005 Nokia. All rights reserved. |
8c74b5e5 | 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 | |
8c74b5e5 | 10 | */ |
846e33c7 | 11 | |
d02b48c6 | 12 | #include <stdio.h> |
fc24f0bf | 13 | #include <time.h> |
dee0cc10 | 14 | #include <assert.h> |
706457b7 DMSP |
15 | #include "../ssl_local.h" |
16 | #include "statem_local.h" | |
ec577822 BM |
17 | #include <openssl/buffer.h> |
18 | #include <openssl/rand.h> | |
19 | #include <openssl/objects.h> | |
20 | #include <openssl/evp.h> | |
dbad1690 | 21 | #include <openssl/md5.h> |
3c27208f | 22 | #include <openssl/dh.h> |
d7e498ac | 23 | #include <openssl/rsa.h> |
d095b68d | 24 | #include <openssl/bn.h> |
3c27208f | 25 | #include <openssl/engine.h> |
49b26f54 | 26 | #include <openssl/trace.h> |
1ee22dc2 MC |
27 | #include <openssl/core_names.h> |
28 | #include <openssl/param_build.h> | |
449bdf37 | 29 | #include "internal/cryptlib.h" |
f9b3bff6 | 30 | |
38b051a1 TM |
31 | static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL_CONNECTION *s, |
32 | PACKET *pkt); | |
33 | static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL_CONNECTION *s, | |
34 | PACKET *pkt); | |
35 | ||
36 | static ossl_inline int cert_req_allowed(SSL_CONNECTION *s); | |
37 | static int key_exchange_expected(SSL_CONNECTION *s); | |
38 | static int ssl_cipher_list_to_bytes(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *sk, | |
ae2f7b37 | 39 | WPACKET *pkt); |
ea262260 | 40 | |
3c95ef22 TS |
41 | static ossl_inline int received_server_cert(SSL_CONNECTION *sc) |
42 | { | |
43 | return sc->session->peer_rpk != NULL || sc->session->peer != NULL; | |
44 | } | |
45 | ||
61ae935a MC |
46 | /* |
47 | * Is a CertificateRequest message allowed at the moment or not? | |
48 | * | |
49 | * Return values are: | |
50 | * 1: Yes | |
51 | * 0: No | |
52 | */ | |
38b051a1 | 53 | static ossl_inline int cert_req_allowed(SSL_CONNECTION *s) |
61ae935a MC |
54 | { |
55 | /* TLS does not like anon-DH with client cert */ | |
b7fa1f98 | 56 | if ((s->version > SSL3_VERSION |
555cbb32 TS |
57 | && (s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL)) |
58 | || (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK))) | |
61ae935a MC |
59 | return 0; |
60 | ||
61 | return 1; | |
62 | } | |
63 | ||
64 | /* | |
a455d0f6 | 65 | * Should we expect the ServerKeyExchange message or not? |
61ae935a MC |
66 | * |
67 | * Return values are: | |
68 | * 1: Yes | |
69 | * 0: No | |
70 | */ | |
38b051a1 | 71 | static int key_exchange_expected(SSL_CONNECTION *s) |
61ae935a | 72 | { |
555cbb32 | 73 | long alg_k = s->s3.tmp.new_cipher->algorithm_mkey; |
61ae935a MC |
74 | |
75 | /* | |
76 | * Can't skip server key exchange if this is an ephemeral | |
a455d0f6 | 77 | * ciphersuite or for SRP |
61ae935a | 78 | */ |
a455d0f6 MC |
79 | if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK |
80 | | SSL_kSRP)) { | |
81 | return 1; | |
61ae935a MC |
82 | } |
83 | ||
a455d0f6 | 84 | return 0; |
61ae935a MC |
85 | } |
86 | ||
0f1e51ea MC |
87 | /* |
88 | * ossl_statem_client_read_transition() encapsulates the logic for the allowed | |
89 | * handshake state transitions when a TLS1.3 client is reading messages from the | |
90 | * server. The message type that the server has sent is provided in |mt|. The | |
91 | * current state is in |s->statem.hand_state|. | |
92 | * | |
94ed2c67 MC |
93 | * Return values are 1 for success (transition allowed) and 0 on error |
94 | * (transition not allowed) | |
0f1e51ea | 95 | */ |
38b051a1 | 96 | static int ossl_statem_client13_read_transition(SSL_CONNECTION *s, int mt) |
0f1e51ea MC |
97 | { |
98 | OSSL_STATEM *st = &s->statem; | |
99 | ||
100 | /* | |
101 | * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't | |
102 | * yet negotiated TLSv1.3 at that point so that is handled by | |
103 | * ossl_statem_client_read_transition() | |
104 | */ | |
105 | ||
106 | switch (st->hand_state) { | |
107 | default: | |
108 | break; | |
109 | ||
3847d426 MC |
110 | case TLS_ST_CW_CLNT_HELLO: |
111 | /* | |
112 | * This must a ClientHello following a HelloRetryRequest, so the only | |
113 | * thing we can get now is a ServerHello. | |
114 | */ | |
115 | if (mt == SSL3_MT_SERVER_HELLO) { | |
116 | st->hand_state = TLS_ST_CR_SRVR_HELLO; | |
117 | return 1; | |
118 | } | |
119 | break; | |
120 | ||
0f1e51ea | 121 | case TLS_ST_CR_SRVR_HELLO: |
e46f2334 MC |
122 | if (mt == SSL3_MT_ENCRYPTED_EXTENSIONS) { |
123 | st->hand_state = TLS_ST_CR_ENCRYPTED_EXTENSIONS; | |
124 | return 1; | |
125 | } | |
126 | break; | |
127 | ||
128 | case TLS_ST_CR_ENCRYPTED_EXTENSIONS: | |
0f1e51ea | 129 | if (s->hit) { |
92760c21 MC |
130 | if (mt == SSL3_MT_FINISHED) { |
131 | st->hand_state = TLS_ST_CR_FINISHED; | |
0f1e51ea MC |
132 | return 1; |
133 | } | |
134 | } else { | |
92760c21 MC |
135 | if (mt == SSL3_MT_CERTIFICATE_REQUEST) { |
136 | st->hand_state = TLS_ST_CR_CERT_REQ; | |
137 | return 1; | |
f5ca0b04 MC |
138 | } |
139 | if (mt == SSL3_MT_CERTIFICATE) { | |
0f1e51ea MC |
140 | st->hand_state = TLS_ST_CR_CERT; |
141 | return 1; | |
142 | } | |
b67cb09f TS |
143 | #ifndef OPENSSL_NO_COMP_ALG |
144 | if (mt == SSL3_MT_COMPRESSED_CERTIFICATE | |
145 | && s->ext.compress_certificate_sent) { | |
146 | st->hand_state = TLS_ST_CR_COMP_CERT; | |
147 | return 1; | |
148 | } | |
149 | #endif | |
0f1e51ea MC |
150 | } |
151 | break; | |
152 | ||
92760c21 MC |
153 | case TLS_ST_CR_CERT_REQ: |
154 | if (mt == SSL3_MT_CERTIFICATE) { | |
155 | st->hand_state = TLS_ST_CR_CERT; | |
156 | return 1; | |
157 | } | |
b67cb09f TS |
158 | #ifndef OPENSSL_NO_COMP_ALG |
159 | if (mt == SSL3_MT_COMPRESSED_CERTIFICATE | |
160 | && s->ext.compress_certificate_sent) { | |
161 | st->hand_state = TLS_ST_CR_COMP_CERT; | |
162 | return 1; | |
163 | } | |
164 | #endif | |
92760c21 MC |
165 | break; |
166 | ||
0f1e51ea | 167 | case TLS_ST_CR_CERT: |
b67cb09f | 168 | case TLS_ST_CR_COMP_CERT: |
2c5dfdc3 MC |
169 | if (mt == SSL3_MT_CERTIFICATE_VERIFY) { |
170 | st->hand_state = TLS_ST_CR_CERT_VRFY; | |
171 | return 1; | |
172 | } | |
173 | break; | |
174 | ||
175 | case TLS_ST_CR_CERT_VRFY: | |
0f1e51ea MC |
176 | if (mt == SSL3_MT_FINISHED) { |
177 | st->hand_state = TLS_ST_CR_FINISHED; | |
178 | return 1; | |
179 | } | |
180 | break; | |
cc2455bf MC |
181 | |
182 | case TLS_ST_OK: | |
183 | if (mt == SSL3_MT_NEWSESSION_TICKET) { | |
184 | st->hand_state = TLS_ST_CR_SESSION_TICKET; | |
185 | return 1; | |
186 | } | |
50a0af2e | 187 | if (mt == SSL3_MT_KEY_UPDATE && !SSL_IS_QUIC_HANDSHAKE(s)) { |
e1c3de44 MC |
188 | st->hand_state = TLS_ST_CR_KEY_UPDATE; |
189 | return 1; | |
190 | } | |
9d75dce3 | 191 | if (mt == SSL3_MT_CERTIFICATE_REQUEST) { |
5c587fb6 | 192 | #if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION |
407820c0 P |
193 | /* Restore digest for PHA before adding message.*/ |
194 | # error Internal DTLS version error | |
9d75dce3 | 195 | #endif |
38b051a1 TM |
196 | if (!SSL_CONNECTION_IS_DTLS(s) |
197 | && s->post_handshake_auth == SSL_PHA_EXT_SENT) { | |
9d75dce3 TS |
198 | s->post_handshake_auth = SSL_PHA_REQUESTED; |
199 | /* | |
200 | * In TLS, this is called before the message is added to the | |
201 | * digest. In DTLS, this is expected to be called after adding | |
202 | * to the digest. Either move the digest restore, or add the | |
203 | * message here after the swap, or do it after the clientFinished? | |
204 | */ | |
205 | if (!tls13_restore_handshake_digest_for_pha(s)) { | |
206 | /* SSLfatal() already called */ | |
207 | return 0; | |
208 | } | |
209 | st->hand_state = TLS_ST_CR_CERT_REQ; | |
210 | return 1; | |
211 | } | |
212 | } | |
cc2455bf | 213 | break; |
0f1e51ea MC |
214 | } |
215 | ||
0f1e51ea | 216 | /* No valid transition found */ |
0f1e51ea MC |
217 | return 0; |
218 | } | |
219 | ||
61ae935a | 220 | /* |
8481f583 MC |
221 | * ossl_statem_client_read_transition() encapsulates the logic for the allowed |
222 | * handshake state transitions when the client is reading messages from the | |
223 | * server. The message type that the server has sent is provided in |mt|. The | |
224 | * current state is in |s->statem.hand_state|. | |
61ae935a | 225 | * |
94ed2c67 MC |
226 | * Return values are 1 for success (transition allowed) and 0 on error |
227 | * (transition not allowed) | |
61ae935a | 228 | */ |
38b051a1 | 229 | int ossl_statem_client_read_transition(SSL_CONNECTION *s, int mt) |
61ae935a | 230 | { |
d6f1a6e9 | 231 | OSSL_STATEM *st = &s->statem; |
a455d0f6 | 232 | int ske_expected; |
61ae935a | 233 | |
0f1e51ea | 234 | /* |
3847d426 MC |
235 | * Note that after writing the first ClientHello we don't know what version |
236 | * we are going to negotiate yet, so we don't take this branch until later. | |
0f1e51ea | 237 | */ |
38b051a1 | 238 | if (SSL_CONNECTION_IS_TLS13(s)) { |
5abeaf35 MC |
239 | if (!ossl_statem_client13_read_transition(s, mt)) |
240 | goto err; | |
241 | return 1; | |
242 | } | |
0f1e51ea | 243 | |
a230b26e | 244 | switch (st->hand_state) { |
f3b3d7f0 RS |
245 | default: |
246 | break; | |
247 | ||
61ae935a MC |
248 | case TLS_ST_CW_CLNT_HELLO: |
249 | if (mt == SSL3_MT_SERVER_HELLO) { | |
250 | st->hand_state = TLS_ST_CR_SRVR_HELLO; | |
251 | return 1; | |
252 | } | |
253 | ||
38b051a1 | 254 | if (SSL_CONNECTION_IS_DTLS(s)) { |
61ae935a MC |
255 | if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) { |
256 | st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST; | |
257 | return 1; | |
258 | } | |
259 | } | |
260 | break; | |
261 | ||
d7f8783f | 262 | case TLS_ST_EARLY_DATA: |
4004ce5f MC |
263 | /* |
264 | * We've not actually selected TLSv1.3 yet, but we have sent early | |
265 | * data. The only thing allowed now is a ServerHello or a | |
266 | * HelloRetryRequest. | |
267 | */ | |
268 | if (mt == SSL3_MT_SERVER_HELLO) { | |
269 | st->hand_state = TLS_ST_CR_SRVR_HELLO; | |
270 | return 1; | |
271 | } | |
4004ce5f MC |
272 | break; |
273 | ||
61ae935a MC |
274 | case TLS_ST_CR_SRVR_HELLO: |
275 | if (s->hit) { | |
aff8c126 | 276 | if (s->ext.ticket_expected) { |
61ae935a MC |
277 | if (mt == SSL3_MT_NEWSESSION_TICKET) { |
278 | st->hand_state = TLS_ST_CR_SESSION_TICKET; | |
279 | return 1; | |
280 | } | |
281 | } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | |
282 | st->hand_state = TLS_ST_CR_CHANGE; | |
283 | return 1; | |
284 | } | |
285 | } else { | |
38b051a1 TM |
286 | if (SSL_CONNECTION_IS_DTLS(s) |
287 | && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) { | |
61ae935a MC |
288 | st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST; |
289 | return 1; | |
ad3819c2 | 290 | } else if (s->version >= TLS1_VERSION |
aff8c126 RS |
291 | && s->ext.session_secret_cb != NULL |
292 | && s->session->ext.tick != NULL | |
a230b26e | 293 | && mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
ad3819c2 MC |
294 | /* |
295 | * Normally, we can tell if the server is resuming the session | |
296 | * from the session ID. EAP-FAST (RFC 4851), however, relies on | |
297 | * the next server message after the ServerHello to determine if | |
298 | * the server is resuming. | |
299 | */ | |
300 | s->hit = 1; | |
301 | st->hand_state = TLS_ST_CR_CHANGE; | |
302 | return 1; | |
555cbb32 | 303 | } else if (!(s->s3.tmp.new_cipher->algorithm_auth |
a230b26e | 304 | & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) { |
61ae935a MC |
305 | if (mt == SSL3_MT_CERTIFICATE) { |
306 | st->hand_state = TLS_ST_CR_CERT; | |
307 | return 1; | |
308 | } | |
309 | } else { | |
a455d0f6 | 310 | ske_expected = key_exchange_expected(s); |
a455d0f6 MC |
311 | /* SKE is optional for some PSK ciphersuites */ |
312 | if (ske_expected | |
555cbb32 | 313 | || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK) |
a230b26e | 314 | && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) { |
a455d0f6 MC |
315 | if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) { |
316 | st->hand_state = TLS_ST_CR_KEY_EXCH; | |
317 | return 1; | |
318 | } | |
319 | } else if (mt == SSL3_MT_CERTIFICATE_REQUEST | |
a230b26e EK |
320 | && cert_req_allowed(s)) { |
321 | st->hand_state = TLS_ST_CR_CERT_REQ; | |
322 | return 1; | |
a455d0f6 | 323 | } else if (mt == SSL3_MT_SERVER_DONE) { |
a230b26e EK |
324 | st->hand_state = TLS_ST_CR_SRVR_DONE; |
325 | return 1; | |
61ae935a MC |
326 | } |
327 | } | |
328 | } | |
329 | break; | |
330 | ||
331 | case TLS_ST_CR_CERT: | |
b67cb09f | 332 | case TLS_ST_CR_COMP_CERT: |
bb1aaab4 MC |
333 | /* |
334 | * The CertificateStatus message is optional even if | |
aff8c126 | 335 | * |ext.status_expected| is set |
bb1aaab4 | 336 | */ |
aff8c126 | 337 | if (s->ext.status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) { |
bb1aaab4 MC |
338 | st->hand_state = TLS_ST_CR_CERT_STATUS; |
339 | return 1; | |
a455d0f6 MC |
340 | } |
341 | /* Fall through */ | |
342 | ||
343 | case TLS_ST_CR_CERT_STATUS: | |
344 | ske_expected = key_exchange_expected(s); | |
a455d0f6 | 345 | /* SKE is optional for some PSK ciphersuites */ |
555cbb32 | 346 | if (ske_expected || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK) |
a230b26e | 347 | && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) { |
61ae935a MC |
348 | if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) { |
349 | st->hand_state = TLS_ST_CR_KEY_EXCH; | |
350 | return 1; | |
61ae935a | 351 | } |
672f3337 | 352 | goto err; |
61ae935a | 353 | } |
a455d0f6 | 354 | /* Fall through */ |
61ae935a | 355 | |
a455d0f6 MC |
356 | case TLS_ST_CR_KEY_EXCH: |
357 | if (mt == SSL3_MT_CERTIFICATE_REQUEST) { | |
358 | if (cert_req_allowed(s)) { | |
61ae935a MC |
359 | st->hand_state = TLS_ST_CR_CERT_REQ; |
360 | return 1; | |
61ae935a | 361 | } |
672f3337 | 362 | goto err; |
61ae935a | 363 | } |
a455d0f6 | 364 | /* Fall through */ |
61ae935a MC |
365 | |
366 | case TLS_ST_CR_CERT_REQ: | |
367 | if (mt == SSL3_MT_SERVER_DONE) { | |
368 | st->hand_state = TLS_ST_CR_SRVR_DONE; | |
369 | return 1; | |
370 | } | |
371 | break; | |
372 | ||
373 | case TLS_ST_CW_FINISHED: | |
aff8c126 | 374 | if (s->ext.ticket_expected) { |
c45d6b2b DB |
375 | if (mt == SSL3_MT_NEWSESSION_TICKET) { |
376 | st->hand_state = TLS_ST_CR_SESSION_TICKET; | |
377 | return 1; | |
378 | } | |
61ae935a MC |
379 | } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
380 | st->hand_state = TLS_ST_CR_CHANGE; | |
381 | return 1; | |
382 | } | |
383 | break; | |
384 | ||
385 | case TLS_ST_CR_SESSION_TICKET: | |
386 | if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | |
387 | st->hand_state = TLS_ST_CR_CHANGE; | |
388 | return 1; | |
389 | } | |
390 | break; | |
391 | ||
392 | case TLS_ST_CR_CHANGE: | |
393 | if (mt == SSL3_MT_FINISHED) { | |
394 | st->hand_state = TLS_ST_CR_FINISHED; | |
395 | return 1; | |
396 | } | |
397 | break; | |
c7f47786 MC |
398 | |
399 | case TLS_ST_OK: | |
4004ce5f | 400 | if (mt == SSL3_MT_HELLO_REQUEST) { |
c7f47786 MC |
401 | st->hand_state = TLS_ST_CR_HELLO_REQ; |
402 | return 1; | |
403 | } | |
404 | break; | |
61ae935a MC |
405 | } |
406 | ||
672f3337 | 407 | err: |
61ae935a | 408 | /* No valid transition found */ |
38b051a1 | 409 | if (SSL_CONNECTION_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
f20404fc MC |
410 | BIO *rbio; |
411 | ||
412 | /* | |
413 | * CCS messages don't have a message sequence number so this is probably | |
414 | * because of an out-of-order CCS. We'll just drop it. | |
415 | */ | |
416 | s->init_num = 0; | |
417 | s->rwstate = SSL_READING; | |
38b051a1 | 418 | rbio = SSL_get_rbio(SSL_CONNECTION_GET_SSL(s)); |
f20404fc MC |
419 | BIO_clear_retry_flags(rbio); |
420 | BIO_set_retry_read(rbio); | |
421 | return 0; | |
422 | } | |
c48ffbcc | 423 | SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
61ae935a MC |
424 | return 0; |
425 | } | |
426 | ||
3c95ef22 TS |
427 | static int do_compressed_cert(SSL_CONNECTION *sc) |
428 | { | |
429 | /* If we negotiated RPK, we won't try to compress it */ | |
430 | return sc->ext.client_cert_type == TLSEXT_cert_type_x509 | |
431 | && sc->ext.compress_certificate_from_peer[0] != TLSEXT_comp_cert_none; | |
432 | } | |
433 | ||
61ae935a | 434 | /* |
0f1e51ea MC |
435 | * ossl_statem_client13_write_transition() works out what handshake state to |
436 | * move to next when the TLSv1.3 client is writing messages to be sent to the | |
437 | * server. | |
0f1e51ea | 438 | */ |
38b051a1 | 439 | static WRITE_TRAN ossl_statem_client13_write_transition(SSL_CONNECTION *s) |
0f1e51ea MC |
440 | { |
441 | OSSL_STATEM *st = &s->statem; | |
442 | ||
443 | /* | |
3847d426 MC |
444 | * Note: There are no cases for TLS_ST_BEFORE because we haven't negotiated |
445 | * TLSv1.3 yet at that point. They are handled by | |
446 | * ossl_statem_client_write_transition(). | |
0f1e51ea MC |
447 | */ |
448 | switch (st->hand_state) { | |
e43e6b19 TS |
449 | default: |
450 | /* Shouldn't happen */ | |
c48ffbcc | 451 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
e43e6b19 TS |
452 | return WRITE_TRAN_ERROR; |
453 | ||
9d75dce3 TS |
454 | case TLS_ST_CR_CERT_REQ: |
455 | if (s->post_handshake_auth == SSL_PHA_REQUESTED) { | |
3c95ef22 | 456 | if (do_compressed_cert(s)) |
b67cb09f TS |
457 | st->hand_state = TLS_ST_CW_COMP_CERT; |
458 | else | |
459 | st->hand_state = TLS_ST_CW_CERT; | |
9d75dce3 TS |
460 | return WRITE_TRAN_CONTINUE; |
461 | } | |
1bf4cb0f MC |
462 | /* |
463 | * We should only get here if we received a CertificateRequest after | |
464 | * we already sent close_notify | |
465 | */ | |
466 | if (!ossl_assert((s->shutdown & SSL_SENT_SHUTDOWN) != 0)) { | |
467 | /* Shouldn't happen - same as default case */ | |
c48ffbcc | 468 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1bf4cb0f MC |
469 | return WRITE_TRAN_ERROR; |
470 | } | |
471 | st->hand_state = TLS_ST_OK; | |
472 | return WRITE_TRAN_CONTINUE; | |
0f1e51ea | 473 | |
92760c21 | 474 | case TLS_ST_CR_FINISHED: |
ef6c191b MC |
475 | if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY |
476 | || s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING) | |
d7f8783f | 477 | st->hand_state = TLS_ST_PENDING_EARLY_DATA_END; |
4d02f870 | 478 | else if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0 |
fc7129dc | 479 | && s->hello_retry_request == SSL_HRR_NONE) |
4d02f870 | 480 | st->hand_state = TLS_ST_CW_CHANGE; |
b67cb09f TS |
481 | else if (s->s3.tmp.cert_req == 0) |
482 | st->hand_state = TLS_ST_CW_FINISHED; | |
3c95ef22 | 483 | else if (do_compressed_cert(s)) |
b67cb09f | 484 | st->hand_state = TLS_ST_CW_COMP_CERT; |
564547e4 | 485 | else |
b67cb09f | 486 | st->hand_state = TLS_ST_CW_CERT; |
cee0628e JC |
487 | |
488 | s->ts_msg_read = ossl_time_now(); | |
564547e4 MC |
489 | return WRITE_TRAN_CONTINUE; |
490 | ||
d7f8783f | 491 | case TLS_ST_PENDING_EARLY_DATA_END: |
ef6c191b MC |
492 | if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) { |
493 | st->hand_state = TLS_ST_CW_END_OF_EARLY_DATA; | |
494 | return WRITE_TRAN_CONTINUE; | |
495 | } | |
496 | /* Fall through */ | |
497 | ||
498 | case TLS_ST_CW_END_OF_EARLY_DATA: | |
4d02f870 | 499 | case TLS_ST_CW_CHANGE: |
b67cb09f TS |
500 | if (s->s3.tmp.cert_req == 0) |
501 | st->hand_state = TLS_ST_CW_FINISHED; | |
3c95ef22 | 502 | else if (do_compressed_cert(s)) |
b67cb09f TS |
503 | st->hand_state = TLS_ST_CW_COMP_CERT; |
504 | else | |
505 | st->hand_state = TLS_ST_CW_CERT; | |
0f1e51ea MC |
506 | return WRITE_TRAN_CONTINUE; |
507 | ||
b67cb09f | 508 | case TLS_ST_CW_COMP_CERT: |
0f1e51ea MC |
509 | case TLS_ST_CW_CERT: |
510 | /* If a non-empty Certificate we also send CertificateVerify */ | |
555cbb32 | 511 | st->hand_state = (s->s3.tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY |
92760c21 | 512 | : TLS_ST_CW_FINISHED; |
0f1e51ea MC |
513 | return WRITE_TRAN_CONTINUE; |
514 | ||
515 | case TLS_ST_CW_CERT_VRFY: | |
0f1e51ea MC |
516 | st->hand_state = TLS_ST_CW_FINISHED; |
517 | return WRITE_TRAN_CONTINUE; | |
518 | ||
e1c3de44 | 519 | case TLS_ST_CR_KEY_UPDATE: |
9412b3ad | 520 | case TLS_ST_CW_KEY_UPDATE: |
cc2455bf | 521 | case TLS_ST_CR_SESSION_TICKET: |
0f1e51ea | 522 | case TLS_ST_CW_FINISHED: |
94ed2c67 | 523 | st->hand_state = TLS_ST_OK; |
94ed2c67 | 524 | return WRITE_TRAN_CONTINUE; |
cc2455bf MC |
525 | |
526 | case TLS_ST_OK: | |
9412b3ad MC |
527 | if (s->key_update != SSL_KEY_UPDATE_NONE) { |
528 | st->hand_state = TLS_ST_CW_KEY_UPDATE; | |
529 | return WRITE_TRAN_CONTINUE; | |
530 | } | |
531 | ||
532 | /* Try to read from the server instead */ | |
cc2455bf | 533 | return WRITE_TRAN_FINISHED; |
0f1e51ea MC |
534 | } |
535 | } | |
536 | ||
537 | /* | |
538 | * ossl_statem_client_write_transition() works out what handshake state to | |
539 | * move to next when the client is writing messages to be sent to the server. | |
61ae935a | 540 | */ |
38b051a1 | 541 | WRITE_TRAN ossl_statem_client_write_transition(SSL_CONNECTION *s) |
61ae935a | 542 | { |
d6f1a6e9 | 543 | OSSL_STATEM *st = &s->statem; |
61ae935a | 544 | |
0f1e51ea MC |
545 | /* |
546 | * Note that immediately before/after a ClientHello we don't know what | |
547 | * version we are going to negotiate yet, so we don't take this branch until | |
548 | * later | |
549 | */ | |
38b051a1 | 550 | if (SSL_CONNECTION_IS_TLS13(s)) |
0f1e51ea MC |
551 | return ossl_statem_client13_write_transition(s); |
552 | ||
a230b26e | 553 | switch (st->hand_state) { |
f3b3d7f0 RS |
554 | default: |
555 | /* Shouldn't happen */ | |
c48ffbcc | 556 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f3b3d7f0 RS |
557 | return WRITE_TRAN_ERROR; |
558 | ||
a230b26e | 559 | case TLS_ST_OK: |
c7f47786 MC |
560 | if (!s->renegotiate) { |
561 | /* | |
562 | * We haven't requested a renegotiation ourselves so we must have | |
563 | * received a message from the server. Better read it. | |
564 | */ | |
565 | return WRITE_TRAN_FINISHED; | |
566 | } | |
018fcbec BE |
567 | /* Renegotiation */ |
568 | /* fall thru */ | |
a230b26e EK |
569 | case TLS_ST_BEFORE: |
570 | st->hand_state = TLS_ST_CW_CLNT_HELLO; | |
571 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 572 | |
a230b26e | 573 | case TLS_ST_CW_CLNT_HELLO: |
49e7fe12 MC |
574 | if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) { |
575 | /* | |
576 | * We are assuming this is a TLSv1.3 connection, although we haven't | |
577 | * actually selected a version yet. | |
578 | */ | |
066904cc MC |
579 | if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0) |
580 | st->hand_state = TLS_ST_CW_CHANGE; | |
581 | else | |
582 | st->hand_state = TLS_ST_EARLY_DATA; | |
49e7fe12 MC |
583 | return WRITE_TRAN_CONTINUE; |
584 | } | |
a230b26e EK |
585 | /* |
586 | * No transition at the end of writing because we don't know what | |
587 | * we will be sent | |
588 | */ | |
cee0628e | 589 | s->ts_msg_write = ossl_time_now(); |
a230b26e | 590 | return WRITE_TRAN_FINISHED; |
61ae935a | 591 | |
597c51bc | 592 | case TLS_ST_CR_SRVR_HELLO: |
318d3c0e MC |
593 | /* |
594 | * We only get here in TLSv1.3. We just received an HRR, so issue a | |
595 | * CCS unless middlebox compat mode is off, or we already issued one | |
596 | * because we did early data. | |
597 | */ | |
598 | if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0 | |
599 | && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) | |
600 | st->hand_state = TLS_ST_CW_CHANGE; | |
601 | else | |
602 | st->hand_state = TLS_ST_CW_CLNT_HELLO; | |
a2b97bdf MC |
603 | return WRITE_TRAN_CONTINUE; |
604 | ||
d7f8783f | 605 | case TLS_ST_EARLY_DATA: |
cee0628e | 606 | s->ts_msg_write = ossl_time_now(); |
4004ce5f MC |
607 | return WRITE_TRAN_FINISHED; |
608 | ||
a230b26e EK |
609 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
610 | st->hand_state = TLS_ST_CW_CLNT_HELLO; | |
611 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 612 | |
a230b26e | 613 | case TLS_ST_CR_SRVR_DONE: |
cee0628e | 614 | s->ts_msg_read = ossl_time_now(); |
555cbb32 | 615 | if (s->s3.tmp.cert_req) |
a230b26e EK |
616 | st->hand_state = TLS_ST_CW_CERT; |
617 | else | |
61ae935a | 618 | st->hand_state = TLS_ST_CW_KEY_EXCH; |
a230b26e | 619 | return WRITE_TRAN_CONTINUE; |
61ae935a | 620 | |
a230b26e EK |
621 | case TLS_ST_CW_CERT: |
622 | st->hand_state = TLS_ST_CW_KEY_EXCH; | |
623 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 624 | |
a230b26e EK |
625 | case TLS_ST_CW_KEY_EXCH: |
626 | /* | |
627 | * For TLS, cert_req is set to 2, so a cert chain of nothing is | |
628 | * sent, but no verify packet is sent | |
629 | */ | |
630 | /* | |
631 | * XXX: For now, we do not support client authentication in ECDH | |
632 | * cipher suites with ECDH (rather than ECDSA) certificates. We | |
633 | * need to skip the certificate verify message when client's | |
634 | * ECDH public key is sent inside the client certificate. | |
635 | */ | |
555cbb32 | 636 | if (s->s3.tmp.cert_req == 1) { |
a230b26e EK |
637 | st->hand_state = TLS_ST_CW_CERT_VRFY; |
638 | } else { | |
61ae935a | 639 | st->hand_state = TLS_ST_CW_CHANGE; |
a230b26e | 640 | } |
555cbb32 | 641 | if (s->s3.flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { |
a230b26e EK |
642 | st->hand_state = TLS_ST_CW_CHANGE; |
643 | } | |
644 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 645 | |
a230b26e EK |
646 | case TLS_ST_CW_CERT_VRFY: |
647 | st->hand_state = TLS_ST_CW_CHANGE; | |
648 | return WRITE_TRAN_CONTINUE; | |
649 | ||
650 | case TLS_ST_CW_CHANGE: | |
318d3c0e MC |
651 | if (s->hello_retry_request == SSL_HRR_PENDING) { |
652 | st->hand_state = TLS_ST_CW_CLNT_HELLO; | |
653 | } else if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) { | |
066904cc MC |
654 | st->hand_state = TLS_ST_EARLY_DATA; |
655 | } else { | |
61ae935a | 656 | #if defined(OPENSSL_NO_NEXTPROTONEG) |
a230b26e | 657 | st->hand_state = TLS_ST_CW_FINISHED; |
066904cc | 658 | #else |
38b051a1 | 659 | if (!SSL_CONNECTION_IS_DTLS(s) && s->s3.npn_seen) |
066904cc MC |
660 | st->hand_state = TLS_ST_CW_NEXT_PROTO; |
661 | else | |
662 | st->hand_state = TLS_ST_CW_FINISHED; | |
61ae935a | 663 | #endif |
066904cc | 664 | } |
a230b26e | 665 | return WRITE_TRAN_CONTINUE; |
61ae935a MC |
666 | |
667 | #if !defined(OPENSSL_NO_NEXTPROTONEG) | |
a230b26e EK |
668 | case TLS_ST_CW_NEXT_PROTO: |
669 | st->hand_state = TLS_ST_CW_FINISHED; | |
670 | return WRITE_TRAN_CONTINUE; | |
61ae935a MC |
671 | #endif |
672 | ||
a230b26e EK |
673 | case TLS_ST_CW_FINISHED: |
674 | if (s->hit) { | |
675 | st->hand_state = TLS_ST_OK; | |
a230b26e EK |
676 | return WRITE_TRAN_CONTINUE; |
677 | } else { | |
678 | return WRITE_TRAN_FINISHED; | |
679 | } | |
61ae935a | 680 | |
a230b26e EK |
681 | case TLS_ST_CR_FINISHED: |
682 | if (s->hit) { | |
683 | st->hand_state = TLS_ST_CW_CHANGE; | |
684 | return WRITE_TRAN_CONTINUE; | |
685 | } else { | |
686 | st->hand_state = TLS_ST_OK; | |
a230b26e EK |
687 | return WRITE_TRAN_CONTINUE; |
688 | } | |
c7f47786 MC |
689 | |
690 | case TLS_ST_CR_HELLO_REQ: | |
691 | /* | |
692 | * If we can renegotiate now then do so, otherwise wait for a more | |
693 | * convenient time. | |
694 | */ | |
38b051a1 | 695 | if (ssl3_renegotiate_check(SSL_CONNECTION_GET_SSL(s), 1)) { |
c7f47786 | 696 | if (!tls_setup_handshake(s)) { |
f63a17d6 | 697 | /* SSLfatal() already called */ |
c7f47786 MC |
698 | return WRITE_TRAN_ERROR; |
699 | } | |
700 | st->hand_state = TLS_ST_CW_CLNT_HELLO; | |
701 | return WRITE_TRAN_CONTINUE; | |
702 | } | |
703 | st->hand_state = TLS_ST_OK; | |
c7f47786 | 704 | return WRITE_TRAN_CONTINUE; |
61ae935a MC |
705 | } |
706 | } | |
707 | ||
708 | /* | |
709 | * Perform any pre work that needs to be done prior to sending a message from | |
710 | * the client to the server. | |
711 | */ | |
38b051a1 | 712 | WORK_STATE ossl_statem_client_pre_work(SSL_CONNECTION *s, WORK_STATE wst) |
61ae935a | 713 | { |
d6f1a6e9 | 714 | OSSL_STATEM *st = &s->statem; |
61ae935a | 715 | |
a230b26e | 716 | switch (st->hand_state) { |
f3b3d7f0 RS |
717 | default: |
718 | /* No pre work to be done */ | |
719 | break; | |
720 | ||
61ae935a MC |
721 | case TLS_ST_CW_CLNT_HELLO: |
722 | s->shutdown = 0; | |
38b051a1 | 723 | if (SSL_CONNECTION_IS_DTLS(s)) { |
61ae935a | 724 | /* every DTLS ClientHello resets Finished MAC */ |
2c4a056f | 725 | if (!ssl3_init_finished_mac(s)) { |
f63a17d6 | 726 | /* SSLfatal() already called */ |
2c4a056f MC |
727 | return WORK_ERROR; |
728 | } | |
2c50d7fb MC |
729 | } else if (s->ext.early_data == SSL_EARLY_DATA_REJECTED) { |
730 | /* | |
731 | * This must be a second ClientHello after an HRR following an | |
732 | * earlier rejected attempt to send early data. Since we were | |
733 | * previously encrypting the early data we now need to reset the | |
734 | * write record layer in order to write in plaintext again. | |
735 | */ | |
736 | if (!ssl_set_new_record_layer(s, | |
737 | TLS_ANY_VERSION, | |
738 | OSSL_RECORD_DIRECTION_WRITE, | |
739 | OSSL_RECORD_PROTECTION_LEVEL_NONE, | |
3f9175c7 MC |
740 | NULL, 0, NULL, 0, NULL, 0, NULL, 0, |
741 | NULL, 0, NID_undef, NULL, NULL, | |
742 | NULL)) { | |
2c50d7fb MC |
743 | /* SSLfatal already called */ |
744 | return WORK_ERROR; | |
745 | } | |
61ae935a MC |
746 | } |
747 | break; | |
748 | ||
61ae935a | 749 | case TLS_ST_CW_CHANGE: |
38b051a1 | 750 | if (SSL_CONNECTION_IS_DTLS(s)) { |
61ae935a MC |
751 | if (s->hit) { |
752 | /* | |
753 | * We're into the last flight so we don't retransmit these | |
754 | * messages unless we need to. | |
755 | */ | |
756 | st->use_timer = 0; | |
757 | } | |
758 | #ifndef OPENSSL_NO_SCTP | |
38b051a1 | 759 | if (BIO_dgram_is_sctp(SSL_get_wbio(SSL_CONNECTION_GET_SSL(s)))) { |
a2c2e000 | 760 | /* Calls SSLfatal() as required */ |
61ae935a | 761 | return dtls_wait_for_dry(s); |
a2c2e000 | 762 | } |
61ae935a MC |
763 | #endif |
764 | } | |
f3b3d7f0 | 765 | break; |
61ae935a | 766 | |
d7f8783f | 767 | case TLS_ST_PENDING_EARLY_DATA_END: |
ef6c191b MC |
768 | /* |
769 | * If we've been called by SSL_do_handshake()/SSL_write(), or we did not | |
770 | * attempt to write early data before calling SSL_read() then we press | |
771 | * on with the handshake. Otherwise we pause here. | |
772 | */ | |
773 | if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING | |
774 | || s->early_data_state == SSL_EARLY_DATA_NONE) | |
775 | return WORK_FINISHED_CONTINUE; | |
776 | /* Fall through */ | |
777 | ||
778 | case TLS_ST_EARLY_DATA: | |
2a8db717 MC |
779 | return tls_finish_handshake(s, wst, 0, 1); |
780 | ||
61ae935a | 781 | case TLS_ST_OK: |
a2c2e000 | 782 | /* Calls SSLfatal() as required */ |
2a8db717 | 783 | return tls_finish_handshake(s, wst, 1, 1); |
61ae935a MC |
784 | } |
785 | ||
786 | return WORK_FINISHED_CONTINUE; | |
787 | } | |
788 | ||
789 | /* | |
790 | * Perform any work that needs to be done after sending a message from the | |
791 | * client to the server. | |
792 | */ | |
38b051a1 | 793 | WORK_STATE ossl_statem_client_post_work(SSL_CONNECTION *s, WORK_STATE wst) |
61ae935a | 794 | { |
d6f1a6e9 | 795 | OSSL_STATEM *st = &s->statem; |
38b051a1 | 796 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
61ae935a MC |
797 | |
798 | s->init_num = 0; | |
799 | ||
a230b26e | 800 | switch (st->hand_state) { |
f3b3d7f0 RS |
801 | default: |
802 | /* No post work to be done */ | |
803 | break; | |
804 | ||
61ae935a | 805 | case TLS_ST_CW_CLNT_HELLO: |
6cb42265 | 806 | if (s->early_data_state == SSL_EARLY_DATA_CONNECTING |
5cc807da | 807 | && s->max_early_data > 0) { |
6cb42265 MC |
808 | /* |
809 | * We haven't selected TLSv1.3 yet so we don't call the change | |
810 | * cipher state function associated with the SSL_METHOD. Instead | |
811 | * we call tls13_change_cipher_state() directly. | |
812 | */ | |
5cc807da | 813 | if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0) { |
5cc807da MC |
814 | if (!tls13_change_cipher_state(s, |
815 | SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { | |
816 | /* SSLfatal() already called */ | |
817 | return WORK_ERROR; | |
818 | } | |
a2c2e000 | 819 | } |
5cc807da MC |
820 | /* else we're in compat mode so we delay flushing until after CCS */ |
821 | } else if (!statem_flush(s)) { | |
822 | return WORK_MORE_A; | |
823 | } | |
824 | ||
38b051a1 | 825 | if (SSL_CONNECTION_IS_DTLS(s)) { |
5cc807da MC |
826 | /* Treat the next message as the first packet */ |
827 | s->first_packet = 1; | |
6cb42265 | 828 | } |
61ae935a MC |
829 | break; |
830 | ||
831 | case TLS_ST_CW_KEY_EXCH: | |
a2c2e000 MC |
832 | if (tls_client_key_exchange_post_work(s) == 0) { |
833 | /* SSLfatal() already called */ | |
61ae935a | 834 | return WORK_ERROR; |
a2c2e000 | 835 | } |
61ae935a MC |
836 | break; |
837 | ||
838 | case TLS_ST_CW_CHANGE: | |
38b051a1 TM |
839 | if (SSL_CONNECTION_IS_TLS13(s) |
840 | || s->hello_retry_request == SSL_HRR_PENDING) | |
4d02f870 | 841 | break; |
066904cc MC |
842 | if (s->early_data_state == SSL_EARLY_DATA_CONNECTING |
843 | && s->max_early_data > 0) { | |
844 | /* | |
845 | * We haven't selected TLSv1.3 yet so we don't call the change | |
846 | * cipher state function associated with the SSL_METHOD. Instead | |
847 | * we call tls13_change_cipher_state() directly. | |
848 | */ | |
849 | if (!tls13_change_cipher_state(s, | |
850 | SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) | |
851 | return WORK_ERROR; | |
852 | break; | |
853 | } | |
555cbb32 | 854 | s->session->cipher = s->s3.tmp.new_cipher; |
61ae935a MC |
855 | #ifdef OPENSSL_NO_COMP |
856 | s->session->compress_meth = 0; | |
857 | #else | |
555cbb32 | 858 | if (s->s3.tmp.new_compression == NULL) |
61ae935a MC |
859 | s->session->compress_meth = 0; |
860 | else | |
555cbb32 | 861 | s->session->compress_meth = s->s3.tmp.new_compression->id; |
61ae935a | 862 | #endif |
38b051a1 | 863 | if (!ssl->method->ssl3_enc->setup_key_block(s)) { |
a2c2e000 | 864 | /* SSLfatal() already called */ |
61ae935a | 865 | return WORK_ERROR; |
a2c2e000 | 866 | } |
61ae935a | 867 | |
38b051a1 | 868 | if (!ssl->method->ssl3_enc->change_cipher_state(s, |
a2c2e000 MC |
869 | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
870 | /* SSLfatal() already called */ | |
61ae935a | 871 | return WORK_ERROR; |
a2c2e000 | 872 | } |
61ae935a | 873 | |
38b051a1 | 874 | if (SSL_CONNECTION_IS_DTLS(s)) { |
61ae935a MC |
875 | #ifndef OPENSSL_NO_SCTP |
876 | if (s->hit) { | |
877 | /* | |
878 | * Change to new shared key of SCTP-Auth, will be ignored if | |
879 | * no SCTP used. | |
880 | */ | |
38b051a1 | 881 | BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, |
61ae935a MC |
882 | 0, NULL); |
883 | } | |
884 | #endif | |
885 | ||
b92fc4ae | 886 | dtls1_increment_epoch(s, SSL3_CC_WRITE); |
61ae935a MC |
887 | } |
888 | break; | |
889 | ||
890 | case TLS_ST_CW_FINISHED: | |
891 | #ifndef OPENSSL_NO_SCTP | |
38b051a1 | 892 | if (wst == WORK_MORE_A && SSL_CONNECTION_IS_DTLS(s) && s->hit == 0) { |
61ae935a MC |
893 | /* |
894 | * Change to new shared key of SCTP-Auth, will be ignored if | |
895 | * no SCTP used. | |
896 | */ | |
38b051a1 | 897 | BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, |
61ae935a MC |
898 | 0, NULL); |
899 | } | |
900 | #endif | |
901 | if (statem_flush(s) != 1) | |
902 | return WORK_MORE_B; | |
92760c21 | 903 | |
38b051a1 | 904 | if (SSL_CONNECTION_IS_TLS13(s)) { |
9d75dce3 | 905 | if (!tls13_save_handshake_digest_for_pha(s)) { |
a2c2e000 MC |
906 | /* SSLfatal() already called */ |
907 | return WORK_ERROR; | |
908 | } | |
9d75dce3 | 909 | if (s->post_handshake_auth != SSL_PHA_REQUESTED) { |
38b051a1 | 910 | if (!ssl->method->ssl3_enc->change_cipher_state(s, |
9d75dce3 TS |
911 | SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
912 | /* SSLfatal() already called */ | |
913 | return WORK_ERROR; | |
914 | } | |
915 | } | |
92760c21 | 916 | } |
61ae935a | 917 | break; |
9412b3ad MC |
918 | |
919 | case TLS_ST_CW_KEY_UPDATE: | |
920 | if (statem_flush(s) != 1) | |
921 | return WORK_MORE_A; | |
a2c2e000 MC |
922 | if (!tls13_update_key(s, 1)) { |
923 | /* SSLfatal() already called */ | |
57389a32 | 924 | return WORK_ERROR; |
a2c2e000 | 925 | } |
9412b3ad | 926 | break; |
61ae935a MC |
927 | } |
928 | ||
929 | return WORK_FINISHED_CONTINUE; | |
930 | } | |
931 | ||
932 | /* | |
6392fb8e MC |
933 | * Get the message construction function and message type for sending from the |
934 | * client | |
61ae935a MC |
935 | * |
936 | * Valid return values are: | |
937 | * 1: Success | |
938 | * 0: Error | |
939 | */ | |
38b051a1 | 940 | int ossl_statem_client_construct_message(SSL_CONNECTION *s, |
a15c953f | 941 | confunc_f *confunc, int *mt) |
61ae935a | 942 | { |
d6f1a6e9 | 943 | OSSL_STATEM *st = &s->statem; |
61ae935a | 944 | |
4a01c59f MC |
945 | switch (st->hand_state) { |
946 | default: | |
947 | /* Shouldn't happen */ | |
c48ffbcc | 948 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE); |
4a01c59f MC |
949 | return 0; |
950 | ||
951 | case TLS_ST_CW_CHANGE: | |
38b051a1 | 952 | if (SSL_CONNECTION_IS_DTLS(s)) |
6392fb8e | 953 | *confunc = dtls_construct_change_cipher_spec; |
4a01c59f | 954 | else |
6392fb8e MC |
955 | *confunc = tls_construct_change_cipher_spec; |
956 | *mt = SSL3_MT_CHANGE_CIPHER_SPEC; | |
4a01c59f MC |
957 | break; |
958 | ||
959 | case TLS_ST_CW_CLNT_HELLO: | |
6392fb8e MC |
960 | *confunc = tls_construct_client_hello; |
961 | *mt = SSL3_MT_CLIENT_HELLO; | |
4a01c59f MC |
962 | break; |
963 | ||
ef6c191b MC |
964 | case TLS_ST_CW_END_OF_EARLY_DATA: |
965 | *confunc = tls_construct_end_of_early_data; | |
966 | *mt = SSL3_MT_END_OF_EARLY_DATA; | |
967 | break; | |
968 | ||
969 | case TLS_ST_PENDING_EARLY_DATA_END: | |
970 | *confunc = NULL; | |
971 | *mt = SSL3_MT_DUMMY; | |
972 | break; | |
973 | ||
4a01c59f | 974 | case TLS_ST_CW_CERT: |
6392fb8e MC |
975 | *confunc = tls_construct_client_certificate; |
976 | *mt = SSL3_MT_CERTIFICATE; | |
4a01c59f MC |
977 | break; |
978 | ||
b67cb09f TS |
979 | #ifndef OPENSSL_NO_COMP_ALG |
980 | case TLS_ST_CW_COMP_CERT: | |
981 | *confunc = tls_construct_client_compressed_certificate; | |
982 | *mt = SSL3_MT_COMPRESSED_CERTIFICATE; | |
983 | break; | |
984 | #endif | |
985 | ||
4a01c59f | 986 | case TLS_ST_CW_KEY_EXCH: |
6392fb8e MC |
987 | *confunc = tls_construct_client_key_exchange; |
988 | *mt = SSL3_MT_CLIENT_KEY_EXCHANGE; | |
4a01c59f MC |
989 | break; |
990 | ||
991 | case TLS_ST_CW_CERT_VRFY: | |
d8bc1399 | 992 | *confunc = tls_construct_cert_verify; |
6392fb8e | 993 | *mt = SSL3_MT_CERTIFICATE_VERIFY; |
4a01c59f | 994 | break; |
61ae935a MC |
995 | |
996 | #if !defined(OPENSSL_NO_NEXTPROTONEG) | |
4a01c59f | 997 | case TLS_ST_CW_NEXT_PROTO: |
6392fb8e MC |
998 | *confunc = tls_construct_next_proto; |
999 | *mt = SSL3_MT_NEXT_PROTO; | |
4a01c59f | 1000 | break; |
61ae935a | 1001 | #endif |
4a01c59f | 1002 | case TLS_ST_CW_FINISHED: |
6392fb8e MC |
1003 | *confunc = tls_construct_finished; |
1004 | *mt = SSL3_MT_FINISHED; | |
4a01c59f | 1005 | break; |
9412b3ad MC |
1006 | |
1007 | case TLS_ST_CW_KEY_UPDATE: | |
1008 | *confunc = tls_construct_key_update; | |
1009 | *mt = SSL3_MT_KEY_UPDATE; | |
1010 | break; | |
4a01c59f | 1011 | } |
5923ad4b | 1012 | |
5923ad4b | 1013 | return 1; |
61ae935a MC |
1014 | } |
1015 | ||
1016 | /* | |
1017 | * Returns the maximum allowed length for the current message that we are | |
1018 | * reading. Excludes the message header. | |
1019 | */ | |
38b051a1 | 1020 | size_t ossl_statem_client_max_message_size(SSL_CONNECTION *s) |
61ae935a | 1021 | { |
d6f1a6e9 | 1022 | OSSL_STATEM *st = &s->statem; |
61ae935a | 1023 | |
a230b26e | 1024 | switch (st->hand_state) { |
f3b3d7f0 RS |
1025 | default: |
1026 | /* Shouldn't happen */ | |
1027 | return 0; | |
1028 | ||
a230b26e EK |
1029 | case TLS_ST_CR_SRVR_HELLO: |
1030 | return SERVER_HELLO_MAX_LENGTH; | |
61ae935a | 1031 | |
a230b26e EK |
1032 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
1033 | return HELLO_VERIFY_REQUEST_MAX_LENGTH; | |
61ae935a | 1034 | |
b67cb09f | 1035 | case TLS_ST_CR_COMP_CERT: |
a230b26e EK |
1036 | case TLS_ST_CR_CERT: |
1037 | return s->max_cert_list; | |
61ae935a | 1038 | |
2c5dfdc3 | 1039 | case TLS_ST_CR_CERT_VRFY: |
c6d14bfd | 1040 | return CERTIFICATE_VERIFY_MAX_LENGTH; |
2c5dfdc3 | 1041 | |
a230b26e EK |
1042 | case TLS_ST_CR_CERT_STATUS: |
1043 | return SSL3_RT_MAX_PLAIN_LENGTH; | |
61ae935a | 1044 | |
a230b26e EK |
1045 | case TLS_ST_CR_KEY_EXCH: |
1046 | return SERVER_KEY_EXCH_MAX_LENGTH; | |
61ae935a | 1047 | |
a230b26e EK |
1048 | case TLS_ST_CR_CERT_REQ: |
1049 | /* | |
1050 | * Set to s->max_cert_list for compatibility with previous releases. In | |
1051 | * practice these messages can get quite long if servers are configured | |
1052 | * to provide a long list of acceptable CAs | |
1053 | */ | |
1054 | return s->max_cert_list; | |
61ae935a | 1055 | |
a230b26e EK |
1056 | case TLS_ST_CR_SRVR_DONE: |
1057 | return SERVER_HELLO_DONE_MAX_LENGTH; | |
61ae935a | 1058 | |
a230b26e EK |
1059 | case TLS_ST_CR_CHANGE: |
1060 | if (s->version == DTLS1_BAD_VER) | |
1061 | return 3; | |
1062 | return CCS_MAX_LENGTH; | |
61ae935a | 1063 | |
a230b26e | 1064 | case TLS_ST_CR_SESSION_TICKET: |
38b051a1 TM |
1065 | return (SSL_CONNECTION_IS_TLS13(s)) ? SESSION_TICKET_MAX_LENGTH_TLS13 |
1066 | : SESSION_TICKET_MAX_LENGTH_TLS12; | |
61ae935a | 1067 | |
a230b26e EK |
1068 | case TLS_ST_CR_FINISHED: |
1069 | return FINISHED_MAX_LENGTH; | |
e46f2334 MC |
1070 | |
1071 | case TLS_ST_CR_ENCRYPTED_EXTENSIONS: | |
1072 | return ENCRYPTED_EXTENSIONS_MAX_LENGTH; | |
e1c3de44 MC |
1073 | |
1074 | case TLS_ST_CR_KEY_UPDATE: | |
1075 | return KEY_UPDATE_MAX_LENGTH; | |
61ae935a | 1076 | } |
61ae935a MC |
1077 | } |
1078 | ||
1079 | /* | |
0c3eb279 | 1080 | * Process a message that the client has received from the server. |
61ae935a | 1081 | */ |
38b051a1 TM |
1082 | MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL_CONNECTION *s, |
1083 | PACKET *pkt) | |
61ae935a | 1084 | { |
d6f1a6e9 | 1085 | OSSL_STATEM *st = &s->statem; |
61ae935a | 1086 | |
a230b26e | 1087 | switch (st->hand_state) { |
f3b3d7f0 RS |
1088 | default: |
1089 | /* Shouldn't happen */ | |
c48ffbcc | 1090 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f3b3d7f0 RS |
1091 | return MSG_PROCESS_ERROR; |
1092 | ||
a230b26e EK |
1093 | case TLS_ST_CR_SRVR_HELLO: |
1094 | return tls_process_server_hello(s, pkt); | |
61ae935a | 1095 | |
a230b26e EK |
1096 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
1097 | return dtls_process_hello_verify(s, pkt); | |
61ae935a | 1098 | |
a230b26e EK |
1099 | case TLS_ST_CR_CERT: |
1100 | return tls_process_server_certificate(s, pkt); | |
61ae935a | 1101 | |
b67cb09f TS |
1102 | #ifndef OPENSSL_NO_COMP_ALG |
1103 | case TLS_ST_CR_COMP_CERT: | |
1104 | return tls_process_server_compressed_certificate(s, pkt); | |
1105 | #endif | |
1106 | ||
2c5dfdc3 MC |
1107 | case TLS_ST_CR_CERT_VRFY: |
1108 | return tls_process_cert_verify(s, pkt); | |
1109 | ||
a230b26e EK |
1110 | case TLS_ST_CR_CERT_STATUS: |
1111 | return tls_process_cert_status(s, pkt); | |
61ae935a | 1112 | |
a230b26e EK |
1113 | case TLS_ST_CR_KEY_EXCH: |
1114 | return tls_process_key_exchange(s, pkt); | |
61ae935a | 1115 | |
a230b26e EK |
1116 | case TLS_ST_CR_CERT_REQ: |
1117 | return tls_process_certificate_request(s, pkt); | |
61ae935a | 1118 | |
a230b26e EK |
1119 | case TLS_ST_CR_SRVR_DONE: |
1120 | return tls_process_server_done(s, pkt); | |
61ae935a | 1121 | |
a230b26e EK |
1122 | case TLS_ST_CR_CHANGE: |
1123 | return tls_process_change_cipher_spec(s, pkt); | |
61ae935a | 1124 | |
a230b26e EK |
1125 | case TLS_ST_CR_SESSION_TICKET: |
1126 | return tls_process_new_session_ticket(s, pkt); | |
61ae935a | 1127 | |
a230b26e EK |
1128 | case TLS_ST_CR_FINISHED: |
1129 | return tls_process_finished(s, pkt); | |
e46f2334 | 1130 | |
c7f47786 MC |
1131 | case TLS_ST_CR_HELLO_REQ: |
1132 | return tls_process_hello_req(s, pkt); | |
1133 | ||
e46f2334 MC |
1134 | case TLS_ST_CR_ENCRYPTED_EXTENSIONS: |
1135 | return tls_process_encrypted_extensions(s, pkt); | |
e1c3de44 MC |
1136 | |
1137 | case TLS_ST_CR_KEY_UPDATE: | |
1138 | return tls_process_key_update(s, pkt); | |
61ae935a | 1139 | } |
61ae935a MC |
1140 | } |
1141 | ||
1142 | /* | |
1143 | * Perform any further processing required following the receipt of a message | |
1144 | * from the server | |
1145 | */ | |
38b051a1 TM |
1146 | WORK_STATE ossl_statem_client_post_process_message(SSL_CONNECTION *s, |
1147 | WORK_STATE wst) | |
61ae935a | 1148 | { |
d6f1a6e9 | 1149 | OSSL_STATEM *st = &s->statem; |
61ae935a | 1150 | |
a230b26e | 1151 | switch (st->hand_state) { |
f3b3d7f0 RS |
1152 | default: |
1153 | /* Shouldn't happen */ | |
c48ffbcc | 1154 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f3b3d7f0 RS |
1155 | return WORK_ERROR; |
1156 | ||
0c3eb279 | 1157 | case TLS_ST_CR_CERT: |
b67cb09f | 1158 | case TLS_ST_CR_COMP_CERT: |
0c3eb279 DDO |
1159 | return tls_post_process_server_certificate(s, wst); |
1160 | ||
e4562014 | 1161 | case TLS_ST_CR_CERT_VRFY: |
05c4f1d5 MC |
1162 | case TLS_ST_CR_CERT_REQ: |
1163 | return tls_prepare_client_certificate(s, wst); | |
61ae935a | 1164 | } |
61ae935a MC |
1165 | } |
1166 | ||
67ec6d2b | 1167 | CON_FUNC_RETURN tls_construct_client_hello(SSL_CONNECTION *s, WPACKET *pkt) |
0f113f3e | 1168 | { |
2c7b4dbc | 1169 | unsigned char *p; |
ec60ccc1 MC |
1170 | size_t sess_id_len; |
1171 | int i, protverr; | |
09b6c2ef | 1172 | #ifndef OPENSSL_NO_COMP |
0f113f3e MC |
1173 | SSL_COMP *comp; |
1174 | #endif | |
b9908bf9 | 1175 | SSL_SESSION *sess = s->session; |
a5816a5a | 1176 | unsigned char *session_id; |
38b051a1 | 1177 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
0f113f3e | 1178 | |
b9908bf9 | 1179 | /* Work out what SSL/TLS/DTLS version to use */ |
4fa52141 VD |
1180 | protverr = ssl_set_client_hello_version(s); |
1181 | if (protverr != 0) { | |
c48ffbcc | 1182 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, protverr); |
67ec6d2b | 1183 | return CON_FUNC_ERROR; |
4fa52141 | 1184 | } |
0f113f3e | 1185 | |
e586eac8 | 1186 | if (sess == NULL |
4fd12788 | 1187 | || !ssl_version_supported(s, sess->ssl_version, NULL) |
e586eac8 | 1188 | || !SSL_SESSION_is_resumable(sess)) { |
fc7129dc MC |
1189 | if (s->hello_retry_request == SSL_HRR_NONE |
1190 | && !ssl_get_new_session(s, 0)) { | |
f63a17d6 | 1191 | /* SSLfatal() already called */ |
67ec6d2b | 1192 | return CON_FUNC_ERROR; |
f63a17d6 | 1193 | } |
b9908bf9 MC |
1194 | } |
1195 | /* else use the pre-loaded session */ | |
0f113f3e | 1196 | |
555cbb32 | 1197 | p = s->s3.client_random; |
0f113f3e | 1198 | |
b9908bf9 MC |
1199 | /* |
1200 | * for DTLS if client_random is initialized, reuse it, we are | |
1201 | * required to use same upon reply to HelloVerify | |
1202 | */ | |
38b051a1 | 1203 | if (SSL_CONNECTION_IS_DTLS(s)) { |
b9908bf9 MC |
1204 | size_t idx; |
1205 | i = 1; | |
555cbb32 | 1206 | for (idx = 0; idx < sizeof(s->s3.client_random); idx++) { |
b9908bf9 MC |
1207 | if (p[idx]) { |
1208 | i = 0; | |
1209 | break; | |
0f113f3e | 1210 | } |
0f113f3e | 1211 | } |
751b26b1 | 1212 | } else { |
fc7129dc | 1213 | i = (s->hello_retry_request == SSL_HRR_NONE); |
751b26b1 | 1214 | } |
0f113f3e | 1215 | |
555cbb32 | 1216 | if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3.client_random), |
f63a17d6 | 1217 | DOWNGRADE_NONE) <= 0) { |
c48ffbcc | 1218 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 1219 | return CON_FUNC_ERROR; |
f63a17d6 | 1220 | } |
b9908bf9 | 1221 | |
b9908bf9 MC |
1222 | /*- |
1223 | * version indicates the negotiated version: for example from | |
1224 | * an SSLv2/v3 compatible client hello). The client_version | |
1225 | * field is the maximum version we permit and it is also | |
1226 | * used in RSA encrypted premaster secrets. Some servers can | |
1227 | * choke if we initially report a higher version then | |
1228 | * renegotiate to a lower one in the premaster secret. This | |
1229 | * didn't happen with TLS 1.0 as most servers supported it | |
1230 | * but it can with TLS 1.1 or later if the server only supports | |
1231 | * 1.0. | |
1232 | * | |
1233 | * Possible scenario with previous logic: | |
1234 | * 1. Client hello indicates TLS 1.2 | |
1235 | * 2. Server hello says TLS 1.0 | |
1236 | * 3. RSA encrypted premaster secret uses 1.2. | |
8483a003 | 1237 | * 4. Handshake proceeds using TLS 1.0. |
b9908bf9 MC |
1238 | * 5. Server sends hello request to renegotiate. |
1239 | * 6. Client hello indicates TLS v1.0 as we now | |
1240 | * know that is maximum server supports. | |
1241 | * 7. Server chokes on RSA encrypted premaster secret | |
1242 | * containing version 1.0. | |
1243 | * | |
1244 | * For interoperability it should be OK to always use the | |
1245 | * maximum version we support in client hello and then rely | |
1246 | * on the checking of version to ensure the servers isn't | |
1247 | * being inconsistent: for example initially negotiating with | |
1248 | * TLS 1.0 and renegotiating with TLS 1.2. We do this by using | |
1249 | * client_version in client hello and not resetting it to | |
1250 | * the negotiated version. | |
cd998837 MC |
1251 | * |
1252 | * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the | |
16bce0e0 | 1253 | * supported_versions extension for the real supported versions. |
b9908bf9 | 1254 | */ |
7acb8b64 | 1255 | if (!WPACKET_put_bytes_u16(pkt, s->client_version) |
555cbb32 | 1256 | || !WPACKET_memcpy(pkt, s->s3.client_random, SSL3_RANDOM_SIZE)) { |
c48ffbcc | 1257 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 1258 | return CON_FUNC_ERROR; |
2c7b4dbc | 1259 | } |
b9908bf9 MC |
1260 | |
1261 | /* Session ID */ | |
a5816a5a MC |
1262 | session_id = s->session->session_id; |
1263 | if (s->new_session || s->session->ssl_version == TLS1_3_VERSION) { | |
1264 | if (s->version == TLS1_3_VERSION | |
1265 | && (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0) { | |
1266 | sess_id_len = sizeof(s->tmp_session_id); | |
1267 | s->tmp_session_id_len = sess_id_len; | |
1268 | session_id = s->tmp_session_id; | |
fc7129dc | 1269 | if (s->hello_retry_request == SSL_HRR_NONE |
38b051a1 | 1270 | && RAND_bytes_ex(sctx->libctx, s->tmp_session_id, |
0f8815aa | 1271 | sess_id_len, 0) <= 0) { |
c48ffbcc | 1272 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 1273 | return CON_FUNC_ERROR; |
a5816a5a MC |
1274 | } |
1275 | } else { | |
1276 | sess_id_len = 0; | |
1277 | } | |
1278 | } else { | |
dee0cc10 | 1279 | assert(s->session->session_id_length <= sizeof(s->session->session_id)); |
ec60ccc1 | 1280 | sess_id_len = s->session->session_id_length; |
a5816a5a MC |
1281 | if (s->version == TLS1_3_VERSION) { |
1282 | s->tmp_session_id_len = sess_id_len; | |
1283 | memcpy(s->tmp_session_id, s->session->session_id, sess_id_len); | |
1284 | } | |
1285 | } | |
dee0cc10 | 1286 | if (!WPACKET_start_sub_packet_u8(pkt) |
a5816a5a | 1287 | || (sess_id_len != 0 && !WPACKET_memcpy(pkt, session_id, |
ec60ccc1 | 1288 | sess_id_len)) |
7cea05dc | 1289 | || !WPACKET_close(pkt)) { |
c48ffbcc | 1290 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 1291 | return CON_FUNC_ERROR; |
b9908bf9 | 1292 | } |
0f113f3e | 1293 | |
b9908bf9 | 1294 | /* cookie stuff for DTLS */ |
38b051a1 | 1295 | if (SSL_CONNECTION_IS_DTLS(s)) { |
2c7b4dbc | 1296 | if (s->d1->cookie_len > sizeof(s->d1->cookie) |
7cea05dc | 1297 | || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie, |
b2b3024e | 1298 | s->d1->cookie_len)) { |
c48ffbcc | 1299 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 1300 | return CON_FUNC_ERROR; |
0f113f3e | 1301 | } |
b9908bf9 MC |
1302 | } |
1303 | ||
1304 | /* Ciphers supported */ | |
7cea05dc | 1305 | if (!WPACKET_start_sub_packet_u16(pkt)) { |
c48ffbcc | 1306 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 1307 | return CON_FUNC_ERROR; |
2c7b4dbc | 1308 | } |
635c8f77 | 1309 | |
38b051a1 TM |
1310 | if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(SSL_CONNECTION_GET_SSL(s)), |
1311 | pkt)) { | |
f63a17d6 | 1312 | /* SSLfatal() already called */ |
67ec6d2b | 1313 | return CON_FUNC_ERROR; |
f63a17d6 | 1314 | } |
7cea05dc | 1315 | if (!WPACKET_close(pkt)) { |
c48ffbcc | 1316 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 1317 | return CON_FUNC_ERROR; |
b9908bf9 | 1318 | } |
0f113f3e | 1319 | |
b9908bf9 | 1320 | /* COMPRESSION */ |
7cea05dc | 1321 | if (!WPACKET_start_sub_packet_u8(pkt)) { |
c48ffbcc | 1322 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 1323 | return CON_FUNC_ERROR; |
2c7b4dbc MC |
1324 | } |
1325 | #ifndef OPENSSL_NO_COMP | |
c19602b5 | 1326 | if (ssl_allow_compression(s) |
38b051a1 TM |
1327 | && sctx->comp_methods |
1328 | && (SSL_CONNECTION_IS_DTLS(s) | |
1329 | || s->s3.tmp.max_ver < TLS1_3_VERSION)) { | |
1330 | int compnum = sk_SSL_COMP_num(sctx->comp_methods); | |
2c7b4dbc | 1331 | for (i = 0; i < compnum; i++) { |
38b051a1 | 1332 | comp = sk_SSL_COMP_value(sctx->comp_methods, i); |
7cea05dc | 1333 | if (!WPACKET_put_bytes_u8(pkt, comp->id)) { |
c48ffbcc | 1334 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 1335 | return CON_FUNC_ERROR; |
2c7b4dbc MC |
1336 | } |
1337 | } | |
b9908bf9 | 1338 | } |
09b6c2ef | 1339 | #endif |
2c7b4dbc | 1340 | /* Add the NULL method */ |
7cea05dc | 1341 | if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) { |
c48ffbcc | 1342 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 1343 | return CON_FUNC_ERROR; |
2c7b4dbc | 1344 | } |
761772d7 | 1345 | |
b9908bf9 | 1346 | /* TLS extensions */ |
f63a17d6 MC |
1347 | if (!tls_construct_extensions(s, pkt, SSL_EXT_CLIENT_HELLO, NULL, 0)) { |
1348 | /* SSLfatal() already called */ | |
67ec6d2b | 1349 | return CON_FUNC_ERROR; |
b9908bf9 | 1350 | } |
0f113f3e | 1351 | |
67ec6d2b | 1352 | return CON_FUNC_SUCCESS; |
0f113f3e | 1353 | } |
d02b48c6 | 1354 | |
38b051a1 | 1355 | MSG_PROCESS_RETURN dtls_process_hello_verify(SSL_CONNECTION *s, PACKET *pkt) |
8ba708e5 | 1356 | { |
cb150cbc | 1357 | size_t cookie_len; |
8ba708e5 MC |
1358 | PACKET cookiepkt; |
1359 | ||
1360 | if (!PACKET_forward(pkt, 2) | |
a230b26e | 1361 | || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) { |
c48ffbcc | 1362 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 1363 | return MSG_PROCESS_ERROR; |
8ba708e5 MC |
1364 | } |
1365 | ||
1366 | cookie_len = PACKET_remaining(&cookiepkt); | |
1367 | if (cookie_len > sizeof(s->d1->cookie)) { | |
c48ffbcc | 1368 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_TOO_LONG); |
f63a17d6 | 1369 | return MSG_PROCESS_ERROR; |
8ba708e5 MC |
1370 | } |
1371 | ||
1372 | if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) { | |
c48ffbcc | 1373 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 1374 | return MSG_PROCESS_ERROR; |
8ba708e5 MC |
1375 | } |
1376 | s->d1->cookie_len = cookie_len; | |
1377 | ||
1378 | return MSG_PROCESS_FINISHED_READING; | |
8ba708e5 MC |
1379 | } |
1380 | ||
38b051a1 TM |
1381 | static int set_client_ciphersuite(SSL_CONNECTION *s, |
1382 | const unsigned char *cipherchars) | |
b9908bf9 MC |
1383 | { |
1384 | STACK_OF(SSL_CIPHER) *sk; | |
1385 | const SSL_CIPHER *c; | |
11c67eea | 1386 | int i; |
38b051a1 | 1387 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
11c67eea MC |
1388 | |
1389 | c = ssl_get_cipher_by_char(s, cipherchars, 0); | |
1390 | if (c == NULL) { | |
1391 | /* unknown cipher */ | |
c48ffbcc | 1392 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CIPHER_RETURNED); |
11c67eea MC |
1393 | return 0; |
1394 | } | |
1395 | /* | |
1396 | * If it is a disabled cipher we either didn't send it in client hello, | |
1397 | * or it's not allowed for the selected protocol. So we return an error. | |
1398 | */ | |
8af91fd9 | 1399 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK, 1)) { |
c48ffbcc | 1400 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED); |
11c67eea MC |
1401 | return 0; |
1402 | } | |
1403 | ||
1404 | sk = ssl_get_ciphers_by_id(s); | |
1405 | i = sk_SSL_CIPHER_find(sk, c); | |
1406 | if (i < 0) { | |
1407 | /* we did not say we would use this cipher */ | |
c48ffbcc | 1408 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED); |
11c67eea MC |
1409 | return 0; |
1410 | } | |
1411 | ||
38b051a1 | 1412 | if (SSL_CONNECTION_IS_TLS13(s) && s->s3.tmp.new_cipher != NULL |
555cbb32 | 1413 | && s->s3.tmp.new_cipher->id != c->id) { |
11c67eea | 1414 | /* ServerHello selected a different ciphersuite to that in the HRR */ |
c48ffbcc | 1415 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED); |
11c67eea MC |
1416 | return 0; |
1417 | } | |
1418 | ||
1419 | /* | |
1420 | * Depending on the session caching (internal/external), the cipher | |
1421 | * and/or cipher_id values may not be set. Make sure that cipher_id is | |
1422 | * set and use it for comparison. | |
1423 | */ | |
1424 | if (s->session->cipher != NULL) | |
1425 | s->session->cipher_id = s->session->cipher->id; | |
1426 | if (s->hit && (s->session->cipher_id != c->id)) { | |
38b051a1 TM |
1427 | if (SSL_CONNECTION_IS_TLS13(s)) { |
1428 | const EVP_MD *md = ssl_md(sctx, c->algorithm2); | |
b740012f | 1429 | |
a055a881 MC |
1430 | /* |
1431 | * In TLSv1.3 it is valid for the server to select a different | |
1432 | * ciphersuite as long as the hash is the same. | |
1433 | */ | |
b740012f | 1434 | if (md == NULL |
38b051a1 | 1435 | || md != ssl_md(sctx, s->session->cipher->algorithm2)) { |
f63a17d6 | 1436 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
f63a17d6 | 1437 | SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED); |
a055a881 MC |
1438 | return 0; |
1439 | } | |
1440 | } else { | |
1441 | /* | |
1442 | * Prior to TLSv1.3 resuming a session always meant using the same | |
1443 | * ciphersuite. | |
1444 | */ | |
c48ffbcc | 1445 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
f63a17d6 | 1446 | SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
a055a881 MC |
1447 | return 0; |
1448 | } | |
11c67eea | 1449 | } |
555cbb32 | 1450 | s->s3.tmp.new_cipher = c; |
11c67eea MC |
1451 | |
1452 | return 1; | |
1453 | } | |
1454 | ||
38b051a1 | 1455 | MSG_PROCESS_RETURN tls_process_server_hello(SSL_CONNECTION *s, PACKET *pkt) |
11c67eea | 1456 | { |
332eb390 | 1457 | PACKET session_id, extpkt; |
b9908bf9 | 1458 | size_t session_id_len; |
b6981744 | 1459 | const unsigned char *cipherchars; |
597c51bc | 1460 | int hrr = 0; |
b9908bf9 | 1461 | unsigned int compression; |
4fa52141 | 1462 | unsigned int sversion; |
3434f40b | 1463 | unsigned int context; |
332eb390 | 1464 | RAW_EXTENSION *extensions = NULL; |
38b051a1 | 1465 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
b9908bf9 MC |
1466 | #ifndef OPENSSL_NO_COMP |
1467 | SSL_COMP *comp; | |
1468 | #endif | |
1469 | ||
4fa52141 | 1470 | if (!PACKET_get_net_2(pkt, &sversion)) { |
c48ffbcc | 1471 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 1472 | goto err; |
4fa52141 | 1473 | } |
50932c4a | 1474 | |
c3043dcd | 1475 | /* load the server random */ |
597c51bc MC |
1476 | if (s->version == TLS1_3_VERSION |
1477 | && sversion == TLS1_2_VERSION | |
1478 | && PACKET_remaining(pkt) >= SSL3_RANDOM_SIZE | |
1479 | && memcmp(hrrrandom, PACKET_data(pkt), SSL3_RANDOM_SIZE) == 0) { | |
d204a50b TM |
1480 | if (s->hello_retry_request != SSL_HRR_NONE) { |
1481 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); | |
1482 | goto err; | |
1483 | } | |
fc7129dc | 1484 | s->hello_retry_request = SSL_HRR_PENDING; |
9cd9e097 | 1485 | /* Tell the record layer that we know we're going to get TLSv1.3 */ |
1d367677 MC |
1486 | if (!ssl_set_record_protocol_version(s, s->version)) { |
1487 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1488 | goto err; | |
1489 | } | |
fc7129dc | 1490 | hrr = 1; |
597c51bc | 1491 | if (!PACKET_forward(pkt, SSL3_RANDOM_SIZE)) { |
c48ffbcc | 1492 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
597c51bc MC |
1493 | goto err; |
1494 | } | |
1495 | } else { | |
555cbb32 | 1496 | if (!PACKET_copy_bytes(pkt, s->s3.server_random, SSL3_RANDOM_SIZE)) { |
c48ffbcc | 1497 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
597c51bc MC |
1498 | goto err; |
1499 | } | |
c3043dcd MC |
1500 | } |
1501 | ||
88050dd1 MC |
1502 | /* Get the session-id. */ |
1503 | if (!PACKET_get_length_prefixed_1(pkt, &session_id)) { | |
c48ffbcc | 1504 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 1505 | goto err; |
0f113f3e | 1506 | } |
88050dd1 MC |
1507 | session_id_len = PACKET_remaining(&session_id); |
1508 | if (session_id_len > sizeof(s->session->session_id) | |
1509 | || session_id_len > SSL3_SESSION_ID_SIZE) { | |
c48ffbcc | 1510 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_SSL3_SESSION_ID_TOO_LONG); |
f63a17d6 | 1511 | goto err; |
524420d8 MC |
1512 | } |
1513 | ||
73999b62 | 1514 | if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) { |
c48ffbcc | 1515 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 1516 | goto err; |
fc5ce51d EK |
1517 | } |
1518 | ||
88050dd1 | 1519 | if (!PACKET_get_1(pkt, &compression)) { |
c48ffbcc | 1520 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
88050dd1 | 1521 | goto err; |
4ff65f77 MC |
1522 | } |
1523 | ||
1524 | /* TLS extensions */ | |
597c51bc | 1525 | if (PACKET_remaining(pkt) == 0 && !hrr) { |
4ff65f77 | 1526 | PACKET_null_init(&extpkt); |
26b9172a MC |
1527 | } else if (!PACKET_as_length_prefixed_2(pkt, &extpkt) |
1528 | || PACKET_remaining(pkt) != 0) { | |
c48ffbcc | 1529 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH); |
f63a17d6 | 1530 | goto err; |
4ff65f77 MC |
1531 | } |
1532 | ||
597c51bc MC |
1533 | if (!hrr) { |
1534 | if (!tls_collect_extensions(s, &extpkt, | |
1535 | SSL_EXT_TLS1_2_SERVER_HELLO | |
1536 | | SSL_EXT_TLS1_3_SERVER_HELLO, | |
1537 | &extensions, NULL, 1)) { | |
1538 | /* SSLfatal() already called */ | |
1539 | goto err; | |
1540 | } | |
1541 | ||
1542 | if (!ssl_choose_client_version(s, sversion, extensions)) { | |
1543 | /* SSLfatal() already called */ | |
1544 | goto err; | |
1545 | } | |
88050dd1 MC |
1546 | } |
1547 | ||
38b051a1 | 1548 | if (SSL_CONNECTION_IS_TLS13(s) || hrr) { |
597c51bc MC |
1549 | if (compression != 0) { |
1550 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, | |
597c51bc MC |
1551 | SSL_R_INVALID_COMPRESSION_ALGORITHM); |
1552 | goto err; | |
1553 | } | |
1554 | ||
1555 | if (session_id_len != s->tmp_session_id_len | |
1556 | || memcmp(PACKET_data(&session_id), s->tmp_session_id, | |
1557 | session_id_len) != 0) { | |
c48ffbcc | 1558 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_SESSION_ID); |
597c51bc MC |
1559 | goto err; |
1560 | } | |
1561 | } | |
1562 | ||
1563 | if (hrr) { | |
1564 | if (!set_client_ciphersuite(s, cipherchars)) { | |
1565 | /* SSLfatal() already called */ | |
1566 | goto err; | |
1567 | } | |
1568 | ||
1569 | return tls_process_as_hello_retry_request(s, &extpkt); | |
88050dd1 MC |
1570 | } |
1571 | ||
1572 | /* | |
1573 | * Now we have chosen the version we need to check again that the extensions | |
1574 | * are appropriate for this version. | |
1575 | */ | |
38b051a1 TM |
1576 | context = SSL_CONNECTION_IS_TLS13(s) ? SSL_EXT_TLS1_3_SERVER_HELLO |
1577 | : SSL_EXT_TLS1_2_SERVER_HELLO; | |
88050dd1 | 1578 | if (!tls_validate_all_contexts(s, context, extensions)) { |
c48ffbcc | 1579 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION); |
88050dd1 MC |
1580 | goto err; |
1581 | } | |
1582 | ||
4ff65f77 MC |
1583 | s->hit = 0; |
1584 | ||
38b051a1 | 1585 | if (SSL_CONNECTION_IS_TLS13(s)) { |
a5816a5a MC |
1586 | /* |
1587 | * In TLSv1.3 a ServerHello message signals a key change so the end of | |
1588 | * the message must be on a record boundary. | |
1589 | */ | |
1590 | if (RECORD_LAYER_processed_read_pending(&s->rlayer)) { | |
1591 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, | |
a5816a5a MC |
1592 | SSL_R_NOT_ON_RECORD_BOUNDARY); |
1593 | goto err; | |
1594 | } | |
1595 | ||
4ff65f77 MC |
1596 | /* This will set s->hit if we are resuming */ |
1597 | if (!tls_parse_extension(s, TLSEXT_IDX_psk, | |
fe874d27 | 1598 | SSL_EXT_TLS1_3_SERVER_HELLO, |
88050dd1 | 1599 | extensions, NULL, 0)) { |
f63a17d6 MC |
1600 | /* SSLfatal() already called */ |
1601 | goto err; | |
1602 | } | |
4ff65f77 | 1603 | } else { |
8c1a5343 | 1604 | /* |
4ff65f77 MC |
1605 | * Check if we can resume the session based on external pre-shared |
1606 | * secret. EAP-FAST (RFC 4851) supports two types of session resumption. | |
1607 | * Resumption based on server-side state works with session IDs. | |
1608 | * Resumption based on pre-shared Protected Access Credentials (PACs) | |
1609 | * works by overriding the SessionTicket extension at the application | |
1610 | * layer, and does not send a session ID. (We do not know whether | |
1611 | * EAP-FAST servers would honour the session ID.) Therefore, the session | |
1612 | * ID alone is not a reliable indicator of session resumption, so we | |
1613 | * first check if we can resume, and later peek at the next handshake | |
1614 | * message to see if the server wants to resume. | |
8c1a5343 | 1615 | */ |
4ff65f77 MC |
1616 | if (s->version >= TLS1_VERSION |
1617 | && s->ext.session_secret_cb != NULL && s->session->ext.tick) { | |
1618 | const SSL_CIPHER *pref_cipher = NULL; | |
1619 | /* | |
1620 | * s->session->master_key_length is a size_t, but this is an int for | |
1621 | * backwards compat reasons | |
1622 | */ | |
1623 | int master_key_length; | |
38b051a1 | 1624 | |
4ff65f77 | 1625 | master_key_length = sizeof(s->session->master_key); |
38b051a1 | 1626 | if (s->ext.session_secret_cb(ssl, s->session->master_key, |
4ff65f77 MC |
1627 | &master_key_length, |
1628 | NULL, &pref_cipher, | |
1629 | s->ext.session_secret_cb_arg) | |
1630 | && master_key_length > 0) { | |
1631 | s->session->master_key_length = master_key_length; | |
1632 | s->session->cipher = pref_cipher ? | |
60d685d1 | 1633 | pref_cipher : ssl_get_cipher_by_char(s, cipherchars, 0); |
4ff65f77 | 1634 | } else { |
c48ffbcc | 1635 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 1636 | goto err; |
4ff65f77 | 1637 | } |
0f113f3e | 1638 | } |
4ff65f77 MC |
1639 | |
1640 | if (session_id_len != 0 | |
1641 | && session_id_len == s->session->session_id_length | |
1642 | && memcmp(PACKET_data(&session_id), s->session->session_id, | |
1643 | session_id_len) == 0) | |
1644 | s->hit = 1; | |
50932c4a MC |
1645 | } |
1646 | ||
4ff65f77 | 1647 | if (s->hit) { |
0f113f3e | 1648 | if (s->sid_ctx_length != s->session->sid_ctx_length |
4ff65f77 | 1649 | || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) { |
0f113f3e | 1650 | /* actually a client application bug */ |
f63a17d6 | 1651 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
f63a17d6 MC |
1652 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
1653 | goto err; | |
0f113f3e | 1654 | } |
6e3d0153 | 1655 | } else { |
0f113f3e | 1656 | /* |
6e3d0153 | 1657 | * If we were trying for session-id reuse but the server |
4ff65f77 | 1658 | * didn't resume, make a new SSL_SESSION. |
6e3d0153 EK |
1659 | * In the case of EAP-FAST and PAC, we do not send a session ID, |
1660 | * so the PAC-based session secret is always preserved. It'll be | |
1661 | * overwritten if the server refuses resumption. | |
0f113f3e | 1662 | */ |
c96ce52c | 1663 | if (s->session->session_id_length > 0) { |
acce0557 | 1664 | ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss); |
0f113f3e | 1665 | if (!ssl_get_new_session(s, 0)) { |
f63a17d6 MC |
1666 | /* SSLfatal() already called */ |
1667 | goto err; | |
0f113f3e MC |
1668 | } |
1669 | } | |
50932c4a | 1670 | |
ccae4a15 | 1671 | s->session->ssl_version = s->version; |
a5816a5a MC |
1672 | /* |
1673 | * In TLSv1.2 and below we save the session id we were sent so we can | |
1674 | * resume it later. In TLSv1.3 the session id we were sent is just an | |
1675 | * echo of what we originally sent in the ClientHello and should not be | |
1676 | * used for resumption. | |
1677 | */ | |
38b051a1 | 1678 | if (!SSL_CONNECTION_IS_TLS13(s)) { |
a5816a5a MC |
1679 | s->session->session_id_length = session_id_len; |
1680 | /* session_id_len could be 0 */ | |
1681 | if (session_id_len > 0) | |
1682 | memcpy(s->session->session_id, PACKET_data(&session_id), | |
1683 | session_id_len); | |
1684 | } | |
0f113f3e | 1685 | } |
fc5ce51d | 1686 | |
ccae4a15 FI |
1687 | /* Session version and negotiated protocol version should match */ |
1688 | if (s->version != s->session->ssl_version) { | |
c48ffbcc | 1689 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, |
f63a17d6 MC |
1690 | SSL_R_SSL_SESSION_VERSION_MISMATCH); |
1691 | goto err; | |
ccae4a15 | 1692 | } |
0f113f3e | 1693 | /* |
3eb2aff4 KR |
1694 | * Now that we know the version, update the check to see if it's an allowed |
1695 | * version. | |
1696 | */ | |
555cbb32 TS |
1697 | s->s3.tmp.min_ver = s->version; |
1698 | s->s3.tmp.max_ver = s->version; | |
0f113f3e | 1699 | |
11c67eea | 1700 | if (!set_client_ciphersuite(s, cipherchars)) { |
f63a17d6 MC |
1701 | /* SSLfatal() already called */ |
1702 | goto err; | |
0f113f3e MC |
1703 | } |
1704 | ||
09b6c2ef | 1705 | #ifdef OPENSSL_NO_COMP |
fc5ce51d | 1706 | if (compression != 0) { |
c48ffbcc | 1707 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
f63a17d6 MC |
1708 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
1709 | goto err; | |
0f113f3e MC |
1710 | } |
1711 | /* | |
1712 | * If compression is disabled we'd better not try to resume a session | |
1713 | * using compression. | |
1714 | */ | |
1715 | if (s->session->compress_meth != 0) { | |
c48ffbcc | 1716 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION); |
f63a17d6 | 1717 | goto err; |
0f113f3e | 1718 | } |
09b6c2ef | 1719 | #else |
fc5ce51d | 1720 | if (s->hit && compression != s->session->compress_meth) { |
c48ffbcc | 1721 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
dd5a4279 | 1722 | SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED); |
f63a17d6 | 1723 | goto err; |
0f113f3e | 1724 | } |
fc5ce51d | 1725 | if (compression == 0) |
0f113f3e MC |
1726 | comp = NULL; |
1727 | else if (!ssl_allow_compression(s)) { | |
c48ffbcc | 1728 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COMPRESSION_DISABLED); |
f63a17d6 | 1729 | goto err; |
fc5ce51d | 1730 | } else { |
38b051a1 TM |
1731 | comp = ssl3_comp_find(SSL_CONNECTION_GET_CTX(s)->comp_methods, |
1732 | compression); | |
fc5ce51d | 1733 | } |
0f113f3e | 1734 | |
fc5ce51d | 1735 | if (compression != 0 && comp == NULL) { |
c48ffbcc | 1736 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
f63a17d6 MC |
1737 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
1738 | goto err; | |
0f113f3e | 1739 | } else { |
555cbb32 | 1740 | s->s3.tmp.new_compression = comp; |
0f113f3e | 1741 | } |
09b6c2ef | 1742 | #endif |
761772d7 | 1743 | |
f63a17d6 MC |
1744 | if (!tls_parse_all_extensions(s, context, extensions, NULL, 0, 1)) { |
1745 | /* SSLfatal() already called */ | |
1746 | goto err; | |
1747 | } | |
332eb390 | 1748 | |
8723588e | 1749 | #ifndef OPENSSL_NO_SCTP |
38b051a1 | 1750 | if (SSL_CONNECTION_IS_DTLS(s) && s->hit) { |
8723588e MC |
1751 | unsigned char sctpauthkey[64]; |
1752 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; | |
09d62b33 | 1753 | size_t labellen; |
8723588e MC |
1754 | |
1755 | /* | |
1756 | * Add new shared key for SCTP-Auth, will be ignored if | |
1757 | * no SCTP used. | |
1758 | */ | |
141eb8c6 MC |
1759 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
1760 | sizeof(DTLS1_SCTP_AUTH_LABEL)); | |
8723588e | 1761 | |
09d62b33 MT |
1762 | /* Don't include the terminating zero. */ |
1763 | labellen = sizeof(labelbuffer) - 1; | |
1764 | if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG) | |
1765 | labellen += 1; | |
1766 | ||
38b051a1 | 1767 | if (SSL_export_keying_material(ssl, sctpauthkey, |
a230b26e EK |
1768 | sizeof(sctpauthkey), |
1769 | labelbuffer, | |
09d62b33 | 1770 | labellen, NULL, 0, 0) <= 0) { |
c48ffbcc | 1771 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 MC |
1772 | goto err; |
1773 | } | |
8723588e | 1774 | |
38b051a1 | 1775 | BIO_ctrl(SSL_get_wbio(ssl), |
8723588e MC |
1776 | BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, |
1777 | sizeof(sctpauthkey), sctpauthkey); | |
1778 | } | |
1779 | #endif | |
1780 | ||
92760c21 MC |
1781 | /* |
1782 | * In TLSv1.3 we have some post-processing to change cipher state, otherwise | |
1783 | * we're done with this message | |
1784 | */ | |
38b051a1 TM |
1785 | if (SSL_CONNECTION_IS_TLS13(s) |
1786 | && (!ssl->method->ssl3_enc->setup_key_block(s) | |
1787 | || !ssl->method->ssl3_enc->change_cipher_state(s, | |
92760c21 | 1788 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_READ))) { |
f63a17d6 MC |
1789 | /* SSLfatal() already called */ |
1790 | goto err; | |
92760c21 MC |
1791 | } |
1792 | ||
1b0286a3 | 1793 | OPENSSL_free(extensions); |
b9908bf9 | 1794 | return MSG_PROCESS_CONTINUE_READING; |
f63a17d6 | 1795 | err: |
1b0286a3 | 1796 | OPENSSL_free(extensions); |
b9908bf9 | 1797 | return MSG_PROCESS_ERROR; |
0f113f3e | 1798 | } |
d02b48c6 | 1799 | |
38b051a1 | 1800 | static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL_CONNECTION *s, |
597c51bc | 1801 | PACKET *extpkt) |
3847d426 | 1802 | { |
3847d426 | 1803 | RAW_EXTENSION *extensions = NULL; |
3847d426 | 1804 | |
d4504fe5 | 1805 | /* |
f471f60a MC |
1806 | * If we were sending early_data then any alerts should not be sent using |
1807 | * the old wrlmethod. | |
d4504fe5 | 1808 | */ |
f471f60a MC |
1809 | if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING |
1810 | && !ssl_set_new_record_layer(s, | |
1811 | TLS_ANY_VERSION, | |
1812 | OSSL_RECORD_DIRECTION_WRITE, | |
1813 | OSSL_RECORD_PROTECTION_LEVEL_NONE, | |
3f9175c7 MC |
1814 | NULL, 0, NULL, 0, NULL, 0, NULL, 0, |
1815 | NULL, 0, NID_undef, NULL, NULL, NULL)) { | |
f471f60a MC |
1816 | /* SSLfatal already called */ |
1817 | goto err; | |
1818 | } | |
1819 | /* We are definitely going to be using TLSv1.3 */ | |
1820 | s->rlayer.wrlmethod->set_protocol_version(s->rlayer.wrl, TLS1_3_VERSION); | |
d4504fe5 | 1821 | |
597c51bc | 1822 | if (!tls_collect_extensions(s, extpkt, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST, |
f63a17d6 | 1823 | &extensions, NULL, 1) |
fe874d27 | 1824 | || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST, |
f63a17d6 MC |
1825 | extensions, NULL, 0, 1)) { |
1826 | /* SSLfatal() already called */ | |
1827 | goto err; | |
1828 | } | |
3847d426 MC |
1829 | |
1830 | OPENSSL_free(extensions); | |
66d4bf6b MC |
1831 | extensions = NULL; |
1832 | ||
5b64ce89 | 1833 | if (s->ext.tls13_cookie_len == 0 && s->s3.tmp.pkey != NULL) { |
66d4bf6b MC |
1834 | /* |
1835 | * We didn't receive a cookie or a new key_share so the next | |
1836 | * ClientHello will not change | |
1837 | */ | |
c48ffbcc | 1838 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CHANGE_FOLLOWING_HRR); |
f63a17d6 | 1839 | goto err; |
66d4bf6b | 1840 | } |
3847d426 | 1841 | |
11c67eea MC |
1842 | /* |
1843 | * Re-initialise the Transcript Hash. We're going to prepopulate it with | |
1844 | * a synthetic message_hash in place of ClientHello1. | |
1845 | */ | |
43054d3d | 1846 | if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) { |
f63a17d6 MC |
1847 | /* SSLfatal() already called */ |
1848 | goto err; | |
11c67eea MC |
1849 | } |
1850 | ||
1851 | /* | |
1852 | * Add this message to the Transcript Hash. Normally this is done | |
1853 | * automatically prior to the message processing stage. However due to the | |
1854 | * need to create the synthetic message hash, we defer that step until now | |
1855 | * for HRR messages. | |
1856 | */ | |
1857 | if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, | |
1858 | s->init_num + SSL3_HM_HEADER_LENGTH)) { | |
f63a17d6 MC |
1859 | /* SSLfatal() already called */ |
1860 | goto err; | |
11c67eea MC |
1861 | } |
1862 | ||
3847d426 | 1863 | return MSG_PROCESS_FINISHED_READING; |
f63a17d6 | 1864 | err: |
3847d426 MC |
1865 | OPENSSL_free(extensions); |
1866 | return MSG_PROCESS_ERROR; | |
1867 | } | |
1868 | ||
3c95ef22 TS |
1869 | MSG_PROCESS_RETURN tls_process_server_rpk(SSL_CONNECTION *sc, PACKET *pkt) |
1870 | { | |
1871 | EVP_PKEY *peer_rpk; | |
1872 | ||
1873 | if (!tls_process_rpk(sc, pkt, &peer_rpk)) { | |
1874 | /* SSLfatal() already called */ | |
1875 | return MSG_PROCESS_ERROR; | |
1876 | } | |
1877 | ||
1878 | if (peer_rpk == NULL) { | |
1879 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_CERTIFICATE); | |
1880 | return MSG_PROCESS_ERROR; | |
1881 | } | |
1882 | ||
1883 | EVP_PKEY_free(sc->session->peer_rpk); | |
1884 | sc->session->peer_rpk = peer_rpk; | |
1885 | ||
1886 | return MSG_PROCESS_CONTINUE_PROCESSING; | |
1887 | } | |
1888 | ||
1889 | static WORK_STATE tls_post_process_server_rpk(SSL_CONNECTION *sc, | |
1890 | WORK_STATE wst) | |
1891 | { | |
1892 | size_t certidx; | |
1893 | const SSL_CERT_LOOKUP *clu; | |
1894 | ||
1895 | if (sc->session->peer_rpk == NULL) { | |
1896 | SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, | |
1897 | SSL_R_INVALID_RAW_PUBLIC_KEY); | |
1898 | return WORK_ERROR; | |
1899 | } | |
1900 | ||
1901 | if (sc->rwstate == SSL_RETRY_VERIFY) | |
1902 | sc->rwstate = SSL_NOTHING; | |
1903 | if (ssl_verify_rpk(sc, sc->session->peer_rpk) > 0 | |
1904 | && sc->rwstate == SSL_RETRY_VERIFY) | |
1905 | return WORK_MORE_A; | |
1906 | ||
1907 | if ((clu = ssl_cert_lookup_by_pkey(sc->session->peer_rpk, &certidx, | |
1908 | SSL_CONNECTION_GET_CTX(sc))) == NULL) { | |
1909 | SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE); | |
1910 | return WORK_ERROR; | |
1911 | } | |
1912 | ||
1913 | /* | |
1914 | * Check certificate type is consistent with ciphersuite. For TLS 1.3 | |
1915 | * skip check since TLS 1.3 ciphersuites can be used with any certificate | |
1916 | * type. | |
1917 | */ | |
1918 | if (!SSL_CONNECTION_IS_TLS13(sc)) { | |
1919 | if ((clu->amask & sc->s3.tmp.new_cipher->algorithm_auth) == 0) { | |
1920 | SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_RPK_TYPE); | |
1921 | return WORK_ERROR; | |
1922 | } | |
1923 | } | |
1924 | ||
1925 | /* Ensure there is no peer/peer_chain */ | |
1926 | X509_free(sc->session->peer); | |
1927 | sc->session->peer = NULL; | |
1928 | sk_X509_pop_free(sc->session->peer_chain, X509_free); | |
1929 | sc->session->peer_chain = NULL; | |
1930 | sc->session->verify_result = sc->verify_result; | |
1931 | ||
1932 | /* Save the current hash state for when we receive the CertificateVerify */ | |
1933 | if (SSL_CONNECTION_IS_TLS13(sc) | |
1934 | && !ssl_handshake_hash(sc, sc->cert_verify_hash, | |
1935 | sizeof(sc->cert_verify_hash), | |
1936 | &sc->cert_verify_hash_len)) { | |
1937 | /* SSLfatal() already called */ | |
1938 | return WORK_ERROR; | |
1939 | } | |
1940 | ||
1941 | return WORK_FINISHED_CONTINUE; | |
1942 | } | |
1943 | ||
3201abeb | 1944 | /* prepare server cert verification by setting s->session->peer_chain from pkt */ |
38b051a1 TM |
1945 | MSG_PROCESS_RETURN tls_process_server_certificate(SSL_CONNECTION *s, |
1946 | PACKET *pkt) | |
b9908bf9 | 1947 | { |
b9908bf9 MC |
1948 | unsigned long cert_list_len, cert_len; |
1949 | X509 *x = NULL; | |
b6981744 | 1950 | const unsigned char *certstart, *certbytes; |
0c3eb279 | 1951 | size_t chainidx; |
e96e0f8e | 1952 | unsigned int context = 0; |
38b051a1 | 1953 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
0f113f3e | 1954 | |
3c95ef22 TS |
1955 | if (s->ext.server_cert_type == TLSEXT_cert_type_rpk) |
1956 | return tls_process_server_rpk(s, pkt); | |
1957 | if (s->ext.server_cert_type != TLSEXT_cert_type_x509) { | |
1958 | SSLfatal(s, SSL_AD_UNSUPPORTED_CERTIFICATE, | |
1959 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); | |
1960 | goto err; | |
1961 | } | |
1962 | ||
0c3eb279 | 1963 | if ((s->session->peer_chain = sk_X509_new_null()) == NULL) { |
e077455e | 1964 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
cc273a93 | 1965 | goto err; |
0f113f3e MC |
1966 | } |
1967 | ||
38b051a1 | 1968 | if ((SSL_CONNECTION_IS_TLS13(s) && !PACKET_get_1(pkt, &context)) |
e96e0f8e MC |
1969 | || context != 0 |
1970 | || !PACKET_get_net_3(pkt, &cert_list_len) | |
1a281aab MC |
1971 | || PACKET_remaining(pkt) != cert_list_len |
1972 | || PACKET_remaining(pkt) == 0) { | |
c48ffbcc | 1973 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 1974 | goto err; |
0f113f3e | 1975 | } |
d805a57b | 1976 | for (chainidx = 0; PACKET_remaining(pkt); chainidx++) { |
73999b62 | 1977 | if (!PACKET_get_net_3(pkt, &cert_len) |
a230b26e | 1978 | || !PACKET_get_bytes(pkt, &certbytes, cert_len)) { |
c48ffbcc | 1979 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH); |
f63a17d6 | 1980 | goto err; |
0f113f3e MC |
1981 | } |
1982 | ||
df758a85 | 1983 | certstart = certbytes; |
38b051a1 | 1984 | x = X509_new_ex(sctx->libctx, sctx->propq); |
0f113f3e | 1985 | if (x == NULL) { |
e077455e | 1986 | SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB); |
6725682d SL |
1987 | goto err; |
1988 | } | |
1989 | if (d2i_X509(&x, (const unsigned char **)&certbytes, | |
1990 | cert_len) == NULL) { | |
c48ffbcc | 1991 | SSLfatal(s, SSL_AD_BAD_CERTIFICATE, ERR_R_ASN1_LIB); |
f63a17d6 | 1992 | goto err; |
0f113f3e | 1993 | } |
6725682d | 1994 | |
df758a85 | 1995 | if (certbytes != (certstart + cert_len)) { |
c48ffbcc | 1996 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH); |
f63a17d6 | 1997 | goto err; |
0f113f3e | 1998 | } |
e96e0f8e | 1999 | |
38b051a1 | 2000 | if (SSL_CONNECTION_IS_TLS13(s)) { |
e96e0f8e MC |
2001 | RAW_EXTENSION *rawexts = NULL; |
2002 | PACKET extensions; | |
2003 | ||
2004 | if (!PACKET_get_length_prefixed_2(pkt, &extensions)) { | |
c48ffbcc | 2005 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH); |
f63a17d6 | 2006 | goto err; |
e96e0f8e | 2007 | } |
fe874d27 MC |
2008 | if (!tls_collect_extensions(s, &extensions, |
2009 | SSL_EXT_TLS1_3_CERTIFICATE, &rawexts, | |
f63a17d6 | 2010 | NULL, chainidx == 0) |
8e1634ec | 2011 | || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE, |
f63a17d6 | 2012 | rawexts, x, chainidx, |
8e1634ec | 2013 | PACKET_remaining(pkt) == 0)) { |
5ee289ea | 2014 | OPENSSL_free(rawexts); |
f63a17d6 MC |
2015 | /* SSLfatal already called */ |
2016 | goto err; | |
5ee289ea MC |
2017 | } |
2018 | OPENSSL_free(rawexts); | |
e96e0f8e MC |
2019 | } |
2020 | ||
0c3eb279 | 2021 | if (!sk_X509_push(s->session->peer_chain, x)) { |
e077455e | 2022 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
cc273a93 | 2023 | goto err; |
0f113f3e MC |
2024 | } |
2025 | x = NULL; | |
0f113f3e | 2026 | } |
0c3eb279 DDO |
2027 | return MSG_PROCESS_CONTINUE_PROCESSING; |
2028 | ||
2029 | err: | |
2030 | X509_free(x); | |
79b2a2f2 | 2031 | OSSL_STACK_OF_X509_free(s->session->peer_chain); |
0c3eb279 DDO |
2032 | s->session->peer_chain = NULL; |
2033 | return MSG_PROCESS_ERROR; | |
2034 | } | |
0f113f3e | 2035 | |
0c3eb279 DDO |
2036 | /* |
2037 | * Verify the s->session->peer_chain and check server cert type. | |
2038 | * On success set s->session->peer and s->session->verify_result. | |
2039 | * Else the peer certificate verification callback may request retry. | |
2040 | */ | |
38b051a1 TM |
2041 | WORK_STATE tls_post_process_server_certificate(SSL_CONNECTION *s, |
2042 | WORK_STATE wst) | |
0c3eb279 DDO |
2043 | { |
2044 | X509 *x; | |
2045 | EVP_PKEY *pkey = NULL; | |
2046 | const SSL_CERT_LOOKUP *clu; | |
2047 | size_t certidx; | |
2048 | int i; | |
2049 | ||
3c95ef22 TS |
2050 | if (s->ext.server_cert_type == TLSEXT_cert_type_rpk) |
2051 | return tls_post_process_server_rpk(s, wst); | |
2052 | ||
dfb39f73 TM |
2053 | if (s->rwstate == SSL_RETRY_VERIFY) |
2054 | s->rwstate = SSL_NOTHING; | |
0c3eb279 | 2055 | i = ssl_verify_cert_chain(s, s->session->peer_chain); |
dfb39f73 | 2056 | if (i > 0 && s->rwstate == SSL_RETRY_VERIFY) { |
0c3eb279 DDO |
2057 | return WORK_MORE_A; |
2058 | } | |
c8e2f98c MC |
2059 | /* |
2060 | * The documented interface is that SSL_VERIFY_PEER should be set in order | |
2061 | * for client side verification of the server certificate to take place. | |
2062 | * However, historically the code has only checked that *any* flag is set | |
2063 | * to cause server verification to take place. Use of the other flags makes | |
2064 | * no sense in client mode. An attempt to clean up the semantics was | |
2065 | * reverted because at least one application *only* set | |
2066 | * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused | |
2067 | * server verification to take place, after the clean up it silently did | |
2068 | * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags | |
2069 | * sent to them because they are void functions. Therefore, we now use the | |
2070 | * (less clean) historic behaviour of performing validation if any flag is | |
2071 | * set. The *documented* interface remains the same. | |
2072 | */ | |
dfb39f73 | 2073 | if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) { |
c6d38183 | 2074 | SSLfatal(s, ssl_x509err2alert(s->verify_result), |
f63a17d6 | 2075 | SSL_R_CERTIFICATE_VERIFY_FAILED); |
0c3eb279 | 2076 | return WORK_ERROR; |
0f113f3e MC |
2077 | } |
2078 | ERR_clear_error(); /* but we keep s->verify_result */ | |
0f113f3e | 2079 | |
0f113f3e MC |
2080 | /* |
2081 | * Inconsistency alert: cert_chain does include the peer's certificate, | |
d4d78943 | 2082 | * which we don't include in statem_srvr.c |
0f113f3e | 2083 | */ |
0c3eb279 | 2084 | x = sk_X509_value(s->session->peer_chain, 0); |
0f113f3e | 2085 | |
8382fd3a | 2086 | pkey = X509_get0_pubkey(x); |
0f113f3e | 2087 | |
55a9a16f | 2088 | if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) { |
c48ffbcc | 2089 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
f63a17d6 | 2090 | SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); |
0c3eb279 | 2091 | return WORK_ERROR; |
0f113f3e MC |
2092 | } |
2093 | ||
ee58915c MB |
2094 | if ((clu = ssl_cert_lookup_by_pkey(pkey, &certidx, |
2095 | SSL_CONNECTION_GET_CTX(s))) == NULL) { | |
c48ffbcc | 2096 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
0c3eb279 | 2097 | return WORK_ERROR; |
0f113f3e | 2098 | } |
05b8486e DSH |
2099 | /* |
2100 | * Check certificate type is consistent with ciphersuite. For TLS 1.3 | |
2101 | * skip check since TLS 1.3 ciphersuites can be used with any certificate | |
2102 | * type. | |
2103 | */ | |
38b051a1 | 2104 | if (!SSL_CONNECTION_IS_TLS13(s)) { |
555cbb32 | 2105 | if ((clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0) { |
c48ffbcc | 2106 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CERTIFICATE_TYPE); |
0c3eb279 | 2107 | return WORK_ERROR; |
05b8486e | 2108 | } |
0f113f3e | 2109 | } |
55a9a16f MC |
2110 | |
2111 | X509_free(s->session->peer); | |
05f0fb9f | 2112 | X509_up_ref(x); |
55a9a16f | 2113 | s->session->peer = x; |
0f113f3e | 2114 | s->session->verify_result = s->verify_result; |
3c95ef22 TS |
2115 | /* Ensure there is no RPK */ |
2116 | EVP_PKEY_free(s->session->peer_rpk); | |
2117 | s->session->peer_rpk = NULL; | |
2c5dfdc3 MC |
2118 | |
2119 | /* Save the current hash state for when we receive the CertificateVerify */ | |
38b051a1 | 2120 | if (SSL_CONNECTION_IS_TLS13(s) |
2c5dfdc3 MC |
2121 | && !ssl_handshake_hash(s, s->cert_verify_hash, |
2122 | sizeof(s->cert_verify_hash), | |
2123 | &s->cert_verify_hash_len)) { | |
f63a17d6 | 2124 | /* SSLfatal() already called */; |
0c3eb279 | 2125 | return WORK_ERROR; |
2c5dfdc3 | 2126 | } |
0c3eb279 | 2127 | return WORK_FINISHED_CONTINUE; |
0f113f3e | 2128 | } |
d02b48c6 | 2129 | |
b67cb09f TS |
2130 | #ifndef OPENSSL_NO_COMP_ALG |
2131 | MSG_PROCESS_RETURN tls_process_server_compressed_certificate(SSL_CONNECTION *sc, PACKET *pkt) | |
2132 | { | |
2133 | MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR; | |
2134 | PACKET tmppkt; | |
2135 | BUF_MEM *buf = BUF_MEM_new(); | |
2136 | ||
2137 | if (tls13_process_compressed_certificate(sc, pkt, &tmppkt, buf) != MSG_PROCESS_ERROR) | |
2138 | ret = tls_process_server_certificate(sc, &tmppkt); | |
2139 | ||
2140 | BUF_MEM_free(buf); | |
2141 | return ret; | |
2142 | } | |
2143 | #endif | |
2144 | ||
38b051a1 | 2145 | static int tls_process_ske_psk_preamble(SSL_CONNECTION *s, PACKET *pkt) |
02a74590 MC |
2146 | { |
2147 | #ifndef OPENSSL_NO_PSK | |
7dc1c647 | 2148 | PACKET psk_identity_hint; |
02a74590 | 2149 | |
7dc1c647 MC |
2150 | /* PSK ciphersuites are preceded by an identity hint */ |
2151 | ||
2152 | if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) { | |
c48ffbcc | 2153 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
7dc1c647 MC |
2154 | return 0; |
2155 | } | |
2156 | ||
2157 | /* | |
2158 | * Store PSK identity hint for later use, hint is used in | |
2159 | * tls_construct_client_key_exchange. Assume that the maximum length of | |
2160 | * a PSK identity hint can be as long as the maximum length of a PSK | |
2161 | * identity. | |
2162 | */ | |
2163 | if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) { | |
c48ffbcc | 2164 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DATA_LENGTH_TOO_LONG); |
7dc1c647 MC |
2165 | return 0; |
2166 | } | |
02a74590 | 2167 | |
7dc1c647 MC |
2168 | if (PACKET_remaining(&psk_identity_hint) == 0) { |
2169 | OPENSSL_free(s->session->psk_identity_hint); | |
2170 | s->session->psk_identity_hint = NULL; | |
2171 | } else if (!PACKET_strndup(&psk_identity_hint, | |
a230b26e | 2172 | &s->session->psk_identity_hint)) { |
c48ffbcc | 2173 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
7dc1c647 MC |
2174 | return 0; |
2175 | } | |
2176 | ||
2177 | return 1; | |
2178 | #else | |
c48ffbcc | 2179 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
7dc1c647 | 2180 | return 0; |
02a74590 MC |
2181 | #endif |
2182 | } | |
2183 | ||
38b051a1 | 2184 | static int tls_process_ske_srp(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey) |
25c6c10c MC |
2185 | { |
2186 | #ifndef OPENSSL_NO_SRP | |
2187 | PACKET prime, generator, salt, server_pub; | |
2188 | ||
2189 | if (!PACKET_get_length_prefixed_2(pkt, &prime) | |
2190 | || !PACKET_get_length_prefixed_2(pkt, &generator) | |
2191 | || !PACKET_get_length_prefixed_1(pkt, &salt) | |
2192 | || !PACKET_get_length_prefixed_2(pkt, &server_pub)) { | |
c48ffbcc | 2193 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
25c6c10c MC |
2194 | return 0; |
2195 | } | |
2196 | ||
2197 | if ((s->srp_ctx.N = | |
2198 | BN_bin2bn(PACKET_data(&prime), | |
348240c6 | 2199 | (int)PACKET_remaining(&prime), NULL)) == NULL |
25c6c10c MC |
2200 | || (s->srp_ctx.g = |
2201 | BN_bin2bn(PACKET_data(&generator), | |
348240c6 | 2202 | (int)PACKET_remaining(&generator), NULL)) == NULL |
25c6c10c MC |
2203 | || (s->srp_ctx.s = |
2204 | BN_bin2bn(PACKET_data(&salt), | |
348240c6 | 2205 | (int)PACKET_remaining(&salt), NULL)) == NULL |
25c6c10c MC |
2206 | || (s->srp_ctx.B = |
2207 | BN_bin2bn(PACKET_data(&server_pub), | |
348240c6 | 2208 | (int)PACKET_remaining(&server_pub), NULL)) == NULL) { |
c48ffbcc | 2209 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB); |
25c6c10c MC |
2210 | return 0; |
2211 | } | |
2212 | ||
a2c2e000 MC |
2213 | if (!srp_verify_server_param(s)) { |
2214 | /* SSLfatal() already called */ | |
25c6c10c MC |
2215 | return 0; |
2216 | } | |
2217 | ||
2218 | /* We must check if there is a certificate */ | |
555cbb32 | 2219 | if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS)) |
3c95ef22 | 2220 | *pkey = tls_get_peer_pkey(s); |
25c6c10c MC |
2221 | |
2222 | return 1; | |
2223 | #else | |
c48ffbcc | 2224 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
25c6c10c MC |
2225 | return 0; |
2226 | #endif | |
2227 | } | |
2228 | ||
38b051a1 | 2229 | static int tls_process_ske_dhe(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey) |
e01a610d | 2230 | { |
e01a610d MC |
2231 | PACKET prime, generator, pub_key; |
2232 | EVP_PKEY *peer_tmp = NULL; | |
e01a610d | 2233 | BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL; |
1ee22dc2 MC |
2234 | EVP_PKEY_CTX *pctx = NULL; |
2235 | OSSL_PARAM *params = NULL; | |
2236 | OSSL_PARAM_BLD *tmpl = NULL; | |
38b051a1 | 2237 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1ee22dc2 | 2238 | int ret = 0; |
26505153 | 2239 | |
e01a610d MC |
2240 | if (!PACKET_get_length_prefixed_2(pkt, &prime) |
2241 | || !PACKET_get_length_prefixed_2(pkt, &generator) | |
2242 | || !PACKET_get_length_prefixed_2(pkt, &pub_key)) { | |
c48ffbcc | 2243 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
e01a610d MC |
2244 | return 0; |
2245 | } | |
2246 | ||
348240c6 MC |
2247 | p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL); |
2248 | g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator), | |
2249 | NULL); | |
2250 | bnpub_key = BN_bin2bn(PACKET_data(&pub_key), | |
2251 | (int)PACKET_remaining(&pub_key), NULL); | |
e01a610d | 2252 | if (p == NULL || g == NULL || bnpub_key == NULL) { |
c48ffbcc | 2253 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB); |
e01a610d MC |
2254 | goto err; |
2255 | } | |
2256 | ||
1ee22dc2 MC |
2257 | tmpl = OSSL_PARAM_BLD_new(); |
2258 | if (tmpl == NULL | |
2259 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p) | |
2260 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g) | |
2261 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, | |
2262 | bnpub_key) | |
2263 | || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) { | |
2264 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
e01a610d MC |
2265 | goto err; |
2266 | } | |
e01a610d | 2267 | |
38b051a1 | 2268 | pctx = EVP_PKEY_CTX_new_from_name(sctx->libctx, "DH", sctx->propq); |
1ee22dc2 MC |
2269 | if (pctx == NULL) { |
2270 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
26505153 RL |
2271 | goto err; |
2272 | } | |
2db985b7 SL |
2273 | if (EVP_PKEY_fromdata_init(pctx) <= 0 |
2274 | || EVP_PKEY_fromdata(pctx, &peer_tmp, EVP_PKEY_KEYPAIR, params) <= 0) { | |
1ee22dc2 | 2275 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_DH_VALUE); |
e01a610d MC |
2276 | goto err; |
2277 | } | |
e01a610d | 2278 | |
1ee22dc2 | 2279 | EVP_PKEY_CTX_free(pctx); |
38b051a1 | 2280 | pctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, peer_tmp, sctx->propq); |
1ee22dc2 | 2281 | if (pctx == NULL |
899e2564 MC |
2282 | /* |
2283 | * EVP_PKEY_param_check() will verify that the DH params are using | |
2284 | * a safe prime. In this context, because we're using ephemeral DH, | |
2285 | * we're ok with it not being a safe prime. | |
2286 | * EVP_PKEY_param_check_quick() skips the safe prime check. | |
2287 | */ | |
2288 | || EVP_PKEY_param_check_quick(pctx) != 1 | |
1ee22dc2 MC |
2289 | || EVP_PKEY_public_check(pctx) != 1) { |
2290 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_DH_VALUE); | |
e01a610d MC |
2291 | goto err; |
2292 | } | |
2293 | ||
ed576acd TM |
2294 | if (!ssl_security(s, SSL_SECOP_TMP_DH, |
2295 | EVP_PKEY_get_security_bits(peer_tmp), | |
47e81a1b | 2296 | 0, peer_tmp)) { |
c48ffbcc | 2297 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL); |
ada66e78 P |
2298 | goto err; |
2299 | } | |
2300 | ||
555cbb32 | 2301 | s->s3.peer_tmp = peer_tmp; |
1ee22dc2 | 2302 | peer_tmp = NULL; |
e01a610d MC |
2303 | |
2304 | /* | |
2305 | * FIXME: This makes assumptions about which ciphersuites come with | |
2306 | * public keys. We should have a less ad-hoc way of doing this | |
2307 | */ | |
555cbb32 | 2308 | if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS)) |
3c95ef22 | 2309 | *pkey = tls_get_peer_pkey(s); |
e01a610d MC |
2310 | /* else anonymous DH, so no certificate or pkey. */ |
2311 | ||
1ee22dc2 | 2312 | ret = 1; |
e01a610d MC |
2313 | |
2314 | err: | |
1ee22dc2 | 2315 | OSSL_PARAM_BLD_free(tmpl); |
3f883c7c | 2316 | OSSL_PARAM_free(params); |
1ee22dc2 MC |
2317 | EVP_PKEY_free(peer_tmp); |
2318 | EVP_PKEY_CTX_free(pctx); | |
e01a610d MC |
2319 | BN_free(p); |
2320 | BN_free(g); | |
2321 | BN_free(bnpub_key); | |
e01a610d | 2322 | |
1ee22dc2 | 2323 | return ret; |
e01a610d MC |
2324 | } |
2325 | ||
38b051a1 | 2326 | static int tls_process_ske_ecdhe(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey) |
ff74aeb1 | 2327 | { |
ff74aeb1 | 2328 | PACKET encoded_pt; |
6447e818 | 2329 | unsigned int curve_type, curve_id; |
ff74aeb1 MC |
2330 | |
2331 | /* | |
2332 | * Extract elliptic curve parameters and the server's ephemeral ECDH | |
6447e818 | 2333 | * public key. We only support named (not generic) curves and |
ff74aeb1 MC |
2334 | * ECParameters in this case is just three bytes. |
2335 | */ | |
6447e818 | 2336 | if (!PACKET_get_1(pkt, &curve_type) || !PACKET_get_net_2(pkt, &curve_id)) { |
c48ffbcc | 2337 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT); |
ff74aeb1 MC |
2338 | return 0; |
2339 | } | |
2340 | /* | |
6447e818 DSH |
2341 | * Check curve is named curve type and one of our preferences, if not |
2342 | * server has sent an invalid curve. | |
ff74aeb1 | 2343 | */ |
dcf8b01f MC |
2344 | if (curve_type != NAMED_CURVE_TYPE |
2345 | || !tls1_check_group_id(s, curve_id, 1)) { | |
c48ffbcc | 2346 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE); |
ff74aeb1 MC |
2347 | return 0; |
2348 | } | |
2349 | ||
ada66e78 | 2350 | if ((s->s3.peer_tmp = ssl_generate_param_group(s, curve_id)) == NULL) { |
c48ffbcc | 2351 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
a2c2e000 | 2352 | SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); |
ff74aeb1 MC |
2353 | return 0; |
2354 | } | |
2355 | ||
ff74aeb1 | 2356 | if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) { |
c48ffbcc | 2357 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
ff74aeb1 MC |
2358 | return 0; |
2359 | } | |
2360 | ||
5ac8fb58 MC |
2361 | if (EVP_PKEY_set1_encoded_public_key(s->s3.peer_tmp, |
2362 | PACKET_data(&encoded_pt), | |
2363 | PACKET_remaining(&encoded_pt)) <= 0) { | |
c48ffbcc | 2364 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT); |
ff74aeb1 MC |
2365 | return 0; |
2366 | } | |
2367 | ||
2368 | /* | |
2369 | * The ECC/TLS specification does not mention the use of DSA to sign | |
2370 | * ECParameters in the server key exchange message. We do support RSA | |
2371 | * and ECDSA. | |
2372 | */ | |
555cbb32 | 2373 | if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA) |
3c95ef22 | 2374 | *pkey = tls_get_peer_pkey(s); |
555cbb32 | 2375 | else if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aRSA) |
3c95ef22 | 2376 | *pkey = tls_get_peer_pkey(s); |
ff74aeb1 MC |
2377 | /* else anonymous ECDH, so no certificate or pkey. */ |
2378 | ||
aa6bd216 BK |
2379 | /* Cache the agreed upon group in the SSL_SESSION */ |
2380 | s->session->kex_group = curve_id; | |
ff74aeb1 | 2381 | return 1; |
ff74aeb1 MC |
2382 | } |
2383 | ||
38b051a1 | 2384 | MSG_PROCESS_RETURN tls_process_key_exchange(SSL_CONNECTION *s, PACKET *pkt) |
b9908bf9 | 2385 | { |
e1e588ac | 2386 | long alg_k; |
b9908bf9 | 2387 | EVP_PKEY *pkey = NULL; |
fe3066ee MC |
2388 | EVP_MD_CTX *md_ctx = NULL; |
2389 | EVP_PKEY_CTX *pctx = NULL; | |
73999b62 | 2390 | PACKET save_param_start, signature; |
38b051a1 | 2391 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
b9908bf9 | 2392 | |
555cbb32 | 2393 | alg_k = s->s3.tmp.new_cipher->algorithm_mkey; |
b9908bf9 | 2394 | |
73999b62 | 2395 | save_param_start = *pkt; |
8d92c1f8 | 2396 | |
555cbb32 TS |
2397 | EVP_PKEY_free(s->s3.peer_tmp); |
2398 | s->s3.peer_tmp = NULL; | |
d02b48c6 | 2399 | |
7689082b | 2400 | if (alg_k & SSL_PSK) { |
a2c2e000 MC |
2401 | if (!tls_process_ske_psk_preamble(s, pkt)) { |
2402 | /* SSLfatal() already called */ | |
7dc1c647 | 2403 | goto err; |
a2c2e000 | 2404 | } |
7689082b DSH |
2405 | } |
2406 | ||
2407 | /* Nothing else to do for plain PSK or RSAPSK */ | |
2408 | if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) { | |
25c6c10c | 2409 | } else if (alg_k & SSL_kSRP) { |
a2c2e000 MC |
2410 | if (!tls_process_ske_srp(s, pkt, &pkey)) { |
2411 | /* SSLfatal() already called */ | |
0f113f3e | 2412 | goto err; |
a2c2e000 | 2413 | } |
e01a610d | 2414 | } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
a2c2e000 MC |
2415 | if (!tls_process_ske_dhe(s, pkt, &pkey)) { |
2416 | /* SSLfatal() already called */ | |
e01a610d | 2417 | goto err; |
a2c2e000 | 2418 | } |
ff74aeb1 | 2419 | } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
a2c2e000 MC |
2420 | if (!tls_process_ske_ecdhe(s, pkt, &pkey)) { |
2421 | /* SSLfatal() already called */ | |
ff74aeb1 | 2422 | goto err; |
a2c2e000 | 2423 | } |
0f113f3e | 2424 | } else if (alg_k) { |
c48ffbcc | 2425 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
e1e588ac | 2426 | goto err; |
0f113f3e | 2427 | } |
0f113f3e | 2428 | |
0f113f3e MC |
2429 | /* if it was signed, check the signature */ |
2430 | if (pkey != NULL) { | |
32942870 | 2431 | PACKET params; |
be8dba2c | 2432 | const EVP_MD *md = NULL; |
72ceb6a6 DSH |
2433 | unsigned char *tbs; |
2434 | size_t tbslen; | |
2435 | int rv; | |
e1e588ac | 2436 | |
32942870 EK |
2437 | /* |
2438 | * |pkt| now points to the beginning of the signature, so the difference | |
2439 | * equals the length of the parameters. | |
2440 | */ | |
2441 | if (!PACKET_get_sub_packet(&save_param_start, ¶ms, | |
2442 | PACKET_remaining(&save_param_start) - | |
73999b62 | 2443 | PACKET_remaining(pkt))) { |
c48ffbcc | 2444 | SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR); |
e1e588ac | 2445 | goto err; |
32942870 EK |
2446 | } |
2447 | ||
0f113f3e | 2448 | if (SSL_USE_SIGALGS(s)) { |
703bcee0 | 2449 | unsigned int sigalg; |
703bcee0 MC |
2450 | |
2451 | if (!PACKET_get_net_2(pkt, &sigalg)) { | |
c48ffbcc | 2452 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT); |
e1e588ac | 2453 | goto err; |
0f113f3e | 2454 | } |
f63a17d6 MC |
2455 | if (tls12_check_peer_sigalg(s, sigalg, pkey) <=0) { |
2456 | /* SSLfatal() already called */ | |
0f113f3e | 2457 | goto err; |
0f113f3e | 2458 | } |
f365a3e2 | 2459 | } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) { |
97b8db1a TM |
2460 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
2461 | SSL_R_LEGACY_SIGALG_DISALLOWED_OR_UNSUPPORTED); | |
f365a3e2 | 2462 | goto err; |
32942870 | 2463 | } |
0f113f3e | 2464 | |
38b051a1 | 2465 | if (!tls1_lookup_md(sctx, s->s3.tmp.peer_sigalg, &md)) { |
c48ffbcc | 2466 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
7cd1420b | 2467 | SSL_R_NO_SUITABLE_DIGEST_ALGORITHM); |
b2021556 DSH |
2468 | goto err; |
2469 | } | |
44f23cd2 | 2470 | if (SSL_USE_SIGALGS(s)) |
49b26f54 | 2471 | OSSL_TRACE1(TLS, "USING TLSv1.2 HASH %s\n", |
ed576acd | 2472 | md == NULL ? "n/a" : EVP_MD_get0_name(md)); |
f365a3e2 | 2473 | |
73999b62 MC |
2474 | if (!PACKET_get_length_prefixed_2(pkt, &signature) |
2475 | || PACKET_remaining(pkt) != 0) { | |
c48ffbcc | 2476 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
e1e588ac | 2477 | goto err; |
0f113f3e | 2478 | } |
e1e588ac MC |
2479 | |
2480 | md_ctx = EVP_MD_CTX_new(); | |
2481 | if (md_ctx == NULL) { | |
e077455e | 2482 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
e1e588ac | 2483 | goto err; |
0f113f3e | 2484 | } |
e1e588ac | 2485 | |
d8652be0 | 2486 | if (EVP_DigestVerifyInit_ex(md_ctx, &pctx, |
ed576acd | 2487 | md == NULL ? NULL : EVP_MD_get0_name(md), |
38b051a1 | 2488 | sctx->libctx, sctx->propq, pkey, |
d38b6ae9 | 2489 | NULL) <= 0) { |
c48ffbcc | 2490 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
fe3066ee MC |
2491 | goto err; |
2492 | } | |
5554facb | 2493 | if (SSL_USE_PSS(s)) { |
fe3066ee | 2494 | if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 |
91410d40 | 2495 | || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, |
968ae5b3 | 2496 | RSA_PSS_SALTLEN_DIGEST) <= 0) { |
c48ffbcc | 2497 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
fe3066ee MC |
2498 | goto err; |
2499 | } | |
2500 | } | |
72ceb6a6 DSH |
2501 | tbslen = construct_key_exchange_tbs(s, &tbs, PACKET_data(¶ms), |
2502 | PACKET_remaining(¶ms)); | |
2503 | if (tbslen == 0) { | |
f63a17d6 | 2504 | /* SSLfatal() already called */ |
e1e588ac | 2505 | goto err; |
192e4bbb | 2506 | } |
72ceb6a6 DSH |
2507 | |
2508 | rv = EVP_DigestVerify(md_ctx, PACKET_data(&signature), | |
2509 | PACKET_remaining(&signature), tbs, tbslen); | |
2510 | OPENSSL_free(tbs); | |
cfba0675 | 2511 | if (rv <= 0) { |
c48ffbcc | 2512 | SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_SIGNATURE); |
e1e588ac | 2513 | goto err; |
0f113f3e | 2514 | } |
e1e588ac | 2515 | EVP_MD_CTX_free(md_ctx); |
fe3066ee | 2516 | md_ctx = NULL; |
0f113f3e | 2517 | } else { |
7689082b | 2518 | /* aNULL, aSRP or PSK do not need public keys */ |
555cbb32 | 2519 | if (!(s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) |
a230b26e | 2520 | && !(alg_k & SSL_PSK)) { |
0f113f3e | 2521 | /* Might be wrong key type, check it */ |
e1e588ac | 2522 | if (ssl3_check_cert_and_algorithm(s)) { |
c48ffbcc | 2523 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_DATA); |
e1e588ac | 2524 | } |
a2c2e000 | 2525 | /* else this shouldn't happen, SSLfatal() already called */ |
0f113f3e MC |
2526 | goto err; |
2527 | } | |
2528 | /* still data left over */ | |
73999b62 | 2529 | if (PACKET_remaining(pkt) != 0) { |
c48ffbcc | 2530 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_EXTRA_DATA_IN_MESSAGE); |
e1e588ac | 2531 | goto err; |
0f113f3e MC |
2532 | } |
2533 | } | |
e1e588ac | 2534 | |
b9908bf9 | 2535 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e | 2536 | err: |
fe3066ee | 2537 | EVP_MD_CTX_free(md_ctx); |
b9908bf9 | 2538 | return MSG_PROCESS_ERROR; |
0f113f3e | 2539 | } |
d02b48c6 | 2540 | |
38b051a1 TM |
2541 | MSG_PROCESS_RETURN tls_process_certificate_request(SSL_CONNECTION *s, |
2542 | PACKET *pkt) | |
b9908bf9 | 2543 | { |
32f66107 | 2544 | /* Clear certificate validity flags */ |
ee58915c MB |
2545 | if (s->s3.tmp.valid_flags != NULL) |
2546 | memset(s->s3.tmp.valid_flags, 0, s->ssl_pkey_num * sizeof(uint32_t)); | |
2547 | else | |
2548 | s->s3.tmp.valid_flags = OPENSSL_zalloc(s->ssl_pkey_num * sizeof(uint32_t)); | |
2549 | ||
2550 | /* Give up for good if allocation didn't work */ | |
2551 | if (s->s3.tmp.valid_flags == NULL) | |
2552 | return 0; | |
0f113f3e | 2553 | |
38b051a1 | 2554 | if (SSL_CONNECTION_IS_TLS13(s)) { |
32f66107 DSH |
2555 | PACKET reqctx, extensions; |
2556 | RAW_EXTENSION *rawexts = NULL; | |
03f44b97 | 2557 | |
1bf4cb0f MC |
2558 | if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) { |
2559 | /* | |
2560 | * We already sent close_notify. This can only happen in TLSv1.3 | |
2561 | * post-handshake messages. We can't reasonably respond to this, so | |
2562 | * we just ignore it | |
2563 | */ | |
2564 | return MSG_PROCESS_FINISHED_READING; | |
2565 | } | |
2566 | ||
03f44b97 | 2567 | /* Free and zero certificate types: it is not present in TLS 1.3 */ |
555cbb32 TS |
2568 | OPENSSL_free(s->s3.tmp.ctype); |
2569 | s->s3.tmp.ctype = NULL; | |
2570 | s->s3.tmp.ctype_len = 0; | |
9d75dce3 TS |
2571 | OPENSSL_free(s->pha_context); |
2572 | s->pha_context = NULL; | |
39a14059 | 2573 | s->pha_context_len = 0; |
32f66107 | 2574 | |
9d75dce3 TS |
2575 | if (!PACKET_get_length_prefixed_1(pkt, &reqctx) || |
2576 | !PACKET_memdup(&reqctx, &s->pha_context, &s->pha_context_len)) { | |
c48ffbcc | 2577 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 2578 | return MSG_PROCESS_ERROR; |
03f44b97 | 2579 | } |
32f66107 DSH |
2580 | |
2581 | if (!PACKET_get_length_prefixed_2(pkt, &extensions)) { | |
c48ffbcc | 2582 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH); |
f63a17d6 | 2583 | return MSG_PROCESS_ERROR; |
32f66107 DSH |
2584 | } |
2585 | if (!tls_collect_extensions(s, &extensions, | |
fe874d27 | 2586 | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, |
f63a17d6 | 2587 | &rawexts, NULL, 1) |
fe874d27 | 2588 | || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, |
f63a17d6 MC |
2589 | rawexts, NULL, 0, 1)) { |
2590 | /* SSLfatal() already called */ | |
32f66107 | 2591 | OPENSSL_free(rawexts); |
f63a17d6 | 2592 | return MSG_PROCESS_ERROR; |
32f66107 DSH |
2593 | } |
2594 | OPENSSL_free(rawexts); | |
2595 | if (!tls1_process_sigalgs(s)) { | |
c48ffbcc | 2596 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH); |
f63a17d6 | 2597 | return MSG_PROCESS_ERROR; |
32f66107 | 2598 | } |
03f44b97 DSH |
2599 | } else { |
2600 | PACKET ctypes; | |
75c13e78 | 2601 | |
03f44b97 DSH |
2602 | /* get the certificate types */ |
2603 | if (!PACKET_get_length_prefixed_1(pkt, &ctypes)) { | |
c48ffbcc | 2604 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 2605 | return MSG_PROCESS_ERROR; |
03f44b97 DSH |
2606 | } |
2607 | ||
555cbb32 | 2608 | if (!PACKET_memdup(&ctypes, &s->s3.tmp.ctype, &s->s3.tmp.ctype_len)) { |
c48ffbcc | 2609 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 2610 | return MSG_PROCESS_ERROR; |
03f44b97 | 2611 | } |
ac112332 | 2612 | |
32f66107 DSH |
2613 | if (SSL_USE_SIGALGS(s)) { |
2614 | PACKET sigalgs; | |
703bcee0 | 2615 | |
32f66107 | 2616 | if (!PACKET_get_length_prefixed_2(pkt, &sigalgs)) { |
c48ffbcc | 2617 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 2618 | return MSG_PROCESS_ERROR; |
32f66107 | 2619 | } |
ac112332 | 2620 | |
c589c34e BK |
2621 | /* |
2622 | * Despite this being for certificates, preserve compatibility | |
2623 | * with pre-TLS 1.3 and use the regular sigalgs field. | |
2624 | */ | |
2625 | if (!tls1_save_sigalgs(s, &sigalgs, 0)) { | |
f63a17d6 | 2626 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
f63a17d6 MC |
2627 | SSL_R_SIGNATURE_ALGORITHMS_ERROR); |
2628 | return MSG_PROCESS_ERROR; | |
32f66107 DSH |
2629 | } |
2630 | if (!tls1_process_sigalgs(s)) { | |
e077455e | 2631 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); |
f63a17d6 | 2632 | return MSG_PROCESS_ERROR; |
32f66107 | 2633 | } |
0f113f3e | 2634 | } |
0f113f3e | 2635 | |
32f66107 | 2636 | /* get the CA RDNs */ |
f63a17d6 MC |
2637 | if (!parse_ca_names(s, pkt)) { |
2638 | /* SSLfatal() already called */ | |
2639 | return MSG_PROCESS_ERROR; | |
2640 | } | |
03f44b97 DSH |
2641 | } |
2642 | ||
2643 | if (PACKET_remaining(pkt) != 0) { | |
c48ffbcc | 2644 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 2645 | return MSG_PROCESS_ERROR; |
03f44b97 | 2646 | } |
0f113f3e | 2647 | |
0f113f3e | 2648 | /* we should setup a certificate to return.... */ |
555cbb32 | 2649 | s->s3.tmp.cert_req = 1; |
0f113f3e | 2650 | |
e4562014 MC |
2651 | /* |
2652 | * In TLSv1.3 we don't prepare the client certificate yet. We wait until | |
2653 | * after the CertificateVerify message has been received. This is because | |
2654 | * in TLSv1.3 the CertificateRequest arrives before the Certificate message | |
2655 | * but in TLSv1.2 it is the other way around. We want to make sure that | |
8c2bfd25 | 2656 | * SSL_get1_peer_certificate() returns something sensible in |
e4562014 MC |
2657 | * client_cert_cb. |
2658 | */ | |
38b051a1 TM |
2659 | if (SSL_CONNECTION_IS_TLS13(s) |
2660 | && s->post_handshake_auth != SSL_PHA_REQUESTED) | |
e4562014 MC |
2661 | return MSG_PROCESS_CONTINUE_READING; |
2662 | ||
f63a17d6 | 2663 | return MSG_PROCESS_CONTINUE_PROCESSING; |
0f113f3e MC |
2664 | } |
2665 | ||
38b051a1 TM |
2666 | MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL_CONNECTION *s, |
2667 | PACKET *pkt) | |
b9908bf9 | 2668 | { |
b9908bf9 | 2669 | unsigned int ticklen; |
9ac6244b | 2670 | unsigned long ticket_lifetime_hint, age_add = 0; |
ec60ccc1 | 2671 | unsigned int sess_len; |
de1df7e9 | 2672 | RAW_EXTENSION *exts = NULL; |
9b6a8254 | 2673 | PACKET nonce; |
abd86cec | 2674 | EVP_MD *sha256 = NULL; |
38b051a1 | 2675 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
b9908bf9 | 2676 | |
6cf2dbd9 MC |
2677 | PACKET_null_init(&nonce); |
2678 | ||
73999b62 | 2679 | if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint) |
38b051a1 | 2680 | || (SSL_CONNECTION_IS_TLS13(s) |
9b6a8254 | 2681 | && (!PACKET_get_net_4(pkt, &age_add) |
6cf2dbd9 | 2682 | || !PACKET_get_length_prefixed_1(pkt, &nonce))) |
a230b26e | 2683 | || !PACKET_get_net_2(pkt, &ticklen) |
38b051a1 TM |
2684 | || (SSL_CONNECTION_IS_TLS13(s) ? (ticklen == 0 |
2685 | || PACKET_remaining(pkt) < ticklen) | |
2686 | : PACKET_remaining(pkt) != ticklen)) { | |
c48ffbcc | 2687 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 2688 | goto err; |
e711da71 EK |
2689 | } |
2690 | ||
de1df7e9 MC |
2691 | /* |
2692 | * Server is allowed to change its mind (in <=TLSv1.2) and send an empty | |
2693 | * ticket. We already checked this TLSv1.3 case above, so it should never | |
2694 | * be 0 here in that instance | |
2695 | */ | |
e711da71 | 2696 | if (ticklen == 0) |
c9de4a20 | 2697 | return MSG_PROCESS_CONTINUE_READING; |
e711da71 | 2698 | |
150840b9 MC |
2699 | /* |
2700 | * Sessions must be immutable once they go into the session cache. Otherwise | |
2701 | * we can get multi-thread problems. Therefore we don't "update" sessions, | |
2702 | * we replace them with a duplicate. In TLSv1.3 we need to do this every | |
2703 | * time a NewSessionTicket arrives because those messages arrive | |
2704 | * post-handshake and the session may have already gone into the session | |
2705 | * cache. | |
2706 | */ | |
38b051a1 | 2707 | if (SSL_CONNECTION_IS_TLS13(s) || s->session->session_id_length > 0) { |
98ece4ee | 2708 | SSL_SESSION *new_sess; |
1f156321 | 2709 | |
98ece4ee MC |
2710 | /* |
2711 | * We reused an existing session, so we need to replace it with a new | |
2712 | * one | |
2713 | */ | |
5d61491c | 2714 | if ((new_sess = ssl_session_dup(s->session, 0)) == 0) { |
e077455e | 2715 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); |
f63a17d6 | 2716 | goto err; |
5d61491c MC |
2717 | } |
2718 | ||
1f156321 | 2719 | if ((s->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) != 0 |
38b051a1 | 2720 | && !SSL_CONNECTION_IS_TLS13(s)) { |
1f156321 MC |
2721 | /* |
2722 | * In TLSv1.2 and below the arrival of a new tickets signals that | |
2723 | * any old ticket we were using is now out of date, so we remove the | |
2724 | * old session from the cache. We carry on if this fails | |
2725 | */ | |
2726 | SSL_CTX_remove_session(s->session_ctx, s->session); | |
2727 | } | |
2728 | ||
98ece4ee MC |
2729 | SSL_SESSION_free(s->session); |
2730 | s->session = new_sess; | |
2731 | } | |
2732 | ||
f0131dc0 | 2733 | s->session->time = ossl_time_now(); |
25959e04 | 2734 | ssl_session_calculate_timeout(s->session); |
fc24f0bf | 2735 | |
aff8c126 RS |
2736 | OPENSSL_free(s->session->ext.tick); |
2737 | s->session->ext.tick = NULL; | |
2738 | s->session->ext.ticklen = 0; | |
e711da71 | 2739 | |
aff8c126 RS |
2740 | s->session->ext.tick = OPENSSL_malloc(ticklen); |
2741 | if (s->session->ext.tick == NULL) { | |
e077455e | 2742 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
0f113f3e MC |
2743 | goto err; |
2744 | } | |
aff8c126 | 2745 | if (!PACKET_copy_bytes(pkt, s->session->ext.tick, ticklen)) { |
c48ffbcc | 2746 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 2747 | goto err; |
561e12bb | 2748 | } |
e711da71 | 2749 | |
aff8c126 | 2750 | s->session->ext.tick_lifetime_hint = ticket_lifetime_hint; |
fc24f0bf | 2751 | s->session->ext.tick_age_add = age_add; |
aff8c126 | 2752 | s->session->ext.ticklen = ticklen; |
de1df7e9 | 2753 | |
38b051a1 | 2754 | if (SSL_CONNECTION_IS_TLS13(s)) { |
de1df7e9 MC |
2755 | PACKET extpkt; |
2756 | ||
2757 | if (!PACKET_as_length_prefixed_2(pkt, &extpkt) | |
1cde0259 | 2758 | || PACKET_remaining(pkt) != 0) { |
c48ffbcc | 2759 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1cde0259 MC |
2760 | goto err; |
2761 | } | |
2762 | ||
2763 | if (!tls_collect_extensions(s, &extpkt, | |
2764 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET, &exts, | |
2765 | NULL, 1) | |
fe874d27 MC |
2766 | || !tls_parse_all_extensions(s, |
2767 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET, | |
f63a17d6 MC |
2768 | exts, NULL, 0, 1)) { |
2769 | /* SSLfatal() already called */ | |
2770 | goto err; | |
de1df7e9 MC |
2771 | } |
2772 | } | |
2773 | ||
0f113f3e MC |
2774 | /* |
2775 | * There are two ways to detect a resumed ticket session. One is to set | |
2776 | * an appropriate session ID and then the server must return a match in | |
2777 | * ServerHello. This allows the normal client session ID matching to work | |
2778 | * and we know much earlier that the ticket has been accepted. The | |
2779 | * other way is to set zero length session ID when the ticket is | |
2780 | * presented and rely on the handshake to determine session resumption. | |
2781 | * We choose the former approach because this fits in with assumptions | |
abd86cec MC |
2782 | * elsewhere in OpenSSL. The session ID is set to the SHA256 hash of the |
2783 | * ticket. | |
0f113f3e | 2784 | */ |
38b051a1 | 2785 | sha256 = EVP_MD_fetch(sctx->libctx, "SHA2-256", sctx->propq); |
abd86cec | 2786 | if (sha256 == NULL) { |
5a2d0ef3 RL |
2787 | /* Error is already recorded */ |
2788 | SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR); | |
abd86cec MC |
2789 | goto err; |
2790 | } | |
ec60ccc1 | 2791 | /* |
407820c0 | 2792 | * We use sess_len here because EVP_Digest expects an int |
ec60ccc1 MC |
2793 | * but s->session->session_id_length is a size_t |
2794 | */ | |
aff8c126 | 2795 | if (!EVP_Digest(s->session->ext.tick, ticklen, |
ec60ccc1 | 2796 | s->session->session_id, &sess_len, |
abd86cec | 2797 | sha256, NULL)) { |
c48ffbcc | 2798 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
d166ed8c DSH |
2799 | goto err; |
2800 | } | |
abd86cec MC |
2801 | EVP_MD_free(sha256); |
2802 | sha256 = NULL; | |
ec60ccc1 | 2803 | s->session->session_id_length = sess_len; |
4cb00457 | 2804 | s->session->not_resumable = 0; |
de1df7e9 MC |
2805 | |
2806 | /* This is a standalone message in TLSv1.3, so there is no more to read */ | |
38b051a1 | 2807 | if (SSL_CONNECTION_IS_TLS13(s)) { |
4ff1a526 | 2808 | const EVP_MD *md = ssl_handshake_md(s); |
ed576acd | 2809 | int hashleni = EVP_MD_get_size(md); |
4ff1a526 MC |
2810 | size_t hashlen; |
2811 | static const unsigned char nonce_label[] = "resumption"; | |
2812 | ||
2813 | /* Ensure cast to size_t is safe */ | |
2814 | if (!ossl_assert(hashleni >= 0)) { | |
c48ffbcc | 2815 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4ff1a526 MC |
2816 | goto err; |
2817 | } | |
2818 | hashlen = (size_t)hashleni; | |
2819 | ||
2820 | if (!tls13_hkdf_expand(s, md, s->resumption_master_secret, | |
2821 | nonce_label, | |
2822 | sizeof(nonce_label) - 1, | |
6cf2dbd9 MC |
2823 | PACKET_data(&nonce), |
2824 | PACKET_remaining(&nonce), | |
4ff1a526 | 2825 | s->session->master_key, |
0fb2815b | 2826 | hashlen, 1)) { |
4ff1a526 MC |
2827 | /* SSLfatal() already called */ |
2828 | goto err; | |
2829 | } | |
2830 | s->session->master_key_length = hashlen; | |
2831 | ||
33d93417 | 2832 | OPENSSL_free(exts); |
de1df7e9 MC |
2833 | ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); |
2834 | return MSG_PROCESS_FINISHED_READING; | |
2835 | } | |
2836 | ||
b9908bf9 | 2837 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e | 2838 | err: |
abd86cec | 2839 | EVP_MD_free(sha256); |
33d93417 | 2840 | OPENSSL_free(exts); |
b9908bf9 | 2841 | return MSG_PROCESS_ERROR; |
0f113f3e | 2842 | } |
67c8e7f4 | 2843 | |
f63e4288 MC |
2844 | /* |
2845 | * In TLSv1.3 this is called from the extensions code, otherwise it is used to | |
f63a17d6 | 2846 | * parse a separate message. Returns 1 on success or 0 on failure |
f63e4288 | 2847 | */ |
38b051a1 | 2848 | int tls_process_cert_status_body(SSL_CONNECTION *s, PACKET *pkt) |
b9908bf9 | 2849 | { |
8b0e934a | 2850 | size_t resplen; |
b9908bf9 | 2851 | unsigned int type; |
b9908bf9 | 2852 | |
73999b62 | 2853 | if (!PACKET_get_1(pkt, &type) |
a230b26e | 2854 | || type != TLSEXT_STATUSTYPE_ocsp) { |
c48ffbcc | 2855 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_UNSUPPORTED_STATUS_TYPE); |
f63e4288 | 2856 | return 0; |
0f113f3e | 2857 | } |
56a26ce3 MC |
2858 | if (!PACKET_get_net_3_len(pkt, &resplen) |
2859 | || PACKET_remaining(pkt) != resplen) { | |
c48ffbcc | 2860 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63e4288 | 2861 | return 0; |
0f113f3e | 2862 | } |
8cbfcc70 RS |
2863 | s->ext.ocsp.resp = OPENSSL_malloc(resplen); |
2864 | if (s->ext.ocsp.resp == NULL) { | |
39a14059 | 2865 | s->ext.ocsp.resp_len = 0; |
e077455e | 2866 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
f63e4288 | 2867 | return 0; |
0f113f3e | 2868 | } |
39a14059 | 2869 | s->ext.ocsp.resp_len = resplen; |
8cbfcc70 | 2870 | if (!PACKET_copy_bytes(pkt, s->ext.ocsp.resp, resplen)) { |
c48ffbcc | 2871 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63e4288 | 2872 | return 0; |
ac63710a | 2873 | } |
f63e4288 MC |
2874 | |
2875 | return 1; | |
2876 | } | |
2faa1b48 | 2877 | |
f63e4288 | 2878 | |
38b051a1 | 2879 | MSG_PROCESS_RETURN tls_process_cert_status(SSL_CONNECTION *s, PACKET *pkt) |
f63e4288 | 2880 | { |
f63a17d6 MC |
2881 | if (!tls_process_cert_status_body(s, pkt)) { |
2882 | /* SSLfatal() already called */ | |
f63e4288 MC |
2883 | return MSG_PROCESS_ERROR; |
2884 | } | |
2885 | ||
b9908bf9 | 2886 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e | 2887 | } |
d02b48c6 | 2888 | |
7776a36c MC |
2889 | /* |
2890 | * Perform miscellaneous checks and processing after we have received the | |
2891 | * server's initial flight. In TLS1.3 this is after the Server Finished message. | |
6530c490 MC |
2892 | * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0 |
2893 | * on failure. | |
7776a36c | 2894 | */ |
38b051a1 | 2895 | int tls_process_initial_server_flight(SSL_CONNECTION *s) |
b9908bf9 | 2896 | { |
38b051a1 TM |
2897 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
2898 | ||
a455d0f6 MC |
2899 | /* |
2900 | * at this point we check that we have the required stuff from | |
2901 | * the server | |
2902 | */ | |
2903 | if (!ssl3_check_cert_and_algorithm(s)) { | |
f63a17d6 | 2904 | /* SSLfatal() already called */ |
7776a36c | 2905 | return 0; |
a455d0f6 MC |
2906 | } |
2907 | ||
bb1aaab4 | 2908 | /* |
aff8c126 RS |
2909 | * Call the ocsp status callback if needed. The |ext.ocsp.resp| and |
2910 | * |ext.ocsp.resp_len| values will be set if we actually received a status | |
bb1aaab4 MC |
2911 | * message, or NULL and -1 otherwise |
2912 | */ | |
aff8c126 | 2913 | if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing |
38b051a1 TM |
2914 | && sctx->ext.status_cb != NULL) { |
2915 | int ret = sctx->ext.status_cb(SSL_CONNECTION_GET_SSL(s), | |
2916 | sctx->ext.status_arg); | |
aff8c126 | 2917 | |
bb1aaab4 | 2918 | if (ret == 0) { |
f63a17d6 | 2919 | SSLfatal(s, SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE, |
f63a17d6 | 2920 | SSL_R_INVALID_STATUS_RESPONSE); |
7776a36c | 2921 | return 0; |
bb1aaab4 MC |
2922 | } |
2923 | if (ret < 0) { | |
c0f4400c DB |
2924 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
2925 | SSL_R_OCSP_CALLBACK_FAILURE); | |
7776a36c | 2926 | return 0; |
bb1aaab4 MC |
2927 | } |
2928 | } | |
ed29e82a RP |
2929 | #ifndef OPENSSL_NO_CT |
2930 | if (s->ct_validation_callback != NULL) { | |
43341433 VD |
2931 | /* Note we validate the SCTs whether or not we abort on error */ |
2932 | if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) { | |
f63a17d6 | 2933 | /* SSLfatal() already called */ |
7776a36c | 2934 | return 0; |
ed29e82a RP |
2935 | } |
2936 | } | |
2937 | #endif | |
2938 | ||
7776a36c MC |
2939 | return 1; |
2940 | } | |
2941 | ||
38b051a1 | 2942 | MSG_PROCESS_RETURN tls_process_server_done(SSL_CONNECTION *s, PACKET *pkt) |
7776a36c | 2943 | { |
7776a36c MC |
2944 | if (PACKET_remaining(pkt) > 0) { |
2945 | /* should contain no data */ | |
c48ffbcc | 2946 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
f63a17d6 | 2947 | return MSG_PROCESS_ERROR; |
7776a36c MC |
2948 | } |
2949 | #ifndef OPENSSL_NO_SRP | |
555cbb32 | 2950 | if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) { |
76cb077f | 2951 | if (ssl_srp_calc_a_param_intern(s) <= 0) { |
c48ffbcc | 2952 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SRP_A_CALC); |
f63a17d6 | 2953 | return MSG_PROCESS_ERROR; |
7776a36c MC |
2954 | } |
2955 | } | |
2956 | #endif | |
2957 | ||
f63a17d6 MC |
2958 | if (!tls_process_initial_server_flight(s)) { |
2959 | /* SSLfatal() already called */ | |
2960 | return MSG_PROCESS_ERROR; | |
2961 | } | |
7776a36c | 2962 | |
bd79bcb4 | 2963 | return MSG_PROCESS_FINISHED_READING; |
0f113f3e | 2964 | } |
176f31dd | 2965 | |
38b051a1 | 2966 | static int tls_construct_cke_psk_preamble(SSL_CONNECTION *s, WPACKET *pkt) |
0f113f3e | 2967 | { |
7689082b | 2968 | #ifndef OPENSSL_NO_PSK |
13c0ec4a MC |
2969 | int ret = 0; |
2970 | /* | |
2971 | * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a | |
2972 | * \0-terminated identity. The last byte is for us for simulating | |
2973 | * strnlen. | |
2974 | */ | |
2975 | char identity[PSK_MAX_IDENTITY_LEN + 1]; | |
2976 | size_t identitylen = 0; | |
2977 | unsigned char psk[PSK_MAX_PSK_LEN]; | |
2978 | unsigned char *tmppsk = NULL; | |
2979 | char *tmpidentity = NULL; | |
2980 | size_t psklen = 0; | |
2981 | ||
2982 | if (s->psk_client_callback == NULL) { | |
c48ffbcc | 2983 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_CLIENT_CB); |
13c0ec4a MC |
2984 | goto err; |
2985 | } | |
d02b48c6 | 2986 | |
13c0ec4a | 2987 | memset(identity, 0, sizeof(identity)); |
d02b48c6 | 2988 | |
38b051a1 TM |
2989 | psklen = s->psk_client_callback(SSL_CONNECTION_GET_SSL(s), |
2990 | s->session->psk_identity_hint, | |
13c0ec4a MC |
2991 | identity, sizeof(identity) - 1, |
2992 | psk, sizeof(psk)); | |
7689082b | 2993 | |
13c0ec4a | 2994 | if (psklen > PSK_MAX_PSK_LEN) { |
c48ffbcc | 2995 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR); |
3de7f014 | 2996 | psklen = PSK_MAX_PSK_LEN; /* Avoid overrunning the array on cleanse */ |
13c0ec4a MC |
2997 | goto err; |
2998 | } else if (psklen == 0) { | |
c48ffbcc | 2999 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_PSK_IDENTITY_NOT_FOUND); |
13c0ec4a MC |
3000 | goto err; |
3001 | } | |
7689082b | 3002 | |
13c0ec4a MC |
3003 | identitylen = strlen(identity); |
3004 | if (identitylen > PSK_MAX_IDENTITY_LEN) { | |
c48ffbcc | 3005 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
3006 | goto err; |
3007 | } | |
7689082b | 3008 | |
13c0ec4a MC |
3009 | tmppsk = OPENSSL_memdup(psk, psklen); |
3010 | tmpidentity = OPENSSL_strdup(identity); | |
3011 | if (tmppsk == NULL || tmpidentity == NULL) { | |
e077455e | 3012 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
13c0ec4a MC |
3013 | goto err; |
3014 | } | |
7689082b | 3015 | |
555cbb32 TS |
3016 | OPENSSL_free(s->s3.tmp.psk); |
3017 | s->s3.tmp.psk = tmppsk; | |
3018 | s->s3.tmp.psklen = psklen; | |
13c0ec4a MC |
3019 | tmppsk = NULL; |
3020 | OPENSSL_free(s->session->psk_identity); | |
3021 | s->session->psk_identity = tmpidentity; | |
3022 | tmpidentity = NULL; | |
f1ec23c0 | 3023 | |
b2b3024e | 3024 | if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) { |
c48ffbcc | 3025 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f1ec23c0 MC |
3026 | goto err; |
3027 | } | |
7689082b | 3028 | |
13c0ec4a | 3029 | ret = 1; |
0bce0b02 | 3030 | |
13c0ec4a MC |
3031 | err: |
3032 | OPENSSL_cleanse(psk, psklen); | |
3033 | OPENSSL_cleanse(identity, sizeof(identity)); | |
3034 | OPENSSL_clear_free(tmppsk, psklen); | |
3035 | OPENSSL_clear_free(tmpidentity, identitylen); | |
d02b48c6 | 3036 | |
13c0ec4a MC |
3037 | return ret; |
3038 | #else | |
c48ffbcc | 3039 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
13c0ec4a | 3040 | return 0; |
b9908bf9 | 3041 | #endif |
13c0ec4a | 3042 | } |
b9908bf9 | 3043 | |
38b051a1 | 3044 | static int tls_construct_cke_rsa(SSL_CONNECTION *s, WPACKET *pkt) |
13c0ec4a | 3045 | { |
f1ec23c0 | 3046 | unsigned char *encdata = NULL; |
13c0ec4a MC |
3047 | EVP_PKEY *pkey = NULL; |
3048 | EVP_PKEY_CTX *pctx = NULL; | |
3049 | size_t enclen; | |
3050 | unsigned char *pms = NULL; | |
3051 | size_t pmslen = 0; | |
38b051a1 | 3052 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
b9908bf9 | 3053 | |
3c95ef22 | 3054 | if (!received_server_cert(s)) { |
13c0ec4a MC |
3055 | /* |
3056 | * We should always have a server certificate with SSL_kRSA. | |
3057 | */ | |
c48ffbcc | 3058 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
3059 | return 0; |
3060 | } | |
0f113f3e | 3061 | |
3c95ef22 TS |
3062 | if ((pkey = tls_get_peer_pkey(s)) == NULL) { |
3063 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
3064 | return 0; | |
3065 | } | |
3066 | ||
d7e498ac | 3067 | if (!EVP_PKEY_is_a(pkey, "RSA")) { |
c48ffbcc | 3068 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
3069 | return 0; |
3070 | } | |
0f113f3e | 3071 | |
13c0ec4a MC |
3072 | pmslen = SSL_MAX_MASTER_KEY_LENGTH; |
3073 | pms = OPENSSL_malloc(pmslen); | |
3074 | if (pms == NULL) { | |
e077455e | 3075 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
13c0ec4a MC |
3076 | return 0; |
3077 | } | |
0bce0b02 | 3078 | |
13c0ec4a MC |
3079 | pms[0] = s->client_version >> 8; |
3080 | pms[1] = s->client_version & 0xff; | |
38b051a1 | 3081 | if (RAND_bytes_ex(sctx->libctx, pms + 2, pmslen - 2, 0) <= 0) { |
e077455e | 3082 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_RAND_LIB); |
13c0ec4a MC |
3083 | goto err; |
3084 | } | |
0f113f3e | 3085 | |
13c0ec4a | 3086 | /* Fix buf for TLS and beyond */ |
f1ec23c0 | 3087 | if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) { |
c48ffbcc | 3088 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f1ec23c0 MC |
3089 | goto err; |
3090 | } | |
3aceb9ec | 3091 | |
38b051a1 | 3092 | pctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pkey, sctx->propq); |
13c0ec4a MC |
3093 | if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0 |
3094 | || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) { | |
c48ffbcc | 3095 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
13c0ec4a MC |
3096 | goto err; |
3097 | } | |
f1ec23c0 MC |
3098 | if (!WPACKET_allocate_bytes(pkt, enclen, &encdata) |
3099 | || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) { | |
c48ffbcc | 3100 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_RSA_ENCRYPT); |
13c0ec4a MC |
3101 | goto err; |
3102 | } | |
13c0ec4a MC |
3103 | EVP_PKEY_CTX_free(pctx); |
3104 | pctx = NULL; | |
0f113f3e | 3105 | |
13c0ec4a | 3106 | /* Fix buf for TLS and beyond */ |
f1ec23c0 | 3107 | if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) { |
c48ffbcc | 3108 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f1ec23c0 | 3109 | goto err; |
b9908bf9 | 3110 | } |
13c0ec4a | 3111 | |
2faa1b48 | 3112 | /* Log the premaster secret, if logging is enabled. */ |
a2c2e000 MC |
3113 | if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) { |
3114 | /* SSLfatal() already called */ | |
2faa1b48 | 3115 | goto err; |
a2c2e000 | 3116 | } |
2faa1b48 | 3117 | |
555cbb32 TS |
3118 | s->s3.tmp.pms = pms; |
3119 | s->s3.tmp.pmslen = pmslen; | |
26fb4b03 | 3120 | |
13c0ec4a MC |
3121 | return 1; |
3122 | err: | |
3123 | OPENSSL_clear_free(pms, pmslen); | |
3124 | EVP_PKEY_CTX_free(pctx); | |
3125 | ||
3126 | return 0; | |
13c0ec4a MC |
3127 | } |
3128 | ||
38b051a1 | 3129 | static int tls_construct_cke_dhe(SSL_CONNECTION *s, WPACKET *pkt) |
a8c1c704 | 3130 | { |
a8c1c704 | 3131 | EVP_PKEY *ckey = NULL, *skey = NULL; |
f1ec23c0 | 3132 | unsigned char *keybytes = NULL; |
807b0a1d | 3133 | int prime_len; |
cb5a427a MC |
3134 | unsigned char *encoded_pub = NULL; |
3135 | size_t encoded_pub_len, pad_len; | |
3136 | int ret = 0; | |
a8c1c704 | 3137 | |
555cbb32 | 3138 | skey = s->s3.peer_tmp; |
a2c2e000 | 3139 | if (skey == NULL) { |
c48ffbcc | 3140 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f1ec23c0 | 3141 | goto err; |
a2c2e000 | 3142 | } |
f1ec23c0 | 3143 | |
0f00ed77 | 3144 | ckey = ssl_generate_pkey(s, skey); |
a2c2e000 | 3145 | if (ckey == NULL) { |
c48ffbcc | 3146 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b599ce3b | 3147 | goto err; |
a2c2e000 | 3148 | } |
b599ce3b | 3149 | |
a2c2e000 MC |
3150 | if (ssl_derive(s, ckey, skey, 0) == 0) { |
3151 | /* SSLfatal() already called */ | |
f1ec23c0 | 3152 | goto err; |
a2c2e000 | 3153 | } |
a8c1c704 MC |
3154 | |
3155 | /* send off the data */ | |
cb5a427a MC |
3156 | |
3157 | /* Generate encoding of server key */ | |
3158 | encoded_pub_len = EVP_PKEY_get1_encoded_public_key(ckey, &encoded_pub); | |
3159 | if (encoded_pub_len == 0) { | |
3160 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
10481d33 | 3161 | EVP_PKEY_free(ckey); |
cb5a427a MC |
3162 | return EXT_RETURN_FAIL; |
3163 | } | |
3164 | ||
807b0a1d FM |
3165 | /* |
3166 | * For interoperability with some versions of the Microsoft TLS | |
3167 | * stack, we need to zero pad the DHE pub key to the same length | |
cb5a427a | 3168 | * as the prime. |
807b0a1d | 3169 | */ |
ed576acd | 3170 | prime_len = EVP_PKEY_get_size(ckey); |
cb5a427a MC |
3171 | pad_len = prime_len - encoded_pub_len; |
3172 | if (pad_len > 0) { | |
3173 | if (!WPACKET_sub_allocate_bytes_u16(pkt, pad_len, &keybytes)) { | |
3174 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
3175 | goto err; | |
3176 | } | |
3177 | memset(keybytes, 0, pad_len); | |
3178 | } | |
3179 | ||
3180 | if (!WPACKET_sub_memcpy_u16(pkt, encoded_pub, encoded_pub_len)) { | |
c48ffbcc | 3181 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f1ec23c0 | 3182 | goto err; |
a2c2e000 | 3183 | } |
f1ec23c0 | 3184 | |
cb5a427a | 3185 | ret = 1; |
f1ec23c0 | 3186 | err: |
cb5a427a | 3187 | OPENSSL_free(encoded_pub); |
f1ec23c0 | 3188 | EVP_PKEY_free(ckey); |
cb5a427a | 3189 | return ret; |
a8c1c704 MC |
3190 | } |
3191 | ||
38b051a1 | 3192 | static int tls_construct_cke_ecdhe(SSL_CONNECTION *s, WPACKET *pkt) |
67ad5aab | 3193 | { |
67ad5aab | 3194 | unsigned char *encodedPoint = NULL; |
348240c6 | 3195 | size_t encoded_pt_len = 0; |
67ad5aab | 3196 | EVP_PKEY *ckey = NULL, *skey = NULL; |
f1ec23c0 | 3197 | int ret = 0; |
67ad5aab | 3198 | |
555cbb32 | 3199 | skey = s->s3.peer_tmp; |
ec24630a | 3200 | if (skey == NULL) { |
c48ffbcc | 3201 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ad5aab MC |
3202 | return 0; |
3203 | } | |
3204 | ||
0f00ed77 | 3205 | ckey = ssl_generate_pkey(s, skey); |
b599ce3b | 3206 | if (ckey == NULL) { |
e077455e | 3207 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); |
b599ce3b MC |
3208 | goto err; |
3209 | } | |
67ad5aab | 3210 | |
0f1e51ea | 3211 | if (ssl_derive(s, ckey, skey, 0) == 0) { |
a2c2e000 | 3212 | /* SSLfatal() already called */ |
67ad5aab MC |
3213 | goto err; |
3214 | } | |
3215 | ||
3216 | /* Generate encoding of client key */ | |
5ac8fb58 | 3217 | encoded_pt_len = EVP_PKEY_get1_encoded_public_key(ckey, &encodedPoint); |
67ad5aab MC |
3218 | |
3219 | if (encoded_pt_len == 0) { | |
c48ffbcc | 3220 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB); |
67ad5aab MC |
3221 | goto err; |
3222 | } | |
3223 | ||
b2b3024e | 3224 | if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) { |
c48ffbcc | 3225 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f1ec23c0 MC |
3226 | goto err; |
3227 | } | |
67ad5aab | 3228 | |
f1ec23c0 | 3229 | ret = 1; |
67ad5aab | 3230 | err: |
f1ec23c0 | 3231 | OPENSSL_free(encodedPoint); |
67ad5aab | 3232 | EVP_PKEY_free(ckey); |
f1ec23c0 | 3233 | return ret; |
67ad5aab MC |
3234 | } |
3235 | ||
38b051a1 | 3236 | static int tls_construct_cke_gost(SSL_CONNECTION *s, WPACKET *pkt) |
e00e0b3d MC |
3237 | { |
3238 | #ifndef OPENSSL_NO_GOST | |
3239 | /* GOST key exchange message creation */ | |
3240 | EVP_PKEY_CTX *pkey_ctx = NULL; | |
3c95ef22 | 3241 | EVP_PKEY *pkey = NULL; |
e00e0b3d MC |
3242 | size_t msglen; |
3243 | unsigned int md_len; | |
3244 | unsigned char shared_ukm[32], tmp[256]; | |
3245 | EVP_MD_CTX *ukm_hash = NULL; | |
3246 | int dgst_nid = NID_id_GostR3411_94; | |
3247 | unsigned char *pms = NULL; | |
3248 | size_t pmslen = 0; | |
38b051a1 | 3249 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
e00e0b3d | 3250 | |
555cbb32 | 3251 | if ((s->s3.tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0) |
e00e0b3d MC |
3252 | dgst_nid = NID_id_GostR3411_2012_256; |
3253 | ||
3254 | /* | |
1ee4b98e | 3255 | * Get server certificate PKEY and create ctx from it |
e00e0b3d | 3256 | */ |
3c95ef22 | 3257 | if ((pkey = tls_get_peer_pkey(s)) == NULL) { |
c48ffbcc RL |
3258 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
3259 | SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); | |
e00e0b3d MC |
3260 | return 0; |
3261 | } | |
3262 | ||
38b051a1 | 3263 | pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, |
3c95ef22 | 3264 | pkey, |
38b051a1 | 3265 | sctx->propq); |
e00e0b3d | 3266 | if (pkey_ctx == NULL) { |
e077455e | 3267 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
e00e0b3d MC |
3268 | return 0; |
3269 | } | |
3270 | /* | |
3271 | * If we have send a certificate, and certificate key | |
3272 | * parameters match those of server certificate, use | |
3273 | * certificate key for key exchange | |
3274 | */ | |
3275 | ||
3276 | /* Otherwise, generate ephemeral key pair */ | |
3277 | pmslen = 32; | |
3278 | pms = OPENSSL_malloc(pmslen); | |
3279 | if (pms == NULL) { | |
e077455e | 3280 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
2f3930bc | 3281 | goto err; |
e00e0b3d MC |
3282 | } |
3283 | ||
3284 | if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0 | |
348240c6 | 3285 | /* Generate session key |
348240c6 | 3286 | */ |
38b051a1 | 3287 | || RAND_bytes_ex(sctx->libctx, pms, pmslen, 0) <= 0) { |
c48ffbcc | 3288 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
e00e0b3d MC |
3289 | goto err; |
3290 | }; | |
e00e0b3d MC |
3291 | /* |
3292 | * Compute shared IV and store it in algorithm-specific context | |
3293 | * data | |
3294 | */ | |
3295 | ukm_hash = EVP_MD_CTX_new(); | |
3296 | if (ukm_hash == NULL | |
a230b26e | 3297 | || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0 |
555cbb32 | 3298 | || EVP_DigestUpdate(ukm_hash, s->s3.client_random, |
a230b26e | 3299 | SSL3_RANDOM_SIZE) <= 0 |
555cbb32 | 3300 | || EVP_DigestUpdate(ukm_hash, s->s3.server_random, |
a230b26e EK |
3301 | SSL3_RANDOM_SIZE) <= 0 |
3302 | || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) { | |
c48ffbcc | 3303 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
e00e0b3d MC |
3304 | goto err; |
3305 | } | |
3306 | EVP_MD_CTX_free(ukm_hash); | |
3307 | ukm_hash = NULL; | |
3308 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, | |
7b1264ba | 3309 | EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) <= 0) { |
c48ffbcc | 3310 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); |
e00e0b3d MC |
3311 | goto err; |
3312 | } | |
3313 | /* Make GOST keytransport blob message */ | |
3314 | /* | |
3315 | * Encapsulate it into sequence | |
3316 | */ | |
e00e0b3d MC |
3317 | msglen = 255; |
3318 | if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) { | |
c48ffbcc | 3319 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); |
e00e0b3d MC |
3320 | goto err; |
3321 | } | |
f1ec23c0 | 3322 | |
08029dfa MC |
3323 | if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED) |
3324 | || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81)) | |
b2b3024e | 3325 | || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) { |
c48ffbcc | 3326 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f1ec23c0 | 3327 | goto err; |
e00e0b3d | 3328 | } |
f1ec23c0 | 3329 | |
e00e0b3d | 3330 | EVP_PKEY_CTX_free(pkey_ctx); |
555cbb32 TS |
3331 | s->s3.tmp.pms = pms; |
3332 | s->s3.tmp.pmslen = pmslen; | |
e00e0b3d MC |
3333 | |
3334 | return 1; | |
3335 | err: | |
3336 | EVP_PKEY_CTX_free(pkey_ctx); | |
3337 | OPENSSL_clear_free(pms, pmslen); | |
3338 | EVP_MD_CTX_free(ukm_hash); | |
3339 | return 0; | |
3340 | #else | |
c48ffbcc | 3341 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
e00e0b3d MC |
3342 | return 0; |
3343 | #endif | |
3344 | } | |
3345 | ||
5a5530a2 | 3346 | #ifndef OPENSSL_NO_GOST |
38b051a1 | 3347 | int ossl_gost18_cke_cipher_nid(const SSL_CONNECTION *s) |
5a5530a2 DB |
3348 | { |
3349 | if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_MAGMA) != 0) | |
3350 | return NID_magma_ctr; | |
3351 | else if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_KUZNYECHIK) != 0) | |
3352 | return NID_kuznyechik_ctr; | |
3353 | ||
3354 | return NID_undef; | |
3355 | } | |
3356 | ||
38b051a1 | 3357 | int ossl_gost_ukm(const SSL_CONNECTION *s, unsigned char *dgst_buf) |
5a5530a2 DB |
3358 | { |
3359 | EVP_MD_CTX * hash = NULL; | |
3360 | unsigned int md_len; | |
38b051a1 TM |
3361 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
3362 | const EVP_MD *md = ssl_evp_md_fetch(sctx->libctx, NID_id_GostR3411_2012_256, | |
3363 | sctx->propq); | |
5a5530a2 DB |
3364 | |
3365 | if (md == NULL) | |
3366 | return 0; | |
3367 | ||
3368 | if ((hash = EVP_MD_CTX_new()) == NULL | |
3369 | || EVP_DigestInit(hash, md) <= 0 | |
3370 | || EVP_DigestUpdate(hash, s->s3.client_random, SSL3_RANDOM_SIZE) <= 0 | |
3371 | || EVP_DigestUpdate(hash, s->s3.server_random, SSL3_RANDOM_SIZE) <= 0 | |
3372 | || EVP_DigestFinal_ex(hash, dgst_buf, &md_len) <= 0) { | |
3373 | EVP_MD_CTX_free(hash); | |
3374 | ssl_evp_md_free(md); | |
3375 | return 0; | |
3376 | } | |
3377 | ||
3378 | EVP_MD_CTX_free(hash); | |
3379 | ssl_evp_md_free(md); | |
3380 | return 1; | |
3381 | } | |
3382 | #endif | |
3383 | ||
38b051a1 | 3384 | static int tls_construct_cke_gost18(SSL_CONNECTION *s, WPACKET *pkt) |
5a5530a2 DB |
3385 | { |
3386 | #ifndef OPENSSL_NO_GOST | |
3387 | /* GOST 2018 key exchange message creation */ | |
2b5e8999 DB |
3388 | unsigned char rnd_dgst[32]; |
3389 | unsigned char *encdata = NULL; | |
5a5530a2 | 3390 | EVP_PKEY_CTX *pkey_ctx = NULL; |
3c95ef22 | 3391 | EVP_PKEY *pkey; |
5a5530a2 DB |
3392 | unsigned char *pms = NULL; |
3393 | size_t pmslen = 0; | |
3394 | size_t msglen; | |
6dd4b77a | 3395 | int cipher_nid = ossl_gost18_cke_cipher_nid(s); |
38b051a1 | 3396 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
5a5530a2 DB |
3397 | |
3398 | if (cipher_nid == NID_undef) { | |
c48ffbcc | 3399 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
5a5530a2 DB |
3400 | return 0; |
3401 | } | |
3402 | ||
6dd4b77a | 3403 | if (ossl_gost_ukm(s, rnd_dgst) <= 0) { |
c48ffbcc | 3404 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
5a5530a2 DB |
3405 | goto err; |
3406 | } | |
3407 | ||
3408 | /* Pre-master secret - random bytes */ | |
3409 | pmslen = 32; | |
3410 | pms = OPENSSL_malloc(pmslen); | |
3411 | if (pms == NULL) { | |
e077455e | 3412 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
5a5530a2 DB |
3413 | goto err; |
3414 | } | |
3415 | ||
38b051a1 | 3416 | if (RAND_bytes_ex(sctx->libctx, pms, pmslen, 0) <= 0) { |
c48ffbcc | 3417 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
5a5530a2 DB |
3418 | goto err; |
3419 | } | |
3420 | ||
3421 | /* Get server certificate PKEY and create ctx from it */ | |
3c95ef22 | 3422 | if ((pkey = tls_get_peer_pkey(s)) == NULL) { |
c48ffbcc RL |
3423 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
3424 | SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); | |
1b87116a | 3425 | goto err; |
5a5530a2 DB |
3426 | } |
3427 | ||
38b051a1 | 3428 | pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, |
3c95ef22 | 3429 | pkey, |
38b051a1 | 3430 | sctx->propq); |
5a5530a2 | 3431 | if (pkey_ctx == NULL) { |
e077455e | 3432 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
1b87116a | 3433 | goto err; |
5a5530a2 DB |
3434 | } |
3435 | ||
1287dabd | 3436 | if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0) { |
c48ffbcc | 3437 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
5a5530a2 DB |
3438 | goto err; |
3439 | }; | |
3440 | ||
3441 | /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code */ | |
3442 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, | |
7b1264ba | 3443 | EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst) <= 0) { |
c48ffbcc | 3444 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); |
5a5530a2 DB |
3445 | goto err; |
3446 | } | |
3447 | ||
3448 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, | |
7b1264ba | 3449 | EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL) <= 0) { |
c48ffbcc | 3450 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); |
5a5530a2 DB |
3451 | goto err; |
3452 | } | |
3453 | ||
2b5e8999 DB |
3454 | if (EVP_PKEY_encrypt(pkey_ctx, NULL, &msglen, pms, pmslen) <= 0) { |
3455 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); | |
5a5530a2 DB |
3456 | goto err; |
3457 | } | |
3458 | ||
2b5e8999 DB |
3459 | if (!WPACKET_allocate_bytes(pkt, msglen, &encdata) |
3460 | || EVP_PKEY_encrypt(pkey_ctx, encdata, &msglen, pms, pmslen) <= 0) { | |
3461 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); | |
5a5530a2 DB |
3462 | goto err; |
3463 | } | |
3464 | ||
3465 | EVP_PKEY_CTX_free(pkey_ctx); | |
2b5e8999 | 3466 | pkey_ctx = NULL; |
5a5530a2 DB |
3467 | s->s3.tmp.pms = pms; |
3468 | s->s3.tmp.pmslen = pmslen; | |
3469 | ||
3470 | return 1; | |
3471 | err: | |
3472 | EVP_PKEY_CTX_free(pkey_ctx); | |
3473 | OPENSSL_clear_free(pms, pmslen); | |
3474 | return 0; | |
3475 | #else | |
c48ffbcc | 3476 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
5a5530a2 DB |
3477 | return 0; |
3478 | #endif | |
3479 | } | |
3480 | ||
38b051a1 | 3481 | static int tls_construct_cke_srp(SSL_CONNECTION *s, WPACKET *pkt) |
840a2bf8 | 3482 | { |
8b9546c7 | 3483 | #ifndef OPENSSL_NO_SRP |
f1ec23c0 MC |
3484 | unsigned char *abytes = NULL; |
3485 | ||
3486 | if (s->srp_ctx.A == NULL | |
b2b3024e MC |
3487 | || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A), |
3488 | &abytes)) { | |
c48ffbcc | 3489 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
840a2bf8 MC |
3490 | return 0; |
3491 | } | |
f1ec23c0 MC |
3492 | BN_bn2bin(s->srp_ctx.A, abytes); |
3493 | ||
840a2bf8 MC |
3494 | OPENSSL_free(s->session->srp_username); |
3495 | s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login); | |
3496 | if (s->session->srp_username == NULL) { | |
e077455e | 3497 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
840a2bf8 MC |
3498 | return 0; |
3499 | } | |
3500 | ||
3501 | return 1; | |
3502 | #else | |
c48ffbcc | 3503 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
840a2bf8 MC |
3504 | return 0; |
3505 | #endif | |
3506 | } | |
3507 | ||
67ec6d2b MC |
3508 | CON_FUNC_RETURN tls_construct_client_key_exchange(SSL_CONNECTION *s, |
3509 | WPACKET *pkt) | |
13c0ec4a | 3510 | { |
13c0ec4a | 3511 | unsigned long alg_k; |
13c0ec4a | 3512 | |
555cbb32 | 3513 | alg_k = s->s3.tmp.new_cipher->algorithm_mkey; |
13c0ec4a | 3514 | |
a2c2e000 MC |
3515 | /* |
3516 | * All of the construct functions below call SSLfatal() if necessary so | |
3517 | * no need to do so here. | |
3518 | */ | |
13c0ec4a | 3519 | if ((alg_k & SSL_PSK) |
a2c2e000 | 3520 | && !tls_construct_cke_psk_preamble(s, pkt)) |
13c0ec4a MC |
3521 | goto err; |
3522 | ||
f1ec23c0 | 3523 | if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) { |
a2c2e000 | 3524 | if (!tls_construct_cke_rsa(s, pkt)) |
13c0ec4a | 3525 | goto err; |
a8c1c704 | 3526 | } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
a2c2e000 | 3527 | if (!tls_construct_cke_dhe(s, pkt)) |
b9908bf9 | 3528 | goto err; |
67ad5aab | 3529 | } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
a2c2e000 | 3530 | if (!tls_construct_cke_ecdhe(s, pkt)) |
ce0c1f2b | 3531 | goto err; |
e00e0b3d | 3532 | } else if (alg_k & SSL_kGOST) { |
a2c2e000 | 3533 | if (!tls_construct_cke_gost(s, pkt)) |
a71edf3b | 3534 | goto err; |
5a5530a2 DB |
3535 | } else if (alg_k & SSL_kGOST18) { |
3536 | if (!tls_construct_cke_gost18(s, pkt)) | |
3537 | goto err; | |
840a2bf8 | 3538 | } else if (alg_k & SSL_kSRP) { |
a2c2e000 | 3539 | if (!tls_construct_cke_srp(s, pkt)) |
69f68237 | 3540 | goto err; |
4a424545 | 3541 | } else if (!(alg_k & SSL_kPSK)) { |
c48ffbcc | 3542 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b9908bf9 MC |
3543 | goto err; |
3544 | } | |
3545 | ||
67ec6d2b | 3546 | return CON_FUNC_SUCCESS; |
0f113f3e | 3547 | err: |
555cbb32 TS |
3548 | OPENSSL_clear_free(s->s3.tmp.pms, s->s3.tmp.pmslen); |
3549 | s->s3.tmp.pms = NULL; | |
39a14059 | 3550 | s->s3.tmp.pmslen = 0; |
7689082b | 3551 | #ifndef OPENSSL_NO_PSK |
555cbb32 TS |
3552 | OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen); |
3553 | s->s3.tmp.psk = NULL; | |
39a14059 | 3554 | s->s3.tmp.psklen = 0; |
0f113f3e | 3555 | #endif |
67ec6d2b | 3556 | return CON_FUNC_ERROR; |
b9908bf9 MC |
3557 | } |
3558 | ||
38b051a1 | 3559 | int tls_client_key_exchange_post_work(SSL_CONNECTION *s) |
b9908bf9 MC |
3560 | { |
3561 | unsigned char *pms = NULL; | |
3562 | size_t pmslen = 0; | |
3563 | ||
555cbb32 TS |
3564 | pms = s->s3.tmp.pms; |
3565 | pmslen = s->s3.tmp.pmslen; | |
6f137370 | 3566 | |
b9908bf9 MC |
3567 | #ifndef OPENSSL_NO_SRP |
3568 | /* Check for SRP */ | |
555cbb32 | 3569 | if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) { |
b9908bf9 | 3570 | if (!srp_generate_client_master_secret(s)) { |
a2c2e000 | 3571 | /* SSLfatal() already called */ |
b9908bf9 MC |
3572 | goto err; |
3573 | } | |
3574 | return 1; | |
3575 | } | |
3576 | #endif | |
b9908bf9 | 3577 | |
555cbb32 | 3578 | if (pms == NULL && !(s->s3.tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { |
e077455e | 3579 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_PASSED_INVALID_ARGUMENT); |
b9908bf9 MC |
3580 | goto err; |
3581 | } | |
3582 | if (!ssl_generate_master_secret(s, pms, pmslen, 1)) { | |
a2c2e000 | 3583 | /* SSLfatal() already called */ |
6f137370 MC |
3584 | /* ssl_generate_master_secret frees the pms even on error */ |
3585 | pms = NULL; | |
3586 | pmslen = 0; | |
b9908bf9 MC |
3587 | goto err; |
3588 | } | |
6f137370 MC |
3589 | pms = NULL; |
3590 | pmslen = 0; | |
473483d4 MC |
3591 | |
3592 | #ifndef OPENSSL_NO_SCTP | |
38b051a1 | 3593 | if (SSL_CONNECTION_IS_DTLS(s)) { |
473483d4 MC |
3594 | unsigned char sctpauthkey[64]; |
3595 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; | |
09d62b33 | 3596 | size_t labellen; |
38b051a1 | 3597 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
473483d4 MC |
3598 | |
3599 | /* | |
3600 | * Add new shared key for SCTP-Auth, will be ignored if no SCTP | |
3601 | * used. | |
3602 | */ | |
141eb8c6 MC |
3603 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
3604 | sizeof(DTLS1_SCTP_AUTH_LABEL)); | |
473483d4 | 3605 | |
09d62b33 MT |
3606 | /* Don't include the terminating zero. */ |
3607 | labellen = sizeof(labelbuffer) - 1; | |
3608 | if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG) | |
3609 | labellen += 1; | |
3610 | ||
38b051a1 | 3611 | if (SSL_export_keying_material(ssl, sctpauthkey, |
a230b26e | 3612 | sizeof(sctpauthkey), labelbuffer, |
09d62b33 | 3613 | labellen, NULL, 0, 0) <= 0) { |
c48ffbcc | 3614 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
473483d4 | 3615 | goto err; |
a2c2e000 | 3616 | } |
473483d4 | 3617 | |
38b051a1 | 3618 | BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, |
473483d4 MC |
3619 | sizeof(sctpauthkey), sctpauthkey); |
3620 | } | |
3621 | #endif | |
3622 | ||
b9908bf9 MC |
3623 | return 1; |
3624 | err: | |
3625 | OPENSSL_clear_free(pms, pmslen); | |
555cbb32 | 3626 | s->s3.tmp.pms = NULL; |
39a14059 | 3627 | s->s3.tmp.pmslen = 0; |
b9908bf9 | 3628 | return 0; |
0f113f3e | 3629 | } |
d02b48c6 | 3630 | |
0f113f3e MC |
3631 | /* |
3632 | * Check a certificate can be used for client authentication. Currently check | |
3633 | * cert exists, if we have a suitable digest for TLS 1.2 if static DH client | |
3634 | * certificates can be used and optionally checks suitability for Suite B. | |
0d609395 | 3635 | */ |
38b051a1 | 3636 | static int ssl3_check_client_certificate(SSL_CONNECTION *s) |
0f113f3e | 3637 | { |
0f113f3e | 3638 | /* If no suitable signature algorithm can't use certificate */ |
555cbb32 | 3639 | if (!tls_choose_sigalg(s, 0) || s->s3.tmp.sigalg == NULL) |
0f113f3e MC |
3640 | return 0; |
3641 | /* | |
3642 | * If strict mode check suitability of chain before using it. This also | |
3643 | * adjusts suite B digest if necessary. | |
3644 | */ | |
3645 | if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && | |
3646 | !tls1_check_chain(s, NULL, NULL, NULL, -2)) | |
3647 | return 0; | |
0f113f3e MC |
3648 | return 1; |
3649 | } | |
0d609395 | 3650 | |
38b051a1 | 3651 | WORK_STATE tls_prepare_client_certificate(SSL_CONNECTION *s, WORK_STATE wst) |
0f113f3e MC |
3652 | { |
3653 | X509 *x509 = NULL; | |
3654 | EVP_PKEY *pkey = NULL; | |
3655 | int i; | |
38b051a1 | 3656 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
0f113f3e | 3657 | |
b9908bf9 | 3658 | if (wst == WORK_MORE_A) { |
0f113f3e MC |
3659 | /* Let cert callback update client certificates if required */ |
3660 | if (s->cert->cert_cb) { | |
38b051a1 | 3661 | i = s->cert->cert_cb(ssl, s->cert->cert_cb_arg); |
0f113f3e MC |
3662 | if (i < 0) { |
3663 | s->rwstate = SSL_X509_LOOKUP; | |
b9908bf9 | 3664 | return WORK_MORE_A; |
0f113f3e MC |
3665 | } |
3666 | if (i == 0) { | |
c48ffbcc | 3667 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED); |
eb5fd03b | 3668 | return WORK_ERROR; |
0f113f3e MC |
3669 | } |
3670 | s->rwstate = SSL_NOTHING; | |
3671 | } | |
9d75dce3 TS |
3672 | if (ssl3_check_client_certificate(s)) { |
3673 | if (s->post_handshake_auth == SSL_PHA_REQUESTED) { | |
3674 | return WORK_FINISHED_STOP; | |
3675 | } | |
b9908bf9 | 3676 | return WORK_FINISHED_CONTINUE; |
9d75dce3 | 3677 | } |
b9908bf9 MC |
3678 | |
3679 | /* Fall through to WORK_MORE_B */ | |
3680 | wst = WORK_MORE_B; | |
0f113f3e MC |
3681 | } |
3682 | ||
3683 | /* We need to get a client cert */ | |
b9908bf9 | 3684 | if (wst == WORK_MORE_B) { |
0f113f3e MC |
3685 | /* |
3686 | * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP; | |
3687 | * return(-1); We then get retied later | |
3688 | */ | |
0f113f3e MC |
3689 | i = ssl_do_client_cert_cb(s, &x509, &pkey); |
3690 | if (i < 0) { | |
3691 | s->rwstate = SSL_X509_LOOKUP; | |
b9908bf9 | 3692 | return WORK_MORE_B; |
0f113f3e MC |
3693 | } |
3694 | s->rwstate = SSL_NOTHING; | |
3695 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { | |
38b051a1 TM |
3696 | if (!SSL_use_certificate(ssl, x509) |
3697 | || !SSL_use_PrivateKey(ssl, pkey)) | |
0f113f3e MC |
3698 | i = 0; |
3699 | } else if (i == 1) { | |
3700 | i = 0; | |
6849b73c | 3701 | ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); |
0f113f3e MC |
3702 | } |
3703 | ||
222561fe | 3704 | X509_free(x509); |
25aaa98a | 3705 | EVP_PKEY_free(pkey); |
0f113f3e MC |
3706 | if (i && !ssl3_check_client_certificate(s)) |
3707 | i = 0; | |
3708 | if (i == 0) { | |
3709 | if (s->version == SSL3_VERSION) { | |
555cbb32 | 3710 | s->s3.tmp.cert_req = 0; |
0f113f3e | 3711 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); |
b9908bf9 | 3712 | return WORK_FINISHED_CONTINUE; |
0f113f3e | 3713 | } else { |
555cbb32 | 3714 | s->s3.tmp.cert_req = 2; |
b67cb09f | 3715 | s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none; |
124037fd | 3716 | if (!ssl3_digest_cached_records(s, 0)) { |
f63a17d6 | 3717 | /* SSLfatal() already called */ |
eb5fd03b | 3718 | return WORK_ERROR; |
dab18ab5 | 3719 | } |
0f113f3e MC |
3720 | } |
3721 | } | |
3722 | ||
b67cb09f TS |
3723 | if (!SSL_CONNECTION_IS_TLS13(s) |
3724 | || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0) | |
3725 | s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none; | |
3726 | ||
9d75dce3 TS |
3727 | if (s->post_handshake_auth == SSL_PHA_REQUESTED) |
3728 | return WORK_FINISHED_STOP; | |
b9908bf9 | 3729 | return WORK_FINISHED_CONTINUE; |
0f113f3e MC |
3730 | } |
3731 | ||
b9908bf9 | 3732 | /* Shouldn't ever get here */ |
c48ffbcc | 3733 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b9908bf9 MC |
3734 | return WORK_ERROR; |
3735 | } | |
3736 | ||
67ec6d2b MC |
3737 | CON_FUNC_RETURN tls_construct_client_certificate(SSL_CONNECTION *s, |
3738 | WPACKET *pkt) | |
b9908bf9 | 3739 | { |
3c95ef22 | 3740 | CERT_PKEY *cpk = NULL; |
38b051a1 TM |
3741 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
3742 | ||
3743 | if (SSL_CONNECTION_IS_TLS13(s)) { | |
9d75dce3 TS |
3744 | if (s->pha_context == NULL) { |
3745 | /* no context available, add 0-length context */ | |
3746 | if (!WPACKET_put_bytes_u8(pkt, 0)) { | |
c48ffbcc | 3747 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 3748 | return CON_FUNC_ERROR; |
9d75dce3 TS |
3749 | } |
3750 | } else if (!WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) { | |
c48ffbcc | 3751 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 3752 | return CON_FUNC_ERROR; |
9d75dce3 | 3753 | } |
f63a17d6 | 3754 | } |
3c95ef22 TS |
3755 | if (s->s3.tmp.cert_req != 2) |
3756 | cpk = s->cert->key; | |
3757 | switch (s->ext.client_cert_type) { | |
3758 | case TLSEXT_cert_type_rpk: | |
3759 | if (!tls_output_rpk(s, pkt, cpk)) { | |
3760 | /* SSLfatal() already called */ | |
3761 | return CON_FUNC_ERROR; | |
3762 | } | |
3763 | break; | |
3764 | case TLSEXT_cert_type_x509: | |
3765 | if (!ssl3_output_cert_chain(s, pkt, cpk, 0)) { | |
3766 | /* SSLfatal() already called */ | |
3767 | return CON_FUNC_ERROR; | |
3768 | } | |
3769 | break; | |
3770 | default: | |
3771 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
67ec6d2b | 3772 | return CON_FUNC_ERROR; |
f7e393be MC |
3773 | } |
3774 | ||
38b051a1 | 3775 | if (SSL_CONNECTION_IS_TLS13(s) |
f7e393be | 3776 | && SSL_IS_FIRST_HANDSHAKE(s) |
38b051a1 | 3777 | && (!ssl->method->ssl3_enc->change_cipher_state(s, |
f7e393be | 3778 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) { |
c31ad0bb | 3779 | /* |
a2c2e000 | 3780 | * This is a fatal error, which leaves enc_write_ctx in an inconsistent |
f63a17d6 | 3781 | * state and thus ssl3_send_alert may crash. |
c31ad0bb | 3782 | */ |
c48ffbcc | 3783 | SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_CANNOT_CHANGE_CIPHER); |
67ec6d2b | 3784 | return CON_FUNC_ERROR; |
0f113f3e | 3785 | } |
b9908bf9 | 3786 | |
67ec6d2b | 3787 | return CON_FUNC_SUCCESS; |
0f113f3e MC |
3788 | } |
3789 | ||
b67cb09f TS |
3790 | #ifndef OPENSSL_NO_COMP_ALG |
3791 | CON_FUNC_RETURN tls_construct_client_compressed_certificate(SSL_CONNECTION *sc, | |
3792 | WPACKET *pkt) | |
3793 | { | |
3794 | SSL *ssl = SSL_CONNECTION_GET_SSL(sc); | |
3795 | WPACKET tmppkt; | |
3796 | BUF_MEM *buf = NULL; | |
3797 | size_t length; | |
3798 | size_t max_length; | |
3799 | COMP_METHOD *method; | |
3800 | COMP_CTX *comp = NULL; | |
3801 | int comp_len; | |
3802 | int ret = 0; | |
3803 | int alg = sc->ext.compress_certificate_from_peer[0]; | |
3804 | ||
3805 | /* Note that sc->s3.tmp.cert_req == 2 is checked in write transition */ | |
3806 | ||
3807 | if ((buf = BUF_MEM_new()) == NULL || !WPACKET_init(&tmppkt, buf)) | |
3808 | goto err; | |
3809 | ||
3810 | /* Use the |tmppkt| for the to-be-compressed data */ | |
3811 | if (sc->pha_context == NULL) { | |
3812 | /* no context available, add 0-length context */ | |
3813 | if (!WPACKET_put_bytes_u8(&tmppkt, 0)) | |
3814 | goto err; | |
3815 | } else if (!WPACKET_sub_memcpy_u8(&tmppkt, sc->pha_context, sc->pha_context_len)) | |
3816 | goto err; | |
3817 | ||
72620ac7 | 3818 | if (!ssl3_output_cert_chain(sc, &tmppkt, sc->cert->key, 0)) { |
b67cb09f TS |
3819 | /* SSLfatal() already called */ |
3820 | goto out; | |
3821 | } | |
3822 | ||
3823 | /* continue with the real |pkt| */ | |
3824 | if (!WPACKET_put_bytes_u16(pkt, alg) | |
3825 | || !WPACKET_get_total_written(&tmppkt, &length) | |
3826 | || !WPACKET_put_bytes_u24(pkt, length)) | |
3827 | goto err; | |
3828 | ||
3829 | switch (alg) { | |
3830 | case TLSEXT_comp_cert_zlib: | |
3840271e | 3831 | method = COMP_zlib_oneshot(); |
b67cb09f TS |
3832 | break; |
3833 | case TLSEXT_comp_cert_brotli: | |
3834 | method = COMP_brotli_oneshot(); | |
3835 | break; | |
3836 | case TLSEXT_comp_cert_zstd: | |
3837 | method = COMP_zstd_oneshot(); | |
3838 | break; | |
3839 | default: | |
3840 | goto err; | |
3841 | } | |
3842 | max_length = ossl_calculate_comp_expansion(alg, length); | |
3843 | ||
7e3cacac TS |
3844 | if ((comp = COMP_CTX_new(method)) == NULL |
3845 | || !WPACKET_start_sub_packet_u24(pkt) | |
3846 | || !WPACKET_reserve_bytes(pkt, max_length, NULL)) | |
b67cb09f TS |
3847 | goto err; |
3848 | ||
3849 | comp_len = COMP_compress_block(comp, WPACKET_get_curr(pkt), max_length, | |
3850 | (unsigned char *)buf->data, length); | |
3851 | if (comp_len <= 0) | |
3852 | goto err; | |
3853 | ||
3854 | if (!WPACKET_allocate_bytes(pkt, comp_len, NULL) | |
3855 | || !WPACKET_close(pkt)) | |
3856 | goto err; | |
3857 | ||
3858 | if (SSL_IS_FIRST_HANDSHAKE(sc) | |
3859 | && (!ssl->method->ssl3_enc->change_cipher_state(sc, | |
3860 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) { | |
3861 | /* | |
3862 | * This is a fatal error, which leaves sc->enc_write_ctx in an | |
3863 | * inconsistent state and thus ssl3_send_alert may crash. | |
3864 | */ | |
3865 | SSLfatal(sc, SSL_AD_NO_ALERT, SSL_R_CANNOT_CHANGE_CIPHER); | |
3866 | goto out; | |
3867 | } | |
3868 | ret = 1; | |
3869 | goto out; | |
3870 | ||
3871 | err: | |
3872 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
3873 | out: | |
d06d5d6b TS |
3874 | if (buf != NULL) { |
3875 | /* If |buf| is NULL, then |tmppkt| could not have been initialized */ | |
3876 | WPACKET_cleanup(&tmppkt); | |
3877 | } | |
b67cb09f TS |
3878 | BUF_MEM_free(buf); |
3879 | COMP_CTX_free(comp); | |
3880 | return ret; | |
3881 | } | |
3882 | #endif | |
3883 | ||
38b051a1 | 3884 | int ssl3_check_cert_and_algorithm(SSL_CONNECTION *s) |
0f113f3e | 3885 | { |
dd24857b DSH |
3886 | const SSL_CERT_LOOKUP *clu; |
3887 | size_t idx; | |
0f113f3e | 3888 | long alg_k, alg_a; |
3c95ef22 | 3889 | EVP_PKEY *pkey; |
d02b48c6 | 3890 | |
555cbb32 TS |
3891 | alg_k = s->s3.tmp.new_cipher->algorithm_mkey; |
3892 | alg_a = s->s3.tmp.new_cipher->algorithm_auth; | |
d02b48c6 | 3893 | |
0f113f3e | 3894 | /* we don't have a certificate */ |
dd24857b DSH |
3895 | if (!(alg_a & SSL_aCERT)) |
3896 | return 1; | |
d02b48c6 | 3897 | |
0f113f3e | 3898 | /* This is the passed certificate */ |
3c95ef22 TS |
3899 | pkey = tls_get_peer_pkey(s); |
3900 | clu = ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s)); | |
d02b48c6 | 3901 | |
dd24857b DSH |
3902 | /* Check certificate is recognised and suitable for cipher */ |
3903 | if (clu == NULL || (alg_a & clu->amask) == 0) { | |
c48ffbcc | 3904 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_SIGNING_CERT); |
f63a17d6 | 3905 | return 0; |
0f113f3e | 3906 | } |
0f113f3e | 3907 | |
dd24857b | 3908 | if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && idx != SSL_PKEY_RSA) { |
f63a17d6 | 3909 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
f63a17d6 MC |
3910 | SSL_R_MISSING_RSA_ENCRYPTING_CERT); |
3911 | return 0; | |
0f113f3e | 3912 | } |
5b64ce89 | 3913 | |
555cbb32 | 3914 | if ((alg_k & SSL_kDHE) && (s->s3.peer_tmp == NULL)) { |
c48ffbcc | 3915 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 3916 | return 0; |
0f113f3e | 3917 | } |
d02b48c6 | 3918 | |
3c95ef22 TS |
3919 | /* Early out to skip the checks below */ |
3920 | if (s->session->peer_rpk != NULL) | |
3921 | return 1; | |
3922 | ||
3923 | if (clu->amask & SSL_aECDSA) { | |
3924 | if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s)) | |
3925 | return 1; | |
3926 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_ECC_CERT); | |
3927 | return 0; | |
3928 | } | |
3929 | ||
dd24857b | 3930 | return 1; |
0f113f3e MC |
3931 | } |
3932 | ||
e481f9b9 | 3933 | #ifndef OPENSSL_NO_NEXTPROTONEG |
67ec6d2b | 3934 | CON_FUNC_RETURN tls_construct_next_proto(SSL_CONNECTION *s, WPACKET *pkt) |
b9908bf9 | 3935 | { |
15e6be6c MC |
3936 | size_t len, padding_len; |
3937 | unsigned char *padding = NULL; | |
15e6be6c | 3938 | |
aff8c126 | 3939 | len = s->ext.npn_len; |
b9908bf9 | 3940 | padding_len = 32 - ((len + 2) % 32); |
15e6be6c | 3941 | |
aff8c126 | 3942 | if (!WPACKET_sub_memcpy_u8(pkt, s->ext.npn, len) |
7cea05dc | 3943 | || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) { |
c48ffbcc | 3944 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
67ec6d2b | 3945 | return CON_FUNC_ERROR; |
15e6be6c MC |
3946 | } |
3947 | ||
3948 | memset(padding, 0, padding_len); | |
3949 | ||
67ec6d2b | 3950 | return CON_FUNC_SUCCESS; |
b9908bf9 | 3951 | } |
6434abbf | 3952 | #endif |
368888bc | 3953 | |
38b051a1 | 3954 | MSG_PROCESS_RETURN tls_process_hello_req(SSL_CONNECTION *s, PACKET *pkt) |
c7f47786 | 3955 | { |
38b051a1 TM |
3956 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
3957 | ||
c7f47786 MC |
3958 | if (PACKET_remaining(pkt) > 0) { |
3959 | /* should contain no data */ | |
c48ffbcc | 3960 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
c7f47786 MC |
3961 | return MSG_PROCESS_ERROR; |
3962 | } | |
3963 | ||
db0f35dd TS |
3964 | if ((s->options & SSL_OP_NO_RENEGOTIATION)) { |
3965 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION); | |
3966 | return MSG_PROCESS_FINISHED_READING; | |
3967 | } | |
3968 | ||
c7f47786 | 3969 | /* |
1f04f23e MC |
3970 | * This is a historical discrepancy (not in the RFC) maintained for |
3971 | * compatibility reasons. If a TLS client receives a HelloRequest it will | |
3972 | * attempt an abbreviated handshake. However if a DTLS client receives a | |
3973 | * HelloRequest it will do a full handshake. Either behaviour is reasonable | |
3974 | * but doing one for TLS and another for DTLS is odd. | |
c7f47786 | 3975 | */ |
38b051a1 TM |
3976 | if (SSL_CONNECTION_IS_DTLS(s)) |
3977 | SSL_renegotiate(ssl); | |
c7f47786 | 3978 | else |
38b051a1 | 3979 | SSL_renegotiate_abbreviated(ssl); |
c7f47786 MC |
3980 | |
3981 | return MSG_PROCESS_FINISHED_READING; | |
3982 | } | |
3983 | ||
38b051a1 TM |
3984 | static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL_CONNECTION *s, |
3985 | PACKET *pkt) | |
e46f2334 | 3986 | { |
e46f2334 | 3987 | PACKET extensions; |
3434f40b | 3988 | RAW_EXTENSION *rawexts = NULL; |
e46f2334 | 3989 | |
26b9172a MC |
3990 | if (!PACKET_as_length_prefixed_2(pkt, &extensions) |
3991 | || PACKET_remaining(pkt) != 0) { | |
c48ffbcc | 3992 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
e46f2334 MC |
3993 | goto err; |
3994 | } | |
3995 | ||
fe874d27 MC |
3996 | if (!tls_collect_extensions(s, &extensions, |
3997 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, &rawexts, | |
f63a17d6 | 3998 | NULL, 1) |
fe874d27 | 3999 | || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, |
f63a17d6 MC |
4000 | rawexts, NULL, 0, 1)) { |
4001 | /* SSLfatal() already called */ | |
3434f40b | 4002 | goto err; |
f63a17d6 | 4003 | } |
3434f40b | 4004 | |
1b0286a3 | 4005 | OPENSSL_free(rawexts); |
e46f2334 MC |
4006 | return MSG_PROCESS_CONTINUE_READING; |
4007 | ||
4008 | err: | |
1b0286a3 | 4009 | OPENSSL_free(rawexts); |
e46f2334 MC |
4010 | return MSG_PROCESS_ERROR; |
4011 | } | |
4012 | ||
38b051a1 | 4013 | int ssl_do_client_cert_cb(SSL_CONNECTION *s, X509 **px509, EVP_PKEY **ppkey) |
0f113f3e MC |
4014 | { |
4015 | int i = 0; | |
38b051a1 TM |
4016 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
4017 | ||
368888bc | 4018 | #ifndef OPENSSL_NO_ENGINE |
38b051a1 | 4019 | if (sctx->client_cert_engine) { |
301fcb28 | 4020 | i = tls_engine_load_ssl_client_cert(s, px509, ppkey); |
0f113f3e MC |
4021 | if (i != 0) |
4022 | return i; | |
4023 | } | |
4024 | #endif | |
38b051a1 TM |
4025 | if (sctx->client_cert_cb) |
4026 | i = sctx->client_cert_cb(SSL_CONNECTION_GET_SSL(s), px509, ppkey); | |
0f113f3e MC |
4027 | return i; |
4028 | } | |
d45ba43d | 4029 | |
38b051a1 TM |
4030 | int ssl_cipher_list_to_bytes(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *sk, |
4031 | WPACKET *pkt) | |
d45ba43d | 4032 | { |
2c7b4dbc | 4033 | int i; |
aafec89c | 4034 | size_t totlen = 0, len, maxlen, maxverok = 0; |
1eef26bd TM |
4035 | int empty_reneg_info_scsv = !s->renegotiate |
4036 | && (SSL_CONNECTION_IS_DTLS(s) | |
4037 | || s->min_proto_version < TLS1_3_VERSION); | |
38b051a1 | 4038 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
1d0c08b4 | 4039 | |
d45ba43d | 4040 | /* Set disabled masks for this session */ |
1d0c08b4 | 4041 | if (!ssl_set_client_disabled(s)) { |
c48ffbcc | 4042 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_PROTOCOLS_AVAILABLE); |
1d0c08b4 MC |
4043 | return 0; |
4044 | } | |
d45ba43d | 4045 | |
f63a17d6 | 4046 | if (sk == NULL) { |
c48ffbcc | 4047 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
26a7d938 | 4048 | return 0; |
f63a17d6 | 4049 | } |
d45ba43d | 4050 | |
2c7b4dbc MC |
4051 | #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH |
4052 | # if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6 | |
4053 | # error Max cipher length too short | |
4054 | # endif | |
4055 | /* | |
4056 | * Some servers hang if client hello > 256 bytes as hack workaround | |
4057 | * chop number of supported ciphers to keep it well below this if we | |
4058 | * use TLS v1.2 | |
4059 | */ | |
38b051a1 | 4060 | if (TLS1_get_version(ssl) >= TLS1_2_VERSION) |
2c7b4dbc MC |
4061 | maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1; |
4062 | else | |
4063 | #endif | |
4064 | /* Maximum length that can be stored in 2 bytes. Length must be even */ | |
4065 | maxlen = 0xfffe; | |
4066 | ||
4067 | if (empty_reneg_info_scsv) | |
4068 | maxlen -= 2; | |
4069 | if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) | |
4070 | maxlen -= 2; | |
4071 | ||
4072 | for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) { | |
4073 | const SSL_CIPHER *c; | |
4074 | ||
d45ba43d MC |
4075 | c = sk_SSL_CIPHER_value(sk, i); |
4076 | /* Skip disabled ciphers */ | |
8af91fd9 | 4077 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) |
d45ba43d | 4078 | continue; |
2c7b4dbc | 4079 | |
38b051a1 | 4080 | if (!ssl->method->put_cipher_by_char(c, pkt, &len)) { |
c48ffbcc | 4081 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2c7b4dbc MC |
4082 | return 0; |
4083 | } | |
4084 | ||
aafec89c MC |
4085 | /* Sanity check that the maximum version we offer has ciphers enabled */ |
4086 | if (!maxverok) { | |
38b051a1 | 4087 | if (SSL_CONNECTION_IS_DTLS(s)) { |
555cbb32 TS |
4088 | if (DTLS_VERSION_GE(c->max_dtls, s->s3.tmp.max_ver) |
4089 | && DTLS_VERSION_LE(c->min_dtls, s->s3.tmp.max_ver)) | |
aafec89c MC |
4090 | maxverok = 1; |
4091 | } else { | |
555cbb32 TS |
4092 | if (c->max_tls >= s->s3.tmp.max_ver |
4093 | && c->min_tls <= s->s3.tmp.max_ver) | |
aafec89c MC |
4094 | maxverok = 1; |
4095 | } | |
4096 | } | |
4097 | ||
2c7b4dbc | 4098 | totlen += len; |
d45ba43d | 4099 | } |
2c7b4dbc | 4100 | |
aafec89c | 4101 | if (totlen == 0 || !maxverok) { |
c48ffbcc RL |
4102 | const char *maxvertext = |
4103 | !maxverok | |
4104 | ? "No ciphers enabled for max supported SSL/TLS version" | |
4105 | : NULL; | |
aafec89c | 4106 | |
c48ffbcc RL |
4107 | SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_CIPHERS_AVAILABLE, |
4108 | maxvertext); | |
2c7b4dbc MC |
4109 | return 0; |
4110 | } | |
4111 | ||
4112 | if (totlen != 0) { | |
d45ba43d MC |
4113 | if (empty_reneg_info_scsv) { |
4114 | static SSL_CIPHER scsv = { | |
bbb4ceb8 | 4115 | 0, NULL, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
d45ba43d | 4116 | }; |
38b051a1 | 4117 | if (!ssl->method->put_cipher_by_char(&scsv, pkt, &len)) { |
c48ffbcc | 4118 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2c7b4dbc MC |
4119 | return 0; |
4120 | } | |
d45ba43d MC |
4121 | } |
4122 | if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) { | |
4123 | static SSL_CIPHER scsv = { | |
bbb4ceb8 | 4124 | 0, NULL, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
d45ba43d | 4125 | }; |
38b051a1 | 4126 | if (!ssl->method->put_cipher_by_char(&scsv, pkt, &len)) { |
c48ffbcc | 4127 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2c7b4dbc MC |
4128 | return 0; |
4129 | } | |
d45ba43d MC |
4130 | } |
4131 | } | |
4132 | ||
2c7b4dbc | 4133 | return 1; |
d45ba43d | 4134 | } |
ef6c191b | 4135 | |
67ec6d2b | 4136 | CON_FUNC_RETURN tls_construct_end_of_early_data(SSL_CONNECTION *s, WPACKET *pkt) |
ef6c191b MC |
4137 | { |
4138 | if (s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY | |
4139 | && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) { | |
c48ffbcc | 4140 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
67ec6d2b | 4141 | return CON_FUNC_ERROR; |
ef6c191b MC |
4142 | } |
4143 | ||
4144 | s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING; | |
67ec6d2b | 4145 | return CON_FUNC_SUCCESS; |
ef6c191b | 4146 | } |