2
0

cms_ess.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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/rand.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/ess.h"
  19. #include "crypto/cms.h"
  20. DEFINE_STACK_OF(GENERAL_NAMES)
  21. DEFINE_STACK_OF(CMS_SignerInfo)
  22. DEFINE_STACK_OF(ESS_CERT_ID)
  23. DEFINE_STACK_OF(ESS_CERT_ID_V2)
  24. DEFINE_STACK_OF(X509)
  25. IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest)
  26. /* ESS services */
  27. int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr)
  28. {
  29. ASN1_STRING *str;
  30. CMS_ReceiptRequest *rr;
  31. ASN1_OBJECT *obj = OBJ_nid2obj(NID_id_smime_aa_receiptRequest);
  32. if (prr != NULL)
  33. *prr = NULL;
  34. str = CMS_signed_get0_data_by_OBJ(si, obj, -3, V_ASN1_SEQUENCE);
  35. if (str == NULL)
  36. return 0;
  37. rr = ASN1_item_unpack(str, ASN1_ITEM_rptr(CMS_ReceiptRequest));
  38. if (rr == NULL)
  39. return -1;
  40. if (prr != NULL)
  41. *prr = rr;
  42. else
  43. CMS_ReceiptRequest_free(rr);
  44. return 1;
  45. }
  46. /*
  47. First, get the ESS_SIGNING_CERT(V2) signed attribute from |si|.
  48. Then check matching of each cert of trust |chain| with one of
  49. the |cert_ids|(Hash+IssuerID) list from this ESS_SIGNING_CERT.
  50. Derived from ts_check_signing_certs()
  51. */
  52. int ess_check_signing_certs(CMS_SignerInfo *si, STACK_OF(X509) *chain)
  53. {
  54. ESS_SIGNING_CERT *ss = NULL;
  55. ESS_SIGNING_CERT_V2 *ssv2 = NULL;
  56. X509 *cert;
  57. int i = 0, ret = 0;
  58. if (cms_signerinfo_get_signing_cert(si, &ss) > 0 && ss->cert_ids != NULL) {
  59. STACK_OF(ESS_CERT_ID) *cert_ids = ss->cert_ids;
  60. cert = sk_X509_value(chain, 0);
  61. if (ess_find_cert(cert_ids, cert) != 0)
  62. goto err;
  63. /*
  64. * Check the other certificates of the chain.
  65. * Fail if no signing certificate ids found for each certificate.
  66. */
  67. if (sk_ESS_CERT_ID_num(cert_ids) > 1) {
  68. /* for each chain cert, try to find its cert id */
  69. for (i = 1; i < sk_X509_num(chain); ++i) {
  70. cert = sk_X509_value(chain, i);
  71. if (ess_find_cert(cert_ids, cert) < 0)
  72. goto err;
  73. }
  74. }
  75. } else if (cms_signerinfo_get_signing_cert_v2(si, &ssv2) > 0
  76. && ssv2->cert_ids!= NULL) {
  77. STACK_OF(ESS_CERT_ID_V2) *cert_ids_v2 = ssv2->cert_ids;
  78. cert = sk_X509_value(chain, 0);
  79. if (ess_find_cert_v2(cert_ids_v2, cert) != 0)
  80. goto err;
  81. /*
  82. * Check the other certificates of the chain.
  83. * Fail if no signing certificate ids found for each certificate.
  84. */
  85. if (sk_ESS_CERT_ID_V2_num(cert_ids_v2) > 1) {
  86. /* for each chain cert, try to find its cert id */
  87. for (i = 1; i < sk_X509_num(chain); ++i) {
  88. cert = sk_X509_value(chain, i);
  89. if (ess_find_cert_v2(cert_ids_v2, cert) < 0)
  90. goto err;
  91. }
  92. }
  93. } else {
  94. CMSerr(CMS_F_ESS_CHECK_SIGNING_CERTS,
  95. CMS_R_ESS_NO_SIGNING_CERTID_ATTRIBUTE);
  96. return 0;
  97. }
  98. ret = 1;
  99. err:
  100. if (!ret)
  101. CMSerr(CMS_F_ESS_CHECK_SIGNING_CERTS,
  102. CMS_R_ESS_SIGNING_CERTID_MISMATCH_ERROR);
  103. ESS_SIGNING_CERT_free(ss);
  104. ESS_SIGNING_CERT_V2_free(ssv2);
  105. return ret;
  106. }
  107. CMS_ReceiptRequest *CMS_ReceiptRequest_create0(unsigned char *id, int idlen,
  108. int allorfirst,
  109. STACK_OF(GENERAL_NAMES)
  110. *receiptList, STACK_OF(GENERAL_NAMES)
  111. *receiptsTo)
  112. {
  113. CMS_ReceiptRequest *rr;
  114. rr = CMS_ReceiptRequest_new();
  115. if (rr == NULL)
  116. goto merr;
  117. if (id)
  118. ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
  119. else {
  120. if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
  121. goto merr;
  122. if (RAND_bytes(rr->signedContentIdentifier->data, 32) <= 0)
  123. goto err;
  124. }
  125. sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
  126. rr->receiptsTo = receiptsTo;
  127. if (receiptList) {
  128. rr->receiptsFrom->type = 1;
  129. rr->receiptsFrom->d.receiptList = receiptList;
  130. } else {
  131. rr->receiptsFrom->type = 0;
  132. rr->receiptsFrom->d.allOrFirstTier = allorfirst;
  133. }
  134. return rr;
  135. merr:
  136. CMSerr(CMS_F_CMS_RECEIPTREQUEST_CREATE0, ERR_R_MALLOC_FAILURE);
  137. err:
  138. CMS_ReceiptRequest_free(rr);
  139. return NULL;
  140. }
  141. int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
  142. {
  143. unsigned char *rrder = NULL;
  144. int rrderlen, r = 0;
  145. rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
  146. if (rrderlen < 0)
  147. goto merr;
  148. if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
  149. V_ASN1_SEQUENCE, rrder, rrderlen))
  150. goto merr;
  151. r = 1;
  152. merr:
  153. if (!r)
  154. CMSerr(CMS_F_CMS_ADD1_RECEIPTREQUEST, ERR_R_MALLOC_FAILURE);
  155. OPENSSL_free(rrder);
  156. return r;
  157. }
  158. void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
  159. ASN1_STRING **pcid,
  160. int *pallorfirst,
  161. STACK_OF(GENERAL_NAMES) **plist,
  162. STACK_OF(GENERAL_NAMES) **prto)
  163. {
  164. if (pcid)
  165. *pcid = rr->signedContentIdentifier;
  166. if (rr->receiptsFrom->type == 0) {
  167. if (pallorfirst)
  168. *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
  169. if (plist)
  170. *plist = NULL;
  171. } else {
  172. if (pallorfirst)
  173. *pallorfirst = -1;
  174. if (plist)
  175. *plist = rr->receiptsFrom->d.receiptList;
  176. }
  177. if (prto)
  178. *prto = rr->receiptsTo;
  179. }
  180. /* Digest a SignerInfo structure for msgSigDigest attribute processing */
  181. static int cms_msgSigDigest(CMS_SignerInfo *si,
  182. unsigned char *dig, unsigned int *diglen)
  183. {
  184. const EVP_MD *md;
  185. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  186. if (md == NULL)
  187. return 0;
  188. if (!ASN1_item_digest(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
  189. si->signedAttrs, dig, diglen))
  190. return 0;
  191. return 1;
  192. }
  193. /* Add a msgSigDigest attribute to a SignerInfo */
  194. int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
  195. {
  196. unsigned char dig[EVP_MAX_MD_SIZE];
  197. unsigned int diglen;
  198. if (!cms_msgSigDigest(src, dig, &diglen)) {
  199. CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, CMS_R_MSGSIGDIGEST_ERROR);
  200. return 0;
  201. }
  202. if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
  203. V_ASN1_OCTET_STRING, dig, diglen)) {
  204. CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, ERR_R_MALLOC_FAILURE);
  205. return 0;
  206. }
  207. return 1;
  208. }
  209. /* Verify signed receipt after it has already passed normal CMS verify */
  210. int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
  211. {
  212. int r = 0, i;
  213. CMS_ReceiptRequest *rr = NULL;
  214. CMS_Receipt *rct = NULL;
  215. STACK_OF(CMS_SignerInfo) *sis, *osis;
  216. CMS_SignerInfo *si, *osi = NULL;
  217. ASN1_OCTET_STRING *msig, **pcont;
  218. ASN1_OBJECT *octype;
  219. unsigned char dig[EVP_MAX_MD_SIZE];
  220. unsigned int diglen;
  221. /* Get SignerInfos, also checks SignedData content type */
  222. osis = CMS_get0_SignerInfos(req_cms);
  223. sis = CMS_get0_SignerInfos(cms);
  224. if (!osis || !sis)
  225. goto err;
  226. if (sk_CMS_SignerInfo_num(sis) != 1) {
  227. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NEED_ONE_SIGNER);
  228. goto err;
  229. }
  230. /* Check receipt content type */
  231. if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
  232. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NOT_A_SIGNED_RECEIPT);
  233. goto err;
  234. }
  235. /* Extract and decode receipt content */
  236. pcont = CMS_get0_content(cms);
  237. if (pcont == NULL || *pcont == NULL) {
  238. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT);
  239. goto err;
  240. }
  241. rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
  242. if (!rct) {
  243. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_RECEIPT_DECODE_ERROR);
  244. goto err;
  245. }
  246. /* Locate original request */
  247. for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
  248. osi = sk_CMS_SignerInfo_value(osis, i);
  249. if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
  250. break;
  251. }
  252. if (i == sk_CMS_SignerInfo_num(osis)) {
  253. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MATCHING_SIGNATURE);
  254. goto err;
  255. }
  256. si = sk_CMS_SignerInfo_value(sis, 0);
  257. /* Get msgSigDigest value and compare */
  258. msig = CMS_signed_get0_data_by_OBJ(si,
  259. OBJ_nid2obj
  260. (NID_id_smime_aa_msgSigDigest), -3,
  261. V_ASN1_OCTET_STRING);
  262. if (!msig) {
  263. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MSGSIGDIGEST);
  264. goto err;
  265. }
  266. if (!cms_msgSigDigest(osi, dig, &diglen)) {
  267. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_ERROR);
  268. goto err;
  269. }
  270. if (diglen != (unsigned int)msig->length) {
  271. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
  272. goto err;
  273. }
  274. if (memcmp(dig, msig->data, diglen)) {
  275. CMSerr(CMS_F_CMS_RECEIPT_VERIFY,
  276. CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
  277. goto err;
  278. }
  279. /* Compare content types */
  280. octype = CMS_signed_get0_data_by_OBJ(osi,
  281. OBJ_nid2obj(NID_pkcs9_contentType),
  282. -3, V_ASN1_OBJECT);
  283. if (!octype) {
  284. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT_TYPE);
  285. goto err;
  286. }
  287. /* Compare details in receipt request */
  288. if (OBJ_cmp(octype, rct->contentType)) {
  289. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENT_TYPE_MISMATCH);
  290. goto err;
  291. }
  292. /* Get original receipt request details */
  293. if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
  294. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_RECEIPT_REQUEST);
  295. goto err;
  296. }
  297. if (ASN1_STRING_cmp(rr->signedContentIdentifier,
  298. rct->signedContentIdentifier)) {
  299. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENTIDENTIFIER_MISMATCH);
  300. goto err;
  301. }
  302. r = 1;
  303. err:
  304. CMS_ReceiptRequest_free(rr);
  305. M_ASN1_free_of(rct, CMS_Receipt);
  306. return r;
  307. }
  308. /*
  309. * Encode a Receipt into an OCTET STRING read for including into content of a
  310. * SignedData ContentInfo.
  311. */
  312. ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si)
  313. {
  314. CMS_Receipt rct;
  315. CMS_ReceiptRequest *rr = NULL;
  316. ASN1_OBJECT *ctype;
  317. ASN1_OCTET_STRING *os = NULL;
  318. /* Get original receipt request */
  319. /* Get original receipt request details */
  320. if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
  321. CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_RECEIPT_REQUEST);
  322. goto err;
  323. }
  324. /* Get original content type */
  325. ctype = CMS_signed_get0_data_by_OBJ(si,
  326. OBJ_nid2obj(NID_pkcs9_contentType),
  327. -3, V_ASN1_OBJECT);
  328. if (!ctype) {
  329. CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_CONTENT_TYPE);
  330. goto err;
  331. }
  332. rct.version = 1;
  333. rct.contentType = ctype;
  334. rct.signedContentIdentifier = rr->signedContentIdentifier;
  335. rct.originatorSignatureValue = si->signature;
  336. os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
  337. err:
  338. CMS_ReceiptRequest_free(rr);
  339. return os;
  340. }
  341. /*
  342. * Add signer certificate's V2 digest |sc| to a SignerInfo structure |si|
  343. */
  344. int cms_add1_signing_cert_v2(CMS_SignerInfo *si, ESS_SIGNING_CERT_V2 *sc)
  345. {
  346. ASN1_STRING *seq = NULL;
  347. unsigned char *p, *pp;
  348. int len;
  349. /* Add SigningCertificateV2 signed attribute to the signer info. */
  350. len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
  351. if ((pp = OPENSSL_malloc(len)) == NULL)
  352. goto err;
  353. p = pp;
  354. i2d_ESS_SIGNING_CERT_V2(sc, &p);
  355. if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
  356. goto err;
  357. OPENSSL_free(pp);
  358. pp = NULL;
  359. if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificateV2,
  360. V_ASN1_SEQUENCE, seq, -1))
  361. goto err;
  362. ASN1_STRING_free(seq);
  363. return 1;
  364. err:
  365. CMSerr(CMS_F_CMS_ADD1_SIGNING_CERT_V2, ERR_R_MALLOC_FAILURE);
  366. ASN1_STRING_free(seq);
  367. OPENSSL_free(pp);
  368. return 0;
  369. }
  370. /*
  371. * Add signer certificate's digest |sc| to a SignerInfo structure |si|
  372. */
  373. int cms_add1_signing_cert(CMS_SignerInfo *si, ESS_SIGNING_CERT *sc)
  374. {
  375. ASN1_STRING *seq = NULL;
  376. unsigned char *p, *pp;
  377. int len;
  378. /* Add SigningCertificate signed attribute to the signer info. */
  379. len = i2d_ESS_SIGNING_CERT(sc, NULL);
  380. if ((pp = OPENSSL_malloc(len)) == NULL)
  381. goto err;
  382. p = pp;
  383. i2d_ESS_SIGNING_CERT(sc, &p);
  384. if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
  385. goto err;
  386. OPENSSL_free(pp);
  387. pp = NULL;
  388. if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificate,
  389. V_ASN1_SEQUENCE, seq, -1))
  390. goto err;
  391. ASN1_STRING_free(seq);
  392. return 1;
  393. err:
  394. CMSerr(CMS_F_CMS_ADD1_SIGNING_CERT, ERR_R_MALLOC_FAILURE);
  395. ASN1_STRING_free(seq);
  396. OPENSSL_free(pp);
  397. return 0;
  398. }