/src/openssl/ssl/statem/statem_srvr.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
4 | | * Copyright 2005 Nokia. All rights reserved. |
5 | | * |
6 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
7 | | * this file except in compliance with the License. You can obtain a copy |
8 | | * in the file LICENSE in the source distribution or at |
9 | | * https://www.openssl.org/source/license.html |
10 | | */ |
11 | | |
12 | | #include "internal/e_os.h" |
13 | | |
14 | | #include <stdio.h> |
15 | | #include "../ssl_local.h" |
16 | | #include "statem_local.h" |
17 | | #include "internal/constant_time.h" |
18 | | #include "internal/cryptlib.h" |
19 | | #include "internal/ssl_unwrap.h" |
20 | | #include <openssl/buffer.h> |
21 | | #include <openssl/rand.h> |
22 | | #include <openssl/objects.h> |
23 | | #include <openssl/evp.h> |
24 | | #include <openssl/x509.h> |
25 | | #include <openssl/dh.h> |
26 | | #include <openssl/rsa.h> |
27 | | #include <openssl/bn.h> |
28 | | #include <openssl/md5.h> |
29 | | #include <openssl/trace.h> |
30 | | #include <openssl/core_names.h> |
31 | | #include <openssl/asn1t.h> |
32 | | #include <openssl/comp.h> |
33 | | #include "internal/comp.h" |
34 | | |
35 | 0 | #define TICKET_NONCE_SIZE 8 |
36 | | |
37 | | typedef struct { |
38 | | ASN1_TYPE *kxBlob; |
39 | | ASN1_TYPE *opaqueBlob; |
40 | | } GOST_KX_MESSAGE; |
41 | | |
42 | | DECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE) |
43 | | |
44 | | ASN1_SEQUENCE(GOST_KX_MESSAGE) = { |
45 | | ASN1_SIMPLE(GOST_KX_MESSAGE, kxBlob, ASN1_ANY), |
46 | | ASN1_OPT(GOST_KX_MESSAGE, opaqueBlob, ASN1_ANY), |
47 | | } ASN1_SEQUENCE_END(GOST_KX_MESSAGE) |
48 | | |
49 | | IMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE) |
50 | | |
51 | | static CON_FUNC_RETURN tls_construct_encrypted_extensions(SSL_CONNECTION *s, |
52 | | WPACKET *pkt); |
53 | | |
54 | | static ossl_inline int received_client_cert(const SSL_CONNECTION *sc) |
55 | 0 | { |
56 | 0 | return sc->session->peer_rpk != NULL || sc->session->peer != NULL; |
57 | 0 | } |
58 | | |
59 | | /* |
60 | | * ossl_statem_server13_read_transition() encapsulates the logic for the allowed |
61 | | * handshake state transitions when a TLSv1.3 server is reading messages from |
62 | | * the client. The message type that the client has sent is provided in |mt|. |
63 | | * The current state is in |s->statem.hand_state|. |
64 | | * |
65 | | * Return values are 1 for success (transition allowed) and 0 on error |
66 | | * (transition not allowed) |
67 | | */ |
68 | | static int ossl_statem_server13_read_transition(SSL_CONNECTION *s, int mt) |
69 | 0 | { |
70 | 0 | OSSL_STATEM *st = &s->statem; |
71 | | |
72 | | /* |
73 | | * Note: There is no case for TLS_ST_BEFORE because at that stage we have |
74 | | * not negotiated TLSv1.3 yet, so that case is handled by |
75 | | * ossl_statem_server_read_transition() |
76 | | */ |
77 | 0 | switch (st->hand_state) { |
78 | 0 | default: |
79 | 0 | break; |
80 | | |
81 | 0 | case TLS_ST_EARLY_DATA: |
82 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING) { |
83 | 0 | if (mt == SSL3_MT_CLIENT_HELLO) { |
84 | 0 | st->hand_state = TLS_ST_SR_CLNT_HELLO; |
85 | 0 | return 1; |
86 | 0 | } |
87 | 0 | break; |
88 | 0 | } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED |
89 | 0 | && !SSL_NO_EOED(s)) { |
90 | 0 | if (mt == SSL3_MT_END_OF_EARLY_DATA) { |
91 | 0 | st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA; |
92 | 0 | return 1; |
93 | 0 | } |
94 | 0 | break; |
95 | 0 | } |
96 | | /* Fall through */ |
97 | | |
98 | 0 | case TLS_ST_SR_END_OF_EARLY_DATA: |
99 | 0 | case TLS_ST_SW_FINISHED: |
100 | 0 | if (s->s3.tmp.cert_request) { |
101 | 0 | if (mt == SSL3_MT_CERTIFICATE) { |
102 | 0 | st->hand_state = TLS_ST_SR_CERT; |
103 | 0 | return 1; |
104 | 0 | } |
105 | | #ifndef OPENSSL_NO_COMP_ALG |
106 | | if (mt == SSL3_MT_COMPRESSED_CERTIFICATE |
107 | | && s->ext.compress_certificate_sent) { |
108 | | st->hand_state = TLS_ST_SR_COMP_CERT; |
109 | | return 1; |
110 | | } |
111 | | #endif |
112 | 0 | } else { |
113 | 0 | if (mt == SSL3_MT_FINISHED) { |
114 | 0 | st->hand_state = TLS_ST_SR_FINISHED; |
115 | 0 | return 1; |
116 | 0 | } |
117 | 0 | } |
118 | 0 | break; |
119 | | |
120 | 0 | case TLS_ST_SR_COMP_CERT: |
121 | 0 | case TLS_ST_SR_CERT: |
122 | 0 | if (!received_client_cert(s)) { |
123 | 0 | if (mt == SSL3_MT_FINISHED) { |
124 | 0 | st->hand_state = TLS_ST_SR_FINISHED; |
125 | 0 | return 1; |
126 | 0 | } |
127 | 0 | } else { |
128 | 0 | if (mt == SSL3_MT_CERTIFICATE_VERIFY) { |
129 | 0 | st->hand_state = TLS_ST_SR_CERT_VRFY; |
130 | 0 | return 1; |
131 | 0 | } |
132 | 0 | } |
133 | 0 | break; |
134 | | |
135 | 0 | case TLS_ST_SR_CERT_VRFY: |
136 | 0 | if (mt == SSL3_MT_FINISHED) { |
137 | 0 | st->hand_state = TLS_ST_SR_FINISHED; |
138 | 0 | return 1; |
139 | 0 | } |
140 | 0 | break; |
141 | | |
142 | 0 | case TLS_ST_OK: |
143 | | /* |
144 | | * Its never ok to start processing handshake messages in the middle of |
145 | | * early data (i.e. before we've received the end of early data alert) |
146 | | */ |
147 | 0 | if (s->early_data_state == SSL_EARLY_DATA_READING) |
148 | 0 | break; |
149 | | |
150 | 0 | if (s->post_handshake_auth == SSL_PHA_REQUESTED) { |
151 | 0 | if (mt == SSL3_MT_CERTIFICATE) { |
152 | 0 | st->hand_state = TLS_ST_SR_CERT; |
153 | 0 | return 1; |
154 | 0 | } |
155 | | #ifndef OPENSSL_NO_COMP_ALG |
156 | | if (mt == SSL3_MT_COMPRESSED_CERTIFICATE |
157 | | && s->ext.compress_certificate_sent) { |
158 | | st->hand_state = TLS_ST_SR_COMP_CERT; |
159 | | return 1; |
160 | | } |
161 | | #endif |
162 | 0 | } |
163 | | |
164 | 0 | if (mt == SSL3_MT_KEY_UPDATE && !SSL_IS_QUIC_HANDSHAKE(s)) { |
165 | 0 | st->hand_state = TLS_ST_SR_KEY_UPDATE; |
166 | 0 | return 1; |
167 | 0 | } |
168 | 0 | break; |
169 | 0 | } |
170 | | |
171 | | /* No valid transition found */ |
172 | 0 | return 0; |
173 | 0 | } |
174 | | |
175 | | /* |
176 | | * ossl_statem_server_read_transition() encapsulates the logic for the allowed |
177 | | * handshake state transitions when the server is reading messages from the |
178 | | * client. The message type that the client has sent is provided in |mt|. The |
179 | | * current state is in |s->statem.hand_state|. |
180 | | * |
181 | | * Return values are 1 for success (transition allowed) and 0 on error |
182 | | * (transition not allowed) |
183 | | */ |
184 | | int ossl_statem_server_read_transition(SSL_CONNECTION *s, int mt) |
185 | 0 | { |
186 | 0 | OSSL_STATEM *st = &s->statem; |
187 | |
|
188 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
189 | 0 | if (!ossl_statem_server13_read_transition(s, mt)) |
190 | 0 | goto err; |
191 | 0 | return 1; |
192 | 0 | } |
193 | | |
194 | 0 | switch (st->hand_state) { |
195 | 0 | default: |
196 | 0 | break; |
197 | | |
198 | 0 | case TLS_ST_BEFORE: |
199 | 0 | case TLS_ST_OK: |
200 | 0 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: |
201 | 0 | if (mt == SSL3_MT_CLIENT_HELLO) { |
202 | 0 | st->hand_state = TLS_ST_SR_CLNT_HELLO; |
203 | 0 | return 1; |
204 | 0 | } |
205 | 0 | break; |
206 | | |
207 | 0 | case TLS_ST_SW_SRVR_DONE: |
208 | | /* |
209 | | * If we get a CKE message after a ServerDone then either |
210 | | * 1) We didn't request a Certificate |
211 | | * OR |
212 | | * 2) If we did request one then |
213 | | * a) We allow no Certificate to be returned |
214 | | * AND |
215 | | * b) We are running SSL3 (in TLS1.0+ the client must return a 0 |
216 | | * list if we requested a certificate) |
217 | | */ |
218 | 0 | if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) { |
219 | 0 | if (s->s3.tmp.cert_request) { |
220 | 0 | if (s->version == SSL3_VERSION) { |
221 | 0 | if ((s->verify_mode & SSL_VERIFY_PEER) |
222 | 0 | && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { |
223 | | /* |
224 | | * This isn't an unexpected message as such - we're just |
225 | | * not going to accept it because we require a client |
226 | | * cert. |
227 | | */ |
228 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
229 | 0 | SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
230 | 0 | return 0; |
231 | 0 | } |
232 | 0 | st->hand_state = TLS_ST_SR_KEY_EXCH; |
233 | 0 | return 1; |
234 | 0 | } |
235 | 0 | } else { |
236 | 0 | st->hand_state = TLS_ST_SR_KEY_EXCH; |
237 | 0 | return 1; |
238 | 0 | } |
239 | 0 | } else if (s->s3.tmp.cert_request) { |
240 | 0 | if (mt == SSL3_MT_CERTIFICATE) { |
241 | 0 | st->hand_state = TLS_ST_SR_CERT; |
242 | 0 | return 1; |
243 | 0 | } |
244 | 0 | } |
245 | 0 | break; |
246 | | |
247 | 0 | case TLS_ST_SR_CERT: |
248 | 0 | if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) { |
249 | 0 | st->hand_state = TLS_ST_SR_KEY_EXCH; |
250 | 0 | return 1; |
251 | 0 | } |
252 | 0 | break; |
253 | | |
254 | 0 | case TLS_ST_SR_KEY_EXCH: |
255 | | /* |
256 | | * We should only process a CertificateVerify message if we have |
257 | | * received a Certificate from the client. If so then |s->session->peer| |
258 | | * will be non NULL. In some instances a CertificateVerify message is |
259 | | * not required even if the peer has sent a Certificate (e.g. such as in |
260 | | * the case of static DH). In that case |st->no_cert_verify| should be |
261 | | * set. |
262 | | */ |
263 | 0 | if (!received_client_cert(s) || st->no_cert_verify) { |
264 | 0 | if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
265 | | /* |
266 | | * For the ECDH ciphersuites when the client sends its ECDH |
267 | | * pub key in a certificate, the CertificateVerify message is |
268 | | * not sent. Also for GOST ciphersuites when the client uses |
269 | | * its key from the certificate for key exchange. |
270 | | */ |
271 | 0 | st->hand_state = TLS_ST_SR_CHANGE; |
272 | 0 | return 1; |
273 | 0 | } |
274 | 0 | } else { |
275 | 0 | if (mt == SSL3_MT_CERTIFICATE_VERIFY) { |
276 | 0 | st->hand_state = TLS_ST_SR_CERT_VRFY; |
277 | 0 | return 1; |
278 | 0 | } |
279 | 0 | } |
280 | 0 | break; |
281 | | |
282 | 0 | case TLS_ST_SR_CERT_VRFY: |
283 | 0 | if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
284 | 0 | st->hand_state = TLS_ST_SR_CHANGE; |
285 | 0 | return 1; |
286 | 0 | } |
287 | 0 | break; |
288 | | |
289 | 0 | case TLS_ST_SR_CHANGE: |
290 | 0 | #ifndef OPENSSL_NO_NEXTPROTONEG |
291 | 0 | if (s->s3.npn_seen) { |
292 | 0 | if (mt == SSL3_MT_NEXT_PROTO) { |
293 | 0 | st->hand_state = TLS_ST_SR_NEXT_PROTO; |
294 | 0 | return 1; |
295 | 0 | } |
296 | 0 | } else { |
297 | 0 | #endif |
298 | 0 | if (mt == SSL3_MT_FINISHED) { |
299 | 0 | st->hand_state = TLS_ST_SR_FINISHED; |
300 | 0 | return 1; |
301 | 0 | } |
302 | 0 | #ifndef OPENSSL_NO_NEXTPROTONEG |
303 | 0 | } |
304 | 0 | #endif |
305 | 0 | break; |
306 | | |
307 | 0 | #ifndef OPENSSL_NO_NEXTPROTONEG |
308 | 0 | case TLS_ST_SR_NEXT_PROTO: |
309 | 0 | if (mt == SSL3_MT_FINISHED) { |
310 | 0 | st->hand_state = TLS_ST_SR_FINISHED; |
311 | 0 | return 1; |
312 | 0 | } |
313 | 0 | break; |
314 | 0 | #endif |
315 | | |
316 | 0 | case TLS_ST_SW_FINISHED: |
317 | 0 | if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
318 | 0 | st->hand_state = TLS_ST_SR_CHANGE; |
319 | 0 | return 1; |
320 | 0 | } |
321 | 0 | break; |
322 | 0 | } |
323 | | |
324 | 0 | err: |
325 | | /* No valid transition found */ |
326 | 0 | if (SSL_CONNECTION_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
327 | 0 | BIO *rbio; |
328 | | |
329 | | /* |
330 | | * CCS messages don't have a message sequence number so this is probably |
331 | | * because of an out-of-order CCS. We'll just drop it. |
332 | | */ |
333 | 0 | s->init_num = 0; |
334 | 0 | s->rwstate = SSL_READING; |
335 | 0 | rbio = SSL_get_rbio(SSL_CONNECTION_GET_SSL(s)); |
336 | 0 | BIO_clear_retry_flags(rbio); |
337 | 0 | BIO_set_retry_read(rbio); |
338 | 0 | return 0; |
339 | 0 | } |
340 | 0 | SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
341 | 0 | return 0; |
342 | 0 | } |
343 | | |
344 | | /* |
345 | | * Should we send a ServerKeyExchange message? |
346 | | * |
347 | | * Valid return values are: |
348 | | * 1: Yes |
349 | | * 0: No |
350 | | */ |
351 | | static int send_server_key_exchange(SSL_CONNECTION *s) |
352 | 0 | { |
353 | 0 | unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey; |
354 | | |
355 | | /* |
356 | | * only send a ServerKeyExchange if DH or fortezza but we have a |
357 | | * sign only certificate PSK: may send PSK identity hints For |
358 | | * ECC ciphersuites, we send a serverKeyExchange message only if |
359 | | * the cipher suite is either ECDH-anon or ECDHE. In other cases, |
360 | | * the server certificate contains the server's public key for |
361 | | * key exchange. |
362 | | */ |
363 | 0 | if (alg_k & (SSL_kDHE | SSL_kECDHE) |
364 | | /* |
365 | | * PSK: send ServerKeyExchange if PSK identity hint if |
366 | | * provided |
367 | | */ |
368 | 0 | #ifndef OPENSSL_NO_PSK |
369 | | /* Only send SKE if we have identity hint for plain PSK */ |
370 | 0 | || ((alg_k & (SSL_kPSK | SSL_kRSAPSK)) |
371 | 0 | && s->cert->psk_identity_hint) |
372 | | /* For other PSK always send SKE */ |
373 | 0 | || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK))) |
374 | 0 | #endif |
375 | 0 | #ifndef OPENSSL_NO_SRP |
376 | | /* SRP: send ServerKeyExchange */ |
377 | 0 | || (alg_k & SSL_kSRP) |
378 | 0 | #endif |
379 | 0 | ) { |
380 | 0 | return 1; |
381 | 0 | } |
382 | | |
383 | 0 | return 0; |
384 | 0 | } |
385 | | |
386 | | /* |
387 | | * Used to determine if we should send a CompressedCertificate message |
388 | | * |
389 | | * Returns the algorithm to use, TLSEXT_comp_cert_none means no compression |
390 | | */ |
391 | | static int get_compressed_certificate_alg(SSL_CONNECTION *sc) |
392 | 0 | { |
393 | | #ifndef OPENSSL_NO_COMP_ALG |
394 | | int *alg = sc->ext.compress_certificate_from_peer; |
395 | | |
396 | | if (sc->s3.tmp.cert == NULL) |
397 | | return TLSEXT_comp_cert_none; |
398 | | |
399 | | for (; *alg != TLSEXT_comp_cert_none; alg++) { |
400 | | if (sc->s3.tmp.cert->comp_cert[*alg] != NULL) |
401 | | return *alg; |
402 | | } |
403 | | #endif |
404 | 0 | return TLSEXT_comp_cert_none; |
405 | 0 | } |
406 | | |
407 | | /* |
408 | | * Should we send a CertificateRequest message? |
409 | | * |
410 | | * Valid return values are: |
411 | | * 1: Yes |
412 | | * 0: No |
413 | | */ |
414 | | int send_certificate_request(SSL_CONNECTION *s) |
415 | 0 | { |
416 | 0 | if ( |
417 | | /* don't request cert unless asked for it: */ |
418 | 0 | s->verify_mode & SSL_VERIFY_PEER |
419 | | /* |
420 | | * don't request if post-handshake-only unless doing |
421 | | * post-handshake in TLSv1.3: |
422 | | */ |
423 | 0 | && (!SSL_CONNECTION_IS_TLS13(s) |
424 | 0 | || !(s->verify_mode & SSL_VERIFY_POST_HANDSHAKE) |
425 | 0 | || s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) |
426 | | /* |
427 | | * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert |
428 | | * a second time: |
429 | | */ |
430 | 0 | && (s->certreqs_sent < 1 || |
431 | 0 | !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) |
432 | | /* |
433 | | * never request cert in anonymous ciphersuites (see |
434 | | * section "Certificate request" in SSL 3 drafts and in |
435 | | * RFC 2246): |
436 | | */ |
437 | 0 | && (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL) |
438 | | /* |
439 | | * ... except when the application insists on |
440 | | * verification (against the specs, but statem_clnt.c accepts |
441 | | * this for SSL 3) |
442 | | */ |
443 | 0 | || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) |
444 | | /* don't request certificate for SRP auth */ |
445 | 0 | && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aSRP) |
446 | | /* |
447 | | * With normal PSK Certificates and Certificate Requests |
448 | | * are omitted |
449 | | */ |
450 | 0 | && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aPSK)) { |
451 | 0 | return 1; |
452 | 0 | } |
453 | | |
454 | 0 | return 0; |
455 | 0 | } |
456 | | |
457 | | static int do_compressed_cert(SSL_CONNECTION *sc) |
458 | 0 | { |
459 | | /* If we negotiated RPK, we won't attempt to compress it */ |
460 | 0 | return sc->ext.server_cert_type == TLSEXT_cert_type_x509 |
461 | 0 | && get_compressed_certificate_alg(sc) != TLSEXT_comp_cert_none; |
462 | 0 | } |
463 | | |
464 | | /* |
465 | | * ossl_statem_server13_write_transition() works out what handshake state to |
466 | | * move to next when a TLSv1.3 server is writing messages to be sent to the |
467 | | * client. |
468 | | */ |
469 | | static WRITE_TRAN ossl_statem_server13_write_transition(SSL_CONNECTION *s) |
470 | 0 | { |
471 | 0 | OSSL_STATEM *st = &s->statem; |
472 | | |
473 | | /* |
474 | | * No case for TLS_ST_BEFORE, because at that stage we have not negotiated |
475 | | * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition() |
476 | | */ |
477 | |
|
478 | 0 | switch (st->hand_state) { |
479 | 0 | default: |
480 | | /* Shouldn't happen */ |
481 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
482 | 0 | return WRITE_TRAN_ERROR; |
483 | | |
484 | 0 | case TLS_ST_OK: |
485 | 0 | if (s->key_update != SSL_KEY_UPDATE_NONE) { |
486 | 0 | st->hand_state = TLS_ST_SW_KEY_UPDATE; |
487 | 0 | return WRITE_TRAN_CONTINUE; |
488 | 0 | } |
489 | 0 | if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { |
490 | 0 | st->hand_state = TLS_ST_SW_CERT_REQ; |
491 | 0 | return WRITE_TRAN_CONTINUE; |
492 | 0 | } |
493 | 0 | if (s->ext.extra_tickets_expected > 0) { |
494 | 0 | st->hand_state = TLS_ST_SW_SESSION_TICKET; |
495 | 0 | return WRITE_TRAN_CONTINUE; |
496 | 0 | } |
497 | | /* Try to read from the client instead */ |
498 | 0 | return WRITE_TRAN_FINISHED; |
499 | | |
500 | 0 | case TLS_ST_SR_CLNT_HELLO: |
501 | 0 | st->hand_state = TLS_ST_SW_SRVR_HELLO; |
502 | 0 | return WRITE_TRAN_CONTINUE; |
503 | | |
504 | 0 | case TLS_ST_SW_SRVR_HELLO: |
505 | 0 | if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0 |
506 | 0 | && s->hello_retry_request != SSL_HRR_COMPLETE) |
507 | 0 | st->hand_state = TLS_ST_SW_CHANGE; |
508 | 0 | else if (s->hello_retry_request == SSL_HRR_PENDING) |
509 | 0 | st->hand_state = TLS_ST_EARLY_DATA; |
510 | 0 | else |
511 | 0 | st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS; |
512 | 0 | return WRITE_TRAN_CONTINUE; |
513 | | |
514 | 0 | case TLS_ST_SW_CHANGE: |
515 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING) |
516 | 0 | st->hand_state = TLS_ST_EARLY_DATA; |
517 | 0 | else |
518 | 0 | st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS; |
519 | 0 | return WRITE_TRAN_CONTINUE; |
520 | | |
521 | 0 | case TLS_ST_SW_ENCRYPTED_EXTENSIONS: |
522 | 0 | if (s->hit) |
523 | 0 | st->hand_state = TLS_ST_SW_FINISHED; |
524 | 0 | else if (send_certificate_request(s)) |
525 | 0 | st->hand_state = TLS_ST_SW_CERT_REQ; |
526 | 0 | else if (do_compressed_cert(s)) |
527 | 0 | st->hand_state = TLS_ST_SW_COMP_CERT; |
528 | 0 | else |
529 | 0 | st->hand_state = TLS_ST_SW_CERT; |
530 | |
|
531 | 0 | return WRITE_TRAN_CONTINUE; |
532 | | |
533 | 0 | case TLS_ST_SW_CERT_REQ: |
534 | 0 | if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { |
535 | 0 | s->post_handshake_auth = SSL_PHA_REQUESTED; |
536 | 0 | st->hand_state = TLS_ST_OK; |
537 | 0 | } else if (do_compressed_cert(s)) { |
538 | 0 | st->hand_state = TLS_ST_SW_COMP_CERT; |
539 | 0 | } else { |
540 | 0 | st->hand_state = TLS_ST_SW_CERT; |
541 | 0 | } |
542 | 0 | return WRITE_TRAN_CONTINUE; |
543 | | |
544 | 0 | case TLS_ST_SW_COMP_CERT: |
545 | 0 | case TLS_ST_SW_CERT: |
546 | 0 | st->hand_state = TLS_ST_SW_CERT_VRFY; |
547 | 0 | return WRITE_TRAN_CONTINUE; |
548 | | |
549 | 0 | case TLS_ST_SW_CERT_VRFY: |
550 | 0 | st->hand_state = TLS_ST_SW_FINISHED; |
551 | 0 | return WRITE_TRAN_CONTINUE; |
552 | | |
553 | 0 | case TLS_ST_SW_FINISHED: |
554 | 0 | st->hand_state = TLS_ST_EARLY_DATA; |
555 | 0 | s->ts_msg_write = ossl_time_now(); |
556 | 0 | return WRITE_TRAN_CONTINUE; |
557 | | |
558 | 0 | case TLS_ST_EARLY_DATA: |
559 | 0 | return WRITE_TRAN_FINISHED; |
560 | | |
561 | 0 | case TLS_ST_SR_FINISHED: |
562 | 0 | s->ts_msg_read = ossl_time_now(); |
563 | | /* |
564 | | * Technically we have finished the handshake at this point, but we're |
565 | | * going to remain "in_init" for now and write out any session tickets |
566 | | * immediately. |
567 | | */ |
568 | 0 | if (s->post_handshake_auth == SSL_PHA_REQUESTED) { |
569 | 0 | s->post_handshake_auth = SSL_PHA_EXT_RECEIVED; |
570 | 0 | } else if (!s->ext.ticket_expected) { |
571 | | /* |
572 | | * If we're not going to renew the ticket then we just finish the |
573 | | * handshake at this point. |
574 | | */ |
575 | 0 | st->hand_state = TLS_ST_OK; |
576 | 0 | return WRITE_TRAN_CONTINUE; |
577 | 0 | } |
578 | 0 | if (s->num_tickets > s->sent_tickets) |
579 | 0 | st->hand_state = TLS_ST_SW_SESSION_TICKET; |
580 | 0 | else |
581 | 0 | st->hand_state = TLS_ST_OK; |
582 | 0 | return WRITE_TRAN_CONTINUE; |
583 | | |
584 | 0 | case TLS_ST_SR_KEY_UPDATE: |
585 | 0 | case TLS_ST_SW_KEY_UPDATE: |
586 | 0 | st->hand_state = TLS_ST_OK; |
587 | 0 | return WRITE_TRAN_CONTINUE; |
588 | | |
589 | 0 | case TLS_ST_SW_SESSION_TICKET: |
590 | | /* In a resumption we only ever send a maximum of one new ticket. |
591 | | * Following an initial handshake we send the number of tickets we have |
592 | | * been configured for. |
593 | | */ |
594 | 0 | if (!SSL_IS_FIRST_HANDSHAKE(s) && s->ext.extra_tickets_expected > 0) { |
595 | 0 | return WRITE_TRAN_CONTINUE; |
596 | 0 | } else if (s->hit || s->num_tickets <= s->sent_tickets) { |
597 | | /* We've written enough tickets out. */ |
598 | 0 | st->hand_state = TLS_ST_OK; |
599 | 0 | } |
600 | 0 | return WRITE_TRAN_CONTINUE; |
601 | 0 | } |
602 | 0 | } |
603 | | |
604 | | /* |
605 | | * ossl_statem_server_write_transition() works out what handshake state to move |
606 | | * to next when the server is writing messages to be sent to the client. |
607 | | */ |
608 | | WRITE_TRAN ossl_statem_server_write_transition(SSL_CONNECTION *s) |
609 | 0 | { |
610 | 0 | OSSL_STATEM *st = &s->statem; |
611 | | |
612 | | /* |
613 | | * Note that before the ClientHello we don't know what version we are going |
614 | | * to negotiate yet, so we don't take this branch until later |
615 | | */ |
616 | |
|
617 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) |
618 | 0 | return ossl_statem_server13_write_transition(s); |
619 | | |
620 | 0 | switch (st->hand_state) { |
621 | 0 | default: |
622 | | /* Shouldn't happen */ |
623 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
624 | 0 | return WRITE_TRAN_ERROR; |
625 | | |
626 | 0 | case TLS_ST_OK: |
627 | 0 | if (st->request_state == TLS_ST_SW_HELLO_REQ) { |
628 | | /* We must be trying to renegotiate */ |
629 | 0 | st->hand_state = TLS_ST_SW_HELLO_REQ; |
630 | 0 | st->request_state = TLS_ST_BEFORE; |
631 | 0 | return WRITE_TRAN_CONTINUE; |
632 | 0 | } |
633 | | /* Must be an incoming ClientHello */ |
634 | 0 | if (!tls_setup_handshake(s)) { |
635 | | /* SSLfatal() already called */ |
636 | 0 | return WRITE_TRAN_ERROR; |
637 | 0 | } |
638 | | /* Fall through */ |
639 | | |
640 | 0 | case TLS_ST_BEFORE: |
641 | | /* Just go straight to trying to read from the client */ |
642 | 0 | return WRITE_TRAN_FINISHED; |
643 | | |
644 | 0 | case TLS_ST_SW_HELLO_REQ: |
645 | 0 | st->hand_state = TLS_ST_OK; |
646 | 0 | return WRITE_TRAN_CONTINUE; |
647 | | |
648 | 0 | case TLS_ST_SR_CLNT_HELLO: |
649 | 0 | if (SSL_CONNECTION_IS_DTLS(s) && !s->d1->cookie_verified |
650 | 0 | && (SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE)) { |
651 | 0 | st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST; |
652 | 0 | } else if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) { |
653 | | /* We must have rejected the renegotiation */ |
654 | 0 | st->hand_state = TLS_ST_OK; |
655 | 0 | return WRITE_TRAN_CONTINUE; |
656 | 0 | } else { |
657 | 0 | st->hand_state = TLS_ST_SW_SRVR_HELLO; |
658 | 0 | } |
659 | 0 | return WRITE_TRAN_CONTINUE; |
660 | | |
661 | 0 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: |
662 | 0 | return WRITE_TRAN_FINISHED; |
663 | | |
664 | 0 | case TLS_ST_SW_SRVR_HELLO: |
665 | 0 | if (s->hit) { |
666 | 0 | if (s->ext.ticket_expected) |
667 | 0 | st->hand_state = TLS_ST_SW_SESSION_TICKET; |
668 | 0 | else |
669 | 0 | st->hand_state = TLS_ST_SW_CHANGE; |
670 | 0 | } else { |
671 | | /* Check if it is anon DH or anon ECDH, */ |
672 | | /* normal PSK or SRP */ |
673 | 0 | if (!(s->s3.tmp.new_cipher->algorithm_auth & |
674 | 0 | (SSL_aNULL | SSL_aSRP | SSL_aPSK))) { |
675 | 0 | st->hand_state = TLS_ST_SW_CERT; |
676 | 0 | } else if (send_server_key_exchange(s)) { |
677 | 0 | st->hand_state = TLS_ST_SW_KEY_EXCH; |
678 | 0 | } else if (send_certificate_request(s)) { |
679 | 0 | st->hand_state = TLS_ST_SW_CERT_REQ; |
680 | 0 | } else { |
681 | 0 | st->hand_state = TLS_ST_SW_SRVR_DONE; |
682 | 0 | } |
683 | 0 | } |
684 | 0 | return WRITE_TRAN_CONTINUE; |
685 | | |
686 | 0 | case TLS_ST_SW_CERT: |
687 | 0 | if (s->ext.status_expected) { |
688 | 0 | st->hand_state = TLS_ST_SW_CERT_STATUS; |
689 | 0 | return WRITE_TRAN_CONTINUE; |
690 | 0 | } |
691 | | /* Fall through */ |
692 | | |
693 | 0 | case TLS_ST_SW_CERT_STATUS: |
694 | 0 | if (send_server_key_exchange(s)) { |
695 | 0 | st->hand_state = TLS_ST_SW_KEY_EXCH; |
696 | 0 | return WRITE_TRAN_CONTINUE; |
697 | 0 | } |
698 | | /* Fall through */ |
699 | | |
700 | 0 | case TLS_ST_SW_KEY_EXCH: |
701 | 0 | if (send_certificate_request(s)) { |
702 | 0 | st->hand_state = TLS_ST_SW_CERT_REQ; |
703 | 0 | return WRITE_TRAN_CONTINUE; |
704 | 0 | } |
705 | | /* Fall through */ |
706 | | |
707 | 0 | case TLS_ST_SW_CERT_REQ: |
708 | 0 | st->hand_state = TLS_ST_SW_SRVR_DONE; |
709 | 0 | return WRITE_TRAN_CONTINUE; |
710 | | |
711 | 0 | case TLS_ST_SW_SRVR_DONE: |
712 | 0 | s->ts_msg_write = ossl_time_now(); |
713 | 0 | return WRITE_TRAN_FINISHED; |
714 | | |
715 | 0 | case TLS_ST_SR_FINISHED: |
716 | 0 | s->ts_msg_read = ossl_time_now(); |
717 | 0 | if (s->hit) { |
718 | 0 | st->hand_state = TLS_ST_OK; |
719 | 0 | return WRITE_TRAN_CONTINUE; |
720 | 0 | } else if (s->ext.ticket_expected) { |
721 | 0 | st->hand_state = TLS_ST_SW_SESSION_TICKET; |
722 | 0 | } else { |
723 | 0 | st->hand_state = TLS_ST_SW_CHANGE; |
724 | 0 | } |
725 | 0 | return WRITE_TRAN_CONTINUE; |
726 | | |
727 | 0 | case TLS_ST_SW_SESSION_TICKET: |
728 | 0 | st->hand_state = TLS_ST_SW_CHANGE; |
729 | 0 | return WRITE_TRAN_CONTINUE; |
730 | | |
731 | 0 | case TLS_ST_SW_CHANGE: |
732 | 0 | st->hand_state = TLS_ST_SW_FINISHED; |
733 | 0 | return WRITE_TRAN_CONTINUE; |
734 | | |
735 | 0 | case TLS_ST_SW_FINISHED: |
736 | 0 | if (s->hit) { |
737 | 0 | return WRITE_TRAN_FINISHED; |
738 | 0 | } |
739 | 0 | st->hand_state = TLS_ST_OK; |
740 | 0 | return WRITE_TRAN_CONTINUE; |
741 | 0 | } |
742 | 0 | } |
743 | | |
744 | | /* |
745 | | * Perform any pre work that needs to be done prior to sending a message from |
746 | | * the server to the client. |
747 | | */ |
748 | | WORK_STATE ossl_statem_server_pre_work(SSL_CONNECTION *s, WORK_STATE wst) |
749 | 0 | { |
750 | 0 | OSSL_STATEM *st = &s->statem; |
751 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
752 | |
|
753 | 0 | switch (st->hand_state) { |
754 | 0 | default: |
755 | | /* No pre work to be done */ |
756 | 0 | break; |
757 | | |
758 | 0 | case TLS_ST_SW_HELLO_REQ: |
759 | 0 | s->shutdown = 0; |
760 | 0 | if (SSL_CONNECTION_IS_DTLS(s)) |
761 | 0 | dtls1_clear_sent_buffer(s); |
762 | 0 | break; |
763 | | |
764 | 0 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: |
765 | 0 | s->shutdown = 0; |
766 | 0 | if (SSL_CONNECTION_IS_DTLS(s)) { |
767 | 0 | dtls1_clear_sent_buffer(s); |
768 | | /* We don't buffer this message so don't use the timer */ |
769 | 0 | st->use_timer = 0; |
770 | 0 | } |
771 | 0 | break; |
772 | | |
773 | 0 | case TLS_ST_SW_SRVR_HELLO: |
774 | 0 | if (SSL_CONNECTION_IS_DTLS(s)) { |
775 | | /* |
776 | | * Messages we write from now on should be buffered and |
777 | | * retransmitted if necessary, so we need to use the timer now |
778 | | */ |
779 | 0 | st->use_timer = 1; |
780 | 0 | } |
781 | 0 | break; |
782 | | |
783 | 0 | case TLS_ST_SW_SRVR_DONE: |
784 | | #ifndef OPENSSL_NO_SCTP |
785 | | if (SSL_CONNECTION_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(ssl))) { |
786 | | /* Calls SSLfatal() as required */ |
787 | | return dtls_wait_for_dry(s); |
788 | | } |
789 | | #endif |
790 | 0 | return WORK_FINISHED_CONTINUE; |
791 | | |
792 | 0 | case TLS_ST_SW_SESSION_TICKET: |
793 | 0 | if (SSL_CONNECTION_IS_TLS13(s) && s->sent_tickets == 0 |
794 | 0 | && s->ext.extra_tickets_expected == 0) { |
795 | | /* |
796 | | * Actually this is the end of the handshake, but we're going |
797 | | * straight into writing the session ticket out. So we finish off |
798 | | * the handshake, but keep the various buffers active. |
799 | | * |
800 | | * Calls SSLfatal as required. |
801 | | */ |
802 | 0 | return tls_finish_handshake(s, wst, 0, 0); |
803 | 0 | } |
804 | 0 | if (SSL_CONNECTION_IS_DTLS(s)) { |
805 | | /* |
806 | | * We're into the last flight. We don't retransmit the last flight |
807 | | * unless we need to, so we don't use the timer |
808 | | */ |
809 | 0 | st->use_timer = 0; |
810 | 0 | } |
811 | 0 | break; |
812 | | |
813 | 0 | case TLS_ST_SW_CHANGE: |
814 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) |
815 | 0 | break; |
816 | | /* Writes to s->session are only safe for initial handshakes */ |
817 | 0 | if (s->session->cipher == NULL) { |
818 | 0 | s->session->cipher = s->s3.tmp.new_cipher; |
819 | 0 | } else if (s->session->cipher != s->s3.tmp.new_cipher) { |
820 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
821 | 0 | return WORK_ERROR; |
822 | 0 | } |
823 | 0 | if (!ssl->method->ssl3_enc->setup_key_block(s)) { |
824 | | /* SSLfatal() already called */ |
825 | 0 | return WORK_ERROR; |
826 | 0 | } |
827 | 0 | if (SSL_CONNECTION_IS_DTLS(s)) { |
828 | | /* |
829 | | * We're into the last flight. We don't retransmit the last flight |
830 | | * unless we need to, so we don't use the timer. This might have |
831 | | * already been set to 0 if we sent a NewSessionTicket message, |
832 | | * but we'll set it again here in case we didn't. |
833 | | */ |
834 | 0 | st->use_timer = 0; |
835 | 0 | } |
836 | 0 | return WORK_FINISHED_CONTINUE; |
837 | | |
838 | 0 | case TLS_ST_EARLY_DATA: |
839 | 0 | if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING |
840 | 0 | && (s->s3.flags & TLS1_FLAGS_STATELESS) == 0) |
841 | 0 | return WORK_FINISHED_CONTINUE; |
842 | | |
843 | | /* |
844 | | * In QUIC with 0-RTT we just carry on when otherwise we would stop |
845 | | * to allow the server to read early data |
846 | | */ |
847 | 0 | if (SSL_NO_EOED(s) && s->ext.early_data == SSL_EARLY_DATA_ACCEPTED |
848 | 0 | && s->early_data_state != SSL_EARLY_DATA_FINISHED_READING) { |
849 | 0 | s->early_data_state = SSL_EARLY_DATA_FINISHED_READING; |
850 | 0 | if (!ssl->method->ssl3_enc->change_cipher_state(s, SSL3_CC_HANDSHAKE |
851 | 0 | | SSL3_CHANGE_CIPHER_SERVER_READ)) { |
852 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
853 | 0 | return WORK_ERROR; |
854 | 0 | } |
855 | 0 | return WORK_FINISHED_SWAP; |
856 | 0 | } |
857 | | /* Fall through */ |
858 | | |
859 | 0 | case TLS_ST_OK: |
860 | | /* Calls SSLfatal() as required */ |
861 | 0 | return tls_finish_handshake(s, wst, 1, 1); |
862 | 0 | } |
863 | | |
864 | 0 | return WORK_FINISHED_CONTINUE; |
865 | 0 | } |
866 | | |
867 | | static ossl_inline int conn_is_closed(void) |
868 | 0 | { |
869 | 0 | switch (get_last_sys_error()) { |
870 | 0 | #if defined(EPIPE) |
871 | 0 | case EPIPE: |
872 | 0 | return 1; |
873 | 0 | #endif |
874 | 0 | #if defined(ECONNRESET) |
875 | 0 | case ECONNRESET: |
876 | 0 | return 1; |
877 | 0 | #endif |
878 | | #if defined(WSAECONNRESET) |
879 | | case WSAECONNRESET: |
880 | | return 1; |
881 | | #endif |
882 | 0 | default: |
883 | 0 | return 0; |
884 | 0 | } |
885 | 0 | } |
886 | | |
887 | | /* |
888 | | * Perform any work that needs to be done after sending a message from the |
889 | | * server to the client. |
890 | | */ |
891 | | WORK_STATE ossl_statem_server_post_work(SSL_CONNECTION *s, WORK_STATE wst) |
892 | 0 | { |
893 | 0 | OSSL_STATEM *st = &s->statem; |
894 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
895 | |
|
896 | 0 | s->init_num = 0; |
897 | |
|
898 | 0 | switch (st->hand_state) { |
899 | 0 | default: |
900 | | /* No post work to be done */ |
901 | 0 | break; |
902 | | |
903 | 0 | case TLS_ST_SW_HELLO_REQ: |
904 | 0 | if (statem_flush(s) != 1) |
905 | 0 | return WORK_MORE_A; |
906 | 0 | if (!ssl3_init_finished_mac(s)) { |
907 | | /* SSLfatal() already called */ |
908 | 0 | return WORK_ERROR; |
909 | 0 | } |
910 | 0 | break; |
911 | | |
912 | 0 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: |
913 | 0 | if (statem_flush(s) != 1) |
914 | 0 | return WORK_MORE_A; |
915 | | /* HelloVerifyRequest resets Finished MAC */ |
916 | 0 | if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) { |
917 | | /* SSLfatal() already called */ |
918 | 0 | return WORK_ERROR; |
919 | 0 | } |
920 | | /* |
921 | | * The next message should be another ClientHello which we need to |
922 | | * treat like it was the first packet |
923 | | */ |
924 | 0 | s->first_packet = 1; |
925 | 0 | break; |
926 | | |
927 | 0 | case TLS_ST_SW_SRVR_HELLO: |
928 | 0 | if (SSL_CONNECTION_IS_TLS13(s) |
929 | 0 | && s->hello_retry_request == SSL_HRR_PENDING) { |
930 | 0 | if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0 |
931 | 0 | && statem_flush(s) != 1) |
932 | 0 | return WORK_MORE_A; |
933 | 0 | break; |
934 | 0 | } |
935 | | #ifndef OPENSSL_NO_SCTP |
936 | | if (SSL_CONNECTION_IS_DTLS(s) && s->hit) { |
937 | | unsigned char sctpauthkey[64]; |
938 | | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; |
939 | | size_t labellen; |
940 | | |
941 | | /* |
942 | | * Add new shared key for SCTP-Auth, will be ignored if no |
943 | | * SCTP used. |
944 | | */ |
945 | | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
946 | | sizeof(DTLS1_SCTP_AUTH_LABEL)); |
947 | | |
948 | | /* Don't include the terminating zero. */ |
949 | | labellen = sizeof(labelbuffer) - 1; |
950 | | if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG) |
951 | | labellen += 1; |
952 | | |
953 | | if (SSL_export_keying_material(ssl, sctpauthkey, |
954 | | sizeof(sctpauthkey), labelbuffer, |
955 | | labellen, NULL, 0, |
956 | | 0) <= 0) { |
957 | | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
958 | | return WORK_ERROR; |
959 | | } |
960 | | |
961 | | BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, |
962 | | sizeof(sctpauthkey), sctpauthkey); |
963 | | } |
964 | | #endif |
965 | 0 | if (!SSL_CONNECTION_IS_TLS13(s) |
966 | 0 | || ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0 |
967 | 0 | && s->hello_retry_request != SSL_HRR_COMPLETE)) |
968 | 0 | break; |
969 | | /* Fall through */ |
970 | | |
971 | 0 | case TLS_ST_SW_CHANGE: |
972 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING) { |
973 | 0 | if (!statem_flush(s)) |
974 | 0 | return WORK_MORE_A; |
975 | 0 | break; |
976 | 0 | } |
977 | | |
978 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
979 | 0 | if (!ssl->method->ssl3_enc->setup_key_block(s) |
980 | 0 | || !tls13_store_handshake_traffic_hash(s) |
981 | 0 | || !ssl->method->ssl3_enc->change_cipher_state(s, |
982 | 0 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) { |
983 | | /* SSLfatal() already called */ |
984 | 0 | return WORK_ERROR; |
985 | 0 | } |
986 | | |
987 | 0 | if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED |
988 | 0 | && !ssl->method->ssl3_enc->change_cipher_state(s, |
989 | 0 | SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ)) { |
990 | | /* SSLfatal() already called */ |
991 | 0 | return WORK_ERROR; |
992 | 0 | } |
993 | | /* |
994 | | * We don't yet know whether the next record we are going to receive |
995 | | * is an unencrypted alert, an encrypted alert, or an encrypted |
996 | | * handshake message. We temporarily tolerate unencrypted alerts. |
997 | | */ |
998 | 0 | if (s->rlayer.rrlmethod->set_plain_alerts != NULL) |
999 | 0 | s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 1); |
1000 | 0 | break; |
1001 | 0 | } |
1002 | | |
1003 | | #ifndef OPENSSL_NO_SCTP |
1004 | | if (SSL_CONNECTION_IS_DTLS(s) && !s->hit) { |
1005 | | /* |
1006 | | * Change to new shared key of SCTP-Auth, will be ignored if |
1007 | | * no SCTP used. |
1008 | | */ |
1009 | | BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, |
1010 | | 0, NULL); |
1011 | | } |
1012 | | #endif |
1013 | 0 | if (!ssl->method->ssl3_enc->change_cipher_state(s, |
1014 | 0 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) { |
1015 | | /* SSLfatal() already called */ |
1016 | 0 | return WORK_ERROR; |
1017 | 0 | } |
1018 | 0 | break; |
1019 | | |
1020 | 0 | case TLS_ST_SW_SRVR_DONE: |
1021 | 0 | if (statem_flush(s) != 1) |
1022 | 0 | return WORK_MORE_A; |
1023 | 0 | break; |
1024 | | |
1025 | 0 | case TLS_ST_SW_FINISHED: |
1026 | 0 | if (statem_flush(s) != 1) |
1027 | 0 | return WORK_MORE_A; |
1028 | | #ifndef OPENSSL_NO_SCTP |
1029 | | if (SSL_CONNECTION_IS_DTLS(s) && s->hit) { |
1030 | | /* |
1031 | | * Change to new shared key of SCTP-Auth, will be ignored if |
1032 | | * no SCTP used. |
1033 | | */ |
1034 | | BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, |
1035 | | 0, NULL); |
1036 | | } |
1037 | | #endif |
1038 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
1039 | | /* TLS 1.3 gets the secret size from the handshake md */ |
1040 | 0 | size_t dummy; |
1041 | 0 | if (!ssl->method->ssl3_enc->generate_master_secret(s, |
1042 | 0 | s->master_secret, s->handshake_secret, 0, |
1043 | 0 | &dummy) |
1044 | 0 | || !tls13_store_server_finished_hash(s) |
1045 | 0 | || !ssl->method->ssl3_enc->change_cipher_state(s, |
1046 | 0 | SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE)) |
1047 | | /* SSLfatal() already called */ |
1048 | 0 | return WORK_ERROR; |
1049 | 0 | } |
1050 | 0 | break; |
1051 | | |
1052 | 0 | case TLS_ST_SW_CERT_REQ: |
1053 | 0 | if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { |
1054 | 0 | if (statem_flush(s) != 1) |
1055 | 0 | return WORK_MORE_A; |
1056 | 0 | } else { |
1057 | 0 | if (!SSL_CONNECTION_IS_TLS13(s) |
1058 | 0 | || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0) |
1059 | 0 | s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none; |
1060 | 0 | } |
1061 | 0 | break; |
1062 | | |
1063 | 0 | case TLS_ST_SW_ENCRYPTED_EXTENSIONS: |
1064 | 0 | if (!s->hit && !send_certificate_request(s)) { |
1065 | 0 | if (!SSL_CONNECTION_IS_TLS13(s) |
1066 | 0 | || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0) |
1067 | 0 | s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none; |
1068 | 0 | } |
1069 | 0 | break; |
1070 | | |
1071 | 0 | case TLS_ST_SW_KEY_UPDATE: |
1072 | 0 | if (statem_flush(s) != 1) |
1073 | 0 | return WORK_MORE_A; |
1074 | 0 | if (!tls13_update_key(s, 1)) { |
1075 | | /* SSLfatal() already called */ |
1076 | 0 | return WORK_ERROR; |
1077 | 0 | } |
1078 | 0 | break; |
1079 | | |
1080 | 0 | case TLS_ST_SW_SESSION_TICKET: |
1081 | 0 | clear_sys_error(); |
1082 | 0 | if (SSL_CONNECTION_IS_TLS13(s) && statem_flush(s) != 1) { |
1083 | 0 | if (SSL_get_error(ssl, 0) == SSL_ERROR_SYSCALL |
1084 | 0 | && conn_is_closed()) { |
1085 | | /* |
1086 | | * We ignore connection closed errors in TLSv1.3 when sending a |
1087 | | * NewSessionTicket and behave as if we were successful. This is |
1088 | | * so that we are still able to read data sent to us by a client |
1089 | | * that closes soon after the end of the handshake without |
1090 | | * waiting to read our post-handshake NewSessionTickets. |
1091 | | */ |
1092 | 0 | s->rwstate = SSL_NOTHING; |
1093 | 0 | break; |
1094 | 0 | } |
1095 | | |
1096 | 0 | return WORK_MORE_A; |
1097 | 0 | } |
1098 | 0 | break; |
1099 | 0 | } |
1100 | | |
1101 | 0 | return WORK_FINISHED_CONTINUE; |
1102 | 0 | } |
1103 | | |
1104 | | /* |
1105 | | * Get the message construction function and message type for sending from the |
1106 | | * server |
1107 | | * |
1108 | | * Valid return values are: |
1109 | | * 1: Success |
1110 | | * 0: Error |
1111 | | */ |
1112 | | int ossl_statem_server_construct_message(SSL_CONNECTION *s, |
1113 | | confunc_f *confunc, int *mt) |
1114 | 0 | { |
1115 | 0 | OSSL_STATEM *st = &s->statem; |
1116 | |
|
1117 | 0 | switch (st->hand_state) { |
1118 | 0 | default: |
1119 | | /* Shouldn't happen */ |
1120 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE); |
1121 | 0 | return 0; |
1122 | | |
1123 | 0 | case TLS_ST_SW_CHANGE: |
1124 | 0 | if (SSL_CONNECTION_IS_DTLS(s)) |
1125 | 0 | *confunc = dtls_construct_change_cipher_spec; |
1126 | 0 | else |
1127 | 0 | *confunc = tls_construct_change_cipher_spec; |
1128 | 0 | *mt = SSL3_MT_CHANGE_CIPHER_SPEC; |
1129 | 0 | break; |
1130 | | |
1131 | 0 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: |
1132 | 0 | *confunc = dtls_construct_hello_verify_request; |
1133 | 0 | *mt = DTLS1_MT_HELLO_VERIFY_REQUEST; |
1134 | 0 | break; |
1135 | | |
1136 | 0 | case TLS_ST_SW_HELLO_REQ: |
1137 | | /* No construction function needed */ |
1138 | 0 | *confunc = NULL; |
1139 | 0 | *mt = SSL3_MT_HELLO_REQUEST; |
1140 | 0 | break; |
1141 | | |
1142 | 0 | case TLS_ST_SW_SRVR_HELLO: |
1143 | 0 | *confunc = tls_construct_server_hello; |
1144 | 0 | *mt = SSL3_MT_SERVER_HELLO; |
1145 | 0 | break; |
1146 | | |
1147 | 0 | case TLS_ST_SW_CERT: |
1148 | 0 | *confunc = tls_construct_server_certificate; |
1149 | 0 | *mt = SSL3_MT_CERTIFICATE; |
1150 | 0 | break; |
1151 | | |
1152 | | #ifndef OPENSSL_NO_COMP_ALG |
1153 | | case TLS_ST_SW_COMP_CERT: |
1154 | | *confunc = tls_construct_server_compressed_certificate; |
1155 | | *mt = SSL3_MT_COMPRESSED_CERTIFICATE; |
1156 | | break; |
1157 | | #endif |
1158 | | |
1159 | 0 | case TLS_ST_SW_CERT_VRFY: |
1160 | 0 | *confunc = tls_construct_cert_verify; |
1161 | 0 | *mt = SSL3_MT_CERTIFICATE_VERIFY; |
1162 | 0 | break; |
1163 | | |
1164 | | |
1165 | 0 | case TLS_ST_SW_KEY_EXCH: |
1166 | 0 | *confunc = tls_construct_server_key_exchange; |
1167 | 0 | *mt = SSL3_MT_SERVER_KEY_EXCHANGE; |
1168 | 0 | break; |
1169 | | |
1170 | 0 | case TLS_ST_SW_CERT_REQ: |
1171 | 0 | *confunc = tls_construct_certificate_request; |
1172 | 0 | *mt = SSL3_MT_CERTIFICATE_REQUEST; |
1173 | 0 | break; |
1174 | | |
1175 | 0 | case TLS_ST_SW_SRVR_DONE: |
1176 | 0 | *confunc = tls_construct_server_done; |
1177 | 0 | *mt = SSL3_MT_SERVER_DONE; |
1178 | 0 | break; |
1179 | | |
1180 | 0 | case TLS_ST_SW_SESSION_TICKET: |
1181 | 0 | *confunc = tls_construct_new_session_ticket; |
1182 | 0 | *mt = SSL3_MT_NEWSESSION_TICKET; |
1183 | 0 | break; |
1184 | | |
1185 | 0 | case TLS_ST_SW_CERT_STATUS: |
1186 | 0 | *confunc = tls_construct_cert_status; |
1187 | 0 | *mt = SSL3_MT_CERTIFICATE_STATUS; |
1188 | 0 | break; |
1189 | | |
1190 | 0 | case TLS_ST_SW_FINISHED: |
1191 | 0 | *confunc = tls_construct_finished; |
1192 | 0 | *mt = SSL3_MT_FINISHED; |
1193 | 0 | break; |
1194 | | |
1195 | 0 | case TLS_ST_EARLY_DATA: |
1196 | 0 | *confunc = NULL; |
1197 | 0 | *mt = SSL3_MT_DUMMY; |
1198 | 0 | break; |
1199 | | |
1200 | 0 | case TLS_ST_SW_ENCRYPTED_EXTENSIONS: |
1201 | 0 | *confunc = tls_construct_encrypted_extensions; |
1202 | 0 | *mt = SSL3_MT_ENCRYPTED_EXTENSIONS; |
1203 | 0 | break; |
1204 | | |
1205 | 0 | case TLS_ST_SW_KEY_UPDATE: |
1206 | 0 | *confunc = tls_construct_key_update; |
1207 | 0 | *mt = SSL3_MT_KEY_UPDATE; |
1208 | 0 | break; |
1209 | 0 | } |
1210 | | |
1211 | 0 | return 1; |
1212 | 0 | } |
1213 | | |
1214 | | /* |
1215 | | * Maximum size (excluding the Handshake header) of a ClientHello message, |
1216 | | * calculated as follows: |
1217 | | * |
1218 | | * 2 + # client_version |
1219 | | * 32 + # only valid length for random |
1220 | | * 1 + # length of session_id |
1221 | | * 32 + # maximum size for session_id |
1222 | | * 2 + # length of cipher suites |
1223 | | * 2^16-2 + # maximum length of cipher suites array |
1224 | | * 1 + # length of compression_methods |
1225 | | * 2^8-1 + # maximum length of compression methods |
1226 | | * 2 + # length of extensions |
1227 | | * 2^16-1 # maximum length of extensions |
1228 | | */ |
1229 | 0 | #define CLIENT_HELLO_MAX_LENGTH 131396 |
1230 | | |
1231 | 0 | #define CLIENT_KEY_EXCH_MAX_LENGTH 2048 |
1232 | 0 | #define NEXT_PROTO_MAX_LENGTH 514 |
1233 | | |
1234 | | /* |
1235 | | * Returns the maximum allowed length for the current message that we are |
1236 | | * reading. Excludes the message header. |
1237 | | */ |
1238 | | size_t ossl_statem_server_max_message_size(SSL_CONNECTION *s) |
1239 | 0 | { |
1240 | 0 | OSSL_STATEM *st = &s->statem; |
1241 | |
|
1242 | 0 | switch (st->hand_state) { |
1243 | 0 | default: |
1244 | | /* Shouldn't happen */ |
1245 | 0 | return 0; |
1246 | | |
1247 | 0 | case TLS_ST_SR_CLNT_HELLO: |
1248 | 0 | return CLIENT_HELLO_MAX_LENGTH; |
1249 | | |
1250 | 0 | case TLS_ST_SR_END_OF_EARLY_DATA: |
1251 | 0 | return END_OF_EARLY_DATA_MAX_LENGTH; |
1252 | | |
1253 | 0 | case TLS_ST_SR_COMP_CERT: |
1254 | 0 | case TLS_ST_SR_CERT: |
1255 | 0 | return s->max_cert_list; |
1256 | | |
1257 | 0 | case TLS_ST_SR_KEY_EXCH: |
1258 | 0 | return CLIENT_KEY_EXCH_MAX_LENGTH; |
1259 | | |
1260 | 0 | case TLS_ST_SR_CERT_VRFY: |
1261 | 0 | return CERTIFICATE_VERIFY_MAX_LENGTH; |
1262 | | |
1263 | 0 | #ifndef OPENSSL_NO_NEXTPROTONEG |
1264 | 0 | case TLS_ST_SR_NEXT_PROTO: |
1265 | 0 | return NEXT_PROTO_MAX_LENGTH; |
1266 | 0 | #endif |
1267 | | |
1268 | 0 | case TLS_ST_SR_CHANGE: |
1269 | 0 | return CCS_MAX_LENGTH; |
1270 | | |
1271 | 0 | case TLS_ST_SR_FINISHED: |
1272 | 0 | return FINISHED_MAX_LENGTH; |
1273 | | |
1274 | 0 | case TLS_ST_SR_KEY_UPDATE: |
1275 | 0 | return KEY_UPDATE_MAX_LENGTH; |
1276 | 0 | } |
1277 | 0 | } |
1278 | | |
1279 | | /* |
1280 | | * Process a message that the server has received from the client. |
1281 | | */ |
1282 | | MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL_CONNECTION *s, |
1283 | | PACKET *pkt) |
1284 | 0 | { |
1285 | 0 | OSSL_STATEM *st = &s->statem; |
1286 | |
|
1287 | 0 | switch (st->hand_state) { |
1288 | 0 | default: |
1289 | | /* Shouldn't happen */ |
1290 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1291 | 0 | return MSG_PROCESS_ERROR; |
1292 | | |
1293 | 0 | case TLS_ST_SR_CLNT_HELLO: |
1294 | 0 | return tls_process_client_hello(s, pkt); |
1295 | | |
1296 | 0 | case TLS_ST_SR_END_OF_EARLY_DATA: |
1297 | 0 | return tls_process_end_of_early_data(s, pkt); |
1298 | | |
1299 | 0 | case TLS_ST_SR_CERT: |
1300 | 0 | return tls_process_client_certificate(s, pkt); |
1301 | | |
1302 | | #ifndef OPENSSL_NO_COMP_ALG |
1303 | | case TLS_ST_SR_COMP_CERT: |
1304 | | return tls_process_client_compressed_certificate(s, pkt); |
1305 | | #endif |
1306 | | |
1307 | 0 | case TLS_ST_SR_KEY_EXCH: |
1308 | 0 | return tls_process_client_key_exchange(s, pkt); |
1309 | | |
1310 | 0 | case TLS_ST_SR_CERT_VRFY: |
1311 | 0 | return tls_process_cert_verify(s, pkt); |
1312 | | |
1313 | 0 | #ifndef OPENSSL_NO_NEXTPROTONEG |
1314 | 0 | case TLS_ST_SR_NEXT_PROTO: |
1315 | 0 | return tls_process_next_proto(s, pkt); |
1316 | 0 | #endif |
1317 | | |
1318 | 0 | case TLS_ST_SR_CHANGE: |
1319 | 0 | return tls_process_change_cipher_spec(s, pkt); |
1320 | | |
1321 | 0 | case TLS_ST_SR_FINISHED: |
1322 | 0 | return tls_process_finished(s, pkt); |
1323 | | |
1324 | 0 | case TLS_ST_SR_KEY_UPDATE: |
1325 | 0 | return tls_process_key_update(s, pkt); |
1326 | |
|
1327 | 0 | } |
1328 | 0 | } |
1329 | | |
1330 | | /* |
1331 | | * Perform any further processing required following the receipt of a message |
1332 | | * from the client |
1333 | | */ |
1334 | | WORK_STATE ossl_statem_server_post_process_message(SSL_CONNECTION *s, |
1335 | | WORK_STATE wst) |
1336 | 0 | { |
1337 | 0 | OSSL_STATEM *st = &s->statem; |
1338 | |
|
1339 | 0 | switch (st->hand_state) { |
1340 | 0 | default: |
1341 | | /* Shouldn't happen */ |
1342 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1343 | 0 | return WORK_ERROR; |
1344 | | |
1345 | 0 | case TLS_ST_SR_CLNT_HELLO: |
1346 | 0 | return tls_post_process_client_hello(s, wst); |
1347 | | |
1348 | 0 | case TLS_ST_SR_KEY_EXCH: |
1349 | 0 | return tls_post_process_client_key_exchange(s, wst); |
1350 | 0 | } |
1351 | 0 | } |
1352 | | |
1353 | | #ifndef OPENSSL_NO_SRP |
1354 | | /* Returns 1 on success, 0 for retryable error, -1 for fatal error */ |
1355 | | static int ssl_check_srp_ext_ClientHello(SSL_CONNECTION *s) |
1356 | 0 | { |
1357 | 0 | int ret; |
1358 | 0 | int al = SSL_AD_UNRECOGNIZED_NAME; |
1359 | |
|
1360 | 0 | if ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) && |
1361 | 0 | (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) { |
1362 | 0 | if (s->srp_ctx.login == NULL) { |
1363 | | /* |
1364 | | * RFC 5054 says SHOULD reject, we do so if There is no srp |
1365 | | * login name |
1366 | | */ |
1367 | 0 | SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY, |
1368 | 0 | SSL_R_PSK_IDENTITY_NOT_FOUND); |
1369 | 0 | return -1; |
1370 | 0 | } else { |
1371 | 0 | ret = ssl_srp_server_param_with_username_intern(s, &al); |
1372 | 0 | if (ret < 0) |
1373 | 0 | return 0; |
1374 | 0 | if (ret == SSL3_AL_FATAL) { |
1375 | 0 | SSLfatal(s, al, |
1376 | 0 | al == SSL_AD_UNKNOWN_PSK_IDENTITY |
1377 | 0 | ? SSL_R_PSK_IDENTITY_NOT_FOUND |
1378 | 0 | : SSL_R_CLIENTHELLO_TLSEXT); |
1379 | 0 | return -1; |
1380 | 0 | } |
1381 | 0 | } |
1382 | 0 | } |
1383 | 0 | return 1; |
1384 | 0 | } |
1385 | | #endif |
1386 | | |
1387 | | int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie, |
1388 | | size_t cookie_len) |
1389 | 0 | { |
1390 | | /* Always use DTLS 1.0 version: see RFC 6347 */ |
1391 | 0 | if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION) |
1392 | 0 | || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len)) |
1393 | 0 | return 0; |
1394 | | |
1395 | 0 | return 1; |
1396 | 0 | } |
1397 | | |
1398 | | CON_FUNC_RETURN dtls_construct_hello_verify_request(SSL_CONNECTION *s, |
1399 | | WPACKET *pkt) |
1400 | 0 | { |
1401 | 0 | unsigned int cookie_leni; |
1402 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1403 | |
|
1404 | 0 | if (sctx->app_gen_cookie_cb == NULL |
1405 | 0 | || sctx->app_gen_cookie_cb(SSL_CONNECTION_GET_USER_SSL(s), s->d1->cookie, |
1406 | 0 | &cookie_leni) == 0 |
1407 | 0 | || cookie_leni > DTLS1_COOKIE_LENGTH) { |
1408 | 0 | SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_COOKIE_GEN_CALLBACK_FAILURE); |
1409 | 0 | return CON_FUNC_ERROR; |
1410 | 0 | } |
1411 | 0 | s->d1->cookie_len = cookie_leni; |
1412 | |
|
1413 | 0 | if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie, |
1414 | 0 | s->d1->cookie_len)) { |
1415 | 0 | SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR); |
1416 | 0 | return CON_FUNC_ERROR; |
1417 | 0 | } |
1418 | | |
1419 | 0 | return CON_FUNC_SUCCESS; |
1420 | 0 | } |
1421 | | |
1422 | | /*- |
1423 | | * ssl_check_for_safari attempts to fingerprint Safari using OS X |
1424 | | * SecureTransport using the TLS extension block in |hello|. |
1425 | | * Safari, since 10.6, sends exactly these extensions, in this order: |
1426 | | * SNI, |
1427 | | * elliptic_curves |
1428 | | * ec_point_formats |
1429 | | * signature_algorithms (for TLSv1.2 only) |
1430 | | * |
1431 | | * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8, |
1432 | | * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them. |
1433 | | * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from |
1434 | | * 10.8..10.8.3 (which don't work). |
1435 | | */ |
1436 | | static void ssl_check_for_safari(SSL_CONNECTION *s, |
1437 | | const CLIENTHELLO_MSG *hello) |
1438 | 0 | { |
1439 | 0 | static const unsigned char kSafariExtensionsBlock[] = { |
1440 | 0 | 0x00, 0x0a, /* elliptic_curves extension */ |
1441 | 0 | 0x00, 0x08, /* 8 bytes */ |
1442 | 0 | 0x00, 0x06, /* 6 bytes of curve ids */ |
1443 | 0 | 0x00, 0x17, /* P-256 */ |
1444 | 0 | 0x00, 0x18, /* P-384 */ |
1445 | 0 | 0x00, 0x19, /* P-521 */ |
1446 | |
|
1447 | 0 | 0x00, 0x0b, /* ec_point_formats */ |
1448 | 0 | 0x00, 0x02, /* 2 bytes */ |
1449 | 0 | 0x01, /* 1 point format */ |
1450 | 0 | 0x00, /* uncompressed */ |
1451 | | /* The following is only present in TLS 1.2 */ |
1452 | 0 | 0x00, 0x0d, /* signature_algorithms */ |
1453 | 0 | 0x00, 0x0c, /* 12 bytes */ |
1454 | 0 | 0x00, 0x0a, /* 10 bytes */ |
1455 | 0 | 0x05, 0x01, /* SHA-384/RSA */ |
1456 | 0 | 0x04, 0x01, /* SHA-256/RSA */ |
1457 | 0 | 0x02, 0x01, /* SHA-1/RSA */ |
1458 | 0 | 0x04, 0x03, /* SHA-256/ECDSA */ |
1459 | 0 | 0x02, 0x03, /* SHA-1/ECDSA */ |
1460 | 0 | }; |
1461 | | /* Length of the common prefix (first two extensions). */ |
1462 | 0 | static const size_t kSafariCommonExtensionsLength = 18; |
1463 | 0 | unsigned int type; |
1464 | 0 | PACKET sni, tmppkt; |
1465 | 0 | size_t ext_len; |
1466 | |
|
1467 | 0 | tmppkt = hello->extensions; |
1468 | |
|
1469 | 0 | if (!PACKET_forward(&tmppkt, 2) |
1470 | 0 | || !PACKET_get_net_2(&tmppkt, &type) |
1471 | 0 | || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) { |
1472 | 0 | return; |
1473 | 0 | } |
1474 | | |
1475 | 0 | if (type != TLSEXT_TYPE_server_name) |
1476 | 0 | return; |
1477 | | |
1478 | 0 | ext_len = TLS1_get_client_version( |
1479 | 0 | SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION ? |
1480 | 0 | sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength; |
1481 | |
|
1482 | 0 | s->s3.is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock, |
1483 | 0 | ext_len); |
1484 | 0 | } |
1485 | | |
1486 | | #define RENEG_OPTIONS_OK(options) \ |
1487 | 0 | ((options & SSL_OP_NO_RENEGOTIATION) == 0 \ |
1488 | 0 | && (options & SSL_OP_ALLOW_CLIENT_RENEGOTIATION) != 0) |
1489 | | |
1490 | | MSG_PROCESS_RETURN tls_process_client_hello(SSL_CONNECTION *s, PACKET *pkt) |
1491 | 0 | { |
1492 | | /* |cookie| will only be initialized for DTLS. */ |
1493 | 0 | PACKET session_id, compression, extensions, cookie; |
1494 | 0 | static const unsigned char null_compression = 0; |
1495 | 0 | CLIENTHELLO_MSG *clienthello = NULL; |
1496 | | |
1497 | | /* Check if this is actually an unexpected renegotiation ClientHello */ |
1498 | 0 | if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) { |
1499 | 0 | if (!ossl_assert(!SSL_CONNECTION_IS_TLS13(s))) { |
1500 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1501 | 0 | goto err; |
1502 | 0 | } |
1503 | 0 | if (!RENEG_OPTIONS_OK(s->options) |
1504 | 0 | || (!s->s3.send_connection_binding |
1505 | 0 | && (s->options |
1506 | 0 | & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) == 0)) { |
1507 | 0 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION); |
1508 | 0 | return MSG_PROCESS_FINISHED_READING; |
1509 | 0 | } |
1510 | 0 | s->renegotiate = 1; |
1511 | 0 | s->new_session = 1; |
1512 | 0 | } |
1513 | | |
1514 | 0 | clienthello = OPENSSL_zalloc(sizeof(*clienthello)); |
1515 | 0 | if (clienthello == NULL) { |
1516 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1517 | 0 | goto err; |
1518 | 0 | } |
1519 | | |
1520 | | /* |
1521 | | * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure. |
1522 | | */ |
1523 | 0 | clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer); |
1524 | 0 | PACKET_null_init(&cookie); |
1525 | |
|
1526 | 0 | if (clienthello->isv2) { |
1527 | 0 | unsigned int mt; |
1528 | |
|
1529 | 0 | if (!SSL_IS_FIRST_HANDSHAKE(s) |
1530 | 0 | || s->hello_retry_request != SSL_HRR_NONE) { |
1531 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
1532 | 0 | goto err; |
1533 | 0 | } |
1534 | | |
1535 | | /*- |
1536 | | * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2 |
1537 | | * header is sent directly on the wire, not wrapped as a TLS |
1538 | | * record. Our record layer just processes the message length and passes |
1539 | | * the rest right through. Its format is: |
1540 | | * Byte Content |
1541 | | * 0-1 msg_length - decoded by the record layer |
1542 | | * 2 msg_type - s->init_msg points here |
1543 | | * 3-4 version |
1544 | | * 5-6 cipher_spec_length |
1545 | | * 7-8 session_id_length |
1546 | | * 9-10 challenge_length |
1547 | | * ... ... |
1548 | | */ |
1549 | | |
1550 | 0 | if (!PACKET_get_1(pkt, &mt) |
1551 | 0 | || mt != SSL2_MT_CLIENT_HELLO) { |
1552 | | /* |
1553 | | * Should never happen. We should have tested this in the record |
1554 | | * layer in order to have determined that this is an SSLv2 record |
1555 | | * in the first place |
1556 | | */ |
1557 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1558 | 0 | goto err; |
1559 | 0 | } |
1560 | 0 | } |
1561 | | |
1562 | 0 | if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) { |
1563 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT); |
1564 | 0 | goto err; |
1565 | 0 | } |
1566 | | |
1567 | | /* Parse the message and load client random. */ |
1568 | 0 | if (clienthello->isv2) { |
1569 | | /* |
1570 | | * Handle an SSLv2 backwards compatible ClientHello |
1571 | | * Note, this is only for SSLv3+ using the backward compatible format. |
1572 | | * Real SSLv2 is not supported, and is rejected below. |
1573 | | */ |
1574 | 0 | unsigned int ciphersuite_len, session_id_len, challenge_len; |
1575 | 0 | PACKET challenge; |
1576 | |
|
1577 | 0 | if (!PACKET_get_net_2(pkt, &ciphersuite_len) |
1578 | 0 | || !PACKET_get_net_2(pkt, &session_id_len) |
1579 | 0 | || !PACKET_get_net_2(pkt, &challenge_len)) { |
1580 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH); |
1581 | 0 | goto err; |
1582 | 0 | } |
1583 | | |
1584 | 0 | if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
1585 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_MISMATCH); |
1586 | 0 | goto err; |
1587 | 0 | } |
1588 | | |
1589 | 0 | if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites, |
1590 | 0 | ciphersuite_len) |
1591 | 0 | || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len) |
1592 | 0 | || !PACKET_get_sub_packet(pkt, &challenge, challenge_len) |
1593 | | /* No extensions. */ |
1594 | 0 | || PACKET_remaining(pkt) != 0) { |
1595 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH); |
1596 | 0 | goto err; |
1597 | 0 | } |
1598 | 0 | clienthello->session_id_len = session_id_len; |
1599 | | |
1600 | | /* Load the client random and compression list. We use SSL3_RANDOM_SIZE |
1601 | | * here rather than sizeof(clienthello->random) because that is the limit |
1602 | | * for SSLv3 and it is fixed. It won't change even if |
1603 | | * sizeof(clienthello->random) does. |
1604 | | */ |
1605 | 0 | challenge_len = challenge_len > SSL3_RANDOM_SIZE |
1606 | 0 | ? SSL3_RANDOM_SIZE : challenge_len; |
1607 | 0 | memset(clienthello->random, 0, SSL3_RANDOM_SIZE); |
1608 | 0 | if (!PACKET_copy_bytes(&challenge, |
1609 | 0 | clienthello->random + SSL3_RANDOM_SIZE - |
1610 | 0 | challenge_len, challenge_len) |
1611 | | /* Advertise only null compression. */ |
1612 | 0 | || !PACKET_buf_init(&compression, &null_compression, 1)) { |
1613 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1614 | 0 | goto err; |
1615 | 0 | } |
1616 | | |
1617 | 0 | PACKET_null_init(&clienthello->extensions); |
1618 | 0 | } else { |
1619 | | /* Regular ClientHello. */ |
1620 | 0 | if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE) |
1621 | 0 | || !PACKET_get_length_prefixed_1(pkt, &session_id) |
1622 | 0 | || !PACKET_copy_all(&session_id, clienthello->session_id, |
1623 | 0 | SSL_MAX_SSL_SESSION_ID_LENGTH, |
1624 | 0 | &clienthello->session_id_len)) { |
1625 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1626 | 0 | goto err; |
1627 | 0 | } |
1628 | | |
1629 | 0 | if (SSL_CONNECTION_IS_DTLS(s)) { |
1630 | 0 | if (!PACKET_get_length_prefixed_1(pkt, &cookie)) { |
1631 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1632 | 0 | goto err; |
1633 | 0 | } |
1634 | 0 | if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie, |
1635 | 0 | DTLS1_COOKIE_LENGTH, |
1636 | 0 | &clienthello->dtls_cookie_len)) { |
1637 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1638 | 0 | goto err; |
1639 | 0 | } |
1640 | | /* |
1641 | | * If we require cookies and this ClientHello doesn't contain one, |
1642 | | * just return since we do not want to allocate any memory yet. |
1643 | | * So check cookie length... |
1644 | | */ |
1645 | 0 | if (SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE) { |
1646 | 0 | if (clienthello->dtls_cookie_len == 0) { |
1647 | 0 | OPENSSL_free(clienthello); |
1648 | 0 | return MSG_PROCESS_FINISHED_READING; |
1649 | 0 | } |
1650 | 0 | } |
1651 | 0 | } |
1652 | | |
1653 | 0 | if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) { |
1654 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1655 | 0 | goto err; |
1656 | 0 | } |
1657 | | |
1658 | 0 | if (!PACKET_get_length_prefixed_1(pkt, &compression)) { |
1659 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1660 | 0 | goto err; |
1661 | 0 | } |
1662 | | |
1663 | | /* Could be empty. */ |
1664 | 0 | if (PACKET_remaining(pkt) == 0) { |
1665 | 0 | PACKET_null_init(&clienthello->extensions); |
1666 | 0 | } else { |
1667 | 0 | if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions) |
1668 | 0 | || PACKET_remaining(pkt) != 0) { |
1669 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1670 | 0 | goto err; |
1671 | 0 | } |
1672 | 0 | } |
1673 | 0 | } |
1674 | | |
1675 | 0 | if (!PACKET_copy_all(&compression, clienthello->compressions, |
1676 | 0 | MAX_COMPRESSIONS_SIZE, |
1677 | 0 | &clienthello->compressions_len)) { |
1678 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1679 | 0 | goto err; |
1680 | 0 | } |
1681 | | |
1682 | | /* Preserve the raw extensions PACKET for later use */ |
1683 | 0 | extensions = clienthello->extensions; |
1684 | 0 | if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO, |
1685 | 0 | &clienthello->pre_proc_exts, |
1686 | 0 | &clienthello->pre_proc_exts_len, 1)) { |
1687 | | /* SSLfatal already been called */ |
1688 | 0 | goto err; |
1689 | 0 | } |
1690 | 0 | s->clienthello = clienthello; |
1691 | |
|
1692 | 0 | return MSG_PROCESS_CONTINUE_PROCESSING; |
1693 | | |
1694 | 0 | err: |
1695 | 0 | if (clienthello != NULL) |
1696 | 0 | OPENSSL_free(clienthello->pre_proc_exts); |
1697 | 0 | OPENSSL_free(clienthello); |
1698 | |
|
1699 | 0 | return MSG_PROCESS_ERROR; |
1700 | 0 | } |
1701 | | |
1702 | | static int tls_early_post_process_client_hello(SSL_CONNECTION *s) |
1703 | 0 | { |
1704 | 0 | unsigned int j; |
1705 | 0 | int i, al = SSL_AD_INTERNAL_ERROR; |
1706 | 0 | int protverr; |
1707 | 0 | unsigned long id; |
1708 | 0 | #ifndef OPENSSL_NO_COMP |
1709 | 0 | SSL_COMP *comp = NULL; |
1710 | 0 | #endif |
1711 | 0 | const SSL_CIPHER *c; |
1712 | 0 | STACK_OF(SSL_CIPHER) *ciphers = NULL; |
1713 | 0 | STACK_OF(SSL_CIPHER) *scsvs = NULL; |
1714 | 0 | CLIENTHELLO_MSG *clienthello = s->clienthello; |
1715 | 0 | DOWNGRADE dgrd = DOWNGRADE_NONE; |
1716 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1717 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
1718 | 0 | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
1719 | | |
1720 | | /* Finished parsing the ClientHello, now we can start processing it */ |
1721 | | /* Give the ClientHello callback a crack at things */ |
1722 | 0 | if (sctx->client_hello_cb != NULL) { |
1723 | | /* A failure in the ClientHello callback terminates the connection. */ |
1724 | 0 | switch (sctx->client_hello_cb(ussl, &al, sctx->client_hello_cb_arg)) { |
1725 | 0 | case SSL_CLIENT_HELLO_SUCCESS: |
1726 | 0 | break; |
1727 | 0 | case SSL_CLIENT_HELLO_RETRY: |
1728 | 0 | s->rwstate = SSL_CLIENT_HELLO_CB; |
1729 | 0 | return -1; |
1730 | 0 | case SSL_CLIENT_HELLO_ERROR: |
1731 | 0 | default: |
1732 | 0 | SSLfatal(s, al, SSL_R_CALLBACK_FAILED); |
1733 | 0 | goto err; |
1734 | 0 | } |
1735 | 0 | } |
1736 | | |
1737 | | /* Set up the client_random */ |
1738 | 0 | memcpy(s->s3.client_random, clienthello->random, SSL3_RANDOM_SIZE); |
1739 | | |
1740 | | /* Choose the version */ |
1741 | |
|
1742 | 0 | if (clienthello->isv2) { |
1743 | 0 | if (clienthello->legacy_version == SSL2_VERSION |
1744 | 0 | || (clienthello->legacy_version & 0xff00) |
1745 | 0 | != (SSL3_VERSION_MAJOR << 8)) { |
1746 | | /* |
1747 | | * This is real SSLv2 or something completely unknown. We don't |
1748 | | * support it. |
1749 | | */ |
1750 | 0 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNKNOWN_PROTOCOL); |
1751 | 0 | goto err; |
1752 | 0 | } |
1753 | | /* SSLv3/TLS */ |
1754 | 0 | s->client_version = clienthello->legacy_version; |
1755 | 0 | } |
1756 | | |
1757 | | /* Choose the server SSL/TLS/DTLS version. */ |
1758 | 0 | protverr = ssl_choose_server_version(s, clienthello, &dgrd); |
1759 | |
|
1760 | 0 | if (protverr) { |
1761 | 0 | if (SSL_IS_FIRST_HANDSHAKE(s)) { |
1762 | | /* like ssl3_get_record, send alert using remote version number */ |
1763 | 0 | s->version = s->client_version = clienthello->legacy_version; |
1764 | 0 | } |
1765 | 0 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr); |
1766 | 0 | goto err; |
1767 | 0 | } |
1768 | | |
1769 | | /* TLSv1.3 specifies that a ClientHello must end on a record boundary */ |
1770 | 0 | if (SSL_CONNECTION_IS_TLS13(s) |
1771 | 0 | && RECORD_LAYER_processed_read_pending(&s->rlayer)) { |
1772 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY); |
1773 | 0 | goto err; |
1774 | 0 | } |
1775 | | |
1776 | 0 | if (SSL_CONNECTION_IS_DTLS(s)) { |
1777 | | /* Empty cookie was already handled above by returning early. */ |
1778 | 0 | if (SSL_get_options(ssl) & SSL_OP_COOKIE_EXCHANGE) { |
1779 | 0 | if (sctx->app_verify_cookie_cb != NULL) { |
1780 | 0 | if (sctx->app_verify_cookie_cb(ussl, clienthello->dtls_cookie, |
1781 | 0 | (unsigned int)clienthello->dtls_cookie_len) == 0) { |
1782 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
1783 | 0 | SSL_R_COOKIE_MISMATCH); |
1784 | 0 | goto err; |
1785 | | /* else cookie verification succeeded */ |
1786 | 0 | } |
1787 | | /* default verification */ |
1788 | 0 | } else if (s->d1->cookie_len != clienthello->dtls_cookie_len |
1789 | 0 | || memcmp(clienthello->dtls_cookie, s->d1->cookie, |
1790 | 0 | s->d1->cookie_len) != 0) { |
1791 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_COOKIE_MISMATCH); |
1792 | 0 | goto err; |
1793 | 0 | } |
1794 | 0 | s->d1->cookie_verified = 1; |
1795 | 0 | } |
1796 | 0 | } |
1797 | | |
1798 | 0 | s->hit = 0; |
1799 | |
|
1800 | 0 | if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites, |
1801 | 0 | clienthello->isv2) || |
1802 | 0 | !ossl_bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, |
1803 | 0 | &scsvs, clienthello->isv2, 1)) { |
1804 | | /* SSLfatal() already called */ |
1805 | 0 | goto err; |
1806 | 0 | } |
1807 | | |
1808 | 0 | s->s3.send_connection_binding = 0; |
1809 | | /* Check what signalling cipher-suite values were received. */ |
1810 | 0 | if (scsvs != NULL) { |
1811 | 0 | for (i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) { |
1812 | 0 | c = sk_SSL_CIPHER_value(scsvs, i); |
1813 | 0 | if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) { |
1814 | 0 | if (s->renegotiate) { |
1815 | | /* SCSV is fatal if renegotiating */ |
1816 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
1817 | 0 | SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING); |
1818 | 0 | goto err; |
1819 | 0 | } |
1820 | 0 | s->s3.send_connection_binding = 1; |
1821 | 0 | } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV && |
1822 | 0 | !ssl_check_version_downgrade(s)) { |
1823 | | /* |
1824 | | * This SCSV indicates that the client previously tried |
1825 | | * a higher version. We should fail if the current version |
1826 | | * is an unexpected downgrade, as that indicates that the first |
1827 | | * connection may have been tampered with in order to trigger |
1828 | | * an insecure downgrade. |
1829 | | */ |
1830 | 0 | SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK, |
1831 | 0 | SSL_R_INAPPROPRIATE_FALLBACK); |
1832 | 0 | goto err; |
1833 | 0 | } |
1834 | 0 | } |
1835 | 0 | } |
1836 | | |
1837 | | /* For TLSv1.3 we must select the ciphersuite *before* session resumption */ |
1838 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
1839 | 0 | const SSL_CIPHER *cipher = |
1840 | 0 | ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(ssl)); |
1841 | |
|
1842 | 0 | if (cipher == NULL) { |
1843 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER); |
1844 | 0 | goto err; |
1845 | 0 | } |
1846 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING |
1847 | 0 | && (s->s3.tmp.new_cipher == NULL |
1848 | 0 | || s->s3.tmp.new_cipher->id != cipher->id)) { |
1849 | | /* |
1850 | | * A previous HRR picked a different ciphersuite to the one we |
1851 | | * just selected. Something must have changed. |
1852 | | */ |
1853 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER); |
1854 | 0 | goto err; |
1855 | 0 | } |
1856 | 0 | s->s3.tmp.new_cipher = cipher; |
1857 | 0 | } |
1858 | | |
1859 | | /* We need to do this before getting the session */ |
1860 | 0 | if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret, |
1861 | 0 | SSL_EXT_CLIENT_HELLO, |
1862 | 0 | clienthello->pre_proc_exts, NULL, 0)) { |
1863 | | /* SSLfatal() already called */ |
1864 | 0 | goto err; |
1865 | 0 | } |
1866 | | |
1867 | | /* |
1868 | | * We don't allow resumption in a backwards compatible ClientHello. |
1869 | | * In TLS1.1+, session_id MUST be empty. |
1870 | | * |
1871 | | * Versions before 0.9.7 always allow clients to resume sessions in |
1872 | | * renegotiation. 0.9.7 and later allow this by default, but optionally |
1873 | | * ignore resumption requests with flag |
1874 | | * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather |
1875 | | * than a change to default behavior so that applications relying on |
1876 | | * this for security won't even compile against older library versions). |
1877 | | * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to |
1878 | | * request renegotiation but not a new session (s->new_session remains |
1879 | | * unset): for servers, this essentially just means that the |
1880 | | * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be |
1881 | | * ignored. |
1882 | | */ |
1883 | 0 | if (clienthello->isv2 || |
1884 | 0 | (s->new_session && |
1885 | 0 | (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) { |
1886 | 0 | if (!ssl_get_new_session(s, 1)) { |
1887 | | /* SSLfatal() already called */ |
1888 | 0 | goto err; |
1889 | 0 | } |
1890 | 0 | } else { |
1891 | 0 | i = ssl_get_prev_session(s, clienthello); |
1892 | 0 | if (i == 1) { |
1893 | | /* previous session */ |
1894 | 0 | s->hit = 1; |
1895 | 0 | } else if (i == -1) { |
1896 | | /* SSLfatal() already called */ |
1897 | 0 | goto err; |
1898 | 0 | } else { |
1899 | | /* i == 0 */ |
1900 | 0 | if (!ssl_get_new_session(s, 1)) { |
1901 | | /* SSLfatal() already called */ |
1902 | 0 | goto err; |
1903 | 0 | } |
1904 | 0 | } |
1905 | 0 | } |
1906 | | |
1907 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
1908 | 0 | memcpy(s->tmp_session_id, s->clienthello->session_id, |
1909 | 0 | s->clienthello->session_id_len); |
1910 | 0 | s->tmp_session_id_len = s->clienthello->session_id_len; |
1911 | 0 | } |
1912 | | |
1913 | | /* |
1914 | | * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check |
1915 | | * ciphersuite compatibility with the session as part of resumption. |
1916 | | */ |
1917 | 0 | if (!SSL_CONNECTION_IS_TLS13(s) && s->hit) { |
1918 | 0 | j = 0; |
1919 | 0 | id = s->session->cipher->id; |
1920 | |
|
1921 | 0 | OSSL_TRACE_BEGIN(TLS_CIPHER) { |
1922 | 0 | BIO_printf(trc_out, "client sent %d ciphers\n", |
1923 | 0 | sk_SSL_CIPHER_num(ciphers)); |
1924 | 0 | } |
1925 | 0 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
1926 | 0 | c = sk_SSL_CIPHER_value(ciphers, i); |
1927 | 0 | if (trc_out != NULL) |
1928 | 0 | BIO_printf(trc_out, "client [%2d of %2d]:%s\n", i, |
1929 | 0 | sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c)); |
1930 | 0 | if (c->id == id) { |
1931 | 0 | j = 1; |
1932 | 0 | break; |
1933 | 0 | } |
1934 | 0 | } |
1935 | 0 | if (j == 0) { |
1936 | | /* |
1937 | | * we need to have the cipher in the cipher list if we are asked |
1938 | | * to reuse it |
1939 | | */ |
1940 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1941 | 0 | SSL_R_REQUIRED_CIPHER_MISSING); |
1942 | 0 | OSSL_TRACE_CANCEL(TLS_CIPHER); |
1943 | 0 | goto err; |
1944 | 0 | } |
1945 | 0 | OSSL_TRACE_END(TLS_CIPHER); |
1946 | 0 | } |
1947 | | |
1948 | | /* At least one compression method must be preset. */ |
1949 | 0 | if (clienthello->compressions_len == 0) { |
1950 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_COMPRESSION_SPECIFIED); |
1951 | 0 | goto err; |
1952 | 0 | } |
1953 | | /* Make sure at least the null compression is supported. */ |
1954 | 0 | if (memchr(clienthello->compressions, 0, |
1955 | 0 | clienthello->compressions_len) == NULL) { |
1956 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1957 | 0 | SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING); |
1958 | 0 | goto err; |
1959 | 0 | } |
1960 | | |
1961 | 0 | if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG) |
1962 | 0 | ssl_check_for_safari(s, clienthello); |
1963 | | |
1964 | | /* TLS extensions */ |
1965 | 0 | if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO, |
1966 | 0 | clienthello->pre_proc_exts, NULL, 0, 1)) { |
1967 | | /* SSLfatal() already called */ |
1968 | 0 | goto err; |
1969 | 0 | } |
1970 | | |
1971 | | /* |
1972 | | * Check if we want to use external pre-shared secret for this handshake |
1973 | | * for not reused session only. We need to generate server_random before |
1974 | | * calling tls_session_secret_cb in order to allow SessionTicket |
1975 | | * processing to use it in key derivation. |
1976 | | */ |
1977 | 0 | { |
1978 | 0 | unsigned char *pos; |
1979 | 0 | pos = s->s3.server_random; |
1980 | 0 | if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) { |
1981 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1982 | 0 | goto err; |
1983 | 0 | } |
1984 | 0 | } |
1985 | | |
1986 | 0 | if (!s->hit && !tls1_set_server_sigalgs(s)) { |
1987 | | /* SSLfatal() already called */ |
1988 | 0 | goto err; |
1989 | 0 | } |
1990 | | |
1991 | 0 | if (!s->hit |
1992 | 0 | && s->version >= TLS1_VERSION |
1993 | 0 | && !SSL_CONNECTION_IS_TLS13(s) |
1994 | 0 | && !SSL_CONNECTION_IS_DTLS(s) |
1995 | 0 | && s->ext.session_secret_cb != NULL) { |
1996 | 0 | const SSL_CIPHER *pref_cipher = NULL; |
1997 | | /* |
1998 | | * s->session->master_key_length is a size_t, but this is an int for |
1999 | | * backwards compat reasons |
2000 | | */ |
2001 | 0 | int master_key_length; |
2002 | |
|
2003 | 0 | master_key_length = sizeof(s->session->master_key); |
2004 | 0 | if (s->ext.session_secret_cb(ussl, s->session->master_key, |
2005 | 0 | &master_key_length, ciphers, |
2006 | 0 | &pref_cipher, |
2007 | 0 | s->ext.session_secret_cb_arg) |
2008 | 0 | && master_key_length > 0) { |
2009 | 0 | s->session->master_key_length = master_key_length; |
2010 | 0 | s->hit = 1; |
2011 | 0 | s->peer_ciphers = ciphers; |
2012 | 0 | s->session->verify_result = X509_V_OK; |
2013 | |
|
2014 | 0 | ciphers = NULL; |
2015 | | |
2016 | | /* check if some cipher was preferred by call back */ |
2017 | 0 | if (pref_cipher == NULL) |
2018 | 0 | pref_cipher = ssl3_choose_cipher(s, s->peer_ciphers, |
2019 | 0 | SSL_get_ciphers(ssl)); |
2020 | 0 | if (pref_cipher == NULL) { |
2021 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER); |
2022 | 0 | goto err; |
2023 | 0 | } |
2024 | | |
2025 | 0 | s->session->cipher = pref_cipher; |
2026 | 0 | sk_SSL_CIPHER_free(s->cipher_list); |
2027 | 0 | s->cipher_list = sk_SSL_CIPHER_dup(s->peer_ciphers); |
2028 | 0 | sk_SSL_CIPHER_free(s->cipher_list_by_id); |
2029 | 0 | s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->peer_ciphers); |
2030 | 0 | } |
2031 | 0 | } |
2032 | | |
2033 | | /* |
2034 | | * Worst case, we will use the NULL compression, but if we have other |
2035 | | * options, we will now look for them. We have complen-1 compression |
2036 | | * algorithms from the client, starting at q. |
2037 | | */ |
2038 | 0 | s->s3.tmp.new_compression = NULL; |
2039 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
2040 | | /* |
2041 | | * We already checked above that the NULL compression method appears in |
2042 | | * the list. Now we check there aren't any others (which is illegal in |
2043 | | * a TLSv1.3 ClientHello. |
2044 | | */ |
2045 | 0 | if (clienthello->compressions_len != 1) { |
2046 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
2047 | 0 | SSL_R_INVALID_COMPRESSION_ALGORITHM); |
2048 | 0 | goto err; |
2049 | 0 | } |
2050 | 0 | } |
2051 | 0 | #ifndef OPENSSL_NO_COMP |
2052 | | /* This only happens if we have a cache hit */ |
2053 | 0 | else if (s->session->compress_meth != 0) { |
2054 | 0 | int m, comp_id = s->session->compress_meth; |
2055 | 0 | unsigned int k; |
2056 | | /* Perform sanity checks on resumed compression algorithm */ |
2057 | | /* Can't disable compression */ |
2058 | 0 | if (!ssl_allow_compression(s)) { |
2059 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
2060 | 0 | SSL_R_INCONSISTENT_COMPRESSION); |
2061 | 0 | goto err; |
2062 | 0 | } |
2063 | | /* Look for resumed compression method */ |
2064 | 0 | for (m = 0; m < sk_SSL_COMP_num(sctx->comp_methods); m++) { |
2065 | 0 | comp = sk_SSL_COMP_value(sctx->comp_methods, m); |
2066 | 0 | if (comp_id == comp->id) { |
2067 | 0 | s->s3.tmp.new_compression = comp; |
2068 | 0 | break; |
2069 | 0 | } |
2070 | 0 | } |
2071 | 0 | if (s->s3.tmp.new_compression == NULL) { |
2072 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
2073 | 0 | SSL_R_INVALID_COMPRESSION_ALGORITHM); |
2074 | 0 | goto err; |
2075 | 0 | } |
2076 | | /* Look for resumed method in compression list */ |
2077 | 0 | for (k = 0; k < clienthello->compressions_len; k++) { |
2078 | 0 | if (clienthello->compressions[k] == comp_id) |
2079 | 0 | break; |
2080 | 0 | } |
2081 | 0 | if (k >= clienthello->compressions_len) { |
2082 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
2083 | 0 | SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING); |
2084 | 0 | goto err; |
2085 | 0 | } |
2086 | 0 | } else if (s->hit) { |
2087 | 0 | comp = NULL; |
2088 | 0 | } else if (ssl_allow_compression(s) && sctx->comp_methods) { |
2089 | | /* See if we have a match */ |
2090 | 0 | int m, nn, v, done = 0; |
2091 | 0 | unsigned int o; |
2092 | |
|
2093 | 0 | nn = sk_SSL_COMP_num(sctx->comp_methods); |
2094 | 0 | for (m = 0; m < nn; m++) { |
2095 | 0 | comp = sk_SSL_COMP_value(sctx->comp_methods, m); |
2096 | 0 | v = comp->id; |
2097 | 0 | for (o = 0; o < clienthello->compressions_len; o++) { |
2098 | 0 | if (v == clienthello->compressions[o]) { |
2099 | 0 | done = 1; |
2100 | 0 | break; |
2101 | 0 | } |
2102 | 0 | } |
2103 | 0 | if (done) |
2104 | 0 | break; |
2105 | 0 | } |
2106 | 0 | if (done) |
2107 | 0 | s->s3.tmp.new_compression = comp; |
2108 | 0 | else |
2109 | 0 | comp = NULL; |
2110 | 0 | } |
2111 | | #else |
2112 | | /* |
2113 | | * If compression is disabled we'd better not try to resume a session |
2114 | | * using compression. |
2115 | | */ |
2116 | | if (s->session->compress_meth != 0) { |
2117 | | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION); |
2118 | | goto err; |
2119 | | } |
2120 | | #endif |
2121 | | |
2122 | | /* |
2123 | | * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher |
2124 | | */ |
2125 | | |
2126 | 0 | if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) { |
2127 | 0 | sk_SSL_CIPHER_free(s->peer_ciphers); |
2128 | 0 | s->peer_ciphers = ciphers; |
2129 | 0 | if (ciphers == NULL) { |
2130 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2131 | 0 | goto err; |
2132 | 0 | } |
2133 | 0 | ciphers = NULL; |
2134 | 0 | } |
2135 | | |
2136 | 0 | if (!s->hit) { |
2137 | | #ifdef OPENSSL_NO_COMP |
2138 | | s->session->compress_meth = 0; |
2139 | | #else |
2140 | 0 | s->session->compress_meth = (comp == NULL) ? 0 : comp->id; |
2141 | 0 | #endif |
2142 | 0 | } |
2143 | |
|
2144 | 0 | sk_SSL_CIPHER_free(ciphers); |
2145 | 0 | sk_SSL_CIPHER_free(scsvs); |
2146 | 0 | OPENSSL_free(clienthello->pre_proc_exts); |
2147 | 0 | OPENSSL_free(s->clienthello); |
2148 | 0 | s->clienthello = NULL; |
2149 | 0 | return 1; |
2150 | 0 | err: |
2151 | 0 | sk_SSL_CIPHER_free(ciphers); |
2152 | 0 | sk_SSL_CIPHER_free(scsvs); |
2153 | 0 | OPENSSL_free(clienthello->pre_proc_exts); |
2154 | 0 | OPENSSL_free(s->clienthello); |
2155 | 0 | s->clienthello = NULL; |
2156 | |
|
2157 | 0 | return 0; |
2158 | 0 | } |
2159 | | |
2160 | | /* |
2161 | | * Call the status request callback if needed. Upon success, returns 1. |
2162 | | * Upon failure, returns 0. |
2163 | | */ |
2164 | | static int tls_handle_status_request(SSL_CONNECTION *s) |
2165 | 0 | { |
2166 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
2167 | |
|
2168 | 0 | s->ext.status_expected = 0; |
2169 | | |
2170 | | /* |
2171 | | * If status request then ask callback what to do. Note: this must be |
2172 | | * called after servername callbacks in case the certificate has changed, |
2173 | | * and must be called after the cipher has been chosen because this may |
2174 | | * influence which certificate is sent |
2175 | | */ |
2176 | 0 | if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && sctx != NULL |
2177 | 0 | && sctx->ext.status_cb != NULL) { |
2178 | 0 | int ret; |
2179 | | |
2180 | | /* If no certificate can't return certificate status */ |
2181 | 0 | if (s->s3.tmp.cert != NULL) { |
2182 | | /* |
2183 | | * Set current certificate to one we will use so SSL_get_certificate |
2184 | | * et al can pick it up. |
2185 | | */ |
2186 | 0 | s->cert->key = s->s3.tmp.cert; |
2187 | 0 | ret = sctx->ext.status_cb(SSL_CONNECTION_GET_USER_SSL(s), |
2188 | 0 | sctx->ext.status_arg); |
2189 | 0 | switch (ret) { |
2190 | | /* We don't want to send a status request response */ |
2191 | 0 | case SSL_TLSEXT_ERR_NOACK: |
2192 | 0 | s->ext.status_expected = 0; |
2193 | 0 | break; |
2194 | | /* status request response should be sent */ |
2195 | 0 | case SSL_TLSEXT_ERR_OK: |
2196 | 0 | if (s->ext.ocsp.resp) |
2197 | 0 | s->ext.status_expected = 1; |
2198 | 0 | break; |
2199 | | /* something bad happened */ |
2200 | 0 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
2201 | 0 | default: |
2202 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CLIENTHELLO_TLSEXT); |
2203 | 0 | return 0; |
2204 | 0 | } |
2205 | 0 | } |
2206 | 0 | } |
2207 | | |
2208 | 0 | return 1; |
2209 | 0 | } |
2210 | | |
2211 | | /* |
2212 | | * Call the alpn_select callback if needed. Upon success, returns 1. |
2213 | | * Upon failure, returns 0. |
2214 | | */ |
2215 | | int tls_handle_alpn(SSL_CONNECTION *s) |
2216 | 0 | { |
2217 | 0 | const unsigned char *selected = NULL; |
2218 | 0 | unsigned char selected_len = 0; |
2219 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
2220 | |
|
2221 | 0 | if (sctx->ext.alpn_select_cb != NULL && s->s3.alpn_proposed != NULL) { |
2222 | 0 | int r = sctx->ext.alpn_select_cb(SSL_CONNECTION_GET_USER_SSL(s), |
2223 | 0 | &selected, &selected_len, |
2224 | 0 | s->s3.alpn_proposed, |
2225 | 0 | (unsigned int)s->s3.alpn_proposed_len, |
2226 | 0 | sctx->ext.alpn_select_cb_arg); |
2227 | |
|
2228 | 0 | if (r == SSL_TLSEXT_ERR_OK) { |
2229 | 0 | OPENSSL_free(s->s3.alpn_selected); |
2230 | 0 | s->s3.alpn_selected = OPENSSL_memdup(selected, selected_len); |
2231 | 0 | if (s->s3.alpn_selected == NULL) { |
2232 | 0 | s->s3.alpn_selected_len = 0; |
2233 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2234 | 0 | return 0; |
2235 | 0 | } |
2236 | 0 | s->s3.alpn_selected_len = selected_len; |
2237 | 0 | #ifndef OPENSSL_NO_NEXTPROTONEG |
2238 | | /* ALPN takes precedence over NPN. */ |
2239 | 0 | s->s3.npn_seen = 0; |
2240 | 0 | #endif |
2241 | | |
2242 | | /* Check ALPN is consistent with session */ |
2243 | 0 | if (s->session->ext.alpn_selected == NULL |
2244 | 0 | || selected_len != s->session->ext.alpn_selected_len |
2245 | 0 | || memcmp(selected, s->session->ext.alpn_selected, |
2246 | 0 | selected_len) != 0) { |
2247 | | /* Not consistent so can't be used for early_data */ |
2248 | 0 | s->ext.early_data_ok = 0; |
2249 | |
|
2250 | 0 | if (!s->hit) { |
2251 | | /* |
2252 | | * This is a new session and so alpn_selected should have |
2253 | | * been initialised to NULL. We should update it with the |
2254 | | * selected ALPN. |
2255 | | */ |
2256 | 0 | if (!ossl_assert(s->session->ext.alpn_selected == NULL)) { |
2257 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
2258 | 0 | ERR_R_INTERNAL_ERROR); |
2259 | 0 | return 0; |
2260 | 0 | } |
2261 | 0 | s->session->ext.alpn_selected = OPENSSL_memdup(selected, |
2262 | 0 | selected_len); |
2263 | 0 | if (s->session->ext.alpn_selected == NULL) { |
2264 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
2265 | 0 | ERR_R_INTERNAL_ERROR); |
2266 | 0 | return 0; |
2267 | 0 | } |
2268 | 0 | s->session->ext.alpn_selected_len = selected_len; |
2269 | 0 | } |
2270 | 0 | } |
2271 | | |
2272 | 0 | return 1; |
2273 | 0 | } else if (r != SSL_TLSEXT_ERR_NOACK) { |
2274 | 0 | SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL, |
2275 | 0 | SSL_R_NO_APPLICATION_PROTOCOL); |
2276 | 0 | return 0; |
2277 | 0 | } |
2278 | | /* |
2279 | | * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was |
2280 | | * present. |
2281 | | */ |
2282 | 0 | } |
2283 | | |
2284 | | /* Check ALPN is consistent with session */ |
2285 | 0 | if (s->session->ext.alpn_selected != NULL) { |
2286 | | /* Not consistent so can't be used for early_data */ |
2287 | 0 | s->ext.early_data_ok = 0; |
2288 | 0 | } |
2289 | |
|
2290 | 0 | return 1; |
2291 | 0 | } |
2292 | | |
2293 | | WORK_STATE tls_post_process_client_hello(SSL_CONNECTION *s, WORK_STATE wst) |
2294 | 0 | { |
2295 | 0 | const SSL_CIPHER *cipher; |
2296 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
2297 | 0 | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
2298 | |
|
2299 | 0 | if (wst == WORK_MORE_A) { |
2300 | 0 | int rv = tls_early_post_process_client_hello(s); |
2301 | 0 | if (rv == 0) { |
2302 | | /* SSLfatal() was already called */ |
2303 | 0 | goto err; |
2304 | 0 | } |
2305 | 0 | if (rv < 0) |
2306 | 0 | return WORK_MORE_A; |
2307 | 0 | wst = WORK_MORE_B; |
2308 | 0 | } |
2309 | 0 | if (wst == WORK_MORE_B) { |
2310 | 0 | if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) { |
2311 | | /* Let cert callback update server certificates if required */ |
2312 | 0 | if (!s->hit && s->cert->cert_cb != NULL) { |
2313 | 0 | int rv = s->cert->cert_cb(ussl, s->cert->cert_cb_arg); |
2314 | |
|
2315 | 0 | if (rv == 0) { |
2316 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CERT_CB_ERROR); |
2317 | 0 | goto err; |
2318 | 0 | } |
2319 | 0 | if (rv < 0) { |
2320 | 0 | s->rwstate = SSL_X509_LOOKUP; |
2321 | 0 | return WORK_MORE_B; |
2322 | 0 | } |
2323 | 0 | s->rwstate = SSL_NOTHING; |
2324 | 0 | } |
2325 | | |
2326 | | /* In TLSv1.3 we selected the ciphersuite before resumption */ |
2327 | 0 | if (!SSL_CONNECTION_IS_TLS13(s)) { |
2328 | 0 | cipher = |
2329 | 0 | ssl3_choose_cipher(s, s->peer_ciphers, |
2330 | 0 | SSL_get_ciphers(ssl)); |
2331 | |
|
2332 | 0 | if (cipher == NULL) { |
2333 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
2334 | 0 | SSL_R_NO_SHARED_CIPHER); |
2335 | 0 | goto err; |
2336 | 0 | } |
2337 | 0 | s->s3.tmp.new_cipher = cipher; |
2338 | 0 | } |
2339 | 0 | if (!s->hit) { |
2340 | 0 | if (!tls_choose_sigalg(s, 1)) { |
2341 | | /* SSLfatal already called */ |
2342 | 0 | goto err; |
2343 | 0 | } |
2344 | | /* check whether we should disable session resumption */ |
2345 | 0 | if (s->not_resumable_session_cb != NULL) |
2346 | 0 | s->session->not_resumable = |
2347 | 0 | s->not_resumable_session_cb(ussl, |
2348 | 0 | ((s->s3.tmp.new_cipher->algorithm_mkey |
2349 | 0 | & (SSL_kDHE | SSL_kECDHE)) != 0)); |
2350 | 0 | if (s->session->not_resumable) |
2351 | | /* do not send a session ticket */ |
2352 | 0 | s->ext.ticket_expected = 0; |
2353 | 0 | } |
2354 | 0 | } else { |
2355 | | /* Session-id reuse */ |
2356 | 0 | s->s3.tmp.new_cipher = s->session->cipher; |
2357 | 0 | } |
2358 | | |
2359 | | /*- |
2360 | | * we now have the following setup. |
2361 | | * client_random |
2362 | | * cipher_list - our preferred list of ciphers |
2363 | | * ciphers - the client's preferred list of ciphers |
2364 | | * compression - basically ignored right now |
2365 | | * ssl version is set - sslv3 |
2366 | | * s->session - The ssl session has been setup. |
2367 | | * s->hit - session reuse flag |
2368 | | * s->s3.tmp.new_cipher - the new cipher to use. |
2369 | | */ |
2370 | | |
2371 | | /* |
2372 | | * Call status_request callback if needed. Has to be done after the |
2373 | | * certificate callbacks etc above. |
2374 | | */ |
2375 | 0 | if (!tls_handle_status_request(s)) { |
2376 | | /* SSLfatal() already called */ |
2377 | 0 | goto err; |
2378 | 0 | } |
2379 | | /* |
2380 | | * Call alpn_select callback if needed. Has to be done after SNI and |
2381 | | * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3 |
2382 | | * we already did this because cipher negotiation happens earlier, and |
2383 | | * we must handle ALPN before we decide whether to accept early_data. |
2384 | | */ |
2385 | 0 | if (!SSL_CONNECTION_IS_TLS13(s) && !tls_handle_alpn(s)) { |
2386 | | /* SSLfatal() already called */ |
2387 | 0 | goto err; |
2388 | 0 | } |
2389 | | |
2390 | 0 | wst = WORK_MORE_C; |
2391 | 0 | } |
2392 | 0 | #ifndef OPENSSL_NO_SRP |
2393 | 0 | if (wst == WORK_MORE_C) { |
2394 | 0 | int ret; |
2395 | 0 | if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) { |
2396 | | /* |
2397 | | * callback indicates further work to be done |
2398 | | */ |
2399 | 0 | s->rwstate = SSL_X509_LOOKUP; |
2400 | 0 | return WORK_MORE_C; |
2401 | 0 | } |
2402 | 0 | if (ret < 0) { |
2403 | | /* SSLfatal() already called */ |
2404 | 0 | goto err; |
2405 | 0 | } |
2406 | 0 | } |
2407 | 0 | #endif |
2408 | | |
2409 | 0 | return WORK_FINISHED_STOP; |
2410 | 0 | err: |
2411 | 0 | return WORK_ERROR; |
2412 | 0 | } |
2413 | | |
2414 | | CON_FUNC_RETURN tls_construct_server_hello(SSL_CONNECTION *s, WPACKET *pkt) |
2415 | 0 | { |
2416 | 0 | int compm; |
2417 | 0 | size_t sl, len; |
2418 | 0 | int version; |
2419 | 0 | unsigned char *session_id; |
2420 | 0 | int usetls13 = SSL_CONNECTION_IS_TLS13(s) |
2421 | 0 | || s->hello_retry_request == SSL_HRR_PENDING; |
2422 | |
|
2423 | 0 | version = usetls13 ? TLS1_2_VERSION : s->version; |
2424 | 0 | if (!WPACKET_put_bytes_u16(pkt, version) |
2425 | | /* |
2426 | | * Random stuff. Filling of the server_random takes place in |
2427 | | * tls_process_client_hello() |
2428 | | */ |
2429 | 0 | || !WPACKET_memcpy(pkt, |
2430 | 0 | s->hello_retry_request == SSL_HRR_PENDING |
2431 | 0 | ? hrrrandom : s->s3.server_random, |
2432 | 0 | SSL3_RANDOM_SIZE)) { |
2433 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2434 | 0 | return CON_FUNC_ERROR; |
2435 | 0 | } |
2436 | | |
2437 | | /*- |
2438 | | * There are several cases for the session ID to send |
2439 | | * back in the server hello: |
2440 | | * - For session reuse from the session cache, |
2441 | | * we send back the old session ID. |
2442 | | * - If stateless session reuse (using a session ticket) |
2443 | | * is successful, we send back the client's "session ID" |
2444 | | * (which doesn't actually identify the session). |
2445 | | * - If it is a new session, we send back the new |
2446 | | * session ID. |
2447 | | * - However, if we want the new session to be single-use, |
2448 | | * we send back a 0-length session ID. |
2449 | | * - In TLSv1.3 we echo back the session id sent to us by the client |
2450 | | * regardless |
2451 | | * s->hit is non-zero in either case of session reuse, |
2452 | | * so the following won't overwrite an ID that we're supposed |
2453 | | * to send back. |
2454 | | */ |
2455 | 0 | if (!(SSL_CONNECTION_GET_CTX(s)->session_cache_mode & SSL_SESS_CACHE_SERVER) |
2456 | 0 | && !s->hit) |
2457 | 0 | s->session->session_id_length = 0; |
2458 | |
|
2459 | 0 | if (usetls13) { |
2460 | 0 | sl = s->tmp_session_id_len; |
2461 | 0 | session_id = s->tmp_session_id; |
2462 | 0 | } else { |
2463 | 0 | sl = s->session->session_id_length; |
2464 | 0 | session_id = s->session->session_id; |
2465 | 0 | } |
2466 | |
|
2467 | 0 | if (sl > sizeof(s->session->session_id)) { |
2468 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2469 | 0 | return CON_FUNC_ERROR; |
2470 | 0 | } |
2471 | | |
2472 | | /* set up the compression method */ |
2473 | | #ifdef OPENSSL_NO_COMP |
2474 | | compm = 0; |
2475 | | #else |
2476 | 0 | if (usetls13 || s->s3.tmp.new_compression == NULL) |
2477 | 0 | compm = 0; |
2478 | 0 | else |
2479 | 0 | compm = s->s3.tmp.new_compression->id; |
2480 | 0 | #endif |
2481 | |
|
2482 | 0 | if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl) |
2483 | 0 | || !SSL_CONNECTION_GET_SSL(s)->method->put_cipher_by_char(s->s3.tmp.new_cipher, |
2484 | 0 | pkt, &len) |
2485 | 0 | || !WPACKET_put_bytes_u8(pkt, compm)) { |
2486 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2487 | 0 | return CON_FUNC_ERROR; |
2488 | 0 | } |
2489 | | |
2490 | 0 | if (!tls_construct_extensions(s, pkt, |
2491 | 0 | s->hello_retry_request == SSL_HRR_PENDING |
2492 | 0 | ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST |
2493 | 0 | : (SSL_CONNECTION_IS_TLS13(s) |
2494 | 0 | ? SSL_EXT_TLS1_3_SERVER_HELLO |
2495 | 0 | : SSL_EXT_TLS1_2_SERVER_HELLO), |
2496 | 0 | NULL, 0)) { |
2497 | | /* SSLfatal() already called */ |
2498 | 0 | return CON_FUNC_ERROR; |
2499 | 0 | } |
2500 | | |
2501 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING) { |
2502 | | /* Ditch the session. We'll create a new one next time around */ |
2503 | 0 | SSL_SESSION_free(s->session); |
2504 | 0 | s->session = NULL; |
2505 | 0 | s->hit = 0; |
2506 | | |
2507 | | /* |
2508 | | * Re-initialise the Transcript Hash. We're going to prepopulate it with |
2509 | | * a synthetic message_hash in place of ClientHello1. |
2510 | | */ |
2511 | 0 | if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) { |
2512 | | /* SSLfatal() already called */ |
2513 | 0 | return CON_FUNC_ERROR; |
2514 | 0 | } |
2515 | 0 | } else if (!(s->verify_mode & SSL_VERIFY_PEER) |
2516 | 0 | && !ssl3_digest_cached_records(s, 0)) { |
2517 | 0 | /* SSLfatal() already called */; |
2518 | 0 | return CON_FUNC_ERROR; |
2519 | 0 | } |
2520 | | |
2521 | 0 | return CON_FUNC_SUCCESS; |
2522 | 0 | } |
2523 | | |
2524 | | CON_FUNC_RETURN tls_construct_server_done(SSL_CONNECTION *s, WPACKET *pkt) |
2525 | 0 | { |
2526 | 0 | if (!s->s3.tmp.cert_request) { |
2527 | 0 | if (!ssl3_digest_cached_records(s, 0)) { |
2528 | | /* SSLfatal() already called */ |
2529 | 0 | return CON_FUNC_ERROR; |
2530 | 0 | } |
2531 | 0 | } |
2532 | 0 | return CON_FUNC_SUCCESS; |
2533 | 0 | } |
2534 | | |
2535 | | CON_FUNC_RETURN tls_construct_server_key_exchange(SSL_CONNECTION *s, |
2536 | | WPACKET *pkt) |
2537 | 0 | { |
2538 | 0 | EVP_PKEY *pkdh = NULL; |
2539 | 0 | unsigned char *encodedPoint = NULL; |
2540 | 0 | size_t encodedlen = 0; |
2541 | 0 | int curve_id = 0; |
2542 | 0 | const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg; |
2543 | 0 | int i; |
2544 | 0 | unsigned long type; |
2545 | 0 | BIGNUM *r[4]; |
2546 | 0 | EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); |
2547 | 0 | EVP_PKEY_CTX *pctx = NULL; |
2548 | 0 | size_t paramlen, paramoffset; |
2549 | 0 | int freer = 0; |
2550 | 0 | CON_FUNC_RETURN ret = CON_FUNC_ERROR; |
2551 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
2552 | |
|
2553 | 0 | if (!WPACKET_get_total_written(pkt, ¶moffset)) { |
2554 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2555 | 0 | goto err; |
2556 | 0 | } |
2557 | | |
2558 | 0 | if (md_ctx == NULL) { |
2559 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
2560 | 0 | goto err; |
2561 | 0 | } |
2562 | | |
2563 | 0 | type = s->s3.tmp.new_cipher->algorithm_mkey; |
2564 | |
|
2565 | 0 | r[0] = r[1] = r[2] = r[3] = NULL; |
2566 | 0 | #ifndef OPENSSL_NO_PSK |
2567 | | /* Plain PSK or RSAPSK nothing to do */ |
2568 | 0 | if (type & (SSL_kPSK | SSL_kRSAPSK)) { |
2569 | 0 | } else |
2570 | 0 | #endif /* !OPENSSL_NO_PSK */ |
2571 | 0 | if (type & (SSL_kDHE | SSL_kDHEPSK)) { |
2572 | 0 | CERT *cert = s->cert; |
2573 | 0 | EVP_PKEY *pkdhp = NULL; |
2574 | |
|
2575 | 0 | if (s->cert->dh_tmp_auto) { |
2576 | 0 | pkdh = ssl_get_auto_dh(s); |
2577 | 0 | if (pkdh == NULL) { |
2578 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2579 | 0 | goto err; |
2580 | 0 | } |
2581 | 0 | pkdhp = pkdh; |
2582 | 0 | } else { |
2583 | 0 | pkdhp = cert->dh_tmp; |
2584 | 0 | } |
2585 | 0 | #if !defined(OPENSSL_NO_DEPRECATED_3_0) |
2586 | 0 | if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) { |
2587 | 0 | pkdh = ssl_dh_to_pkey(s->cert->dh_tmp_cb(SSL_CONNECTION_GET_USER_SSL(s), |
2588 | 0 | 0, 1024)); |
2589 | 0 | if (pkdh == NULL) { |
2590 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2591 | 0 | goto err; |
2592 | 0 | } |
2593 | 0 | pkdhp = pkdh; |
2594 | 0 | } |
2595 | 0 | #endif |
2596 | 0 | if (pkdhp == NULL) { |
2597 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY); |
2598 | 0 | goto err; |
2599 | 0 | } |
2600 | 0 | if (!ssl_security(s, SSL_SECOP_TMP_DH, |
2601 | 0 | EVP_PKEY_get_security_bits(pkdhp), 0, pkdhp)) { |
2602 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL); |
2603 | 0 | goto err; |
2604 | 0 | } |
2605 | 0 | if (s->s3.tmp.pkey != NULL) { |
2606 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2607 | 0 | goto err; |
2608 | 0 | } |
2609 | | |
2610 | 0 | s->s3.tmp.pkey = ssl_generate_pkey(s, pkdhp); |
2611 | 0 | if (s->s3.tmp.pkey == NULL) { |
2612 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2613 | 0 | goto err; |
2614 | 0 | } |
2615 | | |
2616 | 0 | EVP_PKEY_free(pkdh); |
2617 | 0 | pkdh = NULL; |
2618 | | |
2619 | | /* These BIGNUMs need to be freed when we're finished */ |
2620 | 0 | freer = 1; |
2621 | 0 | if (!EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_P, |
2622 | 0 | &r[0]) |
2623 | 0 | || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_G, |
2624 | 0 | &r[1]) |
2625 | 0 | || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey, |
2626 | 0 | OSSL_PKEY_PARAM_PUB_KEY, &r[2])) { |
2627 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2628 | 0 | goto err; |
2629 | 0 | } |
2630 | 0 | } else if (type & (SSL_kECDHE | SSL_kECDHEPSK)) { |
2631 | |
|
2632 | 0 | if (s->s3.tmp.pkey != NULL) { |
2633 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2634 | 0 | goto err; |
2635 | 0 | } |
2636 | | |
2637 | | /* Get NID of appropriate shared curve */ |
2638 | 0 | curve_id = tls1_shared_group(s, -2); |
2639 | 0 | if (curve_id == 0) { |
2640 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
2641 | 0 | SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); |
2642 | 0 | goto err; |
2643 | 0 | } |
2644 | | /* Cache the group used in the SSL_SESSION */ |
2645 | 0 | s->session->kex_group = curve_id; |
2646 | | /* Generate a new key for this curve */ |
2647 | 0 | s->s3.tmp.pkey = ssl_generate_pkey_group(s, curve_id); |
2648 | 0 | if (s->s3.tmp.pkey == NULL) { |
2649 | | /* SSLfatal() already called */ |
2650 | 0 | goto err; |
2651 | 0 | } |
2652 | | |
2653 | | /* Encode the public key. */ |
2654 | 0 | encodedlen = EVP_PKEY_get1_encoded_public_key(s->s3.tmp.pkey, |
2655 | 0 | &encodedPoint); |
2656 | 0 | if (encodedlen == 0) { |
2657 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB); |
2658 | 0 | goto err; |
2659 | 0 | } |
2660 | | |
2661 | | /* |
2662 | | * We'll generate the serverKeyExchange message explicitly so we |
2663 | | * can set these to NULLs |
2664 | | */ |
2665 | 0 | r[0] = NULL; |
2666 | 0 | r[1] = NULL; |
2667 | 0 | r[2] = NULL; |
2668 | 0 | r[3] = NULL; |
2669 | 0 | } else |
2670 | 0 | #ifndef OPENSSL_NO_SRP |
2671 | 0 | if (type & SSL_kSRP) { |
2672 | 0 | if ((s->srp_ctx.N == NULL) || |
2673 | 0 | (s->srp_ctx.g == NULL) || |
2674 | 0 | (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) { |
2675 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_SRP_PARAM); |
2676 | 0 | goto err; |
2677 | 0 | } |
2678 | 0 | r[0] = s->srp_ctx.N; |
2679 | 0 | r[1] = s->srp_ctx.g; |
2680 | 0 | r[2] = s->srp_ctx.s; |
2681 | 0 | r[3] = s->srp_ctx.B; |
2682 | 0 | } else |
2683 | 0 | #endif |
2684 | 0 | { |
2685 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); |
2686 | 0 | goto err; |
2687 | 0 | } |
2688 | | |
2689 | 0 | if (((s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0) |
2690 | 0 | || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) { |
2691 | 0 | lu = NULL; |
2692 | 0 | } else if (lu == NULL) { |
2693 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR); |
2694 | 0 | goto err; |
2695 | 0 | } |
2696 | | |
2697 | 0 | #ifndef OPENSSL_NO_PSK |
2698 | 0 | if (type & SSL_PSK) { |
2699 | 0 | size_t len = (s->cert->psk_identity_hint == NULL) |
2700 | 0 | ? 0 : strlen(s->cert->psk_identity_hint); |
2701 | | |
2702 | | /* |
2703 | | * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already |
2704 | | * checked this when we set the identity hint - but just in case |
2705 | | */ |
2706 | 0 | if (len > PSK_MAX_IDENTITY_LEN |
2707 | 0 | || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint, |
2708 | 0 | len)) { |
2709 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2710 | 0 | goto err; |
2711 | 0 | } |
2712 | 0 | } |
2713 | 0 | #endif |
2714 | | |
2715 | 0 | for (i = 0; i < 4 && r[i] != NULL; i++) { |
2716 | 0 | unsigned char *binval; |
2717 | 0 | int res; |
2718 | |
|
2719 | 0 | #ifndef OPENSSL_NO_SRP |
2720 | 0 | if ((i == 2) && (type & SSL_kSRP)) { |
2721 | 0 | res = WPACKET_start_sub_packet_u8(pkt); |
2722 | 0 | } else |
2723 | 0 | #endif |
2724 | 0 | res = WPACKET_start_sub_packet_u16(pkt); |
2725 | |
|
2726 | 0 | if (!res) { |
2727 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2728 | 0 | goto err; |
2729 | 0 | } |
2730 | | |
2731 | | /*- |
2732 | | * for interoperability with some versions of the Microsoft TLS |
2733 | | * stack, we need to zero pad the DHE pub key to the same length |
2734 | | * as the prime |
2735 | | */ |
2736 | 0 | if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) { |
2737 | 0 | size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]); |
2738 | |
|
2739 | 0 | if (len > 0) { |
2740 | 0 | if (!WPACKET_allocate_bytes(pkt, len, &binval)) { |
2741 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2742 | 0 | goto err; |
2743 | 0 | } |
2744 | 0 | memset(binval, 0, len); |
2745 | 0 | } |
2746 | 0 | } |
2747 | | |
2748 | 0 | if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval) |
2749 | 0 | || !WPACKET_close(pkt)) { |
2750 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2751 | 0 | goto err; |
2752 | 0 | } |
2753 | | |
2754 | 0 | BN_bn2bin(r[i], binval); |
2755 | 0 | } |
2756 | | |
2757 | 0 | if (type & (SSL_kECDHE | SSL_kECDHEPSK)) { |
2758 | | /* |
2759 | | * We only support named (not generic) curves. In this situation, the |
2760 | | * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName] |
2761 | | * [1 byte length of encoded point], followed by the actual encoded |
2762 | | * point itself |
2763 | | */ |
2764 | 0 | if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE) |
2765 | 0 | || !WPACKET_put_bytes_u8(pkt, 0) |
2766 | 0 | || !WPACKET_put_bytes_u8(pkt, curve_id) |
2767 | 0 | || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) { |
2768 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2769 | 0 | goto err; |
2770 | 0 | } |
2771 | 0 | OPENSSL_free(encodedPoint); |
2772 | 0 | encodedPoint = NULL; |
2773 | 0 | } |
2774 | | |
2775 | | /* not anonymous */ |
2776 | 0 | if (lu != NULL) { |
2777 | 0 | EVP_PKEY *pkey = s->s3.tmp.cert->privatekey; |
2778 | 0 | const EVP_MD *md; |
2779 | 0 | unsigned char *sigbytes1, *sigbytes2, *tbs; |
2780 | 0 | size_t siglen = 0, tbslen; |
2781 | |
|
2782 | 0 | if (pkey == NULL || !tls1_lookup_md(sctx, lu, &md)) { |
2783 | | /* Should never happen */ |
2784 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2785 | 0 | goto err; |
2786 | 0 | } |
2787 | | /* Get length of the parameters we have written above */ |
2788 | 0 | if (!WPACKET_get_length(pkt, ¶mlen)) { |
2789 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2790 | 0 | goto err; |
2791 | 0 | } |
2792 | | /* send signature algorithm */ |
2793 | 0 | if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) { |
2794 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2795 | 0 | goto err; |
2796 | 0 | } |
2797 | | |
2798 | 0 | if (EVP_DigestSignInit_ex(md_ctx, &pctx, |
2799 | 0 | md == NULL ? NULL : EVP_MD_get0_name(md), |
2800 | 0 | sctx->libctx, sctx->propq, pkey, |
2801 | 0 | NULL) <= 0) { |
2802 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2803 | 0 | goto err; |
2804 | 0 | } |
2805 | 0 | if (lu->sig == EVP_PKEY_RSA_PSS) { |
2806 | 0 | if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 |
2807 | 0 | || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) { |
2808 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
2809 | 0 | goto err; |
2810 | 0 | } |
2811 | 0 | } |
2812 | 0 | tbslen = construct_key_exchange_tbs(s, &tbs, |
2813 | 0 | s->init_buf->data + paramoffset, |
2814 | 0 | paramlen); |
2815 | 0 | if (tbslen == 0) { |
2816 | | /* SSLfatal() already called */ |
2817 | 0 | goto err; |
2818 | 0 | } |
2819 | | |
2820 | 0 | if (EVP_DigestSign(md_ctx, NULL, &siglen, tbs, tbslen) <=0 |
2821 | 0 | || !WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1) |
2822 | 0 | || EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen) <= 0 |
2823 | 0 | || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2) |
2824 | 0 | || sigbytes1 != sigbytes2) { |
2825 | 0 | OPENSSL_free(tbs); |
2826 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2827 | 0 | goto err; |
2828 | 0 | } |
2829 | 0 | OPENSSL_free(tbs); |
2830 | 0 | } |
2831 | | |
2832 | 0 | ret = CON_FUNC_SUCCESS; |
2833 | 0 | err: |
2834 | 0 | EVP_PKEY_free(pkdh); |
2835 | 0 | OPENSSL_free(encodedPoint); |
2836 | 0 | EVP_MD_CTX_free(md_ctx); |
2837 | 0 | if (freer) { |
2838 | 0 | BN_free(r[0]); |
2839 | 0 | BN_free(r[1]); |
2840 | 0 | BN_free(r[2]); |
2841 | 0 | BN_free(r[3]); |
2842 | 0 | } |
2843 | 0 | return ret; |
2844 | 0 | } |
2845 | | |
2846 | | CON_FUNC_RETURN tls_construct_certificate_request(SSL_CONNECTION *s, |
2847 | | WPACKET *pkt) |
2848 | 0 | { |
2849 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
2850 | | /* Send random context when doing post-handshake auth */ |
2851 | 0 | if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { |
2852 | 0 | OPENSSL_free(s->pha_context); |
2853 | 0 | s->pha_context_len = 32; |
2854 | 0 | if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL) { |
2855 | 0 | s->pha_context_len = 0; |
2856 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2857 | 0 | return CON_FUNC_ERROR; |
2858 | 0 | } |
2859 | 0 | if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx, |
2860 | 0 | s->pha_context, s->pha_context_len, 0) <= 0 |
2861 | 0 | || !WPACKET_sub_memcpy_u8(pkt, s->pha_context, |
2862 | 0 | s->pha_context_len)) { |
2863 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2864 | 0 | return CON_FUNC_ERROR; |
2865 | 0 | } |
2866 | | /* reset the handshake hash back to just after the ClientFinished */ |
2867 | 0 | if (!tls13_restore_handshake_digest_for_pha(s)) { |
2868 | | /* SSLfatal() already called */ |
2869 | 0 | return CON_FUNC_ERROR; |
2870 | 0 | } |
2871 | 0 | } else { |
2872 | 0 | if (!WPACKET_put_bytes_u8(pkt, 0)) { |
2873 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2874 | 0 | return CON_FUNC_ERROR; |
2875 | 0 | } |
2876 | 0 | } |
2877 | | |
2878 | 0 | if (!tls_construct_extensions(s, pkt, |
2879 | 0 | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL, |
2880 | 0 | 0)) { |
2881 | | /* SSLfatal() already called */ |
2882 | 0 | return CON_FUNC_ERROR; |
2883 | 0 | } |
2884 | 0 | goto done; |
2885 | 0 | } |
2886 | | |
2887 | | /* get the list of acceptable cert types */ |
2888 | 0 | if (!WPACKET_start_sub_packet_u8(pkt) |
2889 | 0 | || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) { |
2890 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2891 | 0 | return CON_FUNC_ERROR; |
2892 | 0 | } |
2893 | | |
2894 | 0 | if (SSL_USE_SIGALGS(s)) { |
2895 | 0 | const uint16_t *psigs; |
2896 | 0 | size_t nl = tls12_get_psigalgs(s, 1, &psigs); |
2897 | |
|
2898 | 0 | if (!WPACKET_start_sub_packet_u16(pkt) |
2899 | 0 | || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH) |
2900 | 0 | || !tls12_copy_sigalgs(s, pkt, psigs, nl) |
2901 | 0 | || !WPACKET_close(pkt)) { |
2902 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2903 | 0 | return CON_FUNC_ERROR; |
2904 | 0 | } |
2905 | 0 | } |
2906 | | |
2907 | 0 | if (!construct_ca_names(s, get_ca_names(s), pkt)) { |
2908 | | /* SSLfatal() already called */ |
2909 | 0 | return CON_FUNC_ERROR; |
2910 | 0 | } |
2911 | | |
2912 | 0 | done: |
2913 | 0 | s->certreqs_sent++; |
2914 | 0 | s->s3.tmp.cert_request = 1; |
2915 | 0 | return CON_FUNC_SUCCESS; |
2916 | 0 | } |
2917 | | |
2918 | | static int tls_process_cke_psk_preamble(SSL_CONNECTION *s, PACKET *pkt) |
2919 | 0 | { |
2920 | 0 | #ifndef OPENSSL_NO_PSK |
2921 | 0 | unsigned char psk[PSK_MAX_PSK_LEN]; |
2922 | 0 | size_t psklen; |
2923 | 0 | PACKET psk_identity; |
2924 | |
|
2925 | 0 | if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) { |
2926 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
2927 | 0 | return 0; |
2928 | 0 | } |
2929 | 0 | if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) { |
2930 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DATA_LENGTH_TOO_LONG); |
2931 | 0 | return 0; |
2932 | 0 | } |
2933 | 0 | if (s->psk_server_callback == NULL) { |
2934 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_SERVER_CB); |
2935 | 0 | return 0; |
2936 | 0 | } |
2937 | | |
2938 | 0 | if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) { |
2939 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2940 | 0 | return 0; |
2941 | 0 | } |
2942 | | |
2943 | 0 | psklen = s->psk_server_callback(SSL_CONNECTION_GET_USER_SSL(s), |
2944 | 0 | s->session->psk_identity, |
2945 | 0 | psk, sizeof(psk)); |
2946 | |
|
2947 | 0 | if (psklen > PSK_MAX_PSK_LEN) { |
2948 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2949 | 0 | return 0; |
2950 | 0 | } else if (psklen == 0) { |
2951 | | /* |
2952 | | * PSK related to the given identity not found |
2953 | | */ |
2954 | 0 | SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY, SSL_R_PSK_IDENTITY_NOT_FOUND); |
2955 | 0 | return 0; |
2956 | 0 | } |
2957 | | |
2958 | 0 | OPENSSL_free(s->s3.tmp.psk); |
2959 | 0 | s->s3.tmp.psk = OPENSSL_memdup(psk, psklen); |
2960 | 0 | OPENSSL_cleanse(psk, psklen); |
2961 | |
|
2962 | 0 | if (s->s3.tmp.psk == NULL) { |
2963 | 0 | s->s3.tmp.psklen = 0; |
2964 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
2965 | 0 | return 0; |
2966 | 0 | } |
2967 | | |
2968 | 0 | s->s3.tmp.psklen = psklen; |
2969 | |
|
2970 | 0 | return 1; |
2971 | | #else |
2972 | | /* Should never happen */ |
2973 | | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2974 | | return 0; |
2975 | | #endif |
2976 | 0 | } |
2977 | | |
2978 | | static int tls_process_cke_rsa(SSL_CONNECTION *s, PACKET *pkt) |
2979 | 0 | { |
2980 | 0 | size_t outlen; |
2981 | 0 | PACKET enc_premaster; |
2982 | 0 | EVP_PKEY *rsa = NULL; |
2983 | 0 | unsigned char *rsa_decrypt = NULL; |
2984 | 0 | int ret = 0; |
2985 | 0 | EVP_PKEY_CTX *ctx = NULL; |
2986 | 0 | OSSL_PARAM params[3], *p = params; |
2987 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
2988 | |
|
2989 | 0 | rsa = s->cert->pkeys[SSL_PKEY_RSA].privatekey; |
2990 | 0 | if (rsa == NULL) { |
2991 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_RSA_CERTIFICATE); |
2992 | 0 | return 0; |
2993 | 0 | } |
2994 | | |
2995 | | /* SSLv3 and pre-standard DTLS omit the length bytes. */ |
2996 | 0 | if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) { |
2997 | 0 | enc_premaster = *pkt; |
2998 | 0 | } else { |
2999 | 0 | if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster) |
3000 | 0 | || PACKET_remaining(pkt) != 0) { |
3001 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
3002 | 0 | return 0; |
3003 | 0 | } |
3004 | 0 | } |
3005 | | |
3006 | 0 | outlen = SSL_MAX_MASTER_KEY_LENGTH; |
3007 | 0 | rsa_decrypt = OPENSSL_malloc(outlen); |
3008 | 0 | if (rsa_decrypt == NULL) { |
3009 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
3010 | 0 | return 0; |
3011 | 0 | } |
3012 | | |
3013 | 0 | ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, rsa, sctx->propq); |
3014 | 0 | if (ctx == NULL) { |
3015 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
3016 | 0 | goto err; |
3017 | 0 | } |
3018 | | |
3019 | | /* |
3020 | | * We must not leak whether a decryption failure occurs because of |
3021 | | * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246, |
3022 | | * section 7.4.7.1). We use the special padding type |
3023 | | * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automatically decrypt the |
3024 | | * RSA, check the padding and check that the client version is as expected |
3025 | | * in the premaster secret. If any of that fails then the function appears |
3026 | | * to return successfully but with a random result. The call below could |
3027 | | * still fail if the input is publicly invalid. |
3028 | | * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1 |
3029 | | */ |
3030 | 0 | if (EVP_PKEY_decrypt_init(ctx) <= 0 |
3031 | 0 | || EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_WITH_TLS_PADDING) <= 0) { |
3032 | 0 | SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED); |
3033 | 0 | goto err; |
3034 | 0 | } |
3035 | | |
3036 | 0 | *p++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, |
3037 | 0 | (unsigned int *)&s->client_version); |
3038 | 0 | if ((s->options & SSL_OP_TLS_ROLLBACK_BUG) != 0) |
3039 | 0 | *p++ = OSSL_PARAM_construct_uint( |
3040 | 0 | OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, |
3041 | 0 | (unsigned int *)&s->version); |
3042 | 0 | *p++ = OSSL_PARAM_construct_end(); |
3043 | |
|
3044 | 0 | if (!EVP_PKEY_CTX_set_params(ctx, params) |
3045 | 0 | || EVP_PKEY_decrypt(ctx, rsa_decrypt, &outlen, |
3046 | 0 | PACKET_data(&enc_premaster), |
3047 | 0 | PACKET_remaining(&enc_premaster)) <= 0) { |
3048 | 0 | SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED); |
3049 | 0 | goto err; |
3050 | 0 | } |
3051 | | |
3052 | | /* |
3053 | | * This test should never fail (otherwise we should have failed above) but |
3054 | | * we double check anyway. |
3055 | | */ |
3056 | 0 | if (outlen != SSL_MAX_MASTER_KEY_LENGTH) { |
3057 | 0 | OPENSSL_cleanse(rsa_decrypt, SSL_MAX_MASTER_KEY_LENGTH); |
3058 | 0 | SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED); |
3059 | 0 | goto err; |
3060 | 0 | } |
3061 | | |
3062 | | /* Also cleanses rsa_decrypt (on success or failure) */ |
3063 | 0 | if (!ssl_generate_master_secret(s, rsa_decrypt, outlen, 0)) { |
3064 | | /* SSLfatal() already called */ |
3065 | 0 | goto err; |
3066 | 0 | } |
3067 | | |
3068 | 0 | ret = 1; |
3069 | 0 | err: |
3070 | 0 | OPENSSL_free(rsa_decrypt); |
3071 | 0 | EVP_PKEY_CTX_free(ctx); |
3072 | 0 | return ret; |
3073 | 0 | } |
3074 | | |
3075 | | static int tls_process_cke_dhe(SSL_CONNECTION *s, PACKET *pkt) |
3076 | 0 | { |
3077 | 0 | EVP_PKEY *skey = NULL; |
3078 | 0 | unsigned int i; |
3079 | 0 | const unsigned char *data; |
3080 | 0 | EVP_PKEY *ckey = NULL; |
3081 | 0 | int ret = 0; |
3082 | |
|
3083 | 0 | if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) { |
3084 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); |
3085 | 0 | goto err; |
3086 | 0 | } |
3087 | 0 | skey = s->s3.tmp.pkey; |
3088 | 0 | if (skey == NULL) { |
3089 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY); |
3090 | 0 | goto err; |
3091 | 0 | } |
3092 | | |
3093 | 0 | if (PACKET_remaining(pkt) == 0L) { |
3094 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_MISSING_TMP_DH_KEY); |
3095 | 0 | goto err; |
3096 | 0 | } |
3097 | 0 | if (!PACKET_get_bytes(pkt, &data, i)) { |
3098 | | /* We already checked we have enough data */ |
3099 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3100 | 0 | goto err; |
3101 | 0 | } |
3102 | 0 | ckey = EVP_PKEY_new(); |
3103 | 0 | if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) { |
3104 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED); |
3105 | 0 | goto err; |
3106 | 0 | } |
3107 | | |
3108 | 0 | if (EVP_PKEY_set1_encoded_public_key(ckey, data, i) <= 0) { |
3109 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
3110 | 0 | goto err; |
3111 | 0 | } |
3112 | | |
3113 | 0 | if (ssl_derive(s, skey, ckey, 1) == 0) { |
3114 | | /* SSLfatal() already called */ |
3115 | 0 | goto err; |
3116 | 0 | } |
3117 | | |
3118 | 0 | ret = 1; |
3119 | 0 | EVP_PKEY_free(s->s3.tmp.pkey); |
3120 | 0 | s->s3.tmp.pkey = NULL; |
3121 | 0 | err: |
3122 | 0 | EVP_PKEY_free(ckey); |
3123 | 0 | return ret; |
3124 | 0 | } |
3125 | | |
3126 | | static int tls_process_cke_ecdhe(SSL_CONNECTION *s, PACKET *pkt) |
3127 | 0 | { |
3128 | 0 | EVP_PKEY *skey = s->s3.tmp.pkey; |
3129 | 0 | EVP_PKEY *ckey = NULL; |
3130 | 0 | int ret = 0; |
3131 | |
|
3132 | 0 | if (PACKET_remaining(pkt) == 0L) { |
3133 | | /* We don't support ECDH client auth */ |
3134 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_TMP_ECDH_KEY); |
3135 | 0 | goto err; |
3136 | 0 | } else { |
3137 | 0 | unsigned int i; |
3138 | 0 | const unsigned char *data; |
3139 | | |
3140 | | /* |
3141 | | * Get client's public key from encoded point in the |
3142 | | * ClientKeyExchange message. |
3143 | | */ |
3144 | | |
3145 | | /* |
3146 | | * Get encoded point length |
3147 | | * empty key should be handled here |
3148 | | */ |
3149 | 0 | if (!PACKET_get_1(pkt, &i) || i == 0 || !PACKET_get_bytes(pkt, &data, i) |
3150 | 0 | || PACKET_remaining(pkt) != 0) { |
3151 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
3152 | 0 | goto err; |
3153 | 0 | } |
3154 | 0 | if (skey == NULL) { |
3155 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_ECDH_KEY); |
3156 | 0 | goto err; |
3157 | 0 | } |
3158 | | |
3159 | 0 | ckey = EVP_PKEY_new(); |
3160 | 0 | if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) { |
3161 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED); |
3162 | 0 | goto err; |
3163 | 0 | } |
3164 | | |
3165 | 0 | if (EVP_PKEY_set1_encoded_public_key(ckey, data, i) <= 0) { |
3166 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
3167 | 0 | goto err; |
3168 | 0 | } |
3169 | 0 | } |
3170 | | |
3171 | 0 | if (ssl_derive(s, skey, ckey, 1) == 0) { |
3172 | | /* SSLfatal() already called */ |
3173 | 0 | goto err; |
3174 | 0 | } |
3175 | | |
3176 | 0 | ret = 1; |
3177 | 0 | EVP_PKEY_free(s->s3.tmp.pkey); |
3178 | 0 | s->s3.tmp.pkey = NULL; |
3179 | 0 | err: |
3180 | 0 | EVP_PKEY_free(ckey); |
3181 | |
|
3182 | 0 | return ret; |
3183 | 0 | } |
3184 | | |
3185 | | static int tls_process_cke_srp(SSL_CONNECTION *s, PACKET *pkt) |
3186 | 0 | { |
3187 | 0 | #ifndef OPENSSL_NO_SRP |
3188 | 0 | unsigned int i; |
3189 | 0 | const unsigned char *data; |
3190 | |
|
3191 | 0 | if (!PACKET_get_net_2(pkt, &i) |
3192 | 0 | || !PACKET_get_bytes(pkt, &data, i)) { |
3193 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRP_A_LENGTH); |
3194 | 0 | return 0; |
3195 | 0 | } |
3196 | 0 | if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) { |
3197 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB); |
3198 | 0 | return 0; |
3199 | 0 | } |
3200 | 0 | if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) { |
3201 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRP_PARAMETERS); |
3202 | 0 | return 0; |
3203 | 0 | } |
3204 | 0 | OPENSSL_free(s->session->srp_username); |
3205 | 0 | s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login); |
3206 | 0 | if (s->session->srp_username == NULL) { |
3207 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
3208 | 0 | return 0; |
3209 | 0 | } |
3210 | | |
3211 | 0 | if (!srp_generate_server_master_secret(s)) { |
3212 | | /* SSLfatal() already called */ |
3213 | 0 | return 0; |
3214 | 0 | } |
3215 | | |
3216 | 0 | return 1; |
3217 | | #else |
3218 | | /* Should never happen */ |
3219 | | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3220 | | return 0; |
3221 | | #endif |
3222 | 0 | } |
3223 | | |
3224 | | static int tls_process_cke_gost(SSL_CONNECTION *s, PACKET *pkt) |
3225 | 0 | { |
3226 | 0 | #ifndef OPENSSL_NO_GOST |
3227 | 0 | EVP_PKEY_CTX *pkey_ctx; |
3228 | 0 | EVP_PKEY *client_pub_pkey = NULL, *pk = NULL; |
3229 | 0 | unsigned char premaster_secret[32]; |
3230 | 0 | const unsigned char *start; |
3231 | 0 | size_t outlen = sizeof(premaster_secret), inlen; |
3232 | 0 | unsigned long alg_a; |
3233 | 0 | GOST_KX_MESSAGE *pKX = NULL; |
3234 | 0 | const unsigned char *ptr; |
3235 | 0 | int ret = 0; |
3236 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
3237 | | |
3238 | | /* Get our certificate private key */ |
3239 | 0 | alg_a = s->s3.tmp.new_cipher->algorithm_auth; |
3240 | 0 | if (alg_a & SSL_aGOST12) { |
3241 | | /* |
3242 | | * New GOST ciphersuites have SSL_aGOST01 bit too |
3243 | | */ |
3244 | 0 | pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey; |
3245 | 0 | if (pk == NULL) { |
3246 | 0 | pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey; |
3247 | 0 | } |
3248 | 0 | if (pk == NULL) { |
3249 | 0 | pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey; |
3250 | 0 | } |
3251 | 0 | } else if (alg_a & SSL_aGOST01) { |
3252 | 0 | pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey; |
3253 | 0 | } |
3254 | |
|
3255 | 0 | pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq); |
3256 | 0 | if (pkey_ctx == NULL) { |
3257 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
3258 | 0 | return 0; |
3259 | 0 | } |
3260 | 0 | if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) { |
3261 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3262 | 0 | goto err; |
3263 | 0 | } |
3264 | | /* |
3265 | | * If client certificate is present and is of the same type, maybe |
3266 | | * use it for key exchange. Don't mind errors from |
3267 | | * EVP_PKEY_derive_set_peer, because it is completely valid to use a |
3268 | | * client certificate for authorization only. |
3269 | | */ |
3270 | 0 | client_pub_pkey = tls_get_peer_pkey(s); |
3271 | 0 | if (client_pub_pkey) { |
3272 | 0 | if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0) |
3273 | 0 | ERR_clear_error(); |
3274 | 0 | } |
3275 | |
|
3276 | 0 | ptr = PACKET_data(pkt); |
3277 | | /* Some implementations provide extra data in the opaqueBlob |
3278 | | * We have nothing to do with this blob so we just skip it */ |
3279 | 0 | pKX = d2i_GOST_KX_MESSAGE(NULL, &ptr, (long)PACKET_remaining(pkt)); |
3280 | 0 | if (pKX == NULL |
3281 | 0 | || pKX->kxBlob == NULL |
3282 | 0 | || ASN1_TYPE_get(pKX->kxBlob) != V_ASN1_SEQUENCE) { |
3283 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED); |
3284 | 0 | goto err; |
3285 | 0 | } |
3286 | | |
3287 | 0 | if (!PACKET_forward(pkt, ptr - PACKET_data(pkt))) { |
3288 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED); |
3289 | 0 | goto err; |
3290 | 0 | } |
3291 | | |
3292 | 0 | if (PACKET_remaining(pkt) != 0) { |
3293 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED); |
3294 | 0 | goto err; |
3295 | 0 | } |
3296 | | |
3297 | 0 | inlen = pKX->kxBlob->value.sequence->length; |
3298 | 0 | start = pKX->kxBlob->value.sequence->data; |
3299 | |
|
3300 | 0 | if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start, |
3301 | 0 | inlen) <= 0) { |
3302 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED); |
3303 | 0 | goto err; |
3304 | 0 | } |
3305 | | /* Generate master secret */ |
3306 | 0 | if (!ssl_generate_master_secret(s, premaster_secret, outlen, 0)) { |
3307 | | /* SSLfatal() already called */ |
3308 | 0 | goto err; |
3309 | 0 | } |
3310 | | /* Check if pubkey from client certificate was used */ |
3311 | 0 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, |
3312 | 0 | NULL) > 0) |
3313 | 0 | s->statem.no_cert_verify = 1; |
3314 | |
|
3315 | 0 | ret = 1; |
3316 | 0 | err: |
3317 | 0 | EVP_PKEY_CTX_free(pkey_ctx); |
3318 | 0 | GOST_KX_MESSAGE_free(pKX); |
3319 | 0 | return ret; |
3320 | | #else |
3321 | | /* Should never happen */ |
3322 | | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3323 | | return 0; |
3324 | | #endif |
3325 | 0 | } |
3326 | | |
3327 | | static int tls_process_cke_gost18(SSL_CONNECTION *s, PACKET *pkt) |
3328 | 0 | { |
3329 | 0 | #ifndef OPENSSL_NO_GOST |
3330 | 0 | unsigned char rnd_dgst[32]; |
3331 | 0 | EVP_PKEY_CTX *pkey_ctx = NULL; |
3332 | 0 | EVP_PKEY *pk = NULL; |
3333 | 0 | unsigned char premaster_secret[32]; |
3334 | 0 | const unsigned char *start = NULL; |
3335 | 0 | size_t outlen = sizeof(premaster_secret), inlen = 0; |
3336 | 0 | int ret = 0; |
3337 | 0 | int cipher_nid = ossl_gost18_cke_cipher_nid(s); |
3338 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
3339 | |
|
3340 | 0 | if (cipher_nid == NID_undef) { |
3341 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3342 | 0 | return 0; |
3343 | 0 | } |
3344 | | |
3345 | 0 | if (ossl_gost_ukm(s, rnd_dgst) <= 0) { |
3346 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3347 | 0 | goto err; |
3348 | 0 | } |
3349 | | |
3350 | | /* Get our certificate private key */ |
3351 | 0 | pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey != NULL ? |
3352 | 0 | s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey : |
3353 | 0 | s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey; |
3354 | 0 | if (pk == NULL) { |
3355 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE); |
3356 | 0 | goto err; |
3357 | 0 | } |
3358 | | |
3359 | 0 | pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq); |
3360 | 0 | if (pkey_ctx == NULL) { |
3361 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
3362 | 0 | goto err; |
3363 | 0 | } |
3364 | 0 | if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) { |
3365 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3366 | 0 | goto err; |
3367 | 0 | } |
3368 | | |
3369 | | /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */ |
3370 | 0 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT, |
3371 | 0 | EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst) <= 0) { |
3372 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); |
3373 | 0 | goto err; |
3374 | 0 | } |
3375 | | |
3376 | 0 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT, |
3377 | 0 | EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL) <= 0) { |
3378 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); |
3379 | 0 | goto err; |
3380 | 0 | } |
3381 | 0 | inlen = PACKET_remaining(pkt); |
3382 | 0 | start = PACKET_data(pkt); |
3383 | |
|
3384 | 0 | if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) { |
3385 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED); |
3386 | 0 | goto err; |
3387 | 0 | } |
3388 | | /* Generate master secret */ |
3389 | 0 | if (!ssl_generate_master_secret(s, premaster_secret, outlen, 0)) { |
3390 | | /* SSLfatal() already called */ |
3391 | 0 | goto err; |
3392 | 0 | } |
3393 | 0 | ret = 1; |
3394 | |
|
3395 | 0 | err: |
3396 | 0 | EVP_PKEY_CTX_free(pkey_ctx); |
3397 | 0 | return ret; |
3398 | | #else |
3399 | | /* Should never happen */ |
3400 | | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3401 | | return 0; |
3402 | | #endif |
3403 | 0 | } |
3404 | | |
3405 | | MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL_CONNECTION *s, |
3406 | | PACKET *pkt) |
3407 | 0 | { |
3408 | 0 | unsigned long alg_k; |
3409 | |
|
3410 | 0 | alg_k = s->s3.tmp.new_cipher->algorithm_mkey; |
3411 | | |
3412 | | /* For PSK parse and retrieve identity, obtain PSK key */ |
3413 | 0 | if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) { |
3414 | | /* SSLfatal() already called */ |
3415 | 0 | goto err; |
3416 | 0 | } |
3417 | | |
3418 | 0 | if (alg_k & SSL_kPSK) { |
3419 | | /* Identity extracted earlier: should be nothing left */ |
3420 | 0 | if (PACKET_remaining(pkt) != 0) { |
3421 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
3422 | 0 | goto err; |
3423 | 0 | } |
3424 | | /* PSK handled by ssl_generate_master_secret */ |
3425 | 0 | if (!ssl_generate_master_secret(s, NULL, 0, 0)) { |
3426 | | /* SSLfatal() already called */ |
3427 | 0 | goto err; |
3428 | 0 | } |
3429 | 0 | } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) { |
3430 | 0 | if (!tls_process_cke_rsa(s, pkt)) { |
3431 | | /* SSLfatal() already called */ |
3432 | 0 | goto err; |
3433 | 0 | } |
3434 | 0 | } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
3435 | 0 | if (!tls_process_cke_dhe(s, pkt)) { |
3436 | | /* SSLfatal() already called */ |
3437 | 0 | goto err; |
3438 | 0 | } |
3439 | 0 | } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
3440 | 0 | if (!tls_process_cke_ecdhe(s, pkt)) { |
3441 | | /* SSLfatal() already called */ |
3442 | 0 | goto err; |
3443 | 0 | } |
3444 | 0 | } else if (alg_k & SSL_kSRP) { |
3445 | 0 | if (!tls_process_cke_srp(s, pkt)) { |
3446 | | /* SSLfatal() already called */ |
3447 | 0 | goto err; |
3448 | 0 | } |
3449 | 0 | } else if (alg_k & SSL_kGOST) { |
3450 | 0 | if (!tls_process_cke_gost(s, pkt)) { |
3451 | | /* SSLfatal() already called */ |
3452 | 0 | goto err; |
3453 | 0 | } |
3454 | 0 | } else if (alg_k & SSL_kGOST18) { |
3455 | 0 | if (!tls_process_cke_gost18(s, pkt)) { |
3456 | | /* SSLfatal() already called */ |
3457 | 0 | goto err; |
3458 | 0 | } |
3459 | 0 | } else { |
3460 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE); |
3461 | 0 | goto err; |
3462 | 0 | } |
3463 | | |
3464 | 0 | return MSG_PROCESS_CONTINUE_PROCESSING; |
3465 | 0 | err: |
3466 | 0 | #ifndef OPENSSL_NO_PSK |
3467 | 0 | OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen); |
3468 | 0 | s->s3.tmp.psk = NULL; |
3469 | 0 | s->s3.tmp.psklen = 0; |
3470 | 0 | #endif |
3471 | 0 | return MSG_PROCESS_ERROR; |
3472 | 0 | } |
3473 | | |
3474 | | WORK_STATE tls_post_process_client_key_exchange(SSL_CONNECTION *s, |
3475 | | WORK_STATE wst) |
3476 | 0 | { |
3477 | | #ifndef OPENSSL_NO_SCTP |
3478 | | if (wst == WORK_MORE_A) { |
3479 | | if (SSL_CONNECTION_IS_DTLS(s)) { |
3480 | | unsigned char sctpauthkey[64]; |
3481 | | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; |
3482 | | size_t labellen; |
3483 | | /* |
3484 | | * Add new shared key for SCTP-Auth, will be ignored if no SCTP |
3485 | | * used. |
3486 | | */ |
3487 | | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
3488 | | sizeof(DTLS1_SCTP_AUTH_LABEL)); |
3489 | | |
3490 | | /* Don't include the terminating zero. */ |
3491 | | labellen = sizeof(labelbuffer) - 1; |
3492 | | if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG) |
3493 | | labellen += 1; |
3494 | | |
3495 | | if (SSL_export_keying_material(SSL_CONNECTION_GET_SSL(s), |
3496 | | sctpauthkey, |
3497 | | sizeof(sctpauthkey), labelbuffer, |
3498 | | labellen, NULL, 0, |
3499 | | 0) <= 0) { |
3500 | | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3501 | | return WORK_ERROR; |
3502 | | } |
3503 | | |
3504 | | BIO_ctrl(s->wbio, BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, |
3505 | | sizeof(sctpauthkey), sctpauthkey); |
3506 | | } |
3507 | | } |
3508 | | #endif |
3509 | |
|
3510 | 0 | if (s->statem.no_cert_verify || !received_client_cert(s)) { |
3511 | | /* |
3512 | | * No certificate verify or no peer certificate so we no longer need |
3513 | | * the handshake_buffer |
3514 | | */ |
3515 | 0 | if (!ssl3_digest_cached_records(s, 0)) { |
3516 | | /* SSLfatal() already called */ |
3517 | 0 | return WORK_ERROR; |
3518 | 0 | } |
3519 | 0 | return WORK_FINISHED_CONTINUE; |
3520 | 0 | } else { |
3521 | 0 | if (!s->s3.handshake_buffer) { |
3522 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3523 | 0 | return WORK_ERROR; |
3524 | 0 | } |
3525 | | /* |
3526 | | * For sigalgs freeze the handshake buffer. If we support |
3527 | | * extms we've done this already so this is a no-op |
3528 | | */ |
3529 | 0 | if (!ssl3_digest_cached_records(s, 1)) { |
3530 | | /* SSLfatal() already called */ |
3531 | 0 | return WORK_ERROR; |
3532 | 0 | } |
3533 | 0 | } |
3534 | | |
3535 | 0 | return WORK_FINISHED_CONTINUE; |
3536 | 0 | } |
3537 | | |
3538 | | MSG_PROCESS_RETURN tls_process_client_rpk(SSL_CONNECTION *sc, PACKET *pkt) |
3539 | 0 | { |
3540 | 0 | MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR; |
3541 | 0 | SSL_SESSION *new_sess = NULL; |
3542 | 0 | EVP_PKEY *peer_rpk = NULL; |
3543 | |
|
3544 | 0 | if (!tls_process_rpk(sc, pkt, &peer_rpk)) { |
3545 | | /* SSLfatal already called */ |
3546 | 0 | goto err; |
3547 | 0 | } |
3548 | | |
3549 | 0 | if (peer_rpk == NULL) { |
3550 | 0 | if ((sc->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) |
3551 | 0 | && (sc->verify_mode & SSL_VERIFY_PEER)) { |
3552 | 0 | SSLfatal(sc, SSL_AD_CERTIFICATE_REQUIRED, |
3553 | 0 | SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
3554 | 0 | goto err; |
3555 | 0 | } |
3556 | 0 | } else { |
3557 | 0 | if (ssl_verify_rpk(sc, peer_rpk) <= 0) { |
3558 | 0 | SSLfatal(sc, ssl_x509err2alert(sc->verify_result), |
3559 | 0 | SSL_R_CERTIFICATE_VERIFY_FAILED); |
3560 | 0 | goto err; |
3561 | 0 | } |
3562 | 0 | } |
3563 | | |
3564 | | /* |
3565 | | * Sessions must be immutable once they go into the session cache. Otherwise |
3566 | | * we can get multi-thread problems. Therefore we don't "update" sessions, |
3567 | | * we replace them with a duplicate. Here, we need to do this every time |
3568 | | * a new RPK (or certificate) is received via post-handshake authentication, |
3569 | | * as the session may have already gone into the session cache. |
3570 | | */ |
3571 | | |
3572 | 0 | if (sc->post_handshake_auth == SSL_PHA_REQUESTED) { |
3573 | 0 | if ((new_sess = ssl_session_dup(sc->session, 0)) == NULL) { |
3574 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE); |
3575 | 0 | goto err; |
3576 | 0 | } |
3577 | | |
3578 | 0 | SSL_SESSION_free(sc->session); |
3579 | 0 | sc->session = new_sess; |
3580 | 0 | } |
3581 | | |
3582 | | /* Ensure there is no peer/peer_chain */ |
3583 | 0 | X509_free(sc->session->peer); |
3584 | 0 | sc->session->peer = NULL; |
3585 | 0 | sk_X509_pop_free(sc->session->peer_chain, X509_free); |
3586 | 0 | sc->session->peer_chain = NULL; |
3587 | | /* Save RPK */ |
3588 | 0 | EVP_PKEY_free(sc->session->peer_rpk); |
3589 | 0 | sc->session->peer_rpk = peer_rpk; |
3590 | 0 | peer_rpk = NULL; |
3591 | |
|
3592 | 0 | sc->session->verify_result = sc->verify_result; |
3593 | | |
3594 | | /* |
3595 | | * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE |
3596 | | * message |
3597 | | */ |
3598 | 0 | if (SSL_CONNECTION_IS_TLS13(sc)) { |
3599 | 0 | if (!ssl3_digest_cached_records(sc, 1)) { |
3600 | | /* SSLfatal() already called */ |
3601 | 0 | goto err; |
3602 | 0 | } |
3603 | | |
3604 | | /* Save the current hash state for when we receive the CertificateVerify */ |
3605 | 0 | if (!ssl_handshake_hash(sc, sc->cert_verify_hash, |
3606 | 0 | sizeof(sc->cert_verify_hash), |
3607 | 0 | &sc->cert_verify_hash_len)) { |
3608 | 0 | /* SSLfatal() already called */; |
3609 | 0 | goto err; |
3610 | 0 | } |
3611 | | |
3612 | | /* resend session tickets */ |
3613 | 0 | sc->sent_tickets = 0; |
3614 | 0 | } |
3615 | | |
3616 | 0 | ret = MSG_PROCESS_CONTINUE_READING; |
3617 | |
|
3618 | 0 | err: |
3619 | 0 | EVP_PKEY_free(peer_rpk); |
3620 | 0 | return ret; |
3621 | 0 | } |
3622 | | |
3623 | | MSG_PROCESS_RETURN tls_process_client_certificate(SSL_CONNECTION *s, |
3624 | | PACKET *pkt) |
3625 | 0 | { |
3626 | 0 | int i; |
3627 | 0 | MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR; |
3628 | 0 | X509 *x = NULL; |
3629 | 0 | unsigned long l; |
3630 | 0 | const unsigned char *certstart, *certbytes; |
3631 | 0 | STACK_OF(X509) *sk = NULL; |
3632 | 0 | PACKET spkt, context; |
3633 | 0 | size_t chainidx; |
3634 | 0 | SSL_SESSION *new_sess = NULL; |
3635 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
3636 | | |
3637 | | /* |
3638 | | * To get this far we must have read encrypted data from the client. We no |
3639 | | * longer tolerate unencrypted alerts. This is ignored if less than TLSv1.3 |
3640 | | */ |
3641 | 0 | if (s->rlayer.rrlmethod->set_plain_alerts != NULL) |
3642 | 0 | s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 0); |
3643 | |
|
3644 | 0 | if (s->ext.client_cert_type == TLSEXT_cert_type_rpk) |
3645 | 0 | return tls_process_client_rpk(s, pkt); |
3646 | | |
3647 | 0 | if (s->ext.client_cert_type != TLSEXT_cert_type_x509) { |
3648 | 0 | SSLfatal(s, SSL_AD_UNSUPPORTED_CERTIFICATE, |
3649 | 0 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
3650 | 0 | goto err; |
3651 | 0 | } |
3652 | | |
3653 | 0 | if ((sk = sk_X509_new_null()) == NULL) { |
3654 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
3655 | 0 | goto err; |
3656 | 0 | } |
3657 | | |
3658 | 0 | if (SSL_CONNECTION_IS_TLS13(s) |
3659 | 0 | && (!PACKET_get_length_prefixed_1(pkt, &context) |
3660 | 0 | || (s->pha_context == NULL && PACKET_remaining(&context) != 0) |
3661 | 0 | || (s->pha_context != NULL |
3662 | 0 | && !PACKET_equal(&context, s->pha_context, |
3663 | 0 | s->pha_context_len)))) { |
3664 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT); |
3665 | 0 | goto err; |
3666 | 0 | } |
3667 | | |
3668 | 0 | if (!PACKET_get_length_prefixed_3(pkt, &spkt) |
3669 | 0 | || PACKET_remaining(pkt) != 0) { |
3670 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
3671 | 0 | goto err; |
3672 | 0 | } |
3673 | | |
3674 | 0 | for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) { |
3675 | 0 | if (!PACKET_get_net_3(&spkt, &l) |
3676 | 0 | || !PACKET_get_bytes(&spkt, &certbytes, l)) { |
3677 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH); |
3678 | 0 | goto err; |
3679 | 0 | } |
3680 | | |
3681 | 0 | certstart = certbytes; |
3682 | 0 | x = X509_new_ex(sctx->libctx, sctx->propq); |
3683 | 0 | if (x == NULL) { |
3684 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_X509_LIB); |
3685 | 0 | goto err; |
3686 | 0 | } |
3687 | 0 | if (d2i_X509(&x, (const unsigned char **)&certbytes, l) == NULL) { |
3688 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB); |
3689 | 0 | goto err; |
3690 | 0 | } |
3691 | | |
3692 | 0 | if (certbytes != (certstart + l)) { |
3693 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH); |
3694 | 0 | goto err; |
3695 | 0 | } |
3696 | | |
3697 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
3698 | 0 | RAW_EXTENSION *rawexts = NULL; |
3699 | 0 | PACKET extensions; |
3700 | |
|
3701 | 0 | if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) { |
3702 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH); |
3703 | 0 | goto err; |
3704 | 0 | } |
3705 | 0 | if (!tls_collect_extensions(s, &extensions, |
3706 | 0 | SSL_EXT_TLS1_3_CERTIFICATE, &rawexts, |
3707 | 0 | NULL, chainidx == 0) |
3708 | 0 | || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE, |
3709 | 0 | rawexts, x, chainidx, |
3710 | 0 | PACKET_remaining(&spkt) == 0)) { |
3711 | 0 | OPENSSL_free(rawexts); |
3712 | 0 | goto err; |
3713 | 0 | } |
3714 | 0 | OPENSSL_free(rawexts); |
3715 | 0 | } |
3716 | | |
3717 | 0 | if (!sk_X509_push(sk, x)) { |
3718 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
3719 | 0 | goto err; |
3720 | 0 | } |
3721 | 0 | x = NULL; |
3722 | 0 | } |
3723 | | |
3724 | 0 | if (sk_X509_num(sk) <= 0) { |
3725 | | /* TLS does not mind 0 certs returned */ |
3726 | 0 | if (s->version == SSL3_VERSION) { |
3727 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
3728 | 0 | SSL_R_NO_CERTIFICATES_RETURNED); |
3729 | 0 | goto err; |
3730 | 0 | } |
3731 | | /* Fail for TLS only if we required a certificate */ |
3732 | 0 | else if ((s->verify_mode & SSL_VERIFY_PEER) && |
3733 | 0 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { |
3734 | 0 | SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED, |
3735 | 0 | SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
3736 | 0 | goto err; |
3737 | 0 | } |
3738 | | /* No client certificate so digest cached records */ |
3739 | 0 | if (s->s3.handshake_buffer && !ssl3_digest_cached_records(s, 0)) { |
3740 | | /* SSLfatal() already called */ |
3741 | 0 | goto err; |
3742 | 0 | } |
3743 | 0 | } else { |
3744 | 0 | EVP_PKEY *pkey; |
3745 | 0 | i = ssl_verify_cert_chain(s, sk); |
3746 | 0 | if (i <= 0) { |
3747 | 0 | SSLfatal(s, ssl_x509err2alert(s->verify_result), |
3748 | 0 | SSL_R_CERTIFICATE_VERIFY_FAILED); |
3749 | 0 | goto err; |
3750 | 0 | } |
3751 | 0 | pkey = X509_get0_pubkey(sk_X509_value(sk, 0)); |
3752 | 0 | if (pkey == NULL) { |
3753 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
3754 | 0 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
3755 | 0 | goto err; |
3756 | 0 | } |
3757 | 0 | } |
3758 | | |
3759 | | /* |
3760 | | * Sessions must be immutable once they go into the session cache. Otherwise |
3761 | | * we can get multi-thread problems. Therefore we don't "update" sessions, |
3762 | | * we replace them with a duplicate. Here, we need to do this every time |
3763 | | * a new certificate is received via post-handshake authentication, as the |
3764 | | * session may have already gone into the session cache. |
3765 | | */ |
3766 | | |
3767 | 0 | if (s->post_handshake_auth == SSL_PHA_REQUESTED) { |
3768 | 0 | if ((new_sess = ssl_session_dup(s->session, 0)) == 0) { |
3769 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); |
3770 | 0 | goto err; |
3771 | 0 | } |
3772 | | |
3773 | 0 | SSL_SESSION_free(s->session); |
3774 | 0 | s->session = new_sess; |
3775 | 0 | } |
3776 | | |
3777 | 0 | X509_free(s->session->peer); |
3778 | 0 | s->session->peer = sk_X509_shift(sk); |
3779 | 0 | s->session->verify_result = s->verify_result; |
3780 | |
|
3781 | 0 | OSSL_STACK_OF_X509_free(s->session->peer_chain); |
3782 | 0 | s->session->peer_chain = sk; |
3783 | 0 | sk = NULL; |
3784 | | /* Ensure there is no RPK */ |
3785 | 0 | EVP_PKEY_free(s->session->peer_rpk); |
3786 | 0 | s->session->peer_rpk = NULL; |
3787 | | |
3788 | | /* |
3789 | | * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE |
3790 | | * message |
3791 | | */ |
3792 | 0 | if (SSL_CONNECTION_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) { |
3793 | | /* SSLfatal() already called */ |
3794 | 0 | goto err; |
3795 | 0 | } |
3796 | | |
3797 | | /* |
3798 | | * Inconsistency alert: cert_chain does *not* include the peer's own |
3799 | | * certificate, while we do include it in statem_clnt.c |
3800 | | */ |
3801 | | |
3802 | | /* Save the current hash state for when we receive the CertificateVerify */ |
3803 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
3804 | 0 | if (!ssl_handshake_hash(s, s->cert_verify_hash, |
3805 | 0 | sizeof(s->cert_verify_hash), |
3806 | 0 | &s->cert_verify_hash_len)) { |
3807 | | /* SSLfatal() already called */ |
3808 | 0 | goto err; |
3809 | 0 | } |
3810 | | |
3811 | | /* Resend session tickets */ |
3812 | 0 | s->sent_tickets = 0; |
3813 | 0 | } |
3814 | | |
3815 | 0 | ret = MSG_PROCESS_CONTINUE_READING; |
3816 | |
|
3817 | 0 | err: |
3818 | 0 | X509_free(x); |
3819 | 0 | OSSL_STACK_OF_X509_free(sk); |
3820 | 0 | return ret; |
3821 | 0 | } |
3822 | | |
3823 | | #ifndef OPENSSL_NO_COMP_ALG |
3824 | | MSG_PROCESS_RETURN tls_process_client_compressed_certificate(SSL_CONNECTION *sc, PACKET *pkt) |
3825 | | { |
3826 | | MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR; |
3827 | | PACKET tmppkt; |
3828 | | BUF_MEM *buf = BUF_MEM_new(); |
3829 | | |
3830 | | if (tls13_process_compressed_certificate(sc, pkt, &tmppkt, buf) != MSG_PROCESS_ERROR) |
3831 | | ret = tls_process_client_certificate(sc, &tmppkt); |
3832 | | |
3833 | | BUF_MEM_free(buf); |
3834 | | return ret; |
3835 | | } |
3836 | | #endif |
3837 | | |
3838 | | CON_FUNC_RETURN tls_construct_server_certificate(SSL_CONNECTION *s, WPACKET *pkt) |
3839 | 0 | { |
3840 | 0 | CERT_PKEY *cpk = s->s3.tmp.cert; |
3841 | |
|
3842 | 0 | if (cpk == NULL) { |
3843 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3844 | 0 | return CON_FUNC_ERROR; |
3845 | 0 | } |
3846 | | |
3847 | | /* |
3848 | | * In TLSv1.3 the certificate chain is always preceded by a 0 length context |
3849 | | * for the server Certificate message |
3850 | | */ |
3851 | 0 | if (SSL_CONNECTION_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) { |
3852 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3853 | 0 | return CON_FUNC_ERROR; |
3854 | 0 | } |
3855 | 0 | switch (s->ext.server_cert_type) { |
3856 | 0 | case TLSEXT_cert_type_rpk: |
3857 | 0 | if (!tls_output_rpk(s, pkt, cpk)) { |
3858 | | /* SSLfatal() already called */ |
3859 | 0 | return 0; |
3860 | 0 | } |
3861 | 0 | break; |
3862 | 0 | case TLSEXT_cert_type_x509: |
3863 | 0 | if (!ssl3_output_cert_chain(s, pkt, cpk, 0)) { |
3864 | | /* SSLfatal() already called */ |
3865 | 0 | return 0; |
3866 | 0 | } |
3867 | 0 | break; |
3868 | 0 | default: |
3869 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3870 | 0 | return 0; |
3871 | 0 | } |
3872 | | |
3873 | 0 | return CON_FUNC_SUCCESS; |
3874 | 0 | } |
3875 | | |
3876 | | #ifndef OPENSSL_NO_COMP_ALG |
3877 | | CON_FUNC_RETURN tls_construct_server_compressed_certificate(SSL_CONNECTION *sc, WPACKET *pkt) |
3878 | | { |
3879 | | int alg = get_compressed_certificate_alg(sc); |
3880 | | OSSL_COMP_CERT *cc = sc->s3.tmp.cert->comp_cert[alg]; |
3881 | | |
3882 | | if (!ossl_assert(cc != NULL)) { |
3883 | | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3884 | | return 0; |
3885 | | } |
3886 | | /* |
3887 | | * Server can't compress on-demand |
3888 | | * Use pre-compressed certificate |
3889 | | */ |
3890 | | if (!WPACKET_put_bytes_u16(pkt, alg) |
3891 | | || !WPACKET_put_bytes_u24(pkt, cc->orig_len) |
3892 | | || !WPACKET_start_sub_packet_u24(pkt) |
3893 | | || !WPACKET_memcpy(pkt, cc->data, cc->len) |
3894 | | || !WPACKET_close(pkt)) |
3895 | | return 0; |
3896 | | |
3897 | | sc->s3.tmp.cert->cert_comp_used++; |
3898 | | return 1; |
3899 | | } |
3900 | | #endif |
3901 | | |
3902 | | static int create_ticket_prequel(SSL_CONNECTION *s, WPACKET *pkt, |
3903 | | uint32_t age_add, unsigned char *tick_nonce) |
3904 | 0 | { |
3905 | 0 | uint32_t timeout = (uint32_t)ossl_time2seconds(s->session->timeout); |
3906 | | |
3907 | | /* |
3908 | | * Ticket lifetime hint: |
3909 | | * In TLSv1.3 we reset the "time" field above, and always specify the |
3910 | | * timeout, limited to a 1 week period per RFC8446. |
3911 | | * For TLSv1.2 this is advisory only and we leave this unspecified for |
3912 | | * resumed session (for simplicity). |
3913 | | */ |
3914 | 0 | #define ONE_WEEK_SEC (7 * 24 * 60 * 60) |
3915 | |
|
3916 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
3917 | 0 | if (ossl_time_compare(s->session->timeout, |
3918 | 0 | ossl_seconds2time(ONE_WEEK_SEC)) > 0) |
3919 | 0 | timeout = ONE_WEEK_SEC; |
3920 | 0 | } else if (s->hit) |
3921 | 0 | timeout = 0; |
3922 | |
|
3923 | 0 | if (!WPACKET_put_bytes_u32(pkt, timeout)) { |
3924 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3925 | 0 | return 0; |
3926 | 0 | } |
3927 | | |
3928 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
3929 | 0 | if (!WPACKET_put_bytes_u32(pkt, age_add) |
3930 | 0 | || !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) { |
3931 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3932 | 0 | return 0; |
3933 | 0 | } |
3934 | 0 | } |
3935 | | |
3936 | | /* Start the sub-packet for the actual ticket data */ |
3937 | 0 | if (!WPACKET_start_sub_packet_u16(pkt)) { |
3938 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3939 | 0 | return 0; |
3940 | 0 | } |
3941 | | |
3942 | 0 | return 1; |
3943 | 0 | } |
3944 | | |
3945 | | static CON_FUNC_RETURN construct_stateless_ticket(SSL_CONNECTION *s, |
3946 | | WPACKET *pkt, |
3947 | | uint32_t age_add, |
3948 | | unsigned char *tick_nonce) |
3949 | 0 | { |
3950 | 0 | unsigned char *senc = NULL; |
3951 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
3952 | 0 | SSL_HMAC *hctx = NULL; |
3953 | 0 | unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2; |
3954 | 0 | const unsigned char *const_p; |
3955 | 0 | int len, slen_full, slen, lenfinal; |
3956 | 0 | SSL_SESSION *sess; |
3957 | 0 | size_t hlen; |
3958 | 0 | SSL_CTX *tctx = s->session_ctx; |
3959 | 0 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
3960 | 0 | unsigned char key_name[TLSEXT_KEYNAME_LENGTH]; |
3961 | 0 | int iv_len; |
3962 | 0 | CON_FUNC_RETURN ok = CON_FUNC_ERROR; |
3963 | 0 | size_t macoffset, macendoffset; |
3964 | 0 | SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s); |
3965 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
3966 | | |
3967 | | /* get session encoding length */ |
3968 | 0 | slen_full = i2d_SSL_SESSION(s->session, NULL); |
3969 | | /* |
3970 | | * Some length values are 16 bits, so forget it if session is too |
3971 | | * long |
3972 | | */ |
3973 | 0 | if (slen_full == 0 || slen_full > 0xFF00) { |
3974 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3975 | 0 | goto err; |
3976 | 0 | } |
3977 | 0 | senc = OPENSSL_malloc(slen_full); |
3978 | 0 | if (senc == NULL) { |
3979 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
3980 | 0 | goto err; |
3981 | 0 | } |
3982 | | |
3983 | 0 | ctx = EVP_CIPHER_CTX_new(); |
3984 | 0 | if (ctx == NULL) { |
3985 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
3986 | 0 | goto err; |
3987 | 0 | } |
3988 | 0 | hctx = ssl_hmac_new(tctx); |
3989 | 0 | if (hctx == NULL) { |
3990 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); |
3991 | 0 | goto err; |
3992 | 0 | } |
3993 | | |
3994 | 0 | p = senc; |
3995 | 0 | if (!i2d_SSL_SESSION(s->session, &p)) { |
3996 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
3997 | 0 | goto err; |
3998 | 0 | } |
3999 | | |
4000 | | /* |
4001 | | * create a fresh copy (not shared with other threads) to clean up |
4002 | | */ |
4003 | 0 | const_p = senc; |
4004 | 0 | sess = d2i_SSL_SESSION_ex(NULL, &const_p, slen_full, sctx->libctx, |
4005 | 0 | sctx->propq); |
4006 | 0 | if (sess == NULL) { |
4007 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4008 | 0 | goto err; |
4009 | 0 | } |
4010 | | |
4011 | 0 | slen = i2d_SSL_SESSION(sess, NULL); |
4012 | 0 | if (slen == 0 || slen > slen_full) { |
4013 | | /* shouldn't ever happen */ |
4014 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4015 | 0 | SSL_SESSION_free(sess); |
4016 | 0 | goto err; |
4017 | 0 | } |
4018 | 0 | p = senc; |
4019 | 0 | if (!i2d_SSL_SESSION(sess, &p)) { |
4020 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4021 | 0 | SSL_SESSION_free(sess); |
4022 | 0 | goto err; |
4023 | 0 | } |
4024 | 0 | SSL_SESSION_free(sess); |
4025 | | |
4026 | | /* |
4027 | | * Initialize HMAC and cipher contexts. If callback present it does |
4028 | | * all the work otherwise use generated values from parent ctx. |
4029 | | */ |
4030 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4031 | 0 | if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL) |
4032 | | #else |
4033 | | if (tctx->ext.ticket_key_evp_cb != NULL) |
4034 | | #endif |
4035 | 0 | { |
4036 | 0 | int ret = 0; |
4037 | |
|
4038 | 0 | if (tctx->ext.ticket_key_evp_cb != NULL) |
4039 | 0 | ret = tctx->ext.ticket_key_evp_cb(ssl, key_name, iv, ctx, |
4040 | 0 | ssl_hmac_get0_EVP_MAC_CTX(hctx), |
4041 | 0 | 1); |
4042 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4043 | 0 | else if (tctx->ext.ticket_key_cb != NULL) |
4044 | | /* if 0 is returned, write an empty ticket */ |
4045 | 0 | ret = tctx->ext.ticket_key_cb(ssl, key_name, iv, ctx, |
4046 | 0 | ssl_hmac_get0_HMAC_CTX(hctx), 1); |
4047 | 0 | #endif |
4048 | |
|
4049 | 0 | if (ret == 0) { |
4050 | | /* |
4051 | | * In TLSv1.2 we construct a 0 length ticket. In TLSv1.3 a 0 |
4052 | | * length ticket is not allowed so we abort construction of the |
4053 | | * ticket |
4054 | | */ |
4055 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
4056 | 0 | ok = CON_FUNC_DONT_SEND; |
4057 | 0 | goto err; |
4058 | 0 | } |
4059 | | /* Put timeout and length */ |
4060 | 0 | if (!WPACKET_put_bytes_u32(pkt, 0) |
4061 | 0 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
4062 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4063 | 0 | goto err; |
4064 | 0 | } |
4065 | 0 | OPENSSL_free(senc); |
4066 | 0 | EVP_CIPHER_CTX_free(ctx); |
4067 | 0 | ssl_hmac_free(hctx); |
4068 | 0 | return CON_FUNC_SUCCESS; |
4069 | 0 | } |
4070 | 0 | if (ret < 0) { |
4071 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED); |
4072 | 0 | goto err; |
4073 | 0 | } |
4074 | 0 | iv_len = EVP_CIPHER_CTX_get_iv_length(ctx); |
4075 | 0 | if (iv_len < 0) { |
4076 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4077 | 0 | goto err; |
4078 | 0 | } |
4079 | 0 | } else { |
4080 | 0 | EVP_CIPHER *cipher = EVP_CIPHER_fetch(sctx->libctx, "AES-256-CBC", |
4081 | 0 | sctx->propq); |
4082 | |
|
4083 | 0 | if (cipher == NULL) { |
4084 | | /* Error is already recorded */ |
4085 | 0 | SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR); |
4086 | 0 | goto err; |
4087 | 0 | } |
4088 | | |
4089 | 0 | iv_len = EVP_CIPHER_get_iv_length(cipher); |
4090 | 0 | if (iv_len < 0 |
4091 | 0 | || RAND_bytes_ex(sctx->libctx, iv, iv_len, 0) <= 0 |
4092 | 0 | || !EVP_EncryptInit_ex(ctx, cipher, NULL, |
4093 | 0 | tctx->ext.secure->tick_aes_key, iv) |
4094 | 0 | || !ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key, |
4095 | 0 | sizeof(tctx->ext.secure->tick_hmac_key), |
4096 | 0 | "SHA256")) { |
4097 | 0 | EVP_CIPHER_free(cipher); |
4098 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4099 | 0 | goto err; |
4100 | 0 | } |
4101 | 0 | EVP_CIPHER_free(cipher); |
4102 | 0 | memcpy(key_name, tctx->ext.tick_key_name, |
4103 | 0 | sizeof(tctx->ext.tick_key_name)); |
4104 | 0 | } |
4105 | | |
4106 | 0 | if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) { |
4107 | | /* SSLfatal() already called */ |
4108 | 0 | goto err; |
4109 | 0 | } |
4110 | | |
4111 | 0 | if (!WPACKET_get_total_written(pkt, &macoffset) |
4112 | | /* Output key name */ |
4113 | 0 | || !WPACKET_memcpy(pkt, key_name, sizeof(key_name)) |
4114 | | /* output IV */ |
4115 | 0 | || !WPACKET_memcpy(pkt, iv, iv_len) |
4116 | 0 | || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH, |
4117 | 0 | &encdata1) |
4118 | | /* Encrypt session data */ |
4119 | 0 | || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen) |
4120 | 0 | || !WPACKET_allocate_bytes(pkt, len, &encdata2) |
4121 | 0 | || encdata1 != encdata2 |
4122 | 0 | || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal) |
4123 | 0 | || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2) |
4124 | 0 | || encdata1 + len != encdata2 |
4125 | 0 | || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH |
4126 | 0 | || !WPACKET_get_total_written(pkt, &macendoffset) |
4127 | 0 | || !ssl_hmac_update(hctx, |
4128 | 0 | (unsigned char *)s->init_buf->data + macoffset, |
4129 | 0 | macendoffset - macoffset) |
4130 | 0 | || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1) |
4131 | 0 | || !ssl_hmac_final(hctx, macdata1, &hlen, EVP_MAX_MD_SIZE) |
4132 | 0 | || hlen > EVP_MAX_MD_SIZE |
4133 | 0 | || !WPACKET_allocate_bytes(pkt, hlen, &macdata2) |
4134 | 0 | || macdata1 != macdata2) { |
4135 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4136 | 0 | goto err; |
4137 | 0 | } |
4138 | | |
4139 | | /* Close the sub-packet created by create_ticket_prequel() */ |
4140 | 0 | if (!WPACKET_close(pkt)) { |
4141 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4142 | 0 | goto err; |
4143 | 0 | } |
4144 | | |
4145 | 0 | ok = CON_FUNC_SUCCESS; |
4146 | 0 | err: |
4147 | 0 | OPENSSL_free(senc); |
4148 | 0 | EVP_CIPHER_CTX_free(ctx); |
4149 | 0 | ssl_hmac_free(hctx); |
4150 | 0 | return ok; |
4151 | 0 | } |
4152 | | |
4153 | | static int construct_stateful_ticket(SSL_CONNECTION *s, WPACKET *pkt, |
4154 | | uint32_t age_add, |
4155 | | unsigned char *tick_nonce) |
4156 | 0 | { |
4157 | 0 | if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) { |
4158 | | /* SSLfatal() already called */ |
4159 | 0 | return 0; |
4160 | 0 | } |
4161 | | |
4162 | 0 | if (!WPACKET_memcpy(pkt, s->session->session_id, |
4163 | 0 | s->session->session_id_length) |
4164 | 0 | || !WPACKET_close(pkt)) { |
4165 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4166 | 0 | return 0; |
4167 | 0 | } |
4168 | | |
4169 | 0 | return 1; |
4170 | 0 | } |
4171 | | |
4172 | | static void tls_update_ticket_counts(SSL_CONNECTION *s) |
4173 | 0 | { |
4174 | | /* |
4175 | | * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets| |
4176 | | * gets reset to 0 if we send more tickets following a post-handshake |
4177 | | * auth, but |next_ticket_nonce| does not. If we're sending extra |
4178 | | * tickets, decrement the count of pending extra tickets. |
4179 | | */ |
4180 | 0 | s->sent_tickets++; |
4181 | 0 | s->next_ticket_nonce++; |
4182 | 0 | if (s->ext.extra_tickets_expected > 0) |
4183 | 0 | s->ext.extra_tickets_expected--; |
4184 | 0 | } |
4185 | | |
4186 | | CON_FUNC_RETURN tls_construct_new_session_ticket(SSL_CONNECTION *s, WPACKET *pkt) |
4187 | 0 | { |
4188 | 0 | SSL_CTX *tctx = s->session_ctx; |
4189 | 0 | unsigned char tick_nonce[TICKET_NONCE_SIZE]; |
4190 | 0 | union { |
4191 | 0 | unsigned char age_add_c[sizeof(uint32_t)]; |
4192 | 0 | uint32_t age_add; |
4193 | 0 | } age_add_u; |
4194 | 0 | CON_FUNC_RETURN ret = CON_FUNC_ERROR; |
4195 | |
|
4196 | 0 | age_add_u.age_add = 0; |
4197 | |
|
4198 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
4199 | 0 | size_t i, hashlen; |
4200 | 0 | uint64_t nonce; |
4201 | 0 | static const unsigned char nonce_label[] = "resumption"; |
4202 | 0 | const EVP_MD *md = ssl_handshake_md(s); |
4203 | 0 | int hashleni = EVP_MD_get_size(md); |
4204 | | |
4205 | | /* Ensure cast to size_t is safe */ |
4206 | 0 | if (!ossl_assert(hashleni > 0)) { |
4207 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4208 | 0 | goto err; |
4209 | 0 | } |
4210 | 0 | hashlen = (size_t)hashleni; |
4211 | | |
4212 | | /* |
4213 | | * If we already sent one NewSessionTicket, or we resumed then |
4214 | | * s->session may already be in a cache and so we must not modify it. |
4215 | | * Instead we need to take a copy of it and modify that. |
4216 | | */ |
4217 | 0 | if (s->sent_tickets != 0 || s->hit) { |
4218 | 0 | SSL_SESSION *new_sess = ssl_session_dup(s->session, 0); |
4219 | |
|
4220 | 0 | if (new_sess == NULL) { |
4221 | | /* SSLfatal already called */ |
4222 | 0 | goto err; |
4223 | 0 | } |
4224 | | |
4225 | 0 | SSL_SESSION_free(s->session); |
4226 | 0 | s->session = new_sess; |
4227 | 0 | } |
4228 | | |
4229 | 0 | if (!ssl_generate_session_id(s, s->session)) { |
4230 | | /* SSLfatal() already called */ |
4231 | 0 | goto err; |
4232 | 0 | } |
4233 | 0 | if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx, |
4234 | 0 | age_add_u.age_add_c, sizeof(age_add_u), 0) <= 0) { |
4235 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4236 | 0 | goto err; |
4237 | 0 | } |
4238 | 0 | s->session->ext.tick_age_add = age_add_u.age_add; |
4239 | |
|
4240 | 0 | nonce = s->next_ticket_nonce; |
4241 | 0 | for (i = TICKET_NONCE_SIZE; i > 0; i--) { |
4242 | 0 | tick_nonce[i - 1] = (unsigned char)(nonce & 0xff); |
4243 | 0 | nonce >>= 8; |
4244 | 0 | } |
4245 | |
|
4246 | 0 | if (!tls13_hkdf_expand(s, md, s->resumption_master_secret, |
4247 | 0 | nonce_label, |
4248 | 0 | sizeof(nonce_label) - 1, |
4249 | 0 | tick_nonce, |
4250 | 0 | TICKET_NONCE_SIZE, |
4251 | 0 | s->session->master_key, |
4252 | 0 | hashlen, 1)) { |
4253 | | /* SSLfatal() already called */ |
4254 | 0 | goto err; |
4255 | 0 | } |
4256 | 0 | s->session->master_key_length = hashlen; |
4257 | |
|
4258 | 0 | s->session->time = ossl_time_now(); |
4259 | 0 | ssl_session_calculate_timeout(s->session); |
4260 | 0 | if (s->s3.alpn_selected != NULL) { |
4261 | 0 | OPENSSL_free(s->session->ext.alpn_selected); |
4262 | 0 | s->session->ext.alpn_selected = |
4263 | 0 | OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len); |
4264 | 0 | if (s->session->ext.alpn_selected == NULL) { |
4265 | 0 | s->session->ext.alpn_selected_len = 0; |
4266 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
4267 | 0 | goto err; |
4268 | 0 | } |
4269 | 0 | s->session->ext.alpn_selected_len = s->s3.alpn_selected_len; |
4270 | 0 | } |
4271 | 0 | s->session->ext.max_early_data = s->max_early_data; |
4272 | 0 | } |
4273 | | |
4274 | 0 | if (tctx->generate_ticket_cb != NULL && |
4275 | 0 | tctx->generate_ticket_cb(SSL_CONNECTION_GET_USER_SSL(s), |
4276 | 0 | tctx->ticket_cb_data) == 0) { |
4277 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4278 | 0 | goto err; |
4279 | 0 | } |
4280 | | /* |
4281 | | * If we are using anti-replay protection then we behave as if |
4282 | | * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there |
4283 | | * is no point in using full stateless tickets. |
4284 | | */ |
4285 | 0 | if (SSL_CONNECTION_IS_TLS13(s) |
4286 | 0 | && ((s->options & SSL_OP_NO_TICKET) != 0 |
4287 | 0 | || (s->max_early_data > 0 |
4288 | 0 | && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) { |
4289 | 0 | if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) { |
4290 | | /* SSLfatal() already called */ |
4291 | 0 | goto err; |
4292 | 0 | } |
4293 | 0 | } else { |
4294 | 0 | CON_FUNC_RETURN tmpret; |
4295 | |
|
4296 | 0 | tmpret = construct_stateless_ticket(s, pkt, age_add_u.age_add, |
4297 | 0 | tick_nonce); |
4298 | 0 | if (tmpret != CON_FUNC_SUCCESS) { |
4299 | 0 | if (tmpret == CON_FUNC_DONT_SEND) { |
4300 | | /* Non-fatal. Abort construction but continue */ |
4301 | 0 | ret = CON_FUNC_DONT_SEND; |
4302 | | /* We count this as a success so update the counts anwyay */ |
4303 | 0 | tls_update_ticket_counts(s); |
4304 | 0 | } |
4305 | | /* else SSLfatal() already called */ |
4306 | 0 | goto err; |
4307 | 0 | } |
4308 | 0 | } |
4309 | | |
4310 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
4311 | 0 | if (!tls_construct_extensions(s, pkt, |
4312 | 0 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET, |
4313 | 0 | NULL, 0)) { |
4314 | | /* SSLfatal() already called */ |
4315 | 0 | goto err; |
4316 | 0 | } |
4317 | 0 | tls_update_ticket_counts(s); |
4318 | 0 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); |
4319 | 0 | } |
4320 | | |
4321 | 0 | ret = CON_FUNC_SUCCESS; |
4322 | 0 | err: |
4323 | 0 | return ret; |
4324 | 0 | } |
4325 | | |
4326 | | /* |
4327 | | * In TLSv1.3 this is called from the extensions code, otherwise it is used to |
4328 | | * create a separate message. Returns 1 on success or 0 on failure. |
4329 | | */ |
4330 | | int tls_construct_cert_status_body(SSL_CONNECTION *s, WPACKET *pkt) |
4331 | 0 | { |
4332 | 0 | if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type) |
4333 | 0 | || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp, |
4334 | 0 | s->ext.ocsp.resp_len)) { |
4335 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4336 | 0 | return 0; |
4337 | 0 | } |
4338 | | |
4339 | 0 | return 1; |
4340 | 0 | } |
4341 | | |
4342 | | CON_FUNC_RETURN tls_construct_cert_status(SSL_CONNECTION *s, WPACKET *pkt) |
4343 | 0 | { |
4344 | 0 | if (!tls_construct_cert_status_body(s, pkt)) { |
4345 | | /* SSLfatal() already called */ |
4346 | 0 | return CON_FUNC_ERROR; |
4347 | 0 | } |
4348 | | |
4349 | 0 | return CON_FUNC_SUCCESS; |
4350 | 0 | } |
4351 | | |
4352 | | #ifndef OPENSSL_NO_NEXTPROTONEG |
4353 | | /* |
4354 | | * tls_process_next_proto reads a Next Protocol Negotiation handshake message. |
4355 | | * It sets the next_proto member in s if found |
4356 | | */ |
4357 | | MSG_PROCESS_RETURN tls_process_next_proto(SSL_CONNECTION *s, PACKET *pkt) |
4358 | 0 | { |
4359 | 0 | PACKET next_proto, padding; |
4360 | 0 | size_t next_proto_len; |
4361 | | |
4362 | | /*- |
4363 | | * The payload looks like: |
4364 | | * uint8 proto_len; |
4365 | | * uint8 proto[proto_len]; |
4366 | | * uint8 padding_len; |
4367 | | * uint8 padding[padding_len]; |
4368 | | */ |
4369 | 0 | if (!PACKET_get_length_prefixed_1(pkt, &next_proto) |
4370 | 0 | || !PACKET_get_length_prefixed_1(pkt, &padding) |
4371 | 0 | || PACKET_remaining(pkt) > 0) { |
4372 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
4373 | 0 | return MSG_PROCESS_ERROR; |
4374 | 0 | } |
4375 | | |
4376 | 0 | if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) { |
4377 | 0 | s->ext.npn_len = 0; |
4378 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4379 | 0 | return MSG_PROCESS_ERROR; |
4380 | 0 | } |
4381 | | |
4382 | 0 | s->ext.npn_len = (unsigned char)next_proto_len; |
4383 | |
|
4384 | 0 | return MSG_PROCESS_CONTINUE_READING; |
4385 | 0 | } |
4386 | | #endif |
4387 | | |
4388 | | static CON_FUNC_RETURN tls_construct_encrypted_extensions(SSL_CONNECTION *s, |
4389 | | WPACKET *pkt) |
4390 | 0 | { |
4391 | 0 | if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, |
4392 | 0 | NULL, 0)) { |
4393 | | /* SSLfatal() already called */ |
4394 | 0 | return CON_FUNC_ERROR; |
4395 | 0 | } |
4396 | | |
4397 | 0 | return CON_FUNC_SUCCESS; |
4398 | 0 | } |
4399 | | |
4400 | | MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL_CONNECTION *s, PACKET *pkt) |
4401 | 0 | { |
4402 | 0 | if (PACKET_remaining(pkt) != 0) { |
4403 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
4404 | 0 | return MSG_PROCESS_ERROR; |
4405 | 0 | } |
4406 | | |
4407 | 0 | if (s->early_data_state != SSL_EARLY_DATA_READING |
4408 | 0 | && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) { |
4409 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4410 | 0 | return MSG_PROCESS_ERROR; |
4411 | 0 | } |
4412 | | |
4413 | | /* |
4414 | | * EndOfEarlyData signals a key change so the end of the message must be on |
4415 | | * a record boundary. |
4416 | | */ |
4417 | 0 | if (RECORD_LAYER_processed_read_pending(&s->rlayer)) { |
4418 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY); |
4419 | 0 | return MSG_PROCESS_ERROR; |
4420 | 0 | } |
4421 | | |
4422 | 0 | s->early_data_state = SSL_EARLY_DATA_FINISHED_READING; |
4423 | 0 | if (!SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->change_cipher_state(s, |
4424 | 0 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) { |
4425 | | /* SSLfatal() already called */ |
4426 | 0 | return MSG_PROCESS_ERROR; |
4427 | 0 | } |
4428 | | |
4429 | 0 | return MSG_PROCESS_CONTINUE_READING; |
4430 | 0 | } |