pk7_lib.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. * Copyright 1995-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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/objects.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/pkcs7.h>
  14. #include "crypto/asn1.h"
  15. #include "crypto/evp.h"
  16. #include "crypto/x509.h" /* for sk_X509_add1_cert() */
  17. #include "pk7_local.h"
  18. long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg)
  19. {
  20. int nid;
  21. long ret;
  22. nid = OBJ_obj2nid(p7->type);
  23. switch (cmd) {
  24. /* NOTE(emilia): does not support detached digested data. */
  25. case PKCS7_OP_SET_DETACHED_SIGNATURE:
  26. if (nid == NID_pkcs7_signed) {
  27. if (p7->d.sign == NULL) {
  28. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
  29. ret = 0;
  30. break;
  31. }
  32. ret = p7->detached = (int)larg;
  33. if (ret && PKCS7_type_is_data(p7->d.sign->contents)) {
  34. ASN1_OCTET_STRING *os;
  35. os = p7->d.sign->contents->d.data;
  36. ASN1_OCTET_STRING_free(os);
  37. p7->d.sign->contents->d.data = NULL;
  38. }
  39. } else {
  40. ERR_raise(ERR_LIB_PKCS7,
  41. PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
  42. ret = 0;
  43. }
  44. break;
  45. case PKCS7_OP_GET_DETACHED_SIGNATURE:
  46. if (nid == NID_pkcs7_signed) {
  47. if (p7->d.sign == NULL || p7->d.sign->contents->d.ptr == NULL)
  48. ret = 1;
  49. else
  50. ret = 0;
  51. p7->detached = ret;
  52. } else {
  53. ERR_raise(ERR_LIB_PKCS7,
  54. PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
  55. ret = 0;
  56. }
  57. break;
  58. default:
  59. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_OPERATION);
  60. ret = 0;
  61. }
  62. return ret;
  63. }
  64. int PKCS7_content_new(PKCS7 *p7, int type)
  65. {
  66. PKCS7 *ret = NULL;
  67. if ((ret = PKCS7_new()) == NULL)
  68. goto err;
  69. if (!PKCS7_set_type(ret, type))
  70. goto err;
  71. if (!PKCS7_set_content(p7, ret))
  72. goto err;
  73. return 1;
  74. err:
  75. PKCS7_free(ret);
  76. return 0;
  77. }
  78. int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data)
  79. {
  80. int i;
  81. i = OBJ_obj2nid(p7->type);
  82. switch (i) {
  83. case NID_pkcs7_signed:
  84. PKCS7_free(p7->d.sign->contents);
  85. p7->d.sign->contents = p7_data;
  86. break;
  87. case NID_pkcs7_digest:
  88. PKCS7_free(p7->d.digest->contents);
  89. p7->d.digest->contents = p7_data;
  90. break;
  91. case NID_pkcs7_data:
  92. case NID_pkcs7_enveloped:
  93. case NID_pkcs7_signedAndEnveloped:
  94. case NID_pkcs7_encrypted:
  95. default:
  96. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  97. goto err;
  98. }
  99. return 1;
  100. err:
  101. return 0;
  102. }
  103. int PKCS7_set_type(PKCS7 *p7, int type)
  104. {
  105. ASN1_OBJECT *obj;
  106. /*
  107. * PKCS7_content_free(p7);
  108. */
  109. obj = OBJ_nid2obj(type); /* will not fail */
  110. switch (type) {
  111. case NID_pkcs7_signed:
  112. p7->type = obj;
  113. if ((p7->d.sign = PKCS7_SIGNED_new()) == NULL)
  114. goto err;
  115. if (!ASN1_INTEGER_set(p7->d.sign->version, 1)) {
  116. PKCS7_SIGNED_free(p7->d.sign);
  117. p7->d.sign = NULL;
  118. goto err;
  119. }
  120. break;
  121. case NID_pkcs7_data:
  122. p7->type = obj;
  123. if ((p7->d.data = ASN1_OCTET_STRING_new()) == NULL)
  124. goto err;
  125. break;
  126. case NID_pkcs7_signedAndEnveloped:
  127. p7->type = obj;
  128. if ((p7->d.signed_and_enveloped = PKCS7_SIGN_ENVELOPE_new())
  129. == NULL)
  130. goto err;
  131. if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1))
  132. goto err;
  133. p7->d.signed_and_enveloped->enc_data->content_type
  134. = OBJ_nid2obj(NID_pkcs7_data);
  135. break;
  136. case NID_pkcs7_enveloped:
  137. p7->type = obj;
  138. if ((p7->d.enveloped = PKCS7_ENVELOPE_new())
  139. == NULL)
  140. goto err;
  141. if (!ASN1_INTEGER_set(p7->d.enveloped->version, 0))
  142. goto err;
  143. p7->d.enveloped->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
  144. break;
  145. case NID_pkcs7_encrypted:
  146. p7->type = obj;
  147. if ((p7->d.encrypted = PKCS7_ENCRYPT_new())
  148. == NULL)
  149. goto err;
  150. if (!ASN1_INTEGER_set(p7->d.encrypted->version, 0))
  151. goto err;
  152. p7->d.encrypted->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
  153. break;
  154. case NID_pkcs7_digest:
  155. p7->type = obj;
  156. if ((p7->d.digest = PKCS7_DIGEST_new())
  157. == NULL)
  158. goto err;
  159. if (!ASN1_INTEGER_set(p7->d.digest->version, 0))
  160. goto err;
  161. break;
  162. default:
  163. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  164. goto err;
  165. }
  166. return 1;
  167. err:
  168. return 0;
  169. }
  170. int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other)
  171. {
  172. p7->type = OBJ_nid2obj(type);
  173. p7->d.other = other;
  174. return 1;
  175. }
  176. int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi)
  177. {
  178. int i, j;
  179. ASN1_OBJECT *obj;
  180. X509_ALGOR *alg;
  181. STACK_OF(PKCS7_SIGNER_INFO) *signer_sk;
  182. STACK_OF(X509_ALGOR) *md_sk;
  183. i = OBJ_obj2nid(p7->type);
  184. switch (i) {
  185. case NID_pkcs7_signed:
  186. signer_sk = p7->d.sign->signer_info;
  187. md_sk = p7->d.sign->md_algs;
  188. break;
  189. case NID_pkcs7_signedAndEnveloped:
  190. signer_sk = p7->d.signed_and_enveloped->signer_info;
  191. md_sk = p7->d.signed_and_enveloped->md_algs;
  192. break;
  193. default:
  194. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
  195. return 0;
  196. }
  197. obj = psi->digest_alg->algorithm;
  198. /* If the digest is not currently listed, add it */
  199. j = 0;
  200. for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
  201. alg = sk_X509_ALGOR_value(md_sk, i);
  202. if (OBJ_cmp(obj, alg->algorithm) == 0) {
  203. j = 1;
  204. break;
  205. }
  206. }
  207. if (!j) { /* we need to add another algorithm */
  208. int nid;
  209. if ((alg = X509_ALGOR_new()) == NULL
  210. || (alg->parameter = ASN1_TYPE_new()) == NULL) {
  211. X509_ALGOR_free(alg);
  212. ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
  213. return 0;
  214. }
  215. /*
  216. * If there is a constant copy of the ASN1 OBJECT in libcrypto, then
  217. * use that. Otherwise, use a dynamically duplicated copy
  218. */
  219. if ((nid = OBJ_obj2nid(obj)) != NID_undef)
  220. alg->algorithm = OBJ_nid2obj(nid);
  221. else
  222. alg->algorithm = OBJ_dup(obj);
  223. alg->parameter->type = V_ASN1_NULL;
  224. if (alg->algorithm == NULL || !sk_X509_ALGOR_push(md_sk, alg)) {
  225. X509_ALGOR_free(alg);
  226. return 0;
  227. }
  228. }
  229. psi->ctx = ossl_pkcs7_get0_ctx(p7);
  230. if (!sk_PKCS7_SIGNER_INFO_push(signer_sk, psi))
  231. return 0;
  232. return 1;
  233. }
  234. int PKCS7_add_certificate(PKCS7 *p7, X509 *x509)
  235. {
  236. int i;
  237. STACK_OF(X509) **sk;
  238. i = OBJ_obj2nid(p7->type);
  239. switch (i) {
  240. case NID_pkcs7_signed:
  241. sk = &(p7->d.sign->cert);
  242. break;
  243. case NID_pkcs7_signedAndEnveloped:
  244. sk = &(p7->d.signed_and_enveloped->cert);
  245. break;
  246. default:
  247. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
  248. return 0;
  249. }
  250. return ossl_x509_add_cert_new(sk, x509, X509_ADD_FLAG_UP_REF);
  251. }
  252. int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl)
  253. {
  254. int i;
  255. STACK_OF(X509_CRL) **sk;
  256. i = OBJ_obj2nid(p7->type);
  257. switch (i) {
  258. case NID_pkcs7_signed:
  259. sk = &(p7->d.sign->crl);
  260. break;
  261. case NID_pkcs7_signedAndEnveloped:
  262. sk = &(p7->d.signed_and_enveloped->crl);
  263. break;
  264. default:
  265. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
  266. return 0;
  267. }
  268. if (*sk == NULL)
  269. *sk = sk_X509_CRL_new_null();
  270. if (*sk == NULL) {
  271. ERR_raise(ERR_LIB_PKCS7, ERR_R_CRYPTO_LIB);
  272. return 0;
  273. }
  274. if (!X509_CRL_up_ref(crl))
  275. return 0;
  276. if (!sk_X509_CRL_push(*sk, crl)) {
  277. X509_CRL_free(crl);
  278. return 0;
  279. }
  280. return 1;
  281. }
  282. static int pkcs7_ecdsa_or_dsa_sign_verify_setup(PKCS7_SIGNER_INFO *si,
  283. int verify)
  284. {
  285. if (!verify) {
  286. int snid, hnid;
  287. X509_ALGOR *alg1, *alg2;
  288. EVP_PKEY *pkey = si->pkey;
  289. PKCS7_SIGNER_INFO_get0_algs(si, NULL, &alg1, &alg2);
  290. if (alg1 == NULL || alg1->algorithm == NULL)
  291. return -1;
  292. hnid = OBJ_obj2nid(alg1->algorithm);
  293. if (hnid == NID_undef)
  294. return -1;
  295. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_get_id(pkey)))
  296. return -1;
  297. return X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, NULL);
  298. }
  299. return 1;
  300. }
  301. static int pkcs7_rsa_sign_verify_setup(PKCS7_SIGNER_INFO *si, int verify)
  302. {
  303. if (!verify) {
  304. X509_ALGOR *alg = NULL;
  305. PKCS7_SIGNER_INFO_get0_algs(si, NULL, NULL, &alg);
  306. if (alg != NULL)
  307. return X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption),
  308. V_ASN1_NULL, NULL);
  309. }
  310. return 1;
  311. }
  312. int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
  313. const EVP_MD *dgst)
  314. {
  315. int ret;
  316. /* We now need to add another PKCS7_SIGNER_INFO entry */
  317. if (!ASN1_INTEGER_set(p7i->version, 1))
  318. return 0;
  319. if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
  320. X509_get_issuer_name(x509)))
  321. return 0;
  322. /*
  323. * because ASN1_INTEGER_set is used to set a 'long' we will do things the
  324. * ugly way.
  325. */
  326. ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
  327. if (!(p7i->issuer_and_serial->serial =
  328. ASN1_INTEGER_dup(X509_get0_serialNumber(x509))))
  329. return 0;
  330. /* lets keep the pkey around for a while */
  331. if (!EVP_PKEY_up_ref(pkey))
  332. return 0;
  333. p7i->pkey = pkey;
  334. /* Set the algorithms */
  335. if (!X509_ALGOR_set0(p7i->digest_alg, OBJ_nid2obj(EVP_MD_get_type(dgst)),
  336. V_ASN1_NULL, NULL))
  337. return 0;
  338. if (EVP_PKEY_is_a(pkey, "EC") || EVP_PKEY_is_a(pkey, "DSA"))
  339. return pkcs7_ecdsa_or_dsa_sign_verify_setup(p7i, 0);
  340. if (EVP_PKEY_is_a(pkey, "RSA"))
  341. return pkcs7_rsa_sign_verify_setup(p7i, 0);
  342. if (pkey->ameth != NULL && pkey->ameth->pkey_ctrl != NULL) {
  343. ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_SIGN, 0, p7i);
  344. if (ret > 0)
  345. return 1;
  346. if (ret != -2) {
  347. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNING_CTRL_FAILURE);
  348. return 0;
  349. }
  350. }
  351. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  352. return 0;
  353. }
  354. PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey,
  355. const EVP_MD *dgst)
  356. {
  357. PKCS7_SIGNER_INFO *si = NULL;
  358. if (dgst == NULL) {
  359. int def_nid;
  360. if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0)
  361. goto err;
  362. dgst = EVP_get_digestbynid(def_nid);
  363. if (dgst == NULL) {
  364. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_DEFAULT_DIGEST);
  365. goto err;
  366. }
  367. }
  368. if ((si = PKCS7_SIGNER_INFO_new()) == NULL)
  369. goto err;
  370. if (PKCS7_SIGNER_INFO_set(si, x509, pkey, dgst) <= 0)
  371. goto err;
  372. if (!PKCS7_add_signer(p7, si))
  373. goto err;
  374. return si;
  375. err:
  376. PKCS7_SIGNER_INFO_free(si);
  377. return NULL;
  378. }
  379. STACK_OF(X509) *pkcs7_get0_certificates(const PKCS7 *p7)
  380. {
  381. if (p7->d.ptr == NULL)
  382. return NULL;
  383. if (PKCS7_type_is_signed(p7))
  384. return p7->d.sign->cert;
  385. if (PKCS7_type_is_signedAndEnveloped(p7))
  386. return p7->d.signed_and_enveloped->cert;
  387. return NULL;
  388. }
  389. static STACK_OF(PKCS7_RECIP_INFO) *pkcs7_get_recipient_info(const PKCS7 *p7)
  390. {
  391. if (p7->d.ptr == NULL)
  392. return NULL;
  393. if (PKCS7_type_is_signedAndEnveloped(p7))
  394. return p7->d.signed_and_enveloped->recipientinfo;
  395. if (PKCS7_type_is_enveloped(p7))
  396. return p7->d.enveloped->recipientinfo;
  397. return NULL;
  398. }
  399. /*
  400. * Set up the library context into any loaded structure that needs it.
  401. * i.e loaded X509 objects.
  402. */
  403. void ossl_pkcs7_resolve_libctx(PKCS7 *p7)
  404. {
  405. int i;
  406. const PKCS7_CTX *ctx = ossl_pkcs7_get0_ctx(p7);
  407. OSSL_LIB_CTX *libctx = ossl_pkcs7_ctx_get0_libctx(ctx);
  408. const char *propq = ossl_pkcs7_ctx_get0_propq(ctx);
  409. STACK_OF(PKCS7_RECIP_INFO) *rinfos;
  410. STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
  411. STACK_OF(X509) *certs;
  412. if (ctx == NULL || p7->d.ptr == NULL)
  413. return;
  414. rinfos = pkcs7_get_recipient_info(p7);
  415. sinfos = PKCS7_get_signer_info(p7);
  416. certs = pkcs7_get0_certificates(p7);
  417. for (i = 0; i < sk_X509_num(certs); i++)
  418. ossl_x509_set0_libctx(sk_X509_value(certs, i), libctx, propq);
  419. for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rinfos); i++) {
  420. PKCS7_RECIP_INFO *ri = sk_PKCS7_RECIP_INFO_value(rinfos, i);
  421. ossl_x509_set0_libctx(ri->cert, libctx, propq);
  422. }
  423. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
  424. PKCS7_SIGNER_INFO *si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
  425. if (si != NULL)
  426. si->ctx = ctx;
  427. }
  428. }
  429. const PKCS7_CTX *ossl_pkcs7_get0_ctx(const PKCS7 *p7)
  430. {
  431. return p7 != NULL ? &p7->ctx : NULL;
  432. }
  433. void ossl_pkcs7_set0_libctx(PKCS7 *p7, OSSL_LIB_CTX *ctx)
  434. {
  435. p7->ctx.libctx = ctx;
  436. }
  437. int ossl_pkcs7_set1_propq(PKCS7 *p7, const char *propq)
  438. {
  439. if (p7->ctx.propq != NULL) {
  440. OPENSSL_free(p7->ctx.propq);
  441. p7->ctx.propq = NULL;
  442. }
  443. if (propq != NULL) {
  444. p7->ctx.propq = OPENSSL_strdup(propq);
  445. if (p7->ctx.propq == NULL)
  446. return 0;
  447. }
  448. return 1;
  449. }
  450. int ossl_pkcs7_ctx_propagate(const PKCS7 *from, PKCS7 *to)
  451. {
  452. ossl_pkcs7_set0_libctx(to, from->ctx.libctx);
  453. if (!ossl_pkcs7_set1_propq(to, from->ctx.propq))
  454. return 0;
  455. ossl_pkcs7_resolve_libctx(to);
  456. return 1;
  457. }
  458. OSSL_LIB_CTX *ossl_pkcs7_ctx_get0_libctx(const PKCS7_CTX *ctx)
  459. {
  460. return ctx != NULL ? ctx->libctx : NULL;
  461. }
  462. const char *ossl_pkcs7_ctx_get0_propq(const PKCS7_CTX *ctx)
  463. {
  464. return ctx != NULL ? ctx->propq : NULL;
  465. }
  466. int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md)
  467. {
  468. if (PKCS7_type_is_digest(p7)) {
  469. if ((p7->d.digest->md->parameter = ASN1_TYPE_new()) == NULL) {
  470. ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
  471. return 0;
  472. }
  473. p7->d.digest->md->parameter->type = V_ASN1_NULL;
  474. p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
  475. return 1;
  476. }
  477. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
  478. return 1;
  479. }
  480. STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7)
  481. {
  482. if (p7 == NULL || p7->d.ptr == NULL)
  483. return NULL;
  484. if (PKCS7_type_is_signed(p7)) {
  485. return p7->d.sign->signer_info;
  486. } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
  487. return p7->d.signed_and_enveloped->signer_info;
  488. } else
  489. return NULL;
  490. }
  491. void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk,
  492. X509_ALGOR **pdig, X509_ALGOR **psig)
  493. {
  494. if (pk)
  495. *pk = si->pkey;
  496. if (pdig)
  497. *pdig = si->digest_alg;
  498. if (psig)
  499. *psig = si->digest_enc_alg;
  500. }
  501. void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc)
  502. {
  503. if (penc)
  504. *penc = ri->key_enc_algor;
  505. }
  506. PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509)
  507. {
  508. PKCS7_RECIP_INFO *ri;
  509. if ((ri = PKCS7_RECIP_INFO_new()) == NULL)
  510. goto err;
  511. if (PKCS7_RECIP_INFO_set(ri, x509) <= 0)
  512. goto err;
  513. if (!PKCS7_add_recipient_info(p7, ri))
  514. goto err;
  515. ri->ctx = ossl_pkcs7_get0_ctx(p7);
  516. return ri;
  517. err:
  518. PKCS7_RECIP_INFO_free(ri);
  519. return NULL;
  520. }
  521. int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri)
  522. {
  523. int i;
  524. STACK_OF(PKCS7_RECIP_INFO) *sk;
  525. i = OBJ_obj2nid(p7->type);
  526. switch (i) {
  527. case NID_pkcs7_signedAndEnveloped:
  528. sk = p7->d.signed_and_enveloped->recipientinfo;
  529. break;
  530. case NID_pkcs7_enveloped:
  531. sk = p7->d.enveloped->recipientinfo;
  532. break;
  533. default:
  534. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
  535. return 0;
  536. }
  537. if (!sk_PKCS7_RECIP_INFO_push(sk, ri))
  538. return 0;
  539. return 1;
  540. }
  541. static int pkcs7_rsa_encrypt_decrypt_setup(PKCS7_RECIP_INFO *ri, int decrypt)
  542. {
  543. X509_ALGOR *alg = NULL;
  544. if (!decrypt) {
  545. PKCS7_RECIP_INFO_get0_alg(ri, &alg);
  546. if (alg != NULL)
  547. return X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption),
  548. V_ASN1_NULL, NULL);
  549. }
  550. return 1;
  551. }
  552. int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509)
  553. {
  554. int ret;
  555. EVP_PKEY *pkey = NULL;
  556. if (!ASN1_INTEGER_set(p7i->version, 0))
  557. return 0;
  558. if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
  559. X509_get_issuer_name(x509)))
  560. return 0;
  561. ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
  562. if (!(p7i->issuer_and_serial->serial =
  563. ASN1_INTEGER_dup(X509_get0_serialNumber(x509))))
  564. return 0;
  565. pkey = X509_get0_pubkey(x509);
  566. if (pkey == NULL)
  567. return 0;
  568. if (EVP_PKEY_is_a(pkey, "RSA-PSS"))
  569. return -2;
  570. if (EVP_PKEY_is_a(pkey, "RSA")) {
  571. if (pkcs7_rsa_encrypt_decrypt_setup(p7i, 0) <= 0)
  572. goto err;
  573. goto finished;
  574. }
  575. if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL) {
  576. ERR_raise(ERR_LIB_PKCS7,
  577. PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  578. goto err;
  579. }
  580. ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_ENCRYPT, 0, p7i);
  581. if (ret == -2) {
  582. ERR_raise(ERR_LIB_PKCS7,
  583. PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  584. goto err;
  585. }
  586. if (ret <= 0) {
  587. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_ENCRYPTION_CTRL_FAILURE);
  588. goto err;
  589. }
  590. finished:
  591. if (!X509_up_ref(x509))
  592. goto err;
  593. p7i->cert = x509;
  594. return 1;
  595. err:
  596. return 0;
  597. }
  598. X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
  599. {
  600. if (PKCS7_type_is_signed(p7))
  601. return (X509_find_by_issuer_and_serial(p7->d.sign->cert,
  602. si->issuer_and_serial->issuer,
  603. si->
  604. issuer_and_serial->serial));
  605. else
  606. return NULL;
  607. }
  608. int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher)
  609. {
  610. int i;
  611. PKCS7_ENC_CONTENT *ec;
  612. i = OBJ_obj2nid(p7->type);
  613. switch (i) {
  614. case NID_pkcs7_signedAndEnveloped:
  615. ec = p7->d.signed_and_enveloped->enc_data;
  616. break;
  617. case NID_pkcs7_enveloped:
  618. ec = p7->d.enveloped->enc_data;
  619. break;
  620. default:
  621. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
  622. return 0;
  623. }
  624. /* Check cipher OID exists and has data in it */
  625. i = EVP_CIPHER_get_type(cipher);
  626. if (i == NID_undef) {
  627. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
  628. return 0;
  629. }
  630. ec->cipher = cipher;
  631. ec->ctx = ossl_pkcs7_get0_ctx(p7);
  632. return 1;
  633. }
  634. /* unfortunately cannot constify BIO_new_NDEF() due to this and CMS_stream() */
  635. int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7)
  636. {
  637. ASN1_OCTET_STRING *os = NULL;
  638. switch (OBJ_obj2nid(p7->type)) {
  639. case NID_pkcs7_data:
  640. os = p7->d.data;
  641. break;
  642. case NID_pkcs7_signedAndEnveloped:
  643. os = p7->d.signed_and_enveloped->enc_data->enc_data;
  644. if (os == NULL) {
  645. os = ASN1_OCTET_STRING_new();
  646. p7->d.signed_and_enveloped->enc_data->enc_data = os;
  647. }
  648. break;
  649. case NID_pkcs7_enveloped:
  650. os = p7->d.enveloped->enc_data->enc_data;
  651. if (os == NULL) {
  652. os = ASN1_OCTET_STRING_new();
  653. p7->d.enveloped->enc_data->enc_data = os;
  654. }
  655. break;
  656. case NID_pkcs7_signed:
  657. os = p7->d.sign->contents->d.data;
  658. break;
  659. default:
  660. os = NULL;
  661. break;
  662. }
  663. if (os == NULL)
  664. return 0;
  665. os->flags |= ASN1_STRING_FLAG_NDEF;
  666. *boundary = &os->data;
  667. return 1;
  668. }