cms_env.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. /*
  2. * Copyright 2008-2023 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 "internal/sizes.h"
  17. #include "crypto/asn1.h"
  18. #include "crypto/evp.h"
  19. #include "crypto/x509.h"
  20. #include "cms_local.h"
  21. /* CMS EnvelopedData Utilities */
  22. static void cms_env_set_version(CMS_EnvelopedData *env);
  23. #define CMS_ENVELOPED_STANDARD 1
  24. #define CMS_ENVELOPED_AUTH 2
  25. static int cms_get_enveloped_type(const CMS_ContentInfo *cms)
  26. {
  27. int nid = OBJ_obj2nid(cms->contentType);
  28. switch (nid) {
  29. case NID_pkcs7_enveloped:
  30. return CMS_ENVELOPED_STANDARD;
  31. case NID_id_smime_ct_authEnvelopedData:
  32. return CMS_ENVELOPED_AUTH;
  33. default:
  34. ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
  35. return 0;
  36. }
  37. }
  38. CMS_EnvelopedData *ossl_cms_get0_enveloped(CMS_ContentInfo *cms)
  39. {
  40. if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) {
  41. ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
  42. return NULL;
  43. }
  44. return cms->d.envelopedData;
  45. }
  46. CMS_AuthEnvelopedData *ossl_cms_get0_auth_enveloped(CMS_ContentInfo *cms)
  47. {
  48. if (OBJ_obj2nid(cms->contentType) != NID_id_smime_ct_authEnvelopedData) {
  49. ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
  50. return NULL;
  51. }
  52. return cms->d.authEnvelopedData;
  53. }
  54. static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
  55. {
  56. if (cms->d.other == NULL) {
  57. cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
  58. if (cms->d.envelopedData == NULL) {
  59. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  60. return NULL;
  61. }
  62. cms->d.envelopedData->version = 0;
  63. cms->d.envelopedData->encryptedContentInfo->contentType =
  64. OBJ_nid2obj(NID_pkcs7_data);
  65. ASN1_OBJECT_free(cms->contentType);
  66. cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
  67. return cms->d.envelopedData;
  68. }
  69. return ossl_cms_get0_enveloped(cms);
  70. }
  71. static CMS_AuthEnvelopedData *
  72. cms_auth_enveloped_data_init(CMS_ContentInfo *cms)
  73. {
  74. if (cms->d.other == NULL) {
  75. cms->d.authEnvelopedData = M_ASN1_new_of(CMS_AuthEnvelopedData);
  76. if (cms->d.authEnvelopedData == NULL) {
  77. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  78. return NULL;
  79. }
  80. /* Defined in RFC 5083 - Section 2.1. "AuthEnvelopedData Type" */
  81. cms->d.authEnvelopedData->version = 0;
  82. cms->d.authEnvelopedData->authEncryptedContentInfo->contentType =
  83. OBJ_nid2obj(NID_pkcs7_data);
  84. ASN1_OBJECT_free(cms->contentType);
  85. cms->contentType = OBJ_nid2obj(NID_id_smime_ct_authEnvelopedData);
  86. return cms->d.authEnvelopedData;
  87. }
  88. return ossl_cms_get0_auth_enveloped(cms);
  89. }
  90. int ossl_cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
  91. {
  92. EVP_PKEY *pkey;
  93. int i;
  94. if (ri->type == CMS_RECIPINFO_TRANS)
  95. pkey = ri->d.ktri->pkey;
  96. else if (ri->type == CMS_RECIPINFO_AGREE) {
  97. EVP_PKEY_CTX *pctx = ri->d.kari->pctx;
  98. if (pctx == NULL)
  99. return 0;
  100. pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  101. if (pkey == NULL)
  102. return 0;
  103. } else
  104. return 0;
  105. if (EVP_PKEY_is_a(pkey, "DHX") || EVP_PKEY_is_a(pkey, "DH"))
  106. return ossl_cms_dh_envelope(ri, cmd);
  107. else if (EVP_PKEY_is_a(pkey, "EC"))
  108. return ossl_cms_ecdh_envelope(ri, cmd);
  109. else if (EVP_PKEY_is_a(pkey, "RSA"))
  110. return ossl_cms_rsa_envelope(ri, cmd);
  111. /* Something else? We'll give engines etc a chance to handle this */
  112. if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
  113. return 1;
  114. i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
  115. if (i == -2) {
  116. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  117. return 0;
  118. }
  119. if (i <= 0) {
  120. ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
  121. return 0;
  122. }
  123. return 1;
  124. }
  125. CMS_EncryptedContentInfo *ossl_cms_get0_env_enc_content(const CMS_ContentInfo *cms)
  126. {
  127. switch (cms_get_enveloped_type(cms)) {
  128. case CMS_ENVELOPED_STANDARD:
  129. return cms->d.envelopedData == NULL ? NULL
  130. : cms->d.envelopedData->encryptedContentInfo;
  131. case CMS_ENVELOPED_AUTH:
  132. return cms->d.authEnvelopedData == NULL ? NULL
  133. : cms->d.authEnvelopedData->authEncryptedContentInfo;
  134. default:
  135. return NULL;
  136. }
  137. }
  138. STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
  139. {
  140. switch (cms_get_enveloped_type(cms)) {
  141. case CMS_ENVELOPED_STANDARD:
  142. return cms->d.envelopedData->recipientInfos;
  143. case CMS_ENVELOPED_AUTH:
  144. return cms->d.authEnvelopedData->recipientInfos;
  145. default:
  146. return NULL;
  147. }
  148. }
  149. void ossl_cms_RecipientInfos_set_cmsctx(CMS_ContentInfo *cms)
  150. {
  151. int i;
  152. CMS_RecipientInfo *ri;
  153. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  154. STACK_OF(CMS_RecipientInfo) *rinfos = CMS_get0_RecipientInfos(cms);
  155. for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
  156. ri = sk_CMS_RecipientInfo_value(rinfos, i);
  157. if (ri != NULL) {
  158. switch (ri->type) {
  159. case CMS_RECIPINFO_AGREE:
  160. ri->d.kari->cms_ctx = ctx;
  161. break;
  162. case CMS_RECIPINFO_TRANS:
  163. ri->d.ktri->cms_ctx = ctx;
  164. ossl_x509_set0_libctx(ri->d.ktri->recip,
  165. ossl_cms_ctx_get0_libctx(ctx),
  166. ossl_cms_ctx_get0_propq(ctx));
  167. break;
  168. case CMS_RECIPINFO_KEK:
  169. ri->d.kekri->cms_ctx = ctx;
  170. break;
  171. case CMS_RECIPINFO_PASS:
  172. ri->d.pwri->cms_ctx = ctx;
  173. break;
  174. default:
  175. break;
  176. }
  177. }
  178. }
  179. }
  180. int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
  181. {
  182. return ri->type;
  183. }
  184. EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
  185. {
  186. if (ri->type == CMS_RECIPINFO_TRANS)
  187. return ri->d.ktri->pctx;
  188. else if (ri->type == CMS_RECIPINFO_AGREE)
  189. return ri->d.kari->pctx;
  190. return NULL;
  191. }
  192. CMS_ContentInfo *CMS_EnvelopedData_create_ex(const EVP_CIPHER *cipher,
  193. OSSL_LIB_CTX *libctx,
  194. const char *propq)
  195. {
  196. CMS_ContentInfo *cms;
  197. CMS_EnvelopedData *env;
  198. cms = CMS_ContentInfo_new_ex(libctx, propq);
  199. if (cms == NULL)
  200. goto merr;
  201. env = cms_enveloped_data_init(cms);
  202. if (env == NULL)
  203. goto merr;
  204. if (!ossl_cms_EncryptedContent_init(env->encryptedContentInfo, cipher, NULL,
  205. 0, ossl_cms_get0_cmsctx(cms)))
  206. goto merr;
  207. return cms;
  208. merr:
  209. CMS_ContentInfo_free(cms);
  210. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  211. return NULL;
  212. }
  213. CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
  214. {
  215. return CMS_EnvelopedData_create_ex(cipher, NULL, NULL);
  216. }
  217. CMS_ContentInfo *
  218. CMS_AuthEnvelopedData_create_ex(const EVP_CIPHER *cipher, OSSL_LIB_CTX *libctx,
  219. const char *propq)
  220. {
  221. CMS_ContentInfo *cms;
  222. CMS_AuthEnvelopedData *aenv;
  223. cms = CMS_ContentInfo_new_ex(libctx, propq);
  224. if (cms == NULL)
  225. goto merr;
  226. aenv = cms_auth_enveloped_data_init(cms);
  227. if (aenv == NULL)
  228. goto merr;
  229. if (!ossl_cms_EncryptedContent_init(aenv->authEncryptedContentInfo,
  230. cipher, NULL, 0,
  231. ossl_cms_get0_cmsctx(cms)))
  232. goto merr;
  233. return cms;
  234. merr:
  235. CMS_ContentInfo_free(cms);
  236. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  237. return NULL;
  238. }
  239. CMS_ContentInfo *CMS_AuthEnvelopedData_create(const EVP_CIPHER *cipher)
  240. {
  241. return CMS_AuthEnvelopedData_create_ex(cipher, NULL, NULL);
  242. }
  243. /* Key Transport Recipient Info (KTRI) routines */
  244. /* Initialise a ktri based on passed certificate and key */
  245. static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
  246. EVP_PKEY *pk, unsigned int flags,
  247. const CMS_CTX *ctx)
  248. {
  249. CMS_KeyTransRecipientInfo *ktri;
  250. int idtype;
  251. ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
  252. if (!ri->d.ktri)
  253. return 0;
  254. ri->type = CMS_RECIPINFO_TRANS;
  255. ktri = ri->d.ktri;
  256. ktri->cms_ctx = ctx;
  257. if (flags & CMS_USE_KEYID) {
  258. ktri->version = 2;
  259. idtype = CMS_RECIPINFO_KEYIDENTIFIER;
  260. } else {
  261. ktri->version = 0;
  262. idtype = CMS_RECIPINFO_ISSUER_SERIAL;
  263. }
  264. /*
  265. * Not a typo: RecipientIdentifier and SignerIdentifier are the same
  266. * structure.
  267. */
  268. if (!ossl_cms_set1_SignerIdentifier(ktri->rid, recip, idtype, ctx))
  269. return 0;
  270. X509_up_ref(recip);
  271. EVP_PKEY_up_ref(pk);
  272. ktri->pkey = pk;
  273. ktri->recip = recip;
  274. if (flags & CMS_KEY_PARAM) {
  275. ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
  276. ktri->pkey,
  277. ossl_cms_ctx_get0_propq(ctx));
  278. if (ktri->pctx == NULL)
  279. return 0;
  280. if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
  281. return 0;
  282. } else if (!ossl_cms_env_asn1_ctrl(ri, 0))
  283. return 0;
  284. return 1;
  285. }
  286. /*
  287. * Add a recipient certificate using appropriate type of RecipientInfo
  288. */
  289. CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip,
  290. EVP_PKEY *originatorPrivKey,
  291. X509 *originator, unsigned int flags)
  292. {
  293. CMS_RecipientInfo *ri = NULL;
  294. STACK_OF(CMS_RecipientInfo) *ris;
  295. EVP_PKEY *pk = NULL;
  296. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  297. ris = CMS_get0_RecipientInfos(cms);
  298. if (ris == NULL)
  299. goto err;
  300. /* Initialize recipient info */
  301. ri = M_ASN1_new_of(CMS_RecipientInfo);
  302. if (ri == NULL)
  303. goto merr;
  304. pk = X509_get0_pubkey(recip);
  305. if (pk == NULL) {
  306. ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_GETTING_PUBLIC_KEY);
  307. goto err;
  308. }
  309. switch (ossl_cms_pkey_get_ri_type(pk)) {
  310. case CMS_RECIPINFO_TRANS:
  311. if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags, ctx))
  312. goto err;
  313. break;
  314. case CMS_RECIPINFO_AGREE:
  315. if (!ossl_cms_RecipientInfo_kari_init(ri, recip, pk, originator,
  316. originatorPrivKey, flags, ctx))
  317. goto err;
  318. break;
  319. default:
  320. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  321. goto err;
  322. }
  323. if (!sk_CMS_RecipientInfo_push(ris, ri))
  324. goto merr;
  325. return ri;
  326. merr:
  327. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  328. err:
  329. M_ASN1_free_of(ri, CMS_RecipientInfo);
  330. return NULL;
  331. }
  332. CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, X509 *recip,
  333. unsigned int flags)
  334. {
  335. return CMS_add1_recipient(cms, recip, NULL, NULL, flags);
  336. }
  337. int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
  338. EVP_PKEY **pk, X509 **recip,
  339. X509_ALGOR **palg)
  340. {
  341. CMS_KeyTransRecipientInfo *ktri;
  342. if (ri->type != CMS_RECIPINFO_TRANS) {
  343. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
  344. return 0;
  345. }
  346. ktri = ri->d.ktri;
  347. if (pk)
  348. *pk = ktri->pkey;
  349. if (recip)
  350. *recip = ktri->recip;
  351. if (palg)
  352. *palg = ktri->keyEncryptionAlgorithm;
  353. return 1;
  354. }
  355. int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
  356. ASN1_OCTET_STRING **keyid,
  357. X509_NAME **issuer,
  358. ASN1_INTEGER **sno)
  359. {
  360. CMS_KeyTransRecipientInfo *ktri;
  361. if (ri->type != CMS_RECIPINFO_TRANS) {
  362. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
  363. return 0;
  364. }
  365. ktri = ri->d.ktri;
  366. return ossl_cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer,
  367. sno);
  368. }
  369. int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
  370. {
  371. if (ri->type != CMS_RECIPINFO_TRANS) {
  372. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
  373. return -2;
  374. }
  375. return ossl_cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
  376. }
  377. int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
  378. {
  379. if (ri->type != CMS_RECIPINFO_TRANS) {
  380. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
  381. return 0;
  382. }
  383. EVP_PKEY_free(ri->d.ktri->pkey);
  384. ri->d.ktri->pkey = pkey;
  385. return 1;
  386. }
  387. /* Encrypt content key in key transport recipient info */
  388. static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms,
  389. CMS_RecipientInfo *ri)
  390. {
  391. CMS_KeyTransRecipientInfo *ktri;
  392. CMS_EncryptedContentInfo *ec;
  393. EVP_PKEY_CTX *pctx;
  394. unsigned char *ek = NULL;
  395. size_t eklen;
  396. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  397. int ret = 0;
  398. if (ri->type != CMS_RECIPINFO_TRANS) {
  399. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
  400. return 0;
  401. }
  402. ktri = ri->d.ktri;
  403. ec = ossl_cms_get0_env_enc_content(cms);
  404. pctx = ktri->pctx;
  405. if (pctx) {
  406. if (!ossl_cms_env_asn1_ctrl(ri, 0))
  407. goto err;
  408. } else {
  409. pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
  410. ktri->pkey,
  411. ossl_cms_ctx_get0_propq(ctx));
  412. if (pctx == NULL)
  413. return 0;
  414. if (EVP_PKEY_encrypt_init(pctx) <= 0)
  415. goto err;
  416. }
  417. if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
  418. goto err;
  419. ek = OPENSSL_malloc(eklen);
  420. if (ek == NULL) {
  421. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  422. goto err;
  423. }
  424. if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
  425. goto err;
  426. ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
  427. ek = NULL;
  428. ret = 1;
  429. err:
  430. EVP_PKEY_CTX_free(pctx);
  431. ktri->pctx = NULL;
  432. OPENSSL_free(ek);
  433. return ret;
  434. }
  435. /* Decrypt content key from KTRI */
  436. static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
  437. CMS_RecipientInfo *ri)
  438. {
  439. CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
  440. EVP_PKEY *pkey = ktri->pkey;
  441. unsigned char *ek = NULL;
  442. size_t eklen;
  443. int ret = 0;
  444. size_t fixlen = 0;
  445. const EVP_CIPHER *cipher = NULL;
  446. EVP_CIPHER *fetched_cipher = NULL;
  447. CMS_EncryptedContentInfo *ec;
  448. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  449. OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
  450. const char *propq = ossl_cms_ctx_get0_propq(ctx);
  451. ec = ossl_cms_get0_env_enc_content(cms);
  452. if (ktri->pkey == NULL) {
  453. ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY);
  454. return 0;
  455. }
  456. if (cms->d.envelopedData->encryptedContentInfo->havenocert
  457. && !cms->d.envelopedData->encryptedContentInfo->debug) {
  458. X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
  459. char name[OSSL_MAX_NAME_SIZE];
  460. OBJ_obj2txt(name, sizeof(name), calg->algorithm, 0);
  461. (void)ERR_set_mark();
  462. fetched_cipher = EVP_CIPHER_fetch(libctx, name, propq);
  463. if (fetched_cipher != NULL)
  464. cipher = fetched_cipher;
  465. else
  466. cipher = EVP_get_cipherbyobj(calg->algorithm);
  467. if (cipher == NULL) {
  468. (void)ERR_clear_last_mark();
  469. ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
  470. return 0;
  471. }
  472. (void)ERR_pop_to_mark();
  473. fixlen = EVP_CIPHER_get_key_length(cipher);
  474. EVP_CIPHER_free(fetched_cipher);
  475. }
  476. ktri->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
  477. if (ktri->pctx == NULL)
  478. goto err;
  479. if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
  480. goto err;
  481. if (!ossl_cms_env_asn1_ctrl(ri, 1))
  482. goto err;
  483. if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
  484. ktri->encryptedKey->data,
  485. ktri->encryptedKey->length) <= 0)
  486. goto err;
  487. ek = OPENSSL_malloc(eklen);
  488. if (ek == NULL) {
  489. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  490. goto err;
  491. }
  492. if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
  493. ktri->encryptedKey->data,
  494. ktri->encryptedKey->length) <= 0
  495. || eklen == 0
  496. || (fixlen != 0 && eklen != fixlen)) {
  497. ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
  498. goto err;
  499. }
  500. ret = 1;
  501. OPENSSL_clear_free(ec->key, ec->keylen);
  502. ec->key = ek;
  503. ec->keylen = eklen;
  504. err:
  505. EVP_PKEY_CTX_free(ktri->pctx);
  506. ktri->pctx = NULL;
  507. if (!ret)
  508. OPENSSL_free(ek);
  509. return ret;
  510. }
  511. /* Key Encrypted Key (KEK) RecipientInfo routines */
  512. int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
  513. const unsigned char *id, size_t idlen)
  514. {
  515. ASN1_OCTET_STRING tmp_os;
  516. CMS_KEKRecipientInfo *kekri;
  517. if (ri->type != CMS_RECIPINFO_KEK) {
  518. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
  519. return -2;
  520. }
  521. kekri = ri->d.kekri;
  522. tmp_os.type = V_ASN1_OCTET_STRING;
  523. tmp_os.flags = 0;
  524. tmp_os.data = (unsigned char *)id;
  525. tmp_os.length = (int)idlen;
  526. return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
  527. }
  528. /* For now hard code AES key wrap info */
  529. static size_t aes_wrap_keylen(int nid)
  530. {
  531. switch (nid) {
  532. case NID_id_aes128_wrap:
  533. return 16;
  534. case NID_id_aes192_wrap:
  535. return 24;
  536. case NID_id_aes256_wrap:
  537. return 32;
  538. default:
  539. return 0;
  540. }
  541. }
  542. CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
  543. unsigned char *key, size_t keylen,
  544. unsigned char *id, size_t idlen,
  545. ASN1_GENERALIZEDTIME *date,
  546. ASN1_OBJECT *otherTypeId,
  547. ASN1_TYPE *otherType)
  548. {
  549. CMS_RecipientInfo *ri = NULL;
  550. CMS_KEKRecipientInfo *kekri;
  551. STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
  552. if (ris == NULL)
  553. goto err;
  554. if (nid == NID_undef) {
  555. switch (keylen) {
  556. case 16:
  557. nid = NID_id_aes128_wrap;
  558. break;
  559. case 24:
  560. nid = NID_id_aes192_wrap;
  561. break;
  562. case 32:
  563. nid = NID_id_aes256_wrap;
  564. break;
  565. default:
  566. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
  567. goto err;
  568. }
  569. } else {
  570. size_t exp_keylen = aes_wrap_keylen(nid);
  571. if (!exp_keylen) {
  572. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEK_ALGORITHM);
  573. goto err;
  574. }
  575. if (keylen != exp_keylen) {
  576. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
  577. goto err;
  578. }
  579. }
  580. /* Initialize recipient info */
  581. ri = M_ASN1_new_of(CMS_RecipientInfo);
  582. if (!ri)
  583. goto merr;
  584. ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
  585. if (!ri->d.kekri)
  586. goto merr;
  587. ri->type = CMS_RECIPINFO_KEK;
  588. kekri = ri->d.kekri;
  589. if (otherTypeId) {
  590. kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
  591. if (kekri->kekid->other == NULL)
  592. goto merr;
  593. }
  594. if (!sk_CMS_RecipientInfo_push(ris, ri))
  595. goto merr;
  596. /* After this point no calls can fail */
  597. kekri->version = 4;
  598. kekri->key = key;
  599. kekri->keylen = keylen;
  600. ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
  601. kekri->kekid->date = date;
  602. if (kekri->kekid->other) {
  603. kekri->kekid->other->keyAttrId = otherTypeId;
  604. kekri->kekid->other->keyAttr = otherType;
  605. }
  606. X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
  607. OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
  608. return ri;
  609. merr:
  610. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  611. err:
  612. M_ASN1_free_of(ri, CMS_RecipientInfo);
  613. return NULL;
  614. }
  615. int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
  616. X509_ALGOR **palg,
  617. ASN1_OCTET_STRING **pid,
  618. ASN1_GENERALIZEDTIME **pdate,
  619. ASN1_OBJECT **potherid,
  620. ASN1_TYPE **pothertype)
  621. {
  622. CMS_KEKIdentifier *rkid;
  623. if (ri->type != CMS_RECIPINFO_KEK) {
  624. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
  625. return 0;
  626. }
  627. rkid = ri->d.kekri->kekid;
  628. if (palg)
  629. *palg = ri->d.kekri->keyEncryptionAlgorithm;
  630. if (pid)
  631. *pid = rkid->keyIdentifier;
  632. if (pdate)
  633. *pdate = rkid->date;
  634. if (potherid) {
  635. if (rkid->other)
  636. *potherid = rkid->other->keyAttrId;
  637. else
  638. *potherid = NULL;
  639. }
  640. if (pothertype) {
  641. if (rkid->other)
  642. *pothertype = rkid->other->keyAttr;
  643. else
  644. *pothertype = NULL;
  645. }
  646. return 1;
  647. }
  648. int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
  649. unsigned char *key, size_t keylen)
  650. {
  651. CMS_KEKRecipientInfo *kekri;
  652. if (ri->type != CMS_RECIPINFO_KEK) {
  653. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
  654. return 0;
  655. }
  656. kekri = ri->d.kekri;
  657. kekri->key = key;
  658. kekri->keylen = keylen;
  659. return 1;
  660. }
  661. static EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen, const CMS_CTX *ctx)
  662. {
  663. const char *alg = NULL;
  664. switch(keylen) {
  665. case 16:
  666. alg = "AES-128-WRAP";
  667. break;
  668. case 24:
  669. alg = "AES-192-WRAP";
  670. break;
  671. case 32:
  672. alg = "AES-256-WRAP";
  673. break;
  674. default:
  675. return NULL;
  676. }
  677. return EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(ctx), alg,
  678. ossl_cms_ctx_get0_propq(ctx));
  679. }
  680. /* Encrypt content key in KEK recipient info */
  681. static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
  682. CMS_RecipientInfo *ri)
  683. {
  684. CMS_EncryptedContentInfo *ec;
  685. CMS_KEKRecipientInfo *kekri;
  686. unsigned char *wkey = NULL;
  687. int wkeylen;
  688. int r = 0;
  689. EVP_CIPHER *cipher = NULL;
  690. int outlen = 0;
  691. EVP_CIPHER_CTX *ctx = NULL;
  692. const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
  693. ec = ossl_cms_get0_env_enc_content(cms);
  694. if (ec == NULL)
  695. return 0;
  696. kekri = ri->d.kekri;
  697. if (kekri->key == NULL) {
  698. ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
  699. return 0;
  700. }
  701. cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
  702. if (cipher == NULL) {
  703. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
  704. goto err;
  705. }
  706. /* 8 byte prefix for AES wrap ciphers */
  707. wkey = OPENSSL_malloc(ec->keylen + 8);
  708. if (wkey == NULL) {
  709. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  710. goto err;
  711. }
  712. ctx = EVP_CIPHER_CTX_new();
  713. if (ctx == NULL) {
  714. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  715. goto err;
  716. }
  717. EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  718. if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
  719. || !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, ec->keylen)
  720. || !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) {
  721. ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR);
  722. goto err;
  723. }
  724. wkeylen += outlen;
  725. if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) {
  726. ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR);
  727. goto err;
  728. }
  729. ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
  730. r = 1;
  731. err:
  732. EVP_CIPHER_free(cipher);
  733. if (!r)
  734. OPENSSL_free(wkey);
  735. EVP_CIPHER_CTX_free(ctx);
  736. return r;
  737. }
  738. /* Decrypt content key in KEK recipient info */
  739. static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
  740. CMS_RecipientInfo *ri)
  741. {
  742. CMS_EncryptedContentInfo *ec;
  743. CMS_KEKRecipientInfo *kekri;
  744. unsigned char *ukey = NULL;
  745. int ukeylen;
  746. int r = 0, wrap_nid;
  747. EVP_CIPHER *cipher = NULL;
  748. int outlen = 0;
  749. EVP_CIPHER_CTX *ctx = NULL;
  750. const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
  751. ec = ossl_cms_get0_env_enc_content(cms);
  752. if (ec == NULL)
  753. return 0;
  754. kekri = ri->d.kekri;
  755. if (!kekri->key) {
  756. ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
  757. return 0;
  758. }
  759. wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
  760. if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
  761. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
  762. return 0;
  763. }
  764. /* If encrypted key length is invalid don't bother */
  765. if (kekri->encryptedKey->length < 16) {
  766. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
  767. goto err;
  768. }
  769. cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
  770. if (cipher == NULL) {
  771. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
  772. goto err;
  773. }
  774. ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
  775. if (ukey == NULL) {
  776. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  777. goto err;
  778. }
  779. ctx = EVP_CIPHER_CTX_new();
  780. if (ctx == NULL) {
  781. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  782. goto err;
  783. }
  784. if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
  785. || !EVP_DecryptUpdate(ctx, ukey, &ukeylen,
  786. kekri->encryptedKey->data,
  787. kekri->encryptedKey->length)
  788. || !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) {
  789. ERR_raise(ERR_LIB_CMS, CMS_R_UNWRAP_ERROR);
  790. goto err;
  791. }
  792. ukeylen += outlen;
  793. OPENSSL_clear_free(ec->key, ec->keylen);
  794. ec->key = ukey;
  795. ec->keylen = ukeylen;
  796. r = 1;
  797. err:
  798. EVP_CIPHER_free(cipher);
  799. if (!r)
  800. OPENSSL_free(ukey);
  801. EVP_CIPHER_CTX_free(ctx);
  802. return r;
  803. }
  804. int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  805. {
  806. switch (ri->type) {
  807. case CMS_RECIPINFO_TRANS:
  808. return cms_RecipientInfo_ktri_decrypt(cms, ri);
  809. case CMS_RECIPINFO_KEK:
  810. return cms_RecipientInfo_kekri_decrypt(cms, ri);
  811. case CMS_RECIPINFO_PASS:
  812. return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 0);
  813. default:
  814. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
  815. return 0;
  816. }
  817. }
  818. int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  819. {
  820. switch (ri->type) {
  821. case CMS_RECIPINFO_TRANS:
  822. return cms_RecipientInfo_ktri_encrypt(cms, ri);
  823. case CMS_RECIPINFO_AGREE:
  824. return ossl_cms_RecipientInfo_kari_encrypt(cms, ri);
  825. case CMS_RECIPINFO_KEK:
  826. return cms_RecipientInfo_kekri_encrypt(cms, ri);
  827. case CMS_RECIPINFO_PASS:
  828. return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 1);
  829. default:
  830. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
  831. return 0;
  832. }
  833. }
  834. /* Check structures and fixup version numbers (if necessary) */
  835. static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
  836. {
  837. CMS_OriginatorInfo *org = env->originatorInfo;
  838. int i;
  839. if (org == NULL)
  840. return;
  841. for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
  842. CMS_CertificateChoices *cch;
  843. cch = sk_CMS_CertificateChoices_value(org->certificates, i);
  844. if (cch->type == CMS_CERTCHOICE_OTHER) {
  845. env->version = 4;
  846. return;
  847. } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
  848. if (env->version < 3)
  849. env->version = 3;
  850. }
  851. }
  852. for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
  853. CMS_RevocationInfoChoice *rch;
  854. rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
  855. if (rch->type == CMS_REVCHOICE_OTHER) {
  856. env->version = 4;
  857. return;
  858. }
  859. }
  860. }
  861. static void cms_env_set_version(CMS_EnvelopedData *env)
  862. {
  863. int i;
  864. CMS_RecipientInfo *ri;
  865. /*
  866. * Can't set version higher than 4 so if 4 or more already nothing to do.
  867. */
  868. if (env->version >= 4)
  869. return;
  870. cms_env_set_originfo_version(env);
  871. if (env->version >= 3)
  872. return;
  873. for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
  874. ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
  875. if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
  876. env->version = 3;
  877. return;
  878. } else if (ri->type != CMS_RECIPINFO_TRANS
  879. || ri->d.ktri->version != 0) {
  880. env->version = 2;
  881. }
  882. }
  883. if (env->originatorInfo || env->unprotectedAttrs)
  884. env->version = 2;
  885. if (env->version == 2)
  886. return;
  887. env->version = 0;
  888. }
  889. static int cms_env_encrypt_content_key(const CMS_ContentInfo *cms,
  890. STACK_OF(CMS_RecipientInfo) *ris)
  891. {
  892. int i;
  893. CMS_RecipientInfo *ri;
  894. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  895. ri = sk_CMS_RecipientInfo_value(ris, i);
  896. if (CMS_RecipientInfo_encrypt(cms, ri) <= 0)
  897. return -1;
  898. }
  899. return 1;
  900. }
  901. static void cms_env_clear_ec(CMS_EncryptedContentInfo *ec)
  902. {
  903. ec->cipher = NULL;
  904. OPENSSL_clear_free(ec->key, ec->keylen);
  905. ec->key = NULL;
  906. ec->keylen = 0;
  907. }
  908. static BIO *cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo *cms)
  909. {
  910. CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo;
  911. BIO *contentBio = ossl_cms_EncryptedContent_init_bio(ec,
  912. ossl_cms_get0_cmsctx(cms));
  913. EVP_CIPHER_CTX *ctx = NULL;
  914. if (contentBio == NULL)
  915. return NULL;
  916. BIO_get_cipher_ctx(contentBio, &ctx);
  917. if (ctx == NULL) {
  918. BIO_free(contentBio);
  919. return NULL;
  920. }
  921. /*
  922. * If the selected cipher supports unprotected attributes,
  923. * deal with it using special ctrl function
  924. */
  925. if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
  926. & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0
  927. && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, 0,
  928. cms->d.envelopedData->unprotectedAttrs) <= 0) {
  929. BIO_free(contentBio);
  930. return NULL;
  931. }
  932. return contentBio;
  933. }
  934. static BIO *cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo *cms)
  935. {
  936. CMS_EncryptedContentInfo *ec;
  937. STACK_OF(CMS_RecipientInfo) *rinfos;
  938. int ok = 0;
  939. BIO *ret;
  940. CMS_EnvelopedData *env = cms->d.envelopedData;
  941. /* Get BIO first to set up key */
  942. ec = env->encryptedContentInfo;
  943. ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms));
  944. /* If error end of processing */
  945. if (!ret)
  946. return ret;
  947. /* Now encrypt content key according to each RecipientInfo type */
  948. rinfos = env->recipientInfos;
  949. if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
  950. ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO);
  951. goto err;
  952. }
  953. /* And finally set the version */
  954. cms_env_set_version(env);
  955. ok = 1;
  956. err:
  957. cms_env_clear_ec(ec);
  958. if (ok)
  959. return ret;
  960. BIO_free(ret);
  961. return NULL;
  962. }
  963. BIO *ossl_cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
  964. {
  965. if (cms->d.envelopedData->encryptedContentInfo->cipher != NULL) {
  966. /* If cipher is set it's encryption */
  967. return cms_EnvelopedData_Encryption_init_bio(cms);
  968. }
  969. /* If cipher is not set it's decryption */
  970. return cms_EnvelopedData_Decryption_init_bio(cms);
  971. }
  972. BIO *ossl_cms_AuthEnvelopedData_init_bio(CMS_ContentInfo *cms)
  973. {
  974. CMS_EncryptedContentInfo *ec;
  975. STACK_OF(CMS_RecipientInfo) *rinfos;
  976. int ok = 0;
  977. BIO *ret;
  978. CMS_AuthEnvelopedData *aenv = cms->d.authEnvelopedData;
  979. /* Get BIO first to set up key */
  980. ec = aenv->authEncryptedContentInfo;
  981. /* Set tag for decryption */
  982. if (ec->cipher == NULL) {
  983. ec->tag = aenv->mac->data;
  984. ec->taglen = aenv->mac->length;
  985. }
  986. ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms));
  987. /* If error or no cipher end of processing */
  988. if (ret == NULL || ec->cipher == NULL)
  989. return ret;
  990. /* Now encrypt content key according to each RecipientInfo type */
  991. rinfos = aenv->recipientInfos;
  992. if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
  993. ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO);
  994. goto err;
  995. }
  996. /* And finally set the version */
  997. aenv->version = 0;
  998. ok = 1;
  999. err:
  1000. cms_env_clear_ec(ec);
  1001. if (ok)
  1002. return ret;
  1003. BIO_free(ret);
  1004. return NULL;
  1005. }
  1006. int ossl_cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain)
  1007. {
  1008. CMS_EnvelopedData *env = NULL;
  1009. EVP_CIPHER_CTX *ctx = NULL;
  1010. BIO *mbio = BIO_find_type(chain, BIO_TYPE_CIPHER);
  1011. env = ossl_cms_get0_enveloped(cms);
  1012. if (env == NULL)
  1013. return 0;
  1014. if (mbio == NULL) {
  1015. ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND);
  1016. return 0;
  1017. }
  1018. BIO_get_cipher_ctx(mbio, &ctx);
  1019. /*
  1020. * If the selected cipher supports unprotected attributes,
  1021. * deal with it using special ctrl function
  1022. */
  1023. if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
  1024. & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0) {
  1025. if (env->unprotectedAttrs == NULL)
  1026. env->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null();
  1027. if (env->unprotectedAttrs == NULL) {
  1028. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  1029. return 0;
  1030. }
  1031. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED,
  1032. 1, env->unprotectedAttrs) <= 0) {
  1033. ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
  1034. return 0;
  1035. }
  1036. }
  1037. cms_env_set_version(cms->d.envelopedData);
  1038. return 1;
  1039. }
  1040. int ossl_cms_AuthEnvelopedData_final(CMS_ContentInfo *cms, BIO *cmsbio)
  1041. {
  1042. EVP_CIPHER_CTX *ctx;
  1043. unsigned char *tag = NULL;
  1044. int taglen, ok = 0;
  1045. BIO_get_cipher_ctx(cmsbio, &ctx);
  1046. /*
  1047. * The tag is set only for encryption. There is nothing to do for
  1048. * decryption.
  1049. */
  1050. if (!EVP_CIPHER_CTX_is_encrypting(ctx))
  1051. return 1;
  1052. taglen = EVP_CIPHER_CTX_get_tag_length(ctx);
  1053. if (taglen <= 0
  1054. || (tag = OPENSSL_malloc(taglen)) == NULL
  1055. || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
  1056. tag) <= 0) {
  1057. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_GET_TAG);
  1058. goto err;
  1059. }
  1060. if (!ASN1_OCTET_STRING_set(cms->d.authEnvelopedData->mac, tag, taglen))
  1061. goto err;
  1062. ok = 1;
  1063. err:
  1064. OPENSSL_free(tag);
  1065. return ok;
  1066. }
  1067. /*
  1068. * Get RecipientInfo type (if any) supported by a key (public or private). To
  1069. * retain compatibility with previous behaviour if the ctrl value isn't
  1070. * supported we assume key transport.
  1071. */
  1072. int ossl_cms_pkey_get_ri_type(EVP_PKEY *pk)
  1073. {
  1074. /* Check types that we know about */
  1075. if (EVP_PKEY_is_a(pk, "DH"))
  1076. return CMS_RECIPINFO_AGREE;
  1077. else if (EVP_PKEY_is_a(pk, "DHX"))
  1078. return CMS_RECIPINFO_AGREE;
  1079. else if (EVP_PKEY_is_a(pk, "DSA"))
  1080. return CMS_RECIPINFO_NONE;
  1081. else if (EVP_PKEY_is_a(pk, "EC"))
  1082. return CMS_RECIPINFO_AGREE;
  1083. else if (EVP_PKEY_is_a(pk, "RSA"))
  1084. return CMS_RECIPINFO_TRANS;
  1085. /*
  1086. * Otherwise this might ben an engine implementation, so see if we can get
  1087. * the type from the ameth.
  1088. */
  1089. if (pk->ameth && pk->ameth->pkey_ctrl) {
  1090. int i, r;
  1091. i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
  1092. if (i > 0)
  1093. return r;
  1094. }
  1095. return CMS_RECIPINFO_TRANS;
  1096. }
  1097. int ossl_cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type)
  1098. {
  1099. int supportedRiType;
  1100. if (pk->ameth != NULL && pk->ameth->pkey_ctrl != NULL) {
  1101. int i, r;
  1102. i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED,
  1103. ri_type, &r);
  1104. if (i > 0)
  1105. return r;
  1106. }
  1107. supportedRiType = ossl_cms_pkey_get_ri_type(pk);
  1108. if (supportedRiType < 0)
  1109. return 0;
  1110. return (supportedRiType == ri_type);
  1111. }