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