cms_sd.c 28 KB

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