cms_env.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /* crypto/cms/cms_env.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. */
  53. #include "cryptlib.h"
  54. #include <openssl/asn1t.h>
  55. #include <openssl/pem.h>
  56. #include <openssl/x509v3.h>
  57. #include <openssl/err.h>
  58. #include <openssl/cms.h>
  59. #include <openssl/rand.h>
  60. #include <openssl/aes.h>
  61. #include "cms_lcl.h"
  62. #include "asn1_locl.h"
  63. /* CMS EnvelopedData Utilities */
  64. DECLARE_ASN1_ITEM(CMS_EnvelopedData)
  65. DECLARE_ASN1_ITEM(CMS_RecipientInfo)
  66. DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
  67. DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
  68. DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
  69. DECLARE_STACK_OF(CMS_RecipientInfo)
  70. static CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
  71. {
  72. if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped)
  73. {
  74. CMSerr(CMS_F_CMS_GET0_ENVELOPED,
  75. CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
  76. return NULL;
  77. }
  78. return cms->d.envelopedData;
  79. }
  80. static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
  81. {
  82. if (cms->d.other == NULL)
  83. {
  84. cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
  85. if (!cms->d.envelopedData)
  86. {
  87. CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT,
  88. ERR_R_MALLOC_FAILURE);
  89. return NULL;
  90. }
  91. cms->d.envelopedData->version = 0;
  92. cms->d.envelopedData->encryptedContentInfo->contentType =
  93. OBJ_nid2obj(NID_pkcs7_data);
  94. ASN1_OBJECT_free(cms->contentType);
  95. cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
  96. return cms->d.envelopedData;
  97. }
  98. return cms_get0_enveloped(cms);
  99. }
  100. STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
  101. {
  102. CMS_EnvelopedData *env;
  103. env = cms_get0_enveloped(cms);
  104. if (!env)
  105. return NULL;
  106. return env->recipientInfos;
  107. }
  108. int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
  109. {
  110. return ri->type;
  111. }
  112. CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
  113. {
  114. CMS_ContentInfo *cms;
  115. CMS_EnvelopedData *env;
  116. cms = CMS_ContentInfo_new();
  117. if (!cms)
  118. goto merr;
  119. env = cms_enveloped_data_init(cms);
  120. if (!env)
  121. goto merr;
  122. if (!cms_EncryptedContent_init(env->encryptedContentInfo,
  123. cipher, NULL, 0))
  124. goto merr;
  125. return cms;
  126. merr:
  127. if (cms)
  128. CMS_ContentInfo_free(cms);
  129. CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
  130. return NULL;
  131. }
  132. /* Key Transport Recipient Info (KTRI) routines */
  133. /* Add a recipient certificate. For now only handle key transport.
  134. * If we ever handle key agreement will need updating.
  135. */
  136. CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
  137. X509 *recip, unsigned int flags)
  138. {
  139. CMS_RecipientInfo *ri = NULL;
  140. CMS_KeyTransRecipientInfo *ktri;
  141. CMS_EnvelopedData *env;
  142. EVP_PKEY *pk = NULL;
  143. int i, type;
  144. env = cms_get0_enveloped(cms);
  145. if (!env)
  146. goto err;
  147. /* Initialize recipient info */
  148. ri = M_ASN1_new_of(CMS_RecipientInfo);
  149. if (!ri)
  150. goto merr;
  151. /* Initialize and add key transport recipient info */
  152. ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
  153. if (!ri->d.ktri)
  154. goto merr;
  155. ri->type = CMS_RECIPINFO_TRANS;
  156. ktri = ri->d.ktri;
  157. X509_check_purpose(recip, -1, -1);
  158. pk = X509_get_pubkey(recip);
  159. if (!pk)
  160. {
  161. CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
  162. CMS_R_ERROR_GETTING_PUBLIC_KEY);
  163. goto err;
  164. }
  165. CRYPTO_add(&recip->references, 1, CRYPTO_LOCK_X509);
  166. ktri->pkey = pk;
  167. ktri->recip = recip;
  168. if (flags & CMS_USE_KEYID)
  169. {
  170. ktri->version = 2;
  171. type = CMS_RECIPINFO_KEYIDENTIFIER;
  172. }
  173. else
  174. {
  175. ktri->version = 0;
  176. type = CMS_RECIPINFO_ISSUER_SERIAL;
  177. }
  178. /* Not a typo: RecipientIdentifier and SignerIdentifier are the
  179. * same structure.
  180. */
  181. if (!cms_set1_SignerIdentifier(ktri->rid, recip, type))
  182. goto err;
  183. if (pk->ameth && pk->ameth->pkey_ctrl)
  184. {
  185. i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_ENVELOPE,
  186. 0, ri);
  187. if (i == -2)
  188. {
  189. CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
  190. CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  191. goto err;
  192. }
  193. if (i <= 0)
  194. {
  195. CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
  196. CMS_R_CTRL_FAILURE);
  197. goto err;
  198. }
  199. }
  200. if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
  201. goto merr;
  202. return ri;
  203. merr:
  204. CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
  205. err:
  206. if (ri)
  207. M_ASN1_free_of(ri, CMS_RecipientInfo);
  208. return NULL;
  209. }
  210. int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
  211. EVP_PKEY **pk, X509 **recip,
  212. X509_ALGOR **palg)
  213. {
  214. CMS_KeyTransRecipientInfo *ktri;
  215. if (ri->type != CMS_RECIPINFO_TRANS)
  216. {
  217. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
  218. CMS_R_NOT_KEY_TRANSPORT);
  219. return 0;
  220. }
  221. ktri = ri->d.ktri;
  222. if (pk)
  223. *pk = ktri->pkey;
  224. if (recip)
  225. *recip = ktri->recip;
  226. if (palg)
  227. *palg = ktri->keyEncryptionAlgorithm;
  228. return 1;
  229. }
  230. int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
  231. ASN1_OCTET_STRING **keyid,
  232. X509_NAME **issuer, ASN1_INTEGER **sno)
  233. {
  234. CMS_KeyTransRecipientInfo *ktri;
  235. if (ri->type != CMS_RECIPINFO_TRANS)
  236. {
  237. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
  238. CMS_R_NOT_KEY_TRANSPORT);
  239. return 0;
  240. }
  241. ktri = ri->d.ktri;
  242. return cms_SignerIdentifier_get0_signer_id(ktri->rid,
  243. keyid, issuer, sno);
  244. }
  245. int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
  246. {
  247. if (ri->type != CMS_RECIPINFO_TRANS)
  248. {
  249. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
  250. CMS_R_NOT_KEY_TRANSPORT);
  251. return -2;
  252. }
  253. return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
  254. }
  255. int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
  256. {
  257. if (ri->type != CMS_RECIPINFO_TRANS)
  258. {
  259. CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY,
  260. CMS_R_NOT_KEY_TRANSPORT);
  261. return 0;
  262. }
  263. ri->d.ktri->pkey = pkey;
  264. return 1;
  265. }
  266. /* Encrypt content key in key transport recipient info */
  267. static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms,
  268. CMS_RecipientInfo *ri)
  269. {
  270. CMS_KeyTransRecipientInfo *ktri;
  271. CMS_EncryptedContentInfo *ec;
  272. EVP_PKEY_CTX *pctx = NULL;
  273. unsigned char *ek = NULL;
  274. size_t eklen;
  275. int ret = 0;
  276. if (ri->type != CMS_RECIPINFO_TRANS)
  277. {
  278. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
  279. CMS_R_NOT_KEY_TRANSPORT);
  280. return 0;
  281. }
  282. ktri = ri->d.ktri;
  283. ec = cms->d.envelopedData->encryptedContentInfo;
  284. pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
  285. if (!pctx)
  286. return 0;
  287. if (EVP_PKEY_encrypt_init(pctx) <= 0)
  288. goto err;
  289. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
  290. EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0)
  291. {
  292. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
  293. goto err;
  294. }
  295. if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
  296. goto err;
  297. ek = OPENSSL_malloc(eklen);
  298. if (ek == NULL)
  299. {
  300. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT,
  301. ERR_R_MALLOC_FAILURE);
  302. goto err;
  303. }
  304. if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
  305. goto err;
  306. ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
  307. ek = NULL;
  308. ret = 1;
  309. err:
  310. if (pctx)
  311. EVP_PKEY_CTX_free(pctx);
  312. if (ek)
  313. OPENSSL_free(ek);
  314. return ret;
  315. }
  316. /* Decrypt content key from KTRI */
  317. static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
  318. CMS_RecipientInfo *ri)
  319. {
  320. CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
  321. EVP_PKEY_CTX *pctx = NULL;
  322. unsigned char *ek = NULL;
  323. size_t eklen;
  324. int ret = 0;
  325. if (ktri->pkey == NULL)
  326. {
  327. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT,
  328. CMS_R_NO_PRIVATE_KEY);
  329. return 0;
  330. }
  331. pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
  332. if (!pctx)
  333. return 0;
  334. if (EVP_PKEY_decrypt_init(pctx) <= 0)
  335. goto err;
  336. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT,
  337. EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0)
  338. {
  339. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
  340. goto err;
  341. }
  342. if (EVP_PKEY_decrypt(pctx, NULL, &eklen,
  343. ktri->encryptedKey->data,
  344. ktri->encryptedKey->length) <= 0)
  345. goto err;
  346. ek = OPENSSL_malloc(eklen);
  347. if (ek == NULL)
  348. {
  349. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT,
  350. ERR_R_MALLOC_FAILURE);
  351. goto err;
  352. }
  353. if (EVP_PKEY_decrypt(pctx, ek, &eklen,
  354. ktri->encryptedKey->data,
  355. ktri->encryptedKey->length) <= 0)
  356. {
  357. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
  358. goto err;
  359. }
  360. ret = 1;
  361. cms->d.envelopedData->encryptedContentInfo->key = ek;
  362. cms->d.envelopedData->encryptedContentInfo->keylen = eklen;
  363. err:
  364. if (pctx)
  365. EVP_PKEY_CTX_free(pctx);
  366. if (!ret && ek)
  367. OPENSSL_free(ek);
  368. return ret;
  369. }
  370. /* Key Encrypted Key (KEK) RecipientInfo routines */
  371. int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
  372. const unsigned char *id, size_t idlen)
  373. {
  374. ASN1_OCTET_STRING tmp_os;
  375. CMS_KEKRecipientInfo *kekri;
  376. if (ri->type != CMS_RECIPINFO_KEK)
  377. {
  378. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
  379. return -2;
  380. }
  381. kekri = ri->d.kekri;
  382. tmp_os.type = V_ASN1_OCTET_STRING;
  383. tmp_os.flags = 0;
  384. tmp_os.data = (unsigned char *)id;
  385. tmp_os.length = (int)idlen;
  386. return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
  387. }
  388. /* For now hard code AES key wrap info */
  389. static size_t aes_wrap_keylen(int nid)
  390. {
  391. switch (nid)
  392. {
  393. case NID_id_aes128_wrap:
  394. return 16;
  395. case NID_id_aes192_wrap:
  396. return 24;
  397. case NID_id_aes256_wrap:
  398. return 32;
  399. default:
  400. return 0;
  401. }
  402. }
  403. CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
  404. unsigned char *key, size_t keylen,
  405. unsigned char *id, size_t idlen,
  406. ASN1_GENERALIZEDTIME *date,
  407. ASN1_OBJECT *otherTypeId,
  408. ASN1_TYPE *otherType)
  409. {
  410. CMS_RecipientInfo *ri = NULL;
  411. CMS_EnvelopedData *env;
  412. CMS_KEKRecipientInfo *kekri;
  413. env = cms_get0_enveloped(cms);
  414. if (!env)
  415. goto err;
  416. if (nid == NID_undef)
  417. {
  418. switch (keylen)
  419. {
  420. case 16:
  421. nid = NID_id_aes128_wrap;
  422. break;
  423. case 24:
  424. nid = NID_id_aes192_wrap;
  425. break;
  426. case 32:
  427. nid = NID_id_aes256_wrap;
  428. break;
  429. default:
  430. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
  431. CMS_R_INVALID_KEY_LENGTH);
  432. goto err;
  433. }
  434. }
  435. else
  436. {
  437. size_t exp_keylen = aes_wrap_keylen(nid);
  438. if (!exp_keylen)
  439. {
  440. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
  441. CMS_R_UNSUPPORTED_KEK_ALGORITHM);
  442. goto err;
  443. }
  444. if (keylen != exp_keylen)
  445. {
  446. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
  447. CMS_R_INVALID_KEY_LENGTH);
  448. goto err;
  449. }
  450. }
  451. /* Initialize recipient info */
  452. ri = M_ASN1_new_of(CMS_RecipientInfo);
  453. if (!ri)
  454. goto merr;
  455. ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
  456. if (!ri->d.kekri)
  457. goto merr;
  458. ri->type = CMS_RECIPINFO_KEK;
  459. kekri = ri->d.kekri;
  460. if (otherTypeId)
  461. {
  462. kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
  463. if (kekri->kekid->other == NULL)
  464. goto merr;
  465. }
  466. if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
  467. goto merr;
  468. /* After this point no calls can fail */
  469. kekri->version = 4;
  470. kekri->key = key;
  471. kekri->keylen = keylen;
  472. ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
  473. kekri->kekid->date = date;
  474. if (kekri->kekid->other)
  475. {
  476. kekri->kekid->other->keyAttrId = otherTypeId;
  477. kekri->kekid->other->keyAttr = otherType;
  478. }
  479. X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
  480. OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
  481. return ri;
  482. merr:
  483. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
  484. err:
  485. if (ri)
  486. M_ASN1_free_of(ri, CMS_RecipientInfo);
  487. return NULL;
  488. }
  489. int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
  490. X509_ALGOR **palg,
  491. ASN1_OCTET_STRING **pid,
  492. ASN1_GENERALIZEDTIME **pdate,
  493. ASN1_OBJECT **potherid,
  494. ASN1_TYPE **pothertype)
  495. {
  496. CMS_KEKIdentifier *rkid;
  497. if (ri->type != CMS_RECIPINFO_KEK)
  498. {
  499. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
  500. return 0;
  501. }
  502. rkid = ri->d.kekri->kekid;
  503. if (palg)
  504. *palg = ri->d.kekri->keyEncryptionAlgorithm;
  505. if (pid)
  506. *pid = rkid->keyIdentifier;
  507. if (pdate)
  508. *pdate = rkid->date;
  509. if (potherid)
  510. {
  511. if (rkid->other)
  512. *potherid = rkid->other->keyAttrId;
  513. else
  514. *potherid = NULL;
  515. }
  516. if (pothertype)
  517. {
  518. if (rkid->other)
  519. *pothertype = rkid->other->keyAttr;
  520. else
  521. *pothertype = NULL;
  522. }
  523. return 1;
  524. }
  525. int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
  526. unsigned char *key, size_t keylen)
  527. {
  528. CMS_KEKRecipientInfo *kekri;
  529. if (ri->type != CMS_RECIPINFO_KEK)
  530. {
  531. CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
  532. return 0;
  533. }
  534. kekri = ri->d.kekri;
  535. kekri->key = key;
  536. kekri->keylen = keylen;
  537. return 1;
  538. }
  539. /* Encrypt content key in KEK recipient info */
  540. static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
  541. CMS_RecipientInfo *ri)
  542. {
  543. CMS_EncryptedContentInfo *ec;
  544. CMS_KEKRecipientInfo *kekri;
  545. AES_KEY actx;
  546. unsigned char *wkey = NULL;
  547. int wkeylen;
  548. int r = 0;
  549. ec = cms->d.envelopedData->encryptedContentInfo;
  550. kekri = ri->d.kekri;
  551. if (!kekri->key)
  552. {
  553. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
  554. return 0;
  555. }
  556. if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx))
  557. {
  558. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
  559. CMS_R_ERROR_SETTING_KEY);
  560. goto err;
  561. }
  562. wkey = OPENSSL_malloc(ec->keylen + 8);
  563. if (!wkey)
  564. {
  565. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
  566. ERR_R_MALLOC_FAILURE);
  567. goto err;
  568. }
  569. wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
  570. if (wkeylen <= 0)
  571. {
  572. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
  573. goto err;
  574. }
  575. ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
  576. r = 1;
  577. err:
  578. if (!r && wkey)
  579. OPENSSL_free(wkey);
  580. OPENSSL_cleanse(&actx, sizeof(actx));
  581. return r;
  582. }
  583. /* Decrypt content key in KEK recipient info */
  584. static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
  585. CMS_RecipientInfo *ri)
  586. {
  587. CMS_EncryptedContentInfo *ec;
  588. CMS_KEKRecipientInfo *kekri;
  589. AES_KEY actx;
  590. unsigned char *ukey = NULL;
  591. int ukeylen;
  592. int r = 0, wrap_nid;
  593. ec = cms->d.envelopedData->encryptedContentInfo;
  594. kekri = ri->d.kekri;
  595. if (!kekri->key)
  596. {
  597. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
  598. return 0;
  599. }
  600. wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
  601. if (aes_wrap_keylen(wrap_nid) != kekri->keylen)
  602. {
  603. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
  604. CMS_R_INVALID_KEY_LENGTH);
  605. return 0;
  606. }
  607. /* If encrypted key length is invalid don't bother */
  608. if (kekri->encryptedKey->length < 16)
  609. {
  610. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
  611. CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
  612. goto err;
  613. }
  614. if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx))
  615. {
  616. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
  617. CMS_R_ERROR_SETTING_KEY);
  618. goto err;
  619. }
  620. ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
  621. if (!ukey)
  622. {
  623. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
  624. ERR_R_MALLOC_FAILURE);
  625. goto err;
  626. }
  627. ukeylen = AES_unwrap_key(&actx, NULL, ukey,
  628. kekri->encryptedKey->data,
  629. kekri->encryptedKey->length);
  630. if (ukeylen <= 0)
  631. {
  632. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
  633. CMS_R_UNWRAP_ERROR);
  634. goto err;
  635. }
  636. ec->key = ukey;
  637. ec->keylen = ukeylen;
  638. r = 1;
  639. err:
  640. if (!r && ukey)
  641. OPENSSL_free(ukey);
  642. OPENSSL_cleanse(&actx, sizeof(actx));
  643. return r;
  644. }
  645. int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  646. {
  647. switch(ri->type)
  648. {
  649. case CMS_RECIPINFO_TRANS:
  650. return cms_RecipientInfo_ktri_decrypt(cms, ri);
  651. case CMS_RECIPINFO_KEK:
  652. return cms_RecipientInfo_kekri_decrypt(cms, ri);
  653. default:
  654. CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
  655. CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE);
  656. return 0;
  657. }
  658. }
  659. BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
  660. {
  661. CMS_EncryptedContentInfo *ec;
  662. STACK_OF(CMS_RecipientInfo) *rinfos;
  663. CMS_RecipientInfo *ri;
  664. int i, r, ok = 0;
  665. BIO *ret;
  666. /* Get BIO first to set up key */
  667. ec = cms->d.envelopedData->encryptedContentInfo;
  668. ret = cms_EncryptedContent_init_bio(ec);
  669. /* If error or no cipher end of processing */
  670. if (!ret || !ec->cipher)
  671. return ret;
  672. /* Now encrypt content key according to each RecipientInfo type */
  673. rinfos = cms->d.envelopedData->recipientInfos;
  674. for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++)
  675. {
  676. ri = sk_CMS_RecipientInfo_value(rinfos, i);
  677. switch (ri->type)
  678. {
  679. case CMS_RECIPINFO_TRANS:
  680. r = cms_RecipientInfo_ktri_encrypt(cms, ri);
  681. break;
  682. case CMS_RECIPINFO_KEK:
  683. r = cms_RecipientInfo_kekri_encrypt(cms, ri);
  684. break;
  685. default:
  686. CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
  687. CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
  688. goto err;
  689. }
  690. if (r <= 0)
  691. {
  692. CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
  693. CMS_R_ERROR_SETTING_RECIPIENTINFO);
  694. goto err;
  695. }
  696. }
  697. ok = 1;
  698. err:
  699. ec->cipher = NULL;
  700. if (ec->key)
  701. {
  702. OPENSSL_cleanse(ec->key, ec->keylen);
  703. OPENSSL_free(ec->key);
  704. ec->key = NULL;
  705. ec->keylen = 0;
  706. }
  707. if (ok)
  708. return ret;
  709. BIO_free(ret);
  710. return NULL;
  711. }