A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

923 lines
26 KiB

  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. 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;
  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. }
  580. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  581. EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0) {
  582. CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
  583. goto err;
  584. }
  585. alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
  586. ASN1_ITEM_rptr(CMS_Attributes_Sign));
  587. if (!abuf)
  588. goto err;
  589. if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
  590. goto err;
  591. if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
  592. goto err;
  593. OPENSSL_free(abuf);
  594. abuf = OPENSSL_malloc(siglen);
  595. if (abuf == NULL)
  596. goto err;
  597. if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
  598. goto err;
  599. if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
  600. EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0) {
  601. CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
  602. goto err;
  603. }
  604. EVP_MD_CTX_reset(mctx);
  605. ASN1_STRING_set0(si->signature, abuf, siglen);
  606. return 1;
  607. err:
  608. OPENSSL_free(abuf);
  609. EVP_MD_CTX_reset(mctx);
  610. return 0;
  611. }
  612. int CMS_SignerInfo_verify(CMS_SignerInfo *si)
  613. {
  614. EVP_MD_CTX *mctx = NULL;
  615. unsigned char *abuf = NULL;
  616. int alen, r = -1;
  617. const EVP_MD *md = NULL;
  618. if (!si->pkey) {
  619. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);
  620. return -1;
  621. }
  622. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  623. if (md == NULL)
  624. return -1;
  625. if (si->mctx == NULL)
  626. si->mctx = EVP_MD_CTX_new();
  627. mctx = si->mctx;
  628. if (EVP_DigestVerifyInit(mctx, &si->pctx, md, NULL, si->pkey) <= 0)
  629. goto err;
  630. if (!cms_sd_asn1_ctrl(si, 1))
  631. goto err;
  632. alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
  633. ASN1_ITEM_rptr(CMS_Attributes_Verify));
  634. if (!abuf)
  635. goto err;
  636. r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
  637. OPENSSL_free(abuf);
  638. if (r <= 0) {
  639. r = -1;
  640. goto err;
  641. }
  642. r = EVP_DigestVerifyFinal(mctx,
  643. si->signature->data, si->signature->length);
  644. if (r <= 0)
  645. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
  646. err:
  647. EVP_MD_CTX_reset(mctx);
  648. return r;
  649. }
  650. /* Create a chain of digest BIOs from a CMS ContentInfo */
  651. BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)
  652. {
  653. int i;
  654. CMS_SignedData *sd;
  655. BIO *chain = NULL;
  656. sd = cms_get0_signed(cms);
  657. if (!sd)
  658. return NULL;
  659. if (cms->d.signedData->encapContentInfo->partial)
  660. cms_sd_set_version(sd);
  661. for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
  662. X509_ALGOR *digestAlgorithm;
  663. BIO *mdbio;
  664. digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
  665. mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm);
  666. if (!mdbio)
  667. goto err;
  668. if (chain)
  669. BIO_push(chain, mdbio);
  670. else
  671. chain = mdbio;
  672. }
  673. return chain;
  674. err:
  675. BIO_free_all(chain);
  676. return NULL;
  677. }
  678. int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
  679. {
  680. ASN1_OCTET_STRING *os = NULL;
  681. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  682. EVP_PKEY_CTX *pkctx = NULL;
  683. int r = -1;
  684. unsigned char mval[EVP_MAX_MD_SIZE];
  685. unsigned int mlen;
  686. if (mctx == NULL) {
  687. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE);
  688. goto err;
  689. }
  690. /* If we have any signed attributes look for messageDigest value */
  691. if (CMS_signed_get_attr_count(si) >= 0) {
  692. os = CMS_signed_get0_data_by_OBJ(si,
  693. OBJ_nid2obj(NID_pkcs9_messageDigest),
  694. -3, V_ASN1_OCTET_STRING);
  695. if (!os) {
  696. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  697. CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
  698. goto err;
  699. }
  700. }
  701. if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
  702. goto err;
  703. if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
  704. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  705. CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
  706. goto err;
  707. }
  708. /* If messageDigest found compare it */
  709. if (os) {
  710. if (mlen != (unsigned int)os->length) {
  711. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  712. CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
  713. goto err;
  714. }
  715. if (memcmp(mval, os->data, mlen)) {
  716. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  717. CMS_R_VERIFICATION_FAILURE);
  718. r = 0;
  719. } else
  720. r = 1;
  721. } else {
  722. const EVP_MD *md = EVP_MD_CTX_md(mctx);
  723. pkctx = EVP_PKEY_CTX_new(si->pkey, NULL);
  724. if (pkctx == NULL)
  725. goto err;
  726. if (EVP_PKEY_verify_init(pkctx) <= 0)
  727. goto err;
  728. if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
  729. goto err;
  730. si->pctx = pkctx;
  731. if (!cms_sd_asn1_ctrl(si, 1))
  732. goto err;
  733. r = EVP_PKEY_verify(pkctx, si->signature->data,
  734. si->signature->length, mval, mlen);
  735. if (r <= 0) {
  736. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
  737. CMS_R_VERIFICATION_FAILURE);
  738. r = 0;
  739. }
  740. }
  741. err:
  742. EVP_PKEY_CTX_free(pkctx);
  743. EVP_MD_CTX_free(mctx);
  744. return r;
  745. }
  746. int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
  747. {
  748. unsigned char *smder = NULL;
  749. int smderlen, r;
  750. smderlen = i2d_X509_ALGORS(algs, &smder);
  751. if (smderlen <= 0)
  752. return 0;
  753. r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
  754. V_ASN1_SEQUENCE, smder, smderlen);
  755. OPENSSL_free(smder);
  756. return r;
  757. }
  758. int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
  759. int algnid, int keysize)
  760. {
  761. X509_ALGOR *alg;
  762. ASN1_INTEGER *key = NULL;
  763. if (keysize > 0) {
  764. key = ASN1_INTEGER_new();
  765. if (key == NULL || !ASN1_INTEGER_set(key, keysize))
  766. return 0;
  767. }
  768. alg = X509_ALGOR_new();
  769. if (alg == NULL) {
  770. ASN1_INTEGER_free(key);
  771. return 0;
  772. }
  773. X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
  774. key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
  775. if (*algs == NULL)
  776. *algs = sk_X509_ALGOR_new_null();
  777. if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
  778. X509_ALGOR_free(alg);
  779. return 0;
  780. }
  781. return 1;
  782. }
  783. /* Check to see if a cipher exists and if so add S/MIME capabilities */
  784. static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
  785. {
  786. if (EVP_get_cipherbynid(nid))
  787. return CMS_add_simple_smimecap(sk, nid, arg);
  788. return 1;
  789. }
  790. static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
  791. {
  792. if (EVP_get_digestbynid(nid))
  793. return CMS_add_simple_smimecap(sk, nid, arg);
  794. return 1;
  795. }
  796. int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
  797. {
  798. if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
  799. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
  800. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
  801. || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
  802. || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
  803. || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
  804. || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
  805. || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
  806. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
  807. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
  808. || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
  809. || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))
  810. return 0;
  811. return 1;
  812. }