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