cms_env.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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/evp.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. static const EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen)
  518. {
  519. switch(keylen) {
  520. case 16:
  521. return EVP_aes_128_wrap();
  522. case 24:
  523. return EVP_aes_192_wrap();
  524. case 32:
  525. return EVP_aes_256_wrap();
  526. }
  527. return NULL;
  528. }
  529. /* Encrypt content key in KEK recipient info */
  530. static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
  531. CMS_RecipientInfo *ri)
  532. {
  533. CMS_EncryptedContentInfo *ec;
  534. CMS_KEKRecipientInfo *kekri;
  535. unsigned char *wkey = NULL;
  536. int wkeylen;
  537. int r = 0;
  538. const EVP_CIPHER *cipher = NULL;
  539. int outlen = 0;
  540. EVP_CIPHER_CTX *ctx = NULL;
  541. ec = cms->d.envelopedData->encryptedContentInfo;
  542. kekri = ri->d.kekri;
  543. if (kekri->key == NULL) {
  544. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
  545. return 0;
  546. }
  547. cipher = cms_get_key_wrap_cipher(kekri->keylen);
  548. if (cipher == NULL) {
  549. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_INVALID_KEY_LENGTH);
  550. goto err;
  551. }
  552. /* 8 byte prefix for AES wrap ciphers */
  553. wkey = OPENSSL_malloc(ec->keylen + 8);
  554. if (wkey == NULL) {
  555. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
  556. goto err;
  557. }
  558. ctx = EVP_CIPHER_CTX_new();
  559. if (ctx == NULL) {
  560. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
  561. goto err;
  562. }
  563. EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  564. if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
  565. || !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, ec->keylen)
  566. || !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) {
  567. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
  568. goto err;
  569. }
  570. wkeylen += outlen;
  571. if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) {
  572. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
  573. goto err;
  574. }
  575. ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
  576. r = 1;
  577. err:
  578. if (!r)
  579. OPENSSL_free(wkey);
  580. EVP_CIPHER_CTX_free(ctx);
  581. return r;
  582. }
  583. /* Decrypt content key in KEK recipient info */
  584. static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
  585. CMS_RecipientInfo *ri)
  586. {
  587. CMS_EncryptedContentInfo *ec;
  588. CMS_KEKRecipientInfo *kekri;
  589. unsigned char *ukey = NULL;
  590. int ukeylen;
  591. int r = 0, wrap_nid;
  592. const EVP_CIPHER *cipher = NULL;
  593. int outlen = 0;
  594. EVP_CIPHER_CTX *ctx = NULL;
  595. ec = cms->d.envelopedData->encryptedContentInfo;
  596. kekri = ri->d.kekri;
  597. if (!kekri->key) {
  598. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
  599. return 0;
  600. }
  601. wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
  602. if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
  603. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
  604. CMS_R_INVALID_KEY_LENGTH);
  605. return 0;
  606. }
  607. /* If encrypted key length is invalid don't bother */
  608. if (kekri->encryptedKey->length < 16) {
  609. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
  610. CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
  611. goto err;
  612. }
  613. cipher = cms_get_key_wrap_cipher(kekri->keylen);
  614. if (cipher == NULL) {
  615. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_INVALID_KEY_LENGTH);
  616. goto err;
  617. }
  618. ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
  619. if (ukey == NULL) {
  620. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
  621. goto err;
  622. }
  623. ctx = EVP_CIPHER_CTX_new();
  624. if (ctx == NULL) {
  625. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
  626. goto err;
  627. }
  628. if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
  629. || !EVP_DecryptUpdate(ctx, ukey, &ukeylen,
  630. kekri->encryptedKey->data,
  631. kekri->encryptedKey->length)
  632. || !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) {
  633. CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR);
  634. goto err;
  635. }
  636. ukeylen += outlen;
  637. ec->key = ukey;
  638. ec->keylen = ukeylen;
  639. r = 1;
  640. err:
  641. if (!r)
  642. OPENSSL_free(ukey);
  643. EVP_CIPHER_CTX_free(ctx);
  644. return r;
  645. }
  646. int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  647. {
  648. switch (ri->type) {
  649. case CMS_RECIPINFO_TRANS:
  650. return cms_RecipientInfo_ktri_decrypt(cms, ri);
  651. case CMS_RECIPINFO_KEK:
  652. return cms_RecipientInfo_kekri_decrypt(cms, ri);
  653. case CMS_RECIPINFO_PASS:
  654. return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
  655. default:
  656. CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
  657. CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
  658. return 0;
  659. }
  660. }
  661. int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  662. {
  663. switch (ri->type) {
  664. case CMS_RECIPINFO_TRANS:
  665. return cms_RecipientInfo_ktri_encrypt(cms, ri);
  666. case CMS_RECIPINFO_AGREE:
  667. return cms_RecipientInfo_kari_encrypt(cms, ri);
  668. case CMS_RECIPINFO_KEK:
  669. return cms_RecipientInfo_kekri_encrypt(cms, ri);
  670. case CMS_RECIPINFO_PASS:
  671. return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
  672. default:
  673. CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,
  674. CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
  675. return 0;
  676. }
  677. }
  678. /* Check structures and fixup version numbers (if necessary) */
  679. static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
  680. {
  681. CMS_OriginatorInfo *org = env->originatorInfo;
  682. int i;
  683. if (org == NULL)
  684. return;
  685. for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
  686. CMS_CertificateChoices *cch;
  687. cch = sk_CMS_CertificateChoices_value(org->certificates, i);
  688. if (cch->type == CMS_CERTCHOICE_OTHER) {
  689. env->version = 4;
  690. return;
  691. } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
  692. if (env->version < 3)
  693. env->version = 3;
  694. }
  695. }
  696. for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
  697. CMS_RevocationInfoChoice *rch;
  698. rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
  699. if (rch->type == CMS_REVCHOICE_OTHER) {
  700. env->version = 4;
  701. return;
  702. }
  703. }
  704. }
  705. static void cms_env_set_version(CMS_EnvelopedData *env)
  706. {
  707. int i;
  708. CMS_RecipientInfo *ri;
  709. /*
  710. * Can't set version higher than 4 so if 4 or more already nothing to do.
  711. */
  712. if (env->version >= 4)
  713. return;
  714. cms_env_set_originfo_version(env);
  715. if (env->version >= 3)
  716. return;
  717. for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
  718. ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
  719. if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
  720. env->version = 3;
  721. return;
  722. } else if (ri->type != CMS_RECIPINFO_TRANS
  723. || ri->d.ktri->version != 0) {
  724. env->version = 2;
  725. }
  726. }
  727. if (env->originatorInfo || env->unprotectedAttrs)
  728. env->version = 2;
  729. if (env->version == 2)
  730. return;
  731. env->version = 0;
  732. }
  733. BIO *cms_EnvelopedData_init_bio(const CMS_ContentInfo *cms)
  734. {
  735. CMS_EncryptedContentInfo *ec;
  736. STACK_OF(CMS_RecipientInfo) *rinfos;
  737. CMS_RecipientInfo *ri;
  738. int i, ok = 0;
  739. BIO *ret;
  740. /* Get BIO first to set up key */
  741. ec = cms->d.envelopedData->encryptedContentInfo;
  742. ret = cms_EncryptedContent_init_bio(ec);
  743. /* If error or no cipher end of processing */
  744. if (!ret || !ec->cipher)
  745. return ret;
  746. /* Now encrypt content key according to each RecipientInfo type */
  747. rinfos = cms->d.envelopedData->recipientInfos;
  748. for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
  749. ri = sk_CMS_RecipientInfo_value(rinfos, i);
  750. if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) {
  751. CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
  752. CMS_R_ERROR_SETTING_RECIPIENTINFO);
  753. goto err;
  754. }
  755. }
  756. cms_env_set_version(cms->d.envelopedData);
  757. ok = 1;
  758. err:
  759. ec->cipher = NULL;
  760. OPENSSL_clear_free(ec->key, ec->keylen);
  761. ec->key = NULL;
  762. ec->keylen = 0;
  763. if (ok)
  764. return ret;
  765. BIO_free(ret);
  766. return NULL;
  767. }
  768. /*
  769. * Get RecipientInfo type (if any) supported by a key (public or private). To
  770. * retain compatibility with previous behaviour if the ctrl value isn't
  771. * supported we assume key transport.
  772. */
  773. int cms_pkey_get_ri_type(EVP_PKEY *pk)
  774. {
  775. if (pk->ameth && pk->ameth->pkey_ctrl) {
  776. int i, r;
  777. i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
  778. if (i > 0)
  779. return r;
  780. }
  781. return CMS_RECIPINFO_TRANS;
  782. }