cms_sd.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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 <openssl/ess.h>
  17. #include "cms_local.h"
  18. #include "crypto/asn1.h"
  19. #include "crypto/evp.h"
  20. #include "crypto/cms.h"
  21. #include "crypto/ess.h"
  22. /* CMS SignedData Utilities */
  23. static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
  24. {
  25. if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed) {
  26. CMSerr(CMS_F_CMS_GET0_SIGNED, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA);
  27. return NULL;
  28. }
  29. return cms->d.signedData;
  30. }
  31. static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms)
  32. {
  33. if (cms->d.other == NULL) {
  34. cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
  35. if (!cms->d.signedData) {
  36. CMSerr(CMS_F_CMS_SIGNED_DATA_INIT, ERR_R_MALLOC_FAILURE);
  37. return NULL;
  38. }
  39. cms->d.signedData->version = 1;
  40. cms->d.signedData->encapContentInfo->eContentType =
  41. OBJ_nid2obj(NID_pkcs7_data);
  42. cms->d.signedData->encapContentInfo->partial = 1;
  43. ASN1_OBJECT_free(cms->contentType);
  44. cms->contentType = OBJ_nid2obj(NID_pkcs7_signed);
  45. return cms->d.signedData;
  46. }
  47. return cms_get0_signed(cms);
  48. }
  49. /* Just initialise SignedData e.g. for certs only structure */
  50. int CMS_SignedData_init(CMS_ContentInfo *cms)
  51. {
  52. if (cms_signed_data_init(cms))
  53. return 1;
  54. else
  55. return 0;
  56. }
  57. /* Check structures and fixup version numbers (if necessary) */
  58. static void cms_sd_set_version(CMS_SignedData *sd)
  59. {
  60. int i;
  61. CMS_CertificateChoices *cch;
  62. CMS_RevocationInfoChoice *rch;
  63. CMS_SignerInfo *si;
  64. for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++) {
  65. cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
  66. if (cch->type == CMS_CERTCHOICE_OTHER) {
  67. if (sd->version < 5)
  68. sd->version = 5;
  69. } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
  70. if (sd->version < 4)
  71. sd->version = 4;
  72. } else if (cch->type == CMS_CERTCHOICE_V1ACERT) {
  73. if (sd->version < 3)
  74. sd->version = 3;
  75. }
  76. }
  77. for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++) {
  78. rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
  79. if (rch->type == CMS_REVCHOICE_OTHER) {
  80. if (sd->version < 5)
  81. sd->version = 5;
  82. }
  83. }
  84. if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
  85. && (sd->version < 3))
  86. sd->version = 3;
  87. for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
  88. si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
  89. if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
  90. if (si->version < 3)
  91. si->version = 3;
  92. if (sd->version < 3)
  93. sd->version = 3;
  94. } else if (si->version < 1)
  95. si->version = 1;
  96. }
  97. if (sd->version < 1)
  98. sd->version = 1;
  99. }
  100. /*
  101. * RFC 5652 Section 11.1 Content Type
  102. * The content-type attribute within signed-data MUST
  103. * 1) be present if there are signed attributes
  104. * 2) match the content type in the signed-data,
  105. * 3) be a signed attribute.
  106. * 4) not have more than one copy of the attribute.
  107. *
  108. * Note that since the CMS_SignerInfo_sign() always adds the "signing time"
  109. * attribute, the content type attribute MUST be added also.
  110. * Assumptions: This assumes that the attribute does not already exist.
  111. */
  112. static int cms_set_si_contentType_attr(CMS_ContentInfo *cms, CMS_SignerInfo *si)
  113. {
  114. ASN1_OBJECT *ctype = cms->d.signedData->encapContentInfo->eContentType;
  115. /* Add the contentType attribute */
  116. return CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
  117. V_ASN1_OBJECT, ctype, -1) > 0;
  118. }
  119. /* Copy an existing messageDigest value */
  120. static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
  121. {
  122. STACK_OF(CMS_SignerInfo) *sinfos;
  123. CMS_SignerInfo *sitmp;
  124. int i;
  125. sinfos = CMS_get0_SignerInfos(cms);
  126. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  127. ASN1_OCTET_STRING *messageDigest;
  128. sitmp = sk_CMS_SignerInfo_value(sinfos, i);
  129. if (sitmp == si)
  130. continue;
  131. if (CMS_signed_get_attr_count(sitmp) < 0)
  132. continue;
  133. if (OBJ_cmp(si->digestAlgorithm->algorithm,
  134. sitmp->digestAlgorithm->algorithm))
  135. continue;
  136. messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
  137. OBJ_nid2obj
  138. (NID_pkcs9_messageDigest),
  139. -3, V_ASN1_OCTET_STRING);
  140. if (!messageDigest) {
  141. CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST,
  142. CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
  143. return 0;
  144. }
  145. if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
  146. V_ASN1_OCTET_STRING,
  147. messageDigest, -1))
  148. return 1;
  149. else
  150. return 0;
  151. }
  152. CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, CMS_R_NO_MATCHING_DIGEST);
  153. return 0;
  154. }
  155. int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type)
  156. {
  157. switch (type) {
  158. case CMS_SIGNERINFO_ISSUER_SERIAL:
  159. if (!cms_set1_ias(&sid->d.issuerAndSerialNumber, cert))
  160. return 0;
  161. break;
  162. case CMS_SIGNERINFO_KEYIDENTIFIER:
  163. if (!cms_set1_keyid(&sid->d.subjectKeyIdentifier, cert))
  164. return 0;
  165. break;
  166. default:
  167. CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, CMS_R_UNKNOWN_ID);
  168. return 0;
  169. }
  170. sid->type = type;
  171. return 1;
  172. }
  173. int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
  174. ASN1_OCTET_STRING **keyid,
  175. X509_NAME **issuer,
  176. ASN1_INTEGER **sno)
  177. {
  178. if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) {
  179. if (issuer)
  180. *issuer = sid->d.issuerAndSerialNumber->issuer;
  181. if (sno)
  182. *sno = sid->d.issuerAndSerialNumber->serialNumber;
  183. } else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
  184. if (keyid)
  185. *keyid = sid->d.subjectKeyIdentifier;
  186. } else
  187. return 0;
  188. return 1;
  189. }
  190. int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
  191. {
  192. if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
  193. return cms_ias_cert_cmp(sid->d.issuerAndSerialNumber, cert);
  194. else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
  195. return cms_keyid_cert_cmp(sid->d.subjectKeyIdentifier, cert);
  196. else
  197. return -1;
  198. }
  199. static int cms_sd_asn1_ctrl(CMS_SignerInfo *si, int cmd)
  200. {
  201. EVP_PKEY *pkey = si->pkey;
  202. int i;
  203. if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
  204. return 1;
  205. i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_SIGN, cmd, si);
  206. if (i == -2) {
  207. CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  208. return 0;
  209. }
  210. if (i <= 0) {
  211. CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_CTRL_FAILURE);
  212. return 0;
  213. }
  214. return 1;
  215. }
  216. CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
  217. X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
  218. unsigned int flags)
  219. {
  220. CMS_SignedData *sd;
  221. CMS_SignerInfo *si = NULL;
  222. X509_ALGOR *alg;
  223. int i, type;
  224. if (!X509_check_private_key(signer, pk)) {
  225. CMSerr(CMS_F_CMS_ADD1_SIGNER,
  226. CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  227. return NULL;
  228. }
  229. sd = cms_signed_data_init(cms);
  230. if (!sd)
  231. goto err;
  232. si = M_ASN1_new_of(CMS_SignerInfo);
  233. if (!si)
  234. goto merr;
  235. /* Call for side-effect of computing hash and caching extensions */
  236. X509_check_purpose(signer, -1, -1);
  237. X509_up_ref(signer);
  238. EVP_PKEY_up_ref(pk);
  239. si->pkey = pk;
  240. si->signer = signer;
  241. si->mctx = EVP_MD_CTX_new();
  242. si->pctx = NULL;
  243. if (si->mctx == NULL) {
  244. CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
  245. goto err;
  246. }
  247. if (flags & CMS_USE_KEYID) {
  248. si->version = 3;
  249. if (sd->version < 3)
  250. sd->version = 3;
  251. type = CMS_SIGNERINFO_KEYIDENTIFIER;
  252. } else {
  253. type = CMS_SIGNERINFO_ISSUER_SERIAL;
  254. si->version = 1;
  255. }
  256. if (!cms_set1_SignerIdentifier(si->sid, signer, type))
  257. goto err;
  258. if (md == NULL) {
  259. int def_nid;
  260. if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0)
  261. goto err;
  262. md = EVP_get_digestbynid(def_nid);
  263. if (md == NULL) {
  264. CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DEFAULT_DIGEST);
  265. goto err;
  266. }
  267. }
  268. if (!md) {
  269. CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DIGEST_SET);
  270. goto err;
  271. }
  272. X509_ALGOR_set_md(si->digestAlgorithm, md);
  273. /* See if digest is present in digestAlgorithms */
  274. for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
  275. const ASN1_OBJECT *aoid;
  276. alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
  277. X509_ALGOR_get0(&aoid, NULL, NULL, alg);
  278. if (OBJ_obj2nid(aoid) == EVP_MD_type(md))
  279. break;
  280. }
  281. if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) {
  282. alg = X509_ALGOR_new();
  283. if (alg == NULL)
  284. goto merr;
  285. X509_ALGOR_set_md(alg, md);
  286. if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) {
  287. X509_ALGOR_free(alg);
  288. goto merr;
  289. }
  290. }
  291. if (!(flags & CMS_KEY_PARAM) && !cms_sd_asn1_ctrl(si, 0))
  292. goto err;
  293. if (!(flags & CMS_NOATTR)) {
  294. /*
  295. * Initialize signed attributes structure so other attributes
  296. * such as signing time etc are added later even if we add none here.
  297. */
  298. if (!si->signedAttrs) {
  299. si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
  300. if (!si->signedAttrs)
  301. goto merr;
  302. }
  303. if (!(flags & CMS_NOSMIMECAP)) {
  304. STACK_OF(X509_ALGOR) *smcap = NULL;
  305. i = CMS_add_standard_smimecap(&smcap);
  306. if (i)
  307. i = CMS_add_smimecap(si, smcap);
  308. sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
  309. if (!i)
  310. goto merr;
  311. }
  312. if (flags & CMS_CADES) {
  313. ESS_SIGNING_CERT *sc = NULL;
  314. ESS_SIGNING_CERT_V2 *sc2 = NULL;
  315. int add_sc;
  316. if (md == EVP_sha1() || md == NULL) {
  317. if ((sc = ESS_SIGNING_CERT_new_init(signer,
  318. NULL, 1)) == NULL)
  319. goto err;
  320. add_sc = cms_add1_signing_cert(si, sc);
  321. ESS_SIGNING_CERT_free(sc);
  322. } else {
  323. if ((sc2 = ESS_SIGNING_CERT_V2_new_init(md, signer,
  324. NULL, 1)) == NULL)
  325. goto err;
  326. add_sc = cms_add1_signing_cert_v2(si, sc2);
  327. ESS_SIGNING_CERT_V2_free(sc2);
  328. }
  329. if (!add_sc)
  330. goto err;
  331. }
  332. if (flags & CMS_REUSE_DIGEST) {
  333. if (!cms_copy_messageDigest(cms, si))
  334. goto err;
  335. if (!cms_set_si_contentType_attr(cms, si))
  336. goto err;
  337. if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) &&
  338. !CMS_SignerInfo_sign(si))
  339. goto err;
  340. }
  341. }
  342. if (!(flags & CMS_NOCERTS)) {
  343. /* NB ignore -1 return for duplicate cert */
  344. if (!CMS_add1_cert(cms, signer))
  345. goto merr;
  346. }
  347. if (flags & CMS_KEY_PARAM) {
  348. if (flags & CMS_NOATTR) {
  349. si->pctx = EVP_PKEY_CTX_new(si->pkey, NULL);
  350. if (si->pctx == NULL)
  351. goto err;
  352. if (EVP_PKEY_sign_init(si->pctx) <= 0)
  353. goto err;
  354. if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
  355. goto err;
  356. } else if (EVP_DigestSignInit(si->mctx, &si->pctx, md, NULL, pk) <= 0)
  357. goto err;
  358. }
  359. if (!sd->signerInfos)
  360. sd->signerInfos = sk_CMS_SignerInfo_new_null();
  361. if (!sd->signerInfos || !sk_CMS_SignerInfo_push(sd->signerInfos, si))
  362. goto merr;
  363. return si;
  364. merr:
  365. CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
  366. err:
  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. EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si)
  392. {
  393. return si->pctx;
  394. }
  395. EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si)
  396. {
  397. return si->mctx;
  398. }
  399. STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
  400. {
  401. CMS_SignedData *sd;
  402. sd = cms_get0_signed(cms);
  403. if (!sd)
  404. return NULL;
  405. return sd->signerInfos;
  406. }
  407. STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
  408. {
  409. STACK_OF(X509) *signers = NULL;
  410. STACK_OF(CMS_SignerInfo) *sinfos;
  411. CMS_SignerInfo *si;
  412. int i;
  413. sinfos = CMS_get0_SignerInfos(cms);
  414. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  415. si = sk_CMS_SignerInfo_value(sinfos, i);
  416. if (si->signer) {
  417. if (!signers) {
  418. signers = sk_X509_new_null();
  419. if (!signers)
  420. return NULL;
  421. }
  422. if (!sk_X509_push(signers, si->signer)) {
  423. sk_X509_free(signers);
  424. return NULL;
  425. }
  426. }
  427. }
  428. return signers;
  429. }
  430. void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
  431. {
  432. if (signer) {
  433. X509_up_ref(signer);
  434. EVP_PKEY_free(si->pkey);
  435. si->pkey = X509_get_pubkey(signer);
  436. }
  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 = EVP_MD_CTX_new();
  513. int r = 0;
  514. EVP_PKEY_CTX *pctx = NULL;
  515. if (mctx == NULL) {
  516. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
  517. return 0;
  518. }
  519. if (!si->pkey) {
  520. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
  521. goto err;
  522. }
  523. if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
  524. goto err;
  525. /* Set SignerInfo algorithm details if we used custom parameter */
  526. if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
  527. goto err;
  528. /*
  529. * If any signed attributes calculate and add messageDigest attribute
  530. */
  531. if (CMS_signed_get_attr_count(si) >= 0) {
  532. unsigned char md[EVP_MAX_MD_SIZE];
  533. unsigned int mdlen;
  534. if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
  535. goto err;
  536. if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
  537. V_ASN1_OCTET_STRING, md, mdlen))
  538. goto err;
  539. /* Copy content type across */
  540. if (!cms_set_si_contentType_attr(cms, si))
  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 == NULL) {
  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. OPENSSL_free(sig);
  560. goto err;
  561. }
  562. ASN1_STRING_set0(si->signature, sig, siglen);
  563. } else {
  564. unsigned char *sig;
  565. unsigned int siglen;
  566. sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
  567. if (sig == NULL) {
  568. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
  569. goto err;
  570. }
  571. if (!EVP_SignFinal(mctx, sig, &siglen, si->pkey)) {
  572. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_SIGNFINAL_ERROR);
  573. OPENSSL_free(sig);
  574. goto err;
  575. }
  576. ASN1_STRING_set0(si->signature, sig, siglen);
  577. }
  578. r = 1;
  579. err:
  580. EVP_MD_CTX_free(mctx);
  581. EVP_PKEY_CTX_free(pctx);
  582. return r;
  583. }
  584. int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)
  585. {
  586. STACK_OF(CMS_SignerInfo) *sinfos;
  587. CMS_SignerInfo *si;
  588. int i;
  589. sinfos = CMS_get0_SignerInfos(cms);
  590. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  591. si = sk_CMS_SignerInfo_value(sinfos, i);
  592. if (!cms_SignerInfo_content_sign(cms, si, chain))
  593. return 0;
  594. }
  595. cms->d.signedData->encapContentInfo->partial = 0;
  596. return 1;
  597. }
  598. int CMS_SignerInfo_sign(CMS_SignerInfo *si)
  599. {
  600. EVP_MD_CTX *mctx = si->mctx;
  601. EVP_PKEY_CTX *pctx = NULL;
  602. unsigned char *abuf = NULL;
  603. int alen;
  604. size_t siglen;
  605. const EVP_MD *md = NULL;
  606. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  607. if (md == NULL)
  608. return 0;
  609. if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
  610. if (!cms_add1_signingTime(si, NULL))
  611. goto err;
  612. }
  613. if (!CMS_si_check_attributes(si))
  614. goto err;
  615. if (si->pctx)
  616. pctx = si->pctx;
  617. else {
  618. EVP_MD_CTX_reset(mctx);
  619. if (EVP_DigestSignInit(mctx, &pctx, md, NULL, si->pkey) <= 0)
  620. goto err;
  621. si->pctx = pctx;
  622. }
  623. /*
  624. * TODO(3.0): This causes problems when providers are in use, so disabled
  625. * for now. Can we get rid of this completely? AFAICT this ctrl has been
  626. * present since CMS was first put in - but has never been used to do
  627. * anything. All internal implementations just return 1 and ignore this ctrl
  628. * and have always done so by the looks of things. To fix this we could
  629. * convert this ctrl into a param, which would require us to send all the
  630. * signer info data as a set of params...but that is non-trivial and since
  631. * this isn't used by anything it may be better just to remove it.
  632. */
  633. #if 0
  634. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  635. EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0) {
  636. CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
  637. goto err;
  638. }
  639. #endif
  640. alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
  641. ASN1_ITEM_rptr(CMS_Attributes_Sign));
  642. if (!abuf)
  643. goto err;
  644. if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
  645. goto err;
  646. if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
  647. goto err;
  648. OPENSSL_free(abuf);
  649. abuf = OPENSSL_malloc(siglen);
  650. if (abuf == NULL)
  651. goto err;
  652. if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
  653. goto err;
  654. /*
  655. * TODO(3.0): This causes problems when providers are in use, so disabled
  656. * for now. Can we get rid of this completely? AFAICT this ctrl has been
  657. * present since CMS was first put in - but has never been used to do
  658. * anything. All internal implementations just return 1 and ignore this ctrl
  659. * and have always done so by the looks of things. To fix this we could
  660. * convert this ctrl into a param, which would require us to send all the
  661. * signer info data as a set of params...but that is non-trivial and since
  662. * this isn't used by anything it may be better just to remove it.
  663. */
  664. #if 0
  665. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  666. EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0) {
  667. CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
  668. goto err;
  669. }
  670. #endif
  671. EVP_MD_CTX_reset(mctx);
  672. ASN1_STRING_set0(si->signature, abuf, siglen);
  673. return 1;
  674. err:
  675. OPENSSL_free(abuf);
  676. EVP_MD_CTX_reset(mctx);
  677. return 0;
  678. }
  679. int CMS_SignerInfo_verify(CMS_SignerInfo *si)
  680. {
  681. EVP_MD_CTX *mctx = NULL;
  682. unsigned char *abuf = NULL;
  683. int alen, r = -1;
  684. const EVP_MD *md = NULL;
  685. if (!si->pkey) {
  686. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);
  687. return -1;
  688. }
  689. if (!CMS_si_check_attributes(si))
  690. return -1;
  691. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  692. if (md == NULL)
  693. return -1;
  694. if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) {
  695. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, ERR_R_MALLOC_FAILURE);
  696. return -1;
  697. }
  698. mctx = si->mctx;
  699. if (EVP_DigestVerifyInit(mctx, &si->pctx, md, NULL, si->pkey) <= 0)
  700. goto err;
  701. if (!cms_sd_asn1_ctrl(si, 1))
  702. goto err;
  703. alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
  704. ASN1_ITEM_rptr(CMS_Attributes_Verify));
  705. if (!abuf)
  706. goto err;
  707. r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
  708. OPENSSL_free(abuf);
  709. if (r <= 0) {
  710. r = -1;
  711. goto err;
  712. }
  713. r = EVP_DigestVerifyFinal(mctx,
  714. si->signature->data, si->signature->length);
  715. if (r <= 0)
  716. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
  717. err:
  718. EVP_MD_CTX_reset(mctx);
  719. return r;
  720. }
  721. /* Create a chain of digest BIOs from a CMS ContentInfo */
  722. BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)
  723. {
  724. int i;
  725. CMS_SignedData *sd;
  726. BIO *chain = NULL;
  727. sd = cms_get0_signed(cms);
  728. if (!sd)
  729. return NULL;
  730. if (cms->d.signedData->encapContentInfo->partial)
  731. cms_sd_set_version(sd);
  732. for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
  733. X509_ALGOR *digestAlgorithm;
  734. BIO *mdbio;
  735. digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
  736. mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm);
  737. if (!mdbio)
  738. goto err;
  739. if (chain)
  740. BIO_push(chain, mdbio);
  741. else
  742. chain = mdbio;
  743. }
  744. return chain;
  745. err:
  746. BIO_free_all(chain);
  747. return NULL;
  748. }
  749. int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
  750. {
  751. ASN1_OCTET_STRING *os = NULL;
  752. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  753. EVP_PKEY_CTX *pkctx = NULL;
  754. int r = -1;
  755. unsigned char mval[EVP_MAX_MD_SIZE];
  756. unsigned int mlen;
  757. if (mctx == NULL) {
  758. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE);
  759. goto err;
  760. }
  761. /* If we have any signed attributes look for messageDigest value */
  762. if (CMS_signed_get_attr_count(si) >= 0) {
  763. os = CMS_signed_get0_data_by_OBJ(si,
  764. OBJ_nid2obj(NID_pkcs9_messageDigest),
  765. -3, V_ASN1_OCTET_STRING);
  766. if (!os) {
  767. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  768. CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
  769. goto err;
  770. }
  771. }
  772. if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
  773. goto err;
  774. if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
  775. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  776. CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
  777. goto err;
  778. }
  779. /* If messageDigest found compare it */
  780. if (os) {
  781. if (mlen != (unsigned int)os->length) {
  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. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  788. CMS_R_VERIFICATION_FAILURE);
  789. r = 0;
  790. } else
  791. r = 1;
  792. } else {
  793. const EVP_MD *md = EVP_MD_CTX_md(mctx);
  794. pkctx = EVP_PKEY_CTX_new(si->pkey, NULL);
  795. if (pkctx == NULL)
  796. goto err;
  797. if (EVP_PKEY_verify_init(pkctx) <= 0)
  798. goto err;
  799. if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
  800. goto err;
  801. si->pctx = pkctx;
  802. if (!cms_sd_asn1_ctrl(si, 1))
  803. goto err;
  804. r = EVP_PKEY_verify(pkctx, si->signature->data,
  805. si->signature->length, mval, mlen);
  806. if (r <= 0) {
  807. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  808. CMS_R_VERIFICATION_FAILURE);
  809. r = 0;
  810. }
  811. }
  812. err:
  813. EVP_PKEY_CTX_free(pkctx);
  814. EVP_MD_CTX_free(mctx);
  815. return r;
  816. }
  817. int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
  818. {
  819. unsigned char *smder = NULL;
  820. int smderlen, r;
  821. smderlen = i2d_X509_ALGORS(algs, &smder);
  822. if (smderlen <= 0)
  823. return 0;
  824. r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
  825. V_ASN1_SEQUENCE, smder, smderlen);
  826. OPENSSL_free(smder);
  827. return r;
  828. }
  829. int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
  830. int algnid, int keysize)
  831. {
  832. X509_ALGOR *alg;
  833. ASN1_INTEGER *key = NULL;
  834. if (keysize > 0) {
  835. key = ASN1_INTEGER_new();
  836. if (key == NULL || !ASN1_INTEGER_set(key, keysize))
  837. return 0;
  838. }
  839. alg = X509_ALGOR_new();
  840. if (alg == NULL) {
  841. ASN1_INTEGER_free(key);
  842. return 0;
  843. }
  844. X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
  845. key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
  846. if (*algs == NULL)
  847. *algs = sk_X509_ALGOR_new_null();
  848. if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
  849. X509_ALGOR_free(alg);
  850. return 0;
  851. }
  852. return 1;
  853. }
  854. /* Check to see if a cipher exists and if so add S/MIME capabilities */
  855. static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
  856. {
  857. if (EVP_get_cipherbynid(nid))
  858. return CMS_add_simple_smimecap(sk, nid, arg);
  859. return 1;
  860. }
  861. static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
  862. {
  863. if (EVP_get_digestbynid(nid))
  864. return CMS_add_simple_smimecap(sk, nid, arg);
  865. return 1;
  866. }
  867. int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
  868. {
  869. if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
  870. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
  871. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
  872. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
  873. || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
  874. || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
  875. || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
  876. || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
  877. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
  878. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
  879. || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
  880. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))
  881. return 0;
  882. return 1;
  883. }