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