2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
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
13 #ifndef OPENSSL_NO_CMS
18 # include <openssl/hmac.h>
19 # include <openssl/cms.h>
20 # include <openssl/evp.h>
21 # include <openssl/kdf.h>
22 # include <openssl/x509.h>
23 # include <openssl/obj_mac.h>
24 # include <openssl/core_names.h>
25 # include "internal/cryptlib.h"
26 # include "internal/numbers.h"
27 # include "internal/evp_int.h"
28 # include "internal/provider_ctx.h"
29 # include "internal/providercommonerr.h"
30 # include "internal/provider_algs.h"
32 # define X942KDF_MAX_INLEN (1 << 30)
34 static OSSL_OP_kdf_newctx_fn x942kdf_new;
35 static OSSL_OP_kdf_freectx_fn x942kdf_free;
36 static OSSL_OP_kdf_reset_fn x942kdf_reset;
37 static OSSL_OP_kdf_derive_fn x942kdf_derive;
38 static OSSL_OP_kdf_settable_ctx_params_fn x942kdf_settable_ctx_params;
39 static OSSL_OP_kdf_set_ctx_params_fn x942kdf_set_ctx_params;
40 static OSSL_OP_kdf_gettable_ctx_params_fn x942kdf_gettable_ctx_params;
41 static OSSL_OP_kdf_get_ctx_params_fn x942kdf_get_ctx_params;
46 unsigned char *secret;
54 /* A table of allowed wrapping algorithms and the associated output lengths */
57 size_t keklen; /* size in bytes */
59 { NID_id_smime_alg_CMS3DESwrap, 24 },
60 { NID_id_smime_alg_CMSRC2wrap, 16 },
61 { NID_id_aes128_wrap, 16 },
62 { NID_id_aes192_wrap, 24 },
63 { NID_id_aes256_wrap, 32 },
64 { NID_id_camellia128_wrap, 16 },
65 { NID_id_camellia192_wrap, 24 },
66 { NID_id_camellia256_wrap, 32 }
69 /* Skip past an ASN1 structure: for OBJECT skip content octets too */
70 static int skip_asn1(unsigned char **pp, long *plen, int exptag)
74 const unsigned char *q = *pp;
76 i = ASN1_get_object(&q, &tmplen, &tag, &xclass, *plen);
77 if ((i & 0x80) != 0 || tag != exptag || xclass != V_ASN1_UNIVERSAL)
79 if (tag == V_ASN1_OBJECT)
81 *pp = (unsigned char *)q;
87 * Encode the other info structure.
89 * RFC2631 Section 2.1.2 Contains the following definition for otherinfo
91 * OtherInfo ::= SEQUENCE {
92 * keyInfo KeySpecificInfo,
93 * partyAInfo [0] OCTET STRING OPTIONAL,
94 * suppPubInfo [2] OCTET STRING
97 * KeySpecificInfo ::= SEQUENCE {
98 * algorithm OBJECT IDENTIFIER,
99 * counter OCTET STRING SIZE (4..4)
102 * |nid| is the algorithm object identifier.
103 * |keylen| is the length (in bytes) of the generated KEK. It is stored into
104 * suppPubInfo (in bits).
105 * |ukm| is the optional user keying material that is stored into partyAInfo. It
107 * |ukmlen| is the user keying material length (in bytes).
108 * |der| is the returned encoded data. It must be freed by the caller.
109 * |der_len| is the returned size of the encoded data.
110 * |out_ctr| returns a pointer to the counter data which is embedded inside the
111 * encoded data. This allows the counter bytes to be updated without re-encoding.
113 * Returns: 1 if successfully encoded, or 0 otherwise.
114 * Assumptions: |der|, |der_len| & |out_ctr| are not NULL.
116 static int x942_encode_otherinfo(int nid, size_t keylen,
117 const unsigned char *ukm, size_t ukmlen,
118 unsigned char **der, size_t *der_len,
119 unsigned char **out_ctr)
121 unsigned char *p, *encoded = NULL;
122 int ret = 0, encoded_len;
124 /* "magic" value to check offset is sane */
125 static unsigned char ctr[4] = { 0x00, 0x00, 0x00, 0x01 };
126 X509_ALGOR *ksi = NULL;
127 ASN1_OBJECT *alg_oid = NULL;
128 ASN1_OCTET_STRING *ctr_oct = NULL, *ukm_oct = NULL;
130 /* set the KeySpecificInfo - which contains an algorithm oid and counter */
131 ksi = X509_ALGOR_new();
132 alg_oid = OBJ_dup(OBJ_nid2obj(nid));
133 ctr_oct = ASN1_OCTET_STRING_new();
137 || !ASN1_OCTET_STRING_set(ctr_oct, ctr, sizeof(ctr))
138 || !X509_ALGOR_set0(ksi, alg_oid, V_ASN1_OCTET_STRING, ctr_oct))
140 /* NULL these as they now belong to ksi */
144 /* Set the optional partyAInfo */
146 ukm_oct = ASN1_OCTET_STRING_new();
149 ASN1_OCTET_STRING_set(ukm_oct, (unsigned char *)ukm, ukmlen);
151 /* Generate the OtherInfo DER data */
152 encoded_len = CMS_SharedInfo_encode(&encoded, ksi, ukm_oct, keylen);
153 if (encoded_len <= 0)
156 /* Parse the encoded data to find the offset of the counter data */
158 tlen = (long)encoded_len;
159 if (skip_asn1(&p, &tlen, V_ASN1_SEQUENCE)
160 && skip_asn1(&p, &tlen, V_ASN1_SEQUENCE)
161 && skip_asn1(&p, &tlen, V_ASN1_OBJECT)
162 && skip_asn1(&p, &tlen, V_ASN1_OCTET_STRING)
163 && CRYPTO_memcmp(p, ctr, 4) == 0) {
166 *der_len = (size_t)encoded_len;
171 OPENSSL_free(encoded);
172 ASN1_OCTET_STRING_free(ctr_oct);
173 ASN1_OCTET_STRING_free(ukm_oct);
174 ASN1_OBJECT_free(alg_oid);
175 X509_ALGOR_free(ksi);
179 static int x942kdf_hash_kdm(const EVP_MD *kdf_md,
180 const unsigned char *z, size_t z_len,
181 const unsigned char *other, size_t other_len,
183 unsigned char *derived_key, size_t derived_key_len)
186 size_t counter, out_len, len = derived_key_len;
187 unsigned char mac[EVP_MAX_MD_SIZE];
188 unsigned char *out = derived_key;
189 EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
191 if (z_len > X942KDF_MAX_INLEN || other_len > X942KDF_MAX_INLEN
192 || derived_key_len > X942KDF_MAX_INLEN
193 || derived_key_len == 0) {
194 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
198 hlen = EVP_MD_size(kdf_md);
201 out_len = (size_t)hlen;
203 ctx = EVP_MD_CTX_create();
204 ctx_init = EVP_MD_CTX_create();
205 if (ctx == NULL || ctx_init == NULL)
208 if (!EVP_DigestInit(ctx_init, kdf_md))
211 for (counter = 1;; counter++) {
212 /* updating the ctr modifies 4 bytes in the 'other' buffer */
213 ctr[0] = (unsigned char)((counter >> 24) & 0xff);
214 ctr[1] = (unsigned char)((counter >> 16) & 0xff);
215 ctr[2] = (unsigned char)((counter >> 8) & 0xff);
216 ctr[3] = (unsigned char)(counter & 0xff);
218 if (!EVP_MD_CTX_copy_ex(ctx, ctx_init)
219 || !EVP_DigestUpdate(ctx, z, z_len)
220 || !EVP_DigestUpdate(ctx, other, other_len))
222 if (len >= out_len) {
223 if (!EVP_DigestFinal_ex(ctx, out, NULL))
230 if (!EVP_DigestFinal_ex(ctx, mac, NULL))
232 memcpy(out, mac, len);
238 EVP_MD_CTX_free(ctx);
239 EVP_MD_CTX_free(ctx_init);
240 OPENSSL_cleanse(mac, sizeof(mac));
244 static void *x942kdf_new(void *provctx)
248 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
249 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
250 ctx->provctx = provctx;
254 static void x942kdf_reset(void *vctx)
256 KDF_X942 *ctx = (KDF_X942 *)vctx;
258 OPENSSL_clear_free(ctx->secret, ctx->secret_len);
259 OPENSSL_clear_free(ctx->ukm, ctx->ukm_len);
260 memset(ctx, 0, sizeof(*ctx));
263 static void x942kdf_free(void *vctx)
265 KDF_X942 *ctx = (KDF_X942 *)vctx;
268 EVP_MD_meth_free(ctx->md);
272 static int x942kdf_set_buffer(unsigned char **out, size_t *out_len,
275 if (p->data_size == 0 || p->data == NULL)
280 return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
283 static size_t x942kdf_size(KDF_X942 *ctx)
287 if (ctx->md == NULL) {
288 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
291 len = EVP_MD_size(ctx->md);
292 return (len <= 0) ? 0 : (size_t)len;
295 static int x942kdf_derive(void *vctx, unsigned char *key, size_t keylen)
297 KDF_X942 *ctx = (KDF_X942 *)vctx;
300 unsigned char *der = NULL;
303 if (ctx->secret == NULL) {
304 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
307 if (ctx->md == NULL) {
308 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
311 if (ctx->cek_nid == NID_undef) {
312 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CEK_ALG);
315 if (ctx->ukm != NULL && ctx->ukm_len >= X942KDF_MAX_INLEN) {
317 * Note the ukm length MUST be 512 bits.
318 * For backwards compatibility the old check is being done.
320 ERR_raise(ERR_LIB_PROV, PROV_R_INAVLID_UKM_LENGTH);
323 if (keylen != ctx->dkm_len) {
324 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CEK_ALG);
327 /* generate the otherinfo der */
328 if (!x942_encode_otherinfo(ctx->cek_nid, ctx->dkm_len,
329 ctx->ukm, ctx->ukm_len,
330 &der, &der_len, &ctr)) {
331 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_ENCODING);
334 ret = x942kdf_hash_kdm(ctx->md, ctx->secret, ctx->secret_len,
335 der, der_len, ctr, key, keylen);
340 static int x942kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
343 KDF_X942 *ctx = vctx;
345 const char *properties = NULL;
348 /* Grab search properties, this should be before the digest lookup */
349 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PROPERTIES))
351 if (p->data_type != OSSL_PARAM_UTF8_STRING)
353 properties = p->data;
355 /* Handle aliasing of digest parameter names */
356 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) {
357 if (p->data_type != OSSL_PARAM_UTF8_STRING)
359 md = EVP_MD_fetch(PROV_LIBRARY_CONTEXT_OF(ctx->provctx), p->data,
362 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
365 EVP_MD_meth_free(ctx->md);
369 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL
370 || (p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
371 if (!x942kdf_set_buffer(&ctx->secret, &ctx->secret_len, p))
374 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_UKM)) != NULL)
375 if (!x942kdf_set_buffer(&ctx->ukm, &ctx->ukm_len, p))
378 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CEK_ALG)) != NULL) {
379 if (p->data_type != OSSL_PARAM_UTF8_STRING)
381 ctx->cek_nid = OBJ_sn2nid(p->data);
382 for (i = 0; i < OSSL_NELEM(kek_algs); i++)
383 if (kek_algs[i].nid == ctx->cek_nid)
385 ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_CEK_ALG);
388 ctx->dkm_len = kek_algs[i].keklen;
393 static const OSSL_PARAM *x942kdf_settable_ctx_params(void)
395 static const OSSL_PARAM known_settable_ctx_params[] = {
396 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
397 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
398 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
399 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
400 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_UKM, NULL, 0),
401 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CEK_ALG, NULL, 0),
404 return known_settable_ctx_params;
407 static int x942kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
409 KDF_X942 *ctx = (KDF_X942 *)vctx;
412 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
413 return OSSL_PARAM_set_size_t(p, x942kdf_size(ctx));
417 static const OSSL_PARAM *x942kdf_gettable_ctx_params(void)
419 static const OSSL_PARAM known_gettable_ctx_params[] = {
420 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
423 return known_gettable_ctx_params;
426 const OSSL_DISPATCH kdf_x942_kdf_functions[] = {
427 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))x942kdf_new },
428 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))x942kdf_free },
429 { OSSL_FUNC_KDF_RESET, (void(*)(void))x942kdf_reset },
430 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))x942kdf_derive },
431 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
432 (void(*)(void))x942kdf_settable_ctx_params },
433 { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))x942kdf_set_ctx_params },
434 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
435 (void(*)(void))x942kdf_gettable_ctx_params },
436 { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))x942kdf_get_ctx_params },
440 #endif /* OPENSSL_NO_CMS */