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