]>
Commit | Line | Data |
---|---|---|
846e33c7 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
3813046d | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
3813046d | 8 | */ |
846e33c7 | 9 | |
ea262260 BM |
10 | /* ==================================================================== |
11 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | |
0f113f3e | 12 | * ECC cipher suite support in OpenSSL originally developed by |
ea262260 BM |
13 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
14 | */ | |
d02b48c6 | 15 | |
48948d53 | 16 | #include <limits.h> |
f2d9a32c | 17 | #include <string.h> |
d02b48c6 | 18 | #include <stdio.h> |
8ba708e5 | 19 | #include "../ssl_locl.h" |
61ae935a | 20 | #include "statem_locl.h" |
ec577822 | 21 | #include <openssl/buffer.h> |
ec577822 BM |
22 | #include <openssl/objects.h> |
23 | #include <openssl/evp.h> | |
24 | #include <openssl/x509.h> | |
d02b48c6 | 25 | |
0f113f3e MC |
26 | /* |
27 | * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or | |
28 | * SSL3_RT_CHANGE_CIPHER_SPEC) | |
29 | */ | |
e7ecc7d4 | 30 | int ssl3_do_write(SSL *s, int type) |
0f113f3e MC |
31 | { |
32 | int ret; | |
7ee8627f | 33 | size_t written = 0; |
0f113f3e MC |
34 | |
35 | ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off], | |
7ee8627f | 36 | s->init_num, &written); |
0f113f3e MC |
37 | if (ret < 0) |
38 | return (-1); | |
39 | if (type == SSL3_RT_HANDSHAKE) | |
40 | /* | |
41 | * should not be done for 'Hello Request's, but in that case we'll | |
42 | * ignore the result anyway | |
43 | */ | |
d166ed8c DSH |
44 | if (!ssl3_finish_mac(s, |
45 | (unsigned char *)&s->init_buf->data[s->init_off], | |
7ee8627f | 46 | written)) |
d166ed8c | 47 | return -1; |
0f113f3e | 48 | |
7ee8627f | 49 | if (written == s->init_num) { |
0f113f3e MC |
50 | if (s->msg_callback) |
51 | s->msg_callback(1, s->version, type, s->init_buf->data, | |
52 | (size_t)(s->init_off + s->init_num), s, | |
53 | s->msg_callback_arg); | |
54 | return (1); | |
55 | } | |
7ee8627f MC |
56 | s->init_off += written; |
57 | s->init_num -= written; | |
0f113f3e MC |
58 | return (0); |
59 | } | |
e7ecc7d4 | 60 | |
4a01c59f | 61 | int tls_close_construct_packet(SSL *s, WPACKET *pkt, int htype) |
2c7b4dbc MC |
62 | { |
63 | size_t msglen; | |
64 | ||
4a01c59f | 65 | if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt)) |
f1ec23c0 | 66 | || !WPACKET_get_length(pkt, &msglen) |
7cea05dc | 67 | || msglen > INT_MAX) |
2c7b4dbc MC |
68 | return 0; |
69 | s->init_num = (int)msglen; | |
70 | s->init_off = 0; | |
71 | ||
72 | return 1; | |
73 | } | |
74 | ||
1f5b44e9 MC |
75 | int tls_setup_handshake(SSL *s) |
76 | { | |
c7f47786 MC |
77 | if (!ssl3_init_finished_mac(s)) |
78 | return 0; | |
79 | ||
80 | if (s->server) { | |
81 | if (SSL_IS_FIRST_HANDSHAKE(s)) { | |
82 | s->ctx->stats.sess_accept++; | |
83 | } else if (!s->s3->send_connection_binding && | |
84 | !(s->options & | |
85 | SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { | |
86 | /* | |
87 | * Server attempting to renegotiate with client that doesn't | |
88 | * support secure renegotiation. | |
89 | */ | |
90 | SSLerr(SSL_F_TLS_SETUP_HANDSHAKE, | |
91 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); | |
92 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
93 | return 0; | |
94 | } else { | |
95 | s->ctx->stats.sess_accept_renegotiate++; | |
96 | ||
97 | s->s3->tmp.cert_request = 0; | |
98 | } | |
99 | } else { | |
100 | if (SSL_IS_FIRST_HANDSHAKE(s)) | |
101 | s->ctx->stats.sess_connect++; | |
102 | else | |
103 | s->ctx->stats.sess_connect_renegotiate++; | |
104 | ||
105 | /* mark client_random uninitialized */ | |
106 | memset(s->s3->client_random, 0, sizeof(s->s3->client_random)); | |
107 | s->hit = 0; | |
108 | ||
109 | s->s3->tmp.cert_req = 0; | |
110 | ||
1f5b44e9 | 111 | if (SSL_IS_DTLS(s)) |
c7f47786 | 112 | s->statem.use_timer = 1; |
c7f47786 MC |
113 | } |
114 | ||
115 | return 1; | |
116 | } | |
117 | ||
2c5dfdc3 MC |
118 | /* |
119 | * Size of the to-be-signed TLS13 data, without the hash size itself: | |
120 | * 64 bytes of value 32, 33 context bytes, 1 byte separator | |
121 | */ | |
122 | #define TLS13_TBS_START_SIZE 64 | |
123 | #define TLS13_TBS_PREAMBLE_SIZE (TLS13_TBS_START_SIZE + 33 + 1) | |
124 | ||
125 | static int get_cert_verify_tbs_data(SSL *s, unsigned char *tls13tbs, | |
126 | void **hdata, size_t *hdatalen) | |
127 | { | |
128 | static const char *servercontext = "TLS 1.3, server CertificateVerify"; | |
129 | static const char *clientcontext = "TLS 1.3, client CertificateVerify"; | |
130 | ||
131 | if (SSL_IS_TLS13(s)) { | |
132 | size_t hashlen; | |
133 | ||
134 | /* Set the first 64 bytes of to-be-signed data to octet 32 */ | |
135 | memset(tls13tbs, 32, TLS13_TBS_START_SIZE); | |
136 | /* This copies the 33 bytes of context plus the 0 separator byte */ | |
137 | if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY | |
138 | || s->statem.hand_state == TLS_ST_SW_CERT_VRFY) | |
139 | strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, servercontext); | |
140 | else | |
141 | strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, clientcontext); | |
142 | ||
143 | /* | |
144 | * If we're currently reading then we need to use the saved handshake | |
145 | * hash value. We can't use the current handshake hash state because | |
146 | * that includes the CertVerify itself. | |
147 | */ | |
148 | if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY | |
149 | || s->statem.hand_state == TLS_ST_SR_CERT_VRFY) { | |
150 | memcpy(tls13tbs + TLS13_TBS_PREAMBLE_SIZE, s->cert_verify_hash, | |
151 | s->cert_verify_hash_len); | |
152 | hashlen = s->cert_verify_hash_len; | |
153 | } else if (!ssl_handshake_hash(s, tls13tbs + TLS13_TBS_PREAMBLE_SIZE, | |
154 | EVP_MAX_MD_SIZE, &hashlen)) { | |
155 | return 0; | |
156 | } | |
157 | ||
158 | *hdata = tls13tbs; | |
159 | *hdatalen = TLS13_TBS_PREAMBLE_SIZE + hashlen; | |
160 | } else { | |
161 | size_t retlen; | |
162 | ||
163 | retlen = BIO_get_mem_data(s->s3->handshake_buffer, hdata); | |
164 | if (retlen <= 0) | |
165 | return 0; | |
166 | *hdatalen = retlen; | |
167 | } | |
168 | ||
169 | return 1; | |
170 | } | |
171 | ||
d8bc1399 MC |
172 | int tls_construct_cert_verify(SSL *s, WPACKET *pkt) |
173 | { | |
ad4dd362 DSH |
174 | EVP_PKEY *pkey = NULL; |
175 | const EVP_MD *md = NULL; | |
d8bc1399 | 176 | EVP_MD_CTX *mctx = NULL; |
5f9b64a2 MC |
177 | EVP_PKEY_CTX *pctx = NULL; |
178 | size_t hdatalen = 0, siglen = 0; | |
d8bc1399 MC |
179 | void *hdata; |
180 | unsigned char *sig = NULL; | |
2c5dfdc3 | 181 | unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE]; |
ad4dd362 | 182 | const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg; |
2c5dfdc3 | 183 | |
ad4dd362 DSH |
184 | if (lu == NULL || s->s3->tmp.cert == NULL) { |
185 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR); | |
186 | goto err; | |
187 | } | |
188 | pkey = s->s3->tmp.cert->privatekey; | |
189 | md = ssl_md(lu->hash_idx); | |
190 | ||
191 | if (pkey == NULL || md == NULL) { | |
192 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR); | |
193 | goto err; | |
194 | } | |
d8bc1399 MC |
195 | |
196 | mctx = EVP_MD_CTX_new(); | |
197 | if (mctx == NULL) { | |
198 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_MALLOC_FAILURE); | |
199 | goto err; | |
200 | } | |
d8bc1399 | 201 | |
2c5dfdc3 MC |
202 | /* Get the data to be signed */ |
203 | if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) { | |
d8bc1399 MC |
204 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR); |
205 | goto err; | |
206 | } | |
207 | ||
ad4dd362 | 208 | if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) { |
d8bc1399 MC |
209 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR); |
210 | goto err; | |
211 | } | |
5f9b64a2 MC |
212 | siglen = EVP_PKEY_size(pkey); |
213 | sig = OPENSSL_malloc(siglen); | |
d8bc1399 MC |
214 | if (sig == NULL) { |
215 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_MALLOC_FAILURE); | |
216 | goto err; | |
217 | } | |
5f9b64a2 MC |
218 | |
219 | if (EVP_DigestSignInit(mctx, &pctx, md, NULL, pkey) <= 0 | |
220 | || EVP_DigestSignUpdate(mctx, hdata, hdatalen) <= 0) { | |
221 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB); | |
222 | goto err; | |
223 | } | |
224 | ||
ad4dd362 | 225 | if (lu->sig == EVP_PKEY_RSA_PSS) { |
5f9b64a2 | 226 | if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 |
968ae5b3 DSH |
227 | || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, |
228 | RSA_PSS_SALTLEN_DIGEST) <= 0) { | |
5f9b64a2 MC |
229 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB); |
230 | goto err; | |
231 | } | |
232 | } else if (s->version == SSL3_VERSION) { | |
233 | if (!EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET, | |
234 | (int)s->session->master_key_length, | |
235 | s->session->master_key)) { | |
236 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB); | |
237 | goto err; | |
238 | } | |
239 | } | |
240 | ||
241 | if (EVP_DigestSignFinal(mctx, sig, &siglen) <= 0) { | |
d8bc1399 MC |
242 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB); |
243 | goto err; | |
244 | } | |
5f9b64a2 | 245 | |
d8bc1399 MC |
246 | #ifndef OPENSSL_NO_GOST |
247 | { | |
ad4dd362 DSH |
248 | int pktype = lu->sig; |
249 | ||
d8bc1399 MC |
250 | if (pktype == NID_id_GostR3410_2001 |
251 | || pktype == NID_id_GostR3410_2012_256 | |
252 | || pktype == NID_id_GostR3410_2012_512) | |
5f9b64a2 | 253 | BUF_reverse(sig, NULL, siglen); |
d8bc1399 MC |
254 | } |
255 | #endif | |
256 | ||
5f9b64a2 | 257 | if (!WPACKET_sub_memcpy_u16(pkt, sig, siglen)) { |
d8bc1399 MC |
258 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR); |
259 | goto err; | |
260 | } | |
261 | ||
262 | /* Digest cached records and discard handshake buffer */ | |
263 | if (!ssl3_digest_cached_records(s, 0)) | |
264 | goto err; | |
265 | ||
266 | OPENSSL_free(sig); | |
267 | EVP_MD_CTX_free(mctx); | |
268 | return 1; | |
269 | err: | |
270 | OPENSSL_free(sig); | |
271 | EVP_MD_CTX_free(mctx); | |
272 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
273 | return 0; | |
274 | } | |
275 | ||
276 | MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt) | |
277 | { | |
278 | EVP_PKEY *pkey = NULL; | |
703bcee0 | 279 | const unsigned char *data; |
d8bc1399 MC |
280 | #ifndef OPENSSL_NO_GOST |
281 | unsigned char *gost_data = NULL; | |
282 | #endif | |
5f9b64a2 | 283 | int al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR; |
5554facb | 284 | int type = 0, j, pktype; |
d8bc1399 MC |
285 | unsigned int len; |
286 | X509 *peer; | |
287 | const EVP_MD *md = NULL; | |
2c5dfdc3 | 288 | size_t hdatalen = 0; |
d8bc1399 | 289 | void *hdata; |
2c5dfdc3 | 290 | unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE]; |
d8bc1399 | 291 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); |
5f9b64a2 | 292 | EVP_PKEY_CTX *pctx = NULL; |
d8bc1399 MC |
293 | |
294 | if (mctx == NULL) { | |
295 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE); | |
d8bc1399 MC |
296 | goto f_err; |
297 | } | |
298 | ||
299 | peer = s->session->peer; | |
300 | pkey = X509_get0_pubkey(peer); | |
83b4049a BE |
301 | if (pkey == NULL) { |
302 | al = SSL_AD_INTERNAL_ERROR; | |
303 | goto f_err; | |
304 | } | |
305 | ||
5f9b64a2 | 306 | pktype = EVP_PKEY_id(pkey); |
d8bc1399 MC |
307 | type = X509_certificate_type(peer, pkey); |
308 | ||
309 | if (!(type & EVP_PKT_SIGN)) { | |
310 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, | |
311 | SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); | |
312 | al = SSL_AD_ILLEGAL_PARAMETER; | |
313 | goto f_err; | |
314 | } | |
315 | ||
316 | /* Check for broken implementations of GOST ciphersuites */ | |
317 | /* | |
318 | * If key is GOST and n is exactly 64, it is bare signature without | |
319 | * length field (CryptoPro implementations at least till CSP 4.0) | |
320 | */ | |
321 | #ifndef OPENSSL_NO_GOST | |
322 | if (PACKET_remaining(pkt) == 64 | |
323 | && EVP_PKEY_id(pkey) == NID_id_GostR3410_2001) { | |
324 | len = 64; | |
325 | } else | |
326 | #endif | |
327 | { | |
328 | if (SSL_USE_SIGALGS(s)) { | |
329 | int rv; | |
703bcee0 | 330 | unsigned int sigalg; |
d8bc1399 | 331 | |
703bcee0 | 332 | if (!PACKET_get_net_2(pkt, &sigalg)) { |
d8bc1399 MC |
333 | al = SSL_AD_DECODE_ERROR; |
334 | goto f_err; | |
335 | } | |
5554facb | 336 | rv = tls12_check_peer_sigalg(s, sigalg, pkey); |
d8bc1399 | 337 | if (rv == -1) { |
d8bc1399 MC |
338 | goto f_err; |
339 | } else if (rv == 0) { | |
340 | al = SSL_AD_DECODE_ERROR; | |
341 | goto f_err; | |
342 | } | |
343 | #ifdef SSL_DEBUG | |
344 | fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); | |
345 | #endif | |
f365a3e2 | 346 | } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) { |
d8bc1399 MC |
347 | al = SSL_AD_INTERNAL_ERROR; |
348 | goto f_err; | |
d8bc1399 MC |
349 | } |
350 | ||
f365a3e2 DSH |
351 | md = ssl_md(s->s3->tmp.peer_sigalg->hash_idx); |
352 | ||
d8bc1399 MC |
353 | if (!PACKET_get_net_2(pkt, &len)) { |
354 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH); | |
355 | al = SSL_AD_DECODE_ERROR; | |
356 | goto f_err; | |
357 | } | |
358 | } | |
359 | j = EVP_PKEY_size(pkey); | |
360 | if (((int)len > j) || ((int)PACKET_remaining(pkt) > j) | |
361 | || (PACKET_remaining(pkt) == 0)) { | |
362 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE); | |
363 | al = SSL_AD_DECODE_ERROR; | |
364 | goto f_err; | |
365 | } | |
366 | if (!PACKET_get_bytes(pkt, &data, len)) { | |
367 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH); | |
368 | al = SSL_AD_DECODE_ERROR; | |
369 | goto f_err; | |
370 | } | |
371 | ||
2c5dfdc3 | 372 | if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) { |
d8bc1399 | 373 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_INTERNAL_ERROR); |
d8bc1399 MC |
374 | goto f_err; |
375 | } | |
376 | ||
377 | #ifdef SSL_DEBUG | |
378 | fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md)); | |
379 | #endif | |
5f9b64a2 MC |
380 | if (EVP_DigestVerifyInit(mctx, &pctx, md, NULL, pkey) <= 0 |
381 | || EVP_DigestVerifyUpdate(mctx, hdata, hdatalen) <= 0) { | |
d8bc1399 | 382 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB); |
d8bc1399 MC |
383 | goto f_err; |
384 | } | |
385 | #ifndef OPENSSL_NO_GOST | |
386 | { | |
d8bc1399 MC |
387 | if (pktype == NID_id_GostR3410_2001 |
388 | || pktype == NID_id_GostR3410_2012_256 | |
389 | || pktype == NID_id_GostR3410_2012_512) { | |
390 | if ((gost_data = OPENSSL_malloc(len)) == NULL) { | |
391 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE); | |
d8bc1399 MC |
392 | goto f_err; |
393 | } | |
394 | BUF_reverse(gost_data, data, len); | |
395 | data = gost_data; | |
396 | } | |
397 | } | |
398 | #endif | |
399 | ||
5554facb | 400 | if (SSL_USE_PSS(s)) { |
5f9b64a2 | 401 | if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 |
968ae5b3 DSH |
402 | || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, |
403 | RSA_PSS_SALTLEN_DIGEST) <= 0) { | |
5f9b64a2 MC |
404 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB); |
405 | goto f_err; | |
406 | } | |
407 | } else if (s->version == SSL3_VERSION | |
d8bc1399 MC |
408 | && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET, |
409 | (int)s->session->master_key_length, | |
410 | s->session->master_key)) { | |
411 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB); | |
d8bc1399 MC |
412 | goto f_err; |
413 | } | |
414 | ||
5f9b64a2 | 415 | if (EVP_DigestVerifyFinal(mctx, data, len) <= 0) { |
d8bc1399 MC |
416 | al = SSL_AD_DECRYPT_ERROR; |
417 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_SIGNATURE); | |
418 | goto f_err; | |
419 | } | |
420 | ||
2c5dfdc3 MC |
421 | if (SSL_IS_TLS13(s)) |
422 | ret = MSG_PROCESS_CONTINUE_READING; | |
423 | else | |
424 | ret = MSG_PROCESS_CONTINUE_PROCESSING; | |
d8bc1399 MC |
425 | if (0) { |
426 | f_err: | |
427 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
428 | ossl_statem_set_error(s); | |
429 | } | |
430 | BIO_free(s->s3->handshake_buffer); | |
431 | s->s3->handshake_buffer = NULL; | |
432 | EVP_MD_CTX_free(mctx); | |
433 | #ifndef OPENSSL_NO_GOST | |
434 | OPENSSL_free(gost_data); | |
435 | #endif | |
436 | return ret; | |
437 | } | |
438 | ||
229185e6 | 439 | int tls_construct_finished(SSL *s, WPACKET *pkt) |
0f113f3e | 440 | { |
12472b45 | 441 | size_t finish_md_len; |
229185e6 | 442 | const char *sender; |
8b0e934a | 443 | size_t slen; |
229185e6 | 444 | |
f7e393be MC |
445 | /* This is a real handshake so make sure we clean it up at the end */ |
446 | if (!s->server) | |
447 | s->statem.cleanuphand = 1; | |
448 | ||
449 | /* | |
450 | * We only change the keys if we didn't already do this when we sent the | |
451 | * client certificate | |
452 | */ | |
453 | if (SSL_IS_TLS13(s) | |
454 | && !s->server | |
455 | && s->s3->tmp.cert_req == 0 | |
456 | && (!s->method->ssl3_enc->change_cipher_state(s, | |
457 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) { | |
458 | SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER); | |
459 | goto err; | |
460 | } | |
461 | ||
229185e6 MC |
462 | if (s->server) { |
463 | sender = s->method->ssl3_enc->server_finished_label; | |
464 | slen = s->method->ssl3_enc->server_finished_label_len; | |
465 | } else { | |
466 | sender = s->method->ssl3_enc->client_finished_label; | |
467 | slen = s->method->ssl3_enc->client_finished_label_len; | |
468 | } | |
0f113f3e | 469 | |
12472b45 MC |
470 | finish_md_len = s->method->ssl3_enc->final_finish_mac(s, |
471 | sender, slen, | |
472 | s->s3->tmp.finish_md); | |
473 | if (finish_md_len == 0) { | |
4f89bfbf MC |
474 | SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR); |
475 | goto err; | |
476 | } | |
477 | ||
12472b45 | 478 | s->s3->tmp.finish_md_len = finish_md_len; |
4f89bfbf | 479 | |
12472b45 | 480 | if (!WPACKET_memcpy(pkt, s->s3->tmp.finish_md, finish_md_len)) { |
4f89bfbf MC |
481 | SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR); |
482 | goto err; | |
483 | } | |
0f113f3e | 484 | |
2c7bd692 CB |
485 | /* |
486 | * Log the master secret, if logging is enabled. We don't log it for | |
487 | * TLSv1.3: there's a different key schedule for that. | |
488 | */ | |
489 | if (!SSL_IS_TLS13(s) && !ssl_log_secret(s, MASTER_SECRET_LABEL, | |
490 | s->session->master_key, | |
491 | s->session->master_key_length)) | |
2faa1b48 CB |
492 | return 0; |
493 | ||
b9908bf9 MC |
494 | /* |
495 | * Copy the finished so we can use it for renegotiation checks | |
496 | */ | |
23a635c0 | 497 | if (!s->server) { |
12472b45 MC |
498 | OPENSSL_assert(finish_md_len <= EVP_MAX_MD_SIZE); |
499 | memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md, | |
500 | finish_md_len); | |
501 | s->s3->previous_client_finished_len = finish_md_len; | |
b9908bf9 | 502 | } else { |
12472b45 MC |
503 | OPENSSL_assert(finish_md_len <= EVP_MAX_MD_SIZE); |
504 | memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md, | |
505 | finish_md_len); | |
506 | s->s3->previous_server_finished_len = finish_md_len; | |
b9908bf9 | 507 | } |
0f113f3e | 508 | |
b9908bf9 | 509 | return 1; |
4f89bfbf | 510 | err: |
4f89bfbf MC |
511 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
512 | return 0; | |
0f113f3e | 513 | } |
d02b48c6 | 514 | |
44c04a2e MC |
515 | int tls_construct_key_update(SSL *s, WPACKET *pkt) |
516 | { | |
517 | if (!WPACKET_put_bytes_u8(pkt, s->key_update)) { | |
518 | SSLerr(SSL_F_TLS_CONSTRUCT_KEY_UPDATE, ERR_R_INTERNAL_ERROR); | |
519 | goto err; | |
520 | } | |
521 | ||
9412b3ad | 522 | s->key_update = SSL_KEY_UPDATE_NONE; |
44c04a2e | 523 | return 1; |
f14afcaa | 524 | |
44c04a2e MC |
525 | err: |
526 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
527 | return 0; | |
528 | } | |
529 | ||
e1c3de44 MC |
530 | MSG_PROCESS_RETURN tls_process_key_update(SSL *s, PACKET *pkt) |
531 | { | |
57389a32 | 532 | int al; |
e1c3de44 MC |
533 | unsigned int updatetype; |
534 | ||
82f992cb MC |
535 | s->key_update_count++; |
536 | if (s->key_update_count > MAX_KEY_UPDATE_MESSAGES) { | |
537 | al = SSL_AD_ILLEGAL_PARAMETER; | |
538 | SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE, SSL_R_TOO_MANY_KEY_UPDATES); | |
539 | goto err; | |
540 | } | |
541 | ||
524420d8 MC |
542 | /* |
543 | * A KeyUpdate message signals a key change so the end of the message must | |
544 | * be on a record boundary. | |
545 | */ | |
546 | if (RECORD_LAYER_processed_read_pending(&s->rlayer)) { | |
547 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
548 | SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE, SSL_R_NOT_ON_RECORD_BOUNDARY); | |
549 | goto err; | |
550 | } | |
551 | ||
e1c3de44 MC |
552 | if (!PACKET_get_1(pkt, &updatetype) |
553 | || PACKET_remaining(pkt) != 0 | |
554 | || (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED | |
555 | && updatetype != SSL_KEY_UPDATE_REQUESTED)) { | |
57389a32 | 556 | al = SSL_AD_DECODE_ERROR; |
e1c3de44 | 557 | SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE, SSL_R_BAD_KEY_UPDATE); |
57389a32 | 558 | goto err; |
e1c3de44 MC |
559 | } |
560 | ||
5bf47933 MC |
561 | /* |
562 | * If we get a request for us to update our sending keys too then, we need | |
563 | * to additionally send a KeyUpdate message. However that message should | |
564 | * not also request an update (otherwise we get into an infinite loop). | |
565 | */ | |
566 | if (updatetype == SSL_KEY_UPDATE_REQUESTED) | |
567 | s->key_update = SSL_KEY_UPDATE_NOT_REQUESTED; | |
568 | ||
57389a32 MC |
569 | if (!tls13_update_key(s, 0)) { |
570 | al = SSL_AD_INTERNAL_ERROR; | |
571 | SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE, ERR_R_INTERNAL_ERROR); | |
572 | goto err; | |
573 | } | |
574 | ||
e1c3de44 | 575 | return MSG_PROCESS_FINISHED_READING; |
57389a32 MC |
576 | err: |
577 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
578 | ossl_statem_set_error(s); | |
579 | return MSG_PROCESS_ERROR; | |
e1c3de44 MC |
580 | } |
581 | ||
bf48836c | 582 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
583 | /* |
584 | * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen | |
585 | * to far. | |
586 | */ | |
ee2ffc27 | 587 | static void ssl3_take_mac(SSL *s) |
0f113f3e MC |
588 | { |
589 | const char *sender; | |
8b0e934a | 590 | size_t slen; |
0f113f3e MC |
591 | /* |
592 | * If no new cipher setup return immediately: other functions will set | |
593 | * the appropriate error. | |
594 | */ | |
595 | if (s->s3->tmp.new_cipher == NULL) | |
596 | return; | |
49ae7423 | 597 | if (!s->server) { |
0f113f3e MC |
598 | sender = s->method->ssl3_enc->server_finished_label; |
599 | slen = s->method->ssl3_enc->server_finished_label_len; | |
600 | } else { | |
601 | sender = s->method->ssl3_enc->client_finished_label; | |
602 | slen = s->method->ssl3_enc->client_finished_label_len; | |
603 | } | |
604 | ||
605 | s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s, | |
606 | sender, | |
607 | slen, | |
608 | s->s3->tmp.peer_finish_md); | |
609 | } | |
ee2ffc27 BL |
610 | #endif |
611 | ||
be3583fa | 612 | MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt) |
b9908bf9 MC |
613 | { |
614 | int al; | |
348240c6 | 615 | size_t remain; |
4fa52141 | 616 | |
73999b62 | 617 | remain = PACKET_remaining(pkt); |
657da85e MC |
618 | /* |
619 | * 'Change Cipher Spec' is just a single byte, which should already have | |
c69f2adf MC |
620 | * been consumed by ssl_get_message() so there should be no bytes left, |
621 | * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes | |
657da85e | 622 | */ |
c69f2adf | 623 | if (SSL_IS_DTLS(s)) { |
73999b62 | 624 | if ((s->version == DTLS1_BAD_VER |
a230b26e EK |
625 | && remain != DTLS1_CCS_HEADER_LENGTH + 1) |
626 | || (s->version != DTLS1_BAD_VER | |
627 | && remain != DTLS1_CCS_HEADER_LENGTH - 1)) { | |
628 | al = SSL_AD_ILLEGAL_PARAMETER; | |
629 | SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, | |
630 | SSL_R_BAD_CHANGE_CIPHER_SPEC); | |
631 | goto f_err; | |
c69f2adf MC |
632 | } |
633 | } else { | |
73999b62 | 634 | if (remain != 0) { |
c69f2adf | 635 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 MC |
636 | SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, |
637 | SSL_R_BAD_CHANGE_CIPHER_SPEC); | |
c69f2adf MC |
638 | goto f_err; |
639 | } | |
657da85e MC |
640 | } |
641 | ||
642 | /* Check we have a cipher to change to */ | |
643 | if (s->s3->tmp.new_cipher == NULL) { | |
644 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
b9908bf9 | 645 | SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY); |
657da85e MC |
646 | goto f_err; |
647 | } | |
648 | ||
649 | s->s3->change_cipher_spec = 1; | |
650 | if (!ssl3_do_change_cipher_spec(s)) { | |
651 | al = SSL_AD_INTERNAL_ERROR; | |
b9908bf9 | 652 | SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR); |
657da85e MC |
653 | goto f_err; |
654 | } | |
655 | ||
c69f2adf MC |
656 | if (SSL_IS_DTLS(s)) { |
657 | dtls1_reset_seq_numbers(s, SSL3_CC_READ); | |
658 | ||
659 | if (s->version == DTLS1_BAD_VER) | |
660 | s->d1->handshake_read_seq++; | |
661 | ||
662 | #ifndef OPENSSL_NO_SCTP | |
663 | /* | |
664 | * Remember that a CCS has been received, so that an old key of | |
665 | * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no | |
666 | * SCTP is used | |
667 | */ | |
668 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL); | |
669 | #endif | |
670 | } | |
671 | ||
b9908bf9 | 672 | return MSG_PROCESS_CONTINUE_READING; |
657da85e MC |
673 | f_err: |
674 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 675 | ossl_statem_set_error(s); |
b9908bf9 | 676 | return MSG_PROCESS_ERROR; |
657da85e MC |
677 | } |
678 | ||
be3583fa | 679 | MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt) |
b9908bf9 | 680 | { |
7776a36c | 681 | int al = SSL_AD_INTERNAL_ERROR; |
12472b45 | 682 | size_t md_len; |
b9908bf9 | 683 | |
d781d247 MC |
684 | |
685 | /* This is a real handshake so make sure we clean it up at the end */ | |
f7e393be MC |
686 | if (s->server) |
687 | s->statem.cleanuphand = 1; | |
d781d247 | 688 | |
524420d8 MC |
689 | /* |
690 | * In TLSv1.3 a Finished message signals a key change so the end of the | |
691 | * message must be on a record boundary. | |
692 | */ | |
693 | if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) { | |
694 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
695 | SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_NOT_ON_RECORD_BOUNDARY); | |
696 | goto f_err; | |
697 | } | |
698 | ||
0f113f3e | 699 | /* If this occurs, we have missed a message */ |
92760c21 | 700 | if (!SSL_IS_TLS13(s) && !s->s3->change_cipher_spec) { |
0f113f3e | 701 | al = SSL_AD_UNEXPECTED_MESSAGE; |
b9908bf9 | 702 | SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_GOT_A_FIN_BEFORE_A_CCS); |
0f113f3e MC |
703 | goto f_err; |
704 | } | |
705 | s->s3->change_cipher_spec = 0; | |
706 | ||
12472b45 | 707 | md_len = s->s3->tmp.peer_finish_md_len; |
0f113f3e | 708 | |
12472b45 | 709 | if (md_len != PACKET_remaining(pkt)) { |
0f113f3e | 710 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 711 | SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_BAD_DIGEST_LENGTH); |
0f113f3e MC |
712 | goto f_err; |
713 | } | |
714 | ||
12472b45 MC |
715 | if (CRYPTO_memcmp(PACKET_data(pkt), s->s3->tmp.peer_finish_md, |
716 | md_len) != 0) { | |
0f113f3e | 717 | al = SSL_AD_DECRYPT_ERROR; |
b9908bf9 | 718 | SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_DIGEST_CHECK_FAILED); |
0f113f3e MC |
719 | goto f_err; |
720 | } | |
721 | ||
722 | /* | |
723 | * Copy the finished so we can use it for renegotiation checks | |
724 | */ | |
23a635c0 | 725 | if (s->server) { |
12472b45 MC |
726 | OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE); |
727 | memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md, | |
728 | md_len); | |
729 | s->s3->previous_client_finished_len = md_len; | |
0f113f3e | 730 | } else { |
12472b45 MC |
731 | OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE); |
732 | memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md, | |
733 | md_len); | |
734 | s->s3->previous_server_finished_len = md_len; | |
0f113f3e MC |
735 | } |
736 | ||
7776a36c MC |
737 | /* |
738 | * In TLS1.3 we also have to change cipher state and do any final processing | |
739 | * of the initial server flight (if we are a client) | |
740 | */ | |
92760c21 MC |
741 | if (SSL_IS_TLS13(s)) { |
742 | if (s->server) { | |
743 | if (!s->method->ssl3_enc->change_cipher_state(s, | |
744 | SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_READ)) { | |
92760c21 MC |
745 | SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER); |
746 | goto f_err; | |
747 | } | |
748 | } else { | |
749 | if (!s->method->ssl3_enc->generate_master_secret(s, | |
ec15acb6 | 750 | s->master_secret, s->handshake_secret, 0, |
92760c21 | 751 | &s->session->master_key_length)) { |
92760c21 MC |
752 | SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER); |
753 | goto f_err; | |
754 | } | |
755 | if (!s->method->ssl3_enc->change_cipher_state(s, | |
756 | SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) { | |
92760c21 MC |
757 | SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER); |
758 | goto f_err; | |
759 | } | |
7776a36c MC |
760 | if (!tls_process_initial_server_flight(s, &al)) |
761 | goto f_err; | |
92760c21 MC |
762 | } |
763 | } | |
764 | ||
e6575156 | 765 | return MSG_PROCESS_FINISHED_READING; |
0f113f3e MC |
766 | f_err: |
767 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 768 | ossl_statem_set_error(s); |
b9908bf9 | 769 | return MSG_PROCESS_ERROR; |
0f113f3e | 770 | } |
d02b48c6 | 771 | |
7cea05dc | 772 | int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt) |
b9908bf9 | 773 | { |
7cea05dc | 774 | if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) { |
3c106325 | 775 | SSLerr(SSL_F_TLS_CONSTRUCT_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR); |
85a7a5e6 MC |
776 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
777 | return 0; | |
778 | } | |
b9908bf9 | 779 | |
b9908bf9 MC |
780 | return 1; |
781 | } | |
782 | ||
e96e0f8e MC |
783 | /* Add a certificate to the WPACKET */ |
784 | static int ssl_add_cert_to_wpacket(SSL *s, WPACKET *pkt, X509 *x, int chain, | |
785 | int *al) | |
0f113f3e | 786 | { |
e96e0f8e MC |
787 | int len; |
788 | unsigned char *outbytes; | |
789 | ||
790 | len = i2d_X509(x, NULL); | |
791 | if (len < 0) { | |
f63e4288 | 792 | SSLerr(SSL_F_SSL_ADD_CERT_TO_WPACKET, ERR_R_BUF_LIB); |
e96e0f8e MC |
793 | *al = SSL_AD_INTERNAL_ERROR; |
794 | return 0; | |
795 | } | |
796 | if (!WPACKET_sub_allocate_bytes_u24(pkt, len, &outbytes) | |
797 | || i2d_X509(x, &outbytes) != len) { | |
f63e4288 | 798 | SSLerr(SSL_F_SSL_ADD_CERT_TO_WPACKET, ERR_R_INTERNAL_ERROR); |
e96e0f8e MC |
799 | *al = SSL_AD_INTERNAL_ERROR; |
800 | return 0; | |
801 | } | |
802 | ||
803 | if (SSL_IS_TLS13(s) | |
804 | && !tls_construct_extensions(s, pkt, EXT_TLS1_3_CERTIFICATE, x, | |
805 | chain, al)) | |
806 | return 0; | |
807 | ||
808 | return 1; | |
809 | } | |
810 | ||
811 | /* Add certificate chain to provided WPACKET */ | |
812 | static int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk, int *al) | |
813 | { | |
814 | int i, chain_count; | |
815 | X509 *x; | |
816 | STACK_OF(X509) *extra_certs; | |
817 | STACK_OF(X509) *chain = NULL; | |
818 | X509_STORE *chain_store; | |
819 | int tmpal = SSL_AD_INTERNAL_ERROR; | |
820 | ||
821 | if (cpk == NULL || cpk->x509 == NULL) | |
822 | return 1; | |
823 | ||
824 | x = cpk->x509; | |
825 | ||
826 | /* | |
827 | * If we have a certificate specific chain use it, else use parent ctx. | |
828 | */ | |
d805a57b | 829 | if (cpk->chain != NULL) |
e96e0f8e MC |
830 | extra_certs = cpk->chain; |
831 | else | |
832 | extra_certs = s->ctx->extra_certs; | |
833 | ||
834 | if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) | |
835 | chain_store = NULL; | |
836 | else if (s->cert->chain_store) | |
837 | chain_store = s->cert->chain_store; | |
838 | else | |
839 | chain_store = s->ctx->cert_store; | |
840 | ||
d805a57b | 841 | if (chain_store != NULL) { |
e96e0f8e MC |
842 | X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new(); |
843 | ||
844 | if (xs_ctx == NULL) { | |
845 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_MALLOC_FAILURE); | |
846 | goto err; | |
847 | } | |
848 | if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) { | |
849 | X509_STORE_CTX_free(xs_ctx); | |
850 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB); | |
851 | goto err; | |
852 | } | |
853 | /* | |
854 | * It is valid for the chain not to be complete (because normally we | |
855 | * don't include the root cert in the chain). Therefore we deliberately | |
856 | * ignore the error return from this call. We're not actually verifying | |
857 | * the cert - we're just building as much of the chain as we can | |
858 | */ | |
859 | (void)X509_verify_cert(xs_ctx); | |
860 | /* Don't leave errors in the queue */ | |
861 | ERR_clear_error(); | |
862 | chain = X509_STORE_CTX_get0_chain(xs_ctx); | |
863 | i = ssl_security_cert_chain(s, chain, NULL, 0); | |
864 | if (i != 1) { | |
865 | #if 0 | |
866 | /* Dummy error calls so mkerr generates them */ | |
867 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL); | |
868 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL); | |
869 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK); | |
870 | #endif | |
871 | X509_STORE_CTX_free(xs_ctx); | |
872 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i); | |
873 | goto err; | |
874 | } | |
875 | chain_count = sk_X509_num(chain); | |
876 | for (i = 0; i < chain_count; i++) { | |
877 | x = sk_X509_value(chain, i); | |
878 | ||
879 | if (!ssl_add_cert_to_wpacket(s, pkt, x, i, &tmpal)) { | |
880 | X509_STORE_CTX_free(xs_ctx); | |
881 | goto err; | |
882 | } | |
883 | } | |
884 | X509_STORE_CTX_free(xs_ctx); | |
885 | } else { | |
886 | i = ssl_security_cert_chain(s, extra_certs, x, 0); | |
887 | if (i != 1) { | |
888 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i); | |
889 | goto err; | |
890 | } | |
891 | if (!ssl_add_cert_to_wpacket(s, pkt, x, 0, &tmpal)) | |
892 | goto err; | |
893 | for (i = 0; i < sk_X509_num(extra_certs); i++) { | |
894 | x = sk_X509_value(extra_certs, i); | |
895 | if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1, &tmpal)) | |
896 | goto err; | |
897 | } | |
898 | } | |
899 | return 1; | |
900 | ||
901 | err: | |
902 | *al = tmpal; | |
903 | return 0; | |
904 | } | |
905 | ||
906 | unsigned long ssl3_output_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk, | |
907 | int *al) | |
908 | { | |
909 | int tmpal = SSL_AD_INTERNAL_ERROR; | |
910 | ||
5923ad4b | 911 | if (!WPACKET_start_sub_packet_u24(pkt) |
e96e0f8e | 912 | || !ssl_add_cert_chain(s, pkt, cpk, &tmpal) |
5923ad4b | 913 | || !WPACKET_close(pkt)) { |
c49e1912 | 914 | SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_INTERNAL_ERROR); |
e96e0f8e | 915 | *al = tmpal; |
7cea05dc | 916 | return 0; |
77d514c5 | 917 | } |
c49e1912 | 918 | return 1; |
0f113f3e MC |
919 | } |
920 | ||
30f05b19 MC |
921 | /* |
922 | * Tidy up after the end of a handshake. In the case of SCTP this may result | |
923 | * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is | |
924 | * freed up as well. | |
925 | */ | |
926 | WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst, int clearbufs) | |
8723588e MC |
927 | { |
928 | void (*cb) (const SSL *ssl, int type, int val) = NULL; | |
929 | ||
930 | #ifndef OPENSSL_NO_SCTP | |
931 | if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) { | |
be3583fa | 932 | WORK_STATE ret; |
8723588e MC |
933 | ret = dtls_wait_for_dry(s); |
934 | if (ret != WORK_FINISHED_CONTINUE) | |
935 | return ret; | |
936 | } | |
937 | #endif | |
938 | ||
30f05b19 MC |
939 | if (clearbufs) { |
940 | if (!SSL_IS_DTLS(s)) { | |
941 | /* | |
942 | * We don't do this in DTLS because we may still need the init_buf | |
943 | * in case there are any unexpected retransmits | |
944 | */ | |
945 | BUF_MEM_free(s->init_buf); | |
946 | s->init_buf = NULL; | |
947 | } | |
948 | ssl_free_wbio_buffer(s); | |
949 | s->init_num = 0; | |
473483d4 | 950 | } |
8723588e | 951 | |
c7f47786 | 952 | if (s->statem.cleanuphand) { |
8723588e MC |
953 | /* skipped if we just sent a HelloRequest */ |
954 | s->renegotiate = 0; | |
955 | s->new_session = 0; | |
c7f47786 | 956 | s->statem.cleanuphand = 0; |
8723588e | 957 | |
30f05b19 MC |
958 | ssl3_cleanup_key_block(s); |
959 | ||
8723588e | 960 | if (s->server) { |
8723588e MC |
961 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); |
962 | ||
963 | s->ctx->stats.sess_accept_good++; | |
fe3a3291 | 964 | s->handshake_func = ossl_statem_accept; |
8723588e MC |
965 | } else { |
966 | ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); | |
967 | if (s->hit) | |
968 | s->ctx->stats.sess_hit++; | |
969 | ||
fe3a3291 | 970 | s->handshake_func = ossl_statem_connect; |
8723588e MC |
971 | s->ctx->stats.sess_connect_good++; |
972 | } | |
973 | ||
974 | if (s->info_callback != NULL) | |
975 | cb = s->info_callback; | |
976 | else if (s->ctx->info_callback != NULL) | |
977 | cb = s->ctx->info_callback; | |
978 | ||
979 | if (cb != NULL) | |
980 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); | |
981 | ||
982 | if (SSL_IS_DTLS(s)) { | |
983 | /* done with handshaking */ | |
984 | s->d1->handshake_read_seq = 0; | |
985 | s->d1->handshake_write_seq = 0; | |
986 | s->d1->next_handshake_write_seq = 0; | |
f5c7f5df | 987 | dtls1_clear_received_buffer(s); |
8723588e MC |
988 | } |
989 | } | |
990 | ||
30f05b19 MC |
991 | /* |
992 | * If we've not cleared the buffers its because we've got more work to do, | |
993 | * so continue. | |
994 | */ | |
995 | if (!clearbufs) | |
996 | return WORK_FINISHED_CONTINUE; | |
997 | ||
4004ce5f | 998 | ossl_statem_set_in_init(s, 0); |
8723588e MC |
999 | return WORK_FINISHED_STOP; |
1000 | } | |
1001 | ||
9ab930b2 MC |
1002 | int tls_get_message_header(SSL *s, int *mt) |
1003 | { | |
1004 | /* s->init_num < SSL3_HM_HEADER_LENGTH */ | |
1005 | int skip_message, i, recvd_type, al; | |
1006 | unsigned char *p; | |
54105ddd | 1007 | size_t l, readbytes; |
9ab930b2 MC |
1008 | |
1009 | p = (unsigned char *)s->init_buf->data; | |
1010 | ||
1011 | do { | |
1012 | while (s->init_num < SSL3_HM_HEADER_LENGTH) { | |
1013 | i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type, | |
a230b26e EK |
1014 | &p[s->init_num], |
1015 | SSL3_HM_HEADER_LENGTH - s->init_num, | |
54105ddd | 1016 | 0, &readbytes); |
9ab930b2 MC |
1017 | if (i <= 0) { |
1018 | s->rwstate = SSL_READING; | |
1019 | return 0; | |
32ec4153 | 1020 | } |
9ab930b2 | 1021 | if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) { |
1257adec | 1022 | /* |
a230b26e EK |
1023 | * A ChangeCipherSpec must be a single byte and may not occur |
1024 | * in the middle of a handshake message. | |
1025 | */ | |
54105ddd | 1026 | if (s->init_num != 0 || readbytes != 1 || p[0] != SSL3_MT_CCS) { |
1257adec DB |
1027 | al = SSL_AD_UNEXPECTED_MESSAGE; |
1028 | SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, | |
1029 | SSL_R_BAD_CHANGE_CIPHER_SPEC); | |
1030 | goto f_err; | |
1031 | } | |
9ab930b2 | 1032 | s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC; |
54105ddd | 1033 | s->init_num = readbytes - 1; |
c4377574 | 1034 | s->init_msg = s->init_buf->data; |
54105ddd | 1035 | s->s3->tmp.message_size = readbytes; |
9ab930b2 MC |
1036 | return 1; |
1037 | } else if (recvd_type != SSL3_RT_HANDSHAKE) { | |
1038 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
1039 | SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_CCS_RECEIVED_EARLY); | |
32ec4153 MC |
1040 | goto f_err; |
1041 | } | |
54105ddd | 1042 | s->init_num += readbytes; |
9ab930b2 MC |
1043 | } |
1044 | ||
1045 | skip_message = 0; | |
1046 | if (!s->server) | |
c7f47786 MC |
1047 | if (s->statem.hand_state != TLS_ST_OK |
1048 | && p[0] == SSL3_MT_HELLO_REQUEST) | |
9ab930b2 MC |
1049 | /* |
1050 | * The server may always send 'Hello Request' messages -- | |
1051 | * we are doing a handshake anyway now, so ignore them if | |
1052 | * their format is correct. Does not count for 'Finished' | |
1053 | * MAC. | |
1054 | */ | |
1055 | if (p[1] == 0 && p[2] == 0 && p[3] == 0) { | |
1056 | s->init_num = 0; | |
1057 | skip_message = 1; | |
1058 | ||
1059 | if (s->msg_callback) | |
1060 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, | |
1061 | p, SSL3_HM_HEADER_LENGTH, s, | |
1062 | s->msg_callback_arg); | |
1063 | } | |
1064 | } while (skip_message); | |
1065 | /* s->init_num == SSL3_HM_HEADER_LENGTH */ | |
1066 | ||
1067 | *mt = *p; | |
1068 | s->s3->tmp.message_type = *(p++); | |
32ec4153 | 1069 | |
e8aa8b6c | 1070 | if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) { |
9ab930b2 MC |
1071 | /* |
1072 | * Only happens with SSLv3+ in an SSLv2 backward compatible | |
1073 | * ClientHello | |
e8aa8b6c F |
1074 | * |
1075 | * Total message size is the remaining record bytes to read | |
1076 | * plus the SSL3_HM_HEADER_LENGTH bytes that we already read | |
9ab930b2 | 1077 | */ |
9ab930b2 MC |
1078 | l = RECORD_LAYER_get_rrec_length(&s->rlayer) |
1079 | + SSL3_HM_HEADER_LENGTH; | |
9ab930b2 MC |
1080 | s->s3->tmp.message_size = l; |
1081 | ||
1082 | s->init_msg = s->init_buf->data; | |
1083 | s->init_num = SSL3_HM_HEADER_LENGTH; | |
1084 | } else { | |
1085 | n2l3(p, l); | |
1086 | /* BUF_MEM_grow takes an 'int' parameter */ | |
1087 | if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) { | |
1088 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1089 | SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_EXCESSIVE_MESSAGE_SIZE); | |
1090 | goto f_err; | |
32ec4153 | 1091 | } |
9ab930b2 MC |
1092 | s->s3->tmp.message_size = l; |
1093 | ||
1094 | s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH; | |
1095 | s->init_num = 0; | |
1096 | } | |
1097 | ||
1098 | return 1; | |
1099 | f_err: | |
1100 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
9ab930b2 MC |
1101 | return 0; |
1102 | } | |
1103 | ||
eda75751 | 1104 | int tls_get_message_body(SSL *s, size_t *len) |
9ab930b2 | 1105 | { |
54105ddd | 1106 | size_t n, readbytes; |
9ab930b2 MC |
1107 | unsigned char *p; |
1108 | int i; | |
1109 | ||
1110 | if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) { | |
1111 | /* We've already read everything in */ | |
1112 | *len = (unsigned long)s->init_num; | |
1113 | return 1; | |
0f113f3e MC |
1114 | } |
1115 | ||
0f113f3e MC |
1116 | p = s->init_msg; |
1117 | n = s->s3->tmp.message_size - s->init_num; | |
1118 | while (n > 0) { | |
657da85e | 1119 | i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL, |
54105ddd | 1120 | &p[s->init_num], n, 0, &readbytes); |
0f113f3e MC |
1121 | if (i <= 0) { |
1122 | s->rwstate = SSL_READING; | |
9ab930b2 MC |
1123 | *len = 0; |
1124 | return 0; | |
0f113f3e | 1125 | } |
54105ddd MC |
1126 | s->init_num += readbytes; |
1127 | n -= readbytes; | |
0f113f3e | 1128 | } |
ee2ffc27 | 1129 | |
bf48836c | 1130 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
1131 | /* |
1132 | * If receiving Finished, record MAC of prior handshake messages for | |
1133 | * Finished verification. | |
1134 | */ | |
1135 | if (*s->init_buf->data == SSL3_MT_FINISHED) | |
1136 | ssl3_take_mac(s); | |
ee2ffc27 BL |
1137 | #endif |
1138 | ||
0f113f3e | 1139 | /* Feed this message into MAC computation. */ |
e8aa8b6c | 1140 | if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) { |
d166ed8c DSH |
1141 | if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, |
1142 | s->init_num)) { | |
1143 | SSLerr(SSL_F_TLS_GET_MESSAGE_BODY, ERR_R_EVP_LIB); | |
1144 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
1145 | *len = 0; | |
1146 | return 0; | |
1147 | } | |
32ec4153 | 1148 | if (s->msg_callback) |
a230b26e | 1149 | s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data, |
32ec4153 MC |
1150 | (size_t)s->init_num, s, s->msg_callback_arg); |
1151 | } else { | |
11c67eea MC |
1152 | /* |
1153 | * We defer feeding in the HRR until later. We'll do it as part of | |
1154 | * processing the message | |
1155 | */ | |
1156 | if (s->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST | |
1157 | && !ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, | |
1158 | s->init_num + SSL3_HM_HEADER_LENGTH)) { | |
d166ed8c DSH |
1159 | SSLerr(SSL_F_TLS_GET_MESSAGE_BODY, ERR_R_EVP_LIB); |
1160 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
1161 | *len = 0; | |
1162 | return 0; | |
1163 | } | |
32ec4153 MC |
1164 | if (s->msg_callback) |
1165 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data, | |
1166 | (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s, | |
1167 | s->msg_callback_arg); | |
1168 | } | |
1169 | ||
eda75751 | 1170 | *len = s->init_num; |
9ab930b2 | 1171 | return 1; |
0f113f3e | 1172 | } |
d02b48c6 | 1173 | |
2e5ead83 | 1174 | int ssl_cert_type(const X509 *x, const EVP_PKEY *pk) |
0f113f3e | 1175 | { |
a230b26e | 1176 | if (pk == NULL && (pk = X509_get0_pubkey(x)) == NULL) |
17a72388 VD |
1177 | return -1; |
1178 | ||
1179 | switch (EVP_PKEY_id(pk)) { | |
1180 | default: | |
1181 | return -1; | |
1182 | case EVP_PKEY_RSA: | |
d0ff28f8 | 1183 | return SSL_PKEY_RSA; |
17a72388 VD |
1184 | case EVP_PKEY_DSA: |
1185 | return SSL_PKEY_DSA_SIGN; | |
ea262260 | 1186 | #ifndef OPENSSL_NO_EC |
17a72388 VD |
1187 | case EVP_PKEY_EC: |
1188 | return SSL_PKEY_ECC; | |
ea262260 | 1189 | #endif |
2a9b9654 | 1190 | #ifndef OPENSSL_NO_GOST |
17a72388 VD |
1191 | case NID_id_GostR3410_2001: |
1192 | return SSL_PKEY_GOST01; | |
1193 | case NID_id_GostR3410_2012_256: | |
1194 | return SSL_PKEY_GOST12_256; | |
1195 | case NID_id_GostR3410_2012_512: | |
1196 | return SSL_PKEY_GOST12_512; | |
2a9b9654 | 1197 | #endif |
82049c54 | 1198 | } |
0f113f3e | 1199 | } |
d02b48c6 | 1200 | |
6b691a5c | 1201 | int ssl_verify_alarm_type(long type) |
0f113f3e MC |
1202 | { |
1203 | int al; | |
1204 | ||
1205 | switch (type) { | |
1206 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: | |
1207 | case X509_V_ERR_UNABLE_TO_GET_CRL: | |
1208 | case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: | |
1209 | al = SSL_AD_UNKNOWN_CA; | |
1210 | break; | |
1211 | case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: | |
1212 | case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: | |
1213 | case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: | |
1214 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: | |
1215 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: | |
1216 | case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: | |
1217 | case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: | |
1218 | case X509_V_ERR_CERT_NOT_YET_VALID: | |
1219 | case X509_V_ERR_CRL_NOT_YET_VALID: | |
1220 | case X509_V_ERR_CERT_UNTRUSTED: | |
1221 | case X509_V_ERR_CERT_REJECTED: | |
f3e235ed VD |
1222 | case X509_V_ERR_HOSTNAME_MISMATCH: |
1223 | case X509_V_ERR_EMAIL_MISMATCH: | |
1224 | case X509_V_ERR_IP_ADDRESS_MISMATCH: | |
1225 | case X509_V_ERR_DANE_NO_MATCH: | |
1226 | case X509_V_ERR_EE_KEY_TOO_SMALL: | |
1227 | case X509_V_ERR_CA_KEY_TOO_SMALL: | |
1228 | case X509_V_ERR_CA_MD_TOO_WEAK: | |
0f113f3e MC |
1229 | al = SSL_AD_BAD_CERTIFICATE; |
1230 | break; | |
1231 | case X509_V_ERR_CERT_SIGNATURE_FAILURE: | |
1232 | case X509_V_ERR_CRL_SIGNATURE_FAILURE: | |
1233 | al = SSL_AD_DECRYPT_ERROR; | |
1234 | break; | |
1235 | case X509_V_ERR_CERT_HAS_EXPIRED: | |
1236 | case X509_V_ERR_CRL_HAS_EXPIRED: | |
1237 | al = SSL_AD_CERTIFICATE_EXPIRED; | |
1238 | break; | |
1239 | case X509_V_ERR_CERT_REVOKED: | |
1240 | al = SSL_AD_CERTIFICATE_REVOKED; | |
1241 | break; | |
f3e235ed | 1242 | case X509_V_ERR_UNSPECIFIED: |
0f113f3e | 1243 | case X509_V_ERR_OUT_OF_MEM: |
f3e235ed VD |
1244 | case X509_V_ERR_INVALID_CALL: |
1245 | case X509_V_ERR_STORE_LOOKUP: | |
0f113f3e MC |
1246 | al = SSL_AD_INTERNAL_ERROR; |
1247 | break; | |
1248 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: | |
1249 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: | |
1250 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: | |
1251 | case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: | |
1252 | case X509_V_ERR_CERT_CHAIN_TOO_LONG: | |
1253 | case X509_V_ERR_PATH_LENGTH_EXCEEDED: | |
1254 | case X509_V_ERR_INVALID_CA: | |
1255 | al = SSL_AD_UNKNOWN_CA; | |
1256 | break; | |
1257 | case X509_V_ERR_APPLICATION_VERIFICATION: | |
1258 | al = SSL_AD_HANDSHAKE_FAILURE; | |
1259 | break; | |
1260 | case X509_V_ERR_INVALID_PURPOSE: | |
1261 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; | |
1262 | break; | |
1263 | default: | |
1264 | al = SSL_AD_CERTIFICATE_UNKNOWN; | |
1265 | break; | |
1266 | } | |
1267 | return (al); | |
1268 | } | |
d02b48c6 | 1269 | |
b362ccab | 1270 | int ssl_allow_compression(SSL *s) |
0f113f3e MC |
1271 | { |
1272 | if (s->options & SSL_OP_NO_COMPRESSION) | |
1273 | return 0; | |
1274 | return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL); | |
1275 | } | |
4fa52141 | 1276 | |
068c358a | 1277 | static int version_cmp(const SSL *s, int a, int b) |
4fa52141 VD |
1278 | { |
1279 | int dtls = SSL_IS_DTLS(s); | |
1280 | ||
1281 | if (a == b) | |
1282 | return 0; | |
1283 | if (!dtls) | |
1284 | return a < b ? -1 : 1; | |
1285 | return DTLS_VERSION_LT(a, b) ? -1 : 1; | |
1286 | } | |
1287 | ||
1288 | typedef struct { | |
1289 | int version; | |
a230b26e EK |
1290 | const SSL_METHOD *(*cmeth) (void); |
1291 | const SSL_METHOD *(*smeth) (void); | |
4fa52141 VD |
1292 | } version_info; |
1293 | ||
582a17d6 MC |
1294 | #if TLS_MAX_VERSION != TLS1_3_VERSION |
1295 | # error Code needs update for TLS_method() support beyond TLS1_3_VERSION. | |
4fa52141 VD |
1296 | #endif |
1297 | ||
f7f2a01d | 1298 | /* Must be in order high to low */ |
4fa52141 | 1299 | static const version_info tls_version_table[] = { |
582a17d6 MC |
1300 | #ifndef OPENSSL_NO_TLS1_3 |
1301 | {TLS1_3_VERSION, tlsv1_3_client_method, tlsv1_3_server_method}, | |
1302 | #else | |
1303 | {TLS1_3_VERSION, NULL, NULL}, | |
1304 | #endif | |
6b01bed2 | 1305 | #ifndef OPENSSL_NO_TLS1_2 |
a230b26e | 1306 | {TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method}, |
6b01bed2 | 1307 | #else |
a230b26e | 1308 | {TLS1_2_VERSION, NULL, NULL}, |
6b01bed2 VD |
1309 | #endif |
1310 | #ifndef OPENSSL_NO_TLS1_1 | |
a230b26e | 1311 | {TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method}, |
6b01bed2 | 1312 | #else |
a230b26e | 1313 | {TLS1_1_VERSION, NULL, NULL}, |
6b01bed2 VD |
1314 | #endif |
1315 | #ifndef OPENSSL_NO_TLS1 | |
a230b26e | 1316 | {TLS1_VERSION, tlsv1_client_method, tlsv1_server_method}, |
6b01bed2 | 1317 | #else |
a230b26e | 1318 | {TLS1_VERSION, NULL, NULL}, |
6b01bed2 | 1319 | #endif |
4fa52141 | 1320 | #ifndef OPENSSL_NO_SSL3 |
a230b26e | 1321 | {SSL3_VERSION, sslv3_client_method, sslv3_server_method}, |
6b01bed2 | 1322 | #else |
a230b26e | 1323 | {SSL3_VERSION, NULL, NULL}, |
4fa52141 | 1324 | #endif |
a230b26e | 1325 | {0, NULL, NULL}, |
4fa52141 VD |
1326 | }; |
1327 | ||
1328 | #if DTLS_MAX_VERSION != DTLS1_2_VERSION | |
1329 | # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION. | |
1330 | #endif | |
1331 | ||
f7f2a01d | 1332 | /* Must be in order high to low */ |
4fa52141 | 1333 | static const version_info dtls_version_table[] = { |
6b01bed2 | 1334 | #ifndef OPENSSL_NO_DTLS1_2 |
a230b26e | 1335 | {DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method}, |
6b01bed2 | 1336 | #else |
a230b26e | 1337 | {DTLS1_2_VERSION, NULL, NULL}, |
6b01bed2 VD |
1338 | #endif |
1339 | #ifndef OPENSSL_NO_DTLS1 | |
a230b26e EK |
1340 | {DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method}, |
1341 | {DTLS1_BAD_VER, dtls_bad_ver_client_method, NULL}, | |
6b01bed2 | 1342 | #else |
a230b26e EK |
1343 | {DTLS1_VERSION, NULL, NULL}, |
1344 | {DTLS1_BAD_VER, NULL, NULL}, | |
6b01bed2 | 1345 | #endif |
a230b26e | 1346 | {0, NULL, NULL}, |
4fa52141 VD |
1347 | }; |
1348 | ||
1349 | /* | |
1350 | * ssl_method_error - Check whether an SSL_METHOD is enabled. | |
1351 | * | |
1352 | * @s: The SSL handle for the candidate method | |
1353 | * @method: the intended method. | |
1354 | * | |
1355 | * Returns 0 on success, or an SSL error reason on failure. | |
1356 | */ | |
068c358a | 1357 | static int ssl_method_error(const SSL *s, const SSL_METHOD *method) |
4fa52141 VD |
1358 | { |
1359 | int version = method->version; | |
1360 | ||
1361 | if ((s->min_proto_version != 0 && | |
1362 | version_cmp(s, version, s->min_proto_version) < 0) || | |
1363 | ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0) | |
1364 | return SSL_R_VERSION_TOO_LOW; | |
1365 | ||
1366 | if (s->max_proto_version != 0 && | |
a230b26e | 1367 | version_cmp(s, version, s->max_proto_version) > 0) |
4fa52141 VD |
1368 | return SSL_R_VERSION_TOO_HIGH; |
1369 | ||
1370 | if ((s->options & method->mask) != 0) | |
1371 | return SSL_R_UNSUPPORTED_PROTOCOL; | |
1372 | if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s)) | |
1373 | return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE; | |
4fa52141 VD |
1374 | |
1375 | return 0; | |
1376 | } | |
1377 | ||
ccae4a15 FI |
1378 | /* |
1379 | * ssl_version_supported - Check that the specified `version` is supported by | |
1380 | * `SSL *` instance | |
1381 | * | |
1382 | * @s: The SSL handle for the candidate method | |
1383 | * @version: Protocol version to test against | |
1384 | * | |
1385 | * Returns 1 when supported, otherwise 0 | |
1386 | */ | |
1387 | int ssl_version_supported(const SSL *s, int version) | |
1388 | { | |
1389 | const version_info *vent; | |
1390 | const version_info *table; | |
1391 | ||
1392 | switch (s->method->version) { | |
1393 | default: | |
1394 | /* Version should match method version for non-ANY method */ | |
1395 | return version_cmp(s, version, s->version) == 0; | |
1396 | case TLS_ANY_VERSION: | |
1397 | table = tls_version_table; | |
1398 | break; | |
1399 | case DTLS_ANY_VERSION: | |
1400 | table = dtls_version_table; | |
1401 | break; | |
1402 | } | |
1403 | ||
1404 | for (vent = table; | |
1405 | vent->version != 0 && version_cmp(s, version, vent->version) <= 0; | |
1406 | ++vent) { | |
1407 | if (vent->cmeth != NULL && | |
1408 | version_cmp(s, version, vent->version) == 0 && | |
1409 | ssl_method_error(s, vent->cmeth()) == 0) { | |
1410 | return 1; | |
1411 | } | |
1412 | } | |
1413 | return 0; | |
1414 | } | |
1415 | ||
4fa52141 VD |
1416 | /* |
1417 | * ssl_check_version_downgrade - In response to RFC7507 SCSV version | |
1418 | * fallback indication from a client check whether we're using the highest | |
1419 | * supported protocol version. | |
1420 | * | |
1421 | * @s server SSL handle. | |
1422 | * | |
1423 | * Returns 1 when using the highest enabled version, 0 otherwise. | |
1424 | */ | |
1425 | int ssl_check_version_downgrade(SSL *s) | |
1426 | { | |
1427 | const version_info *vent; | |
1428 | const version_info *table; | |
1429 | ||
1430 | /* | |
1431 | * Check that the current protocol is the highest enabled version | |
1432 | * (according to s->ctx->method, as version negotiation may have changed | |
1433 | * s->method). | |
1434 | */ | |
1435 | if (s->version == s->ctx->method->version) | |
1436 | return 1; | |
1437 | ||
1438 | /* | |
1439 | * Apparently we're using a version-flexible SSL_METHOD (not at its | |
1440 | * highest protocol version). | |
1441 | */ | |
1442 | if (s->ctx->method->version == TLS_method()->version) | |
1443 | table = tls_version_table; | |
1444 | else if (s->ctx->method->version == DTLS_method()->version) | |
1445 | table = dtls_version_table; | |
1446 | else { | |
1447 | /* Unexpected state; fail closed. */ | |
1448 | return 0; | |
1449 | } | |
1450 | ||
1451 | for (vent = table; vent->version != 0; ++vent) { | |
a230b26e | 1452 | if (vent->smeth != NULL && ssl_method_error(s, vent->smeth()) == 0) |
4fa52141 VD |
1453 | return s->version == vent->version; |
1454 | } | |
1455 | return 0; | |
1456 | } | |
1457 | ||
1458 | /* | |
1459 | * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS | |
1460 | * protocols, provided the initial (D)TLS method is version-flexible. This | |
1461 | * function sanity-checks the proposed value and makes sure the method is | |
1462 | * version-flexible, then sets the limit if all is well. | |
1463 | * | |
1464 | * @method_version: The version of the current SSL_METHOD. | |
1465 | * @version: the intended limit. | |
1466 | * @bound: pointer to limit to be updated. | |
1467 | * | |
1468 | * Returns 1 on success, 0 on failure. | |
1469 | */ | |
1470 | int ssl_set_version_bound(int method_version, int version, int *bound) | |
1471 | { | |
869e978c KR |
1472 | if (version == 0) { |
1473 | *bound = version; | |
1474 | return 1; | |
1475 | } | |
1476 | ||
4fa52141 VD |
1477 | /*- |
1478 | * Restrict TLS methods to TLS protocol versions. | |
1479 | * Restrict DTLS methods to DTLS protocol versions. | |
1480 | * Note, DTLS version numbers are decreasing, use comparison macros. | |
1481 | * | |
1482 | * Note that for both lower-bounds we use explicit versions, not | |
1483 | * (D)TLS_MIN_VERSION. This is because we don't want to break user | |
1484 | * configurations. If the MIN (supported) version ever rises, the user's | |
1485 | * "floor" remains valid even if no longer available. We don't expect the | |
1486 | * MAX ceiling to ever get lower, so making that variable makes sense. | |
1487 | */ | |
1488 | switch (method_version) { | |
1489 | default: | |
1490 | /* | |
1491 | * XXX For fixed version methods, should we always fail and not set any | |
1492 | * bounds, always succeed and not set any bounds, or set the bounds and | |
1493 | * arrange to fail later if they are not met? At present fixed-version | |
1494 | * methods are not subject to controls that disable individual protocol | |
1495 | * versions. | |
1496 | */ | |
1497 | return 0; | |
1498 | ||
1499 | case TLS_ANY_VERSION: | |
1500 | if (version < SSL3_VERSION || version > TLS_MAX_VERSION) | |
1501 | return 0; | |
1502 | break; | |
1503 | ||
1504 | case DTLS_ANY_VERSION: | |
1505 | if (DTLS_VERSION_GT(version, DTLS_MAX_VERSION) || | |
032924c4 | 1506 | DTLS_VERSION_LT(version, DTLS1_BAD_VER)) |
4fa52141 VD |
1507 | return 0; |
1508 | break; | |
1509 | } | |
1510 | ||
1511 | *bound = version; | |
1512 | return 1; | |
1513 | } | |
1514 | ||
f7f2a01d MC |
1515 | static void check_for_downgrade(SSL *s, int vers, DOWNGRADE *dgrd) |
1516 | { | |
1517 | if (vers == TLS1_2_VERSION | |
1518 | && ssl_version_supported(s, TLS1_3_VERSION)) { | |
1519 | *dgrd = DOWNGRADE_TO_1_2; | |
1520 | } else if (!SSL_IS_DTLS(s) && vers < TLS1_2_VERSION | |
1521 | && (ssl_version_supported(s, TLS1_2_VERSION) | |
1522 | || ssl_version_supported(s, TLS1_3_VERSION))) { | |
1523 | *dgrd = DOWNGRADE_TO_1_1; | |
1524 | } else { | |
1525 | *dgrd = DOWNGRADE_NONE; | |
1526 | } | |
1527 | } | |
1528 | ||
4fa52141 VD |
1529 | /* |
1530 | * ssl_choose_server_version - Choose server (D)TLS version. Called when the | |
1531 | * client HELLO is received to select the final server protocol version and | |
1532 | * the version specific method. | |
1533 | * | |
1534 | * @s: server SSL handle. | |
1535 | * | |
1536 | * Returns 0 on success or an SSL error reason number on failure. | |
1537 | */ | |
f7f2a01d | 1538 | int ssl_choose_server_version(SSL *s, CLIENTHELLO_MSG *hello, DOWNGRADE *dgrd) |
4fa52141 VD |
1539 | { |
1540 | /*- | |
1541 | * With version-flexible methods we have an initial state with: | |
1542 | * | |
1543 | * s->method->version == (D)TLS_ANY_VERSION, | |
1544 | * s->version == (D)TLS_MAX_VERSION. | |
1545 | * | |
1546 | * So we detect version-flexible methods via the method version, not the | |
1547 | * handle version. | |
1548 | */ | |
1549 | int server_version = s->method->version; | |
df7ce507 | 1550 | int client_version = hello->legacy_version; |
4fa52141 VD |
1551 | const version_info *vent; |
1552 | const version_info *table; | |
1553 | int disabled = 0; | |
cd998837 | 1554 | RAW_EXTENSION *suppversions; |
4fa52141 | 1555 | |
1ab3836b MC |
1556 | s->client_version = client_version; |
1557 | ||
4fa52141 VD |
1558 | switch (server_version) { |
1559 | default: | |
7d061fce MC |
1560 | if (!SSL_IS_TLS13(s)) { |
1561 | if (version_cmp(s, client_version, s->version) < 0) | |
1562 | return SSL_R_WRONG_SSL_VERSION; | |
f7f2a01d | 1563 | *dgrd = DOWNGRADE_NONE; |
7d061fce MC |
1564 | /* |
1565 | * If this SSL handle is not from a version flexible method we don't | |
1566 | * (and never did) check min/max FIPS or Suite B constraints. Hope | |
1567 | * that's OK. It is up to the caller to not choose fixed protocol | |
1568 | * versions they don't want. If not, then easy to fix, just return | |
1569 | * ssl_method_error(s, s->method) | |
1570 | */ | |
1571 | return 0; | |
1572 | } | |
d2f42576 | 1573 | /* |
7d061fce MC |
1574 | * Fall through if we are TLSv1.3 already (this means we must be after |
1575 | * a HelloRetryRequest | |
4fa52141 | 1576 | */ |
4fa52141 VD |
1577 | case TLS_ANY_VERSION: |
1578 | table = tls_version_table; | |
1579 | break; | |
1580 | case DTLS_ANY_VERSION: | |
1581 | table = dtls_version_table; | |
1582 | break; | |
1583 | } | |
1584 | ||
70af3d8e | 1585 | suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions]; |
cd998837 | 1586 | |
70af3d8e | 1587 | if (suppversions->present && !SSL_IS_DTLS(s)) { |
cd998837 MC |
1588 | unsigned int candidate_vers = 0; |
1589 | unsigned int best_vers = 0; | |
1590 | const SSL_METHOD *best_method = NULL; | |
1591 | PACKET versionslist; | |
1592 | ||
6b473aca MC |
1593 | suppversions->parsed = 1; |
1594 | ||
16bce0e0 | 1595 | if (!PACKET_as_length_prefixed_1(&suppversions->data, &versionslist)) { |
cd998837 MC |
1596 | /* Trailing or invalid data? */ |
1597 | return SSL_R_LENGTH_MISMATCH; | |
1598 | } | |
1599 | ||
1600 | while (PACKET_get_net_2(&versionslist, &candidate_vers)) { | |
1601 | /* TODO(TLS1.3): Remove this before release */ | |
1602 | if (candidate_vers == TLS1_3_VERSION_DRAFT) | |
1603 | candidate_vers = TLS1_3_VERSION; | |
f2342b7a MC |
1604 | /* |
1605 | * TODO(TLS1.3): There is some discussion on the TLS list about | |
1ee4b98e | 1606 | * whether to ignore versions <TLS1.2 in supported_versions. At the |
f2342b7a MC |
1607 | * moment we honour them if present. To be reviewed later |
1608 | */ | |
cd998837 MC |
1609 | if (version_cmp(s, candidate_vers, best_vers) <= 0) |
1610 | continue; | |
1611 | for (vent = table; | |
1612 | vent->version != 0 && vent->version != (int)candidate_vers; | |
16bce0e0 | 1613 | ++vent) |
bf0ba5e7 | 1614 | continue; |
bf85ef1b | 1615 | if (vent->version != 0 && vent->smeth != NULL) { |
cd998837 MC |
1616 | const SSL_METHOD *method; |
1617 | ||
1618 | method = vent->smeth(); | |
1619 | if (ssl_method_error(s, method) == 0) { | |
1620 | best_vers = candidate_vers; | |
1621 | best_method = method; | |
1622 | } | |
1623 | } | |
1624 | } | |
1625 | if (PACKET_remaining(&versionslist) != 0) { | |
1626 | /* Trailing data? */ | |
1627 | return SSL_R_LENGTH_MISMATCH; | |
1628 | } | |
1629 | ||
1630 | if (best_vers > 0) { | |
7d061fce MC |
1631 | if (SSL_IS_TLS13(s)) { |
1632 | /* | |
1633 | * We get here if this is after a HelloRetryRequest. In this | |
1634 | * case we just check that we still negotiated TLSv1.3 | |
1635 | */ | |
1636 | if (best_vers != TLS1_3_VERSION) | |
1637 | return SSL_R_UNSUPPORTED_PROTOCOL; | |
1638 | return 0; | |
1639 | } | |
f7f2a01d | 1640 | check_for_downgrade(s, best_vers, dgrd); |
cd998837 MC |
1641 | s->version = best_vers; |
1642 | s->method = best_method; | |
1643 | return 0; | |
1644 | } | |
1645 | return SSL_R_UNSUPPORTED_PROTOCOL; | |
1646 | } | |
1647 | ||
1648 | /* | |
1649 | * If the supported versions extension isn't present, then the highest | |
1650 | * version we can negotiate is TLSv1.2 | |
1651 | */ | |
1652 | if (version_cmp(s, client_version, TLS1_3_VERSION) >= 0) | |
1653 | client_version = TLS1_2_VERSION; | |
1654 | ||
1655 | /* | |
1656 | * No supported versions extension, so we just use the version supplied in | |
1657 | * the ClientHello. | |
1658 | */ | |
4fa52141 VD |
1659 | for (vent = table; vent->version != 0; ++vent) { |
1660 | const SSL_METHOD *method; | |
1661 | ||
1662 | if (vent->smeth == NULL || | |
1663 | version_cmp(s, client_version, vent->version) < 0) | |
1664 | continue; | |
1665 | method = vent->smeth(); | |
1666 | if (ssl_method_error(s, method) == 0) { | |
f7f2a01d | 1667 | check_for_downgrade(s, vent->version, dgrd); |
4fa52141 VD |
1668 | s->version = vent->version; |
1669 | s->method = method; | |
1670 | return 0; | |
1671 | } | |
1672 | disabled = 1; | |
1673 | } | |
1674 | return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW; | |
1675 | } | |
1676 | ||
1677 | /* | |
1678 | * ssl_choose_client_version - Choose client (D)TLS version. Called when the | |
1679 | * server HELLO is received to select the final client protocol version and | |
1680 | * the version specific method. | |
1681 | * | |
1682 | * @s: client SSL handle. | |
1683 | * @version: The proposed version from the server's HELLO. | |
c3043dcd MC |
1684 | * @checkdgrd: Whether to check the downgrade sentinels in the server_random |
1685 | * @al: Where to store any alert value that may be generated | |
4fa52141 VD |
1686 | * |
1687 | * Returns 0 on success or an SSL error reason number on failure. | |
1688 | */ | |
c3043dcd | 1689 | int ssl_choose_client_version(SSL *s, int version, int checkdgrd, int *al) |
4fa52141 VD |
1690 | { |
1691 | const version_info *vent; | |
1692 | const version_info *table; | |
c3043dcd | 1693 | int highver = 0; |
4fa52141 | 1694 | |
b97667ce MC |
1695 | /* TODO(TLS1.3): Remove this before release */ |
1696 | if (version == TLS1_3_VERSION_DRAFT) | |
1697 | version = TLS1_3_VERSION; | |
1698 | ||
c3043dcd MC |
1699 | if (s->hello_retry_request && version != TLS1_3_VERSION) { |
1700 | *al = SSL_AD_PROTOCOL_VERSION; | |
1701 | return SSL_R_WRONG_SSL_VERSION; | |
1702 | } | |
1703 | ||
4fa52141 VD |
1704 | switch (s->method->version) { |
1705 | default: | |
c3043dcd MC |
1706 | if (version != s->version) { |
1707 | *al = SSL_AD_PROTOCOL_VERSION; | |
4fa52141 | 1708 | return SSL_R_WRONG_SSL_VERSION; |
c3043dcd | 1709 | } |
4fa52141 VD |
1710 | /* |
1711 | * If this SSL handle is not from a version flexible method we don't | |
1712 | * (and never did) check min/max, FIPS or Suite B constraints. Hope | |
1713 | * that's OK. It is up to the caller to not choose fixed protocol | |
1714 | * versions they don't want. If not, then easy to fix, just return | |
1715 | * ssl_method_error(s, s->method) | |
1716 | */ | |
4fa52141 VD |
1717 | return 0; |
1718 | case TLS_ANY_VERSION: | |
1719 | table = tls_version_table; | |
1720 | break; | |
1721 | case DTLS_ANY_VERSION: | |
1722 | table = dtls_version_table; | |
1723 | break; | |
1724 | } | |
1725 | ||
1726 | for (vent = table; vent->version != 0; ++vent) { | |
1727 | const SSL_METHOD *method; | |
1728 | int err; | |
1729 | ||
4fa52141 | 1730 | if (vent->cmeth == NULL) |
c3043dcd MC |
1731 | continue; |
1732 | ||
1733 | if (highver != 0 && version != vent->version) | |
1734 | continue; | |
3847d426 | 1735 | |
4fa52141 VD |
1736 | method = vent->cmeth(); |
1737 | err = ssl_method_error(s, method); | |
c3043dcd MC |
1738 | if (err != 0) { |
1739 | if (version == vent->version) { | |
1740 | *al = SSL_AD_PROTOCOL_VERSION; | |
1741 | return err; | |
1742 | } | |
1743 | ||
1744 | continue; | |
1745 | } | |
1746 | if (highver == 0) | |
1747 | highver = vent->version; | |
1748 | ||
1749 | if (version != vent->version) | |
1750 | continue; | |
1751 | ||
1752 | #ifndef OPENSSL_NO_TLS13DOWNGRADE | |
1753 | /* Check for downgrades */ | |
1754 | if (checkdgrd) { | |
1755 | if (version == TLS1_2_VERSION && highver > version) { | |
1756 | if (memcmp(tls12downgrade, | |
1757 | s->s3->server_random + SSL3_RANDOM_SIZE | |
1758 | - sizeof(tls12downgrade), | |
1759 | sizeof(tls12downgrade)) == 0) { | |
1760 | *al = SSL_AD_ILLEGAL_PARAMETER; | |
1761 | return SSL_R_INAPPROPRIATE_FALLBACK; | |
1762 | } | |
1763 | } else if (!SSL_IS_DTLS(s) | |
1764 | && version < TLS1_2_VERSION | |
1765 | && highver > version) { | |
1766 | if (memcmp(tls11downgrade, | |
1767 | s->s3->server_random + SSL3_RANDOM_SIZE | |
1768 | - sizeof(tls11downgrade), | |
1769 | sizeof(tls11downgrade)) == 0) { | |
1770 | *al = SSL_AD_ILLEGAL_PARAMETER; | |
1771 | return SSL_R_INAPPROPRIATE_FALLBACK; | |
1772 | } | |
1773 | } | |
1774 | } | |
1775 | #endif | |
1776 | ||
4fa52141 | 1777 | s->method = method; |
ccae4a15 | 1778 | s->version = version; |
4fa52141 VD |
1779 | return 0; |
1780 | } | |
1781 | ||
c3043dcd | 1782 | *al = SSL_AD_PROTOCOL_VERSION; |
4fa52141 VD |
1783 | return SSL_R_UNSUPPORTED_PROTOCOL; |
1784 | } | |
1785 | ||
068c358a KR |
1786 | /* |
1787 | * ssl_get_client_min_max_version - get minimum and maximum client version | |
1788 | * @s: The SSL connection | |
1789 | * @min_version: The minimum supported version | |
1790 | * @max_version: The maximum supported version | |
1791 | * | |
1792 | * Work out what version we should be using for the initial ClientHello if the | |
1793 | * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx | |
1794 | * options, the MinProtocol and MaxProtocol configuration commands, any Suite B | |
b53338cb | 1795 | * constraints and any floor imposed by the security level here, |
068c358a | 1796 | * so we don't advertise the wrong protocol version to only reject the outcome later. |
4fa52141 | 1797 | * |
0485d540 | 1798 | * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled, |
4fa52141 VD |
1799 | * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol |
1800 | * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1. | |
1801 | * | |
068c358a KR |
1802 | * Returns 0 on success or an SSL error reason number on failure. On failure |
1803 | * min_version and max_version will also be set to 0. | |
4fa52141 | 1804 | */ |
a230b26e EK |
1805 | int ssl_get_client_min_max_version(const SSL *s, int *min_version, |
1806 | int *max_version) | |
4fa52141 VD |
1807 | { |
1808 | int version; | |
1809 | int hole; | |
1810 | const SSL_METHOD *single = NULL; | |
1811 | const SSL_METHOD *method; | |
1812 | const version_info *table; | |
1813 | const version_info *vent; | |
1814 | ||
1815 | switch (s->method->version) { | |
1816 | default: | |
1817 | /* | |
1818 | * If this SSL handle is not from a version flexible method we don't | |
1819 | * (and never did) check min/max FIPS or Suite B constraints. Hope | |
1820 | * that's OK. It is up to the caller to not choose fixed protocol | |
1821 | * versions they don't want. If not, then easy to fix, just return | |
1822 | * ssl_method_error(s, s->method) | |
1823 | */ | |
068c358a | 1824 | *min_version = *max_version = s->version; |
4fa52141 VD |
1825 | return 0; |
1826 | case TLS_ANY_VERSION: | |
1827 | table = tls_version_table; | |
1828 | break; | |
1829 | case DTLS_ANY_VERSION: | |
1830 | table = dtls_version_table; | |
1831 | break; | |
1832 | } | |
1833 | ||
1834 | /* | |
1835 | * SSL_OP_NO_X disables all protocols above X *if* there are some protocols | |
1836 | * below X enabled. This is required in order to maintain the "version | |
1837 | * capability" vector contiguous. Any versions with a NULL client method | |
1838 | * (protocol version client is disabled at compile-time) is also a "hole". | |
1839 | * | |
1840 | * Our initial state is hole == 1, version == 0. That is, versions above | |
1841 | * the first version in the method table are disabled (a "hole" above | |
1842 | * the valid protocol entries) and we don't have a selected version yet. | |
1843 | * | |
1844 | * Whenever "hole == 1", and we hit an enabled method, its version becomes | |
1845 | * the selected version, and the method becomes a candidate "single" | |
1846 | * method. We're no longer in a hole, so "hole" becomes 0. | |
1847 | * | |
1848 | * If "hole == 0" and we hit an enabled method, then "single" is cleared, | |
1849 | * as we support a contiguous range of at least two methods. If we hit | |
1850 | * a disabled method, then hole becomes true again, but nothing else | |
1851 | * changes yet, because all the remaining methods may be disabled too. | |
1852 | * If we again hit an enabled method after the new hole, it becomes | |
1853 | * selected, as we start from scratch. | |
1854 | */ | |
068c358a | 1855 | *min_version = version = 0; |
4fa52141 VD |
1856 | hole = 1; |
1857 | for (vent = table; vent->version != 0; ++vent) { | |
1858 | /* | |
1859 | * A table entry with a NULL client method is still a hole in the | |
1860 | * "version capability" vector. | |
1861 | */ | |
1862 | if (vent->cmeth == NULL) { | |
1863 | hole = 1; | |
1864 | continue; | |
1865 | } | |
1866 | method = vent->cmeth(); | |
1867 | if (ssl_method_error(s, method) != 0) { | |
1868 | hole = 1; | |
1869 | } else if (!hole) { | |
1870 | single = NULL; | |
068c358a | 1871 | *min_version = method->version; |
4fa52141 VD |
1872 | } else { |
1873 | version = (single = method)->version; | |
068c358a | 1874 | *min_version = version; |
4fa52141 VD |
1875 | hole = 0; |
1876 | } | |
1877 | } | |
1878 | ||
068c358a KR |
1879 | *max_version = version; |
1880 | ||
4fa52141 VD |
1881 | /* Fail if everything is disabled */ |
1882 | if (version == 0) | |
1883 | return SSL_R_NO_PROTOCOLS_AVAILABLE; | |
1884 | ||
068c358a KR |
1885 | return 0; |
1886 | } | |
1887 | ||
1888 | /* | |
1889 | * ssl_set_client_hello_version - Work out what version we should be using for | |
7acb8b64 | 1890 | * the initial ClientHello.legacy_version field. |
068c358a KR |
1891 | * |
1892 | * @s: client SSL handle. | |
1893 | * | |
1894 | * Returns 0 on success or an SSL error reason number on failure. | |
1895 | */ | |
1896 | int ssl_set_client_hello_version(SSL *s) | |
1897 | { | |
3eb2aff4 | 1898 | int ver_min, ver_max, ret; |
068c358a | 1899 | |
3eb2aff4 | 1900 | ret = ssl_get_client_min_max_version(s, &ver_min, &ver_max); |
068c358a KR |
1901 | |
1902 | if (ret != 0) | |
1903 | return ret; | |
1904 | ||
7acb8b64 MC |
1905 | s->version = ver_max; |
1906 | ||
1907 | /* TLS1.3 always uses TLS1.2 in the legacy_version field */ | |
1908 | if (!SSL_IS_DTLS(s) && ver_max > TLS1_2_VERSION) | |
1909 | ver_max = TLS1_2_VERSION; | |
1910 | ||
1911 | s->client_version = ver_max; | |
4fa52141 VD |
1912 | return 0; |
1913 | } | |
aff9929b MC |
1914 | |
1915 | /* | |
1916 | * Checks a list of |groups| to determine if the |group_id| is in it. If it is | |
1917 | * and |checkallow| is 1 then additionally check if the group is allowed to be | |
1918 | * used. Returns 1 if the group is in the list (and allowed if |checkallow| is | |
1919 | * 1) or 0 otherwise. | |
1920 | */ | |
deb2d5e7 | 1921 | #ifndef OPENSSL_NO_EC |
aff9929b MC |
1922 | int check_in_list(SSL *s, unsigned int group_id, const unsigned char *groups, |
1923 | size_t num_groups, int checkallow) | |
1924 | { | |
1925 | size_t i; | |
1926 | ||
1927 | if (groups == NULL || num_groups == 0) | |
1928 | return 0; | |
1929 | ||
1930 | for (i = 0; i < num_groups; i++, groups += 2) { | |
1931 | unsigned int share_id = (groups[0] << 8) | (groups[1]); | |
1932 | ||
1933 | if (group_id == share_id | |
1934 | && (!checkallow | |
1935 | || tls_curve_allowed(s, groups, SSL_SECOP_CURVE_CHECK))) { | |
1936 | break; | |
1937 | } | |
1938 | } | |
1939 | ||
1940 | /* If i == num_groups then not in the list */ | |
1941 | return i < num_groups; | |
1942 | } | |
deb2d5e7 | 1943 | #endif |
11c67eea MC |
1944 | |
1945 | /* Replace ClientHello1 in the transcript hash with a synthetic message */ | |
1946 | int create_synthetic_message_hash(SSL *s) | |
1947 | { | |
1948 | unsigned char hashval[EVP_MAX_MD_SIZE]; | |
1949 | size_t hashlen = 0; | |
635b7d3f MC |
1950 | unsigned char msghdr[SSL3_HM_HEADER_LENGTH]; |
1951 | ||
1952 | memset(msghdr, 0, sizeof(msghdr)); | |
11c67eea MC |
1953 | |
1954 | /* Get the hash of the initial ClientHello */ | |
1955 | if (!ssl3_digest_cached_records(s, 0) | |
1956 | || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) { | |
1957 | SSLerr(SSL_F_CREATE_SYNTHETIC_MESSAGE_HASH, ERR_R_INTERNAL_ERROR); | |
1958 | return 0; | |
1959 | } | |
1960 | ||
1961 | /* Reinitialise the transcript hash */ | |
1962 | if (!ssl3_init_finished_mac(s)) | |
1963 | return 0; | |
1964 | ||
1965 | /* Inject the synthetic message_hash message */ | |
635b7d3f | 1966 | msghdr[0] = SSL3_MT_MESSAGE_HASH; |
11c67eea MC |
1967 | msghdr[SSL3_HM_HEADER_LENGTH - 1] = hashlen; |
1968 | if (!ssl3_finish_mac(s, msghdr, SSL3_HM_HEADER_LENGTH) | |
1969 | || !ssl3_finish_mac(s, hashval, hashlen)) { | |
1970 | SSLerr(SSL_F_CREATE_SYNTHETIC_MESSAGE_HASH, ERR_R_INTERNAL_ERROR); | |
1971 | return 0; | |
1972 | } | |
1973 | ||
1974 | return 1; | |
1975 | } | |
5d6cca05 DSH |
1976 | |
1977 | static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b) | |
1978 | { | |
1979 | return X509_NAME_cmp(*a, *b); | |
1980 | } | |
1981 | ||
1982 | int parse_ca_names(SSL *s, PACKET *pkt, int *al) | |
1983 | { | |
1984 | STACK_OF(X509_NAME) *ca_sk = sk_X509_NAME_new(ca_dn_cmp); | |
1985 | X509_NAME *xn = NULL; | |
1986 | PACKET cadns; | |
1987 | ||
1988 | if (ca_sk == NULL) { | |
1989 | SSLerr(SSL_F_PARSE_CA_NAMES, ERR_R_MALLOC_FAILURE); | |
1990 | goto decerr; | |
1991 | } | |
1992 | /* get the CA RDNs */ | |
1993 | if (!PACKET_get_length_prefixed_2(pkt, &cadns)) { | |
1994 | *al = SSL_AD_DECODE_ERROR; | |
1995 | SSLerr(SSL_F_PARSE_CA_NAMES, SSL_R_LENGTH_MISMATCH); | |
1996 | goto decerr; | |
1997 | } | |
1998 | ||
1999 | while (PACKET_remaining(&cadns)) { | |
2000 | const unsigned char *namestart, *namebytes; | |
2001 | unsigned int name_len; | |
2002 | ||
2003 | if (!PACKET_get_net_2(&cadns, &name_len) | |
2004 | || !PACKET_get_bytes(&cadns, &namebytes, name_len)) { | |
2005 | SSLerr(SSL_F_PARSE_CA_NAMES, SSL_R_LENGTH_MISMATCH); | |
2006 | goto decerr; | |
2007 | } | |
2008 | ||
2009 | namestart = namebytes; | |
2010 | if ((xn = d2i_X509_NAME(NULL, &namebytes, name_len)) == NULL) { | |
2011 | SSLerr(SSL_F_PARSE_CA_NAMES, ERR_R_ASN1_LIB); | |
2012 | goto decerr; | |
2013 | } | |
2014 | if (namebytes != (namestart + name_len)) { | |
2015 | SSLerr(SSL_F_PARSE_CA_NAMES, SSL_R_CA_DN_LENGTH_MISMATCH); | |
2016 | goto decerr; | |
2017 | } | |
2018 | ||
2019 | if (!sk_X509_NAME_push(ca_sk, xn)) { | |
2020 | SSLerr(SSL_F_PARSE_CA_NAMES, ERR_R_MALLOC_FAILURE); | |
2021 | *al = SSL_AD_INTERNAL_ERROR; | |
2022 | goto err; | |
2023 | } | |
2024 | xn = NULL; | |
2025 | } | |
2026 | ||
2027 | sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free); | |
2028 | s->s3->tmp.ca_names = ca_sk; | |
2029 | ||
2030 | return 1; | |
2031 | ||
2032 | decerr: | |
2033 | *al = SSL_AD_DECODE_ERROR; | |
2034 | err: | |
2035 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); | |
2036 | X509_NAME_free(xn); | |
2037 | return 0; | |
2038 | } | |
2039 | ||
2040 | int construct_ca_names(SSL *s, WPACKET *pkt) | |
2041 | { | |
2042 | STACK_OF(X509_NAME) *ca_sk = SSL_get_client_CA_list(s); | |
2043 | ||
2044 | /* Start sub-packet for client CA list */ | |
2045 | if (!WPACKET_start_sub_packet_u16(pkt)) | |
2046 | return 0; | |
2047 | ||
2048 | if (ca_sk != NULL) { | |
2049 | int i; | |
2050 | ||
2051 | for (i = 0; i < sk_X509_NAME_num(ca_sk); i++) { | |
2052 | unsigned char *namebytes; | |
2053 | X509_NAME *name = sk_X509_NAME_value(ca_sk, i); | |
2054 | int namelen; | |
2055 | ||
2056 | if (name == NULL | |
2057 | || (namelen = i2d_X509_NAME(name, NULL)) < 0 | |
2058 | || !WPACKET_sub_allocate_bytes_u16(pkt, namelen, | |
2059 | &namebytes) | |
2060 | || i2d_X509_NAME(name, &namebytes) != namelen) { | |
2061 | return 0; | |
2062 | } | |
2063 | } | |
2064 | } | |
2065 | ||
2066 | if (!WPACKET_close(pkt)) | |
2067 | return 0; | |
2068 | ||
2069 | return 1; | |
2070 | } |