cms_ess.c 14 KB

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