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