cms_env.c 22 KB

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