summaryrefslogtreecommitdiff
path: root/src/lib/libssl/s3_both.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/s3_both.c')
-rw-r--r--src/lib/libssl/s3_both.c721
1 files changed, 0 insertions, 721 deletions
diff --git a/src/lib/libssl/s3_both.c b/src/lib/libssl/s3_both.c
deleted file mode 100644
index 5a1d1e7443..0000000000
--- a/src/lib/libssl/s3_both.c
+++ /dev/null
@@ -1,721 +0,0 @@
1/* $OpenBSD: s3_both.c,v 1.43 2015/07/18 19:41:54 doug 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/* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC) */
131int
132ssl3_do_write(SSL *s, int type)
133{
134 int ret;
135
136 ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off],
137 s->init_num);
138 if (ret < 0)
139 return (-1);
140 if (type == SSL3_RT_HANDSHAKE)
141 /* should not be done for 'Hello Request's, but in that case
142 * we'll ignore the result anyway */
143 ssl3_finish_mac(s,(unsigned char *)&s->init_buf->data[s->init_off], ret);
144
145 if (ret == s->init_num) {
146 if (s->msg_callback)
147 s->msg_callback(1, s->version, type, s->init_buf->data, (size_t)(s->init_off + s->init_num), s, s->msg_callback_arg);
148 return (1);
149 }
150 s->init_off += ret;
151 s->init_num -= ret;
152 return (0);
153}
154
155int
156ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen)
157{
158 unsigned char *p, *d;
159 unsigned long l;
160 int md_len;
161
162 if (s->state == a) {
163 d = (unsigned char *)s->init_buf->data;
164 p = &(d[4]);
165
166 md_len = s->method->ssl3_enc->finish_mac_length;
167 if (s->method->ssl3_enc->final_finish_mac(s, sender, slen,
168 s->s3->tmp.finish_md) != md_len)
169 return (0);
170 s->s3->tmp.finish_md_len = md_len;
171 memcpy(p, s->s3->tmp.finish_md, md_len);
172 p += md_len;
173 l = md_len;
174
175 /* Copy finished so we can use it for renegotiation checks. */
176 OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE);
177 if (s->type == SSL_ST_CONNECT) {
178 memcpy(s->s3->previous_client_finished,
179 s->s3->tmp.finish_md, md_len);
180 s->s3->previous_client_finished_len = md_len;
181 } else {
182 memcpy(s->s3->previous_server_finished,
183 s->s3->tmp.finish_md, md_len);
184 s->s3->previous_server_finished_len = md_len;
185 }
186
187 *(d++) = SSL3_MT_FINISHED;
188 l2n3(l, d);
189 s->init_num = (int)l + 4;
190 s->init_off = 0;
191
192 s->state = b;
193 }
194
195 /* SSL3_ST_SEND_xxxxxx_HELLO_B */
196 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
197}
198
199/* ssl3_take_mac calculates the Finished MAC for the handshakes messages seen to far. */
200static void
201ssl3_take_mac(SSL *s)
202{
203 const char *sender;
204 int slen;
205 /* If no new cipher setup return immediately: other functions will
206 * set the appropriate error.
207 */
208 if (s->s3->tmp.new_cipher == NULL)
209 return;
210 if (s->state & SSL_ST_CONNECT) {
211 sender = s->method->ssl3_enc->server_finished_label;
212 slen = s->method->ssl3_enc->server_finished_label_len;
213 } else {
214 sender = s->method->ssl3_enc->client_finished_label;
215 slen = s->method->ssl3_enc->client_finished_label_len;
216 }
217
218 s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
219 sender, slen, s->s3->tmp.peer_finish_md);
220}
221
222int
223ssl3_get_finished(SSL *s, int a, int b)
224{
225 int al, ok, md_len;
226 long n;
227 CBS cbs;
228
229 n = s->method->ssl_get_message(s, a, b, SSL3_MT_FINISHED,
230 64, /* should actually be 36+4 :-) */ &ok);
231
232 if (!ok)
233 return ((int)n);
234
235 /* If this occurs, we have missed a message */
236 if (!s->s3->change_cipher_spec) {
237 al = SSL_AD_UNEXPECTED_MESSAGE;
238 SSLerr(SSL_F_SSL3_GET_FINISHED, SSL_R_GOT_A_FIN_BEFORE_A_CCS);
239 goto f_err;
240 }
241 s->s3->change_cipher_spec = 0;
242
243 md_len = s->method->ssl3_enc->finish_mac_length;
244
245 if (n < 0) {
246 al = SSL_AD_DECODE_ERROR;
247 SSLerr(SSL_F_SSL3_GET_FINISHED, SSL_R_BAD_DIGEST_LENGTH);
248 goto f_err;
249 }
250
251 CBS_init(&cbs, s->init_msg, n);
252
253 if (s->s3->tmp.peer_finish_md_len != md_len ||
254 CBS_len(&cbs) != md_len) {
255 al = SSL_AD_DECODE_ERROR;
256 SSLerr(SSL_F_SSL3_GET_FINISHED, SSL_R_BAD_DIGEST_LENGTH);
257 goto f_err;
258 }
259
260 if (!CBS_mem_equal(&cbs, s->s3->tmp.peer_finish_md, CBS_len(&cbs))) {
261 al = SSL_AD_DECRYPT_ERROR;
262 SSLerr(SSL_F_SSL3_GET_FINISHED, SSL_R_DIGEST_CHECK_FAILED);
263 goto f_err;
264 }
265
266 /* Copy finished so we can use it for renegotiation checks. */
267 OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE);
268 if (s->type == SSL_ST_ACCEPT) {
269 memcpy(s->s3->previous_client_finished,
270 s->s3->tmp.peer_finish_md, md_len);
271 s->s3->previous_client_finished_len = md_len;
272 } else {
273 memcpy(s->s3->previous_server_finished,
274 s->s3->tmp.peer_finish_md, md_len);
275 s->s3->previous_server_finished_len = md_len;
276 }
277
278 return (1);
279f_err:
280 ssl3_send_alert(s, SSL3_AL_FATAL, al);
281 return (0);
282}
283
284/* for these 2 messages, we need to
285 * ssl->enc_read_ctx re-init
286 * ssl->s3->read_sequence zero
287 * ssl->s3->read_mac_secret re-init
288 * ssl->session->read_sym_enc assign
289 * ssl->session->read_hash assign
290 */
291int
292ssl3_send_change_cipher_spec(SSL *s, int a, int b)
293{
294 unsigned char *p;
295
296 if (s->state == a) {
297 p = (unsigned char *)s->init_buf->data;
298 *p = SSL3_MT_CCS;
299 s->init_num = 1;
300 s->init_off = 0;
301
302 s->state = b;
303 }
304
305 /* SSL3_ST_CW_CHANGE_B */
306 return (ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC));
307}
308
309static int
310ssl3_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
311{
312 int n;
313 unsigned char *p;
314
315 n = i2d_X509(x, NULL);
316 if (!BUF_MEM_grow_clean(buf, n + (*l) + 3)) {
317 SSLerr(SSL_F_SSL3_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);
318 return (-1);
319 }
320 p = (unsigned char *)&(buf->data[*l]);
321 l2n3(n, p);
322 i2d_X509(x, &p);
323 *l += n + 3;
324
325 return (0);
326}
327
328unsigned long
329ssl3_output_cert_chain(SSL *s, X509 *x)
330{
331 unsigned char *p;
332 int i;
333 unsigned long l = 7;
334 BUF_MEM *buf;
335 int no_chain;
336
337 if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || s->ctx->extra_certs)
338 no_chain = 1;
339 else
340 no_chain = 0;
341
342 /* TLSv1 sends a chain with nothing in it, instead of an alert */
343 buf = s->init_buf;
344 if (!BUF_MEM_grow_clean(buf, 10)) {
345 SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_BUF_LIB);
346 return (0);
347 }
348 if (x != NULL) {
349 if (no_chain) {
350 if (ssl3_add_cert_to_buf(buf, &l, x))
351 return (0);
352 } else {
353 X509_STORE_CTX xs_ctx;
354
355 if (!X509_STORE_CTX_init(&xs_ctx, s->ctx->cert_store, x, NULL)) {
356 SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_X509_LIB);
357 return (0);
358 }
359 X509_verify_cert(&xs_ctx);
360 /* Don't leave errors in the queue */
361 ERR_clear_error();
362 for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
363 x = sk_X509_value(xs_ctx.chain, i);
364
365 if (ssl3_add_cert_to_buf(buf, &l, x)) {
366 X509_STORE_CTX_cleanup(&xs_ctx);
367 return 0;
368 }
369 }
370 X509_STORE_CTX_cleanup(&xs_ctx);
371 }
372 }
373 /* Thawte special :-) */
374 for (i = 0; i < sk_X509_num(s->ctx->extra_certs); i++) {
375 x = sk_X509_value(s->ctx->extra_certs, i);
376 if (ssl3_add_cert_to_buf(buf, &l, x))
377 return (0);
378 }
379
380 l -= 7;
381 p = (unsigned char *)&(buf->data[4]);
382 l2n3(l, p);
383 l += 3;
384 p = (unsigned char *)&(buf->data[0]);
385 *(p++) = SSL3_MT_CERTIFICATE;
386 l2n3(l, p);
387 l += 4;
388 return (l);
389}
390
391/* Obtain handshake message of message type 'mt' (any if mt == -1),
392 * maximum acceptable body length 'max'.
393 * The first four bytes (msg_type and length) are read in state 'st1',
394 * the body is read in state 'stn'.
395 */
396long
397ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
398{
399 unsigned char *p;
400 uint32_t l;
401 long n;
402 int i, al;
403 CBS cbs;
404 uint8_t u8;
405
406 if (s->s3->tmp.reuse_message) {
407 s->s3->tmp.reuse_message = 0;
408 if ((mt >= 0) && (s->s3->tmp.message_type != mt)) {
409 al = SSL_AD_UNEXPECTED_MESSAGE;
410 SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
411 goto f_err;
412 }
413 *ok = 1;
414 s->init_msg = s->init_buf->data + 4;
415 s->init_num = (int)s->s3->tmp.message_size;
416 return s->init_num;
417 }
418
419 p = (unsigned char *)s->init_buf->data;
420
421 if (s->state == st1) /* s->init_num < 4 */
422 {
423 int skip_message;
424
425 do {
426 while (s->init_num < 4) {
427 i = s->method->ssl_read_bytes(s,
428 SSL3_RT_HANDSHAKE, &p[s->init_num],
429 4 - s->init_num, 0);
430 if (i <= 0) {
431 s->rwstate = SSL_READING;
432 *ok = 0;
433 return i;
434 }
435 s->init_num += i;
436 }
437
438 skip_message = 0;
439 if (!s->server && p[0] == SSL3_MT_HELLO_REQUEST) {
440 /*
441 * The server may always send 'Hello Request'
442 * messages -- we are doing a handshake anyway
443 * now, so ignore them if their format is
444 * correct. Does not count for 'Finished' MAC.
445 */
446 if (p[1] == 0 && p[2] == 0 &&p[3] == 0) {
447 s->init_num = 0;
448 skip_message = 1;
449
450 if (s->msg_callback)
451 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, 4, s, s->msg_callback_arg);
452 }
453 }
454
455 } while (skip_message);
456
457 /* s->init_num == 4 */
458
459 if ((mt >= 0) && (*p != mt)) {
460 al = SSL_AD_UNEXPECTED_MESSAGE;
461 SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
462 goto f_err;
463 }
464
465 /* XXX remove call to n2l3 */
466 CBS_init(&cbs, p, 4);
467 if (!CBS_get_u8(&cbs, &u8) ||
468 !CBS_get_u24(&cbs, &l)) {
469 SSLerr(SSL_F_SSL3_GET_MESSAGE, ERR_R_BUF_LIB);
470 goto err;
471 }
472 s->s3->tmp.message_type = u8;
473
474 if (l > (unsigned long)max) {
475 al = SSL_AD_ILLEGAL_PARAMETER;
476 SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
477 goto f_err;
478 }
479 if (l && !BUF_MEM_grow_clean(s->init_buf, l + 4)) {
480 SSLerr(SSL_F_SSL3_GET_MESSAGE, ERR_R_BUF_LIB);
481 goto err;
482 }
483 s->s3->tmp.message_size = l;
484 s->state = stn;
485
486 s->init_msg = s->init_buf->data + 4;
487 s->init_num = 0;
488 }
489
490 /* next state (stn) */
491 p = s->init_msg;
492 n = s->s3->tmp.message_size - s->init_num;
493 while (n > 0) {
494 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE,
495 &p[s->init_num], n, 0);
496 if (i <= 0) {
497 s->rwstate = SSL_READING;
498 *ok = 0;
499 return i;
500 }
501 s->init_num += i;
502 n -= i;
503 }
504
505 /* If receiving Finished, record MAC of prior handshake messages for
506 * Finished verification. */
507 if (*s->init_buf->data == SSL3_MT_FINISHED)
508 ssl3_take_mac(s);
509
510 /* Feed this message into MAC computation. */
511 ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, s->init_num + 4);
512 if (s->msg_callback)
513 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data, (size_t)s->init_num + 4, s, s->msg_callback_arg);
514 *ok = 1;
515 return s->init_num;
516f_err:
517 ssl3_send_alert(s, SSL3_AL_FATAL, al);
518err:
519 *ok = 0;
520 return (-1);
521}
522
523int
524ssl_cert_type(X509 *x, EVP_PKEY *pkey)
525{
526 EVP_PKEY *pk;
527 int ret = -1, i;
528
529 if (pkey == NULL)
530 pk = X509_get_pubkey(x);
531 else
532 pk = pkey;
533 if (pk == NULL)
534 goto err;
535
536 i = pk->type;
537 if (i == EVP_PKEY_RSA) {
538 ret = SSL_PKEY_RSA_ENC;
539 } else if (i == EVP_PKEY_DSA) {
540 ret = SSL_PKEY_DSA_SIGN;
541 }
542 else if (i == EVP_PKEY_EC) {
543 ret = SSL_PKEY_ECC;
544 } else if (i == NID_id_GostR3410_2001 || i == NID_id_GostR3410_2001_cc) {
545 ret = SSL_PKEY_GOST01;
546 }
547err:
548 if (!pkey)
549 EVP_PKEY_free(pk);
550 return (ret);
551}
552
553int
554ssl_verify_alarm_type(long type)
555{
556 int al;
557
558 switch (type) {
559 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
560 case X509_V_ERR_UNABLE_TO_GET_CRL:
561 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
562 al = SSL_AD_UNKNOWN_CA;
563 break;
564 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
565 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
566 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
567 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
568 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
569 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
570 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
571 case X509_V_ERR_CERT_NOT_YET_VALID:
572 case X509_V_ERR_CRL_NOT_YET_VALID:
573 case X509_V_ERR_CERT_UNTRUSTED:
574 case X509_V_ERR_CERT_REJECTED:
575 al = SSL_AD_BAD_CERTIFICATE;
576 break;
577 case X509_V_ERR_CERT_SIGNATURE_FAILURE:
578 case X509_V_ERR_CRL_SIGNATURE_FAILURE:
579 al = SSL_AD_DECRYPT_ERROR;
580 break;
581 case X509_V_ERR_CERT_HAS_EXPIRED:
582 case X509_V_ERR_CRL_HAS_EXPIRED:
583 al = SSL_AD_CERTIFICATE_EXPIRED;
584 break;
585 case X509_V_ERR_CERT_REVOKED:
586 al = SSL_AD_CERTIFICATE_REVOKED;
587 break;
588 case X509_V_ERR_OUT_OF_MEM:
589 al = SSL_AD_INTERNAL_ERROR;
590 break;
591 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
592 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
593 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
594 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
595 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
596 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
597 case X509_V_ERR_INVALID_CA:
598 al = SSL_AD_UNKNOWN_CA;
599 break;
600 case X509_V_ERR_APPLICATION_VERIFICATION:
601 al = SSL_AD_HANDSHAKE_FAILURE;
602 break;
603 case X509_V_ERR_INVALID_PURPOSE:
604 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
605 break;
606 default:
607 al = SSL_AD_CERTIFICATE_UNKNOWN;
608 break;
609 }
610 return (al);
611}
612
613int
614ssl3_setup_init_buffer(SSL *s)
615{
616 BUF_MEM *buf = NULL;
617
618 if (s->init_buf != NULL)
619 return (1);
620
621 if ((buf = BUF_MEM_new()) == NULL)
622 goto err;
623 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH))
624 goto err;
625
626 s->init_buf = buf;
627 return (1);
628
629err:
630 BUF_MEM_free(buf);
631 return (0);
632}
633
634int
635ssl3_setup_read_buffer(SSL *s)
636{
637 unsigned char *p;
638 size_t len, align, headerlen;
639
640 if (SSL_IS_DTLS(s))
641 headerlen = DTLS1_RT_HEADER_LENGTH;
642 else
643 headerlen = SSL3_RT_HEADER_LENGTH;
644
645 align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
646
647 if (s->s3->rbuf.buf == NULL) {
648 len = SSL3_RT_MAX_PLAIN_LENGTH +
649 SSL3_RT_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
650 if ((p = malloc(len)) == NULL)
651 goto err;
652 s->s3->rbuf.buf = p;
653 s->s3->rbuf.len = len;
654 }
655
656 s->packet = &(s->s3->rbuf.buf[0]);
657 return 1;
658
659err:
660 SSLerr(SSL_F_SSL3_SETUP_READ_BUFFER, ERR_R_MALLOC_FAILURE);
661 return 0;
662}
663
664int
665ssl3_setup_write_buffer(SSL *s)
666{
667 unsigned char *p;
668 size_t len, align, headerlen;
669
670 if (SSL_IS_DTLS(s))
671 headerlen = DTLS1_RT_HEADER_LENGTH + 1;
672 else
673 headerlen = SSL3_RT_HEADER_LENGTH;
674
675 align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
676
677 if (s->s3->wbuf.buf == NULL) {
678 len = s->max_send_fragment +
679 SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
680 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
681 len += headerlen + align +
682 SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
683
684 if ((p = malloc(len)) == NULL)
685 goto err;
686 s->s3->wbuf.buf = p;
687 s->s3->wbuf.len = len;
688 }
689
690 return 1;
691
692err:
693 SSLerr(SSL_F_SSL3_SETUP_WRITE_BUFFER, ERR_R_MALLOC_FAILURE);
694 return 0;
695}
696
697int
698ssl3_setup_buffers(SSL *s)
699{
700 if (!ssl3_setup_read_buffer(s))
701 return 0;
702 if (!ssl3_setup_write_buffer(s))
703 return 0;
704 return 1;
705}
706
707int
708ssl3_release_write_buffer(SSL *s)
709{
710 free(s->s3->wbuf.buf);
711 s->s3->wbuf.buf = NULL;
712 return 1;
713}
714
715int
716ssl3_release_read_buffer(SSL *s)
717{
718 free(s->s3->rbuf.buf);
719 s->s3->rbuf.buf = NULL;
720 return 1;
721}