cms_sd.c 28 KB

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