summaryrefslogtreecommitdiff
path: root/src/lib/libssl/t1_enc.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libssl/t1_enc.c1244
1 files changed, 0 insertions, 1244 deletions
diff --git a/src/lib/libssl/t1_enc.c b/src/lib/libssl/t1_enc.c
deleted file mode 100644
index 5cd1688a37..0000000000
--- a/src/lib/libssl/t1_enc.c
+++ /dev/null
@@ -1,1244 +0,0 @@
1/* $OpenBSD: t1_enc.c,v 1.79 2015/07/17 07:04:41 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-2007 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 2005 Nokia. All rights reserved.
113 *
114 * The portions of the attached software ("Contribution") is developed by
115 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
116 * license.
117 *
118 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
119 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
120 * support (see RFC 4279) to OpenSSL.
121 *
122 * No patent licenses or other rights except those expressly stated in
123 * the OpenSSL open source license shall be deemed granted or received
124 * expressly, by implication, estoppel, or otherwise.
125 *
126 * No assurances are provided by Nokia that the Contribution does not
127 * infringe the patent or other intellectual property rights of any third
128 * party or that the license provides you with all the necessary rights
129 * to make use of the Contribution.
130 *
131 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
132 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
133 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
134 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
135 * OTHERWISE.
136 */
137
138#include <stdio.h>
139
140#include "ssl_locl.h"
141
142#include <openssl/evp.h>
143#include <openssl/hmac.h>
144#include <openssl/md5.h>
145
146/* seed1 through seed5 are virtually concatenated */
147static int
148tls1_P_hash(const EVP_MD *md, const unsigned char *sec, int sec_len,
149 const void *seed1, int seed1_len, const void *seed2, int seed2_len,
150 const void *seed3, int seed3_len, const void *seed4, int seed4_len,
151 const void *seed5, int seed5_len, unsigned char *out, int olen)
152{
153 int chunk;
154 size_t j;
155 EVP_MD_CTX ctx, ctx_tmp;
156 EVP_PKEY *mac_key;
157 unsigned char A1[EVP_MAX_MD_SIZE];
158 size_t A1_len;
159 int ret = 0;
160
161 chunk = EVP_MD_size(md);
162 OPENSSL_assert(chunk >= 0);
163
164 EVP_MD_CTX_init(&ctx);
165 EVP_MD_CTX_init(&ctx_tmp);
166 mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, sec, sec_len);
167 if (!mac_key)
168 goto err;
169 if (!EVP_DigestSignInit(&ctx, NULL, md, NULL, mac_key))
170 goto err;
171 if (!EVP_DigestSignInit(&ctx_tmp, NULL, md, NULL, mac_key))
172 goto err;
173 if (seed1 && !EVP_DigestSignUpdate(&ctx, seed1, seed1_len))
174 goto err;
175 if (seed2 && !EVP_DigestSignUpdate(&ctx, seed2, seed2_len))
176 goto err;
177 if (seed3 && !EVP_DigestSignUpdate(&ctx, seed3, seed3_len))
178 goto err;
179 if (seed4 && !EVP_DigestSignUpdate(&ctx, seed4, seed4_len))
180 goto err;
181 if (seed5 && !EVP_DigestSignUpdate(&ctx, seed5, seed5_len))
182 goto err;
183 if (!EVP_DigestSignFinal(&ctx, A1, &A1_len))
184 goto err;
185
186 for (;;) {
187 /* Reinit mac contexts */
188 if (!EVP_DigestSignInit(&ctx, NULL, md, NULL, mac_key))
189 goto err;
190 if (!EVP_DigestSignInit(&ctx_tmp, NULL, md, NULL, mac_key))
191 goto err;
192 if (!EVP_DigestSignUpdate(&ctx, A1, A1_len))
193 goto err;
194 if (!EVP_DigestSignUpdate(&ctx_tmp, A1, A1_len))
195 goto err;
196 if (seed1 && !EVP_DigestSignUpdate(&ctx, seed1, seed1_len))
197 goto err;
198 if (seed2 && !EVP_DigestSignUpdate(&ctx, seed2, seed2_len))
199 goto err;
200 if (seed3 && !EVP_DigestSignUpdate(&ctx, seed3, seed3_len))
201 goto err;
202 if (seed4 && !EVP_DigestSignUpdate(&ctx, seed4, seed4_len))
203 goto err;
204 if (seed5 && !EVP_DigestSignUpdate(&ctx, seed5, seed5_len))
205 goto err;
206
207 if (olen > chunk) {
208 if (!EVP_DigestSignFinal(&ctx, out, &j))
209 goto err;
210 out += j;
211 olen -= j;
212 /* calc the next A1 value */
213 if (!EVP_DigestSignFinal(&ctx_tmp, A1, &A1_len))
214 goto err;
215 } else {
216 /* last one */
217 if (!EVP_DigestSignFinal(&ctx, A1, &A1_len))
218 goto err;
219 memcpy(out, A1, olen);
220 break;
221 }
222 }
223 ret = 1;
224
225err:
226 EVP_PKEY_free(mac_key);
227 EVP_MD_CTX_cleanup(&ctx);
228 EVP_MD_CTX_cleanup(&ctx_tmp);
229 OPENSSL_cleanse(A1, sizeof(A1));
230 return ret;
231}
232
233/* seed1 through seed5 are virtually concatenated */
234static int
235tls1_PRF(long digest_mask, const void *seed1, int seed1_len, const void *seed2,
236 int seed2_len, const void *seed3, int seed3_len, const void *seed4,
237 int seed4_len, const void *seed5, int seed5_len, const unsigned char *sec,
238 int slen, unsigned char *out1, unsigned char *out2, int olen)
239{
240 int len, i, idx, count;
241 const unsigned char *S1;
242 long m;
243 const EVP_MD *md;
244 int ret = 0;
245
246 /* Count number of digests and partition sec evenly */
247 count = 0;
248 for (idx = 0; ssl_get_handshake_digest(idx, &m, &md); idx++) {
249 if ((m << TLS1_PRF_DGST_SHIFT) & digest_mask)
250 count++;
251 }
252 if (count == 0) {
253 SSLerr(SSL_F_TLS1_PRF,
254 SSL_R_SSL_HANDSHAKE_FAILURE);
255 goto err;
256 }
257 len = slen / count;
258 if (count == 1)
259 slen = 0;
260 S1 = sec;
261 memset(out1, 0, olen);
262 for (idx = 0; ssl_get_handshake_digest(idx, &m, &md); idx++) {
263 if ((m << TLS1_PRF_DGST_SHIFT) & digest_mask) {
264 if (!md) {
265 SSLerr(SSL_F_TLS1_PRF,
266 SSL_R_UNSUPPORTED_DIGEST_TYPE);
267 goto err;
268 }
269 if (!tls1_P_hash(md , S1, len + (slen&1), seed1,
270 seed1_len, seed2, seed2_len, seed3, seed3_len,
271 seed4, seed4_len, seed5, seed5_len, out2, olen))
272 goto err;
273 S1 += len;
274 for (i = 0; i < olen; i++) {
275 out1[i] ^= out2[i];
276 }
277 }
278 }
279 ret = 1;
280
281err:
282 return ret;
283}
284
285static int
286tls1_generate_key_block(SSL *s, unsigned char *km, unsigned char *tmp, int num)
287{
288 int ret;
289
290 ret = tls1_PRF(ssl_get_algorithm2(s),
291 TLS_MD_KEY_EXPANSION_CONST, TLS_MD_KEY_EXPANSION_CONST_SIZE,
292 s->s3->server_random, SSL3_RANDOM_SIZE,
293 s->s3->client_random, SSL3_RANDOM_SIZE,
294 NULL, 0, NULL, 0,
295 s->session->master_key, s->session->master_key_length,
296 km, tmp, num);
297 return ret;
298}
299
300/*
301 * tls1_aead_ctx_init allocates aead_ctx, if needed. It returns 1 on success
302 * and 0 on failure.
303 */
304static int
305tls1_aead_ctx_init(SSL_AEAD_CTX **aead_ctx)
306{
307 if (*aead_ctx != NULL) {
308 EVP_AEAD_CTX_cleanup(&(*aead_ctx)->ctx);
309 return (1);
310 }
311
312 *aead_ctx = malloc(sizeof(SSL_AEAD_CTX));
313 if (*aead_ctx == NULL) {
314 SSLerr(SSL_F_TLS1_AEAD_CTX_INIT, ERR_R_MALLOC_FAILURE);
315 return (0);
316 }
317
318 return (1);
319}
320
321static int
322tls1_change_cipher_state_aead(SSL *s, char is_read, const unsigned char *key,
323 unsigned key_len, const unsigned char *iv, unsigned iv_len)
324{
325 const EVP_AEAD *aead = s->s3->tmp.new_aead;
326 SSL_AEAD_CTX *aead_ctx;
327
328 if (is_read) {
329 if (!tls1_aead_ctx_init(&s->aead_read_ctx))
330 return 0;
331 aead_ctx = s->aead_read_ctx;
332 } else {
333 if (!tls1_aead_ctx_init(&s->aead_write_ctx))
334 return 0;
335 aead_ctx = s->aead_write_ctx;
336 }
337
338 if (!EVP_AEAD_CTX_init(&aead_ctx->ctx, aead, key, key_len,
339 EVP_AEAD_DEFAULT_TAG_LENGTH, NULL))
340 return (0);
341 if (iv_len > sizeof(aead_ctx->fixed_nonce)) {
342 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE_AEAD,
343 ERR_R_INTERNAL_ERROR);
344 return (0);
345 }
346 memcpy(aead_ctx->fixed_nonce, iv, iv_len);
347 aead_ctx->fixed_nonce_len = iv_len;
348 aead_ctx->variable_nonce_len = 8; /* always the case, currently. */
349 aead_ctx->variable_nonce_in_record =
350 (s->s3->tmp.new_cipher->algorithm2 &
351 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD) != 0;
352 if (aead_ctx->variable_nonce_len + aead_ctx->fixed_nonce_len !=
353 EVP_AEAD_nonce_length(aead)) {
354 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE_AEAD,
355 ERR_R_INTERNAL_ERROR);
356 return (0);
357 }
358 aead_ctx->tag_len = EVP_AEAD_max_overhead(aead);
359
360 return (1);
361}
362
363/*
364 * tls1_change_cipher_state_cipher performs the work needed to switch cipher
365 * states when using EVP_CIPHER. The argument is_read is true iff this function
366 * is being called due to reading, as opposed to writing, a ChangeCipherSpec
367 * message. In order to support export ciphersuites, use_client_keys indicates
368 * whether the key material provided is in the "client write" direction.
369 */
370static int
371tls1_change_cipher_state_cipher(SSL *s, char is_read, char use_client_keys,
372 const unsigned char *mac_secret, unsigned int mac_secret_size,
373 const unsigned char *key, unsigned int key_len, const unsigned char *iv,
374 unsigned int iv_len)
375{
376 EVP_CIPHER_CTX *cipher_ctx;
377 const EVP_CIPHER *cipher;
378 EVP_MD_CTX *mac_ctx;
379 const EVP_MD *mac;
380 int mac_type;
381
382 cipher = s->s3->tmp.new_sym_enc;
383 mac = s->s3->tmp.new_hash;
384 mac_type = s->s3->tmp.new_mac_pkey_type;
385
386 if (is_read) {
387 if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
388 s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
389 else
390 s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
391
392 EVP_CIPHER_CTX_free(s->enc_read_ctx);
393 s->enc_read_ctx = NULL;
394 EVP_MD_CTX_destroy(s->read_hash);
395 s->read_hash = NULL;
396
397 if ((cipher_ctx = EVP_CIPHER_CTX_new()) == NULL)
398 goto err;
399 s->enc_read_ctx = cipher_ctx;
400 if ((mac_ctx = EVP_MD_CTX_create()) == NULL)
401 goto err;
402 s->read_hash = mac_ctx;
403 } else {
404 if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
405 s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
406 else
407 s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
408
409 /*
410 * DTLS fragments retain a pointer to the compression, cipher
411 * and hash contexts, so that it can restore state in order
412 * to perform retransmissions. As such, we cannot free write
413 * contexts that are used for DTLS - these are instead freed
414 * by DTLS when its frees a ChangeCipherSpec fragment.
415 */
416 if (!SSL_IS_DTLS(s)) {
417 EVP_CIPHER_CTX_free(s->enc_write_ctx);
418 s->enc_write_ctx = NULL;
419 EVP_MD_CTX_destroy(s->write_hash);
420 s->write_hash = NULL;
421 }
422 if ((cipher_ctx = EVP_CIPHER_CTX_new()) == NULL)
423 goto err;
424 s->enc_write_ctx = cipher_ctx;
425 if ((mac_ctx = EVP_MD_CTX_create()) == NULL)
426 goto err;
427 s->write_hash = mac_ctx;
428 }
429
430 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE) {
431 EVP_CipherInit_ex(cipher_ctx, cipher, NULL, key, NULL,
432 !is_read);
433 EVP_CIPHER_CTX_ctrl(cipher_ctx, EVP_CTRL_GCM_SET_IV_FIXED,
434 iv_len, (unsigned char *)iv);
435 } else
436 EVP_CipherInit_ex(cipher_ctx, cipher, NULL, key, iv, !is_read);
437
438 if (!(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
439 EVP_PKEY *mac_key = EVP_PKEY_new_mac_key(mac_type, NULL,
440 mac_secret, mac_secret_size);
441 if (mac_key == NULL)
442 goto err;
443 EVP_DigestSignInit(mac_ctx, NULL, mac, NULL, mac_key);
444 EVP_PKEY_free(mac_key);
445 } else if (mac_secret_size > 0) {
446 /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
447 EVP_CIPHER_CTX_ctrl(cipher_ctx, EVP_CTRL_AEAD_SET_MAC_KEY,
448 mac_secret_size, (unsigned char *)mac_secret);
449 }
450
451 if (s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT) {
452 int nid;
453 if (s->s3->tmp.new_cipher->algorithm2 & SSL_HANDSHAKE_MAC_GOST94)
454 nid = NID_id_Gost28147_89_CryptoPro_A_ParamSet;
455 else
456 nid = NID_id_tc26_gost_28147_param_Z;
457
458 EVP_CIPHER_CTX_ctrl(cipher_ctx, EVP_CTRL_GOST_SET_SBOX, nid, 0);
459 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_GOST89MAC)
460 EVP_MD_CTX_ctrl(mac_ctx, EVP_MD_CTRL_GOST_SET_SBOX, nid, 0);
461 }
462
463 return (1);
464
465err:
466 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE_CIPHER, ERR_R_MALLOC_FAILURE);
467 return (0);
468}
469
470int
471tls1_change_cipher_state(SSL *s, int which)
472{
473 const unsigned char *client_write_mac_secret, *server_write_mac_secret;
474 const unsigned char *client_write_key, *server_write_key;
475 const unsigned char *client_write_iv, *server_write_iv;
476 const unsigned char *mac_secret, *key, *iv;
477 int mac_secret_size, key_len, iv_len;
478 unsigned char *key_block, *seq;
479 const EVP_CIPHER *cipher;
480 const EVP_AEAD *aead;
481 char is_read, use_client_keys;
482
483
484 cipher = s->s3->tmp.new_sym_enc;
485 aead = s->s3->tmp.new_aead;
486
487 /*
488 * is_read is true if we have just read a ChangeCipherSpec message,
489 * that is we need to update the read cipherspec. Otherwise we have
490 * just written one.
491 */
492 is_read = (which & SSL3_CC_READ) != 0;
493
494 /*
495 * use_client_keys is true if we wish to use the keys for the "client
496 * write" direction. This is the case if we're a client sending a
497 * ChangeCipherSpec, or a server reading a client's ChangeCipherSpec.
498 */
499 use_client_keys = ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
500 (which == SSL3_CHANGE_CIPHER_SERVER_READ));
501
502
503 /*
504 * Reset sequence number to zero - for DTLS this is handled in
505 * dtls1_reset_seq_numbers().
506 */
507 if (!SSL_IS_DTLS(s)) {
508 seq = is_read ? s->s3->read_sequence : s->s3->write_sequence;
509 memset(seq, 0, SSL3_SEQUENCE_SIZE);
510 }
511
512 if (aead != NULL) {
513 key_len = EVP_AEAD_key_length(aead);
514 iv_len = SSL_CIPHER_AEAD_FIXED_NONCE_LEN(s->s3->tmp.new_cipher);
515 } else {
516 key_len = EVP_CIPHER_key_length(cipher);
517 iv_len = EVP_CIPHER_iv_length(cipher);
518
519 /* If GCM mode only part of IV comes from PRF. */
520 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE)
521 iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
522 }
523
524 mac_secret_size = s->s3->tmp.new_mac_secret_size;
525
526 key_block = s->s3->tmp.key_block;
527 client_write_mac_secret = key_block;
528 key_block += mac_secret_size;
529 server_write_mac_secret = key_block;
530 key_block += mac_secret_size;
531 client_write_key = key_block;
532 key_block += key_len;
533 server_write_key = key_block;
534 key_block += key_len;
535 client_write_iv = key_block;
536 key_block += iv_len;
537 server_write_iv = key_block;
538 key_block += iv_len;
539
540 if (use_client_keys) {
541 mac_secret = client_write_mac_secret;
542 key = client_write_key;
543 iv = client_write_iv;
544 } else {
545 mac_secret = server_write_mac_secret;
546 key = server_write_key;
547 iv = server_write_iv;
548 }
549
550 if (key_block - s->s3->tmp.key_block != s->s3->tmp.key_block_length) {
551 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
552 goto err2;
553 }
554
555 if (is_read) {
556 memcpy(s->s3->read_mac_secret, mac_secret, mac_secret_size);
557 s->s3->read_mac_secret_size = mac_secret_size;
558 } else {
559 memcpy(s->s3->write_mac_secret, mac_secret, mac_secret_size);
560 s->s3->write_mac_secret_size = mac_secret_size;
561 }
562
563 if (aead != NULL) {
564 return tls1_change_cipher_state_aead(s, is_read, key, key_len,
565 iv, iv_len);
566 }
567
568 return tls1_change_cipher_state_cipher(s, is_read, use_client_keys,
569 mac_secret, mac_secret_size, key, key_len, iv, iv_len);
570
571err2:
572 return (0);
573}
574
575int
576tls1_setup_key_block(SSL *s)
577{
578 unsigned char *key_block, *tmp_block = NULL;
579 int mac_type = NID_undef, mac_secret_size = 0;
580 int key_block_len, key_len, iv_len;
581 const EVP_CIPHER *cipher = NULL;
582 const EVP_AEAD *aead = NULL;
583 const EVP_MD *mac = NULL;
584 int ret = 0;
585
586 if (s->s3->tmp.key_block_length != 0)
587 return (1);
588
589 if (s->session->cipher &&
590 (s->session->cipher->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD)) {
591 if (!ssl_cipher_get_evp_aead(s->session, &aead)) {
592 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,
593 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
594 return (0);
595 }
596 key_len = EVP_AEAD_key_length(aead);
597 iv_len = SSL_CIPHER_AEAD_FIXED_NONCE_LEN(s->session->cipher);
598 } else {
599 if (!ssl_cipher_get_evp(s->session, &cipher, &mac, &mac_type,
600 &mac_secret_size)) {
601 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,
602 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
603 return (0);
604 }
605 key_len = EVP_CIPHER_key_length(cipher);
606 iv_len = EVP_CIPHER_iv_length(cipher);
607
608 /* If GCM mode only part of IV comes from PRF. */
609 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE)
610 iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
611 }
612
613 s->s3->tmp.new_aead = aead;
614 s->s3->tmp.new_sym_enc = cipher;
615 s->s3->tmp.new_hash = mac;
616 s->s3->tmp.new_mac_pkey_type = mac_type;
617 s->s3->tmp.new_mac_secret_size = mac_secret_size;
618
619 ssl3_cleanup_key_block(s);
620
621 if ((key_block = reallocarray(NULL, mac_secret_size + key_len + iv_len,
622 2)) == NULL) {
623 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE);
624 goto err;
625 }
626 key_block_len = (mac_secret_size + key_len + iv_len) * 2;
627
628 s->s3->tmp.key_block_length = key_block_len;
629 s->s3->tmp.key_block = key_block;
630
631 if ((tmp_block = malloc(key_block_len)) == NULL) {
632 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE);
633 goto err;
634 }
635
636 if (!tls1_generate_key_block(s, key_block, tmp_block, key_block_len))
637 goto err;
638
639 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) &&
640 s->method->version <= TLS1_VERSION) {
641 /*
642 * Enable vulnerability countermeasure for CBC ciphers with
643 * known-IV problem (http://www.openssl.org/~bodo/tls-cbc.txt)
644 */
645 s->s3->need_empty_fragments = 1;
646
647 if (s->session->cipher != NULL) {
648 if (s->session->cipher->algorithm_enc == SSL_eNULL)
649 s->s3->need_empty_fragments = 0;
650
651#ifndef OPENSSL_NO_RC4
652 if (s->session->cipher->algorithm_enc == SSL_RC4)
653 s->s3->need_empty_fragments = 0;
654#endif
655 }
656 }
657
658 ret = 1;
659
660err:
661 if (tmp_block) {
662 OPENSSL_cleanse(tmp_block, key_block_len);
663 free(tmp_block);
664 }
665 return (ret);
666}
667
668/* tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively.
669 *
670 * Returns:
671 * 0: (in non-constant time) if the record is publically invalid (i.e. too
672 * short etc).
673 * 1: if the record's padding is valid / the encryption was successful.
674 * -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
675 * an internal error occured.
676 */
677int
678tls1_enc(SSL *s, int send)
679{
680 const SSL_AEAD_CTX *aead;
681 const EVP_CIPHER *enc;
682 EVP_CIPHER_CTX *ds;
683 SSL3_RECORD *rec;
684 unsigned char *seq;
685 unsigned long l;
686 int bs, i, j, k, pad = 0, ret, mac_size = 0;
687
688 if (send) {
689 aead = s->aead_write_ctx;
690 rec = &s->s3->wrec;
691 seq = s->s3->write_sequence;
692 } else {
693 aead = s->aead_read_ctx;
694 rec = &s->s3->rrec;
695 seq = s->s3->read_sequence;
696 }
697
698 if (aead) {
699 unsigned char ad[13], *in, *out, nonce[16];
700 unsigned nonce_used;
701 ssize_t n;
702
703 if (SSL_IS_DTLS(s)) {
704 dtls1_build_sequence_number(ad, seq,
705 send ? s->d1->w_epoch : s->d1->r_epoch);
706 } else {
707 memcpy(ad, seq, SSL3_SEQUENCE_SIZE);
708 ssl3_record_sequence_increment(seq);
709 }
710
711 ad[8] = rec->type;
712 ad[9] = (unsigned char)(s->version >> 8);
713 ad[10] = (unsigned char)(s->version);
714
715 if (aead->fixed_nonce_len +
716 aead->variable_nonce_len > sizeof(nonce) ||
717 aead->variable_nonce_len > 8)
718 return -1; /* internal error - should never happen. */
719
720 memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
721 nonce_used = aead->fixed_nonce_len;
722
723 if (send) {
724 size_t len = rec->length;
725 size_t eivlen = 0;
726 in = rec->input;
727 out = rec->data;
728
729 /*
730 * When sending we use the sequence number as the
731 * variable part of the nonce.
732 */
733 if (aead->variable_nonce_len > 8)
734 return -1;
735 memcpy(nonce + nonce_used, ad,
736 aead->variable_nonce_len);
737 nonce_used += aead->variable_nonce_len;
738
739 /*
740 * In do_ssl3_write, rec->input is moved forward by
741 * variable_nonce_len in order to leave space for the
742 * variable nonce. Thus we can copy the sequence number
743 * bytes into place without overwriting any of the
744 * plaintext.
745 */
746 if (aead->variable_nonce_in_record) {
747 memcpy(out, ad, aead->variable_nonce_len);
748 len -= aead->variable_nonce_len;
749 eivlen = aead->variable_nonce_len;
750 }
751
752 ad[11] = len >> 8;
753 ad[12] = len & 0xff;
754
755 if (!EVP_AEAD_CTX_seal(&aead->ctx,
756 out + eivlen, &n, len + aead->tag_len, nonce,
757 nonce_used, in + eivlen, len, ad, sizeof(ad)))
758 return -1;
759 if (n >= 0 && aead->variable_nonce_in_record)
760 n += aead->variable_nonce_len;
761 } else {
762 /* receive */
763 size_t len = rec->length;
764
765 if (rec->data != rec->input)
766 return -1; /* internal error - should never happen. */
767 out = in = rec->input;
768
769 if (len < aead->variable_nonce_len)
770 return 0;
771 memcpy(nonce + nonce_used,
772 aead->variable_nonce_in_record ? in : ad,
773 aead->variable_nonce_len);
774 nonce_used += aead->variable_nonce_len;
775
776 if (aead->variable_nonce_in_record) {
777 in += aead->variable_nonce_len;
778 len -= aead->variable_nonce_len;
779 out += aead->variable_nonce_len;
780 }
781
782 if (len < aead->tag_len)
783 return 0;
784 len -= aead->tag_len;
785
786 ad[11] = len >> 8;
787 ad[12] = len & 0xff;
788
789 if (!EVP_AEAD_CTX_open(&aead->ctx, out, &n, len, nonce,
790 nonce_used, in, len + aead->tag_len, ad,
791 sizeof(ad)))
792 return -1;
793
794 rec->data = rec->input = out;
795 }
796
797 if (n == -1)
798 return -1;
799 rec->length = n;
800
801 return 1;
802 }
803
804 if (send) {
805 if (EVP_MD_CTX_md(s->write_hash)) {
806 int n = EVP_MD_CTX_size(s->write_hash);
807 OPENSSL_assert(n >= 0);
808 }
809 ds = s->enc_write_ctx;
810 if (s->enc_write_ctx == NULL)
811 enc = NULL;
812 else {
813 int ivlen = 0;
814 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
815 if (SSL_USE_EXPLICIT_IV(s) &&
816 EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
817 ivlen = EVP_CIPHER_iv_length(enc);
818 if (ivlen > 1) {
819 if (rec->data != rec->input)
820 /* we can't write into the input stream:
821 * Can this ever happen?? (steve)
822 */
823 fprintf(stderr,
824 "%s:%d: rec->data != rec->input\n",
825 __FILE__, __LINE__);
826 else
827 arc4random_buf(rec->input, ivlen);
828 }
829 }
830 } else {
831 if (EVP_MD_CTX_md(s->read_hash)) {
832 int n = EVP_MD_CTX_size(s->read_hash);
833 OPENSSL_assert(n >= 0);
834 }
835 ds = s->enc_read_ctx;
836 if (s->enc_read_ctx == NULL)
837 enc = NULL;
838 else
839 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
840 }
841
842 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
843 memmove(rec->data, rec->input, rec->length);
844 rec->input = rec->data;
845 ret = 1;
846 } else {
847 l = rec->length;
848 bs = EVP_CIPHER_block_size(ds->cipher);
849
850 if (EVP_CIPHER_flags(ds->cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) {
851 unsigned char buf[13];
852
853 if (SSL_IS_DTLS(s)) {
854 dtls1_build_sequence_number(buf, seq,
855 send ? s->d1->w_epoch : s->d1->r_epoch);
856 } else {
857 memcpy(buf, seq, SSL3_SEQUENCE_SIZE);
858 ssl3_record_sequence_increment(seq);
859 }
860
861 buf[8] = rec->type;
862 buf[9] = (unsigned char)(s->version >> 8);
863 buf[10] = (unsigned char)(s->version);
864 buf[11] = rec->length >> 8;
865 buf[12] = rec->length & 0xff;
866 pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD, 13, buf);
867 if (send) {
868 l += pad;
869 rec->length += pad;
870 }
871 } else if ((bs != 1) && send) {
872 i = bs - ((int)l % bs);
873
874 /* Add weird padding of upto 256 bytes */
875
876 /* we need to add 'i' padding bytes of value j */
877 j = i - 1;
878 for (k = (int)l; k < (int)(l + i); k++)
879 rec->input[k] = j;
880 l += i;
881 rec->length += i;
882 }
883
884 if (!send) {
885 if (l == 0 || l % bs != 0)
886 return 0;
887 }
888
889 i = EVP_Cipher(ds, rec->data, rec->input, l);
890 if ((EVP_CIPHER_flags(ds->cipher) &
891 EVP_CIPH_FLAG_CUSTOM_CIPHER) ? (i < 0) : (i == 0))
892 return -1; /* AEAD can fail to verify MAC */
893 if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE && !send) {
894 rec->data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
895 rec->input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
896 rec->length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
897 }
898
899 ret = 1;
900 if (EVP_MD_CTX_md(s->read_hash) != NULL)
901 mac_size = EVP_MD_CTX_size(s->read_hash);
902 if ((bs != 1) && !send)
903 ret = tls1_cbc_remove_padding(s, rec, bs, mac_size);
904 if (pad && !send)
905 rec->length -= pad;
906 }
907 return ret;
908}
909
910int
911tls1_cert_verify_mac(SSL *s, int md_nid, unsigned char *out)
912{
913 EVP_MD_CTX ctx, *d = NULL;
914 unsigned int ret;
915 int i;
916
917 if (s->s3->handshake_buffer)
918 if (!ssl3_digest_cached_records(s))
919 return 0;
920
921 for (i = 0; i < SSL_MAX_DIGEST; i++) {
922 if (s->s3->handshake_dgst[i] &&
923 EVP_MD_CTX_type(s->s3->handshake_dgst[i]) == md_nid) {
924 d = s->s3->handshake_dgst[i];
925 break;
926 }
927 }
928 if (d == NULL) {
929 SSLerr(SSL_F_TLS1_CERT_VERIFY_MAC, SSL_R_NO_REQUIRED_DIGEST);
930 return 0;
931 }
932
933 EVP_MD_CTX_init(&ctx);
934 if (!EVP_MD_CTX_copy_ex(&ctx, d))
935 return 0;
936 EVP_DigestFinal_ex(&ctx, out, &ret);
937 EVP_MD_CTX_cleanup(&ctx);
938
939 return ((int)ret);
940}
941
942int
943tls1_final_finish_mac(SSL *s, const char *str, int slen, unsigned char *out)
944{
945 unsigned int i;
946 EVP_MD_CTX ctx;
947 unsigned char buf[2*EVP_MAX_MD_SIZE];
948 unsigned char *q, buf2[12];
949 int idx;
950 long mask;
951 int err = 0;
952 const EVP_MD *md;
953
954 q = buf;
955
956 if (s->s3->handshake_buffer)
957 if (!ssl3_digest_cached_records(s))
958 return 0;
959
960 EVP_MD_CTX_init(&ctx);
961
962 for (idx = 0; ssl_get_handshake_digest(idx, &mask, &md); idx++) {
963 if (ssl_get_algorithm2(s) & mask) {
964 int hashsize = EVP_MD_size(md);
965 EVP_MD_CTX *hdgst = s->s3->handshake_dgst[idx];
966 if (!hdgst || hashsize < 0 ||
967 hashsize > (int)(sizeof buf - (size_t)(q - buf))) {
968 /* internal error: 'buf' is too small for this cipersuite! */
969 err = 1;
970 } else {
971 if (!EVP_MD_CTX_copy_ex(&ctx, hdgst) ||
972 !EVP_DigestFinal_ex(&ctx, q, &i) ||
973 (i != (unsigned int)hashsize))
974 err = 1;
975 q += hashsize;
976 }
977 }
978 }
979
980 if (!tls1_PRF(ssl_get_algorithm2(s), str, slen, buf, (int)(q - buf),
981 NULL, 0, NULL, 0, NULL, 0,
982 s->session->master_key, s->session->master_key_length,
983 out, buf2, sizeof buf2))
984 err = 1;
985 EVP_MD_CTX_cleanup(&ctx);
986
987 if (err)
988 return 0;
989 else
990 return sizeof buf2;
991}
992
993int
994tls1_mac(SSL *ssl, unsigned char *md, int send)
995{
996 SSL3_RECORD *rec;
997 unsigned char *seq;
998 EVP_MD_CTX *hash;
999 size_t md_size, orig_len;
1000 EVP_MD_CTX hmac, *mac_ctx;
1001 unsigned char header[13];
1002 int stream_mac = (send ?
1003 (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM) :
1004 (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM));
1005 int t;
1006
1007 if (send) {
1008 rec = &(ssl->s3->wrec);
1009 seq = &(ssl->s3->write_sequence[0]);
1010 hash = ssl->write_hash;
1011 } else {
1012 rec = &(ssl->s3->rrec);
1013 seq = &(ssl->s3->read_sequence[0]);
1014 hash = ssl->read_hash;
1015 }
1016
1017 t = EVP_MD_CTX_size(hash);
1018 OPENSSL_assert(t >= 0);
1019 md_size = t;
1020
1021 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
1022 if (stream_mac) {
1023 mac_ctx = hash;
1024 } else {
1025 if (!EVP_MD_CTX_copy(&hmac, hash))
1026 return -1;
1027 mac_ctx = &hmac;
1028 }
1029
1030 if (SSL_IS_DTLS(ssl))
1031 dtls1_build_sequence_number(header, seq,
1032 send ? ssl->d1->w_epoch : ssl->d1->r_epoch);
1033 else
1034 memcpy(header, seq, SSL3_SEQUENCE_SIZE);
1035
1036 /* kludge: tls1_cbc_remove_padding passes padding length in rec->type */
1037 orig_len = rec->length + md_size + ((unsigned int)rec->type >> 8);
1038 rec->type &= 0xff;
1039
1040 header[8] = rec->type;
1041 header[9] = (unsigned char)(ssl->version >> 8);
1042 header[10] = (unsigned char)(ssl->version);
1043 header[11] = (rec->length) >> 8;
1044 header[12] = (rec->length) & 0xff;
1045
1046 if (!send &&
1047 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1048 ssl3_cbc_record_digest_supported(mac_ctx)) {
1049 /* This is a CBC-encrypted record. We must avoid leaking any
1050 * timing-side channel information about how many blocks of
1051 * data we are hashing because that gives an attacker a
1052 * timing-oracle. */
1053 if (!ssl3_cbc_digest_record(mac_ctx,
1054 md, &md_size, header, rec->input,
1055 rec->length + md_size, orig_len,
1056 ssl->s3->read_mac_secret,
1057 ssl->s3->read_mac_secret_size,
1058 0 /* not SSLv3 */))
1059 return -1;
1060 } else {
1061 EVP_DigestSignUpdate(mac_ctx, header, sizeof(header));
1062 EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length);
1063 t = EVP_DigestSignFinal(mac_ctx, md, &md_size);
1064 OPENSSL_assert(t > 0);
1065 }
1066
1067 if (!stream_mac)
1068 EVP_MD_CTX_cleanup(&hmac);
1069
1070 if (!SSL_IS_DTLS(ssl))
1071 ssl3_record_sequence_increment(seq);
1072
1073 return (md_size);
1074}
1075
1076int
1077tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
1078 int len)
1079{
1080 unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH];
1081
1082 tls1_PRF(ssl_get_algorithm2(s),
1083 TLS_MD_MASTER_SECRET_CONST, TLS_MD_MASTER_SECRET_CONST_SIZE,
1084 s->s3->client_random, SSL3_RANDOM_SIZE, NULL, 0,
1085 s->s3->server_random, SSL3_RANDOM_SIZE, NULL, 0,
1086 p, len, s->session->master_key, buff, sizeof buff);
1087
1088 return (SSL3_MASTER_SECRET_SIZE);
1089}
1090
1091int
1092tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1093 const char *label, size_t llen, const unsigned char *context,
1094 size_t contextlen, int use_context)
1095{
1096 unsigned char *buff;
1097 unsigned char *val = NULL;
1098 size_t vallen, currentvalpos;
1099 int rv;
1100
1101 buff = malloc(olen);
1102 if (buff == NULL)
1103 goto err2;
1104
1105 /* construct PRF arguments
1106 * we construct the PRF argument ourself rather than passing separate
1107 * values into the TLS PRF to ensure that the concatenation of values
1108 * does not create a prohibited label.
1109 */
1110 vallen = llen + SSL3_RANDOM_SIZE * 2;
1111 if (use_context) {
1112 vallen += 2 + contextlen;
1113 }
1114
1115 val = malloc(vallen);
1116 if (val == NULL)
1117 goto err2;
1118 currentvalpos = 0;
1119 memcpy(val + currentvalpos, (unsigned char *) label, llen);
1120 currentvalpos += llen;
1121 memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE);
1122 currentvalpos += SSL3_RANDOM_SIZE;
1123 memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE);
1124 currentvalpos += SSL3_RANDOM_SIZE;
1125
1126 if (use_context) {
1127 val[currentvalpos] = (contextlen >> 8) & 0xff;
1128 currentvalpos++;
1129 val[currentvalpos] = contextlen & 0xff;
1130 currentvalpos++;
1131 if ((contextlen > 0) || (context != NULL)) {
1132 memcpy(val + currentvalpos, context, contextlen);
1133 }
1134 }
1135
1136 /* disallow prohibited labels
1137 * note that SSL3_RANDOM_SIZE > max(prohibited label len) =
1138 * 15, so size of val > max(prohibited label len) = 15 and the
1139 * comparisons won't have buffer overflow
1140 */
1141 if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
1142 TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
1143 goto err1;
1144 if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
1145 TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
1146 goto err1;
1147 if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
1148 TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
1149 goto err1;
1150 if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
1151 TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
1152 goto err1;
1153
1154 rv = tls1_PRF(ssl_get_algorithm2(s),
1155 val, vallen, NULL, 0, NULL, 0, NULL, 0, NULL, 0,
1156 s->session->master_key, s->session->master_key_length,
1157 out, buff, olen);
1158
1159 goto ret;
1160err1:
1161 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL,
1162 SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
1163 rv = 0;
1164 goto ret;
1165err2:
1166 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE);
1167 rv = 0;
1168ret:
1169 free(buff);
1170 free(val);
1171
1172 return (rv);
1173}
1174
1175int
1176tls1_alert_code(int code)
1177{
1178 switch (code) {
1179 case SSL_AD_CLOSE_NOTIFY:
1180 return (SSL3_AD_CLOSE_NOTIFY);
1181 case SSL_AD_UNEXPECTED_MESSAGE:
1182 return (SSL3_AD_UNEXPECTED_MESSAGE);
1183 case SSL_AD_BAD_RECORD_MAC:
1184 return (SSL3_AD_BAD_RECORD_MAC);
1185 case SSL_AD_DECRYPTION_FAILED:
1186 return (TLS1_AD_DECRYPTION_FAILED);
1187 case SSL_AD_RECORD_OVERFLOW:
1188 return (TLS1_AD_RECORD_OVERFLOW);
1189 case SSL_AD_DECOMPRESSION_FAILURE:
1190 return (SSL3_AD_DECOMPRESSION_FAILURE);
1191 case SSL_AD_HANDSHAKE_FAILURE:
1192 return (SSL3_AD_HANDSHAKE_FAILURE);
1193 case SSL_AD_NO_CERTIFICATE:
1194 return (-1);
1195 case SSL_AD_BAD_CERTIFICATE:
1196 return (SSL3_AD_BAD_CERTIFICATE);
1197 case SSL_AD_UNSUPPORTED_CERTIFICATE:
1198 return (SSL3_AD_UNSUPPORTED_CERTIFICATE);
1199 case SSL_AD_CERTIFICATE_REVOKED:
1200 return (SSL3_AD_CERTIFICATE_REVOKED);
1201 case SSL_AD_CERTIFICATE_EXPIRED:
1202 return (SSL3_AD_CERTIFICATE_EXPIRED);
1203 case SSL_AD_CERTIFICATE_UNKNOWN:
1204 return (SSL3_AD_CERTIFICATE_UNKNOWN);
1205 case SSL_AD_ILLEGAL_PARAMETER:
1206 return (SSL3_AD_ILLEGAL_PARAMETER);
1207 case SSL_AD_UNKNOWN_CA:
1208 return (TLS1_AD_UNKNOWN_CA);
1209 case SSL_AD_ACCESS_DENIED:
1210 return (TLS1_AD_ACCESS_DENIED);
1211 case SSL_AD_DECODE_ERROR:
1212 return (TLS1_AD_DECODE_ERROR);
1213 case SSL_AD_DECRYPT_ERROR:
1214 return (TLS1_AD_DECRYPT_ERROR);
1215 case SSL_AD_EXPORT_RESTRICTION:
1216 return (TLS1_AD_EXPORT_RESTRICTION);
1217 case SSL_AD_PROTOCOL_VERSION:
1218 return (TLS1_AD_PROTOCOL_VERSION);
1219 case SSL_AD_INSUFFICIENT_SECURITY:
1220 return (TLS1_AD_INSUFFICIENT_SECURITY);
1221 case SSL_AD_INTERNAL_ERROR:
1222 return (TLS1_AD_INTERNAL_ERROR);
1223 case SSL_AD_INAPPROPRIATE_FALLBACK:
1224 return(TLS1_AD_INAPPROPRIATE_FALLBACK);
1225 case SSL_AD_USER_CANCELLED:
1226 return (TLS1_AD_USER_CANCELLED);
1227 case SSL_AD_NO_RENEGOTIATION:
1228 return (TLS1_AD_NO_RENEGOTIATION);
1229 case SSL_AD_UNSUPPORTED_EXTENSION:
1230 return (TLS1_AD_UNSUPPORTED_EXTENSION);
1231 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
1232 return (TLS1_AD_CERTIFICATE_UNOBTAINABLE);
1233 case SSL_AD_UNRECOGNIZED_NAME:
1234 return (TLS1_AD_UNRECOGNIZED_NAME);
1235 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
1236 return (TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
1237 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
1238 return (TLS1_AD_BAD_CERTIFICATE_HASH_VALUE);
1239 case SSL_AD_UNKNOWN_PSK_IDENTITY:
1240 return (TLS1_AD_UNKNOWN_PSK_IDENTITY);
1241 default:
1242 return (-1);
1243 }
1244}