cms_kari.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * Copyright 2013-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. * Low level key APIs (DH etc) are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include "internal/cryptlib.h"
  15. #include <openssl/asn1t.h>
  16. #include <openssl/pem.h>
  17. #include <openssl/x509v3.h>
  18. #include <openssl/err.h>
  19. #include <openssl/cms.h>
  20. #include <openssl/aes.h>
  21. #include "cms_local.h"
  22. #include "crypto/asn1.h"
  23. /* Key Agreement Recipient Info (KARI) routines */
  24. int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri,
  25. X509_ALGOR **palg,
  26. ASN1_OCTET_STRING **pukm)
  27. {
  28. if (ri->type != CMS_RECIPINFO_AGREE) {
  29. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT);
  30. return 0;
  31. }
  32. if (palg)
  33. *palg = ri->d.kari->keyEncryptionAlgorithm;
  34. if (pukm)
  35. *pukm = ri->d.kari->ukm;
  36. return 1;
  37. }
  38. /* Retrieve recipient encrypted keys from a kari */
  39. STACK_OF(CMS_RecipientEncryptedKey)
  40. *CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo *ri)
  41. {
  42. if (ri->type != CMS_RECIPINFO_AGREE) {
  43. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT);
  44. return NULL;
  45. }
  46. return ri->d.kari->recipientEncryptedKeys;
  47. }
  48. int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo *ri,
  49. X509_ALGOR **pubalg,
  50. ASN1_BIT_STRING **pubkey,
  51. ASN1_OCTET_STRING **keyid,
  52. X509_NAME **issuer,
  53. ASN1_INTEGER **sno)
  54. {
  55. CMS_OriginatorIdentifierOrKey *oik;
  56. if (ri->type != CMS_RECIPINFO_AGREE) {
  57. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT);
  58. return 0;
  59. }
  60. oik = ri->d.kari->originator;
  61. if (issuer)
  62. *issuer = NULL;
  63. if (sno)
  64. *sno = NULL;
  65. if (keyid)
  66. *keyid = NULL;
  67. if (pubalg)
  68. *pubalg = NULL;
  69. if (pubkey)
  70. *pubkey = NULL;
  71. if (oik->type == CMS_OIK_ISSUER_SERIAL) {
  72. if (issuer)
  73. *issuer = oik->d.issuerAndSerialNumber->issuer;
  74. if (sno)
  75. *sno = oik->d.issuerAndSerialNumber->serialNumber;
  76. } else if (oik->type == CMS_OIK_KEYIDENTIFIER) {
  77. if (keyid)
  78. *keyid = oik->d.subjectKeyIdentifier;
  79. } else if (oik->type == CMS_OIK_PUBKEY) {
  80. if (pubalg)
  81. *pubalg = oik->d.originatorKey->algorithm;
  82. if (pubkey)
  83. *pubkey = oik->d.originatorKey->publicKey;
  84. } else
  85. return 0;
  86. return 1;
  87. }
  88. int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo *ri, X509 *cert)
  89. {
  90. CMS_OriginatorIdentifierOrKey *oik;
  91. if (ri->type != CMS_RECIPINFO_AGREE) {
  92. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT);
  93. return -2;
  94. }
  95. oik = ri->d.kari->originator;
  96. if (oik->type == CMS_OIK_ISSUER_SERIAL)
  97. return ossl_cms_ias_cert_cmp(oik->d.issuerAndSerialNumber, cert);
  98. else if (oik->type == CMS_OIK_KEYIDENTIFIER)
  99. return ossl_cms_keyid_cert_cmp(oik->d.subjectKeyIdentifier, cert);
  100. return -1;
  101. }
  102. int CMS_RecipientEncryptedKey_get0_id(CMS_RecipientEncryptedKey *rek,
  103. ASN1_OCTET_STRING **keyid,
  104. ASN1_GENERALIZEDTIME **tm,
  105. CMS_OtherKeyAttribute **other,
  106. X509_NAME **issuer, ASN1_INTEGER **sno)
  107. {
  108. CMS_KeyAgreeRecipientIdentifier *rid = rek->rid;
  109. if (rid->type == CMS_REK_ISSUER_SERIAL) {
  110. if (issuer)
  111. *issuer = rid->d.issuerAndSerialNumber->issuer;
  112. if (sno)
  113. *sno = rid->d.issuerAndSerialNumber->serialNumber;
  114. if (keyid)
  115. *keyid = NULL;
  116. if (tm)
  117. *tm = NULL;
  118. if (other)
  119. *other = NULL;
  120. } else if (rid->type == CMS_REK_KEYIDENTIFIER) {
  121. if (keyid)
  122. *keyid = rid->d.rKeyId->subjectKeyIdentifier;
  123. if (tm)
  124. *tm = rid->d.rKeyId->date;
  125. if (other)
  126. *other = rid->d.rKeyId->other;
  127. if (issuer)
  128. *issuer = NULL;
  129. if (sno)
  130. *sno = NULL;
  131. } else
  132. return 0;
  133. return 1;
  134. }
  135. int CMS_RecipientEncryptedKey_cert_cmp(CMS_RecipientEncryptedKey *rek,
  136. X509 *cert)
  137. {
  138. CMS_KeyAgreeRecipientIdentifier *rid = rek->rid;
  139. if (rid->type == CMS_REK_ISSUER_SERIAL)
  140. return ossl_cms_ias_cert_cmp(rid->d.issuerAndSerialNumber, cert);
  141. else if (rid->type == CMS_REK_KEYIDENTIFIER)
  142. return ossl_cms_keyid_cert_cmp(rid->d.rKeyId->subjectKeyIdentifier,
  143. cert);
  144. else
  145. return -1;
  146. }
  147. int CMS_RecipientInfo_kari_set0_pkey_and_peer(CMS_RecipientInfo *ri,
  148. EVP_PKEY *pk, X509 *peer)
  149. {
  150. EVP_PKEY_CTX *pctx;
  151. CMS_KeyAgreeRecipientInfo *kari = ri->d.kari;
  152. EVP_PKEY_CTX_free(kari->pctx);
  153. kari->pctx = NULL;
  154. if (pk == NULL)
  155. return 1;
  156. pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(kari->cms_ctx),
  157. pk,
  158. ossl_cms_ctx_get0_propq(kari->cms_ctx));
  159. if (pctx == NULL || EVP_PKEY_derive_init(pctx) <= 0)
  160. goto err;
  161. if (peer != NULL) {
  162. EVP_PKEY *pub_pkey = X509_get0_pubkey(peer);
  163. if (EVP_PKEY_derive_set_peer(pctx, pub_pkey) <= 0)
  164. goto err;
  165. }
  166. kari->pctx = pctx;
  167. return 1;
  168. err:
  169. EVP_PKEY_CTX_free(pctx);
  170. return 0;
  171. }
  172. int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pk)
  173. {
  174. return CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, NULL);
  175. }
  176. EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri)
  177. {
  178. if (ri->type == CMS_RECIPINFO_AGREE)
  179. return ri->d.kari->ctx;
  180. return NULL;
  181. }
  182. /*
  183. * Derive KEK and decrypt/encrypt with it to produce either the original CEK
  184. * or the encrypted CEK.
  185. */
  186. static int cms_kek_cipher(unsigned char **pout, size_t *poutlen,
  187. const unsigned char *in, size_t inlen,
  188. CMS_KeyAgreeRecipientInfo *kari, int enc)
  189. {
  190. /* Key encryption key */
  191. unsigned char kek[EVP_MAX_KEY_LENGTH];
  192. size_t keklen;
  193. int rv = 0;
  194. unsigned char *out = NULL;
  195. int outlen;
  196. keklen = EVP_CIPHER_CTX_get_key_length(kari->ctx);
  197. if (keklen > EVP_MAX_KEY_LENGTH)
  198. return 0;
  199. /* Derive KEK */
  200. if (EVP_PKEY_derive(kari->pctx, kek, &keklen) <= 0)
  201. goto err;
  202. /* Set KEK in context */
  203. if (!EVP_CipherInit_ex(kari->ctx, NULL, NULL, kek, NULL, enc))
  204. goto err;
  205. /* obtain output length of ciphered key */
  206. if (!EVP_CipherUpdate(kari->ctx, NULL, &outlen, in, inlen))
  207. goto err;
  208. out = OPENSSL_malloc(outlen);
  209. if (out == NULL)
  210. goto err;
  211. if (!EVP_CipherUpdate(kari->ctx, out, &outlen, in, inlen))
  212. goto err;
  213. *pout = out;
  214. *poutlen = (size_t)outlen;
  215. rv = 1;
  216. err:
  217. OPENSSL_cleanse(kek, keklen);
  218. if (!rv)
  219. OPENSSL_free(out);
  220. EVP_CIPHER_CTX_reset(kari->ctx);
  221. /* FIXME: WHY IS kari->pctx freed here? /RL */
  222. EVP_PKEY_CTX_free(kari->pctx);
  223. kari->pctx = NULL;
  224. return rv;
  225. }
  226. int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo *cms,
  227. CMS_RecipientInfo *ri,
  228. CMS_RecipientEncryptedKey *rek)
  229. {
  230. int rv = 0;
  231. unsigned char *enckey = NULL, *cek = NULL;
  232. size_t enckeylen;
  233. size_t ceklen;
  234. CMS_EncryptedContentInfo *ec;
  235. enckeylen = rek->encryptedKey->length;
  236. enckey = rek->encryptedKey->data;
  237. /* Setup all parameters to derive KEK */
  238. if (!ossl_cms_env_asn1_ctrl(ri, 1))
  239. goto err;
  240. /* Attempt to decrypt CEK */
  241. if (!cms_kek_cipher(&cek, &ceklen, enckey, enckeylen, ri->d.kari, 0))
  242. goto err;
  243. ec = ossl_cms_get0_env_enc_content(cms);
  244. OPENSSL_clear_free(ec->key, ec->keylen);
  245. ec->key = cek;
  246. ec->keylen = ceklen;
  247. cek = NULL;
  248. rv = 1;
  249. err:
  250. OPENSSL_free(cek);
  251. return rv;
  252. }
  253. /* Create ephemeral key and initialise context based on it */
  254. static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo *kari,
  255. EVP_PKEY *pk)
  256. {
  257. EVP_PKEY_CTX *pctx = NULL;
  258. EVP_PKEY *ekey = NULL;
  259. int rv = 0;
  260. const CMS_CTX *ctx = kari->cms_ctx;
  261. OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
  262. const char *propq = ossl_cms_ctx_get0_propq(ctx);
  263. pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propq);
  264. if (pctx == NULL)
  265. goto err;
  266. if (EVP_PKEY_keygen_init(pctx) <= 0)
  267. goto err;
  268. if (EVP_PKEY_keygen(pctx, &ekey) <= 0)
  269. goto err;
  270. EVP_PKEY_CTX_free(pctx);
  271. pctx = EVP_PKEY_CTX_new_from_pkey(libctx, ekey, propq);
  272. if (pctx == NULL)
  273. goto err;
  274. if (EVP_PKEY_derive_init(pctx) <= 0)
  275. goto err;
  276. kari->pctx = pctx;
  277. rv = 1;
  278. err:
  279. if (!rv)
  280. EVP_PKEY_CTX_free(pctx);
  281. EVP_PKEY_free(ekey);
  282. return rv;
  283. }
  284. /* Set originator private key and initialise context based on it */
  285. static int cms_kari_set_originator_private_key(CMS_KeyAgreeRecipientInfo *kari,
  286. EVP_PKEY *originatorPrivKey )
  287. {
  288. EVP_PKEY_CTX *pctx = NULL;
  289. int rv = 0;
  290. const CMS_CTX *ctx = kari->cms_ctx;
  291. pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
  292. originatorPrivKey,
  293. ossl_cms_ctx_get0_propq(ctx));
  294. if (pctx == NULL)
  295. goto err;
  296. if (EVP_PKEY_derive_init(pctx) <= 0)
  297. goto err;
  298. kari->pctx = pctx;
  299. rv = 1;
  300. err:
  301. if (rv == 0)
  302. EVP_PKEY_CTX_free(pctx);
  303. return rv;
  304. }
  305. /* Initialise a kari based on passed certificate and key */
  306. int ossl_cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
  307. EVP_PKEY *recipPubKey, X509 *originator,
  308. EVP_PKEY *originatorPrivKey,
  309. unsigned int flags, const CMS_CTX *ctx)
  310. {
  311. CMS_KeyAgreeRecipientInfo *kari;
  312. CMS_RecipientEncryptedKey *rek = NULL;
  313. ri->d.kari = M_ASN1_new_of(CMS_KeyAgreeRecipientInfo);
  314. if (ri->d.kari == NULL)
  315. return 0;
  316. ri->type = CMS_RECIPINFO_AGREE;
  317. kari = ri->d.kari;
  318. kari->version = 3;
  319. kari->cms_ctx = ctx;
  320. rek = M_ASN1_new_of(CMS_RecipientEncryptedKey);
  321. if (rek == NULL)
  322. return 0;
  323. if (!sk_CMS_RecipientEncryptedKey_push(kari->recipientEncryptedKeys, rek)) {
  324. M_ASN1_free_of(rek, CMS_RecipientEncryptedKey);
  325. return 0;
  326. }
  327. if (flags & CMS_USE_KEYID) {
  328. rek->rid->type = CMS_REK_KEYIDENTIFIER;
  329. rek->rid->d.rKeyId = M_ASN1_new_of(CMS_RecipientKeyIdentifier);
  330. if (rek->rid->d.rKeyId == NULL)
  331. return 0;
  332. if (!ossl_cms_set1_keyid(&rek->rid->d.rKeyId->subjectKeyIdentifier, recip))
  333. return 0;
  334. } else {
  335. rek->rid->type = CMS_REK_ISSUER_SERIAL;
  336. if (!ossl_cms_set1_ias(&rek->rid->d.issuerAndSerialNumber, recip))
  337. return 0;
  338. }
  339. if (originatorPrivKey == NULL && originator == NULL) {
  340. /* Create ephemeral key */
  341. if (!cms_kari_create_ephemeral_key(kari, recipPubKey))
  342. return 0;
  343. } else {
  344. /* Use originator key */
  345. CMS_OriginatorIdentifierOrKey *oik = ri->d.kari->originator;
  346. if (originatorPrivKey == NULL || originator == NULL)
  347. return 0;
  348. if (flags & CMS_USE_ORIGINATOR_KEYID) {
  349. oik->type = CMS_OIK_KEYIDENTIFIER;
  350. oik->d.subjectKeyIdentifier = ASN1_OCTET_STRING_new();
  351. if (oik->d.subjectKeyIdentifier == NULL)
  352. return 0;
  353. if (!ossl_cms_set1_keyid(&oik->d.subjectKeyIdentifier, originator))
  354. return 0;
  355. } else {
  356. oik->type = CMS_REK_ISSUER_SERIAL;
  357. if (!ossl_cms_set1_ias(&oik->d.issuerAndSerialNumber, originator))
  358. return 0;
  359. }
  360. if (!cms_kari_set_originator_private_key(kari, originatorPrivKey))
  361. return 0;
  362. }
  363. EVP_PKEY_up_ref(recipPubKey);
  364. rek->pkey = recipPubKey;
  365. return 1;
  366. }
  367. static int cms_wrap_init(CMS_KeyAgreeRecipientInfo *kari,
  368. const EVP_CIPHER *cipher)
  369. {
  370. const CMS_CTX *cms_ctx = kari->cms_ctx;
  371. EVP_CIPHER_CTX *ctx = kari->ctx;
  372. const EVP_CIPHER *kekcipher;
  373. EVP_CIPHER *fetched_kekcipher;
  374. const char *kekcipher_name;
  375. int keylen;
  376. int ret;
  377. /* If a suitable wrap algorithm is already set nothing to do */
  378. kekcipher = EVP_CIPHER_CTX_get0_cipher(ctx);
  379. if (kekcipher != NULL) {
  380. if (EVP_CIPHER_CTX_get_mode(ctx) != EVP_CIPH_WRAP_MODE)
  381. return 0;
  382. return 1;
  383. }
  384. if (cipher == NULL)
  385. return 0;
  386. keylen = EVP_CIPHER_get_key_length(cipher);
  387. if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_GET_WRAP_CIPHER) != 0) {
  388. ret = EVP_CIPHER_meth_get_ctrl(cipher)(NULL, EVP_CTRL_GET_WRAP_CIPHER,
  389. 0, &kekcipher);
  390. if (ret <= 0)
  391. return 0;
  392. if (kekcipher != NULL) {
  393. if (EVP_CIPHER_get_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
  394. return 0;
  395. kekcipher_name = EVP_CIPHER_get0_name(kekcipher);
  396. goto enc;
  397. }
  398. }
  399. /*
  400. * Pick a cipher based on content encryption cipher. If it is DES3 use
  401. * DES3 wrap otherwise use AES wrap similar to key size.
  402. */
  403. #ifndef OPENSSL_NO_DES
  404. if (EVP_CIPHER_get_type(cipher) == NID_des_ede3_cbc)
  405. kekcipher_name = SN_id_smime_alg_CMS3DESwrap;
  406. else
  407. #endif
  408. if (keylen <= 16)
  409. kekcipher_name = SN_id_aes128_wrap;
  410. else if (keylen <= 24)
  411. kekcipher_name = SN_id_aes192_wrap;
  412. else
  413. kekcipher_name = SN_id_aes256_wrap;
  414. enc:
  415. fetched_kekcipher = EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(cms_ctx),
  416. kekcipher_name,
  417. ossl_cms_ctx_get0_propq(cms_ctx));
  418. if (fetched_kekcipher == NULL)
  419. return 0;
  420. ret = EVP_EncryptInit_ex(ctx, fetched_kekcipher, NULL, NULL, NULL);
  421. EVP_CIPHER_free(fetched_kekcipher);
  422. return ret;
  423. }
  424. /* Encrypt content key in key agreement recipient info */
  425. int ossl_cms_RecipientInfo_kari_encrypt(const CMS_ContentInfo *cms,
  426. CMS_RecipientInfo *ri)
  427. {
  428. CMS_KeyAgreeRecipientInfo *kari;
  429. CMS_EncryptedContentInfo *ec;
  430. CMS_RecipientEncryptedKey *rek;
  431. STACK_OF(CMS_RecipientEncryptedKey) *reks;
  432. int i;
  433. if (ri->type != CMS_RECIPINFO_AGREE) {
  434. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_AGREEMENT);
  435. return 0;
  436. }
  437. kari = ri->d.kari;
  438. reks = kari->recipientEncryptedKeys;
  439. ec = ossl_cms_get0_env_enc_content(cms);
  440. /* Initialise wrap algorithm parameters */
  441. if (!cms_wrap_init(kari, ec->cipher))
  442. return 0;
  443. /*
  444. * If no originator key set up initialise for ephemeral key the public key
  445. * ASN1 structure will set the actual public key value.
  446. */
  447. if (kari->originator->type == -1) {
  448. CMS_OriginatorIdentifierOrKey *oik = kari->originator;
  449. oik->type = CMS_OIK_PUBKEY;
  450. oik->d.originatorKey = M_ASN1_new_of(CMS_OriginatorPublicKey);
  451. if (!oik->d.originatorKey)
  452. return 0;
  453. }
  454. /* Initialise KDF algorithm */
  455. if (!ossl_cms_env_asn1_ctrl(ri, 0))
  456. return 0;
  457. /* For each rek, derive KEK, encrypt CEK */
  458. for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
  459. unsigned char *enckey;
  460. size_t enckeylen;
  461. rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
  462. if (EVP_PKEY_derive_set_peer(kari->pctx, rek->pkey) <= 0)
  463. return 0;
  464. if (!cms_kek_cipher(&enckey, &enckeylen, ec->key, ec->keylen,
  465. kari, 1))
  466. return 0;
  467. ASN1_STRING_set0(rek->encryptedKey, enckey, enckeylen);
  468. }
  469. return 1;
  470. }