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