cms_sd.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /* crypto/cms/cms_sd.c */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) 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. * [email protected].
  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/x509v3.h>
  58. #include <openssl/err.h>
  59. #include <openssl/cms.h>
  60. #include "cms_lcl.h"
  61. #include "asn1_locl.h"
  62. /* CMS SignedData Utilities */
  63. DECLARE_ASN1_ITEM(CMS_SignedData)
  64. static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
  65. {
  66. if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed) {
  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. cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
  76. if (!cms->d.signedData) {
  77. CMSerr(CMS_F_CMS_SIGNED_DATA_INIT, ERR_R_MALLOC_FAILURE);
  78. return NULL;
  79. }
  80. cms->d.signedData->version = 1;
  81. cms->d.signedData->encapContentInfo->eContentType =
  82. OBJ_nid2obj(NID_pkcs7_data);
  83. cms->d.signedData->encapContentInfo->partial = 1;
  84. ASN1_OBJECT_free(cms->contentType);
  85. cms->contentType = OBJ_nid2obj(NID_pkcs7_signed);
  86. return cms->d.signedData;
  87. }
  88. return cms_get0_signed(cms);
  89. }
  90. /* Just initialize SignedData e.g. for certs only structure */
  91. int CMS_SignedData_init(CMS_ContentInfo *cms)
  92. {
  93. if (cms_signed_data_init(cms))
  94. return 1;
  95. else
  96. return 0;
  97. }
  98. /* Check structures and fixup version numbers (if necessary) */
  99. static void cms_sd_set_version(CMS_SignedData *sd)
  100. {
  101. int i;
  102. CMS_CertificateChoices *cch;
  103. CMS_RevocationInfoChoice *rch;
  104. CMS_SignerInfo *si;
  105. for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++) {
  106. cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
  107. if (cch->type == CMS_CERTCHOICE_OTHER) {
  108. if (sd->version < 5)
  109. sd->version = 5;
  110. } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
  111. if (sd->version < 4)
  112. sd->version = 4;
  113. } else if (cch->type == CMS_CERTCHOICE_V1ACERT) {
  114. if (sd->version < 3)
  115. sd->version = 3;
  116. }
  117. }
  118. for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++) {
  119. rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
  120. if (rch->type == CMS_REVCHOICE_OTHER) {
  121. if (sd->version < 5)
  122. sd->version = 5;
  123. }
  124. }
  125. if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
  126. && (sd->version < 3))
  127. sd->version = 3;
  128. for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
  129. si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
  130. if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
  131. if (si->version < 3)
  132. si->version = 3;
  133. if (sd->version < 3)
  134. sd->version = 3;
  135. } else if (si->version < 1)
  136. si->version = 1;
  137. }
  138. if (sd->version < 1)
  139. sd->version = 1;
  140. }
  141. /* Copy an existing messageDigest value */
  142. static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
  143. {
  144. STACK_OF(CMS_SignerInfo) *sinfos;
  145. CMS_SignerInfo *sitmp;
  146. int i;
  147. sinfos = CMS_get0_SignerInfos(cms);
  148. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  149. ASN1_OCTET_STRING *messageDigest;
  150. sitmp = sk_CMS_SignerInfo_value(sinfos, i);
  151. if (sitmp == si)
  152. continue;
  153. if (CMS_signed_get_attr_count(sitmp) < 0)
  154. continue;
  155. if (OBJ_cmp(si->digestAlgorithm->algorithm,
  156. sitmp->digestAlgorithm->algorithm))
  157. continue;
  158. messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
  159. OBJ_nid2obj
  160. (NID_pkcs9_messageDigest),
  161. -3, V_ASN1_OCTET_STRING);
  162. if (!messageDigest) {
  163. CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST,
  164. CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
  165. return 0;
  166. }
  167. if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
  168. V_ASN1_OCTET_STRING,
  169. messageDigest, -1))
  170. return 1;
  171. else
  172. return 0;
  173. }
  174. CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, CMS_R_NO_MATCHING_DIGEST);
  175. return 0;
  176. }
  177. int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type)
  178. {
  179. switch (type) {
  180. case CMS_SIGNERINFO_ISSUER_SERIAL:
  181. sid->d.issuerAndSerialNumber =
  182. M_ASN1_new_of(CMS_IssuerAndSerialNumber);
  183. if (!sid->d.issuerAndSerialNumber)
  184. goto merr;
  185. if (!X509_NAME_set(&sid->d.issuerAndSerialNumber->issuer,
  186. X509_get_issuer_name(cert)))
  187. goto merr;
  188. if (!ASN1_STRING_copy(sid->d.issuerAndSerialNumber->serialNumber,
  189. X509_get_serialNumber(cert)))
  190. goto merr;
  191. break;
  192. case CMS_SIGNERINFO_KEYIDENTIFIER:
  193. if (!cert->skid) {
  194. CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER,
  195. CMS_R_CERTIFICATE_HAS_NO_KEYID);
  196. return 0;
  197. }
  198. sid->d.subjectKeyIdentifier = ASN1_STRING_dup(cert->skid);
  199. if (!sid->d.subjectKeyIdentifier)
  200. goto merr;
  201. break;
  202. default:
  203. CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, CMS_R_UNKNOWN_ID);
  204. return 0;
  205. }
  206. sid->type = type;
  207. return 1;
  208. merr:
  209. CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, ERR_R_MALLOC_FAILURE);
  210. return 0;
  211. }
  212. int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
  213. ASN1_OCTET_STRING **keyid,
  214. X509_NAME **issuer,
  215. ASN1_INTEGER **sno)
  216. {
  217. if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) {
  218. if (issuer)
  219. *issuer = sid->d.issuerAndSerialNumber->issuer;
  220. if (sno)
  221. *sno = sid->d.issuerAndSerialNumber->serialNumber;
  222. } else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
  223. if (keyid)
  224. *keyid = sid->d.subjectKeyIdentifier;
  225. } else
  226. return 0;
  227. return 1;
  228. }
  229. int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
  230. {
  231. int ret;
  232. if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) {
  233. ret = X509_NAME_cmp(sid->d.issuerAndSerialNumber->issuer,
  234. X509_get_issuer_name(cert));
  235. if (ret)
  236. return ret;
  237. return ASN1_INTEGER_cmp(sid->d.issuerAndSerialNumber->serialNumber,
  238. X509_get_serialNumber(cert));
  239. } else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
  240. X509_check_purpose(cert, -1, -1);
  241. if (!cert->skid)
  242. return -1;
  243. return ASN1_OCTET_STRING_cmp(sid->d.subjectKeyIdentifier, cert->skid);
  244. } else
  245. return -1;
  246. }
  247. CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
  248. X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
  249. unsigned int flags)
  250. {
  251. CMS_SignedData *sd;
  252. CMS_SignerInfo *si = NULL;
  253. X509_ALGOR *alg;
  254. int i, type;
  255. if (!X509_check_private_key(signer, pk)) {
  256. CMSerr(CMS_F_CMS_ADD1_SIGNER,
  257. CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  258. return NULL;
  259. }
  260. sd = cms_signed_data_init(cms);
  261. if (!sd)
  262. goto err;
  263. si = M_ASN1_new_of(CMS_SignerInfo);
  264. if (!si)
  265. goto merr;
  266. X509_check_purpose(signer, -1, -1);
  267. CRYPTO_add(&pk->references, 1, CRYPTO_LOCK_EVP_PKEY);
  268. CRYPTO_add(&signer->references, 1, CRYPTO_LOCK_X509);
  269. si->pkey = pk;
  270. si->signer = signer;
  271. if (flags & CMS_USE_KEYID) {
  272. si->version = 3;
  273. if (sd->version < 3)
  274. sd->version = 3;
  275. type = CMS_SIGNERINFO_KEYIDENTIFIER;
  276. } else {
  277. type = CMS_SIGNERINFO_ISSUER_SERIAL;
  278. si->version = 1;
  279. }
  280. if (!cms_set1_SignerIdentifier(si->sid, signer, type))
  281. goto err;
  282. if (md == NULL) {
  283. int def_nid;
  284. if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0)
  285. goto err;
  286. md = EVP_get_digestbynid(def_nid);
  287. if (md == NULL) {
  288. CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DEFAULT_DIGEST);
  289. goto err;
  290. }
  291. }
  292. if (!md) {
  293. CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DIGEST_SET);
  294. goto err;
  295. }
  296. cms_DigestAlgorithm_set(si->digestAlgorithm, md);
  297. /* See if digest is present in digestAlgorithms */
  298. for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
  299. ASN1_OBJECT *aoid;
  300. alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
  301. X509_ALGOR_get0(&aoid, NULL, NULL, alg);
  302. if (OBJ_obj2nid(aoid) == EVP_MD_type(md))
  303. break;
  304. }
  305. if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) {
  306. alg = X509_ALGOR_new();
  307. if (!alg)
  308. goto merr;
  309. cms_DigestAlgorithm_set(alg, md);
  310. if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) {
  311. X509_ALGOR_free(alg);
  312. goto merr;
  313. }
  314. }
  315. if (pk->ameth && pk->ameth->pkey_ctrl) {
  316. i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_SIGN, 0, si);
  317. if (i == -2) {
  318. CMSerr(CMS_F_CMS_ADD1_SIGNER,
  319. CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  320. goto err;
  321. }
  322. if (i <= 0) {
  323. CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_CTRL_FAILURE);
  324. goto err;
  325. }
  326. }
  327. if (!(flags & CMS_NOATTR)) {
  328. /*
  329. * Initialialize signed attributes strutucture so other attributes
  330. * such as signing time etc are added later even if we add none here.
  331. */
  332. if (!si->signedAttrs) {
  333. si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
  334. if (!si->signedAttrs)
  335. goto merr;
  336. }
  337. if (!(flags & CMS_NOSMIMECAP)) {
  338. STACK_OF(X509_ALGOR) *smcap = NULL;
  339. i = CMS_add_standard_smimecap(&smcap);
  340. if (i)
  341. i = CMS_add_smimecap(si, smcap);
  342. sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
  343. if (!i)
  344. goto merr;
  345. }
  346. if (flags & CMS_REUSE_DIGEST) {
  347. if (!cms_copy_messageDigest(cms, si))
  348. goto err;
  349. if (!(flags & CMS_PARTIAL) && !CMS_SignerInfo_sign(si))
  350. goto err;
  351. }
  352. }
  353. if (!(flags & CMS_NOCERTS)) {
  354. /* NB ignore -1 return for duplicate cert */
  355. if (!CMS_add1_cert(cms, signer))
  356. goto merr;
  357. }
  358. if (!sd->signerInfos)
  359. sd->signerInfos = sk_CMS_SignerInfo_new_null();
  360. if (!sd->signerInfos || !sk_CMS_SignerInfo_push(sd->signerInfos, si))
  361. goto merr;
  362. return si;
  363. merr:
  364. CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
  365. err:
  366. if (si)
  367. M_ASN1_free_of(si, CMS_SignerInfo);
  368. return NULL;
  369. }
  370. static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
  371. {
  372. ASN1_TIME *tt;
  373. int r = 0;
  374. if (t)
  375. tt = t;
  376. else
  377. tt = X509_gmtime_adj(NULL, 0);
  378. if (!tt)
  379. goto merr;
  380. if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
  381. tt->type, tt, -1) <= 0)
  382. goto merr;
  383. r = 1;
  384. merr:
  385. if (!t)
  386. ASN1_TIME_free(tt);
  387. if (!r)
  388. CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME, ERR_R_MALLOC_FAILURE);
  389. return r;
  390. }
  391. STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
  392. {
  393. CMS_SignedData *sd;
  394. sd = cms_get0_signed(cms);
  395. if (!sd)
  396. return NULL;
  397. return sd->signerInfos;
  398. }
  399. STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
  400. {
  401. STACK_OF(X509) *signers = NULL;
  402. STACK_OF(CMS_SignerInfo) *sinfos;
  403. CMS_SignerInfo *si;
  404. int i;
  405. sinfos = CMS_get0_SignerInfos(cms);
  406. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  407. si = sk_CMS_SignerInfo_value(sinfos, i);
  408. if (si->signer) {
  409. if (!signers) {
  410. signers = sk_X509_new_null();
  411. if (!signers)
  412. return NULL;
  413. }
  414. if (!sk_X509_push(signers, si->signer)) {
  415. sk_X509_free(signers);
  416. return NULL;
  417. }
  418. }
  419. }
  420. return signers;
  421. }
  422. void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
  423. {
  424. if (signer) {
  425. CRYPTO_add(&signer->references, 1, CRYPTO_LOCK_X509);
  426. if (si->pkey)
  427. EVP_PKEY_free(si->pkey);
  428. si->pkey = X509_get_pubkey(signer);
  429. }
  430. if (si->signer)
  431. X509_free(si->signer);
  432. si->signer = signer;
  433. }
  434. int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
  435. ASN1_OCTET_STRING **keyid,
  436. X509_NAME **issuer, ASN1_INTEGER **sno)
  437. {
  438. return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
  439. }
  440. int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
  441. {
  442. return cms_SignerIdentifier_cert_cmp(si->sid, cert);
  443. }
  444. int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
  445. unsigned int flags)
  446. {
  447. CMS_SignedData *sd;
  448. CMS_SignerInfo *si;
  449. CMS_CertificateChoices *cch;
  450. STACK_OF(CMS_CertificateChoices) *certs;
  451. X509 *x;
  452. int i, j;
  453. int ret = 0;
  454. sd = cms_get0_signed(cms);
  455. if (!sd)
  456. return -1;
  457. certs = sd->certificates;
  458. for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
  459. si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
  460. if (si->signer)
  461. continue;
  462. for (j = 0; j < sk_X509_num(scerts); j++) {
  463. x = sk_X509_value(scerts, j);
  464. if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
  465. CMS_SignerInfo_set1_signer_cert(si, x);
  466. ret++;
  467. break;
  468. }
  469. }
  470. if (si->signer || (flags & CMS_NOINTERN))
  471. continue;
  472. for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) {
  473. cch = sk_CMS_CertificateChoices_value(certs, j);
  474. if (cch->type != 0)
  475. continue;
  476. x = cch->d.certificate;
  477. if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
  478. CMS_SignerInfo_set1_signer_cert(si, x);
  479. ret++;
  480. break;
  481. }
  482. }
  483. }
  484. return ret;
  485. }
  486. void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
  487. X509 **signer, X509_ALGOR **pdig,
  488. X509_ALGOR **psig)
  489. {
  490. if (pk)
  491. *pk = si->pkey;
  492. if (signer)
  493. *signer = si->signer;
  494. if (pdig)
  495. *pdig = si->digestAlgorithm;
  496. if (psig)
  497. *psig = si->signatureAlgorithm;
  498. }
  499. static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
  500. CMS_SignerInfo *si, BIO *chain)
  501. {
  502. EVP_MD_CTX mctx;
  503. int r = 0;
  504. EVP_MD_CTX_init(&mctx);
  505. if (!si->pkey) {
  506. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
  507. return 0;
  508. }
  509. if (!cms_DigestAlgorithm_find_ctx(&mctx, chain, si->digestAlgorithm))
  510. goto err;
  511. /*
  512. * If any signed attributes calculate and add messageDigest attribute
  513. */
  514. if (CMS_signed_get_attr_count(si) >= 0) {
  515. ASN1_OBJECT *ctype =
  516. cms->d.signedData->encapContentInfo->eContentType;
  517. unsigned char md[EVP_MAX_MD_SIZE];
  518. unsigned int mdlen;
  519. if (!EVP_DigestFinal_ex(&mctx, md, &mdlen))
  520. goto err;
  521. if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
  522. V_ASN1_OCTET_STRING, md, mdlen))
  523. goto err;
  524. /* Copy content type across */
  525. if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
  526. V_ASN1_OBJECT, ctype, -1) <= 0)
  527. goto err;
  528. if (!CMS_SignerInfo_sign(si))
  529. goto err;
  530. } else {
  531. unsigned char *sig;
  532. unsigned int siglen;
  533. sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
  534. if (!sig) {
  535. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
  536. goto err;
  537. }
  538. if (!EVP_SignFinal(&mctx, sig, &siglen, si->pkey)) {
  539. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_SIGNFINAL_ERROR);
  540. OPENSSL_free(sig);
  541. goto err;
  542. }
  543. ASN1_STRING_set0(si->signature, sig, siglen);
  544. }
  545. r = 1;
  546. err:
  547. EVP_MD_CTX_cleanup(&mctx);
  548. return r;
  549. }
  550. int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)
  551. {
  552. STACK_OF(CMS_SignerInfo) *sinfos;
  553. CMS_SignerInfo *si;
  554. int i;
  555. sinfos = CMS_get0_SignerInfos(cms);
  556. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  557. si = sk_CMS_SignerInfo_value(sinfos, i);
  558. if (!cms_SignerInfo_content_sign(cms, si, chain))
  559. return 0;
  560. }
  561. cms->d.signedData->encapContentInfo->partial = 0;
  562. return 1;
  563. }
  564. int CMS_SignerInfo_sign(CMS_SignerInfo *si)
  565. {
  566. EVP_MD_CTX mctx;
  567. EVP_PKEY_CTX *pctx;
  568. unsigned char *abuf = NULL;
  569. int alen;
  570. size_t siglen;
  571. const EVP_MD *md = NULL;
  572. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  573. if (md == NULL)
  574. return 0;
  575. EVP_MD_CTX_init(&mctx);
  576. if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
  577. if (!cms_add1_signingTime(si, NULL))
  578. goto err;
  579. }
  580. if (EVP_DigestSignInit(&mctx, &pctx, md, NULL, si->pkey) <= 0)
  581. goto err;
  582. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  583. EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0) {
  584. CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
  585. goto err;
  586. }
  587. alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
  588. ASN1_ITEM_rptr(CMS_Attributes_Sign));
  589. if (!abuf)
  590. goto err;
  591. if (EVP_DigestSignUpdate(&mctx, abuf, alen) <= 0)
  592. goto err;
  593. if (EVP_DigestSignFinal(&mctx, NULL, &siglen) <= 0)
  594. goto err;
  595. OPENSSL_free(abuf);
  596. abuf = OPENSSL_malloc(siglen);
  597. if (!abuf)
  598. goto err;
  599. if (EVP_DigestSignFinal(&mctx, abuf, &siglen) <= 0)
  600. goto err;
  601. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  602. EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0) {
  603. CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
  604. goto err;
  605. }
  606. EVP_MD_CTX_cleanup(&mctx);
  607. ASN1_STRING_set0(si->signature, abuf, siglen);
  608. return 1;
  609. err:
  610. if (abuf)
  611. OPENSSL_free(abuf);
  612. EVP_MD_CTX_cleanup(&mctx);
  613. return 0;
  614. }
  615. int CMS_SignerInfo_verify(CMS_SignerInfo *si)
  616. {
  617. EVP_MD_CTX mctx;
  618. EVP_PKEY_CTX *pctx;
  619. unsigned char *abuf = NULL;
  620. int alen, r = -1;
  621. const EVP_MD *md = NULL;
  622. if (!si->pkey) {
  623. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);
  624. return -1;
  625. }
  626. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  627. if (md == NULL)
  628. return -1;
  629. EVP_MD_CTX_init(&mctx);
  630. if (EVP_DigestVerifyInit(&mctx, &pctx, md, NULL, si->pkey) <= 0)
  631. goto err;
  632. alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
  633. ASN1_ITEM_rptr(CMS_Attributes_Verify));
  634. if (!abuf)
  635. goto err;
  636. r = EVP_DigestVerifyUpdate(&mctx, abuf, alen);
  637. OPENSSL_free(abuf);
  638. if (r <= 0) {
  639. r = -1;
  640. goto err;
  641. }
  642. r = EVP_DigestVerifyFinal(&mctx,
  643. si->signature->data, si->signature->length);
  644. if (r <= 0)
  645. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
  646. err:
  647. EVP_MD_CTX_cleanup(&mctx);
  648. return r;
  649. }
  650. /* Create a chain of digest BIOs from a CMS ContentInfo */
  651. BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)
  652. {
  653. int i;
  654. CMS_SignedData *sd;
  655. BIO *chain = NULL;
  656. sd = cms_get0_signed(cms);
  657. if (!sd)
  658. return NULL;
  659. if (cms->d.signedData->encapContentInfo->partial)
  660. cms_sd_set_version(sd);
  661. for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
  662. X509_ALGOR *digestAlgorithm;
  663. BIO *mdbio;
  664. digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
  665. mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm);
  666. if (!mdbio)
  667. goto err;
  668. if (chain)
  669. BIO_push(chain, mdbio);
  670. else
  671. chain = mdbio;
  672. }
  673. return chain;
  674. err:
  675. if (chain)
  676. BIO_free_all(chain);
  677. return NULL;
  678. }
  679. int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
  680. {
  681. ASN1_OCTET_STRING *os = NULL;
  682. EVP_MD_CTX mctx;
  683. int r = -1;
  684. EVP_MD_CTX_init(&mctx);
  685. /* If we have any signed attributes look for messageDigest value */
  686. if (CMS_signed_get_attr_count(si) >= 0) {
  687. os = CMS_signed_get0_data_by_OBJ(si,
  688. OBJ_nid2obj(NID_pkcs9_messageDigest),
  689. -3, V_ASN1_OCTET_STRING);
  690. if (!os) {
  691. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  692. CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
  693. goto err;
  694. }
  695. }
  696. if (!cms_DigestAlgorithm_find_ctx(&mctx, chain, si->digestAlgorithm))
  697. goto err;
  698. /* If messageDigest found compare it */
  699. if (os) {
  700. unsigned char mval[EVP_MAX_MD_SIZE];
  701. unsigned int mlen;
  702. if (EVP_DigestFinal_ex(&mctx, mval, &mlen) <= 0) {
  703. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  704. CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
  705. goto err;
  706. }
  707. if (mlen != (unsigned int)os->length) {
  708. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  709. CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
  710. goto err;
  711. }
  712. if (memcmp(mval, os->data, mlen)) {
  713. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  714. CMS_R_VERIFICATION_FAILURE);
  715. r = 0;
  716. } else
  717. r = 1;
  718. } else {
  719. r = EVP_VerifyFinal(&mctx, si->signature->data,
  720. si->signature->length, si->pkey);
  721. if (r <= 0) {
  722. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  723. CMS_R_VERIFICATION_FAILURE);
  724. r = 0;
  725. }
  726. }
  727. err:
  728. EVP_MD_CTX_cleanup(&mctx);
  729. return r;
  730. }
  731. int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
  732. {
  733. unsigned char *smder = NULL;
  734. int smderlen, r;
  735. smderlen = i2d_X509_ALGORS(algs, &smder);
  736. if (smderlen <= 0)
  737. return 0;
  738. r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
  739. V_ASN1_SEQUENCE, smder, smderlen);
  740. OPENSSL_free(smder);
  741. return r;
  742. }
  743. int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
  744. int algnid, int keysize)
  745. {
  746. X509_ALGOR *alg;
  747. ASN1_INTEGER *key = NULL;
  748. if (keysize > 0) {
  749. key = ASN1_INTEGER_new();
  750. if (!key || !ASN1_INTEGER_set(key, keysize))
  751. return 0;
  752. }
  753. alg = X509_ALGOR_new();
  754. if (!alg) {
  755. if (key)
  756. ASN1_INTEGER_free(key);
  757. return 0;
  758. }
  759. X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
  760. key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
  761. if (!*algs)
  762. *algs = sk_X509_ALGOR_new_null();
  763. if (!*algs || !sk_X509_ALGOR_push(*algs, alg)) {
  764. X509_ALGOR_free(alg);
  765. return 0;
  766. }
  767. return 1;
  768. }
  769. /* Check to see if a cipher exists and if so add S/MIME capabilities */
  770. static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
  771. {
  772. if (EVP_get_cipherbynid(nid))
  773. return CMS_add_simple_smimecap(sk, nid, arg);
  774. return 1;
  775. }
  776. static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
  777. {
  778. if (EVP_get_digestbynid(nid))
  779. return CMS_add_simple_smimecap(sk, nid, arg);
  780. return 1;
  781. }
  782. int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
  783. {
  784. if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
  785. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
  786. || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
  787. || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
  788. || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
  789. || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
  790. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
  791. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
  792. || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
  793. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))
  794. return 0;
  795. return 1;
  796. }