2
0

cms_env.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*
  2. * Copyright 2008-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. EVP_PKEY_free(ri->d.ktri->pkey);
  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(const 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(const 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_RECIPIENTINFO_TYPE);
  609. return 0;
  610. }
  611. }
  612. int CMS_RecipientInfo_encrypt(const 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->originatorInfo || env->unprotectedAttrs)
  679. env->version = 2;
  680. if (env->version == 2)
  681. return;
  682. env->version = 0;
  683. }
  684. BIO *cms_EnvelopedData_init_bio(const 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. }