]>
Commit | Line | Data |
---|---|---|
58964a49 | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
2 | * All rights reserved. |
3 | * | |
4 | * This package is an SSL implementation written | |
5 | * by Eric Young (eay@cryptsoft.com). | |
6 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 7 | * |
d02b48c6 RE |
8 | * This library is free for commercial and non-commercial use as long as |
9 | * the following conditions are aheared to. The following conditions | |
10 | * apply to all code found in this distribution, be it the RC4, RSA, | |
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
12 | * included with this distribution is covered by the same copyright terms | |
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 14 | * |
d02b48c6 RE |
15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | * the code are not to be removed. | |
17 | * If this package is used in a product, Eric Young should be given attribution | |
18 | * as the author of the parts of the library used. | |
19 | * This can be in the form of a textual message at program startup or | |
20 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 21 | * |
d02b48c6 RE |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions | |
24 | * are met: | |
25 | * 1. Redistributions of source code must retain the copyright | |
26 | * notice, this list of conditions and the following disclaimer. | |
27 | * 2. Redistributions in binary form must reproduce the above copyright | |
28 | * notice, this list of conditions and the following disclaimer in the | |
29 | * documentation and/or other materials provided with the distribution. | |
30 | * 3. All advertising materials mentioning features or use of this software | |
31 | * must display the following acknowledgement: | |
32 | * "This product includes cryptographic software written by | |
33 | * Eric Young (eay@cryptsoft.com)" | |
34 | * The word 'cryptographic' can be left out if the rouines from the library | |
35 | * being used are not cryptographic related :-). | |
0f113f3e | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 39 | * |
d02b48c6 RE |
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
50 | * SUCH DAMAGE. | |
0f113f3e | 51 | * |
d02b48c6 RE |
52 | * The licence and distribution terms for any publically available version or |
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
54 | * copied and put under another distribution licence | |
55 | * [including the GNU Public Licence.] | |
56 | */ | |
82b0bf0b | 57 | /* ==================================================================== |
52b8dad8 | 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
82b0bf0b BM |
59 | * |
60 | * Redistribution and use in source and binary forms, with or without | |
61 | * modification, are permitted provided that the following conditions | |
62 | * are met: | |
63 | * | |
64 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 65 | * notice, this list of conditions and the following disclaimer. |
82b0bf0b BM |
66 | * |
67 | * 2. Redistributions in binary form must reproduce the above copyright | |
68 | * notice, this list of conditions and the following disclaimer in | |
69 | * the documentation and/or other materials provided with the | |
70 | * distribution. | |
71 | * | |
72 | * 3. All advertising materials mentioning features or use of this | |
73 | * software must display the following acknowledgment: | |
74 | * "This product includes software developed by the OpenSSL Project | |
75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
76 | * | |
77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
78 | * endorse or promote products derived from this software without | |
79 | * prior written permission. For written permission, please contact | |
80 | * openssl-core@openssl.org. | |
81 | * | |
82 | * 5. Products derived from this software may not be called "OpenSSL" | |
83 | * nor may "OpenSSL" appear in their names without prior written | |
84 | * permission of the OpenSSL Project. | |
85 | * | |
86 | * 6. Redistributions of any form whatsoever must retain the following | |
87 | * acknowledgment: | |
88 | * "This product includes software developed by the OpenSSL Project | |
89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
90 | * | |
91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
102 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
103 | * ==================================================================== | |
104 | * | |
105 | * This product includes cryptographic software written by Eric Young | |
106 | * (eay@cryptsoft.com). This product includes software written by Tim | |
107 | * Hudson (tjh@cryptsoft.com). | |
108 | * | |
109 | */ | |
ddac1974 NL |
110 | /* ==================================================================== |
111 | * Copyright 2005 Nokia. All rights reserved. | |
112 | * | |
113 | * The portions of the attached software ("Contribution") is developed by | |
114 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source | |
115 | * license. | |
116 | * | |
117 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of | |
118 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites | |
119 | * support (see RFC 4279) to OpenSSL. | |
120 | * | |
121 | * No patent licenses or other rights except those expressly stated in | |
122 | * the OpenSSL open source license shall be deemed granted or received | |
123 | * expressly, by implication, estoppel, or otherwise. | |
124 | * | |
125 | * No assurances are provided by Nokia that the Contribution does not | |
126 | * infringe the patent or other intellectual property rights of any third | |
127 | * party or that the license provides you with all the necessary rights | |
128 | * to make use of the Contribution. | |
129 | * | |
130 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN | |
131 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA | |
132 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY | |
133 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR | |
134 | * OTHERWISE. | |
135 | */ | |
d02b48c6 RE |
136 | |
137 | #include <stdio.h> | |
d02b48c6 | 138 | #include "ssl_locl.h" |
7b63c0fa | 139 | #include <openssl/evp.h> |
dbad1690 | 140 | #include <openssl/md5.h> |
d02b48c6 | 141 | |
027e257b | 142 | static int ssl3_generate_key_block(SSL *s, unsigned char *km, int num) |
0f113f3e | 143 | { |
6e59a892 RL |
144 | EVP_MD_CTX *m5; |
145 | EVP_MD_CTX *s1; | |
0f113f3e MC |
146 | unsigned char buf[16], smd[SHA_DIGEST_LENGTH]; |
147 | unsigned char c = 'A'; | |
148 | unsigned int i, j, k; | |
6e59a892 | 149 | int ret = 0; |
58964a49 | 150 | |
ca570cfd | 151 | #ifdef CHARSET_EBCDIC |
0f113f3e | 152 | c = os_toascii[c]; /* 'A' in ASCII */ |
ca570cfd | 153 | #endif |
0f113f3e | 154 | k = 0; |
bfb0641f RL |
155 | m5 = EVP_MD_CTX_new(); |
156 | s1 = EVP_MD_CTX_new(); | |
6e59a892 RL |
157 | if (m5 == NULL || s1 == NULL) { |
158 | SSLerr(SSL_F_SSL3_GENERATE_KEY_BLOCK, ERR_R_MALLOC_FAILURE); | |
159 | goto err; | |
160 | } | |
161 | EVP_MD_CTX_set_flags(m5, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); | |
0f113f3e MC |
162 | for (i = 0; (int)i < num; i += MD5_DIGEST_LENGTH) { |
163 | k++; | |
164 | if (k > sizeof buf) { | |
165 | /* bug: 'buf' is too small for this ciphersuite */ | |
166 | SSLerr(SSL_F_SSL3_GENERATE_KEY_BLOCK, ERR_R_INTERNAL_ERROR); | |
167 | return 0; | |
168 | } | |
169 | ||
170 | for (j = 0; j < k; j++) | |
171 | buf[j] = c; | |
172 | c++; | |
6e59a892 RL |
173 | EVP_DigestInit_ex(s1, EVP_sha1(), NULL); |
174 | EVP_DigestUpdate(s1, buf, k); | |
175 | EVP_DigestUpdate(s1, s->session->master_key, | |
0f113f3e | 176 | s->session->master_key_length); |
6e59a892 RL |
177 | EVP_DigestUpdate(s1, s->s3->server_random, SSL3_RANDOM_SIZE); |
178 | EVP_DigestUpdate(s1, s->s3->client_random, SSL3_RANDOM_SIZE); | |
179 | EVP_DigestFinal_ex(s1, smd, NULL); | |
0f113f3e | 180 | |
6e59a892 RL |
181 | EVP_DigestInit_ex(m5, EVP_md5(), NULL); |
182 | EVP_DigestUpdate(m5, s->session->master_key, | |
0f113f3e | 183 | s->session->master_key_length); |
6e59a892 | 184 | EVP_DigestUpdate(m5, smd, SHA_DIGEST_LENGTH); |
0f113f3e | 185 | if ((int)(i + MD5_DIGEST_LENGTH) > num) { |
6e59a892 | 186 | EVP_DigestFinal_ex(m5, smd, NULL); |
0f113f3e MC |
187 | memcpy(km, smd, (num - i)); |
188 | } else | |
6e59a892 | 189 | EVP_DigestFinal_ex(m5, km, NULL); |
0f113f3e MC |
190 | |
191 | km += MD5_DIGEST_LENGTH; | |
192 | } | |
e0f9bf1d | 193 | OPENSSL_cleanse(smd, sizeof(smd)); |
6e59a892 RL |
194 | ret = 1; |
195 | err: | |
bfb0641f RL |
196 | EVP_MD_CTX_free(m5); |
197 | EVP_MD_CTX_free(s1); | |
6e59a892 | 198 | return ret; |
0f113f3e | 199 | } |
58964a49 | 200 | |
6b691a5c | 201 | int ssl3_change_cipher_state(SSL *s, int which) |
0f113f3e MC |
202 | { |
203 | unsigned char *p, *mac_secret; | |
204 | unsigned char exp_key[EVP_MAX_KEY_LENGTH]; | |
205 | unsigned char exp_iv[EVP_MAX_IV_LENGTH]; | |
361a1191 | 206 | unsigned char *ms, *key, *iv; |
0f113f3e MC |
207 | EVP_CIPHER_CTX *dd; |
208 | const EVP_CIPHER *c; | |
09b6c2ef | 209 | #ifndef OPENSSL_NO_COMP |
0f113f3e | 210 | COMP_METHOD *comp; |
09b6c2ef | 211 | #endif |
0f113f3e | 212 | const EVP_MD *m; |
361a1191 | 213 | int n, i, j, k, cl; |
0f113f3e MC |
214 | int reuse_dd = 0; |
215 | ||
0f113f3e MC |
216 | c = s->s3->tmp.new_sym_enc; |
217 | m = s->s3->tmp.new_hash; | |
218 | /* m == NULL will lead to a crash later */ | |
219 | OPENSSL_assert(m); | |
09b6c2ef | 220 | #ifndef OPENSSL_NO_COMP |
0f113f3e MC |
221 | if (s->s3->tmp.new_compression == NULL) |
222 | comp = NULL; | |
223 | else | |
224 | comp = s->s3->tmp.new_compression->method; | |
09b6c2ef | 225 | #endif |
d02b48c6 | 226 | |
0f113f3e MC |
227 | if (which & SSL3_CC_READ) { |
228 | if (s->enc_read_ctx != NULL) | |
229 | reuse_dd = 1; | |
846ec07d | 230 | else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL) |
0f113f3e MC |
231 | goto err; |
232 | else | |
233 | /* | |
234 | * make sure it's intialized in case we exit later with an error | |
235 | */ | |
846ec07d | 236 | EVP_CIPHER_CTX_reset(s->enc_read_ctx); |
0f113f3e MC |
237 | dd = s->enc_read_ctx; |
238 | ||
5f3d93e4 | 239 | if (ssl_replace_hash(&s->read_hash, m) == NULL) { |
69f68237 MC |
240 | SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); |
241 | goto err2; | |
242 | } | |
09b6c2ef | 243 | #ifndef OPENSSL_NO_COMP |
0f113f3e | 244 | /* COMPRESS */ |
efa7dd64 RS |
245 | COMP_CTX_free(s->expand); |
246 | s->expand = NULL; | |
0f113f3e MC |
247 | if (comp != NULL) { |
248 | s->expand = COMP_CTX_new(comp); | |
249 | if (s->expand == NULL) { | |
250 | SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, | |
251 | SSL_R_COMPRESSION_LIBRARY_ERROR); | |
252 | goto err2; | |
253 | } | |
61986d32 | 254 | if (!RECORD_LAYER_setup_comp_buffer(&s->rlayer)) |
0f113f3e MC |
255 | goto err; |
256 | } | |
09b6c2ef | 257 | #endif |
de07f311 | 258 | RECORD_LAYER_reset_read_sequence(&s->rlayer); |
0f113f3e MC |
259 | mac_secret = &(s->s3->read_mac_secret[0]); |
260 | } else { | |
261 | if (s->enc_write_ctx != NULL) | |
262 | reuse_dd = 1; | |
846ec07d | 263 | else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL) |
0f113f3e MC |
264 | goto err; |
265 | else | |
266 | /* | |
267 | * make sure it's intialized in case we exit later with an error | |
268 | */ | |
846ec07d | 269 | EVP_CIPHER_CTX_reset(s->enc_write_ctx); |
0f113f3e | 270 | dd = s->enc_write_ctx; |
5f3d93e4 | 271 | if (ssl_replace_hash(&s->write_hash, m) == NULL) { |
69f68237 MC |
272 | SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); |
273 | goto err2; | |
274 | } | |
09b6c2ef | 275 | #ifndef OPENSSL_NO_COMP |
0f113f3e | 276 | /* COMPRESS */ |
efa7dd64 RS |
277 | COMP_CTX_free(s->compress); |
278 | s->compress = NULL; | |
0f113f3e MC |
279 | if (comp != NULL) { |
280 | s->compress = COMP_CTX_new(comp); | |
281 | if (s->compress == NULL) { | |
282 | SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, | |
283 | SSL_R_COMPRESSION_LIBRARY_ERROR); | |
284 | goto err2; | |
285 | } | |
286 | } | |
09b6c2ef | 287 | #endif |
de07f311 | 288 | RECORD_LAYER_reset_write_sequence(&s->rlayer); |
0f113f3e MC |
289 | mac_secret = &(s->s3->write_mac_secret[0]); |
290 | } | |
291 | ||
292 | if (reuse_dd) | |
846ec07d | 293 | EVP_CIPHER_CTX_reset(dd); |
0f113f3e MC |
294 | |
295 | p = s->s3->tmp.key_block; | |
296 | i = EVP_MD_size(m); | |
297 | if (i < 0) | |
298 | goto err2; | |
299 | cl = EVP_CIPHER_key_length(c); | |
361a1191 | 300 | j = cl; |
0f113f3e MC |
301 | k = EVP_CIPHER_iv_length(c); |
302 | if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) || | |
303 | (which == SSL3_CHANGE_CIPHER_SERVER_READ)) { | |
304 | ms = &(p[0]); | |
305 | n = i + i; | |
306 | key = &(p[n]); | |
307 | n += j + j; | |
308 | iv = &(p[n]); | |
309 | n += k + k; | |
0f113f3e MC |
310 | } else { |
311 | n = i; | |
312 | ms = &(p[n]); | |
313 | n += i + j; | |
314 | key = &(p[n]); | |
315 | n += j + k; | |
316 | iv = &(p[n]); | |
317 | n += k; | |
0f113f3e MC |
318 | } |
319 | ||
320 | if (n > s->s3->tmp.key_block_length) { | |
321 | SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); | |
322 | goto err2; | |
323 | } | |
324 | ||
0f113f3e | 325 | memcpy(mac_secret, ms, i); |
0f113f3e MC |
326 | |
327 | EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE)); | |
58964a49 | 328 | |
1cf218bc | 329 | #ifdef OPENSSL_SSL_TRACE_CRYPTO |
0f113f3e MC |
330 | if (s->msg_callback) { |
331 | ||
332 | int wh = which & SSL3_CC_WRITE ? | |
333 | TLS1_RT_CRYPTO_WRITE : TLS1_RT_CRYPTO_READ; | |
334 | s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_MAC, | |
335 | mac_secret, EVP_MD_size(m), s, s->msg_callback_arg); | |
336 | if (c->key_len) | |
337 | s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_KEY, | |
338 | key, c->key_len, s, s->msg_callback_arg); | |
339 | if (k) { | |
340 | s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_IV, | |
341 | iv, k, s, s->msg_callback_arg); | |
342 | } | |
343 | } | |
1cf218bc DSH |
344 | #endif |
345 | ||
e0f9bf1d RS |
346 | OPENSSL_cleanse(exp_key, sizeof(exp_key)); |
347 | OPENSSL_cleanse(exp_iv, sizeof(exp_iv)); | |
0f113f3e MC |
348 | return (1); |
349 | err: | |
350 | SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE); | |
351 | err2: | |
e0f9bf1d RS |
352 | OPENSSL_cleanse(exp_key, sizeof(exp_key)); |
353 | OPENSSL_cleanse(exp_iv, sizeof(exp_iv)); | |
0f113f3e MC |
354 | return (0); |
355 | } | |
d02b48c6 | 356 | |
6b691a5c | 357 | int ssl3_setup_key_block(SSL *s) |
0f113f3e MC |
358 | { |
359 | unsigned char *p; | |
360 | const EVP_CIPHER *c; | |
361 | const EVP_MD *hash; | |
362 | int num; | |
363 | int ret = 0; | |
364 | SSL_COMP *comp; | |
365 | ||
366 | if (s->s3->tmp.key_block_length != 0) | |
367 | return (1); | |
368 | ||
369 | if (!ssl_cipher_get_evp(s->session, &c, &hash, NULL, NULL, &comp, 0)) { | |
370 | SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE); | |
371 | return (0); | |
372 | } | |
373 | ||
374 | s->s3->tmp.new_sym_enc = c; | |
375 | s->s3->tmp.new_hash = hash; | |
09b6c2ef | 376 | #ifdef OPENSSL_NO_COMP |
0f113f3e | 377 | s->s3->tmp.new_compression = NULL; |
09b6c2ef | 378 | #else |
0f113f3e | 379 | s->s3->tmp.new_compression = comp; |
09b6c2ef | 380 | #endif |
d02b48c6 | 381 | |
0f113f3e MC |
382 | num = EVP_MD_size(hash); |
383 | if (num < 0) | |
384 | return 0; | |
385 | ||
386 | num = EVP_CIPHER_key_length(c) + num + EVP_CIPHER_iv_length(c); | |
387 | num *= 2; | |
0eab41fb | 388 | |
0f113f3e | 389 | ssl3_cleanup_key_block(s); |
d02b48c6 | 390 | |
0f113f3e MC |
391 | if ((p = OPENSSL_malloc(num)) == NULL) |
392 | goto err; | |
d02b48c6 | 393 | |
0f113f3e MC |
394 | s->s3->tmp.key_block_length = num; |
395 | s->s3->tmp.key_block = p; | |
d02b48c6 | 396 | |
0f113f3e | 397 | ret = ssl3_generate_key_block(s, p, num); |
d02b48c6 | 398 | |
0f113f3e MC |
399 | if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)) { |
400 | /* | |
401 | * enable vulnerability countermeasure for CBC ciphers with known-IV | |
402 | * problem (http://www.openssl.org/~bodo/tls-cbc.txt) | |
403 | */ | |
404 | s->s3->need_empty_fragments = 1; | |
82b0bf0b | 405 | |
0f113f3e MC |
406 | if (s->session->cipher != NULL) { |
407 | if (s->session->cipher->algorithm_enc == SSL_eNULL) | |
408 | s->s3->need_empty_fragments = 0; | |
c21506ba | 409 | |
82b0bf0b | 410 | #ifndef OPENSSL_NO_RC4 |
0f113f3e MC |
411 | if (s->session->cipher->algorithm_enc == SSL_RC4) |
412 | s->s3->need_empty_fragments = 0; | |
82b0bf0b | 413 | #endif |
0f113f3e MC |
414 | } |
415 | } | |
d02b48c6 | 416 | |
0f113f3e MC |
417 | return ret; |
418 | ||
419 | err: | |
420 | SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE); | |
421 | return (0); | |
422 | } | |
d02b48c6 | 423 | |
6b691a5c | 424 | void ssl3_cleanup_key_block(SSL *s) |
0f113f3e | 425 | { |
4b45c6e5 RS |
426 | OPENSSL_clear_free(s->s3->tmp.key_block, s->s3->tmp.key_block_length); |
427 | s->s3->tmp.key_block = NULL; | |
0f113f3e MC |
428 | s->s3->tmp.key_block_length = 0; |
429 | } | |
d02b48c6 | 430 | |
6b691a5c | 431 | void ssl3_init_finished_mac(SSL *s) |
0f113f3e | 432 | { |
85fb6fda | 433 | ssl3_free_digest_list(s); |
0f113f3e MC |
434 | s->s3->handshake_buffer = BIO_new(BIO_s_mem()); |
435 | (void)BIO_set_close(s->s3->handshake_buffer, BIO_CLOSE); | |
436 | } | |
437 | ||
c7238204 DSH |
438 | /* |
439 | * Free digest list. Also frees handshake buffer since they are always freed | |
440 | * together. | |
441 | */ | |
442 | ||
0f113f3e MC |
443 | void ssl3_free_digest_list(SSL *s) |
444 | { | |
c7238204 DSH |
445 | BIO_free(s->s3->handshake_buffer); |
446 | s->s3->handshake_buffer = NULL; | |
bfb0641f | 447 | EVP_MD_CTX_free(s->s3->handshake_dgst); |
0f113f3e MC |
448 | s->s3->handshake_dgst = NULL; |
449 | } | |
81025661 | 450 | |
6b691a5c | 451 | void ssl3_finish_mac(SSL *s, const unsigned char *buf, int len) |
0f113f3e | 452 | { |
28ba2541 | 453 | if (s->s3->handshake_dgst == NULL) |
0f113f3e | 454 | BIO_write(s->s3->handshake_buffer, (void *)buf, len); |
28ba2541 DSH |
455 | else |
456 | EVP_DigestUpdate(s->s3->handshake_dgst, buf, len); | |
0f113f3e | 457 | } |
6ba71a71 | 458 | |
124037fd | 459 | int ssl3_digest_cached_records(SSL *s, int keep) |
0f113f3e | 460 | { |
0f113f3e MC |
461 | const EVP_MD *md; |
462 | long hdatalen; | |
463 | void *hdata; | |
464 | ||
0f113f3e | 465 | if (s->s3->handshake_dgst == NULL) { |
124037fd DSH |
466 | hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); |
467 | if (hdatalen <= 0) { | |
468 | SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, SSL_R_BAD_HANDSHAKE_LENGTH); | |
469 | return 0; | |
470 | } | |
0f113f3e | 471 | |
bfb0641f | 472 | s->s3->handshake_dgst = EVP_MD_CTX_new(); |
28ba2541 DSH |
473 | if (s->s3->handshake_dgst == NULL) { |
474 | SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, ERR_R_MALLOC_FAILURE); | |
475 | return 0; | |
476 | } | |
477 | ||
478 | md = ssl_handshake_md(s); | |
479 | if (md == NULL) { | |
480 | SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, ERR_R_INTERNAL_ERROR); | |
481 | return 0; | |
0f113f3e | 482 | } |
124037fd | 483 | |
28ba2541 DSH |
484 | EVP_DigestInit_ex(s->s3->handshake_dgst, md, NULL); |
485 | EVP_DigestUpdate(s->s3->handshake_dgst, hdata, hdatalen); | |
486 | ||
0f113f3e | 487 | } |
124037fd | 488 | if (keep == 0) { |
0f113f3e MC |
489 | BIO_free(s->s3->handshake_buffer); |
490 | s->s3->handshake_buffer = NULL; | |
491 | } | |
492 | ||
493 | return 1; | |
494 | } | |
6ba71a71 | 495 | |
28ba2541 | 496 | int ssl3_final_finish_mac(SSL *s, const char *sender, int len, unsigned char *p) |
0f113f3e | 497 | { |
28ba2541 | 498 | int ret; |
6e59a892 | 499 | EVP_MD_CTX *ctx = NULL; |
0f113f3e | 500 | |
124037fd DSH |
501 | if (!ssl3_digest_cached_records(s, 0)) |
502 | return 0; | |
0f113f3e | 503 | |
28ba2541 | 504 | if (EVP_MD_CTX_type(s->s3->handshake_dgst) != NID_md5_sha1) { |
600fdc71 | 505 | SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, SSL_R_NO_REQUIRED_DIGEST); |
0f113f3e MC |
506 | return 0; |
507 | } | |
28ba2541 | 508 | |
bfb0641f | 509 | ctx = EVP_MD_CTX_new(); |
6e59a892 RL |
510 | if (ctx == NULL) { |
511 | SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_MALLOC_FAILURE); | |
512 | return 0; | |
513 | } | |
514 | EVP_MD_CTX_copy_ex(ctx, s->s3->handshake_dgst); | |
28ba2541 | 515 | |
6e59a892 | 516 | ret = EVP_MD_CTX_size(ctx); |
28ba2541 | 517 | if (ret < 0) { |
bfb0641f | 518 | EVP_MD_CTX_reset(ctx); |
0f113f3e | 519 | return 0; |
28ba2541 | 520 | } |
0f113f3e | 521 | |
6e59a892 RL |
522 | if ((sender != NULL && EVP_DigestUpdate(ctx, sender, len) <= 0) |
523 | || EVP_MD_CTX_ctrl(ctx, EVP_CTRL_SSL3_MASTER_SECRET, | |
28ba2541 DSH |
524 | s->session->master_key_length, |
525 | s->session->master_key) <= 0 | |
6e59a892 | 526 | || EVP_DigestFinal_ex(ctx, p, NULL) <= 0) { |
600fdc71 | 527 | SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_INTERNAL_ERROR); |
5f3d93e4 MC |
528 | ret = 0; |
529 | } | |
0f113f3e | 530 | |
bfb0641f | 531 | EVP_MD_CTX_free(ctx); |
0f113f3e | 532 | |
28ba2541 | 533 | return ret; |
0f113f3e | 534 | } |
d02b48c6 | 535 | |
6b691a5c | 536 | int ssl3_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p, |
0f113f3e MC |
537 | int len) |
538 | { | |
539 | static const unsigned char *salt[3] = { | |
ca570cfd | 540 | #ifndef CHARSET_EBCDIC |
0f113f3e MC |
541 | (const unsigned char *)"A", |
542 | (const unsigned char *)"BB", | |
543 | (const unsigned char *)"CCC", | |
ca570cfd | 544 | #else |
0f113f3e MC |
545 | (const unsigned char *)"\x41", |
546 | (const unsigned char *)"\x42\x42", | |
547 | (const unsigned char *)"\x43\x43\x43", | |
ca570cfd | 548 | #endif |
0f113f3e MC |
549 | }; |
550 | unsigned char buf[EVP_MAX_MD_SIZE]; | |
bfb0641f | 551 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
0f113f3e MC |
552 | int i, ret = 0; |
553 | unsigned int n; | |
81f57e5a | 554 | #ifdef OPENSSL_SSL_TRACE_CRYPTO |
0f113f3e | 555 | unsigned char *tmpout = out; |
1cf218bc | 556 | #endif |
d02b48c6 | 557 | |
6e59a892 RL |
558 | if (ctx == NULL) { |
559 | SSLerr(SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_MALLOC_FAILURE); | |
560 | return 0; | |
561 | } | |
0f113f3e | 562 | for (i = 0; i < 3; i++) { |
6e59a892 RL |
563 | if (EVP_DigestInit_ex(ctx, s->ctx->sha1, NULL) <= 0 |
564 | || EVP_DigestUpdate(ctx, salt[i], | |
5f3d93e4 | 565 | strlen((const char *)salt[i])) <= 0 |
6e59a892 RL |
566 | || EVP_DigestUpdate(ctx, p, len) <= 0 |
567 | || EVP_DigestUpdate(ctx, &(s->s3->client_random[0]), | |
5f3d93e4 | 568 | SSL3_RANDOM_SIZE) <= 0 |
6e59a892 | 569 | || EVP_DigestUpdate(ctx, &(s->s3->server_random[0]), |
5f3d93e4 | 570 | SSL3_RANDOM_SIZE) <= 0 |
6e59a892 | 571 | || EVP_DigestFinal_ex(ctx, buf, &n) <= 0 |
5f3d93e4 | 572 | |
6e59a892 RL |
573 | || EVP_DigestInit_ex(ctx, s->ctx->md5, NULL) <= 0 |
574 | || EVP_DigestUpdate(ctx, p, len) <= 0 | |
575 | || EVP_DigestUpdate(ctx, buf, n) <= 0 | |
576 | || EVP_DigestFinal_ex(ctx, out, &n) <= 0) { | |
5f3d93e4 MC |
577 | SSLerr(SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_INTERNAL_ERROR); |
578 | ret = 0; | |
579 | break; | |
580 | } | |
0f113f3e MC |
581 | out += n; |
582 | ret += n; | |
583 | } | |
bfb0641f | 584 | EVP_MD_CTX_free(ctx); |
1cf218bc | 585 | |
81f57e5a | 586 | #ifdef OPENSSL_SSL_TRACE_CRYPTO |
5f3d93e4 | 587 | if (ret > 0 && s->msg_callback) { |
0f113f3e MC |
588 | s->msg_callback(2, s->version, TLS1_RT_CRYPTO_PREMASTER, |
589 | p, len, s, s->msg_callback_arg); | |
590 | s->msg_callback(2, s->version, TLS1_RT_CRYPTO_CLIENT_RANDOM, | |
591 | s->s3->client_random, SSL3_RANDOM_SIZE, | |
592 | s, s->msg_callback_arg); | |
593 | s->msg_callback(2, s->version, TLS1_RT_CRYPTO_SERVER_RANDOM, | |
594 | s->s3->server_random, SSL3_RANDOM_SIZE, | |
595 | s, s->msg_callback_arg); | |
596 | s->msg_callback(2, s->version, TLS1_RT_CRYPTO_MASTER, | |
597 | tmpout, SSL3_MASTER_SECRET_SIZE, | |
598 | s, s->msg_callback_arg); | |
599 | } | |
1cf218bc | 600 | #endif |
e0f9bf1d | 601 | OPENSSL_cleanse(buf, sizeof(buf)); |
0f113f3e MC |
602 | return (ret); |
603 | } | |
d02b48c6 | 604 | |
6b691a5c | 605 | int ssl3_alert_code(int code) |
0f113f3e MC |
606 | { |
607 | switch (code) { | |
608 | case SSL_AD_CLOSE_NOTIFY: | |
609 | return (SSL3_AD_CLOSE_NOTIFY); | |
610 | case SSL_AD_UNEXPECTED_MESSAGE: | |
611 | return (SSL3_AD_UNEXPECTED_MESSAGE); | |
612 | case SSL_AD_BAD_RECORD_MAC: | |
613 | return (SSL3_AD_BAD_RECORD_MAC); | |
614 | case SSL_AD_DECRYPTION_FAILED: | |
615 | return (SSL3_AD_BAD_RECORD_MAC); | |
616 | case SSL_AD_RECORD_OVERFLOW: | |
617 | return (SSL3_AD_BAD_RECORD_MAC); | |
618 | case SSL_AD_DECOMPRESSION_FAILURE: | |
619 | return (SSL3_AD_DECOMPRESSION_FAILURE); | |
620 | case SSL_AD_HANDSHAKE_FAILURE: | |
621 | return (SSL3_AD_HANDSHAKE_FAILURE); | |
622 | case SSL_AD_NO_CERTIFICATE: | |
623 | return (SSL3_AD_NO_CERTIFICATE); | |
624 | case SSL_AD_BAD_CERTIFICATE: | |
625 | return (SSL3_AD_BAD_CERTIFICATE); | |
626 | case SSL_AD_UNSUPPORTED_CERTIFICATE: | |
627 | return (SSL3_AD_UNSUPPORTED_CERTIFICATE); | |
628 | case SSL_AD_CERTIFICATE_REVOKED: | |
629 | return (SSL3_AD_CERTIFICATE_REVOKED); | |
630 | case SSL_AD_CERTIFICATE_EXPIRED: | |
631 | return (SSL3_AD_CERTIFICATE_EXPIRED); | |
632 | case SSL_AD_CERTIFICATE_UNKNOWN: | |
633 | return (SSL3_AD_CERTIFICATE_UNKNOWN); | |
634 | case SSL_AD_ILLEGAL_PARAMETER: | |
635 | return (SSL3_AD_ILLEGAL_PARAMETER); | |
636 | case SSL_AD_UNKNOWN_CA: | |
637 | return (SSL3_AD_BAD_CERTIFICATE); | |
638 | case SSL_AD_ACCESS_DENIED: | |
639 | return (SSL3_AD_HANDSHAKE_FAILURE); | |
640 | case SSL_AD_DECODE_ERROR: | |
641 | return (SSL3_AD_HANDSHAKE_FAILURE); | |
642 | case SSL_AD_DECRYPT_ERROR: | |
643 | return (SSL3_AD_HANDSHAKE_FAILURE); | |
644 | case SSL_AD_EXPORT_RESTRICTION: | |
645 | return (SSL3_AD_HANDSHAKE_FAILURE); | |
646 | case SSL_AD_PROTOCOL_VERSION: | |
647 | return (SSL3_AD_HANDSHAKE_FAILURE); | |
648 | case SSL_AD_INSUFFICIENT_SECURITY: | |
649 | return (SSL3_AD_HANDSHAKE_FAILURE); | |
650 | case SSL_AD_INTERNAL_ERROR: | |
651 | return (SSL3_AD_HANDSHAKE_FAILURE); | |
652 | case SSL_AD_USER_CANCELLED: | |
653 | return (SSL3_AD_HANDSHAKE_FAILURE); | |
654 | case SSL_AD_NO_RENEGOTIATION: | |
655 | return (-1); /* Don't send it :-) */ | |
656 | case SSL_AD_UNSUPPORTED_EXTENSION: | |
657 | return (SSL3_AD_HANDSHAKE_FAILURE); | |
658 | case SSL_AD_CERTIFICATE_UNOBTAINABLE: | |
659 | return (SSL3_AD_HANDSHAKE_FAILURE); | |
660 | case SSL_AD_UNRECOGNIZED_NAME: | |
661 | return (SSL3_AD_HANDSHAKE_FAILURE); | |
662 | case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE: | |
663 | return (SSL3_AD_HANDSHAKE_FAILURE); | |
664 | case SSL_AD_BAD_CERTIFICATE_HASH_VALUE: | |
665 | return (SSL3_AD_HANDSHAKE_FAILURE); | |
666 | case SSL_AD_UNKNOWN_PSK_IDENTITY: | |
667 | return (TLS1_AD_UNKNOWN_PSK_IDENTITY); | |
668 | case SSL_AD_INAPPROPRIATE_FALLBACK: | |
669 | return (TLS1_AD_INAPPROPRIATE_FALLBACK); | |
06217867 EK |
670 | case SSL_AD_NO_APPLICATION_PROTOCOL: |
671 | return (TLS1_AD_NO_APPLICATION_PROTOCOL); | |
0f113f3e MC |
672 | default: |
673 | return (-1); | |
674 | } | |
675 | } |