2
0

cms_env.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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/aes.h>
  16. #include "cms_lcl.h"
  17. #include "internal/asn1_int.h"
  18. #include "internal/evp_int.h"
  19. /* CMS EnvelopedData Utilities */
  20. CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
  21. {
  22. if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) {
  23. CMSerr(CMS_F_CMS_GET0_ENVELOPED,
  24. CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
  25. return NULL;
  26. }
  27. return cms->d.envelopedData;
  28. }
  29. static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
  30. {
  31. if (cms->d.other == NULL) {
  32. cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
  33. if (!cms->d.envelopedData) {
  34. CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT, ERR_R_MALLOC_FAILURE);
  35. return NULL;
  36. }
  37. cms->d.envelopedData->version = 0;
  38. cms->d.envelopedData->encryptedContentInfo->contentType =
  39. OBJ_nid2obj(NID_pkcs7_data);
  40. ASN1_OBJECT_free(cms->contentType);
  41. cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
  42. return cms->d.envelopedData;
  43. }
  44. return cms_get0_enveloped(cms);
  45. }
  46. int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
  47. {
  48. EVP_PKEY *pkey;
  49. int i;
  50. if (ri->type == CMS_RECIPINFO_TRANS)
  51. pkey = ri->d.ktri->pkey;
  52. else if (ri->type == CMS_RECIPINFO_AGREE) {
  53. EVP_PKEY_CTX *pctx = ri->d.kari->pctx;
  54. if (!pctx)
  55. return 0;
  56. pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  57. if (!pkey)
  58. return 0;
  59. } else
  60. return 0;
  61. if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
  62. return 1;
  63. i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
  64. if (i == -2) {
  65. CMSerr(CMS_F_CMS_ENV_ASN1_CTRL,
  66. CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  67. return 0;
  68. }
  69. if (i <= 0) {
  70. CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE);
  71. return 0;
  72. }
  73. return 1;
  74. }
  75. STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
  76. {
  77. CMS_EnvelopedData *env;
  78. env = cms_get0_enveloped(cms);
  79. if (!env)
  80. return NULL;
  81. return env->recipientInfos;
  82. }
  83. int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
  84. {
  85. return ri->type;
  86. }
  87. EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
  88. {
  89. if (ri->type == CMS_RECIPINFO_TRANS)
  90. return ri->d.ktri->pctx;
  91. else if (ri->type == CMS_RECIPINFO_AGREE)
  92. return ri->d.kari->pctx;
  93. return NULL;
  94. }
  95. CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
  96. {
  97. CMS_ContentInfo *cms;
  98. CMS_EnvelopedData *env;
  99. cms = CMS_ContentInfo_new();
  100. if (cms == NULL)
  101. goto merr;
  102. env = cms_enveloped_data_init(cms);
  103. if (env == NULL)
  104. goto merr;
  105. if (!cms_EncryptedContent_init(env->encryptedContentInfo,
  106. cipher, NULL, 0))
  107. goto merr;
  108. return cms;
  109. merr:
  110. CMS_ContentInfo_free(cms);
  111. CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
  112. return NULL;
  113. }
  114. /* Key Transport Recipient Info (KTRI) routines */
  115. /* Initialise a ktri based on passed certificate and key */
  116. static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
  117. EVP_PKEY *pk, unsigned int flags)
  118. {
  119. CMS_KeyTransRecipientInfo *ktri;
  120. int idtype;
  121. ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
  122. if (!ri->d.ktri)
  123. return 0;
  124. ri->type = CMS_RECIPINFO_TRANS;
  125. ktri = ri->d.ktri;
  126. if (flags & CMS_USE_KEYID) {
  127. ktri->version = 2;
  128. idtype = CMS_RECIPINFO_KEYIDENTIFIER;
  129. } else {
  130. ktri->version = 0;
  131. idtype = CMS_RECIPINFO_ISSUER_SERIAL;
  132. }
  133. /*
  134. * Not a typo: RecipientIdentifier and SignerIdentifier are the same
  135. * structure.
  136. */
  137. if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype))
  138. return 0;
  139. X509_up_ref(recip);
  140. EVP_PKEY_up_ref(pk);
  141. ktri->pkey = pk;
  142. ktri->recip = recip;
  143. if (flags & CMS_KEY_PARAM) {
  144. ktri->pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
  145. if (ktri->pctx == NULL)
  146. return 0;
  147. if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
  148. return 0;
  149. } else if (!cms_env_asn1_ctrl(ri, 0))
  150. return 0;
  151. return 1;
  152. }
  153. /*
  154. * Add a recipient certificate using appropriate type of RecipientInfo
  155. */
  156. CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
  157. X509 *recip, unsigned int flags)
  158. {
  159. CMS_RecipientInfo *ri = NULL;
  160. CMS_EnvelopedData *env;
  161. EVP_PKEY *pk = NULL;
  162. env = cms_get0_enveloped(cms);
  163. if (!env)
  164. goto err;
  165. /* Initialize recipient info */
  166. ri = M_ASN1_new_of(CMS_RecipientInfo);
  167. if (!ri)
  168. goto merr;
  169. pk = X509_get0_pubkey(recip);
  170. if (!pk) {
  171. CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, CMS_R_ERROR_GETTING_PUBLIC_KEY);
  172. goto err;
  173. }
  174. switch (cms_pkey_get_ri_type(pk)) {
  175. case CMS_RECIPINFO_TRANS:
  176. if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags))
  177. goto err;
  178. break;
  179. case CMS_RECIPINFO_AGREE:
  180. if (!cms_RecipientInfo_kari_init(ri, recip, pk, flags))
  181. goto err;
  182. break;
  183. default:
  184. CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
  185. CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  186. goto err;
  187. }
  188. if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
  189. goto merr;
  190. return ri;
  191. merr:
  192. CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
  193. err:
  194. M_ASN1_free_of(ri, CMS_RecipientInfo);
  195. return NULL;
  196. }
  197. int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
  198. EVP_PKEY **pk, X509 **recip,
  199. X509_ALGOR **palg)
  200. {
  201. CMS_KeyTransRecipientInfo *ktri;
  202. if (ri->type != CMS_RECIPINFO_TRANS) {
  203. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
  204. CMS_R_NOT_KEY_TRANSPORT);
  205. return 0;
  206. }
  207. ktri = ri->d.ktri;
  208. if (pk)
  209. *pk = ktri->pkey;
  210. if (recip)
  211. *recip = ktri->recip;
  212. if (palg)
  213. *palg = ktri->keyEncryptionAlgorithm;
  214. return 1;
  215. }
  216. int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
  217. ASN1_OCTET_STRING **keyid,
  218. X509_NAME **issuer,
  219. ASN1_INTEGER **sno)
  220. {
  221. CMS_KeyTransRecipientInfo *ktri;
  222. if (ri->type != CMS_RECIPINFO_TRANS) {
  223. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
  224. CMS_R_NOT_KEY_TRANSPORT);
  225. return 0;
  226. }
  227. ktri = ri->d.ktri;
  228. return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
  229. }
  230. int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
  231. {
  232. if (ri->type != CMS_RECIPINFO_TRANS) {
  233. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
  234. CMS_R_NOT_KEY_TRANSPORT);
  235. return -2;
  236. }
  237. return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
  238. }
  239. int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
  240. {
  241. if (ri->type != CMS_RECIPINFO_TRANS) {
  242. CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT);
  243. return 0;
  244. }
  245. ri->d.ktri->pkey = pkey;
  246. return 1;
  247. }
  248. /* Encrypt content key in key transport recipient info */
  249. static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms,
  250. CMS_RecipientInfo *ri)
  251. {
  252. CMS_KeyTransRecipientInfo *ktri;
  253. CMS_EncryptedContentInfo *ec;
  254. EVP_PKEY_CTX *pctx;
  255. unsigned char *ek = NULL;
  256. size_t eklen;
  257. int ret = 0;
  258. if (ri->type != CMS_RECIPINFO_TRANS) {
  259. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_NOT_KEY_TRANSPORT);
  260. return 0;
  261. }
  262. ktri = ri->d.ktri;
  263. ec = cms->d.envelopedData->encryptedContentInfo;
  264. pctx = ktri->pctx;
  265. if (pctx) {
  266. if (!cms_env_asn1_ctrl(ri, 0))
  267. goto err;
  268. } else {
  269. pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
  270. if (pctx == NULL)
  271. return 0;
  272. if (EVP_PKEY_encrypt_init(pctx) <= 0)
  273. goto err;
  274. }
  275. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
  276. EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0) {
  277. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
  278. goto err;
  279. }
  280. if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
  281. goto err;
  282. ek = OPENSSL_malloc(eklen);
  283. if (ek == NULL) {
  284. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
  285. goto err;
  286. }
  287. if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
  288. goto err;
  289. ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
  290. ek = NULL;
  291. ret = 1;
  292. err:
  293. EVP_PKEY_CTX_free(pctx);
  294. ktri->pctx = NULL;
  295. OPENSSL_free(ek);
  296. return ret;
  297. }
  298. /* Decrypt content key from KTRI */
  299. static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
  300. CMS_RecipientInfo *ri)
  301. {
  302. CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
  303. EVP_PKEY *pkey = ktri->pkey;
  304. unsigned char *ek = NULL;
  305. size_t eklen;
  306. int ret = 0;
  307. CMS_EncryptedContentInfo *ec;
  308. ec = cms->d.envelopedData->encryptedContentInfo;
  309. if (ktri->pkey == NULL) {
  310. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_NO_PRIVATE_KEY);
  311. return 0;
  312. }
  313. ktri->pctx = EVP_PKEY_CTX_new(pkey, NULL);
  314. if (ktri->pctx == NULL)
  315. return 0;
  316. if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
  317. goto err;
  318. if (!cms_env_asn1_ctrl(ri, 1))
  319. goto err;
  320. if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
  321. EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) {
  322. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
  323. goto err;
  324. }
  325. if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
  326. ktri->encryptedKey->data,
  327. ktri->encryptedKey->length) <= 0)
  328. goto err;
  329. ek = OPENSSL_malloc(eklen);
  330. if (ek == NULL) {
  331. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, ERR_R_MALLOC_FAILURE);
  332. goto err;
  333. }
  334. if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
  335. ktri->encryptedKey->data,
  336. ktri->encryptedKey->length) <= 0) {
  337. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
  338. goto err;
  339. }
  340. ret = 1;
  341. OPENSSL_clear_free(ec->key, ec->keylen);
  342. ec->key = ek;
  343. ec->keylen = eklen;
  344. err:
  345. EVP_PKEY_CTX_free(ktri->pctx);
  346. ktri->pctx = NULL;
  347. if (!ret)
  348. OPENSSL_free(ek);
  349. return ret;
  350. }
  351. /* Key Encrypted Key (KEK) RecipientInfo routines */
  352. int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
  353. const unsigned char *id, size_t idlen)
  354. {
  355. ASN1_OCTET_STRING tmp_os;
  356. CMS_KEKRecipientInfo *kekri;
  357. if (ri->type != CMS_RECIPINFO_KEK) {
  358. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
  359. return -2;
  360. }
  361. kekri = ri->d.kekri;
  362. tmp_os.type = V_ASN1_OCTET_STRING;
  363. tmp_os.flags = 0;
  364. tmp_os.data = (unsigned char *)id;
  365. tmp_os.length = (int)idlen;
  366. return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
  367. }
  368. /* For now hard code AES key wrap info */
  369. static size_t aes_wrap_keylen(int nid)
  370. {
  371. switch (nid) {
  372. case NID_id_aes128_wrap:
  373. return 16;
  374. case NID_id_aes192_wrap:
  375. return 24;
  376. case NID_id_aes256_wrap:
  377. return 32;
  378. default:
  379. return 0;
  380. }
  381. }
  382. CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
  383. unsigned char *key, size_t keylen,
  384. unsigned char *id, size_t idlen,
  385. ASN1_GENERALIZEDTIME *date,
  386. ASN1_OBJECT *otherTypeId,
  387. ASN1_TYPE *otherType)
  388. {
  389. CMS_RecipientInfo *ri = NULL;
  390. CMS_EnvelopedData *env;
  391. CMS_KEKRecipientInfo *kekri;
  392. env = cms_get0_enveloped(cms);
  393. if (!env)
  394. goto err;
  395. if (nid == NID_undef) {
  396. switch (keylen) {
  397. case 16:
  398. nid = NID_id_aes128_wrap;
  399. break;
  400. case 24:
  401. nid = NID_id_aes192_wrap;
  402. break;
  403. case 32:
  404. nid = NID_id_aes256_wrap;
  405. break;
  406. default:
  407. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
  408. goto err;
  409. }
  410. } else {
  411. size_t exp_keylen = aes_wrap_keylen(nid);
  412. if (!exp_keylen) {
  413. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
  414. CMS_R_UNSUPPORTED_KEK_ALGORITHM);
  415. goto err;
  416. }
  417. if (keylen != exp_keylen) {
  418. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
  419. goto err;
  420. }
  421. }
  422. /* Initialize recipient info */
  423. ri = M_ASN1_new_of(CMS_RecipientInfo);
  424. if (!ri)
  425. goto merr;
  426. ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
  427. if (!ri->d.kekri)
  428. goto merr;
  429. ri->type = CMS_RECIPINFO_KEK;
  430. kekri = ri->d.kekri;
  431. if (otherTypeId) {
  432. kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
  433. if (kekri->kekid->other == NULL)
  434. goto merr;
  435. }
  436. if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
  437. goto merr;
  438. /* After this point no calls can fail */
  439. kekri->version = 4;
  440. kekri->key = key;
  441. kekri->keylen = keylen;
  442. ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
  443. kekri->kekid->date = date;
  444. if (kekri->kekid->other) {
  445. kekri->kekid->other->keyAttrId = otherTypeId;
  446. kekri->kekid->other->keyAttr = otherType;
  447. }
  448. X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
  449. OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
  450. return ri;
  451. merr:
  452. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
  453. err:
  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 == NULL) {
  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)
  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 == NULL) {
  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)
  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_RECIPIENTINFO_TYPE);
  608. return 0;
  609. }
  610. }
  611. int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  612. {
  613. switch (ri->type) {
  614. case CMS_RECIPINFO_TRANS:
  615. return cms_RecipientInfo_ktri_encrypt(cms, ri);
  616. case CMS_RECIPINFO_AGREE:
  617. return cms_RecipientInfo_kari_encrypt(cms, ri);
  618. case CMS_RECIPINFO_KEK:
  619. return cms_RecipientInfo_kekri_encrypt(cms, ri);
  620. case CMS_RECIPINFO_PASS:
  621. return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
  622. default:
  623. CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,
  624. CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
  625. return 0;
  626. }
  627. }
  628. /* Check structures and fixup version numbers (if necessary) */
  629. static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
  630. {
  631. CMS_OriginatorInfo *org = env->originatorInfo;
  632. int i;
  633. if (org == NULL)
  634. return;
  635. for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
  636. CMS_CertificateChoices *cch;
  637. cch = sk_CMS_CertificateChoices_value(org->certificates, i);
  638. if (cch->type == CMS_CERTCHOICE_OTHER) {
  639. env->version = 4;
  640. return;
  641. } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
  642. if (env->version < 3)
  643. env->version = 3;
  644. }
  645. }
  646. for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
  647. CMS_RevocationInfoChoice *rch;
  648. rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
  649. if (rch->type == CMS_REVCHOICE_OTHER) {
  650. env->version = 4;
  651. return;
  652. }
  653. }
  654. }
  655. static void cms_env_set_version(CMS_EnvelopedData *env)
  656. {
  657. int i;
  658. CMS_RecipientInfo *ri;
  659. /*
  660. * Can't set version higher than 4 so if 4 or more already nothing to do.
  661. */
  662. if (env->version >= 4)
  663. return;
  664. cms_env_set_originfo_version(env);
  665. if (env->version >= 3)
  666. return;
  667. for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
  668. ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
  669. if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
  670. env->version = 3;
  671. return;
  672. } else if (ri->type != CMS_RECIPINFO_TRANS
  673. || ri->d.ktri->version != 0) {
  674. env->version = 2;
  675. }
  676. }
  677. if (env->originatorInfo || env->unprotectedAttrs)
  678. env->version = 2;
  679. if (env->version == 2)
  680. return;
  681. env->version = 0;
  682. }
  683. BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
  684. {
  685. CMS_EncryptedContentInfo *ec;
  686. STACK_OF(CMS_RecipientInfo) *rinfos;
  687. CMS_RecipientInfo *ri;
  688. int i, ok = 0;
  689. BIO *ret;
  690. /* Get BIO first to set up key */
  691. ec = cms->d.envelopedData->encryptedContentInfo;
  692. ret = cms_EncryptedContent_init_bio(ec);
  693. /* If error or no cipher end of processing */
  694. if (!ret || !ec->cipher)
  695. return ret;
  696. /* Now encrypt content key according to each RecipientInfo type */
  697. rinfos = cms->d.envelopedData->recipientInfos;
  698. for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
  699. ri = sk_CMS_RecipientInfo_value(rinfos, i);
  700. if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) {
  701. CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
  702. CMS_R_ERROR_SETTING_RECIPIENTINFO);
  703. goto err;
  704. }
  705. }
  706. cms_env_set_version(cms->d.envelopedData);
  707. ok = 1;
  708. err:
  709. ec->cipher = NULL;
  710. OPENSSL_clear_free(ec->key, ec->keylen);
  711. ec->key = NULL;
  712. ec->keylen = 0;
  713. if (ok)
  714. return ret;
  715. BIO_free(ret);
  716. return NULL;
  717. }
  718. /*
  719. * Get RecipientInfo type (if any) supported by a key (public or private). To
  720. * retain compatibility with previous behaviour if the ctrl value isn't
  721. * supported we assume key transport.
  722. */
  723. int cms_pkey_get_ri_type(EVP_PKEY *pk)
  724. {
  725. if (pk->ameth && pk->ameth->pkey_ctrl) {
  726. int i, r;
  727. i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
  728. if (i > 0)
  729. return r;
  730. }
  731. return CMS_RECIPINFO_TRANS;
  732. }