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