xref: /minix3/crypto/external/bsd/openssl/dist/ssl/t1_enc.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* ssl/t1_enc.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc /* ====================================================================
59ebfedea0SLionel Sambuc  * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
60ebfedea0SLionel Sambuc  *
61ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
62ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
63ebfedea0SLionel Sambuc  * are met:
64ebfedea0SLionel Sambuc  *
65ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
66ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
67ebfedea0SLionel Sambuc  *
68ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
69ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
70ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
71ebfedea0SLionel Sambuc  *    distribution.
72ebfedea0SLionel Sambuc  *
73ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
74ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
75ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
76ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77ebfedea0SLionel Sambuc  *
78ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
80ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
81ebfedea0SLionel Sambuc  *    openssl-core@openssl.org.
82ebfedea0SLionel Sambuc  *
83ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
84ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
85ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
86ebfedea0SLionel Sambuc  *
87ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
88ebfedea0SLionel Sambuc  *    acknowledgment:
89ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
90ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91ebfedea0SLionel Sambuc  *
92ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
104ebfedea0SLionel Sambuc  * ====================================================================
105ebfedea0SLionel Sambuc  *
106ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
107ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
108ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
109ebfedea0SLionel Sambuc  *
110ebfedea0SLionel Sambuc  */
111ebfedea0SLionel Sambuc /* ====================================================================
112ebfedea0SLionel Sambuc  * Copyright 2005 Nokia. All rights reserved.
113ebfedea0SLionel Sambuc  *
114ebfedea0SLionel Sambuc  * The portions of the attached software ("Contribution") is developed by
115ebfedea0SLionel Sambuc  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
116ebfedea0SLionel Sambuc  * license.
117ebfedea0SLionel Sambuc  *
118ebfedea0SLionel Sambuc  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
119ebfedea0SLionel Sambuc  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
120ebfedea0SLionel Sambuc  * support (see RFC 4279) to OpenSSL.
121ebfedea0SLionel Sambuc  *
122ebfedea0SLionel Sambuc  * No patent licenses or other rights except those expressly stated in
123ebfedea0SLionel Sambuc  * the OpenSSL open source license shall be deemed granted or received
124ebfedea0SLionel Sambuc  * expressly, by implication, estoppel, or otherwise.
125ebfedea0SLionel Sambuc  *
126ebfedea0SLionel Sambuc  * No assurances are provided by Nokia that the Contribution does not
127ebfedea0SLionel Sambuc  * infringe the patent or other intellectual property rights of any third
128ebfedea0SLionel Sambuc  * party or that the license provides you with all the necessary rights
129ebfedea0SLionel Sambuc  * to make use of the Contribution.
130ebfedea0SLionel Sambuc  *
131ebfedea0SLionel Sambuc  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
132ebfedea0SLionel Sambuc  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
133ebfedea0SLionel Sambuc  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
134ebfedea0SLionel Sambuc  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
135ebfedea0SLionel Sambuc  * OTHERWISE.
136ebfedea0SLionel Sambuc  */
137ebfedea0SLionel Sambuc 
138ebfedea0SLionel Sambuc #include <stdio.h>
139ebfedea0SLionel Sambuc #include "ssl_locl.h"
140ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
141ebfedea0SLionel Sambuc # include <openssl/comp.h>
142ebfedea0SLionel Sambuc #endif
143ebfedea0SLionel Sambuc #include <openssl/evp.h>
144ebfedea0SLionel Sambuc #include <openssl/hmac.h>
145ebfedea0SLionel Sambuc #include <openssl/md5.h>
146ebfedea0SLionel Sambuc #include <openssl/rand.h>
147ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
148ebfedea0SLionel Sambuc # include <openssl/des.h>
149ebfedea0SLionel Sambuc #endif
150ebfedea0SLionel Sambuc 
151ebfedea0SLionel Sambuc /* seed1 through seed5 are virtually concatenated */
tls1_P_hash(const EVP_MD * md,const unsigned char * sec,int sec_len,const void * seed1,int seed1_len,const void * seed2,int seed2_len,const void * seed3,int seed3_len,const void * seed4,int seed4_len,const void * seed5,int seed5_len,unsigned char * out,int olen)152ebfedea0SLionel Sambuc static int tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
153ebfedea0SLionel Sambuc                        int sec_len,
154ebfedea0SLionel Sambuc                        const void *seed1, int seed1_len,
155ebfedea0SLionel Sambuc                        const void *seed2, int seed2_len,
156ebfedea0SLionel Sambuc                        const void *seed3, int seed3_len,
157ebfedea0SLionel Sambuc                        const void *seed4, int seed4_len,
158ebfedea0SLionel Sambuc                        const void *seed5, int seed5_len,
159ebfedea0SLionel Sambuc                        unsigned char *out, int olen)
160ebfedea0SLionel Sambuc {
161ebfedea0SLionel Sambuc     int chunk;
162ebfedea0SLionel Sambuc     size_t j;
163ebfedea0SLionel Sambuc     EVP_MD_CTX ctx, ctx_tmp;
164ebfedea0SLionel Sambuc     EVP_PKEY *mac_key;
165ebfedea0SLionel Sambuc     unsigned char A1[EVP_MAX_MD_SIZE];
166ebfedea0SLionel Sambuc     size_t A1_len;
167ebfedea0SLionel Sambuc     int ret = 0;
168ebfedea0SLionel Sambuc 
169ebfedea0SLionel Sambuc     chunk = EVP_MD_size(md);
170ebfedea0SLionel Sambuc     OPENSSL_assert(chunk >= 0);
171ebfedea0SLionel Sambuc 
172ebfedea0SLionel Sambuc     EVP_MD_CTX_init(&ctx);
173ebfedea0SLionel Sambuc     EVP_MD_CTX_init(&ctx_tmp);
174ebfedea0SLionel Sambuc     EVP_MD_CTX_set_flags(&ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
175ebfedea0SLionel Sambuc     EVP_MD_CTX_set_flags(&ctx_tmp, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
176ebfedea0SLionel Sambuc     mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, sec, sec_len);
177ebfedea0SLionel Sambuc     if (!mac_key)
178ebfedea0SLionel Sambuc         goto err;
179ebfedea0SLionel Sambuc     if (!EVP_DigestSignInit(&ctx, NULL, md, NULL, mac_key))
180ebfedea0SLionel Sambuc         goto err;
181ebfedea0SLionel Sambuc     if (!EVP_DigestSignInit(&ctx_tmp, NULL, md, NULL, mac_key))
182ebfedea0SLionel Sambuc         goto err;
183ebfedea0SLionel Sambuc     if (seed1 && !EVP_DigestSignUpdate(&ctx, seed1, seed1_len))
184ebfedea0SLionel Sambuc         goto err;
185ebfedea0SLionel Sambuc     if (seed2 && !EVP_DigestSignUpdate(&ctx, seed2, seed2_len))
186ebfedea0SLionel Sambuc         goto err;
187ebfedea0SLionel Sambuc     if (seed3 && !EVP_DigestSignUpdate(&ctx, seed3, seed3_len))
188ebfedea0SLionel Sambuc         goto err;
189ebfedea0SLionel Sambuc     if (seed4 && !EVP_DigestSignUpdate(&ctx, seed4, seed4_len))
190ebfedea0SLionel Sambuc         goto err;
191ebfedea0SLionel Sambuc     if (seed5 && !EVP_DigestSignUpdate(&ctx, seed5, seed5_len))
192ebfedea0SLionel Sambuc         goto err;
193ebfedea0SLionel Sambuc     if (!EVP_DigestSignFinal(&ctx, A1, &A1_len))
194ebfedea0SLionel Sambuc         goto err;
195ebfedea0SLionel Sambuc 
196*0a6a1f1dSLionel Sambuc     for (;;) {
197ebfedea0SLionel Sambuc         /* Reinit mac contexts */
198ebfedea0SLionel Sambuc         if (!EVP_DigestSignInit(&ctx, NULL, md, NULL, mac_key))
199ebfedea0SLionel Sambuc             goto err;
200ebfedea0SLionel Sambuc         if (!EVP_DigestSignInit(&ctx_tmp, NULL, md, NULL, mac_key))
201ebfedea0SLionel Sambuc             goto err;
202ebfedea0SLionel Sambuc         if (!EVP_DigestSignUpdate(&ctx, A1, A1_len))
203ebfedea0SLionel Sambuc             goto err;
204ebfedea0SLionel Sambuc         if (!EVP_DigestSignUpdate(&ctx_tmp, A1, A1_len))
205ebfedea0SLionel Sambuc             goto err;
206ebfedea0SLionel Sambuc         if (seed1 && !EVP_DigestSignUpdate(&ctx, seed1, seed1_len))
207ebfedea0SLionel Sambuc             goto err;
208ebfedea0SLionel Sambuc         if (seed2 && !EVP_DigestSignUpdate(&ctx, seed2, seed2_len))
209ebfedea0SLionel Sambuc             goto err;
210ebfedea0SLionel Sambuc         if (seed3 && !EVP_DigestSignUpdate(&ctx, seed3, seed3_len))
211ebfedea0SLionel Sambuc             goto err;
212ebfedea0SLionel Sambuc         if (seed4 && !EVP_DigestSignUpdate(&ctx, seed4, seed4_len))
213ebfedea0SLionel Sambuc             goto err;
214ebfedea0SLionel Sambuc         if (seed5 && !EVP_DigestSignUpdate(&ctx, seed5, seed5_len))
215ebfedea0SLionel Sambuc             goto err;
216ebfedea0SLionel Sambuc 
217*0a6a1f1dSLionel Sambuc         if (olen > chunk) {
218ebfedea0SLionel Sambuc             if (!EVP_DigestSignFinal(&ctx, out, &j))
219ebfedea0SLionel Sambuc                 goto err;
220ebfedea0SLionel Sambuc             out += j;
221ebfedea0SLionel Sambuc             olen -= j;
222ebfedea0SLionel Sambuc             /* calc the next A1 value */
223ebfedea0SLionel Sambuc             if (!EVP_DigestSignFinal(&ctx_tmp, A1, &A1_len))
224ebfedea0SLionel Sambuc                 goto err;
225*0a6a1f1dSLionel Sambuc         } else {                /* last one */
226*0a6a1f1dSLionel Sambuc 
227ebfedea0SLionel Sambuc             if (!EVP_DigestSignFinal(&ctx, A1, &A1_len))
228ebfedea0SLionel Sambuc                 goto err;
229ebfedea0SLionel Sambuc             memcpy(out, A1, olen);
230ebfedea0SLionel Sambuc             break;
231ebfedea0SLionel Sambuc         }
232ebfedea0SLionel Sambuc     }
233ebfedea0SLionel Sambuc     ret = 1;
234ebfedea0SLionel Sambuc  err:
235ebfedea0SLionel Sambuc     EVP_PKEY_free(mac_key);
236ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(&ctx);
237ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(&ctx_tmp);
238ebfedea0SLionel Sambuc     OPENSSL_cleanse(A1, sizeof(A1));
239ebfedea0SLionel Sambuc     return ret;
240ebfedea0SLionel Sambuc }
241ebfedea0SLionel Sambuc 
242ebfedea0SLionel Sambuc /* seed1 through seed5 are virtually concatenated */
tls1_PRF(long digest_mask,const void * seed1,int seed1_len,const void * seed2,int seed2_len,const void * seed3,int seed3_len,const void * seed4,int seed4_len,const void * seed5,int seed5_len,const unsigned char * sec,int slen,unsigned char * out1,unsigned char * out2,int olen)243ebfedea0SLionel Sambuc static int tls1_PRF(long digest_mask,
244ebfedea0SLionel Sambuc                     const void *seed1, int seed1_len,
245ebfedea0SLionel Sambuc                     const void *seed2, int seed2_len,
246ebfedea0SLionel Sambuc                     const void *seed3, int seed3_len,
247ebfedea0SLionel Sambuc                     const void *seed4, int seed4_len,
248ebfedea0SLionel Sambuc                     const void *seed5, int seed5_len,
249ebfedea0SLionel Sambuc                     const unsigned char *sec, int slen,
250*0a6a1f1dSLionel Sambuc                     unsigned char *out1, unsigned char *out2, int olen)
251ebfedea0SLionel Sambuc {
252ebfedea0SLionel Sambuc     int len, i, idx, count;
253ebfedea0SLionel Sambuc     const unsigned char *S1;
254ebfedea0SLionel Sambuc     long m;
255ebfedea0SLionel Sambuc     const EVP_MD *md;
256ebfedea0SLionel Sambuc     int ret = 0;
257ebfedea0SLionel Sambuc 
258ebfedea0SLionel Sambuc     /* Count number of digests and partition sec evenly */
259ebfedea0SLionel Sambuc     count = 0;
260ebfedea0SLionel Sambuc     for (idx = 0; ssl_get_handshake_digest(idx, &m, &md); idx++) {
261*0a6a1f1dSLionel Sambuc         if ((m << TLS1_PRF_DGST_SHIFT) & digest_mask)
262*0a6a1f1dSLionel Sambuc             count++;
263*0a6a1f1dSLionel Sambuc     }
264*0a6a1f1dSLionel Sambuc     if (!count) {
265*0a6a1f1dSLionel Sambuc         /* Should never happen */
266*0a6a1f1dSLionel Sambuc         SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
267*0a6a1f1dSLionel Sambuc         goto err;
268ebfedea0SLionel Sambuc     }
269ebfedea0SLionel Sambuc     len = slen / count;
270ebfedea0SLionel Sambuc     if (count == 1)
271ebfedea0SLionel Sambuc         slen = 0;
272ebfedea0SLionel Sambuc     S1 = sec;
273ebfedea0SLionel Sambuc     memset(out1, 0, olen);
274ebfedea0SLionel Sambuc     for (idx = 0; ssl_get_handshake_digest(idx, &m, &md); idx++) {
275ebfedea0SLionel Sambuc         if ((m << TLS1_PRF_DGST_SHIFT) & digest_mask) {
276ebfedea0SLionel Sambuc             if (!md) {
277*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_TLS1_PRF, SSL_R_UNSUPPORTED_DIGEST_TYPE);
278ebfedea0SLionel Sambuc                 goto err;
279ebfedea0SLionel Sambuc             }
280ebfedea0SLionel Sambuc             if (!tls1_P_hash(md, S1, len + (slen & 1),
281*0a6a1f1dSLionel Sambuc                              seed1, seed1_len, seed2, seed2_len, seed3,
282*0a6a1f1dSLionel Sambuc                              seed3_len, seed4, seed4_len, seed5, seed5_len,
283ebfedea0SLionel Sambuc                              out2, olen))
284ebfedea0SLionel Sambuc                 goto err;
285ebfedea0SLionel Sambuc             S1 += len;
286*0a6a1f1dSLionel Sambuc             for (i = 0; i < olen; i++) {
287ebfedea0SLionel Sambuc                 out1[i] ^= out2[i];
288ebfedea0SLionel Sambuc             }
289ebfedea0SLionel Sambuc         }
290ebfedea0SLionel Sambuc     }
291ebfedea0SLionel Sambuc     ret = 1;
292ebfedea0SLionel Sambuc  err:
293ebfedea0SLionel Sambuc     return ret;
294ebfedea0SLionel Sambuc }
295*0a6a1f1dSLionel Sambuc 
tls1_generate_key_block(SSL * s,unsigned char * km,unsigned char * tmp,int num)296ebfedea0SLionel Sambuc static int tls1_generate_key_block(SSL *s, unsigned char *km,
297ebfedea0SLionel Sambuc                                    unsigned char *tmp, int num)
298ebfedea0SLionel Sambuc {
299ebfedea0SLionel Sambuc     int ret;
300ebfedea0SLionel Sambuc     ret = tls1_PRF(ssl_get_algorithm2(s),
301*0a6a1f1dSLionel Sambuc                    TLS_MD_KEY_EXPANSION_CONST,
302*0a6a1f1dSLionel Sambuc                    TLS_MD_KEY_EXPANSION_CONST_SIZE, s->s3->server_random,
303*0a6a1f1dSLionel Sambuc                    SSL3_RANDOM_SIZE, s->s3->client_random, SSL3_RANDOM_SIZE,
304*0a6a1f1dSLionel Sambuc                    NULL, 0, NULL, 0, s->session->master_key,
305*0a6a1f1dSLionel Sambuc                    s->session->master_key_length, km, tmp, num);
306ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
307*0a6a1f1dSLionel Sambuc     fprintf(stderr, "tls1_generate_key_block() ==> %d byte master_key =\n\t",
308ebfedea0SLionel Sambuc             s->session->master_key_length);
309ebfedea0SLionel Sambuc     {
310ebfedea0SLionel Sambuc         int i;
311*0a6a1f1dSLionel Sambuc         for (i = 0; i < s->session->master_key_length; i++) {
312*0a6a1f1dSLionel Sambuc             fprintf(stderr, "%02X", s->session->master_key[i]);
313ebfedea0SLionel Sambuc         }
314*0a6a1f1dSLionel Sambuc         fprintf(stderr, "\n");
315*0a6a1f1dSLionel Sambuc     }
316ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
317ebfedea0SLionel Sambuc     return ret;
318ebfedea0SLionel Sambuc }
319ebfedea0SLionel Sambuc 
tls1_change_cipher_state(SSL * s,int which)320ebfedea0SLionel Sambuc int tls1_change_cipher_state(SSL *s, int which)
321ebfedea0SLionel Sambuc {
322ebfedea0SLionel Sambuc     static const unsigned char empty[] = "";
323ebfedea0SLionel Sambuc     unsigned char *p, *mac_secret;
324ebfedea0SLionel Sambuc     unsigned char *exp_label;
325ebfedea0SLionel Sambuc     unsigned char tmp1[EVP_MAX_KEY_LENGTH];
326ebfedea0SLionel Sambuc     unsigned char tmp2[EVP_MAX_KEY_LENGTH];
327ebfedea0SLionel Sambuc     unsigned char iv1[EVP_MAX_IV_LENGTH * 2];
328ebfedea0SLionel Sambuc     unsigned char iv2[EVP_MAX_IV_LENGTH * 2];
329ebfedea0SLionel Sambuc     unsigned char *ms, *key, *iv;
330ebfedea0SLionel Sambuc     int client_write;
331ebfedea0SLionel Sambuc     EVP_CIPHER_CTX *dd;
332ebfedea0SLionel Sambuc     const EVP_CIPHER *c;
333ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
334ebfedea0SLionel Sambuc     const SSL_COMP *comp;
335ebfedea0SLionel Sambuc #endif
336ebfedea0SLionel Sambuc     const EVP_MD *m;
337ebfedea0SLionel Sambuc     int mac_type;
338ebfedea0SLionel Sambuc     int *mac_secret_size;
339ebfedea0SLionel Sambuc     EVP_MD_CTX *mac_ctx;
340ebfedea0SLionel Sambuc     EVP_PKEY *mac_key;
341ebfedea0SLionel Sambuc     int is_export, n, i, j, k, exp_label_len, cl;
342ebfedea0SLionel Sambuc     int reuse_dd = 0;
343ebfedea0SLionel Sambuc 
344ebfedea0SLionel Sambuc     is_export = SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
345ebfedea0SLionel Sambuc     c = s->s3->tmp.new_sym_enc;
346ebfedea0SLionel Sambuc     m = s->s3->tmp.new_hash;
347ebfedea0SLionel Sambuc     mac_type = s->s3->tmp.new_mac_pkey_type;
348ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
349ebfedea0SLionel Sambuc     comp = s->s3->tmp.new_compression;
350ebfedea0SLionel Sambuc #endif
351ebfedea0SLionel Sambuc 
352ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
353*0a6a1f1dSLionel Sambuc     fprintf(stderr, "tls1_change_cipher_state(which= %d) w/\n", which);
354*0a6a1f1dSLionel Sambuc     fprintf(stderr, "\talg= %ld/%ld, comp= %p\n",
355ebfedea0SLionel Sambuc             s->s3->tmp.new_cipher->algorithm_mkey,
356*0a6a1f1dSLionel Sambuc             s->s3->tmp.new_cipher->algorithm_auth, comp);
357*0a6a1f1dSLionel Sambuc     fprintf(stderr, "\tevp_cipher == %p ==? &d_cbc_ede_cipher3\n", c);
358*0a6a1f1dSLionel Sambuc     fprintf(stderr, "\tevp_cipher: nid, blksz= %d, %d, keylen=%d, ivlen=%d\n",
359ebfedea0SLionel Sambuc             c->nid, c->block_size, c->key_len, c->iv_len);
360*0a6a1f1dSLionel Sambuc     fprintf(stderr, "\tkey_block: len= %d, data= ",
361*0a6a1f1dSLionel Sambuc             s->s3->tmp.key_block_length);
362ebfedea0SLionel Sambuc     {
363ebfedea0SLionel Sambuc         int i;
364ebfedea0SLionel Sambuc         for (i = 0; i < s->s3->tmp.key_block_length; i++)
365*0a6a1f1dSLionel Sambuc             fprintf(stderr, "%02x", s->s3->tmp.key_block[i]);
366*0a6a1f1dSLionel Sambuc         fprintf(stderr, "\n");
367ebfedea0SLionel Sambuc     }
368ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
369ebfedea0SLionel Sambuc 
370*0a6a1f1dSLionel Sambuc     if (which & SSL3_CC_READ) {
371ebfedea0SLionel Sambuc         if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
372ebfedea0SLionel Sambuc             s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
373ebfedea0SLionel Sambuc         else
374ebfedea0SLionel Sambuc             s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
375ebfedea0SLionel Sambuc 
376ebfedea0SLionel Sambuc         if (s->enc_read_ctx != NULL)
377ebfedea0SLionel Sambuc             reuse_dd = 1;
378*0a6a1f1dSLionel Sambuc         else if ((s->enc_read_ctx =
379*0a6a1f1dSLionel Sambuc                   OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)
380ebfedea0SLionel Sambuc             goto err;
381ebfedea0SLionel Sambuc         else
382*0a6a1f1dSLionel Sambuc             /*
383*0a6a1f1dSLionel Sambuc              * make sure it's intialized in case we exit later with an error
384*0a6a1f1dSLionel Sambuc              */
385ebfedea0SLionel Sambuc             EVP_CIPHER_CTX_init(s->enc_read_ctx);
386ebfedea0SLionel Sambuc         dd = s->enc_read_ctx;
387ebfedea0SLionel Sambuc         mac_ctx = ssl_replace_hash(&s->read_hash, NULL);
388ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
389*0a6a1f1dSLionel Sambuc         if (s->expand != NULL) {
390ebfedea0SLionel Sambuc             COMP_CTX_free(s->expand);
391ebfedea0SLionel Sambuc             s->expand = NULL;
392ebfedea0SLionel Sambuc         }
393*0a6a1f1dSLionel Sambuc         if (comp != NULL) {
394ebfedea0SLionel Sambuc             s->expand = COMP_CTX_new(comp->method);
395*0a6a1f1dSLionel Sambuc             if (s->expand == NULL) {
396*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,
397*0a6a1f1dSLionel Sambuc                        SSL_R_COMPRESSION_LIBRARY_ERROR);
398ebfedea0SLionel Sambuc                 goto err2;
399ebfedea0SLionel Sambuc             }
400ebfedea0SLionel Sambuc             if (s->s3->rrec.comp == NULL)
401ebfedea0SLionel Sambuc                 s->s3->rrec.comp = (unsigned char *)
402ebfedea0SLionel Sambuc                     OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
403ebfedea0SLionel Sambuc             if (s->s3->rrec.comp == NULL)
404ebfedea0SLionel Sambuc                 goto err;
405ebfedea0SLionel Sambuc         }
406ebfedea0SLionel Sambuc #endif
407*0a6a1f1dSLionel Sambuc         /*
408*0a6a1f1dSLionel Sambuc          * this is done by dtls1_reset_seq_numbers for DTLS1_VERSION
409*0a6a1f1dSLionel Sambuc          */
410ebfedea0SLionel Sambuc         if (s->version != DTLS1_VERSION)
411ebfedea0SLionel Sambuc             memset(&(s->s3->read_sequence[0]), 0, 8);
412ebfedea0SLionel Sambuc         mac_secret = &(s->s3->read_mac_secret[0]);
413ebfedea0SLionel Sambuc         mac_secret_size = &(s->s3->read_mac_secret_size);
414*0a6a1f1dSLionel Sambuc     } else {
415ebfedea0SLionel Sambuc         if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
416ebfedea0SLionel Sambuc             s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
417ebfedea0SLionel Sambuc         else
418ebfedea0SLionel Sambuc             s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
419*0a6a1f1dSLionel Sambuc         if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s))
420ebfedea0SLionel Sambuc             reuse_dd = 1;
421*0a6a1f1dSLionel Sambuc         else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL)
422ebfedea0SLionel Sambuc             goto err;
423ebfedea0SLionel Sambuc         dd = s->enc_write_ctx;
424*0a6a1f1dSLionel Sambuc         if (SSL_IS_DTLS(s)) {
425*0a6a1f1dSLionel Sambuc             mac_ctx = EVP_MD_CTX_create();
426*0a6a1f1dSLionel Sambuc             if (!mac_ctx)
427*0a6a1f1dSLionel Sambuc                 goto err;
428*0a6a1f1dSLionel Sambuc             s->write_hash = mac_ctx;
429*0a6a1f1dSLionel Sambuc         } else
430ebfedea0SLionel Sambuc             mac_ctx = ssl_replace_hash(&s->write_hash, NULL);
431ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
432*0a6a1f1dSLionel Sambuc         if (s->compress != NULL) {
433ebfedea0SLionel Sambuc             COMP_CTX_free(s->compress);
434ebfedea0SLionel Sambuc             s->compress = NULL;
435ebfedea0SLionel Sambuc         }
436*0a6a1f1dSLionel Sambuc         if (comp != NULL) {
437ebfedea0SLionel Sambuc             s->compress = COMP_CTX_new(comp->method);
438*0a6a1f1dSLionel Sambuc             if (s->compress == NULL) {
439*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,
440*0a6a1f1dSLionel Sambuc                        SSL_R_COMPRESSION_LIBRARY_ERROR);
441ebfedea0SLionel Sambuc                 goto err2;
442ebfedea0SLionel Sambuc             }
443ebfedea0SLionel Sambuc         }
444ebfedea0SLionel Sambuc #endif
445*0a6a1f1dSLionel Sambuc         /*
446*0a6a1f1dSLionel Sambuc          * this is done by dtls1_reset_seq_numbers for DTLS1_VERSION
447*0a6a1f1dSLionel Sambuc          */
448ebfedea0SLionel Sambuc         if (s->version != DTLS1_VERSION)
449ebfedea0SLionel Sambuc             memset(&(s->s3->write_sequence[0]), 0, 8);
450ebfedea0SLionel Sambuc         mac_secret = &(s->s3->write_mac_secret[0]);
451ebfedea0SLionel Sambuc         mac_secret_size = &(s->s3->write_mac_secret_size);
452ebfedea0SLionel Sambuc     }
453ebfedea0SLionel Sambuc 
454ebfedea0SLionel Sambuc     if (reuse_dd)
455ebfedea0SLionel Sambuc         EVP_CIPHER_CTX_cleanup(dd);
456ebfedea0SLionel Sambuc 
457ebfedea0SLionel Sambuc     p = s->s3->tmp.key_block;
458ebfedea0SLionel Sambuc     i = *mac_secret_size = s->s3->tmp.new_mac_secret_size;
459ebfedea0SLionel Sambuc 
460ebfedea0SLionel Sambuc     cl = EVP_CIPHER_key_length(c);
461ebfedea0SLionel Sambuc     j = is_export ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ?
462ebfedea0SLionel Sambuc                      cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;
463ebfedea0SLionel Sambuc     /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
464ebfedea0SLionel Sambuc     /* If GCM mode only part of IV comes from PRF */
465ebfedea0SLionel Sambuc     if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
466ebfedea0SLionel Sambuc         k = EVP_GCM_TLS_FIXED_IV_LEN;
467ebfedea0SLionel Sambuc     else
468ebfedea0SLionel Sambuc         k = EVP_CIPHER_iv_length(c);
469ebfedea0SLionel Sambuc     if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
470*0a6a1f1dSLionel Sambuc         (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
471*0a6a1f1dSLionel Sambuc         ms = &(p[0]);
472*0a6a1f1dSLionel Sambuc         n = i + i;
473*0a6a1f1dSLionel Sambuc         key = &(p[n]);
474*0a6a1f1dSLionel Sambuc         n += j + j;
475*0a6a1f1dSLionel Sambuc         iv = &(p[n]);
476*0a6a1f1dSLionel Sambuc         n += k + k;
477ebfedea0SLionel Sambuc         exp_label = (unsigned char *)TLS_MD_CLIENT_WRITE_KEY_CONST;
478ebfedea0SLionel Sambuc         exp_label_len = TLS_MD_CLIENT_WRITE_KEY_CONST_SIZE;
479ebfedea0SLionel Sambuc         client_write = 1;
480*0a6a1f1dSLionel Sambuc     } else {
481ebfedea0SLionel Sambuc         n = i;
482*0a6a1f1dSLionel Sambuc         ms = &(p[n]);
483*0a6a1f1dSLionel Sambuc         n += i + j;
484*0a6a1f1dSLionel Sambuc         key = &(p[n]);
485*0a6a1f1dSLionel Sambuc         n += j + k;
486*0a6a1f1dSLionel Sambuc         iv = &(p[n]);
487*0a6a1f1dSLionel Sambuc         n += k;
488ebfedea0SLionel Sambuc         exp_label = (unsigned char *)TLS_MD_SERVER_WRITE_KEY_CONST;
489ebfedea0SLionel Sambuc         exp_label_len = TLS_MD_SERVER_WRITE_KEY_CONST_SIZE;
490ebfedea0SLionel Sambuc         client_write = 0;
491ebfedea0SLionel Sambuc     }
492ebfedea0SLionel Sambuc 
493*0a6a1f1dSLionel Sambuc     if (n > s->s3->tmp.key_block_length) {
494ebfedea0SLionel Sambuc         SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
495ebfedea0SLionel Sambuc         goto err2;
496ebfedea0SLionel Sambuc     }
497ebfedea0SLionel Sambuc 
498ebfedea0SLionel Sambuc     memcpy(mac_secret, ms, i);
499ebfedea0SLionel Sambuc 
500*0a6a1f1dSLionel Sambuc     if (!(EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
501ebfedea0SLionel Sambuc         mac_key = EVP_PKEY_new_mac_key(mac_type, NULL,
502ebfedea0SLionel Sambuc                                        mac_secret, *mac_secret_size);
503ebfedea0SLionel Sambuc         EVP_DigestSignInit(mac_ctx, NULL, m, NULL, mac_key);
504ebfedea0SLionel Sambuc         EVP_PKEY_free(mac_key);
505ebfedea0SLionel Sambuc     }
506ebfedea0SLionel Sambuc #ifdef TLS_DEBUG
507ebfedea0SLionel Sambuc     printf("which = %04X\nmac key=", which);
508ebfedea0SLionel Sambuc     {
509*0a6a1f1dSLionel Sambuc         int z;
510*0a6a1f1dSLionel Sambuc         for (z = 0; z < i; z++)
511*0a6a1f1dSLionel Sambuc             printf("%02X%c", ms[z], ((z + 1) % 16) ? ' ' : '\n');
512*0a6a1f1dSLionel Sambuc     }
513*0a6a1f1dSLionel Sambuc #endif
514*0a6a1f1dSLionel Sambuc     if (is_export) {
515*0a6a1f1dSLionel Sambuc         /*
516*0a6a1f1dSLionel Sambuc          * In here I set both the read and write key/iv to the same value
517*0a6a1f1dSLionel Sambuc          * since only the correct one will be used :-).
518ebfedea0SLionel Sambuc          */
519ebfedea0SLionel Sambuc         if (!tls1_PRF(ssl_get_algorithm2(s),
520ebfedea0SLionel Sambuc                       exp_label, exp_label_len,
521ebfedea0SLionel Sambuc                       s->s3->client_random, SSL3_RANDOM_SIZE,
522ebfedea0SLionel Sambuc                       s->s3->server_random, SSL3_RANDOM_SIZE,
523ebfedea0SLionel Sambuc                       NULL, 0, NULL, 0,
524ebfedea0SLionel Sambuc                       key, j, tmp1, tmp2, EVP_CIPHER_key_length(c)))
525ebfedea0SLionel Sambuc             goto err2;
526ebfedea0SLionel Sambuc         key = tmp1;
527ebfedea0SLionel Sambuc 
528*0a6a1f1dSLionel Sambuc         if (k > 0) {
529ebfedea0SLionel Sambuc             if (!tls1_PRF(ssl_get_algorithm2(s),
530ebfedea0SLionel Sambuc                           TLS_MD_IV_BLOCK_CONST, TLS_MD_IV_BLOCK_CONST_SIZE,
531ebfedea0SLionel Sambuc                           s->s3->client_random, SSL3_RANDOM_SIZE,
532ebfedea0SLionel Sambuc                           s->s3->server_random, SSL3_RANDOM_SIZE,
533*0a6a1f1dSLionel Sambuc                           NULL, 0, NULL, 0, empty, 0, iv1, iv2, k * 2))
534ebfedea0SLionel Sambuc                 goto err2;
535ebfedea0SLionel Sambuc             if (client_write)
536ebfedea0SLionel Sambuc                 iv = iv1;
537ebfedea0SLionel Sambuc             else
538ebfedea0SLionel Sambuc                 iv = &(iv1[k]);
539ebfedea0SLionel Sambuc         }
540ebfedea0SLionel Sambuc     }
541ebfedea0SLionel Sambuc 
542ebfedea0SLionel Sambuc     s->session->key_arg_length = 0;
543ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
544ebfedea0SLionel Sambuc     {
545ebfedea0SLionel Sambuc         int i;
546*0a6a1f1dSLionel Sambuc         fprintf(stderr, "EVP_CipherInit_ex(dd,c,key=,iv=,which)\n");
547*0a6a1f1dSLionel Sambuc         fprintf(stderr, "\tkey= ");
548*0a6a1f1dSLionel Sambuc         for (i = 0; i < c->key_len; i++)
549*0a6a1f1dSLionel Sambuc             fprintf(stderr, "%02x", key[i]);
550*0a6a1f1dSLionel Sambuc         fprintf(stderr, "\n");
551*0a6a1f1dSLionel Sambuc         fprintf(stderr, "\t iv= ");
552*0a6a1f1dSLionel Sambuc         for (i = 0; i < c->iv_len; i++)
553*0a6a1f1dSLionel Sambuc             fprintf(stderr, "%02x", iv[i]);
554*0a6a1f1dSLionel Sambuc         fprintf(stderr, "\n");
555ebfedea0SLionel Sambuc     }
556ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
557ebfedea0SLionel Sambuc 
558*0a6a1f1dSLionel Sambuc     if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) {
559*0a6a1f1dSLionel Sambuc         if (!EVP_CipherInit_ex(dd, c, NULL, key, NULL, (which & SSL3_CC_WRITE))
560*0a6a1f1dSLionel Sambuc             || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, k, iv)) {
561*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
562*0a6a1f1dSLionel Sambuc             goto err2;
563ebfedea0SLionel Sambuc         }
564*0a6a1f1dSLionel Sambuc     } else {
565*0a6a1f1dSLionel Sambuc         if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) {
566*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
567*0a6a1f1dSLionel Sambuc             goto err2;
568*0a6a1f1dSLionel Sambuc         }
569*0a6a1f1dSLionel Sambuc     }
570ebfedea0SLionel Sambuc     /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
571*0a6a1f1dSLionel Sambuc     if ((EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER) && *mac_secret_size
572*0a6a1f1dSLionel Sambuc         && !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_MAC_KEY,
573*0a6a1f1dSLionel Sambuc                                 *mac_secret_size, mac_secret)) {
574*0a6a1f1dSLionel Sambuc         SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
575*0a6a1f1dSLionel Sambuc         goto err2;
576*0a6a1f1dSLionel Sambuc     }
577ebfedea0SLionel Sambuc 
578ebfedea0SLionel Sambuc #ifdef TLS_DEBUG
579ebfedea0SLionel Sambuc     printf("which = %04X\nkey=", which);
580*0a6a1f1dSLionel Sambuc     {
581*0a6a1f1dSLionel Sambuc         int z;
582*0a6a1f1dSLionel Sambuc         for (z = 0; z < EVP_CIPHER_key_length(c); z++)
583*0a6a1f1dSLionel Sambuc             printf("%02X%c", key[z], ((z + 1) % 16) ? ' ' : '\n');
584*0a6a1f1dSLionel Sambuc     }
585ebfedea0SLionel Sambuc     printf("\niv=");
586*0a6a1f1dSLionel Sambuc     {
587*0a6a1f1dSLionel Sambuc         int z;
588*0a6a1f1dSLionel Sambuc         for (z = 0; z < k; z++)
589*0a6a1f1dSLionel Sambuc             printf("%02X%c", iv[z], ((z + 1) % 16) ? ' ' : '\n');
590*0a6a1f1dSLionel Sambuc     }
591ebfedea0SLionel Sambuc     printf("\n");
592ebfedea0SLionel Sambuc #endif
593ebfedea0SLionel Sambuc 
594ebfedea0SLionel Sambuc     OPENSSL_cleanse(tmp1, sizeof(tmp1));
595ebfedea0SLionel Sambuc     OPENSSL_cleanse(tmp2, sizeof(tmp1));
596ebfedea0SLionel Sambuc     OPENSSL_cleanse(iv1, sizeof(iv1));
597ebfedea0SLionel Sambuc     OPENSSL_cleanse(iv2, sizeof(iv2));
598ebfedea0SLionel Sambuc     return (1);
599ebfedea0SLionel Sambuc  err:
600ebfedea0SLionel Sambuc     SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
601ebfedea0SLionel Sambuc  err2:
602ebfedea0SLionel Sambuc     return (0);
603ebfedea0SLionel Sambuc }
604ebfedea0SLionel Sambuc 
tls1_setup_key_block(SSL * s)605ebfedea0SLionel Sambuc int tls1_setup_key_block(SSL *s)
606ebfedea0SLionel Sambuc {
607ebfedea0SLionel Sambuc     unsigned char *p1, *p2 = NULL;
608ebfedea0SLionel Sambuc     const EVP_CIPHER *c;
609ebfedea0SLionel Sambuc     const EVP_MD *hash;
610ebfedea0SLionel Sambuc     int num;
611ebfedea0SLionel Sambuc     SSL_COMP *comp;
612ebfedea0SLionel Sambuc     int mac_type = NID_undef, mac_secret_size = 0;
613ebfedea0SLionel Sambuc     int ret = 0;
614ebfedea0SLionel Sambuc 
615ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
616*0a6a1f1dSLionel Sambuc     fprintf(stderr, "tls1_setup_key_block()\n");
617ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
618ebfedea0SLionel Sambuc 
619ebfedea0SLionel Sambuc     if (s->s3->tmp.key_block_length != 0)
620ebfedea0SLionel Sambuc         return (1);
621ebfedea0SLionel Sambuc 
622*0a6a1f1dSLionel Sambuc     if (!ssl_cipher_get_evp
623*0a6a1f1dSLionel Sambuc         (s->session, &c, &hash, &mac_type, &mac_secret_size, &comp)) {
624ebfedea0SLionel Sambuc         SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
625ebfedea0SLionel Sambuc         return (0);
626ebfedea0SLionel Sambuc     }
627ebfedea0SLionel Sambuc 
628ebfedea0SLionel Sambuc     s->s3->tmp.new_sym_enc = c;
629ebfedea0SLionel Sambuc     s->s3->tmp.new_hash = hash;
630ebfedea0SLionel Sambuc     s->s3->tmp.new_mac_pkey_type = mac_type;
631ebfedea0SLionel Sambuc     s->s3->tmp.new_mac_secret_size = mac_secret_size;
632*0a6a1f1dSLionel Sambuc     num =
633*0a6a1f1dSLionel Sambuc         EVP_CIPHER_key_length(c) + mac_secret_size + EVP_CIPHER_iv_length(c);
634ebfedea0SLionel Sambuc     num *= 2;
635ebfedea0SLionel Sambuc 
636ebfedea0SLionel Sambuc     ssl3_cleanup_key_block(s);
637ebfedea0SLionel Sambuc 
638*0a6a1f1dSLionel Sambuc     if ((p1 = (unsigned char *)OPENSSL_malloc(num)) == NULL) {
639ebfedea0SLionel Sambuc         SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE);
640ebfedea0SLionel Sambuc         goto err;
641ebfedea0SLionel Sambuc     }
642ebfedea0SLionel Sambuc 
643ebfedea0SLionel Sambuc     s->s3->tmp.key_block_length = num;
644ebfedea0SLionel Sambuc     s->s3->tmp.key_block = p1;
645ebfedea0SLionel Sambuc 
646*0a6a1f1dSLionel Sambuc     if ((p2 = (unsigned char *)OPENSSL_malloc(num)) == NULL) {
647ebfedea0SLionel Sambuc         SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE);
648*0a6a1f1dSLionel Sambuc         OPENSSL_free(p1);
649ebfedea0SLionel Sambuc         goto err;
650ebfedea0SLionel Sambuc     }
651ebfedea0SLionel Sambuc #ifdef TLS_DEBUG
652ebfedea0SLionel Sambuc     printf("client random\n");
653*0a6a1f1dSLionel Sambuc     {
654*0a6a1f1dSLionel Sambuc         int z;
655*0a6a1f1dSLionel Sambuc         for (z = 0; z < SSL3_RANDOM_SIZE; z++)
656*0a6a1f1dSLionel Sambuc             printf("%02X%c", s->s3->client_random[z],
657*0a6a1f1dSLionel Sambuc                    ((z + 1) % 16) ? ' ' : '\n');
658*0a6a1f1dSLionel Sambuc     }
659ebfedea0SLionel Sambuc     printf("server random\n");
660*0a6a1f1dSLionel Sambuc     {
661*0a6a1f1dSLionel Sambuc         int z;
662*0a6a1f1dSLionel Sambuc         for (z = 0; z < SSL3_RANDOM_SIZE; z++)
663*0a6a1f1dSLionel Sambuc             printf("%02X%c", s->s3->server_random[z],
664*0a6a1f1dSLionel Sambuc                    ((z + 1) % 16) ? ' ' : '\n');
665*0a6a1f1dSLionel Sambuc     }
666ebfedea0SLionel Sambuc     printf("pre-master\n");
667*0a6a1f1dSLionel Sambuc     {
668*0a6a1f1dSLionel Sambuc         int z;
669*0a6a1f1dSLionel Sambuc         for (z = 0; z < s->session->master_key_length; z++)
670*0a6a1f1dSLionel Sambuc             printf("%02X%c", s->session->master_key[z],
671*0a6a1f1dSLionel Sambuc                    ((z + 1) % 16) ? ' ' : '\n');
672*0a6a1f1dSLionel Sambuc     }
673ebfedea0SLionel Sambuc #endif
674ebfedea0SLionel Sambuc     if (!tls1_generate_key_block(s, p1, p2, num))
675ebfedea0SLionel Sambuc         goto err;
676ebfedea0SLionel Sambuc #ifdef TLS_DEBUG
677ebfedea0SLionel Sambuc     printf("\nkey block\n");
678*0a6a1f1dSLionel Sambuc     {
679*0a6a1f1dSLionel Sambuc         int z;
680*0a6a1f1dSLionel Sambuc         for (z = 0; z < num; z++)
681*0a6a1f1dSLionel Sambuc             printf("%02X%c", p1[z], ((z + 1) % 16) ? ' ' : '\n');
682*0a6a1f1dSLionel Sambuc     }
683ebfedea0SLionel Sambuc #endif
684ebfedea0SLionel Sambuc 
685ebfedea0SLionel Sambuc     if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)
686*0a6a1f1dSLionel Sambuc         && s->method->version <= TLS1_VERSION) {
687*0a6a1f1dSLionel Sambuc         /*
688*0a6a1f1dSLionel Sambuc          * enable vulnerability countermeasure for CBC ciphers with known-IV
689*0a6a1f1dSLionel Sambuc          * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
690ebfedea0SLionel Sambuc          */
691ebfedea0SLionel Sambuc         s->s3->need_empty_fragments = 1;
692ebfedea0SLionel Sambuc 
693*0a6a1f1dSLionel Sambuc         if (s->session->cipher != NULL) {
694ebfedea0SLionel Sambuc             if (s->session->cipher->algorithm_enc == SSL_eNULL)
695ebfedea0SLionel Sambuc                 s->s3->need_empty_fragments = 0;
696ebfedea0SLionel Sambuc 
697ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RC4
698ebfedea0SLionel Sambuc             if (s->session->cipher->algorithm_enc == SSL_RC4)
699ebfedea0SLionel Sambuc                 s->s3->need_empty_fragments = 0;
700ebfedea0SLionel Sambuc #endif
701ebfedea0SLionel Sambuc         }
702ebfedea0SLionel Sambuc     }
703ebfedea0SLionel Sambuc 
704ebfedea0SLionel Sambuc     ret = 1;
705ebfedea0SLionel Sambuc  err:
706*0a6a1f1dSLionel Sambuc     if (p2) {
707ebfedea0SLionel Sambuc         OPENSSL_cleanse(p2, num);
708ebfedea0SLionel Sambuc         OPENSSL_free(p2);
709ebfedea0SLionel Sambuc     }
710ebfedea0SLionel Sambuc     return (ret);
711ebfedea0SLionel Sambuc }
712ebfedea0SLionel Sambuc 
713*0a6a1f1dSLionel Sambuc /*-
714*0a6a1f1dSLionel Sambuc  * tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively.
715ebfedea0SLionel Sambuc  *
716ebfedea0SLionel Sambuc  * Returns:
717ebfedea0SLionel Sambuc  *   0: (in non-constant time) if the record is publically invalid (i.e. too
718ebfedea0SLionel Sambuc  *       short etc).
719ebfedea0SLionel Sambuc  *   1: if the record's padding is valid / the encryption was successful.
720ebfedea0SLionel Sambuc  *   -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
721ebfedea0SLionel Sambuc  *       an internal error occured.
722ebfedea0SLionel Sambuc  */
tls1_enc(SSL * s,int send)723ebfedea0SLionel Sambuc int tls1_enc(SSL *s, int send)
724ebfedea0SLionel Sambuc {
725ebfedea0SLionel Sambuc     SSL3_RECORD *rec;
726ebfedea0SLionel Sambuc     EVP_CIPHER_CTX *ds;
727ebfedea0SLionel Sambuc     unsigned long l;
728ebfedea0SLionel Sambuc     int bs, i, j, k, pad = 0, ret, mac_size = 0;
729ebfedea0SLionel Sambuc     const EVP_CIPHER *enc;
730ebfedea0SLionel Sambuc 
731*0a6a1f1dSLionel Sambuc     if (send) {
732*0a6a1f1dSLionel Sambuc         if (EVP_MD_CTX_md(s->write_hash)) {
733ebfedea0SLionel Sambuc             int n = EVP_MD_CTX_size(s->write_hash);
734ebfedea0SLionel Sambuc             OPENSSL_assert(n >= 0);
735ebfedea0SLionel Sambuc         }
736ebfedea0SLionel Sambuc         ds = s->enc_write_ctx;
737ebfedea0SLionel Sambuc         rec = &(s->s3->wrec);
738ebfedea0SLionel Sambuc         if (s->enc_write_ctx == NULL)
739ebfedea0SLionel Sambuc             enc = NULL;
740*0a6a1f1dSLionel Sambuc         else {
741ebfedea0SLionel Sambuc             int ivlen;
742ebfedea0SLionel Sambuc             enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
743ebfedea0SLionel Sambuc             /* For TLSv1.1 and later explicit IV */
744ebfedea0SLionel Sambuc             if (s->version >= TLS1_1_VERSION
745ebfedea0SLionel Sambuc                 && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
746ebfedea0SLionel Sambuc                 ivlen = EVP_CIPHER_iv_length(enc);
747ebfedea0SLionel Sambuc             else
748ebfedea0SLionel Sambuc                 ivlen = 0;
749*0a6a1f1dSLionel Sambuc             if (ivlen > 1) {
750ebfedea0SLionel Sambuc                 if (rec->data != rec->input)
751*0a6a1f1dSLionel Sambuc                     /*
752*0a6a1f1dSLionel Sambuc                      * we can't write into the input stream: Can this ever
753*0a6a1f1dSLionel Sambuc                      * happen?? (steve)
754ebfedea0SLionel Sambuc                      */
755ebfedea0SLionel Sambuc                     fprintf(stderr,
756ebfedea0SLionel Sambuc                             "%s:%d: rec->data != rec->input\n",
757ebfedea0SLionel Sambuc                             __FILE__, __LINE__);
758ebfedea0SLionel Sambuc                 else if (RAND_bytes(rec->input, ivlen) <= 0)
759ebfedea0SLionel Sambuc                     return -1;
760ebfedea0SLionel Sambuc             }
761ebfedea0SLionel Sambuc         }
762*0a6a1f1dSLionel Sambuc     } else {
763*0a6a1f1dSLionel Sambuc         if (EVP_MD_CTX_md(s->read_hash)) {
764ebfedea0SLionel Sambuc             int n = EVP_MD_CTX_size(s->read_hash);
765ebfedea0SLionel Sambuc             OPENSSL_assert(n >= 0);
766ebfedea0SLionel Sambuc         }
767ebfedea0SLionel Sambuc         ds = s->enc_read_ctx;
768ebfedea0SLionel Sambuc         rec = &(s->s3->rrec);
769ebfedea0SLionel Sambuc         if (s->enc_read_ctx == NULL)
770ebfedea0SLionel Sambuc             enc = NULL;
771ebfedea0SLionel Sambuc         else
772ebfedea0SLionel Sambuc             enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
773ebfedea0SLionel Sambuc     }
774ebfedea0SLionel Sambuc 
775ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
776*0a6a1f1dSLionel Sambuc     fprintf(stderr, "tls1_enc(%d)\n", send);
777ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
778ebfedea0SLionel Sambuc 
779*0a6a1f1dSLionel Sambuc     if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
780ebfedea0SLionel Sambuc         memmove(rec->data, rec->input, rec->length);
781ebfedea0SLionel Sambuc         rec->input = rec->data;
782ebfedea0SLionel Sambuc         ret = 1;
783*0a6a1f1dSLionel Sambuc     } else {
784ebfedea0SLionel Sambuc         l = rec->length;
785ebfedea0SLionel Sambuc         bs = EVP_CIPHER_block_size(ds->cipher);
786ebfedea0SLionel Sambuc 
787*0a6a1f1dSLionel Sambuc         if (EVP_CIPHER_flags(ds->cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) {
788*0a6a1f1dSLionel Sambuc             unsigned char buf[EVP_AEAD_TLS1_AAD_LEN], *seq;
789ebfedea0SLionel Sambuc 
790ebfedea0SLionel Sambuc             seq = send ? s->s3->write_sequence : s->s3->read_sequence;
791ebfedea0SLionel Sambuc 
792*0a6a1f1dSLionel Sambuc             if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) {
793ebfedea0SLionel Sambuc                 unsigned char dtlsseq[9], *p = dtlsseq;
794ebfedea0SLionel Sambuc 
795ebfedea0SLionel Sambuc                 s2n(send ? s->d1->w_epoch : s->d1->r_epoch, p);
796ebfedea0SLionel Sambuc                 memcpy(p, &seq[2], 6);
797ebfedea0SLionel Sambuc                 memcpy(buf, dtlsseq, 8);
798*0a6a1f1dSLionel Sambuc             } else {
799ebfedea0SLionel Sambuc                 memcpy(buf, seq, 8);
800*0a6a1f1dSLionel Sambuc                 for (i = 7; i >= 0; i--) { /* increment */
801ebfedea0SLionel Sambuc                     ++seq[i];
802*0a6a1f1dSLionel Sambuc                     if (seq[i] != 0)
803*0a6a1f1dSLionel Sambuc                         break;
804ebfedea0SLionel Sambuc                 }
805ebfedea0SLionel Sambuc             }
806ebfedea0SLionel Sambuc 
807ebfedea0SLionel Sambuc             buf[8] = rec->type;
808ebfedea0SLionel Sambuc             buf[9] = (unsigned char)(s->version >> 8);
809ebfedea0SLionel Sambuc             buf[10] = (unsigned char)(s->version);
810ebfedea0SLionel Sambuc             buf[11] = rec->length >> 8;
811ebfedea0SLionel Sambuc             buf[12] = rec->length & 0xff;
812*0a6a1f1dSLionel Sambuc             pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
813*0a6a1f1dSLionel Sambuc                                       EVP_AEAD_TLS1_AAD_LEN, buf);
814*0a6a1f1dSLionel Sambuc             if (pad <= 0)
815*0a6a1f1dSLionel Sambuc                 return -1;
816*0a6a1f1dSLionel Sambuc             if (send) {
817ebfedea0SLionel Sambuc                 l += pad;
818ebfedea0SLionel Sambuc                 rec->length += pad;
819ebfedea0SLionel Sambuc             }
820*0a6a1f1dSLionel Sambuc         } else if ((bs != 1) && send) {
821ebfedea0SLionel Sambuc             i = bs - ((int)l % bs);
822ebfedea0SLionel Sambuc 
823ebfedea0SLionel Sambuc             /* Add weird padding of upto 256 bytes */
824ebfedea0SLionel Sambuc 
825ebfedea0SLionel Sambuc             /* we need to add 'i' padding bytes of value j */
826ebfedea0SLionel Sambuc             j = i - 1;
827*0a6a1f1dSLionel Sambuc             if (s->options & SSL_OP_TLS_BLOCK_PADDING_BUG) {
828ebfedea0SLionel Sambuc                 if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG)
829ebfedea0SLionel Sambuc                     j++;
830ebfedea0SLionel Sambuc             }
831ebfedea0SLionel Sambuc             for (k = (int)l; k < (int)(l + i); k++)
832ebfedea0SLionel Sambuc                 rec->input[k] = j;
833ebfedea0SLionel Sambuc             l += i;
834ebfedea0SLionel Sambuc             rec->length += i;
835ebfedea0SLionel Sambuc         }
836ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
837ebfedea0SLionel Sambuc         {
838ebfedea0SLionel Sambuc             unsigned long ui;
839*0a6a1f1dSLionel Sambuc             fprintf(stderr,
840*0a6a1f1dSLionel Sambuc                     "EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n",
841ebfedea0SLionel Sambuc                     ds, rec->data, rec->input, l);
842*0a6a1f1dSLionel Sambuc             fprintf(stderr,
843*0a6a1f1dSLionel Sambuc                     "\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%lu %lu], %d iv_len\n",
844*0a6a1f1dSLionel Sambuc                     ds->buf_len, ds->cipher->key_len, DES_KEY_SZ,
845*0a6a1f1dSLionel Sambuc                     DES_SCHEDULE_SZ, ds->cipher->iv_len);
846*0a6a1f1dSLionel Sambuc             fprintf(stderr, "\t\tIV: ");
847*0a6a1f1dSLionel Sambuc             for (i = 0; i < ds->cipher->iv_len; i++)
848*0a6a1f1dSLionel Sambuc                 fprintf(stderr, "%02X", ds->iv[i]);
849*0a6a1f1dSLionel Sambuc             fprintf(stderr, "\n");
850*0a6a1f1dSLionel Sambuc             fprintf(stderr, "\trec->input=");
851*0a6a1f1dSLionel Sambuc             for (ui = 0; ui < l; ui++)
852*0a6a1f1dSLionel Sambuc                 fprintf(stderr, " %02x", rec->input[ui]);
853*0a6a1f1dSLionel Sambuc             fprintf(stderr, "\n");
854ebfedea0SLionel Sambuc         }
855ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
856ebfedea0SLionel Sambuc 
857*0a6a1f1dSLionel Sambuc         if (!send) {
858ebfedea0SLionel Sambuc             if (l == 0 || l % bs != 0)
859ebfedea0SLionel Sambuc                 return 0;
860ebfedea0SLionel Sambuc         }
861ebfedea0SLionel Sambuc 
862ebfedea0SLionel Sambuc         i = EVP_Cipher(ds, rec->data, rec->input, l);
863ebfedea0SLionel Sambuc         if ((EVP_CIPHER_flags(ds->cipher) & EVP_CIPH_FLAG_CUSTOM_CIPHER)
864ebfedea0SLionel Sambuc             ? (i < 0)
865ebfedea0SLionel Sambuc             : (i == 0))
866ebfedea0SLionel Sambuc             return -1;          /* AEAD can fail to verify MAC */
867*0a6a1f1dSLionel Sambuc         if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE && !send) {
868ebfedea0SLionel Sambuc             rec->data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
869ebfedea0SLionel Sambuc             rec->input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
870ebfedea0SLionel Sambuc             rec->length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
871ebfedea0SLionel Sambuc         }
872ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
873ebfedea0SLionel Sambuc         {
874ebfedea0SLionel Sambuc             unsigned long i;
875*0a6a1f1dSLionel Sambuc             fprintf(stderr, "\trec->data=");
876ebfedea0SLionel Sambuc             for (i = 0; i < l; i++)
877*0a6a1f1dSLionel Sambuc                 fprintf(stderr, " %02x", rec->data[i]);
878*0a6a1f1dSLionel Sambuc             fprintf(stderr, "\n");
879ebfedea0SLionel Sambuc         }
880ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
881ebfedea0SLionel Sambuc 
882ebfedea0SLionel Sambuc         ret = 1;
883ebfedea0SLionel Sambuc         if (EVP_MD_CTX_md(s->read_hash) != NULL)
884ebfedea0SLionel Sambuc             mac_size = EVP_MD_CTX_size(s->read_hash);
885ebfedea0SLionel Sambuc         if ((bs != 1) && !send)
886ebfedea0SLionel Sambuc             ret = tls1_cbc_remove_padding(s, rec, bs, mac_size);
887ebfedea0SLionel Sambuc         if (pad && !send)
888ebfedea0SLionel Sambuc             rec->length -= pad;
889ebfedea0SLionel Sambuc     }
890ebfedea0SLionel Sambuc     return ret;
891ebfedea0SLionel Sambuc }
892ebfedea0SLionel Sambuc 
tls1_cert_verify_mac(SSL * s,int md_nid,unsigned char * out)893ebfedea0SLionel Sambuc int tls1_cert_verify_mac(SSL *s, int md_nid, unsigned char *out)
894ebfedea0SLionel Sambuc {
895ebfedea0SLionel Sambuc     unsigned int ret;
896ebfedea0SLionel Sambuc     EVP_MD_CTX ctx, *d = NULL;
897ebfedea0SLionel Sambuc     int i;
898ebfedea0SLionel Sambuc 
899ebfedea0SLionel Sambuc     if (s->s3->handshake_buffer)
900ebfedea0SLionel Sambuc         if (!ssl3_digest_cached_records(s))
901ebfedea0SLionel Sambuc             return 0;
902ebfedea0SLionel Sambuc 
903ebfedea0SLionel Sambuc     if (s->s3->handshake_dgst) {
904*0a6a1f1dSLionel Sambuc 	for (i = 0; i < SSL_MAX_DIGEST; i++) {
905*0a6a1f1dSLionel Sambuc 	    if (s->s3->handshake_dgst[i]
906*0a6a1f1dSLionel Sambuc 		&& EVP_MD_CTX_type(s->s3->handshake_dgst[i]) == md_nid) {
907ebfedea0SLionel Sambuc 		d = s->s3->handshake_dgst[i];
908ebfedea0SLionel Sambuc 		break;
909ebfedea0SLionel Sambuc 	    }
910ebfedea0SLionel Sambuc 	}
911ebfedea0SLionel Sambuc     }
912ebfedea0SLionel Sambuc     if (!d) {
913ebfedea0SLionel Sambuc         SSLerr(SSL_F_TLS1_CERT_VERIFY_MAC, SSL_R_NO_REQUIRED_DIGEST);
914ebfedea0SLionel Sambuc         return 0;
915ebfedea0SLionel Sambuc     }
916ebfedea0SLionel Sambuc 
917ebfedea0SLionel Sambuc     EVP_MD_CTX_init(&ctx);
918ebfedea0SLionel Sambuc     EVP_MD_CTX_copy_ex(&ctx, d);
919ebfedea0SLionel Sambuc     EVP_DigestFinal_ex(&ctx, out, &ret);
920ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(&ctx);
921ebfedea0SLionel Sambuc     return ((int)ret);
922ebfedea0SLionel Sambuc }
923ebfedea0SLionel Sambuc 
tls1_final_finish_mac(SSL * s,const char * str,int slen,unsigned char * out)924ebfedea0SLionel Sambuc int tls1_final_finish_mac(SSL *s,
925ebfedea0SLionel Sambuc                           const char *str, int slen, unsigned char *out)
926ebfedea0SLionel Sambuc {
927ebfedea0SLionel Sambuc     unsigned int i;
928ebfedea0SLionel Sambuc     EVP_MD_CTX ctx;
929ebfedea0SLionel Sambuc     unsigned char buf[2 * EVP_MAX_MD_SIZE];
930ebfedea0SLionel Sambuc     unsigned char *q, buf2[12];
931ebfedea0SLionel Sambuc     int idx;
932ebfedea0SLionel Sambuc     long mask;
933ebfedea0SLionel Sambuc     int err = 0;
934ebfedea0SLionel Sambuc     const EVP_MD *md;
935ebfedea0SLionel Sambuc 
936ebfedea0SLionel Sambuc     q = buf;
937ebfedea0SLionel Sambuc 
938ebfedea0SLionel Sambuc     if (s->s3->handshake_buffer)
939ebfedea0SLionel Sambuc         if (!ssl3_digest_cached_records(s))
940ebfedea0SLionel Sambuc             return 0;
941ebfedea0SLionel Sambuc 
942ebfedea0SLionel Sambuc     EVP_MD_CTX_init(&ctx);
943ebfedea0SLionel Sambuc 
944*0a6a1f1dSLionel Sambuc     for (idx = 0; ssl_get_handshake_digest(idx, &mask, &md); idx++) {
945*0a6a1f1dSLionel Sambuc         if (mask & ssl_get_algorithm2(s)) {
946ebfedea0SLionel Sambuc             int hashsize = EVP_MD_size(md);
947*0a6a1f1dSLionel Sambuc             EVP_MD_CTX *hdgst = s->s3->handshake_dgst[idx];
948*0a6a1f1dSLionel Sambuc             if (!hdgst || hashsize < 0
949*0a6a1f1dSLionel Sambuc                 || hashsize > (int)(sizeof buf - (size_t)(q - buf))) {
950*0a6a1f1dSLionel Sambuc                 /*
951*0a6a1f1dSLionel Sambuc                  * internal error: 'buf' is too small for this cipersuite!
952*0a6a1f1dSLionel Sambuc                  */
953ebfedea0SLionel Sambuc                 err = 1;
954*0a6a1f1dSLionel Sambuc             } else {
955*0a6a1f1dSLionel Sambuc                 if (!EVP_MD_CTX_copy_ex(&ctx, hdgst) ||
956*0a6a1f1dSLionel Sambuc                     !EVP_DigestFinal_ex(&ctx, q, &i) ||
957*0a6a1f1dSLionel Sambuc                     (i != (unsigned int)hashsize))
958ebfedea0SLionel Sambuc                     err = 1;
959*0a6a1f1dSLionel Sambuc                 q += hashsize;
960ebfedea0SLionel Sambuc             }
961ebfedea0SLionel Sambuc         }
962ebfedea0SLionel Sambuc     }
963ebfedea0SLionel Sambuc 
964ebfedea0SLionel Sambuc     if (!tls1_PRF(ssl_get_algorithm2(s),
965ebfedea0SLionel Sambuc                   str, slen, buf, (int)(q - buf), NULL, 0, NULL, 0, NULL, 0,
966ebfedea0SLionel Sambuc                   s->session->master_key, s->session->master_key_length,
967ebfedea0SLionel Sambuc                   out, buf2, sizeof buf2))
968ebfedea0SLionel Sambuc         err = 1;
969ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(&ctx);
970ebfedea0SLionel Sambuc 
971*0a6a1f1dSLionel Sambuc     OPENSSL_cleanse(buf, (int)(q - buf));
972*0a6a1f1dSLionel Sambuc     OPENSSL_cleanse(buf2, sizeof(buf2));
973ebfedea0SLionel Sambuc     if (err)
974ebfedea0SLionel Sambuc         return 0;
975ebfedea0SLionel Sambuc     else
976ebfedea0SLionel Sambuc         return sizeof buf2;
977ebfedea0SLionel Sambuc }
978ebfedea0SLionel Sambuc 
tls1_mac(SSL * ssl,unsigned char * md,int send)979ebfedea0SLionel Sambuc int tls1_mac(SSL *ssl, unsigned char *md, int send)
980ebfedea0SLionel Sambuc {
981ebfedea0SLionel Sambuc     SSL3_RECORD *rec;
982ebfedea0SLionel Sambuc     unsigned char *seq;
983ebfedea0SLionel Sambuc     EVP_MD_CTX *hash;
984ebfedea0SLionel Sambuc     size_t md_size, orig_len;
985ebfedea0SLionel Sambuc     int i;
986ebfedea0SLionel Sambuc     EVP_MD_CTX hmac, *mac_ctx;
987ebfedea0SLionel Sambuc     unsigned char header[13];
988*0a6a1f1dSLionel Sambuc     int stream_mac = (send ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
989*0a6a1f1dSLionel Sambuc                       : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM));
990ebfedea0SLionel Sambuc     int t;
991ebfedea0SLionel Sambuc 
992*0a6a1f1dSLionel Sambuc     if (send) {
993ebfedea0SLionel Sambuc         rec = &(ssl->s3->wrec);
994ebfedea0SLionel Sambuc         seq = &(ssl->s3->write_sequence[0]);
995ebfedea0SLionel Sambuc         hash = ssl->write_hash;
996*0a6a1f1dSLionel Sambuc     } else {
997ebfedea0SLionel Sambuc         rec = &(ssl->s3->rrec);
998ebfedea0SLionel Sambuc         seq = &(ssl->s3->read_sequence[0]);
999ebfedea0SLionel Sambuc         hash = ssl->read_hash;
1000ebfedea0SLionel Sambuc     }
1001ebfedea0SLionel Sambuc 
1002ebfedea0SLionel Sambuc     t = EVP_MD_CTX_size(hash);
1003ebfedea0SLionel Sambuc     OPENSSL_assert(t >= 0);
1004ebfedea0SLionel Sambuc     md_size = t;
1005ebfedea0SLionel Sambuc 
1006ebfedea0SLionel Sambuc     /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
1007*0a6a1f1dSLionel Sambuc     if (stream_mac) {
1008ebfedea0SLionel Sambuc         mac_ctx = hash;
1009*0a6a1f1dSLionel Sambuc     } else {
1010*0a6a1f1dSLionel Sambuc         if (!EVP_MD_CTX_copy(&hmac, hash))
1011*0a6a1f1dSLionel Sambuc             return -1;
1012ebfedea0SLionel Sambuc         mac_ctx = &hmac;
1013ebfedea0SLionel Sambuc     }
1014ebfedea0SLionel Sambuc 
1015*0a6a1f1dSLionel Sambuc     if (ssl->version == DTLS1_VERSION || ssl->version == DTLS1_BAD_VER) {
1016ebfedea0SLionel Sambuc         unsigned char dtlsseq[8], *p = dtlsseq;
1017ebfedea0SLionel Sambuc 
1018ebfedea0SLionel Sambuc         s2n(send ? ssl->d1->w_epoch : ssl->d1->r_epoch, p);
1019ebfedea0SLionel Sambuc         memcpy(p, &seq[2], 6);
1020ebfedea0SLionel Sambuc 
1021ebfedea0SLionel Sambuc         memcpy(header, dtlsseq, 8);
1022*0a6a1f1dSLionel Sambuc     } else
1023ebfedea0SLionel Sambuc         memcpy(header, seq, 8);
1024ebfedea0SLionel Sambuc 
1025*0a6a1f1dSLionel Sambuc     /*
1026*0a6a1f1dSLionel Sambuc      * kludge: tls1_cbc_remove_padding passes padding length in rec->type
1027*0a6a1f1dSLionel Sambuc      */
1028ebfedea0SLionel Sambuc     orig_len = rec->length + md_size + ((unsigned int)rec->type >> 8);
1029ebfedea0SLionel Sambuc     rec->type &= 0xff;
1030ebfedea0SLionel Sambuc 
1031ebfedea0SLionel Sambuc     header[8] = rec->type;
1032ebfedea0SLionel Sambuc     header[9] = (unsigned char)(ssl->version >> 8);
1033ebfedea0SLionel Sambuc     header[10] = (unsigned char)(ssl->version);
1034ebfedea0SLionel Sambuc     header[11] = (rec->length) >> 8;
1035ebfedea0SLionel Sambuc     header[12] = (rec->length) & 0xff;
1036ebfedea0SLionel Sambuc 
1037ebfedea0SLionel Sambuc     if (!send &&
1038ebfedea0SLionel Sambuc         EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1039*0a6a1f1dSLionel Sambuc         ssl3_cbc_record_digest_supported(mac_ctx)) {
1040*0a6a1f1dSLionel Sambuc         /*
1041*0a6a1f1dSLionel Sambuc          * This is a CBC-encrypted record. We must avoid leaking any
1042*0a6a1f1dSLionel Sambuc          * timing-side channel information about how many blocks of data we
1043*0a6a1f1dSLionel Sambuc          * are hashing because that gives an attacker a timing-oracle.
1044*0a6a1f1dSLionel Sambuc          */
1045*0a6a1f1dSLionel Sambuc         /* Final param == not SSLv3 */
1046*0a6a1f1dSLionel Sambuc         ssl3_cbc_digest_record(mac_ctx,
1047ebfedea0SLionel Sambuc                                md, &md_size,
1048ebfedea0SLionel Sambuc                                header, rec->input,
1049ebfedea0SLionel Sambuc                                rec->length + md_size, orig_len,
1050ebfedea0SLionel Sambuc                                ssl->s3->read_mac_secret,
1051*0a6a1f1dSLionel Sambuc                                ssl->s3->read_mac_secret_size, 0);
1052*0a6a1f1dSLionel Sambuc     } else {
1053ebfedea0SLionel Sambuc         EVP_DigestSignUpdate(mac_ctx, header, sizeof(header));
1054ebfedea0SLionel Sambuc         EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length);
1055ebfedea0SLionel Sambuc         t = EVP_DigestSignFinal(mac_ctx, md, &md_size);
1056ebfedea0SLionel Sambuc         OPENSSL_assert(t > 0);
1057ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
1058ebfedea0SLionel Sambuc         if (!send && FIPS_mode())
1059*0a6a1f1dSLionel Sambuc             tls_fips_digest_extra(ssl->enc_read_ctx,
1060*0a6a1f1dSLionel Sambuc                                   mac_ctx, rec->input, rec->length, orig_len);
1061ebfedea0SLionel Sambuc #endif
1062ebfedea0SLionel Sambuc     }
1063ebfedea0SLionel Sambuc 
1064ebfedea0SLionel Sambuc     if (!stream_mac)
1065ebfedea0SLionel Sambuc         EVP_MD_CTX_cleanup(&hmac);
1066ebfedea0SLionel Sambuc #ifdef TLS_DEBUG
1067*0a6a1f1dSLionel Sambuc     fprintf(stderr, "seq=");
1068*0a6a1f1dSLionel Sambuc     {
1069*0a6a1f1dSLionel Sambuc         int z;
1070*0a6a1f1dSLionel Sambuc         for (z = 0; z < 8; z++)
1071*0a6a1f1dSLionel Sambuc             fprintf(stderr, "%02X ", seq[z]);
1072*0a6a1f1dSLionel Sambuc         fprintf(stderr, "\n");
1073*0a6a1f1dSLionel Sambuc     }
1074*0a6a1f1dSLionel Sambuc     fprintf(stderr, "rec=");
1075*0a6a1f1dSLionel Sambuc     {
1076*0a6a1f1dSLionel Sambuc         unsigned int z;
1077*0a6a1f1dSLionel Sambuc         for (z = 0; z < rec->length; z++)
1078*0a6a1f1dSLionel Sambuc             fprintf(stderr, "%02X ", rec->data[z]);
1079*0a6a1f1dSLionel Sambuc         fprintf(stderr, "\n");
1080*0a6a1f1dSLionel Sambuc     }
1081ebfedea0SLionel Sambuc #endif
1082ebfedea0SLionel Sambuc 
1083*0a6a1f1dSLionel Sambuc     if (ssl->version != DTLS1_VERSION && ssl->version != DTLS1_BAD_VER) {
1084*0a6a1f1dSLionel Sambuc         for (i = 7; i >= 0; i--) {
1085ebfedea0SLionel Sambuc             ++seq[i];
1086*0a6a1f1dSLionel Sambuc             if (seq[i] != 0)
1087*0a6a1f1dSLionel Sambuc                 break;
1088ebfedea0SLionel Sambuc         }
1089ebfedea0SLionel Sambuc     }
1090ebfedea0SLionel Sambuc #ifdef TLS_DEBUG
1091*0a6a1f1dSLionel Sambuc     {
1092*0a6a1f1dSLionel Sambuc         unsigned int z;
1093*0a6a1f1dSLionel Sambuc         for (z = 0; z < md_size; z++)
1094*0a6a1f1dSLionel Sambuc             fprintf(stderr, "%02X ", md[z]);
1095*0a6a1f1dSLionel Sambuc         fprintf(stderr, "\n");
1096*0a6a1f1dSLionel Sambuc     }
1097ebfedea0SLionel Sambuc #endif
1098ebfedea0SLionel Sambuc     return (md_size);
1099ebfedea0SLionel Sambuc }
1100ebfedea0SLionel Sambuc 
tls1_generate_master_secret(SSL * s,unsigned char * out,unsigned char * p,int len)1101ebfedea0SLionel Sambuc int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
1102ebfedea0SLionel Sambuc                                 int len)
1103ebfedea0SLionel Sambuc {
1104ebfedea0SLionel Sambuc     unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH];
1105ebfedea0SLionel Sambuc     const void *co = NULL, *so = NULL;
1106ebfedea0SLionel Sambuc     int col = 0, sol = 0;
1107ebfedea0SLionel Sambuc 
1108ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
1109*0a6a1f1dSLionel Sambuc     fprintf(stderr, "tls1_generate_master_secret(%p,%p, %p, %d)\n", s, out, p,
1110*0a6a1f1dSLionel Sambuc             len);
1111ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
1112ebfedea0SLionel Sambuc 
1113ebfedea0SLionel Sambuc #ifdef TLSEXT_TYPE_opaque_prf_input
1114*0a6a1f1dSLionel Sambuc     if (s->s3->client_opaque_prf_input != NULL
1115*0a6a1f1dSLionel Sambuc         && s->s3->server_opaque_prf_input != NULL
1116*0a6a1f1dSLionel Sambuc         && s->s3->client_opaque_prf_input_len > 0
1117*0a6a1f1dSLionel Sambuc         && s->s3->client_opaque_prf_input_len ==
1118*0a6a1f1dSLionel Sambuc         s->s3->server_opaque_prf_input_len) {
1119ebfedea0SLionel Sambuc         co = s->s3->client_opaque_prf_input;
1120ebfedea0SLionel Sambuc         col = s->s3->server_opaque_prf_input_len;
1121ebfedea0SLionel Sambuc         so = s->s3->server_opaque_prf_input;
1122*0a6a1f1dSLionel Sambuc         /*
1123*0a6a1f1dSLionel Sambuc          * must be same as col (see
1124*0a6a1f1dSLionel Sambuc          * draft-resc-00.txts-opaque-prf-input-00.txt, section 3.1)
1125*0a6a1f1dSLionel Sambuc          */
1126*0a6a1f1dSLionel Sambuc         sol = s->s3->client_opaque_prf_input_len;
1127ebfedea0SLionel Sambuc     }
1128ebfedea0SLionel Sambuc #endif
1129ebfedea0SLionel Sambuc 
1130ebfedea0SLionel Sambuc     tls1_PRF(ssl_get_algorithm2(s),
1131ebfedea0SLionel Sambuc              TLS_MD_MASTER_SECRET_CONST, TLS_MD_MASTER_SECRET_CONST_SIZE,
1132ebfedea0SLionel Sambuc              s->s3->client_random, SSL3_RANDOM_SIZE,
1133ebfedea0SLionel Sambuc              co, col,
1134ebfedea0SLionel Sambuc              s->s3->server_random, SSL3_RANDOM_SIZE,
1135*0a6a1f1dSLionel Sambuc              so, sol, p, len, s->session->master_key, buff, sizeof buff);
1136*0a6a1f1dSLionel Sambuc     OPENSSL_cleanse(buff, sizeof buff);
1137ebfedea0SLionel Sambuc #ifdef SSL_DEBUG
1138ebfedea0SLionel Sambuc     fprintf(stderr, "Premaster Secret:\n");
1139ebfedea0SLionel Sambuc     BIO_dump_fp(stderr, (char *)p, len);
1140ebfedea0SLionel Sambuc     fprintf(stderr, "Client Random:\n");
1141ebfedea0SLionel Sambuc     BIO_dump_fp(stderr, (char *)s->s3->client_random, SSL3_RANDOM_SIZE);
1142ebfedea0SLionel Sambuc     fprintf(stderr, "Server Random:\n");
1143ebfedea0SLionel Sambuc     BIO_dump_fp(stderr, (char *)s->s3->server_random, SSL3_RANDOM_SIZE);
1144ebfedea0SLionel Sambuc     fprintf(stderr, "Master Secret:\n");
1145*0a6a1f1dSLionel Sambuc     BIO_dump_fp(stderr, (char *)s->session->master_key,
1146*0a6a1f1dSLionel Sambuc                 SSL3_MASTER_SECRET_SIZE);
1147ebfedea0SLionel Sambuc #endif
1148ebfedea0SLionel Sambuc 
1149ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
1150*0a6a1f1dSLionel Sambuc     fprintf(stderr, "tls1_generate_master_secret() complete\n");
1151ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
1152ebfedea0SLionel Sambuc     return (SSL3_MASTER_SECRET_SIZE);
1153ebfedea0SLionel Sambuc }
1154ebfedea0SLionel Sambuc 
tls1_export_keying_material(SSL * s,unsigned char * out,size_t olen,const char * label,size_t llen,const unsigned char * context,size_t contextlen,int use_context)1155ebfedea0SLionel Sambuc int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1156*0a6a1f1dSLionel Sambuc                                 const char *label, size_t llen,
1157*0a6a1f1dSLionel Sambuc                                 const unsigned char *context,
1158ebfedea0SLionel Sambuc                                 size_t contextlen, int use_context)
1159ebfedea0SLionel Sambuc {
1160ebfedea0SLionel Sambuc     unsigned char *buff;
1161ebfedea0SLionel Sambuc     unsigned char *val = NULL;
1162ebfedea0SLionel Sambuc     size_t vallen, currentvalpos;
1163ebfedea0SLionel Sambuc     int rv;
1164ebfedea0SLionel Sambuc 
1165ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
1166*0a6a1f1dSLionel Sambuc     fprintf(stderr, "tls1_export_keying_material(%p,%p,%lu,%s,%lu,%p,%lu)\n",
1167*0a6a1f1dSLionel Sambuc             s, out, olen, label, llen, context, contextlen);
1168ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
1169ebfedea0SLionel Sambuc 
1170ebfedea0SLionel Sambuc     buff = OPENSSL_malloc(olen);
1171*0a6a1f1dSLionel Sambuc     if (buff == NULL)
1172*0a6a1f1dSLionel Sambuc         goto err2;
1173ebfedea0SLionel Sambuc 
1174*0a6a1f1dSLionel Sambuc     /*
1175*0a6a1f1dSLionel Sambuc      * construct PRF arguments we construct the PRF argument ourself rather
1176*0a6a1f1dSLionel Sambuc      * than passing separate values into the TLS PRF to ensure that the
1177*0a6a1f1dSLionel Sambuc      * concatenation of values does not create a prohibited label.
1178ebfedea0SLionel Sambuc      */
1179ebfedea0SLionel Sambuc     vallen = llen + SSL3_RANDOM_SIZE * 2;
1180*0a6a1f1dSLionel Sambuc     if (use_context) {
1181ebfedea0SLionel Sambuc         vallen += 2 + contextlen;
1182ebfedea0SLionel Sambuc     }
1183ebfedea0SLionel Sambuc 
1184ebfedea0SLionel Sambuc     val = OPENSSL_malloc(vallen);
1185*0a6a1f1dSLionel Sambuc     if (val == NULL)
1186*0a6a1f1dSLionel Sambuc         goto err2;
1187ebfedea0SLionel Sambuc     currentvalpos = 0;
1188ebfedea0SLionel Sambuc     memcpy(val + currentvalpos, (unsigned char *)label, llen);
1189ebfedea0SLionel Sambuc     currentvalpos += llen;
1190ebfedea0SLionel Sambuc     memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE);
1191ebfedea0SLionel Sambuc     currentvalpos += SSL3_RANDOM_SIZE;
1192ebfedea0SLionel Sambuc     memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE);
1193ebfedea0SLionel Sambuc     currentvalpos += SSL3_RANDOM_SIZE;
1194ebfedea0SLionel Sambuc 
1195*0a6a1f1dSLionel Sambuc     if (use_context) {
1196ebfedea0SLionel Sambuc         val[currentvalpos] = (contextlen >> 8) & 0xff;
1197ebfedea0SLionel Sambuc         currentvalpos++;
1198ebfedea0SLionel Sambuc         val[currentvalpos] = contextlen & 0xff;
1199ebfedea0SLionel Sambuc         currentvalpos++;
1200*0a6a1f1dSLionel Sambuc         if ((contextlen > 0) || (context != NULL)) {
1201ebfedea0SLionel Sambuc             memcpy(val + currentvalpos, context, contextlen);
1202ebfedea0SLionel Sambuc         }
1203ebfedea0SLionel Sambuc     }
1204ebfedea0SLionel Sambuc 
1205*0a6a1f1dSLionel Sambuc     /*
1206*0a6a1f1dSLionel Sambuc      * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
1207*0a6a1f1dSLionel Sambuc      * label len) = 15, so size of val > max(prohibited label len) = 15 and
1208*0a6a1f1dSLionel Sambuc      * the comparisons won't have buffer overflow
1209ebfedea0SLionel Sambuc      */
1210ebfedea0SLionel Sambuc     if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
1211*0a6a1f1dSLionel Sambuc                TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
1212*0a6a1f1dSLionel Sambuc         goto err1;
1213ebfedea0SLionel Sambuc     if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
1214*0a6a1f1dSLionel Sambuc                TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
1215*0a6a1f1dSLionel Sambuc         goto err1;
1216ebfedea0SLionel Sambuc     if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
1217*0a6a1f1dSLionel Sambuc                TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
1218*0a6a1f1dSLionel Sambuc         goto err1;
1219ebfedea0SLionel Sambuc     if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
1220*0a6a1f1dSLionel Sambuc                TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
1221*0a6a1f1dSLionel Sambuc         goto err1;
1222ebfedea0SLionel Sambuc 
1223*0a6a1f1dSLionel Sambuc     rv = tls1_PRF(ssl_get_algorithm2(s),
1224ebfedea0SLionel Sambuc                   val, vallen,
1225ebfedea0SLionel Sambuc                   NULL, 0,
1226ebfedea0SLionel Sambuc                   NULL, 0,
1227ebfedea0SLionel Sambuc                   NULL, 0,
1228ebfedea0SLionel Sambuc                   NULL, 0,
1229ebfedea0SLionel Sambuc                   s->session->master_key, s->session->master_key_length,
1230ebfedea0SLionel Sambuc                   out, buff, olen);
1231*0a6a1f1dSLionel Sambuc     OPENSSL_cleanse(val, vallen);
1232*0a6a1f1dSLionel Sambuc     OPENSSL_cleanse(buff, olen);
1233ebfedea0SLionel Sambuc 
1234ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
1235*0a6a1f1dSLionel Sambuc     fprintf(stderr, "tls1_export_keying_material() complete\n");
1236ebfedea0SLionel Sambuc #endif                          /* KSSL_DEBUG */
1237ebfedea0SLionel Sambuc     goto ret;
1238ebfedea0SLionel Sambuc  err1:
1239*0a6a1f1dSLionel Sambuc     SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL,
1240*0a6a1f1dSLionel Sambuc            SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
1241ebfedea0SLionel Sambuc     rv = 0;
1242ebfedea0SLionel Sambuc     goto ret;
1243ebfedea0SLionel Sambuc  err2:
1244ebfedea0SLionel Sambuc     SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE);
1245ebfedea0SLionel Sambuc     rv = 0;
1246ebfedea0SLionel Sambuc  ret:
1247*0a6a1f1dSLionel Sambuc     if (buff != NULL)
1248*0a6a1f1dSLionel Sambuc         OPENSSL_free(buff);
1249*0a6a1f1dSLionel Sambuc     if (val != NULL)
1250*0a6a1f1dSLionel Sambuc         OPENSSL_free(val);
1251ebfedea0SLionel Sambuc     return (rv);
1252ebfedea0SLionel Sambuc }
1253ebfedea0SLionel Sambuc 
tls1_alert_code(int code)1254ebfedea0SLionel Sambuc int tls1_alert_code(int code)
1255ebfedea0SLionel Sambuc {
1256*0a6a1f1dSLionel Sambuc     switch (code) {
1257*0a6a1f1dSLionel Sambuc     case SSL_AD_CLOSE_NOTIFY:
1258*0a6a1f1dSLionel Sambuc         return (SSL3_AD_CLOSE_NOTIFY);
1259*0a6a1f1dSLionel Sambuc     case SSL_AD_UNEXPECTED_MESSAGE:
1260*0a6a1f1dSLionel Sambuc         return (SSL3_AD_UNEXPECTED_MESSAGE);
1261*0a6a1f1dSLionel Sambuc     case SSL_AD_BAD_RECORD_MAC:
1262*0a6a1f1dSLionel Sambuc         return (SSL3_AD_BAD_RECORD_MAC);
1263*0a6a1f1dSLionel Sambuc     case SSL_AD_DECRYPTION_FAILED:
1264*0a6a1f1dSLionel Sambuc         return (TLS1_AD_DECRYPTION_FAILED);
1265*0a6a1f1dSLionel Sambuc     case SSL_AD_RECORD_OVERFLOW:
1266*0a6a1f1dSLionel Sambuc         return (TLS1_AD_RECORD_OVERFLOW);
1267*0a6a1f1dSLionel Sambuc     case SSL_AD_DECOMPRESSION_FAILURE:
1268*0a6a1f1dSLionel Sambuc         return (SSL3_AD_DECOMPRESSION_FAILURE);
1269*0a6a1f1dSLionel Sambuc     case SSL_AD_HANDSHAKE_FAILURE:
1270*0a6a1f1dSLionel Sambuc         return (SSL3_AD_HANDSHAKE_FAILURE);
1271*0a6a1f1dSLionel Sambuc     case SSL_AD_NO_CERTIFICATE:
1272*0a6a1f1dSLionel Sambuc         return (-1);
1273*0a6a1f1dSLionel Sambuc     case SSL_AD_BAD_CERTIFICATE:
1274*0a6a1f1dSLionel Sambuc         return (SSL3_AD_BAD_CERTIFICATE);
1275*0a6a1f1dSLionel Sambuc     case SSL_AD_UNSUPPORTED_CERTIFICATE:
1276*0a6a1f1dSLionel Sambuc         return (SSL3_AD_UNSUPPORTED_CERTIFICATE);
1277*0a6a1f1dSLionel Sambuc     case SSL_AD_CERTIFICATE_REVOKED:
1278*0a6a1f1dSLionel Sambuc         return (SSL3_AD_CERTIFICATE_REVOKED);
1279*0a6a1f1dSLionel Sambuc     case SSL_AD_CERTIFICATE_EXPIRED:
1280*0a6a1f1dSLionel Sambuc         return (SSL3_AD_CERTIFICATE_EXPIRED);
1281*0a6a1f1dSLionel Sambuc     case SSL_AD_CERTIFICATE_UNKNOWN:
1282*0a6a1f1dSLionel Sambuc         return (SSL3_AD_CERTIFICATE_UNKNOWN);
1283*0a6a1f1dSLionel Sambuc     case SSL_AD_ILLEGAL_PARAMETER:
1284*0a6a1f1dSLionel Sambuc         return (SSL3_AD_ILLEGAL_PARAMETER);
1285*0a6a1f1dSLionel Sambuc     case SSL_AD_UNKNOWN_CA:
1286*0a6a1f1dSLionel Sambuc         return (TLS1_AD_UNKNOWN_CA);
1287*0a6a1f1dSLionel Sambuc     case SSL_AD_ACCESS_DENIED:
1288*0a6a1f1dSLionel Sambuc         return (TLS1_AD_ACCESS_DENIED);
1289*0a6a1f1dSLionel Sambuc     case SSL_AD_DECODE_ERROR:
1290*0a6a1f1dSLionel Sambuc         return (TLS1_AD_DECODE_ERROR);
1291*0a6a1f1dSLionel Sambuc     case SSL_AD_DECRYPT_ERROR:
1292*0a6a1f1dSLionel Sambuc         return (TLS1_AD_DECRYPT_ERROR);
1293*0a6a1f1dSLionel Sambuc     case SSL_AD_EXPORT_RESTRICTION:
1294*0a6a1f1dSLionel Sambuc         return (TLS1_AD_EXPORT_RESTRICTION);
1295*0a6a1f1dSLionel Sambuc     case SSL_AD_PROTOCOL_VERSION:
1296*0a6a1f1dSLionel Sambuc         return (TLS1_AD_PROTOCOL_VERSION);
1297*0a6a1f1dSLionel Sambuc     case SSL_AD_INSUFFICIENT_SECURITY:
1298*0a6a1f1dSLionel Sambuc         return (TLS1_AD_INSUFFICIENT_SECURITY);
1299*0a6a1f1dSLionel Sambuc     case SSL_AD_INTERNAL_ERROR:
1300*0a6a1f1dSLionel Sambuc         return (TLS1_AD_INTERNAL_ERROR);
1301*0a6a1f1dSLionel Sambuc     case SSL_AD_USER_CANCELLED:
1302*0a6a1f1dSLionel Sambuc         return (TLS1_AD_USER_CANCELLED);
1303*0a6a1f1dSLionel Sambuc     case SSL_AD_NO_RENEGOTIATION:
1304*0a6a1f1dSLionel Sambuc         return (TLS1_AD_NO_RENEGOTIATION);
1305*0a6a1f1dSLionel Sambuc     case SSL_AD_UNSUPPORTED_EXTENSION:
1306*0a6a1f1dSLionel Sambuc         return (TLS1_AD_UNSUPPORTED_EXTENSION);
1307*0a6a1f1dSLionel Sambuc     case SSL_AD_CERTIFICATE_UNOBTAINABLE:
1308*0a6a1f1dSLionel Sambuc         return (TLS1_AD_CERTIFICATE_UNOBTAINABLE);
1309*0a6a1f1dSLionel Sambuc     case SSL_AD_UNRECOGNIZED_NAME:
1310*0a6a1f1dSLionel Sambuc         return (TLS1_AD_UNRECOGNIZED_NAME);
1311*0a6a1f1dSLionel Sambuc     case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
1312*0a6a1f1dSLionel Sambuc         return (TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
1313*0a6a1f1dSLionel Sambuc     case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
1314*0a6a1f1dSLionel Sambuc         return (TLS1_AD_BAD_CERTIFICATE_HASH_VALUE);
1315*0a6a1f1dSLionel Sambuc     case SSL_AD_UNKNOWN_PSK_IDENTITY:
1316*0a6a1f1dSLionel Sambuc         return (TLS1_AD_UNKNOWN_PSK_IDENTITY);
1317*0a6a1f1dSLionel Sambuc     case SSL_AD_INAPPROPRIATE_FALLBACK:
1318*0a6a1f1dSLionel Sambuc         return (TLS1_AD_INAPPROPRIATE_FALLBACK);
1319*0a6a1f1dSLionel Sambuc #if 0
1320*0a6a1f1dSLionel Sambuc         /* not appropriate for TLS, not used for DTLS */
1321*0a6a1f1dSLionel Sambuc     case DTLS1_AD_MISSING_HANDSHAKE_MESSAGE:
1322*0a6a1f1dSLionel Sambuc         return (DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
1323ebfedea0SLionel Sambuc #endif
1324*0a6a1f1dSLionel Sambuc     default:
1325*0a6a1f1dSLionel Sambuc         return (-1);
1326ebfedea0SLionel Sambuc     }
1327ebfedea0SLionel Sambuc }
1328