cms_env.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. /*
  2. * Copyright 2008-2024 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. X509_up_ref(recip);
  313. EVP_PKEY_up_ref(pk);
  314. ktri->pkey = pk;
  315. ktri->recip = recip;
  316. if (flags & CMS_KEY_PARAM) {
  317. ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
  318. ktri->pkey,
  319. ossl_cms_ctx_get0_propq(ctx));
  320. if (ktri->pctx == NULL)
  321. return 0;
  322. if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
  323. return 0;
  324. } else if (!ossl_cms_env_asn1_ctrl(ri, 0))
  325. return 0;
  326. return 1;
  327. }
  328. /*
  329. * Add a recipient certificate using appropriate type of RecipientInfo
  330. */
  331. CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip,
  332. EVP_PKEY *originatorPrivKey,
  333. X509 *originator, unsigned int flags)
  334. {
  335. CMS_RecipientInfo *ri = NULL;
  336. STACK_OF(CMS_RecipientInfo) *ris;
  337. EVP_PKEY *pk = NULL;
  338. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  339. ris = CMS_get0_RecipientInfos(cms);
  340. if (ris == NULL)
  341. goto err;
  342. /* Initialize recipient info */
  343. ri = M_ASN1_new_of(CMS_RecipientInfo);
  344. if (ri == NULL) {
  345. ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
  346. goto err;
  347. }
  348. pk = X509_get0_pubkey(recip);
  349. if (pk == NULL) {
  350. ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_GETTING_PUBLIC_KEY);
  351. goto err;
  352. }
  353. switch (ossl_cms_pkey_get_ri_type(pk)) {
  354. case CMS_RECIPINFO_TRANS:
  355. if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags, ctx))
  356. goto err;
  357. break;
  358. case CMS_RECIPINFO_AGREE:
  359. if (!ossl_cms_RecipientInfo_kari_init(ri, recip, pk, originator,
  360. originatorPrivKey, flags, ctx))
  361. goto err;
  362. break;
  363. default:
  364. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  365. goto err;
  366. }
  367. if (!sk_CMS_RecipientInfo_push(ris, ri)) {
  368. ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
  369. goto err;
  370. }
  371. return ri;
  372. err:
  373. M_ASN1_free_of(ri, CMS_RecipientInfo);
  374. return NULL;
  375. }
  376. CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, X509 *recip,
  377. unsigned int flags)
  378. {
  379. return CMS_add1_recipient(cms, recip, NULL, NULL, flags);
  380. }
  381. int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
  382. EVP_PKEY **pk, X509 **recip,
  383. X509_ALGOR **palg)
  384. {
  385. CMS_KeyTransRecipientInfo *ktri;
  386. if (ri->type != CMS_RECIPINFO_TRANS) {
  387. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
  388. return 0;
  389. }
  390. ktri = ri->d.ktri;
  391. if (pk)
  392. *pk = ktri->pkey;
  393. if (recip)
  394. *recip = ktri->recip;
  395. if (palg)
  396. *palg = ktri->keyEncryptionAlgorithm;
  397. return 1;
  398. }
  399. int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
  400. ASN1_OCTET_STRING **keyid,
  401. X509_NAME **issuer,
  402. ASN1_INTEGER **sno)
  403. {
  404. CMS_KeyTransRecipientInfo *ktri;
  405. if (ri->type != CMS_RECIPINFO_TRANS) {
  406. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
  407. return 0;
  408. }
  409. ktri = ri->d.ktri;
  410. return ossl_cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer,
  411. sno);
  412. }
  413. int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
  414. {
  415. if (ri->type != CMS_RECIPINFO_TRANS) {
  416. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
  417. return -2;
  418. }
  419. return ossl_cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
  420. }
  421. int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
  422. {
  423. if (ri->type != CMS_RECIPINFO_TRANS) {
  424. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
  425. return 0;
  426. }
  427. EVP_PKEY_free(ri->d.ktri->pkey);
  428. ri->d.ktri->pkey = pkey;
  429. return 1;
  430. }
  431. /* Encrypt content key in key transport recipient info */
  432. static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms,
  433. CMS_RecipientInfo *ri)
  434. {
  435. CMS_KeyTransRecipientInfo *ktri;
  436. CMS_EncryptedContentInfo *ec;
  437. EVP_PKEY_CTX *pctx;
  438. unsigned char *ek = NULL;
  439. size_t eklen;
  440. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  441. int ret = 0;
  442. if (ri->type != CMS_RECIPINFO_TRANS) {
  443. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
  444. return 0;
  445. }
  446. ktri = ri->d.ktri;
  447. ec = ossl_cms_get0_env_enc_content(cms);
  448. pctx = ktri->pctx;
  449. if (pctx) {
  450. if (!ossl_cms_env_asn1_ctrl(ri, 0))
  451. goto err;
  452. } else {
  453. pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
  454. ktri->pkey,
  455. ossl_cms_ctx_get0_propq(ctx));
  456. if (pctx == NULL)
  457. return 0;
  458. if (EVP_PKEY_encrypt_init(pctx) <= 0)
  459. goto err;
  460. }
  461. if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
  462. goto err;
  463. ek = OPENSSL_malloc(eklen);
  464. if (ek == NULL)
  465. goto err;
  466. if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
  467. goto err;
  468. ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
  469. ek = NULL;
  470. ret = 1;
  471. err:
  472. EVP_PKEY_CTX_free(pctx);
  473. ktri->pctx = NULL;
  474. OPENSSL_free(ek);
  475. return ret;
  476. }
  477. /* Decrypt content key from KTRI */
  478. static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
  479. CMS_RecipientInfo *ri)
  480. {
  481. CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
  482. EVP_PKEY *pkey = ktri->pkey;
  483. unsigned char *ek = NULL;
  484. size_t eklen;
  485. int ret = 0;
  486. size_t fixlen = 0;
  487. const EVP_CIPHER *cipher = NULL;
  488. EVP_CIPHER *fetched_cipher = NULL;
  489. CMS_EncryptedContentInfo *ec;
  490. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  491. OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
  492. const char *propq = ossl_cms_ctx_get0_propq(ctx);
  493. ec = ossl_cms_get0_env_enc_content(cms);
  494. if (ktri->pkey == NULL) {
  495. ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY);
  496. return 0;
  497. }
  498. if (cms->d.envelopedData->encryptedContentInfo->havenocert
  499. && !cms->d.envelopedData->encryptedContentInfo->debug) {
  500. X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
  501. char name[OSSL_MAX_NAME_SIZE];
  502. OBJ_obj2txt(name, sizeof(name), calg->algorithm, 0);
  503. (void)ERR_set_mark();
  504. fetched_cipher = EVP_CIPHER_fetch(libctx, name, propq);
  505. if (fetched_cipher != NULL)
  506. cipher = fetched_cipher;
  507. else
  508. cipher = EVP_get_cipherbyobj(calg->algorithm);
  509. if (cipher == NULL) {
  510. (void)ERR_clear_last_mark();
  511. ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
  512. return 0;
  513. }
  514. (void)ERR_pop_to_mark();
  515. fixlen = EVP_CIPHER_get_key_length(cipher);
  516. EVP_CIPHER_free(fetched_cipher);
  517. }
  518. ktri->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
  519. if (ktri->pctx == NULL)
  520. goto err;
  521. if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
  522. goto err;
  523. if (!ossl_cms_env_asn1_ctrl(ri, 1))
  524. goto err;
  525. if (EVP_PKEY_is_a(pkey, "RSA"))
  526. /* upper layer CMS code incorrectly assumes that a successful RSA
  527. * decryption means that the key matches ciphertext (which never
  528. * was the case, implicit rejection or not), so to make it work
  529. * disable implicit rejection for RSA keys */
  530. EVP_PKEY_CTX_ctrl_str(ktri->pctx, "rsa_pkcs1_implicit_rejection", "0");
  531. if (evp_pkey_decrypt_alloc(ktri->pctx, &ek, &eklen, fixlen,
  532. ktri->encryptedKey->data,
  533. ktri->encryptedKey->length) <= 0)
  534. goto err;
  535. ret = 1;
  536. OPENSSL_clear_free(ec->key, ec->keylen);
  537. ec->key = ek;
  538. ec->keylen = eklen;
  539. err:
  540. EVP_PKEY_CTX_free(ktri->pctx);
  541. ktri->pctx = NULL;
  542. if (!ret)
  543. OPENSSL_free(ek);
  544. return ret;
  545. }
  546. /* Key Encrypted Key (KEK) RecipientInfo routines */
  547. int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
  548. const unsigned char *id, size_t idlen)
  549. {
  550. ASN1_OCTET_STRING tmp_os;
  551. CMS_KEKRecipientInfo *kekri;
  552. if (ri->type != CMS_RECIPINFO_KEK) {
  553. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
  554. return -2;
  555. }
  556. kekri = ri->d.kekri;
  557. tmp_os.type = V_ASN1_OCTET_STRING;
  558. tmp_os.flags = 0;
  559. tmp_os.data = (unsigned char *)id;
  560. tmp_os.length = (int)idlen;
  561. return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
  562. }
  563. /* For now hard code AES key wrap info */
  564. static size_t aes_wrap_keylen(int nid)
  565. {
  566. switch (nid) {
  567. case NID_id_aes128_wrap:
  568. return 16;
  569. case NID_id_aes192_wrap:
  570. return 24;
  571. case NID_id_aes256_wrap:
  572. return 32;
  573. default:
  574. return 0;
  575. }
  576. }
  577. CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
  578. unsigned char *key, size_t keylen,
  579. unsigned char *id, size_t idlen,
  580. ASN1_GENERALIZEDTIME *date,
  581. ASN1_OBJECT *otherTypeId,
  582. ASN1_TYPE *otherType)
  583. {
  584. CMS_RecipientInfo *ri = NULL;
  585. CMS_KEKRecipientInfo *kekri;
  586. STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
  587. if (ris == NULL)
  588. goto err;
  589. if (nid == NID_undef) {
  590. switch (keylen) {
  591. case 16:
  592. nid = NID_id_aes128_wrap;
  593. break;
  594. case 24:
  595. nid = NID_id_aes192_wrap;
  596. break;
  597. case 32:
  598. nid = NID_id_aes256_wrap;
  599. break;
  600. default:
  601. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
  602. goto err;
  603. }
  604. } else {
  605. size_t exp_keylen = aes_wrap_keylen(nid);
  606. if (!exp_keylen) {
  607. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEK_ALGORITHM);
  608. goto err;
  609. }
  610. if (keylen != exp_keylen) {
  611. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
  612. goto err;
  613. }
  614. }
  615. /* Initialize recipient info */
  616. ri = M_ASN1_new_of(CMS_RecipientInfo);
  617. if (!ri) {
  618. ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
  619. goto err;
  620. }
  621. ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
  622. if (!ri->d.kekri) {
  623. ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
  624. goto err;
  625. }
  626. ri->type = CMS_RECIPINFO_KEK;
  627. kekri = ri->d.kekri;
  628. if (otherTypeId) {
  629. kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
  630. if (kekri->kekid->other == NULL) {
  631. ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
  632. goto err;
  633. }
  634. }
  635. if (!sk_CMS_RecipientInfo_push(ris, ri)) {
  636. ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
  637. goto err;
  638. }
  639. /* After this point no calls can fail */
  640. kekri->version = 4;
  641. kekri->key = key;
  642. kekri->keylen = keylen;
  643. ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
  644. kekri->kekid->date = date;
  645. if (kekri->kekid->other) {
  646. kekri->kekid->other->keyAttrId = otherTypeId;
  647. kekri->kekid->other->keyAttr = otherType;
  648. }
  649. (void)X509_ALGOR_set0(kekri->keyEncryptionAlgorithm, OBJ_nid2obj(nid),
  650. V_ASN1_UNDEF, NULL); /* cannot fail */
  651. return ri;
  652. err:
  653. M_ASN1_free_of(ri, CMS_RecipientInfo);
  654. return NULL;
  655. }
  656. int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
  657. X509_ALGOR **palg,
  658. ASN1_OCTET_STRING **pid,
  659. ASN1_GENERALIZEDTIME **pdate,
  660. ASN1_OBJECT **potherid,
  661. ASN1_TYPE **pothertype)
  662. {
  663. CMS_KEKIdentifier *rkid;
  664. if (ri->type != CMS_RECIPINFO_KEK) {
  665. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
  666. return 0;
  667. }
  668. rkid = ri->d.kekri->kekid;
  669. if (palg)
  670. *palg = ri->d.kekri->keyEncryptionAlgorithm;
  671. if (pid)
  672. *pid = rkid->keyIdentifier;
  673. if (pdate)
  674. *pdate = rkid->date;
  675. if (potherid) {
  676. if (rkid->other)
  677. *potherid = rkid->other->keyAttrId;
  678. else
  679. *potherid = NULL;
  680. }
  681. if (pothertype) {
  682. if (rkid->other)
  683. *pothertype = rkid->other->keyAttr;
  684. else
  685. *pothertype = NULL;
  686. }
  687. return 1;
  688. }
  689. int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
  690. unsigned char *key, size_t keylen)
  691. {
  692. CMS_KEKRecipientInfo *kekri;
  693. if (ri->type != CMS_RECIPINFO_KEK) {
  694. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
  695. return 0;
  696. }
  697. kekri = ri->d.kekri;
  698. kekri->key = key;
  699. kekri->keylen = keylen;
  700. return 1;
  701. }
  702. static EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen, const CMS_CTX *ctx)
  703. {
  704. const char *alg = NULL;
  705. switch (keylen) {
  706. case 16:
  707. alg = "AES-128-WRAP";
  708. break;
  709. case 24:
  710. alg = "AES-192-WRAP";
  711. break;
  712. case 32:
  713. alg = "AES-256-WRAP";
  714. break;
  715. default:
  716. return NULL;
  717. }
  718. return EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(ctx), alg,
  719. ossl_cms_ctx_get0_propq(ctx));
  720. }
  721. /* Encrypt content key in KEK recipient info */
  722. static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
  723. CMS_RecipientInfo *ri)
  724. {
  725. CMS_EncryptedContentInfo *ec;
  726. CMS_KEKRecipientInfo *kekri;
  727. unsigned char *wkey = NULL;
  728. int wkeylen;
  729. int r = 0;
  730. EVP_CIPHER *cipher = NULL;
  731. int outlen = 0;
  732. EVP_CIPHER_CTX *ctx = NULL;
  733. const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
  734. ec = ossl_cms_get0_env_enc_content(cms);
  735. if (ec == NULL)
  736. return 0;
  737. kekri = ri->d.kekri;
  738. if (kekri->key == NULL) {
  739. ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
  740. return 0;
  741. }
  742. cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
  743. if (cipher == NULL) {
  744. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
  745. goto err;
  746. }
  747. /* 8 byte prefix for AES wrap ciphers */
  748. wkey = OPENSSL_malloc(ec->keylen + 8);
  749. if (wkey == NULL)
  750. goto err;
  751. ctx = EVP_CIPHER_CTX_new();
  752. if (ctx == NULL) {
  753. ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
  754. goto err;
  755. }
  756. EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  757. if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
  758. || !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, ec->keylen)
  759. || !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) {
  760. ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR);
  761. goto err;
  762. }
  763. wkeylen += outlen;
  764. if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) {
  765. ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR);
  766. goto err;
  767. }
  768. ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
  769. r = 1;
  770. err:
  771. EVP_CIPHER_free(cipher);
  772. if (!r)
  773. OPENSSL_free(wkey);
  774. EVP_CIPHER_CTX_free(ctx);
  775. return r;
  776. }
  777. /* Decrypt content key in KEK recipient info */
  778. static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
  779. CMS_RecipientInfo *ri)
  780. {
  781. CMS_EncryptedContentInfo *ec;
  782. CMS_KEKRecipientInfo *kekri;
  783. unsigned char *ukey = NULL;
  784. int ukeylen;
  785. int r = 0, wrap_nid;
  786. EVP_CIPHER *cipher = NULL;
  787. int outlen = 0;
  788. EVP_CIPHER_CTX *ctx = NULL;
  789. const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms);
  790. ec = ossl_cms_get0_env_enc_content(cms);
  791. if (ec == NULL)
  792. return 0;
  793. kekri = ri->d.kekri;
  794. if (!kekri->key) {
  795. ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY);
  796. return 0;
  797. }
  798. wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
  799. if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
  800. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
  801. return 0;
  802. }
  803. /* If encrypted key length is invalid don't bother */
  804. if (kekri->encryptedKey->length < 16) {
  805. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
  806. goto err;
  807. }
  808. cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
  809. if (cipher == NULL) {
  810. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
  811. goto err;
  812. }
  813. ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
  814. if (ukey == NULL)
  815. goto err;
  816. ctx = EVP_CIPHER_CTX_new();
  817. if (ctx == NULL) {
  818. ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
  819. goto err;
  820. }
  821. if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
  822. || !EVP_DecryptUpdate(ctx, ukey, &ukeylen,
  823. kekri->encryptedKey->data,
  824. kekri->encryptedKey->length)
  825. || !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) {
  826. ERR_raise(ERR_LIB_CMS, CMS_R_UNWRAP_ERROR);
  827. goto err;
  828. }
  829. ukeylen += outlen;
  830. OPENSSL_clear_free(ec->key, ec->keylen);
  831. ec->key = ukey;
  832. ec->keylen = ukeylen;
  833. r = 1;
  834. err:
  835. EVP_CIPHER_free(cipher);
  836. if (!r)
  837. OPENSSL_free(ukey);
  838. EVP_CIPHER_CTX_free(ctx);
  839. return r;
  840. }
  841. int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  842. {
  843. switch (ri->type) {
  844. case CMS_RECIPINFO_TRANS:
  845. return cms_RecipientInfo_ktri_decrypt(cms, ri);
  846. case CMS_RECIPINFO_KEK:
  847. return cms_RecipientInfo_kekri_decrypt(cms, ri);
  848. case CMS_RECIPINFO_PASS:
  849. return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 0);
  850. default:
  851. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
  852. return 0;
  853. }
  854. }
  855. int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  856. {
  857. switch (ri->type) {
  858. case CMS_RECIPINFO_TRANS:
  859. return cms_RecipientInfo_ktri_encrypt(cms, ri);
  860. case CMS_RECIPINFO_AGREE:
  861. return ossl_cms_RecipientInfo_kari_encrypt(cms, ri);
  862. case CMS_RECIPINFO_KEK:
  863. return cms_RecipientInfo_kekri_encrypt(cms, ri);
  864. case CMS_RECIPINFO_PASS:
  865. return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 1);
  866. default:
  867. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
  868. return 0;
  869. }
  870. }
  871. /* Check structures and fixup version numbers (if necessary) */
  872. static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
  873. {
  874. CMS_OriginatorInfo *org = env->originatorInfo;
  875. int i;
  876. if (org == NULL)
  877. return;
  878. for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
  879. CMS_CertificateChoices *cch;
  880. cch = sk_CMS_CertificateChoices_value(org->certificates, i);
  881. if (cch->type == CMS_CERTCHOICE_OTHER) {
  882. env->version = 4;
  883. return;
  884. } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
  885. if (env->version < 3)
  886. env->version = 3;
  887. }
  888. }
  889. for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
  890. CMS_RevocationInfoChoice *rch;
  891. rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
  892. if (rch->type == CMS_REVCHOICE_OTHER) {
  893. env->version = 4;
  894. return;
  895. }
  896. }
  897. }
  898. static void cms_env_set_version(CMS_EnvelopedData *env)
  899. {
  900. int i;
  901. CMS_RecipientInfo *ri;
  902. /*
  903. * Can't set version higher than 4 so if 4 or more already nothing to do.
  904. */
  905. if (env->version >= 4)
  906. return;
  907. cms_env_set_originfo_version(env);
  908. if (env->version >= 3)
  909. return;
  910. for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
  911. ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
  912. if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
  913. env->version = 3;
  914. return;
  915. } else if (ri->type != CMS_RECIPINFO_TRANS
  916. || ri->d.ktri->version != 0) {
  917. env->version = 2;
  918. }
  919. }
  920. if (env->originatorInfo || env->unprotectedAttrs)
  921. env->version = 2;
  922. if (env->version == 2)
  923. return;
  924. env->version = 0;
  925. }
  926. static int cms_env_encrypt_content_key(const CMS_ContentInfo *cms,
  927. STACK_OF(CMS_RecipientInfo) *ris)
  928. {
  929. int i;
  930. CMS_RecipientInfo *ri;
  931. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  932. ri = sk_CMS_RecipientInfo_value(ris, i);
  933. if (CMS_RecipientInfo_encrypt(cms, ri) <= 0)
  934. return -1;
  935. }
  936. return 1;
  937. }
  938. static void cms_env_clear_ec(CMS_EncryptedContentInfo *ec)
  939. {
  940. ec->cipher = NULL;
  941. OPENSSL_clear_free(ec->key, ec->keylen);
  942. ec->key = NULL;
  943. ec->keylen = 0;
  944. }
  945. static BIO *cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo *cms)
  946. {
  947. CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo;
  948. BIO *contentBio = ossl_cms_EncryptedContent_init_bio(ec,
  949. ossl_cms_get0_cmsctx(cms));
  950. EVP_CIPHER_CTX *ctx = NULL;
  951. if (contentBio == NULL)
  952. return NULL;
  953. BIO_get_cipher_ctx(contentBio, &ctx);
  954. if (ctx == NULL) {
  955. BIO_free(contentBio);
  956. return NULL;
  957. }
  958. /*
  959. * If the selected cipher supports unprotected attributes,
  960. * deal with it using special ctrl function
  961. */
  962. if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
  963. & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0
  964. && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, 0,
  965. cms->d.envelopedData->unprotectedAttrs) <= 0) {
  966. BIO_free(contentBio);
  967. return NULL;
  968. }
  969. return contentBio;
  970. }
  971. static BIO *cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo *cms)
  972. {
  973. CMS_EncryptedContentInfo *ec;
  974. STACK_OF(CMS_RecipientInfo) *rinfos;
  975. int ok = 0;
  976. BIO *ret;
  977. CMS_EnvelopedData *env = cms->d.envelopedData;
  978. /* Get BIO first to set up key */
  979. ec = env->encryptedContentInfo;
  980. ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms));
  981. /* If error end of processing */
  982. if (!ret)
  983. return ret;
  984. /* Now encrypt content key according to each RecipientInfo type */
  985. rinfos = env->recipientInfos;
  986. if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
  987. ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO);
  988. goto err;
  989. }
  990. /* And finally set the version */
  991. cms_env_set_version(env);
  992. ok = 1;
  993. err:
  994. cms_env_clear_ec(ec);
  995. if (ok)
  996. return ret;
  997. BIO_free(ret);
  998. return NULL;
  999. }
  1000. BIO *ossl_cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
  1001. {
  1002. if (cms->d.envelopedData->encryptedContentInfo->cipher != NULL) {
  1003. /* If cipher is set it's encryption */
  1004. return cms_EnvelopedData_Encryption_init_bio(cms);
  1005. }
  1006. /* If cipher is not set it's decryption */
  1007. return cms_EnvelopedData_Decryption_init_bio(cms);
  1008. }
  1009. BIO *ossl_cms_AuthEnvelopedData_init_bio(CMS_ContentInfo *cms)
  1010. {
  1011. CMS_EncryptedContentInfo *ec;
  1012. STACK_OF(CMS_RecipientInfo) *rinfos;
  1013. int ok = 0;
  1014. BIO *ret;
  1015. CMS_AuthEnvelopedData *aenv = cms->d.authEnvelopedData;
  1016. /* Get BIO first to set up key */
  1017. ec = aenv->authEncryptedContentInfo;
  1018. /* Set tag for decryption */
  1019. if (ec->cipher == NULL) {
  1020. ec->tag = aenv->mac->data;
  1021. ec->taglen = aenv->mac->length;
  1022. }
  1023. ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms));
  1024. /* If error or no cipher end of processing */
  1025. if (ret == NULL || ec->cipher == NULL)
  1026. return ret;
  1027. /* Now encrypt content key according to each RecipientInfo type */
  1028. rinfos = aenv->recipientInfos;
  1029. if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
  1030. ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO);
  1031. goto err;
  1032. }
  1033. /* And finally set the version */
  1034. aenv->version = 0;
  1035. ok = 1;
  1036. err:
  1037. cms_env_clear_ec(ec);
  1038. if (ok)
  1039. return ret;
  1040. BIO_free(ret);
  1041. return NULL;
  1042. }
  1043. int ossl_cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain)
  1044. {
  1045. CMS_EnvelopedData *env = NULL;
  1046. EVP_CIPHER_CTX *ctx = NULL;
  1047. BIO *mbio = BIO_find_type(chain, BIO_TYPE_CIPHER);
  1048. env = ossl_cms_get0_enveloped(cms);
  1049. if (env == NULL)
  1050. return 0;
  1051. if (mbio == NULL) {
  1052. ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND);
  1053. return 0;
  1054. }
  1055. BIO_get_cipher_ctx(mbio, &ctx);
  1056. /*
  1057. * If the selected cipher supports unprotected attributes,
  1058. * deal with it using special ctrl function
  1059. */
  1060. if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
  1061. & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0) {
  1062. if (env->unprotectedAttrs == NULL)
  1063. env->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null();
  1064. if (env->unprotectedAttrs == NULL) {
  1065. ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
  1066. return 0;
  1067. }
  1068. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED,
  1069. 1, env->unprotectedAttrs) <= 0) {
  1070. ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
  1071. return 0;
  1072. }
  1073. }
  1074. cms_env_set_version(cms->d.envelopedData);
  1075. return 1;
  1076. }
  1077. int ossl_cms_AuthEnvelopedData_final(CMS_ContentInfo *cms, BIO *cmsbio)
  1078. {
  1079. EVP_CIPHER_CTX *ctx;
  1080. unsigned char *tag = NULL;
  1081. int taglen, ok = 0;
  1082. BIO_get_cipher_ctx(cmsbio, &ctx);
  1083. /*
  1084. * The tag is set only for encryption. There is nothing to do for
  1085. * decryption.
  1086. */
  1087. if (!EVP_CIPHER_CTX_is_encrypting(ctx))
  1088. return 1;
  1089. taglen = EVP_CIPHER_CTX_get_tag_length(ctx);
  1090. if (taglen <= 0
  1091. || (tag = OPENSSL_malloc(taglen)) == NULL
  1092. || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
  1093. tag) <= 0) {
  1094. ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_GET_TAG);
  1095. goto err;
  1096. }
  1097. if (!ASN1_OCTET_STRING_set(cms->d.authEnvelopedData->mac, tag, taglen))
  1098. goto err;
  1099. ok = 1;
  1100. err:
  1101. OPENSSL_free(tag);
  1102. return ok;
  1103. }
  1104. /*
  1105. * Get RecipientInfo type (if any) supported by a key (public or private). To
  1106. * retain compatibility with previous behaviour if the ctrl value isn't
  1107. * supported we assume key transport.
  1108. */
  1109. int ossl_cms_pkey_get_ri_type(EVP_PKEY *pk)
  1110. {
  1111. /* Check types that we know about */
  1112. if (EVP_PKEY_is_a(pk, "DH"))
  1113. return CMS_RECIPINFO_AGREE;
  1114. else if (EVP_PKEY_is_a(pk, "DHX"))
  1115. return CMS_RECIPINFO_AGREE;
  1116. else if (EVP_PKEY_is_a(pk, "DSA"))
  1117. return CMS_RECIPINFO_NONE;
  1118. else if (EVP_PKEY_is_a(pk, "EC"))
  1119. return CMS_RECIPINFO_AGREE;
  1120. else if (EVP_PKEY_is_a(pk, "RSA"))
  1121. return CMS_RECIPINFO_TRANS;
  1122. /*
  1123. * Otherwise this might ben an engine implementation, so see if we can get
  1124. * the type from the ameth.
  1125. */
  1126. if (pk->ameth && pk->ameth->pkey_ctrl) {
  1127. int i, r;
  1128. i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
  1129. if (i > 0)
  1130. return r;
  1131. }
  1132. return CMS_RECIPINFO_TRANS;
  1133. }
  1134. int ossl_cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type)
  1135. {
  1136. int supportedRiType;
  1137. if (pk->ameth != NULL && pk->ameth->pkey_ctrl != NULL) {
  1138. int i, r;
  1139. i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED,
  1140. ri_type, &r);
  1141. if (i > 0)
  1142. return r;
  1143. }
  1144. supportedRiType = ossl_cms_pkey_get_ri_type(pk);
  1145. if (supportedRiType < 0)
  1146. return 0;
  1147. return (supportedRiType == ri_type);
  1148. }