x942kdf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * Copyright 2019 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. #include "e_os.h"
  11. #ifndef OPENSSL_NO_CMS
  12. # include <stdlib.h>
  13. # include <stdarg.h>
  14. # include <string.h>
  15. # include <openssl/hmac.h>
  16. # include <openssl/cms.h>
  17. # include <openssl/evp.h>
  18. # include <openssl/kdf.h>
  19. # include <openssl/x509.h>
  20. # include <openssl/obj_mac.h>
  21. # include <openssl/core_names.h>
  22. # include "internal/cryptlib.h"
  23. # include "internal/numbers.h"
  24. # include "crypto/evp.h"
  25. # include "internal/provider_ctx.h"
  26. # include "internal/providercommonerr.h"
  27. # include "internal/provider_algs.h"
  28. # include "internal/provider_util.h"
  29. # define X942KDF_MAX_INLEN (1 << 30)
  30. static OSSL_OP_kdf_newctx_fn x942kdf_new;
  31. static OSSL_OP_kdf_freectx_fn x942kdf_free;
  32. static OSSL_OP_kdf_reset_fn x942kdf_reset;
  33. static OSSL_OP_kdf_derive_fn x942kdf_derive;
  34. static OSSL_OP_kdf_settable_ctx_params_fn x942kdf_settable_ctx_params;
  35. static OSSL_OP_kdf_set_ctx_params_fn x942kdf_set_ctx_params;
  36. static OSSL_OP_kdf_gettable_ctx_params_fn x942kdf_gettable_ctx_params;
  37. static OSSL_OP_kdf_get_ctx_params_fn x942kdf_get_ctx_params;
  38. typedef struct {
  39. void *provctx;
  40. PROV_DIGEST digest;
  41. unsigned char *secret;
  42. size_t secret_len;
  43. int cek_nid;
  44. unsigned char *ukm;
  45. size_t ukm_len;
  46. size_t dkm_len;
  47. } KDF_X942;
  48. /* A table of allowed wrapping algorithms and the associated output lengths */
  49. static const struct {
  50. int nid;
  51. size_t keklen; /* size in bytes */
  52. } kek_algs[] = {
  53. { NID_id_smime_alg_CMS3DESwrap, 24 },
  54. { NID_id_smime_alg_CMSRC2wrap, 16 },
  55. { NID_id_aes128_wrap, 16 },
  56. { NID_id_aes192_wrap, 24 },
  57. { NID_id_aes256_wrap, 32 },
  58. { NID_id_camellia128_wrap, 16 },
  59. { NID_id_camellia192_wrap, 24 },
  60. { NID_id_camellia256_wrap, 32 }
  61. };
  62. /* Skip past an ASN1 structure: for OBJECT skip content octets too */
  63. static int skip_asn1(unsigned char **pp, long *plen, int exptag)
  64. {
  65. int i, tag, xclass;
  66. long tmplen;
  67. const unsigned char *q = *pp;
  68. i = ASN1_get_object(&q, &tmplen, &tag, &xclass, *plen);
  69. if ((i & 0x80) != 0 || tag != exptag || xclass != V_ASN1_UNIVERSAL)
  70. return 0;
  71. if (tag == V_ASN1_OBJECT)
  72. q += tmplen;
  73. *pp = (unsigned char *)q;
  74. *plen -= q - *pp;
  75. return 1;
  76. }
  77. /*
  78. * Encode the other info structure.
  79. *
  80. * RFC2631 Section 2.1.2 Contains the following definition for otherinfo
  81. *
  82. * OtherInfo ::= SEQUENCE {
  83. * keyInfo KeySpecificInfo,
  84. * partyAInfo [0] OCTET STRING OPTIONAL,
  85. * suppPubInfo [2] OCTET STRING
  86. * }
  87. *
  88. * KeySpecificInfo ::= SEQUENCE {
  89. * algorithm OBJECT IDENTIFIER,
  90. * counter OCTET STRING SIZE (4..4)
  91. * }
  92. *
  93. * |nid| is the algorithm object identifier.
  94. * |keylen| is the length (in bytes) of the generated KEK. It is stored into
  95. * suppPubInfo (in bits).
  96. * |ukm| is the optional user keying material that is stored into partyAInfo. It
  97. * can be NULL.
  98. * |ukmlen| is the user keying material length (in bytes).
  99. * |der| is the returned encoded data. It must be freed by the caller.
  100. * |der_len| is the returned size of the encoded data.
  101. * |out_ctr| returns a pointer to the counter data which is embedded inside the
  102. * encoded data. This allows the counter bytes to be updated without re-encoding.
  103. *
  104. * Returns: 1 if successfully encoded, or 0 otherwise.
  105. * Assumptions: |der|, |der_len| & |out_ctr| are not NULL.
  106. */
  107. static int x942_encode_otherinfo(int nid, size_t keylen,
  108. const unsigned char *ukm, size_t ukmlen,
  109. unsigned char **der, size_t *der_len,
  110. unsigned char **out_ctr)
  111. {
  112. unsigned char *p, *encoded = NULL;
  113. int ret = 0, encoded_len;
  114. long tlen;
  115. /* "magic" value to check offset is sane */
  116. static unsigned char ctr[4] = { 0x00, 0x00, 0x00, 0x01 };
  117. X509_ALGOR *ksi = NULL;
  118. ASN1_OBJECT *alg_oid = NULL;
  119. ASN1_OCTET_STRING *ctr_oct = NULL, *ukm_oct = NULL;
  120. /* set the KeySpecificInfo - which contains an algorithm oid and counter */
  121. ksi = X509_ALGOR_new();
  122. alg_oid = OBJ_dup(OBJ_nid2obj(nid));
  123. ctr_oct = ASN1_OCTET_STRING_new();
  124. if (ksi == NULL
  125. || alg_oid == NULL
  126. || ctr_oct == NULL
  127. || !ASN1_OCTET_STRING_set(ctr_oct, ctr, sizeof(ctr))
  128. || !X509_ALGOR_set0(ksi, alg_oid, V_ASN1_OCTET_STRING, ctr_oct))
  129. goto err;
  130. /* NULL these as they now belong to ksi */
  131. alg_oid = NULL;
  132. ctr_oct = NULL;
  133. /* Set the optional partyAInfo */
  134. if (ukm != NULL) {
  135. ukm_oct = ASN1_OCTET_STRING_new();
  136. if (ukm_oct == NULL)
  137. goto err;
  138. ASN1_OCTET_STRING_set(ukm_oct, (unsigned char *)ukm, ukmlen);
  139. }
  140. /* Generate the OtherInfo DER data */
  141. encoded_len = CMS_SharedInfo_encode(&encoded, ksi, ukm_oct, keylen);
  142. if (encoded_len <= 0)
  143. goto err;
  144. /* Parse the encoded data to find the offset of the counter data */
  145. p = encoded;
  146. tlen = (long)encoded_len;
  147. if (skip_asn1(&p, &tlen, V_ASN1_SEQUENCE)
  148. && skip_asn1(&p, &tlen, V_ASN1_SEQUENCE)
  149. && skip_asn1(&p, &tlen, V_ASN1_OBJECT)
  150. && skip_asn1(&p, &tlen, V_ASN1_OCTET_STRING)
  151. && CRYPTO_memcmp(p, ctr, 4) == 0) {
  152. *out_ctr = p;
  153. *der = encoded;
  154. *der_len = (size_t)encoded_len;
  155. ret = 1;
  156. }
  157. err:
  158. if (ret != 1)
  159. OPENSSL_free(encoded);
  160. ASN1_OCTET_STRING_free(ctr_oct);
  161. ASN1_OCTET_STRING_free(ukm_oct);
  162. ASN1_OBJECT_free(alg_oid);
  163. X509_ALGOR_free(ksi);
  164. return ret;
  165. }
  166. static int x942kdf_hash_kdm(const EVP_MD *kdf_md,
  167. const unsigned char *z, size_t z_len,
  168. const unsigned char *other, size_t other_len,
  169. unsigned char *ctr,
  170. unsigned char *derived_key, size_t derived_key_len)
  171. {
  172. int ret = 0, hlen;
  173. size_t counter, out_len, len = derived_key_len;
  174. unsigned char mac[EVP_MAX_MD_SIZE];
  175. unsigned char *out = derived_key;
  176. EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
  177. if (z_len > X942KDF_MAX_INLEN || other_len > X942KDF_MAX_INLEN
  178. || derived_key_len > X942KDF_MAX_INLEN
  179. || derived_key_len == 0) {
  180. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
  181. return 0;
  182. }
  183. hlen = EVP_MD_size(kdf_md);
  184. if (hlen <= 0)
  185. return 0;
  186. out_len = (size_t)hlen;
  187. ctx = EVP_MD_CTX_create();
  188. ctx_init = EVP_MD_CTX_create();
  189. if (ctx == NULL || ctx_init == NULL)
  190. goto end;
  191. if (!EVP_DigestInit(ctx_init, kdf_md))
  192. goto end;
  193. for (counter = 1;; counter++) {
  194. /* updating the ctr modifies 4 bytes in the 'other' buffer */
  195. ctr[0] = (unsigned char)((counter >> 24) & 0xff);
  196. ctr[1] = (unsigned char)((counter >> 16) & 0xff);
  197. ctr[2] = (unsigned char)((counter >> 8) & 0xff);
  198. ctr[3] = (unsigned char)(counter & 0xff);
  199. if (!EVP_MD_CTX_copy_ex(ctx, ctx_init)
  200. || !EVP_DigestUpdate(ctx, z, z_len)
  201. || !EVP_DigestUpdate(ctx, other, other_len))
  202. goto end;
  203. if (len >= out_len) {
  204. if (!EVP_DigestFinal_ex(ctx, out, NULL))
  205. goto end;
  206. out += out_len;
  207. len -= out_len;
  208. if (len == 0)
  209. break;
  210. } else {
  211. if (!EVP_DigestFinal_ex(ctx, mac, NULL))
  212. goto end;
  213. memcpy(out, mac, len);
  214. break;
  215. }
  216. }
  217. ret = 1;
  218. end:
  219. EVP_MD_CTX_free(ctx);
  220. EVP_MD_CTX_free(ctx_init);
  221. OPENSSL_cleanse(mac, sizeof(mac));
  222. return ret;
  223. }
  224. static void *x942kdf_new(void *provctx)
  225. {
  226. KDF_X942 *ctx;
  227. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
  228. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  229. ctx->provctx = provctx;
  230. return ctx;
  231. }
  232. static void x942kdf_reset(void *vctx)
  233. {
  234. KDF_X942 *ctx = (KDF_X942 *)vctx;
  235. ossl_prov_digest_reset(&ctx->digest);
  236. OPENSSL_clear_free(ctx->secret, ctx->secret_len);
  237. OPENSSL_clear_free(ctx->ukm, ctx->ukm_len);
  238. memset(ctx, 0, sizeof(*ctx));
  239. }
  240. static void x942kdf_free(void *vctx)
  241. {
  242. KDF_X942 *ctx = (KDF_X942 *)vctx;
  243. x942kdf_reset(ctx);
  244. OPENSSL_free(ctx);
  245. }
  246. static int x942kdf_set_buffer(unsigned char **out, size_t *out_len,
  247. const OSSL_PARAM *p)
  248. {
  249. if (p->data_size == 0 || p->data == NULL)
  250. return 1;
  251. OPENSSL_free(*out);
  252. *out = NULL;
  253. return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
  254. }
  255. static size_t x942kdf_size(KDF_X942 *ctx)
  256. {
  257. int len;
  258. const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
  259. if (md == NULL) {
  260. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  261. return 0;
  262. }
  263. len = EVP_MD_size(md);
  264. return (len <= 0) ? 0 : (size_t)len;
  265. }
  266. static int x942kdf_derive(void *vctx, unsigned char *key, size_t keylen)
  267. {
  268. KDF_X942 *ctx = (KDF_X942 *)vctx;
  269. const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
  270. int ret = 0;
  271. unsigned char *ctr;
  272. unsigned char *der = NULL;
  273. size_t der_len = 0;
  274. if (ctx->secret == NULL) {
  275. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
  276. return 0;
  277. }
  278. if (md == NULL) {
  279. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  280. return 0;
  281. }
  282. if (ctx->cek_nid == NID_undef) {
  283. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CEK_ALG);
  284. return 0;
  285. }
  286. if (ctx->ukm != NULL && ctx->ukm_len >= X942KDF_MAX_INLEN) {
  287. /*
  288. * Note the ukm length MUST be 512 bits.
  289. * For backwards compatibility the old check is being done.
  290. */
  291. ERR_raise(ERR_LIB_PROV, PROV_R_INAVLID_UKM_LENGTH);
  292. return 0;
  293. }
  294. if (keylen != ctx->dkm_len) {
  295. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CEK_ALG);
  296. return 0;
  297. }
  298. /* generate the otherinfo der */
  299. if (!x942_encode_otherinfo(ctx->cek_nid, ctx->dkm_len,
  300. ctx->ukm, ctx->ukm_len,
  301. &der, &der_len, &ctr)) {
  302. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_ENCODING);
  303. return 0;
  304. }
  305. ret = x942kdf_hash_kdm(md, ctx->secret, ctx->secret_len,
  306. der, der_len, ctr, key, keylen);
  307. OPENSSL_free(der);
  308. return ret;
  309. }
  310. static int x942kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  311. {
  312. const OSSL_PARAM *p;
  313. KDF_X942 *ctx = vctx;
  314. OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
  315. size_t i;
  316. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
  317. return 0;
  318. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL
  319. || (p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
  320. if (!x942kdf_set_buffer(&ctx->secret, &ctx->secret_len, p))
  321. return 0;
  322. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_UKM)) != NULL)
  323. if (!x942kdf_set_buffer(&ctx->ukm, &ctx->ukm_len, p))
  324. return 0;
  325. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CEK_ALG)) != NULL) {
  326. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  327. return 0;
  328. ctx->cek_nid = OBJ_sn2nid(p->data);
  329. for (i = 0; i < OSSL_NELEM(kek_algs); i++)
  330. if (kek_algs[i].nid == ctx->cek_nid)
  331. goto cek_found;
  332. ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_CEK_ALG);
  333. return 0;
  334. cek_found:
  335. ctx->dkm_len = kek_algs[i].keklen;
  336. }
  337. return 1;
  338. }
  339. static const OSSL_PARAM *x942kdf_settable_ctx_params(void)
  340. {
  341. static const OSSL_PARAM known_settable_ctx_params[] = {
  342. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  343. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  344. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
  345. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
  346. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_UKM, NULL, 0),
  347. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CEK_ALG, NULL, 0),
  348. OSSL_PARAM_END
  349. };
  350. return known_settable_ctx_params;
  351. }
  352. static int x942kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
  353. {
  354. KDF_X942 *ctx = (KDF_X942 *)vctx;
  355. OSSL_PARAM *p;
  356. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  357. return OSSL_PARAM_set_size_t(p, x942kdf_size(ctx));
  358. return -2;
  359. }
  360. static const OSSL_PARAM *x942kdf_gettable_ctx_params(void)
  361. {
  362. static const OSSL_PARAM known_gettable_ctx_params[] = {
  363. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  364. OSSL_PARAM_END
  365. };
  366. return known_gettable_ctx_params;
  367. }
  368. const OSSL_DISPATCH kdf_x942_kdf_functions[] = {
  369. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))x942kdf_new },
  370. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))x942kdf_free },
  371. { OSSL_FUNC_KDF_RESET, (void(*)(void))x942kdf_reset },
  372. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))x942kdf_derive },
  373. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  374. (void(*)(void))x942kdf_settable_ctx_params },
  375. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))x942kdf_set_ctx_params },
  376. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  377. (void(*)(void))x942kdf_gettable_ctx_params },
  378. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))x942kdf_get_ctx_params },
  379. { 0, NULL }
  380. };
  381. #endif /* OPENSSL_NO_CMS */