cms_sd.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. /* crypto/cms/cms_sd.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. */
  53. #include "cryptlib.h"
  54. #include <openssl/asn1t.h>
  55. #include <openssl/pem.h>
  56. #include <openssl/x509v3.h>
  57. #include <openssl/err.h>
  58. #include <openssl/cms.h>
  59. #include "cms_lcl.h"
  60. #include "asn1_locl.h"
  61. /* CMS SignedData Utilities */
  62. DECLARE_ASN1_ITEM(CMS_SignedData)
  63. static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
  64. {
  65. if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed)
  66. {
  67. CMSerr(CMS_F_CMS_GET0_SIGNED, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA);
  68. return NULL;
  69. }
  70. return cms->d.signedData;
  71. }
  72. static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms)
  73. {
  74. if (cms->d.other == NULL)
  75. {
  76. cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
  77. if (!cms->d.signedData)
  78. {
  79. CMSerr(CMS_F_CMS_SIGNED_DATA_INIT, ERR_R_MALLOC_FAILURE);
  80. return NULL;
  81. }
  82. cms->d.signedData->version = 1;
  83. cms->d.signedData->encapContentInfo->eContentType =
  84. OBJ_nid2obj(NID_pkcs7_data);
  85. cms->d.signedData->encapContentInfo->partial = 1;
  86. ASN1_OBJECT_free(cms->contentType);
  87. cms->contentType = OBJ_nid2obj(NID_pkcs7_signed);
  88. return cms->d.signedData;
  89. }
  90. return cms_get0_signed(cms);
  91. }
  92. /* Just initialize SignedData e.g. for certs only structure */
  93. int CMS_SignedData_init(CMS_ContentInfo *cms)
  94. {
  95. if (cms_signed_data_init(cms))
  96. return 1;
  97. else
  98. return 0;
  99. }
  100. /* Check structures and fixup version numbers (if necessary) */
  101. static void cms_sd_set_version(CMS_SignedData *sd)
  102. {
  103. int i;
  104. CMS_CertificateChoices *cch;
  105. CMS_RevocationInfoChoice *rch;
  106. CMS_SignerInfo *si;
  107. for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++)
  108. {
  109. cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
  110. if (cch->type == CMS_CERTCHOICE_OTHER)
  111. {
  112. if (sd->version < 5)
  113. sd->version = 5;
  114. }
  115. else if (cch->type == CMS_CERTCHOICE_V2ACERT)
  116. {
  117. if (sd->version < 4)
  118. sd->version = 4;
  119. }
  120. else if (cch->type == CMS_CERTCHOICE_V1ACERT)
  121. {
  122. if (sd->version < 3)
  123. sd->version = 3;
  124. }
  125. }
  126. for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++)
  127. {
  128. rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
  129. if (rch->type == CMS_REVCHOICE_OTHER)
  130. {
  131. if (sd->version < 5)
  132. sd->version = 5;
  133. }
  134. }
  135. if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
  136. && (sd->version < 3))
  137. sd->version = 3;
  138. for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++)
  139. {
  140. si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
  141. if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
  142. {
  143. if (si->version < 3)
  144. si->version = 3;
  145. if (sd->version < 3)
  146. sd->version = 3;
  147. }
  148. else
  149. sd->version = 1;
  150. }
  151. if (sd->version < 1)
  152. sd->version = 1;
  153. }
  154. /* Copy an existing messageDigest value */
  155. static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
  156. {
  157. STACK_OF(CMS_SignerInfo) *sinfos;
  158. CMS_SignerInfo *sitmp;
  159. int i;
  160. sinfos = CMS_get0_SignerInfos(cms);
  161. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
  162. {
  163. ASN1_OCTET_STRING *messageDigest;
  164. sitmp = sk_CMS_SignerInfo_value(sinfos, i);
  165. if (sitmp == si)
  166. continue;
  167. if (CMS_signed_get_attr_count(sitmp) < 0)
  168. continue;
  169. if (OBJ_cmp(si->digestAlgorithm->algorithm,
  170. sitmp->digestAlgorithm->algorithm))
  171. continue;
  172. messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
  173. OBJ_nid2obj(NID_pkcs9_messageDigest),
  174. -3, V_ASN1_OCTET_STRING);
  175. if (!messageDigest)
  176. {
  177. CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST,
  178. CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
  179. return 0;
  180. }
  181. if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
  182. V_ASN1_OCTET_STRING,
  183. messageDigest, -1))
  184. return 1;
  185. else
  186. return 0;
  187. }
  188. CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, CMS_R_NO_MATCHING_DIGEST);
  189. return 0;
  190. }
  191. int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type)
  192. {
  193. switch(type)
  194. {
  195. case CMS_SIGNERINFO_ISSUER_SERIAL:
  196. sid->d.issuerAndSerialNumber =
  197. M_ASN1_new_of(CMS_IssuerAndSerialNumber);
  198. if (!sid->d.issuerAndSerialNumber)
  199. goto merr;
  200. if (!X509_NAME_set(&sid->d.issuerAndSerialNumber->issuer,
  201. X509_get_issuer_name(cert)))
  202. goto merr;
  203. if (!ASN1_STRING_copy(
  204. sid->d.issuerAndSerialNumber->serialNumber,
  205. X509_get_serialNumber(cert)))
  206. goto merr;
  207. break;
  208. case CMS_SIGNERINFO_KEYIDENTIFIER:
  209. if (!cert->skid)
  210. {
  211. CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER,
  212. CMS_R_CERTIFICATE_HAS_NO_KEYID);
  213. return 0;
  214. }
  215. sid->d.subjectKeyIdentifier = ASN1_STRING_dup(cert->skid);
  216. if (!sid->d.subjectKeyIdentifier)
  217. goto merr;
  218. break;
  219. default:
  220. CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, CMS_R_UNKNOWN_ID);
  221. return 0;
  222. }
  223. sid->type = type;
  224. return 1;
  225. merr:
  226. CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, ERR_R_MALLOC_FAILURE);
  227. return 0;
  228. }
  229. int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
  230. ASN1_OCTET_STRING **keyid,
  231. X509_NAME **issuer, ASN1_INTEGER **sno)
  232. {
  233. if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
  234. {
  235. if (issuer)
  236. *issuer = sid->d.issuerAndSerialNumber->issuer;
  237. if (sno)
  238. *sno = sid->d.issuerAndSerialNumber->serialNumber;
  239. }
  240. else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
  241. {
  242. if (keyid)
  243. *keyid = sid->d.subjectKeyIdentifier;
  244. }
  245. else
  246. return 0;
  247. return 1;
  248. }
  249. int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
  250. {
  251. int ret;
  252. if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
  253. {
  254. ret = X509_NAME_cmp(sid->d.issuerAndSerialNumber->issuer,
  255. X509_get_issuer_name(cert));
  256. if (ret)
  257. return ret;
  258. return ASN1_INTEGER_cmp(sid->d.issuerAndSerialNumber->serialNumber,
  259. X509_get_serialNumber(cert));
  260. }
  261. else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
  262. {
  263. X509_check_purpose(cert, -1, -1);
  264. if (!cert->skid)
  265. return -1;
  266. return ASN1_OCTET_STRING_cmp(sid->d.subjectKeyIdentifier,
  267. cert->skid);
  268. }
  269. else
  270. return -1;
  271. }
  272. CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
  273. X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
  274. unsigned int flags)
  275. {
  276. CMS_SignedData *sd;
  277. CMS_SignerInfo *si = NULL;
  278. X509_ALGOR *alg;
  279. int i, type;
  280. if(!X509_check_private_key(signer, pk))
  281. {
  282. CMSerr(CMS_F_CMS_ADD1_SIGNER,
  283. CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  284. return NULL;
  285. }
  286. sd = cms_signed_data_init(cms);
  287. if (!sd)
  288. goto err;
  289. si = M_ASN1_new_of(CMS_SignerInfo);
  290. if (!si)
  291. goto merr;
  292. X509_check_purpose(signer, -1, -1);
  293. CRYPTO_add(&pk->references, 1, CRYPTO_LOCK_EVP_PKEY);
  294. CRYPTO_add(&signer->references, 1, CRYPTO_LOCK_X509);
  295. si->pkey = pk;
  296. si->signer = signer;
  297. if (flags & CMS_USE_KEYID)
  298. {
  299. si->version = 3;
  300. if (sd->version < 3)
  301. sd->version = 3;
  302. type = CMS_SIGNERINFO_KEYIDENTIFIER;
  303. }
  304. else
  305. {
  306. type = CMS_SIGNERINFO_ISSUER_SERIAL;
  307. si->version = 1;
  308. }
  309. if (!cms_set1_SignerIdentifier(si->sid, signer, type))
  310. goto err;
  311. if (md == NULL)
  312. {
  313. int def_nid;
  314. if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0)
  315. goto err;
  316. md = EVP_get_digestbynid(def_nid);
  317. if (md == NULL)
  318. {
  319. CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DEFAULT_DIGEST);
  320. goto err;
  321. }
  322. }
  323. if (!md)
  324. {
  325. CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DIGEST_SET);
  326. goto err;
  327. }
  328. X509_ALGOR_set_md(si->digestAlgorithm, md);
  329. /* See if digest is present in digestAlgorithms */
  330. for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++)
  331. {
  332. ASN1_OBJECT *aoid;
  333. alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
  334. X509_ALGOR_get0(&aoid, NULL, NULL, alg);
  335. if (OBJ_obj2nid(aoid) == EVP_MD_type(md))
  336. break;
  337. }
  338. if (i == sk_X509_ALGOR_num(sd->digestAlgorithms))
  339. {
  340. alg = X509_ALGOR_new();
  341. if (!alg)
  342. goto merr;
  343. X509_ALGOR_set_md(alg, md);
  344. if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg))
  345. {
  346. X509_ALGOR_free(alg);
  347. goto merr;
  348. }
  349. }
  350. if (pk->ameth && pk->ameth->pkey_ctrl)
  351. {
  352. i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_SIGN,
  353. 0, si);
  354. if (i == -2)
  355. {
  356. CMSerr(CMS_F_CMS_ADD1_SIGNER,
  357. CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  358. goto err;
  359. }
  360. if (i <= 0)
  361. {
  362. CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_CTRL_FAILURE);
  363. goto err;
  364. }
  365. }
  366. if (!(flags & CMS_NOATTR))
  367. {
  368. /* Initialialize signed attributes strutucture so other
  369. * attributes such as signing time etc are added later
  370. * even if we add none here.
  371. */
  372. if (!si->signedAttrs)
  373. {
  374. si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
  375. if (!si->signedAttrs)
  376. goto merr;
  377. }
  378. if (!(flags & CMS_NOSMIMECAP))
  379. {
  380. STACK_OF(X509_ALGOR) *smcap = NULL;
  381. i = CMS_add_standard_smimecap(&smcap);
  382. if (i)
  383. i = CMS_add_smimecap(si, smcap);
  384. sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
  385. if (!i)
  386. goto merr;
  387. }
  388. if (flags & CMS_REUSE_DIGEST)
  389. {
  390. if (!cms_copy_messageDigest(cms, si))
  391. goto err;
  392. if (!(flags & CMS_PARTIAL) &&
  393. !CMS_SignerInfo_sign(si))
  394. goto err;
  395. }
  396. }
  397. if (!(flags & CMS_NOCERTS))
  398. {
  399. /* NB ignore -1 return for duplicate cert */
  400. if (!CMS_add1_cert(cms, signer))
  401. goto merr;
  402. }
  403. if (!sd->signerInfos)
  404. sd->signerInfos = sk_CMS_SignerInfo_new_null();
  405. if (!sd->signerInfos ||
  406. !sk_CMS_SignerInfo_push(sd->signerInfos, si))
  407. goto merr;
  408. return si;
  409. merr:
  410. CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
  411. err:
  412. if (si)
  413. M_ASN1_free_of(si, CMS_SignerInfo);
  414. return NULL;
  415. }
  416. static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
  417. {
  418. ASN1_TIME *tt;
  419. int r = 0;
  420. if (t)
  421. tt = t;
  422. else
  423. tt = X509_gmtime_adj(NULL, 0);
  424. if (!tt)
  425. goto merr;
  426. if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
  427. tt->type, tt, -1) <= 0)
  428. goto merr;
  429. r = 1;
  430. merr:
  431. if (!t)
  432. ASN1_TIME_free(tt);
  433. if (!r)
  434. CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME, ERR_R_MALLOC_FAILURE);
  435. return r;
  436. }
  437. STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
  438. {
  439. CMS_SignedData *sd;
  440. sd = cms_get0_signed(cms);
  441. if (!sd)
  442. return NULL;
  443. return sd->signerInfos;
  444. }
  445. STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
  446. {
  447. STACK_OF(X509) *signers = NULL;
  448. STACK_OF(CMS_SignerInfo) *sinfos;
  449. CMS_SignerInfo *si;
  450. int i;
  451. sinfos = CMS_get0_SignerInfos(cms);
  452. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
  453. {
  454. si = sk_CMS_SignerInfo_value(sinfos, i);
  455. if (si->signer)
  456. {
  457. if (!signers)
  458. {
  459. signers = sk_X509_new_null();
  460. if (!signers)
  461. return NULL;
  462. }
  463. if (!sk_X509_push(signers, si->signer))
  464. {
  465. sk_X509_free(signers);
  466. return NULL;
  467. }
  468. }
  469. }
  470. return signers;
  471. }
  472. void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
  473. {
  474. if (signer)
  475. {
  476. CRYPTO_add(&signer->references, 1, CRYPTO_LOCK_X509);
  477. if (si->pkey)
  478. EVP_PKEY_free(si->pkey);
  479. si->pkey = X509_get_pubkey(signer);
  480. }
  481. if (si->signer)
  482. X509_free(si->signer);
  483. si->signer = signer;
  484. }
  485. int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
  486. ASN1_OCTET_STRING **keyid,
  487. X509_NAME **issuer, ASN1_INTEGER **sno)
  488. {
  489. return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
  490. }
  491. int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
  492. {
  493. return cms_SignerIdentifier_cert_cmp(si->sid, cert);
  494. }
  495. int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
  496. unsigned int flags)
  497. {
  498. CMS_SignedData *sd;
  499. CMS_SignerInfo *si;
  500. CMS_CertificateChoices *cch;
  501. STACK_OF(CMS_CertificateChoices) *certs;
  502. X509 *x;
  503. int i, j;
  504. int ret = 0;
  505. sd = cms_get0_signed(cms);
  506. if (!sd)
  507. return -1;
  508. certs = sd->certificates;
  509. for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++)
  510. {
  511. si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
  512. if (si->signer)
  513. continue;
  514. for (j = 0; j < sk_X509_num(scerts); j++)
  515. {
  516. x = sk_X509_value(scerts, j);
  517. if (CMS_SignerInfo_cert_cmp(si, x) == 0)
  518. {
  519. CMS_SignerInfo_set1_signer_cert(si, x);
  520. ret++;
  521. break;
  522. }
  523. }
  524. if (si->signer || (flags & CMS_NOINTERN))
  525. continue;
  526. for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++)
  527. {
  528. cch = sk_CMS_CertificateChoices_value(certs, j);
  529. if (cch->type != 0)
  530. continue;
  531. x = cch->d.certificate;
  532. if (CMS_SignerInfo_cert_cmp(si, x) == 0)
  533. {
  534. CMS_SignerInfo_set1_signer_cert(si, x);
  535. ret++;
  536. break;
  537. }
  538. }
  539. }
  540. return ret;
  541. }
  542. void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk, X509 **signer,
  543. X509_ALGOR **pdig, X509_ALGOR **psig)
  544. {
  545. if (pk)
  546. *pk = si->pkey;
  547. if (signer)
  548. *signer = si->signer;
  549. if (pdig)
  550. *pdig = si->digestAlgorithm;
  551. if (psig)
  552. *psig = si->signatureAlgorithm;
  553. }
  554. static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
  555. CMS_SignerInfo *si, BIO *chain)
  556. {
  557. EVP_MD_CTX mctx;
  558. int r = 0;
  559. EVP_MD_CTX_init(&mctx);
  560. if (!si->pkey)
  561. {
  562. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
  563. return 0;
  564. }
  565. if (!cms_DigestAlgorithm_find_ctx(&mctx, chain, si->digestAlgorithm))
  566. goto err;
  567. /* If any signed attributes calculate and add messageDigest attribute */
  568. if (CMS_signed_get_attr_count(si) >= 0)
  569. {
  570. ASN1_OBJECT *ctype =
  571. cms->d.signedData->encapContentInfo->eContentType;
  572. unsigned char md[EVP_MAX_MD_SIZE];
  573. unsigned int mdlen;
  574. if (!EVP_DigestFinal_ex(&mctx, md, &mdlen))
  575. goto err;
  576. if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
  577. V_ASN1_OCTET_STRING,
  578. md, mdlen))
  579. goto err;
  580. /* Copy content type across */
  581. if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
  582. V_ASN1_OBJECT, ctype, -1) <= 0)
  583. goto err;
  584. if (!CMS_SignerInfo_sign(si))
  585. goto err;
  586. }
  587. else
  588. {
  589. unsigned char *sig;
  590. unsigned int siglen;
  591. sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
  592. if (!sig)
  593. {
  594. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN,
  595. ERR_R_MALLOC_FAILURE);
  596. goto err;
  597. }
  598. if (!EVP_SignFinal(&mctx, sig, &siglen, si->pkey))
  599. {
  600. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN,
  601. CMS_R_SIGNFINAL_ERROR);
  602. OPENSSL_free(sig);
  603. goto err;
  604. }
  605. ASN1_STRING_set0(si->signature, sig, siglen);
  606. }
  607. r = 1;
  608. err:
  609. EVP_MD_CTX_cleanup(&mctx);
  610. return r;
  611. }
  612. int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)
  613. {
  614. STACK_OF(CMS_SignerInfo) *sinfos;
  615. CMS_SignerInfo *si;
  616. int i;
  617. sinfos = CMS_get0_SignerInfos(cms);
  618. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
  619. {
  620. si = sk_CMS_SignerInfo_value(sinfos, i);
  621. if (!cms_SignerInfo_content_sign(cms, si, chain))
  622. return 0;
  623. }
  624. cms->d.signedData->encapContentInfo->partial = 0;
  625. return 1;
  626. }
  627. int CMS_SignerInfo_sign(CMS_SignerInfo *si)
  628. {
  629. EVP_MD_CTX mctx;
  630. EVP_PKEY_CTX *pctx;
  631. unsigned char *abuf = NULL;
  632. int alen;
  633. size_t siglen;
  634. const EVP_MD *md = NULL;
  635. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  636. if (md == NULL)
  637. return 0;
  638. EVP_MD_CTX_init(&mctx);
  639. if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0)
  640. {
  641. if (!cms_add1_signingTime(si, NULL))
  642. goto err;
  643. }
  644. if (EVP_DigestSignInit(&mctx, &pctx, md, NULL, si->pkey) <= 0)
  645. goto err;
  646. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  647. EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0)
  648. {
  649. CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
  650. goto err;
  651. }
  652. alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs,&abuf,
  653. ASN1_ITEM_rptr(CMS_Attributes_Sign));
  654. if(!abuf)
  655. goto err;
  656. if (EVP_DigestSignUpdate(&mctx, abuf, alen) <= 0)
  657. goto err;
  658. if (EVP_DigestSignFinal(&mctx, NULL, &siglen) <= 0)
  659. goto err;
  660. OPENSSL_free(abuf);
  661. abuf = OPENSSL_malloc(siglen);
  662. if(!abuf)
  663. goto err;
  664. if (EVP_DigestSignFinal(&mctx, abuf, &siglen) <= 0)
  665. goto err;
  666. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  667. EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0)
  668. {
  669. CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
  670. goto err;
  671. }
  672. EVP_MD_CTX_cleanup(&mctx);
  673. ASN1_STRING_set0(si->signature, abuf, siglen);
  674. return 1;
  675. err:
  676. if (abuf)
  677. OPENSSL_free(abuf);
  678. EVP_MD_CTX_cleanup(&mctx);
  679. return 0;
  680. }
  681. int CMS_SignerInfo_verify(CMS_SignerInfo *si)
  682. {
  683. EVP_MD_CTX mctx;
  684. EVP_PKEY_CTX *pctx;
  685. unsigned char *abuf = NULL;
  686. int alen, r = -1;
  687. const EVP_MD *md = NULL;
  688. if (!si->pkey)
  689. {
  690. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);
  691. return -1;
  692. }
  693. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  694. if (md == NULL)
  695. return -1;
  696. EVP_MD_CTX_init(&mctx);
  697. if (EVP_DigestVerifyInit(&mctx, &pctx, md, NULL, si->pkey) <= 0)
  698. goto err;
  699. alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs,&abuf,
  700. ASN1_ITEM_rptr(CMS_Attributes_Verify));
  701. if(!abuf)
  702. goto err;
  703. r = EVP_DigestVerifyUpdate(&mctx, abuf, alen);
  704. OPENSSL_free(abuf);
  705. if (r <= 0)
  706. {
  707. r = -1;
  708. goto err;
  709. }
  710. r = EVP_DigestVerifyFinal(&mctx,
  711. si->signature->data, si->signature->length);
  712. if (r <= 0)
  713. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
  714. err:
  715. EVP_MD_CTX_cleanup(&mctx);
  716. return r;
  717. }
  718. /* Create a chain of digest BIOs from a CMS ContentInfo */
  719. BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)
  720. {
  721. int i;
  722. CMS_SignedData *sd;
  723. BIO *chain = NULL;
  724. sd = cms_get0_signed(cms);
  725. if (!sd)
  726. return NULL;
  727. if (cms->d.signedData->encapContentInfo->partial)
  728. cms_sd_set_version(sd);
  729. for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++)
  730. {
  731. X509_ALGOR *digestAlgorithm;
  732. BIO *mdbio;
  733. digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
  734. mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm);
  735. if (!mdbio)
  736. goto err;
  737. if (chain)
  738. BIO_push(chain, mdbio);
  739. else
  740. chain = mdbio;
  741. }
  742. return chain;
  743. err:
  744. if (chain)
  745. BIO_free_all(chain);
  746. return NULL;
  747. }
  748. int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
  749. {
  750. ASN1_OCTET_STRING *os = NULL;
  751. EVP_MD_CTX mctx;
  752. int r = -1;
  753. EVP_MD_CTX_init(&mctx);
  754. /* If we have any signed attributes look for messageDigest value */
  755. if (CMS_signed_get_attr_count(si) >= 0)
  756. {
  757. os = CMS_signed_get0_data_by_OBJ(si,
  758. OBJ_nid2obj(NID_pkcs9_messageDigest),
  759. -3, V_ASN1_OCTET_STRING);
  760. if (!os)
  761. {
  762. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  763. CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
  764. goto err;
  765. }
  766. }
  767. if (!cms_DigestAlgorithm_find_ctx(&mctx, chain, si->digestAlgorithm))
  768. goto err;
  769. /* If messageDigest found compare it */
  770. if (os)
  771. {
  772. unsigned char mval[EVP_MAX_MD_SIZE];
  773. unsigned int mlen;
  774. if (EVP_DigestFinal_ex(&mctx, mval, &mlen) <= 0)
  775. {
  776. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  777. CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
  778. goto err;
  779. }
  780. if (mlen != (unsigned int)os->length)
  781. {
  782. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  783. CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
  784. goto err;
  785. }
  786. if (memcmp(mval, os->data, mlen))
  787. {
  788. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  789. CMS_R_VERIFICATION_FAILURE);
  790. r = 0;
  791. }
  792. else
  793. r = 1;
  794. }
  795. else
  796. {
  797. r = EVP_VerifyFinal(&mctx, si->signature->data,
  798. si->signature->length, si->pkey);
  799. if (r <= 0)
  800. {
  801. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  802. CMS_R_VERIFICATION_FAILURE);
  803. r = 0;
  804. }
  805. }
  806. err:
  807. EVP_MD_CTX_cleanup(&mctx);
  808. return r;
  809. }
  810. int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
  811. {
  812. unsigned char *smder = NULL;
  813. int smderlen, r;
  814. smderlen = i2d_X509_ALGORS(algs, &smder);
  815. if (smderlen <= 0)
  816. return 0;
  817. r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
  818. V_ASN1_SEQUENCE, smder, smderlen);
  819. OPENSSL_free(smder);
  820. return r;
  821. }
  822. int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
  823. int algnid, int keysize)
  824. {
  825. X509_ALGOR *alg;
  826. ASN1_INTEGER *key = NULL;
  827. if (keysize > 0)
  828. {
  829. key = ASN1_INTEGER_new();
  830. if (!key || !ASN1_INTEGER_set(key, keysize))
  831. return 0;
  832. }
  833. alg = X509_ALGOR_new();
  834. if (!alg)
  835. {
  836. if (key)
  837. ASN1_INTEGER_free(key);
  838. return 0;
  839. }
  840. X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
  841. key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
  842. if (!*algs)
  843. *algs = sk_X509_ALGOR_new_null();
  844. if (!*algs || !sk_X509_ALGOR_push(*algs, alg))
  845. {
  846. X509_ALGOR_free(alg);
  847. return 0;
  848. }
  849. return 1;
  850. }
  851. /* Check to see if a cipher exists and if so add S/MIME capabilities */
  852. static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
  853. {
  854. if (EVP_get_cipherbynid(nid))
  855. return CMS_add_simple_smimecap(sk, nid, arg);
  856. return 1;
  857. }
  858. static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
  859. {
  860. if (EVP_get_digestbynid(nid))
  861. return CMS_add_simple_smimecap(sk, nid, arg);
  862. return 1;
  863. }
  864. int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
  865. {
  866. if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
  867. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
  868. || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
  869. || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
  870. || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
  871. || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
  872. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
  873. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
  874. || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
  875. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))
  876. return 0;
  877. return 1;
  878. }