VirtualBox

source: vbox/trunk/src/libs/openssl-3.3.2/ssl/statem/statem_lib.c@ 108669

Last change on this file since 108669 was 108206, checked in by vboxsync, 3 months ago

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 96.3 KB
Line 
1/*
2 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 *
5 * Licensed under the Apache License 2.0 (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
9 */
10
11#include <limits.h>
12#include <string.h>
13#include <stdio.h>
14#include "../ssl_local.h"
15#include "statem_local.h"
16#include "internal/cryptlib.h"
17#include <openssl/buffer.h>
18#include <openssl/objects.h>
19#include <openssl/evp.h>
20#include <openssl/rsa.h>
21#include <openssl/x509.h>
22#include <openssl/trace.h>
23#include <openssl/encoder.h>
24
25/*
26 * Map error codes to TLS/SSL alart types.
27 */
28typedef struct x509err2alert_st {
29 int x509err;
30 int alert;
31} X509ERR2ALERT;
32
33/* Fixed value used in the ServerHello random field to identify an HRR */
34const unsigned char hrrrandom[] = {
35 0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02,
36 0x1e, 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e,
37 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c
38};
39
40int ossl_statem_set_mutator(SSL *s,
41 ossl_statem_mutate_handshake_cb mutate_handshake_cb,
42 ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb,
43 void *mutatearg)
44{
45 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
46
47 if (sc == NULL)
48 return 0;
49
50 sc->statem.mutate_handshake_cb = mutate_handshake_cb;
51 sc->statem.mutatearg = mutatearg;
52 sc->statem.finish_mutate_handshake_cb = finish_mutate_handshake_cb;
53
54 return 1;
55}
56
57/*
58 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
59 * SSL3_RT_CHANGE_CIPHER_SPEC)
60 */
61int ssl3_do_write(SSL_CONNECTION *s, uint8_t type)
62{
63 int ret;
64 size_t written = 0;
65 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
66
67 /*
68 * If we're running the test suite then we may need to mutate the message
69 * we've been asked to write. Does not happen in normal operation.
70 */
71 if (s->statem.mutate_handshake_cb != NULL
72 && !s->statem.write_in_progress
73 && type == SSL3_RT_HANDSHAKE
74 && s->init_num >= SSL3_HM_HEADER_LENGTH) {
75 unsigned char *msg;
76 size_t msglen;
77
78 if (!s->statem.mutate_handshake_cb((unsigned char *)s->init_buf->data,
79 s->init_num,
80 &msg, &msglen,
81 s->statem.mutatearg))
82 return -1;
83 if (msglen < SSL3_HM_HEADER_LENGTH
84 || !BUF_MEM_grow(s->init_buf, msglen))
85 return -1;
86 memcpy(s->init_buf->data, msg, msglen);
87 s->init_num = msglen;
88 s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
89 s->statem.finish_mutate_handshake_cb(s->statem.mutatearg);
90 s->statem.write_in_progress = 1;
91 }
92
93 ret = ssl3_write_bytes(ssl, type, &s->init_buf->data[s->init_off],
94 s->init_num, &written);
95 if (ret <= 0)
96 return -1;
97 if (type == SSL3_RT_HANDSHAKE)
98 /*
99 * should not be done for 'Hello Request's, but in that case we'll
100 * ignore the result anyway
101 * TLS1.3 KeyUpdate and NewSessionTicket do not need to be added
102 */
103 if (!SSL_CONNECTION_IS_TLS13(s)
104 || (s->statem.hand_state != TLS_ST_SW_SESSION_TICKET
105 && s->statem.hand_state != TLS_ST_CW_KEY_UPDATE
106 && s->statem.hand_state != TLS_ST_SW_KEY_UPDATE))
107 if (!ssl3_finish_mac(s,
108 (unsigned char *)&s->init_buf->data[s->init_off],
109 written))
110 return -1;
111 if (written == s->init_num) {
112 s->statem.write_in_progress = 0;
113 if (s->msg_callback)
114 s->msg_callback(1, s->version, type, s->init_buf->data,
115 (size_t)(s->init_off + s->init_num), ssl,
116 s->msg_callback_arg);
117 return 1;
118 }
119 s->init_off += written;
120 s->init_num -= written;
121 return 0;
122}
123
124int tls_close_construct_packet(SSL_CONNECTION *s, WPACKET *pkt, int htype)
125{
126 size_t msglen;
127
128 if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))
129 || !WPACKET_get_length(pkt, &msglen)
130 || msglen > INT_MAX)
131 return 0;
132 s->init_num = (int)msglen;
133 s->init_off = 0;
134
135 return 1;
136}
137
138int tls_setup_handshake(SSL_CONNECTION *s)
139{
140 int ver_min, ver_max, ok;
141 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
142 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
143
144 if (!ssl3_init_finished_mac(s)) {
145 /* SSLfatal() already called */
146 return 0;
147 }
148
149 /* Reset any extension flags */
150 memset(s->ext.extflags, 0, sizeof(s->ext.extflags));
151
152 if (ssl_get_min_max_version(s, &ver_min, &ver_max, NULL) != 0) {
153 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
154 return 0;
155 }
156
157 /* Sanity check that we have MD5-SHA1 if we need it */
158 if (sctx->ssl_digest_methods[SSL_MD_MD5_SHA1_IDX] == NULL) {
159 int negotiated_minversion;
160 int md5sha1_needed_maxversion = SSL_CONNECTION_IS_DTLS(s)
161 ? DTLS1_VERSION : TLS1_1_VERSION;
162
163 /* We don't have MD5-SHA1 - do we need it? */
164 if (ssl_version_cmp(s, ver_max, md5sha1_needed_maxversion) <= 0) {
165 SSLfatal_data(s, SSL_AD_HANDSHAKE_FAILURE,
166 SSL_R_NO_SUITABLE_DIGEST_ALGORITHM,
167 "The max supported SSL/TLS version needs the"
168 " MD5-SHA1 digest but it is not available"
169 " in the loaded providers. Use (D)TLSv1.2 or"
170 " above, or load different providers");
171 return 0;
172 }
173
174 ok = 1;
175
176 /* Don't allow TLSv1.1 or below to be negotiated */
177 negotiated_minversion = SSL_CONNECTION_IS_DTLS(s) ?
178 DTLS1_2_VERSION : TLS1_2_VERSION;
179 if (ssl_version_cmp(s, ver_min, negotiated_minversion) < 0)
180 ok = SSL_set_min_proto_version(ssl, negotiated_minversion);
181 if (!ok) {
182 /* Shouldn't happen */
183 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR);
184 return 0;
185 }
186 }
187
188 ok = 0;
189 if (s->server) {
190 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
191 int i;
192
193 /*
194 * Sanity check that the maximum version we accept has ciphers
195 * enabled. For clients we do this check during construction of the
196 * ClientHello.
197 */
198 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
199 const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
200 int cipher_minprotover = SSL_CONNECTION_IS_DTLS(s)
201 ? c->min_dtls : c->min_tls;
202 int cipher_maxprotover = SSL_CONNECTION_IS_DTLS(s)
203 ? c->max_dtls : c->max_tls;
204
205 if (ssl_version_cmp(s, ver_max, cipher_minprotover) >= 0
206 && ssl_version_cmp(s, ver_max, cipher_maxprotover) <= 0) {
207 ok = 1;
208 break;
209 }
210 }
211 if (!ok) {
212 SSLfatal_data(s, SSL_AD_HANDSHAKE_FAILURE,
213 SSL_R_NO_CIPHERS_AVAILABLE,
214 "No ciphers enabled for max supported "
215 "SSL/TLS version");
216 return 0;
217 }
218 if (SSL_IS_FIRST_HANDSHAKE(s)) {
219 /* N.B. s->session_ctx == s->ctx here */
220 ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_accept);
221 } else {
222 /* N.B. s->ctx may not equal s->session_ctx */
223 ssl_tsan_counter(sctx, &sctx->stats.sess_accept_renegotiate);
224
225 s->s3.tmp.cert_request = 0;
226 }
227 } else {
228 if (SSL_IS_FIRST_HANDSHAKE(s))
229 ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_connect);
230 else
231 ssl_tsan_counter(s->session_ctx,
232 &s->session_ctx->stats.sess_connect_renegotiate);
233
234 /* mark client_random uninitialized */
235 memset(s->s3.client_random, 0, sizeof(s->s3.client_random));
236 s->hit = 0;
237
238 s->s3.tmp.cert_req = 0;
239
240 if (SSL_CONNECTION_IS_DTLS(s))
241 s->statem.use_timer = 1;
242 }
243
244 return 1;
245}
246
247/*
248 * Size of the to-be-signed TLS13 data, without the hash size itself:
249 * 64 bytes of value 32, 33 context bytes, 1 byte separator
250 */
251#define TLS13_TBS_START_SIZE 64
252#define TLS13_TBS_PREAMBLE_SIZE (TLS13_TBS_START_SIZE + 33 + 1)
253
254static int get_cert_verify_tbs_data(SSL_CONNECTION *s, unsigned char *tls13tbs,
255 void **hdata, size_t *hdatalen)
256{
257 /* ASCII: "TLS 1.3, server CertificateVerify", in hex for EBCDIC compatibility */
258 static const char servercontext[] = "\x54\x4c\x53\x20\x31\x2e\x33\x2c\x20\x73\x65\x72"
259 "\x76\x65\x72\x20\x43\x65\x72\x74\x69\x66\x69\x63\x61\x74\x65\x56\x65\x72\x69\x66\x79";
260 /* ASCII: "TLS 1.3, client CertificateVerify", in hex for EBCDIC compatibility */
261 static const char clientcontext[] = "\x54\x4c\x53\x20\x31\x2e\x33\x2c\x20\x63\x6c\x69"
262 "\x65\x6e\x74\x20\x43\x65\x72\x74\x69\x66\x69\x63\x61\x74\x65\x56\x65\x72\x69\x66\x79";
263
264 if (SSL_CONNECTION_IS_TLS13(s)) {
265 size_t hashlen;
266
267 /* Set the first 64 bytes of to-be-signed data to octet 32 */
268 memset(tls13tbs, 32, TLS13_TBS_START_SIZE);
269 /* This copies the 33 bytes of context plus the 0 separator byte */
270 if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
271 || s->statem.hand_state == TLS_ST_SW_CERT_VRFY)
272 strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, servercontext);
273 else
274 strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, clientcontext);
275
276 /*
277 * If we're currently reading then we need to use the saved handshake
278 * hash value. We can't use the current handshake hash state because
279 * that includes the CertVerify itself.
280 */
281 if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
282 || s->statem.hand_state == TLS_ST_SR_CERT_VRFY) {
283 memcpy(tls13tbs + TLS13_TBS_PREAMBLE_SIZE, s->cert_verify_hash,
284 s->cert_verify_hash_len);
285 hashlen = s->cert_verify_hash_len;
286 } else if (!ssl_handshake_hash(s, tls13tbs + TLS13_TBS_PREAMBLE_SIZE,
287 EVP_MAX_MD_SIZE, &hashlen)) {
288 /* SSLfatal() already called */
289 return 0;
290 }
291
292 *hdata = tls13tbs;
293 *hdatalen = TLS13_TBS_PREAMBLE_SIZE + hashlen;
294 } else {
295 size_t retlen;
296 long retlen_l;
297
298 retlen = retlen_l = BIO_get_mem_data(s->s3.handshake_buffer, hdata);
299 if (retlen_l <= 0) {
300 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
301 return 0;
302 }
303 *hdatalen = retlen;
304 }
305
306 return 1;
307}
308
309CON_FUNC_RETURN tls_construct_cert_verify(SSL_CONNECTION *s, WPACKET *pkt)
310{
311 EVP_PKEY *pkey = NULL;
312 const EVP_MD *md = NULL;
313 EVP_MD_CTX *mctx = NULL;
314 EVP_PKEY_CTX *pctx = NULL;
315 size_t hdatalen = 0, siglen = 0;
316 void *hdata;
317 unsigned char *sig = NULL;
318 unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
319 const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg;
320 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
321
322 if (lu == NULL || s->s3.tmp.cert == NULL) {
323 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
324 goto err;
325 }
326 pkey = s->s3.tmp.cert->privatekey;
327
328 if (pkey == NULL || !tls1_lookup_md(sctx, lu, &md)) {
329 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
330 goto err;
331 }
332
333 mctx = EVP_MD_CTX_new();
334 if (mctx == NULL) {
335 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
336 goto err;
337 }
338
339 /* Get the data to be signed */
340 if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
341 /* SSLfatal() already called */
342 goto err;
343 }
344
345 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
346 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
347 goto err;
348 }
349
350 if (EVP_DigestSignInit_ex(mctx, &pctx,
351 md == NULL ? NULL : EVP_MD_get0_name(md),
352 sctx->libctx, sctx->propq, pkey,
353 NULL) <= 0) {
354 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
355 goto err;
356 }
357
358 if (lu->sig == EVP_PKEY_RSA_PSS) {
359 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
360 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
361 RSA_PSS_SALTLEN_DIGEST) <= 0) {
362 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
363 goto err;
364 }
365 }
366 if (s->version == SSL3_VERSION) {
367 /*
368 * Here we use EVP_DigestSignUpdate followed by EVP_DigestSignFinal
369 * in order to add the EVP_CTRL_SSL3_MASTER_SECRET call between them.
370 */
371 if (EVP_DigestSignUpdate(mctx, hdata, hdatalen) <= 0
372 || EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
373 (int)s->session->master_key_length,
374 s->session->master_key) <= 0
375 || EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0) {
376
377 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
378 goto err;
379 }
380 sig = OPENSSL_malloc(siglen);
381 if (sig == NULL
382 || EVP_DigestSignFinal(mctx, sig, &siglen) <= 0) {
383 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
384 goto err;
385 }
386 } else {
387 /*
388 * Here we *must* use EVP_DigestSign() because Ed25519/Ed448 does not
389 * support streaming via EVP_DigestSignUpdate/EVP_DigestSignFinal
390 */
391 if (EVP_DigestSign(mctx, NULL, &siglen, hdata, hdatalen) <= 0) {
392 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
393 goto err;
394 }
395 sig = OPENSSL_malloc(siglen);
396 if (sig == NULL
397 || EVP_DigestSign(mctx, sig, &siglen, hdata, hdatalen) <= 0) {
398 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
399 goto err;
400 }
401 }
402
403#ifndef OPENSSL_NO_GOST
404 {
405 int pktype = lu->sig;
406
407 if (pktype == NID_id_GostR3410_2001
408 || pktype == NID_id_GostR3410_2012_256
409 || pktype == NID_id_GostR3410_2012_512)
410 BUF_reverse(sig, NULL, siglen);
411 }
412#endif
413
414 if (!WPACKET_sub_memcpy_u16(pkt, sig, siglen)) {
415 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
416 goto err;
417 }
418
419 /* Digest cached records and discard handshake buffer */
420 if (!ssl3_digest_cached_records(s, 0)) {
421 /* SSLfatal() already called */
422 goto err;
423 }
424
425 OPENSSL_free(sig);
426 EVP_MD_CTX_free(mctx);
427 return CON_FUNC_SUCCESS;
428 err:
429 OPENSSL_free(sig);
430 EVP_MD_CTX_free(mctx);
431 return CON_FUNC_ERROR;
432}
433
434MSG_PROCESS_RETURN tls_process_cert_verify(SSL_CONNECTION *s, PACKET *pkt)
435{
436 EVP_PKEY *pkey = NULL;
437 const unsigned char *data;
438#ifndef OPENSSL_NO_GOST
439 unsigned char *gost_data = NULL;
440#endif
441 MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
442 int j;
443 unsigned int len;
444 const EVP_MD *md = NULL;
445 size_t hdatalen = 0;
446 void *hdata;
447 unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
448 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
449 EVP_PKEY_CTX *pctx = NULL;
450 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
451
452 if (mctx == NULL) {
453 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
454 goto err;
455 }
456
457 pkey = tls_get_peer_pkey(s);
458 if (pkey == NULL) {
459 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
460 goto err;
461 }
462
463 if (ssl_cert_lookup_by_pkey(pkey, NULL, sctx) == NULL) {
464 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
465 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
466 goto err;
467 }
468
469 if (SSL_USE_SIGALGS(s)) {
470 unsigned int sigalg;
471
472 if (!PACKET_get_net_2(pkt, &sigalg)) {
473 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_PACKET);
474 goto err;
475 }
476 if (tls12_check_peer_sigalg(s, sigalg, pkey) <= 0) {
477 /* SSLfatal() already called */
478 goto err;
479 }
480 } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
481 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
482 SSL_R_LEGACY_SIGALG_DISALLOWED_OR_UNSUPPORTED);
483 goto err;
484 }
485
486 if (!tls1_lookup_md(sctx, s->s3.tmp.peer_sigalg, &md)) {
487 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
488 goto err;
489 }
490
491 if (SSL_USE_SIGALGS(s))
492 OSSL_TRACE1(TLS, "USING TLSv1.2 HASH %s\n",
493 md == NULL ? "n/a" : EVP_MD_get0_name(md));
494
495 /* Check for broken implementations of GOST ciphersuites */
496 /*
497 * If key is GOST and len is exactly 64 or 128, it is signature without
498 * length field (CryptoPro implementations at least till TLS 1.2)
499 */
500#ifndef OPENSSL_NO_GOST
501 if (!SSL_USE_SIGALGS(s)
502 && ((PACKET_remaining(pkt) == 64
503 && (EVP_PKEY_get_id(pkey) == NID_id_GostR3410_2001
504 || EVP_PKEY_get_id(pkey) == NID_id_GostR3410_2012_256))
505 || (PACKET_remaining(pkt) == 128
506 && EVP_PKEY_get_id(pkey) == NID_id_GostR3410_2012_512))) {
507 len = PACKET_remaining(pkt);
508 } else
509#endif
510 if (!PACKET_get_net_2(pkt, &len)) {
511 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
512 goto err;
513 }
514
515 if (!PACKET_get_bytes(pkt, &data, len)) {
516 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
517 goto err;
518 }
519 if (PACKET_remaining(pkt) != 0) {
520 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
521 goto err;
522 }
523
524 if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
525 /* SSLfatal() already called */
526 goto err;
527 }
528
529 OSSL_TRACE1(TLS, "Using client verify alg %s\n",
530 md == NULL ? "n/a" : EVP_MD_get0_name(md));
531
532 if (EVP_DigestVerifyInit_ex(mctx, &pctx,
533 md == NULL ? NULL : EVP_MD_get0_name(md),
534 sctx->libctx, sctx->propq, pkey,
535 NULL) <= 0) {
536 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
537 goto err;
538 }
539#ifndef OPENSSL_NO_GOST
540 {
541 int pktype = EVP_PKEY_get_id(pkey);
542 if (pktype == NID_id_GostR3410_2001
543 || pktype == NID_id_GostR3410_2012_256
544 || pktype == NID_id_GostR3410_2012_512) {
545 if ((gost_data = OPENSSL_malloc(len)) == NULL)
546 goto err;
547 BUF_reverse(gost_data, data, len);
548 data = gost_data;
549 }
550 }
551#endif
552
553 if (SSL_USE_PSS(s)) {
554 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
555 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
556 RSA_PSS_SALTLEN_DIGEST) <= 0) {
557 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
558 goto err;
559 }
560 }
561 if (s->version == SSL3_VERSION) {
562 if (EVP_DigestVerifyUpdate(mctx, hdata, hdatalen) <= 0
563 || EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
564 (int)s->session->master_key_length,
565 s->session->master_key) <= 0) {
566 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
567 goto err;
568 }
569 if (EVP_DigestVerifyFinal(mctx, data, len) <= 0) {
570 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_SIGNATURE);
571 goto err;
572 }
573 } else {
574 j = EVP_DigestVerify(mctx, data, len, hdata, hdatalen);
575#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
576 /* Ignore bad signatures when fuzzing */
577 if (SSL_IS_QUIC_HANDSHAKE(s))
578 j = 1;
579#endif
580 if (j <= 0) {
581 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_SIGNATURE);
582 goto err;
583 }
584 }
585
586 /*
587 * In TLSv1.3 on the client side we make sure we prepare the client
588 * certificate after the CertVerify instead of when we get the
589 * CertificateRequest. This is because in TLSv1.3 the CertificateRequest
590 * comes *before* the Certificate message. In TLSv1.2 it comes after. We
591 * want to make sure that SSL_get1_peer_certificate() will return the actual
592 * server certificate from the client_cert_cb callback.
593 */
594 if (!s->server && SSL_CONNECTION_IS_TLS13(s) && s->s3.tmp.cert_req == 1)
595 ret = MSG_PROCESS_CONTINUE_PROCESSING;
596 else
597 ret = MSG_PROCESS_CONTINUE_READING;
598 err:
599 BIO_free(s->s3.handshake_buffer);
600 s->s3.handshake_buffer = NULL;
601 EVP_MD_CTX_free(mctx);
602#ifndef OPENSSL_NO_GOST
603 OPENSSL_free(gost_data);
604#endif
605 return ret;
606}
607
608CON_FUNC_RETURN tls_construct_finished(SSL_CONNECTION *s, WPACKET *pkt)
609{
610 size_t finish_md_len;
611 const char *sender;
612 size_t slen;
613 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
614
615 /* This is a real handshake so make sure we clean it up at the end */
616 if (!s->server && s->post_handshake_auth != SSL_PHA_REQUESTED)
617 s->statem.cleanuphand = 1;
618
619 /*
620 * If we attempted to write early data or we're in middlebox compat mode
621 * then we deferred changing the handshake write keys to the last possible
622 * moment. If we didn't already do this when we sent the client certificate
623 * then we need to do it now.
624 */
625 if (SSL_CONNECTION_IS_TLS13(s)
626 && !s->server
627 && (s->early_data_state != SSL_EARLY_DATA_NONE
628 || (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
629 && s->s3.tmp.cert_req == 0
630 && (!ssl->method->ssl3_enc->change_cipher_state(s,
631 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {;
632 /* SSLfatal() already called */
633 return CON_FUNC_ERROR;
634 }
635
636 if (s->server) {
637 sender = ssl->method->ssl3_enc->server_finished_label;
638 slen = ssl->method->ssl3_enc->server_finished_label_len;
639 } else {
640 sender = ssl->method->ssl3_enc->client_finished_label;
641 slen = ssl->method->ssl3_enc->client_finished_label_len;
642 }
643
644 finish_md_len = ssl->method->ssl3_enc->final_finish_mac(s,
645 sender, slen,
646 s->s3.tmp.finish_md);
647 if (finish_md_len == 0) {
648 /* SSLfatal() already called */
649 return CON_FUNC_ERROR;
650 }
651
652 s->s3.tmp.finish_md_len = finish_md_len;
653
654 if (!WPACKET_memcpy(pkt, s->s3.tmp.finish_md, finish_md_len)) {
655 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
656 return CON_FUNC_ERROR;
657 }
658
659 /*
660 * Log the master secret, if logging is enabled. We don't log it for
661 * TLSv1.3: there's a different key schedule for that.
662 */
663 if (!SSL_CONNECTION_IS_TLS13(s)
664 && !ssl_log_secret(s, MASTER_SECRET_LABEL, s->session->master_key,
665 s->session->master_key_length)) {
666 /* SSLfatal() already called */
667 return CON_FUNC_ERROR;
668 }
669
670 /*
671 * Copy the finished so we can use it for renegotiation checks
672 */
673 if (!ossl_assert(finish_md_len <= EVP_MAX_MD_SIZE)) {
674 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
675 return CON_FUNC_ERROR;
676 }
677 if (!s->server) {
678 memcpy(s->s3.previous_client_finished, s->s3.tmp.finish_md,
679 finish_md_len);
680 s->s3.previous_client_finished_len = finish_md_len;
681 } else {
682 memcpy(s->s3.previous_server_finished, s->s3.tmp.finish_md,
683 finish_md_len);
684 s->s3.previous_server_finished_len = finish_md_len;
685 }
686
687 return CON_FUNC_SUCCESS;
688}
689
690CON_FUNC_RETURN tls_construct_key_update(SSL_CONNECTION *s, WPACKET *pkt)
691{
692 if (!WPACKET_put_bytes_u8(pkt, s->key_update)) {
693 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
694 return CON_FUNC_ERROR;
695 }
696
697 s->key_update = SSL_KEY_UPDATE_NONE;
698 return CON_FUNC_SUCCESS;
699}
700
701MSG_PROCESS_RETURN tls_process_key_update(SSL_CONNECTION *s, PACKET *pkt)
702{
703 unsigned int updatetype;
704
705 /*
706 * A KeyUpdate message signals a key change so the end of the message must
707 * be on a record boundary.
708 */
709 if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
710 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
711 return MSG_PROCESS_ERROR;
712 }
713
714 if (!PACKET_get_1(pkt, &updatetype)
715 || PACKET_remaining(pkt) != 0) {
716 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_KEY_UPDATE);
717 return MSG_PROCESS_ERROR;
718 }
719
720 /*
721 * There are only two defined key update types. Fail if we get a value we
722 * didn't recognise.
723 */
724 if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
725 && updatetype != SSL_KEY_UPDATE_REQUESTED) {
726 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_UPDATE);
727 return MSG_PROCESS_ERROR;
728 }
729
730 /*
731 * If we get a request for us to update our sending keys too then, we need
732 * to additionally send a KeyUpdate message. However that message should
733 * not also request an update (otherwise we get into an infinite loop).
734 */
735 if (updatetype == SSL_KEY_UPDATE_REQUESTED)
736 s->key_update = SSL_KEY_UPDATE_NOT_REQUESTED;
737
738 if (!tls13_update_key(s, 0)) {
739 /* SSLfatal() already called */
740 return MSG_PROCESS_ERROR;
741 }
742
743 return MSG_PROCESS_FINISHED_READING;
744}
745
746/*
747 * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
748 * to far.
749 */
750int ssl3_take_mac(SSL_CONNECTION *s)
751{
752 const char *sender;
753 size_t slen;
754 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
755
756 if (!s->server) {
757 sender = ssl->method->ssl3_enc->server_finished_label;
758 slen = ssl->method->ssl3_enc->server_finished_label_len;
759 } else {
760 sender = ssl->method->ssl3_enc->client_finished_label;
761 slen = ssl->method->ssl3_enc->client_finished_label_len;
762 }
763
764 s->s3.tmp.peer_finish_md_len =
765 ssl->method->ssl3_enc->final_finish_mac(s, sender, slen,
766 s->s3.tmp.peer_finish_md);
767
768 if (s->s3.tmp.peer_finish_md_len == 0) {
769 /* SSLfatal() already called */
770 return 0;
771 }
772
773 return 1;
774}
775
776MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL_CONNECTION *s,
777 PACKET *pkt)
778{
779 size_t remain;
780
781 remain = PACKET_remaining(pkt);
782 /*
783 * 'Change Cipher Spec' is just a single byte, which should already have
784 * been consumed by ssl_get_message() so there should be no bytes left,
785 * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
786 */
787 if (SSL_CONNECTION_IS_DTLS(s)) {
788 if ((s->version == DTLS1_BAD_VER
789 && remain != DTLS1_CCS_HEADER_LENGTH + 1)
790 || (s->version != DTLS1_BAD_VER
791 && remain != DTLS1_CCS_HEADER_LENGTH - 1)) {
792 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_CHANGE_CIPHER_SPEC);
793 return MSG_PROCESS_ERROR;
794 }
795 } else {
796 if (remain != 0) {
797 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_CHANGE_CIPHER_SPEC);
798 return MSG_PROCESS_ERROR;
799 }
800 }
801
802 /* Check we have a cipher to change to */
803 if (s->s3.tmp.new_cipher == NULL) {
804 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY);
805 return MSG_PROCESS_ERROR;
806 }
807
808 s->s3.change_cipher_spec = 1;
809 if (!ssl3_do_change_cipher_spec(s)) {
810 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
811 return MSG_PROCESS_ERROR;
812 }
813
814 if (SSL_CONNECTION_IS_DTLS(s)) {
815 if (s->version == DTLS1_BAD_VER)
816 s->d1->handshake_read_seq++;
817
818#ifndef OPENSSL_NO_SCTP
819 /*
820 * Remember that a CCS has been received, so that an old key of
821 * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
822 * SCTP is used
823 */
824 BIO_ctrl(SSL_get_wbio(SSL_CONNECTION_GET_SSL(s)),
825 BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
826#endif
827 }
828
829 return MSG_PROCESS_CONTINUE_READING;
830}
831
832MSG_PROCESS_RETURN tls_process_finished(SSL_CONNECTION *s, PACKET *pkt)
833{
834 size_t md_len;
835 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
836 int was_first = SSL_IS_FIRST_HANDSHAKE(s);
837 int ok;
838
839
840 /* This is a real handshake so make sure we clean it up at the end */
841 if (s->server) {
842 /*
843 * To get this far we must have read encrypted data from the client. We
844 * no longer tolerate unencrypted alerts. This is ignored if less than
845 * TLSv1.3
846 */
847 if (s->rlayer.rrlmethod->set_plain_alerts != NULL)
848 s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 0);
849 if (s->post_handshake_auth != SSL_PHA_REQUESTED)
850 s->statem.cleanuphand = 1;
851 if (SSL_CONNECTION_IS_TLS13(s)
852 && !tls13_save_handshake_digest_for_pha(s)) {
853 /* SSLfatal() already called */
854 return MSG_PROCESS_ERROR;
855 }
856 }
857
858 /*
859 * In TLSv1.3 a Finished message signals a key change so the end of the
860 * message must be on a record boundary.
861 */
862 if (SSL_CONNECTION_IS_TLS13(s)
863 && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
864 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
865 return MSG_PROCESS_ERROR;
866 }
867
868 /* If this occurs, we have missed a message */
869 if (!SSL_CONNECTION_IS_TLS13(s) && !s->s3.change_cipher_spec) {
870 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_GOT_A_FIN_BEFORE_A_CCS);
871 return MSG_PROCESS_ERROR;
872 }
873 s->s3.change_cipher_spec = 0;
874
875 md_len = s->s3.tmp.peer_finish_md_len;
876
877 if (md_len != PACKET_remaining(pkt)) {
878 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_DIGEST_LENGTH);
879 return MSG_PROCESS_ERROR;
880 }
881
882 ok = CRYPTO_memcmp(PACKET_data(pkt), s->s3.tmp.peer_finish_md,
883 md_len);
884#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
885 if (ok != 0) {
886 if ((PACKET_data(pkt)[0] ^ s->s3.tmp.peer_finish_md[0]) != 0xFF) {
887 ok = 0;
888 }
889 }
890#endif
891 if (ok != 0) {
892 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DIGEST_CHECK_FAILED);
893 return MSG_PROCESS_ERROR;
894 }
895
896 /*
897 * Copy the finished so we can use it for renegotiation checks
898 */
899 if (!ossl_assert(md_len <= EVP_MAX_MD_SIZE)) {
900 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
901 return MSG_PROCESS_ERROR;
902 }
903 if (s->server) {
904 memcpy(s->s3.previous_client_finished, s->s3.tmp.peer_finish_md,
905 md_len);
906 s->s3.previous_client_finished_len = md_len;
907 } else {
908 memcpy(s->s3.previous_server_finished, s->s3.tmp.peer_finish_md,
909 md_len);
910 s->s3.previous_server_finished_len = md_len;
911 }
912
913 /*
914 * In TLS1.3 we also have to change cipher state and do any final processing
915 * of the initial server flight (if we are a client)
916 */
917 if (SSL_CONNECTION_IS_TLS13(s)) {
918 if (s->server) {
919 if (s->post_handshake_auth != SSL_PHA_REQUESTED &&
920 !ssl->method->ssl3_enc->change_cipher_state(s,
921 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_READ)) {
922 /* SSLfatal() already called */
923 return MSG_PROCESS_ERROR;
924 }
925 } else {
926 /* TLS 1.3 gets the secret size from the handshake md */
927 size_t dummy;
928 if (!ssl->method->ssl3_enc->generate_master_secret(s,
929 s->master_secret, s->handshake_secret, 0,
930 &dummy)) {
931 /* SSLfatal() already called */
932 return MSG_PROCESS_ERROR;
933 }
934 if (!ssl->method->ssl3_enc->change_cipher_state(s,
935 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
936 /* SSLfatal() already called */
937 return MSG_PROCESS_ERROR;
938 }
939 if (!tls_process_initial_server_flight(s)) {
940 /* SSLfatal() already called */
941 return MSG_PROCESS_ERROR;
942 }
943 }
944 }
945
946 if (was_first
947 && !SSL_IS_FIRST_HANDSHAKE(s)
948 && s->rlayer.rrlmethod->set_first_handshake != NULL)
949 s->rlayer.rrlmethod->set_first_handshake(s->rlayer.rrl, 0);
950
951 return MSG_PROCESS_FINISHED_READING;
952}
953
954CON_FUNC_RETURN tls_construct_change_cipher_spec(SSL_CONNECTION *s, WPACKET *pkt)
955{
956 if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) {
957 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
958 return CON_FUNC_ERROR;
959 }
960
961 return CON_FUNC_SUCCESS;
962}
963
964/* Add a certificate to the WPACKET */
965static int ssl_add_cert_to_wpacket(SSL_CONNECTION *s, WPACKET *pkt,
966 X509 *x, int chain, int for_comp)
967{
968 int len;
969 unsigned char *outbytes;
970 int context = SSL_EXT_TLS1_3_CERTIFICATE;
971
972 if (for_comp)
973 context |= SSL_EXT_TLS1_3_CERTIFICATE_COMPRESSION;
974
975 len = i2d_X509(x, NULL);
976 if (len < 0) {
977 if (!for_comp)
978 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BUF_LIB);
979 return 0;
980 }
981 if (!WPACKET_sub_allocate_bytes_u24(pkt, len, &outbytes)
982 || i2d_X509(x, &outbytes) != len) {
983 if (!for_comp)
984 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
985 return 0;
986 }
987
988 if ((SSL_CONNECTION_IS_TLS13(s) || for_comp)
989 && !tls_construct_extensions(s, pkt, context, x, chain)) {
990 /* SSLfatal() already called */
991 return 0;
992 }
993
994 return 1;
995}
996
997/* Add certificate chain to provided WPACKET */
998static int ssl_add_cert_chain(SSL_CONNECTION *s, WPACKET *pkt, CERT_PKEY *cpk, int for_comp)
999{
1000 int i, chain_count;
1001 X509 *x;
1002 STACK_OF(X509) *extra_certs;
1003 STACK_OF(X509) *chain = NULL;
1004 X509_STORE *chain_store;
1005 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1006
1007 if (cpk == NULL || cpk->x509 == NULL)
1008 return 1;
1009
1010 x = cpk->x509;
1011
1012 /*
1013 * If we have a certificate specific chain use it, else use parent ctx.
1014 */
1015 if (cpk->chain != NULL)
1016 extra_certs = cpk->chain;
1017 else
1018 extra_certs = sctx->extra_certs;
1019
1020 if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
1021 chain_store = NULL;
1022 else if (s->cert->chain_store)
1023 chain_store = s->cert->chain_store;
1024 else
1025 chain_store = sctx->cert_store;
1026
1027 if (chain_store != NULL) {
1028 X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new_ex(sctx->libctx,
1029 sctx->propq);
1030
1031 if (xs_ctx == NULL) {
1032 if (!for_comp)
1033 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_X509_LIB);
1034 return 0;
1035 }
1036 if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {
1037 X509_STORE_CTX_free(xs_ctx);
1038 if (!for_comp)
1039 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_X509_LIB);
1040 return 0;
1041 }
1042 /*
1043 * It is valid for the chain not to be complete (because normally we
1044 * don't include the root cert in the chain). Therefore we deliberately
1045 * ignore the error return from this call. We're not actually verifying
1046 * the cert - we're just building as much of the chain as we can
1047 */
1048 (void)X509_verify_cert(xs_ctx);
1049 /* Don't leave errors in the queue */
1050 ERR_clear_error();
1051 chain = X509_STORE_CTX_get0_chain(xs_ctx);
1052 i = ssl_security_cert_chain(s, chain, NULL, 0);
1053 if (i != 1) {
1054#if 0
1055 /* Dummy error calls so mkerr generates them */
1056 ERR_raise(ERR_LIB_SSL, SSL_R_EE_KEY_TOO_SMALL);
1057 ERR_raise(ERR_LIB_SSL, SSL_R_CA_KEY_TOO_SMALL);
1058 ERR_raise(ERR_LIB_SSL, SSL_R_CA_MD_TOO_WEAK);
1059#endif
1060 X509_STORE_CTX_free(xs_ctx);
1061 if (!for_comp)
1062 SSLfatal(s, SSL_AD_INTERNAL_ERROR, i);
1063 return 0;
1064 }
1065 chain_count = sk_X509_num(chain);
1066 for (i = 0; i < chain_count; i++) {
1067 x = sk_X509_value(chain, i);
1068
1069 if (!ssl_add_cert_to_wpacket(s, pkt, x, i, for_comp)) {
1070 /* SSLfatal() already called */
1071 X509_STORE_CTX_free(xs_ctx);
1072 return 0;
1073 }
1074 }
1075 X509_STORE_CTX_free(xs_ctx);
1076 } else {
1077 i = ssl_security_cert_chain(s, extra_certs, x, 0);
1078 if (i != 1) {
1079 if (!for_comp)
1080 SSLfatal(s, SSL_AD_INTERNAL_ERROR, i);
1081 return 0;
1082 }
1083 if (!ssl_add_cert_to_wpacket(s, pkt, x, 0, for_comp)) {
1084 /* SSLfatal() already called */
1085 return 0;
1086 }
1087 for (i = 0; i < sk_X509_num(extra_certs); i++) {
1088 x = sk_X509_value(extra_certs, i);
1089 if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1, for_comp)) {
1090 /* SSLfatal() already called */
1091 return 0;
1092 }
1093 }
1094 }
1095 return 1;
1096}
1097
1098EVP_PKEY* tls_get_peer_pkey(const SSL_CONNECTION *sc)
1099{
1100 if (sc->session->peer_rpk != NULL)
1101 return sc->session->peer_rpk;
1102 if (sc->session->peer != NULL)
1103 return X509_get0_pubkey(sc->session->peer);
1104 return NULL;
1105}
1106
1107int tls_process_rpk(SSL_CONNECTION *sc, PACKET *pkt, EVP_PKEY **peer_rpk)
1108{
1109 EVP_PKEY *pkey = NULL;
1110 int ret = 0;
1111 RAW_EXTENSION *rawexts = NULL;
1112 PACKET extensions;
1113 PACKET context;
1114 unsigned long cert_len = 0, spki_len = 0;
1115 const unsigned char *spki, *spkistart;
1116 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(sc);
1117
1118 /*-
1119 * ----------------------------
1120 * TLS 1.3 Certificate message:
1121 * ----------------------------
1122 * https://datatracker.ietf.org/doc/html/rfc8446#section-4.4.2
1123 *
1124 * enum {
1125 * X509(0),
1126 * RawPublicKey(2),
1127 * (255)
1128 * } CertificateType;
1129 *
1130 * struct {
1131 * select (certificate_type) {
1132 * case RawPublicKey:
1133 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
1134 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
1135 *
1136 * case X509:
1137 * opaque cert_data<1..2^24-1>;
1138 * };
1139 * Extension extensions<0..2^16-1>;
1140 * } CertificateEntry;
1141 *
1142 * struct {
1143 * opaque certificate_request_context<0..2^8-1>;
1144 * CertificateEntry certificate_list<0..2^24-1>;
1145 * } Certificate;
1146 *
1147 * The client MUST send a Certificate message if and only if the server
1148 * has requested client authentication via a CertificateRequest message
1149 * (Section 4.3.2). If the server requests client authentication but no
1150 * suitable certificate is available, the client MUST send a Certificate
1151 * message containing no certificates (i.e., with the "certificate_list"
1152 * field having length 0).
1153 *
1154 * ----------------------------
1155 * TLS 1.2 Certificate message:
1156 * ----------------------------
1157 * https://datatracker.ietf.org/doc/html/rfc7250#section-3
1158 *
1159 * opaque ASN.1Cert<1..2^24-1>;
1160 *
1161 * struct {
1162 * select(certificate_type){
1163 *
1164 * // certificate type defined in this document.
1165 * case RawPublicKey:
1166 * opaque ASN.1_subjectPublicKeyInfo<1..2^24-1>;
1167 *
1168 * // X.509 certificate defined in RFC 5246
1169 * case X.509:
1170 * ASN.1Cert certificate_list<0..2^24-1>;
1171 *
1172 * // Additional certificate type based on
1173 * // "TLS Certificate Types" subregistry
1174 * };
1175 * } Certificate;
1176 *
1177 * -------------
1178 * Consequently:
1179 * -------------
1180 * After the (TLS 1.3 only) context octet string (1 byte length + data) the
1181 * Certificate message has a 3-byte length that is zero in the client to
1182 * server message when the client has no RPK to send. In that case, there
1183 * are no (TLS 1.3 only) per-certificate extensions either, because the
1184 * [CertificateEntry] list is empty.
1185 *
1186 * In the server to client direction, or when the client had an RPK to send,
1187 * the TLS 1.3 message just prepends the length of the RPK+extensions,
1188 * while TLS <= 1.2 sends just the RPK (octet-string).
1189 *
1190 * The context must be zero-length in the server to client direction, and
1191 * must match the value recorded in the certificate request in the client
1192 * to server direction.
1193 */
1194 if (SSL_CONNECTION_IS_TLS13(sc)) {
1195 if (!PACKET_get_length_prefixed_1(pkt, &context)) {
1196 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT);
1197 goto err;
1198 }
1199 if (sc->server) {
1200 if (sc->pha_context == NULL) {
1201 if (PACKET_remaining(&context) != 0) {
1202 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT);
1203 goto err;
1204 }
1205 } else {
1206 if (!PACKET_equal(&context, sc->pha_context, sc->pha_context_len)) {
1207 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT);
1208 goto err;
1209 }
1210 }
1211 } else {
1212 if (PACKET_remaining(&context) != 0) {
1213 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT);
1214 goto err;
1215 }
1216 }
1217 }
1218
1219 if (!PACKET_get_net_3(pkt, &cert_len)
1220 || PACKET_remaining(pkt) != cert_len) {
1221 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1222 goto err;
1223 }
1224
1225 /*
1226 * The list length may be zero when there is no RPK. In the case of TLS
1227 * 1.2 this is actually the RPK length, which cannot be zero as specified,
1228 * but that breaks the ability of the client to decline client auth. We
1229 * overload the 0 RPK length to mean "no RPK". This interpretation is
1230 * also used some other (reference?) implementations, but is not supported
1231 * by the verbatim RFC7250 text.
1232 */
1233 if (cert_len == 0)
1234 return 1;
1235
1236 if (SSL_CONNECTION_IS_TLS13(sc)) {
1237 /*
1238 * With TLS 1.3, a non-empty explicit-length RPK octet-string followed
1239 * by a possibly empty extension block.
1240 */
1241 if (!PACKET_get_net_3(pkt, &spki_len)) {
1242 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1243 goto err;
1244 }
1245 if (spki_len == 0) {
1246 /* empty RPK */
1247 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_EMPTY_RAW_PUBLIC_KEY);
1248 goto err;
1249 }
1250 } else {
1251 spki_len = cert_len;
1252 }
1253
1254 if (!PACKET_get_bytes(pkt, &spki, spki_len)) {
1255 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1256 goto err;
1257 }
1258 spkistart = spki;
1259 if ((pkey = d2i_PUBKEY_ex(NULL, &spki, spki_len, sctx->libctx, sctx->propq)) == NULL
1260 || spki != (spkistart + spki_len)) {
1261 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1262 goto err;
1263 }
1264 if (EVP_PKEY_missing_parameters(pkey)) {
1265 SSLfatal(sc, SSL_AD_INTERNAL_ERROR,
1266 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1267 goto err;
1268 }
1269
1270 /* Process the Extensions block */
1271 if (SSL_CONNECTION_IS_TLS13(sc)) {
1272 if (PACKET_remaining(pkt) != (cert_len - 3 - spki_len)) {
1273 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
1274 goto err;
1275 }
1276 if (!PACKET_as_length_prefixed_2(pkt, &extensions)
1277 || PACKET_remaining(pkt) != 0) {
1278 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1279 goto err;
1280 }
1281 if (!tls_collect_extensions(sc, &extensions, SSL_EXT_TLS1_3_RAW_PUBLIC_KEY,
1282 &rawexts, NULL, 1)) {
1283 /* SSLfatal already called */
1284 goto err;
1285 }
1286 /* chain index is always zero and fin always 1 for RPK */
1287 if (!tls_parse_all_extensions(sc, SSL_EXT_TLS1_3_RAW_PUBLIC_KEY,
1288 rawexts, NULL, 0, 1)) {
1289 /* SSLfatal already called */
1290 goto err;
1291 }
1292 }
1293 ret = 1;
1294 if (peer_rpk != NULL) {
1295 *peer_rpk = pkey;
1296 pkey = NULL;
1297 }
1298
1299 err:
1300 OPENSSL_free(rawexts);
1301 EVP_PKEY_free(pkey);
1302 return ret;
1303}
1304
1305unsigned long tls_output_rpk(SSL_CONNECTION *sc, WPACKET *pkt, CERT_PKEY *cpk)
1306{
1307 int pdata_len = 0;
1308 unsigned char *pdata = NULL;
1309 X509_PUBKEY *xpk = NULL;
1310 unsigned long ret = 0;
1311 X509 *x509 = NULL;
1312
1313 if (cpk != NULL && cpk->x509 != NULL) {
1314 x509 = cpk->x509;
1315 /* Get the RPK from the certificate */
1316 xpk = X509_get_X509_PUBKEY(cpk->x509);
1317 if (xpk == NULL) {
1318 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1319 goto err;
1320 }
1321 pdata_len = i2d_X509_PUBKEY(xpk, &pdata);
1322 } else if (cpk != NULL && cpk->privatekey != NULL) {
1323 /* Get the RPK from the private key */
1324 pdata_len = i2d_PUBKEY(cpk->privatekey, &pdata);
1325 } else {
1326 /* The server RPK is not optional */
1327 if (sc->server) {
1328 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1329 goto err;
1330 }
1331 /* The client can send a zero length certificate list */
1332 if (!WPACKET_sub_memcpy_u24(pkt, pdata, pdata_len)) {
1333 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1334 goto err;
1335 }
1336 return 1;
1337 }
1338
1339 if (pdata_len <= 0) {
1340 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1341 goto err;
1342 }
1343
1344 /*
1345 * TLSv1.2 is _just_ the raw public key
1346 * TLSv1.3 includes extensions, so there's a length wrapper
1347 */
1348 if (SSL_CONNECTION_IS_TLS13(sc)) {
1349 if (!WPACKET_start_sub_packet_u24(pkt)) {
1350 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1351 goto err;
1352 }
1353 }
1354
1355 if (!WPACKET_sub_memcpy_u24(pkt, pdata, pdata_len)) {
1356 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1357 goto err;
1358 }
1359
1360 if (SSL_CONNECTION_IS_TLS13(sc)) {
1361 /*
1362 * Only send extensions relevant to raw public keys. Until such
1363 * extensions are defined, this will be an empty set of extensions.
1364 * |x509| may be NULL, which raw public-key extensions need to handle.
1365 */
1366 if (!tls_construct_extensions(sc, pkt, SSL_EXT_TLS1_3_RAW_PUBLIC_KEY,
1367 x509, 0)) {
1368 /* SSLfatal() already called */
1369 goto err;
1370 }
1371 if (!WPACKET_close(pkt)) {
1372 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1373 goto err;
1374 }
1375 }
1376
1377 ret = 1;
1378 err:
1379 OPENSSL_free(pdata);
1380 return ret;
1381}
1382
1383unsigned long ssl3_output_cert_chain(SSL_CONNECTION *s, WPACKET *pkt,
1384 CERT_PKEY *cpk, int for_comp)
1385{
1386 if (!WPACKET_start_sub_packet_u24(pkt)) {
1387 if (!for_comp)
1388 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1389 return 0;
1390 }
1391
1392 if (!ssl_add_cert_chain(s, pkt, cpk, for_comp))
1393 return 0;
1394
1395 if (!WPACKET_close(pkt)) {
1396 if (!for_comp)
1397 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1398 return 0;
1399 }
1400
1401 return 1;
1402}
1403
1404/*
1405 * Tidy up after the end of a handshake. In the case of SCTP this may result
1406 * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is
1407 * freed up as well.
1408 */
1409WORK_STATE tls_finish_handshake(SSL_CONNECTION *s, ossl_unused WORK_STATE wst,
1410 int clearbufs, int stop)
1411{
1412 void (*cb) (const SSL *ssl, int type, int val) = NULL;
1413 int cleanuphand = s->statem.cleanuphand;
1414 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1415 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1416
1417 if (clearbufs) {
1418 if (!SSL_CONNECTION_IS_DTLS(s)
1419#ifndef OPENSSL_NO_SCTP
1420 /*
1421 * RFC6083: SCTP provides a reliable and in-sequence transport service for DTLS
1422 * messages that require it. Therefore, DTLS procedures for retransmissions
1423 * MUST NOT be used.
1424 * Hence the init_buf can be cleared when DTLS over SCTP as transport is used.
1425 */
1426 || BIO_dgram_is_sctp(SSL_get_wbio(ssl))
1427#endif
1428 ) {
1429 /*
1430 * We don't do this in DTLS over UDP because we may still need the init_buf
1431 * in case there are any unexpected retransmits
1432 */
1433 BUF_MEM_free(s->init_buf);
1434 s->init_buf = NULL;
1435 }
1436
1437 if (!ssl_free_wbio_buffer(s)) {
1438 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1439 return WORK_ERROR;
1440 }
1441 s->init_num = 0;
1442 }
1443
1444 if (SSL_CONNECTION_IS_TLS13(s) && !s->server
1445 && s->post_handshake_auth == SSL_PHA_REQUESTED)
1446 s->post_handshake_auth = SSL_PHA_EXT_SENT;
1447
1448 /*
1449 * Only set if there was a Finished message and this isn't after a TLSv1.3
1450 * post handshake exchange
1451 */
1452 if (cleanuphand) {
1453 /* skipped if we just sent a HelloRequest */
1454 s->renegotiate = 0;
1455 s->new_session = 0;
1456 s->statem.cleanuphand = 0;
1457 s->ext.ticket_expected = 0;
1458
1459 ssl3_cleanup_key_block(s);
1460
1461 if (s->server) {
1462 /*
1463 * In TLSv1.3 we update the cache as part of constructing the
1464 * NewSessionTicket
1465 */
1466 if (!SSL_CONNECTION_IS_TLS13(s))
1467 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
1468
1469 /* N.B. s->ctx may not equal s->session_ctx */
1470 ssl_tsan_counter(sctx, &sctx->stats.sess_accept_good);
1471 s->handshake_func = ossl_statem_accept;
1472 } else {
1473 if (SSL_CONNECTION_IS_TLS13(s)) {
1474 /*
1475 * We encourage applications to only use TLSv1.3 tickets once,
1476 * so we remove this one from the cache.
1477 */
1478 if ((s->session_ctx->session_cache_mode
1479 & SSL_SESS_CACHE_CLIENT) != 0)
1480 SSL_CTX_remove_session(s->session_ctx, s->session);
1481 } else {
1482 /*
1483 * In TLSv1.3 we update the cache as part of processing the
1484 * NewSessionTicket
1485 */
1486 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
1487 }
1488 if (s->hit)
1489 ssl_tsan_counter(s->session_ctx,
1490 &s->session_ctx->stats.sess_hit);
1491
1492 s->handshake_func = ossl_statem_connect;
1493 ssl_tsan_counter(s->session_ctx,
1494 &s->session_ctx->stats.sess_connect_good);
1495 }
1496
1497 if (SSL_CONNECTION_IS_DTLS(s)) {
1498 /* done with handshaking */
1499 s->d1->handshake_read_seq = 0;
1500 s->d1->handshake_write_seq = 0;
1501 s->d1->next_handshake_write_seq = 0;
1502 dtls1_clear_received_buffer(s);
1503 }
1504 }
1505
1506 if (s->info_callback != NULL)
1507 cb = s->info_callback;
1508 else if (sctx->info_callback != NULL)
1509 cb = sctx->info_callback;
1510
1511 /* The callback may expect us to not be in init at handshake done */
1512 ossl_statem_set_in_init(s, 0);
1513
1514 if (cb != NULL) {
1515 if (cleanuphand
1516 || !SSL_CONNECTION_IS_TLS13(s)
1517 || SSL_IS_FIRST_HANDSHAKE(s))
1518 cb(ssl, SSL_CB_HANDSHAKE_DONE, 1);
1519 }
1520
1521 if (!stop) {
1522 /* If we've got more work to do we go back into init */
1523 ossl_statem_set_in_init(s, 1);
1524 return WORK_FINISHED_CONTINUE;
1525 }
1526
1527 return WORK_FINISHED_STOP;
1528}
1529
1530int tls_get_message_header(SSL_CONNECTION *s, int *mt)
1531{
1532 /* s->init_num < SSL3_HM_HEADER_LENGTH */
1533 int skip_message, i;
1534 uint8_t recvd_type;
1535 unsigned char *p;
1536 size_t l, readbytes;
1537 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1538
1539 p = (unsigned char *)s->init_buf->data;
1540
1541 do {
1542 while (s->init_num < SSL3_HM_HEADER_LENGTH) {
1543 i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, &recvd_type,
1544 &p[s->init_num],
1545 SSL3_HM_HEADER_LENGTH - s->init_num,
1546 0, &readbytes);
1547 if (i <= 0) {
1548 s->rwstate = SSL_READING;
1549 return 0;
1550 }
1551 if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1552 /*
1553 * A ChangeCipherSpec must be a single byte and may not occur
1554 * in the middle of a handshake message.
1555 */
1556 if (s->init_num != 0 || readbytes != 1 || p[0] != SSL3_MT_CCS) {
1557 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1558 SSL_R_BAD_CHANGE_CIPHER_SPEC);
1559 return 0;
1560 }
1561 if (s->statem.hand_state == TLS_ST_BEFORE
1562 && (s->s3.flags & TLS1_FLAGS_STATELESS) != 0) {
1563 /*
1564 * We are stateless and we received a CCS. Probably this is
1565 * from a client between the first and second ClientHellos.
1566 * We should ignore this, but return an error because we do
1567 * not return success until we see the second ClientHello
1568 * with a valid cookie.
1569 */
1570 return 0;
1571 }
1572 s->s3.tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
1573 s->init_num = readbytes - 1;
1574 s->init_msg = s->init_buf->data;
1575 s->s3.tmp.message_size = readbytes;
1576 return 1;
1577 } else if (recvd_type != SSL3_RT_HANDSHAKE) {
1578 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1579 SSL_R_CCS_RECEIVED_EARLY);
1580 return 0;
1581 }
1582 s->init_num += readbytes;
1583 }
1584
1585 skip_message = 0;
1586 if (!s->server)
1587 if (s->statem.hand_state != TLS_ST_OK
1588 && p[0] == SSL3_MT_HELLO_REQUEST)
1589 /*
1590 * The server may always send 'Hello Request' messages --
1591 * we are doing a handshake anyway now, so ignore them if
1592 * their format is correct. Does not count for 'Finished'
1593 * MAC.
1594 */
1595 if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
1596 s->init_num = 0;
1597 skip_message = 1;
1598
1599 if (s->msg_callback)
1600 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
1601 p, SSL3_HM_HEADER_LENGTH, ssl,
1602 s->msg_callback_arg);
1603 }
1604 } while (skip_message);
1605 /* s->init_num == SSL3_HM_HEADER_LENGTH */
1606
1607 *mt = *p;
1608 s->s3.tmp.message_type = *(p++);
1609
1610 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1611 /*
1612 * Only happens with SSLv3+ in an SSLv2 backward compatible
1613 * ClientHello
1614 *
1615 * Total message size is the remaining record bytes to read
1616 * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
1617 */
1618 l = s->rlayer.tlsrecs[0].length + SSL3_HM_HEADER_LENGTH;
1619 s->s3.tmp.message_size = l;
1620
1621 s->init_msg = s->init_buf->data;
1622 s->init_num = SSL3_HM_HEADER_LENGTH;
1623 } else {
1624 n2l3(p, l);
1625 /* BUF_MEM_grow takes an 'int' parameter */
1626 if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
1627 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1628 SSL_R_EXCESSIVE_MESSAGE_SIZE);
1629 return 0;
1630 }
1631 s->s3.tmp.message_size = l;
1632
1633 s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
1634 s->init_num = 0;
1635 }
1636
1637 return 1;
1638}
1639
1640int tls_get_message_body(SSL_CONNECTION *s, size_t *len)
1641{
1642 size_t n, readbytes;
1643 unsigned char *p;
1644 int i;
1645 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1646
1647 if (s->s3.tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
1648 /* We've already read everything in */
1649 *len = (unsigned long)s->init_num;
1650 return 1;
1651 }
1652
1653 p = s->init_msg;
1654 n = s->s3.tmp.message_size - s->init_num;
1655 while (n > 0) {
1656 i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
1657 &p[s->init_num], n, 0, &readbytes);
1658 if (i <= 0) {
1659 s->rwstate = SSL_READING;
1660 *len = 0;
1661 return 0;
1662 }
1663 s->init_num += readbytes;
1664 n -= readbytes;
1665 }
1666
1667 /*
1668 * If receiving Finished, record MAC of prior handshake messages for
1669 * Finished verification.
1670 */
1671 if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) {
1672 /* SSLfatal() already called */
1673 *len = 0;
1674 return 0;
1675 }
1676
1677 /* Feed this message into MAC computation. */
1678 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1679 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1680 s->init_num)) {
1681 /* SSLfatal() already called */
1682 *len = 0;
1683 return 0;
1684 }
1685 if (s->msg_callback)
1686 s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data,
1687 (size_t)s->init_num, ssl, s->msg_callback_arg);
1688 } else {
1689 /*
1690 * We defer feeding in the HRR until later. We'll do it as part of
1691 * processing the message
1692 * The TLsv1.3 handshake transcript stops at the ClientFinished
1693 * message.
1694 */
1695#define SERVER_HELLO_RANDOM_OFFSET (SSL3_HM_HEADER_LENGTH + 2)
1696 /* KeyUpdate and NewSessionTicket do not need to be added */
1697 if (!SSL_CONNECTION_IS_TLS13(s)
1698 || (s->s3.tmp.message_type != SSL3_MT_NEWSESSION_TICKET
1699 && s->s3.tmp.message_type != SSL3_MT_KEY_UPDATE)) {
1700 if (s->s3.tmp.message_type != SSL3_MT_SERVER_HELLO
1701 || s->init_num < SERVER_HELLO_RANDOM_OFFSET + SSL3_RANDOM_SIZE
1702 || memcmp(hrrrandom,
1703 s->init_buf->data + SERVER_HELLO_RANDOM_OFFSET,
1704 SSL3_RANDOM_SIZE) != 0) {
1705 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1706 s->init_num + SSL3_HM_HEADER_LENGTH)) {
1707 /* SSLfatal() already called */
1708 *len = 0;
1709 return 0;
1710 }
1711 }
1712 }
1713 if (s->msg_callback)
1714 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
1715 (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, ssl,
1716 s->msg_callback_arg);
1717 }
1718
1719 *len = s->init_num;
1720 return 1;
1721}
1722
1723static const X509ERR2ALERT x509table[] = {
1724 {X509_V_ERR_APPLICATION_VERIFICATION, SSL_AD_HANDSHAKE_FAILURE},
1725 {X509_V_ERR_CA_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE},
1726 {X509_V_ERR_EC_KEY_EXPLICIT_PARAMS, SSL_AD_BAD_CERTIFICATE},
1727 {X509_V_ERR_CA_MD_TOO_WEAK, SSL_AD_BAD_CERTIFICATE},
1728 {X509_V_ERR_CERT_CHAIN_TOO_LONG, SSL_AD_UNKNOWN_CA},
1729 {X509_V_ERR_CERT_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED},
1730 {X509_V_ERR_CERT_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE},
1731 {X509_V_ERR_CERT_REJECTED, SSL_AD_BAD_CERTIFICATE},
1732 {X509_V_ERR_CERT_REVOKED, SSL_AD_CERTIFICATE_REVOKED},
1733 {X509_V_ERR_CERT_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR},
1734 {X509_V_ERR_CERT_UNTRUSTED, SSL_AD_BAD_CERTIFICATE},
1735 {X509_V_ERR_CRL_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED},
1736 {X509_V_ERR_CRL_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE},
1737 {X509_V_ERR_CRL_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR},
1738 {X509_V_ERR_DANE_NO_MATCH, SSL_AD_BAD_CERTIFICATE},
1739 {X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT, SSL_AD_UNKNOWN_CA},
1740 {X509_V_ERR_EE_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE},
1741 {X509_V_ERR_EMAIL_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1742 {X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD, SSL_AD_BAD_CERTIFICATE},
1743 {X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD, SSL_AD_BAD_CERTIFICATE},
1744 {X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE},
1745 {X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE},
1746 {X509_V_ERR_HOSTNAME_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1747 {X509_V_ERR_INVALID_CA, SSL_AD_UNKNOWN_CA},
1748 {X509_V_ERR_INVALID_CALL, SSL_AD_INTERNAL_ERROR},
1749 {X509_V_ERR_INVALID_PURPOSE, SSL_AD_UNSUPPORTED_CERTIFICATE},
1750 {X509_V_ERR_IP_ADDRESS_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1751 {X509_V_ERR_OUT_OF_MEM, SSL_AD_INTERNAL_ERROR},
1752 {X509_V_ERR_PATH_LENGTH_EXCEEDED, SSL_AD_UNKNOWN_CA},
1753 {X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN, SSL_AD_UNKNOWN_CA},
1754 {X509_V_ERR_STORE_LOOKUP, SSL_AD_INTERNAL_ERROR},
1755 {X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY, SSL_AD_BAD_CERTIFICATE},
1756 {X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE, SSL_AD_BAD_CERTIFICATE},
1757 {X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE, SSL_AD_BAD_CERTIFICATE},
1758 {X509_V_ERR_UNABLE_TO_GET_CRL, SSL_AD_UNKNOWN_CA},
1759 {X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER, SSL_AD_UNKNOWN_CA},
1760 {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT, SSL_AD_UNKNOWN_CA},
1761 {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, SSL_AD_UNKNOWN_CA},
1762 {X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE, SSL_AD_UNKNOWN_CA},
1763 {X509_V_ERR_UNSPECIFIED, SSL_AD_INTERNAL_ERROR},
1764
1765 /* Last entry; return this if we don't find the value above. */
1766 {X509_V_OK, SSL_AD_CERTIFICATE_UNKNOWN}
1767};
1768
1769int ssl_x509err2alert(int x509err)
1770{
1771 const X509ERR2ALERT *tp;
1772
1773 for (tp = x509table; tp->x509err != X509_V_OK; ++tp)
1774 if (tp->x509err == x509err)
1775 break;
1776 return tp->alert;
1777}
1778
1779int ssl_allow_compression(SSL_CONNECTION *s)
1780{
1781 if (s->options & SSL_OP_NO_COMPRESSION)
1782 return 0;
1783 return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL);
1784}
1785
1786/*
1787 * SSL/TLS/DTLS version comparison
1788 *
1789 * Returns
1790 * 0 if versiona is equal to versionb
1791 * 1 if versiona is greater than versionb
1792 * -1 if versiona is less than versionb
1793 */
1794int ssl_version_cmp(const SSL_CONNECTION *s, int versiona, int versionb)
1795{
1796 int dtls = SSL_CONNECTION_IS_DTLS(s);
1797
1798 if (versiona == versionb)
1799 return 0;
1800 if (!dtls)
1801 return versiona < versionb ? -1 : 1;
1802 return DTLS_VERSION_LT(versiona, versionb) ? -1 : 1;
1803}
1804
1805typedef struct {
1806 int version;
1807 const SSL_METHOD *(*cmeth) (void);
1808 const SSL_METHOD *(*smeth) (void);
1809} version_info;
1810
1811#if TLS_MAX_VERSION_INTERNAL != TLS1_3_VERSION
1812# error Code needs update for TLS_method() support beyond TLS1_3_VERSION.
1813#endif
1814
1815/* Must be in order high to low */
1816static const version_info tls_version_table[] = {
1817#ifndef OPENSSL_NO_TLS1_3
1818 {TLS1_3_VERSION, tlsv1_3_client_method, tlsv1_3_server_method},
1819#else
1820 {TLS1_3_VERSION, NULL, NULL},
1821#endif
1822#ifndef OPENSSL_NO_TLS1_2
1823 {TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method},
1824#else
1825 {TLS1_2_VERSION, NULL, NULL},
1826#endif
1827#ifndef OPENSSL_NO_TLS1_1
1828 {TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method},
1829#else
1830 {TLS1_1_VERSION, NULL, NULL},
1831#endif
1832#ifndef OPENSSL_NO_TLS1
1833 {TLS1_VERSION, tlsv1_client_method, tlsv1_server_method},
1834#else
1835 {TLS1_VERSION, NULL, NULL},
1836#endif
1837#ifndef OPENSSL_NO_SSL3
1838 {SSL3_VERSION, sslv3_client_method, sslv3_server_method},
1839#else
1840 {SSL3_VERSION, NULL, NULL},
1841#endif
1842 {0, NULL, NULL},
1843};
1844
1845#if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION
1846# error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
1847#endif
1848
1849/* Must be in order high to low */
1850static const version_info dtls_version_table[] = {
1851#ifndef OPENSSL_NO_DTLS1_2
1852 {DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method},
1853#else
1854 {DTLS1_2_VERSION, NULL, NULL},
1855#endif
1856#ifndef OPENSSL_NO_DTLS1
1857 {DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method},
1858 {DTLS1_BAD_VER, dtls_bad_ver_client_method, NULL},
1859#else
1860 {DTLS1_VERSION, NULL, NULL},
1861 {DTLS1_BAD_VER, NULL, NULL},
1862#endif
1863 {0, NULL, NULL},
1864};
1865
1866/*
1867 * ssl_method_error - Check whether an SSL_METHOD is enabled.
1868 *
1869 * @s: The SSL handle for the candidate method
1870 * @method: the intended method.
1871 *
1872 * Returns 0 on success, or an SSL error reason on failure.
1873 */
1874static int ssl_method_error(const SSL_CONNECTION *s, const SSL_METHOD *method)
1875{
1876 int version = method->version;
1877
1878 if ((s->min_proto_version != 0 &&
1879 ssl_version_cmp(s, version, s->min_proto_version) < 0) ||
1880 ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0)
1881 return SSL_R_VERSION_TOO_LOW;
1882
1883 if (s->max_proto_version != 0 &&
1884 ssl_version_cmp(s, version, s->max_proto_version) > 0)
1885 return SSL_R_VERSION_TOO_HIGH;
1886
1887 if ((s->options & method->mask) != 0)
1888 return SSL_R_UNSUPPORTED_PROTOCOL;
1889 if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s))
1890 return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE;
1891
1892 return 0;
1893}
1894
1895/*
1896 * Only called by servers. Returns 1 if the server has a TLSv1.3 capable
1897 * certificate type, or has PSK or a certificate callback configured, or has
1898 * a servername callback configure. Otherwise returns 0.
1899 */
1900static int is_tls13_capable(const SSL_CONNECTION *s)
1901{
1902 size_t i;
1903 int curve;
1904 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1905
1906 if (!ossl_assert(sctx != NULL) || !ossl_assert(s->session_ctx != NULL))
1907 return 0;
1908
1909 /*
1910 * A servername callback can change the available certs, so if a servername
1911 * cb is set then we just assume TLSv1.3 will be ok
1912 */
1913 if (sctx->ext.servername_cb != NULL
1914 || s->session_ctx->ext.servername_cb != NULL)
1915 return 1;
1916
1917#ifndef OPENSSL_NO_PSK
1918 if (s->psk_server_callback != NULL)
1919 return 1;
1920#endif
1921
1922 if (s->psk_find_session_cb != NULL || s->cert->cert_cb != NULL)
1923 return 1;
1924
1925 /* All provider-based sig algs are required to support at least TLS1.3 */
1926 for (i = 0; i < s->ssl_pkey_num; i++) {
1927 /* Skip over certs disallowed for TLSv1.3 */
1928 switch (i) {
1929 case SSL_PKEY_DSA_SIGN:
1930 case SSL_PKEY_GOST01:
1931 case SSL_PKEY_GOST12_256:
1932 case SSL_PKEY_GOST12_512:
1933 continue;
1934 default:
1935 break;
1936 }
1937 if (!ssl_has_cert(s, i))
1938 continue;
1939 if (i != SSL_PKEY_ECC)
1940 return 1;
1941 /*
1942 * Prior to TLSv1.3 sig algs allowed any curve to be used. TLSv1.3 is
1943 * more restrictive so check that our sig algs are consistent with this
1944 * EC cert. See section 4.2.3 of RFC8446.
1945 */
1946 curve = ssl_get_EC_curve_nid(s->cert->pkeys[SSL_PKEY_ECC].privatekey);
1947 if (tls_check_sigalg_curve(s, curve))
1948 return 1;
1949 }
1950
1951 return 0;
1952}
1953
1954/*
1955 * ssl_version_supported - Check that the specified `version` is supported by
1956 * `SSL *` instance
1957 *
1958 * @s: The SSL handle for the candidate method
1959 * @version: Protocol version to test against
1960 *
1961 * Returns 1 when supported, otherwise 0
1962 */
1963int ssl_version_supported(const SSL_CONNECTION *s, int version,
1964 const SSL_METHOD **meth)
1965{
1966 const version_info *vent;
1967 const version_info *table;
1968
1969 switch (SSL_CONNECTION_GET_SSL(s)->method->version) {
1970 default:
1971 /* Version should match method version for non-ANY method */
1972 return ssl_version_cmp(s, version, s->version) == 0;
1973 case TLS_ANY_VERSION:
1974 table = tls_version_table;
1975 break;
1976 case DTLS_ANY_VERSION:
1977 table = dtls_version_table;
1978 break;
1979 }
1980
1981 for (vent = table;
1982 vent->version != 0 && ssl_version_cmp(s, version, vent->version) <= 0;
1983 ++vent) {
1984 const SSL_METHOD *(*thismeth)(void) = s->server ? vent->smeth
1985 : vent->cmeth;
1986
1987 if (thismeth != NULL
1988 && ssl_version_cmp(s, version, vent->version) == 0
1989 && ssl_method_error(s, thismeth()) == 0
1990 && (!s->server
1991 || version != TLS1_3_VERSION
1992 || is_tls13_capable(s))) {
1993 if (meth != NULL)
1994 *meth = thismeth();
1995 return 1;
1996 }
1997 }
1998 return 0;
1999}
2000
2001/*
2002 * ssl_check_version_downgrade - In response to RFC7507 SCSV version
2003 * fallback indication from a client check whether we're using the highest
2004 * supported protocol version.
2005 *
2006 * @s server SSL handle.
2007 *
2008 * Returns 1 when using the highest enabled version, 0 otherwise.
2009 */
2010int ssl_check_version_downgrade(SSL_CONNECTION *s)
2011{
2012 const version_info *vent;
2013 const version_info *table;
2014 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
2015
2016 /*
2017 * Check that the current protocol is the highest enabled version
2018 * (according to ssl->defltmethod, as version negotiation may have changed
2019 * s->method).
2020 */
2021 if (s->version == ssl->defltmeth->version)
2022 return 1;
2023
2024 /*
2025 * Apparently we're using a version-flexible SSL_METHOD (not at its
2026 * highest protocol version).
2027 */
2028 if (ssl->defltmeth->version == TLS_method()->version)
2029 table = tls_version_table;
2030 else if (ssl->defltmeth->version == DTLS_method()->version)
2031 table = dtls_version_table;
2032 else {
2033 /* Unexpected state; fail closed. */
2034 return 0;
2035 }
2036
2037 for (vent = table; vent->version != 0; ++vent) {
2038 if (vent->smeth != NULL && ssl_method_error(s, vent->smeth()) == 0)
2039 return s->version == vent->version;
2040 }
2041 return 0;
2042}
2043
2044/*
2045 * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
2046 * protocols, provided the initial (D)TLS method is version-flexible. This
2047 * function sanity-checks the proposed value and makes sure the method is
2048 * version-flexible, then sets the limit if all is well.
2049 *
2050 * @method_version: The version of the current SSL_METHOD.
2051 * @version: the intended limit.
2052 * @bound: pointer to limit to be updated.
2053 *
2054 * Returns 1 on success, 0 on failure.
2055 */
2056int ssl_set_version_bound(int method_version, int version, int *bound)
2057{
2058 int valid_tls;
2059 int valid_dtls;
2060
2061 if (version == 0) {
2062 *bound = version;
2063 return 1;
2064 }
2065
2066 valid_tls = version >= SSL3_VERSION && version <= TLS_MAX_VERSION_INTERNAL;
2067 valid_dtls =
2068 /* We support client side pre-standardisation version of DTLS */
2069 (version == DTLS1_BAD_VER)
2070 || (DTLS_VERSION_LE(version, DTLS_MAX_VERSION_INTERNAL)
2071 && DTLS_VERSION_GE(version, DTLS1_VERSION));
2072
2073 if (!valid_tls && !valid_dtls)
2074 return 0;
2075
2076 /*-
2077 * Restrict TLS methods to TLS protocol versions.
2078 * Restrict DTLS methods to DTLS protocol versions.
2079 * Note, DTLS version numbers are decreasing, use comparison macros.
2080 *
2081 * Note that for both lower-bounds we use explicit versions, not
2082 * (D)TLS_MIN_VERSION. This is because we don't want to break user
2083 * configurations. If the MIN (supported) version ever rises, the user's
2084 * "floor" remains valid even if no longer available. We don't expect the
2085 * MAX ceiling to ever get lower, so making that variable makes sense.
2086 *
2087 * We ignore attempts to set bounds on version-inflexible methods,
2088 * returning success.
2089 */
2090 switch (method_version) {
2091 default:
2092 break;
2093
2094 case TLS_ANY_VERSION:
2095 if (valid_tls)
2096 *bound = version;
2097 break;
2098
2099 case DTLS_ANY_VERSION:
2100 if (valid_dtls)
2101 *bound = version;
2102 break;
2103 }
2104 return 1;
2105}
2106
2107static void check_for_downgrade(SSL_CONNECTION *s, int vers, DOWNGRADE *dgrd)
2108{
2109 if (vers == TLS1_2_VERSION
2110 && ssl_version_supported(s, TLS1_3_VERSION, NULL)) {
2111 *dgrd = DOWNGRADE_TO_1_2;
2112 } else if (!SSL_CONNECTION_IS_DTLS(s)
2113 && vers < TLS1_2_VERSION
2114 /*
2115 * We need to ensure that a server that disables TLSv1.2
2116 * (creating a hole between TLSv1.3 and TLSv1.1) can still
2117 * complete handshakes with clients that support TLSv1.2 and
2118 * below. Therefore we do not enable the sentinel if TLSv1.3 is
2119 * enabled and TLSv1.2 is not.
2120 */
2121 && ssl_version_supported(s, TLS1_2_VERSION, NULL)) {
2122 *dgrd = DOWNGRADE_TO_1_1;
2123 } else {
2124 *dgrd = DOWNGRADE_NONE;
2125 }
2126}
2127
2128/*
2129 * ssl_choose_server_version - Choose server (D)TLS version. Called when the
2130 * client HELLO is received to select the final server protocol version and
2131 * the version specific method.
2132 *
2133 * @s: server SSL handle.
2134 *
2135 * Returns 0 on success or an SSL error reason number on failure.
2136 */
2137int ssl_choose_server_version(SSL_CONNECTION *s, CLIENTHELLO_MSG *hello,
2138 DOWNGRADE *dgrd)
2139{
2140 /*-
2141 * With version-flexible methods we have an initial state with:
2142 *
2143 * s->method->version == (D)TLS_ANY_VERSION,
2144 * s->version == (D)TLS_MAX_VERSION_INTERNAL.
2145 *
2146 * So we detect version-flexible methods via the method version, not the
2147 * handle version.
2148 */
2149 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
2150 int server_version = ssl->method->version;
2151 int client_version = hello->legacy_version;
2152 const version_info *vent;
2153 const version_info *table;
2154 int disabled = 0;
2155 RAW_EXTENSION *suppversions;
2156
2157 s->client_version = client_version;
2158
2159 switch (server_version) {
2160 default:
2161 if (!SSL_CONNECTION_IS_TLS13(s)) {
2162 if (ssl_version_cmp(s, client_version, s->version) < 0)
2163 return SSL_R_WRONG_SSL_VERSION;
2164 *dgrd = DOWNGRADE_NONE;
2165 /*
2166 * If this SSL handle is not from a version flexible method we don't
2167 * (and never did) check min/max FIPS or Suite B constraints. Hope
2168 * that's OK. It is up to the caller to not choose fixed protocol
2169 * versions they don't want. If not, then easy to fix, just return
2170 * ssl_method_error(s, s->method)
2171 */
2172 return 0;
2173 }
2174 /*
2175 * Fall through if we are TLSv1.3 already (this means we must be after
2176 * a HelloRetryRequest
2177 */
2178 /* fall thru */
2179 case TLS_ANY_VERSION:
2180 table = tls_version_table;
2181 break;
2182 case DTLS_ANY_VERSION:
2183 table = dtls_version_table;
2184 break;
2185 }
2186
2187 suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions];
2188
2189 /* If we did an HRR then supported versions is mandatory */
2190 if (!suppversions->present && s->hello_retry_request != SSL_HRR_NONE)
2191 return SSL_R_UNSUPPORTED_PROTOCOL;
2192
2193 if (suppversions->present && !SSL_CONNECTION_IS_DTLS(s)) {
2194 unsigned int candidate_vers = 0;
2195 unsigned int best_vers = 0;
2196 const SSL_METHOD *best_method = NULL;
2197 PACKET versionslist;
2198
2199 suppversions->parsed = 1;
2200
2201 if (!PACKET_as_length_prefixed_1(&suppversions->data, &versionslist)) {
2202 /* Trailing or invalid data? */
2203 return SSL_R_LENGTH_MISMATCH;
2204 }
2205
2206 /*
2207 * The TLSv1.3 spec says the client MUST set this to TLS1_2_VERSION.
2208 * The spec only requires servers to check that it isn't SSLv3:
2209 * "Any endpoint receiving a Hello message with
2210 * ClientHello.legacy_version or ServerHello.legacy_version set to
2211 * 0x0300 MUST abort the handshake with a "protocol_version" alert."
2212 * We are slightly stricter and require that it isn't SSLv3 or lower.
2213 * We tolerate TLSv1 and TLSv1.1.
2214 */
2215 if (client_version <= SSL3_VERSION)
2216 return SSL_R_BAD_LEGACY_VERSION;
2217
2218 while (PACKET_get_net_2(&versionslist, &candidate_vers)) {
2219 if (ssl_version_cmp(s, candidate_vers, best_vers) <= 0)
2220 continue;
2221 if (ssl_version_supported(s, candidate_vers, &best_method))
2222 best_vers = candidate_vers;
2223 }
2224 if (PACKET_remaining(&versionslist) != 0) {
2225 /* Trailing data? */
2226 return SSL_R_LENGTH_MISMATCH;
2227 }
2228
2229 if (best_vers > 0) {
2230 if (s->hello_retry_request != SSL_HRR_NONE) {
2231 /*
2232 * This is after a HelloRetryRequest so we better check that we
2233 * negotiated TLSv1.3
2234 */
2235 if (best_vers != TLS1_3_VERSION)
2236 return SSL_R_UNSUPPORTED_PROTOCOL;
2237 return 0;
2238 }
2239 check_for_downgrade(s, best_vers, dgrd);
2240 s->version = best_vers;
2241 ssl->method = best_method;
2242 if (!ssl_set_record_protocol_version(s, best_vers))
2243 return ERR_R_INTERNAL_ERROR;
2244
2245 return 0;
2246 }
2247 return SSL_R_UNSUPPORTED_PROTOCOL;
2248 }
2249
2250 /*
2251 * If the supported versions extension isn't present, then the highest
2252 * version we can negotiate is TLSv1.2
2253 */
2254 if (ssl_version_cmp(s, client_version, TLS1_3_VERSION) >= 0)
2255 client_version = TLS1_2_VERSION;
2256
2257 /*
2258 * No supported versions extension, so we just use the version supplied in
2259 * the ClientHello.
2260 */
2261 for (vent = table; vent->version != 0; ++vent) {
2262 const SSL_METHOD *method;
2263
2264 if (vent->smeth == NULL ||
2265 ssl_version_cmp(s, client_version, vent->version) < 0)
2266 continue;
2267 method = vent->smeth();
2268 if (ssl_method_error(s, method) == 0) {
2269 check_for_downgrade(s, vent->version, dgrd);
2270 s->version = vent->version;
2271 ssl->method = method;
2272 if (!ssl_set_record_protocol_version(s, s->version))
2273 return ERR_R_INTERNAL_ERROR;
2274
2275 return 0;
2276 }
2277 disabled = 1;
2278 }
2279 return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW;
2280}
2281
2282/*
2283 * ssl_choose_client_version - Choose client (D)TLS version. Called when the
2284 * server HELLO is received to select the final client protocol version and
2285 * the version specific method.
2286 *
2287 * @s: client SSL handle.
2288 * @version: The proposed version from the server's HELLO.
2289 * @extensions: The extensions received
2290 *
2291 * Returns 1 on success or 0 on error.
2292 */
2293int ssl_choose_client_version(SSL_CONNECTION *s, int version,
2294 RAW_EXTENSION *extensions)
2295{
2296 const version_info *vent;
2297 const version_info *table;
2298 int ret, ver_min, ver_max, real_max, origv;
2299 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
2300
2301 origv = s->version;
2302 s->version = version;
2303
2304 /* This will overwrite s->version if the extension is present */
2305 if (!tls_parse_extension(s, TLSEXT_IDX_supported_versions,
2306 SSL_EXT_TLS1_2_SERVER_HELLO
2307 | SSL_EXT_TLS1_3_SERVER_HELLO, extensions,
2308 NULL, 0)) {
2309 s->version = origv;
2310 return 0;
2311 }
2312
2313 if (s->hello_retry_request != SSL_HRR_NONE
2314 && s->version != TLS1_3_VERSION) {
2315 s->version = origv;
2316 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_WRONG_SSL_VERSION);
2317 return 0;
2318 }
2319
2320 switch (ssl->method->version) {
2321 default:
2322 if (s->version != ssl->method->version) {
2323 s->version = origv;
2324 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_WRONG_SSL_VERSION);
2325 return 0;
2326 }
2327 /*
2328 * If this SSL handle is not from a version flexible method we don't
2329 * (and never did) check min/max, FIPS or Suite B constraints. Hope
2330 * that's OK. It is up to the caller to not choose fixed protocol
2331 * versions they don't want. If not, then easy to fix, just return
2332 * ssl_method_error(s, s->method)
2333 */
2334 if (!ssl_set_record_protocol_version(s, s->version)) {
2335 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2336 return 0;
2337 }
2338 return 1;
2339 case TLS_ANY_VERSION:
2340 table = tls_version_table;
2341 break;
2342 case DTLS_ANY_VERSION:
2343 table = dtls_version_table;
2344 break;
2345 }
2346
2347 ret = ssl_get_min_max_version(s, &ver_min, &ver_max, &real_max);
2348 if (ret != 0) {
2349 s->version = origv;
2350 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, ret);
2351 return 0;
2352 }
2353 if (ssl_version_cmp(s, s->version, ver_min) < 0
2354 || ssl_version_cmp(s, s->version, ver_max) > 0) {
2355 s->version = origv;
2356 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNSUPPORTED_PROTOCOL);
2357 return 0;
2358 }
2359
2360 if ((s->mode & SSL_MODE_SEND_FALLBACK_SCSV) == 0)
2361 real_max = ver_max;
2362
2363 /* Check for downgrades */
2364 if (s->version == TLS1_2_VERSION && real_max > s->version) {
2365 if (memcmp(tls12downgrade,
2366 s->s3.server_random + SSL3_RANDOM_SIZE
2367 - sizeof(tls12downgrade),
2368 sizeof(tls12downgrade)) == 0) {
2369 s->version = origv;
2370 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
2371 SSL_R_INAPPROPRIATE_FALLBACK);
2372 return 0;
2373 }
2374 } else if (!SSL_CONNECTION_IS_DTLS(s)
2375 && s->version < TLS1_2_VERSION
2376 && real_max > s->version) {
2377 if (memcmp(tls11downgrade,
2378 s->s3.server_random + SSL3_RANDOM_SIZE
2379 - sizeof(tls11downgrade),
2380 sizeof(tls11downgrade)) == 0) {
2381 s->version = origv;
2382 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
2383 SSL_R_INAPPROPRIATE_FALLBACK);
2384 return 0;
2385 }
2386 }
2387
2388 for (vent = table; vent->version != 0; ++vent) {
2389 if (vent->cmeth == NULL || s->version != vent->version)
2390 continue;
2391
2392 ssl->method = vent->cmeth();
2393 if (!ssl_set_record_protocol_version(s, s->version)) {
2394 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2395 return 0;
2396 }
2397 return 1;
2398 }
2399
2400 s->version = origv;
2401 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNSUPPORTED_PROTOCOL);
2402 return 0;
2403}
2404
2405/*
2406 * ssl_get_min_max_version - get minimum and maximum protocol version
2407 * @s: The SSL connection
2408 * @min_version: The minimum supported version
2409 * @max_version: The maximum supported version
2410 * @real_max: The highest version below the lowest compile time version hole
2411 * where that hole lies above at least one run-time enabled
2412 * protocol.
2413 *
2414 * Work out what version we should be using for the initial ClientHello if the
2415 * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx
2416 * options, the MinProtocol and MaxProtocol configuration commands, any Suite B
2417 * constraints and any floor imposed by the security level here,
2418 * so we don't advertise the wrong protocol version to only reject the outcome later.
2419 *
2420 * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
2421 * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
2422 * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
2423 *
2424 * Returns 0 on success or an SSL error reason number on failure. On failure
2425 * min_version and max_version will also be set to 0.
2426 */
2427int ssl_get_min_max_version(const SSL_CONNECTION *s, int *min_version,
2428 int *max_version, int *real_max)
2429{
2430 int version, tmp_real_max;
2431 int hole;
2432 const SSL_METHOD *method;
2433 const version_info *table;
2434 const version_info *vent;
2435 const SSL *ssl = SSL_CONNECTION_GET_SSL(s);
2436
2437 switch (ssl->method->version) {
2438 default:
2439 /*
2440 * If this SSL handle is not from a version flexible method we don't
2441 * (and never did) check min/max FIPS or Suite B constraints. Hope
2442 * that's OK. It is up to the caller to not choose fixed protocol
2443 * versions they don't want. If not, then easy to fix, just return
2444 * ssl_method_error(s, s->method)
2445 */
2446 *min_version = *max_version = s->version;
2447 /*
2448 * Providing a real_max only makes sense where we're using a version
2449 * flexible method.
2450 */
2451 if (!ossl_assert(real_max == NULL))
2452 return ERR_R_INTERNAL_ERROR;
2453 return 0;
2454 case TLS_ANY_VERSION:
2455 table = tls_version_table;
2456 break;
2457 case DTLS_ANY_VERSION:
2458 table = dtls_version_table;
2459 break;
2460 }
2461
2462 /*
2463 * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
2464 * below X enabled. This is required in order to maintain the "version
2465 * capability" vector contiguous. Any versions with a NULL client method
2466 * (protocol version client is disabled at compile-time) is also a "hole".
2467 *
2468 * Our initial state is hole == 1, version == 0. That is, versions above
2469 * the first version in the method table are disabled (a "hole" above
2470 * the valid protocol entries) and we don't have a selected version yet.
2471 *
2472 * Whenever "hole == 1", and we hit an enabled method, its version becomes
2473 * the selected version. We're no longer in a hole, so "hole" becomes 0.
2474 *
2475 * If "hole == 0" and we hit an enabled method, we support a contiguous
2476 * range of at least two methods. If we hit a disabled method,
2477 * then hole becomes true again, but nothing else changes yet,
2478 * because all the remaining methods may be disabled too.
2479 * If we again hit an enabled method after the new hole, it becomes
2480 * selected, as we start from scratch.
2481 */
2482 *min_version = version = 0;
2483 hole = 1;
2484 if (real_max != NULL)
2485 *real_max = 0;
2486 tmp_real_max = 0;
2487 for (vent = table; vent->version != 0; ++vent) {
2488 /*
2489 * A table entry with a NULL client method is still a hole in the
2490 * "version capability" vector.
2491 */
2492 if (vent->cmeth == NULL) {
2493 hole = 1;
2494 tmp_real_max = 0;
2495 continue;
2496 }
2497 method = vent->cmeth();
2498
2499 if (hole == 1 && tmp_real_max == 0)
2500 tmp_real_max = vent->version;
2501
2502 if (ssl_method_error(s, method) != 0) {
2503 hole = 1;
2504 } else if (!hole) {
2505 *min_version = method->version;
2506 } else {
2507 if (real_max != NULL && tmp_real_max != 0)
2508 *real_max = tmp_real_max;
2509 version = method->version;
2510 *min_version = version;
2511 hole = 0;
2512 }
2513 }
2514
2515 *max_version = version;
2516
2517 /* Fail if everything is disabled */
2518 if (version == 0)
2519 return SSL_R_NO_PROTOCOLS_AVAILABLE;
2520
2521 return 0;
2522}
2523
2524/*
2525 * ssl_set_client_hello_version - Work out what version we should be using for
2526 * the initial ClientHello.legacy_version field.
2527 *
2528 * @s: client SSL handle.
2529 *
2530 * Returns 0 on success or an SSL error reason number on failure.
2531 */
2532int ssl_set_client_hello_version(SSL_CONNECTION *s)
2533{
2534 int ver_min, ver_max, ret;
2535
2536 /*
2537 * In a renegotiation we always send the same client_version that we sent
2538 * last time, regardless of which version we eventually negotiated.
2539 */
2540 if (!SSL_IS_FIRST_HANDSHAKE(s))
2541 return 0;
2542
2543 ret = ssl_get_min_max_version(s, &ver_min, &ver_max, NULL);
2544
2545 if (ret != 0)
2546 return ret;
2547
2548 s->version = ver_max;
2549
2550 if (SSL_CONNECTION_IS_DTLS(s)) {
2551 if (ver_max == DTLS1_BAD_VER) {
2552 /*
2553 * Even though this is technically before version negotiation,
2554 * because we have asked for DTLS1_BAD_VER we will never negotiate
2555 * anything else, and this has impacts on the record layer for when
2556 * we read the ServerHello. So we need to tell the record layer
2557 * about this immediately.
2558 */
2559 if (!ssl_set_record_protocol_version(s, ver_max))
2560 return 0;
2561 }
2562 } else if (ver_max > TLS1_2_VERSION) {
2563 /* TLS1.3 always uses TLS1.2 in the legacy_version field */
2564 ver_max = TLS1_2_VERSION;
2565 }
2566
2567 s->client_version = ver_max;
2568 return 0;
2569}
2570
2571/*
2572 * Checks a list of |groups| to determine if the |group_id| is in it. If it is
2573 * and |checkallow| is 1 then additionally check if the group is allowed to be
2574 * used. Returns 1 if the group is in the list (and allowed if |checkallow| is
2575 * 1) or 0 otherwise.
2576 */
2577int check_in_list(SSL_CONNECTION *s, uint16_t group_id, const uint16_t *groups,
2578 size_t num_groups, int checkallow)
2579{
2580 size_t i;
2581
2582 if (groups == NULL || num_groups == 0)
2583 return 0;
2584
2585 for (i = 0; i < num_groups; i++) {
2586 uint16_t group = groups[i];
2587
2588 if (group_id == group
2589 && (!checkallow
2590 || tls_group_allowed(s, group, SSL_SECOP_CURVE_CHECK))) {
2591 return 1;
2592 }
2593 }
2594
2595 return 0;
2596}
2597
2598/* Replace ClientHello1 in the transcript hash with a synthetic message */
2599int create_synthetic_message_hash(SSL_CONNECTION *s,
2600 const unsigned char *hashval,
2601 size_t hashlen, const unsigned char *hrr,
2602 size_t hrrlen)
2603{
2604 unsigned char hashvaltmp[EVP_MAX_MD_SIZE];
2605 unsigned char msghdr[SSL3_HM_HEADER_LENGTH];
2606
2607 memset(msghdr, 0, sizeof(msghdr));
2608
2609 if (hashval == NULL) {
2610 hashval = hashvaltmp;
2611 hashlen = 0;
2612 /* Get the hash of the initial ClientHello */
2613 if (!ssl3_digest_cached_records(s, 0)
2614 || !ssl_handshake_hash(s, hashvaltmp, sizeof(hashvaltmp),
2615 &hashlen)) {
2616 /* SSLfatal() already called */
2617 return 0;
2618 }
2619 }
2620
2621 /* Reinitialise the transcript hash */
2622 if (!ssl3_init_finished_mac(s)) {
2623 /* SSLfatal() already called */
2624 return 0;
2625 }
2626
2627 /* Inject the synthetic message_hash message */
2628 msghdr[0] = SSL3_MT_MESSAGE_HASH;
2629 msghdr[SSL3_HM_HEADER_LENGTH - 1] = (unsigned char)hashlen;
2630 if (!ssl3_finish_mac(s, msghdr, SSL3_HM_HEADER_LENGTH)
2631 || !ssl3_finish_mac(s, hashval, hashlen)) {
2632 /* SSLfatal() already called */
2633 return 0;
2634 }
2635
2636 /*
2637 * Now re-inject the HRR and current message if appropriate (we just deleted
2638 * it when we reinitialised the transcript hash above). Only necessary after
2639 * receiving a ClientHello2 with a cookie.
2640 */
2641 if (hrr != NULL
2642 && (!ssl3_finish_mac(s, hrr, hrrlen)
2643 || !ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
2644 s->s3.tmp.message_size
2645 + SSL3_HM_HEADER_LENGTH))) {
2646 /* SSLfatal() already called */
2647 return 0;
2648 }
2649
2650 return 1;
2651}
2652
2653static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2654{
2655 return X509_NAME_cmp(*a, *b);
2656}
2657
2658int parse_ca_names(SSL_CONNECTION *s, PACKET *pkt)
2659{
2660 STACK_OF(X509_NAME) *ca_sk = sk_X509_NAME_new(ca_dn_cmp);
2661 X509_NAME *xn = NULL;
2662 PACKET cadns;
2663
2664 if (ca_sk == NULL) {
2665 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
2666 goto err;
2667 }
2668 /* get the CA RDNs */
2669 if (!PACKET_get_length_prefixed_2(pkt, &cadns)) {
2670 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2671 goto err;
2672 }
2673
2674 while (PACKET_remaining(&cadns)) {
2675 const unsigned char *namestart, *namebytes;
2676 unsigned int name_len;
2677
2678 if (!PACKET_get_net_2(&cadns, &name_len)
2679 || !PACKET_get_bytes(&cadns, &namebytes, name_len)) {
2680 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2681 goto err;
2682 }
2683
2684 namestart = namebytes;
2685 if ((xn = d2i_X509_NAME(NULL, &namebytes, name_len)) == NULL) {
2686 SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB);
2687 goto err;
2688 }
2689 if (namebytes != (namestart + name_len)) {
2690 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CA_DN_LENGTH_MISMATCH);
2691 goto err;
2692 }
2693
2694 if (!sk_X509_NAME_push(ca_sk, xn)) {
2695 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
2696 goto err;
2697 }
2698 xn = NULL;
2699 }
2700
2701 sk_X509_NAME_pop_free(s->s3.tmp.peer_ca_names, X509_NAME_free);
2702 s->s3.tmp.peer_ca_names = ca_sk;
2703
2704 return 1;
2705
2706 err:
2707 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2708 X509_NAME_free(xn);
2709 return 0;
2710}
2711
2712const STACK_OF(X509_NAME) *get_ca_names(SSL_CONNECTION *s)
2713{
2714 const STACK_OF(X509_NAME) *ca_sk = NULL;
2715 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
2716
2717 if (s->server) {
2718 ca_sk = SSL_get_client_CA_list(ssl);
2719 if (ca_sk != NULL && sk_X509_NAME_num(ca_sk) == 0)
2720 ca_sk = NULL;
2721 }
2722
2723 if (ca_sk == NULL)
2724 ca_sk = SSL_get0_CA_list(ssl);
2725
2726 return ca_sk;
2727}
2728
2729int construct_ca_names(SSL_CONNECTION *s, const STACK_OF(X509_NAME) *ca_sk,
2730 WPACKET *pkt)
2731{
2732 /* Start sub-packet for client CA list */
2733 if (!WPACKET_start_sub_packet_u16(pkt)) {
2734 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2735 return 0;
2736 }
2737
2738 if ((ca_sk != NULL) && !(s->options & SSL_OP_DISABLE_TLSEXT_CA_NAMES)) {
2739 int i;
2740
2741 for (i = 0; i < sk_X509_NAME_num(ca_sk); i++) {
2742 unsigned char *namebytes;
2743 X509_NAME *name = sk_X509_NAME_value(ca_sk, i);
2744 int namelen;
2745
2746 if (name == NULL
2747 || (namelen = i2d_X509_NAME(name, NULL)) < 0
2748 || !WPACKET_sub_allocate_bytes_u16(pkt, namelen,
2749 &namebytes)
2750 || i2d_X509_NAME(name, &namebytes) != namelen) {
2751 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2752 return 0;
2753 }
2754 }
2755 }
2756
2757 if (!WPACKET_close(pkt)) {
2758 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2759 return 0;
2760 }
2761
2762 return 1;
2763}
2764
2765/* Create a buffer containing data to be signed for server key exchange */
2766size_t construct_key_exchange_tbs(SSL_CONNECTION *s, unsigned char **ptbs,
2767 const void *param, size_t paramlen)
2768{
2769 size_t tbslen = 2 * SSL3_RANDOM_SIZE + paramlen;
2770 unsigned char *tbs = OPENSSL_malloc(tbslen);
2771
2772 if (tbs == NULL) {
2773 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
2774 return 0;
2775 }
2776 memcpy(tbs, s->s3.client_random, SSL3_RANDOM_SIZE);
2777 memcpy(tbs + SSL3_RANDOM_SIZE, s->s3.server_random, SSL3_RANDOM_SIZE);
2778
2779 memcpy(tbs + SSL3_RANDOM_SIZE * 2, param, paramlen);
2780
2781 *ptbs = tbs;
2782 return tbslen;
2783}
2784
2785/*
2786 * Saves the current handshake digest for Post-Handshake Auth,
2787 * Done after ClientFinished is processed, done exactly once
2788 */
2789int tls13_save_handshake_digest_for_pha(SSL_CONNECTION *s)
2790{
2791 if (s->pha_dgst == NULL) {
2792 if (!ssl3_digest_cached_records(s, 1))
2793 /* SSLfatal() already called */
2794 return 0;
2795
2796 s->pha_dgst = EVP_MD_CTX_new();
2797 if (s->pha_dgst == NULL) {
2798 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2799 return 0;
2800 }
2801 if (!EVP_MD_CTX_copy_ex(s->pha_dgst,
2802 s->s3.handshake_dgst)) {
2803 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2804 EVP_MD_CTX_free(s->pha_dgst);
2805 s->pha_dgst = NULL;
2806 return 0;
2807 }
2808 }
2809 return 1;
2810}
2811
2812/*
2813 * Restores the Post-Handshake Auth handshake digest
2814 * Done just before sending/processing the Cert Request
2815 */
2816int tls13_restore_handshake_digest_for_pha(SSL_CONNECTION *s)
2817{
2818 if (s->pha_dgst == NULL) {
2819 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2820 return 0;
2821 }
2822 if (!EVP_MD_CTX_copy_ex(s->s3.handshake_dgst,
2823 s->pha_dgst)) {
2824 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2825 return 0;
2826 }
2827 return 1;
2828}
2829
2830#ifndef OPENSSL_NO_COMP_ALG
2831MSG_PROCESS_RETURN tls13_process_compressed_certificate(SSL_CONNECTION *sc,
2832 PACKET *pkt,
2833 PACKET *tmppkt,
2834 BUF_MEM *buf)
2835{
2836 MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
2837 int comp_alg;
2838 COMP_METHOD *method = NULL;
2839 COMP_CTX *comp = NULL;
2840 size_t expected_length;
2841 size_t comp_length;
2842 int i;
2843 int found = 0;
2844
2845 if (buf == NULL) {
2846 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2847 goto err;
2848 }
2849 if (!PACKET_get_net_2(pkt, (unsigned int*)&comp_alg)) {
2850 SSLfatal(sc, SSL_AD_BAD_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2851 goto err;
2852 }
2853 /* If we have a prefs list, make sure the algorithm is in it */
2854 if (sc->cert_comp_prefs[0] != TLSEXT_comp_cert_none) {
2855 for (i = 0; sc->cert_comp_prefs[i] != TLSEXT_comp_cert_none; i++) {
2856 if (sc->cert_comp_prefs[i] == comp_alg) {
2857 found = 1;
2858 break;
2859 }
2860 }
2861 if (!found) {
2862 SSLfatal(sc, SSL_AD_BAD_CERTIFICATE, SSL_R_BAD_COMPRESSION_ALGORITHM);
2863 goto err;
2864 }
2865 }
2866 if (!ossl_comp_has_alg(comp_alg)) {
2867 SSLfatal(sc, SSL_AD_BAD_CERTIFICATE, SSL_R_BAD_COMPRESSION_ALGORITHM);
2868 goto err;
2869 }
2870 switch (comp_alg) {
2871 case TLSEXT_comp_cert_zlib:
2872 method = COMP_zlib_oneshot();
2873 break;
2874 case TLSEXT_comp_cert_brotli:
2875 method = COMP_brotli_oneshot();
2876 break;
2877 case TLSEXT_comp_cert_zstd:
2878 method = COMP_zstd_oneshot();
2879 break;
2880 default:
2881 SSLfatal(sc, SSL_AD_BAD_CERTIFICATE, SSL_R_BAD_COMPRESSION_ALGORITHM);
2882 goto err;
2883 }
2884
2885 if ((comp = COMP_CTX_new(method)) == NULL
2886 || !PACKET_get_net_3_len(pkt, &expected_length)
2887 || !PACKET_get_net_3_len(pkt, &comp_length)
2888 || PACKET_remaining(pkt) != comp_length
2889 || !BUF_MEM_grow(buf, expected_length)
2890 || !PACKET_buf_init(tmppkt, (unsigned char *)buf->data, expected_length)
2891 || COMP_expand_block(comp, (unsigned char *)buf->data, expected_length,
2892 (unsigned char*)PACKET_data(pkt), comp_length) != (int)expected_length) {
2893 SSLfatal(sc, SSL_AD_BAD_CERTIFICATE, SSL_R_BAD_DECOMPRESSION);
2894 goto err;
2895 }
2896 ret = MSG_PROCESS_CONTINUE_PROCESSING;
2897 err:
2898 COMP_CTX_free(comp);
2899 return ret;
2900}
2901#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette