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