/src/openssl31/ssl/t1_enc.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2022 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_local.h"  | 
13  |  | #include "record/record_local.h"  | 
14  |  | #include "internal/ktls.h"  | 
15  |  | #include "internal/cryptlib.h"  | 
16  |  | #include <openssl/comp.h>  | 
17  |  | #include <openssl/evp.h>  | 
18  |  | #include <openssl/kdf.h>  | 
19  |  | #include <openssl/rand.h>  | 
20  |  | #include <openssl/obj_mac.h>  | 
21  |  | #include <openssl/core_names.h>  | 
22  |  | #include <openssl/trace.h>  | 
23  |  |  | 
24  |  | /* seed1 through seed5 are concatenated */  | 
25  |  | static int tls1_PRF(SSL *s,  | 
26  |  |                     const void *seed1, size_t seed1_len,  | 
27  |  |                     const void *seed2, size_t seed2_len,  | 
28  |  |                     const void *seed3, size_t seed3_len,  | 
29  |  |                     const void *seed4, size_t seed4_len,  | 
30  |  |                     const void *seed5, size_t seed5_len,  | 
31  |  |                     const unsigned char *sec, size_t slen,  | 
32  |  |                     unsigned char *out, size_t olen, int fatal)  | 
33  | 27.5k  | { | 
34  | 27.5k  |     const EVP_MD *md = ssl_prf_md(s);  | 
35  | 27.5k  |     EVP_KDF *kdf;  | 
36  | 27.5k  |     EVP_KDF_CTX *kctx = NULL;  | 
37  | 27.5k  |     OSSL_PARAM params[8], *p = params;  | 
38  | 27.5k  |     const char *mdname;  | 
39  |  |  | 
40  | 27.5k  |     if (md == NULL) { | 
41  |  |         /* Should never happen */  | 
42  | 0  |         if (fatal)  | 
43  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
44  | 0  |         else  | 
45  | 0  |             ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);  | 
46  | 0  |         return 0;  | 
47  | 0  |     }  | 
48  | 27.5k  |     kdf = EVP_KDF_fetch(s->ctx->libctx, OSSL_KDF_NAME_TLS1_PRF, s->ctx->propq);  | 
49  | 27.5k  |     if (kdf == NULL)  | 
50  | 0  |         goto err;  | 
51  | 27.5k  |     kctx = EVP_KDF_CTX_new(kdf);  | 
52  | 27.5k  |     EVP_KDF_free(kdf);  | 
53  | 27.5k  |     if (kctx == NULL)  | 
54  | 0  |         goto err;  | 
55  | 27.5k  |     mdname = EVP_MD_get0_name(md);  | 
56  | 27.5k  |     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,  | 
57  | 27.5k  |                                             (char *)mdname, 0);  | 
58  | 27.5k  |     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET,  | 
59  | 27.5k  |                                              (unsigned char *)sec,  | 
60  | 27.5k  |                                              (size_t)slen);  | 
61  | 27.5k  |     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,  | 
62  | 27.5k  |                                              (void *)seed1, (size_t)seed1_len);  | 
63  | 27.5k  |     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,  | 
64  | 27.5k  |                                              (void *)seed2, (size_t)seed2_len);  | 
65  | 27.5k  |     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,  | 
66  | 27.5k  |                                              (void *)seed3, (size_t)seed3_len);  | 
67  | 27.5k  |     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,  | 
68  | 27.5k  |                                              (void *)seed4, (size_t)seed4_len);  | 
69  | 27.5k  |     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,  | 
70  | 27.5k  |                                              (void *)seed5, (size_t)seed5_len);  | 
71  | 27.5k  |     *p = OSSL_PARAM_construct_end();  | 
72  | 27.5k  |     if (EVP_KDF_derive(kctx, out, olen, params)) { | 
73  | 27.5k  |         EVP_KDF_CTX_free(kctx);  | 
74  | 27.5k  |         return 1;  | 
75  | 27.5k  |     }  | 
76  |  |  | 
77  | 0  |  err:  | 
78  | 0  |     if (fatal)  | 
79  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
80  | 0  |     else  | 
81  | 0  |         ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);  | 
82  | 0  |     EVP_KDF_CTX_free(kctx);  | 
83  | 0  |     return 0;  | 
84  | 27.5k  | }  | 
85  |  |  | 
86  |  | static int tls1_generate_key_block(SSL *s, unsigned char *km, size_t num)  | 
87  | 9.29k  | { | 
88  | 9.29k  |     int ret;  | 
89  |  |  | 
90  |  |     /* Calls SSLfatal() as required */  | 
91  | 9.29k  |     ret = tls1_PRF(s,  | 
92  | 9.29k  |                    TLS_MD_KEY_EXPANSION_CONST,  | 
93  | 9.29k  |                    TLS_MD_KEY_EXPANSION_CONST_SIZE, s->s3.server_random,  | 
94  | 9.29k  |                    SSL3_RANDOM_SIZE, s->s3.client_random, SSL3_RANDOM_SIZE,  | 
95  | 9.29k  |                    NULL, 0, NULL, 0, s->session->master_key,  | 
96  | 9.29k  |                    s->session->master_key_length, km, num, 1);  | 
97  |  |  | 
98  | 9.29k  |     return ret;  | 
99  | 9.29k  | }  | 
100  |  |  | 
101  |  | #ifndef OPENSSL_NO_KTLS  | 
102  |  |  /*  | 
103  |  |   * Count the number of records that were not processed yet from record boundary.  | 
104  |  |   *  | 
105  |  |   * This function assumes that there are only fully formed records read in the  | 
106  |  |   * record layer. If read_ahead is enabled, then this might be false and this  | 
107  |  |   * function will fail.  | 
108  |  |   */  | 
109  |  | # ifndef OPENSSL_NO_KTLS_RX  | 
110  |  | static int count_unprocessed_records(SSL *s)  | 
111  |  | { | 
112  |  |     SSL3_BUFFER *rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);  | 
113  |  |     PACKET pkt, subpkt;  | 
114  |  |     int count = 0;  | 
115  |  |  | 
116  |  |     if (!PACKET_buf_init(&pkt, rbuf->buf + rbuf->offset, rbuf->left))  | 
117  |  |         return -1;  | 
118  |  |  | 
119  |  |     while (PACKET_remaining(&pkt) > 0) { | 
120  |  |         /* Skip record type and version */  | 
121  |  |         if (!PACKET_forward(&pkt, 3))  | 
122  |  |             return -1;  | 
123  |  |  | 
124  |  |         /* Read until next record */  | 
125  |  |         if (!PACKET_get_length_prefixed_2(&pkt, &subpkt))  | 
126  |  |             return -1;  | 
127  |  |  | 
128  |  |         count += 1;  | 
129  |  |     }  | 
130  |  |  | 
131  |  |     return count;  | 
132  |  | }  | 
133  |  | # endif  | 
134  |  | #endif  | 
135  |  |  | 
136  |  |  | 
137  |  | int tls_provider_set_tls_params(SSL *s, EVP_CIPHER_CTX *ctx,  | 
138  |  |                                 const EVP_CIPHER *ciph,  | 
139  |  |                                 const EVP_MD *md)  | 
140  | 4.65k  | { | 
141  |  |     /*  | 
142  |  |      * Provided cipher, the TLS padding/MAC removal is performed provider  | 
143  |  |      * side so we need to tell the ctx about our TLS version and mac size  | 
144  |  |      */  | 
145  | 4.65k  |     OSSL_PARAM params[3], *pprm = params;  | 
146  | 4.65k  |     size_t macsize = 0;  | 
147  | 4.65k  |     int imacsize = -1;  | 
148  |  |  | 
149  | 4.65k  |     if ((EVP_CIPHER_get_flags(ciph) & EVP_CIPH_FLAG_AEAD_CIPHER) == 0  | 
150  |  |                /*  | 
151  |  |                 * We look at s->ext.use_etm instead of SSL_READ_ETM() or  | 
152  |  |                 * SSL_WRITE_ETM() because this test applies to both reading  | 
153  |  |                 * and writing.  | 
154  |  |                 */  | 
155  | 4.65k  |             && !s->ext.use_etm)  | 
156  | 1.81k  |         imacsize = EVP_MD_get_size(md);  | 
157  | 4.65k  |     if (imacsize >= 0)  | 
158  | 1.81k  |         macsize = (size_t)imacsize;  | 
159  |  |  | 
160  | 4.65k  |     *pprm++ = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION,  | 
161  | 4.65k  |                                        &s->version);  | 
162  | 4.65k  |     *pprm++ = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_TLS_MAC_SIZE,  | 
163  | 4.65k  |                                           &macsize);  | 
164  | 4.65k  |     *pprm = OSSL_PARAM_construct_end();  | 
165  |  |  | 
166  | 4.65k  |     if (!EVP_CIPHER_CTX_set_params(ctx, params)) { | 
167  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
168  | 0  |         return 0;  | 
169  | 0  |     }  | 
170  |  |  | 
171  | 4.65k  |     return 1;  | 
172  | 4.65k  | }  | 
173  |  |  | 
174  |  |  | 
175  |  | static int tls_iv_length_within_key_block(const EVP_CIPHER *c)  | 
176  | 21.2k  | { | 
177  |  |     /* If GCM/CCM mode only part of IV comes from PRF */  | 
178  | 21.2k  |     if (EVP_CIPHER_get_mode(c) == EVP_CIPH_GCM_MODE)  | 
179  | 3.32k  |         return EVP_GCM_TLS_FIXED_IV_LEN;  | 
180  | 17.9k  |     else if (EVP_CIPHER_get_mode(c) == EVP_CIPH_CCM_MODE)  | 
181  | 987  |         return EVP_CCM_TLS_FIXED_IV_LEN;  | 
182  | 16.9k  |     else  | 
183  | 16.9k  |         return EVP_CIPHER_get_iv_length(c);  | 
184  | 21.2k  | }  | 
185  |  |  | 
186  |  | int tls1_change_cipher_state(SSL *s, int which)  | 
187  | 3.82k  | { | 
188  | 3.82k  |     unsigned char *p, *mac_secret;  | 
189  | 3.82k  |     unsigned char *ms, *key, *iv;  | 
190  | 3.82k  |     EVP_CIPHER_CTX *dd;  | 
191  | 3.82k  |     const EVP_CIPHER *c;  | 
192  | 3.82k  | #ifndef OPENSSL_NO_COMP  | 
193  | 3.82k  |     const SSL_COMP *comp;  | 
194  | 3.82k  | #endif  | 
195  | 3.82k  |     const EVP_MD *m;  | 
196  | 3.82k  |     int mac_type;  | 
197  | 3.82k  |     size_t *mac_secret_size;  | 
198  | 3.82k  |     EVP_MD_CTX *mac_ctx;  | 
199  | 3.82k  |     EVP_PKEY *mac_key;  | 
200  | 3.82k  |     size_t n, i, j, k, cl;  | 
201  | 3.82k  |     int reuse_dd = 0;  | 
202  |  | #ifndef OPENSSL_NO_KTLS  | 
203  |  |     ktls_crypto_info_t crypto_info;  | 
204  |  |     unsigned char *rec_seq;  | 
205  |  |     void *rl_sequence;  | 
206  |  | # ifndef OPENSSL_NO_KTLS_RX  | 
207  |  |     int count_unprocessed;  | 
208  |  |     int bit;  | 
209  |  | # endif  | 
210  |  |     BIO *bio;  | 
211  |  | #endif  | 
212  |  |  | 
213  | 3.82k  |     c = s->s3.tmp.new_sym_enc;  | 
214  | 3.82k  |     m = s->s3.tmp.new_hash;  | 
215  | 3.82k  |     mac_type = s->s3.tmp.new_mac_pkey_type;  | 
216  | 3.82k  | #ifndef OPENSSL_NO_COMP  | 
217  | 3.82k  |     comp = s->s3.tmp.new_compression;  | 
218  | 3.82k  | #endif  | 
219  |  |  | 
220  | 3.82k  |     if (which & SSL3_CC_READ) { | 
221  | 1.46k  |         if (s->ext.use_etm)  | 
222  | 182  |             s->s3.flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;  | 
223  | 1.28k  |         else  | 
224  | 1.28k  |             s->s3.flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;  | 
225  |  |  | 
226  | 1.46k  |         if (s->s3.tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)  | 
227  | 0  |             s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;  | 
228  | 1.46k  |         else  | 
229  | 1.46k  |             s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;  | 
230  |  |  | 
231  | 1.46k  |         if (s->s3.tmp.new_cipher->algorithm2 & TLS1_TLSTREE)  | 
232  | 0  |             s->mac_flags |= SSL_MAC_FLAG_READ_MAC_TLSTREE;  | 
233  | 1.46k  |         else  | 
234  | 1.46k  |             s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_TLSTREE;  | 
235  |  |  | 
236  | 1.46k  |         if (s->enc_read_ctx != NULL) { | 
237  | 0  |             reuse_dd = 1;  | 
238  | 1.46k  |         } else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL) { | 
239  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);  | 
240  | 0  |             goto err;  | 
241  | 1.46k  |         } else { | 
242  |  |             /*  | 
243  |  |              * make sure it's initialised in case we exit later with an error  | 
244  |  |              */  | 
245  | 1.46k  |             EVP_CIPHER_CTX_reset(s->enc_read_ctx);  | 
246  | 1.46k  |         }  | 
247  | 1.46k  |         dd = s->enc_read_ctx;  | 
248  | 1.46k  |         mac_ctx = ssl_replace_hash(&s->read_hash, NULL);  | 
249  | 1.46k  |         if (mac_ctx == NULL) { | 
250  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
251  | 0  |             goto err;  | 
252  | 0  |         }  | 
253  | 1.46k  | #ifndef OPENSSL_NO_COMP  | 
254  | 1.46k  |         COMP_CTX_free(s->expand);  | 
255  | 1.46k  |         s->expand = NULL;  | 
256  | 1.46k  |         if (comp != NULL) { | 
257  | 0  |             s->expand = COMP_CTX_new(comp->method);  | 
258  | 0  |             if (s->expand == NULL) { | 
259  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR,  | 
260  | 0  |                          SSL_R_COMPRESSION_LIBRARY_ERROR);  | 
261  | 0  |                 goto err;  | 
262  | 0  |             }  | 
263  | 0  |         }  | 
264  | 1.46k  | #endif  | 
265  |  |         /*  | 
266  |  |          * this is done by dtls1_reset_seq_numbers for DTLS  | 
267  |  |          */  | 
268  | 1.46k  |         if (!SSL_IS_DTLS(s))  | 
269  | 1.46k  |             RECORD_LAYER_reset_read_sequence(&s->rlayer);  | 
270  | 1.46k  |         mac_secret = &(s->s3.read_mac_secret[0]);  | 
271  | 1.46k  |         mac_secret_size = &(s->s3.read_mac_secret_size);  | 
272  | 2.35k  |     } else { | 
273  | 2.35k  |         s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;  | 
274  | 2.35k  |         if (s->ext.use_etm)  | 
275  | 231  |             s->s3.flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;  | 
276  | 2.12k  |         else  | 
277  | 2.12k  |             s->s3.flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;  | 
278  |  |  | 
279  | 2.35k  |         if (s->s3.tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)  | 
280  | 0  |             s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;  | 
281  | 2.35k  |         else  | 
282  | 2.35k  |             s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;  | 
283  |  |  | 
284  | 2.35k  |         if (s->s3.tmp.new_cipher->algorithm2 & TLS1_TLSTREE)  | 
285  | 0  |             s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_TLSTREE;  | 
286  | 2.35k  |         else  | 
287  | 2.35k  |             s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_TLSTREE;  | 
288  | 2.35k  |         if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s)) { | 
289  | 0  |             reuse_dd = 1;  | 
290  | 2.35k  |         } else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL) { | 
291  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);  | 
292  | 0  |             goto err;  | 
293  | 0  |         }  | 
294  | 2.35k  |         dd = s->enc_write_ctx;  | 
295  | 2.35k  |         if (SSL_IS_DTLS(s)) { | 
296  | 0  |             mac_ctx = EVP_MD_CTX_new();  | 
297  | 0  |             if (mac_ctx == NULL) { | 
298  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);  | 
299  | 0  |                 goto err;  | 
300  | 0  |             }  | 
301  | 0  |             s->write_hash = mac_ctx;  | 
302  | 2.35k  |         } else { | 
303  | 2.35k  |             mac_ctx = ssl_replace_hash(&s->write_hash, NULL);  | 
304  | 2.35k  |             if (mac_ctx == NULL) { | 
305  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);  | 
306  | 0  |                 goto err;  | 
307  | 0  |             }  | 
308  | 2.35k  |         }  | 
309  | 2.35k  | #ifndef OPENSSL_NO_COMP  | 
310  | 2.35k  |         COMP_CTX_free(s->compress);  | 
311  | 2.35k  |         s->compress = NULL;  | 
312  | 2.35k  |         if (comp != NULL) { | 
313  | 0  |             s->compress = COMP_CTX_new(comp->method);  | 
314  | 0  |             if (s->compress == NULL) { | 
315  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR,  | 
316  | 0  |                          SSL_R_COMPRESSION_LIBRARY_ERROR);  | 
317  | 0  |                 goto err;  | 
318  | 0  |             }  | 
319  | 0  |         }  | 
320  | 2.35k  | #endif  | 
321  |  |         /*  | 
322  |  |          * this is done by dtls1_reset_seq_numbers for DTLS  | 
323  |  |          */  | 
324  | 2.35k  |         if (!SSL_IS_DTLS(s))  | 
325  | 2.35k  |             RECORD_LAYER_reset_write_sequence(&s->rlayer);  | 
326  | 2.35k  |         mac_secret = &(s->s3.write_mac_secret[0]);  | 
327  | 2.35k  |         mac_secret_size = &(s->s3.write_mac_secret_size);  | 
328  | 2.35k  |     }  | 
329  |  |  | 
330  | 3.82k  |     if (reuse_dd)  | 
331  | 0  |         EVP_CIPHER_CTX_reset(dd);  | 
332  |  |  | 
333  | 3.82k  |     p = s->s3.tmp.key_block;  | 
334  | 3.82k  |     i = *mac_secret_size = s->s3.tmp.new_mac_secret_size;  | 
335  |  |  | 
336  | 3.82k  |     cl = EVP_CIPHER_get_key_length(c);  | 
337  | 3.82k  |     j = cl;  | 
338  | 3.82k  |     k = tls_iv_length_within_key_block(c);  | 
339  | 3.82k  |     if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||  | 
340  | 3.82k  |         (which == SSL3_CHANGE_CIPHER_SERVER_READ)) { | 
341  | 3.08k  |         ms = &(p[0]);  | 
342  | 3.08k  |         n = i + i;  | 
343  | 3.08k  |         key = &(p[n]);  | 
344  | 3.08k  |         n += j + j;  | 
345  | 3.08k  |         iv = &(p[n]);  | 
346  | 3.08k  |         n += k + k;  | 
347  | 3.08k  |     } else { | 
348  | 743  |         n = i;  | 
349  | 743  |         ms = &(p[n]);  | 
350  | 743  |         n += i + j;  | 
351  | 743  |         key = &(p[n]);  | 
352  | 743  |         n += j + k;  | 
353  | 743  |         iv = &(p[n]);  | 
354  | 743  |         n += k;  | 
355  | 743  |     }  | 
356  |  |  | 
357  | 3.82k  |     if (n > s->s3.tmp.key_block_length) { | 
358  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
359  | 0  |         goto err;  | 
360  | 0  |     }  | 
361  |  |  | 
362  | 3.82k  |     memcpy(mac_secret, ms, i);  | 
363  |  |  | 
364  | 3.82k  |     if (!(EVP_CIPHER_get_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)) { | 
365  | 1.40k  |         if (mac_type == EVP_PKEY_HMAC) { | 
366  | 1.40k  |             mac_key = EVP_PKEY_new_raw_private_key_ex(s->ctx->libctx, "HMAC",  | 
367  | 1.40k  |                                                       s->ctx->propq, mac_secret,  | 
368  | 1.40k  |                                                       *mac_secret_size);  | 
369  | 1.40k  |         } else { | 
370  |  |             /*  | 
371  |  |              * If its not HMAC then the only other types of MAC we support are  | 
372  |  |              * the GOST MACs, so we need to use the old style way of creating  | 
373  |  |              * a MAC key.  | 
374  |  |              */  | 
375  | 0  |             mac_key = EVP_PKEY_new_mac_key(mac_type, NULL, mac_secret,  | 
376  | 0  |                                            (int)*mac_secret_size);  | 
377  | 0  |         }  | 
378  | 1.40k  |         if (mac_key == NULL  | 
379  | 1.40k  |             || EVP_DigestSignInit_ex(mac_ctx, NULL, EVP_MD_get0_name(m),  | 
380  | 1.40k  |                                      s->ctx->libctx, s->ctx->propq, mac_key,  | 
381  | 1.40k  |                                      NULL) <= 0) { | 
382  | 0  |             EVP_PKEY_free(mac_key);  | 
383  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
384  | 0  |             goto err;  | 
385  | 0  |         }  | 
386  | 1.40k  |         EVP_PKEY_free(mac_key);  | 
387  | 1.40k  |     }  | 
388  |  |  | 
389  | 3.82k  |     OSSL_TRACE_BEGIN(TLS) { | 
390  | 0  |         BIO_printf(trc_out, "which = %04X, mac key:\n", which);  | 
391  | 0  |         BIO_dump_indent(trc_out, ms, i, 4);  | 
392  | 3.82k  |     } OSSL_TRACE_END(TLS);  | 
393  |  |  | 
394  | 3.82k  |     if (EVP_CIPHER_get_mode(c) == EVP_CIPH_GCM_MODE) { | 
395  | 1.07k  |         if (!EVP_CipherInit_ex(dd, c, NULL, key, NULL, (which & SSL3_CC_WRITE))  | 
396  | 1.07k  |             || EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, (int)k,  | 
397  | 1.07k  |                                     iv) <= 0) { | 
398  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
399  | 0  |             goto err;  | 
400  | 0  |         }  | 
401  | 2.75k  |     } else if (EVP_CIPHER_get_mode(c) == EVP_CIPH_CCM_MODE) { | 
402  | 196  |         int taglen;  | 
403  | 196  |         if (s->s3.tmp.  | 
404  | 196  |             new_cipher->algorithm_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))  | 
405  | 147  |             taglen = EVP_CCM8_TLS_TAG_LEN;  | 
406  | 49  |         else  | 
407  | 49  |             taglen = EVP_CCM_TLS_TAG_LEN;  | 
408  | 196  |         if (!EVP_CipherInit_ex(dd, c, NULL, NULL, NULL, (which & SSL3_CC_WRITE))  | 
409  | 196  |             || (EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_IVLEN, 12, NULL) <= 0)  | 
410  | 196  |             || (EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_TAG, taglen, NULL) <= 0)  | 
411  | 196  |             || (EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_CCM_SET_IV_FIXED, (int)k, iv) <= 0)  | 
412  | 196  |             || !EVP_CipherInit_ex(dd, NULL, NULL, key, NULL, -1)) { | 
413  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
414  | 0  |             goto err;  | 
415  | 0  |         }  | 
416  | 2.56k  |     } else { | 
417  | 2.56k  |         if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) { | 
418  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
419  | 0  |             goto err;  | 
420  | 0  |         }  | 
421  | 2.56k  |     }  | 
422  |  |     /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */  | 
423  | 3.82k  |     if ((EVP_CIPHER_get_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)  | 
424  | 3.82k  |         && *mac_secret_size  | 
425  | 3.82k  |         && EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_MAC_KEY,  | 
426  | 1.09k  |                                 (int)*mac_secret_size, mac_secret) <= 0) { | 
427  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
428  | 0  |         goto err;  | 
429  | 0  |     }  | 
430  |  |  | 
431  |  |     /*  | 
432  |  |      * The cipher we actually ended up using in the EVP_CIPHER_CTX may be  | 
433  |  |      * different to that in c if we have an ENGINE in use  | 
434  |  |      */  | 
435  | 3.82k  |     if (EVP_CIPHER_get0_provider(EVP_CIPHER_CTX_get0_cipher(dd)) != NULL  | 
436  | 3.82k  |             && !tls_provider_set_tls_params(s, dd, c, m)) { | 
437  |  |         /* SSLfatal already called */  | 
438  | 0  |         goto err;  | 
439  | 0  |     }  | 
440  |  |  | 
441  |  | #ifndef OPENSSL_NO_KTLS  | 
442  |  |     if (s->compress || (s->options & SSL_OP_ENABLE_KTLS) == 0)  | 
443  |  |         goto skip_ktls;  | 
444  |  |  | 
445  |  |     /* ktls supports only the maximum fragment size */  | 
446  |  |     if (ssl_get_max_send_fragment(s) != SSL3_RT_MAX_PLAIN_LENGTH)  | 
447  |  |         goto skip_ktls;  | 
448  |  |  | 
449  |  |     /* check that cipher is supported */  | 
450  |  |     if (!ktls_check_supported_cipher(s, c, dd))  | 
451  |  |         goto skip_ktls;  | 
452  |  |  | 
453  |  |     if (which & SSL3_CC_WRITE)  | 
454  |  |         bio = s->wbio;  | 
455  |  |     else  | 
456  |  |         bio = s->rbio;  | 
457  |  |  | 
458  |  |     if (!ossl_assert(bio != NULL)) { | 
459  |  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
460  |  |         goto err;  | 
461  |  |     }  | 
462  |  |  | 
463  |  |     /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */  | 
464  |  |     if (which & SSL3_CC_WRITE) { | 
465  |  |        if (BIO_flush(bio) <= 0)  | 
466  |  |            goto skip_ktls;  | 
467  |  |     }  | 
468  |  |  | 
469  |  |     /* ktls doesn't support renegotiation */  | 
470  |  |     if ((BIO_get_ktls_send(s->wbio) && (which & SSL3_CC_WRITE)) ||  | 
471  |  |         (BIO_get_ktls_recv(s->rbio) && (which & SSL3_CC_READ))) { | 
472  |  |         SSLfatal(s, SSL_AD_NO_RENEGOTIATION, ERR_R_INTERNAL_ERROR);  | 
473  |  |         goto err;  | 
474  |  |     }  | 
475  |  |  | 
476  |  |     if (which & SSL3_CC_WRITE)  | 
477  |  |         rl_sequence = RECORD_LAYER_get_write_sequence(&s->rlayer);  | 
478  |  |     else  | 
479  |  |         rl_sequence = RECORD_LAYER_get_read_sequence(&s->rlayer);  | 
480  |  |  | 
481  |  |     if (!ktls_configure_crypto(s, c, dd, rl_sequence, &crypto_info, &rec_seq,  | 
482  |  |                                iv, key, ms, *mac_secret_size))  | 
483  |  |         goto skip_ktls;  | 
484  |  |  | 
485  |  |     if (which & SSL3_CC_READ) { | 
486  |  | # ifndef OPENSSL_NO_KTLS_RX  | 
487  |  |         count_unprocessed = count_unprocessed_records(s);  | 
488  |  |         if (count_unprocessed < 0)  | 
489  |  |             goto skip_ktls;  | 
490  |  |  | 
491  |  |         /* increment the crypto_info record sequence */  | 
492  |  |         while (count_unprocessed) { | 
493  |  |             for (bit = 7; bit >= 0; bit--) { /* increment */ | 
494  |  |                 ++rec_seq[bit];  | 
495  |  |                 if (rec_seq[bit] != 0)  | 
496  |  |                     break;  | 
497  |  |             }  | 
498  |  |             count_unprocessed--;  | 
499  |  |         }  | 
500  |  | # else  | 
501  |  |         goto skip_ktls;  | 
502  |  | # endif  | 
503  |  |     }  | 
504  |  |  | 
505  |  |     /* ktls works with user provided buffers directly */  | 
506  |  |     if (BIO_set_ktls(bio, &crypto_info, which & SSL3_CC_WRITE)) { | 
507  |  |         if (which & SSL3_CC_WRITE)  | 
508  |  |             ssl3_release_write_buffer(s);  | 
509  |  |         SSL_set_options(s, SSL_OP_NO_RENEGOTIATION);  | 
510  |  |     }  | 
511  |  |  | 
512  |  |  skip_ktls:  | 
513  |  | #endif                          /* OPENSSL_NO_KTLS */  | 
514  | 3.82k  |     s->statem.enc_write_state = ENC_WRITE_STATE_VALID;  | 
515  |  |  | 
516  | 3.82k  |     OSSL_TRACE_BEGIN(TLS) { | 
517  | 0  |         BIO_printf(trc_out, "which = %04X, key:\n", which);  | 
518  | 0  |         BIO_dump_indent(trc_out, key, EVP_CIPHER_get_key_length(c), 4);  | 
519  | 0  |         BIO_printf(trc_out, "iv:\n");  | 
520  | 0  |         BIO_dump_indent(trc_out, iv, k, 4);  | 
521  | 3.82k  |     } OSSL_TRACE_END(TLS);  | 
522  |  |  | 
523  | 3.82k  |     return 1;  | 
524  | 0  |  err:  | 
525  | 0  |     return 0;  | 
526  | 3.82k  | }  | 
527  |  |  | 
528  |  | int tls1_setup_key_block(SSL *s)  | 
529  | 3.11k  | { | 
530  | 3.11k  |     unsigned char *p;  | 
531  | 3.11k  |     const EVP_CIPHER *c;  | 
532  | 3.11k  |     const EVP_MD *hash;  | 
533  | 3.11k  |     SSL_COMP *comp;  | 
534  | 3.11k  |     int mac_type = NID_undef;  | 
535  | 3.11k  |     size_t num, mac_secret_size = 0;  | 
536  | 3.11k  |     int ret = 0;  | 
537  |  |  | 
538  | 3.11k  |     if (s->s3.tmp.key_block_length != 0)  | 
539  | 0  |         return 1;  | 
540  |  |  | 
541  | 3.11k  |     if (!ssl_cipher_get_evp(s->ctx, s->session, &c, &hash, &mac_type,  | 
542  | 3.11k  |                             &mac_secret_size, &comp, s->ext.use_etm)) { | 
543  |  |         /* Error is already recorded */  | 
544  | 0  |         SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);  | 
545  | 0  |         return 0;  | 
546  | 0  |     }  | 
547  |  |  | 
548  | 3.11k  |     ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);  | 
549  | 3.11k  |     s->s3.tmp.new_sym_enc = c;  | 
550  | 3.11k  |     ssl_evp_md_free(s->s3.tmp.new_hash);  | 
551  | 3.11k  |     s->s3.tmp.new_hash = hash;  | 
552  | 3.11k  |     s->s3.tmp.new_mac_pkey_type = mac_type;  | 
553  | 3.11k  |     s->s3.tmp.new_mac_secret_size = mac_secret_size;  | 
554  | 3.11k  |     num = mac_secret_size + EVP_CIPHER_get_key_length(c)  | 
555  | 3.11k  |           + tls_iv_length_within_key_block(c);  | 
556  | 3.11k  |     num *= 2;  | 
557  |  |  | 
558  | 3.11k  |     ssl3_cleanup_key_block(s);  | 
559  |  |  | 
560  | 3.11k  |     if ((p = OPENSSL_malloc(num)) == NULL) { | 
561  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);  | 
562  | 0  |         goto err;  | 
563  | 0  |     }  | 
564  |  |  | 
565  | 3.11k  |     s->s3.tmp.key_block_length = num;  | 
566  | 3.11k  |     s->s3.tmp.key_block = p;  | 
567  |  |  | 
568  | 3.11k  |     OSSL_TRACE_BEGIN(TLS) { | 
569  | 0  |         BIO_printf(trc_out, "key block length: %zu\n", num);  | 
570  | 0  |         BIO_printf(trc_out, "client random\n");  | 
571  | 0  |         BIO_dump_indent(trc_out, s->s3.client_random, SSL3_RANDOM_SIZE, 4);  | 
572  | 0  |         BIO_printf(trc_out, "server random\n");  | 
573  | 0  |         BIO_dump_indent(trc_out, s->s3.server_random, SSL3_RANDOM_SIZE, 4);  | 
574  | 0  |         BIO_printf(trc_out, "master key\n");  | 
575  | 0  |         BIO_dump_indent(trc_out,  | 
576  | 0  |                         s->session->master_key,  | 
577  | 0  |                         s->session->master_key_length, 4);  | 
578  | 3.11k  |     } OSSL_TRACE_END(TLS);  | 
579  |  |  | 
580  | 3.11k  |     if (!tls1_generate_key_block(s, p, num)) { | 
581  |  |         /* SSLfatal() already called */  | 
582  | 0  |         goto err;  | 
583  | 0  |     }  | 
584  |  |  | 
585  | 3.11k  |     OSSL_TRACE_BEGIN(TLS) { | 
586  | 0  |         BIO_printf(trc_out, "key block\n");  | 
587  | 0  |         BIO_dump_indent(trc_out, p, num, 4);  | 
588  | 3.11k  |     } OSSL_TRACE_END(TLS);  | 
589  |  |  | 
590  | 3.11k  |     if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)  | 
591  | 3.11k  |         && s->method->version <= TLS1_VERSION) { | 
592  |  |         /*  | 
593  |  |          * enable vulnerability countermeasure for CBC ciphers with known-IV  | 
594  |  |          * problem (http://www.openssl.org/~bodo/tls-cbc.txt)  | 
595  |  |          */  | 
596  | 475  |         s->s3.need_empty_fragments = 1;  | 
597  |  |  | 
598  | 475  |         if (s->session->cipher != NULL) { | 
599  | 475  |             if (s->session->cipher->algorithm_enc == SSL_eNULL)  | 
600  | 65  |                 s->s3.need_empty_fragments = 0;  | 
601  |  |  | 
602  | 475  |             if (s->session->cipher->algorithm_enc == SSL_RC4)  | 
603  | 0  |                 s->s3.need_empty_fragments = 0;  | 
604  | 475  |         }  | 
605  | 475  |     }  | 
606  |  |  | 
607  | 3.11k  |     ret = 1;  | 
608  | 3.11k  |  err:  | 
609  | 3.11k  |     return ret;  | 
610  | 3.11k  | }  | 
611  |  |  | 
612  |  | size_t tls1_final_finish_mac(SSL *s, const char *str, size_t slen,  | 
613  |  |                              unsigned char *out)  | 
614  | 7.20k  | { | 
615  | 7.20k  |     size_t hashlen;  | 
616  | 7.20k  |     unsigned char hash[EVP_MAX_MD_SIZE];  | 
617  | 7.20k  |     size_t finished_size = TLS1_FINISH_MAC_LENGTH;  | 
618  |  |  | 
619  | 7.20k  |     if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kGOST18)  | 
620  | 0  |         finished_size = 32;  | 
621  |  |  | 
622  | 7.20k  |     if (!ssl3_digest_cached_records(s, 0)) { | 
623  |  |         /* SSLfatal() already called */  | 
624  | 0  |         return 0;  | 
625  | 0  |     }  | 
626  |  |  | 
627  | 7.20k  |     if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) { | 
628  |  |         /* SSLfatal() already called */  | 
629  | 0  |         return 0;  | 
630  | 0  |     }  | 
631  |  |  | 
632  | 7.20k  |     if (!tls1_PRF(s, str, slen, hash, hashlen, NULL, 0, NULL, 0, NULL, 0,  | 
633  | 7.20k  |                   s->session->master_key, s->session->master_key_length,  | 
634  | 7.20k  |                   out, finished_size, 1)) { | 
635  |  |         /* SSLfatal() already called */  | 
636  | 0  |         return 0;  | 
637  | 0  |     }  | 
638  | 7.20k  |     OPENSSL_cleanse(hash, hashlen);  | 
639  | 7.20k  |     return finished_size;  | 
640  | 7.20k  | }  | 
641  |  |  | 
642  |  | int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,  | 
643  |  |                                 size_t len, size_t *secret_size)  | 
644  | 11.0k  | { | 
645  | 11.0k  |     if (s->session->flags & SSL_SESS_FLAG_EXTMS) { | 
646  | 1.48k  |         unsigned char hash[EVP_MAX_MD_SIZE * 2];  | 
647  | 1.48k  |         size_t hashlen;  | 
648  |  |         /*  | 
649  |  |          * Digest cached records keeping record buffer (if present): this won't  | 
650  |  |          * affect client auth because we're freezing the buffer at the same  | 
651  |  |          * point (after client key exchange and before certificate verify)  | 
652  |  |          */  | 
653  | 1.48k  |         if (!ssl3_digest_cached_records(s, 1)  | 
654  | 1.48k  |                 || !ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) { | 
655  |  |             /* SSLfatal() already called */  | 
656  | 0  |             return 0;  | 
657  | 0  |         }  | 
658  | 1.48k  |         OSSL_TRACE_BEGIN(TLS) { | 
659  | 0  |             BIO_printf(trc_out, "Handshake hashes:\n");  | 
660  | 0  |             BIO_dump(trc_out, (char *)hash, hashlen);  | 
661  | 1.48k  |         } OSSL_TRACE_END(TLS);  | 
662  | 1.48k  |         if (!tls1_PRF(s,  | 
663  | 1.48k  |                       TLS_MD_EXTENDED_MASTER_SECRET_CONST,  | 
664  | 1.48k  |                       TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE,  | 
665  | 1.48k  |                       hash, hashlen,  | 
666  | 1.48k  |                       NULL, 0,  | 
667  | 1.48k  |                       NULL, 0,  | 
668  | 1.48k  |                       NULL, 0, p, len, out,  | 
669  | 1.48k  |                       SSL3_MASTER_SECRET_SIZE, 1)) { | 
670  |  |             /* SSLfatal() already called */  | 
671  | 0  |             return 0;  | 
672  | 0  |         }  | 
673  | 1.48k  |         OPENSSL_cleanse(hash, hashlen);  | 
674  | 9.54k  |     } else { | 
675  | 9.54k  |         if (!tls1_PRF(s,  | 
676  | 9.54k  |                       TLS_MD_MASTER_SECRET_CONST,  | 
677  | 9.54k  |                       TLS_MD_MASTER_SECRET_CONST_SIZE,  | 
678  | 9.54k  |                       s->s3.client_random, SSL3_RANDOM_SIZE,  | 
679  | 9.54k  |                       NULL, 0,  | 
680  | 9.54k  |                       s->s3.server_random, SSL3_RANDOM_SIZE,  | 
681  | 9.54k  |                       NULL, 0, p, len, out,  | 
682  | 9.54k  |                       SSL3_MASTER_SECRET_SIZE, 1)) { | 
683  |  |            /* SSLfatal() already called */  | 
684  | 0  |             return 0;  | 
685  | 0  |         }  | 
686  | 9.54k  |     }  | 
687  |  |  | 
688  | 11.0k  |     OSSL_TRACE_BEGIN(TLS) { | 
689  | 0  |         BIO_printf(trc_out, "Premaster Secret:\n");  | 
690  | 0  |         BIO_dump_indent(trc_out, p, len, 4);  | 
691  | 0  |         BIO_printf(trc_out, "Client Random:\n");  | 
692  | 0  |         BIO_dump_indent(trc_out, s->s3.client_random, SSL3_RANDOM_SIZE, 4);  | 
693  | 0  |         BIO_printf(trc_out, "Server Random:\n");  | 
694  | 0  |         BIO_dump_indent(trc_out, s->s3.server_random, SSL3_RANDOM_SIZE, 4);  | 
695  | 0  |         BIO_printf(trc_out, "Master Secret:\n");  | 
696  | 0  |         BIO_dump_indent(trc_out,  | 
697  | 0  |                         s->session->master_key,  | 
698  | 0  |                         SSL3_MASTER_SECRET_SIZE, 4);  | 
699  | 11.0k  |     } OSSL_TRACE_END(TLS);  | 
700  |  |  | 
701  | 11.0k  |     *secret_size = SSL3_MASTER_SECRET_SIZE;  | 
702  | 11.0k  |     return 1;  | 
703  | 11.0k  | }  | 
704  |  |  | 
705  |  | int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,  | 
706  |  |                                 const char *label, size_t llen,  | 
707  |  |                                 const unsigned char *context,  | 
708  |  |                                 size_t contextlen, int use_context)  | 
709  | 0  | { | 
710  | 0  |     unsigned char *val = NULL;  | 
711  | 0  |     size_t vallen = 0, currentvalpos;  | 
712  | 0  |     int rv;  | 
713  |  |  | 
714  |  |     /*  | 
715  |  |      * construct PRF arguments we construct the PRF argument ourself rather  | 
716  |  |      * than passing separate values into the TLS PRF to ensure that the  | 
717  |  |      * concatenation of values does not create a prohibited label.  | 
718  |  |      */  | 
719  | 0  |     vallen = llen + SSL3_RANDOM_SIZE * 2;  | 
720  | 0  |     if (use_context) { | 
721  | 0  |         vallen += 2 + contextlen;  | 
722  | 0  |     }  | 
723  |  | 
  | 
724  | 0  |     val = OPENSSL_malloc(vallen);  | 
725  | 0  |     if (val == NULL)  | 
726  | 0  |         goto err2;  | 
727  | 0  |     currentvalpos = 0;  | 
728  | 0  |     memcpy(val + currentvalpos, (unsigned char *)label, llen);  | 
729  | 0  |     currentvalpos += llen;  | 
730  | 0  |     memcpy(val + currentvalpos, s->s3.client_random, SSL3_RANDOM_SIZE);  | 
731  | 0  |     currentvalpos += SSL3_RANDOM_SIZE;  | 
732  | 0  |     memcpy(val + currentvalpos, s->s3.server_random, SSL3_RANDOM_SIZE);  | 
733  | 0  |     currentvalpos += SSL3_RANDOM_SIZE;  | 
734  |  | 
  | 
735  | 0  |     if (use_context) { | 
736  | 0  |         val[currentvalpos] = (contextlen >> 8) & 0xff;  | 
737  | 0  |         currentvalpos++;  | 
738  | 0  |         val[currentvalpos] = contextlen & 0xff;  | 
739  | 0  |         currentvalpos++;  | 
740  | 0  |         if ((contextlen > 0) || (context != NULL)) { | 
741  | 0  |             memcpy(val + currentvalpos, context, contextlen);  | 
742  | 0  |         }  | 
743  | 0  |     }  | 
744  |  |  | 
745  |  |     /*  | 
746  |  |      * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited  | 
747  |  |      * label len) = 15, so size of val > max(prohibited label len) = 15 and  | 
748  |  |      * the comparisons won't have buffer overflow  | 
749  |  |      */  | 
750  | 0  |     if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,  | 
751  | 0  |                TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)  | 
752  | 0  |         goto err1;  | 
753  | 0  |     if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,  | 
754  | 0  |                TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)  | 
755  | 0  |         goto err1;  | 
756  | 0  |     if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,  | 
757  | 0  |                TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)  | 
758  | 0  |         goto err1;  | 
759  | 0  |     if (memcmp(val, TLS_MD_EXTENDED_MASTER_SECRET_CONST,  | 
760  | 0  |                TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE) == 0)  | 
761  | 0  |         goto err1;  | 
762  | 0  |     if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,  | 
763  | 0  |                TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)  | 
764  | 0  |         goto err1;  | 
765  |  |  | 
766  | 0  |     rv = tls1_PRF(s,  | 
767  | 0  |                   val, vallen,  | 
768  | 0  |                   NULL, 0,  | 
769  | 0  |                   NULL, 0,  | 
770  | 0  |                   NULL, 0,  | 
771  | 0  |                   NULL, 0,  | 
772  | 0  |                   s->session->master_key, s->session->master_key_length,  | 
773  | 0  |                   out, olen, 0);  | 
774  |  | 
  | 
