]>
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 | ||
c7f47786 MC |
75 | int tls_setup_handshake(SSL *s) { |
76 | if (!ssl3_init_finished_mac(s)) | |
77 | return 0; | |
78 | ||
79 | if (s->server) { | |
80 | if (SSL_IS_FIRST_HANDSHAKE(s)) { | |
81 | s->ctx->stats.sess_accept++; | |
82 | } else if (!s->s3->send_connection_binding && | |
83 | !(s->options & | |
84 | SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { | |
85 | /* | |
86 | * Server attempting to renegotiate with client that doesn't | |
87 | * support secure renegotiation. | |
88 | */ | |
89 | SSLerr(SSL_F_TLS_SETUP_HANDSHAKE, | |
90 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); | |
91 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
92 | return 0; | |
93 | } else { | |
94 | s->ctx->stats.sess_accept_renegotiate++; | |
95 | ||
96 | s->s3->tmp.cert_request = 0; | |
97 | } | |
98 | } else { | |
99 | if (SSL_IS_FIRST_HANDSHAKE(s)) | |
100 | s->ctx->stats.sess_connect++; | |
101 | else | |
102 | s->ctx->stats.sess_connect_renegotiate++; | |
103 | ||
104 | /* mark client_random uninitialized */ | |
105 | memset(s->s3->client_random, 0, sizeof(s->s3->client_random)); | |
106 | s->hit = 0; | |
107 | ||
108 | s->s3->tmp.cert_req = 0; | |
109 | ||
110 | if (SSL_IS_DTLS(s)) { | |
111 | s->statem.use_timer = 1; | |
112 | } | |
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 | { | |
174 | EVP_PKEY *pkey; | |
2c5dfdc3 | 175 | const EVP_MD *md; |
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]; |
fe3066ee | 182 | int pktype, ispss = 0; |
2c5dfdc3 MC |
183 | |
184 | if (s->server) { | |
185 | /* Only happens in TLSv1.3 */ | |
186 | /* | |
187 | * TODO(TLS1.3): This needs to change. We should not get this from the | |
188 | * cipher. However, for now, we have not done the work to separate the | |
189 | * certificate type from the ciphersuite | |
190 | */ | |
191 | pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md); | |
192 | if (pkey == NULL) | |
193 | goto err; | |
194 | } else { | |
195 | md = s->s3->tmp.md[s->cert->key - s->cert->pkeys]; | |
196 | pkey = s->cert->key->privatekey; | |
197 | } | |
5f9b64a2 | 198 | pktype = EVP_PKEY_id(pkey); |
d8bc1399 MC |
199 | |
200 | mctx = EVP_MD_CTX_new(); | |
201 | if (mctx == NULL) { | |
202 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_MALLOC_FAILURE); | |
203 | goto err; | |
204 | } | |
d8bc1399 | 205 | |
2c5dfdc3 MC |
206 | /* Get the data to be signed */ |
207 | if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) { | |
d8bc1399 MC |
208 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR); |
209 | goto err; | |
210 | } | |
211 | ||
fe3066ee | 212 | if (SSL_USE_SIGALGS(s) && !tls12_get_sigandhash(s, pkt, pkey, md, &ispss)) { |
d8bc1399 MC |
213 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR); |
214 | goto err; | |
215 | } | |
216 | #ifdef SSL_DEBUG | |
217 | fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md)); | |
218 | #endif | |
5f9b64a2 MC |
219 | siglen = EVP_PKEY_size(pkey); |
220 | sig = OPENSSL_malloc(siglen); | |
d8bc1399 MC |
221 | if (sig == NULL) { |
222 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_MALLOC_FAILURE); | |
223 | goto err; | |
224 | } | |
5f9b64a2 MC |
225 | |
226 | if (EVP_DigestSignInit(mctx, &pctx, md, NULL, pkey) <= 0 | |
227 | || EVP_DigestSignUpdate(mctx, hdata, hdatalen) <= 0) { | |
228 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB); | |
229 | goto err; | |
230 | } | |
231 | ||
fe3066ee | 232 | if (ispss) { |
5f9b64a2 MC |
233 | if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 |
234 | /* -1 here means set saltlen to the digest len */ | |
235 | || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1) <= 0) { | |
236 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB); | |
237 | goto err; | |
238 | } | |
239 | } else if (s->version == SSL3_VERSION) { | |
240 | if (!EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET, | |
241 | (int)s->session->master_key_length, | |
242 | s->session->master_key)) { | |
243 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB); | |
244 | goto err; | |
245 | } | |
246 | } | |
247 | ||
248 | if (EVP_DigestSignFinal(mctx, sig, &siglen) <= 0) { | |
d8bc1399 MC |
249 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB); |
250 | goto err; | |
251 | } | |
5f9b64a2 | 252 | |
d8bc1399 MC |
253 | #ifndef OPENSSL_NO_GOST |
254 | { | |
d8bc1399 MC |
255 | if (pktype == NID_id_GostR3410_2001 |
256 | || pktype == NID_id_GostR3410_2012_256 | |
257 | || pktype == NID_id_GostR3410_2012_512) | |
5f9b64a2 | 258 | BUF_reverse(sig, NULL, siglen); |
d8bc1399 MC |
259 | } |
260 | #endif | |
261 | ||
5f9b64a2 | 262 | if (!WPACKET_sub_memcpy_u16(pkt, sig, siglen)) { |
d8bc1399 MC |
263 | SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR); |
264 | goto err; | |
265 | } | |
266 | ||
267 | /* Digest cached records and discard handshake buffer */ | |
268 | if (!ssl3_digest_cached_records(s, 0)) | |
269 | goto err; | |
270 | ||
271 | OPENSSL_free(sig); | |
272 | EVP_MD_CTX_free(mctx); | |
273 | return 1; | |
274 | err: | |
275 | OPENSSL_free(sig); | |
276 | EVP_MD_CTX_free(mctx); | |
277 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
278 | return 0; | |
279 | } | |
280 | ||
281 | MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt) | |
282 | { | |
283 | EVP_PKEY *pkey = NULL; | |
703bcee0 | 284 | const unsigned char *data; |
d8bc1399 MC |
285 | #ifndef OPENSSL_NO_GOST |
286 | unsigned char *gost_data = NULL; | |
287 | #endif | |
5f9b64a2 | 288 | int al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR; |
fe3066ee | 289 | int type = 0, j, pktype, ispss = 0; |
d8bc1399 MC |
290 | unsigned int len; |
291 | X509 *peer; | |
292 | const EVP_MD *md = NULL; | |
2c5dfdc3 | 293 | size_t hdatalen = 0; |
d8bc1399 | 294 | void *hdata; |
2c5dfdc3 | 295 | unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE]; |
d8bc1399 | 296 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); |
5f9b64a2 | 297 | EVP_PKEY_CTX *pctx = NULL; |
d8bc1399 MC |
298 | |
299 | if (mctx == NULL) { | |
300 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE); | |
d8bc1399 MC |
301 | goto f_err; |
302 | } | |
303 | ||
304 | peer = s->session->peer; | |
305 | pkey = X509_get0_pubkey(peer); | |
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 | } | |
703bcee0 | 336 | rv = tls12_check_peer_sigalg(&md, 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 | } | |
fe3066ee | 343 | ispss = SIGID_IS_PSS(sigalg); |
d8bc1399 MC |
344 | #ifdef SSL_DEBUG |
345 | fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); | |
346 | #endif | |
347 | } else { | |
348 | /* Use default digest for this key type */ | |
349 | int idx = ssl_cert_type(NULL, pkey); | |
350 | if (idx >= 0) | |
351 | md = s->s3->tmp.md[idx]; | |
352 | if (md == NULL) { | |
353 | al = SSL_AD_INTERNAL_ERROR; | |
354 | goto f_err; | |
355 | } | |
356 | } | |
357 | ||
358 | if (!PACKET_get_net_2(pkt, &len)) { | |
359 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH); | |
360 | al = SSL_AD_DECODE_ERROR; | |
361 | goto f_err; | |
362 | } | |
363 | } | |
364 | j = EVP_PKEY_size(pkey); | |
365 | if (((int)len > j) || ((int)PACKET_remaining(pkt) > j) | |
366 | || (PACKET_remaining(pkt) == 0)) { | |
367 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE); | |
368 | al = SSL_AD_DECODE_ERROR; | |
369 | goto f_err; | |
370 | } | |
371 | if (!PACKET_get_bytes(pkt, &data, len)) { | |
372 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH); | |
373 | al = SSL_AD_DECODE_ERROR; | |
374 | goto f_err; | |
375 | } | |
376 | ||
2c5dfdc3 | 377 | if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) { |
d8bc1399 | 378 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_INTERNAL_ERROR); |
d8bc1399 MC |
379 | goto f_err; |
380 | } | |
381 | ||
382 | #ifdef SSL_DEBUG | |
383 | fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md)); | |
384 | #endif | |
5f9b64a2 MC |
385 | if (EVP_DigestVerifyInit(mctx, &pctx, md, NULL, pkey) <= 0 |
386 | || EVP_DigestVerifyUpdate(mctx, hdata, hdatalen) <= 0) { | |
d8bc1399 | 387 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB); |
d8bc1399 MC |
388 | goto f_err; |
389 | } | |
390 | #ifndef OPENSSL_NO_GOST | |
391 | { | |
d8bc1399 MC |
392 | if (pktype == NID_id_GostR3410_2001 |
393 | || pktype == NID_id_GostR3410_2012_256 | |
394 | || pktype == NID_id_GostR3410_2012_512) { | |
395 | if ((gost_data = OPENSSL_malloc(len)) == NULL) { | |
396 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE); | |
d8bc1399 MC |
397 | goto f_err; |
398 | } | |
399 | BUF_reverse(gost_data, data, len); | |
400 | data = gost_data; | |
401 | } | |
402 | } | |
403 | #endif | |
404 | ||
fe3066ee | 405 | if (ispss) { |
5f9b64a2 MC |
406 | if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 |
407 | /* -1 here means set saltlen to the digest len */ | |
408 | || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1) <= 0) { | |
409 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB); | |
410 | goto f_err; | |
411 | } | |
412 | } else if (s->version == SSL3_VERSION | |
d8bc1399 MC |
413 | && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET, |
414 | (int)s->session->master_key_length, | |
415 | s->session->master_key)) { | |
416 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB); | |
d8bc1399 MC |
417 | goto f_err; |
418 | } | |
419 | ||
5f9b64a2 | 420 | if (EVP_DigestVerifyFinal(mctx, data, len) <= 0) { |
d8bc1399 MC |
421 | al = SSL_AD_DECRYPT_ERROR; |
422 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_SIGNATURE); | |
423 | goto f_err; | |
424 | } | |
425 | ||
2c5dfdc3 MC |
426 | if (SSL_IS_TLS13(s)) |
427 | ret = MSG_PROCESS_CONTINUE_READING; | |
428 | else | |
429 | ret = MSG_PROCESS_CONTINUE_PROCESSING; | |
d8bc1399 MC |
430 | if (0) { |
431 | f_err: | |
432 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
433 | ossl_statem_set_error(s); | |
434 | } | |
435 | BIO_free(s->s3->handshake_buffer); | |
436 | s->s3->handshake_buffer = NULL; | |
437 | EVP_MD_CTX_free(mctx); | |
438 | #ifndef OPENSSL_NO_GOST | |
439 | OPENSSL_free(gost_data); | |
440 | #endif | |
441 | return ret; | |
442 | } | |
443 | ||
229185e6 | 444 | int tls_construct_finished(SSL *s, WPACKET *pkt) |
0f113f3e | 445 | { |
12472b45 | 446 | size_t finish_md_len; |
229185e6 | 447 | const char *sender; |
8b0e934a | 448 | size_t slen; |
229185e6 MC |
449 | |
450 | if (s->server) { | |
451 | sender = s->method->ssl3_enc->server_finished_label; | |
452 | slen = s->method->ssl3_enc->server_finished_label_len; | |
453 | } else { | |
454 | sender = s->method->ssl3_enc->client_finished_label; | |
455 | slen = s->method->ssl3_enc->client_finished_label_len; | |
456 | } | |
0f113f3e | 457 | |
12472b45 MC |
458 | finish_md_len = s->method->ssl3_enc->final_finish_mac(s, |
459 | sender, slen, | |
460 | s->s3->tmp.finish_md); | |
461 | if (finish_md_len == 0) { | |
4f89bfbf MC |
462 | SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR); |
463 | goto err; | |
464 | } | |
465 | ||
12472b45 | 466 | s->s3->tmp.finish_md_len = finish_md_len; |
4f89bfbf | 467 | |
12472b45 | 468 | if (!WPACKET_memcpy(pkt, s->s3->tmp.finish_md, finish_md_len)) { |
4f89bfbf MC |
469 | SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR); |
470 | goto err; | |
471 | } | |
0f113f3e | 472 | |
2faa1b48 CB |
473 | /* Log the master secret, if logging is enabled. */ |
474 | if (!ssl_log_master_secret(s, s->s3->client_random, SSL3_RANDOM_SIZE, | |
475 | s->session->master_key, | |
476 | s->session->master_key_length)) | |
477 | return 0; | |
478 | ||
b9908bf9 MC |
479 | /* |
480 | * Copy the finished so we can use it for renegotiation checks | |
481 | */ | |
23a635c0 | 482 | if (!s->server) { |
12472b45 MC |
483 | OPENSSL_assert(finish_md_len <= EVP_MAX_MD_SIZE); |
484 | memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md, | |
485 | finish_md_len); | |
486 | s->s3->previous_client_finished_len = finish_md_len; | |
b9908bf9 | 487 | } else { |
12472b45 MC |
488 | OPENSSL_assert(finish_md_len <= EVP_MAX_MD_SIZE); |
489 | memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md, | |
490 | finish_md_len); | |
491 | s->s3->previous_server_finished_len = finish_md_len; | |
b9908bf9 | 492 | } |
0f113f3e | 493 | |
b9908bf9 | 494 | return 1; |
4f89bfbf | 495 | err: |
4f89bfbf MC |
496 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
497 | return 0; | |
0f113f3e | 498 | } |
d02b48c6 | 499 | |
bf48836c | 500 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
501 | /* |
502 | * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen | |
503 | * to far. | |
504 | */ | |
ee2ffc27 | 505 | static void ssl3_take_mac(SSL *s) |
0f113f3e MC |
506 | { |
507 | const char *sender; | |
8b0e934a | 508 | size_t slen; |
0f113f3e MC |
509 | /* |
510 | * If no new cipher setup return immediately: other functions will set | |
511 | * the appropriate error. | |
512 | */ | |
513 | if (s->s3->tmp.new_cipher == NULL) | |
514 | return; | |
49ae7423 | 515 | if (!s->server) { |
0f113f3e MC |
516 | sender = s->method->ssl3_enc->server_finished_label; |
517 | slen = s->method->ssl3_enc->server_finished_label_len; | |
518 | } else { | |
519 | sender = s->method->ssl3_enc->client_finished_label; | |
520 | slen = s->method->ssl3_enc->client_finished_label_len; | |
521 | } | |
522 | ||
523 | s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s, | |
524 | sender, | |
525 | slen, | |
526 | s->s3->tmp.peer_finish_md); | |
527 | } | |
ee2ffc27 BL |
528 | #endif |
529 | ||
be3583fa | 530 | MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt) |
b9908bf9 MC |
531 | { |
532 | int al; | |
348240c6 | 533 | size_t remain; |
4fa52141 | 534 | |
73999b62 | 535 | remain = PACKET_remaining(pkt); |
657da85e MC |
536 | /* |
537 | * 'Change Cipher Spec' is just a single byte, which should already have | |
c69f2adf MC |
538 | * been consumed by ssl_get_message() so there should be no bytes left, |
539 | * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes | |
657da85e | 540 | */ |
c69f2adf | 541 | if (SSL_IS_DTLS(s)) { |
73999b62 | 542 | if ((s->version == DTLS1_BAD_VER |
a230b26e EK |
543 | && remain != DTLS1_CCS_HEADER_LENGTH + 1) |
544 | || (s->version != DTLS1_BAD_VER | |
545 | && remain != DTLS1_CCS_HEADER_LENGTH - 1)) { | |
546 | al = SSL_AD_ILLEGAL_PARAMETER; | |
547 | SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, | |
548 | SSL_R_BAD_CHANGE_CIPHER_SPEC); | |
549 | goto f_err; | |
c69f2adf MC |
550 | } |
551 | } else { | |
73999b62 | 552 | if (remain != 0) { |
c69f2adf | 553 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 MC |
554 | SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, |
555 | SSL_R_BAD_CHANGE_CIPHER_SPEC); | |
c69f2adf MC |
556 | goto f_err; |
557 | } | |
657da85e MC |
558 | } |
559 | ||
560 | /* Check we have a cipher to change to */ | |
561 | if (s->s3->tmp.new_cipher == NULL) { | |
562 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
b9908bf9 | 563 | SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY); |
657da85e MC |
564 | goto f_err; |
565 | } | |
566 | ||
567 | s->s3->change_cipher_spec = 1; | |
568 | if (!ssl3_do_change_cipher_spec(s)) { | |
569 | al = SSL_AD_INTERNAL_ERROR; | |
b9908bf9 | 570 | SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR); |
657da85e MC |
571 | goto f_err; |
572 | } | |
573 | ||
c69f2adf MC |
574 | if (SSL_IS_DTLS(s)) { |
575 | dtls1_reset_seq_numbers(s, SSL3_CC_READ); | |
576 | ||
577 | if (s->version == DTLS1_BAD_VER) | |
578 | s->d1->handshake_read_seq++; | |
579 | ||
580 | #ifndef OPENSSL_NO_SCTP | |
581 | /* | |
582 | * Remember that a CCS has been received, so that an old key of | |
583 | * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no | |
584 | * SCTP is used | |
585 | */ | |
586 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL); | |
587 | #endif | |
588 | } | |
589 | ||
b9908bf9 | 590 | return MSG_PROCESS_CONTINUE_READING; |
657da85e MC |
591 | f_err: |
592 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 593 | ossl_statem_set_error(s); |
b9908bf9 | 594 | return MSG_PROCESS_ERROR; |
657da85e MC |
595 | } |
596 | ||
be3583fa | 597 | MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt) |
b9908bf9 | 598 | { |
7776a36c | 599 | int al = SSL_AD_INTERNAL_ERROR; |
12472b45 | 600 | size_t md_len; |
b9908bf9 | 601 | |
0f113f3e | 602 | /* If this occurs, we have missed a message */ |
92760c21 | 603 | if (!SSL_IS_TLS13(s) && !s->s3->change_cipher_spec) { |
0f113f3e | 604 | al = SSL_AD_UNEXPECTED_MESSAGE; |
b9908bf9 | 605 | SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_GOT_A_FIN_BEFORE_A_CCS); |
0f113f3e MC |
606 | goto f_err; |
607 | } | |
608 | s->s3->change_cipher_spec = 0; | |
609 | ||
12472b45 | 610 | md_len = s->s3->tmp.peer_finish_md_len; |
0f113f3e | 611 | |
12472b45 | 612 | if (md_len != PACKET_remaining(pkt)) { |
0f113f3e | 613 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 614 | SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_BAD_DIGEST_LENGTH); |
0f113f3e MC |
615 | goto f_err; |
616 | } | |
617 | ||
12472b45 MC |
618 | if (CRYPTO_memcmp(PACKET_data(pkt), s->s3->tmp.peer_finish_md, |
619 | md_len) != 0) { | |
0f113f3e | 620 | al = SSL_AD_DECRYPT_ERROR; |
b9908bf9 | 621 | SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_DIGEST_CHECK_FAILED); |
0f113f3e MC |
622 | goto f_err; |
623 | } | |
624 | ||
625 | /* | |
626 | * Copy the finished so we can use it for renegotiation checks | |
627 | */ | |
23a635c0 | 628 | if (s->server) { |
12472b45 MC |
629 | OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE); |
630 | memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md, | |
631 | md_len); | |
632 | s->s3->previous_client_finished_len = md_len; | |
0f113f3e | 633 | } else { |
12472b45 MC |
634 | OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE); |
635 | memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md, | |
636 | md_len); | |
637 | s->s3->previous_server_finished_len = md_len; | |
0f113f3e MC |
638 | } |
639 | ||
7776a36c MC |
640 | /* |
641 | * In TLS1.3 we also have to change cipher state and do any final processing | |
642 | * of the initial server flight (if we are a client) | |
643 | */ | |
92760c21 MC |
644 | if (SSL_IS_TLS13(s)) { |
645 | if (s->server) { | |
646 | if (!s->method->ssl3_enc->change_cipher_state(s, | |
647 | SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_READ)) { | |
92760c21 MC |
648 | SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER); |
649 | goto f_err; | |
650 | } | |
651 | } else { | |
652 | if (!s->method->ssl3_enc->generate_master_secret(s, | |
ec15acb6 | 653 | s->master_secret, s->handshake_secret, 0, |
92760c21 | 654 | &s->session->master_key_length)) { |
92760c21 MC |
655 | SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER); |
656 | goto f_err; | |
657 | } | |
658 | if (!s->method->ssl3_enc->change_cipher_state(s, | |
659 | SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) { | |
92760c21 MC |
660 | SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_CANNOT_CHANGE_CIPHER); |
661 | goto f_err; | |
662 | } | |
7776a36c MC |
663 | if (!tls_process_initial_server_flight(s, &al)) |
664 | goto f_err; | |
92760c21 MC |
665 | } |
666 | } | |
667 | ||
e6575156 | 668 | return MSG_PROCESS_FINISHED_READING; |
0f113f3e MC |
669 | f_err: |
670 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 671 | ossl_statem_set_error(s); |
b9908bf9 | 672 | return MSG_PROCESS_ERROR; |
0f113f3e | 673 | } |
d02b48c6 | 674 | |
7cea05dc | 675 | int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt) |
b9908bf9 | 676 | { |
7cea05dc | 677 | if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) { |
3c106325 | 678 | SSLerr(SSL_F_TLS_CONSTRUCT_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR); |
85a7a5e6 MC |
679 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
680 | return 0; | |
681 | } | |
b9908bf9 | 682 | |
b9908bf9 MC |
683 | return 1; |
684 | } | |
685 | ||
e96e0f8e MC |
686 | /* Add a certificate to the WPACKET */ |
687 | static int ssl_add_cert_to_wpacket(SSL *s, WPACKET *pkt, X509 *x, int chain, | |
688 | int *al) | |
0f113f3e | 689 | { |
e96e0f8e MC |
690 | int len; |
691 | unsigned char *outbytes; | |
692 | ||
693 | len = i2d_X509(x, NULL); | |
694 | if (len < 0) { | |
f63e4288 | 695 | SSLerr(SSL_F_SSL_ADD_CERT_TO_WPACKET, ERR_R_BUF_LIB); |
e96e0f8e MC |
696 | *al = SSL_AD_INTERNAL_ERROR; |
697 | return 0; | |
698 | } | |
699 | if (!WPACKET_sub_allocate_bytes_u24(pkt, len, &outbytes) | |
700 | || i2d_X509(x, &outbytes) != len) { | |
f63e4288 | 701 | SSLerr(SSL_F_SSL_ADD_CERT_TO_WPACKET, ERR_R_INTERNAL_ERROR); |
e96e0f8e MC |
702 | *al = SSL_AD_INTERNAL_ERROR; |
703 | return 0; | |
704 | } | |
705 | ||
706 | if (SSL_IS_TLS13(s) | |
707 | && !tls_construct_extensions(s, pkt, EXT_TLS1_3_CERTIFICATE, x, | |
708 | chain, al)) | |
709 | return 0; | |
710 | ||
711 | return 1; | |
712 | } | |
713 | ||
714 | /* Add certificate chain to provided WPACKET */ | |
715 | static int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk, int *al) | |
716 | { | |
717 | int i, chain_count; | |
718 | X509 *x; | |
719 | STACK_OF(X509) *extra_certs; | |
720 | STACK_OF(X509) *chain = NULL; | |
721 | X509_STORE *chain_store; | |
722 | int tmpal = SSL_AD_INTERNAL_ERROR; | |
723 | ||
724 | if (cpk == NULL || cpk->x509 == NULL) | |
725 | return 1; | |
726 | ||
727 | x = cpk->x509; | |
728 | ||
729 | /* | |
730 | * If we have a certificate specific chain use it, else use parent ctx. | |
731 | */ | |
d805a57b | 732 | if (cpk->chain != NULL) |
e96e0f8e MC |
733 | extra_certs = cpk->chain; |
734 | else | |
735 | extra_certs = s->ctx->extra_certs; | |
736 | ||
737 | if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) | |
738 | chain_store = NULL; | |
739 | else if (s->cert->chain_store) | |
740 | chain_store = s->cert->chain_store; | |
741 | else | |
742 | chain_store = s->ctx->cert_store; | |
743 | ||
d805a57b | 744 | if (chain_store != NULL) { |
e96e0f8e MC |
745 | X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new(); |
746 | ||
747 | if (xs_ctx == NULL) { | |
748 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_MALLOC_FAILURE); | |
749 | goto err; | |
750 | } | |
751 | if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) { | |
752 | X509_STORE_CTX_free(xs_ctx); | |
753 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB); | |
754 | goto err; | |
755 | } | |
756 | /* | |
757 | * It is valid for the chain not to be complete (because normally we | |
758 | * don't include the root cert in the chain). Therefore we deliberately | |
759 | * ignore the error return from this call. We're not actually verifying | |
760 | * the cert - we're just building as much of the chain as we can | |
761 | */ | |
762 | (void)X509_verify_cert(xs_ctx); | |
763 | /* Don't leave errors in the queue */ | |
764 | ERR_clear_error(); | |
765 | chain = X509_STORE_CTX_get0_chain(xs_ctx); | |
766 | i = ssl_security_cert_chain(s, chain, NULL, 0); | |
767 | if (i != 1) { | |
768 | #if 0 | |
769 | /* Dummy error calls so mkerr generates them */ | |
770 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL); | |
771 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL); | |
772 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK); | |
773 | #endif | |
774 | X509_STORE_CTX_free(xs_ctx); | |
775 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i); | |
776 | goto err; | |
777 | } | |
778 | chain_count = sk_X509_num(chain); | |
779 | for (i = 0; i < chain_count; i++) { | |
780 | x = sk_X509_value(chain, i); | |
781 | ||
782 | if (!ssl_add_cert_to_wpacket(s, pkt, x, i, &tmpal)) { | |
783 | X509_STORE_CTX_free(xs_ctx); | |
784 | goto err; | |
785 | } | |
786 | } | |
787 | X509_STORE_CTX_free(xs_ctx); | |
788 | } else { | |
789 | i = ssl_security_cert_chain(s, extra_certs, x, 0); | |
790 | if (i != 1) { | |
791 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i); | |
792 | goto err; | |
793 | } | |
794 | if (!ssl_add_cert_to_wpacket(s, pkt, x, 0, &tmpal)) | |
795 | goto err; | |
796 | for (i = 0; i < sk_X509_num(extra_certs); i++) { | |
797 | x = sk_X509_value(extra_certs, i); | |
798 | if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1, &tmpal)) | |
799 | goto err; | |
800 | } | |
801 | } | |
802 | return 1; | |
803 | ||
804 | err: | |
805 | *al = tmpal; | |
806 | return 0; | |
807 | } | |
808 | ||
809 | unsigned long ssl3_output_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk, | |
810 | int *al) | |
811 | { | |
812 | int tmpal = SSL_AD_INTERNAL_ERROR; | |
813 | ||
5923ad4b | 814 | if (!WPACKET_start_sub_packet_u24(pkt) |
e96e0f8e | 815 | || !ssl_add_cert_chain(s, pkt, cpk, &tmpal) |
5923ad4b | 816 | || !WPACKET_close(pkt)) { |
c49e1912 | 817 | SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_INTERNAL_ERROR); |
e96e0f8e | 818 | *al = tmpal; |
7cea05dc | 819 | return 0; |
77d514c5 | 820 | } |
c49e1912 | 821 | return 1; |
0f113f3e MC |
822 | } |
823 | ||
30f05b19 MC |
824 | /* |
825 | * Tidy up after the end of a handshake. In the case of SCTP this may result | |
826 | * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is | |
827 | * freed up as well. | |
828 | */ | |
829 | WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst, int clearbufs) | |
8723588e MC |
830 | { |
831 | void (*cb) (const SSL *ssl, int type, int val) = NULL; | |
832 | ||
833 | #ifndef OPENSSL_NO_SCTP | |
834 | if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) { | |
be3583fa | 835 | WORK_STATE ret; |
8723588e MC |
836 | ret = dtls_wait_for_dry(s); |
837 | if (ret != WORK_FINISHED_CONTINUE) | |
838 | return ret; | |
839 | } | |
840 | #endif | |
841 | ||
30f05b19 MC |
842 | if (clearbufs) { |
843 | if (!SSL_IS_DTLS(s)) { | |
844 | /* | |
845 | * We don't do this in DTLS because we may still need the init_buf | |
846 | * in case there are any unexpected retransmits | |
847 | */ | |
848 | BUF_MEM_free(s->init_buf); | |
849 | s->init_buf = NULL; | |
850 | } | |
851 | ssl_free_wbio_buffer(s); | |
852 | s->init_num = 0; | |
473483d4 | 853 | } |
8723588e | 854 | |
c7f47786 | 855 | if (s->statem.cleanuphand) { |
8723588e MC |
856 | /* skipped if we just sent a HelloRequest */ |
857 | s->renegotiate = 0; | |
858 | s->new_session = 0; | |
c7f47786 | 859 | s->statem.cleanuphand = 0; |
8723588e | 860 | |
30f05b19 MC |
861 | ssl3_cleanup_key_block(s); |
862 | ||
8723588e | 863 | if (s->server) { |
8723588e MC |
864 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); |
865 | ||
866 | s->ctx->stats.sess_accept_good++; | |
fe3a3291 | 867 | s->handshake_func = ossl_statem_accept; |
8723588e MC |
868 | } else { |
869 | ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); | |
870 | if (s->hit) | |
871 | s->ctx->stats.sess_hit++; | |
872 | ||
fe3a3291 | 873 | s->handshake_func = ossl_statem_connect; |
8723588e MC |
874 | s->ctx->stats.sess_connect_good++; |
875 | } | |
876 | ||
877 | if (s->info_callback != NULL) | |
878 | cb = s->info_callback; | |
879 | else if (s->ctx->info_callback != NULL) | |
880 | cb = s->ctx->info_callback; | |
881 | ||
882 | if (cb != NULL) | |
883 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); | |
884 | ||
885 | if (SSL_IS_DTLS(s)) { | |
886 | /* done with handshaking */ | |
887 | s->d1->handshake_read_seq = 0; | |
888 | s->d1->handshake_write_seq = 0; | |
889 | s->d1->next_handshake_write_seq = 0; | |
f5c7f5df | 890 | dtls1_clear_received_buffer(s); |
8723588e MC |
891 | } |
892 | } | |
893 | ||
30f05b19 MC |
894 | /* |
895 | * If we've not cleared the buffers its because we've got more work to do, | |
896 | * so continue. | |
897 | */ | |
898 | if (!clearbufs) | |
899 | return WORK_FINISHED_CONTINUE; | |
900 | ||
8723588e MC |
901 | return WORK_FINISHED_STOP; |
902 | } | |
903 | ||
9ab930b2 MC |
904 | int tls_get_message_header(SSL *s, int *mt) |
905 | { | |
906 | /* s->init_num < SSL3_HM_HEADER_LENGTH */ | |
907 | int skip_message, i, recvd_type, al; | |
908 | unsigned char *p; | |
54105ddd | 909 | size_t l, readbytes; |
9ab930b2 MC |
910 | |
911 | p = (unsigned char *)s->init_buf->data; | |
912 | ||
913 | do { | |
914 | while (s->init_num < SSL3_HM_HEADER_LENGTH) { | |
915 | i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type, | |
a230b26e EK |
916 | &p[s->init_num], |
917 | SSL3_HM_HEADER_LENGTH - s->init_num, | |
54105ddd | 918 | 0, &readbytes); |
9ab930b2 MC |
919 | if (i <= 0) { |
920 | s->rwstate = SSL_READING; | |
921 | return 0; | |
32ec4153 | 922 | } |
9ab930b2 | 923 | if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) { |
1257adec | 924 | /* |
a230b26e EK |
925 | * A ChangeCipherSpec must be a single byte and may not occur |
926 | * in the middle of a handshake message. | |
927 | */ | |
54105ddd | 928 | if (s->init_num != 0 || readbytes != 1 || p[0] != SSL3_MT_CCS) { |
1257adec DB |
929 | al = SSL_AD_UNEXPECTED_MESSAGE; |
930 | SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, | |
931 | SSL_R_BAD_CHANGE_CIPHER_SPEC); | |
932 | goto f_err; | |
933 | } | |
9ab930b2 | 934 | s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC; |
54105ddd | 935 | s->init_num = readbytes - 1; |
c4377574 | 936 | s->init_msg = s->init_buf->data; |
54105ddd | 937 | s->s3->tmp.message_size = readbytes; |
9ab930b2 MC |
938 | return 1; |
939 | } else if (recvd_type != SSL3_RT_HANDSHAKE) { | |
940 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
941 | SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_CCS_RECEIVED_EARLY); | |
32ec4153 MC |
942 | goto f_err; |
943 | } | |
54105ddd | 944 | s->init_num += readbytes; |
9ab930b2 MC |
945 | } |
946 | ||
947 | skip_message = 0; | |
948 | if (!s->server) | |
c7f47786 MC |
949 | if (s->statem.hand_state != TLS_ST_OK |
950 | && p[0] == SSL3_MT_HELLO_REQUEST) | |
9ab930b2 MC |
951 | /* |
952 | * The server may always send 'Hello Request' messages -- | |
953 | * we are doing a handshake anyway now, so ignore them if | |
954 | * their format is correct. Does not count for 'Finished' | |
955 | * MAC. | |
956 | */ | |
957 | if (p[1] == 0 && p[2] == 0 && p[3] == 0) { | |
958 | s->init_num = 0; | |
959 | skip_message = 1; | |
960 | ||
961 | if (s->msg_callback) | |
962 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, | |
963 | p, SSL3_HM_HEADER_LENGTH, s, | |
964 | s->msg_callback_arg); | |
965 | } | |
966 | } while (skip_message); | |
967 | /* s->init_num == SSL3_HM_HEADER_LENGTH */ | |
968 | ||
969 | *mt = *p; | |
970 | s->s3->tmp.message_type = *(p++); | |
32ec4153 | 971 | |
e8aa8b6c | 972 | if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) { |
9ab930b2 MC |
973 | /* |
974 | * Only happens with SSLv3+ in an SSLv2 backward compatible | |
975 | * ClientHello | |
e8aa8b6c F |
976 | * |
977 | * Total message size is the remaining record bytes to read | |
978 | * plus the SSL3_HM_HEADER_LENGTH bytes that we already read | |
9ab930b2 | 979 | */ |
9ab930b2 MC |
980 | l = RECORD_LAYER_get_rrec_length(&s->rlayer) |
981 | + SSL3_HM_HEADER_LENGTH; | |
9ab930b2 MC |
982 | s->s3->tmp.message_size = l; |
983 | ||
984 | s->init_msg = s->init_buf->data; | |
985 | s->init_num = SSL3_HM_HEADER_LENGTH; | |
986 | } else { | |
987 | n2l3(p, l); | |
988 | /* BUF_MEM_grow takes an 'int' parameter */ | |
989 | if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) { | |
990 | al = SSL_AD_ILLEGAL_PARAMETER; | |
991 | SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_EXCESSIVE_MESSAGE_SIZE); | |
992 | goto f_err; | |
32ec4153 | 993 | } |
9ab930b2 MC |
994 | s->s3->tmp.message_size = l; |
995 | ||
996 | s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH; | |
997 | s->init_num = 0; | |
998 | } | |
999 | ||
1000 | return 1; | |
1001 | f_err: | |
1002 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
9ab930b2 MC |
1003 | return 0; |
1004 | } | |
1005 | ||
eda75751 | 1006 | int tls_get_message_body(SSL *s, size_t *len) |
9ab930b2 | 1007 | { |
54105ddd | 1008 | size_t n, readbytes; |
9ab930b2 MC |
1009 | unsigned char *p; |
1010 | int i; | |
1011 | ||
1012 | if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) { | |
1013 | /* We've already read everything in */ | |
1014 | *len = (unsigned long)s->init_num; | |
1015 | return 1; | |
0f113f3e MC |
1016 | } |
1017 | ||
0f113f3e MC |
1018 | p = s->init_msg; |
1019 | n = s->s3->tmp.message_size - s->init_num; | |
1020 | while (n > 0) { | |
657da85e | 1021 | i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL, |
54105ddd | 1022 | &p[s->init_num], n, 0, &readbytes); |
0f113f3e MC |
1023 | if (i <= 0) { |
1024 | s->rwstate = SSL_READING; | |
9ab930b2 MC |
1025 | *len = 0; |
1026 | return 0; | |
0f113f3e | 1027 | } |
54105ddd MC |
1028 | s->init_num += readbytes; |
1029 | n -= readbytes; | |
0f113f3e | 1030 | } |
ee2ffc27 | 1031 | |
bf48836c | 1032 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
1033 | /* |
1034 | * If receiving Finished, record MAC of prior handshake messages for | |
1035 | * Finished verification. | |
1036 | */ | |
1037 | if (*s->init_buf->data == SSL3_MT_FINISHED) | |
1038 | ssl3_take_mac(s); | |
ee2ffc27 BL |
1039 | #endif |
1040 | ||
0f113f3e | 1041 | /* Feed this message into MAC computation. */ |
e8aa8b6c | 1042 | if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) { |
d166ed8c DSH |
1043 | if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, |
1044 | s->init_num)) { | |
1045 | SSLerr(SSL_F_TLS_GET_MESSAGE_BODY, ERR_R_EVP_LIB); | |
1046 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
1047 | *len = 0; | |
1048 | return 0; | |
1049 | } | |
32ec4153 | 1050 | if (s->msg_callback) |
a230b26e | 1051 | s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data, |
32ec4153 MC |
1052 | (size_t)s->init_num, s, s->msg_callback_arg); |
1053 | } else { | |
d166ed8c | 1054 | if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, |
a230b26e | 1055 | s->init_num + SSL3_HM_HEADER_LENGTH)) { |
d166ed8c DSH |
1056 | SSLerr(SSL_F_TLS_GET_MESSAGE_BODY, ERR_R_EVP_LIB); |
1057 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
1058 | *len = 0; | |
1059 | return 0; | |
1060 | } | |
32ec4153 MC |
1061 | if (s->msg_callback) |
1062 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data, | |
1063 | (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s, | |
1064 | s->msg_callback_arg); | |
1065 | } | |
1066 | ||
eda75751 | 1067 | *len = s->init_num; |
9ab930b2 | 1068 | return 1; |
0f113f3e | 1069 | } |
d02b48c6 | 1070 | |
2e5ead83 | 1071 | int ssl_cert_type(const X509 *x, const EVP_PKEY *pk) |
0f113f3e | 1072 | { |
a230b26e | 1073 | if (pk == NULL && (pk = X509_get0_pubkey(x)) == NULL) |
17a72388 VD |
1074 | return -1; |
1075 | ||
1076 | switch (EVP_PKEY_id(pk)) { | |
1077 | default: | |
1078 | return -1; | |
1079 | case EVP_PKEY_RSA: | |
1080 | return SSL_PKEY_RSA_ENC; | |
1081 | case EVP_PKEY_DSA: | |
1082 | return SSL_PKEY_DSA_SIGN; | |
ea262260 | 1083 | #ifndef OPENSSL_NO_EC |
17a72388 VD |
1084 | case EVP_PKEY_EC: |
1085 | return SSL_PKEY_ECC; | |
ea262260 | 1086 | #endif |
2a9b9654 | 1087 | #ifndef OPENSSL_NO_GOST |
17a72388 VD |
1088 | case NID_id_GostR3410_2001: |
1089 | return SSL_PKEY_GOST01; | |
1090 | case NID_id_GostR3410_2012_256: | |
1091 | return SSL_PKEY_GOST12_256; | |
1092 | case NID_id_GostR3410_2012_512: | |
1093 | return SSL_PKEY_GOST12_512; | |
2a9b9654 | 1094 | #endif |
82049c54 | 1095 | } |
0f113f3e | 1096 | } |
d02b48c6 | 1097 | |
6b691a5c | 1098 | int ssl_verify_alarm_type(long type) |
0f113f3e MC |
1099 | { |
1100 | int al; | |
1101 | ||
1102 | switch (type) { | |
1103 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: | |
1104 | case X509_V_ERR_UNABLE_TO_GET_CRL: | |
1105 | case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: | |
1106 | al = SSL_AD_UNKNOWN_CA; | |
1107 | break; | |
1108 | case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: | |
1109 | case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: | |
1110 | case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: | |
1111 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: | |
1112 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: | |
1113 | case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: | |
1114 | case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: | |
1115 | case X509_V_ERR_CERT_NOT_YET_VALID: | |
1116 | case X509_V_ERR_CRL_NOT_YET_VALID: | |
1117 | case X509_V_ERR_CERT_UNTRUSTED: | |
1118 | case X509_V_ERR_CERT_REJECTED: | |
f3e235ed VD |
1119 | case X509_V_ERR_HOSTNAME_MISMATCH: |
1120 | case X509_V_ERR_EMAIL_MISMATCH: | |
1121 | case X509_V_ERR_IP_ADDRESS_MISMATCH: | |
1122 | case X509_V_ERR_DANE_NO_MATCH: | |
1123 | case X509_V_ERR_EE_KEY_TOO_SMALL: | |
1124 | case X509_V_ERR_CA_KEY_TOO_SMALL: | |
1125 | case X509_V_ERR_CA_MD_TOO_WEAK: | |
0f113f3e MC |
1126 | al = SSL_AD_BAD_CERTIFICATE; |
1127 | break; | |
1128 | case X509_V_ERR_CERT_SIGNATURE_FAILURE: | |
1129 | case X509_V_ERR_CRL_SIGNATURE_FAILURE: | |
1130 | al = SSL_AD_DECRYPT_ERROR; | |
1131 | break; | |
1132 | case X509_V_ERR_CERT_HAS_EXPIRED: | |
1133 | case X509_V_ERR_CRL_HAS_EXPIRED: | |
1134 | al = SSL_AD_CERTIFICATE_EXPIRED; | |
1135 | break; | |
1136 | case X509_V_ERR_CERT_REVOKED: | |
1137 | al = SSL_AD_CERTIFICATE_REVOKED; | |
1138 | break; | |
f3e235ed | 1139 | case X509_V_ERR_UNSPECIFIED: |
0f113f3e | 1140 | case X509_V_ERR_OUT_OF_MEM: |
f3e235ed VD |
1141 | case X509_V_ERR_INVALID_CALL: |
1142 | case X509_V_ERR_STORE_LOOKUP: | |
0f113f3e MC |
1143 | al = SSL_AD_INTERNAL_ERROR; |
1144 | break; | |
1145 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: | |
1146 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: | |
1147 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: | |
1148 | case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: | |
1149 | case X509_V_ERR_CERT_CHAIN_TOO_LONG: | |
1150 | case X509_V_ERR_PATH_LENGTH_EXCEEDED: | |
1151 | case X509_V_ERR_INVALID_CA: | |
1152 | al = SSL_AD_UNKNOWN_CA; | |
1153 | break; | |
1154 | case X509_V_ERR_APPLICATION_VERIFICATION: | |
1155 | al = SSL_AD_HANDSHAKE_FAILURE; | |
1156 | break; | |
1157 | case X509_V_ERR_INVALID_PURPOSE: | |
1158 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; | |
1159 | break; | |
1160 | default: | |
1161 | al = SSL_AD_CERTIFICATE_UNKNOWN; | |
1162 | break; | |
1163 | } | |
1164 | return (al); | |
1165 | } | |
d02b48c6 | 1166 | |
b362ccab | 1167 | int ssl_allow_compression(SSL *s) |
0f113f3e MC |
1168 | { |
1169 | if (s->options & SSL_OP_NO_COMPRESSION) | |
1170 | return 0; | |
1171 | return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL); | |
1172 | } | |
4fa52141 | 1173 | |
068c358a | 1174 | static int version_cmp(const SSL *s, int a, int b) |
4fa52141 VD |
1175 | { |
1176 | int dtls = SSL_IS_DTLS(s); | |
1177 | ||
1178 | if (a == b) | |
1179 | return 0; | |
1180 | if (!dtls) | |
1181 | return a < b ? -1 : 1; | |
1182 | return DTLS_VERSION_LT(a, b) ? -1 : 1; | |
1183 | } | |
1184 | ||
1185 | typedef struct { | |
1186 | int version; | |
a230b26e EK |
1187 | const SSL_METHOD *(*cmeth) (void); |
1188 | const SSL_METHOD *(*smeth) (void); | |
4fa52141 VD |
1189 | } version_info; |
1190 | ||
582a17d6 MC |
1191 | #if TLS_MAX_VERSION != TLS1_3_VERSION |
1192 | # error Code needs update for TLS_method() support beyond TLS1_3_VERSION. | |
4fa52141 VD |
1193 | #endif |
1194 | ||
1195 | static const version_info tls_version_table[] = { | |
582a17d6 MC |
1196 | #ifndef OPENSSL_NO_TLS1_3 |
1197 | {TLS1_3_VERSION, tlsv1_3_client_method, tlsv1_3_server_method}, | |
1198 | #else | |
1199 | {TLS1_3_VERSION, NULL, NULL}, | |
1200 | #endif | |
6b01bed2 | 1201 | #ifndef OPENSSL_NO_TLS1_2 |
a230b26e | 1202 | {TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method}, |
6b01bed2 | 1203 | #else |
a230b26e | 1204 | {TLS1_2_VERSION, NULL, NULL}, |
6b01bed2 VD |
1205 | #endif |
1206 | #ifndef OPENSSL_NO_TLS1_1 | |
a230b26e | 1207 | {TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method}, |
6b01bed2 | 1208 | #else |
a230b26e | 1209 | {TLS1_1_VERSION, NULL, NULL}, |
6b01bed2 VD |
1210 | #endif |
1211 | #ifndef OPENSSL_NO_TLS1 | |
a230b26e | 1212 | {TLS1_VERSION, tlsv1_client_method, tlsv1_server_method}, |
6b01bed2 | 1213 | #else |
a230b26e | 1214 | {TLS1_VERSION, NULL, NULL}, |
6b01bed2 | 1215 | #endif |
4fa52141 | 1216 | #ifndef OPENSSL_NO_SSL3 |
a230b26e | 1217 | {SSL3_VERSION, sslv3_client_method, sslv3_server_method}, |
6b01bed2 | 1218 | #else |
a230b26e | 1219 | {SSL3_VERSION, NULL, NULL}, |
4fa52141 | 1220 | #endif |
a230b26e | 1221 | {0, NULL, NULL}, |
4fa52141 VD |
1222 | }; |
1223 | ||
1224 | #if DTLS_MAX_VERSION != DTLS1_2_VERSION | |
1225 | # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION. | |
1226 | #endif | |
1227 | ||
1228 | static const version_info dtls_version_table[] = { | |
6b01bed2 | 1229 | #ifndef OPENSSL_NO_DTLS1_2 |
a230b26e | 1230 | {DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method}, |
6b01bed2 | 1231 | #else |
a230b26e | 1232 | {DTLS1_2_VERSION, NULL, NULL}, |
6b01bed2 VD |
1233 | #endif |
1234 | #ifndef OPENSSL_NO_DTLS1 | |
a230b26e EK |
1235 | {DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method}, |
1236 | {DTLS1_BAD_VER, dtls_bad_ver_client_method, NULL}, | |
6b01bed2 | 1237 | #else |
a230b26e EK |
1238 | {DTLS1_VERSION, NULL, NULL}, |
1239 | {DTLS1_BAD_VER, NULL, NULL}, | |
6b01bed2 | 1240 | #endif |
a230b26e | 1241 | {0, NULL, NULL}, |
4fa52141 VD |
1242 | }; |
1243 | ||
1244 | /* | |
1245 | * ssl_method_error - Check whether an SSL_METHOD is enabled. | |
1246 | * | |
1247 | * @s: The SSL handle for the candidate method | |
1248 | * @method: the intended method. | |
1249 | * | |
1250 | * Returns 0 on success, or an SSL error reason on failure. | |
1251 | */ | |
068c358a | 1252 | static int ssl_method_error(const SSL *s, const SSL_METHOD *method) |
4fa52141 VD |
1253 | { |
1254 | int version = method->version; | |
1255 | ||
1256 | if ((s->min_proto_version != 0 && | |
1257 | version_cmp(s, version, s->min_proto_version) < 0) || | |
1258 | ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0) | |
1259 | return SSL_R_VERSION_TOO_LOW; | |
1260 | ||
1261 | if (s->max_proto_version != 0 && | |
a230b26e | 1262 | version_cmp(s, version, s->max_proto_version) > 0) |
4fa52141 VD |
1263 | return SSL_R_VERSION_TOO_HIGH; |
1264 | ||
1265 | if ((s->options & method->mask) != 0) | |
1266 | return SSL_R_UNSUPPORTED_PROTOCOL; | |
1267 | if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s)) | |
1268 | return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE; | |
1269 | else if ((method->flags & SSL_METHOD_NO_FIPS) != 0 && FIPS_mode()) | |
1270 | return SSL_R_AT_LEAST_TLS_1_0_NEEDED_IN_FIPS_MODE; | |
1271 | ||
1272 | return 0; | |
1273 | } | |
1274 | ||
ccae4a15 FI |
1275 | /* |
1276 | * ssl_version_supported - Check that the specified `version` is supported by | |
1277 | * `SSL *` instance | |
1278 | * | |
1279 | * @s: The SSL handle for the candidate method | |
1280 | * @version: Protocol version to test against | |
1281 | * | |
1282 | * Returns 1 when supported, otherwise 0 | |
1283 | */ | |
1284 | int ssl_version_supported(const SSL *s, int version) | |
1285 | { | |
1286 | const version_info *vent; | |
1287 | const version_info *table; | |
1288 | ||
1289 | switch (s->method->version) { | |
1290 | default: | |
1291 | /* Version should match method version for non-ANY method */ | |
1292 | return version_cmp(s, version, s->version) == 0; | |
1293 | case TLS_ANY_VERSION: | |
1294 | table = tls_version_table; | |
1295 | break; | |
1296 | case DTLS_ANY_VERSION: | |
1297 | table = dtls_version_table; | |
1298 | break; | |
1299 | } | |
1300 | ||
1301 | for (vent = table; | |
1302 | vent->version != 0 && version_cmp(s, version, vent->version) <= 0; | |
1303 | ++vent) { | |
1304 | if (vent->cmeth != NULL && | |
1305 | version_cmp(s, version, vent->version) == 0 && | |
1306 | ssl_method_error(s, vent->cmeth()) == 0) { | |
1307 | return 1; | |
1308 | } | |
1309 | } | |
1310 | return 0; | |
1311 | } | |
1312 | ||
4fa52141 VD |
1313 | /* |
1314 | * ssl_check_version_downgrade - In response to RFC7507 SCSV version | |
1315 | * fallback indication from a client check whether we're using the highest | |
1316 | * supported protocol version. | |
1317 | * | |
1318 | * @s server SSL handle. | |
1319 | * | |
1320 | * Returns 1 when using the highest enabled version, 0 otherwise. | |
1321 | */ | |
1322 | int ssl_check_version_downgrade(SSL *s) | |
1323 | { | |
1324 | const version_info *vent; | |
1325 | const version_info *table; | |
1326 | ||
1327 | /* | |
1328 | * Check that the current protocol is the highest enabled version | |
1329 | * (according to s->ctx->method, as version negotiation may have changed | |
1330 | * s->method). | |
1331 | */ | |
1332 | if (s->version == s->ctx->method->version) | |
1333 | return 1; | |
1334 | ||
1335 | /* | |
1336 | * Apparently we're using a version-flexible SSL_METHOD (not at its | |
1337 | * highest protocol version). | |
1338 | */ | |
1339 | if (s->ctx->method->version == TLS_method()->version) | |
1340 | table = tls_version_table; | |
1341 | else if (s->ctx->method->version == DTLS_method()->version) | |
1342 | table = dtls_version_table; | |
1343 | else { | |
1344 | /* Unexpected state; fail closed. */ | |
1345 | return 0; | |
1346 | } | |
1347 | ||
1348 | for (vent = table; vent->version != 0; ++vent) { | |
a230b26e | 1349 | if (vent->smeth != NULL && ssl_method_error(s, vent->smeth()) == 0) |
4fa52141 VD |
1350 | return s->version == vent->version; |
1351 | } | |
1352 | return 0; | |
1353 | } | |
1354 | ||
1355 | /* | |
1356 | * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS | |
1357 | * protocols, provided the initial (D)TLS method is version-flexible. This | |
1358 | * function sanity-checks the proposed value and makes sure the method is | |
1359 | * version-flexible, then sets the limit if all is well. | |
1360 | * | |
1361 | * @method_version: The version of the current SSL_METHOD. | |
1362 | * @version: the intended limit. | |
1363 | * @bound: pointer to limit to be updated. | |
1364 | * | |
1365 | * Returns 1 on success, 0 on failure. | |
1366 | */ | |
1367 | int ssl_set_version_bound(int method_version, int version, int *bound) | |
1368 | { | |
869e978c KR |
1369 | if (version == 0) { |
1370 | *bound = version; | |
1371 | return 1; | |
1372 | } | |
1373 | ||
4fa52141 VD |
1374 | /*- |
1375 | * Restrict TLS methods to TLS protocol versions. | |
1376 | * Restrict DTLS methods to DTLS protocol versions. | |
1377 | * Note, DTLS version numbers are decreasing, use comparison macros. | |
1378 | * | |
1379 | * Note that for both lower-bounds we use explicit versions, not | |
1380 | * (D)TLS_MIN_VERSION. This is because we don't want to break user | |
1381 | * configurations. If the MIN (supported) version ever rises, the user's | |
1382 | * "floor" remains valid even if no longer available. We don't expect the | |
1383 | * MAX ceiling to ever get lower, so making that variable makes sense. | |
1384 | */ | |
1385 | switch (method_version) { | |
1386 | default: | |
1387 | /* | |
1388 | * XXX For fixed version methods, should we always fail and not set any | |
1389 | * bounds, always succeed and not set any bounds, or set the bounds and | |
1390 | * arrange to fail later if they are not met? At present fixed-version | |
1391 | * methods are not subject to controls that disable individual protocol | |
1392 | * versions. | |
1393 | */ | |
1394 | return 0; | |
1395 | ||
1396 | case TLS_ANY_VERSION: | |
1397 | if (version < SSL3_VERSION || version > TLS_MAX_VERSION) | |
1398 | return 0; | |
1399 | break; | |
1400 | ||
1401 | case DTLS_ANY_VERSION: | |
1402 | if (DTLS_VERSION_GT(version, DTLS_MAX_VERSION) || | |
032924c4 | 1403 | DTLS_VERSION_LT(version, DTLS1_BAD_VER)) |
4fa52141 VD |
1404 | return 0; |
1405 | break; | |
1406 | } | |
1407 | ||
1408 | *bound = version; | |
1409 | return 1; | |
1410 | } | |
1411 | ||
1412 | /* | |
1413 | * ssl_choose_server_version - Choose server (D)TLS version. Called when the | |
1414 | * client HELLO is received to select the final server protocol version and | |
1415 | * the version specific method. | |
1416 | * | |
1417 | * @s: server SSL handle. | |
1418 | * | |
1419 | * Returns 0 on success or an SSL error reason number on failure. | |
1420 | */ | |
1ab3836b | 1421 | int ssl_choose_server_version(SSL *s, CLIENTHELLO_MSG *hello) |
4fa52141 VD |
1422 | { |
1423 | /*- | |
1424 | * With version-flexible methods we have an initial state with: | |
1425 | * | |
1426 | * s->method->version == (D)TLS_ANY_VERSION, | |
1427 | * s->version == (D)TLS_MAX_VERSION. | |
1428 | * | |
1429 | * So we detect version-flexible methods via the method version, not the | |
1430 | * handle version. | |
1431 | */ | |
1432 | int server_version = s->method->version; | |
df7ce507 | 1433 | int client_version = hello->legacy_version; |
4fa52141 VD |
1434 | const version_info *vent; |
1435 | const version_info *table; | |
1436 | int disabled = 0; | |
cd998837 | 1437 | RAW_EXTENSION *suppversions; |
4fa52141 | 1438 | |
1ab3836b MC |
1439 | s->client_version = client_version; |
1440 | ||
4fa52141 VD |
1441 | switch (server_version) { |
1442 | default: | |
d2f42576 MC |
1443 | /* |
1444 | * TODO(TLS1.3): This check will fail if someone attempts to do | |
1445 | * renegotiation in TLS1.3 at the moment. We need to ensure we disable | |
1446 | * renegotiation for TLS1.3 | |
1447 | */ | |
4fa52141 VD |
1448 | if (version_cmp(s, client_version, s->version) < 0) |
1449 | return SSL_R_WRONG_SSL_VERSION; | |
1450 | /* | |
1451 | * If this SSL handle is not from a version flexible method we don't | |
1452 | * (and never did) check min/max FIPS or Suite B constraints. Hope | |
1453 | * that's OK. It is up to the caller to not choose fixed protocol | |
1454 | * versions they don't want. If not, then easy to fix, just return | |
1455 | * ssl_method_error(s, s->method) | |
1456 | */ | |
1457 | return 0; | |
1458 | case TLS_ANY_VERSION: | |
1459 | table = tls_version_table; | |
1460 | break; | |
1461 | case DTLS_ANY_VERSION: | |
1462 | table = dtls_version_table; | |
1463 | break; | |
1464 | } | |
1465 | ||
70af3d8e | 1466 | suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions]; |
cd998837 | 1467 | |
70af3d8e | 1468 | if (suppversions->present && !SSL_IS_DTLS(s)) { |
cd998837 MC |
1469 | unsigned int candidate_vers = 0; |
1470 | unsigned int best_vers = 0; | |
1471 | const SSL_METHOD *best_method = NULL; | |
1472 | PACKET versionslist; | |
1473 | ||
6b473aca MC |
1474 | suppversions->parsed = 1; |
1475 | ||
16bce0e0 | 1476 | if (!PACKET_as_length_prefixed_1(&suppversions->data, &versionslist)) { |
cd998837 MC |
1477 | /* Trailing or invalid data? */ |
1478 | return SSL_R_LENGTH_MISMATCH; | |
1479 | } | |
1480 | ||
1481 | while (PACKET_get_net_2(&versionslist, &candidate_vers)) { | |
1482 | /* TODO(TLS1.3): Remove this before release */ | |
1483 | if (candidate_vers == TLS1_3_VERSION_DRAFT) | |
1484 | candidate_vers = TLS1_3_VERSION; | |
f2342b7a MC |
1485 | /* |
1486 | * TODO(TLS1.3): There is some discussion on the TLS list about | |
1487 | * wheter to ignore versions <TLS1.2 in supported_versions. At the | |
1488 | * moment we honour them if present. To be reviewed later | |
1489 | */ | |
cd998837 MC |
1490 | if (version_cmp(s, candidate_vers, best_vers) <= 0) |
1491 | continue; | |
1492 | for (vent = table; | |
1493 | vent->version != 0 && vent->version != (int)candidate_vers; | |
16bce0e0 | 1494 | ++vent) |
bf0ba5e7 | 1495 | continue; |
bf85ef1b | 1496 | if (vent->version != 0 && vent->smeth != NULL) { |
cd998837 MC |
1497 | const SSL_METHOD *method; |
1498 | ||
1499 | method = vent->smeth(); | |
1500 | if (ssl_method_error(s, method) == 0) { | |
1501 | best_vers = candidate_vers; | |
1502 | best_method = method; | |
1503 | } | |
1504 | } | |
1505 | } | |
1506 | if (PACKET_remaining(&versionslist) != 0) { | |
1507 | /* Trailing data? */ | |
1508 | return SSL_R_LENGTH_MISMATCH; | |
1509 | } | |
1510 | ||
1511 | if (best_vers > 0) { | |
1512 | s->version = best_vers; | |
1513 | s->method = best_method; | |
1514 | return 0; | |
1515 | } | |
1516 | return SSL_R_UNSUPPORTED_PROTOCOL; | |
1517 | } | |
1518 | ||
1519 | /* | |
1520 | * If the supported versions extension isn't present, then the highest | |
1521 | * version we can negotiate is TLSv1.2 | |
1522 | */ | |
1523 | if (version_cmp(s, client_version, TLS1_3_VERSION) >= 0) | |
1524 | client_version = TLS1_2_VERSION; | |
1525 | ||
1526 | /* | |
1527 | * No supported versions extension, so we just use the version supplied in | |
1528 | * the ClientHello. | |
1529 | */ | |
4fa52141 VD |
1530 | for (vent = table; vent->version != 0; ++vent) { |
1531 | const SSL_METHOD *method; | |
1532 | ||
1533 | if (vent->smeth == NULL || | |
1534 | version_cmp(s, client_version, vent->version) < 0) | |
1535 | continue; | |
1536 | method = vent->smeth(); | |
1537 | if (ssl_method_error(s, method) == 0) { | |
1538 | s->version = vent->version; | |
1539 | s->method = method; | |
1540 | return 0; | |
1541 | } | |
1542 | disabled = 1; | |
1543 | } | |
1544 | return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW; | |
1545 | } | |
1546 | ||
1547 | /* | |
1548 | * ssl_choose_client_version - Choose client (D)TLS version. Called when the | |
1549 | * server HELLO is received to select the final client protocol version and | |
1550 | * the version specific method. | |
1551 | * | |
1552 | * @s: client SSL handle. | |
1553 | * @version: The proposed version from the server's HELLO. | |
1554 | * | |
1555 | * Returns 0 on success or an SSL error reason number on failure. | |
1556 | */ | |
1557 | int ssl_choose_client_version(SSL *s, int version) | |
1558 | { | |
1559 | const version_info *vent; | |
1560 | const version_info *table; | |
1561 | ||
b97667ce MC |
1562 | /* TODO(TLS1.3): Remove this before release */ |
1563 | if (version == TLS1_3_VERSION_DRAFT) | |
1564 | version = TLS1_3_VERSION; | |
1565 | ||
4fa52141 VD |
1566 | switch (s->method->version) { |
1567 | default: | |
1568 | if (version != s->version) | |
1569 | return SSL_R_WRONG_SSL_VERSION; | |
1570 | /* | |
1571 | * If this SSL handle is not from a version flexible method we don't | |
1572 | * (and never did) check min/max, FIPS or Suite B constraints. Hope | |
1573 | * that's OK. It is up to the caller to not choose fixed protocol | |
1574 | * versions they don't want. If not, then easy to fix, just return | |
1575 | * ssl_method_error(s, s->method) | |
1576 | */ | |
4fa52141 VD |
1577 | return 0; |
1578 | case TLS_ANY_VERSION: | |
1579 | table = tls_version_table; | |
1580 | break; | |
1581 | case DTLS_ANY_VERSION: | |
1582 | table = dtls_version_table; | |
1583 | break; | |
1584 | } | |
1585 | ||
1586 | for (vent = table; vent->version != 0; ++vent) { | |
1587 | const SSL_METHOD *method; | |
1588 | int err; | |
1589 | ||
1590 | if (version != vent->version) | |
1591 | continue; | |
1592 | if (vent->cmeth == NULL) | |
1593 | break; | |
1594 | method = vent->cmeth(); | |
1595 | err = ssl_method_error(s, method); | |
1596 | if (err != 0) | |
1597 | return err; | |
1598 | s->method = method; | |
ccae4a15 | 1599 | s->version = version; |
4fa52141 VD |
1600 | return 0; |
1601 | } | |
1602 | ||
1603 | return SSL_R_UNSUPPORTED_PROTOCOL; | |
1604 | } | |
1605 | ||
068c358a KR |
1606 | /* |
1607 | * ssl_get_client_min_max_version - get minimum and maximum client version | |
1608 | * @s: The SSL connection | |
1609 | * @min_version: The minimum supported version | |
1610 | * @max_version: The maximum supported version | |
1611 | * | |
1612 | * Work out what version we should be using for the initial ClientHello if the | |
1613 | * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx | |
1614 | * options, the MinProtocol and MaxProtocol configuration commands, any Suite B | |
1615 | * or FIPS_mode() constraints and any floor imposed by the security level here, | |
1616 | * so we don't advertise the wrong protocol version to only reject the outcome later. | |
4fa52141 | 1617 | * |
0485d540 | 1618 | * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled, |
4fa52141 VD |
1619 | * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol |
1620 | * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1. | |
1621 | * | |
068c358a KR |
1622 | * Returns 0 on success or an SSL error reason number on failure. On failure |
1623 | * min_version and max_version will also be set to 0. | |
4fa52141 | 1624 | */ |
a230b26e EK |
1625 | int ssl_get_client_min_max_version(const SSL *s, int *min_version, |
1626 | int *max_version) | |
4fa52141 VD |
1627 | { |
1628 | int version; | |
1629 | int hole; | |
1630 | const SSL_METHOD *single = NULL; | |
1631 | const SSL_METHOD *method; | |
1632 | const version_info *table; | |
1633 | const version_info *vent; | |
1634 | ||
1635 | switch (s->method->version) { | |
1636 | default: | |
1637 | /* | |
1638 | * If this SSL handle is not from a version flexible method we don't | |
1639 | * (and never did) check min/max FIPS or Suite B constraints. Hope | |
1640 | * that's OK. It is up to the caller to not choose fixed protocol | |
1641 | * versions they don't want. If not, then easy to fix, just return | |
1642 | * ssl_method_error(s, s->method) | |
1643 | */ | |
068c358a | 1644 | *min_version = *max_version = s->version; |
4fa52141 VD |
1645 | return 0; |
1646 | case TLS_ANY_VERSION: | |
1647 | table = tls_version_table; | |
1648 | break; | |
1649 | case DTLS_ANY_VERSION: | |
1650 | table = dtls_version_table; | |
1651 | break; | |
1652 | } | |
1653 | ||
1654 | /* | |
1655 | * SSL_OP_NO_X disables all protocols above X *if* there are some protocols | |
1656 | * below X enabled. This is required in order to maintain the "version | |
1657 | * capability" vector contiguous. Any versions with a NULL client method | |
1658 | * (protocol version client is disabled at compile-time) is also a "hole". | |
1659 | * | |
1660 | * Our initial state is hole == 1, version == 0. That is, versions above | |
1661 | * the first version in the method table are disabled (a "hole" above | |
1662 | * the valid protocol entries) and we don't have a selected version yet. | |
1663 | * | |
1664 | * Whenever "hole == 1", and we hit an enabled method, its version becomes | |
1665 | * the selected version, and the method becomes a candidate "single" | |
1666 | * method. We're no longer in a hole, so "hole" becomes 0. | |
1667 | * | |
1668 | * If "hole == 0" and we hit an enabled method, then "single" is cleared, | |
1669 | * as we support a contiguous range of at least two methods. If we hit | |
1670 | * a disabled method, then hole becomes true again, but nothing else | |
1671 | * changes yet, because all the remaining methods may be disabled too. | |
1672 | * If we again hit an enabled method after the new hole, it becomes | |
1673 | * selected, as we start from scratch. | |
1674 | */ | |
068c358a | 1675 | *min_version = version = 0; |
4fa52141 VD |
1676 | hole = 1; |
1677 | for (vent = table; vent->version != 0; ++vent) { | |
1678 | /* | |
1679 | * A table entry with a NULL client method is still a hole in the | |
1680 | * "version capability" vector. | |
1681 | */ | |
1682 | if (vent->cmeth == NULL) { | |
1683 | hole = 1; | |
1684 | continue; | |
1685 | } | |
1686 | method = vent->cmeth(); | |
1687 | if (ssl_method_error(s, method) != 0) { | |
1688 | hole = 1; | |
1689 | } else if (!hole) { | |
1690 | single = NULL; | |
068c358a | 1691 | *min_version = method->version; |
4fa52141 VD |
1692 | } else { |
1693 | version = (single = method)->version; | |
068c358a | 1694 | *min_version = version; |
4fa52141 VD |
1695 | hole = 0; |
1696 | } | |
1697 | } | |
1698 | ||
068c358a KR |
1699 | *max_version = version; |
1700 | ||
4fa52141 VD |
1701 | /* Fail if everything is disabled */ |
1702 | if (version == 0) | |
1703 | return SSL_R_NO_PROTOCOLS_AVAILABLE; | |
1704 | ||
068c358a KR |
1705 | return 0; |
1706 | } | |
1707 | ||
1708 | /* | |
1709 | * ssl_set_client_hello_version - Work out what version we should be using for | |
7acb8b64 | 1710 | * the initial ClientHello.legacy_version field. |
068c358a KR |
1711 | * |
1712 | * @s: client SSL handle. | |
1713 | * | |
1714 | * Returns 0 on success or an SSL error reason number on failure. | |
1715 | */ | |
1716 | int ssl_set_client_hello_version(SSL *s) | |
1717 | { | |
3eb2aff4 | 1718 | int ver_min, ver_max, ret; |
068c358a | 1719 | |
3eb2aff4 | 1720 | ret = ssl_get_client_min_max_version(s, &ver_min, &ver_max); |
068c358a KR |
1721 | |
1722 | if (ret != 0) | |
1723 | return ret; | |
1724 | ||
7acb8b64 MC |
1725 | s->version = ver_max; |
1726 | ||
1727 | /* TLS1.3 always uses TLS1.2 in the legacy_version field */ | |
1728 | if (!SSL_IS_DTLS(s) && ver_max > TLS1_2_VERSION) | |
1729 | ver_max = TLS1_2_VERSION; | |
1730 | ||
1731 | s->client_version = ver_max; | |
4fa52141 VD |
1732 | return 0; |
1733 | } |