A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

903 lines
23 KiB

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