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