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