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