cms_sd.c 26 KB

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