2
0

cms_env.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /* crypto/cms/cms_env.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) 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. * licensing@OpenSSL.org.
  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 "internal/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 "internal/asn1_int.h"
  64. #include "internal/evp_int.h"
  65. /* CMS EnvelopedData Utilities */
  66. CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
  67. {
  68. if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) {
  69. CMSerr(CMS_F_CMS_GET0_ENVELOPED,
  70. CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
  71. return NULL;
  72. }
  73. return cms->d.envelopedData;
  74. }
  75. static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
  76. {
  77. if (cms->d.other == NULL) {
  78. cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
  79. if (!cms->d.envelopedData) {
  80. CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT, ERR_R_MALLOC_FAILURE);
  81. return NULL;
  82. }
  83. cms->d.envelopedData->version = 0;
  84. cms->d.envelopedData->encryptedContentInfo->contentType =
  85. OBJ_nid2obj(NID_pkcs7_data);
  86. ASN1_OBJECT_free(cms->contentType);
  87. cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
  88. return cms->d.envelopedData;
  89. }
  90. return cms_get0_enveloped(cms);
  91. }
  92. int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
  93. {
  94. EVP_PKEY *pkey;
  95. int i;
  96. if (ri->type == CMS_RECIPINFO_TRANS)
  97. pkey = ri->d.ktri->pkey;
  98. else if (ri->type == CMS_RECIPINFO_AGREE) {
  99. EVP_PKEY_CTX *pctx = ri->d.kari->pctx;
  100. if (!pctx)
  101. return 0;
  102. pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  103. if (!pkey)
  104. return 0;
  105. } else
  106. return 0;
  107. if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
  108. return 1;
  109. i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
  110. if (i == -2) {
  111. CMSerr(CMS_F_CMS_ENV_ASN1_CTRL,
  112. CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  113. return 0;
  114. }
  115. if (i <= 0) {
  116. CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE);
  117. return 0;
  118. }
  119. return 1;
  120. }
  121. STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
  122. {
  123. CMS_EnvelopedData *env;
  124. env = cms_get0_enveloped(cms);
  125. if (!env)
  126. return NULL;
  127. return env->recipientInfos;
  128. }
  129. int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
  130. {
  131. return ri->type;
  132. }
  133. EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
  134. {
  135. if (ri->type == CMS_RECIPINFO_TRANS)
  136. return ri->d.ktri->pctx;
  137. else if (ri->type == CMS_RECIPINFO_AGREE)
  138. return ri->d.kari->pctx;
  139. return NULL;
  140. }
  141. CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
  142. {
  143. CMS_ContentInfo *cms;
  144. CMS_EnvelopedData *env;
  145. cms = CMS_ContentInfo_new();
  146. if (cms == NULL)
  147. goto merr;
  148. env = cms_enveloped_data_init(cms);
  149. if (env == NULL)
  150. goto merr;
  151. if (!cms_EncryptedContent_init(env->encryptedContentInfo,
  152. cipher, NULL, 0))
  153. goto merr;
  154. return cms;
  155. merr:
  156. CMS_ContentInfo_free(cms);
  157. CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
  158. return NULL;
  159. }
  160. /* Key Transport Recipient Info (KTRI) routines */
  161. /* Initialise a ktri based on passed certificate and key */
  162. static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
  163. EVP_PKEY *pk, unsigned int flags)
  164. {
  165. CMS_KeyTransRecipientInfo *ktri;
  166. int idtype;
  167. ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
  168. if (!ri->d.ktri)
  169. return 0;
  170. ri->type = CMS_RECIPINFO_TRANS;
  171. ktri = ri->d.ktri;
  172. if (flags & CMS_USE_KEYID) {
  173. ktri->version = 2;
  174. idtype = CMS_RECIPINFO_KEYIDENTIFIER;
  175. } else {
  176. ktri->version = 0;
  177. idtype = CMS_RECIPINFO_ISSUER_SERIAL;
  178. }
  179. /*
  180. * Not a typo: RecipientIdentifier and SignerIdentifier are the same
  181. * structure.
  182. */
  183. if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype))
  184. return 0;
  185. X509_up_ref(recip);
  186. CRYPTO_add(&pk->references, 1, CRYPTO_LOCK_EVP_PKEY);
  187. ktri->pkey = pk;
  188. ktri->recip = recip;
  189. if (flags & CMS_KEY_PARAM) {
  190. ktri->pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
  191. if (ktri->pctx == NULL)
  192. return 0;
  193. if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
  194. return 0;
  195. } else if (!cms_env_asn1_ctrl(ri, 0))
  196. return 0;
  197. return 1;
  198. }
  199. /*
  200. * Add a recipient certificate using appropriate type of RecipientInfo
  201. */
  202. CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
  203. X509 *recip, unsigned int flags)
  204. {
  205. CMS_RecipientInfo *ri = NULL;
  206. CMS_EnvelopedData *env;
  207. EVP_PKEY *pk = NULL;
  208. env = cms_get0_enveloped(cms);
  209. if (!env)
  210. goto err;
  211. /* Initialize recipient info */
  212. ri = M_ASN1_new_of(CMS_RecipientInfo);
  213. if (!ri)
  214. goto merr;
  215. pk = X509_get0_pubkey(recip);
  216. if (!pk) {
  217. CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, CMS_R_ERROR_GETTING_PUBLIC_KEY);
  218. goto err;
  219. }
  220. switch (cms_pkey_get_ri_type(pk)) {
  221. case CMS_RECIPINFO_TRANS:
  222. if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags))
  223. goto err;
  224. break;
  225. case CMS_RECIPINFO_AGREE:
  226. if (!cms_RecipientInfo_kari_init(ri, recip, pk, flags))
  227. goto err;
  228. break;
  229. default:
  230. CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
  231. CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  232. goto err;
  233. }
  234. if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
  235. goto merr;
  236. return ri;
  237. merr:
  238. CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
  239. err:
  240. M_ASN1_free_of(ri, CMS_RecipientInfo);
  241. return NULL;
  242. }
  243. int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
  244. EVP_PKEY **pk, X509 **recip,
  245. X509_ALGOR **palg)
  246. {
  247. CMS_KeyTransRecipientInfo *ktri;
  248. if (ri->type != CMS_RECIPINFO_TRANS) {
  249. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
  250. CMS_R_NOT_KEY_TRANSPORT);
  251. return 0;
  252. }
  253. ktri = ri->d.ktri;
  254. if (pk)
  255. *pk = ktri->pkey;
  256. if (recip)
  257. *recip = ktri->recip;
  258. if (palg)
  259. *palg = ktri->keyEncryptionAlgorithm;
  260. return 1;
  261. }
  262. int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
  263. ASN1_OCTET_STRING **keyid,
  264. X509_NAME **issuer,
  265. ASN1_INTEGER **sno)
  266. {
  267. CMS_KeyTransRecipientInfo *ktri;
  268. if (ri->type != CMS_RECIPINFO_TRANS) {
  269. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
  270. CMS_R_NOT_KEY_TRANSPORT);
  271. return 0;
  272. }
  273. ktri = ri->d.ktri;
  274. return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
  275. }
  276. int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
  277. {
  278. if (ri->type != CMS_RECIPINFO_TRANS) {
  279. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
  280. CMS_R_NOT_KEY_TRANSPORT);
  281. return -2;
  282. }
  283. return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
  284. }
  285. int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
  286. {
  287. if (ri->type != CMS_RECIPINFO_TRANS) {
  288. CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT);
  289. return 0;
  290. }
  291. ri->d.ktri->pkey = pkey;
  292. return 1;
  293. }
  294. /* Encrypt content key in key transport recipient info */
  295. static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms,
  296. CMS_RecipientInfo *ri)
  297. {
  298. CMS_KeyTransRecipientInfo *ktri;
  299. CMS_EncryptedContentInfo *ec;
  300. EVP_PKEY_CTX *pctx;
  301. unsigned char *ek = NULL;
  302. size_t eklen;
  303. int ret = 0;
  304. if (ri->type != CMS_RECIPINFO_TRANS) {
  305. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_NOT_KEY_TRANSPORT);
  306. return 0;
  307. }
  308. ktri = ri->d.ktri;
  309. ec = cms->d.envelopedData->encryptedContentInfo;
  310. pctx = ktri->pctx;
  311. if (pctx) {
  312. if (!cms_env_asn1_ctrl(ri, 0))
  313. goto err;
  314. } else {
  315. pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
  316. if (pctx == NULL)
  317. return 0;
  318. if (EVP_PKEY_encrypt_init(pctx) <= 0)
  319. goto err;
  320. }
  321. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
  322. EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0) {
  323. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
  324. goto err;
  325. }
  326. if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
  327. goto err;
  328. ek = OPENSSL_malloc(eklen);
  329. if (ek == NULL) {
  330. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
  331. goto err;
  332. }
  333. if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
  334. goto err;
  335. ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
  336. ek = NULL;
  337. ret = 1;
  338. err:
  339. EVP_PKEY_CTX_free(pctx);
  340. ktri->pctx = NULL;
  341. OPENSSL_free(ek);
  342. return ret;
  343. }
  344. /* Decrypt content key from KTRI */
  345. static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
  346. CMS_RecipientInfo *ri)
  347. {
  348. CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
  349. EVP_PKEY *pkey = ktri->pkey;
  350. unsigned char *ek = NULL;
  351. size_t eklen;
  352. int ret = 0;
  353. CMS_EncryptedContentInfo *ec;
  354. ec = cms->d.envelopedData->encryptedContentInfo;
  355. if (ktri->pkey == NULL) {
  356. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_NO_PRIVATE_KEY);
  357. return 0;
  358. }
  359. ktri->pctx = EVP_PKEY_CTX_new(pkey, NULL);
  360. if (ktri->pctx == NULL)
  361. return 0;
  362. if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
  363. goto err;
  364. if (!cms_env_asn1_ctrl(ri, 1))
  365. goto err;
  366. if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
  367. EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) {
  368. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
  369. goto err;
  370. }
  371. if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
  372. ktri->encryptedKey->data,
  373. ktri->encryptedKey->length) <= 0)
  374. goto err;
  375. ek = OPENSSL_malloc(eklen);
  376. if (ek == NULL) {
  377. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, ERR_R_MALLOC_FAILURE);
  378. goto err;
  379. }
  380. if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
  381. ktri->encryptedKey->data,
  382. ktri->encryptedKey->length) <= 0) {
  383. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
  384. goto err;
  385. }
  386. ret = 1;
  387. OPENSSL_clear_free(ec->key, ec->keylen);
  388. ec->key = ek;
  389. ec->keylen = eklen;
  390. err:
  391. EVP_PKEY_CTX_free(ktri->pctx);
  392. ktri->pctx = NULL;
  393. if (!ret)
  394. OPENSSL_free(ek);
  395. return ret;
  396. }
  397. /* Key Encrypted Key (KEK) RecipientInfo routines */
  398. int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
  399. const unsigned char *id, size_t idlen)
  400. {
  401. ASN1_OCTET_STRING tmp_os;
  402. CMS_KEKRecipientInfo *kekri;
  403. if (ri->type != CMS_RECIPINFO_KEK) {
  404. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
  405. return -2;
  406. }
  407. kekri = ri->d.kekri;
  408. tmp_os.type = V_ASN1_OCTET_STRING;
  409. tmp_os.flags = 0;
  410. tmp_os.data = (unsigned char *)id;
  411. tmp_os.length = (int)idlen;
  412. return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
  413. }
  414. /* For now hard code AES key wrap info */
  415. static size_t aes_wrap_keylen(int nid)
  416. {
  417. switch (nid) {
  418. case NID_id_aes128_wrap:
  419. return 16;
  420. case NID_id_aes192_wrap:
  421. return 24;
  422. case NID_id_aes256_wrap:
  423. return 32;
  424. default:
  425. return 0;
  426. }
  427. }
  428. CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
  429. unsigned char *key, size_t keylen,
  430. unsigned char *id, size_t idlen,
  431. ASN1_GENERALIZEDTIME *date,
  432. ASN1_OBJECT *otherTypeId,
  433. ASN1_TYPE *otherType)
  434. {
  435. CMS_RecipientInfo *ri = NULL;
  436. CMS_EnvelopedData *env;
  437. CMS_KEKRecipientInfo *kekri;
  438. env = cms_get0_enveloped(cms);
  439. if (!env)
  440. goto err;
  441. if (nid == NID_undef) {
  442. switch (keylen) {
  443. case 16:
  444. nid = NID_id_aes128_wrap;
  445. break;
  446. case 24:
  447. nid = NID_id_aes192_wrap;
  448. break;
  449. case 32:
  450. nid = NID_id_aes256_wrap;
  451. break;
  452. default:
  453. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
  454. goto err;
  455. }
  456. } else {
  457. size_t exp_keylen = aes_wrap_keylen(nid);
  458. if (!exp_keylen) {
  459. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
  460. CMS_R_UNSUPPORTED_KEK_ALGORITHM);
  461. goto err;
  462. }
  463. if (keylen != exp_keylen) {
  464. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
  465. goto err;
  466. }
  467. }
  468. /* Initialize recipient info */
  469. ri = M_ASN1_new_of(CMS_RecipientInfo);
  470. if (!ri)
  471. goto merr;
  472. ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
  473. if (!ri->d.kekri)
  474. goto merr;
  475. ri->type = CMS_RECIPINFO_KEK;
  476. kekri = ri->d.kekri;
  477. if (otherTypeId) {
  478. kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
  479. if (kekri->kekid->other == NULL)
  480. goto merr;
  481. }
  482. if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
  483. goto merr;
  484. /* After this point no calls can fail */
  485. kekri->version = 4;
  486. kekri->key = key;
  487. kekri->keylen = keylen;
  488. ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
  489. kekri->kekid->date = date;
  490. if (kekri->kekid->other) {
  491. kekri->kekid->other->keyAttrId = otherTypeId;
  492. kekri->kekid->other->keyAttr = otherType;
  493. }
  494. X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
  495. OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
  496. return ri;
  497. merr:
  498. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
  499. err:
  500. M_ASN1_free_of(ri, CMS_RecipientInfo);
  501. return NULL;
  502. }
  503. int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
  504. X509_ALGOR **palg,
  505. ASN1_OCTET_STRING **pid,
  506. ASN1_GENERALIZEDTIME **pdate,
  507. ASN1_OBJECT **potherid,
  508. ASN1_TYPE **pothertype)
  509. {
  510. CMS_KEKIdentifier *rkid;
  511. if (ri->type != CMS_RECIPINFO_KEK) {
  512. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
  513. return 0;
  514. }
  515. rkid = ri->d.kekri->kekid;
  516. if (palg)
  517. *palg = ri->d.kekri->keyEncryptionAlgorithm;
  518. if (pid)
  519. *pid = rkid->keyIdentifier;
  520. if (pdate)
  521. *pdate = rkid->date;
  522. if (potherid) {
  523. if (rkid->other)
  524. *potherid = rkid->other->keyAttrId;
  525. else
  526. *potherid = NULL;
  527. }
  528. if (pothertype) {
  529. if (rkid->other)
  530. *pothertype = rkid->other->keyAttr;
  531. else
  532. *pothertype = NULL;
  533. }
  534. return 1;
  535. }
  536. int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
  537. unsigned char *key, size_t keylen)
  538. {
  539. CMS_KEKRecipientInfo *kekri;
  540. if (ri->type != CMS_RECIPINFO_KEK) {
  541. CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
  542. return 0;
  543. }
  544. kekri = ri->d.kekri;
  545. kekri->key = key;
  546. kekri->keylen = keylen;
  547. return 1;
  548. }
  549. /* Encrypt content key in KEK recipient info */
  550. static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
  551. CMS_RecipientInfo *ri)
  552. {
  553. CMS_EncryptedContentInfo *ec;
  554. CMS_KEKRecipientInfo *kekri;
  555. AES_KEY actx;
  556. unsigned char *wkey = NULL;
  557. int wkeylen;
  558. int r = 0;
  559. ec = cms->d.envelopedData->encryptedContentInfo;
  560. kekri = ri->d.kekri;
  561. if (!kekri->key) {
  562. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
  563. return 0;
  564. }
  565. if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
  566. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
  567. CMS_R_ERROR_SETTING_KEY);
  568. goto err;
  569. }
  570. wkey = OPENSSL_malloc(ec->keylen + 8);
  571. if (wkey == NULL) {
  572. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
  573. goto err;
  574. }
  575. wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
  576. if (wkeylen <= 0) {
  577. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
  578. goto err;
  579. }
  580. ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
  581. r = 1;
  582. err:
  583. if (!r)
  584. OPENSSL_free(wkey);
  585. OPENSSL_cleanse(&actx, sizeof(actx));
  586. return r;
  587. }
  588. /* Decrypt content key in KEK recipient info */
  589. static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
  590. CMS_RecipientInfo *ri)
  591. {
  592. CMS_EncryptedContentInfo *ec;
  593. CMS_KEKRecipientInfo *kekri;
  594. AES_KEY actx;
  595. unsigned char *ukey = NULL;
  596. int ukeylen;
  597. int r = 0, wrap_nid;
  598. ec = cms->d.envelopedData->encryptedContentInfo;
  599. kekri = ri->d.kekri;
  600. if (!kekri->key) {
  601. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
  602. return 0;
  603. }
  604. wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
  605. if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
  606. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
  607. CMS_R_INVALID_KEY_LENGTH);
  608. return 0;
  609. }
  610. /* If encrypted key length is invalid don't bother */
  611. if (kekri->encryptedKey->length < 16) {
  612. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
  613. CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
  614. goto err;
  615. }
  616. if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
  617. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
  618. CMS_R_ERROR_SETTING_KEY);
  619. goto err;
  620. }
  621. ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
  622. if (ukey == NULL) {
  623. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
  624. goto err;
  625. }
  626. ukeylen = AES_unwrap_key(&actx, NULL, ukey,
  627. kekri->encryptedKey->data,
  628. kekri->encryptedKey->length);
  629. if (ukeylen <= 0) {
  630. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR);
  631. goto err;
  632. }
  633. ec->key = ukey;
  634. ec->keylen = ukeylen;
  635. r = 1;
  636. err:
  637. if (!r)
  638. OPENSSL_free(ukey);
  639. OPENSSL_cleanse(&actx, sizeof(actx));
  640. return r;
  641. }
  642. int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  643. {
  644. switch (ri->type) {
  645. case CMS_RECIPINFO_TRANS:
  646. return cms_RecipientInfo_ktri_decrypt(cms, ri);
  647. case CMS_RECIPINFO_KEK:
  648. return cms_RecipientInfo_kekri_decrypt(cms, ri);
  649. case CMS_RECIPINFO_PASS:
  650. return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
  651. default:
  652. CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
  653. CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE);
  654. return 0;
  655. }
  656. }
  657. int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  658. {
  659. switch (ri->type) {
  660. case CMS_RECIPINFO_TRANS:
  661. return cms_RecipientInfo_ktri_encrypt(cms, ri);
  662. case CMS_RECIPINFO_AGREE:
  663. return cms_RecipientInfo_kari_encrypt(cms, ri);
  664. case CMS_RECIPINFO_KEK:
  665. return cms_RecipientInfo_kekri_encrypt(cms, ri);
  666. case CMS_RECIPINFO_PASS:
  667. return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
  668. default:
  669. CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,
  670. CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
  671. return 0;
  672. }
  673. }
  674. /* Check structures and fixup version numbers (if necessary) */
  675. static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
  676. {
  677. CMS_OriginatorInfo *org = env->originatorInfo;
  678. int i;
  679. if (org == NULL)
  680. return;
  681. for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
  682. CMS_CertificateChoices *cch;
  683. cch = sk_CMS_CertificateChoices_value(org->certificates, i);
  684. if (cch->type == CMS_CERTCHOICE_OTHER) {
  685. env->version = 4;
  686. return;
  687. } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
  688. if (env->version < 3)
  689. env->version = 3;
  690. }
  691. }
  692. for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
  693. CMS_RevocationInfoChoice *rch;
  694. rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
  695. if (rch->type == CMS_REVCHOICE_OTHER) {
  696. env->version = 4;
  697. return;
  698. }
  699. }
  700. }
  701. static void cms_env_set_version(CMS_EnvelopedData *env)
  702. {
  703. int i;
  704. CMS_RecipientInfo *ri;
  705. /*
  706. * Can't set version higher than 4 so if 4 or more already nothing to do.
  707. */
  708. if (env->version >= 4)
  709. return;
  710. cms_env_set_originfo_version(env);
  711. if (env->version >= 3)
  712. return;
  713. for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
  714. ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
  715. if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
  716. env->version = 3;
  717. return;
  718. } else if (ri->type != CMS_RECIPINFO_TRANS
  719. || ri->d.ktri->version != 0) {
  720. env->version = 2;
  721. }
  722. }
  723. if (env->version == 2)
  724. return;
  725. if (env->originatorInfo || env->unprotectedAttrs)
  726. env->version = 2;
  727. env->version = 0;
  728. }
  729. BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
  730. {
  731. CMS_EncryptedContentInfo *ec;
  732. STACK_OF(CMS_RecipientInfo) *rinfos;
  733. CMS_RecipientInfo *ri;
  734. int i, ok = 0;
  735. BIO *ret;
  736. /* Get BIO first to set up key */
  737. ec = cms->d.envelopedData->encryptedContentInfo;
  738. ret = cms_EncryptedContent_init_bio(ec);
  739. /* If error or no cipher end of processing */
  740. if (!ret || !ec->cipher)
  741. return ret;
  742. /* Now encrypt content key according to each RecipientInfo type */
  743. rinfos = cms->d.envelopedData->recipientInfos;
  744. for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
  745. ri = sk_CMS_RecipientInfo_value(rinfos, i);
  746. if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) {
  747. CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
  748. CMS_R_ERROR_SETTING_RECIPIENTINFO);
  749. goto err;
  750. }
  751. }
  752. cms_env_set_version(cms->d.envelopedData);
  753. ok = 1;
  754. err:
  755. ec->cipher = NULL;
  756. OPENSSL_clear_free(ec->key, ec->keylen);
  757. ec->key = NULL;
  758. ec->keylen = 0;
  759. if (ok)
  760. return ret;
  761. BIO_free(ret);
  762. return NULL;
  763. }
  764. /*
  765. * Get RecipientInfo type (if any) supported by a key (public or private). To
  766. * retain compatibility with previous behaviour if the ctrl value isn't
  767. * supported we assume key transport.
  768. */
  769. int cms_pkey_get_ri_type(EVP_PKEY *pk)
  770. {
  771. if (pk->ameth && pk->ameth->pkey_ctrl) {
  772. int i, r;
  773. i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
  774. if (i > 0)
  775. return r;
  776. }
  777. return CMS_RECIPINFO_TRANS;
  778. }