Lazily initialise the compression buffer
[openssl.git] / ssl / s3_enc.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
7  *
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).
14  *
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.
21  *
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 :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
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.
51  *
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  */
57 /* ====================================================================
58  * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
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
65  *    notice, this list of conditions and the following disclaimer.
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  */
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  */
136
137 #include <stdio.h>
138 #include "ssl_locl.h"
139 #include <openssl/evp.h>
140 #include <openssl/md5.h>
141
142 static int ssl3_generate_key_block(SSL *s, unsigned char *km, int num)
143 {
144     EVP_MD_CTX *m5;
145     EVP_MD_CTX *s1;
146     unsigned char buf[16], smd[SHA_DIGEST_LENGTH];
147     unsigned char c = 'A';
148     unsigned int i, j, k;
149     int ret = 0;
150
151 #ifdef CHARSET_EBCDIC
152     c = os_toascii[c];          /* 'A' in ASCII */
153 #endif
154     k = 0;
155     m5 = EVP_MD_CTX_new();
156     s1 = EVP_MD_CTX_new();
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);
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++;
173         EVP_DigestInit_ex(s1, EVP_sha1(), NULL);
174         EVP_DigestUpdate(s1, buf, k);
175         EVP_DigestUpdate(s1, s->session->master_key,
176                          s->session->master_key_length);
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);
180
181         EVP_DigestInit_ex(m5, EVP_md5(), NULL);
182         EVP_DigestUpdate(m5, s->session->master_key,
183                          s->session->master_key_length);
184         EVP_DigestUpdate(m5, smd, SHA_DIGEST_LENGTH);
185         if ((int)(i + MD5_DIGEST_LENGTH) > num) {
186             EVP_DigestFinal_ex(m5, smd, NULL);
187             memcpy(km, smd, (num - i));
188         } else
189             EVP_DigestFinal_ex(m5, km, NULL);
190
191         km += MD5_DIGEST_LENGTH;
192     }
193     OPENSSL_cleanse(smd, sizeof(smd));
194     ret = 1;
195  err:
196     EVP_MD_CTX_free(m5);
197     EVP_MD_CTX_free(s1);
198     return ret;
199 }
200
201 int ssl3_change_cipher_state(SSL *s, int which)
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];
206     unsigned char *ms, *key, *iv;
207     EVP_CIPHER_CTX *dd;
208     const EVP_CIPHER *c;
209 #ifndef OPENSSL_NO_COMP
210     COMP_METHOD *comp;
211 #endif
212     const EVP_MD *m;
213     int n, i, j, k, cl;
214     int reuse_dd = 0;
215
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);
220 #ifndef OPENSSL_NO_COMP
221     if (s->s3->tmp.new_compression == NULL)
222         comp = NULL;
223     else
224         comp = s->s3->tmp.new_compression->method;
225 #endif
226
227     if (which & SSL3_CC_READ) {
228         if (s->enc_read_ctx != NULL)
229             reuse_dd = 1;
230         else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL)
231             goto err;
232         else
233             /*
234              * make sure it's intialized in case we exit later with an error
235              */
236             EVP_CIPHER_CTX_reset(s->enc_read_ctx);
237         dd = s->enc_read_ctx;
238
239         if (ssl_replace_hash(&s->read_hash, m) == NULL) {
240                 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
241                 goto err2;
242         }
243 #ifndef OPENSSL_NO_COMP
244         /* COMPRESS */
245         COMP_CTX_free(s->expand);
246         s->expand = NULL;
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             }
254         }
255 #endif
256         RECORD_LAYER_reset_read_sequence(&s->rlayer);
257         mac_secret = &(s->s3->read_mac_secret[0]);
258     } else {
259         if (s->enc_write_ctx != NULL)
260             reuse_dd = 1;
261         else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL)
262             goto err;
263         else
264             /*
265              * make sure it's intialized in case we exit later with an error
266              */
267             EVP_CIPHER_CTX_reset(s->enc_write_ctx);
268         dd = s->enc_write_ctx;
269         if (ssl_replace_hash(&s->write_hash, m) == NULL) {
270                 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
271                 goto err2;
272         }
273 #ifndef OPENSSL_NO_COMP
274         /* COMPRESS */
275         COMP_CTX_free(s->compress);
276         s->compress = NULL;
277         if (comp != NULL) {
278             s->compress = COMP_CTX_new(comp);
279             if (s->compress == NULL) {
280                 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE,
281                        SSL_R_COMPRESSION_LIBRARY_ERROR);
282                 goto err2;
283             }
284         }
285 #endif
286         RECORD_LAYER_reset_write_sequence(&s->rlayer);
287         mac_secret = &(s->s3->write_mac_secret[0]);
288     }
289
290     if (reuse_dd)
291         EVP_CIPHER_CTX_reset(dd);
292
293     p = s->s3->tmp.key_block;
294     i = EVP_MD_size(m);
295     if (i < 0)
296         goto err2;
297     cl = EVP_CIPHER_key_length(c);
298     j = cl;
299     k = EVP_CIPHER_iv_length(c);
300     if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
301         (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
302         ms = &(p[0]);
303         n = i + i;
304         key = &(p[n]);
305         n += j + j;
306         iv = &(p[n]);
307         n += k + k;
308     } else {
309         n = i;
310         ms = &(p[n]);
311         n += i + j;
312         key = &(p[n]);
313         n += j + k;
314         iv = &(p[n]);
315         n += k;
316     }
317
318     if (n > s->s3->tmp.key_block_length) {
319         SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
320         goto err2;
321     }
322
323     memcpy(mac_secret, ms, i);
324
325     EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE));
326
327 #ifdef OPENSSL_SSL_TRACE_CRYPTO
328     if (s->msg_callback) {
329
330         int wh = which & SSL3_CC_WRITE ?
331             TLS1_RT_CRYPTO_WRITE : TLS1_RT_CRYPTO_READ;
332         s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_MAC,
333                         mac_secret, EVP_MD_size(m), s, s->msg_callback_arg);
334         if (c->key_len)
335             s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_KEY,
336                             key, c->key_len, s, s->msg_callback_arg);
337         if (k) {
338             s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_IV,
339                             iv, k, s, s->msg_callback_arg);
340         }
341     }
342 #endif
343
344     OPENSSL_cleanse(exp_key, sizeof(exp_key));
345     OPENSSL_cleanse(exp_iv, sizeof(exp_iv));
346     return (1);
347  err:
348     SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
349  err2:
350     OPENSSL_cleanse(exp_key, sizeof(exp_key));
351     OPENSSL_cleanse(exp_iv, sizeof(exp_iv));
352     return (0);
353 }
354
355 int ssl3_setup_key_block(SSL *s)
356 {
357     unsigned char *p;
358     const EVP_CIPHER *c;
359     const EVP_MD *hash;
360     int num;
361     int ret = 0;
362     SSL_COMP *comp;
363
364     if (s->s3->tmp.key_block_length != 0)
365         return (1);
366
367     if (!ssl_cipher_get_evp(s->session, &c, &hash, NULL, NULL, &comp, 0)) {
368         SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
369         return (0);
370     }
371
372     s->s3->tmp.new_sym_enc = c;
373     s->s3->tmp.new_hash = hash;
374 #ifdef OPENSSL_NO_COMP
375     s->s3->tmp.new_compression = NULL;
376 #else
377     s->s3->tmp.new_compression = comp;
378 #endif
379
380     num = EVP_MD_size(hash);
381     if (num < 0)
382         return 0;
383
384     num = EVP_CIPHER_key_length(c) + num + EVP_CIPHER_iv_length(c);
385     num *= 2;
386
387     ssl3_cleanup_key_block(s);
388
389     if ((p = OPENSSL_malloc(num)) == NULL)
390         goto err;
391
392     s->s3->tmp.key_block_length = num;
393     s->s3->tmp.key_block = p;
394
395     ret = ssl3_generate_key_block(s, p, num);
396
397     if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)) {
398         /*
399          * enable vulnerability countermeasure for CBC ciphers with known-IV
400          * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
401          */
402         s->s3->need_empty_fragments = 1;
403
404         if (s->session->cipher != NULL) {
405             if (s->session->cipher->algorithm_enc == SSL_eNULL)
406                 s->s3->need_empty_fragments = 0;
407
408 #ifndef OPENSSL_NO_RC4
409             if (s->session->cipher->algorithm_enc == SSL_RC4)
410                 s->s3->need_empty_fragments = 0;
411 #endif
412         }
413     }
414
415     return ret;
416
417  err:
418     SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE);
419     return (0);
420 }
421
422 void ssl3_cleanup_key_block(SSL *s)
423 {
424     OPENSSL_clear_free(s->s3->tmp.key_block, s->s3->tmp.key_block_length);
425     s->s3->tmp.key_block = NULL;
426     s->s3->tmp.key_block_length = 0;
427 }
428
429 void ssl3_init_finished_mac(SSL *s)
430 {
431     ssl3_free_digest_list(s);
432     s->s3->handshake_buffer = BIO_new(BIO_s_mem());
433     (void)BIO_set_close(s->s3->handshake_buffer, BIO_CLOSE);
434 }
435
436 /*
437  * Free digest list. Also frees handshake buffer since they are always freed
438  * together.
439  */
440
441 void ssl3_free_digest_list(SSL *s)
442 {
443     BIO_free(s->s3->handshake_buffer);
444     s->s3->handshake_buffer = NULL;
445     EVP_MD_CTX_free(s->s3->handshake_dgst);
446     s->s3->handshake_dgst = NULL;
447 }
448
449 void ssl3_finish_mac(SSL *s, const unsigned char *buf, int len)
450 {
451     if (s->s3->handshake_dgst == NULL)
452         BIO_write(s->s3->handshake_buffer, (void *)buf, len);
453     else
454         EVP_DigestUpdate(s->s3->handshake_dgst, buf, len);
455 }
456
457 int ssl3_digest_cached_records(SSL *s, int keep)
458 {
459     const EVP_MD *md;
460     long hdatalen;
461     void *hdata;
462
463     if (s->s3->handshake_dgst == NULL) {
464         hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
465         if (hdatalen <= 0) {
466             SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, SSL_R_BAD_HANDSHAKE_LENGTH);
467             return 0;
468         }
469
470         s->s3->handshake_dgst = EVP_MD_CTX_new();
471         if (s->s3->handshake_dgst == NULL) {
472             SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, ERR_R_MALLOC_FAILURE);
473             return 0;
474         }
475
476         md = ssl_handshake_md(s);
477         if (md == NULL) {
478             SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, ERR_R_INTERNAL_ERROR);
479             return 0;
480         }
481
482         EVP_DigestInit_ex(s->s3->handshake_dgst, md, NULL);
483         EVP_DigestUpdate(s->s3->handshake_dgst, hdata, hdatalen);
484
485     }
486     if (keep == 0) {
487         BIO_free(s->s3->handshake_buffer);
488         s->s3->handshake_buffer = NULL;
489     }
490
491     return 1;
492 }
493
494 int ssl3_final_finish_mac(SSL *s, const char *sender, int len, unsigned char *p)
495 {
496     int ret;
497     EVP_MD_CTX *ctx = NULL;
498
499     if (!ssl3_digest_cached_records(s, 0))
500         return 0;
501
502     if (EVP_MD_CTX_type(s->s3->handshake_dgst) != NID_md5_sha1) {
503         SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, SSL_R_NO_REQUIRED_DIGEST);
504         return 0;
505     }
506
507     ctx = EVP_MD_CTX_new();
508     if (ctx == NULL) {
509         SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_MALLOC_FAILURE);
510         return 0;
511     }
512     EVP_MD_CTX_copy_ex(ctx, s->s3->handshake_dgst);
513
514     ret = EVP_MD_CTX_size(ctx);
515     if (ret < 0) {
516         EVP_MD_CTX_reset(ctx);
517         return 0;
518     }
519
520     if ((sender != NULL && EVP_DigestUpdate(ctx, sender, len) <= 0)
521             || EVP_MD_CTX_ctrl(ctx, EVP_CTRL_SSL3_MASTER_SECRET,
522                                s->session->master_key_length,
523                                s->session->master_key) <= 0
524             || EVP_DigestFinal_ex(ctx, p, NULL) <= 0) {
525         SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_INTERNAL_ERROR);
526         ret = 0;
527     }
528
529     EVP_MD_CTX_free(ctx);
530
531     return ret;
532 }
533
534 int ssl3_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
535                                 int len)
536 {
537     static const unsigned char *salt[3] = {
538 #ifndef CHARSET_EBCDIC
539         (const unsigned char *)"A",
540         (const unsigned char *)"BB",
541         (const unsigned char *)"CCC",
542 #else
543         (const unsigned char *)"\x41",
544         (const unsigned char *)"\x42\x42",
545         (const unsigned char *)"\x43\x43\x43",
546 #endif
547     };
548     unsigned char buf[EVP_MAX_MD_SIZE];
549     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
550     int i, ret = 0;
551     unsigned int n;
552 #ifdef OPENSSL_SSL_TRACE_CRYPTO
553     unsigned char *tmpout = out;
554 #endif
555
556     if (ctx == NULL) {
557         SSLerr(SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_MALLOC_FAILURE);
558         return 0;
559     }
560     for (i = 0; i < 3; i++) {
561         if (EVP_DigestInit_ex(ctx, s->ctx->sha1, NULL) <= 0
562                 || EVP_DigestUpdate(ctx, salt[i],
563                                     strlen((const char *)salt[i])) <= 0
564                 || EVP_DigestUpdate(ctx, p, len) <= 0
565                 || EVP_DigestUpdate(ctx, &(s->s3->client_random[0]),
566                                     SSL3_RANDOM_SIZE) <= 0
567                 || EVP_DigestUpdate(ctx, &(s->s3->server_random[0]),
568                                     SSL3_RANDOM_SIZE) <= 0
569                 || EVP_DigestFinal_ex(ctx, buf, &n) <= 0
570
571                 || EVP_DigestInit_ex(ctx, s->ctx->md5, NULL) <= 0
572                 || EVP_DigestUpdate(ctx, p, len) <= 0
573                 || EVP_DigestUpdate(ctx, buf, n) <= 0
574                 || EVP_DigestFinal_ex(ctx, out, &n) <= 0) {
575             SSLerr(SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_INTERNAL_ERROR);
576             ret = 0;
577             break;
578         }
579         out += n;
580         ret += n;
581     }
582     EVP_MD_CTX_free(ctx);
583
584 #ifdef OPENSSL_SSL_TRACE_CRYPTO
585     if (ret > 0 && s->msg_callback) {
586         s->msg_callback(2, s->version, TLS1_RT_CRYPTO_PREMASTER,
587                         p, len, s, s->msg_callback_arg);
588         s->msg_callback(2, s->version, TLS1_RT_CRYPTO_CLIENT_RANDOM,
589                         s->s3->client_random, SSL3_RANDOM_SIZE,
590                         s, s->msg_callback_arg);
591         s->msg_callback(2, s->version, TLS1_RT_CRYPTO_SERVER_RANDOM,
592                         s->s3->server_random, SSL3_RANDOM_SIZE,
593                         s, s->msg_callback_arg);
594         s->msg_callback(2, s->version, TLS1_RT_CRYPTO_MASTER,
595                         tmpout, SSL3_MASTER_SECRET_SIZE,
596                         s, s->msg_callback_arg);
597     }
598 #endif
599     OPENSSL_cleanse(buf, sizeof(buf));
600     return (ret);
601 }
602
603 int ssl3_alert_code(int code)
604 {
605     switch (code) {
606     case SSL_AD_CLOSE_NOTIFY:
607         return (SSL3_AD_CLOSE_NOTIFY);
608     case SSL_AD_UNEXPECTED_MESSAGE:
609         return (SSL3_AD_UNEXPECTED_MESSAGE);
610     case SSL_AD_BAD_RECORD_MAC:
611         return (SSL3_AD_BAD_RECORD_MAC);
612     case SSL_AD_DECRYPTION_FAILED:
613         return (SSL3_AD_BAD_RECORD_MAC);
614     case SSL_AD_RECORD_OVERFLOW:
615         return (SSL3_AD_BAD_RECORD_MAC);
616     case SSL_AD_DECOMPRESSION_FAILURE:
617         return (SSL3_AD_DECOMPRESSION_FAILURE);
618     case SSL_AD_HANDSHAKE_FAILURE:
619         return (SSL3_AD_HANDSHAKE_FAILURE);
620     case SSL_AD_NO_CERTIFICATE:
621         return (SSL3_AD_NO_CERTIFICATE);
622     case SSL_AD_BAD_CERTIFICATE:
623         return (SSL3_AD_BAD_CERTIFICATE);
624     case SSL_AD_UNSUPPORTED_CERTIFICATE:
625         return (SSL3_AD_UNSUPPORTED_CERTIFICATE);
626     case SSL_AD_CERTIFICATE_REVOKED:
627         return (SSL3_AD_CERTIFICATE_REVOKED);
628     case SSL_AD_CERTIFICATE_EXPIRED:
629         return (SSL3_AD_CERTIFICATE_EXPIRED);
630     case SSL_AD_CERTIFICATE_UNKNOWN:
631         return (SSL3_AD_CERTIFICATE_UNKNOWN);
632     case SSL_AD_ILLEGAL_PARAMETER:
633         return (SSL3_AD_ILLEGAL_PARAMETER);
634     case SSL_AD_UNKNOWN_CA:
635         return (SSL3_AD_BAD_CERTIFICATE);
636     case SSL_AD_ACCESS_DENIED:
637         return (SSL3_AD_HANDSHAKE_FAILURE);
638     case SSL_AD_DECODE_ERROR:
639         return (SSL3_AD_HANDSHAKE_FAILURE);
640     case SSL_AD_DECRYPT_ERROR:
641         return (SSL3_AD_HANDSHAKE_FAILURE);
642     case SSL_AD_EXPORT_RESTRICTION:
643         return (SSL3_AD_HANDSHAKE_FAILURE);
644     case SSL_AD_PROTOCOL_VERSION:
645         return (SSL3_AD_HANDSHAKE_FAILURE);
646     case SSL_AD_INSUFFICIENT_SECURITY:
647         return (SSL3_AD_HANDSHAKE_FAILURE);
648     case SSL_AD_INTERNAL_ERROR:
649         return (SSL3_AD_HANDSHAKE_FAILURE);
650     case SSL_AD_USER_CANCELLED:
651         return (SSL3_AD_HANDSHAKE_FAILURE);
652     case SSL_AD_NO_RENEGOTIATION:
653         return (-1);            /* Don't send it :-) */
654     case SSL_AD_UNSUPPORTED_EXTENSION:
655         return (SSL3_AD_HANDSHAKE_FAILURE);
656     case SSL_AD_CERTIFICATE_UNOBTAINABLE:
657         return (SSL3_AD_HANDSHAKE_FAILURE);
658     case SSL_AD_UNRECOGNIZED_NAME:
659         return (SSL3_AD_HANDSHAKE_FAILURE);
660     case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
661         return (SSL3_AD_HANDSHAKE_FAILURE);
662     case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
663         return (SSL3_AD_HANDSHAKE_FAILURE);
664     case SSL_AD_UNKNOWN_PSK_IDENTITY:
665         return (TLS1_AD_UNKNOWN_PSK_IDENTITY);
666     case SSL_AD_INAPPROPRIATE_FALLBACK:
667         return (TLS1_AD_INAPPROPRIATE_FALLBACK);
668     case SSL_AD_NO_APPLICATION_PROTOCOL:
669         return (TLS1_AD_NO_APPLICATION_PROTOCOL);
670     default:
671         return (-1);
672     }
673 }