1 /*
2  * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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  * Refer to "The TLS Protocol Version 1.0" Section 5
12  * (https://tools.ietf.org/html/rfc2246#section-5) and
13  * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
14  * (https://tools.ietf.org/html/rfc5246#section-5).
15  *
16  * For TLS v1.0 and TLS v1.1 the TLS PRF algorithm is given by:
17  *
18  *   PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
19  *                              P_SHA-1(S2, label + seed)
20  *
21  * where P_MD5 and P_SHA-1 are defined by P_<hash>, below, and S1 and S2 are
22  * two halves of the secret (with the possibility of one shared byte, in the
23  * case where the length of the original secret is odd).  S1 is taken from the
24  * first half of the secret, S2 from the second half.
25  *
26  * For TLS v1.2 the TLS PRF algorithm is given by:
27  *
28  *   PRF(secret, label, seed) = P_<hash>(secret, label + seed)
29  *
30  * where hash is SHA-256 for all cipher suites defined in RFC 5246 as well as
31  * those published prior to TLS v1.2 while the TLS v1.2 protocol is in effect,
32  * unless defined otherwise by the cipher suite.
33  *
34  * P_<hash> is an expansion function that uses a single hash function to expand
35  * a secret and seed into an arbitrary quantity of output:
36  *
37  *   P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
38  *                            HMAC_<hash>(secret, A(2) + seed) +
39  *                            HMAC_<hash>(secret, A(3) + seed) + ...
40  *
41  * where + indicates concatenation.  P_<hash> can be iterated as many times as
42  * is necessary to produce the required quantity of data.
43  *
44  * A(i) is defined as:
45  *     A(0) = seed
46  *     A(i) = HMAC_<hash>(secret, A(i-1))
47  */
48 #include <stdio.h>
49 #include <stdarg.h>
50 #include <string.h>
51 #include <openssl/evp.h>
52 #include <openssl/kdf.h>
53 #include <openssl/core_names.h>
54 #include <openssl/params.h>
55 #include <openssl/proverr.h>
56 #include "internal/cryptlib.h"
57 #include "internal/numbers.h"
58 #include "crypto/evp.h"
59 #include "prov/provider_ctx.h"
60 #include "prov/providercommon.h"
61 #include "prov/implementations.h"
62 #include "prov/provider_util.h"
63 #include "e_os.h"
64 
65 static OSSL_FUNC_kdf_newctx_fn kdf_tls1_prf_new;
66 static OSSL_FUNC_kdf_freectx_fn kdf_tls1_prf_free;
67 static OSSL_FUNC_kdf_reset_fn kdf_tls1_prf_reset;
68 static OSSL_FUNC_kdf_derive_fn kdf_tls1_prf_derive;
69 static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
70 static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_prf_set_ctx_params;
71 static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_tls1_prf_gettable_ctx_params;
72 static OSSL_FUNC_kdf_get_ctx_params_fn kdf_tls1_prf_get_ctx_params;
73 
74 static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
75                         const unsigned char *sec, size_t slen,
76                         const unsigned char *seed, size_t seed_len,
77                         unsigned char *out, size_t olen);
78 
79 #define TLS1_PRF_MAXBUF 1024
80 
81 /* TLS KDF kdf context structure */
82 typedef struct {
83     void *provctx;
84 
85     /* MAC context for the main digest */
86     EVP_MAC_CTX *P_hash;
87     /* MAC context for SHA1 for the MD5/SHA-1 combined PRF */
88     EVP_MAC_CTX *P_sha1;
89 
90     /* Secret value to use for PRF */
91     unsigned char *sec;
92     size_t seclen;
93     /* Buffer of concatenated seed data */
94     unsigned char seed[TLS1_PRF_MAXBUF];
95     size_t seedlen;
96 } TLS1_PRF;
97 
kdf_tls1_prf_new(void * provctx)98 static void *kdf_tls1_prf_new(void *provctx)
99 {
100     TLS1_PRF *ctx;
101 
102     if (!ossl_prov_is_running())
103         return NULL;
104 
105     if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
106         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
107     ctx->provctx = provctx;
108     return ctx;
109 }
110 
kdf_tls1_prf_free(void * vctx)111 static void kdf_tls1_prf_free(void *vctx)
112 {
113     TLS1_PRF *ctx = (TLS1_PRF *)vctx;
114 
115     if (ctx != NULL) {
116         kdf_tls1_prf_reset(ctx);
117         OPENSSL_free(ctx);
118     }
119 }
120 
kdf_tls1_prf_reset(void * vctx)121 static void kdf_tls1_prf_reset(void *vctx)
122 {
123     TLS1_PRF *ctx = (TLS1_PRF *)vctx;
124     void *provctx = ctx->provctx;
125 
126     EVP_MAC_CTX_free(ctx->P_hash);
127     EVP_MAC_CTX_free(ctx->P_sha1);
128     OPENSSL_clear_free(ctx->sec, ctx->seclen);
129     OPENSSL_cleanse(ctx->seed, ctx->seedlen);
130     memset(ctx, 0, sizeof(*ctx));
131     ctx->provctx = provctx;
132 }
133 
kdf_tls1_prf_derive(void * vctx,unsigned char * key,size_t keylen,const OSSL_PARAM params[])134 static int kdf_tls1_prf_derive(void *vctx, unsigned char *key, size_t keylen,
135                                const OSSL_PARAM params[])
136 {
137     TLS1_PRF *ctx = (TLS1_PRF *)vctx;
138 
139     if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params))
140         return 0;
141 
142     if (ctx->P_hash == NULL) {
143         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
144         return 0;
145     }
146     if (ctx->sec == NULL) {
147         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
148         return 0;
149     }
150     if (ctx->seedlen == 0) {
151         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);
152         return 0;
153     }
154     if (keylen == 0) {
155         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
156         return 0;
157     }
158 
159     return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,
160                         ctx->sec, ctx->seclen,
161                         ctx->seed, ctx->seedlen,
162                         key, keylen);
163 }
164 
kdf_tls1_prf_set_ctx_params(void * vctx,const OSSL_PARAM params[])165 static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
166 {
167     const OSSL_PARAM *p;
168     TLS1_PRF *ctx = vctx;
169     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
170 
171     if (params == NULL)
172         return 1;
173 
174     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) {
175         if (strcasecmp(p->data, SN_md5_sha1) == 0) {
176             if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
177                                                    OSSL_MAC_NAME_HMAC,
178                                                    NULL, SN_md5, libctx)
179                 || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params,
180                                                       OSSL_MAC_NAME_HMAC,
181                                                       NULL, SN_sha1, libctx))
182                 return 0;
183         } else {
184             EVP_MAC_CTX_free(ctx->P_sha1);
185             if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
186                                                    OSSL_MAC_NAME_HMAC,
187                                                    NULL, NULL, libctx))
188                 return 0;
189         }
190     }
191 
192     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) {
193         OPENSSL_clear_free(ctx->sec, ctx->seclen);
194         ctx->sec = NULL;
195         if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen))
196             return 0;
197     }
198     /* The seed fields concatenate, so process them all */
199     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) {
200         for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,
201                                                       OSSL_KDF_PARAM_SEED)) {
202             const void *q = ctx->seed + ctx->seedlen;
203             size_t sz = 0;
204 
205             if (p->data_size != 0
206                 && p->data != NULL
207                 && !OSSL_PARAM_get_octet_string(p, (void **)&q,
208                                                 TLS1_PRF_MAXBUF - ctx->seedlen,
209                                                 &sz))
210                 return 0;
211             ctx->seedlen += sz;
212         }
213     }
214     return 1;
215 }
216 
kdf_tls1_prf_settable_ctx_params(ossl_unused void * ctx,ossl_unused void * provctx)217 static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(
218         ossl_unused void *ctx, ossl_unused void *provctx)
219 {
220     static const OSSL_PARAM known_settable_ctx_params[] = {
221         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
222         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
223         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
224         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
225         OSSL_PARAM_END
226     };
227     return known_settable_ctx_params;
228 }
229 
kdf_tls1_prf_get_ctx_params(void * vctx,OSSL_PARAM params[])230 static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])
231 {
232     OSSL_PARAM *p;
233 
234     if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
235         return OSSL_PARAM_set_size_t(p, SIZE_MAX);
236     return -2;
237 }
238 
kdf_tls1_prf_gettable_ctx_params(ossl_unused void * ctx,ossl_unused void * provctx)239 static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(
240         ossl_unused void *ctx, ossl_unused void *provctx)
241 {
242     static const OSSL_PARAM known_gettable_ctx_params[] = {
243         OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
244         OSSL_PARAM_END
245     };
246     return known_gettable_ctx_params;
247 }
248 
249 const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = {
250     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_tls1_prf_new },
251     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_tls1_prf_free },
252     { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_tls1_prf_reset },
253     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_prf_derive },
254     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
255       (void(*)(void))kdf_tls1_prf_settable_ctx_params },
256     { OSSL_FUNC_KDF_SET_CTX_PARAMS,
257       (void(*)(void))kdf_tls1_prf_set_ctx_params },
258     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
259       (void(*)(void))kdf_tls1_prf_gettable_ctx_params },
260     { OSSL_FUNC_KDF_GET_CTX_PARAMS,
261       (void(*)(void))kdf_tls1_prf_get_ctx_params },
262     { 0, NULL }
263 };
264 
265 /*
266  * Refer to "The TLS Protocol Version 1.0" Section 5
267  * (https://tools.ietf.org/html/rfc2246#section-5) and
268  * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
269  * (https://tools.ietf.org/html/rfc5246#section-5).
270  *
271  * P_<hash> is an expansion function that uses a single hash function to expand
272  * a secret and seed into an arbitrary quantity of output:
273  *
274  *   P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
275  *                            HMAC_<hash>(secret, A(2) + seed) +
276  *                            HMAC_<hash>(secret, A(3) + seed) + ...
277  *
278  * where + indicates concatenation.  P_<hash> can be iterated as many times as
279  * is necessary to produce the required quantity of data.
280  *
281  * A(i) is defined as:
282  *     A(0) = seed
283  *     A(i) = HMAC_<hash>(secret, A(i-1))
284  */
tls1_prf_P_hash(EVP_MAC_CTX * ctx_init,const unsigned char * sec,size_t sec_len,const unsigned char * seed,size_t seed_len,unsigned char * out,size_t olen)285 static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init,
286                            const unsigned char *sec, size_t sec_len,
287                            const unsigned char *seed, size_t seed_len,
288                            unsigned char *out, size_t olen)
289 {
290     size_t chunk;
291     EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;
292     unsigned char Ai[EVP_MAX_MD_SIZE];
293     size_t Ai_len;
294     int ret = 0;
295 
296     if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL))
297         goto err;
298     chunk = EVP_MAC_CTX_get_mac_size(ctx_init);
299     if (chunk == 0)
300         goto err;
301     /* A(0) = seed */
302     ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
303     if (ctx_Ai == NULL)
304         goto err;
305     if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
306         goto err;
307 
308     for (;;) {
309         /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
310         if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
311             goto err;
312         EVP_MAC_CTX_free(ctx_Ai);
313         ctx_Ai = NULL;
314 
315         /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
316         ctx = EVP_MAC_CTX_dup(ctx_init);
317         if (ctx == NULL)
318             goto err;
319         if (!EVP_MAC_update(ctx, Ai, Ai_len))
320             goto err;
321         /* save state for calculating next A(i) value */
322         if (olen > chunk) {
323             ctx_Ai = EVP_MAC_CTX_dup(ctx);
324             if (ctx_Ai == NULL)
325                 goto err;
326         }
327         if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
328             goto err;
329         if (olen <= chunk) {
330             /* last chunk - use Ai as temp bounce buffer */
331             if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
332                 goto err;
333             memcpy(out, Ai, olen);
334             break;
335         }
336         if (!EVP_MAC_final(ctx, out, NULL, olen))
337             goto err;
338         EVP_MAC_CTX_free(ctx);
339         ctx = NULL;
340         out += chunk;
341         olen -= chunk;
342     }
343     ret = 1;
344  err:
345     EVP_MAC_CTX_free(ctx);
346     EVP_MAC_CTX_free(ctx_Ai);
347     OPENSSL_cleanse(Ai, sizeof(Ai));
348     return ret;
349 }
350 
351 /*
352  * Refer to "The TLS Protocol Version 1.0" Section 5
353  * (https://tools.ietf.org/html/rfc2246#section-5) and
354  * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
355  * (https://tools.ietf.org/html/rfc5246#section-5).
356  *
357  * For TLS v1.0 and TLS v1.1:
358  *
359  *   PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
360  *                              P_SHA-1(S2, label + seed)
361  *
362  * S1 is taken from the first half of the secret, S2 from the second half.
363  *
364  *   L_S = length in bytes of secret;
365  *   L_S1 = L_S2 = ceil(L_S / 2);
366  *
367  * For TLS v1.2:
368  *
369  *   PRF(secret, label, seed) = P_<hash>(secret, label + seed)
370  */
tls1_prf_alg(EVP_MAC_CTX * mdctx,EVP_MAC_CTX * sha1ctx,const unsigned char * sec,size_t slen,const unsigned char * seed,size_t seed_len,unsigned char * out,size_t olen)371 static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
372                         const unsigned char *sec, size_t slen,
373                         const unsigned char *seed, size_t seed_len,
374                         unsigned char *out, size_t olen)
375 {
376     if (sha1ctx != NULL) {
377         /* TLS v1.0 and TLS v1.1 */
378         size_t i;
379         unsigned char *tmp;
380         /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
381         size_t L_S1 = (slen + 1) / 2;
382         size_t L_S2 = L_S1;
383 
384         if (!tls1_prf_P_hash(mdctx, sec, L_S1,
385                              seed, seed_len, out, olen))
386             return 0;
387 
388         if ((tmp = OPENSSL_malloc(olen)) == NULL) {
389             ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
390             return 0;
391         }
392 
393         if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2,
394                              seed, seed_len, tmp, olen)) {
395             OPENSSL_clear_free(tmp, olen);
396             return 0;
397         }
398         for (i = 0; i < olen; i++)
399             out[i] ^= tmp[i];
400         OPENSSL_clear_free(tmp, olen);
401         return 1;
402     }
403 
404     /* TLS v1.2 */
405     if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))
406         return 0;
407 
408     return 1;
409 }
410