cms_sd.c 28 KB

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