]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/kdfs/x942kdf.c
Change the provider implementation of X942kdf to use wpacket to do der encoding of...
[thirdparty/openssl.git] / providers / implementations / kdfs / x942kdf.c
1 /*
2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. 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 "e_os.h"
12 #include <openssl/core_names.h>
13 #include <openssl/core_dispatch.h>
14 #include <openssl/err.h>
15 #include <openssl/evp.h>
16 #include <openssl/params.h>
17 #include "internal/packet.h"
18 #include "internal/der.h"
19 #include "prov/provider_ctx.h"
20 #include "prov/providercommonerr.h"
21 #include "prov/implementations.h"
22 #include "prov/provider_util.h"
23 #include "prov/der_wrap.h"
24
25 #define X942KDF_MAX_INLEN (1 << 30)
26
27 static OSSL_FUNC_kdf_newctx_fn x942kdf_new;
28 static OSSL_FUNC_kdf_freectx_fn x942kdf_free;
29 static OSSL_FUNC_kdf_reset_fn x942kdf_reset;
30 static OSSL_FUNC_kdf_derive_fn x942kdf_derive;
31 static OSSL_FUNC_kdf_settable_ctx_params_fn x942kdf_settable_ctx_params;
32 static OSSL_FUNC_kdf_set_ctx_params_fn x942kdf_set_ctx_params;
33 static OSSL_FUNC_kdf_gettable_ctx_params_fn x942kdf_gettable_ctx_params;
34 static OSSL_FUNC_kdf_get_ctx_params_fn x942kdf_get_ctx_params;
35
36 typedef struct {
37 void *provctx;
38 PROV_DIGEST digest;
39 unsigned char *secret;
40 size_t secret_len;
41 unsigned char *ukm;
42 size_t ukm_len;
43 size_t dkm_len;
44 const unsigned char *cek_oid;
45 size_t cek_oid_len;
46 } KDF_X942;
47
48 /*
49 * A table of allowed wrapping algorithms, oids and the associated output
50 * lengths.
51 * NOTE: RC2wrap and camellia128_wrap have been removed as there are no
52 * corresponding ciphers for these operations.
53 */
54 static const struct {
55 const char *name;
56 const unsigned char *oid;
57 size_t oid_len;
58 size_t keklen; /* size in bytes */
59 } kek_algs[] = {
60 { "AES-128-WRAP", der_oid_id_aes128_wrap, DER_OID_SZ_id_aes128_wrap, 16 },
61 { "AES-192-WRAP", der_oid_id_aes192_wrap, DER_OID_SZ_id_aes192_wrap, 24 },
62 { "AES-256-WRAP", der_oid_id_aes256_wrap, DER_OID_SZ_id_aes256_wrap, 32 },
63 #ifndef FIPS_MODULE
64 { "DES3-WRAP", der_oid_id_alg_CMS3DESwrap, DER_OID_SZ_id_alg_CMS3DESwrap,
65 24 },
66 #endif
67 };
68
69 static int find_alg_id(OPENSSL_CTX *libctx, const char *algname, size_t *id)
70 {
71 int ret = 1;
72 size_t i;
73 EVP_CIPHER *cipher;
74
75 cipher = EVP_CIPHER_fetch(libctx, algname, NULL);
76 if (cipher != NULL) {
77 for (i = 0; i < OSSL_NELEM(kek_algs); i++) {
78 if (EVP_CIPHER_is_a(cipher, kek_algs[i].name)) {
79 *id = i;
80 goto end;
81 }
82 }
83 }
84 ret = 0;
85 ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_CEK_ALG);
86 end:
87 EVP_CIPHER_free(cipher);
88 return ret;
89 }
90
91 static int DER_w_keyinfo(WPACKET *pkt,
92 const unsigned char *der_oid, size_t der_oidlen,
93 unsigned char **pcounter)
94 {
95 return DER_w_begin_sequence(pkt, -1)
96 /* Store the initial value of 1 into the counter */
97 && DER_w_octet_string_uint32(pkt, -1, 1)
98 /* Remember where we stored the counter in the buffer */
99 && (pcounter == NULL
100 || (*pcounter = WPACKET_get_curr(pkt)) != NULL)
101 && DER_w_precompiled(pkt, -1, der_oid, der_oidlen)
102 && DER_w_end_sequence(pkt, -1);
103 }
104
105 static int der_encode_sharedinfo(WPACKET *pkt, unsigned char *buf, size_t buflen,
106 const unsigned char *der_oid, size_t der_oidlen,
107 const unsigned char *ukm, size_t ukmlen,
108 uint32_t keylen_bits, unsigned char **pcounter)
109 {
110 return (buf != NULL ? WPACKET_init_der(pkt, buf, buflen) :
111 WPACKET_init_null_der(pkt))
112 && DER_w_begin_sequence(pkt, -1)
113 && DER_w_octet_string_uint32(pkt, 2, keylen_bits)
114 && (ukm == NULL || DER_w_octet_string(pkt, 0, ukm, ukmlen))
115 && DER_w_keyinfo(pkt, der_oid, der_oidlen, pcounter)
116 && DER_w_end_sequence(pkt, -1)
117 && WPACKET_finish(pkt);
118 }
119
120 /*
121 * Encode the other info structure.
122 *
123 * RFC2631 Section 2.1.2 Contains the following definition for otherinfo
124 *
125 * OtherInfo ::= SEQUENCE {
126 * keyInfo KeySpecificInfo,
127 * partyAInfo [0] OCTET STRING OPTIONAL,
128 * suppPubInfo [2] OCTET STRING
129 * }
130 * Note suppPubInfo is the key length (in bits) (stored into 4 bytes)
131 *
132 *
133 * KeySpecificInfo ::= SEQUENCE {
134 * algorithm OBJECT IDENTIFIER,
135 * counter OCTET STRING SIZE (4..4)
136 * }
137 *
138 * |keylen| is the length (in bytes) of the generated KEK. It is stored into
139 * suppPubInfo (in bits).
140 * |cek_oid| The oid of the key wrapping algorithm.
141 * |cek_oidlen| The length (in bytes) of the key wrapping algorithm oid,
142 * |ukm| is the optional user keying material that is stored into partyAInfo. It
143 * can be NULL.
144 * |ukmlen| is the user keying material length (in bytes).
145 * |der| is the returned encoded data. It must be freed by the caller.
146 * |der_len| is the returned size of the encoded data.
147 * |out_ctr| returns a pointer to the counter data which is embedded inside the
148 * encoded data. This allows the counter bytes to be updated without re-encoding.
149 *
150 * Returns: 1 if successfully encoded, or 0 otherwise.
151 * Assumptions: |der|, |der_len| & |out_ctr| are not NULL.
152 */
153 static int x942_encode_otherinfo(size_t keylen,
154 const unsigned char *cek_oid, size_t cek_oidlen,
155 const unsigned char *ukm, size_t ukmlen,
156 unsigned char **der, size_t *der_len,
157 unsigned char **out_ctr)
158 {
159 int ret = 0;
160 unsigned char *pcounter = NULL, *der_buf = NULL;
161 size_t der_buflen = 0;
162 WPACKET pkt;
163 uint32_t keylen_bits;
164
165 /* keylenbits must fit into 4 bytes */
166 if (keylen > 0xFFFFFF)
167 goto err;
168 keylen_bits = 8 * keylen;
169
170 /* Calculate the size of the buffer */
171 if (!der_encode_sharedinfo(&pkt, NULL, 0, cek_oid, cek_oidlen, ukm, ukmlen,
172 keylen_bits, NULL)
173 || !WPACKET_get_total_written(&pkt, &der_buflen))
174 goto err;
175 WPACKET_cleanup(&pkt);
176 /* Alloc the buffer */
177 der_buf = OPENSSL_zalloc(der_buflen);
178 if (der_buf == NULL)
179 goto err;
180 /* Encode into the buffer */
181 if (!der_encode_sharedinfo(&pkt, der_buf, der_buflen, cek_oid, cek_oidlen,
182 ukm, ukmlen, keylen_bits, &pcounter))
183 goto err;
184 /*
185 * Since we allocated the exact size required, the buffer should point to the
186 * start of the alllocated buffer at this point.
187 */
188 if (WPACKET_get_curr(&pkt) != der_buf)
189 goto err;
190
191 /*
192 * The data for the DER encoded octet string of a 32 bit counter = 1
193 * should be 04 04 00 00 00 01
194 * So just check the header is correct and skip over it.
195 * This counter will be incremented in the kdf update loop.
196 */
197 if (pcounter == NULL
198 || pcounter[0] != 0x04
199 || pcounter[1] != 0x04)
200 goto err;
201 *out_ctr = (pcounter + 2);
202 *der = der_buf;
203 *der_len = der_buflen;
204 ret = 1;
205 err:
206 WPACKET_cleanup(&pkt);
207 return ret;
208 }
209
210 static int x942kdf_hash_kdm(const EVP_MD *kdf_md,
211 const unsigned char *z, size_t z_len,
212 const unsigned char *other, size_t other_len,
213 unsigned char *ctr,
214 unsigned char *derived_key, size_t derived_key_len)
215 {
216 int ret = 0, hlen;
217 size_t counter, out_len, len = derived_key_len;
218 unsigned char mac[EVP_MAX_MD_SIZE];
219 unsigned char *out = derived_key;
220 EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
221
222 if (z_len > X942KDF_MAX_INLEN || other_len > X942KDF_MAX_INLEN
223 || derived_key_len > X942KDF_MAX_INLEN
224 || derived_key_len == 0) {
225 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
226 return 0;
227 }
228
229 hlen = EVP_MD_size(kdf_md);
230 if (hlen <= 0)
231 return 0;
232 out_len = (size_t)hlen;
233
234 ctx = EVP_MD_CTX_create();
235 ctx_init = EVP_MD_CTX_create();
236 if (ctx == NULL || ctx_init == NULL)
237 goto end;
238
239 if (!EVP_DigestInit(ctx_init, kdf_md))
240 goto end;
241
242 for (counter = 1;; counter++) {
243 /* updating the ctr modifies 4 bytes in the 'other' buffer */
244 ctr[0] = (unsigned char)((counter >> 24) & 0xff);
245 ctr[1] = (unsigned char)((counter >> 16) & 0xff);
246 ctr[2] = (unsigned char)((counter >> 8) & 0xff);
247 ctr[3] = (unsigned char)(counter & 0xff);
248
249 if (!EVP_MD_CTX_copy_ex(ctx, ctx_init)
250 || !EVP_DigestUpdate(ctx, z, z_len)
251 || !EVP_DigestUpdate(ctx, other, other_len))
252 goto end;
253 if (len >= out_len) {
254 if (!EVP_DigestFinal_ex(ctx, out, NULL))
255 goto end;
256 out += out_len;
257 len -= out_len;
258 if (len == 0)
259 break;
260 } else {
261 if (!EVP_DigestFinal_ex(ctx, mac, NULL))
262 goto end;
263 memcpy(out, mac, len);
264 break;
265 }
266 }
267 ret = 1;
268 end:
269 EVP_MD_CTX_free(ctx);
270 EVP_MD_CTX_free(ctx_init);
271 OPENSSL_cleanse(mac, sizeof(mac));
272 return ret;
273 }
274
275 static void *x942kdf_new(void *provctx)
276 {
277 KDF_X942 *ctx;
278
279 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
280 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
281 ctx->provctx = provctx;
282 return ctx;
283 }
284
285 static void x942kdf_reset(void *vctx)
286 {
287 KDF_X942 *ctx = (KDF_X942 *)vctx;
288 void *provctx = ctx->provctx;
289
290 ossl_prov_digest_reset(&ctx->digest);
291 OPENSSL_clear_free(ctx->secret, ctx->secret_len);
292 OPENSSL_clear_free(ctx->ukm, ctx->ukm_len);
293 memset(ctx, 0, sizeof(*ctx));
294 ctx->provctx = provctx;
295 }
296
297 static void x942kdf_free(void *vctx)
298 {
299 KDF_X942 *ctx = (KDF_X942 *)vctx;
300
301 if (ctx != NULL) {
302 x942kdf_reset(ctx);
303 OPENSSL_free(ctx);
304 }
305 }
306
307 static int x942kdf_set_buffer(unsigned char **out, size_t *out_len,
308 const OSSL_PARAM *p)
309 {
310 if (p->data_size == 0 || p->data == NULL)
311 return 1;
312
313 OPENSSL_free(*out);
314 *out = NULL;
315 return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
316 }
317
318 static size_t x942kdf_size(KDF_X942 *ctx)
319 {
320 int len;
321 const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
322
323 if (md == NULL) {
324 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
325 return 0;
326 }
327 len = EVP_MD_size(md);
328 return (len <= 0) ? 0 : (size_t)len;
329 }
330
331 static int x942kdf_derive(void *vctx, unsigned char *key, size_t keylen)
332 {
333 KDF_X942 *ctx = (KDF_X942 *)vctx;
334 const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
335 int ret = 0;
336 unsigned char *ctr;
337 unsigned char *der = NULL;
338 size_t der_len = 0;
339
340 if (ctx->secret == NULL) {
341 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
342 return 0;
343 }
344 if (md == NULL) {
345 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
346 return 0;
347 }
348 if (ctx->cek_oid == NULL || ctx->cek_oid_len == 0) {
349 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CEK_ALG);
350 return 0;
351 }
352 if (ctx->ukm != NULL && ctx->ukm_len >= X942KDF_MAX_INLEN) {
353 /*
354 * Note the ukm length MUST be 512 bits.
355 * For backwards compatibility the old check is being done.
356 */
357 ERR_raise(ERR_LIB_PROV, PROV_R_INAVLID_UKM_LENGTH);
358 return 0;
359 }
360 /* generate the otherinfo der */
361 if (!x942_encode_otherinfo(ctx->dkm_len,
362 ctx->cek_oid, ctx->cek_oid_len,
363 ctx->ukm, ctx->ukm_len,
364 &der, &der_len, &ctr)) {
365 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_ENCODING);
366 return 0;
367 }
368 ret = x942kdf_hash_kdm(md, ctx->secret, ctx->secret_len,
369 der, der_len, ctr, key, keylen);
370 OPENSSL_free(der);
371 return ret;
372 }
373
374 static int x942kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
375 {
376 const OSSL_PARAM *p;
377 KDF_X942 *ctx = vctx;
378 OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
379 size_t id;
380
381 if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
382 return 0;
383
384 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL
385 || (p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
386 if (!x942kdf_set_buffer(&ctx->secret, &ctx->secret_len, p))
387 return 0;
388
389 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_UKM)) != NULL)
390 if (!x942kdf_set_buffer(&ctx->ukm, &ctx->ukm_len, p))
391 return 0;
392
393 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CEK_ALG)) != NULL) {
394 if (p->data_type != OSSL_PARAM_UTF8_STRING)
395 return 0;
396 if (find_alg_id(provctx, p->data, &id) == 0)
397 return 0;
398 ctx->cek_oid = kek_algs[id].oid;
399 ctx->cek_oid_len = kek_algs[id].oid_len;
400 ctx->dkm_len = kek_algs[id].keklen;
401 }
402 return 1;
403 }
404
405 static const OSSL_PARAM *x942kdf_settable_ctx_params(void)
406 {
407 static const OSSL_PARAM known_settable_ctx_params[] = {
408 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
409 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
410 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
411 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
412 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_UKM, NULL, 0),
413 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CEK_ALG, NULL, 0),
414 OSSL_PARAM_END
415 };
416 return known_settable_ctx_params;
417 }
418
419 static int x942kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
420 {
421 KDF_X942 *ctx = (KDF_X942 *)vctx;
422 OSSL_PARAM *p;
423
424 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
425 return OSSL_PARAM_set_size_t(p, x942kdf_size(ctx));
426 return -2;
427 }
428
429 static const OSSL_PARAM *x942kdf_gettable_ctx_params(void)
430 {
431 static const OSSL_PARAM known_gettable_ctx_params[] = {
432 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
433 OSSL_PARAM_END
434 };
435 return known_gettable_ctx_params;
436 }
437
438 const OSSL_DISPATCH kdf_x942_kdf_functions[] = {
439 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))x942kdf_new },
440 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))x942kdf_free },
441 { OSSL_FUNC_KDF_RESET, (void(*)(void))x942kdf_reset },
442 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))x942kdf_derive },
443 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
444 (void(*)(void))x942kdf_settable_ctx_params },
445 { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))x942kdf_set_ctx_params },
446 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
447 (void(*)(void))x942kdf_gettable_ctx_params },
448 { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))x942kdf_get_ctx_params },
449 { 0, NULL }
450 };