cms_sd.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * Copyright 2008-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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_local.h"
  17. #include "crypto/asn1.h"
  18. #include "crypto/evp.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_REUSE_DIGEST) {
  310. if (!cms_copy_messageDigest(cms, si))
  311. goto err;
  312. if (!cms_set_si_contentType_attr(cms, si))
  313. goto err;
  314. if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) &&
  315. !CMS_SignerInfo_sign(si))
  316. goto err;
  317. }
  318. }
  319. if (!(flags & CMS_NOCERTS)) {
  320. /* NB ignore -1 return for duplicate cert */
  321. if (!CMS_add1_cert(cms, signer))
  322. goto merr;
  323. }
  324. if (flags & CMS_KEY_PARAM) {
  325. if (flags & CMS_NOATTR) {
  326. si->pctx = EVP_PKEY_CTX_new(si->pkey, NULL);
  327. if (si->pctx == NULL)
  328. goto err;
  329. if (EVP_PKEY_sign_init(si->pctx) <= 0)
  330. goto err;
  331. if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
  332. goto err;
  333. } else if (EVP_DigestSignInit(si->mctx, &si->pctx, md, NULL, pk) <=
  334. 0)
  335. goto err;
  336. }
  337. if (!sd->signerInfos)
  338. sd->signerInfos = sk_CMS_SignerInfo_new_null();
  339. if (!sd->signerInfos || !sk_CMS_SignerInfo_push(sd->signerInfos, si))
  340. goto merr;
  341. return si;
  342. merr:
  343. CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
  344. err:
  345. M_ASN1_free_of(si, CMS_SignerInfo);
  346. return NULL;
  347. }
  348. static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
  349. {
  350. ASN1_TIME *tt;
  351. int r = 0;
  352. if (t)
  353. tt = t;
  354. else
  355. tt = X509_gmtime_adj(NULL, 0);
  356. if (!tt)
  357. goto merr;
  358. if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
  359. tt->type, tt, -1) <= 0)
  360. goto merr;
  361. r = 1;
  362. merr:
  363. if (!t)
  364. ASN1_TIME_free(tt);
  365. if (!r)
  366. CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME, ERR_R_MALLOC_FAILURE);
  367. return r;
  368. }
  369. EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si)
  370. {
  371. return si->pctx;
  372. }
  373. EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si)
  374. {
  375. return si->mctx;
  376. }
  377. STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
  378. {
  379. CMS_SignedData *sd;
  380. sd = cms_get0_signed(cms);
  381. if (!sd)
  382. return NULL;
  383. return sd->signerInfos;
  384. }
  385. STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
  386. {
  387. STACK_OF(X509) *signers = NULL;
  388. STACK_OF(CMS_SignerInfo) *sinfos;
  389. CMS_SignerInfo *si;
  390. int i;
  391. sinfos = CMS_get0_SignerInfos(cms);
  392. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  393. si = sk_CMS_SignerInfo_value(sinfos, i);
  394. if (si->signer) {
  395. if (!signers) {
  396. signers = sk_X509_new_null();
  397. if (!signers)
  398. return NULL;
  399. }
  400. if (!sk_X509_push(signers, si->signer)) {
  401. sk_X509_free(signers);
  402. return NULL;
  403. }
  404. }
  405. }
  406. return signers;
  407. }
  408. void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
  409. {
  410. if (signer) {
  411. X509_up_ref(signer);
  412. EVP_PKEY_free(si->pkey);
  413. si->pkey = X509_get_pubkey(signer);
  414. }
  415. X509_free(si->signer);
  416. si->signer = signer;
  417. }
  418. int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
  419. ASN1_OCTET_STRING **keyid,
  420. X509_NAME **issuer, ASN1_INTEGER **sno)
  421. {
  422. return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
  423. }
  424. int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
  425. {
  426. return cms_SignerIdentifier_cert_cmp(si->sid, cert);
  427. }
  428. int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
  429. unsigned int flags)
  430. {
  431. CMS_SignedData *sd;
  432. CMS_SignerInfo *si;
  433. CMS_CertificateChoices *cch;
  434. STACK_OF(CMS_CertificateChoices) *certs;
  435. X509 *x;
  436. int i, j;
  437. int ret = 0;
  438. sd = cms_get0_signed(cms);
  439. if (!sd)
  440. return -1;
  441. certs = sd->certificates;
  442. for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
  443. si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
  444. if (si->signer)
  445. continue;
  446. for (j = 0; j < sk_X509_num(scerts); j++) {
  447. x = sk_X509_value(scerts, j);
  448. if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
  449. CMS_SignerInfo_set1_signer_cert(si, x);
  450. ret++;
  451. break;
  452. }
  453. }
  454. if (si->signer || (flags & CMS_NOINTERN))
  455. continue;
  456. for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) {
  457. cch = sk_CMS_CertificateChoices_value(certs, j);
  458. if (cch->type != 0)
  459. continue;
  460. x = cch->d.certificate;
  461. if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
  462. CMS_SignerInfo_set1_signer_cert(si, x);
  463. ret++;
  464. break;
  465. }
  466. }
  467. }
  468. return ret;
  469. }
  470. void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
  471. X509 **signer, X509_ALGOR **pdig,
  472. X509_ALGOR **psig)
  473. {
  474. if (pk)
  475. *pk = si->pkey;
  476. if (signer)
  477. *signer = si->signer;
  478. if (pdig)
  479. *pdig = si->digestAlgorithm;
  480. if (psig)
  481. *psig = si->signatureAlgorithm;
  482. }
  483. ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si)
  484. {
  485. return si->signature;
  486. }
  487. static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
  488. CMS_SignerInfo *si, BIO *chain)
  489. {
  490. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  491. int r = 0;
  492. EVP_PKEY_CTX *pctx = NULL;
  493. if (mctx == NULL) {
  494. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
  495. return 0;
  496. }
  497. if (!si->pkey) {
  498. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
  499. goto err;
  500. }
  501. if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
  502. goto err;
  503. /* Set SignerInfo algorithm details if we used custom parameter */
  504. if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
  505. goto err;
  506. /*
  507. * If any signed attributes calculate and add messageDigest attribute
  508. */
  509. if (CMS_signed_get_attr_count(si) >= 0) {
  510. unsigned char md[EVP_MAX_MD_SIZE];
  511. unsigned int mdlen;
  512. if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
  513. goto err;
  514. if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
  515. V_ASN1_OCTET_STRING, md, mdlen))
  516. goto err;
  517. /* Copy content type across */
  518. if (!cms_set_si_contentType_attr(cms, si))
  519. goto err;
  520. if (!CMS_SignerInfo_sign(si))
  521. goto err;
  522. } else if (si->pctx) {
  523. unsigned char *sig;
  524. size_t siglen;
  525. unsigned char md[EVP_MAX_MD_SIZE];
  526. unsigned int mdlen;
  527. pctx = si->pctx;
  528. if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
  529. goto err;
  530. siglen = EVP_PKEY_size(si->pkey);
  531. sig = OPENSSL_malloc(siglen);
  532. if (sig == NULL) {
  533. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
  534. goto err;
  535. }
  536. if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
  537. OPENSSL_free(sig);
  538. goto err;
  539. }
  540. ASN1_STRING_set0(si->signature, sig, siglen);
  541. } else {
  542. unsigned char *sig;
  543. unsigned int siglen;
  544. sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
  545. if (sig == NULL) {
  546. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
  547. goto err;
  548. }
  549. if (!EVP_SignFinal(mctx, sig, &siglen, si->pkey)) {
  550. CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_SIGNFINAL_ERROR);
  551. OPENSSL_free(sig);
  552. goto err;
  553. }
  554. ASN1_STRING_set0(si->signature, sig, siglen);
  555. }
  556. r = 1;
  557. err:
  558. EVP_MD_CTX_free(mctx);
  559. EVP_PKEY_CTX_free(pctx);
  560. return r;
  561. }
  562. int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)
  563. {
  564. STACK_OF(CMS_SignerInfo) *sinfos;
  565. CMS_SignerInfo *si;
  566. int i;
  567. sinfos = CMS_get0_SignerInfos(cms);
  568. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  569. si = sk_CMS_SignerInfo_value(sinfos, i);
  570. if (!cms_SignerInfo_content_sign(cms, si, chain))
  571. return 0;
  572. }
  573. cms->d.signedData->encapContentInfo->partial = 0;
  574. return 1;
  575. }
  576. int CMS_SignerInfo_sign(CMS_SignerInfo *si)
  577. {
  578. EVP_MD_CTX *mctx = si->mctx;
  579. EVP_PKEY_CTX *pctx = NULL;
  580. unsigned char *abuf = NULL;
  581. int alen;
  582. size_t siglen;
  583. const EVP_MD *md = NULL;
  584. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  585. if (md == NULL)
  586. return 0;
  587. if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
  588. if (!cms_add1_signingTime(si, NULL))
  589. goto err;
  590. }
  591. if (!CMS_si_check_attributes(si))
  592. goto err;
  593. if (si->pctx)
  594. pctx = si->pctx;
  595. else {
  596. EVP_MD_CTX_reset(mctx);
  597. if (EVP_DigestSignInit(mctx, &pctx, md, NULL, si->pkey) <= 0)
  598. goto err;
  599. si->pctx = pctx;
  600. }
  601. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  602. EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0) {
  603. CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
  604. goto err;
  605. }
  606. alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
  607. ASN1_ITEM_rptr(CMS_Attributes_Sign));
  608. if (!abuf)
  609. goto err;
  610. if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
  611. goto err;
  612. if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
  613. goto err;
  614. OPENSSL_free(abuf);
  615. abuf = OPENSSL_malloc(siglen);
  616. if (abuf == NULL)
  617. goto err;
  618. if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
  619. goto err;
  620. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  621. EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0) {
  622. CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
  623. goto err;
  624. }
  625. EVP_MD_CTX_reset(mctx);
  626. ASN1_STRING_set0(si->signature, abuf, siglen);
  627. return 1;
  628. err:
  629. OPENSSL_free(abuf);
  630. EVP_MD_CTX_reset(mctx);
  631. return 0;
  632. }
  633. int CMS_SignerInfo_verify(CMS_SignerInfo *si)
  634. {
  635. EVP_MD_CTX *mctx = NULL;
  636. unsigned char *abuf = NULL;
  637. int alen, r = -1;
  638. const EVP_MD *md = NULL;
  639. if (!si->pkey) {
  640. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);
  641. return -1;
  642. }
  643. if (!CMS_si_check_attributes(si))
  644. return -1;
  645. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  646. if (md == NULL)
  647. return -1;
  648. if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) {
  649. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, ERR_R_MALLOC_FAILURE);
  650. return -1;
  651. }
  652. mctx = si->mctx;
  653. if (EVP_DigestVerifyInit(mctx, &si->pctx, md, NULL, si->pkey) <= 0)
  654. goto err;
  655. if (!cms_sd_asn1_ctrl(si, 1))
  656. goto err;
  657. alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
  658. ASN1_ITEM_rptr(CMS_Attributes_Verify));
  659. if (!abuf)
  660. goto err;
  661. r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
  662. OPENSSL_free(abuf);
  663. if (r <= 0) {
  664. r = -1;
  665. goto err;
  666. }
  667. r = EVP_DigestVerifyFinal(mctx,
  668. si->signature->data, si->signature->length);
  669. if (r <= 0)
  670. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
  671. err:
  672. EVP_MD_CTX_reset(mctx);
  673. return r;
  674. }
  675. /* Create a chain of digest BIOs from a CMS ContentInfo */
  676. BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)
  677. {
  678. int i;
  679. CMS_SignedData *sd;
  680. BIO *chain = NULL;
  681. sd = cms_get0_signed(cms);
  682. if (!sd)
  683. return NULL;
  684. if (cms->d.signedData->encapContentInfo->partial)
  685. cms_sd_set_version(sd);
  686. for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
  687. X509_ALGOR *digestAlgorithm;
  688. BIO *mdbio;
  689. digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
  690. mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm);
  691. if (!mdbio)
  692. goto err;
  693. if (chain)
  694. BIO_push(chain, mdbio);
  695. else
  696. chain = mdbio;
  697. }
  698. return chain;
  699. err:
  700. BIO_free_all(chain);
  701. return NULL;
  702. }
  703. int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
  704. {
  705. ASN1_OCTET_STRING *os = NULL;
  706. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  707. EVP_PKEY_CTX *pkctx = NULL;
  708. int r = -1;
  709. unsigned char mval[EVP_MAX_MD_SIZE];
  710. unsigned int mlen;
  711. if (mctx == NULL) {
  712. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE);
  713. goto err;
  714. }
  715. /* If we have any signed attributes look for messageDigest value */
  716. if (CMS_signed_get_attr_count(si) >= 0) {
  717. os = CMS_signed_get0_data_by_OBJ(si,
  718. OBJ_nid2obj(NID_pkcs9_messageDigest),
  719. -3, V_ASN1_OCTET_STRING);
  720. if (!os) {
  721. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  722. CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
  723. goto err;
  724. }
  725. }
  726. if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
  727. goto err;
  728. if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
  729. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  730. CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
  731. goto err;
  732. }
  733. /* If messageDigest found compare it */
  734. if (os) {
  735. if (mlen != (unsigned int)os->length) {
  736. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  737. CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
  738. goto err;
  739. }
  740. if (memcmp(mval, os->data, mlen)) {
  741. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  742. CMS_R_VERIFICATION_FAILURE);
  743. r = 0;
  744. } else
  745. r = 1;
  746. } else {
  747. const EVP_MD *md = EVP_MD_CTX_md(mctx);
  748. pkctx = EVP_PKEY_CTX_new(si->pkey, NULL);
  749. if (pkctx == NULL)
  750. goto err;
  751. if (EVP_PKEY_verify_init(pkctx) <= 0)
  752. goto err;
  753. if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
  754. goto err;
  755. si->pctx = pkctx;
  756. if (!cms_sd_asn1_ctrl(si, 1))
  757. goto err;
  758. r = EVP_PKEY_verify(pkctx, si->signature->data,
  759. si->signature->length, mval, mlen);
  760. if (r <= 0) {
  761. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  762. CMS_R_VERIFICATION_FAILURE);
  763. r = 0;
  764. }
  765. }
  766. err:
  767. EVP_PKEY_CTX_free(pkctx);
  768. EVP_MD_CTX_free(mctx);
  769. return r;
  770. }
  771. int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
  772. {
  773. unsigned char *smder = NULL;
  774. int smderlen, r;
  775. smderlen = i2d_X509_ALGORS(algs, &smder);
  776. if (smderlen <= 0)
  777. return 0;
  778. r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
  779. V_ASN1_SEQUENCE, smder, smderlen);
  780. OPENSSL_free(smder);
  781. return r;
  782. }
  783. int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
  784. int algnid, int keysize)
  785. {
  786. X509_ALGOR *alg;
  787. ASN1_INTEGER *key = NULL;
  788. if (keysize > 0) {
  789. key = ASN1_INTEGER_new();
  790. if (key == NULL || !ASN1_INTEGER_set(key, keysize)) {
  791. ASN1_INTEGER_free(key);
  792. return 0;
  793. }
  794. }
  795. alg = X509_ALGOR_new();
  796. if (alg == NULL) {
  797. ASN1_INTEGER_free(key);
  798. return 0;
  799. }
  800. X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
  801. key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
  802. if (*algs == NULL)
  803. *algs = sk_X509_ALGOR_new_null();
  804. if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
  805. X509_ALGOR_free(alg);
  806. return 0;
  807. }
  808. return 1;
  809. }
  810. /* Check to see if a cipher exists and if so add S/MIME capabilities */
  811. static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
  812. {
  813. if (EVP_get_cipherbynid(nid))
  814. return CMS_add_simple_smimecap(sk, nid, arg);
  815. return 1;
  816. }
  817. static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
  818. {
  819. if (EVP_get_digestbynid(nid))
  820. return CMS_add_simple_smimecap(sk, nid, arg);
  821. return 1;
  822. }
  823. int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
  824. {
  825. if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
  826. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
  827. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
  828. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
  829. || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
  830. || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
  831. || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
  832. || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
  833. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
  834. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
  835. || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
  836. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))
  837. return 0;
  838. return 1;
  839. }