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