Add the SSL_stateless() function
[openssl.git] / ssl / t1_enc.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright 2005 Nokia. All rights reserved.
4  *
5  * Licensed under the OpenSSL license (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 #include <stdio.h>
12 #include "ssl_locl.h"
13 #include <openssl/comp.h>
14 #include <openssl/evp.h>
15 #include <openssl/kdf.h>
16 #include <openssl/rand.h>
17
18 /* seed1 through seed5 are concatenated */
19 static int tls1_PRF(SSL *s,
20                     const void *seed1, size_t seed1_len,
21                     const void *seed2, size_t seed2_len,
22                     const void *seed3, size_t seed3_len,
23                     const void *seed4, size_t seed4_len,
24                     const void *seed5, size_t seed5_len,
25                     const unsigned char *sec, size_t slen,
26                     unsigned char *out, size_t olen, int fatal)
27 {
28     const EVP_MD *md = ssl_prf_md(s);
29     EVP_PKEY_CTX *pctx = NULL;
30     int ret = 0;
31
32     if (md == NULL) {
33         /* Should never happen */
34         if (fatal)
35             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_PRF,
36                      ERR_R_INTERNAL_ERROR);
37         else
38             SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
39         return 0;
40     }
41     pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL);
42     if (pctx == NULL || EVP_PKEY_derive_init(pctx) <= 0
43         || EVP_PKEY_CTX_set_tls1_prf_md(pctx, md) <= 0
44         || EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, sec, (int)slen) <= 0
45         || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed1, (int)seed1_len) <= 0
46         || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed2, (int)seed2_len) <= 0
47         || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed3, (int)seed3_len) <= 0
48         || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed4, (int)seed4_len) <= 0
49         || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed5, (int)seed5_len) <= 0
50         || EVP_PKEY_derive(pctx, out, &olen) <= 0) {
51         if (fatal)
52             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_PRF,
53                      ERR_R_INTERNAL_ERROR);
54         else
55             SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
56         goto err;
57     }
58
59     ret = 1;
60
61  err:
62     EVP_PKEY_CTX_free(pctx);
63     return ret;
64 }
65
66 static int tls1_generate_key_block(SSL *s, unsigned char *km, size_t num)
67 {
68     int ret;
69
70     /* Calls SSLfatal() as required */
71     ret = tls1_PRF(s,
72                    TLS_MD_KEY_EXPANSION_CONST,
73                    TLS_MD_KEY_EXPANSION_CONST_SIZE, s->s3->server_random,
74                    SSL3_RANDOM_SIZE, s->s3->client_random, SSL3_RANDOM_SIZE,
75                    NULL, 0, NULL, 0, s->session->master_key,
76                    s->session->master_key_length, km, num, 1);
77
78     return ret;
79 }
80
81 int tls1_change_cipher_state(SSL *s, int which)
82 {
83     unsigned char *p, *mac_secret;
84     unsigned char tmp1[EVP_MAX_KEY_LENGTH];
85     unsigned char tmp2[EVP_MAX_KEY_LENGTH];
86     unsigned char iv1[EVP_MAX_IV_LENGTH * 2];
87     unsigned char iv2[EVP_MAX_IV_LENGTH * 2];
88     unsigned char *ms, *key, *iv;
89     EVP_CIPHER_CTX *dd;
90     const EVP_CIPHER *c;
91 #ifndef OPENSSL_NO_COMP
92     const SSL_COMP *comp;
93 #endif
94     const EVP_MD *m;
95     int mac_type;
96     size_t *mac_secret_size;
97     EVP_MD_CTX *mac_ctx;
98     EVP_PKEY *mac_key;
99     size_t n, i, j, k, cl;
100     int reuse_dd = 0;
101
102     c = s->s3->tmp.new_sym_enc;
103     m = s->s3->tmp.new_hash;
104     mac_type = s->s3->tmp.new_mac_pkey_type;
105 #ifndef OPENSSL_NO_COMP
106     comp = s->s3->tmp.new_compression;
107 #endif
108
109     if (which & SSL3_CC_READ) {
110         if (s->ext.use_etm)
111             s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
112         else
113             s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
114
115         if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
116             s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
117         else
118             s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
119
120         if (s->enc_read_ctx != NULL) {
121             reuse_dd = 1;
122         } else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL) {
123             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
124                      ERR_R_MALLOC_FAILURE);
125             goto err;
126         } else {
127             /*
128              * make sure it's initialised in case we exit later with an error
129              */
130             EVP_CIPHER_CTX_reset(s->enc_read_ctx);
131         }
132         dd = s->enc_read_ctx;
133         mac_ctx = ssl_replace_hash(&s->read_hash, NULL);
134         if (mac_ctx == NULL)
135             goto err;
136 #ifndef OPENSSL_NO_COMP
137         COMP_CTX_free(s->expand);
138         s->expand = NULL;
139         if (comp != NULL) {
140             s->expand = COMP_CTX_new(comp->method);
141             if (s->expand == NULL) {
142                 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
143                          SSL_F_TLS1_CHANGE_CIPHER_STATE,
144                          SSL_R_COMPRESSION_LIBRARY_ERROR);
145                 goto err;
146             }
147         }
148 #endif
149         /*
150          * this is done by dtls1_reset_seq_numbers for DTLS
151          */
152         if (!SSL_IS_DTLS(s))
153             RECORD_LAYER_reset_read_sequence(&s->rlayer);
154         mac_secret = &(s->s3->read_mac_secret[0]);
155         mac_secret_size = &(s->s3->read_mac_secret_size);
156     } else {
157         if (s->ext.use_etm)
158             s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
159         else
160             s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
161
162         if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
163             s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
164         else
165             s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
166         if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s)) {
167             reuse_dd = 1;
168         } else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL) {
169             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
170                      ERR_R_MALLOC_FAILURE);
171             goto err;
172         }
173         dd = s->enc_write_ctx;
174         if (SSL_IS_DTLS(s)) {
175             mac_ctx = EVP_MD_CTX_new();
176             if (mac_ctx == NULL) {
177                 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
178                          SSL_F_TLS1_CHANGE_CIPHER_STATE,
179                          ERR_R_MALLOC_FAILURE);
180                 goto err;
181             }
182             s->write_hash = mac_ctx;
183         } else {
184             mac_ctx = ssl_replace_hash(&s->write_hash, NULL);
185             if (mac_ctx == NULL) {
186                 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
187                          SSL_F_TLS1_CHANGE_CIPHER_STATE,
188                          ERR_R_MALLOC_FAILURE);
189                 goto err;
190             }
191         }
192 #ifndef OPENSSL_NO_COMP
193         COMP_CTX_free(s->compress);
194         s->compress = NULL;
195         if (comp != NULL) {
196             s->compress = COMP_CTX_new(comp->method);
197             if (s->compress == NULL) {
198                 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
199                          SSL_F_TLS1_CHANGE_CIPHER_STATE,
200                         SSL_R_COMPRESSION_LIBRARY_ERROR);
201                 goto err;
202             }
203         }
204 #endif
205         /*
206          * this is done by dtls1_reset_seq_numbers for DTLS
207          */
208         if (!SSL_IS_DTLS(s))
209             RECORD_LAYER_reset_write_sequence(&s->rlayer);
210         mac_secret = &(s->s3->write_mac_secret[0]);
211         mac_secret_size = &(s->s3->write_mac_secret_size);
212     }
213
214     if (reuse_dd)
215         EVP_CIPHER_CTX_reset(dd);
216
217     p = s->s3->tmp.key_block;
218     i = *mac_secret_size = s->s3->tmp.new_mac_secret_size;
219
220     /* TODO(size_t): convert me */
221     cl = EVP_CIPHER_key_length(c);
222     j = cl;
223     /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
224     /* If GCM/CCM mode only part of IV comes from PRF */
225     if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
226         k = EVP_GCM_TLS_FIXED_IV_LEN;
227     else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
228         k = EVP_CCM_TLS_FIXED_IV_LEN;
229     else
230         k = EVP_CIPHER_iv_length(c);
231     if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
232         (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
233         ms = &(p[0]);
234         n = i + i;
235         key = &(p[n]);
236         n += j + j;
237         iv = &(p[n]);
238         n += k + k;
239     } else {
240         n = i;
241         ms = &(p[n]);
242         n += i + j;
243         key = &(p[n]);
244         n += j + k;
245         iv = &(p[n]);
246         n += k;
247     }
248
249     if (n > s->s3->tmp.key_block_length) {
250         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
251                  ERR_R_INTERNAL_ERROR);
252         goto err;
253     }
254
255     memcpy(mac_secret, ms, i);
256
257     if (!(EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
258         /* TODO(size_t): Convert this function */
259         mac_key = EVP_PKEY_new_mac_key(mac_type, NULL,
260                                        mac_secret, (int)*mac_secret_size);
261         if (mac_key == NULL
262             || EVP_DigestSignInit(mac_ctx, NULL, m, NULL, mac_key) <= 0) {
263             EVP_PKEY_free(mac_key);
264             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
265                      ERR_R_INTERNAL_ERROR);
266             goto err;
267         }
268         EVP_PKEY_free(mac_key);
269     }
270 #ifdef SSL_DEBUG
271     printf("which = %04X\nmac key=", which);
272     {
273         size_t z;
274         for (z = 0; z < i; z++)
275             printf("%02X%c", ms[z], ((z + 1) % 16) ? ' ' : '\n');
276     }
277 #endif
278
279     if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) {
280         if (!EVP_CipherInit_ex(dd, c, NULL, key, NULL, (which & SSL3_CC_WRITE))
281             || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, (int)k,
282                                     iv)) {
283             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
284                      ERR_R_INTERNAL_ERROR);
285             goto err;
286         }
287     } else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE) {
288         int taglen;
289         if (s->s3->tmp.
290             new_cipher->algorithm_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
291             taglen = EVP_CCM8_TLS_TAG_LEN;
292         else
293             taglen = EVP_CCM_TLS_TAG_LEN;
294         if (!EVP_CipherInit_ex(dd, c, NULL, NULL, NULL, (which & SSL3_CC_WRITE))
295             || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_IVLEN, 12, NULL)
296             || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_TAG, taglen, NULL)
297             || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_CCM_SET_IV_FIXED, (int)k, iv)
298             || !EVP_CipherInit_ex(dd, NULL, NULL, key, NULL, -1)) {
299             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
300                      ERR_R_INTERNAL_ERROR);
301             goto err;
302         }
303     } else {
304         if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) {
305             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
306                      ERR_R_INTERNAL_ERROR);
307             goto err;
308         }
309     }
310     /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
311     if ((EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER) && *mac_secret_size
312         && !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_MAC_KEY,
313                                 (int)*mac_secret_size, mac_secret)) {
314         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
315                  ERR_R_INTERNAL_ERROR);
316         goto err;
317     }
318
319 #ifdef SSL_DEBUG
320     printf("which = %04X\nkey=", which);
321     {
322         int z;
323         for (z = 0; z < EVP_CIPHER_key_length(c); z++)
324             printf("%02X%c", key[z], ((z + 1) % 16) ? ' ' : '\n');
325     }
326     printf("\niv=");
327     {
328         size_t z;
329         for (z = 0; z < k; z++)
330             printf("%02X%c", iv[z], ((z + 1) % 16) ? ' ' : '\n');
331     }
332     printf("\n");
333 #endif
334
335     OPENSSL_cleanse(tmp1, sizeof(tmp1));
336     OPENSSL_cleanse(tmp2, sizeof(tmp1));
337     OPENSSL_cleanse(iv1, sizeof(iv1));
338     OPENSSL_cleanse(iv2, sizeof(iv2));
339     return 1;
340  err:
341     OPENSSL_cleanse(tmp1, sizeof(tmp1));
342     OPENSSL_cleanse(tmp2, sizeof(tmp1));
343     OPENSSL_cleanse(iv1, sizeof(iv1));
344     OPENSSL_cleanse(iv2, sizeof(iv2));
345     return 0;
346 }
347
348 int tls1_setup_key_block(SSL *s)
349 {
350     unsigned char *p;
351     const EVP_CIPHER *c;
352     const EVP_MD *hash;
353     SSL_COMP *comp;
354     int mac_type = NID_undef;
355     size_t num, mac_secret_size = 0;
356     int ret = 0;
357
358     if (s->s3->tmp.key_block_length != 0)
359         return 1;
360
361     if (!ssl_cipher_get_evp(s->session, &c, &hash, &mac_type, &mac_secret_size,
362                             &comp, s->ext.use_etm)) {
363         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_SETUP_KEY_BLOCK,
364                  SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
365         return 0;
366     }
367
368     s->s3->tmp.new_sym_enc = c;
369     s->s3->tmp.new_hash = hash;
370     s->s3->tmp.new_mac_pkey_type = mac_type;
371     s->s3->tmp.new_mac_secret_size = mac_secret_size;
372     num = EVP_CIPHER_key_length(c) + mac_secret_size + EVP_CIPHER_iv_length(c);
373     num *= 2;
374
375     ssl3_cleanup_key_block(s);
376
377     if ((p = OPENSSL_malloc(num)) == NULL) {
378         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_SETUP_KEY_BLOCK,
379                  ERR_R_MALLOC_FAILURE);
380         goto err;
381     }
382
383     s->s3->tmp.key_block_length = num;
384     s->s3->tmp.key_block = p;
385
386 #ifdef SSL_DEBUG
387     printf("client random\n");
388     {
389         int z;
390         for (z = 0; z < SSL3_RANDOM_SIZE; z++)
391             printf("%02X%c", s->s3->client_random[z],
392                    ((z + 1) % 16) ? ' ' : '\n');
393     }
394     printf("server random\n");
395     {
396         int z;
397         for (z = 0; z < SSL3_RANDOM_SIZE; z++)
398             printf("%02X%c", s->s3->server_random[z],
399                    ((z + 1) % 16) ? ' ' : '\n');
400     }
401     printf("master key\n");
402     {
403         size_t z;
404         for (z = 0; z < s->session->master_key_length; z++)
405             printf("%02X%c", s->session->master_key[z],
406                    ((z + 1) % 16) ? ' ' : '\n');
407     }
408 #endif
409     if (!tls1_generate_key_block(s, p, num)) {
410         /* SSLfatal() already called */
411         goto err;
412     }
413 #ifdef SSL_DEBUG
414     printf("\nkey block\n");
415     {
416         size_t z;
417         for (z = 0; z < num; z++)
418             printf("%02X%c", p[z], ((z + 1) % 16) ? ' ' : '\n');
419     }
420 #endif
421
422     if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)
423         && s->method->version <= TLS1_VERSION) {
424         /*
425          * enable vulnerability countermeasure for CBC ciphers with known-IV
426          * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
427          */
428         s->s3->need_empty_fragments = 1;
429
430         if (s->session->cipher != NULL) {
431             if (s->session->cipher->algorithm_enc == SSL_eNULL)
432                 s->s3->need_empty_fragments = 0;
433
434 #ifndef OPENSSL_NO_RC4
435             if (s->session->cipher->algorithm_enc == SSL_RC4)
436                 s->s3->need_empty_fragments = 0;
437 #endif
438         }
439     }
440
441     ret = 1;
442  err:
443     return ret;
444 }
445
446 size_t tls1_final_finish_mac(SSL *s, const char *str, size_t slen,
447                              unsigned char *out)
448 {
449     size_t hashlen;
450     unsigned char hash[EVP_MAX_MD_SIZE];
451
452     if (!ssl3_digest_cached_records(s, 0)) {
453         /* SSLfatal() already called */
454         return 0;
455     }
456
457     if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
458         /* SSLfatal() already called */
459         return 0;
460     }
461
462     if (!tls1_PRF(s, str, slen, hash, hashlen, NULL, 0, NULL, 0, NULL, 0,
463                   s->session->master_key, s->session->master_key_length,
464                   out, TLS1_FINISH_MAC_LENGTH, 1)) {
465         /* SSLfatal() already called */
466         return 0;
467     }
468     OPENSSL_cleanse(hash, hashlen);
469     return TLS1_FINISH_MAC_LENGTH;
470 }
471
472 int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
473                                 size_t len, size_t *secret_size)
474 {
475     if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
476         unsigned char hash[EVP_MAX_MD_SIZE * 2];
477         size_t hashlen;
478         /*
479          * Digest cached records keeping record buffer (if present): this wont
480          * affect client auth because we're freezing the buffer at the same
481          * point (after client key exchange and before certificate verify)
482          */
483         if (!ssl3_digest_cached_records(s, 1)
484                 || !ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
485             /* SSLfatal() already called */
486             return 0;
487         }
488 #ifdef SSL_DEBUG
489         fprintf(stderr, "Handshake hashes:\n");
490         BIO_dump_fp(stderr, (char *)hash, hashlen);
491 #endif
492         if (!tls1_PRF(s,
493                       TLS_MD_EXTENDED_MASTER_SECRET_CONST,
494                       TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE,
495                       hash, hashlen,
496                       NULL, 0,
497                       NULL, 0,
498                       NULL, 0, p, len, out,
499                       SSL3_MASTER_SECRET_SIZE, 1)) {
500             /* SSLfatal() already called */
501             return 0;
502         }
503         OPENSSL_cleanse(hash, hashlen);
504     } else {
505         if (!tls1_PRF(s,
506                       TLS_MD_MASTER_SECRET_CONST,
507                       TLS_MD_MASTER_SECRET_CONST_SIZE,
508                       s->s3->client_random, SSL3_RANDOM_SIZE,
509                       NULL, 0,
510                       s->s3->server_random, SSL3_RANDOM_SIZE,
511                       NULL, 0, p, len, out,
512                       SSL3_MASTER_SECRET_SIZE, 1)) {
513            /* SSLfatal() already called */
514             return 0;
515         }
516     }
517 #ifdef SSL_DEBUG
518     fprintf(stderr, "Premaster Secret:\n");
519     BIO_dump_fp(stderr, (char *)p, len);
520     fprintf(stderr, "Client Random:\n");
521     BIO_dump_fp(stderr, (char *)s->s3->client_random, SSL3_RANDOM_SIZE);
522     fprintf(stderr, "Server Random:\n");
523     BIO_dump_fp(stderr, (char *)s->s3->server_random, SSL3_RANDOM_SIZE);
524     fprintf(stderr, "Master Secret:\n");
525     BIO_dump_fp(stderr, (char *)s->session->master_key,
526                 SSL3_MASTER_SECRET_SIZE);
527 #endif
528
529     *secret_size = SSL3_MASTER_SECRET_SIZE;
530     return 1;
531 }
532
533 int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
534                                 const char *label, size_t llen,
535                                 const unsigned char *context,
536                                 size_t contextlen, int use_context)
537 {
538     unsigned char *val = NULL;
539     size_t vallen = 0, currentvalpos;
540     int rv;
541
542     /*
543      * construct PRF arguments we construct the PRF argument ourself rather
544      * than passing separate values into the TLS PRF to ensure that the
545      * concatenation of values does not create a prohibited label.
546      */
547     vallen = llen + SSL3_RANDOM_SIZE * 2;
548     if (use_context) {
549         vallen += 2 + contextlen;
550     }
551
552     val = OPENSSL_malloc(vallen);
553     if (val == NULL)
554         goto err2;
555     currentvalpos = 0;
556     memcpy(val + currentvalpos, (unsigned char *)label, llen);
557     currentvalpos += llen;
558     memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE);
559     currentvalpos += SSL3_RANDOM_SIZE;
560     memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE);
561     currentvalpos += SSL3_RANDOM_SIZE;
562
563     if (use_context) {
564         val[currentvalpos] = (contextlen >> 8) & 0xff;
565         currentvalpos++;
566         val[currentvalpos] = contextlen & 0xff;
567         currentvalpos++;
568         if ((contextlen > 0) || (context != NULL)) {
569             memcpy(val + currentvalpos, context, contextlen);
570         }
571     }
572
573     /*
574      * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
575      * label len) = 15, so size of val > max(prohibited label len) = 15 and
576      * the comparisons won't have buffer overflow
577      */
578     if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
579                TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
580         goto err1;
581     if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
582                TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
583         goto err1;
584     if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
585                TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
586         goto err1;
587     if (memcmp(val, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
588                TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE) == 0)
589         goto err1;
590     if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
591                TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
592         goto err1;
593
594     rv = tls1_PRF(s,
595                   val, vallen,
596                   NULL, 0,
597                   NULL, 0,
598                   NULL, 0,
599                   NULL, 0,
600                   s->session->master_key, s->session->master_key_length,
601                   out, olen, 0);
602
603     goto ret;
604  err1:
605     SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
606     rv = 0;
607     goto ret;
608  err2:
609     SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE);
610     rv = 0;
611  ret:
612     OPENSSL_clear_free(val, vallen);
613     return rv;
614 }
615
616 int tls1_alert_code(int code)
617 {
618     switch (code) {
619     case SSL_AD_CLOSE_NOTIFY:
620         return SSL3_AD_CLOSE_NOTIFY;
621     case SSL_AD_UNEXPECTED_MESSAGE:
622         return SSL3_AD_UNEXPECTED_MESSAGE;
623     case SSL_AD_BAD_RECORD_MAC:
624         return SSL3_AD_BAD_RECORD_MAC;
625     case SSL_AD_DECRYPTION_FAILED:
626         return TLS1_AD_DECRYPTION_FAILED;
627     case SSL_AD_RECORD_OVERFLOW:
628         return TLS1_AD_RECORD_OVERFLOW;
629     case SSL_AD_DECOMPRESSION_FAILURE:
630         return SSL3_AD_DECOMPRESSION_FAILURE;
631     case SSL_AD_HANDSHAKE_FAILURE:
632         return SSL3_AD_HANDSHAKE_FAILURE;
633     case SSL_AD_NO_CERTIFICATE:
634         return -1;
635     case SSL_AD_BAD_CERTIFICATE:
636         return SSL3_AD_BAD_CERTIFICATE;
637     case SSL_AD_UNSUPPORTED_CERTIFICATE:
638         return SSL3_AD_UNSUPPORTED_CERTIFICATE;
639     case SSL_AD_CERTIFICATE_REVOKED:
640         return SSL3_AD_CERTIFICATE_REVOKED;
641     case SSL_AD_CERTIFICATE_EXPIRED:
642         return SSL3_AD_CERTIFICATE_EXPIRED;
643     case SSL_AD_CERTIFICATE_UNKNOWN:
644         return SSL3_AD_CERTIFICATE_UNKNOWN;
645     case SSL_AD_ILLEGAL_PARAMETER:
646         return SSL3_AD_ILLEGAL_PARAMETER;
647     case SSL_AD_UNKNOWN_CA:
648         return TLS1_AD_UNKNOWN_CA;
649     case SSL_AD_ACCESS_DENIED:
650         return TLS1_AD_ACCESS_DENIED;
651     case SSL_AD_DECODE_ERROR:
652         return TLS1_AD_DECODE_ERROR;
653     case SSL_AD_DECRYPT_ERROR:
654         return TLS1_AD_DECRYPT_ERROR;
655     case SSL_AD_EXPORT_RESTRICTION:
656         return TLS1_AD_EXPORT_RESTRICTION;
657     case SSL_AD_PROTOCOL_VERSION:
658         return TLS1_AD_PROTOCOL_VERSION;
659     case SSL_AD_INSUFFICIENT_SECURITY:
660         return TLS1_AD_INSUFFICIENT_SECURITY;
661     case SSL_AD_INTERNAL_ERROR:
662         return TLS1_AD_INTERNAL_ERROR;
663     case SSL_AD_USER_CANCELLED:
664         return TLS1_AD_USER_CANCELLED;
665     case SSL_AD_NO_RENEGOTIATION:
666         return TLS1_AD_NO_RENEGOTIATION;
667     case SSL_AD_UNSUPPORTED_EXTENSION:
668         return TLS1_AD_UNSUPPORTED_EXTENSION;
669     case SSL_AD_CERTIFICATE_UNOBTAINABLE:
670         return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
671     case SSL_AD_UNRECOGNIZED_NAME:
672         return TLS1_AD_UNRECOGNIZED_NAME;
673     case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
674         return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
675     case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
676         return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
677     case SSL_AD_UNKNOWN_PSK_IDENTITY:
678         return TLS1_AD_UNKNOWN_PSK_IDENTITY;
679     case SSL_AD_INAPPROPRIATE_FALLBACK:
680         return TLS1_AD_INAPPROPRIATE_FALLBACK;
681     case SSL_AD_NO_APPLICATION_PROTOCOL:
682         return TLS1_AD_NO_APPLICATION_PROTOCOL;
683     case SSL_AD_CERTIFICATE_REQUIRED:
684         return SSL_AD_HANDSHAKE_FAILURE;
685     default:
686         return -1;
687     }
688 }