1*72c33676SMaxim Ag /* $OpenBSD: ssl_both.c,v 1.15 2019/03/25 16:35:48 jsing Exp $ */ 2*72c33676SMaxim Ag /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3*72c33676SMaxim Ag * All rights reserved. 4*72c33676SMaxim Ag * 5*72c33676SMaxim Ag * This package is an SSL implementation written 6*72c33676SMaxim Ag * by Eric Young (eay@cryptsoft.com). 7*72c33676SMaxim Ag * The implementation was written so as to conform with Netscapes SSL. 8*72c33676SMaxim Ag * 9*72c33676SMaxim Ag * This library is free for commercial and non-commercial use as long as 10*72c33676SMaxim Ag * the following conditions are aheared to. The following conditions 11*72c33676SMaxim Ag * apply to all code found in this distribution, be it the RC4, RSA, 12*72c33676SMaxim Ag * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13*72c33676SMaxim Ag * included with this distribution is covered by the same copyright terms 14*72c33676SMaxim Ag * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15*72c33676SMaxim Ag * 16*72c33676SMaxim Ag * Copyright remains Eric Young's, and as such any Copyright notices in 17*72c33676SMaxim Ag * the code are not to be removed. 18*72c33676SMaxim Ag * If this package is used in a product, Eric Young should be given attribution 19*72c33676SMaxim Ag * as the author of the parts of the library used. 20*72c33676SMaxim Ag * This can be in the form of a textual message at program startup or 21*72c33676SMaxim Ag * in documentation (online or textual) provided with the package. 22*72c33676SMaxim Ag * 23*72c33676SMaxim Ag * Redistribution and use in source and binary forms, with or without 24*72c33676SMaxim Ag * modification, are permitted provided that the following conditions 25*72c33676SMaxim Ag * are met: 26*72c33676SMaxim Ag * 1. Redistributions of source code must retain the copyright 27*72c33676SMaxim Ag * notice, this list of conditions and the following disclaimer. 28*72c33676SMaxim Ag * 2. Redistributions in binary form must reproduce the above copyright 29*72c33676SMaxim Ag * notice, this list of conditions and the following disclaimer in the 30*72c33676SMaxim Ag * documentation and/or other materials provided with the distribution. 31*72c33676SMaxim Ag * 3. All advertising materials mentioning features or use of this software 32*72c33676SMaxim Ag * must display the following acknowledgement: 33*72c33676SMaxim Ag * "This product includes cryptographic software written by 34*72c33676SMaxim Ag * Eric Young (eay@cryptsoft.com)" 35*72c33676SMaxim Ag * The word 'cryptographic' can be left out if the rouines from the library 36*72c33676SMaxim Ag * being used are not cryptographic related :-). 37*72c33676SMaxim Ag * 4. If you include any Windows specific code (or a derivative thereof) from 38*72c33676SMaxim Ag * the apps directory (application code) you must include an acknowledgement: 39*72c33676SMaxim Ag * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40*72c33676SMaxim Ag * 41*72c33676SMaxim Ag * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42*72c33676SMaxim Ag * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43*72c33676SMaxim Ag * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44*72c33676SMaxim Ag * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45*72c33676SMaxim Ag * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46*72c33676SMaxim Ag * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47*72c33676SMaxim Ag * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*72c33676SMaxim Ag * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49*72c33676SMaxim Ag * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50*72c33676SMaxim Ag * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51*72c33676SMaxim Ag * SUCH DAMAGE. 52*72c33676SMaxim Ag * 53*72c33676SMaxim Ag * The licence and distribution terms for any publically available version or 54*72c33676SMaxim Ag * derivative of this code cannot be changed. i.e. this code cannot simply be 55*72c33676SMaxim Ag * copied and put under another distribution licence 56*72c33676SMaxim Ag * [including the GNU Public Licence.] 57*72c33676SMaxim Ag */ 58*72c33676SMaxim Ag /* ==================================================================== 59*72c33676SMaxim Ag * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. 60*72c33676SMaxim Ag * 61*72c33676SMaxim Ag * Redistribution and use in source and binary forms, with or without 62*72c33676SMaxim Ag * modification, are permitted provided that the following conditions 63*72c33676SMaxim Ag * are met: 64*72c33676SMaxim Ag * 65*72c33676SMaxim Ag * 1. Redistributions of source code must retain the above copyright 66*72c33676SMaxim Ag * notice, this list of conditions and the following disclaimer. 67*72c33676SMaxim Ag * 68*72c33676SMaxim Ag * 2. Redistributions in binary form must reproduce the above copyright 69*72c33676SMaxim Ag * notice, this list of conditions and the following disclaimer in 70*72c33676SMaxim Ag * the documentation and/or other materials provided with the 71*72c33676SMaxim Ag * distribution. 72*72c33676SMaxim Ag * 73*72c33676SMaxim Ag * 3. All advertising materials mentioning features or use of this 74*72c33676SMaxim Ag * software must display the following acknowledgment: 75*72c33676SMaxim Ag * "This product includes software developed by the OpenSSL Project 76*72c33676SMaxim Ag * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77*72c33676SMaxim Ag * 78*72c33676SMaxim Ag * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79*72c33676SMaxim Ag * endorse or promote products derived from this software without 80*72c33676SMaxim Ag * prior written permission. For written permission, please contact 81*72c33676SMaxim Ag * openssl-core@openssl.org. 82*72c33676SMaxim Ag * 83*72c33676SMaxim Ag * 5. Products derived from this software may not be called "OpenSSL" 84*72c33676SMaxim Ag * nor may "OpenSSL" appear in their names without prior written 85*72c33676SMaxim Ag * permission of the OpenSSL Project. 86*72c33676SMaxim Ag * 87*72c33676SMaxim Ag * 6. Redistributions of any form whatsoever must retain the following 88*72c33676SMaxim Ag * acknowledgment: 89*72c33676SMaxim Ag * "This product includes software developed by the OpenSSL Project 90*72c33676SMaxim Ag * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91*72c33676SMaxim Ag * 92*72c33676SMaxim Ag * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93*72c33676SMaxim Ag * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94*72c33676SMaxim Ag * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95*72c33676SMaxim Ag * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96*72c33676SMaxim Ag * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97*72c33676SMaxim Ag * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98*72c33676SMaxim Ag * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99*72c33676SMaxim Ag * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100*72c33676SMaxim Ag * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101*72c33676SMaxim Ag * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102*72c33676SMaxim Ag * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103*72c33676SMaxim Ag * OF THE POSSIBILITY OF SUCH DAMAGE. 104*72c33676SMaxim Ag * ==================================================================== 105*72c33676SMaxim Ag * 106*72c33676SMaxim Ag * This product includes cryptographic software written by Eric Young 107*72c33676SMaxim Ag * (eay@cryptsoft.com). This product includes software written by Tim 108*72c33676SMaxim Ag * Hudson (tjh@cryptsoft.com). 109*72c33676SMaxim Ag * 110*72c33676SMaxim Ag */ 111*72c33676SMaxim Ag /* ==================================================================== 112*72c33676SMaxim Ag * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 113*72c33676SMaxim Ag * ECC cipher suite support in OpenSSL originally developed by 114*72c33676SMaxim Ag * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. 115*72c33676SMaxim Ag */ 116*72c33676SMaxim Ag 117*72c33676SMaxim Ag #include <limits.h> 118*72c33676SMaxim Ag #include <stdio.h> 119*72c33676SMaxim Ag #include <string.h> 120*72c33676SMaxim Ag 121*72c33676SMaxim Ag #include "ssl_locl.h" 122*72c33676SMaxim Ag 123*72c33676SMaxim Ag #include <openssl/buffer.h> 124*72c33676SMaxim Ag #include <openssl/evp.h> 125*72c33676SMaxim Ag #include <openssl/objects.h> 126*72c33676SMaxim Ag #include <openssl/x509.h> 127*72c33676SMaxim Ag 128*72c33676SMaxim Ag #include "bytestring.h" 129*72c33676SMaxim Ag 130*72c33676SMaxim Ag /* 131*72c33676SMaxim Ag * Send s->internal->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or 132*72c33676SMaxim Ag * SSL3_RT_CHANGE_CIPHER_SPEC). 133*72c33676SMaxim Ag */ 134*72c33676SMaxim Ag int 135*72c33676SMaxim Ag ssl3_do_write(SSL *s, int type) 136*72c33676SMaxim Ag { 137*72c33676SMaxim Ag int ret; 138*72c33676SMaxim Ag 139*72c33676SMaxim Ag ret = ssl3_write_bytes(s, type, &s->internal->init_buf->data[s->internal->init_off], 140*72c33676SMaxim Ag s->internal->init_num); 141*72c33676SMaxim Ag if (ret < 0) 142*72c33676SMaxim Ag return (-1); 143*72c33676SMaxim Ag 144*72c33676SMaxim Ag if (type == SSL3_RT_HANDSHAKE) 145*72c33676SMaxim Ag /* 146*72c33676SMaxim Ag * Should not be done for 'Hello Request's, but in that case 147*72c33676SMaxim Ag * we'll ignore the result anyway. 148*72c33676SMaxim Ag */ 149*72c33676SMaxim Ag tls1_transcript_record(s, 150*72c33676SMaxim Ag (unsigned char *)&s->internal->init_buf->data[s->internal->init_off], ret); 151*72c33676SMaxim Ag 152*72c33676SMaxim Ag if (ret == s->internal->init_num) { 153*72c33676SMaxim Ag if (s->internal->msg_callback) 154*72c33676SMaxim Ag s->internal->msg_callback(1, s->version, type, s->internal->init_buf->data, 155*72c33676SMaxim Ag (size_t)(s->internal->init_off + s->internal->init_num), s, 156*72c33676SMaxim Ag s->internal->msg_callback_arg); 157*72c33676SMaxim Ag return (1); 158*72c33676SMaxim Ag } 159*72c33676SMaxim Ag 160*72c33676SMaxim Ag s->internal->init_off += ret; 161*72c33676SMaxim Ag s->internal->init_num -= ret; 162*72c33676SMaxim Ag 163*72c33676SMaxim Ag return (0); 164*72c33676SMaxim Ag } 165*72c33676SMaxim Ag 166*72c33676SMaxim Ag int 167*72c33676SMaxim Ag ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen) 168*72c33676SMaxim Ag { 169*72c33676SMaxim Ag CBB cbb, finished; 170*72c33676SMaxim Ag int md_len; 171*72c33676SMaxim Ag 172*72c33676SMaxim Ag memset(&cbb, 0, sizeof(cbb)); 173*72c33676SMaxim Ag 174*72c33676SMaxim Ag if (S3I(s)->hs.state == a) { 175*72c33676SMaxim Ag md_len = TLS1_FINISH_MAC_LENGTH; 176*72c33676SMaxim Ag OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE); 177*72c33676SMaxim Ag 178*72c33676SMaxim Ag if (tls1_final_finish_mac(s, sender, slen, 179*72c33676SMaxim Ag S3I(s)->tmp.finish_md) != md_len) 180*72c33676SMaxim Ag return (0); 181*72c33676SMaxim Ag S3I(s)->tmp.finish_md_len = md_len; 182*72c33676SMaxim Ag 183*72c33676SMaxim Ag /* Copy finished so we can use it for renegotiation checks. */ 184*72c33676SMaxim Ag if (s->internal->type == SSL_ST_CONNECT) { 185*72c33676SMaxim Ag memcpy(S3I(s)->previous_client_finished, 186*72c33676SMaxim Ag S3I(s)->tmp.finish_md, md_len); 187*72c33676SMaxim Ag S3I(s)->previous_client_finished_len = md_len; 188*72c33676SMaxim Ag } else { 189*72c33676SMaxim Ag memcpy(S3I(s)->previous_server_finished, 190*72c33676SMaxim Ag S3I(s)->tmp.finish_md, md_len); 191*72c33676SMaxim Ag S3I(s)->previous_server_finished_len = md_len; 192*72c33676SMaxim Ag } 193*72c33676SMaxim Ag 194*72c33676SMaxim Ag if (!ssl3_handshake_msg_start(s, &cbb, &finished, 195*72c33676SMaxim Ag SSL3_MT_FINISHED)) 196*72c33676SMaxim Ag goto err; 197*72c33676SMaxim Ag if (!CBB_add_bytes(&finished, S3I(s)->tmp.finish_md, md_len)) 198*72c33676SMaxim Ag goto err; 199*72c33676SMaxim Ag if (!ssl3_handshake_msg_finish(s, &cbb)) 200*72c33676SMaxim Ag goto err; 201*72c33676SMaxim Ag 202*72c33676SMaxim Ag S3I(s)->hs.state = b; 203*72c33676SMaxim Ag } 204*72c33676SMaxim Ag 205*72c33676SMaxim Ag return (ssl3_handshake_write(s)); 206*72c33676SMaxim Ag 207*72c33676SMaxim Ag err: 208*72c33676SMaxim Ag CBB_cleanup(&cbb); 209*72c33676SMaxim Ag 210*72c33676SMaxim Ag return (-1); 211*72c33676SMaxim Ag } 212*72c33676SMaxim Ag 213*72c33676SMaxim Ag /* 214*72c33676SMaxim Ag * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen 215*72c33676SMaxim Ag * so far. 216*72c33676SMaxim Ag */ 217*72c33676SMaxim Ag static void 218*72c33676SMaxim Ag ssl3_take_mac(SSL *s) 219*72c33676SMaxim Ag { 220*72c33676SMaxim Ag const char *sender; 221*72c33676SMaxim Ag int slen; 222*72c33676SMaxim Ag 223*72c33676SMaxim Ag /* 224*72c33676SMaxim Ag * If no new cipher setup return immediately: other functions will 225*72c33676SMaxim Ag * set the appropriate error. 226*72c33676SMaxim Ag */ 227*72c33676SMaxim Ag if (S3I(s)->hs.new_cipher == NULL) 228*72c33676SMaxim Ag return; 229*72c33676SMaxim Ag 230*72c33676SMaxim Ag if (S3I(s)->hs.state & SSL_ST_CONNECT) { 231*72c33676SMaxim Ag sender = TLS_MD_SERVER_FINISH_CONST; 232*72c33676SMaxim Ag slen = TLS_MD_SERVER_FINISH_CONST_SIZE; 233*72c33676SMaxim Ag } else { 234*72c33676SMaxim Ag sender = TLS_MD_CLIENT_FINISH_CONST; 235*72c33676SMaxim Ag slen = TLS_MD_CLIENT_FINISH_CONST_SIZE; 236*72c33676SMaxim Ag } 237*72c33676SMaxim Ag 238*72c33676SMaxim Ag S3I(s)->tmp.peer_finish_md_len = 239*72c33676SMaxim Ag tls1_final_finish_mac(s, sender, slen, 240*72c33676SMaxim Ag S3I(s)->tmp.peer_finish_md); 241*72c33676SMaxim Ag } 242*72c33676SMaxim Ag 243*72c33676SMaxim Ag int 244*72c33676SMaxim Ag ssl3_get_finished(SSL *s, int a, int b) 245*72c33676SMaxim Ag { 246*72c33676SMaxim Ag int al, ok, md_len; 247*72c33676SMaxim Ag long n; 248*72c33676SMaxim Ag CBS cbs; 249*72c33676SMaxim Ag 250*72c33676SMaxim Ag /* should actually be 36+4 :-) */ 251*72c33676SMaxim Ag n = s->method->internal->ssl_get_message(s, a, b, SSL3_MT_FINISHED, 64, &ok); 252*72c33676SMaxim Ag if (!ok) 253*72c33676SMaxim Ag return ((int)n); 254*72c33676SMaxim Ag 255*72c33676SMaxim Ag /* If this occurs, we have missed a message */ 256*72c33676SMaxim Ag if (!S3I(s)->change_cipher_spec) { 257*72c33676SMaxim Ag al = SSL_AD_UNEXPECTED_MESSAGE; 258*72c33676SMaxim Ag SSLerror(s, SSL_R_GOT_A_FIN_BEFORE_A_CCS); 259*72c33676SMaxim Ag goto f_err; 260*72c33676SMaxim Ag } 261*72c33676SMaxim Ag S3I(s)->change_cipher_spec = 0; 262*72c33676SMaxim Ag 263*72c33676SMaxim Ag md_len = TLS1_FINISH_MAC_LENGTH; 264*72c33676SMaxim Ag 265*72c33676SMaxim Ag if (n < 0) { 266*72c33676SMaxim Ag al = SSL_AD_DECODE_ERROR; 267*72c33676SMaxim Ag SSLerror(s, SSL_R_BAD_DIGEST_LENGTH); 268*72c33676SMaxim Ag goto f_err; 269*72c33676SMaxim Ag } 270*72c33676SMaxim Ag 271*72c33676SMaxim Ag CBS_init(&cbs, s->internal->init_msg, n); 272*72c33676SMaxim Ag 273*72c33676SMaxim Ag if (S3I(s)->tmp.peer_finish_md_len != md_len || 274*72c33676SMaxim Ag CBS_len(&cbs) != md_len) { 275*72c33676SMaxim Ag al = SSL_AD_DECODE_ERROR; 276*72c33676SMaxim Ag SSLerror(s, SSL_R_BAD_DIGEST_LENGTH); 277*72c33676SMaxim Ag goto f_err; 278*72c33676SMaxim Ag } 279*72c33676SMaxim Ag 280*72c33676SMaxim Ag if (!CBS_mem_equal(&cbs, S3I(s)->tmp.peer_finish_md, CBS_len(&cbs))) { 281*72c33676SMaxim Ag al = SSL_AD_DECRYPT_ERROR; 282*72c33676SMaxim Ag SSLerror(s, SSL_R_DIGEST_CHECK_FAILED); 283*72c33676SMaxim Ag goto f_err; 284*72c33676SMaxim Ag } 285*72c33676SMaxim Ag 286*72c33676SMaxim Ag /* Copy finished so we can use it for renegotiation checks. */ 287*72c33676SMaxim Ag OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE); 288*72c33676SMaxim Ag if (s->internal->type == SSL_ST_ACCEPT) { 289*72c33676SMaxim Ag memcpy(S3I(s)->previous_client_finished, 290*72c33676SMaxim Ag S3I(s)->tmp.peer_finish_md, md_len); 291*72c33676SMaxim Ag S3I(s)->previous_client_finished_len = md_len; 292*72c33676SMaxim Ag } else { 293*72c33676SMaxim Ag memcpy(S3I(s)->previous_server_finished, 294*72c33676SMaxim Ag S3I(s)->tmp.peer_finish_md, md_len); 295*72c33676SMaxim Ag S3I(s)->previous_server_finished_len = md_len; 296*72c33676SMaxim Ag } 297*72c33676SMaxim Ag 298*72c33676SMaxim Ag return (1); 299*72c33676SMaxim Ag f_err: 300*72c33676SMaxim Ag ssl3_send_alert(s, SSL3_AL_FATAL, al); 301*72c33676SMaxim Ag return (0); 302*72c33676SMaxim Ag } 303*72c33676SMaxim Ag 304*72c33676SMaxim Ag /* for these 2 messages, we need to 305*72c33676SMaxim Ag * ssl->enc_read_ctx re-init 306*72c33676SMaxim Ag * ssl->s3->internal->read_sequence zero 307*72c33676SMaxim Ag * ssl->s3->internal->read_mac_secret re-init 308*72c33676SMaxim Ag * ssl->session->read_sym_enc assign 309*72c33676SMaxim Ag * ssl->session->read_hash assign 310*72c33676SMaxim Ag */ 311*72c33676SMaxim Ag int 312*72c33676SMaxim Ag ssl3_send_change_cipher_spec(SSL *s, int a, int b) 313*72c33676SMaxim Ag { 314*72c33676SMaxim Ag size_t outlen; 315*72c33676SMaxim Ag CBB cbb; 316*72c33676SMaxim Ag 317*72c33676SMaxim Ag memset(&cbb, 0, sizeof(cbb)); 318*72c33676SMaxim Ag 319*72c33676SMaxim Ag if (S3I(s)->hs.state == a) { 320*72c33676SMaxim Ag if (!CBB_init_fixed(&cbb, s->internal->init_buf->data, 321*72c33676SMaxim Ag s->internal->init_buf->length)) 322*72c33676SMaxim Ag goto err; 323*72c33676SMaxim Ag if (!CBB_add_u8(&cbb, SSL3_MT_CCS)) 324*72c33676SMaxim Ag goto err; 325*72c33676SMaxim Ag if (!CBB_finish(&cbb, NULL, &outlen)) 326*72c33676SMaxim Ag goto err; 327*72c33676SMaxim Ag 328*72c33676SMaxim Ag if (outlen > INT_MAX) 329*72c33676SMaxim Ag goto err; 330*72c33676SMaxim Ag 331*72c33676SMaxim Ag s->internal->init_num = (int)outlen; 332*72c33676SMaxim Ag s->internal->init_off = 0; 333*72c33676SMaxim Ag 334*72c33676SMaxim Ag if (SSL_IS_DTLS(s)) { 335*72c33676SMaxim Ag D1I(s)->handshake_write_seq = 336*72c33676SMaxim Ag D1I(s)->next_handshake_write_seq; 337*72c33676SMaxim Ag dtls1_set_message_header_int(s, SSL3_MT_CCS, 0, 338*72c33676SMaxim Ag D1I(s)->handshake_write_seq, 0, 0); 339*72c33676SMaxim Ag dtls1_buffer_message(s, 1); 340*72c33676SMaxim Ag } 341*72c33676SMaxim Ag 342*72c33676SMaxim Ag S3I(s)->hs.state = b; 343*72c33676SMaxim Ag } 344*72c33676SMaxim Ag 345*72c33676SMaxim Ag /* SSL3_ST_CW_CHANGE_B */ 346*72c33676SMaxim Ag return ssl3_record_write(s, SSL3_RT_CHANGE_CIPHER_SPEC); 347*72c33676SMaxim Ag 348*72c33676SMaxim Ag err: 349*72c33676SMaxim Ag CBB_cleanup(&cbb); 350*72c33676SMaxim Ag 351*72c33676SMaxim Ag return -1; 352*72c33676SMaxim Ag } 353*72c33676SMaxim Ag 354*72c33676SMaxim Ag static int 355*72c33676SMaxim Ag ssl3_add_cert(CBB *cbb, X509 *x) 356*72c33676SMaxim Ag { 357*72c33676SMaxim Ag unsigned char *data; 358*72c33676SMaxim Ag int cert_len; 359*72c33676SMaxim Ag int ret = 0; 360*72c33676SMaxim Ag CBB cert; 361*72c33676SMaxim Ag 362*72c33676SMaxim Ag if ((cert_len = i2d_X509(x, NULL)) < 0) 363*72c33676SMaxim Ag goto err; 364*72c33676SMaxim Ag 365*72c33676SMaxim Ag if (!CBB_add_u24_length_prefixed(cbb, &cert)) 366*72c33676SMaxim Ag goto err; 367*72c33676SMaxim Ag if (!CBB_add_space(&cert, &data, cert_len)) 368*72c33676SMaxim Ag goto err; 369*72c33676SMaxim Ag if (i2d_X509(x, &data) < 0) 370*72c33676SMaxim Ag goto err; 371*72c33676SMaxim Ag if (!CBB_flush(cbb)) 372*72c33676SMaxim Ag goto err; 373*72c33676SMaxim Ag 374*72c33676SMaxim Ag ret = 1; 375*72c33676SMaxim Ag 376*72c33676SMaxim Ag err: 377*72c33676SMaxim Ag return (ret); 378*72c33676SMaxim Ag } 379*72c33676SMaxim Ag 380*72c33676SMaxim Ag int 381*72c33676SMaxim Ag ssl3_output_cert_chain(SSL *s, CBB *cbb, CERT_PKEY *cpk) 382*72c33676SMaxim Ag { 383*72c33676SMaxim Ag X509_STORE_CTX *xs_ctx = NULL; 384*72c33676SMaxim Ag STACK_OF(X509) *chain; 385*72c33676SMaxim Ag CBB cert_list; 386*72c33676SMaxim Ag X509 *x; 387*72c33676SMaxim Ag int ret = 0; 388*72c33676SMaxim Ag int i; 389*72c33676SMaxim Ag 390*72c33676SMaxim Ag if (!CBB_add_u24_length_prefixed(cbb, &cert_list)) 391*72c33676SMaxim Ag goto err; 392*72c33676SMaxim Ag 393*72c33676SMaxim Ag /* Send an empty certificate list when no certificate is available. */ 394*72c33676SMaxim Ag if (cpk == NULL) 395*72c33676SMaxim Ag goto done; 396*72c33676SMaxim Ag 397*72c33676SMaxim Ag if ((chain = cpk->chain) == NULL) 398*72c33676SMaxim Ag chain = s->ctx->extra_certs; 399*72c33676SMaxim Ag 400*72c33676SMaxim Ag if (chain != NULL || (s->internal->mode & SSL_MODE_NO_AUTO_CHAIN)) { 401*72c33676SMaxim Ag if (!ssl3_add_cert(&cert_list, cpk->x509)) 402*72c33676SMaxim Ag goto err; 403*72c33676SMaxim Ag } else { 404*72c33676SMaxim Ag if ((xs_ctx = X509_STORE_CTX_new()) == NULL) 405*72c33676SMaxim Ag goto err; 406*72c33676SMaxim Ag if (!X509_STORE_CTX_init(xs_ctx, s->ctx->cert_store, 407*72c33676SMaxim Ag cpk->x509, NULL)) { 408*72c33676SMaxim Ag SSLerror(s, ERR_R_X509_LIB); 409*72c33676SMaxim Ag goto err; 410*72c33676SMaxim Ag } 411*72c33676SMaxim Ag X509_verify_cert(xs_ctx); 412*72c33676SMaxim Ag ERR_clear_error(); 413*72c33676SMaxim Ag chain = xs_ctx->chain; 414*72c33676SMaxim Ag } 415*72c33676SMaxim Ag 416*72c33676SMaxim Ag for (i = 0; i < sk_X509_num(chain); i++) { 417*72c33676SMaxim Ag x = sk_X509_value(chain, i); 418*72c33676SMaxim Ag if (!ssl3_add_cert(&cert_list, x)) 419*72c33676SMaxim Ag goto err; 420*72c33676SMaxim Ag } 421*72c33676SMaxim Ag 422*72c33676SMaxim Ag done: 423*72c33676SMaxim Ag if (!CBB_flush(cbb)) 424*72c33676SMaxim Ag goto err; 425*72c33676SMaxim Ag 426*72c33676SMaxim Ag ret = 1; 427*72c33676SMaxim Ag 428*72c33676SMaxim Ag err: 429*72c33676SMaxim Ag X509_STORE_CTX_free(xs_ctx); 430*72c33676SMaxim Ag 431*72c33676SMaxim Ag return (ret); 432*72c33676SMaxim Ag } 433*72c33676SMaxim Ag 434*72c33676SMaxim Ag /* 435*72c33676SMaxim Ag * Obtain handshake message of message type 'mt' (any if mt == -1), 436*72c33676SMaxim Ag * maximum acceptable body length 'max'. 437*72c33676SMaxim Ag * The first four bytes (msg_type and length) are read in state 'st1', 438*72c33676SMaxim Ag * the body is read in state 'stn'. 439*72c33676SMaxim Ag */ 440*72c33676SMaxim Ag long 441*72c33676SMaxim Ag ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok) 442*72c33676SMaxim Ag { 443*72c33676SMaxim Ag unsigned char *p; 444*72c33676SMaxim Ag uint32_t l; 445*72c33676SMaxim Ag long n; 446*72c33676SMaxim Ag int i, al; 447*72c33676SMaxim Ag CBS cbs; 448*72c33676SMaxim Ag uint8_t u8; 449*72c33676SMaxim Ag 450*72c33676SMaxim Ag if (S3I(s)->tmp.reuse_message) { 451*72c33676SMaxim Ag S3I(s)->tmp.reuse_message = 0; 452*72c33676SMaxim Ag if ((mt >= 0) && (S3I(s)->tmp.message_type != mt)) { 453*72c33676SMaxim Ag al = SSL_AD_UNEXPECTED_MESSAGE; 454*72c33676SMaxim Ag SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); 455*72c33676SMaxim Ag goto f_err; 456*72c33676SMaxim Ag } 457*72c33676SMaxim Ag *ok = 1; 458*72c33676SMaxim Ag s->internal->init_msg = s->internal->init_buf->data + 4; 459*72c33676SMaxim Ag s->internal->init_num = (int)S3I(s)->tmp.message_size; 460*72c33676SMaxim Ag return s->internal->init_num; 461*72c33676SMaxim Ag } 462*72c33676SMaxim Ag 463*72c33676SMaxim Ag p = (unsigned char *)s->internal->init_buf->data; 464*72c33676SMaxim Ag 465*72c33676SMaxim Ag /* s->internal->init_num < 4 */ 466*72c33676SMaxim Ag if (S3I(s)->hs.state == st1) { 467*72c33676SMaxim Ag int skip_message; 468*72c33676SMaxim Ag 469*72c33676SMaxim Ag do { 470*72c33676SMaxim Ag while (s->internal->init_num < 4) { 471*72c33676SMaxim Ag i = s->method->internal->ssl_read_bytes(s, 472*72c33676SMaxim Ag SSL3_RT_HANDSHAKE, &p[s->internal->init_num], 473*72c33676SMaxim Ag 4 - s->internal->init_num, 0); 474*72c33676SMaxim Ag if (i <= 0) { 475*72c33676SMaxim Ag s->internal->rwstate = SSL_READING; 476*72c33676SMaxim Ag *ok = 0; 477*72c33676SMaxim Ag return i; 478*72c33676SMaxim Ag } 479*72c33676SMaxim Ag s->internal->init_num += i; 480*72c33676SMaxim Ag } 481*72c33676SMaxim Ag 482*72c33676SMaxim Ag skip_message = 0; 483*72c33676SMaxim Ag if (!s->server && p[0] == SSL3_MT_HELLO_REQUEST) { 484*72c33676SMaxim Ag /* 485*72c33676SMaxim Ag * The server may always send 'Hello Request' 486*72c33676SMaxim Ag * messages -- we are doing a handshake anyway 487*72c33676SMaxim Ag * now, so ignore them if their format is 488*72c33676SMaxim Ag * correct. Does not count for 'Finished' MAC. 489*72c33676SMaxim Ag */ 490*72c33676SMaxim Ag if (p[1] == 0 && p[2] == 0 &&p[3] == 0) { 491*72c33676SMaxim Ag s->internal->init_num = 0; 492*72c33676SMaxim Ag skip_message = 1; 493*72c33676SMaxim Ag 494*72c33676SMaxim Ag if (s->internal->msg_callback) 495*72c33676SMaxim Ag s->internal->msg_callback(0, s->version, 496*72c33676SMaxim Ag SSL3_RT_HANDSHAKE, p, 4, s, 497*72c33676SMaxim Ag s->internal->msg_callback_arg); 498*72c33676SMaxim Ag } 499*72c33676SMaxim Ag } 500*72c33676SMaxim Ag } while (skip_message); 501*72c33676SMaxim Ag 502*72c33676SMaxim Ag /* s->internal->init_num == 4 */ 503*72c33676SMaxim Ag 504*72c33676SMaxim Ag if ((mt >= 0) && (*p != mt)) { 505*72c33676SMaxim Ag al = SSL_AD_UNEXPECTED_MESSAGE; 506*72c33676SMaxim Ag SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); 507*72c33676SMaxim Ag goto f_err; 508*72c33676SMaxim Ag } 509*72c33676SMaxim Ag 510*72c33676SMaxim Ag CBS_init(&cbs, p, 4); 511*72c33676SMaxim Ag if (!CBS_get_u8(&cbs, &u8) || 512*72c33676SMaxim Ag !CBS_get_u24(&cbs, &l)) { 513*72c33676SMaxim Ag SSLerror(s, ERR_R_BUF_LIB); 514*72c33676SMaxim Ag goto err; 515*72c33676SMaxim Ag } 516*72c33676SMaxim Ag S3I(s)->tmp.message_type = u8; 517*72c33676SMaxim Ag 518*72c33676SMaxim Ag if (l > (unsigned long)max) { 519*72c33676SMaxim Ag al = SSL_AD_ILLEGAL_PARAMETER; 520*72c33676SMaxim Ag SSLerror(s, SSL_R_EXCESSIVE_MESSAGE_SIZE); 521*72c33676SMaxim Ag goto f_err; 522*72c33676SMaxim Ag } 523*72c33676SMaxim Ag if (l && !BUF_MEM_grow_clean(s->internal->init_buf, l + 4)) { 524*72c33676SMaxim Ag SSLerror(s, ERR_R_BUF_LIB); 525*72c33676SMaxim Ag goto err; 526*72c33676SMaxim Ag } 527*72c33676SMaxim Ag S3I(s)->tmp.message_size = l; 528*72c33676SMaxim Ag S3I(s)->hs.state = stn; 529*72c33676SMaxim Ag 530*72c33676SMaxim Ag s->internal->init_msg = s->internal->init_buf->data + 4; 531*72c33676SMaxim Ag s->internal->init_num = 0; 532*72c33676SMaxim Ag } 533*72c33676SMaxim Ag 534*72c33676SMaxim Ag /* next state (stn) */ 535*72c33676SMaxim Ag p = s->internal->init_msg; 536*72c33676SMaxim Ag n = S3I(s)->tmp.message_size - s->internal->init_num; 537*72c33676SMaxim Ag while (n > 0) { 538*72c33676SMaxim Ag i = s->method->internal->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, 539*72c33676SMaxim Ag &p[s->internal->init_num], n, 0); 540*72c33676SMaxim Ag if (i <= 0) { 541*72c33676SMaxim Ag s->internal->rwstate = SSL_READING; 542*72c33676SMaxim Ag *ok = 0; 543*72c33676SMaxim Ag return i; 544*72c33676SMaxim Ag } 545*72c33676SMaxim Ag s->internal->init_num += i; 546*72c33676SMaxim Ag n -= i; 547*72c33676SMaxim Ag } 548*72c33676SMaxim Ag 549*72c33676SMaxim Ag /* If receiving Finished, record MAC of prior handshake messages for 550*72c33676SMaxim Ag * Finished verification. */ 551*72c33676SMaxim Ag if (*s->internal->init_buf->data == SSL3_MT_FINISHED) 552*72c33676SMaxim Ag ssl3_take_mac(s); 553*72c33676SMaxim Ag 554*72c33676SMaxim Ag /* Feed this message into MAC computation. */ 555*72c33676SMaxim Ag if (s->internal->mac_packet) { 556*72c33676SMaxim Ag tls1_transcript_record(s, (unsigned char *)s->internal->init_buf->data, 557*72c33676SMaxim Ag s->internal->init_num + 4); 558*72c33676SMaxim Ag 559*72c33676SMaxim Ag if (s->internal->msg_callback) 560*72c33676SMaxim Ag s->internal->msg_callback(0, s->version, 561*72c33676SMaxim Ag SSL3_RT_HANDSHAKE, s->internal->init_buf->data, 562*72c33676SMaxim Ag (size_t)s->internal->init_num + 4, s, 563*72c33676SMaxim Ag s->internal->msg_callback_arg); 564*72c33676SMaxim Ag } 565*72c33676SMaxim Ag 566*72c33676SMaxim Ag *ok = 1; 567*72c33676SMaxim Ag return (s->internal->init_num); 568*72c33676SMaxim Ag 569*72c33676SMaxim Ag f_err: 570*72c33676SMaxim Ag ssl3_send_alert(s, SSL3_AL_FATAL, al); 571*72c33676SMaxim Ag err: 572*72c33676SMaxim Ag *ok = 0; 573*72c33676SMaxim Ag return (-1); 574*72c33676SMaxim Ag } 575*72c33676SMaxim Ag 576*72c33676SMaxim Ag int 577*72c33676SMaxim Ag ssl_cert_type(X509 *x, EVP_PKEY *pkey) 578*72c33676SMaxim Ag { 579*72c33676SMaxim Ag EVP_PKEY *pk; 580*72c33676SMaxim Ag int ret = -1, i; 581*72c33676SMaxim Ag 582*72c33676SMaxim Ag if (pkey == NULL) 583*72c33676SMaxim Ag pk = X509_get_pubkey(x); 584*72c33676SMaxim Ag else 585*72c33676SMaxim Ag pk = pkey; 586*72c33676SMaxim Ag if (pk == NULL) 587*72c33676SMaxim Ag goto err; 588*72c33676SMaxim Ag 589*72c33676SMaxim Ag i = pk->type; 590*72c33676SMaxim Ag if (i == EVP_PKEY_RSA) { 591*72c33676SMaxim Ag ret = SSL_PKEY_RSA_ENC; 592*72c33676SMaxim Ag } else if (i == EVP_PKEY_EC) { 593*72c33676SMaxim Ag ret = SSL_PKEY_ECC; 594*72c33676SMaxim Ag } else if (i == NID_id_GostR3410_2001 || 595*72c33676SMaxim Ag i == NID_id_GostR3410_2001_cc) { 596*72c33676SMaxim Ag ret = SSL_PKEY_GOST01; 597*72c33676SMaxim Ag } 598*72c33676SMaxim Ag 599*72c33676SMaxim Ag err: 600*72c33676SMaxim Ag if (!pkey) 601*72c33676SMaxim Ag EVP_PKEY_free(pk); 602*72c33676SMaxim Ag return (ret); 603*72c33676SMaxim Ag } 604*72c33676SMaxim Ag 605*72c33676SMaxim Ag int 606*72c33676SMaxim Ag ssl_verify_alarm_type(long type) 607*72c33676SMaxim Ag { 608*72c33676SMaxim Ag int al; 609*72c33676SMaxim Ag 610*72c33676SMaxim Ag switch (type) { 611*72c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: 612*72c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_GET_CRL: 613*72c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: 614*72c33676SMaxim Ag al = SSL_AD_UNKNOWN_CA; 615*72c33676SMaxim Ag break; 616*72c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: 617*72c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: 618*72c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: 619*72c33676SMaxim Ag case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: 620*72c33676SMaxim Ag case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: 621*72c33676SMaxim Ag case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: 622*72c33676SMaxim Ag case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: 623*72c33676SMaxim Ag case X509_V_ERR_CERT_NOT_YET_VALID: 624*72c33676SMaxim Ag case X509_V_ERR_CRL_NOT_YET_VALID: 625*72c33676SMaxim Ag case X509_V_ERR_CERT_UNTRUSTED: 626*72c33676SMaxim Ag case X509_V_ERR_CERT_REJECTED: 627*72c33676SMaxim Ag al = SSL_AD_BAD_CERTIFICATE; 628*72c33676SMaxim Ag break; 629*72c33676SMaxim Ag case X509_V_ERR_CERT_SIGNATURE_FAILURE: 630*72c33676SMaxim Ag case X509_V_ERR_CRL_SIGNATURE_FAILURE: 631*72c33676SMaxim Ag al = SSL_AD_DECRYPT_ERROR; 632*72c33676SMaxim Ag break; 633*72c33676SMaxim Ag case X509_V_ERR_CERT_HAS_EXPIRED: 634*72c33676SMaxim Ag case X509_V_ERR_CRL_HAS_EXPIRED: 635*72c33676SMaxim Ag al = SSL_AD_CERTIFICATE_EXPIRED; 636*72c33676SMaxim Ag break; 637*72c33676SMaxim Ag case X509_V_ERR_CERT_REVOKED: 638*72c33676SMaxim Ag al = SSL_AD_CERTIFICATE_REVOKED; 639*72c33676SMaxim Ag break; 640*72c33676SMaxim Ag case X509_V_ERR_OUT_OF_MEM: 641*72c33676SMaxim Ag al = SSL_AD_INTERNAL_ERROR; 642*72c33676SMaxim Ag break; 643*72c33676SMaxim Ag case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: 644*72c33676SMaxim Ag case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: 645*72c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: 646*72c33676SMaxim Ag case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: 647*72c33676SMaxim Ag case X509_V_ERR_CERT_CHAIN_TOO_LONG: 648*72c33676SMaxim Ag case X509_V_ERR_PATH_LENGTH_EXCEEDED: 649*72c33676SMaxim Ag case X509_V_ERR_INVALID_CA: 650*72c33676SMaxim Ag al = SSL_AD_UNKNOWN_CA; 651*72c33676SMaxim Ag break; 652*72c33676SMaxim Ag case X509_V_ERR_APPLICATION_VERIFICATION: 653*72c33676SMaxim Ag al = SSL_AD_HANDSHAKE_FAILURE; 654*72c33676SMaxim Ag break; 655*72c33676SMaxim Ag case X509_V_ERR_INVALID_PURPOSE: 656*72c33676SMaxim Ag al = SSL_AD_UNSUPPORTED_CERTIFICATE; 657*72c33676SMaxim Ag break; 658*72c33676SMaxim Ag default: 659*72c33676SMaxim Ag al = SSL_AD_CERTIFICATE_UNKNOWN; 660*72c33676SMaxim Ag break; 661*72c33676SMaxim Ag } 662*72c33676SMaxim Ag return (al); 663*72c33676SMaxim Ag } 664*72c33676SMaxim Ag 665*72c33676SMaxim Ag int 666*72c33676SMaxim Ag ssl3_setup_init_buffer(SSL *s) 667*72c33676SMaxim Ag { 668*72c33676SMaxim Ag BUF_MEM *buf = NULL; 669*72c33676SMaxim Ag 670*72c33676SMaxim Ag if (s->internal->init_buf != NULL) 671*72c33676SMaxim Ag return (1); 672*72c33676SMaxim Ag 673*72c33676SMaxim Ag if ((buf = BUF_MEM_new()) == NULL) 674*72c33676SMaxim Ag goto err; 675*72c33676SMaxim Ag if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) 676*72c33676SMaxim Ag goto err; 677*72c33676SMaxim Ag 678*72c33676SMaxim Ag s->internal->init_buf = buf; 679*72c33676SMaxim Ag return (1); 680*72c33676SMaxim Ag 681*72c33676SMaxim Ag err: 682*72c33676SMaxim Ag BUF_MEM_free(buf); 683*72c33676SMaxim Ag return (0); 684*72c33676SMaxim Ag } 685*72c33676SMaxim Ag 686*72c33676SMaxim Ag int 687*72c33676SMaxim Ag ssl3_setup_read_buffer(SSL *s) 688*72c33676SMaxim Ag { 689*72c33676SMaxim Ag unsigned char *p; 690*72c33676SMaxim Ag size_t len, align, headerlen; 691*72c33676SMaxim Ag 692*72c33676SMaxim Ag if (SSL_IS_DTLS(s)) 693*72c33676SMaxim Ag headerlen = DTLS1_RT_HEADER_LENGTH; 694*72c33676SMaxim Ag else 695*72c33676SMaxim Ag headerlen = SSL3_RT_HEADER_LENGTH; 696*72c33676SMaxim Ag 697*72c33676SMaxim Ag align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1); 698*72c33676SMaxim Ag 699*72c33676SMaxim Ag if (S3I(s)->rbuf.buf == NULL) { 700*72c33676SMaxim Ag len = SSL3_RT_MAX_PLAIN_LENGTH + 701*72c33676SMaxim Ag SSL3_RT_MAX_ENCRYPTED_OVERHEAD + headerlen + align; 702*72c33676SMaxim Ag if ((p = malloc(len)) == NULL) 703*72c33676SMaxim Ag goto err; 704*72c33676SMaxim Ag S3I(s)->rbuf.buf = p; 705*72c33676SMaxim Ag S3I(s)->rbuf.len = len; 706*72c33676SMaxim Ag } 707*72c33676SMaxim Ag 708*72c33676SMaxim Ag s->internal->packet = &(S3I(s)->rbuf.buf[0]); 709*72c33676SMaxim Ag return 1; 710*72c33676SMaxim Ag 711*72c33676SMaxim Ag err: 712*72c33676SMaxim Ag SSLerror(s, ERR_R_MALLOC_FAILURE); 713*72c33676SMaxim Ag return 0; 714*72c33676SMaxim Ag } 715*72c33676SMaxim Ag 716*72c33676SMaxim Ag int 717*72c33676SMaxim Ag ssl3_setup_write_buffer(SSL *s) 718*72c33676SMaxim Ag { 719*72c33676SMaxim Ag unsigned char *p; 720*72c33676SMaxim Ag size_t len, align, headerlen; 721*72c33676SMaxim Ag 722*72c33676SMaxim Ag if (SSL_IS_DTLS(s)) 723*72c33676SMaxim Ag headerlen = DTLS1_RT_HEADER_LENGTH + 1; 724*72c33676SMaxim Ag else 725*72c33676SMaxim Ag headerlen = SSL3_RT_HEADER_LENGTH; 726*72c33676SMaxim Ag 727*72c33676SMaxim Ag align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1); 728*72c33676SMaxim Ag 729*72c33676SMaxim Ag if (S3I(s)->wbuf.buf == NULL) { 730*72c33676SMaxim Ag len = s->max_send_fragment + 731*72c33676SMaxim Ag SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align; 732*72c33676SMaxim Ag if (!(s->internal->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)) 733*72c33676SMaxim Ag len += headerlen + align + 734*72c33676SMaxim Ag SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD; 735*72c33676SMaxim Ag 736*72c33676SMaxim Ag if ((p = malloc(len)) == NULL) 737*72c33676SMaxim Ag goto err; 738*72c33676SMaxim Ag S3I(s)->wbuf.buf = p; 739*72c33676SMaxim Ag S3I(s)->wbuf.len = len; 740*72c33676SMaxim Ag } 741*72c33676SMaxim Ag 742*72c33676SMaxim Ag return 1; 743*72c33676SMaxim Ag 744*72c33676SMaxim Ag err: 745*72c33676SMaxim Ag SSLerror(s, ERR_R_MALLOC_FAILURE); 746*72c33676SMaxim Ag return 0; 747*72c33676SMaxim Ag } 748*72c33676SMaxim Ag 749*72c33676SMaxim Ag int 750*72c33676SMaxim Ag ssl3_setup_buffers(SSL *s) 751*72c33676SMaxim Ag { 752*72c33676SMaxim Ag if (!ssl3_setup_read_buffer(s)) 753*72c33676SMaxim Ag return 0; 754*72c33676SMaxim Ag if (!ssl3_setup_write_buffer(s)) 755*72c33676SMaxim Ag return 0; 756*72c33676SMaxim Ag return 1; 757*72c33676SMaxim Ag } 758*72c33676SMaxim Ag 759*72c33676SMaxim Ag int 760*72c33676SMaxim Ag ssl3_release_write_buffer(SSL *s) 761*72c33676SMaxim Ag { 762*72c33676SMaxim Ag free(S3I(s)->wbuf.buf); 763*72c33676SMaxim Ag S3I(s)->wbuf.buf = NULL; 764*72c33676SMaxim Ag return 1; 765*72c33676SMaxim Ag } 766*72c33676SMaxim Ag 767*72c33676SMaxim Ag int 768*72c33676SMaxim Ag ssl3_release_read_buffer(SSL *s) 769*72c33676SMaxim Ag { 770*72c33676SMaxim Ag free(S3I(s)->rbuf.buf); 771*72c33676SMaxim Ag S3I(s)->rbuf.buf = NULL; 772*72c33676SMaxim Ag return 1; 773*72c33676SMaxim Ag } 774