s3_enc.c revision 1.1 1 1.1 christos /* ssl/s3_enc.c */
2 1.1 christos /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
3 1.1 christos * All rights reserved.
4 1.1 christos *
5 1.1 christos * This package is an SSL implementation written
6 1.1 christos * by Eric Young (eay (at) cryptsoft.com).
7 1.1 christos * The implementation was written so as to conform with Netscapes SSL.
8 1.1 christos *
9 1.1 christos * This library is free for commercial and non-commercial use as long as
10 1.1 christos * the following conditions are aheared to. The following conditions
11 1.1 christos * apply to all code found in this distribution, be it the RC4, RSA,
12 1.1 christos * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 1.1 christos * included with this distribution is covered by the same copyright terms
14 1.1 christos * except that the holder is Tim Hudson (tjh (at) cryptsoft.com).
15 1.1 christos *
16 1.1 christos * Copyright remains Eric Young's, and as such any Copyright notices in
17 1.1 christos * the code are not to be removed.
18 1.1 christos * If this package is used in a product, Eric Young should be given attribution
19 1.1 christos * as the author of the parts of the library used.
20 1.1 christos * This can be in the form of a textual message at program startup or
21 1.1 christos * in documentation (online or textual) provided with the package.
22 1.1 christos *
23 1.1 christos * Redistribution and use in source and binary forms, with or without
24 1.1 christos * modification, are permitted provided that the following conditions
25 1.1 christos * are met:
26 1.1 christos * 1. Redistributions of source code must retain the copyright
27 1.1 christos * notice, this list of conditions and the following disclaimer.
28 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
29 1.1 christos * notice, this list of conditions and the following disclaimer in the
30 1.1 christos * documentation and/or other materials provided with the distribution.
31 1.1 christos * 3. All advertising materials mentioning features or use of this software
32 1.1 christos * must display the following acknowledgement:
33 1.1 christos * "This product includes cryptographic software written by
34 1.1 christos * Eric Young (eay (at) cryptsoft.com)"
35 1.1 christos * The word 'cryptographic' can be left out if the rouines from the library
36 1.1 christos * being used are not cryptographic related :-).
37 1.1 christos * 4. If you include any Windows specific code (or a derivative thereof) from
38 1.1 christos * the apps directory (application code) you must include an acknowledgement:
39 1.1 christos * "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)"
40 1.1 christos *
41 1.1 christos * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 1.1 christos * SUCH DAMAGE.
52 1.1 christos *
53 1.1 christos * The licence and distribution terms for any publically available version or
54 1.1 christos * derivative of this code cannot be changed. i.e. this code cannot simply be
55 1.1 christos * copied and put under another distribution licence
56 1.1 christos * [including the GNU Public Licence.]
57 1.1 christos */
58 1.1 christos /* ====================================================================
59 1.1 christos * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60 1.1 christos *
61 1.1 christos * Redistribution and use in source and binary forms, with or without
62 1.1 christos * modification, are permitted provided that the following conditions
63 1.1 christos * are met:
64 1.1 christos *
65 1.1 christos * 1. Redistributions of source code must retain the above copyright
66 1.1 christos * notice, this list of conditions and the following disclaimer.
67 1.1 christos *
68 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
69 1.1 christos * notice, this list of conditions and the following disclaimer in
70 1.1 christos * the documentation and/or other materials provided with the
71 1.1 christos * distribution.
72 1.1 christos *
73 1.1 christos * 3. All advertising materials mentioning features or use of this
74 1.1 christos * software must display the following acknowledgment:
75 1.1 christos * "This product includes software developed by the OpenSSL Project
76 1.1 christos * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 1.1 christos *
78 1.1 christos * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 1.1 christos * endorse or promote products derived from this software without
80 1.1 christos * prior written permission. For written permission, please contact
81 1.1 christos * openssl-core (at) openssl.org.
82 1.1 christos *
83 1.1 christos * 5. Products derived from this software may not be called "OpenSSL"
84 1.1 christos * nor may "OpenSSL" appear in their names without prior written
85 1.1 christos * permission of the OpenSSL Project.
86 1.1 christos *
87 1.1 christos * 6. Redistributions of any form whatsoever must retain the following
88 1.1 christos * acknowledgment:
89 1.1 christos * "This product includes software developed by the OpenSSL Project
90 1.1 christos * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 1.1 christos *
92 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 1.1 christos * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 1.1 christos * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 1.1 christos * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 1.1 christos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 1.1 christos * OF THE POSSIBILITY OF SUCH DAMAGE.
104 1.1 christos * ====================================================================
105 1.1 christos *
106 1.1 christos * This product includes cryptographic software written by Eric Young
107 1.1 christos * (eay (at) cryptsoft.com). This product includes software written by Tim
108 1.1 christos * Hudson (tjh (at) cryptsoft.com).
109 1.1 christos *
110 1.1 christos */
111 1.1 christos /* ====================================================================
112 1.1 christos * Copyright 2005 Nokia. All rights reserved.
113 1.1 christos *
114 1.1 christos * The portions of the attached software ("Contribution") is developed by
115 1.1 christos * Nokia Corporation and is licensed pursuant to the OpenSSL open source
116 1.1 christos * license.
117 1.1 christos *
118 1.1 christos * The Contribution, originally written by Mika Kousa and Pasi Eronen of
119 1.1 christos * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
120 1.1 christos * support (see RFC 4279) to OpenSSL.
121 1.1 christos *
122 1.1 christos * No patent licenses or other rights except those expressly stated in
123 1.1 christos * the OpenSSL open source license shall be deemed granted or received
124 1.1 christos * expressly, by implication, estoppel, or otherwise.
125 1.1 christos *
126 1.1 christos * No assurances are provided by Nokia that the Contribution does not
127 1.1 christos * infringe the patent or other intellectual property rights of any third
128 1.1 christos * party or that the license provides you with all the necessary rights
129 1.1 christos * to make use of the Contribution.
130 1.1 christos *
131 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
132 1.1 christos * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
133 1.1 christos * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
134 1.1 christos * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
135 1.1 christos * OTHERWISE.
136 1.1 christos */
137 1.1 christos
138 1.1 christos #include <stdio.h>
139 1.1 christos #include "ssl_locl.h"
140 1.1 christos #include <openssl/evp.h>
141 1.1 christos #include <openssl/md5.h>
142 1.1 christos
143 1.1 christos static unsigned char ssl3_pad_1[48] = {
144 1.1 christos 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
145 1.1 christos 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
146 1.1 christos 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
147 1.1 christos 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
148 1.1 christos 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
149 1.1 christos 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36
150 1.1 christos };
151 1.1 christos
152 1.1 christos static unsigned char ssl3_pad_2[48] = {
153 1.1 christos 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
154 1.1 christos 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
155 1.1 christos 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
156 1.1 christos 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
157 1.1 christos 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
158 1.1 christos 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c
159 1.1 christos };
160 1.1 christos
161 1.1 christos static int ssl3_handshake_mac(SSL *s, int md_nid,
162 1.1 christos const char *sender, int len, unsigned char *p);
163 1.1 christos static int ssl3_generate_key_block(SSL *s, unsigned char *km, int num)
164 1.1 christos {
165 1.1 christos EVP_MD_CTX m5;
166 1.1 christos EVP_MD_CTX s1;
167 1.1 christos unsigned char buf[16], smd[SHA_DIGEST_LENGTH];
168 1.1 christos unsigned char c = 'A';
169 1.1 christos unsigned int i, j, k;
170 1.1 christos
171 1.1 christos #ifdef CHARSET_EBCDIC
172 1.1 christos c = os_toascii[c]; /* 'A' in ASCII */
173 1.1 christos #endif
174 1.1 christos k = 0;
175 1.1 christos EVP_MD_CTX_init(&m5);
176 1.1 christos EVP_MD_CTX_set_flags(&m5, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
177 1.1 christos EVP_MD_CTX_init(&s1);
178 1.1 christos for (i = 0; (int)i < num; i += MD5_DIGEST_LENGTH) {
179 1.1 christos k++;
180 1.1 christos if (k > sizeof buf) {
181 1.1 christos /* bug: 'buf' is too small for this ciphersuite */
182 1.1 christos SSLerr(SSL_F_SSL3_GENERATE_KEY_BLOCK, ERR_R_INTERNAL_ERROR);
183 1.1 christos return 0;
184 1.1 christos }
185 1.1 christos
186 1.1 christos for (j = 0; j < k; j++)
187 1.1 christos buf[j] = c;
188 1.1 christos c++;
189 1.1 christos EVP_DigestInit_ex(&s1, EVP_sha1(), NULL);
190 1.1 christos EVP_DigestUpdate(&s1, buf, k);
191 1.1 christos EVP_DigestUpdate(&s1, s->session->master_key,
192 1.1 christos s->session->master_key_length);
193 1.1 christos EVP_DigestUpdate(&s1, s->s3->server_random, SSL3_RANDOM_SIZE);
194 1.1 christos EVP_DigestUpdate(&s1, s->s3->client_random, SSL3_RANDOM_SIZE);
195 1.1 christos EVP_DigestFinal_ex(&s1, smd, NULL);
196 1.1 christos
197 1.1 christos EVP_DigestInit_ex(&m5, EVP_md5(), NULL);
198 1.1 christos EVP_DigestUpdate(&m5, s->session->master_key,
199 1.1 christos s->session->master_key_length);
200 1.1 christos EVP_DigestUpdate(&m5, smd, SHA_DIGEST_LENGTH);
201 1.1 christos if ((int)(i + MD5_DIGEST_LENGTH) > num) {
202 1.1 christos EVP_DigestFinal_ex(&m5, smd, NULL);
203 1.1 christos memcpy(km, smd, (num - i));
204 1.1 christos } else
205 1.1 christos EVP_DigestFinal_ex(&m5, km, NULL);
206 1.1 christos
207 1.1 christos km += MD5_DIGEST_LENGTH;
208 1.1 christos }
209 1.1 christos OPENSSL_cleanse(smd, SHA_DIGEST_LENGTH);
210 1.1 christos EVP_MD_CTX_cleanup(&m5);
211 1.1 christos EVP_MD_CTX_cleanup(&s1);
212 1.1 christos return 1;
213 1.1 christos }
214 1.1 christos
215 1.1 christos int ssl3_change_cipher_state(SSL *s, int which)
216 1.1 christos {
217 1.1 christos unsigned char *p, *mac_secret;
218 1.1 christos unsigned char exp_key[EVP_MAX_KEY_LENGTH];
219 1.1 christos unsigned char exp_iv[EVP_MAX_IV_LENGTH];
220 1.1 christos unsigned char *ms, *key, *iv, *er1, *er2;
221 1.1 christos EVP_CIPHER_CTX *dd;
222 1.1 christos const EVP_CIPHER *c;
223 1.1 christos #ifndef OPENSSL_NO_COMP
224 1.1 christos COMP_METHOD *comp;
225 1.1 christos #endif
226 1.1 christos const EVP_MD *m;
227 1.1 christos EVP_MD_CTX md;
228 1.1 christos int is_exp, n, i, j, k, cl;
229 1.1 christos int reuse_dd = 0;
230 1.1 christos
231 1.1 christos is_exp = SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
232 1.1 christos c = s->s3->tmp.new_sym_enc;
233 1.1 christos m = s->s3->tmp.new_hash;
234 1.1 christos /* m == NULL will lead to a crash later */
235 1.1 christos OPENSSL_assert(m);
236 1.1 christos #ifndef OPENSSL_NO_COMP
237 1.1 christos if (s->s3->tmp.new_compression == NULL)
238 1.1 christos comp = NULL;
239 1.1 christos else
240 1.1 christos comp = s->s3->tmp.new_compression->method;
241 1.1 christos #endif
242 1.1 christos
243 1.1 christos if (which & SSL3_CC_READ) {
244 1.1 christos if (s->enc_read_ctx != NULL)
245 1.1 christos reuse_dd = 1;
246 1.1 christos else if ((s->enc_read_ctx =
247 1.1 christos OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)
248 1.1 christos goto err;
249 1.1 christos else
250 1.1 christos /*
251 1.1 christos * make sure it's intialized in case we exit later with an error
252 1.1 christos */
253 1.1 christos EVP_CIPHER_CTX_init(s->enc_read_ctx);
254 1.1 christos dd = s->enc_read_ctx;
255 1.1 christos
256 1.1 christos if (ssl_replace_hash(&s->read_hash, m) == NULL) {
257 1.1 christos SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
258 1.1 christos goto err2;
259 1.1 christos }
260 1.1 christos #ifndef OPENSSL_NO_COMP
261 1.1 christos /* COMPRESS */
262 1.1 christos if (s->expand != NULL) {
263 1.1 christos COMP_CTX_free(s->expand);
264 1.1 christos s->expand = NULL;
265 1.1 christos }
266 1.1 christos if (comp != NULL) {
267 1.1 christos s->expand = COMP_CTX_new(comp);
268 1.1 christos if (s->expand == NULL) {
269 1.1 christos SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE,
270 1.1 christos SSL_R_COMPRESSION_LIBRARY_ERROR);
271 1.1 christos goto err2;
272 1.1 christos }
273 1.1 christos if (s->s3->rrec.comp == NULL)
274 1.1 christos s->s3->rrec.comp = (unsigned char *)
275 1.1 christos OPENSSL_malloc(SSL3_RT_MAX_PLAIN_LENGTH);
276 1.1 christos if (s->s3->rrec.comp == NULL)
277 1.1 christos goto err;
278 1.1 christos }
279 1.1 christos #endif
280 1.1 christos memset(&(s->s3->read_sequence[0]), 0, 8);
281 1.1 christos mac_secret = &(s->s3->read_mac_secret[0]);
282 1.1 christos } else {
283 1.1 christos if (s->enc_write_ctx != NULL)
284 1.1 christos reuse_dd = 1;
285 1.1 christos else if ((s->enc_write_ctx =
286 1.1 christos OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)
287 1.1 christos goto err;
288 1.1 christos else
289 1.1 christos /*
290 1.1 christos * make sure it's intialized in case we exit later with an error
291 1.1 christos */
292 1.1 christos EVP_CIPHER_CTX_init(s->enc_write_ctx);
293 1.1 christos dd = s->enc_write_ctx;
294 1.1 christos if (ssl_replace_hash(&s->write_hash, m) == NULL) {
295 1.1 christos SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
296 1.1 christos goto err2;
297 1.1 christos }
298 1.1 christos #ifndef OPENSSL_NO_COMP
299 1.1 christos /* COMPRESS */
300 1.1 christos if (s->compress != NULL) {
301 1.1 christos COMP_CTX_free(s->compress);
302 1.1 christos s->compress = NULL;
303 1.1 christos }
304 1.1 christos if (comp != NULL) {
305 1.1 christos s->compress = COMP_CTX_new(comp);
306 1.1 christos if (s->compress == NULL) {
307 1.1 christos SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE,
308 1.1 christos SSL_R_COMPRESSION_LIBRARY_ERROR);
309 1.1 christos goto err2;
310 1.1 christos }
311 1.1 christos }
312 1.1 christos #endif
313 1.1 christos memset(&(s->s3->write_sequence[0]), 0, 8);
314 1.1 christos mac_secret = &(s->s3->write_mac_secret[0]);
315 1.1 christos }
316 1.1 christos
317 1.1 christos if (reuse_dd)
318 1.1 christos EVP_CIPHER_CTX_cleanup(dd);
319 1.1 christos
320 1.1 christos p = s->s3->tmp.key_block;
321 1.1 christos i = EVP_MD_size(m);
322 1.1 christos if (i < 0)
323 1.1 christos goto err2;
324 1.1 christos cl = EVP_CIPHER_key_length(c);
325 1.1 christos j = is_exp ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ?
326 1.1 christos cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;
327 1.1 christos /* Was j=(is_exp)?5:EVP_CIPHER_key_length(c); */
328 1.1 christos k = EVP_CIPHER_iv_length(c);
329 1.1 christos if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
330 1.1 christos (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
331 1.1 christos ms = &(p[0]);
332 1.1 christos n = i + i;
333 1.1 christos key = &(p[n]);
334 1.1 christos n += j + j;
335 1.1 christos iv = &(p[n]);
336 1.1 christos n += k + k;
337 1.1 christos er1 = &(s->s3->client_random[0]);
338 1.1 christos er2 = &(s->s3->server_random[0]);
339 1.1 christos } else {
340 1.1 christos n = i;
341 1.1 christos ms = &(p[n]);
342 1.1 christos n += i + j;
343 1.1 christos key = &(p[n]);
344 1.1 christos n += j + k;
345 1.1 christos iv = &(p[n]);
346 1.1 christos n += k;
347 1.1 christos er1 = &(s->s3->server_random[0]);
348 1.1 christos er2 = &(s->s3->client_random[0]);
349 1.1 christos }
350 1.1 christos
351 1.1 christos if (n > s->s3->tmp.key_block_length) {
352 1.1 christos SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
353 1.1 christos goto err2;
354 1.1 christos }
355 1.1 christos
356 1.1 christos EVP_MD_CTX_init(&md);
357 1.1 christos memcpy(mac_secret, ms, i);
358 1.1 christos if (is_exp) {
359 1.1 christos /*
360 1.1 christos * In here I set both the read and write key/iv to the same value
361 1.1 christos * since only the correct one will be used :-).
362 1.1 christos */
363 1.1 christos EVP_DigestInit_ex(&md, EVP_md5(), NULL);
364 1.1 christos EVP_DigestUpdate(&md, key, j);
365 1.1 christos EVP_DigestUpdate(&md, er1, SSL3_RANDOM_SIZE);
366 1.1 christos EVP_DigestUpdate(&md, er2, SSL3_RANDOM_SIZE);
367 1.1 christos EVP_DigestFinal_ex(&md, &(exp_key[0]), NULL);
368 1.1 christos key = &(exp_key[0]);
369 1.1 christos
370 1.1 christos if (k > 0) {
371 1.1 christos EVP_DigestInit_ex(&md, EVP_md5(), NULL);
372 1.1 christos EVP_DigestUpdate(&md, er1, SSL3_RANDOM_SIZE);
373 1.1 christos EVP_DigestUpdate(&md, er2, SSL3_RANDOM_SIZE);
374 1.1 christos EVP_DigestFinal_ex(&md, &(exp_iv[0]), NULL);
375 1.1 christos iv = &(exp_iv[0]);
376 1.1 christos }
377 1.1 christos }
378 1.1 christos
379 1.1 christos s->session->key_arg_length = 0;
380 1.1 christos
381 1.1 christos EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE));
382 1.1 christos
383 1.1 christos #ifdef OPENSSL_SSL_TRACE_CRYPTO
384 1.1 christos if (s->msg_callback) {
385 1.1 christos
386 1.1 christos int wh = which & SSL3_CC_WRITE ?
387 1.1 christos TLS1_RT_CRYPTO_WRITE : TLS1_RT_CRYPTO_READ;
388 1.1 christos s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_MAC,
389 1.1 christos mac_secret, EVP_MD_size(m), s, s->msg_callback_arg);
390 1.1 christos if (c->key_len)
391 1.1 christos s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_KEY,
392 1.1 christos key, c->key_len, s, s->msg_callback_arg);
393 1.1 christos if (k) {
394 1.1 christos s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_IV,
395 1.1 christos iv, k, s, s->msg_callback_arg);
396 1.1 christos }
397 1.1 christos }
398 1.1 christos #endif
399 1.1 christos
400 1.1 christos OPENSSL_cleanse(&(exp_key[0]), sizeof(exp_key));
401 1.1 christos OPENSSL_cleanse(&(exp_iv[0]), sizeof(exp_iv));
402 1.1 christos EVP_MD_CTX_cleanup(&md);
403 1.1 christos return (1);
404 1.1 christos err:
405 1.1 christos SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
406 1.1 christos err2:
407 1.1 christos return (0);
408 1.1 christos }
409 1.1 christos
410 1.1 christos int ssl3_setup_key_block(SSL *s)
411 1.1 christos {
412 1.1 christos unsigned char *p;
413 1.1 christos const EVP_CIPHER *c;
414 1.1 christos const EVP_MD *hash;
415 1.1 christos int num;
416 1.1 christos int ret = 0;
417 1.1 christos SSL_COMP *comp;
418 1.1 christos
419 1.1 christos if (s->s3->tmp.key_block_length != 0)
420 1.1 christos return (1);
421 1.1 christos
422 1.1 christos if (!ssl_cipher_get_evp(s->session, &c, &hash, NULL, NULL, &comp)) {
423 1.1 christos SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
424 1.1 christos return (0);
425 1.1 christos }
426 1.1 christos
427 1.1 christos s->s3->tmp.new_sym_enc = c;
428 1.1 christos s->s3->tmp.new_hash = hash;
429 1.1 christos #ifdef OPENSSL_NO_COMP
430 1.1 christos s->s3->tmp.new_compression = NULL;
431 1.1 christos #else
432 1.1 christos s->s3->tmp.new_compression = comp;
433 1.1 christos #endif
434 1.1 christos
435 1.1 christos num = EVP_MD_size(hash);
436 1.1 christos if (num < 0)
437 1.1 christos return 0;
438 1.1 christos
439 1.1 christos num = EVP_CIPHER_key_length(c) + num + EVP_CIPHER_iv_length(c);
440 1.1 christos num *= 2;
441 1.1 christos
442 1.1 christos ssl3_cleanup_key_block(s);
443 1.1 christos
444 1.1 christos if ((p = OPENSSL_malloc(num)) == NULL)
445 1.1 christos goto err;
446 1.1 christos
447 1.1 christos s->s3->tmp.key_block_length = num;
448 1.1 christos s->s3->tmp.key_block = p;
449 1.1 christos
450 1.1 christos ret = ssl3_generate_key_block(s, p, num);
451 1.1 christos
452 1.1 christos if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)) {
453 1.1 christos /*
454 1.1 christos * enable vulnerability countermeasure for CBC ciphers with known-IV
455 1.1 christos * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
456 1.1 christos */
457 1.1 christos s->s3->need_empty_fragments = 1;
458 1.1 christos
459 1.1 christos if (s->session->cipher != NULL) {
460 1.1 christos if (s->session->cipher->algorithm_enc == SSL_eNULL)
461 1.1 christos s->s3->need_empty_fragments = 0;
462 1.1 christos
463 1.1 christos #ifndef OPENSSL_NO_RC4
464 1.1 christos if (s->session->cipher->algorithm_enc == SSL_RC4)
465 1.1 christos s->s3->need_empty_fragments = 0;
466 1.1 christos #endif
467 1.1 christos }
468 1.1 christos }
469 1.1 christos
470 1.1 christos return ret;
471 1.1 christos
472 1.1 christos err:
473 1.1 christos SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE);
474 1.1 christos return (0);
475 1.1 christos }
476 1.1 christos
477 1.1 christos void ssl3_cleanup_key_block(SSL *s)
478 1.1 christos {
479 1.1 christos if (s->s3->tmp.key_block != NULL) {
480 1.1 christos OPENSSL_cleanse(s->s3->tmp.key_block, s->s3->tmp.key_block_length);
481 1.1 christos OPENSSL_free(s->s3->tmp.key_block);
482 1.1 christos s->s3->tmp.key_block = NULL;
483 1.1 christos }
484 1.1 christos s->s3->tmp.key_block_length = 0;
485 1.1 christos }
486 1.1 christos
487 1.1 christos /*-
488 1.1 christos * ssl3_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively.
489 1.1 christos *
490 1.1 christos * Returns:
491 1.1 christos * 0: (in non-constant time) if the record is publically invalid (i.e. too
492 1.1 christos * short etc).
493 1.1 christos * 1: if the record's padding is valid / the encryption was successful.
494 1.1 christos * -1: if the record's padding is invalid or, if sending, an internal error
495 1.1 christos * occured.
496 1.1 christos */
497 1.1 christos int ssl3_enc(SSL *s, int send)
498 1.1 christos {
499 1.1 christos SSL3_RECORD *rec;
500 1.1 christos EVP_CIPHER_CTX *ds;
501 1.1 christos unsigned long l;
502 1.1 christos int bs, i, mac_size = 0;
503 1.1 christos const EVP_CIPHER *enc;
504 1.1 christos
505 1.1 christos if (send) {
506 1.1 christos ds = s->enc_write_ctx;
507 1.1 christos rec = &(s->s3->wrec);
508 1.1 christos if (s->enc_write_ctx == NULL)
509 1.1 christos enc = NULL;
510 1.1 christos else
511 1.1 christos enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
512 1.1 christos } else {
513 1.1 christos ds = s->enc_read_ctx;
514 1.1 christos rec = &(s->s3->rrec);
515 1.1 christos if (s->enc_read_ctx == NULL)
516 1.1 christos enc = NULL;
517 1.1 christos else
518 1.1 christos enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
519 1.1 christos }
520 1.1 christos
521 1.1 christos if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
522 1.1 christos memmove(rec->data, rec->input, rec->length);
523 1.1 christos rec->input = rec->data;
524 1.1 christos } else {
525 1.1 christos l = rec->length;
526 1.1 christos bs = EVP_CIPHER_block_size(ds->cipher);
527 1.1 christos
528 1.1 christos /* COMPRESS */
529 1.1 christos
530 1.1 christos if ((bs != 1) && send) {
531 1.1 christos i = bs - ((int)l % bs);
532 1.1 christos
533 1.1 christos /* we need to add 'i-1' padding bytes */
534 1.1 christos l += i;
535 1.1 christos /*
536 1.1 christos * the last of these zero bytes will be overwritten with the
537 1.1 christos * padding length.
538 1.1 christos */
539 1.1 christos memset(&rec->input[rec->length], 0, i);
540 1.1 christos rec->length += i;
541 1.1 christos rec->input[l - 1] = (i - 1);
542 1.1 christos }
543 1.1 christos
544 1.1 christos if (!send) {
545 1.1 christos if (l == 0 || l % bs != 0)
546 1.1 christos return 0;
547 1.1 christos /* otherwise, rec->length >= bs */
548 1.1 christos }
549 1.1 christos
550 1.1 christos if (EVP_Cipher(ds, rec->data, rec->input, l) < 1)
551 1.1 christos return -1;
552 1.1 christos
553 1.1 christos if (EVP_MD_CTX_md(s->read_hash) != NULL)
554 1.1 christos mac_size = EVP_MD_CTX_size(s->read_hash);
555 1.1 christos if ((bs != 1) && !send)
556 1.1 christos return ssl3_cbc_remove_padding(s, rec, bs, mac_size);
557 1.1 christos }
558 1.1 christos return (1);
559 1.1 christos }
560 1.1 christos
561 1.1 christos void ssl3_init_finished_mac(SSL *s)
562 1.1 christos {
563 1.1 christos if (s->s3->handshake_buffer)
564 1.1 christos BIO_free(s->s3->handshake_buffer);
565 1.1 christos if (s->s3->handshake_dgst)
566 1.1 christos ssl3_free_digest_list(s);
567 1.1 christos s->s3->handshake_buffer = BIO_new(BIO_s_mem());
568 1.1 christos (void)BIO_set_close(s->s3->handshake_buffer, BIO_CLOSE);
569 1.1 christos }
570 1.1 christos
571 1.1 christos void ssl3_free_digest_list(SSL *s)
572 1.1 christos {
573 1.1 christos int i;
574 1.1 christos if (!s->s3->handshake_dgst)
575 1.1 christos return;
576 1.1 christos for (i = 0; i < SSL_MAX_DIGEST; i++) {
577 1.1 christos if (s->s3->handshake_dgst[i])
578 1.1 christos EVP_MD_CTX_destroy(s->s3->handshake_dgst[i]);
579 1.1 christos }
580 1.1 christos OPENSSL_free(s->s3->handshake_dgst);
581 1.1 christos s->s3->handshake_dgst = NULL;
582 1.1 christos }
583 1.1 christos
584 1.1 christos void ssl3_finish_mac(SSL *s, const unsigned char *buf, int len)
585 1.1 christos {
586 1.1 christos if (s->s3->handshake_buffer
587 1.1 christos && !(s->s3->flags & TLS1_FLAGS_KEEP_HANDSHAKE)) {
588 1.1 christos BIO_write(s->s3->handshake_buffer, (void *)buf, len);
589 1.1 christos } else if (s->s3->handshake_dgst != NULL) {
590 1.1 christos int i;
591 1.1 christos for (i = 0; i < SSL_MAX_DIGEST; i++) {
592 1.1 christos if (s->s3->handshake_dgst[i] != NULL)
593 1.1 christos EVP_DigestUpdate(s->s3->handshake_dgst[i], buf, len);
594 1.1 christos }
595 1.1 christos }
596 1.1 christos }
597 1.1 christos
598 1.1 christos int ssl3_digest_cached_records(SSL *s)
599 1.1 christos {
600 1.1 christos int i;
601 1.1 christos long mask;
602 1.1 christos const EVP_MD *md;
603 1.1 christos long hdatalen;
604 1.1 christos void *hdata;
605 1.1 christos
606 1.1 christos /* Allocate handshake_dgst array */
607 1.1 christos ssl3_free_digest_list(s);
608 1.1 christos s->s3->handshake_dgst =
609 1.1 christos OPENSSL_malloc(SSL_MAX_DIGEST * sizeof(EVP_MD_CTX *));
610 1.1 christos if (s->s3->handshake_dgst == NULL) {
611 1.1 christos SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, ERR_R_MALLOC_FAILURE);
612 1.1 christos return 0;
613 1.1 christos }
614 1.1 christos memset(s->s3->handshake_dgst, 0, SSL_MAX_DIGEST * sizeof(EVP_MD_CTX *));
615 1.1 christos hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
616 1.1 christos if (hdatalen <= 0) {
617 1.1 christos SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, SSL_R_BAD_HANDSHAKE_LENGTH);
618 1.1 christos return 0;
619 1.1 christos }
620 1.1 christos
621 1.1 christos /* Loop through bitso of algorithm2 field and create MD_CTX-es */
622 1.1 christos for (i = 0; ssl_get_handshake_digest(i, &mask, &md); i++) {
623 1.1 christos if ((mask & ssl_get_algorithm2(s)) && md) {
624 1.1 christos s->s3->handshake_dgst[i] = EVP_MD_CTX_create();
625 1.1 christos #ifdef OPENSSL_FIPS
626 1.1 christos if (EVP_MD_nid(md) == NID_md5) {
627 1.1 christos EVP_MD_CTX_set_flags(s->s3->handshake_dgst[i],
628 1.1 christos EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
629 1.1 christos }
630 1.1 christos #endif
631 1.1 christos if (!EVP_DigestInit_ex(s->s3->handshake_dgst[i], md, NULL)
632 1.1 christos || !EVP_DigestUpdate(s->s3->handshake_dgst[i], hdata,
633 1.1 christos hdatalen)) {
634 1.1 christos SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, ERR_R_INTERNAL_ERROR);
635 1.1 christos return 0;
636 1.1 christos }
637 1.1 christos } else {
638 1.1 christos s->s3->handshake_dgst[i] = NULL;
639 1.1 christos }
640 1.1 christos }
641 1.1 christos if (!(s->s3->flags & TLS1_FLAGS_KEEP_HANDSHAKE)) {
642 1.1 christos /* Free handshake_buffer BIO */
643 1.1 christos BIO_free(s->s3->handshake_buffer);
644 1.1 christos s->s3->handshake_buffer = NULL;
645 1.1 christos }
646 1.1 christos
647 1.1 christos return 1;
648 1.1 christos }
649 1.1 christos
650 1.1 christos int ssl3_cert_verify_mac(SSL *s, int md_nid, unsigned char *p)
651 1.1 christos {
652 1.1 christos return (ssl3_handshake_mac(s, md_nid, NULL, 0, p));
653 1.1 christos }
654 1.1 christos
655 1.1 christos int ssl3_final_finish_mac(SSL *s,
656 1.1 christos const char *sender, int len, unsigned char *p)
657 1.1 christos {
658 1.1 christos int ret, sha1len;
659 1.1 christos ret = ssl3_handshake_mac(s, NID_md5, sender, len, p);
660 1.1 christos if (ret == 0)
661 1.1 christos return 0;
662 1.1 christos
663 1.1 christos p += ret;
664 1.1 christos
665 1.1 christos sha1len = ssl3_handshake_mac(s, NID_sha1, sender, len, p);
666 1.1 christos if (sha1len == 0)
667 1.1 christos return 0;
668 1.1 christos
669 1.1 christos ret += sha1len;
670 1.1 christos return (ret);
671 1.1 christos }
672 1.1 christos
673 1.1 christos static int ssl3_handshake_mac(SSL *s, int md_nid,
674 1.1 christos const char *sender, int len, unsigned char *p)
675 1.1 christos {
676 1.1 christos unsigned int ret;
677 1.1 christos int npad, n;
678 1.1 christos unsigned int i;
679 1.1 christos unsigned char md_buf[EVP_MAX_MD_SIZE];
680 1.1 christos EVP_MD_CTX ctx, *d = NULL;
681 1.1 christos
682 1.1 christos if (s->s3->handshake_buffer)
683 1.1 christos if (!ssl3_digest_cached_records(s))
684 1.1 christos return 0;
685 1.1 christos
686 1.1 christos /*
687 1.1 christos * Search for digest of specified type in the handshake_dgst array
688 1.1 christos */
689 1.1 christos for (i = 0; i < SSL_MAX_DIGEST; i++) {
690 1.1 christos if (s->s3->handshake_dgst[i]
691 1.1 christos && EVP_MD_CTX_type(s->s3->handshake_dgst[i]) == md_nid) {
692 1.1 christos d = s->s3->handshake_dgst[i];
693 1.1 christos break;
694 1.1 christos }
695 1.1 christos }
696 1.1 christos if (!d) {
697 1.1 christos SSLerr(SSL_F_SSL3_HANDSHAKE_MAC, SSL_R_NO_REQUIRED_DIGEST);
698 1.1 christos return 0;
699 1.1 christos }
700 1.1 christos EVP_MD_CTX_init(&ctx);
701 1.1 christos EVP_MD_CTX_set_flags(&ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
702 1.1 christos EVP_MD_CTX_copy_ex(&ctx, d);
703 1.1 christos n = EVP_MD_CTX_size(&ctx);
704 1.1 christos if (n < 0)
705 1.1 christos return 0;
706 1.1 christos
707 1.1 christos npad = (48 / n) * n;
708 1.1 christos if ((sender != NULL && EVP_DigestUpdate(&ctx, sender, len) <= 0)
709 1.1 christos || EVP_DigestUpdate(&ctx, s->session->master_key,
710 1.1 christos s->session->master_key_length) <= 0
711 1.1 christos || EVP_DigestUpdate(&ctx, ssl3_pad_1, npad) <= 0
712 1.1 christos || EVP_DigestFinal_ex(&ctx, md_buf, &i) <= 0
713 1.1 christos
714 1.1 christos || EVP_DigestInit_ex(&ctx, EVP_MD_CTX_md(&ctx), NULL) <= 0
715 1.1 christos || EVP_DigestUpdate(&ctx, s->session->master_key,
716 1.1 christos s->session->master_key_length) <= 0
717 1.1 christos || EVP_DigestUpdate(&ctx, ssl3_pad_2, npad) <= 0
718 1.1 christos || EVP_DigestUpdate(&ctx, md_buf, i) <= 0
719 1.1 christos || EVP_DigestFinal_ex(&ctx, p, &ret) <= 0) {
720 1.1 christos SSLerr(SSL_F_SSL3_HANDSHAKE_MAC, ERR_R_INTERNAL_ERROR);
721 1.1 christos ret = 0;
722 1.1 christos }
723 1.1 christos
724 1.1 christos EVP_MD_CTX_cleanup(&ctx);
725 1.1 christos
726 1.1 christos return ((int)ret);
727 1.1 christos }
728 1.1 christos
729 1.1 christos int n_ssl3_mac(SSL *ssl, unsigned char *md, int send)
730 1.1 christos {
731 1.1 christos SSL3_RECORD *rec;
732 1.1 christos unsigned char *mac_sec, *seq;
733 1.1 christos EVP_MD_CTX md_ctx;
734 1.1 christos const EVP_MD_CTX *hash;
735 1.1 christos unsigned char *p, rec_char;
736 1.1 christos size_t md_size, orig_len;
737 1.1 christos int npad;
738 1.1 christos int t;
739 1.1 christos
740 1.1 christos if (send) {
741 1.1 christos rec = &(ssl->s3->wrec);
742 1.1 christos mac_sec = &(ssl->s3->write_mac_secret[0]);
743 1.1 christos seq = &(ssl->s3->write_sequence[0]);
744 1.1 christos hash = ssl->write_hash;
745 1.1 christos } else {
746 1.1 christos rec = &(ssl->s3->rrec);
747 1.1 christos mac_sec = &(ssl->s3->read_mac_secret[0]);
748 1.1 christos seq = &(ssl->s3->read_sequence[0]);
749 1.1 christos hash = ssl->read_hash;
750 1.1 christos }
751 1.1 christos
752 1.1 christos t = EVP_MD_CTX_size(hash);
753 1.1 christos if (t < 0)
754 1.1 christos return -1;
755 1.1 christos md_size = t;
756 1.1 christos npad = (48 / md_size) * md_size;
757 1.1 christos
758 1.1 christos /*
759 1.1 christos * kludge: ssl3_cbc_remove_padding passes padding length in rec->type
760 1.1 christos */
761 1.1 christos orig_len = rec->length + md_size + ((unsigned int)rec->type >> 8);
762 1.1 christos rec->type &= 0xff;
763 1.1 christos
764 1.1 christos if (!send &&
765 1.1 christos EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
766 1.1 christos ssl3_cbc_record_digest_supported(hash)) {
767 1.1 christos /*
768 1.1 christos * This is a CBC-encrypted record. We must avoid leaking any
769 1.1 christos * timing-side channel information about how many blocks of data we
770 1.1 christos * are hashing because that gives an attacker a timing-oracle.
771 1.1 christos */
772 1.1 christos
773 1.1 christos /*-
774 1.1 christos * npad is, at most, 48 bytes and that's with MD5:
775 1.1 christos * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
776 1.1 christos *
777 1.1 christos * With SHA-1 (the largest hash speced for SSLv3) the hash size
778 1.1 christos * goes up 4, but npad goes down by 8, resulting in a smaller
779 1.1 christos * total size.
780 1.1 christos */
781 1.1 christos unsigned char header[75];
782 1.1 christos unsigned j = 0;
783 1.1 christos memcpy(header + j, mac_sec, md_size);
784 1.1 christos j += md_size;
785 1.1 christos memcpy(header + j, ssl3_pad_1, npad);
786 1.1 christos j += npad;
787 1.1 christos memcpy(header + j, seq, 8);
788 1.1 christos j += 8;
789 1.1 christos header[j++] = rec->type;
790 1.1 christos header[j++] = rec->length >> 8;
791 1.1 christos header[j++] = rec->length & 0xff;
792 1.1 christos
793 1.1 christos /* Final param == is SSLv3 */
794 1.1 christos if (ssl3_cbc_digest_record(hash,
795 1.1 christos md, &md_size,
796 1.1 christos header, rec->input,
797 1.1 christos rec->length + md_size, orig_len,
798 1.1 christos mac_sec, md_size, 1) <= 0)
799 1.1 christos return -1;
800 1.1 christos } else {
801 1.1 christos unsigned int md_size_u;
802 1.1 christos /* Chop the digest off the end :-) */
803 1.1 christos EVP_MD_CTX_init(&md_ctx);
804 1.1 christos
805 1.1 christos rec_char = rec->type;
806 1.1 christos p = md;
807 1.1 christos s2n(rec->length, p);
808 1.1 christos if (EVP_MD_CTX_copy_ex(&md_ctx, hash) <= 0
809 1.1 christos || EVP_DigestUpdate(&md_ctx, mac_sec, md_size) <= 0
810 1.1 christos || EVP_DigestUpdate(&md_ctx, ssl3_pad_1, npad) <= 0
811 1.1 christos || EVP_DigestUpdate(&md_ctx, seq, 8) <= 0
812 1.1 christos || EVP_DigestUpdate(&md_ctx, &rec_char, 1) <= 0
813 1.1 christos || EVP_DigestUpdate(&md_ctx, md, 2) <= 0
814 1.1 christos || EVP_DigestUpdate(&md_ctx, rec->input, rec->length) <= 0
815 1.1 christos || EVP_DigestFinal_ex(&md_ctx, md, NULL) <= 0
816 1.1 christos || EVP_MD_CTX_copy_ex(&md_ctx, hash) <= 0
817 1.1 christos || EVP_DigestUpdate(&md_ctx, mac_sec, md_size) <= 0
818 1.1 christos || EVP_DigestUpdate(&md_ctx, ssl3_pad_2, npad) <= 0
819 1.1 christos || EVP_DigestUpdate(&md_ctx, md, md_size) <= 0
820 1.1 christos || EVP_DigestFinal_ex(&md_ctx, md, &md_size_u) <= 0) {
821 1.1 christos EVP_MD_CTX_cleanup(&md_ctx);
822 1.1 christos return -1;
823 1.1 christos }
824 1.1 christos md_size = md_size_u;
825 1.1 christos
826 1.1 christos EVP_MD_CTX_cleanup(&md_ctx);
827 1.1 christos }
828 1.1 christos
829 1.1 christos ssl3_record_sequence_update(seq);
830 1.1 christos return (md_size);
831 1.1 christos }
832 1.1 christos
833 1.1 christos void ssl3_record_sequence_update(unsigned char *seq)
834 1.1 christos {
835 1.1 christos int i;
836 1.1 christos
837 1.1 christos for (i = 7; i >= 0; i--) {
838 1.1 christos ++seq[i];
839 1.1 christos if (seq[i] != 0)
840 1.1 christos break;
841 1.1 christos }
842 1.1 christos }
843 1.1 christos
844 1.1 christos int ssl3_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
845 1.1 christos int len)
846 1.1 christos {
847 1.1 christos static const unsigned char *salt[3] = {
848 1.1 christos #ifndef CHARSET_EBCDIC
849 1.1 christos (const unsigned char *)"A",
850 1.1 christos (const unsigned char *)"BB",
851 1.1 christos (const unsigned char *)"CCC",
852 1.1 christos #else
853 1.1 christos (const unsigned char *)"\x41",
854 1.1 christos (const unsigned char *)"\x42\x42",
855 1.1 christos (const unsigned char *)"\x43\x43\x43",
856 1.1 christos #endif
857 1.1 christos };
858 1.1 christos unsigned char buf[EVP_MAX_MD_SIZE];
859 1.1 christos EVP_MD_CTX ctx;
860 1.1 christos int i, ret = 0;
861 1.1 christos unsigned int n;
862 1.1 christos #ifdef OPENSSL_SSL_TRACE_CRYPTO
863 1.1 christos unsigned char *tmpout = out;
864 1.1 christos #endif
865 1.1 christos
866 1.1 christos EVP_MD_CTX_init(&ctx);
867 1.1 christos for (i = 0; i < 3; i++) {
868 1.1 christos if (EVP_DigestInit_ex(&ctx, s->ctx->sha1, NULL) <= 0
869 1.1 christos || EVP_DigestUpdate(&ctx, salt[i],
870 1.1 christos strlen((const char *)salt[i])) <= 0
871 1.1 christos || EVP_DigestUpdate(&ctx, p, len) <= 0
872 1.1 christos || EVP_DigestUpdate(&ctx, &(s->s3->client_random[0]),
873 1.1 christos SSL3_RANDOM_SIZE) <= 0
874 1.1 christos || EVP_DigestUpdate(&ctx, &(s->s3->server_random[0]),
875 1.1 christos SSL3_RANDOM_SIZE) <= 0
876 1.1 christos || EVP_DigestFinal_ex(&ctx, buf, &n) <= 0
877 1.1 christos
878 1.1 christos || EVP_DigestInit_ex(&ctx, s->ctx->md5, NULL) <= 0
879 1.1 christos || EVP_DigestUpdate(&ctx, p, len) <= 0
880 1.1 christos || EVP_DigestUpdate(&ctx, buf, n) <= 0
881 1.1 christos || EVP_DigestFinal_ex(&ctx, out, &n) <= 0) {
882 1.1 christos SSLerr(SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_INTERNAL_ERROR);
883 1.1 christos ret = 0;
884 1.1 christos break;
885 1.1 christos }
886 1.1 christos out += n;
887 1.1 christos ret += n;
888 1.1 christos }
889 1.1 christos EVP_MD_CTX_cleanup(&ctx);
890 1.1 christos
891 1.1 christos #ifdef OPENSSL_SSL_TRACE_CRYPTO
892 1.1 christos if (ret > 0 && s->msg_callback) {
893 1.1 christos s->msg_callback(2, s->version, TLS1_RT_CRYPTO_PREMASTER,
894 1.1 christos p, len, s, s->msg_callback_arg);
895 1.1 christos s->msg_callback(2, s->version, TLS1_RT_CRYPTO_CLIENT_RANDOM,
896 1.1 christos s->s3->client_random, SSL3_RANDOM_SIZE,
897 1.1 christos s, s->msg_callback_arg);
898 1.1 christos s->msg_callback(2, s->version, TLS1_RT_CRYPTO_SERVER_RANDOM,
899 1.1 christos s->s3->server_random, SSL3_RANDOM_SIZE,
900 1.1 christos s, s->msg_callback_arg);
901 1.1 christos s->msg_callback(2, s->version, TLS1_RT_CRYPTO_MASTER,
902 1.1 christos tmpout, SSL3_MASTER_SECRET_SIZE,
903 1.1 christos s, s->msg_callback_arg);
904 1.1 christos }
905 1.1 christos #endif
906 1.1 christos OPENSSL_cleanse(buf, sizeof buf);
907 1.1 christos return (ret);
908 1.1 christos }
909 1.1 christos
910 1.1 christos int ssl3_alert_code(int code)
911 1.1 christos {
912 1.1 christos switch (code) {
913 1.1 christos case SSL_AD_CLOSE_NOTIFY:
914 1.1 christos return (SSL3_AD_CLOSE_NOTIFY);
915 1.1 christos case SSL_AD_UNEXPECTED_MESSAGE:
916 1.1 christos return (SSL3_AD_UNEXPECTED_MESSAGE);
917 1.1 christos case SSL_AD_BAD_RECORD_MAC:
918 1.1 christos return (SSL3_AD_BAD_RECORD_MAC);
919 1.1 christos case SSL_AD_DECRYPTION_FAILED:
920 1.1 christos return (SSL3_AD_BAD_RECORD_MAC);
921 1.1 christos case SSL_AD_RECORD_OVERFLOW:
922 1.1 christos return (SSL3_AD_BAD_RECORD_MAC);
923 1.1 christos case SSL_AD_DECOMPRESSION_FAILURE:
924 1.1 christos return (SSL3_AD_DECOMPRESSION_FAILURE);
925 1.1 christos case SSL_AD_HANDSHAKE_FAILURE:
926 1.1 christos return (SSL3_AD_HANDSHAKE_FAILURE);
927 1.1 christos case SSL_AD_NO_CERTIFICATE:
928 1.1 christos return (SSL3_AD_NO_CERTIFICATE);
929 1.1 christos case SSL_AD_BAD_CERTIFICATE:
930 1.1 christos return (SSL3_AD_BAD_CERTIFICATE);
931 1.1 christos case SSL_AD_UNSUPPORTED_CERTIFICATE:
932 1.1 christos return (SSL3_AD_UNSUPPORTED_CERTIFICATE);
933 1.1 christos case SSL_AD_CERTIFICATE_REVOKED:
934 1.1 christos return (SSL3_AD_CERTIFICATE_REVOKED);
935 1.1 christos case SSL_AD_CERTIFICATE_EXPIRED:
936 1.1 christos return (SSL3_AD_CERTIFICATE_EXPIRED);
937 1.1 christos case SSL_AD_CERTIFICATE_UNKNOWN:
938 1.1 christos return (SSL3_AD_CERTIFICATE_UNKNOWN);
939 1.1 christos case SSL_AD_ILLEGAL_PARAMETER:
940 1.1 christos return (SSL3_AD_ILLEGAL_PARAMETER);
941 1.1 christos case SSL_AD_UNKNOWN_CA:
942 1.1 christos return (SSL3_AD_BAD_CERTIFICATE);
943 1.1 christos case SSL_AD_ACCESS_DENIED:
944 1.1 christos return (SSL3_AD_HANDSHAKE_FAILURE);
945 1.1 christos case SSL_AD_DECODE_ERROR:
946 1.1 christos return (SSL3_AD_HANDSHAKE_FAILURE);
947 1.1 christos case SSL_AD_DECRYPT_ERROR:
948 1.1 christos return (SSL3_AD_HANDSHAKE_FAILURE);
949 1.1 christos case SSL_AD_EXPORT_RESTRICTION:
950 1.1 christos return (SSL3_AD_HANDSHAKE_FAILURE);
951 1.1 christos case SSL_AD_PROTOCOL_VERSION:
952 1.1 christos return (SSL3_AD_HANDSHAKE_FAILURE);
953 1.1 christos case SSL_AD_INSUFFICIENT_SECURITY:
954 1.1 christos return (SSL3_AD_HANDSHAKE_FAILURE);
955 1.1 christos case SSL_AD_INTERNAL_ERROR:
956 1.1 christos return (SSL3_AD_HANDSHAKE_FAILURE);
957 1.1 christos case SSL_AD_USER_CANCELLED:
958 1.1 christos return (SSL3_AD_HANDSHAKE_FAILURE);
959 1.1 christos case SSL_AD_NO_RENEGOTIATION:
960 1.1 christos return (-1); /* Don't send it :-) */
961 1.1 christos case SSL_AD_UNSUPPORTED_EXTENSION:
962 1.1 christos return (SSL3_AD_HANDSHAKE_FAILURE);
963 1.1 christos case SSL_AD_CERTIFICATE_UNOBTAINABLE:
964 1.1 christos return (SSL3_AD_HANDSHAKE_FAILURE);
965 1.1 christos case SSL_AD_UNRECOGNIZED_NAME:
966 1.1 christos return (SSL3_AD_HANDSHAKE_FAILURE);
967 1.1 christos case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
968 1.1 christos return (SSL3_AD_HANDSHAKE_FAILURE);
969 1.1 christos case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
970 1.1 christos return (SSL3_AD_HANDSHAKE_FAILURE);
971 1.1 christos case SSL_AD_UNKNOWN_PSK_IDENTITY:
972 1.1 christos return (TLS1_AD_UNKNOWN_PSK_IDENTITY);
973 1.1 christos case SSL_AD_INAPPROPRIATE_FALLBACK:
974 1.1 christos return (TLS1_AD_INAPPROPRIATE_FALLBACK);
975 1.1 christos default:
976 1.1 christos return (-1);
977 1.1 christos }
978 1.1 christos }
979