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