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