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