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