cms_kari.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * Copyright 2013-2020 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. #include "internal/cryptlib.h"
  10. #include <openssl/asn1t.h>
  11. #include <openssl/pem.h>
  12. #include <openssl/x509v3.h>
  13. #include <openssl/err.h>
  14. #include <openssl/cms.h>
  15. #include <openssl/aes.h>
  16. #include "cms_local.h"
  17. #include "crypto/asn1.h"
  18. DEFINE_STACK_OF(CMS_RecipientEncryptedKey)
  19. /* Key Agreement Recipient Info (KARI) routines */
  20. int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri,
  21. X509_ALGOR **palg,
  22. ASN1_OCTET_STRING **pukm)
  23. {
  24. if (ri->type != CMS_RECIPINFO_AGREE) {
  25. CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ALG,
  26. CMS_R_NOT_KEY_AGREEMENT);
  27. return 0;
  28. }
  29. if (palg)
  30. *palg = ri->d.kari->keyEncryptionAlgorithm;
  31. if (pukm)
  32. *pukm = ri->d.kari->ukm;
  33. return 1;
  34. }
  35. /* Retrieve recipient encrypted keys from a kari */
  36. STACK_OF(CMS_RecipientEncryptedKey)
  37. *CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo *ri)
  38. {
  39. if (ri->type != CMS_RECIPINFO_AGREE) {
  40. CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_REKS,
  41. CMS_R_NOT_KEY_AGREEMENT);
  42. return NULL;
  43. }
  44. return ri->d.kari->recipientEncryptedKeys;
  45. }
  46. int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo *ri,
  47. X509_ALGOR **pubalg,
  48. ASN1_BIT_STRING **pubkey,
  49. ASN1_OCTET_STRING **keyid,
  50. X509_NAME **issuer,
  51. ASN1_INTEGER **sno)
  52. {
  53. CMS_OriginatorIdentifierOrKey *oik;
  54. if (ri->type != CMS_RECIPINFO_AGREE) {
  55. CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ORIG_ID,
  56. CMS_R_NOT_KEY_AGREEMENT);
  57. return 0;
  58. }
  59. oik = ri->d.kari->originator;
  60. if (issuer)
  61. *issuer = NULL;
  62. if (sno)
  63. *sno = NULL;
  64. if (keyid)
  65. *keyid = NULL;
  66. if (pubalg)
  67. *pubalg = NULL;
  68. if (pubkey)
  69. *pubkey = NULL;
  70. if (oik->type == CMS_OIK_ISSUER_SERIAL) {
  71. if (issuer)
  72. *issuer = oik->d.issuerAndSerialNumber->issuer;
  73. if (sno)
  74. *sno = oik->d.issuerAndSerialNumber->serialNumber;
  75. } else if (oik->type == CMS_OIK_KEYIDENTIFIER) {
  76. if (keyid)
  77. *keyid = oik->d.subjectKeyIdentifier;
  78. } else if (oik->type == CMS_OIK_PUBKEY) {
  79. if (pubalg)
  80. *pubalg = oik->d.originatorKey->algorithm;
  81. if (pubkey)
  82. *pubkey = oik->d.originatorKey->publicKey;
  83. } else
  84. return 0;
  85. return 1;
  86. }
  87. int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo *ri, X509 *cert)
  88. {
  89. CMS_OriginatorIdentifierOrKey *oik;
  90. if (ri->type != CMS_RECIPINFO_AGREE) {
  91. CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ORIG_ID_CMP,
  92. 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 cms_ias_cert_cmp(oik->d.issuerAndSerialNumber, cert);
  98. else if (oik->type == CMS_OIK_KEYIDENTIFIER)
  99. return 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 cms_ias_cert_cmp(rid->d.issuerAndSerialNumber, cert);
  141. else if (rid->type == CMS_REK_KEYIDENTIFIER)
  142. return cms_keyid_cert_cmp(rid->d.rKeyId->subjectKeyIdentifier, cert);
  143. else
  144. return -1;
  145. }
  146. int CMS_RecipientInfo_kari_set0_pkey_and_peer(CMS_RecipientInfo *ri, EVP_PKEY *pk, X509 *peer)
  147. {
  148. EVP_PKEY_CTX *pctx;
  149. CMS_KeyAgreeRecipientInfo *kari = ri->d.kari;
  150. EVP_PKEY_CTX_free(kari->pctx);
  151. kari->pctx = NULL;
  152. if (pk == NULL)
  153. return 1;
  154. pctx = EVP_PKEY_CTX_new(pk, NULL);
  155. if (pctx == NULL || EVP_PKEY_derive_init(pctx) <= 0)
  156. goto err;
  157. if (peer != NULL) {
  158. EVP_PKEY *pub_pkey = X509_get0_pubkey(peer);
  159. if (EVP_PKEY_derive_set_peer(pctx, pub_pkey) <= 0)
  160. goto err;
  161. }
  162. kari->pctx = pctx;
  163. return 1;
  164. err:
  165. EVP_PKEY_CTX_free(pctx);
  166. return 0;
  167. }
  168. int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pk)
  169. {
  170. return CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, NULL);
  171. }
  172. EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri)
  173. {
  174. if (ri->type == CMS_RECIPINFO_AGREE)
  175. return ri->d.kari->ctx;
  176. return NULL;
  177. }
  178. /*
  179. * Derive KEK and decrypt/encrypt with it to produce either the original CEK
  180. * or the encrypted CEK.
  181. */
  182. static int cms_kek_cipher(unsigned char **pout, size_t *poutlen,
  183. const unsigned char *in, size_t inlen,
  184. CMS_KeyAgreeRecipientInfo *kari, int enc)
  185. {
  186. /* Key encryption key */
  187. unsigned char kek[EVP_MAX_KEY_LENGTH];
  188. size_t keklen;
  189. int rv = 0;
  190. unsigned char *out = NULL;
  191. int outlen;
  192. keklen = EVP_CIPHER_CTX_key_length(kari->ctx);
  193. if (keklen > EVP_MAX_KEY_LENGTH)
  194. return 0;
  195. /* Derive KEK */
  196. if (EVP_PKEY_derive(kari->pctx, kek, &keklen) <= 0)
  197. goto err;
  198. /* Set KEK in context */
  199. if (!EVP_CipherInit_ex(kari->ctx, NULL, NULL, kek, NULL, enc))
  200. goto err;
  201. /* obtain output length of ciphered key */
  202. if (!EVP_CipherUpdate(kari->ctx, NULL, &outlen, in, inlen))
  203. goto err;
  204. out = OPENSSL_malloc(outlen);
  205. if (out == NULL)
  206. goto err;
  207. if (!EVP_CipherUpdate(kari->ctx, out, &outlen, in, inlen))
  208. goto err;
  209. *pout = out;
  210. *poutlen = (size_t)outlen;
  211. rv = 1;
  212. err:
  213. OPENSSL_cleanse(kek, keklen);
  214. if (!rv)
  215. OPENSSL_free(out);
  216. EVP_CIPHER_CTX_reset(kari->ctx);
  217. /* FIXME: WHY IS kari->pctx freed here? /RL */
  218. EVP_PKEY_CTX_free(kari->pctx);
  219. kari->pctx = NULL;
  220. return rv;
  221. }
  222. int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo *cms,
  223. CMS_RecipientInfo *ri,
  224. CMS_RecipientEncryptedKey *rek)
  225. {
  226. int rv = 0;
  227. unsigned char *enckey = NULL, *cek = NULL;
  228. size_t enckeylen;
  229. size_t ceklen;
  230. CMS_EncryptedContentInfo *ec;
  231. {
  232. /*
  233. * TODO(3.0) Remove this when we have functionality to deserialize
  234. * parameters in EVP_PKEY form from an X509_ALGOR.
  235. * This is needed to be able to replace the EC_KEY specific decoding
  236. * that happens in ecdh_cms_set_peerkey() (crypto/ec/ec_ameth.c)
  237. *
  238. * THIS IS TEMPORARY
  239. */
  240. EVP_PKEY_CTX *pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  241. EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  242. EVP_PKEY_get0(pkey);
  243. if (EVP_PKEY_id(pkey) == EVP_PKEY_NONE) {
  244. CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_DECRYPT,
  245. CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  246. goto err;
  247. }
  248. }
  249. enckeylen = rek->encryptedKey->length;
  250. enckey = rek->encryptedKey->data;
  251. /* Setup all parameters to derive KEK */
  252. if (!cms_env_asn1_ctrl(ri, 1))
  253. goto err;
  254. /* Attempt to decrypt CEK */
  255. if (!cms_kek_cipher(&cek, &ceklen, enckey, enckeylen, ri->d.kari, 0))
  256. goto err;
  257. ec = cms->d.envelopedData->encryptedContentInfo;
  258. OPENSSL_clear_free(ec->key, ec->keylen);
  259. ec->key = cek;
  260. ec->keylen = ceklen;
  261. cek = NULL;
  262. rv = 1;
  263. err:
  264. OPENSSL_free(cek);
  265. return rv;
  266. }
  267. /* Create ephemeral key and initialise context based on it */
  268. static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo *kari,
  269. EVP_PKEY *pk)
  270. {
  271. EVP_PKEY_CTX *pctx = NULL;
  272. EVP_PKEY *ekey = NULL;
  273. int rv = 0;
  274. pctx = EVP_PKEY_CTX_new(pk, NULL);
  275. if (pctx == NULL)
  276. goto err;
  277. if (EVP_PKEY_keygen_init(pctx) <= 0)
  278. goto err;
  279. if (EVP_PKEY_keygen(pctx, &ekey) <= 0)
  280. goto err;
  281. EVP_PKEY_CTX_free(pctx);
  282. pctx = EVP_PKEY_CTX_new(ekey, NULL);
  283. if (pctx == NULL)
  284. goto err;
  285. if (EVP_PKEY_derive_init(pctx) <= 0)
  286. goto err;
  287. kari->pctx = pctx;
  288. rv = 1;
  289. err:
  290. if (!rv)
  291. EVP_PKEY_CTX_free(pctx);
  292. EVP_PKEY_free(ekey);
  293. return rv;
  294. }
  295. /* Set originator private key and initialise context based on it */
  296. static int cms_kari_set_originator_private_key(CMS_KeyAgreeRecipientInfo *kari, EVP_PKEY *originatorPrivKey )
  297. {
  298. EVP_PKEY_CTX *pctx = NULL;
  299. int rv = 0;
  300. pctx = EVP_PKEY_CTX_new(originatorPrivKey, NULL);
  301. if (pctx == NULL)
  302. goto err;
  303. if (EVP_PKEY_derive_init(pctx) <= 0)
  304. goto err;
  305. kari->pctx = pctx;
  306. rv = 1;
  307. err:
  308. if (rv == 0)
  309. EVP_PKEY_CTX_free(pctx);
  310. return rv;
  311. }
  312. /* Initialise a kari based on passed certificate and key */
  313. int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip, EVP_PKEY *recipPubKey, X509 * originator, EVP_PKEY *originatorPrivKey, unsigned int flags)
  314. {
  315. CMS_KeyAgreeRecipientInfo *kari;
  316. CMS_RecipientEncryptedKey *rek = NULL;
  317. ri->d.kari = M_ASN1_new_of(CMS_KeyAgreeRecipientInfo);
  318. if (!ri->d.kari)
  319. return 0;
  320. ri->type = CMS_RECIPINFO_AGREE;
  321. kari = ri->d.kari;
  322. kari->version = 3;
  323. rek = M_ASN1_new_of(CMS_RecipientEncryptedKey);
  324. if (rek == NULL)
  325. return 0;
  326. if (!sk_CMS_RecipientEncryptedKey_push(kari->recipientEncryptedKeys, rek)) {
  327. M_ASN1_free_of(rek, CMS_RecipientEncryptedKey);
  328. return 0;
  329. }
  330. if (flags & CMS_USE_KEYID) {
  331. rek->rid->type = CMS_REK_KEYIDENTIFIER;
  332. rek->rid->d.rKeyId = M_ASN1_new_of(CMS_RecipientKeyIdentifier);
  333. if (rek->rid->d.rKeyId == NULL)
  334. return 0;
  335. if (!cms_set1_keyid(&rek->rid->d.rKeyId->subjectKeyIdentifier, recip))
  336. return 0;
  337. } else {
  338. rek->rid->type = CMS_REK_ISSUER_SERIAL;
  339. if (!cms_set1_ias(&rek->rid->d.issuerAndSerialNumber, recip))
  340. return 0;
  341. }
  342. if (originatorPrivKey == NULL && originator == NULL) {
  343. /* Create ephemeral key */
  344. if (!cms_kari_create_ephemeral_key(kari, recipPubKey))
  345. return 0;
  346. } else {
  347. /* Use originator key */
  348. CMS_OriginatorIdentifierOrKey *oik = ri->d.kari->originator;
  349. if (originatorPrivKey == NULL || originator == NULL)
  350. return 0;
  351. if (flags & CMS_USE_ORIGINATOR_KEYID) {
  352. oik->type = CMS_OIK_KEYIDENTIFIER;
  353. oik->d.subjectKeyIdentifier = ASN1_OCTET_STRING_new();
  354. if (oik->d.subjectKeyIdentifier == NULL)
  355. return 0;
  356. if (!cms_set1_keyid(&oik->d.subjectKeyIdentifier, originator))
  357. return 0;
  358. } else {
  359. oik->type = CMS_REK_ISSUER_SERIAL;
  360. if (!cms_set1_ias(&oik->d.issuerAndSerialNumber, originator))
  361. return 0;
  362. }
  363. if (!cms_kari_set_originator_private_key(kari, originatorPrivKey))
  364. return 0;
  365. }
  366. EVP_PKEY_up_ref(recipPubKey);
  367. rek->pkey = recipPubKey;
  368. return 1;
  369. }
  370. static int cms_wrap_init(CMS_KeyAgreeRecipientInfo *kari,
  371. const EVP_CIPHER *cipher)
  372. {
  373. EVP_CIPHER_CTX *ctx = kari->ctx;
  374. const EVP_CIPHER *kekcipher;
  375. int keylen;
  376. int ret;
  377. /* If a suitable wrap algorithm is already set nothing to do */
  378. kekcipher = EVP_CIPHER_CTX_cipher(ctx);
  379. if (kekcipher != NULL) {
  380. if (EVP_CIPHER_CTX_mode(ctx) != EVP_CIPH_WRAP_MODE)
  381. return 0;
  382. return 1;
  383. }
  384. if (cipher == NULL)
  385. return 0;
  386. keylen = EVP_CIPHER_key_length(cipher);
  387. if ((EVP_CIPHER_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_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
  394. return 0;
  395. return EVP_EncryptInit_ex(ctx, kekcipher, NULL, NULL, NULL);
  396. }
  397. }
  398. /*
  399. * Pick a cipher based on content encryption cipher. If it is DES3 use
  400. * DES3 wrap otherwise use AES wrap similar to key size.
  401. */
  402. #ifndef OPENSSL_NO_DES
  403. if (EVP_CIPHER_type(cipher) == NID_des_ede3_cbc)
  404. kekcipher = EVP_des_ede3_wrap();
  405. else
  406. #endif
  407. if (keylen <= 16)
  408. kekcipher = EVP_aes_128_wrap();
  409. else if (keylen <= 24)
  410. kekcipher = EVP_aes_192_wrap();
  411. else
  412. kekcipher = EVP_aes_256_wrap();
  413. return EVP_EncryptInit_ex(ctx, kekcipher, NULL, NULL, NULL);
  414. }
  415. /* Encrypt content key in key agreement recipient info */
  416. int cms_RecipientInfo_kari_encrypt(const CMS_ContentInfo *cms,
  417. CMS_RecipientInfo *ri)
  418. {
  419. CMS_KeyAgreeRecipientInfo *kari;
  420. CMS_EncryptedContentInfo *ec;
  421. CMS_RecipientEncryptedKey *rek;
  422. STACK_OF(CMS_RecipientEncryptedKey) *reks;
  423. int i;
  424. {
  425. /*
  426. * TODO(3.0) Remove this when we have figured out all the details
  427. * need to set up encryption right. With legacy keys, a *lot* is
  428. * happening in the CMS specific EVP_PKEY_ASN1_METHOD functions,
  429. * such as automatically setting a default KDF type, KDF digest,
  430. * all that kind of stuff.
  431. * With EVP_SIGNATURE, setting a default digest is done by getting
  432. * the default MD for the key, and then inject that back into the
  433. * signature implementation... we could do something similar with
  434. * CMS, possibly using CMS specific OSSL_PARAM keys, just like we
  435. * have for certain AlgorithmIdentifier retrievals.
  436. *
  437. * THIS IS TEMPORARY
  438. */
  439. EVP_PKEY_CTX *pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  440. EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  441. EVP_PKEY_get0(pkey);
  442. if (EVP_PKEY_id(pkey) == EVP_PKEY_NONE) {
  443. CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT,
  444. CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  445. return 0;
  446. }
  447. }
  448. if (ri->type != CMS_RECIPINFO_AGREE) {
  449. CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT, CMS_R_NOT_KEY_AGREEMENT);
  450. return 0;
  451. }
  452. kari = ri->d.kari;
  453. reks = kari->recipientEncryptedKeys;
  454. ec = cms->d.envelopedData->encryptedContentInfo;
  455. /* Initialise wrap algorithm parameters */
  456. if (!cms_wrap_init(kari, ec->cipher))
  457. return 0;
  458. /*
  459. * If no originator key set up initialise for ephemeral key the public key
  460. * ASN1 structure will set the actual public key value.
  461. */
  462. if (kari->originator->type == -1) {
  463. CMS_OriginatorIdentifierOrKey *oik = kari->originator;
  464. oik->type = CMS_OIK_PUBKEY;
  465. oik->d.originatorKey = M_ASN1_new_of(CMS_OriginatorPublicKey);
  466. if (!oik->d.originatorKey)
  467. return 0;
  468. }
  469. /* Initialise KDF algorithm */
  470. if (!cms_env_asn1_ctrl(ri, 0))
  471. return 0;
  472. /* For each rek, derive KEK, encrypt CEK */
  473. for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
  474. unsigned char *enckey;
  475. size_t enckeylen;
  476. rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
  477. if (EVP_PKEY_derive_set_peer(kari->pctx, rek->pkey) <= 0)
  478. return 0;
  479. if (!cms_kek_cipher(&enckey, &enckeylen, ec->key, ec->keylen,
  480. kari, 1))
  481. return 0;
  482. ASN1_STRING_set0(rek->encryptedKey, enckey, enckeylen);
  483. }
  484. return 1;
  485. }