/src/openssl36/ssl/statem/statem_lib.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 | | * |
5 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
6 | | * this file except in compliance with the License. You can obtain a copy |
7 | | * in the file LICENSE in the source distribution or at |
8 | | * https://www.openssl.org/source/license.html |
9 | | */ |
10 | | |
11 | | #include <limits.h> |
12 | | #include <string.h> |
13 | | #include <stdio.h> |
14 | | #include "../ssl_local.h" |
15 | | #include "statem_local.h" |
16 | | #include "internal/cryptlib.h" |
17 | | #include "internal/ssl_unwrap.h" |
18 | | #include <openssl/buffer.h> |
19 | | #include <openssl/objects.h> |
20 | | #include <openssl/evp.h> |
21 | | #include <openssl/rsa.h> |
22 | | #include <openssl/x509.h> |
23 | | #include <openssl/trace.h> |
24 | | #include <openssl/encoder.h> |
25 | | |
26 | | /* |
27 | | * Map error codes to TLS/SSL alart types. |
28 | | */ |
29 | | typedef struct x509err2alert_st { |
30 | | int x509err; |
31 | | int alert; |
32 | | } X509ERR2ALERT; |
33 | | |
34 | | /* Fixed value used in the ServerHello random field to identify an HRR */ |
35 | | const unsigned char hrrrandom[] = { |
36 | | 0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, |
37 | | 0x1e, 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, |
38 | | 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c |
39 | | }; |
40 | | |
41 | | int ossl_statem_set_mutator(SSL *s, |
42 | | ossl_statem_mutate_handshake_cb mutate_handshake_cb, |
43 | | ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb, |
44 | | void *mutatearg) |
45 | 0 | { |
46 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
47 | |
|
48 | 0 | if (sc == NULL) |
49 | 0 | return 0; |
50 | | |
51 | 0 | sc->statem.mutate_handshake_cb = mutate_handshake_cb; |
52 | 0 | sc->statem.mutatearg = mutatearg; |
53 | 0 | sc->statem.finish_mutate_handshake_cb = finish_mutate_handshake_cb; |
54 | |
|
55 | 0 | return 1; |
56 | 0 | } |
57 | | |
58 | | /* |
59 | | * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or |
60 | | * SSL3_RT_CHANGE_CIPHER_SPEC) |
61 | | */ |
62 | | int ssl3_do_write(SSL_CONNECTION *s, uint8_t type) |
63 | 178k | { |
64 | 178k | int ret; |
65 | 178k | size_t written = 0; |
66 | 178k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
67 | 178k | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
68 | | |
69 | | /* |
70 | | * If we're running the test suite then we may need to mutate the message |
71 | | * we've been asked to write. Does not happen in normal operation. |
72 | | */ |
73 | 178k | if (s->statem.mutate_handshake_cb != NULL |
74 | 0 | && !s->statem.write_in_progress |
75 | 0 | && type == SSL3_RT_HANDSHAKE |
76 | 0 | && s->init_num >= SSL3_HM_HEADER_LENGTH) { |
77 | 0 | unsigned char *msg; |
78 | 0 | size_t msglen; |
79 | |
|
80 | 0 | if (!s->statem.mutate_handshake_cb((unsigned char *)s->init_buf->data, |
81 | 0 | s->init_num, |
82 | 0 | &msg, &msglen, |
83 | 0 | s->statem.mutatearg)) |
84 | 0 | return -1; |
85 | 0 | if (msglen < SSL3_HM_HEADER_LENGTH |
86 | 0 | || !BUF_MEM_grow(s->init_buf, msglen)) |
87 | 0 | return -1; |
88 | 0 | memcpy(s->init_buf->data, msg, msglen); |
89 | 0 | s->init_num = msglen; |
90 | 0 | s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH; |
91 | 0 | s->statem.finish_mutate_handshake_cb(s->statem.mutatearg); |
92 | 0 | s->statem.write_in_progress = 1; |
93 | 0 | } |
94 | | |
95 | 178k | ret = ssl3_write_bytes(ssl, type, &s->init_buf->data[s->init_off], |
96 | 178k | s->init_num, &written); |
97 | 178k | if (ret <= 0) |
98 | 0 | return -1; |
99 | 178k | if (type == SSL3_RT_HANDSHAKE) |
100 | | /* |
101 | | * should not be done for 'Hello Request's, but in that case we'll |
102 | | * ignore the result anyway |
103 | | * TLS1.3 KeyUpdate and NewSessionTicket do not need to be added |
104 | | */ |
105 | 165k | if (!SSL_CONNECTION_IS_TLS13(s) |
106 | 25.4k | || (s->statem.hand_state != TLS_ST_SW_SESSION_TICKET |
107 | 25.4k | && s->statem.hand_state != TLS_ST_CW_KEY_UPDATE |
108 | 25.4k | && s->statem.hand_state != TLS_ST_SW_KEY_UPDATE)) |
109 | 165k | if (!ssl3_finish_mac(s, |
110 | 165k | (unsigned char *)&s->init_buf->data[s->init_off], |
111 | 165k | written)) |
112 | 0 | return -1; |
113 | 178k | if (written == s->init_num) { |
114 | 178k | s->statem.write_in_progress = 0; |
115 | 178k | if (s->msg_callback) |
116 | 0 | s->msg_callback(1, s->version, type, s->init_buf->data, |
117 | 0 | (size_t)(s->init_off + s->init_num), ussl, |
118 | 0 | s->msg_callback_arg); |
119 | 178k | return 1; |
120 | 178k | } |
121 | 0 | s->init_off += written; |
122 | 0 | s->init_num -= written; |
123 | 0 | return 0; |
124 | 178k | } |
125 | | |
126 | | int tls_close_construct_packet(SSL_CONNECTION *s, WPACKET *pkt, int htype) |
127 | 198k | { |
128 | 198k | size_t msglen; |
129 | | |
130 | 198k | if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt)) |
131 | 198k | || !WPACKET_get_length(pkt, &msglen) |
132 | 198k | || msglen > INT_MAX) |
133 | 0 | return 0; |
134 | 198k | s->init_num = (int)msglen; |
135 | 198k | s->init_off = 0; |
136 | | |
137 | 198k | return 1; |
138 | 198k | } |
139 | | |
140 | | int tls_setup_handshake(SSL_CONNECTION *s) |
141 | 181k | { |
142 | 181k | int ver_min, ver_max, ok; |
143 | 181k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
144 | 181k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
145 | | |
146 | 181k | if (!ssl3_init_finished_mac(s)) { |
147 | | /* SSLfatal() already called */ |
148 | 0 | return 0; |
149 | 0 | } |
150 | | |
151 | | /* Reset any extension flags */ |
152 | 181k | memset(s->ext.extflags, 0, sizeof(s->ext.extflags)); |
153 | | |
154 | 181k | if (ssl_get_min_max_version(s, &ver_min, &ver_max, NULL) != 0) { |
155 | 0 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE); |
156 | 0 | return 0; |
157 | 0 | } |
158 | | |
159 | | /* Sanity check that we have MD5-SHA1 if we need it */ |
160 | 181k | if (sctx->ssl_digest_methods[SSL_MD_MD5_SHA1_IDX] == NULL) { |
161 | 0 | int negotiated_minversion; |
162 | 0 | int md5sha1_needed_maxversion = SSL_CONNECTION_IS_DTLS(s) |
163 | 0 | ? DTLS1_VERSION |
164 | 0 | : TLS1_1_VERSION; |
165 | | |
166 | | /* We don't have MD5-SHA1 - do we need it? */ |
167 | 0 | if (ssl_version_cmp(s, ver_max, md5sha1_needed_maxversion) <= 0) { |
168 | 0 | SSLfatal_data(s, SSL_AD_HANDSHAKE_FAILURE, |
169 | 0 | SSL_R_NO_SUITABLE_DIGEST_ALGORITHM, |
170 | 0 | "The max supported SSL/TLS version needs the" |
171 | 0 | " MD5-SHA1 digest but it is not available" |
172 | 0 | " in the loaded providers. Use (D)TLSv1.2 or" |
173 | 0 | " above, or load different providers"); |
174 | 0 | return 0; |
175 | 0 | } |
176 | | |
177 | 0 | ok = 1; |
178 | | |
179 | | /* Don't allow TLSv1.1 or below to be negotiated */ |
180 | 0 | negotiated_minversion = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_2_VERSION : TLS1_2_VERSION; |
181 | 0 | if (ssl_version_cmp(s, ver_min, negotiated_minversion) < 0) |
182 | 0 | ok = SSL_set_min_proto_version(ssl, negotiated_minversion); |
183 | 0 | if (!ok) { |
184 | | /* Shouldn't happen */ |
185 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR); |
186 | 0 | return 0; |
187 | 0 | } |
188 | 0 | } |
189 | | |
190 | 181k | ok = 0; |
191 | 181k | if (s->server) { |
192 | 75.5k | STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl); |
193 | 75.5k | int i; |
194 | | |
195 | | /* |
196 | | * Sanity check that the maximum version we accept has ciphers |
197 | | * enabled. For clients we do this check during construction of the |
198 | | * ClientHello. |
199 | | */ |
200 | 232k | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
201 | 232k | const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i); |
202 | 232k | int cipher_minprotover = SSL_CONNECTION_IS_DTLS(s) |
203 | 232k | ? c->min_dtls |
204 | 232k | : c->min_tls; |
205 | 232k | int cipher_maxprotover = SSL_CONNECTION_IS_DTLS(s) |
206 | 232k | ? c->max_dtls |
207 | 232k | : c->max_tls; |
208 | | |
209 | 232k | if (ssl_version_cmp(s, ver_max, cipher_minprotover) >= 0 |
210 | 75.5k | && ssl_version_cmp(s, ver_max, cipher_maxprotover) <= 0) { |
211 | 75.5k | ok = 1; |
212 | 75.5k | break; |
213 | 75.5k | } |
214 | 232k | } |
215 | 75.5k | if (!ok) { |
216 | 0 | SSLfatal_data(s, SSL_AD_HANDSHAKE_FAILURE, |
217 | 0 | SSL_R_NO_CIPHERS_AVAILABLE, |
218 | 0 | "No ciphers enabled for max supported " |
219 | 0 | "SSL/TLS version"); |
220 | 0 | return 0; |
221 | 0 | } |
222 | 75.5k | if (SSL_IS_FIRST_HANDSHAKE(s)) { |
223 | | /* N.B. s->session_ctx == s->ctx here */ |
224 | 46.6k | ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_accept); |
225 | 46.6k | } else { |
226 | | /* N.B. s->ctx may not equal s->session_ctx */ |
227 | 28.8k | ssl_tsan_counter(sctx, &sctx->stats.sess_accept_renegotiate); |
228 | | |
229 | 28.8k | s->s3.tmp.cert_request = 0; |
230 | 28.8k | } |
231 | 106k | } else { |
232 | 106k | if (SSL_IS_FIRST_HANDSHAKE(s)) |
233 | 105k | ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_connect); |
234 | 1.23k | else |
235 | 1.23k | ssl_tsan_counter(s->session_ctx, |
236 | 1.23k | &s->session_ctx->stats.sess_connect_renegotiate); |
237 | | |
238 | | /* mark client_random uninitialized */ |
239 | 106k | memset(s->s3.client_random, 0, sizeof(s->s3.client_random)); |
240 | 106k | s->hit = 0; |
241 | | |
242 | 106k | s->s3.tmp.cert_req = 0; |
243 | | |
244 | 106k | if (SSL_CONNECTION_IS_DTLS(s)) |
245 | 21.9k | s->statem.use_timer = 1; |
246 | 106k | } |
247 | | |
248 | 181k | return 1; |
249 | 181k | } |
250 | | |
251 | | /* |
252 | | * Size of the to-be-signed TLS13 data, without the hash size itself: |
253 | | * 64 bytes of value 32, 33 context bytes, 1 byte separator |
254 | | */ |
255 | 76.1k | #define TLS13_TBS_START_SIZE 64 |
256 | 38.0k | #define TLS13_TBS_PREAMBLE_SIZE (TLS13_TBS_START_SIZE + 33 + 1) |
257 | | |
258 | | static int get_cert_verify_tbs_data(SSL_CONNECTION *s, unsigned char *tls13tbs, |
259 | | void **hdata, size_t *hdatalen) |
260 | 19.0k | { |
261 | | /* ASCII: "TLS 1.3, server CertificateVerify", in hex for EBCDIC compatibility */ |
262 | 19.0k | static const char servercontext[] = "\x54\x4c\x53\x20\x31\x2e\x33\x2c\x20\x73\x65\x72" |
263 | 19.0k | "\x76\x65\x72\x20\x43\x65\x72\x74\x69\x66\x69\x63\x61\x74\x65\x56\x65\x72\x69\x66\x79"; |
264 | | /* ASCII: "TLS 1.3, client CertificateVerify", in hex for EBCDIC compatibility */ |
265 | 19.0k | static const char clientcontext[] = "\x54\x4c\x53\x20\x31\x2e\x33\x2c\x20\x63\x6c\x69" |
266 | 19.0k | "\x65\x6e\x74\x20\x43\x65\x72\x74\x69\x66\x69\x63\x61\x74\x65\x56\x65\x72\x69\x66\x79"; |
267 | | |
268 | 19.0k | if (SSL_CONNECTION_IS_TLS13(s)) { |
269 | 19.0k | size_t hashlen; |
270 | | |
271 | | /* Set the first 64 bytes of to-be-signed data to octet 32 */ |
272 | 19.0k | memset(tls13tbs, 32, TLS13_TBS_START_SIZE); |
273 | | /* This copies the 33 bytes of context plus the 0 separator byte */ |
274 | 19.0k | if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY |
275 | 2.99k | || s->statem.hand_state == TLS_ST_SW_CERT_VRFY) |
276 | 19.0k | strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, servercontext); |
277 | 0 | else |
278 | 0 | strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, clientcontext); |
279 | | |
280 | | /* |
281 | | * If we're currently reading then we need to use the saved handshake |
282 | | * hash value. We can't use the current handshake hash state because |
283 | | * that includes the CertVerify itself. |
284 | | */ |
285 | 19.0k | if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY |
286 | 16.0k | || s->statem.hand_state == TLS_ST_SR_CERT_VRFY) { |
287 | 16.0k | memcpy(tls13tbs + TLS13_TBS_PREAMBLE_SIZE, s->cert_verify_hash, |
288 | 16.0k | s->cert_verify_hash_len); |
289 | 16.0k | hashlen = s->cert_verify_hash_len; |
290 | 16.0k | } else if (!ssl_handshake_hash(s, tls13tbs + TLS13_TBS_PREAMBLE_SIZE, |
291 | 2.99k | EVP_MAX_MD_SIZE, &hashlen)) { |
292 | | /* SSLfatal() already called */ |
293 | 0 | return 0; |
294 | 0 | } |
295 | | |
296 | 19.0k | *hdata = tls13tbs; |
297 | 19.0k | *hdatalen = TLS13_TBS_PREAMBLE_SIZE + hashlen; |
298 | 19.0k | } else { |
299 | 0 | size_t retlen; |
300 | 0 | long retlen_l; |
301 | |
|
302 | 0 | retlen = retlen_l = BIO_get_mem_data(s->s3.handshake_buffer, hdata); |
303 | 0 | if (retlen_l <= 0) { |
304 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
305 | 0 | return 0; |
306 | 0 | } |
307 | 0 | *hdatalen = retlen; |
308 | 0 | } |
309 | | |
310 | 19.0k | return 1; |
311 | 19.0k | } |
312 | | |
313 | | CON_FUNC_RETURN tls_construct_cert_verify(SSL_CONNECTION *s, WPACKET *pkt) |
314 | 2.39k | { |
315 | 2.39k | EVP_PKEY *pkey = NULL; |
316 | 2.39k | const EVP_MD *md = NULL; |
317 | 2.39k | EVP_MD_CTX *mctx = NULL; |
318 | 2.39k | EVP_PKEY_CTX *pctx = NULL; |
319 | 2.39k | size_t hdatalen = 0, siglen = 0; |
320 | 2.39k | void *hdata; |
321 | 2.39k | unsigned char *sig = NULL; |
322 | 2.39k | unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE]; |
323 | 2.39k | const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg; |
324 | 2.39k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
325 | | |
326 | 2.39k | if (lu == NULL || s->s3.tmp.cert == NULL) { |
327 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
328 | 0 | goto err; |
329 | 0 | } |
330 | 2.39k | pkey = s->s3.tmp.cert->privatekey; |
331 | | |
332 | 2.39k | if (pkey == NULL || !tls1_lookup_md(sctx, lu, &md)) { |
333 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
334 | 0 | goto err; |
335 | 0 | } |
336 | | |
337 | 2.39k | mctx = EVP_MD_CTX_new(); |
338 | 2.39k | if (mctx == NULL) { |
339 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
340 | 0 | goto err; |
341 | 0 | } |
342 | | |
343 | | /* Get the data to be signed */ |
344 | 2.39k | if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) { |
345 | | /* SSLfatal() already called */ |
346 | 0 | goto err; |
347 | 0 | } |
348 | | |
349 | 2.39k | if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) { |
350 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
351 | 0 | goto err; |
352 | 0 | } |
353 | | |
354 | 2.39k | if (EVP_DigestSignInit_ex(mctx, &pctx, |
355 | 2.39k | md == NULL ? NULL : EVP_MD_get0_name(md), |
356 | 2.39k | sctx->libctx, sctx->propq, pkey, |
357 | 2.39k | NULL) |
358 | 2.39k | <= 0) { |
359 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
360 | 0 | goto err; |
361 | 0 | } |
362 | | |
363 | 2.39k | if (lu->sig == EVP_PKEY_RSA_PSS) { |
364 | 223 | if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 |
365 | 223 | || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, |
366 | 223 | RSA_PSS_SALTLEN_DIGEST) |
367 | 223 | <= 0) { |
368 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
369 | 0 | goto err; |
370 | 0 | } |
371 | 223 | } |
372 | 2.39k | if (s->version == SSL3_VERSION) { |
373 | | /* |
374 | | * Here we use EVP_DigestSignUpdate followed by EVP_DigestSignFinal |
375 | | * in order to add the EVP_CTRL_SSL3_MASTER_SECRET call between them. |
376 | | */ |
377 | 0 | if (EVP_DigestSignUpdate(mctx, hdata, hdatalen) <= 0 |
378 | 0 | || EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET, |
379 | 0 | (int)s->session->master_key_length, |
380 | 0 | s->session->master_key) |
381 | 0 | <= 0 |
382 | 0 | || EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0) { |
383 | |
|
384 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
385 | 0 | goto err; |
386 | 0 | } |
387 | 0 | sig = OPENSSL_malloc(siglen); |
388 | 0 | if (sig == NULL |
389 | 0 | || EVP_DigestSignFinal(mctx, sig, &siglen) <= 0) { |
390 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
391 | 0 | goto err; |
392 | 0 | } |
393 | 2.39k | } else { |
394 | | /* |
395 | | * Here we *must* use EVP_DigestSign() because Ed25519/Ed448 does not |
396 | | * support streaming via EVP_DigestSignUpdate/EVP_DigestSignFinal |
397 | | */ |
398 | 2.39k | if (EVP_DigestSign(mctx, NULL, &siglen, hdata, hdatalen) <= 0) { |
399 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
400 | 0 | goto err; |
401 | 0 | } |
402 | 2.39k | sig = OPENSSL_malloc(siglen); |
403 | 2.39k | if (sig == NULL |
404 | 2.39k | || EVP_DigestSign(mctx, sig, &siglen, hdata, hdatalen) <= 0) { |
405 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
406 | 0 | goto err; |
407 | 0 | } |
408 | 2.39k | } |
409 | | |
410 | 2.39k | #ifndef OPENSSL_NO_GOST |
411 | 2.39k | { |
412 | 2.39k | int pktype = lu->sig; |
413 | | |
414 | 2.39k | if (pktype == NID_id_GostR3410_2001 |
415 | 2.39k | || pktype == NID_id_GostR3410_2012_256 |
416 | 2.39k | || pktype == NID_id_GostR3410_2012_512) |
417 | 0 | BUF_reverse(sig, NULL, siglen); |
418 | 2.39k | } |
419 | 2.39k | #endif |
420 | | |
421 | 2.39k | if (!WPACKET_sub_memcpy_u16(pkt, sig, siglen)) { |
422 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
423 | 0 | goto err; |
424 | 0 | } |
425 | | |
426 | | /* Digest cached records and discard handshake buffer */ |
427 | 2.39k | if (!ssl3_digest_cached_records(s, 0)) { |
428 | | /* SSLfatal() already called */ |
429 | 0 | goto err; |
430 | 0 | } |
431 | | |
432 | 2.39k | OPENSSL_free(sig); |
433 | 2.39k | EVP_MD_CTX_free(mctx); |
434 | 2.39k | return CON_FUNC_SUCCESS; |
435 | 0 | err: |
436 | 0 | OPENSSL_free(sig); |
437 | 0 | EVP_MD_CTX_free(mctx); |
438 | 0 | return CON_FUNC_ERROR; |
439 | 2.39k | } |
440 | | |
441 | | MSG_PROCESS_RETURN tls_process_cert_verify(SSL_CONNECTION *s, PACKET *pkt) |
442 | 13.2k | { |
443 | 13.2k | EVP_PKEY *pkey = NULL; |
444 | 13.2k | const unsigned char *data; |
445 | 13.2k | #ifndef OPENSSL_NO_GOST |
446 | 13.2k | unsigned char *gost_data = NULL; |
447 | 13.2k | #endif |
448 | 13.2k | MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR; |
449 | 13.2k | int j; |
450 | 13.2k | unsigned int len; |
451 | 13.2k | const EVP_MD *md = NULL; |
452 | 13.2k | size_t hdatalen = 0; |
453 | 13.2k | void *hdata; |
454 | 13.2k | unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE]; |
455 | 13.2k | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); |
456 | 13.2k | EVP_PKEY_CTX *pctx = NULL; |
457 | 13.2k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
458 | | |
459 | 13.2k | if (mctx == NULL) { |
460 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
461 | 0 | goto err; |
462 | 0 | } |
463 | | |
464 | 13.2k | pkey = tls_get_peer_pkey(s); |
465 | 13.2k | if (pkey == NULL) { |
466 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
467 | 0 | goto err; |
468 | 0 | } |
469 | | |
470 | 13.2k | if (ssl_cert_lookup_by_pkey(pkey, NULL, sctx) == NULL) { |
471 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
472 | 0 | SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); |
473 | 0 | goto err; |
474 | 0 | } |
475 | | |
476 | 13.2k | if (SSL_USE_SIGALGS(s)) { |
477 | 13.2k | unsigned int sigalg; |
478 | | |
479 | 13.2k | if (!PACKET_get_net_2(pkt, &sigalg)) { |
480 | 37 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_PACKET); |
481 | 37 | goto err; |
482 | 37 | } |
483 | 13.1k | if (tls12_check_peer_sigalg(s, sigalg, pkey) <= 0) { |
484 | | /* SSLfatal() already called */ |
485 | 72 | goto err; |
486 | 72 | } |
487 | 13.1k | } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) { |
488 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
489 | 0 | SSL_R_LEGACY_SIGALG_DISALLOWED_OR_UNSUPPORTED); |
490 | 0 | goto err; |
491 | 0 | } |
492 | | |
493 | 13.0k | if (!tls1_lookup_md(sctx, s->s3.tmp.peer_sigalg, &md)) { |
494 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
495 | 0 | goto err; |
496 | 0 | } |
497 | | |
498 | 13.0k | if (SSL_USE_SIGALGS(s)) |
499 | 13.0k | OSSL_TRACE1(TLS, "USING TLSv1.2 HASH %s\n", |
500 | 13.0k | md == NULL ? "n/a" : EVP_MD_get0_name(md)); |
501 | | |
502 | | /* Check for broken implementations of GOST ciphersuites */ |
503 | | /* |
504 | | * If key is GOST and len is exactly 64 or 128, it is signature without |
505 | | * length field (CryptoPro implementations at least till TLS 1.2) |
506 | | */ |
507 | 13.0k | #ifndef OPENSSL_NO_GOST |
508 | 13.0k | if (!SSL_USE_SIGALGS(s) |
509 | 0 | && ((PACKET_remaining(pkt) == 64 |
510 | 0 | && (EVP_PKEY_get_id(pkey) == NID_id_GostR3410_2001 |
511 | 0 | || EVP_PKEY_get_id(pkey) == NID_id_GostR3410_2012_256)) |
512 | 0 | || (PACKET_remaining(pkt) == 128 |
513 | 0 | && EVP_PKEY_get_id(pkey) == NID_id_GostR3410_2012_512))) { |
514 | 0 | len = (unsigned int)PACKET_remaining(pkt); |
515 | 0 | } else |
516 | 13.0k | #endif |
517 | 13.0k | if (!PACKET_get_net_2(pkt, &len)) { |
518 | 7 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
519 | 7 | goto err; |
520 | 7 | } |
521 | | |
522 | 13.0k | if (!PACKET_get_bytes(pkt, &data, len)) { |
523 | 28 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
524 | 28 | goto err; |
525 | 28 | } |
526 | 13.0k | if (PACKET_remaining(pkt) != 0) { |
527 | 25 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
528 | 25 | goto err; |
529 | 25 | } |
530 | | |
531 | 13.0k | if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) { |
532 | | /* SSLfatal() already called */ |
533 | 0 | goto err; |
534 | 0 | } |
535 | | |
536 | 13.0k | OSSL_TRACE1(TLS, "Using client verify alg %s\n", |
537 | 13.0k | md == NULL ? "n/a" : EVP_MD_get0_name(md)); |
538 | | |
539 | 13.0k | if (EVP_DigestVerifyInit_ex(mctx, &pctx, |
540 | 13.0k | md == NULL ? NULL : EVP_MD_get0_name(md), |
541 | 13.0k | sctx->libctx, sctx->propq, pkey, |
542 | 13.0k | NULL) |
543 | 13.0k | <= 0) { |
544 | 13 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
545 | 13 | goto err; |
546 | 13 | } |
547 | 13.0k | #ifndef OPENSSL_NO_GOST |
548 | 13.0k | { |
549 | 13.0k | int pktype = EVP_PKEY_get_id(pkey); |
550 | 13.0k | if (pktype == NID_id_GostR3410_2001 |
551 | 13.0k | || pktype == NID_id_GostR3410_2012_256 |
552 | 13.0k | || pktype == NID_id_GostR3410_2012_512) { |
553 | 0 | if ((gost_data = OPENSSL_malloc(len)) == NULL) |
554 | 0 | goto err; |
555 | 0 | BUF_reverse(gost_data, data, len); |
556 | 0 | data = gost_data; |
557 | 0 | } |
558 | 13.0k | } |
559 | 13.0k | #endif |
560 | | |
561 | 13.0k | if (SSL_USE_PSS(s)) { |
562 | 13.0k | if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 |
563 | 13.0k | || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, |
564 | 13.0k | RSA_PSS_SALTLEN_DIGEST) |
565 | 13.0k | <= 0) { |
566 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
567 | 0 | goto err; |
568 | 0 | } |
569 | 13.0k | } |
570 | 13.0k | if (s->version == SSL3_VERSION) { |
571 | 0 | if (EVP_DigestVerifyUpdate(mctx, hdata, hdatalen) <= 0 |
572 | 0 | || EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET, |
573 | 0 | (int)s->session->master_key_length, |
574 | 0 | s->session->master_key) |
575 | 0 | <= 0) { |
576 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
577 | 0 | goto err; |
578 | 0 | } |
579 | 0 | if (EVP_DigestVerifyFinal(mctx, data, len) <= 0) { |
580 | 0 | SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_SIGNATURE); |
581 | 0 | goto err; |
582 | 0 | } |
583 | 13.0k | } else { |
584 | 13.0k | j = EVP_DigestVerify(mctx, data, len, hdata, hdatalen); |
585 | 13.0k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
586 | | /* Ignore bad signatures when fuzzing */ |
587 | 13.0k | if (SSL_IS_QUIC_HANDSHAKE(s)) |
588 | 13.0k | j = 1; |
589 | 13.0k | #endif |
590 | 13.0k | if (j <= 0) { |
591 | 0 | SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_SIGNATURE); |
592 | 0 | goto err; |
593 | 0 | } |
594 | 13.0k | } |
595 | | |
596 | | /* |
597 | | * In TLSv1.3 on the client side we make sure we prepare the client |
598 | | * certificate after the CertVerify instead of when we get the |
599 | | * CertificateRequest. This is because in TLSv1.3 the CertificateRequest |
600 | | * comes *before* the Certificate message. In TLSv1.2 it comes after. We |
601 | | * want to make sure that SSL_get1_peer_certificate() will return the actual |
602 | | * server certificate from the client_cert_cb callback. |
603 | | */ |
604 | 13.0k | if (!s->server && SSL_CONNECTION_IS_TLS13(s) && s->s3.tmp.cert_req == 1) |
605 | 0 | ret = MSG_PROCESS_CONTINUE_PROCESSING; |
606 | 13.0k | else |
607 | 13.0k | ret = MSG_PROCESS_CONTINUE_READING; |
608 | 13.2k | err: |
609 | 13.2k | BIO_free(s->s3.handshake_buffer); |
610 | 13.2k | s->s3.handshake_buffer = NULL; |
611 | 13.2k | EVP_MD_CTX_free(mctx); |
612 | 13.2k | #ifndef OPENSSL_NO_GOST |
613 | 13.2k | OPENSSL_free(gost_data); |
614 | 13.2k | #endif |
615 | 13.2k | return ret; |
616 | 13.0k | } |
617 | | |
618 | | CON_FUNC_RETURN tls_construct_finished(SSL_CONNECTION *s, WPACKET *pkt) |
619 | 16.2k | { |
620 | 16.2k | size_t finish_md_len; |
621 | 16.2k | const char *sender; |
622 | 16.2k | size_t slen; |
623 | 16.2k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
624 | | |
625 | | /* This is a real handshake so make sure we clean it up at the end */ |
626 | 16.2k | if (!s->server && s->post_handshake_auth != SSL_PHA_REQUESTED) |
627 | 13.6k | s->statem.cleanuphand = 1; |
628 | | |
629 | | /* |
630 | | * If we attempted to write early data or we're in middlebox compat mode |
631 | | * then we deferred changing the handshake write keys to the last possible |
632 | | * moment. If we didn't already do this when we sent the client certificate |
633 | | * then we need to do it now. |
634 | | */ |
635 | 16.2k | if (SSL_CONNECTION_IS_TLS13(s) |
636 | 8.75k | && !s->server |
637 | 7.18k | && !SSL_IS_QUIC_HANDSHAKE(s) |
638 | 0 | && (s->early_data_state != SSL_EARLY_DATA_NONE |
639 | 0 | || (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0) |
640 | 0 | && s->s3.tmp.cert_req == 0 |
641 | 0 | && (!ssl->method->ssl3_enc->change_cipher_state(s, |
642 | 0 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) { |
643 | 0 | ; |
644 | | /* SSLfatal() already called */ |
645 | 0 | return CON_FUNC_ERROR; |
646 | 0 | } |
647 | | |
648 | 16.2k | if (s->server) { |
649 | 2.59k | sender = ssl->method->ssl3_enc->server_finished_label; |
650 | 2.59k | slen = ssl->method->ssl3_enc->server_finished_label_len; |
651 | 13.6k | } else { |
652 | 13.6k | sender = ssl->method->ssl3_enc->client_finished_label; |
653 | 13.6k | slen = ssl->method->ssl3_enc->client_finished_label_len; |
654 | 13.6k | } |
655 | | |
656 | 16.2k | finish_md_len = ssl->method->ssl3_enc->final_finish_mac(s, |
657 | 16.2k | sender, slen, |
658 | 16.2k | s->s3.tmp.finish_md); |
659 | 16.2k | if (finish_md_len == 0) { |
660 | | /* SSLfatal() already called */ |
661 | 0 | return CON_FUNC_ERROR; |
662 | 0 | } |
663 | | |
664 | 16.2k | s->s3.tmp.finish_md_len = finish_md_len; |
665 | | |
666 | 16.2k | if (!WPACKET_memcpy(pkt, s->s3.tmp.finish_md, finish_md_len)) { |
667 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
668 | 0 | return CON_FUNC_ERROR; |
669 | 0 | } |
670 | | |
671 | | /* |
672 | | * Log the master secret, if logging is enabled. We don't log it for |
673 | | * TLSv1.3: there's a different key schedule for that. |
674 | | */ |
675 | 16.2k | if (!SSL_CONNECTION_IS_TLS13(s) |
676 | 7.53k | && !ssl_log_secret(s, MASTER_SECRET_LABEL, s->session->master_key, |
677 | 7.53k | s->session->master_key_length)) { |
678 | | /* SSLfatal() already called */ |
679 | 0 | return CON_FUNC_ERROR; |
680 | 0 | } |
681 | | |
682 | | /* |
683 | | * Copy the finished so we can use it for renegotiation checks |
684 | | */ |
685 | 16.2k | if (!ossl_assert(finish_md_len <= EVP_MAX_MD_SIZE)) { |
686 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
687 | 0 | return CON_FUNC_ERROR; |
688 | 0 | } |
689 | 16.2k | if (!s->server) { |
690 | 13.6k | memcpy(s->s3.previous_client_finished, s->s3.tmp.finish_md, |
691 | 13.6k | finish_md_len); |
692 | 13.6k | s->s3.previous_client_finished_len = finish_md_len; |
693 | 13.6k | } else { |
694 | 2.59k | memcpy(s->s3.previous_server_finished, s->s3.tmp.finish_md, |
695 | 2.59k | finish_md_len); |
696 | 2.59k | s->s3.previous_server_finished_len = finish_md_len; |
697 | 2.59k | } |
698 | | |
699 | 16.2k | return CON_FUNC_SUCCESS; |
700 | 16.2k | } |
701 | | |
702 | | CON_FUNC_RETURN tls_construct_key_update(SSL_CONNECTION *s, WPACKET *pkt) |
703 | 0 | { |
704 | 0 | if (!WPACKET_put_bytes_u8(pkt, s->key_update)) { |
705 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
706 | 0 | return CON_FUNC_ERROR; |
707 | 0 | } |
708 | | |
709 | 0 | s->key_update = SSL_KEY_UPDATE_NONE; |
710 | 0 | return CON_FUNC_SUCCESS; |
711 | 0 | } |
712 | | |
713 | | MSG_PROCESS_RETURN tls_process_key_update(SSL_CONNECTION *s, PACKET *pkt) |
714 | 0 | { |
715 | 0 | unsigned int updatetype; |
716 | | |
717 | | /* |
718 | | * A KeyUpdate message signals a key change so the end of the message must |
719 | | * be on a record boundary. |
720 | | */ |
721 | 0 | if (RECORD_LAYER_processed_read_pending(&s->rlayer)) { |
722 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY); |
723 | 0 | return MSG_PROCESS_ERROR; |
724 | 0 | } |
725 | | |
726 | 0 | if (!PACKET_get_1(pkt, &updatetype) |
727 | 0 | || PACKET_remaining(pkt) != 0) { |
728 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_KEY_UPDATE); |
729 | 0 | return MSG_PROCESS_ERROR; |
730 | 0 | } |
731 | | |
732 | | /* |
733 | | * There are only two defined key update types. Fail if we get a value we |
734 | | * didn't recognise. |
735 | | */ |
736 | 0 | if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED |
737 | 0 | && updatetype != SSL_KEY_UPDATE_REQUESTED) { |
738 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_UPDATE); |
739 | 0 | return MSG_PROCESS_ERROR; |
740 | 0 | } |
741 | | |
742 | | /* |
743 | | * If we get a request for us to update our sending keys too then, we need |
744 | | * to additionally send a KeyUpdate message. However that message should |
745 | | * not also request an update (otherwise we get into an infinite loop). |
746 | | */ |
747 | 0 | if (updatetype == SSL_KEY_UPDATE_REQUESTED) |
748 | 0 | s->key_update = SSL_KEY_UPDATE_NOT_REQUESTED; |
749 | |
|
750 | 0 | if (!tls13_update_key(s, 0)) { |
751 | | /* SSLfatal() already called */ |
752 | 0 | return MSG_PROCESS_ERROR; |
753 | 0 | } |
754 | | |
755 | 0 | return MSG_PROCESS_FINISHED_READING; |
756 | 0 | } |
757 | | |
758 | | /* |
759 | | * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen |
760 | | * to far. |
761 | | */ |
762 | | int ssl3_take_mac(SSL_CONNECTION *s) |
763 | 15.5k | { |
764 | 15.5k | const char *sender; |
765 | 15.5k | size_t slen; |
766 | 15.5k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
767 | | |
768 | 15.5k | if (!s->server) { |
769 | 14.0k | sender = ssl->method->ssl3_enc->server_finished_label; |
770 | 14.0k | slen = ssl->method->ssl3_enc->server_finished_label_len; |
771 | 14.0k | } else { |
772 | 1.53k | sender = ssl->method->ssl3_enc->client_finished_label; |
773 | 1.53k | slen = ssl->method->ssl3_enc->client_finished_label_len; |
774 | 1.53k | } |
775 | | |
776 | 15.5k | s->s3.tmp.peer_finish_md_len = ssl->method->ssl3_enc->final_finish_mac(s, sender, slen, |
777 | 15.5k | s->s3.tmp.peer_finish_md); |
778 | | |
779 | 15.5k | if (s->s3.tmp.peer_finish_md_len == 0) { |
780 | | /* SSLfatal() already called */ |
781 | 0 | return 0; |
782 | 0 | } |
783 | | |
784 | 15.5k | return 1; |
785 | 15.5k | } |
786 | | |
787 | | MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL_CONNECTION *s, |
788 | | PACKET *pkt) |
789 | 15.8k | { |
790 | 15.8k | size_t remain; |
791 | | |
792 | 15.8k | remain = PACKET_remaining(pkt); |
793 | | /* |
794 | | * 'Change Cipher Spec' is just a single byte, which should already have |
795 | | * been consumed by ssl_get_message() so there should be no bytes left, |
796 | | * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes |
797 | | */ |
798 | 15.8k | if (SSL_CONNECTION_IS_DTLS(s)) { |
799 | 8.09k | if ((s->version == DTLS1_BAD_VER |
800 | 0 | && remain != DTLS1_CCS_HEADER_LENGTH + 1) |
801 | 8.09k | || (s->version != DTLS1_BAD_VER |
802 | 8.09k | && remain != DTLS1_CCS_HEADER_LENGTH - 1)) { |
803 | 6 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
804 | 6 | return MSG_PROCESS_ERROR; |
805 | 6 | } |
806 | 8.09k | } else { |
807 | 7.70k | if (remain != 0) { |
808 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
809 | 0 | return MSG_PROCESS_ERROR; |
810 | 0 | } |
811 | 7.70k | } |
812 | | |
813 | | /* Check we have a cipher to change to */ |
814 | 15.7k | if (s->s3.tmp.new_cipher == NULL) { |
815 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY); |
816 | 0 | return MSG_PROCESS_ERROR; |
817 | 0 | } |
818 | | |
819 | 15.7k | s->s3.change_cipher_spec = 1; |
820 | 15.7k | if (!ssl3_do_change_cipher_spec(s)) { |
821 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
822 | 0 | return MSG_PROCESS_ERROR; |
823 | 0 | } |
824 | | |
825 | 15.7k | if (SSL_CONNECTION_IS_DTLS(s)) { |
826 | 8.09k | if (s->version == DTLS1_BAD_VER) |
827 | 0 | s->d1->handshake_read_seq++; |
828 | | |
829 | | #ifndef OPENSSL_NO_SCTP |
830 | | /* |
831 | | * Remember that a CCS has been received, so that an old key of |
832 | | * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no |
833 | | * SCTP is used |
834 | | */ |
835 | | BIO_ctrl(SSL_get_wbio(SSL_CONNECTION_GET_SSL(s)), |
836 | | BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL); |
837 | | #endif |
838 | 8.09k | } |
839 | | |
840 | 15.7k | return MSG_PROCESS_CONTINUE_READING; |
841 | 15.7k | } |
842 | | |
843 | | MSG_PROCESS_RETURN tls_process_finished(SSL_CONNECTION *s, PACKET *pkt) |
844 | 9.15k | { |
845 | 9.15k | size_t md_len; |
846 | 9.15k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
847 | 9.15k | int was_first = SSL_IS_FIRST_HANDSHAKE(s); |
848 | 9.15k | int ok; |
849 | | |
850 | | /* This is a real handshake so make sure we clean it up at the end */ |
851 | 9.15k | if (s->server) { |
852 | | /* |
853 | | * To get this far we must have read encrypted data from the client. We |
854 | | * no longer tolerate unencrypted alerts. This is ignored if less than |
855 | | * TLSv1.3 |
856 | | */ |
857 | 964 | if (s->rlayer.rrlmethod->set_plain_alerts != NULL) |
858 | 964 | s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 0); |
859 | 964 | if (s->post_handshake_auth != SSL_PHA_REQUESTED) |
860 | 964 | s->statem.cleanuphand = 1; |
861 | 964 | if (SSL_CONNECTION_IS_TLS13(s) |
862 | 0 | && !tls13_save_handshake_digest_for_pha(s)) { |
863 | | /* SSLfatal() already called */ |
864 | 0 | return MSG_PROCESS_ERROR; |
865 | 0 | } |
866 | 964 | } |
867 | | |
868 | | /* |
869 | | * In TLSv1.3 a Finished message signals a key change so the end of the |
870 | | * message must be on a record boundary. |
871 | | */ |
872 | 9.15k | if (SSL_CONNECTION_IS_TLS13(s) |
873 | 7.23k | && RECORD_LAYER_processed_read_pending(&s->rlayer)) { |
874 | 5 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY); |
875 | 5 | return MSG_PROCESS_ERROR; |
876 | 5 | } |
877 | | |
878 | | /* If this occurs, we have missed a message */ |
879 | 9.15k | if (!SSL_CONNECTION_IS_TLS13(s) && !s->s3.change_cipher_spec) { |
880 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_GOT_A_FIN_BEFORE_A_CCS); |
881 | 0 | return MSG_PROCESS_ERROR; |
882 | 0 | } |
883 | 9.15k | s->s3.change_cipher_spec = 0; |
884 | | |
885 | 9.15k | md_len = s->s3.tmp.peer_finish_md_len; |
886 | | |
887 | 9.15k | if (md_len != PACKET_remaining(pkt)) { |
888 | 80 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_DIGEST_LENGTH); |
889 | 80 | return MSG_PROCESS_ERROR; |
890 | 80 | } |
891 | | |
892 | 9.07k | ok = CRYPTO_memcmp(PACKET_data(pkt), s->s3.tmp.peer_finish_md, |
893 | 9.07k | md_len); |
894 | 9.07k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
895 | 9.07k | if (ok != 0) { |
896 | 9.06k | if ((PACKET_data(pkt)[0] ^ s->s3.tmp.peer_finish_md[0]) != 0xFF) { |
897 | 9.04k | ok = 0; |
898 | 9.04k | } |
899 | 9.06k | } |
900 | 9.07k | #endif |
901 | 9.07k | if (ok != 0) { |
902 | 18 | SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DIGEST_CHECK_FAILED); |
903 | 18 | return MSG_PROCESS_ERROR; |
904 | 18 | } |
905 | | |
906 | | /* |
907 | | * Copy the finished so we can use it for renegotiation checks |
908 | | */ |
909 | 9.05k | if (!ossl_assert(md_len <= EVP_MAX_MD_SIZE)) { |
910 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
911 | 0 | return MSG_PROCESS_ERROR; |
912 | 0 | } |
913 | 9.05k | if (s->server) { |
914 | 940 | memcpy(s->s3.previous_client_finished, s->s3.tmp.peer_finish_md, |
915 | 940 | md_len); |
916 | 940 | s->s3.previous_client_finished_len = md_len; |
917 | 8.11k | } else { |
918 | 8.11k | memcpy(s->s3.previous_server_finished, s->s3.tmp.peer_finish_md, |
919 | 8.11k | md_len); |
920 | 8.11k | s->s3.previous_server_finished_len = md_len; |
921 | 8.11k | } |
922 | | |
923 | | /* |
924 | | * In TLS1.3 we also have to change cipher state and do any final processing |
925 | | * of the initial server flight (if we are a client) |
926 | | */ |
927 | 9.05k | if (SSL_CONNECTION_IS_TLS13(s)) { |
928 | 7.18k | if (s->server) { |
929 | 0 | if (s->post_handshake_auth != SSL_PHA_REQUESTED && !ssl->method->ssl3_enc->change_cipher_state(s, SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_READ)) { |
930 | | /* SSLfatal() already called */ |
931 | 0 | return MSG_PROCESS_ERROR; |
932 | 0 | } |
933 | 7.18k | } else { |
934 | | /* TLS 1.3 gets the secret size from the handshake md */ |
935 | 7.18k | size_t dummy; |
936 | 7.18k | if (!ssl->method->ssl3_enc->generate_master_secret(s, |
937 | 7.18k | s->master_secret, s->handshake_secret, 0, |
938 | 7.18k | &dummy)) { |
939 | | /* SSLfatal() already called */ |
940 | 0 | return MSG_PROCESS_ERROR; |
941 | 0 | } |
942 | 7.18k | if (!tls13_store_server_finished_hash(s)) { |
943 | | /* SSLfatal() already called */ |
944 | 0 | return MSG_PROCESS_ERROR; |
945 | 0 | } |
946 | | |
947 | | /* |
948 | | * For non-QUIC we set up the client's app data read keys now, so |
949 | | * that we can go straight into reading 0.5RTT data from the server. |
950 | | * For QUIC we don't do that, and instead defer setting up the keys |
951 | | * until after we have set up the write keys in order to ensure that |
952 | | * write keys are always set up before read keys (so that if we read |
953 | | * a message we have the correct keys in place to ack it) |
954 | | */ |
955 | 7.18k | if (!SSL_IS_QUIC_HANDSHAKE(s) |
956 | 0 | && !ssl->method->ssl3_enc->change_cipher_state(s, |
957 | 0 | SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) { |
958 | | /* SSLfatal() already called */ |
959 | 0 | return MSG_PROCESS_ERROR; |
960 | 0 | } |
961 | 7.18k | if (!tls_process_initial_server_flight(s)) { |
962 | | /* SSLfatal() already called */ |
963 | 0 | return MSG_PROCESS_ERROR; |
964 | 0 | } |
965 | 7.18k | } |
966 | 7.18k | } |
967 | | |
968 | 9.05k | if (was_first |
969 | 8.12k | && !SSL_IS_FIRST_HANDSHAKE(s) |
970 | 0 | && s->rlayer.rrlmethod->set_first_handshake != NULL) |
971 | 0 | s->rlayer.rrlmethod->set_first_handshake(s->rlayer.rrl, 0); |
972 | | |
973 | 9.05k | return MSG_PROCESS_FINISHED_READING; |
974 | 9.05k | } |
975 | | |
976 | | CON_FUNC_RETURN tls_construct_change_cipher_spec(SSL_CONNECTION *s, WPACKET *pkt) |
977 | 14.5k | { |
978 | 14.5k | if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) { |
979 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
980 | 0 | return CON_FUNC_ERROR; |
981 | 0 | } |
982 | | |
983 | 14.5k | return CON_FUNC_SUCCESS; |
984 | 14.5k | } |
985 | | |
986 | | /* Add a certificate to the WPACKET */ |
987 | | static int ssl_add_cert_to_wpacket(SSL_CONNECTION *s, WPACKET *pkt, |
988 | | X509 *x, int chain, int for_comp) |
989 | 24.5k | { |
990 | 24.5k | int len; |
991 | 24.5k | unsigned char *outbytes; |
992 | 24.5k | int context = SSL_EXT_TLS1_3_CERTIFICATE; |
993 | | |
994 | 24.5k | if (for_comp) |
995 | 0 | context |= SSL_EXT_TLS1_3_CERTIFICATE_COMPRESSION; |
996 | | |
997 | 24.5k | len = i2d_X509(x, NULL); |
998 | 24.5k | if (len < 0) { |
999 | 0 | if (!for_comp) |
1000 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BUF_LIB); |
1001 | 0 | return 0; |
1002 | 0 | } |
1003 | 24.5k | if (!WPACKET_sub_allocate_bytes_u24(pkt, len, &outbytes) |
1004 | 24.5k | || i2d_X509(x, &outbytes) != len) { |
1005 | 0 | if (!for_comp) |
1006 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1007 | 0 | return 0; |
1008 | 0 | } |
1009 | | |
1010 | 24.5k | if ((SSL_CONNECTION_IS_TLS13(s) || for_comp) |
1011 | 2.49k | && !tls_construct_extensions(s, pkt, context, x, chain)) { |
1012 | | /* SSLfatal() already called */ |
1013 | 0 | return 0; |
1014 | 0 | } |
1015 | | |
1016 | 24.5k | return 1; |
1017 | 24.5k | } |
1018 | | |
1019 | | /* Add certificate chain to provided WPACKET */ |
1020 | | static int ssl_add_cert_chain(SSL_CONNECTION *s, WPACKET *pkt, CERT_PKEY *cpk, int for_comp) |
1021 | 24.5k | { |
1022 | 24.5k | int i, chain_count; |
1023 | 24.5k | X509 *x; |
1024 | 24.5k | STACK_OF(X509) *extra_certs; |
1025 | 24.5k | STACK_OF(X509) *chain = NULL; |
1026 | 24.5k | X509_STORE *chain_store; |
1027 | 24.5k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1028 | | |
1029 | 24.5k | if (cpk == NULL || cpk->x509 == NULL) |
1030 | 24 | return 1; |
1031 | | |
1032 | 24.5k | x = cpk->x509; |
1033 | | |
1034 | | /* |
1035 | | * If we have a certificate specific chain use it, else use parent ctx. |
1036 | | */ |
1037 | 24.5k | if (cpk->chain != NULL) |
1038 | 0 | extra_certs = cpk->chain; |
1039 | 24.5k | else |
1040 | 24.5k | extra_certs = sctx->extra_certs; |
1041 | | |
1042 | 24.5k | if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) |
1043 | 0 | chain_store = NULL; |
1044 | 24.5k | else if (s->cert->chain_store) |
1045 | 0 | chain_store = s->cert->chain_store; |
1046 | 24.5k | else |
1047 | 24.5k | chain_store = sctx->cert_store; |
1048 | | |
1049 | 24.5k | if (chain_store != NULL) { |
1050 | 24.5k | X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new_ex(sctx->libctx, |
1051 | 24.5k | sctx->propq); |
1052 | | |
1053 | 24.5k | if (xs_ctx == NULL) { |
1054 | 0 | if (!for_comp) |
1055 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_X509_LIB); |
1056 | 0 | return 0; |
1057 | 0 | } |
1058 | 24.5k | if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) { |
1059 | 0 | X509_STORE_CTX_free(xs_ctx); |
1060 | 0 | if (!for_comp) |
1061 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_X509_LIB); |
1062 | 0 | return 0; |
1063 | 0 | } |
1064 | | /* |
1065 | | * It is valid for the chain not to be complete (because normally we |
1066 | | * don't include the root cert in the chain). Therefore we deliberately |
1067 | | * ignore the error return from this call. We're not actually verifying |
1068 | | * the cert - we're just building as much of the chain as we can |
1069 | | */ |
1070 | 24.5k | (void)X509_verify_cert(xs_ctx); |
1071 | | /* Don't leave errors in the queue */ |
1072 | 24.5k | ERR_clear_error(); |
1073 | 24.5k | chain = X509_STORE_CTX_get0_chain(xs_ctx); |
1074 | 24.5k | i = ssl_security_cert_chain(s, chain, NULL, 0); |
1075 | 24.5k | if (i != 1) { |
1076 | | #if 0 |
1077 | | /* Dummy error calls so mkerr generates them */ |
1078 | | ERR_raise(ERR_LIB_SSL, SSL_R_EE_KEY_TOO_SMALL); |
1079 | | ERR_raise(ERR_LIB_SSL, SSL_R_CA_KEY_TOO_SMALL); |
1080 | | ERR_raise(ERR_LIB_SSL, SSL_R_CA_MD_TOO_WEAK); |
1081 | | #endif |
1082 | 0 | X509_STORE_CTX_free(xs_ctx); |
1083 | 0 | if (!for_comp) |
1084 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, i); |
1085 | 0 | return 0; |
1086 | 0 | } |
1087 | 24.5k | chain_count = sk_X509_num(chain); |
1088 | 49.0k | for (i = 0; i < chain_count; i++) { |
1089 | 24.5k | x = sk_X509_value(chain, i); |
1090 | | |
1091 | 24.5k | if (!ssl_add_cert_to_wpacket(s, pkt, x, i, for_comp)) { |
1092 | | /* SSLfatal() already called */ |
1093 | 0 | X509_STORE_CTX_free(xs_ctx); |
1094 | 0 | return 0; |
1095 | 0 | } |
1096 | 24.5k | } |
1097 | 24.5k | X509_STORE_CTX_free(xs_ctx); |
1098 | 24.5k | } else { |
1099 | 0 | i = ssl_security_cert_chain(s, extra_certs, x, 0); |
1100 | 0 | if (i != 1) { |
1101 | 0 | if (!for_comp) |
1102 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, i); |
1103 | 0 | return 0; |
1104 | 0 | } |
1105 | 0 | if (!ssl_add_cert_to_wpacket(s, pkt, x, 0, for_comp)) { |
1106 | | /* SSLfatal() already called */ |
1107 | 0 | return 0; |
1108 | 0 | } |
1109 | 0 | for (i = 0; i < sk_X509_num(extra_certs); i++) { |
1110 | 0 | x = sk_X509_value(extra_certs, i); |
1111 | 0 | if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1, for_comp)) { |
1112 | | /* SSLfatal() already called */ |
1113 | 0 | return 0; |
1114 | 0 | } |
1115 | 0 | } |
1116 | 0 | } |
1117 | 24.5k | return 1; |
1118 | 24.5k | } |
1119 | | |
1120 | | EVP_PKEY *tls_get_peer_pkey(const SSL_CONNECTION *sc) |
1121 | 31.6k | { |
1122 | 31.6k | if (sc->session->peer_rpk != NULL) |
1123 | 0 | return sc->session->peer_rpk; |
1124 | 31.6k | if (sc->session->peer != NULL) |
1125 | 31.6k | return X509_get0_pubkey(sc->session->peer); |
1126 | 0 | return NULL; |
1127 | 31.6k | } |
1128 | | |
1129 | | int tls_process_rpk(SSL_CONNECTION *sc, PACKET *pkt, EVP_PKEY **peer_rpk) |
1130 | 0 | { |
1131 | 0 | EVP_PKEY *pkey = NULL; |
1132 | 0 | int ret = 0; |
1133 | 0 | RAW_EXTENSION *rawexts = NULL; |
1134 | 0 | PACKET extensions; |
1135 | 0 | PACKET context; |
1136 | 0 | unsigned long cert_len = 0, spki_len = 0; |
1137 | 0 | const unsigned char *spki, *spkistart; |
1138 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(sc); |
1139 | | |
1140 | | /*- |
1141 | | * ---------------------------- |
1142 | | * TLS 1.3 Certificate message: |
1143 | | * ---------------------------- |
1144 | | * https://datatracker.ietf.org/doc/html/rfc8446#section-4.4.2 |
1145 | | * |
1146 | | * enum { |
1147 | | * X509(0), |
1148 | | * RawPublicKey(2), |
1149 | | * (255) |
1150 | | * } CertificateType; |
1151 | | * |
1152 | | * struct { |
1153 | | * select (certificate_type) { |
1154 | | * case RawPublicKey: |
1155 | | * // From RFC 7250 ASN.1_subjectPublicKeyInfo |
1156 | | * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>; |
1157 | | * |
1158 | | * case X509: |
1159 | | * opaque cert_data<1..2^24-1>; |
1160 | | * }; |
1161 | | * Extension extensions<0..2^16-1>; |
1162 | | * } CertificateEntry; |
1163 | | * |
1164 | | * struct { |
1165 | | * opaque certificate_request_context<0..2^8-1>; |
1166 | | * CertificateEntry certificate_list<0..2^24-1>; |
1167 | | * } Certificate; |
1168 | | * |
1169 | | * The client MUST send a Certificate message if and only if the server |
1170 | | * has requested client authentication via a CertificateRequest message |
1171 | | * (Section 4.3.2). If the server requests client authentication but no |
1172 | | * suitable certificate is available, the client MUST send a Certificate |
1173 | | * message containing no certificates (i.e., with the "certificate_list" |
1174 | | * field having length 0). |
1175 | | * |
1176 | | * ---------------------------- |
1177 | | * TLS 1.2 Certificate message: |
1178 | | * ---------------------------- |
1179 | | * https://datatracker.ietf.org/doc/html/rfc7250#section-3 |
1180 | | * |
1181 | | * opaque ASN.1Cert<1..2^24-1>; |
1182 | | * |
1183 | | * struct { |
1184 | | * select(certificate_type){ |
1185 | | * |
1186 | | * // certificate type defined in this document. |
1187 | | * case RawPublicKey: |
1188 | | * opaque ASN.1_subjectPublicKeyInfo<1..2^24-1>; |
1189 | | * |
1190 | | * // X.509 certificate defined in RFC 5246 |
1191 | | * case X.509: |
1192 | | * ASN.1Cert certificate_list<0..2^24-1>; |
1193 | | * |
1194 | | * // Additional certificate type based on |
1195 | | * // "TLS Certificate Types" subregistry |
1196 | | * }; |
1197 | | * } Certificate; |
1198 | | * |
1199 | | * ------------- |
1200 | | * Consequently: |
1201 | | * ------------- |
1202 | | * After the (TLS 1.3 only) context octet string (1 byte length + data) the |
1203 | | * Certificate message has a 3-byte length that is zero in the client to |
1204 | | * server message when the client has no RPK to send. In that case, there |
1205 | | * are no (TLS 1.3 only) per-certificate extensions either, because the |
1206 | | * [CertificateEntry] list is empty. |
1207 | | * |
1208 | | * In the server to client direction, or when the client had an RPK to send, |
1209 | | * the TLS 1.3 message just prepends the length of the RPK+extensions, |
1210 | | * while TLS <= 1.2 sends just the RPK (octet-string). |
1211 | | * |
1212 | | * The context must be zero-length in the server to client direction, and |
1213 | | * must match the value recorded in the certificate request in the client |
1214 | | * to server direction. |
1215 | | */ |
1216 | 0 | if (SSL_CONNECTION_IS_TLS13(sc)) { |
1217 | 0 | if (!PACKET_get_length_prefixed_1(pkt, &context)) { |
1218 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT); |
1219 | 0 | goto err; |
1220 | 0 | } |
1221 | 0 | if (sc->server) { |
1222 | 0 | if (sc->pha_context == NULL) { |
1223 | 0 | if (PACKET_remaining(&context) != 0) { |
1224 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT); |
1225 | 0 | goto err; |
1226 | 0 | } |
1227 | 0 | } else { |
1228 | 0 | if (!PACKET_equal(&context, sc->pha_context, sc->pha_context_len)) { |
1229 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT); |
1230 | 0 | goto err; |
1231 | 0 | } |
1232 | 0 | } |
1233 | 0 | } else { |
1234 | 0 | if (PACKET_remaining(&context) != 0) { |
1235 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT); |
1236 | 0 | goto err; |
1237 | 0 | } |
1238 | 0 | } |
1239 | 0 | } |
1240 | | |
1241 | 0 | if (!PACKET_get_net_3(pkt, &cert_len) |
1242 | 0 | || PACKET_remaining(pkt) != cert_len) { |
1243 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1244 | 0 | goto err; |
1245 | 0 | } |
1246 | | |
1247 | | /* |
1248 | | * The list length may be zero when there is no RPK. In the case of TLS |
1249 | | * 1.2 this is actually the RPK length, which cannot be zero as specified, |
1250 | | * but that breaks the ability of the client to decline client auth. We |
1251 | | * overload the 0 RPK length to mean "no RPK". This interpretation is |
1252 | | * also used some other (reference?) implementations, but is not supported |
1253 | | * by the verbatim RFC7250 text. |
1254 | | */ |
1255 | 0 | if (cert_len == 0) |
1256 | 0 | return 1; |
1257 | | |
1258 | 0 | if (SSL_CONNECTION_IS_TLS13(sc)) { |
1259 | | /* |
1260 | | * With TLS 1.3, a non-empty explicit-length RPK octet-string followed |
1261 | | * by a possibly empty extension block. |
1262 | | */ |
1263 | 0 | if (!PACKET_get_net_3(pkt, &spki_len)) { |
1264 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1265 | 0 | goto err; |
1266 | 0 | } |
1267 | 0 | if (spki_len == 0) { |
1268 | | /* empty RPK */ |
1269 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_EMPTY_RAW_PUBLIC_KEY); |
1270 | 0 | goto err; |
1271 | 0 | } |
1272 | 0 | } else { |
1273 | 0 | spki_len = cert_len; |
1274 | 0 | } |
1275 | | |
1276 | 0 | if (!PACKET_get_bytes(pkt, &spki, spki_len)) { |
1277 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1278 | 0 | goto err; |
1279 | 0 | } |
1280 | 0 | spkistart = spki; |
1281 | 0 | if ((pkey = d2i_PUBKEY_ex(NULL, &spki, spki_len, sctx->libctx, sctx->propq)) == NULL |
1282 | 0 | || spki != (spkistart + spki_len)) { |
1283 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1284 | 0 | goto err; |
1285 | 0 | } |
1286 | 0 | if (EVP_PKEY_missing_parameters(pkey)) { |
1287 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, |
1288 | 0 | SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); |
1289 | 0 | goto err; |
1290 | 0 | } |
1291 | | |
1292 | | /* Process the Extensions block */ |
1293 | 0 | if (SSL_CONNECTION_IS_TLS13(sc)) { |
1294 | 0 | if (PACKET_remaining(pkt) != (cert_len - 3 - spki_len)) { |
1295 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH); |
1296 | 0 | goto err; |
1297 | 0 | } |
1298 | 0 | if (!PACKET_as_length_prefixed_2(pkt, &extensions) |
1299 | 0 | || PACKET_remaining(pkt) != 0) { |
1300 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1301 | 0 | goto err; |
1302 | 0 | } |
1303 | 0 | if (!tls_collect_extensions(sc, &extensions, SSL_EXT_TLS1_3_RAW_PUBLIC_KEY, |
1304 | 0 | &rawexts, NULL, 1)) { |
1305 | | /* SSLfatal already called */ |
1306 | 0 | goto err; |
1307 | 0 | } |
1308 | | /* chain index is always zero and fin always 1 for RPK */ |
1309 | 0 | if (!tls_parse_all_extensions(sc, SSL_EXT_TLS1_3_RAW_PUBLIC_KEY, |
1310 | 0 | rawexts, NULL, 0, 1)) { |
1311 | | /* SSLfatal already called */ |
1312 | 0 | goto err; |
1313 | 0 | } |
1314 | 0 | } |
1315 | 0 | ret = 1; |
1316 | 0 | if (peer_rpk != NULL) { |
1317 | 0 | *peer_rpk = pkey; |
1318 | 0 | pkey = NULL; |
1319 | 0 | } |
1320 | |
|
1321 | 0 | err: |
1322 | 0 | OPENSSL_free(rawexts); |
1323 | 0 | EVP_PKEY_free(pkey); |
1324 | 0 | return ret; |
1325 | 0 | } |
1326 | | |
1327 | | unsigned long tls_output_rpk(SSL_CONNECTION *sc, WPACKET *pkt, CERT_PKEY *cpk) |
1328 | 0 | { |
1329 | 0 | int pdata_len = 0; |
1330 | 0 | unsigned char *pdata = NULL; |
1331 | 0 | X509_PUBKEY *xpk = NULL; |
1332 | 0 | unsigned long ret = 0; |
1333 | 0 | X509 *x509 = NULL; |
1334 | |
|
1335 | 0 | if (cpk != NULL && cpk->x509 != NULL) { |
1336 | 0 | x509 = cpk->x509; |
1337 | | /* Get the RPK from the certificate */ |
1338 | 0 | xpk = X509_get_X509_PUBKEY(cpk->x509); |
1339 | 0 | if (xpk == NULL) { |
1340 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1341 | 0 | goto err; |
1342 | 0 | } |
1343 | 0 | pdata_len = i2d_X509_PUBKEY(xpk, &pdata); |
1344 | 0 | } else if (cpk != NULL && cpk->privatekey != NULL) { |
1345 | | /* Get the RPK from the private key */ |
1346 | 0 | pdata_len = i2d_PUBKEY(cpk->privatekey, &pdata); |
1347 | 0 | } else { |
1348 | | /* The server RPK is not optional */ |
1349 | 0 | if (sc->server) { |
1350 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1351 | 0 | goto err; |
1352 | 0 | } |
1353 | | /* The client can send a zero length certificate list */ |
1354 | 0 | if (!WPACKET_sub_memcpy_u24(pkt, pdata, pdata_len)) { |
1355 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1356 | 0 | goto err; |
1357 | 0 | } |
1358 | 0 | return 1; |
1359 | 0 | } |
1360 | | |
1361 | 0 | if (pdata_len <= 0) { |
1362 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1363 | 0 | goto err; |
1364 | 0 | } |
1365 | | |
1366 | | /* |
1367 | | * TLSv1.2 is _just_ the raw public key |
1368 | | * TLSv1.3 includes extensions, so there's a length wrapper |
1369 | | */ |
1370 | 0 | if (SSL_CONNECTION_IS_TLS13(sc)) { |
1371 | 0 | if (!WPACKET_start_sub_packet_u24(pkt)) { |
1372 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1373 | 0 | goto err; |
1374 | 0 | } |
1375 | 0 | } |
1376 | | |
1377 | 0 | if (!WPACKET_sub_memcpy_u24(pkt, pdata, pdata_len)) { |
1378 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1379 | 0 | goto err; |
1380 | 0 | } |
1381 | | |
1382 | 0 | if (SSL_CONNECTION_IS_TLS13(sc)) { |
1383 | | /* |
1384 | | * Only send extensions relevant to raw public keys. Until such |
1385 | | * extensions are defined, this will be an empty set of extensions. |
1386 | | * |x509| may be NULL, which raw public-key extensions need to handle. |
1387 | | */ |
1388 | 0 | if (!tls_construct_extensions(sc, pkt, SSL_EXT_TLS1_3_RAW_PUBLIC_KEY, |
1389 | 0 | x509, 0)) { |
1390 | | /* SSLfatal() already called */ |
1391 | 0 | goto err; |
1392 | 0 | } |
1393 | 0 | if (!WPACKET_close(pkt)) { |
1394 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1395 | 0 | goto err; |
1396 | 0 | } |
1397 | 0 | } |
1398 | | |
1399 | 0 | ret = 1; |
1400 | 0 | err: |
1401 | 0 | OPENSSL_free(pdata); |
1402 | 0 | return ret; |
1403 | 0 | } |
1404 | | |
1405 | | unsigned long ssl3_output_cert_chain(SSL_CONNECTION *s, WPACKET *pkt, |
1406 | | CERT_PKEY *cpk, int for_comp) |
1407 | 24.5k | { |
1408 | 24.5k | if (!WPACKET_start_sub_packet_u24(pkt)) { |
1409 | 0 | if (!for_comp) |
1410 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1411 | 0 | return 0; |
1412 | 0 | } |
1413 | | |
1414 | 24.5k | if (!ssl_add_cert_chain(s, pkt, cpk, for_comp)) |
1415 | 0 | return 0; |
1416 | | |
1417 | 24.5k | if (!WPACKET_close(pkt)) { |
1418 | 0 | if (!for_comp) |
1419 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1420 | 0 | return 0; |
1421 | 0 | } |
1422 | | |
1423 | 24.5k | return 1; |
1424 | 24.5k | } |
1425 | | |
1426 | | /* |
1427 | | * Tidy up after the end of a handshake. In the case of SCTP this may result |
1428 | | * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is |
1429 | | * freed up as well. |
1430 | | */ |
1431 | | WORK_STATE tls_finish_handshake(SSL_CONNECTION *s, ossl_unused WORK_STATE wst, |
1432 | | int clearbufs, int stop) |
1433 | 47.2k | { |
1434 | 47.2k | void (*cb)(const SSL *ssl, int type, int val) = NULL; |
1435 | 47.2k | int cleanuphand = s->statem.cleanuphand; |
1436 | 47.2k | SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s); |
1437 | 47.2k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1438 | | |
1439 | 47.2k | if (clearbufs) { |
1440 | 47.2k | if (!SSL_CONNECTION_IS_DTLS(s) |
1441 | | #ifndef OPENSSL_NO_SCTP |
1442 | | /* |
1443 | | * RFC6083: SCTP provides a reliable and in-sequence transport service for DTLS |
1444 | | * messages that require it. Therefore, DTLS procedures for retransmissions |
1445 | | * MUST NOT be used. |
1446 | | * Hence the init_buf can be cleared when DTLS over SCTP as transport is used. |
1447 | | */ |
1448 | | || BIO_dgram_is_sctp(SSL_get_wbio(SSL_CONNECTION_GET_SSL(s))) |
1449 | | #endif |
1450 | 47.2k | ) { |
1451 | | /* |
1452 | | * We don't do this in DTLS over UDP because we may still need the init_buf |
1453 | | * in case there are any unexpected retransmits |
1454 | | */ |
1455 | 47.2k | BUF_MEM_free(s->init_buf); |
1456 | 47.2k | s->init_buf = NULL; |
1457 | 47.2k | } |
1458 | | |
1459 | 47.2k | if (!ssl_free_wbio_buffer(s)) { |
1460 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1461 | 0 | return WORK_ERROR; |
1462 | 0 | } |
1463 | 47.2k | s->init_num = 0; |
1464 | 47.2k | } |
1465 | | |
1466 | 47.2k | if (SSL_CONNECTION_IS_TLS13(s) && !s->server |
1467 | 14.5k | && s->post_handshake_auth == SSL_PHA_REQUESTED) |
1468 | 0 | s->post_handshake_auth = SSL_PHA_EXT_SENT; |
1469 | | |
1470 | | /* |
1471 | | * Only set if there was a Finished message and this isn't after a TLSv1.3 |
1472 | | * post handshake exchange |
1473 | | */ |
1474 | 47.2k | if (cleanuphand) { |
1475 | | /* skipped if we just sent a HelloRequest */ |
1476 | 15.3k | s->renegotiate = 0; |
1477 | 15.3k | s->new_session = 0; |
1478 | 15.3k | s->statem.cleanuphand = 0; |
1479 | 15.3k | s->ext.ticket_expected = 0; |
1480 | | |
1481 | 15.3k | ssl3_cleanup_key_block(s); |
1482 | | |
1483 | 15.3k | if (s->server) { |
1484 | | /* |
1485 | | * In TLSv1.3 we update the cache as part of constructing the |
1486 | | * NewSessionTicket |
1487 | | */ |
1488 | 1.49k | if (!SSL_CONNECTION_IS_TLS13(s)) |
1489 | 1.49k | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); |
1490 | | |
1491 | | /* N.B. s->ctx may not equal s->session_ctx */ |
1492 | 1.49k | ssl_tsan_counter(sctx, &sctx->stats.sess_accept_good); |
1493 | 1.49k | s->handshake_func = ossl_statem_accept; |
1494 | 13.8k | } else { |
1495 | 13.8k | if (SSL_CONNECTION_IS_TLS13(s)) { |
1496 | | /* |
1497 | | * We encourage applications to only use TLSv1.3 tickets once, |
1498 | | * so we remove this one from the cache. |
1499 | | */ |
1500 | 12.3k | if ((s->session_ctx->session_cache_mode |
1501 | 12.3k | & SSL_SESS_CACHE_CLIENT) |
1502 | 12.3k | != 0) |
1503 | 0 | SSL_CTX_remove_session(s->session_ctx, s->session); |
1504 | 12.3k | } else { |
1505 | | /* |
1506 | | * In TLSv1.3 we update the cache as part of processing the |
1507 | | * NewSessionTicket |
1508 | | */ |
1509 | 1.48k | ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); |
1510 | 1.48k | } |
1511 | 13.8k | if (s->hit) |
1512 | 0 | ssl_tsan_counter(s->session_ctx, |
1513 | 0 | &s->session_ctx->stats.sess_hit); |
1514 | | |
1515 | 13.8k | s->handshake_func = ossl_statem_connect; |
1516 | 13.8k | ssl_tsan_counter(s->session_ctx, |
1517 | 13.8k | &s->session_ctx->stats.sess_connect_good); |
1518 | 13.8k | } |
1519 | | |
1520 | 15.3k | if (SSL_CONNECTION_IS_DTLS(s)) { |
1521 | | /* done with handshaking */ |
1522 | 0 | s->d1->handshake_read_seq = 0; |
1523 | 0 | s->d1->handshake_write_seq = 0; |
1524 | 0 | s->d1->next_handshake_write_seq = 0; |
1525 | 0 | dtls1_clear_received_buffer(s); |
1526 | 0 | } |
1527 | 15.3k | } |
1528 | | |
1529 | 47.2k | if (s->info_callback != NULL) |
1530 | 0 | cb = s->info_callback; |
1531 | 47.2k | else if (sctx->info_callback != NULL) |
1532 | 0 | cb = sctx->info_callback; |
1533 | | |
1534 | | /* The callback may expect us to not be in init at handshake done */ |
1535 | 47.2k | ossl_statem_set_in_init(s, 0); |
1536 | | |
1537 | 47.2k | if (cb != NULL) { |
1538 | 0 | if (cleanuphand |
1539 | 0 | || !SSL_CONNECTION_IS_TLS13(s) |
1540 | 0 | || SSL_IS_FIRST_HANDSHAKE(s)) |
1541 | 0 | cb(ssl, SSL_CB_HANDSHAKE_DONE, 1); |
1542 | 0 | } |
1543 | | |
1544 | 47.2k | if (!stop) { |
1545 | | /* If we've got more work to do we go back into init */ |
1546 | 0 | ossl_statem_set_in_init(s, 1); |
1547 | 0 | return WORK_FINISHED_CONTINUE; |
1548 | 0 | } |
1549 | | |
1550 | 47.2k | return WORK_FINISHED_STOP; |
1551 | 47.2k | } |
1552 | | |
1553 | | int tls_get_message_header(SSL_CONNECTION *s, int *mt) |
1554 | 47.0M | { |
1555 | | /* s->init_num < SSL3_HM_HEADER_LENGTH */ |
1556 | 47.0M | int skip_message, i; |
1557 | 47.0M | uint8_t recvd_type; |
1558 | 47.0M | unsigned char *p; |
1559 | 47.0M | size_t l, readbytes; |
1560 | 47.0M | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
1561 | 47.0M | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
1562 | | |
1563 | 47.0M | p = (unsigned char *)s->init_buf->data; |
1564 | | |
1565 | 47.0M | do { |
1566 | 47.3M | while (s->init_num < SSL3_HM_HEADER_LENGTH) { |
1567 | 47.0M | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, &recvd_type, |
1568 | 47.0M | &p[s->init_num], |
1569 | 47.0M | SSL3_HM_HEADER_LENGTH - s->init_num, |
1570 | 47.0M | 0, &readbytes); |
1571 | 47.0M | if (i <= 0) { |
1572 | 46.7M | s->rwstate = SSL_READING; |
1573 | 46.7M | return 0; |
1574 | 46.7M | } |
1575 | 302k | if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) { |
1576 | | /* |
1577 | | * A ChangeCipherSpec must be a single byte and may not occur |
1578 | | * in the middle of a handshake message. |
1579 | | */ |
1580 | 7.84k | if (s->init_num != 0 || readbytes != 1 || p[0] != SSL3_MT_CCS) { |
1581 | 115 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, |
1582 | 115 | SSL_R_BAD_CHANGE_CIPHER_SPEC); |
1583 | 115 | return 0; |
1584 | 115 | } |
1585 | 7.72k | if (s->statem.hand_state == TLS_ST_BEFORE |
1586 | 6 | && (s->s3.flags & TLS1_FLAGS_STATELESS) != 0) { |
1587 | | /* |
1588 | | * We are stateless and we received a CCS. Probably this is |
1589 | | * from a client between the first and second ClientHellos. |
1590 | | * We should ignore this, but return an error because we do |
1591 | | * not return success until we see the second ClientHello |
1592 | | * with a valid cookie. |
1593 | | */ |
1594 | 0 | return 0; |
1595 | 0 | } |
1596 | 7.72k | s->s3.tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC; |
1597 | 7.72k | s->init_num = readbytes - 1; |
1598 | 7.72k | s->init_msg = s->init_buf->data; |
1599 | 7.72k | s->s3.tmp.message_size = readbytes; |
1600 | 7.72k | return 1; |
1601 | 294k | } else if (recvd_type != SSL3_RT_HANDSHAKE) { |
1602 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, |
1603 | 0 | SSL_R_CCS_RECEIVED_EARLY); |
1604 | 0 | return 0; |
1605 | 0 | } |
1606 | 294k | s->init_num += readbytes; |
1607 | 294k | } |
1608 | | |
1609 | 285k | skip_message = 0; |
1610 | 285k | if (!s->server) |
1611 | 215k | if (s->statem.hand_state != TLS_ST_OK |
1612 | 207k | && p[0] == SSL3_MT_HELLO_REQUEST) |
1613 | | /* |
1614 | | * The server may always send 'Hello Request' messages -- |
1615 | | * we are doing a handshake anyway now, so ignore them if |
1616 | | * their format is correct. Does not count for 'Finished' |
1617 | | * MAC. |
1618 | | */ |
1619 | 18.8k | if (p[1] == 0 && p[2] == 0 && p[3] == 0) { |
1620 | 16.3k | s->init_num = 0; |
1621 | 16.3k | skip_message = 1; |
1622 | | |
1623 | 16.3k | if (s->msg_callback) |
1624 | 0 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, |
1625 | 0 | p, SSL3_HM_HEADER_LENGTH, ussl, |
1626 | 0 | s->msg_callback_arg); |
1627 | 16.3k | } |
1628 | 285k | } while (skip_message); |
1629 | | /* s->init_num == SSL3_HM_HEADER_LENGTH */ |
1630 | | |
1631 | 269k | *mt = *p; |
1632 | 269k | s->s3.tmp.message_type = *(p++); |
1633 | | |
1634 | 269k | if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) { |
1635 | | /* |
1636 | | * Only happens with SSLv3+ in an SSLv2 backward compatible |
1637 | | * ClientHello |
1638 | | * |
1639 | | * Total message size is the remaining record bytes to read |
1640 | | * plus the SSL3_HM_HEADER_LENGTH bytes that we already read |
1641 | | */ |
1642 | 5.98k | l = s->rlayer.tlsrecs[0].length + SSL3_HM_HEADER_LENGTH; |
1643 | 5.98k | s->s3.tmp.message_size = l; |
1644 | | |
1645 | 5.98k | s->init_msg = s->init_buf->data; |
1646 | 5.98k | s->init_num = SSL3_HM_HEADER_LENGTH; |
1647 | 263k | } else { |
1648 | 263k | n2l3(p, l); |
1649 | | /* BUF_MEM_grow takes an 'int' parameter */ |
1650 | 263k | if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) { |
1651 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1652 | 0 | SSL_R_EXCESSIVE_MESSAGE_SIZE); |
1653 | 0 | return 0; |
1654 | 0 | } |
1655 | 263k | s->s3.tmp.message_size = l; |
1656 | | |
1657 | 263k | s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH; |
1658 | 263k | s->init_num = 0; |
1659 | 263k | } |
1660 | | |
1661 | 269k | return 1; |
1662 | 269k | } |
1663 | | |
1664 | | int tls_get_message_body(SSL_CONNECTION *s, size_t *len) |
1665 | 16.2M | { |
1666 | 16.2M | size_t n, readbytes; |
1667 | 16.2M | unsigned char *p; |
1668 | 16.2M | int i; |
1669 | 16.2M | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
1670 | 16.2M | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
1671 | | |
1672 | 16.2M | if (s->s3.tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) { |
1673 | | /* We've already read everything in */ |
1674 | 7.70k | *len = (unsigned long)s->init_num; |
1675 | 7.70k | return 1; |
1676 | 7.70k | } |
1677 | | |
1678 | 16.2M | p = s->init_msg; |
1679 | 16.2M | n = s->s3.tmp.message_size - s->init_num; |
1680 | 16.5M | while (n > 0) { |
1681 | 16.3M | i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL, |
1682 | 16.3M | &p[s->init_num], n, 0, &readbytes); |
1683 | 16.3M | if (i <= 0) { |
1684 | 15.9M | s->rwstate = SSL_READING; |
1685 | 15.9M | *len = 0; |
1686 | 15.9M | return 0; |
1687 | 15.9M | } |
1688 | 332k | s->init_num += readbytes; |
1689 | 332k | n -= readbytes; |
1690 | 332k | } |
1691 | | |
1692 | | /* |
1693 | | * If receiving Finished, record MAC of prior handshake messages for |
1694 | | * Finished verification. |
1695 | | */ |
1696 | 252k | if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) { |
1697 | | /* SSLfatal() already called */ |
1698 | 0 | *len = 0; |
1699 | 0 | return 0; |
1700 | 0 | } |
1701 | | |
1702 | | /* Feed this message into MAC computation. */ |
1703 | 252k | if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) { |
1704 | 5.98k | if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, |
1705 | 5.98k | s->init_num)) { |
1706 | | /* SSLfatal() already called */ |
1707 | 0 | *len = 0; |
1708 | 0 | return 0; |
1709 | 0 | } |
1710 | 5.98k | if (s->msg_callback) |
1711 | 0 | s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data, |
1712 | 0 | (size_t)s->init_num, ussl, s->msg_callback_arg); |
1713 | 246k | } else { |
1714 | | /* |
1715 | | * We defer feeding in the HRR until later. We'll do it as part of |
1716 | | * processing the message |
1717 | | * The TLsv1.3 handshake transcript stops at the ClientFinished |
1718 | | * message. |
1719 | | */ |
1720 | 246k | #define SERVER_HELLO_RANDOM_OFFSET (SSL3_HM_HEADER_LENGTH + 2) |
1721 | | /* KeyUpdate and NewSessionTicket do not need to be added */ |
1722 | 246k | if (!SSL_CONNECTION_IS_TLS13(s) |
1723 | 73.6k | || (s->s3.tmp.message_type != SSL3_MT_NEWSESSION_TICKET |
1724 | 244k | && s->s3.tmp.message_type != SSL3_MT_KEY_UPDATE)) { |
1725 | 244k | if (s->s3.tmp.message_type != SSL3_MT_SERVER_HELLO |
1726 | 62.5k | || s->init_num < SERVER_HELLO_RANDOM_OFFSET + SSL3_RANDOM_SIZE |
1727 | 62.0k | || memcmp(hrrrandom, |
1728 | 62.0k | s->init_buf->data + SERVER_HELLO_RANDOM_OFFSET, |
1729 | 62.0k | SSL3_RANDOM_SIZE) |
1730 | 243k | != 0) { |
1731 | 243k | if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, |
1732 | 243k | s->init_num + SSL3_HM_HEADER_LENGTH)) { |
1733 | | /* SSLfatal() already called */ |
1734 | 0 | *len = 0; |
1735 | 0 | return 0; |
1736 | 0 | } |
1737 | 243k | } |
1738 | 244k | } |
1739 | 246k | if (s->msg_callback) |
1740 | 0 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data, |
1741 | 0 | (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, ussl, |
1742 | 0 | s->msg_callback_arg); |
1743 | 246k | } |
1744 | | |
1745 | 252k | *len = s->init_num; |
1746 | 252k | return 1; |
1747 | 252k | } |
1748 | | |
1749 | | static const X509ERR2ALERT x509table[] = { |
1750 | | { X509_V_ERR_APPLICATION_VERIFICATION, SSL_AD_HANDSHAKE_FAILURE }, |
1751 | | { X509_V_ERR_CA_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE }, |
1752 | | { X509_V_ERR_EC_KEY_EXPLICIT_PARAMS, SSL_AD_BAD_CERTIFICATE }, |
1753 | | { X509_V_ERR_CA_MD_TOO_WEAK, SSL_AD_BAD_CERTIFICATE }, |
1754 | | { X509_V_ERR_CERT_CHAIN_TOO_LONG, SSL_AD_UNKNOWN_CA }, |
1755 | | { X509_V_ERR_CERT_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED }, |
1756 | | { X509_V_ERR_CERT_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE }, |
1757 | | { X509_V_ERR_CERT_REJECTED, SSL_AD_BAD_CERTIFICATE }, |
1758 | | { X509_V_ERR_CERT_REVOKED, SSL_AD_CERTIFICATE_REVOKED }, |
1759 | | { X509_V_ERR_CERT_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR }, |
1760 | | { X509_V_ERR_CERT_UNTRUSTED, SSL_AD_BAD_CERTIFICATE }, |
1761 | | { X509_V_ERR_CRL_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED }, |
1762 | | { X509_V_ERR_CRL_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE }, |
1763 | | { X509_V_ERR_CRL_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR }, |
1764 | | { X509_V_ERR_DANE_NO_MATCH, SSL_AD_BAD_CERTIFICATE }, |
1765 | | { X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT, SSL_AD_UNKNOWN_CA }, |
1766 | | { X509_V_ERR_EE_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE }, |
1767 | | { X509_V_ERR_EMAIL_MISMATCH, SSL_AD_BAD_CERTIFICATE }, |
1768 | | { X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD, SSL_AD_BAD_CERTIFICATE }, |
1769 | | { X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD, SSL_AD_BAD_CERTIFICATE }, |
1770 | | { X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE }, |
1771 | | { X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE }, |
1772 | | { X509_V_ERR_HOSTNAME_MISMATCH, SSL_AD_BAD_CERTIFICATE }, |
1773 | | { X509_V_ERR_INVALID_CA, SSL_AD_UNKNOWN_CA }, |
1774 | | { X509_V_ERR_INVALID_CALL, SSL_AD_INTERNAL_ERROR }, |
1775 | | { X509_V_ERR_INVALID_PURPOSE, SSL_AD_UNSUPPORTED_CERTIFICATE }, |
1776 | | { X509_V_ERR_IP_ADDRESS_MISMATCH, SSL_AD_BAD_CERTIFICATE }, |
1777 | | { X509_V_ERR_OUT_OF_MEM, SSL_AD_INTERNAL_ERROR }, |
1778 | | { X509_V_ERR_PATH_LENGTH_EXCEEDED, SSL_AD_UNKNOWN_CA }, |
1779 | | { X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN, SSL_AD_UNKNOWN_CA }, |
1780 | | { X509_V_ERR_STORE_LOOKUP, SSL_AD_INTERNAL_ERROR }, |
1781 | | { X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY, SSL_AD_BAD_CERTIFICATE }, |
1782 | | { X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE, SSL_AD_BAD_CERTIFICATE }, |
1783 | | { X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE, SSL_AD_BAD_CERTIFICATE }, |
1784 | | { X509_V_ERR_UNABLE_TO_GET_CRL, SSL_AD_UNKNOWN_CA }, |
1785 | | { X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER, SSL_AD_UNKNOWN_CA }, |
1786 | | { X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT, SSL_AD_UNKNOWN_CA }, |
1787 | | { X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, SSL_AD_UNKNOWN_CA }, |
1788 | | { X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE, SSL_AD_UNKNOWN_CA }, |
1789 | | { X509_V_ERR_UNSPECIFIED, SSL_AD_INTERNAL_ERROR }, |
1790 | | |
1791 | | /* Last entry; return this if we don't find the value above. */ |
1792 | | { X509_V_OK, SSL_AD_CERTIFICATE_UNKNOWN } |
1793 | | }; |
1794 | | |
1795 | | int ssl_x509err2alert(int x509err) |
1796 | 0 | { |
1797 | 0 | const X509ERR2ALERT *tp; |
1798 | |
|
1799 | 0 | for (tp = x509table; tp->x509err != X509_V_OK; ++tp) |
1800 | 0 | if (tp->x509err == x509err) |
1801 | 0 | break; |
1802 | 0 | return tp->alert; |
1803 | 0 | } |
1804 | | |
1805 | | int ssl_allow_compression(SSL_CONNECTION *s) |
1806 | 169k | { |
1807 | 169k | if (s->options & SSL_OP_NO_COMPRESSION) |
1808 | 169k | return 0; |
1809 | 0 | return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL); |
1810 | 169k | } |
1811 | | |
1812 | | /* |
1813 | | * SSL/TLS/DTLS version comparison |
1814 | | * |
1815 | | * Returns |
1816 | | * 0 if versiona is equal to versionb |
1817 | | * 1 if versiona is greater than versionb |
1818 | | * -1 if versiona is less than versionb |
1819 | | */ |
1820 | | int ssl_version_cmp(const SSL_CONNECTION *s, int versiona, int versionb) |
1821 | 41.4M | { |
1822 | 41.4M | int dtls = SSL_CONNECTION_IS_DTLS(s); |
1823 | | |
1824 | 41.4M | if (versiona == versionb) |
1825 | 8.22M | return 0; |
1826 | 33.2M | if (!dtls) |
1827 | 22.4M | return versiona < versionb ? -1 : 1; |
1828 | 10.7M | return DTLS_VERSION_LT(versiona, versionb) ? -1 : 1; |
1829 | 33.2M | } |
1830 | | |
1831 | | typedef struct { |
1832 | | int version; |
1833 | | const SSL_METHOD *(*cmeth)(void); |
1834 | | const SSL_METHOD *(*smeth)(void); |
1835 | | } version_info; |
1836 | | |
1837 | | #if TLS_MAX_VERSION_INTERNAL != TLS1_3_VERSION |
1838 | | #error Code needs update for TLS_method() support beyond TLS1_3_VERSION. |
1839 | | #endif |
1840 | | |
1841 | | /* Must be in order high to low */ |
1842 | | static const version_info tls_version_table[] = { |
1843 | | #ifndef OPENSSL_NO_TLS1_3 |
1844 | | { TLS1_3_VERSION, tlsv1_3_client_method, tlsv1_3_server_method }, |
1845 | | #else |
1846 | | { TLS1_3_VERSION, NULL, NULL }, |
1847 | | #endif |
1848 | | #ifndef OPENSSL_NO_TLS1_2 |
1849 | | { TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method }, |
1850 | | #else |
1851 | | { TLS1_2_VERSION, NULL, NULL }, |
1852 | | #endif |
1853 | | #ifndef OPENSSL_NO_TLS1_1 |
1854 | | { TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method }, |
1855 | | #else |
1856 | | { TLS1_1_VERSION, NULL, NULL }, |
1857 | | #endif |
1858 | | #ifndef OPENSSL_NO_TLS1 |
1859 | | { TLS1_VERSION, tlsv1_client_method, tlsv1_server_method }, |
1860 | | #else |
1861 | | { TLS1_VERSION, NULL, NULL }, |
1862 | | #endif |
1863 | | #ifndef OPENSSL_NO_SSL3 |
1864 | | { SSL3_VERSION, sslv3_client_method, sslv3_server_method }, |
1865 | | #else |
1866 | | { SSL3_VERSION, NULL, NULL }, |
1867 | | #endif |
1868 | | { 0, NULL, NULL }, |
1869 | | }; |
1870 | | |
1871 | | #if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION |
1872 | | #error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION. |
1873 | | #endif |
1874 | | |
1875 | | /* Must be in order high to low */ |
1876 | | static const version_info dtls_version_table[] = { |
1877 | | #ifndef OPENSSL_NO_DTLS1_2 |
1878 | | { DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method }, |
1879 | | #else |
1880 | | { DTLS1_2_VERSION, NULL, NULL }, |
1881 | | #endif |
1882 | | #ifndef OPENSSL_NO_DTLS1 |
1883 | | { DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method }, |
1884 | | { DTLS1_BAD_VER, dtls_bad_ver_client_method, NULL }, |
1885 | | #else |
1886 | | { DTLS1_VERSION, NULL, NULL }, |
1887 | | { DTLS1_BAD_VER, NULL, NULL }, |
1888 | | #endif |
1889 | | { 0, NULL, NULL }, |
1890 | | }; |
1891 | | |
1892 | | /* |
1893 | | * ssl_method_error - Check whether an SSL_METHOD is enabled. |
1894 | | * |
1895 | | * @s: The SSL handle for the candidate method |
1896 | | * @method: the intended method. |
1897 | | * |
1898 | | * Returns 0 on success, or an SSL error reason on failure. |
1899 | | */ |
1900 | | static int ssl_method_error(const SSL_CONNECTION *s, const SSL_METHOD *method) |
1901 | 4.38M | { |
1902 | 4.38M | int version = method->version; |
1903 | | |
1904 | 4.38M | if ((s->min_proto_version != 0 && ssl_version_cmp(s, version, s->min_proto_version) < 0) || ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0) |
1905 | 1.43M | return SSL_R_VERSION_TOO_LOW; |
1906 | | |
1907 | 2.95M | if (s->max_proto_version != 0 && ssl_version_cmp(s, version, s->max_proto_version) > 0) |
1908 | 0 | return SSL_R_VERSION_TOO_HIGH; |
1909 | | |
1910 | 2.95M | if ((s->options & method->mask) != 0) |
1911 | 0 | return SSL_R_UNSUPPORTED_PROTOCOL; |
1912 | 2.95M | if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s)) |
1913 | 0 | return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE; |
1914 | | |
1915 | 2.95M | return 0; |
1916 | 2.95M | } |
1917 | | |
1918 | | /* |
1919 | | * Only called by servers. Returns 1 if the server has a TLSv1.3 capable |
1920 | | * certificate type, or has PSK or a certificate callback configured, or has |
1921 | | * a servername callback configure. Otherwise returns 0. |
1922 | | */ |
1923 | | static int is_tls13_capable(const SSL_CONNECTION *s) |
1924 | 21.9k | { |
1925 | 21.9k | size_t i; |
1926 | 21.9k | int curve; |
1927 | 21.9k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1928 | | |
1929 | 21.9k | if (!ossl_assert(sctx != NULL) || !ossl_assert(s->session_ctx != NULL)) |
1930 | 0 | return 0; |
1931 | | |
1932 | | /* |
1933 | | * A servername callback can change the available certs, so if a servername |
1934 | | * cb is set then we just assume TLSv1.3 will be ok |
1935 | | */ |
1936 | 21.9k | if (sctx->ext.servername_cb != NULL |
1937 | 21.9k | || s->session_ctx->ext.servername_cb != NULL) |
1938 | 0 | return 1; |
1939 | | |
1940 | 21.9k | #ifndef OPENSSL_NO_PSK |
1941 | 21.9k | if (s->psk_server_callback != NULL) |
1942 | 0 | return 1; |
1943 | 21.9k | #endif |
1944 | | |
1945 | 21.9k | if (s->psk_find_session_cb != NULL || s->cert->cert_cb != NULL) |
1946 | 0 | return 1; |
1947 | | |
1948 | | /* All provider-based sig algs are required to support at least TLS1.3 */ |
1949 | 21.9k | for (i = 0; i < s->ssl_pkey_num; i++) { |
1950 | | /* Skip over certs disallowed for TLSv1.3 */ |
1951 | 21.9k | switch (i) { |
1952 | 0 | case SSL_PKEY_DSA_SIGN: |
1953 | 0 | case SSL_PKEY_GOST01: |
1954 | 0 | case SSL_PKEY_GOST12_256: |
1955 | 0 | case SSL_PKEY_GOST12_512: |
1956 | 0 | continue; |
1957 | 21.9k | default: |
1958 | 21.9k | break; |
1959 | 21.9k | } |
1960 | 21.9k | if (!ssl_has_cert(s, (int)i)) |
1961 | 0 | continue; |
1962 | 21.9k | if (i != SSL_PKEY_ECC) |
1963 | 21.9k | return 1; |
1964 | | /* |
1965 | | * Prior to TLSv1.3 sig algs allowed any curve to be used. TLSv1.3 is |
1966 | | * more restrictive so check that our sig algs are consistent with this |
1967 | | * EC cert. See section 4.2.3 of RFC8446. |
1968 | | */ |
1969 | 0 | curve = ssl_get_EC_curve_nid(s->cert->pkeys[SSL_PKEY_ECC].privatekey); |
1970 | 0 | if (tls_check_sigalg_curve(s, curve)) |
1971 | 0 | return 1; |
1972 | 0 | } |
1973 | | |
1974 | 0 | return 0; |
1975 | 21.9k | } |
1976 | | |
1977 | | /* |
1978 | | * ssl_version_supported - Check that the specified `version` is supported by |
1979 | | * `SSL *` instance |
1980 | | * |
1981 | | * @s: The SSL handle for the candidate method |
1982 | | * @version: Protocol version to test against |
1983 | | * |
1984 | | * Returns 1 when supported, otherwise 0 |
1985 | | */ |
1986 | | int ssl_version_supported(const SSL_CONNECTION *s, int version, |
1987 | | const SSL_METHOD **meth) |
1988 | 43.7k | { |
1989 | 43.7k | const version_info *vent; |
1990 | 43.7k | const version_info *table; |
1991 | | |
1992 | 43.7k | switch (SSL_CONNECTION_GET_SSL(s)->method->version) { |
1993 | 4.60k | default: |
1994 | | /* Version should match method version for non-ANY method */ |
1995 | 4.60k | return ssl_version_cmp(s, version, s->version) == 0; |
1996 | 35.5k | case TLS_ANY_VERSION: |
1997 | 35.5k | table = tls_version_table; |
1998 | 35.5k | break; |
1999 | 3.56k | case DTLS_ANY_VERSION: |
2000 | 3.56k | table = dtls_version_table; |
2001 | 3.56k | break; |
2002 | 43.7k | } |
2003 | | |
2004 | 39.1k | for (vent = table; |
2005 | 51.3k | vent->version != 0 && ssl_version_cmp(s, version, vent->version) <= 0; |
2006 | 40.5k | ++vent) { |
2007 | 40.5k | const SSL_METHOD *(*thismeth)(void) = s->server ? vent->smeth |
2008 | 40.5k | : vent->cmeth; |
2009 | | |
2010 | 40.5k | if (thismeth != NULL |
2011 | 39.6k | && ssl_version_cmp(s, version, vent->version) == 0 |
2012 | 28.2k | && ssl_method_error(s, thismeth()) == 0 |
2013 | 28.2k | && (!s->server |
2014 | 24.3k | || version != TLS1_3_VERSION |
2015 | 28.2k | || is_tls13_capable(s))) { |
2016 | 28.2k | if (meth != NULL) |
2017 | 7.68k | *meth = thismeth(); |
2018 | 28.2k | return 1; |
2019 | 28.2k | } |
2020 | 40.5k | } |
2021 | 10.8k | return 0; |
2022 | 39.1k | } |
2023 | | |
2024 | | /* |
2025 | | * ssl_check_version_downgrade - In response to RFC7507 SCSV version |
2026 | | * fallback indication from a client check whether we're using the highest |
2027 | | * supported protocol version. |
2028 | | * |
2029 | | * @s server SSL handle. |
2030 | | * |
2031 | | * Returns 1 when using the highest enabled version, 0 otherwise. |
2032 | | */ |
2033 | | int ssl_check_version_downgrade(SSL_CONNECTION *s) |
2034 | 860 | { |
2035 | 860 | const version_info *vent; |
2036 | 860 | const version_info *table; |
2037 | 860 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
2038 | | |
2039 | | /* |
2040 | | * Check that the current protocol is the highest enabled version |
2041 | | * (according to ssl->defltmethod, as version negotiation may have changed |
2042 | | * s->method). |
2043 | | */ |
2044 | 860 | if (s->version == ssl->defltmeth->version) |
2045 | 0 | return 1; |
2046 | | |
2047 | | /* |
2048 | | * Apparently we're using a version-flexible SSL_METHOD (not at its |
2049 | | * highest protocol version). |
2050 | | */ |
2051 | 860 | if (ssl->defltmeth->version == TLS_method()->version) |
2052 | 539 | table = tls_version_table; |
2053 | 321 | else if (ssl->defltmeth->version == DTLS_method()->version) |
2054 | 321 | table = dtls_version_table; |
2055 | 0 | else { |
2056 | | /* Unexpected state; fail closed. */ |
2057 | 0 | return 0; |
2058 | 0 | } |
2059 | | |
2060 | 860 | for (vent = table; vent->version != 0; ++vent) { |
2061 | 860 | if (vent->smeth != NULL && ssl_method_error(s, vent->smeth()) == 0) |
2062 | 860 | return s->version == vent->version; |
2063 | 860 | } |
2064 | 0 | return 0; |
2065 | 860 | } |
2066 | | |
2067 | | /* |
2068 | | * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS |
2069 | | * protocols, provided the initial (D)TLS method is version-flexible. This |
2070 | | * function sanity-checks the proposed value and makes sure the method is |
2071 | | * version-flexible, then sets the limit if all is well. |
2072 | | * |
2073 | | * @method_version: The version of the current SSL_METHOD. |
2074 | | * @version: the intended limit. |
2075 | | * @bound: pointer to limit to be updated. |
2076 | | * |
2077 | | * Returns 1 on success, 0 on failure. |
2078 | | */ |
2079 | | int ssl_set_version_bound(int method_version, int version, int *bound) |
2080 | 122k | { |
2081 | 122k | int valid_tls; |
2082 | 122k | int valid_dtls; |
2083 | | |
2084 | 122k | if (version == 0) { |
2085 | 81.8k | *bound = version; |
2086 | 81.8k | return 1; |
2087 | 81.8k | } |
2088 | | |
2089 | 40.4k | valid_tls = version >= SSL3_VERSION && version <= TLS_MAX_VERSION_INTERNAL; |
2090 | 40.4k | valid_dtls = |
2091 | | /* We support client side pre-standardisation version of DTLS */ |
2092 | 40.4k | (version == DTLS1_BAD_VER) |
2093 | 40.4k | || (DTLS_VERSION_LE(version, DTLS_MAX_VERSION_INTERNAL) |
2094 | 0 | && DTLS_VERSION_GE(version, DTLS1_VERSION)); |
2095 | | |
2096 | 40.4k | if (!valid_tls && !valid_dtls) |
2097 | 0 | return 0; |
2098 | | |
2099 | | /*- |
2100 | | * Restrict TLS methods to TLS protocol versions. |
2101 | | * Restrict DTLS methods to DTLS protocol versions. |
2102 | | * Note, DTLS version numbers are decreasing, use comparison macros. |
2103 | | * |
2104 | | * Note that for both lower-bounds we use explicit versions, not |
2105 | | * (D)TLS_MIN_VERSION. This is because we don't want to break user |
2106 | | * configurations. If the MIN (supported) version ever rises, the user's |
2107 | | * "floor" remains valid even if no longer available. We don't expect the |
2108 | | * MAX ceiling to ever get lower, so making that variable makes sense. |
2109 | | * |
2110 | | * We ignore attempts to set bounds on version-inflexible methods, |
2111 | | * returning success. |
2112 | | */ |
2113 | 40.4k | switch (method_version) { |
2114 | 0 | default: |
2115 | 0 | break; |
2116 | | |
2117 | 40.4k | case TLS_ANY_VERSION: |
2118 | 40.4k | if (valid_tls) |
2119 | 40.4k | *bound = version; |
2120 | 40.4k | break; |
2121 | | |
2122 | 0 | case DTLS_ANY_VERSION: |
2123 | 0 | if (valid_dtls) |
2124 | 0 | *bound = version; |
2125 | 0 | break; |
2126 | 40.4k | } |
2127 | 40.4k | return 1; |
2128 | 40.4k | } |
2129 | | |
2130 | | static void check_for_downgrade(SSL_CONNECTION *s, int vers, DOWNGRADE *dgrd) |
2131 | 41.2k | { |
2132 | 41.2k | if (vers == TLS1_2_VERSION |
2133 | 16.5k | && ssl_version_supported(s, TLS1_3_VERSION, NULL)) { |
2134 | 16.5k | *dgrd = DOWNGRADE_TO_1_2; |
2135 | 24.7k | } else if (!SSL_CONNECTION_IS_DTLS(s) |
2136 | 8.68k | && vers < TLS1_2_VERSION |
2137 | | /* |
2138 | | * We need to ensure that a server that disables TLSv1.2 |
2139 | | * (creating a hole between TLSv1.3 and TLSv1.1) can still |
2140 | | * complete handshakes with clients that support TLSv1.2 and |
2141 | | * below. Therefore we do not enable the sentinel if TLSv1.3 is |
2142 | | * enabled and TLSv1.2 is not. |
2143 | | */ |
2144 | 3.26k | && ssl_version_supported(s, TLS1_2_VERSION, NULL)) { |
2145 | 3.26k | *dgrd = DOWNGRADE_TO_1_1; |
2146 | 21.4k | } else { |
2147 | 21.4k | *dgrd = DOWNGRADE_NONE; |
2148 | 21.4k | } |
2149 | 41.2k | } |
2150 | | |
2151 | | /* |
2152 | | * ssl_choose_server_version - Choose server (D)TLS version. Called when the |
2153 | | * client HELLO is received to select the final server protocol version and |
2154 | | * the version specific method. |
2155 | | * |
2156 | | * @s: server SSL handle. |
2157 | | * |
2158 | | * Returns 0 on success or an SSL error reason number on failure. |
2159 | | */ |
2160 | | int ssl_choose_server_version(SSL_CONNECTION *s, CLIENTHELLO_MSG *hello, |
2161 | | DOWNGRADE *dgrd) |
2162 | 38.0k | { |
2163 | | /*- |
2164 | | * With version-flexible methods we have an initial state with: |
2165 | | * |
2166 | | * s->method->version == (D)TLS_ANY_VERSION, |
2167 | | * s->version == (D)TLS_MAX_VERSION_INTERNAL. |
2168 | | * |
2169 | | * So we detect version-flexible methods via the method version, not the |
2170 | | * handle version. |
2171 | | */ |
2172 | 38.0k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
2173 | 38.0k | int server_version = ssl->method->version; |
2174 | 38.0k | int client_version = hello->legacy_version; |
2175 | 38.0k | const version_info *vent; |
2176 | 38.0k | const version_info *table; |
2177 | 38.0k | int disabled = 0; |
2178 | 38.0k | RAW_EXTENSION *suppversions; |
2179 | | |
2180 | 38.0k | s->client_version = client_version; |
2181 | | |
2182 | 38.0k | switch (server_version) { |
2183 | 333 | default: |
2184 | 333 | if (!SSL_CONNECTION_IS_TLS13(s)) { |
2185 | 0 | if (ssl_version_cmp(s, client_version, s->version) < 0) |
2186 | 0 | return SSL_R_WRONG_SSL_VERSION; |
2187 | 0 | *dgrd = DOWNGRADE_NONE; |
2188 | | /* |
2189 | | * If this SSL handle is not from a version flexible method we don't |
2190 | | * (and never did) check min/max FIPS or Suite B constraints. Hope |
2191 | | * that's OK. It is up to the caller to not choose fixed protocol |
2192 | | * versions they don't want. If not, then easy to fix, just return |
2193 | | * ssl_method_error(s, s->method) |
2194 | | */ |
2195 | 0 | return 0; |
2196 | 0 | } |
2197 | | /* |
2198 | | * Fall through if we are TLSv1.3 already (this means we must be after |
2199 | | * a HelloRetryRequest |
2200 | | */ |
2201 | | /* fall thru */ |
2202 | 21.9k | case TLS_ANY_VERSION: |
2203 | 21.9k | table = tls_version_table; |
2204 | 21.9k | break; |
2205 | 16.1k | case DTLS_ANY_VERSION: |
2206 | 16.1k | table = dtls_version_table; |
2207 | 16.1k | break; |
2208 | 38.0k | } |
2209 | | |
2210 | 38.0k | suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions]; |
2211 | | |
2212 | | /* If we did an HRR then supported versions is mandatory */ |
2213 | 38.0k | if (!suppversions->present && s->hello_retry_request != SSL_HRR_NONE) |
2214 | 8 | return SSL_R_UNSUPPORTED_PROTOCOL; |
2215 | | |
2216 | 38.0k | if (suppversions->present && !SSL_CONNECTION_IS_DTLS(s)) { |
2217 | 5.26k | unsigned int candidate_vers = 0; |
2218 | 5.26k | unsigned int best_vers = 0; |
2219 | 5.26k | const SSL_METHOD *best_method = NULL; |
2220 | 5.26k | PACKET versionslist; |
2221 | | |
2222 | 5.26k | suppversions->parsed = 1; |
2223 | | |
2224 | 5.26k | if (!PACKET_as_length_prefixed_1(&suppversions->data, &versionslist)) { |
2225 | | /* Trailing or invalid data? */ |
2226 | 51 | return SSL_R_LENGTH_MISMATCH; |
2227 | 51 | } |
2228 | | |
2229 | | /* |
2230 | | * The TLSv1.3 spec says the client MUST set this to TLS1_2_VERSION. |
2231 | | * The spec only requires servers to check that it isn't SSLv3: |
2232 | | * "Any endpoint receiving a Hello message with |
2233 | | * ClientHello.legacy_version or ServerHello.legacy_version set to |
2234 | | * 0x0300 MUST abort the handshake with a "protocol_version" alert." |
2235 | | * We are slightly stricter and require that it isn't SSLv3 or lower. |
2236 | | * We tolerate TLSv1 and TLSv1.1. |
2237 | | */ |
2238 | 5.21k | if (client_version <= SSL3_VERSION) |
2239 | 33 | return SSL_R_BAD_LEGACY_VERSION; |
2240 | | |
2241 | 35.0k | while (PACKET_get_net_2(&versionslist, &candidate_vers)) { |
2242 | 29.9k | if (ssl_version_cmp(s, candidate_vers, best_vers) <= 0) |
2243 | 8.76k | continue; |
2244 | 21.1k | if (ssl_version_supported(s, candidate_vers, &best_method)) |
2245 | 7.94k | best_vers = candidate_vers; |
2246 | 21.1k | } |
2247 | 5.17k | if (PACKET_remaining(&versionslist) != 0) { |
2248 | | /* Trailing data? */ |
2249 | 198 | return SSL_R_LENGTH_MISMATCH; |
2250 | 198 | } |
2251 | | |
2252 | 4.98k | if (best_vers > 0) { |
2253 | 4.88k | if (s->hello_retry_request != SSL_HRR_NONE) { |
2254 | | /* |
2255 | | * This is after a HelloRetryRequest so we better check that we |
2256 | | * negotiated TLSv1.3 |
2257 | | */ |
2258 | 254 | if (best_vers != TLS1_3_VERSION) |
2259 | 0 | return SSL_R_UNSUPPORTED_PROTOCOL; |
2260 | 254 | return 0; |
2261 | 254 | } |
2262 | 4.62k | check_for_downgrade(s, best_vers, dgrd); |
2263 | 4.62k | s->version = best_vers; |
2264 | 4.62k | ssl->method = best_method; |
2265 | 4.62k | if (!ssl_set_record_protocol_version(s, best_vers)) |
2266 | 0 | return ERR_R_INTERNAL_ERROR; |
2267 | | |
2268 | 4.62k | return 0; |
2269 | 4.62k | } |
2270 | 99 | return SSL_R_UNSUPPORTED_PROTOCOL; |
2271 | 4.98k | } |
2272 | | |
2273 | | /* |
2274 | | * If the supported versions extension isn't present, then the highest |
2275 | | * version we can negotiate is TLSv1.2 |
2276 | | */ |
2277 | 32.7k | if (ssl_version_cmp(s, client_version, TLS1_3_VERSION) >= 0) |
2278 | 13.6k | client_version = TLS1_2_VERSION; |
2279 | | |
2280 | | /* |
2281 | | * No supported versions extension, so we just use the version supplied in |
2282 | | * the ClientHello. |
2283 | | */ |
2284 | 58.6k | for (vent = table; vent->version != 0; ++vent) { |
2285 | 58.5k | const SSL_METHOD *method; |
2286 | | |
2287 | 58.5k | if (vent->smeth == NULL || ssl_version_cmp(s, client_version, vent->version) < 0) |
2288 | 25.8k | continue; |
2289 | 32.6k | method = vent->smeth(); |
2290 | 32.6k | if (ssl_method_error(s, method) == 0) { |
2291 | 32.6k | check_for_downgrade(s, vent->version, dgrd); |
2292 | 32.6k | s->version = vent->version; |
2293 | 32.6k | ssl->method = method; |
2294 | 32.6k | if (!ssl_set_record_protocol_version(s, s->version)) |
2295 | 0 | return ERR_R_INTERNAL_ERROR; |
2296 | | |
2297 | 32.6k | return 0; |
2298 | 32.6k | } |
2299 | 0 | disabled = 1; |
2300 | 0 | } |
2301 | 154 | return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW; |
2302 | 32.7k | } |
2303 | | |
2304 | | /* |
2305 | | * ssl_choose_client_version - Choose client (D)TLS version. Called when the |
2306 | | * server HELLO is received to select the final client protocol version and |
2307 | | * the version specific method. |
2308 | | * |
2309 | | * @s: client SSL handle. |
2310 | | * @version: The proposed version from the server's HELLO. |
2311 | | * @extensions: The extensions received |
2312 | | * |
2313 | | * Returns 1 on success or 0 on error. |
2314 | | */ |
2315 | | int ssl_choose_client_version(SSL_CONNECTION *s, int version, |
2316 | | RAW_EXTENSION *extensions) |
2317 | 68.5k | { |
2318 | 68.5k | const version_info *vent; |
2319 | 68.5k | const version_info *table; |
2320 | 68.5k | int ret, ver_min, ver_max, real_max, origv; |
2321 | 68.5k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
2322 | | |
2323 | 68.5k | origv = s->version; |
2324 | 68.5k | s->version = version; |
2325 | | |
2326 | | /* This will overwrite s->version if the extension is present */ |
2327 | 68.5k | if (!tls_parse_extension(s, TLSEXT_IDX_supported_versions, |
2328 | 68.5k | SSL_EXT_TLS1_2_SERVER_HELLO |
2329 | 68.5k | | SSL_EXT_TLS1_3_SERVER_HELLO, |
2330 | 68.5k | extensions, |
2331 | 68.5k | NULL, 0)) { |
2332 | 256 | s->version = origv; |
2333 | 256 | return 0; |
2334 | 256 | } |
2335 | | |
2336 | 68.3k | if (s->hello_retry_request != SSL_HRR_NONE |
2337 | 118 | && s->version != TLS1_3_VERSION) { |
2338 | 39 | s->version = origv; |
2339 | 39 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_WRONG_SSL_VERSION); |
2340 | 39 | return 0; |
2341 | 39 | } |
2342 | | |
2343 | 68.2k | switch (ssl->method->version) { |
2344 | 293 | default: |
2345 | 293 | if (s->version != ssl->method->version) { |
2346 | 122 | s->version = origv; |
2347 | 122 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_WRONG_SSL_VERSION); |
2348 | 122 | return 0; |
2349 | 122 | } |
2350 | | /* |
2351 | | * If this SSL handle is not from a version flexible method we don't |
2352 | | * (and never did) check min/max, FIPS or Suite B constraints. Hope |
2353 | | * that's OK. It is up to the caller to not choose fixed protocol |
2354 | | * versions they don't want. If not, then easy to fix, just return |
2355 | | * ssl_method_error(s, s->method) |
2356 | | */ |
2357 | 171 | if (!ssl_set_record_protocol_version(s, s->version)) { |
2358 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2359 | 0 | return 0; |
2360 | 0 | } |
2361 | 171 | return 1; |
2362 | 53.5k | case TLS_ANY_VERSION: |
2363 | 53.5k | table = tls_version_table; |
2364 | 53.5k | break; |
2365 | 14.4k | case DTLS_ANY_VERSION: |
2366 | 14.4k | table = dtls_version_table; |
2367 | 14.4k | break; |
2368 | 68.2k | } |
2369 | | |
2370 | 67.9k | ret = ssl_get_min_max_version(s, &ver_min, &ver_max, &real_max); |
2371 | 67.9k | if (ret != 0) { |
2372 | 0 | s->version = origv; |
2373 | 0 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, ret); |
2374 | 0 | return 0; |
2375 | 0 | } |
2376 | 67.9k | if (ssl_version_cmp(s, s->version, ver_min) < 0 |
2377 | 67.8k | || ssl_version_cmp(s, s->version, ver_max) > 0) { |
2378 | 533 | s->version = origv; |
2379 | 533 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNSUPPORTED_PROTOCOL); |
2380 | 533 | return 0; |
2381 | 533 | } |
2382 | | |
2383 | 67.4k | if ((s->mode & SSL_MODE_SEND_FALLBACK_SCSV) == 0) |
2384 | 67.4k | real_max = ver_max; |
2385 | | |
2386 | | /* Check for downgrades */ |
2387 | | /* TODO(DTLSv1.3): Update this code for DTLSv1.3 */ |
2388 | 67.4k | if (!SSL_CONNECTION_IS_DTLS(s) && real_max > s->version) { |
2389 | | /* Signal applies to all versions */ |
2390 | 27.3k | if (memcmp(tls11downgrade, |
2391 | 27.3k | s->s3.server_random + SSL3_RANDOM_SIZE |
2392 | 27.3k | - sizeof(tls11downgrade), |
2393 | 27.3k | sizeof(tls11downgrade)) |
2394 | 27.3k | == 0) { |
2395 | 6 | s->version = origv; |
2396 | 6 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
2397 | 6 | SSL_R_INAPPROPRIATE_FALLBACK); |
2398 | 6 | return 0; |
2399 | 6 | } |
2400 | | /* Only when accepting TLS1.3 */ |
2401 | 27.3k | if (real_max == TLS1_3_VERSION |
2402 | 27.3k | && memcmp(tls12downgrade, |
2403 | 27.3k | s->s3.server_random + SSL3_RANDOM_SIZE |
2404 | 27.3k | - sizeof(tls12downgrade), |
2405 | 27.3k | sizeof(tls12downgrade)) |
2406 | 27.3k | == 0) { |
2407 | 5 | s->version = origv; |
2408 | 5 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
2409 | 5 | SSL_R_INAPPROPRIATE_FALLBACK); |
2410 | 5 | return 0; |
2411 | 5 | } |
2412 | 27.3k | } |
2413 | | |
2414 | 117k | for (vent = table; vent->version != 0; ++vent) { |
2415 | 117k | if (vent->cmeth == NULL || s->version != vent->version) |
2416 | 49.7k | continue; |
2417 | | |
2418 | 67.4k | ssl->method = vent->cmeth(); |
2419 | 67.4k | if (!ssl_set_record_protocol_version(s, s->version)) { |
2420 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2421 | 0 | return 0; |
2422 | 0 | } |
2423 | 67.4k | return 1; |
2424 | 67.4k | } |
2425 | | |
2426 | 9 | s->version = origv; |
2427 | 9 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNSUPPORTED_PROTOCOL); |
2428 | 9 | return 0; |
2429 | 67.4k | } |
2430 | | |
2431 | | /* |
2432 | | * ssl_get_min_max_version - get minimum and maximum protocol version |
2433 | | * @s: The SSL connection |
2434 | | * @min_version: The minimum supported version |
2435 | | * @max_version: The maximum supported version |
2436 | | * @real_max: The highest version below the lowest compile time version hole |
2437 | | * where that hole lies above at least one run-time enabled |
2438 | | * protocol. |
2439 | | * |
2440 | | * Work out what version we should be using for the initial ClientHello if the |
2441 | | * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx |
2442 | | * options, the MinProtocol and MaxProtocol configuration commands, any Suite B |
2443 | | * constraints and any floor imposed by the security level here, |
2444 | | * so we don't advertise the wrong protocol version to only reject the outcome later. |
2445 | | * |
2446 | | * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled, |
2447 | | * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol |
2448 | | * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1. |
2449 | | * |
2450 | | * Returns 0 on success or an SSL error reason number on failure. On failure |
2451 | | * min_version and max_version will also be set to 0. |
2452 | | */ |
2453 | | int ssl_get_min_max_version(const SSL_CONNECTION *s, int *min_version, |
2454 | | int *max_version, int *real_max) |
2455 | 1.18M | { |
2456 | 1.18M | int version, tmp_real_max; |
2457 | 1.18M | int hole; |
2458 | 1.18M | const SSL_METHOD *method; |
2459 | 1.18M | const version_info *table; |
2460 | 1.18M | const version_info *vent; |
2461 | 1.18M | const SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
2462 | | |
2463 | 1.18M | switch (ssl->method->version) { |
2464 | 43.8k | default: |
2465 | | /* |
2466 | | * If this SSL handle is not from a version flexible method we don't |
2467 | | * (and never did) check min/max FIPS or Suite B constraints. Hope |
2468 | | * that's OK. It is up to the caller to not choose fixed protocol |
2469 | | * versions they don't want. If not, then easy to fix, just return |
2470 | | * ssl_method_error(s, s->method) |
2471 | | */ |
2472 | 43.8k | *min_version = *max_version = s->version; |
2473 | | /* |
2474 | | * Providing a real_max only makes sense where we're using a version |
2475 | | * flexible method. |
2476 | | */ |
2477 | 43.8k | if (!ossl_assert(real_max == NULL)) |
2478 | 0 | return ERR_R_INTERNAL_ERROR; |
2479 | 43.8k | return 0; |
2480 | 902k | case TLS_ANY_VERSION: |
2481 | 902k | table = tls_version_table; |
2482 | 902k | break; |
2483 | 236k | case DTLS_ANY_VERSION: |
2484 | 236k | table = dtls_version_table; |
2485 | 236k | break; |
2486 | 1.18M | } |
2487 | | |
2488 | | /* |
2489 | | * SSL_OP_NO_X disables all protocols above X *if* there are some protocols |
2490 | | * below X enabled. This is required in order to maintain the "version |
2491 | | * capability" vector contiguous. Any versions with a NULL client method |
2492 | | * (protocol version client is disabled at compile-time) is also a "hole". |
2493 | | * |
2494 | | * Our initial state is hole == 1, version == 0. That is, versions above |
2495 | | * the first version in the method table are disabled (a "hole" above |
2496 | | * the valid protocol entries) and we don't have a selected version yet. |
2497 | | * |
2498 | | * Whenever "hole == 1", and we hit an enabled method, its version becomes |
2499 | | * the selected version. We're no longer in a hole, so "hole" becomes 0. |
2500 | | * |
2501 | | * If "hole == 0" and we hit an enabled method, we support a contiguous |
2502 | | * range of at least two methods. If we hit a disabled method, |
2503 | | * then hole becomes true again, but nothing else changes yet, |
2504 | | * because all the remaining methods may be disabled too. |
2505 | | * If we again hit an enabled method after the new hole, it becomes |
2506 | | * selected, as we start from scratch. |
2507 | | */ |
2508 | 1.13M | *min_version = version = 0; |
2509 | 1.13M | hole = 1; |
2510 | 1.13M | if (real_max != NULL) |
2511 | 73.7k | *real_max = 0; |
2512 | 1.13M | tmp_real_max = 0; |
2513 | 6.19M | for (vent = table; vent->version != 0; ++vent) { |
2514 | | /* |
2515 | | * A table entry with a NULL client method is still a hole in the |
2516 | | * "version capability" vector. |
2517 | | */ |
2518 | 5.05M | if (vent->cmeth == NULL) { |
2519 | 738k | hole = 1; |
2520 | 738k | tmp_real_max = 0; |
2521 | 738k | continue; |
2522 | 738k | } |
2523 | 4.31M | method = vent->cmeth(); |
2524 | | |
2525 | 4.31M | if (hole == 1 && tmp_real_max == 0) |
2526 | 1.13M | tmp_real_max = vent->version; |
2527 | | |
2528 | 4.31M | if (ssl_method_error(s, method) != 0) { |
2529 | 1.43M | hole = 1; |
2530 | 2.88M | } else if (!hole) { |
2531 | 1.74M | *min_version = method->version; |
2532 | 1.74M | } else { |
2533 | 1.13M | if (real_max != NULL && tmp_real_max != 0) |
2534 | 73.7k | *real_max = tmp_real_max; |
2535 | 1.13M | version = method->version; |
2536 | 1.13M | *min_version = version; |
2537 | 1.13M | hole = 0; |
2538 | 1.13M | } |
2539 | 4.31M | } |
2540 | | |
2541 | 1.13M | *max_version = version; |
2542 | | |
2543 | | /* Fail if everything is disabled */ |
2544 | 1.13M | if (version == 0) |
2545 | 0 | return SSL_R_NO_PROTOCOLS_AVAILABLE; |
2546 | | |
2547 | 1.13M | return 0; |
2548 | 1.13M | } |
2549 | | |
2550 | | /* |
2551 | | * ssl_set_client_hello_version - Work out what version we should be using for |
2552 | | * the initial ClientHello.legacy_version field. |
2553 | | * |
2554 | | * @s: client SSL handle. |
2555 | | * |
2556 | | * Returns 0 on success or an SSL error reason number on failure. |
2557 | | */ |
2558 | | int ssl_set_client_hello_version(SSL_CONNECTION *s) |
2559 | 111k | { |
2560 | 111k | int ver_min, ver_max, ret; |
2561 | | |
2562 | | /* |
2563 | | * In a renegotiation we always send the same client_version that we sent |
2564 | | * last time, regardless of which version we eventually negotiated. |
2565 | | */ |
2566 | 111k | if (!SSL_IS_FIRST_HANDSHAKE(s)) |
2567 | 1.23k | return 0; |
2568 | | |
2569 | 109k | ret = ssl_get_min_max_version(s, &ver_min, &ver_max, NULL); |
2570 | | |
2571 | 109k | if (ret != 0) |
2572 | 0 | return ret; |
2573 | | |
2574 | 109k | s->version = ver_max; |
2575 | | |
2576 | 109k | if (SSL_CONNECTION_IS_DTLS(s)) { |
2577 | 26.2k | if (ver_max == DTLS1_BAD_VER) { |
2578 | | /* |
2579 | | * Even though this is technically before version negotiation, |
2580 | | * because we have asked for DTLS1_BAD_VER we will never negotiate |
2581 | | * anything else, and this has impacts on the record layer for when |
2582 | | * we read the ServerHello. So we need to tell the record layer |
2583 | | * about this immediately. |
2584 | | */ |
2585 | 231 | if (!ssl_set_record_protocol_version(s, ver_max)) |
2586 | 0 | return 0; |
2587 | 231 | } |
2588 | 83.6k | } else if (ver_max > TLS1_2_VERSION) { |
2589 | | /* TLS1.3 always uses TLS1.2 in the legacy_version field */ |
2590 | 83.6k | ver_max = TLS1_2_VERSION; |
2591 | 83.6k | } |
2592 | | |
2593 | 109k | s->client_version = ver_max; |
2594 | 109k | return 0; |
2595 | 109k | } |
2596 | | |
2597 | | /* |
2598 | | * Checks a list of |groups| to determine if the |group_id| is in it. If it is |
2599 | | * and |checkallow| is 1 then additionally check if the group is allowed to be |
2600 | | * used. Returns 1 if the group is in the list (and allowed if |checkallow| is |
2601 | | * 1) or 0 otherwise. If provided a pointer it will also return the position |
2602 | | * where the group was found. |
2603 | | */ |
2604 | | int check_in_list(SSL_CONNECTION *s, uint16_t group_id, const uint16_t *groups, |
2605 | | size_t num_groups, int checkallow, size_t *pos) |
2606 | 15.6k | { |
2607 | 15.6k | size_t i; |
2608 | | |
2609 | 15.6k | if (groups == NULL || num_groups == 0) |
2610 | 1.51k | return 0; |
2611 | | |
2612 | 37.2k | for (i = 0; i < num_groups; i++) { |
2613 | 29.2k | uint16_t group = groups[i]; |
2614 | | |
2615 | 29.2k | if (group_id == group |
2616 | 6.12k | && (!checkallow |
2617 | 6.12k | || tls_group_allowed(s, group, SSL_SECOP_CURVE_CHECK))) { |
2618 | 6.12k | if (pos != NULL) |
2619 | 4.45k | *pos = i; |
2620 | 6.12k | return 1; |
2621 | 6.12k | } |
2622 | 29.2k | } |
2623 | | |
2624 | 8.03k | return 0; |
2625 | 14.1k | } |
2626 | | |
2627 | | /* Replace ClientHello1 in the transcript hash with a synthetic message */ |
2628 | | int create_synthetic_message_hash(SSL_CONNECTION *s, |
2629 | | const unsigned char *hashval, |
2630 | | size_t hashlen, const unsigned char *hrr, |
2631 | | size_t hrrlen) |
2632 | 1.15k | { |
2633 | 1.15k | unsigned char hashvaltmp[EVP_MAX_MD_SIZE]; |
2634 | 1.15k | unsigned char msghdr[SSL3_HM_HEADER_LENGTH]; |
2635 | | |
2636 | 1.15k | memset(msghdr, 0, sizeof(msghdr)); |
2637 | | |
2638 | 1.15k | if (hashval == NULL) { |
2639 | 1.15k | hashval = hashvaltmp; |
2640 | 1.15k | hashlen = 0; |
2641 | | /* Get the hash of the initial ClientHello */ |
2642 | 1.15k | if (!ssl3_digest_cached_records(s, 0) |
2643 | 1.15k | || !ssl_handshake_hash(s, hashvaltmp, sizeof(hashvaltmp), |
2644 | 1.15k | &hashlen)) { |
2645 | | /* SSLfatal() already called */ |
2646 | 0 | return 0; |
2647 | 0 | } |
2648 | 1.15k | } |
2649 | | |
2650 | | /* Reinitialise the transcript hash */ |
2651 | 1.15k | if (!ssl3_init_finished_mac(s)) { |
2652 | | /* SSLfatal() already called */ |
2653 | 0 | return 0; |
2654 | 0 | } |
2655 | | |
2656 | | /* Inject the synthetic message_hash message */ |
2657 | 1.15k | msghdr[0] = SSL3_MT_MESSAGE_HASH; |
2658 | 1.15k | msghdr[SSL3_HM_HEADER_LENGTH - 1] = (unsigned char)hashlen; |
2659 | 1.15k | if (!ssl3_finish_mac(s, msghdr, SSL3_HM_HEADER_LENGTH) |
2660 | 1.15k | || !ssl3_finish_mac(s, hashval, hashlen)) { |
2661 | | /* SSLfatal() already called */ |
2662 | 0 | return 0; |
2663 | 0 | } |
2664 | | |
2665 | | /* |
2666 | | * Now re-inject the HRR and current message if appropriate (we just deleted |
2667 | | * it when we reinitialised the transcript hash above). Only necessary after |
2668 | | * receiving a ClientHello2 with a cookie. |
2669 | | */ |
2670 | 1.15k | if (hrr != NULL |
2671 | 0 | && (!ssl3_finish_mac(s, hrr, hrrlen) |
2672 | 0 | || !ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, |
2673 | 0 | s->s3.tmp.message_size |
2674 | 0 | + SSL3_HM_HEADER_LENGTH))) { |
2675 | | /* SSLfatal() already called */ |
2676 | 0 | return 0; |
2677 | 0 | } |
2678 | | |
2679 | 1.15k | return 1; |
2680 | 1.15k | } |
2681 | | |
2682 | | static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b) |
2683 | 25 | { |
2684 | 25 | return X509_NAME_cmp(*a, *b); |
2685 | 25 | } |
2686 | | |
2687 | | int parse_ca_names(SSL_CONNECTION *s, PACKET *pkt) |
2688 | 911 | { |
2689 | 911 | STACK_OF(X509_NAME) *ca_sk = sk_X509_NAME_new(ca_dn_cmp); |
2690 | 911 | X509_NAME *xn = NULL; |
2691 | 911 | PACKET cadns; |
2692 | | |
2693 | 911 | if (ca_sk == NULL) { |
2694 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
2695 | 0 | goto err; |
2696 | 0 | } |
2697 | | /* get the CA RDNs */ |
2698 | 911 | if (!PACKET_get_length_prefixed_2(pkt, &cadns)) { |
2699 | 478 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
2700 | 478 | goto err; |
2701 | 478 | } |
2702 | | |
2703 | 753 | while (PACKET_remaining(&cadns)) { |
2704 | 631 | const unsigned char *namestart, *namebytes; |
2705 | 631 | unsigned int name_len; |
2706 | | |
2707 | 631 | if (!PACKET_get_net_2(&cadns, &name_len) |
2708 | 602 | || !PACKET_get_bytes(&cadns, &namebytes, name_len)) { |
2709 | 174 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
2710 | 174 | goto err; |
2711 | 174 | } |
2712 | | |
2713 | 457 | namestart = namebytes; |
2714 | 457 | if ((xn = d2i_X509_NAME(NULL, &namebytes, name_len)) == NULL) { |
2715 | 117 | SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB); |
2716 | 117 | goto err; |
2717 | 117 | } |
2718 | 340 | if (namebytes != (namestart + name_len)) { |
2719 | 20 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CA_DN_LENGTH_MISMATCH); |
2720 | 20 | goto err; |
2721 | 20 | } |
2722 | | |
2723 | 320 | if (!sk_X509_NAME_push(ca_sk, xn)) { |
2724 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
2725 | 0 | goto err; |
2726 | 0 | } |
2727 | 320 | xn = NULL; |
2728 | 320 | } |
2729 | | |
2730 | 122 | sk_X509_NAME_pop_free(s->s3.tmp.peer_ca_names, X509_NAME_free); |
2731 | 122 | s->s3.tmp.peer_ca_names = ca_sk; |
2732 | | |
2733 | 122 | return 1; |
2734 | | |
2735 | 789 | err: |
2736 | 789 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
2737 | 789 | X509_NAME_free(xn); |
2738 | 789 | return 0; |
2739 | 433 | } |
2740 | | |
2741 | | const STACK_OF(X509_NAME) *get_ca_names(SSL_CONNECTION *s) |
2742 | 90.4k | { |
2743 | 90.4k | const STACK_OF(X509_NAME) *ca_sk = NULL; |
2744 | 90.4k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
2745 | | |
2746 | 90.4k | if (s->server) { |
2747 | 0 | ca_sk = SSL_get_client_CA_list(ssl); |
2748 | 0 | if (ca_sk != NULL && sk_X509_NAME_num(ca_sk) == 0) |
2749 | 0 | ca_sk = NULL; |
2750 | 0 | } |
2751 | | |
2752 | 90.4k | if (ca_sk == NULL) |
2753 | 90.4k | ca_sk = SSL_get0_CA_list(ssl); |
2754 | | |
2755 | 90.4k | return ca_sk; |
2756 | 90.4k | } |
2757 | | |
2758 | | int construct_ca_names(SSL_CONNECTION *s, const STACK_OF(X509_NAME) *ca_sk, |
2759 | | WPACKET *pkt) |
2760 | 0 | { |
2761 | | /* Start sub-packet for client CA list */ |
2762 | 0 | if (!WPACKET_start_sub_packet_u16(pkt)) { |
2763 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2764 | 0 | return 0; |
2765 | 0 | } |
2766 | | |
2767 | 0 | if ((ca_sk != NULL) && !(s->options & SSL_OP_DISABLE_TLSEXT_CA_NAMES)) { |
2768 | 0 | int i; |
2769 | |
|
2770 | 0 | for (i = 0; i < sk_X509_NAME_num(ca_sk); i++) { |
2771 | 0 | unsigned char *namebytes; |
2772 | 0 | X509_NAME *name = sk_X509_NAME_value(ca_sk, i); |
2773 | 0 | int namelen; |
2774 | |
|
2775 | 0 | if (name == NULL |
2776 | 0 | || (namelen = i2d_X509_NAME(name, NULL)) < 0 |
2777 | 0 | || !WPACKET_sub_allocate_bytes_u16(pkt, namelen, |
2778 | 0 | &namebytes) |
2779 | 0 | || i2d_X509_NAME(name, &namebytes) != namelen) { |
2780 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2781 | 0 | return 0; |
2782 | 0 | } |
2783 | 0 | } |
2784 | 0 | } |
2785 | | |
2786 | 0 | if (!WPACKET_close(pkt)) { |
2787 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2788 | 0 | return 0; |
2789 | 0 | } |
2790 | | |
2791 | 0 | return 1; |
2792 | 0 | } |
2793 | | |
2794 | | /* Create a buffer containing data to be signed for server key exchange */ |
2795 | | size_t construct_key_exchange_tbs(SSL_CONNECTION *s, unsigned char **ptbs, |
2796 | | const void *param, size_t paramlen) |
2797 | 15.0k | { |
2798 | 15.0k | size_t tbslen = 2 * SSL3_RANDOM_SIZE + paramlen; |
2799 | 15.0k | unsigned char *tbs = OPENSSL_malloc(tbslen); |
2800 | | |
2801 | 15.0k | if (tbs == NULL) { |
2802 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
2803 | 0 | return 0; |
2804 | 0 | } |
2805 | 15.0k | memcpy(tbs, s->s3.client_random, SSL3_RANDOM_SIZE); |
2806 | 15.0k | memcpy(tbs + SSL3_RANDOM_SIZE, s->s3.server_random, SSL3_RANDOM_SIZE); |
2807 | | |
2808 | 15.0k | memcpy(tbs + SSL3_RANDOM_SIZE * 2, param, paramlen); |
2809 | | |
2810 | 15.0k | *ptbs = tbs; |
2811 | 15.0k | return tbslen; |
2812 | 15.0k | } |
2813 | | |
2814 | | /* |
2815 | | * Saves the current handshake digest for Post-Handshake Auth, |
2816 | | * Done after ClientFinished is processed, done exactly once |
2817 | | */ |
2818 | | int tls13_save_handshake_digest_for_pha(SSL_CONNECTION *s) |
2819 | 12.3k | { |
2820 | 12.3k | if (s->pha_dgst == NULL) { |
2821 | 12.3k | if (!ssl3_digest_cached_records(s, 1)) |
2822 | | /* SSLfatal() already called */ |
2823 | 0 | return 0; |
2824 | | |
2825 | 12.3k | s->pha_dgst = EVP_MD_CTX_new(); |
2826 | 12.3k | if (s->pha_dgst == NULL) { |
2827 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2828 | 0 | return 0; |
2829 | 0 | } |
2830 | 12.3k | if (!EVP_MD_CTX_copy_ex(s->pha_dgst, |
2831 | 12.3k | s->s3.handshake_dgst)) { |
2832 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2833 | 0 | EVP_MD_CTX_free(s->pha_dgst); |
2834 | 0 | s->pha_dgst = NULL; |
2835 | 0 | return 0; |
2836 | 0 | } |
2837 | 12.3k | } |
2838 | 12.3k | return 1; |
2839 | 12.3k | } |
2840 | | |
2841 | | /* |
2842 | | * Restores the Post-Handshake Auth handshake digest |
2843 | | * Done just before sending/processing the Cert Request |
2844 | | */ |
2845 | | int tls13_restore_handshake_digest_for_pha(SSL_CONNECTION *s) |
2846 | 0 | { |
2847 | 0 | if (s->pha_dgst == NULL) { |
2848 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2849 | 0 | return 0; |
2850 | 0 | } |
2851 | 0 | if (!EVP_MD_CTX_copy_ex(s->s3.handshake_dgst, |
2852 | 0 | s->pha_dgst)) { |
2853 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2854 | 0 | return 0; |
2855 | 0 | } |
2856 | 0 | return 1; |
2857 | 0 | } |
2858 | | |
2859 | | #ifndef OPENSSL_NO_COMP_ALG |
2860 | | MSG_PROCESS_RETURN tls13_process_compressed_certificate(SSL_CONNECTION *sc, |
2861 | | PACKET *pkt, |
2862 | | PACKET *tmppkt, |
2863 | | BUF_MEM *buf) |
2864 | | { |
2865 | | MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR; |
2866 | | int comp_alg; |
2867 | | COMP_METHOD *method = NULL; |
2868 | | COMP_CTX *comp = NULL; |
2869 | | size_t expected_length; |
2870 | | size_t comp_length; |
2871 | | int i; |
2872 | | int found = 0; |
2873 | | |
2874 | | if (buf == NULL) { |
2875 | | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2876 | | goto err; |
2877 | | } |
2878 | | if (!PACKET_get_net_2(pkt, (unsigned int *)&comp_alg)) { |
2879 | | SSLfatal(sc, SSL_AD_BAD_CERTIFICATE, ERR_R_INTERNAL_ERROR); |
2880 | | goto err; |
2881 | | } |
2882 | | /* If we have a prefs list, make sure the algorithm is in it */ |
2883 | | if (sc->cert_comp_prefs[0] != TLSEXT_comp_cert_none) { |
2884 | | for (i = 0; sc->cert_comp_prefs[i] != TLSEXT_comp_cert_none; i++) { |
2885 | | if (sc->cert_comp_prefs[i] == comp_alg) { |
2886 | | found = 1; |
2887 | | break; |
2888 | | } |
2889 | | } |
2890 | | if (!found) { |
2891 | | SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_COMPRESSION_ALGORITHM); |
2892 | | goto err; |
2893 | | } |
2894 | | } |
2895 | | if (!ossl_comp_has_alg(comp_alg)) { |
2896 | | SSLfatal(sc, SSL_AD_BAD_CERTIFICATE, SSL_R_BAD_COMPRESSION_ALGORITHM); |
2897 | | goto err; |
2898 | | } |
2899 | | switch (comp_alg) { |
2900 | | case TLSEXT_comp_cert_zlib: |
2901 | | method = COMP_zlib_oneshot(); |
2902 | | break; |
2903 | | case TLSEXT_comp_cert_brotli: |
2904 | | method = COMP_brotli_oneshot(); |
2905 | | break; |
2906 | | case TLSEXT_comp_cert_zstd: |
2907 | | method = COMP_zstd_oneshot(); |
2908 | | break; |
2909 | | default: |
2910 | | SSLfatal(sc, SSL_AD_BAD_CERTIFICATE, SSL_R_BAD_COMPRESSION_ALGORITHM); |
2911 | | goto err; |
2912 | | } |
2913 | | |
2914 | | if ((comp = COMP_CTX_new(method)) == NULL |
2915 | | || !PACKET_get_net_3_len(pkt, &expected_length) |
2916 | | || !PACKET_get_net_3_len(pkt, &comp_length)) { |
2917 | | SSLfatal(sc, SSL_AD_BAD_CERTIFICATE, SSL_R_BAD_DECOMPRESSION); |
2918 | | goto err; |
2919 | | } |
2920 | | |
2921 | | if (PACKET_remaining(pkt) != comp_length || comp_length == 0) { |
2922 | | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_DECOMPRESSION); |
2923 | | goto err; |
2924 | | } |
2925 | | |
2926 | | if (!BUF_MEM_grow(buf, expected_length) |
2927 | | || !PACKET_buf_init(tmppkt, (unsigned char *)buf->data, expected_length) |
2928 | | || COMP_expand_block(comp, (unsigned char *)buf->data, (int)expected_length, |
2929 | | (unsigned char *)PACKET_data(pkt), |
2930 | | (int)comp_length) |
2931 | | != (int)expected_length) { |
2932 | | SSLfatal(sc, SSL_AD_BAD_CERTIFICATE, SSL_R_BAD_DECOMPRESSION); |
2933 | | goto err; |
2934 | | } |
2935 | | ret = MSG_PROCESS_CONTINUE_PROCESSING; |
2936 | | err: |
2937 | | COMP_CTX_free(comp); |
2938 | | return ret; |
2939 | | } |
2940 | | #endif |