cms_env.c 38 KB

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