cms_env.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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_local.h"
  17. #include "crypto/asn1.h"
  18. #include "crypto/evp.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 == NULL)
  55. return 0;
  56. pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  57. if (pkey == NULL)
  58. return 0;
  59. } else
  60. return 0;
  61. if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
  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 == NULL) {
  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. size_t fixlen = 0;
  309. CMS_EncryptedContentInfo *ec;
  310. ec = cms->d.envelopedData->encryptedContentInfo;
  311. if (ktri->pkey == NULL) {
  312. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_NO_PRIVATE_KEY);
  313. return 0;
  314. }
  315. if (cms->d.envelopedData->encryptedContentInfo->havenocert
  316. && !cms->d.envelopedData->encryptedContentInfo->debug) {
  317. X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
  318. const EVP_CIPHER *ciph = EVP_get_cipherbyobj(calg->algorithm);
  319. if (ciph == NULL) {
  320. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_UNKNOWN_CIPHER);
  321. return 0;
  322. }
  323. fixlen = EVP_CIPHER_key_length(ciph);
  324. }
  325. ktri->pctx = EVP_PKEY_CTX_new(pkey, NULL);
  326. if (ktri->pctx == NULL)
  327. return 0;
  328. if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
  329. goto err;
  330. if (!cms_env_asn1_ctrl(ri, 1))
  331. goto err;
  332. if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
  333. EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) {
  334. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
  335. goto err;
  336. }
  337. if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
  338. ktri->encryptedKey->data,
  339. ktri->encryptedKey->length) <= 0)
  340. goto err;
  341. ek = OPENSSL_malloc(eklen);
  342. if (ek == NULL) {
  343. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, ERR_R_MALLOC_FAILURE);
  344. goto err;
  345. }
  346. if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
  347. ktri->encryptedKey->data,
  348. ktri->encryptedKey->length) <= 0
  349. || eklen == 0
  350. || (fixlen != 0 && eklen != fixlen)) {
  351. CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
  352. goto err;
  353. }
  354. ret = 1;
  355. OPENSSL_clear_free(ec->key, ec->keylen);
  356. ec->key = ek;
  357. ec->keylen = eklen;
  358. err:
  359. EVP_PKEY_CTX_free(ktri->pctx);
  360. ktri->pctx = NULL;
  361. if (!ret)
  362. OPENSSL_free(ek);
  363. return ret;
  364. }
  365. /* Key Encrypted Key (KEK) RecipientInfo routines */
  366. int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
  367. const unsigned char *id, size_t idlen)
  368. {
  369. ASN1_OCTET_STRING tmp_os;
  370. CMS_KEKRecipientInfo *kekri;
  371. if (ri->type != CMS_RECIPINFO_KEK) {
  372. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
  373. return -2;
  374. }
  375. kekri = ri->d.kekri;
  376. tmp_os.type = V_ASN1_OCTET_STRING;
  377. tmp_os.flags = 0;
  378. tmp_os.data = (unsigned char *)id;
  379. tmp_os.length = (int)idlen;
  380. return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
  381. }
  382. /* For now hard code AES key wrap info */
  383. static size_t aes_wrap_keylen(int nid)
  384. {
  385. switch (nid) {
  386. case NID_id_aes128_wrap:
  387. return 16;
  388. case NID_id_aes192_wrap:
  389. return 24;
  390. case NID_id_aes256_wrap:
  391. return 32;
  392. default:
  393. return 0;
  394. }
  395. }
  396. CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
  397. unsigned char *key, size_t keylen,
  398. unsigned char *id, size_t idlen,
  399. ASN1_GENERALIZEDTIME *date,
  400. ASN1_OBJECT *otherTypeId,
  401. ASN1_TYPE *otherType)
  402. {
  403. CMS_RecipientInfo *ri = NULL;
  404. CMS_EnvelopedData *env;
  405. CMS_KEKRecipientInfo *kekri;
  406. env = cms_get0_enveloped(cms);
  407. if (!env)
  408. goto err;
  409. if (nid == NID_undef) {
  410. switch (keylen) {
  411. case 16:
  412. nid = NID_id_aes128_wrap;
  413. break;
  414. case 24:
  415. nid = NID_id_aes192_wrap;
  416. break;
  417. case 32:
  418. nid = NID_id_aes256_wrap;
  419. break;
  420. default:
  421. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
  422. goto err;
  423. }
  424. } else {
  425. size_t exp_keylen = aes_wrap_keylen(nid);
  426. if (!exp_keylen) {
  427. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
  428. CMS_R_UNSUPPORTED_KEK_ALGORITHM);
  429. goto err;
  430. }
  431. if (keylen != exp_keylen) {
  432. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
  433. goto err;
  434. }
  435. }
  436. /* Initialize recipient info */
  437. ri = M_ASN1_new_of(CMS_RecipientInfo);
  438. if (!ri)
  439. goto merr;
  440. ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
  441. if (!ri->d.kekri)
  442. goto merr;
  443. ri->type = CMS_RECIPINFO_KEK;
  444. kekri = ri->d.kekri;
  445. if (otherTypeId) {
  446. kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
  447. if (kekri->kekid->other == NULL)
  448. goto merr;
  449. }
  450. if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
  451. goto merr;
  452. /* After this point no calls can fail */
  453. kekri->version = 4;
  454. kekri->key = key;
  455. kekri->keylen = keylen;
  456. ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
  457. kekri->kekid->date = date;
  458. if (kekri->kekid->other) {
  459. kekri->kekid->other->keyAttrId = otherTypeId;
  460. kekri->kekid->other->keyAttr = otherType;
  461. }
  462. X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
  463. OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
  464. return ri;
  465. merr:
  466. CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
  467. err:
  468. M_ASN1_free_of(ri, CMS_RecipientInfo);
  469. return NULL;
  470. }
  471. int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
  472. X509_ALGOR **palg,
  473. ASN1_OCTET_STRING **pid,
  474. ASN1_GENERALIZEDTIME **pdate,
  475. ASN1_OBJECT **potherid,
  476. ASN1_TYPE **pothertype)
  477. {
  478. CMS_KEKIdentifier *rkid;
  479. if (ri->type != CMS_RECIPINFO_KEK) {
  480. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
  481. return 0;
  482. }
  483. rkid = ri->d.kekri->kekid;
  484. if (palg)
  485. *palg = ri->d.kekri->keyEncryptionAlgorithm;
  486. if (pid)
  487. *pid = rkid->keyIdentifier;
  488. if (pdate)
  489. *pdate = rkid->date;
  490. if (potherid) {
  491. if (rkid->other)
  492. *potherid = rkid->other->keyAttrId;
  493. else
  494. *potherid = NULL;
  495. }
  496. if (pothertype) {
  497. if (rkid->other)
  498. *pothertype = rkid->other->keyAttr;
  499. else
  500. *pothertype = NULL;
  501. }
  502. return 1;
  503. }
  504. int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
  505. unsigned char *key, size_t keylen)
  506. {
  507. CMS_KEKRecipientInfo *kekri;
  508. if (ri->type != CMS_RECIPINFO_KEK) {
  509. CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
  510. return 0;
  511. }
  512. kekri = ri->d.kekri;
  513. kekri->key = key;
  514. kekri->keylen = keylen;
  515. return 1;
  516. }
  517. /* Encrypt content key in KEK recipient info */
  518. static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
  519. CMS_RecipientInfo *ri)
  520. {
  521. CMS_EncryptedContentInfo *ec;
  522. CMS_KEKRecipientInfo *kekri;
  523. AES_KEY actx;
  524. unsigned char *wkey = NULL;
  525. int wkeylen;
  526. int r = 0;
  527. ec = cms->d.envelopedData->encryptedContentInfo;
  528. kekri = ri->d.kekri;
  529. if (!kekri->key) {
  530. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
  531. return 0;
  532. }
  533. if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
  534. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
  535. CMS_R_ERROR_SETTING_KEY);
  536. goto err;
  537. }
  538. wkey = OPENSSL_malloc(ec->keylen + 8);
  539. if (wkey == NULL) {
  540. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
  541. goto err;
  542. }
  543. wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
  544. if (wkeylen <= 0) {
  545. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
  546. goto err;
  547. }
  548. ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
  549. r = 1;
  550. err:
  551. if (!r)
  552. OPENSSL_free(wkey);
  553. OPENSSL_cleanse(&actx, sizeof(actx));
  554. return r;
  555. }
  556. /* Decrypt content key in KEK recipient info */
  557. static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
  558. CMS_RecipientInfo *ri)
  559. {
  560. CMS_EncryptedContentInfo *ec;
  561. CMS_KEKRecipientInfo *kekri;
  562. AES_KEY actx;
  563. unsigned char *ukey = NULL;
  564. int ukeylen;
  565. int r = 0, wrap_nid;
  566. ec = cms->d.envelopedData->encryptedContentInfo;
  567. kekri = ri->d.kekri;
  568. if (!kekri->key) {
  569. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
  570. return 0;
  571. }
  572. wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
  573. if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
  574. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
  575. CMS_R_INVALID_KEY_LENGTH);
  576. return 0;
  577. }
  578. /* If encrypted key length is invalid don't bother */
  579. if (kekri->encryptedKey->length < 16) {
  580. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
  581. CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
  582. goto err;
  583. }
  584. if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
  585. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
  586. CMS_R_ERROR_SETTING_KEY);
  587. goto err;
  588. }
  589. ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
  590. if (ukey == NULL) {
  591. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
  592. goto err;
  593. }
  594. ukeylen = AES_unwrap_key(&actx, NULL, ukey,
  595. kekri->encryptedKey->data,
  596. kekri->encryptedKey->length);
  597. if (ukeylen <= 0) {
  598. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR);
  599. goto err;
  600. }
  601. ec->key = ukey;
  602. ec->keylen = ukeylen;
  603. r = 1;
  604. err:
  605. if (!r)
  606. OPENSSL_free(ukey);
  607. OPENSSL_cleanse(&actx, sizeof(actx));
  608. return r;
  609. }
  610. int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  611. {
  612. switch (ri->type) {
  613. case CMS_RECIPINFO_TRANS:
  614. return cms_RecipientInfo_ktri_decrypt(cms, ri);
  615. case CMS_RECIPINFO_KEK:
  616. return cms_RecipientInfo_kekri_decrypt(cms, ri);
  617. case CMS_RECIPINFO_PASS:
  618. return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
  619. default:
  620. CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
  621. CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
  622. return 0;
  623. }
  624. }
  625. int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  626. {
  627. switch (ri->type) {
  628. case CMS_RECIPINFO_TRANS:
  629. return cms_RecipientInfo_ktri_encrypt(cms, ri);
  630. case CMS_RECIPINFO_AGREE:
  631. return cms_RecipientInfo_kari_encrypt(cms, ri);
  632. case CMS_RECIPINFO_KEK:
  633. return cms_RecipientInfo_kekri_encrypt(cms, ri);
  634. case CMS_RECIPINFO_PASS:
  635. return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
  636. default:
  637. CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,
  638. CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
  639. return 0;
  640. }
  641. }
  642. /* Check structures and fixup version numbers (if necessary) */
  643. static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
  644. {
  645. CMS_OriginatorInfo *org = env->originatorInfo;
  646. int i;
  647. if (org == NULL)
  648. return;
  649. for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
  650. CMS_CertificateChoices *cch;
  651. cch = sk_CMS_CertificateChoices_value(org->certificates, i);
  652. if (cch->type == CMS_CERTCHOICE_OTHER) {
  653. env->version = 4;
  654. return;
  655. } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
  656. if (env->version < 3)
  657. env->version = 3;
  658. }
  659. }
  660. for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
  661. CMS_RevocationInfoChoice *rch;
  662. rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
  663. if (rch->type == CMS_REVCHOICE_OTHER) {
  664. env->version = 4;
  665. return;
  666. }
  667. }
  668. }
  669. static void cms_env_set_version(CMS_EnvelopedData *env)
  670. {
  671. int i;
  672. CMS_RecipientInfo *ri;
  673. /*
  674. * Can't set version higher than 4 so if 4 or more already nothing to do.
  675. */
  676. if (env->version >= 4)
  677. return;
  678. cms_env_set_originfo_version(env);
  679. if (env->version >= 3)
  680. return;
  681. for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
  682. ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
  683. if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
  684. env->version = 3;
  685. return;
  686. } else if (ri->type != CMS_RECIPINFO_TRANS
  687. || ri->d.ktri->version != 0) {
  688. env->version = 2;
  689. }
  690. }
  691. if (env->originatorInfo || env->unprotectedAttrs)
  692. env->version = 2;
  693. if (env->version == 2)
  694. return;
  695. env->version = 0;
  696. }
  697. BIO *cms_EnvelopedData_init_bio(const CMS_ContentInfo *cms)
  698. {
  699. CMS_EncryptedContentInfo *ec;
  700. STACK_OF(CMS_RecipientInfo) *rinfos;
  701. CMS_RecipientInfo *ri;
  702. int i, ok = 0;
  703. BIO *ret;
  704. /* Get BIO first to set up key */
  705. ec = cms->d.envelopedData->encryptedContentInfo;
  706. ret = cms_EncryptedContent_init_bio(ec);
  707. /* If error or no cipher end of processing */
  708. if (!ret || !ec->cipher)
  709. return ret;
  710. /* Now encrypt content key according to each RecipientInfo type */
  711. rinfos = cms->d.envelopedData->recipientInfos;
  712. for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
  713. ri = sk_CMS_RecipientInfo_value(rinfos, i);
  714. if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) {
  715. CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
  716. CMS_R_ERROR_SETTING_RECIPIENTINFO);
  717. goto err;
  718. }
  719. }
  720. cms_env_set_version(cms->d.envelopedData);
  721. ok = 1;
  722. err:
  723. ec->cipher = NULL;
  724. OPENSSL_clear_free(ec->key, ec->keylen);
  725. ec->key = NULL;
  726. ec->keylen = 0;
  727. if (ok)
  728. return ret;
  729. BIO_free(ret);
  730. return NULL;
  731. }
  732. /*
  733. * Get RecipientInfo type (if any) supported by a key (public or private). To
  734. * retain compatibility with previous behaviour if the ctrl value isn't
  735. * supported we assume key transport.
  736. */
  737. int cms_pkey_get_ri_type(EVP_PKEY *pk)
  738. {
  739. if (pk->ameth && pk->ameth->pkey_ctrl) {
  740. int i, r;
  741. i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
  742. if (i > 0)
  743. return r;
  744. }
  745. return CMS_RECIPINFO_TRANS;
  746. }