1*de0e0e4dSAntonio Huete Jimenez /* $OpenBSD: ssl_both.c,v 1.42 2022/02/05 14:54:10 jsing Exp $ */
272c33676SMaxim Ag /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
372c33676SMaxim Ag * All rights reserved.
472c33676SMaxim Ag *
572c33676SMaxim Ag * This package is an SSL implementation written
672c33676SMaxim Ag * by Eric Young (eay@cryptsoft.com).
772c33676SMaxim Ag * The implementation was written so as to conform with Netscapes SSL.
872c33676SMaxim Ag *
972c33676SMaxim Ag * This library is free for commercial and non-commercial use as long as
1072c33676SMaxim Ag * the following conditions are aheared to. The following conditions
1172c33676SMaxim Ag * apply to all code found in this distribution, be it the RC4, RSA,
1272c33676SMaxim Ag * lhash, DES, etc., code; not just the SSL code. The SSL documentation
1372c33676SMaxim Ag * included with this distribution is covered by the same copyright terms
1472c33676SMaxim Ag * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1572c33676SMaxim Ag *
1672c33676SMaxim Ag * Copyright remains Eric Young's, and as such any Copyright notices in
1772c33676SMaxim Ag * the code are not to be removed.
1872c33676SMaxim Ag * If this package is used in a product, Eric Young should be given attribution
1972c33676SMaxim Ag * as the author of the parts of the library used.
2072c33676SMaxim Ag * This can be in the form of a textual message at program startup or
2172c33676SMaxim Ag * in documentation (online or textual) provided with the package.
2272c33676SMaxim Ag *
2372c33676SMaxim Ag * Redistribution and use in source and binary forms, with or without
2472c33676SMaxim Ag * modification, are permitted provided that the following conditions
2572c33676SMaxim Ag * are met:
2672c33676SMaxim Ag * 1. Redistributions of source code must retain the copyright
2772c33676SMaxim Ag * notice, this list of conditions and the following disclaimer.
2872c33676SMaxim Ag * 2. Redistributions in binary form must reproduce the above copyright
2972c33676SMaxim Ag * notice, this list of conditions and the following disclaimer in the
3072c33676SMaxim Ag * documentation and/or other materials provided with the distribution.
3172c33676SMaxim Ag * 3. All advertising materials mentioning features or use of this software
3272c33676SMaxim Ag * must display the following acknowledgement:
3372c33676SMaxim Ag * "This product includes cryptographic software written by
3472c33676SMaxim Ag * Eric Young (eay@cryptsoft.com)"
3572c33676SMaxim Ag * The word 'cryptographic' can be left out if the rouines from the library
3672c33676SMaxim Ag * being used are not cryptographic related :-).
3772c33676SMaxim Ag * 4. If you include any Windows specific code (or a derivative thereof) from
3872c33676SMaxim Ag * the apps directory (application code) you must include an acknowledgement:
3972c33676SMaxim Ag * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4072c33676SMaxim Ag *
4172c33676SMaxim Ag * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4272c33676SMaxim Ag * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4372c33676SMaxim Ag * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4472c33676SMaxim Ag * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4572c33676SMaxim Ag * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4672c33676SMaxim Ag * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4772c33676SMaxim Ag * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4872c33676SMaxim Ag * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4972c33676SMaxim Ag * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5072c33676SMaxim Ag * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5172c33676SMaxim Ag * SUCH DAMAGE.
5272c33676SMaxim Ag *
5372c33676SMaxim Ag * The licence and distribution terms for any publically available version or
5472c33676SMaxim Ag * derivative of this code cannot be changed. i.e. this code cannot simply be
5572c33676SMaxim Ag * copied and put under another distribution licence
5672c33676SMaxim Ag * [including the GNU Public Licence.]
5772c33676SMaxim Ag */
5872c33676SMaxim Ag /* ====================================================================
5972c33676SMaxim Ag * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
6072c33676SMaxim Ag *
6172c33676SMaxim Ag * Redistribution and use in source and binary forms, with or without
6272c33676SMaxim Ag * modification, are permitted provided that the following conditions
6372c33676SMaxim Ag * are met:
6472c33676SMaxim Ag *
6572c33676SMaxim Ag * 1. Redistributions of source code must retain the above copyright
6672c33676SMaxim Ag * notice, this list of conditions and the following disclaimer.
6772c33676SMaxim Ag *
6872c33676SMaxim Ag * 2. Redistributions in binary form must reproduce the above copyright
6972c33676SMaxim Ag * notice, this list of conditions and the following disclaimer in
7072c33676SMaxim Ag * the documentation and/or other materials provided with the
7172c33676SMaxim Ag * distribution.
7272c33676SMaxim Ag *
7372c33676SMaxim Ag * 3. All advertising materials mentioning features or use of this
7472c33676SMaxim Ag * software must display the following acknowledgment:
7572c33676SMaxim Ag * "This product includes software developed by the OpenSSL Project
7672c33676SMaxim Ag * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
7772c33676SMaxim Ag *
7872c33676SMaxim Ag * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
7972c33676SMaxim Ag * endorse or promote products derived from this software without
8072c33676SMaxim Ag * prior written permission. For written permission, please contact
8172c33676SMaxim Ag * openssl-core@openssl.org.
8272c33676SMaxim Ag *
8372c33676SMaxim Ag * 5. Products derived from this software may not be called "OpenSSL"
8472c33676SMaxim Ag * nor may "OpenSSL" appear in their names without prior written
8572c33676SMaxim Ag * permission of the OpenSSL Project.
8672c33676SMaxim Ag *
8772c33676SMaxim Ag * 6. Redistributions of any form whatsoever must retain the following
8872c33676SMaxim Ag * acknowledgment:
8972c33676SMaxim Ag * "This product includes software developed by the OpenSSL Project
9072c33676SMaxim Ag * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
9172c33676SMaxim Ag *
9272c33676SMaxim Ag * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
9372c33676SMaxim Ag * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9472c33676SMaxim Ag * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
9572c33676SMaxim Ag * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
9672c33676SMaxim Ag * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9772c33676SMaxim Ag * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
9872c33676SMaxim Ag * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
9972c33676SMaxim Ag * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
10072c33676SMaxim Ag * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
10172c33676SMaxim Ag * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10272c33676SMaxim Ag * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
10372c33676SMaxim Ag * OF THE POSSIBILITY OF SUCH DAMAGE.
10472c33676SMaxim Ag * ====================================================================
10572c33676SMaxim Ag *
10672c33676SMaxim Ag * This product includes cryptographic software written by Eric Young
10772c33676SMaxim Ag * (eay@cryptsoft.com). This product includes software written by Tim
10872c33676SMaxim Ag * Hudson (tjh@cryptsoft.com).
10972c33676SMaxim Ag *
11072c33676SMaxim Ag */
11172c33676SMaxim Ag /* ====================================================================
11272c33676SMaxim Ag * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
11372c33676SMaxim Ag * ECC cipher suite support in OpenSSL originally developed by
11472c33676SMaxim Ag * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
11572c33676SMaxim Ag */
11672c33676SMaxim Ag
11772c33676SMaxim Ag #include <limits.h>
11872c33676SMaxim Ag #include <stdio.h>
11972c33676SMaxim Ag #include <string.h>
12072c33676SMaxim Ag
12172c33676SMaxim Ag #include <openssl/buffer.h>
12272c33676SMaxim Ag #include <openssl/evp.h>
12372c33676SMaxim Ag #include <openssl/objects.h>
12472c33676SMaxim Ag #include <openssl/x509.h>
12572c33676SMaxim Ag
12672c33676SMaxim Ag #include "bytestring.h"
127*de0e0e4dSAntonio Huete Jimenez #include "dtls_locl.h"
128*de0e0e4dSAntonio Huete Jimenez #include "ssl_locl.h"
12972c33676SMaxim Ag
13072c33676SMaxim Ag /*
13172c33676SMaxim Ag * Send s->internal->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
13272c33676SMaxim Ag * SSL3_RT_CHANGE_CIPHER_SPEC).
13372c33676SMaxim Ag */
13472c33676SMaxim Ag int
ssl3_do_write(SSL * s,int type)13572c33676SMaxim Ag ssl3_do_write(SSL *s, int type)
13672c33676SMaxim Ag {
13772c33676SMaxim Ag int ret;
13872c33676SMaxim Ag
13972c33676SMaxim Ag ret = ssl3_write_bytes(s, type, &s->internal->init_buf->data[s->internal->init_off],
14072c33676SMaxim Ag s->internal->init_num);
14172c33676SMaxim Ag if (ret < 0)
14272c33676SMaxim Ag return (-1);
14372c33676SMaxim Ag
14472c33676SMaxim Ag if (type == SSL3_RT_HANDSHAKE)
14572c33676SMaxim Ag /*
14672c33676SMaxim Ag * Should not be done for 'Hello Request's, but in that case
14772c33676SMaxim Ag * we'll ignore the result anyway.
14872c33676SMaxim Ag */
14972c33676SMaxim Ag tls1_transcript_record(s,
15072c33676SMaxim Ag (unsigned char *)&s->internal->init_buf->data[s->internal->init_off], ret);
15172c33676SMaxim Ag
15272c33676SMaxim Ag if (ret == s->internal->init_num) {
153*de0e0e4dSAntonio Huete Jimenez ssl_msg_callback(s, 1, type, s->internal->init_buf->data,
154*de0e0e4dSAntonio Huete Jimenez (size_t)(s->internal->init_off + s->internal->init_num));
15572c33676SMaxim Ag return (1);
15672c33676SMaxim Ag }
15772c33676SMaxim Ag
15872c33676SMaxim Ag s->internal->init_off += ret;
15972c33676SMaxim Ag s->internal->init_num -= ret;
16072c33676SMaxim Ag
16172c33676SMaxim Ag return (0);
16272c33676SMaxim Ag }
16372c33676SMaxim Ag
16472c33676SMaxim Ag int
ssl3_send_finished(SSL * s,int state_a,int state_b)165*de0e0e4dSAntonio Huete Jimenez ssl3_send_finished(SSL *s, int state_a, int state_b)
16672c33676SMaxim Ag {
16772c33676SMaxim Ag CBB cbb, finished;
16872c33676SMaxim Ag
16972c33676SMaxim Ag memset(&cbb, 0, sizeof(cbb));
17072c33676SMaxim Ag
171*de0e0e4dSAntonio Huete Jimenez if (s->s3->hs.state == state_a) {
172*de0e0e4dSAntonio Huete Jimenez if (!tls12_derive_finished(s))
173*de0e0e4dSAntonio Huete Jimenez goto err;
17472c33676SMaxim Ag
17572c33676SMaxim Ag /* Copy finished so we can use it for renegotiation checks. */
176*de0e0e4dSAntonio Huete Jimenez if (!s->server) {
177*de0e0e4dSAntonio Huete Jimenez memcpy(s->s3->previous_client_finished,
178*de0e0e4dSAntonio Huete Jimenez s->s3->hs.finished, s->s3->hs.finished_len);
179*de0e0e4dSAntonio Huete Jimenez s->s3->previous_client_finished_len =
180*de0e0e4dSAntonio Huete Jimenez s->s3->hs.finished_len;
18172c33676SMaxim Ag } else {
182*de0e0e4dSAntonio Huete Jimenez memcpy(s->s3->previous_server_finished,
183*de0e0e4dSAntonio Huete Jimenez s->s3->hs.finished, s->s3->hs.finished_len);
184*de0e0e4dSAntonio Huete Jimenez s->s3->previous_server_finished_len =
185*de0e0e4dSAntonio Huete Jimenez s->s3->hs.finished_len;
18672c33676SMaxim Ag }
18772c33676SMaxim Ag
18872c33676SMaxim Ag if (!ssl3_handshake_msg_start(s, &cbb, &finished,
18972c33676SMaxim Ag SSL3_MT_FINISHED))
19072c33676SMaxim Ag goto err;
191*de0e0e4dSAntonio Huete Jimenez if (!CBB_add_bytes(&finished, s->s3->hs.finished,
192*de0e0e4dSAntonio Huete Jimenez s->s3->hs.finished_len))
19372c33676SMaxim Ag goto err;
19472c33676SMaxim Ag if (!ssl3_handshake_msg_finish(s, &cbb))
19572c33676SMaxim Ag goto err;
19672c33676SMaxim Ag
197*de0e0e4dSAntonio Huete Jimenez s->s3->hs.state = state_b;
19872c33676SMaxim Ag }
19972c33676SMaxim Ag
20072c33676SMaxim Ag return (ssl3_handshake_write(s));
20172c33676SMaxim Ag
20272c33676SMaxim Ag err:
20372c33676SMaxim Ag CBB_cleanup(&cbb);
20472c33676SMaxim Ag
20572c33676SMaxim Ag return (-1);
20672c33676SMaxim Ag }
20772c33676SMaxim Ag
20872c33676SMaxim Ag int
ssl3_get_finished(SSL * s,int a,int b)20972c33676SMaxim Ag ssl3_get_finished(SSL *s, int a, int b)
21072c33676SMaxim Ag {
211*de0e0e4dSAntonio Huete Jimenez int al, md_len, ret;
21272c33676SMaxim Ag CBS cbs;
21372c33676SMaxim Ag
21472c33676SMaxim Ag /* should actually be 36+4 :-) */
215*de0e0e4dSAntonio Huete Jimenez if ((ret = ssl3_get_message(s, a, b, SSL3_MT_FINISHED, 64)) <= 0)
216*de0e0e4dSAntonio Huete Jimenez return ret;
21772c33676SMaxim Ag
21872c33676SMaxim Ag /* If this occurs, we have missed a message */
219*de0e0e4dSAntonio Huete Jimenez if (!s->s3->change_cipher_spec) {
22072c33676SMaxim Ag al = SSL_AD_UNEXPECTED_MESSAGE;
22172c33676SMaxim Ag SSLerror(s, SSL_R_GOT_A_FIN_BEFORE_A_CCS);
222*de0e0e4dSAntonio Huete Jimenez goto fatal_err;
22372c33676SMaxim Ag }
224*de0e0e4dSAntonio Huete Jimenez s->s3->change_cipher_spec = 0;
22572c33676SMaxim Ag
22672c33676SMaxim Ag md_len = TLS1_FINISH_MAC_LENGTH;
22772c33676SMaxim Ag
228*de0e0e4dSAntonio Huete Jimenez if (s->internal->init_num < 0) {
22972c33676SMaxim Ag al = SSL_AD_DECODE_ERROR;
23072c33676SMaxim Ag SSLerror(s, SSL_R_BAD_DIGEST_LENGTH);
231*de0e0e4dSAntonio Huete Jimenez goto fatal_err;
23272c33676SMaxim Ag }
23372c33676SMaxim Ag
234*de0e0e4dSAntonio Huete Jimenez CBS_init(&cbs, s->internal->init_msg, s->internal->init_num);
23572c33676SMaxim Ag
236*de0e0e4dSAntonio Huete Jimenez if (s->s3->hs.peer_finished_len != md_len ||
23772c33676SMaxim Ag CBS_len(&cbs) != md_len) {
23872c33676SMaxim Ag al = SSL_AD_DECODE_ERROR;
23972c33676SMaxim Ag SSLerror(s, SSL_R_BAD_DIGEST_LENGTH);
240*de0e0e4dSAntonio Huete Jimenez goto fatal_err;
24172c33676SMaxim Ag }
24272c33676SMaxim Ag
243*de0e0e4dSAntonio Huete Jimenez if (!CBS_mem_equal(&cbs, s->s3->hs.peer_finished, CBS_len(&cbs))) {
24472c33676SMaxim Ag al = SSL_AD_DECRYPT_ERROR;
24572c33676SMaxim Ag SSLerror(s, SSL_R_DIGEST_CHECK_FAILED);
246*de0e0e4dSAntonio Huete Jimenez goto fatal_err;
24772c33676SMaxim Ag }
24872c33676SMaxim Ag
24972c33676SMaxim Ag /* Copy finished so we can use it for renegotiation checks. */
25072c33676SMaxim Ag OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE);
251*de0e0e4dSAntonio Huete Jimenez if (s->server) {
252*de0e0e4dSAntonio Huete Jimenez memcpy(s->s3->previous_client_finished,
253*de0e0e4dSAntonio Huete Jimenez s->s3->hs.peer_finished, md_len);
254*de0e0e4dSAntonio Huete Jimenez s->s3->previous_client_finished_len = md_len;
25572c33676SMaxim Ag } else {
256*de0e0e4dSAntonio Huete Jimenez memcpy(s->s3->previous_server_finished,
257*de0e0e4dSAntonio Huete Jimenez s->s3->hs.peer_finished, md_len);
258*de0e0e4dSAntonio Huete Jimenez s->s3->previous_server_finished_len = md_len;
25972c33676SMaxim Ag }
26072c33676SMaxim Ag
26172c33676SMaxim Ag return (1);
262*de0e0e4dSAntonio Huete Jimenez fatal_err:
26372c33676SMaxim Ag ssl3_send_alert(s, SSL3_AL_FATAL, al);
26472c33676SMaxim Ag return (0);
26572c33676SMaxim Ag }
26672c33676SMaxim Ag
26772c33676SMaxim Ag int
ssl3_send_change_cipher_spec(SSL * s,int a,int b)26872c33676SMaxim Ag ssl3_send_change_cipher_spec(SSL *s, int a, int b)
26972c33676SMaxim Ag {
27072c33676SMaxim Ag size_t outlen;
27172c33676SMaxim Ag CBB cbb;
27272c33676SMaxim Ag
27372c33676SMaxim Ag memset(&cbb, 0, sizeof(cbb));
27472c33676SMaxim Ag
275*de0e0e4dSAntonio Huete Jimenez if (s->s3->hs.state == a) {
27672c33676SMaxim Ag if (!CBB_init_fixed(&cbb, s->internal->init_buf->data,
27772c33676SMaxim Ag s->internal->init_buf->length))
27872c33676SMaxim Ag goto err;
27972c33676SMaxim Ag if (!CBB_add_u8(&cbb, SSL3_MT_CCS))
28072c33676SMaxim Ag goto err;
28172c33676SMaxim Ag if (!CBB_finish(&cbb, NULL, &outlen))
28272c33676SMaxim Ag goto err;
28372c33676SMaxim Ag
28472c33676SMaxim Ag if (outlen > INT_MAX)
28572c33676SMaxim Ag goto err;
28672c33676SMaxim Ag
28772c33676SMaxim Ag s->internal->init_num = (int)outlen;
28872c33676SMaxim Ag s->internal->init_off = 0;
28972c33676SMaxim Ag
290*de0e0e4dSAntonio Huete Jimenez if (SSL_is_dtls(s)) {
291*de0e0e4dSAntonio Huete Jimenez s->d1->handshake_write_seq =
292*de0e0e4dSAntonio Huete Jimenez s->d1->next_handshake_write_seq;
29372c33676SMaxim Ag dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
294*de0e0e4dSAntonio Huete Jimenez s->d1->handshake_write_seq, 0, 0);
29572c33676SMaxim Ag dtls1_buffer_message(s, 1);
29672c33676SMaxim Ag }
29772c33676SMaxim Ag
298*de0e0e4dSAntonio Huete Jimenez s->s3->hs.state = b;
29972c33676SMaxim Ag }
30072c33676SMaxim Ag
30172c33676SMaxim Ag /* SSL3_ST_CW_CHANGE_B */
30272c33676SMaxim Ag return ssl3_record_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
30372c33676SMaxim Ag
30472c33676SMaxim Ag err:
30572c33676SMaxim Ag CBB_cleanup(&cbb);
30672c33676SMaxim Ag
30772c33676SMaxim Ag return -1;
30872c33676SMaxim Ag }
30972c33676SMaxim Ag
31072c33676SMaxim Ag static int
ssl3_add_cert(CBB * cbb,X509 * x)31172c33676SMaxim Ag ssl3_add_cert(CBB *cbb, X509 *x)
31272c33676SMaxim Ag {
31372c33676SMaxim Ag unsigned char *data;
31472c33676SMaxim Ag int cert_len;
31572c33676SMaxim Ag int ret = 0;
31672c33676SMaxim Ag CBB cert;
31772c33676SMaxim Ag
31872c33676SMaxim Ag if ((cert_len = i2d_X509(x, NULL)) < 0)
31972c33676SMaxim Ag goto err;
32072c33676SMaxim Ag
32172c33676SMaxim Ag if (!CBB_add_u24_length_prefixed(cbb, &cert))
32272c33676SMaxim Ag goto err;
32372c33676SMaxim Ag if (!CBB_add_space(&cert, &data, cert_len))
32472c33676SMaxim Ag goto err;
32572c33676SMaxim Ag if (i2d_X509(x, &data) < 0)
32672c33676SMaxim Ag goto err;
32772c33676SMaxim Ag if (!CBB_flush(cbb))
32872c33676SMaxim Ag goto err;
32972c33676SMaxim Ag
33072c33676SMaxim Ag ret = 1;
33172c33676SMaxim Ag
33272c33676SMaxim Ag err:
33372c33676SMaxim Ag return (ret);
33472c33676SMaxim Ag }
33572c33676SMaxim Ag
33672c33676SMaxim Ag int
ssl3_output_cert_chain(SSL * s,CBB * cbb,SSL_CERT_PKEY * cpk)337*de0e0e4dSAntonio Huete Jimenez ssl3_output_cert_chain(SSL *s, CBB *cbb, SSL_CERT_PKEY *cpk)
33872c33676SMaxim Ag {
33972c33676SMaxim Ag X509_STORE_CTX *xs_ctx = NULL;
34072c33676SMaxim Ag STACK_OF(X509) *chain;
34172c33676SMaxim Ag CBB cert_list;
34272c33676SMaxim Ag X509 *x;
34372c33676SMaxim Ag int ret = 0;
34472c33676SMaxim Ag int i;
34572c33676SMaxim Ag
34672c33676SMaxim Ag if (!CBB_add_u24_length_prefixed(cbb, &cert_list))
34772c33676SMaxim Ag goto err;
34872c33676SMaxim Ag
34972c33676SMaxim Ag /* Send an empty certificate list when no certificate is available. */
35072c33676SMaxim Ag if (cpk == NULL)
35172c33676SMaxim Ag goto done;
35272c33676SMaxim Ag
35372c33676SMaxim Ag if ((chain = cpk->chain) == NULL)
35472c33676SMaxim Ag chain = s->ctx->extra_certs;
35572c33676SMaxim Ag
35672c33676SMaxim Ag if (chain != NULL || (s->internal->mode & SSL_MODE_NO_AUTO_CHAIN)) {
35772c33676SMaxim Ag if (!ssl3_add_cert(&cert_list, cpk->x509))
35872c33676SMaxim Ag goto err;
35972c33676SMaxim Ag } else {
36072c33676SMaxim Ag if ((xs_ctx = X509_STORE_CTX_new()) == NULL)
36172c33676SMaxim Ag goto err;
36272c33676SMaxim Ag if (!X509_STORE_CTX_init(xs_ctx, s->ctx->cert_store,
36372c33676SMaxim Ag cpk->x509, NULL)) {
36472c33676SMaxim Ag SSLerror(s, ERR_R_X509_LIB);
36572c33676SMaxim Ag goto err;
36672c33676SMaxim Ag }
3672eb7d3b8SDaniel Fojt X509_VERIFY_PARAM_set_flags(X509_STORE_CTX_get0_param(xs_ctx),
3682eb7d3b8SDaniel Fojt X509_V_FLAG_LEGACY_VERIFY);
36972c33676SMaxim Ag X509_verify_cert(xs_ctx);
37072c33676SMaxim Ag ERR_clear_error();
371*de0e0e4dSAntonio Huete Jimenez chain = X509_STORE_CTX_get0_chain(xs_ctx);
37272c33676SMaxim Ag }
37372c33676SMaxim Ag
37472c33676SMaxim Ag for (i = 0; i < sk_X509_num(chain); i++) {
37572c33676SMaxim Ag x = sk_X509_value(chain, i);
37672c33676SMaxim Ag if (!ssl3_add_cert(&cert_list, x))
37772c33676SMaxim Ag goto err;
37872c33676SMaxim Ag }
37972c33676SMaxim Ag
38072c33676SMaxim Ag done:
38172c33676SMaxim Ag if (!CBB_flush(cbb))
38272c33676SMaxim Ag goto err;
38372c33676SMaxim Ag
38472c33676SMaxim Ag ret = 1;
38572c33676SMaxim Ag
38672c33676SMaxim Ag err:
38772c33676SMaxim Ag X509_STORE_CTX_free(xs_ctx);
38872c33676SMaxim Ag
38972c33676SMaxim Ag return (ret);
39072c33676SMaxim Ag }
39172c33676SMaxim Ag
39272c33676SMaxim Ag /*
39372c33676SMaxim Ag * Obtain handshake message of message type 'mt' (any if mt == -1),
39472c33676SMaxim Ag * maximum acceptable body length 'max'.
39572c33676SMaxim Ag * The first four bytes (msg_type and length) are read in state 'st1',
39672c33676SMaxim Ag * the body is read in state 'stn'.
39772c33676SMaxim Ag */
398*de0e0e4dSAntonio Huete Jimenez int
ssl3_get_message(SSL * s,int st1,int stn,int mt,long max)399*de0e0e4dSAntonio Huete Jimenez ssl3_get_message(SSL *s, int st1, int stn, int mt, long max)
40072c33676SMaxim Ag {
40172c33676SMaxim Ag unsigned char *p;
40272c33676SMaxim Ag uint32_t l;
40372c33676SMaxim Ag long n;
40472c33676SMaxim Ag int i, al;
40572c33676SMaxim Ag CBS cbs;
40672c33676SMaxim Ag uint8_t u8;
40772c33676SMaxim Ag
408*de0e0e4dSAntonio Huete Jimenez if (SSL_is_dtls(s))
409*de0e0e4dSAntonio Huete Jimenez return dtls1_get_message(s, st1, stn, mt, max);
410cca6fc52SDaniel Fojt
411*de0e0e4dSAntonio Huete Jimenez if (s->s3->hs.tls12.reuse_message) {
412*de0e0e4dSAntonio Huete Jimenez s->s3->hs.tls12.reuse_message = 0;
413*de0e0e4dSAntonio Huete Jimenez if ((mt >= 0) && (s->s3->hs.tls12.message_type != mt)) {
41472c33676SMaxim Ag al = SSL_AD_UNEXPECTED_MESSAGE;
41572c33676SMaxim Ag SSLerror(s, SSL_R_UNEXPECTED_MESSAGE);
416*de0e0e4dSAntonio Huete Jimenez goto fatal_err;
41772c33676SMaxim Ag }
418*de0e0e4dSAntonio Huete Jimenez s->internal->init_msg = s->internal->init_buf->data +
419*de0e0e4dSAntonio Huete Jimenez SSL3_HM_HEADER_LENGTH;
420*de0e0e4dSAntonio Huete Jimenez s->internal->init_num = (int)s->s3->hs.tls12.message_size;
421*de0e0e4dSAntonio Huete Jimenez return 1;
42272c33676SMaxim Ag }
42372c33676SMaxim Ag
42472c33676SMaxim Ag p = (unsigned char *)s->internal->init_buf->data;
42572c33676SMaxim Ag
426*de0e0e4dSAntonio Huete Jimenez if (s->s3->hs.state == st1) {
42772c33676SMaxim Ag int skip_message;
42872c33676SMaxim Ag
42972c33676SMaxim Ag do {
430*de0e0e4dSAntonio Huete Jimenez while (s->internal->init_num < SSL3_HM_HEADER_LENGTH) {
431*de0e0e4dSAntonio Huete Jimenez i = s->method->ssl_read_bytes(s,
43272c33676SMaxim Ag SSL3_RT_HANDSHAKE, &p[s->internal->init_num],
433*de0e0e4dSAntonio Huete Jimenez SSL3_HM_HEADER_LENGTH - s->internal->init_num, 0);
43472c33676SMaxim Ag if (i <= 0) {
43572c33676SMaxim Ag s->internal->rwstate = SSL_READING;
43672c33676SMaxim Ag return i;
43772c33676SMaxim Ag }
43872c33676SMaxim Ag s->internal->init_num += i;
43972c33676SMaxim Ag }
44072c33676SMaxim Ag
44172c33676SMaxim Ag skip_message = 0;
44272c33676SMaxim Ag if (!s->server && p[0] == SSL3_MT_HELLO_REQUEST) {
44372c33676SMaxim Ag /*
44472c33676SMaxim Ag * The server may always send 'Hello Request'
44572c33676SMaxim Ag * messages -- we are doing a handshake anyway
44672c33676SMaxim Ag * now, so ignore them if their format is
44772c33676SMaxim Ag * correct. Does not count for 'Finished' MAC.
44872c33676SMaxim Ag */
44972c33676SMaxim Ag if (p[1] == 0 && p[2] == 0 &&p[3] == 0) {
45072c33676SMaxim Ag s->internal->init_num = 0;
45172c33676SMaxim Ag skip_message = 1;
45272c33676SMaxim Ag
453*de0e0e4dSAntonio Huete Jimenez ssl_msg_callback(s, 0,
454*de0e0e4dSAntonio Huete Jimenez SSL3_RT_HANDSHAKE, p,
455*de0e0e4dSAntonio Huete Jimenez SSL3_HM_HEADER_LENGTH);
45672c33676SMaxim Ag }
45772c33676SMaxim Ag }
45872c33676SMaxim Ag } while (skip_message);
45972c33676SMaxim Ag
46072c33676SMaxim Ag if ((mt >= 0) && (*p != mt)) {
46172c33676SMaxim Ag al = SSL_AD_UNEXPECTED_MESSAGE;
46272c33676SMaxim Ag SSLerror(s, SSL_R_UNEXPECTED_MESSAGE);
463*de0e0e4dSAntonio Huete Jimenez goto fatal_err;
46472c33676SMaxim Ag }
46572c33676SMaxim Ag
466*de0e0e4dSAntonio Huete Jimenez CBS_init(&cbs, p, SSL3_HM_HEADER_LENGTH);
46772c33676SMaxim Ag if (!CBS_get_u8(&cbs, &u8) ||
46872c33676SMaxim Ag !CBS_get_u24(&cbs, &l)) {
46972c33676SMaxim Ag SSLerror(s, ERR_R_BUF_LIB);
47072c33676SMaxim Ag goto err;
47172c33676SMaxim Ag }
472*de0e0e4dSAntonio Huete Jimenez s->s3->hs.tls12.message_type = u8;
47372c33676SMaxim Ag
47472c33676SMaxim Ag if (l > (unsigned long)max) {
47572c33676SMaxim Ag al = SSL_AD_ILLEGAL_PARAMETER;
47672c33676SMaxim Ag SSLerror(s, SSL_R_EXCESSIVE_MESSAGE_SIZE);
477*de0e0e4dSAntonio Huete Jimenez goto fatal_err;
47872c33676SMaxim Ag }
479*de0e0e4dSAntonio Huete Jimenez if (l && !BUF_MEM_grow_clean(s->internal->init_buf,
480*de0e0e4dSAntonio Huete Jimenez l + SSL3_HM_HEADER_LENGTH)) {
48172c33676SMaxim Ag SSLerror(s, ERR_R_BUF_LIB);
48272c33676SMaxim Ag goto err;
48372c33676SMaxim Ag }
484*de0e0e4dSAntonio Huete Jimenez s->s3->hs.tls12.message_size = l;
485*de0e0e4dSAntonio Huete Jimenez s->s3->hs.state = stn;
48672c33676SMaxim Ag
487*de0e0e4dSAntonio Huete Jimenez s->internal->init_msg = s->internal->init_buf->data +
488*de0e0e4dSAntonio Huete Jimenez SSL3_HM_HEADER_LENGTH;
48972c33676SMaxim Ag s->internal->init_num = 0;
49072c33676SMaxim Ag }
49172c33676SMaxim Ag
49272c33676SMaxim Ag /* next state (stn) */
49372c33676SMaxim Ag p = s->internal->init_msg;
494*de0e0e4dSAntonio Huete Jimenez n = s->s3->hs.tls12.message_size - s->internal->init_num;
49572c33676SMaxim Ag while (n > 0) {
496*de0e0e4dSAntonio Huete Jimenez i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE,
49772c33676SMaxim Ag &p[s->internal->init_num], n, 0);
49872c33676SMaxim Ag if (i <= 0) {
49972c33676SMaxim Ag s->internal->rwstate = SSL_READING;
50072c33676SMaxim Ag return i;
50172c33676SMaxim Ag }
50272c33676SMaxim Ag s->internal->init_num += i;
50372c33676SMaxim Ag n -= i;
50472c33676SMaxim Ag }
50572c33676SMaxim Ag
50672c33676SMaxim Ag /* Feed this message into MAC computation. */
50772c33676SMaxim Ag if (s->internal->mac_packet) {
50872c33676SMaxim Ag tls1_transcript_record(s, (unsigned char *)s->internal->init_buf->data,
509*de0e0e4dSAntonio Huete Jimenez s->internal->init_num + SSL3_HM_HEADER_LENGTH);
51072c33676SMaxim Ag
511*de0e0e4dSAntonio Huete Jimenez ssl_msg_callback(s, 0, SSL3_RT_HANDSHAKE,
512*de0e0e4dSAntonio Huete Jimenez s->internal->init_buf->data,
513*de0e0e4dSAntonio Huete Jimenez (size_t)s->internal->init_num + SSL3_HM_HEADER_LENGTH);
51472c33676SMaxim Ag }
51572c33676SMaxim Ag
516*de0e0e4dSAntonio Huete Jimenez return 1;
51772c33676SMaxim Ag
518*de0e0e4dSAntonio Huete Jimenez fatal_err:
51972c33676SMaxim Ag ssl3_send_alert(s, SSL3_AL_FATAL, al);
52072c33676SMaxim Ag err:
521*de0e0e4dSAntonio Huete Jimenez return -1;
52272c33676SMaxim Ag }
52372c33676SMaxim Ag
52472c33676SMaxim Ag int
ssl_cert_type(EVP_PKEY * pkey)525*de0e0e4dSAntonio Huete Jimenez ssl_cert_type(EVP_PKEY *pkey)
52672c33676SMaxim Ag {
52772c33676SMaxim Ag if (pkey == NULL)
528*de0e0e4dSAntonio Huete Jimenez return -1;
52972c33676SMaxim Ag
530*de0e0e4dSAntonio Huete Jimenez switch (EVP_PKEY_id(pkey)) {
531*de0e0e4dSAntonio Huete Jimenez case EVP_PKEY_EC:
532*de0e0e4dSAntonio Huete Jimenez return SSL_PKEY_ECC;
533*de0e0e4dSAntonio Huete Jimenez case NID_id_GostR3410_2001:
534*de0e0e4dSAntonio Huete Jimenez case NID_id_GostR3410_2001_cc:
535*de0e0e4dSAntonio Huete Jimenez return SSL_PKEY_GOST01;
536*de0e0e4dSAntonio Huete Jimenez case EVP_PKEY_RSA:
537*de0e0e4dSAntonio Huete Jimenez return SSL_PKEY_RSA;
53872c33676SMaxim Ag }
53972c33676SMaxim Ag
540*de0e0e4dSAntonio Huete Jimenez return -1;
54172c33676SMaxim Ag }
54272c33676SMaxim Ag
54372c33676SMaxim Ag int
ssl_verify_alarm_type(long type)54472c33676SMaxim Ag ssl_verify_alarm_type(long type)
54572c33676SMaxim Ag {
54672c33676SMaxim Ag int al;
54772c33676SMaxim Ag
54872c33676SMaxim Ag switch (type) {
54972c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
55072c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_GET_CRL:
55172c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
55272c33676SMaxim Ag al = SSL_AD_UNKNOWN_CA;
55372c33676SMaxim Ag break;
55472c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
55572c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
55672c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
55772c33676SMaxim Ag case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
55872c33676SMaxim Ag case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
55972c33676SMaxim Ag case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
56072c33676SMaxim Ag case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
56172c33676SMaxim Ag case X509_V_ERR_CERT_NOT_YET_VALID:
56272c33676SMaxim Ag case X509_V_ERR_CRL_NOT_YET_VALID:
56372c33676SMaxim Ag case X509_V_ERR_CERT_UNTRUSTED:
56472c33676SMaxim Ag case X509_V_ERR_CERT_REJECTED:
56572c33676SMaxim Ag al = SSL_AD_BAD_CERTIFICATE;
56672c33676SMaxim Ag break;
56772c33676SMaxim Ag case X509_V_ERR_CERT_SIGNATURE_FAILURE:
56872c33676SMaxim Ag case X509_V_ERR_CRL_SIGNATURE_FAILURE:
56972c33676SMaxim Ag al = SSL_AD_DECRYPT_ERROR;
57072c33676SMaxim Ag break;
57172c33676SMaxim Ag case X509_V_ERR_CERT_HAS_EXPIRED:
57272c33676SMaxim Ag case X509_V_ERR_CRL_HAS_EXPIRED:
57372c33676SMaxim Ag al = SSL_AD_CERTIFICATE_EXPIRED;
57472c33676SMaxim Ag break;
57572c33676SMaxim Ag case X509_V_ERR_CERT_REVOKED:
57672c33676SMaxim Ag al = SSL_AD_CERTIFICATE_REVOKED;
57772c33676SMaxim Ag break;
57872c33676SMaxim Ag case X509_V_ERR_OUT_OF_MEM:
57972c33676SMaxim Ag al = SSL_AD_INTERNAL_ERROR;
58072c33676SMaxim Ag break;
58172c33676SMaxim Ag case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
58272c33676SMaxim Ag case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
58372c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
58472c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
58572c33676SMaxim Ag case X509_V_ERR_CERT_CHAIN_TOO_LONG:
58672c33676SMaxim Ag case X509_V_ERR_PATH_LENGTH_EXCEEDED:
58772c33676SMaxim Ag case X509_V_ERR_INVALID_CA:
58872c33676SMaxim Ag al = SSL_AD_UNKNOWN_CA;
58972c33676SMaxim Ag break;
59072c33676SMaxim Ag case X509_V_ERR_APPLICATION_VERIFICATION:
59172c33676SMaxim Ag al = SSL_AD_HANDSHAKE_FAILURE;
59272c33676SMaxim Ag break;
59372c33676SMaxim Ag case X509_V_ERR_INVALID_PURPOSE:
59472c33676SMaxim Ag al = SSL_AD_UNSUPPORTED_CERTIFICATE;
59572c33676SMaxim Ag break;
59672c33676SMaxim Ag default:
59772c33676SMaxim Ag al = SSL_AD_CERTIFICATE_UNKNOWN;
59872c33676SMaxim Ag break;
59972c33676SMaxim Ag }
60072c33676SMaxim Ag return (al);
60172c33676SMaxim Ag }
60272c33676SMaxim Ag
60372c33676SMaxim Ag int
ssl3_setup_init_buffer(SSL * s)60472c33676SMaxim Ag ssl3_setup_init_buffer(SSL *s)
60572c33676SMaxim Ag {
60672c33676SMaxim Ag BUF_MEM *buf = NULL;
60772c33676SMaxim Ag
60872c33676SMaxim Ag if (s->internal->init_buf != NULL)
60972c33676SMaxim Ag return (1);
61072c33676SMaxim Ag
61172c33676SMaxim Ag if ((buf = BUF_MEM_new()) == NULL)
61272c33676SMaxim Ag goto err;
61372c33676SMaxim Ag if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH))
61472c33676SMaxim Ag goto err;
61572c33676SMaxim Ag
61672c33676SMaxim Ag s->internal->init_buf = buf;
61772c33676SMaxim Ag return (1);
61872c33676SMaxim Ag
61972c33676SMaxim Ag err:
62072c33676SMaxim Ag BUF_MEM_free(buf);
62172c33676SMaxim Ag return (0);
62272c33676SMaxim Ag }
62372c33676SMaxim Ag
6248edacedfSDaniel Fojt void
ssl3_release_init_buffer(SSL * s)6258edacedfSDaniel Fojt ssl3_release_init_buffer(SSL *s)
6268edacedfSDaniel Fojt {
6278edacedfSDaniel Fojt BUF_MEM_free(s->internal->init_buf);
6288edacedfSDaniel Fojt s->internal->init_buf = NULL;
6298edacedfSDaniel Fojt s->internal->init_msg = NULL;
6308edacedfSDaniel Fojt s->internal->init_num = 0;
6318edacedfSDaniel Fojt s->internal->init_off = 0;
6328edacedfSDaniel Fojt }
6338edacedfSDaniel Fojt
63472c33676SMaxim Ag int
ssl3_setup_read_buffer(SSL * s)63572c33676SMaxim Ag ssl3_setup_read_buffer(SSL *s)
63672c33676SMaxim Ag {
63772c33676SMaxim Ag unsigned char *p;
63872c33676SMaxim Ag size_t len, align, headerlen;
63972c33676SMaxim Ag
640*de0e0e4dSAntonio Huete Jimenez if (SSL_is_dtls(s))
64172c33676SMaxim Ag headerlen = DTLS1_RT_HEADER_LENGTH;
64272c33676SMaxim Ag else
64372c33676SMaxim Ag headerlen = SSL3_RT_HEADER_LENGTH;
64472c33676SMaxim Ag
64572c33676SMaxim Ag align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
64672c33676SMaxim Ag
647*de0e0e4dSAntonio Huete Jimenez if (s->s3->rbuf.buf == NULL) {
64872c33676SMaxim Ag len = SSL3_RT_MAX_PLAIN_LENGTH +
64972c33676SMaxim Ag SSL3_RT_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
650cca6fc52SDaniel Fojt if ((p = calloc(1, len)) == NULL)
65172c33676SMaxim Ag goto err;
652*de0e0e4dSAntonio Huete Jimenez s->s3->rbuf.buf = p;
653*de0e0e4dSAntonio Huete Jimenez s->s3->rbuf.len = len;
65472c33676SMaxim Ag }
65572c33676SMaxim Ag
656*de0e0e4dSAntonio Huete Jimenez s->internal->packet = s->s3->rbuf.buf;
65772c33676SMaxim Ag return 1;
65872c33676SMaxim Ag
65972c33676SMaxim Ag err:
66072c33676SMaxim Ag SSLerror(s, ERR_R_MALLOC_FAILURE);
66172c33676SMaxim Ag return 0;
66272c33676SMaxim Ag }
66372c33676SMaxim Ag
66472c33676SMaxim Ag int
ssl3_setup_write_buffer(SSL * s)66572c33676SMaxim Ag ssl3_setup_write_buffer(SSL *s)
66672c33676SMaxim Ag {
66772c33676SMaxim Ag unsigned char *p;
66872c33676SMaxim Ag size_t len, align, headerlen;
66972c33676SMaxim Ag
670*de0e0e4dSAntonio Huete Jimenez if (SSL_is_dtls(s))
67172c33676SMaxim Ag headerlen = DTLS1_RT_HEADER_LENGTH + 1;
67272c33676SMaxim Ag else
67372c33676SMaxim Ag headerlen = SSL3_RT_HEADER_LENGTH;
67472c33676SMaxim Ag
67572c33676SMaxim Ag align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
67672c33676SMaxim Ag
677*de0e0e4dSAntonio Huete Jimenez if (s->s3->wbuf.buf == NULL) {
67872c33676SMaxim Ag len = s->max_send_fragment +
67972c33676SMaxim Ag SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
68072c33676SMaxim Ag if (!(s->internal->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
68172c33676SMaxim Ag len += headerlen + align +
68272c33676SMaxim Ag SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
68372c33676SMaxim Ag
684cca6fc52SDaniel Fojt if ((p = calloc(1, len)) == NULL)
68572c33676SMaxim Ag goto err;
686*de0e0e4dSAntonio Huete Jimenez s->s3->wbuf.buf = p;
687*de0e0e4dSAntonio Huete Jimenez s->s3->wbuf.len = len;
68872c33676SMaxim Ag }
68972c33676SMaxim Ag
69072c33676SMaxim Ag return 1;
69172c33676SMaxim Ag
69272c33676SMaxim Ag err:
69372c33676SMaxim Ag SSLerror(s, ERR_R_MALLOC_FAILURE);
69472c33676SMaxim Ag return 0;
69572c33676SMaxim Ag }
69672c33676SMaxim Ag
69772c33676SMaxim Ag int
ssl3_setup_buffers(SSL * s)69872c33676SMaxim Ag ssl3_setup_buffers(SSL *s)
69972c33676SMaxim Ag {
70072c33676SMaxim Ag if (!ssl3_setup_read_buffer(s))
70172c33676SMaxim Ag return 0;
70272c33676SMaxim Ag if (!ssl3_setup_write_buffer(s))
70372c33676SMaxim Ag return 0;
70472c33676SMaxim Ag return 1;
70572c33676SMaxim Ag }
70672c33676SMaxim Ag
7078edacedfSDaniel Fojt void
ssl3_release_buffer(SSL3_BUFFER_INTERNAL * b)7088edacedfSDaniel Fojt ssl3_release_buffer(SSL3_BUFFER_INTERNAL *b)
70972c33676SMaxim Ag {
7108edacedfSDaniel Fojt freezero(b->buf, b->len);
7118edacedfSDaniel Fojt b->buf = NULL;
7128edacedfSDaniel Fojt b->len = 0;
71372c33676SMaxim Ag }
71472c33676SMaxim Ag
7158edacedfSDaniel Fojt void
ssl3_release_read_buffer(SSL * s)71672c33676SMaxim Ag ssl3_release_read_buffer(SSL *s)
71772c33676SMaxim Ag {
718*de0e0e4dSAntonio Huete Jimenez ssl3_release_buffer(&s->s3->rbuf);
7198edacedfSDaniel Fojt }
7208edacedfSDaniel Fojt
7218edacedfSDaniel Fojt void
ssl3_release_write_buffer(SSL * s)7228edacedfSDaniel Fojt ssl3_release_write_buffer(SSL *s)
7238edacedfSDaniel Fojt {
724*de0e0e4dSAntonio Huete Jimenez ssl3_release_buffer(&s->s3->wbuf);
72572c33676SMaxim Ag }
726