2
0

pk7_lib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "internal/asn1_int.h"
  14. #include "internal/evp_int.h"
  15. long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg)
  16. {
  17. int nid;
  18. long ret;
  19. nid = OBJ_obj2nid(p7->type);
  20. switch (cmd) {
  21. /* NOTE(emilia): does not support detached digested data. */
  22. case PKCS7_OP_SET_DETACHED_SIGNATURE:
  23. if (nid == NID_pkcs7_signed) {
  24. ret = p7->detached = (int)larg;
  25. if (ret && PKCS7_type_is_data(p7->d.sign->contents)) {
  26. ASN1_OCTET_STRING *os;
  27. os = p7->d.sign->contents->d.data;
  28. ASN1_OCTET_STRING_free(os);
  29. p7->d.sign->contents->d.data = NULL;
  30. }
  31. } else {
  32. PKCS7err(PKCS7_F_PKCS7_CTRL,
  33. PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
  34. ret = 0;
  35. }
  36. break;
  37. case PKCS7_OP_GET_DETACHED_SIGNATURE:
  38. if (nid == NID_pkcs7_signed) {
  39. if (!p7->d.sign || !p7->d.sign->contents->d.ptr)
  40. ret = 1;
  41. else
  42. ret = 0;
  43. p7->detached = ret;
  44. } else {
  45. PKCS7err(PKCS7_F_PKCS7_CTRL,
  46. PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
  47. ret = 0;
  48. }
  49. break;
  50. default:
  51. PKCS7err(PKCS7_F_PKCS7_CTRL, PKCS7_R_UNKNOWN_OPERATION);
  52. ret = 0;
  53. }
  54. return ret;
  55. }
  56. int PKCS7_content_new(PKCS7 *p7, int type)
  57. {
  58. PKCS7 *ret = NULL;
  59. if ((ret = PKCS7_new()) == NULL)
  60. goto err;
  61. if (!PKCS7_set_type(ret, type))
  62. goto err;
  63. if (!PKCS7_set_content(p7, ret))
  64. goto err;
  65. return 1;
  66. err:
  67. PKCS7_free(ret);
  68. return 0;
  69. }
  70. int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data)
  71. {
  72. int i;
  73. i = OBJ_obj2nid(p7->type);
  74. switch (i) {
  75. case NID_pkcs7_signed:
  76. PKCS7_free(p7->d.sign->contents);
  77. p7->d.sign->contents = p7_data;
  78. break;
  79. case NID_pkcs7_digest:
  80. PKCS7_free(p7->d.digest->contents);
  81. p7->d.digest->contents = p7_data;
  82. break;
  83. case NID_pkcs7_data:
  84. case NID_pkcs7_enveloped:
  85. case NID_pkcs7_signedAndEnveloped:
  86. case NID_pkcs7_encrypted:
  87. default:
  88. PKCS7err(PKCS7_F_PKCS7_SET_CONTENT, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  89. goto err;
  90. }
  91. return 1;
  92. err:
  93. return 0;
  94. }
  95. int PKCS7_set_type(PKCS7 *p7, int type)
  96. {
  97. ASN1_OBJECT *obj;
  98. /*
  99. * PKCS7_content_free(p7);
  100. */
  101. obj = OBJ_nid2obj(type); /* will not fail */
  102. switch (type) {
  103. case NID_pkcs7_signed:
  104. p7->type = obj;
  105. if ((p7->d.sign = PKCS7_SIGNED_new()) == NULL)
  106. goto err;
  107. if (!ASN1_INTEGER_set(p7->d.sign->version, 1)) {
  108. PKCS7_SIGNED_free(p7->d.sign);
  109. p7->d.sign = NULL;
  110. goto err;
  111. }
  112. break;
  113. case NID_pkcs7_data:
  114. p7->type = obj;
  115. if ((p7->d.data = ASN1_OCTET_STRING_new()) == NULL)
  116. goto err;
  117. break;
  118. case NID_pkcs7_signedAndEnveloped:
  119. p7->type = obj;
  120. if ((p7->d.signed_and_enveloped = PKCS7_SIGN_ENVELOPE_new())
  121. == NULL)
  122. goto err;
  123. if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1))
  124. goto err;
  125. p7->d.signed_and_enveloped->enc_data->content_type
  126. = OBJ_nid2obj(NID_pkcs7_data);
  127. break;
  128. case NID_pkcs7_enveloped:
  129. p7->type = obj;
  130. if ((p7->d.enveloped = PKCS7_ENVELOPE_new())
  131. == NULL)
  132. goto err;
  133. if (!ASN1_INTEGER_set(p7->d.enveloped->version, 0))
  134. goto err;
  135. p7->d.enveloped->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
  136. break;
  137. case NID_pkcs7_encrypted:
  138. p7->type = obj;
  139. if ((p7->d.encrypted = PKCS7_ENCRYPT_new())
  140. == NULL)
  141. goto err;
  142. if (!ASN1_INTEGER_set(p7->d.encrypted->version, 0))
  143. goto err;
  144. p7->d.encrypted->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
  145. break;
  146. case NID_pkcs7_digest:
  147. p7->type = obj;
  148. if ((p7->d.digest = PKCS7_DIGEST_new())
  149. == NULL)
  150. goto err;
  151. if (!ASN1_INTEGER_set(p7->d.digest->version, 0))
  152. goto err;
  153. break;
  154. default:
  155. PKCS7err(PKCS7_F_PKCS7_SET_TYPE, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
  156. goto err;
  157. }
  158. return 1;
  159. err:
  160. return 0;
  161. }
  162. int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other)
  163. {
  164. p7->type = OBJ_nid2obj(type);
  165. p7->d.other = other;
  166. return 1;
  167. }
  168. int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi)
  169. {
  170. int i, j, nid;
  171. X509_ALGOR *alg;
  172. STACK_OF(PKCS7_SIGNER_INFO) *signer_sk;
  173. STACK_OF(X509_ALGOR) *md_sk;
  174. i = OBJ_obj2nid(p7->type);
  175. switch (i) {
  176. case NID_pkcs7_signed:
  177. signer_sk = p7->d.sign->signer_info;
  178. md_sk = p7->d.sign->md_algs;
  179. break;
  180. case NID_pkcs7_signedAndEnveloped:
  181. signer_sk = p7->d.signed_and_enveloped->signer_info;
  182. md_sk = p7->d.signed_and_enveloped->md_algs;
  183. break;
  184. default:
  185. PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, PKCS7_R_WRONG_CONTENT_TYPE);
  186. return 0;
  187. }
  188. nid = OBJ_obj2nid(psi->digest_alg->algorithm);
  189. /* If the digest is not currently listed, add it */
  190. j = 0;
  191. for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
  192. alg = sk_X509_ALGOR_value(md_sk, i);
  193. if (OBJ_obj2nid(alg->algorithm) == nid) {
  194. j = 1;
  195. break;
  196. }
  197. }
  198. if (!j) { /* we need to add another algorithm */
  199. if ((alg = X509_ALGOR_new()) == NULL
  200. || (alg->parameter = ASN1_TYPE_new()) == NULL) {
  201. X509_ALGOR_free(alg);
  202. PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, ERR_R_MALLOC_FAILURE);
  203. return 0;
  204. }
  205. alg->algorithm = OBJ_nid2obj(nid);
  206. alg->parameter->type = V_ASN1_NULL;
  207. if (!sk_X509_ALGOR_push(md_sk, alg)) {
  208. X509_ALGOR_free(alg);
  209. return 0;
  210. }
  211. }
  212. if (!sk_PKCS7_SIGNER_INFO_push(signer_sk, psi))
  213. return 0;
  214. return 1;
  215. }
  216. int PKCS7_add_certificate(PKCS7 *p7, X509 *x509)
  217. {
  218. int i;
  219. STACK_OF(X509) **sk;
  220. i = OBJ_obj2nid(p7->type);
  221. switch (i) {
  222. case NID_pkcs7_signed:
  223. sk = &(p7->d.sign->cert);
  224. break;
  225. case NID_pkcs7_signedAndEnveloped:
  226. sk = &(p7->d.signed_and_enveloped->cert);
  227. break;
  228. default:
  229. PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, PKCS7_R_WRONG_CONTENT_TYPE);
  230. return 0;
  231. }
  232. if (*sk == NULL)
  233. *sk = sk_X509_new_null();
  234. if (*sk == NULL) {
  235. PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  236. return 0;
  237. }
  238. X509_up_ref(x509);
  239. if (!sk_X509_push(*sk, x509)) {
  240. X509_free(x509);
  241. return 0;
  242. }
  243. return 1;
  244. }
  245. int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl)
  246. {
  247. int i;
  248. STACK_OF(X509_CRL) **sk;
  249. i = OBJ_obj2nid(p7->type);
  250. switch (i) {
  251. case NID_pkcs7_signed:
  252. sk = &(p7->d.sign->crl);
  253. break;
  254. case NID_pkcs7_signedAndEnveloped:
  255. sk = &(p7->d.signed_and_enveloped->crl);
  256. break;
  257. default:
  258. PKCS7err(PKCS7_F_PKCS7_ADD_CRL, PKCS7_R_WRONG_CONTENT_TYPE);
  259. return 0;
  260. }
  261. if (*sk == NULL)
  262. *sk = sk_X509_CRL_new_null();
  263. if (*sk == NULL) {
  264. PKCS7err(PKCS7_F_PKCS7_ADD_CRL, ERR_R_MALLOC_FAILURE);
  265. return 0;
  266. }
  267. X509_CRL_up_ref(crl);
  268. if (!sk_X509_CRL_push(*sk, crl)) {
  269. X509_CRL_free(crl);
  270. return 0;
  271. }
  272. return 1;
  273. }
  274. int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
  275. const EVP_MD *dgst)
  276. {
  277. int ret;
  278. /* We now need to add another PKCS7_SIGNER_INFO entry */
  279. if (!ASN1_INTEGER_set(p7i->version, 1))
  280. goto err;
  281. if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
  282. X509_get_issuer_name(x509)))
  283. goto err;
  284. /*
  285. * because ASN1_INTEGER_set is used to set a 'long' we will do things the
  286. * ugly way.
  287. */
  288. ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
  289. if (!(p7i->issuer_and_serial->serial =
  290. ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
  291. goto err;
  292. /* lets keep the pkey around for a while */
  293. EVP_PKEY_up_ref(pkey);
  294. p7i->pkey = pkey;
  295. /* Set the algorithms */
  296. X509_ALGOR_set0(p7i->digest_alg, OBJ_nid2obj(EVP_MD_type(dgst)),
  297. V_ASN1_NULL, NULL);
  298. if (pkey->ameth && pkey->ameth->pkey_ctrl) {
  299. ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_SIGN, 0, p7i);
  300. if (ret > 0)
  301. return 1;
  302. if (ret != -2) {
  303. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET,
  304. PKCS7_R_SIGNING_CTRL_FAILURE);
  305. return 0;
  306. }
  307. }
  308. PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET,
  309. PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  310. err:
  311. return 0;
  312. }
  313. PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey,
  314. const EVP_MD *dgst)
  315. {
  316. PKCS7_SIGNER_INFO *si = NULL;
  317. if (dgst == NULL) {
  318. int def_nid;
  319. if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0)
  320. goto err;
  321. dgst = EVP_get_digestbynid(def_nid);
  322. if (dgst == NULL) {
  323. PKCS7err(PKCS7_F_PKCS7_ADD_SIGNATURE, PKCS7_R_NO_DEFAULT_DIGEST);
  324. goto err;
  325. }
  326. }
  327. if ((si = PKCS7_SIGNER_INFO_new()) == NULL)
  328. goto err;
  329. if (!PKCS7_SIGNER_INFO_set(si, x509, pkey, dgst))
  330. goto err;
  331. if (!PKCS7_add_signer(p7, si))
  332. goto err;
  333. return si;
  334. err:
  335. PKCS7_SIGNER_INFO_free(si);
  336. return NULL;
  337. }
  338. int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md)
  339. {
  340. if (PKCS7_type_is_digest(p7)) {
  341. if ((p7->d.digest->md->parameter = ASN1_TYPE_new()) == NULL) {
  342. PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, ERR_R_MALLOC_FAILURE);
  343. return 0;
  344. }
  345. p7->d.digest->md->parameter->type = V_ASN1_NULL;
  346. p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
  347. return 1;
  348. }
  349. PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, PKCS7_R_WRONG_CONTENT_TYPE);
  350. return 1;
  351. }
  352. STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7)
  353. {
  354. if (p7 == NULL || p7->d.ptr == NULL)
  355. return NULL;
  356. if (PKCS7_type_is_signed(p7)) {
  357. return p7->d.sign->signer_info;
  358. } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
  359. return p7->d.signed_and_enveloped->signer_info;
  360. } else
  361. return NULL;
  362. }
  363. void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk,
  364. X509_ALGOR **pdig, X509_ALGOR **psig)
  365. {
  366. if (pk)
  367. *pk = si->pkey;
  368. if (pdig)
  369. *pdig = si->digest_alg;
  370. if (psig)
  371. *psig = si->digest_enc_alg;
  372. }
  373. void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc)
  374. {
  375. if (penc)
  376. *penc = ri->key_enc_algor;
  377. }
  378. PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509)
  379. {
  380. PKCS7_RECIP_INFO *ri;
  381. if ((ri = PKCS7_RECIP_INFO_new()) == NULL)
  382. goto err;
  383. if (!PKCS7_RECIP_INFO_set(ri, x509))
  384. goto err;
  385. if (!PKCS7_add_recipient_info(p7, ri))
  386. goto err;
  387. return ri;
  388. err:
  389. PKCS7_RECIP_INFO_free(ri);
  390. return NULL;
  391. }
  392. int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri)
  393. {
  394. int i;
  395. STACK_OF(PKCS7_RECIP_INFO) *sk;
  396. i = OBJ_obj2nid(p7->type);
  397. switch (i) {
  398. case NID_pkcs7_signedAndEnveloped:
  399. sk = p7->d.signed_and_enveloped->recipientinfo;
  400. break;
  401. case NID_pkcs7_enveloped:
  402. sk = p7->d.enveloped->recipientinfo;
  403. break;
  404. default:
  405. PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,
  406. PKCS7_R_WRONG_CONTENT_TYPE);
  407. return 0;
  408. }
  409. if (!sk_PKCS7_RECIP_INFO_push(sk, ri))
  410. return 0;
  411. return 1;
  412. }
  413. int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509)
  414. {
  415. int ret;
  416. EVP_PKEY *pkey = NULL;
  417. if (!ASN1_INTEGER_set(p7i->version, 0))
  418. return 0;
  419. if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
  420. X509_get_issuer_name(x509)))
  421. return 0;
  422. ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
  423. if (!(p7i->issuer_and_serial->serial =
  424. ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
  425. return 0;
  426. pkey = X509_get0_pubkey(x509);
  427. if (!pkey || !pkey->ameth || !pkey->ameth->pkey_ctrl) {
  428. PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
  429. PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  430. goto err;
  431. }
  432. ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_ENCRYPT, 0, p7i);
  433. if (ret == -2) {
  434. PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
  435. PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  436. goto err;
  437. }
  438. if (ret <= 0) {
  439. PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET,
  440. PKCS7_R_ENCRYPTION_CTRL_FAILURE);
  441. goto err;
  442. }
  443. X509_up_ref(x509);
  444. p7i->cert = x509;
  445. return 1;
  446. err:
  447. return 0;
  448. }
  449. X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
  450. {
  451. if (PKCS7_type_is_signed(p7))
  452. return (X509_find_by_issuer_and_serial(p7->d.sign->cert,
  453. si->issuer_and_serial->issuer,
  454. si->
  455. issuer_and_serial->serial));
  456. else
  457. return NULL;
  458. }
  459. int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher)
  460. {
  461. int i;
  462. PKCS7_ENC_CONTENT *ec;
  463. i = OBJ_obj2nid(p7->type);
  464. switch (i) {
  465. case NID_pkcs7_signedAndEnveloped:
  466. ec = p7->d.signed_and_enveloped->enc_data;
  467. break;
  468. case NID_pkcs7_enveloped:
  469. ec = p7->d.enveloped->enc_data;
  470. break;
  471. default:
  472. PKCS7err(PKCS7_F_PKCS7_SET_CIPHER, PKCS7_R_WRONG_CONTENT_TYPE);
  473. return 0;
  474. }
  475. /* Check cipher OID exists and has data in it */
  476. i = EVP_CIPHER_type(cipher);
  477. if (i == NID_undef) {
  478. PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,
  479. PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
  480. return 0;
  481. }
  482. ec->cipher = cipher;
  483. return 1;
  484. }
  485. int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7)
  486. {
  487. ASN1_OCTET_STRING *os = NULL;
  488. switch (OBJ_obj2nid(p7->type)) {
  489. case NID_pkcs7_data:
  490. os = p7->d.data;
  491. break;
  492. case NID_pkcs7_signedAndEnveloped:
  493. os = p7->d.signed_and_enveloped->enc_data->enc_data;
  494. if (os == NULL) {
  495. os = ASN1_OCTET_STRING_new();
  496. p7->d.signed_and_enveloped->enc_data->enc_data = os;
  497. }
  498. break;
  499. case NID_pkcs7_enveloped:
  500. os = p7->d.enveloped->enc_data->enc_data;
  501. if (os == NULL) {
  502. os = ASN1_OCTET_STRING_new();
  503. p7->d.enveloped->enc_data->enc_data = os;
  504. }
  505. break;
  506. case NID_pkcs7_signed:
  507. os = p7->d.sign->contents->d.data;
  508. break;
  509. default:
  510. os = NULL;
  511. break;
  512. }
  513. if (os == NULL)
  514. return 0;
  515. os->flags |= ASN1_STRING_FLAG_NDEF;
  516. *boundary = &os->data;
  517. return 1;
  518. }