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