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