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