cms_sd.c 31 KB

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