cms_env.c 26 KB

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