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