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