cms_env.c 38 KB

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