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