1 /* $OpenBSD: ssl_both.c,v 1.29 2021/04/25 13:15:22 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 "ssl_locl.h" 122 123 #include <openssl/buffer.h> 124 #include <openssl/evp.h> 125 #include <openssl/objects.h> 126 #include <openssl/x509.h> 127 128 #include "bytestring.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 if (s->internal->msg_callback) 154 s->internal->msg_callback(1, s->version, type, s->internal->init_buf->data, 155 (size_t)(s->internal->init_off + s->internal->init_num), s, 156 s->internal->msg_callback_arg); 157 return (1); 158 } 159 160 s->internal->init_off += ret; 161 s->internal->init_num -= ret; 162 163 return (0); 164 } 165 166 int 167 ssl3_send_finished(SSL *s, int state_a, int state_b) 168 { 169 CBB cbb, finished; 170 171 memset(&cbb, 0, sizeof(cbb)); 172 173 if (S3I(s)->hs.state == state_a) { 174 if (!tls12_derive_finished(s)) 175 goto err; 176 177 /* Copy finished so we can use it for renegotiation checks. */ 178 if (!s->server) { 179 memcpy(S3I(s)->previous_client_finished, 180 S3I(s)->hs.finished, S3I(s)->hs.finished_len); 181 S3I(s)->previous_client_finished_len = 182 S3I(s)->hs.finished_len; 183 } else { 184 memcpy(S3I(s)->previous_server_finished, 185 S3I(s)->hs.finished, S3I(s)->hs.finished_len); 186 S3I(s)->previous_server_finished_len = 187 S3I(s)->hs.finished_len; 188 } 189 190 if (!ssl3_handshake_msg_start(s, &cbb, &finished, 191 SSL3_MT_FINISHED)) 192 goto err; 193 if (!CBB_add_bytes(&finished, S3I(s)->hs.finished, 194 S3I(s)->hs.finished_len)) 195 goto err; 196 if (!ssl3_handshake_msg_finish(s, &cbb)) 197 goto err; 198 199 S3I(s)->hs.state = state_b; 200 } 201 202 return (ssl3_handshake_write(s)); 203 204 err: 205 CBB_cleanup(&cbb); 206 207 return (-1); 208 } 209 210 int 211 ssl3_get_finished(SSL *s, int a, int b) 212 { 213 int al, ok, md_len; 214 long n; 215 CBS cbs; 216 217 /* should actually be 36+4 :-) */ 218 n = ssl3_get_message(s, a, b, SSL3_MT_FINISHED, 64, &ok); 219 if (!ok) 220 return ((int)n); 221 222 /* If this occurs, we have missed a message */ 223 if (!S3I(s)->change_cipher_spec) { 224 al = SSL_AD_UNEXPECTED_MESSAGE; 225 SSLerror(s, SSL_R_GOT_A_FIN_BEFORE_A_CCS); 226 goto fatal_err; 227 } 228 S3I(s)->change_cipher_spec = 0; 229 230 md_len = TLS1_FINISH_MAC_LENGTH; 231 232 if (n < 0) { 233 al = SSL_AD_DECODE_ERROR; 234 SSLerror(s, SSL_R_BAD_DIGEST_LENGTH); 235 goto fatal_err; 236 } 237 238 CBS_init(&cbs, s->internal->init_msg, n); 239 240 if (S3I(s)->hs.peer_finished_len != md_len || 241 CBS_len(&cbs) != md_len) { 242 al = SSL_AD_DECODE_ERROR; 243 SSLerror(s, SSL_R_BAD_DIGEST_LENGTH); 244 goto fatal_err; 245 } 246 247 if (!CBS_mem_equal(&cbs, S3I(s)->hs.peer_finished, CBS_len(&cbs))) { 248 al = SSL_AD_DECRYPT_ERROR; 249 SSLerror(s, SSL_R_DIGEST_CHECK_FAILED); 250 goto fatal_err; 251 } 252 253 /* Copy finished so we can use it for renegotiation checks. */ 254 OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE); 255 if (s->server) { 256 memcpy(S3I(s)->previous_client_finished, 257 S3I(s)->hs.peer_finished, md_len); 258 S3I(s)->previous_client_finished_len = md_len; 259 } else { 260 memcpy(S3I(s)->previous_server_finished, 261 S3I(s)->hs.peer_finished, md_len); 262 S3I(s)->previous_server_finished_len = md_len; 263 } 264 265 return (1); 266 fatal_err: 267 ssl3_send_alert(s, SSL3_AL_FATAL, al); 268 return (0); 269 } 270 271 int 272 ssl3_send_change_cipher_spec(SSL *s, int a, int b) 273 { 274 size_t outlen; 275 CBB cbb; 276 277 memset(&cbb, 0, sizeof(cbb)); 278 279 if (S3I(s)->hs.state == a) { 280 if (!CBB_init_fixed(&cbb, s->internal->init_buf->data, 281 s->internal->init_buf->length)) 282 goto err; 283 if (!CBB_add_u8(&cbb, SSL3_MT_CCS)) 284 goto err; 285 if (!CBB_finish(&cbb, NULL, &outlen)) 286 goto err; 287 288 if (outlen > INT_MAX) 289 goto err; 290 291 s->internal->init_num = (int)outlen; 292 s->internal->init_off = 0; 293 294 if (SSL_is_dtls(s)) { 295 D1I(s)->handshake_write_seq = 296 D1I(s)->next_handshake_write_seq; 297 dtls1_set_message_header_int(s, SSL3_MT_CCS, 0, 298 D1I(s)->handshake_write_seq, 0, 0); 299 dtls1_buffer_message(s, 1); 300 } 301 302 S3I(s)->hs.state = b; 303 } 304 305 /* SSL3_ST_CW_CHANGE_B */ 306 return ssl3_record_write(s, SSL3_RT_CHANGE_CIPHER_SPEC); 307 308 err: 309 CBB_cleanup(&cbb); 310 311 return -1; 312 } 313 314 static int 315 ssl3_add_cert(CBB *cbb, X509 *x) 316 { 317 unsigned char *data; 318 int cert_len; 319 int ret = 0; 320 CBB cert; 321 322 if ((cert_len = i2d_X509(x, NULL)) < 0) 323 goto err; 324 325 if (!CBB_add_u24_length_prefixed(cbb, &cert)) 326 goto err; 327 if (!CBB_add_space(&cert, &data, cert_len)) 328 goto err; 329 if (i2d_X509(x, &data) < 0) 330 goto err; 331 if (!CBB_flush(cbb)) 332 goto err; 333 334 ret = 1; 335 336 err: 337 return (ret); 338 } 339 340 int 341 ssl3_output_cert_chain(SSL *s, CBB *cbb, CERT_PKEY *cpk) 342 { 343 X509_STORE_CTX *xs_ctx = NULL; 344 STACK_OF(X509) *chain; 345 CBB cert_list; 346 X509 *x; 347 int ret = 0; 348 int i; 349 350 if (!CBB_add_u24_length_prefixed(cbb, &cert_list)) 351 goto err; 352 353 /* Send an empty certificate list when no certificate is available. */ 354 if (cpk == NULL) 355 goto done; 356 357 if ((chain = cpk->chain) == NULL) 358 chain = s->ctx->extra_certs; 359 360 if (chain != NULL || (s->internal->mode & SSL_MODE_NO_AUTO_CHAIN)) { 361 if (!ssl3_add_cert(&cert_list, cpk->x509)) 362 goto err; 363 } else { 364 if ((xs_ctx = X509_STORE_CTX_new()) == NULL) 365 goto err; 366 if (!X509_STORE_CTX_init(xs_ctx, s->ctx->cert_store, 367 cpk->x509, NULL)) { 368 SSLerror(s, ERR_R_X509_LIB); 369 goto err; 370 } 371 X509_VERIFY_PARAM_set_flags(X509_STORE_CTX_get0_param(xs_ctx), 372 X509_V_FLAG_LEGACY_VERIFY); 373 X509_verify_cert(xs_ctx); 374 ERR_clear_error(); 375 chain = xs_ctx->chain; 376 } 377 378 for (i = 0; i < sk_X509_num(chain); i++) { 379 x = sk_X509_value(chain, i); 380 if (!ssl3_add_cert(&cert_list, x)) 381 goto err; 382 } 383 384 done: 385 if (!CBB_flush(cbb)) 386 goto err; 387 388 ret = 1; 389 390 err: 391 X509_STORE_CTX_free(xs_ctx); 392 393 return (ret); 394 } 395 396 /* 397 * Obtain handshake message of message type 'mt' (any if mt == -1), 398 * maximum acceptable body length 'max'. 399 * The first four bytes (msg_type and length) are read in state 'st1', 400 * the body is read in state 'stn'. 401 */ 402 long 403 ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok) 404 { 405 unsigned char *p; 406 uint32_t l; 407 long n; 408 int i, al; 409 CBS cbs; 410 uint8_t u8; 411 412 if (SSL_is_dtls(s)) 413 return (dtls1_get_message(s, st1, stn, mt, max, ok)); 414 415 if (S3I(s)->hs.tls12.reuse_message) { 416 S3I(s)->hs.tls12.reuse_message = 0; 417 if ((mt >= 0) && (S3I(s)->hs.tls12.message_type != mt)) { 418 al = SSL_AD_UNEXPECTED_MESSAGE; 419 SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); 420 goto fatal_err; 421 } 422 *ok = 1; 423 s->internal->init_msg = s->internal->init_buf->data + 4; 424 s->internal->init_num = (int)S3I(s)->hs.tls12.message_size; 425 return s->internal->init_num; 426 } 427 428 p = (unsigned char *)s->internal->init_buf->data; 429 430 /* s->internal->init_num < 4 */ 431 if (S3I(s)->hs.state == st1) { 432 int skip_message; 433 434 do { 435 while (s->internal->init_num < 4) { 436 i = s->method->internal->ssl_read_bytes(s, 437 SSL3_RT_HANDSHAKE, &p[s->internal->init_num], 438 4 - s->internal->init_num, 0); 439 if (i <= 0) { 440 s->internal->rwstate = SSL_READING; 441 *ok = 0; 442 return i; 443 } 444 s->internal->init_num += i; 445 } 446 447 skip_message = 0; 448 if (!s->server && p[0] == SSL3_MT_HELLO_REQUEST) { 449 /* 450 * The server may always send 'Hello Request' 451 * messages -- we are doing a handshake anyway 452 * now, so ignore them if their format is 453 * correct. Does not count for 'Finished' MAC. 454 */ 455 if (p[1] == 0 && p[2] == 0 &&p[3] == 0) { 456 s->internal->init_num = 0; 457 skip_message = 1; 458 459 if (s->internal->msg_callback) 460 s->internal->msg_callback(0, s->version, 461 SSL3_RT_HANDSHAKE, p, 4, s, 462 s->internal->msg_callback_arg); 463 } 464 } 465 } while (skip_message); 466 467 /* s->internal->init_num == 4 */ 468 469 if ((mt >= 0) && (*p != mt)) { 470 al = SSL_AD_UNEXPECTED_MESSAGE; 471 SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); 472 goto fatal_err; 473 } 474 475 CBS_init(&cbs, p, 4); 476 if (!CBS_get_u8(&cbs, &u8) || 477 !CBS_get_u24(&cbs, &l)) { 478 SSLerror(s, ERR_R_BUF_LIB); 479 goto err; 480 } 481 S3I(s)->hs.tls12.message_type = u8; 482 483 if (l > (unsigned long)max) { 484 al = SSL_AD_ILLEGAL_PARAMETER; 485 SSLerror(s, SSL_R_EXCESSIVE_MESSAGE_SIZE); 486 goto fatal_err; 487 } 488 if (l && !BUF_MEM_grow_clean(s->internal->init_buf, l + 4)) { 489 SSLerror(s, ERR_R_BUF_LIB); 490 goto err; 491 } 492 S3I(s)->hs.tls12.message_size = l; 493 S3I(s)->hs.state = stn; 494 495 s->internal->init_msg = s->internal->init_buf->data + 4; 496 s->internal->init_num = 0; 497 } 498 499 /* next state (stn) */ 500 p = s->internal->init_msg; 501 n = S3I(s)->hs.tls12.message_size - s->internal->init_num; 502 while (n > 0) { 503 i = s->method->internal->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, 504 &p[s->internal->init_num], n, 0); 505 if (i <= 0) { 506 s->internal->rwstate = SSL_READING; 507 *ok = 0; 508 return i; 509 } 510 s->internal->init_num += i; 511 n -= i; 512 } 513 514 /* 515 * If receiving Finished, record MAC of prior handshake messages for 516 * Finished verification. 517 */ 518 if (*s->internal->init_buf->data == SSL3_MT_FINISHED) { 519 if (S3I(s)->hs.cipher != NULL) { 520 if (!tls12_derive_peer_finished(s)) 521 goto err; 522 } 523 } 524 525 /* Feed this message into MAC computation. */ 526 if (s->internal->mac_packet) { 527 tls1_transcript_record(s, (unsigned char *)s->internal->init_buf->data, 528 s->internal->init_num + 4); 529 530 if (s->internal->msg_callback) 531 s->internal->msg_callback(0, s->version, 532 SSL3_RT_HANDSHAKE, s->internal->init_buf->data, 533 (size_t)s->internal->init_num + 4, s, 534 s->internal->msg_callback_arg); 535 } 536 537 *ok = 1; 538 return (s->internal->init_num); 539 540 fatal_err: 541 ssl3_send_alert(s, SSL3_AL_FATAL, al); 542 err: 543 *ok = 0; 544 return (-1); 545 } 546 547 int 548 ssl_cert_type(X509 *x, EVP_PKEY *pkey) 549 { 550 EVP_PKEY *pk; 551 int ret = -1, i; 552 553 if (pkey == NULL) 554 pk = X509_get_pubkey(x); 555 else 556 pk = pkey; 557 if (pk == NULL) 558 goto err; 559 560 i = pk->type; 561 if (i == EVP_PKEY_RSA) { 562 ret = SSL_PKEY_RSA; 563 } else if (i == EVP_PKEY_EC) { 564 ret = SSL_PKEY_ECC; 565 } else if (i == NID_id_GostR3410_2001 || 566 i == NID_id_GostR3410_2001_cc) { 567 ret = SSL_PKEY_GOST01; 568 } 569 570 err: 571 if (!pkey) 572 EVP_PKEY_free(pk); 573 return (ret); 574 } 575 576 int 577 ssl_verify_alarm_type(long type) 578 { 579 int al; 580 581 switch (type) { 582 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: 583 case X509_V_ERR_UNABLE_TO_GET_CRL: 584 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: 585 al = SSL_AD_UNKNOWN_CA; 586 break; 587 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: 588 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: 589 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: 590 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: 591 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: 592 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: 593 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: 594 case X509_V_ERR_CERT_NOT_YET_VALID: 595 case X509_V_ERR_CRL_NOT_YET_VALID: 596 case X509_V_ERR_CERT_UNTRUSTED: 597 case X509_V_ERR_CERT_REJECTED: 598 al = SSL_AD_BAD_CERTIFICATE; 599 break; 600 case X509_V_ERR_CERT_SIGNATURE_FAILURE: 601 case X509_V_ERR_CRL_SIGNATURE_FAILURE: 602 al = SSL_AD_DECRYPT_ERROR; 603 break; 604 case X509_V_ERR_CERT_HAS_EXPIRED: 605 case X509_V_ERR_CRL_HAS_EXPIRED: 606 al = SSL_AD_CERTIFICATE_EXPIRED; 607 break; 608 case X509_V_ERR_CERT_REVOKED: 609 al = SSL_AD_CERTIFICATE_REVOKED; 610 break; 611 case X509_V_ERR_OUT_OF_MEM: 612 al = SSL_AD_INTERNAL_ERROR; 613 break; 614 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: 615 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: 616 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: 617 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: 618 case X509_V_ERR_CERT_CHAIN_TOO_LONG: 619 case X509_V_ERR_PATH_LENGTH_EXCEEDED: 620 case X509_V_ERR_INVALID_CA: 621 al = SSL_AD_UNKNOWN_CA; 622 break; 623 case X509_V_ERR_APPLICATION_VERIFICATION: 624 al = SSL_AD_HANDSHAKE_FAILURE; 625 break; 626 case X509_V_ERR_INVALID_PURPOSE: 627 al = SSL_AD_UNSUPPORTED_CERTIFICATE; 628 break; 629 default: 630 al = SSL_AD_CERTIFICATE_UNKNOWN; 631 break; 632 } 633 return (al); 634 } 635 636 int 637 ssl3_setup_init_buffer(SSL *s) 638 { 639 BUF_MEM *buf = NULL; 640 641 if (s->internal->init_buf != NULL) 642 return (1); 643 644 if ((buf = BUF_MEM_new()) == NULL) 645 goto err; 646 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) 647 goto err; 648 649 s->internal->init_buf = buf; 650 return (1); 651 652 err: 653 BUF_MEM_free(buf); 654 return (0); 655 } 656 657 void 658 ssl3_release_init_buffer(SSL *s) 659 { 660 BUF_MEM_free(s->internal->init_buf); 661 s->internal->init_buf = NULL; 662 s->internal->init_msg = NULL; 663 s->internal->init_num = 0; 664 s->internal->init_off = 0; 665 } 666 667 int 668 ssl3_setup_read_buffer(SSL *s) 669 { 670 unsigned char *p; 671 size_t len, align, headerlen; 672 673 if (SSL_is_dtls(s)) 674 headerlen = DTLS1_RT_HEADER_LENGTH; 675 else 676 headerlen = SSL3_RT_HEADER_LENGTH; 677 678 align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1); 679 680 if (S3I(s)->rbuf.buf == NULL) { 681 len = SSL3_RT_MAX_PLAIN_LENGTH + 682 SSL3_RT_MAX_ENCRYPTED_OVERHEAD + headerlen + align; 683 if ((p = calloc(1, len)) == NULL) 684 goto err; 685 S3I(s)->rbuf.buf = p; 686 S3I(s)->rbuf.len = len; 687 } 688 689 s->internal->packet = S3I(s)->rbuf.buf; 690 return 1; 691 692 err: 693 SSLerror(s, ERR_R_MALLOC_FAILURE); 694 return 0; 695 } 696 697 int 698 ssl3_setup_write_buffer(SSL *s) 699 { 700 unsigned char *p; 701 size_t len, align, headerlen; 702 703 if (SSL_is_dtls(s)) 704 headerlen = DTLS1_RT_HEADER_LENGTH + 1; 705 else 706 headerlen = SSL3_RT_HEADER_LENGTH; 707 708 align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1); 709 710 if (S3I(s)->wbuf.buf == NULL) { 711 len = s->max_send_fragment + 712 SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align; 713 if (!(s->internal->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)) 714 len += headerlen + align + 715 SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD; 716 717 if ((p = calloc(1, len)) == NULL) 718 goto err; 719 S3I(s)->wbuf.buf = p; 720 S3I(s)->wbuf.len = len; 721 } 722 723 return 1; 724 725 err: 726 SSLerror(s, ERR_R_MALLOC_FAILURE); 727 return 0; 728 } 729 730 int 731 ssl3_setup_buffers(SSL *s) 732 { 733 if (!ssl3_setup_read_buffer(s)) 734 return 0; 735 if (!ssl3_setup_write_buffer(s)) 736 return 0; 737 return 1; 738 } 739 740 void 741 ssl3_release_buffer(SSL3_BUFFER_INTERNAL *b) 742 { 743 freezero(b->buf, b->len); 744 b->buf = NULL; 745 b->len = 0; 746 } 747 748 void 749 ssl3_release_read_buffer(SSL *s) 750 { 751 ssl3_release_buffer(&S3I(s)->rbuf); 752 } 753 754 void 755 ssl3_release_write_buffer(SSL *s) 756 { 757 ssl3_release_buffer(&S3I(s)->wbuf); 758 } 759