775  | 0  |     goto ret;  | 
776  | 0  |  err1:  | 
777  | 0  |     ERR_raise(ERR_LIB_SSL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);  | 
778  | 0  |     rv = 0;  | 
779  | 0  |     goto ret;  | 
780  | 0  |  err2:  | 
781  | 0  |     ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);  | 
782  | 0  |     rv = 0;  | 
783  | 0  |  ret:  | 
784  | 0  |     OPENSSL_clear_free(val, vallen);  | 
785  | 0  |     return rv;  | 
786  | 0  | }  | 
787  |  |  | 
788  |  | int tls1_alert_code(int code)  | 
789  | 102k  | { | 
790  | 102k  |     switch (code) { | 
791  | 666  |     case SSL_AD_CLOSE_NOTIFY:  | 
792  | 666  |         return SSL3_AD_CLOSE_NOTIFY;  | 
793  | 4.74k  |     case SSL_AD_UNEXPECTED_MESSAGE:  | 
794  | 4.74k  |         return SSL3_AD_UNEXPECTED_MESSAGE;  | 
795  | 2.28k  |     case SSL_AD_BAD_RECORD_MAC:  | 
796  | 2.28k  |         return SSL3_AD_BAD_RECORD_MAC;  | 
797  | 0  |     case SSL_AD_DECRYPTION_FAILED:  | 
798  | 0  |         return TLS1_AD_DECRYPTION_FAILED;  | 
799  | 594  |     case SSL_AD_RECORD_OVERFLOW:  | 
800  | 594  |         return TLS1_AD_RECORD_OVERFLOW;  | 
801  | 0  |     case SSL_AD_DECOMPRESSION_FAILURE:  | 
802  | 0  |         return SSL3_AD_DECOMPRESSION_FAILURE;  | 
803  | 1.98k  |     case SSL_AD_HANDSHAKE_FAILURE:  | 
804  | 1.98k  |         return SSL3_AD_HANDSHAKE_FAILURE;  | 
805  | 0  |     case SSL_AD_NO_CERTIFICATE:  | 
806  | 0  |         return -1;  | 
807  | 7.98k  |     case SSL_AD_BAD_CERTIFICATE:  | 
808  | 7.98k  |         return SSL3_AD_BAD_CERTIFICATE;  | 
809  | 0  |     case SSL_AD_UNSUPPORTED_CERTIFICATE:  | 
810  | 0  |         return SSL3_AD_UNSUPPORTED_CERTIFICATE;  | 
811  | 0  |     case SSL_AD_CERTIFICATE_REVOKED:  | 
812  | 0  |         return SSL3_AD_CERTIFICATE_REVOKED;  | 
813  | 0  |     case SSL_AD_CERTIFICATE_EXPIRED:  | 
814  | 0  |         return SSL3_AD_CERTIFICATE_EXPIRED;  | 
815  | 0  |     case SSL_AD_CERTIFICATE_UNKNOWN:  | 
816  | 0  |         return SSL3_AD_CERTIFICATE_UNKNOWN;  | 
817  | 4.39k  |     case SSL_AD_ILLEGAL_PARAMETER:  | 
818  | 4.39k  |         return SSL3_AD_ILLEGAL_PARAMETER;  | 
819  | 0  |     case SSL_AD_UNKNOWN_CA:  | 
820  | 0  |         return TLS1_AD_UNKNOWN_CA;  | 
821  | 0  |     case SSL_AD_ACCESS_DENIED:  | 
822  | 0  |         return TLS1_AD_ACCESS_DENIED;  | 
823  | 8.36k  |     case SSL_AD_DECODE_ERROR:  | 
824  | 8.36k  |         return TLS1_AD_DECODE_ERROR;  | 
825  | 2.30k  |     case SSL_AD_DECRYPT_ERROR:  | 
826  | 2.30k  |         return TLS1_AD_DECRYPT_ERROR;  | 
827  | 0  |     case SSL_AD_EXPORT_RESTRICTION:  | 
828  | 0  |         return TLS1_AD_EXPORT_RESTRICTION;  | 
829  | 1.71k  |     case SSL_AD_PROTOCOL_VERSION:  | 
830  | 1.71k  |         return TLS1_AD_PROTOCOL_VERSION;  | 
831  | 0  |     case SSL_AD_INSUFFICIENT_SECURITY:  | 
832  | 0  |         return TLS1_AD_INSUFFICIENT_SECURITY;  | 
833  | 2.49k  |     case SSL_AD_INTERNAL_ERROR:  | 
834  | 2.49k  |         return TLS1_AD_INTERNAL_ERROR;  | 
835  | 0  |     case SSL_AD_USER_CANCELLED:  | 
836  | 0  |         return TLS1_AD_USER_CANCELLED;  | 
837  | 64.5k  |     case SSL_AD_NO_RENEGOTIATION:  | 
838  | 64.5k  |         return TLS1_AD_NO_RENEGOTIATION;  | 
839  | 60  |     case SSL_AD_UNSUPPORTED_EXTENSION:  | 
840  | 60  |         return TLS1_AD_UNSUPPORTED_EXTENSION;  | 
841  | 0  |     case SSL_AD_CERTIFICATE_UNOBTAINABLE:  | 
842  | 0  |         return TLS1_AD_CERTIFICATE_UNOBTAINABLE;  | 
843  | 19  |     case SSL_AD_UNRECOGNIZED_NAME:  | 
844  | 19  |         return TLS1_AD_UNRECOGNIZED_NAME;  | 
845  | 0  |     case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:  | 
846  | 0  |         return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;  | 
847  | 0  |     case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:  | 
848  | 0  |         return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;  | 
849  | 0  |     case SSL_AD_UNKNOWN_PSK_IDENTITY:  | 
850  | 0  |         return TLS1_AD_UNKNOWN_PSK_IDENTITY;  | 
851  | 12  |     case SSL_AD_INAPPROPRIATE_FALLBACK:  | 
852  | 12  |         return TLS1_AD_INAPPROPRIATE_FALLBACK;  | 
853  | 0  |     case SSL_AD_NO_APPLICATION_PROTOCOL:  | 
854  | 0  |         return TLS1_AD_NO_APPLICATION_PROTOCOL;  | 
855  | 0  |     case SSL_AD_CERTIFICATE_REQUIRED:  | 
856  | 0  |         return SSL_AD_HANDSHAKE_FAILURE;  | 
857  | 10  |     case TLS13_AD_MISSING_EXTENSION:  | 
858  | 10  |         return SSL_AD_HANDSHAKE_FAILURE;  | 
859  | 0  |     default:  | 
860  | 0  |         return -1;  | 
861  | 102k  |     }  | 
862  | 102k  | }  |