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