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