cms_ess.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright 2008-2021 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. int ossl_cms_check_signing_certs(const CMS_SignerInfo *si,
  43. const STACK_OF(X509) *chain)
  44. {
  45. ESS_SIGNING_CERT *ss = NULL;
  46. ESS_SIGNING_CERT_V2 *ssv2 = NULL;
  47. int ret = ossl_cms_signerinfo_get_signing_cert(si, &ss) >= 0
  48. && ossl_cms_signerinfo_get_signing_cert_v2(si, &ssv2) >= 0
  49. && ossl_ess_check_signing_certs(ss, ssv2, chain, 1);
  50. ESS_SIGNING_CERT_free(ss);
  51. ESS_SIGNING_CERT_V2_free(ssv2);
  52. return ret;
  53. }
  54. CMS_ReceiptRequest *CMS_ReceiptRequest_create0_ex(
  55. unsigned char *id, int idlen, int allorfirst,
  56. STACK_OF(GENERAL_NAMES) *receiptList, STACK_OF(GENERAL_NAMES) *receiptsTo,
  57. OSSL_LIB_CTX *libctx, const char *propq)
  58. {
  59. CMS_ReceiptRequest *rr;
  60. rr = CMS_ReceiptRequest_new();
  61. if (rr == NULL)
  62. goto merr;
  63. if (id)
  64. ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
  65. else {
  66. if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
  67. goto merr;
  68. if (RAND_bytes_ex(libctx, rr->signedContentIdentifier->data, 32) <= 0)
  69. goto err;
  70. }
  71. sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
  72. rr->receiptsTo = receiptsTo;
  73. if (receiptList != NULL) {
  74. rr->receiptsFrom->type = 1;
  75. rr->receiptsFrom->d.receiptList = receiptList;
  76. } else {
  77. rr->receiptsFrom->type = 0;
  78. rr->receiptsFrom->d.allOrFirstTier = allorfirst;
  79. }
  80. return rr;
  81. merr:
  82. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  83. err:
  84. CMS_ReceiptRequest_free(rr);
  85. return NULL;
  86. }
  87. CMS_ReceiptRequest *CMS_ReceiptRequest_create0(
  88. unsigned char *id, int idlen, int allorfirst,
  89. STACK_OF(GENERAL_NAMES) *receiptList, STACK_OF(GENERAL_NAMES) *receiptsTo)
  90. {
  91. return CMS_ReceiptRequest_create0_ex(id, idlen, allorfirst, receiptList,
  92. receiptsTo, NULL, NULL);
  93. }
  94. int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
  95. {
  96. unsigned char *rrder = NULL;
  97. int rrderlen, r = 0;
  98. rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
  99. if (rrderlen < 0)
  100. goto merr;
  101. if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
  102. V_ASN1_SEQUENCE, rrder, rrderlen))
  103. goto merr;
  104. r = 1;
  105. merr:
  106. if (!r)
  107. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  108. OPENSSL_free(rrder);
  109. return r;
  110. }
  111. void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
  112. ASN1_STRING **pcid,
  113. int *pallorfirst,
  114. STACK_OF(GENERAL_NAMES) **plist,
  115. STACK_OF(GENERAL_NAMES) **prto)
  116. {
  117. if (pcid != NULL)
  118. *pcid = rr->signedContentIdentifier;
  119. if (rr->receiptsFrom->type == 0) {
  120. if (pallorfirst != NULL)
  121. *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
  122. if (plist != NULL)
  123. *plist = NULL;
  124. } else {
  125. if (pallorfirst != NULL)
  126. *pallorfirst = -1;
  127. if (plist != NULL)
  128. *plist = rr->receiptsFrom->d.receiptList;
  129. }
  130. if (prto != NULL)
  131. *prto = rr->receiptsTo;
  132. }
  133. /* Digest a SignerInfo structure for msgSigDigest attribute processing */
  134. static int cms_msgSigDigest(CMS_SignerInfo *si,
  135. unsigned char *dig, unsigned int *diglen)
  136. {
  137. const EVP_MD *md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  138. if (md == NULL)
  139. return 0;
  140. if (!ossl_asn1_item_digest_ex(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
  141. si->signedAttrs, dig, diglen,
  142. ossl_cms_ctx_get0_libctx(si->cms_ctx),
  143. ossl_cms_ctx_get0_propq(si->cms_ctx)))
  144. return 0;
  145. return 1;
  146. }
  147. /* Add a msgSigDigest attribute to a SignerInfo */
  148. int ossl_cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
  149. {
  150. unsigned char dig[EVP_MAX_MD_SIZE];
  151. unsigned int diglen;
  152. if (!cms_msgSigDigest(src, dig, &diglen)) {
  153. ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_ERROR);
  154. return 0;
  155. }
  156. if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
  157. V_ASN1_OCTET_STRING, dig, diglen)) {
  158. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  159. return 0;
  160. }
  161. return 1;
  162. }
  163. /* Verify signed receipt after it has already passed normal CMS verify */
  164. int ossl_cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
  165. {
  166. int r = 0, i;
  167. CMS_ReceiptRequest *rr = NULL;
  168. CMS_Receipt *rct = NULL;
  169. STACK_OF(CMS_SignerInfo) *sis, *osis;
  170. CMS_SignerInfo *si, *osi = NULL;
  171. ASN1_OCTET_STRING *msig, **pcont;
  172. ASN1_OBJECT *octype;
  173. unsigned char dig[EVP_MAX_MD_SIZE];
  174. unsigned int diglen;
  175. /* Get SignerInfos, also checks SignedData content type */
  176. osis = CMS_get0_SignerInfos(req_cms);
  177. sis = CMS_get0_SignerInfos(cms);
  178. if (!osis || !sis)
  179. goto err;
  180. if (sk_CMS_SignerInfo_num(sis) != 1) {
  181. ERR_raise(ERR_LIB_CMS, CMS_R_NEED_ONE_SIGNER);
  182. goto err;
  183. }
  184. /* Check receipt content type */
  185. if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
  186. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_A_SIGNED_RECEIPT);
  187. goto err;
  188. }
  189. /* Extract and decode receipt content */
  190. pcont = CMS_get0_content(cms);
  191. if (pcont == NULL || *pcont == NULL) {
  192. ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
  193. goto err;
  194. }
  195. rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
  196. if (!rct) {
  197. ERR_raise(ERR_LIB_CMS, CMS_R_RECEIPT_DECODE_ERROR);
  198. goto err;
  199. }
  200. /* Locate original request */
  201. for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
  202. osi = sk_CMS_SignerInfo_value(osis, i);
  203. if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
  204. break;
  205. }
  206. if (i == sk_CMS_SignerInfo_num(osis)) {
  207. ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_SIGNATURE);
  208. goto err;
  209. }
  210. si = sk_CMS_SignerInfo_value(sis, 0);
  211. /* Get msgSigDigest value and compare */
  212. msig = CMS_signed_get0_data_by_OBJ(si,
  213. OBJ_nid2obj
  214. (NID_id_smime_aa_msgSigDigest), -3,
  215. V_ASN1_OCTET_STRING);
  216. if (!msig) {
  217. ERR_raise(ERR_LIB_CMS, CMS_R_NO_MSGSIGDIGEST);
  218. goto err;
  219. }
  220. if (!cms_msgSigDigest(osi, dig, &diglen)) {
  221. ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_ERROR);
  222. goto err;
  223. }
  224. if (diglen != (unsigned int)msig->length) {
  225. ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
  226. goto err;
  227. }
  228. if (memcmp(dig, msig->data, diglen)) {
  229. ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
  230. goto err;
  231. }
  232. /* Compare content types */
  233. octype = CMS_signed_get0_data_by_OBJ(osi,
  234. OBJ_nid2obj(NID_pkcs9_contentType),
  235. -3, V_ASN1_OBJECT);
  236. if (!octype) {
  237. ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT_TYPE);
  238. goto err;
  239. }
  240. /* Compare details in receipt request */
  241. if (OBJ_cmp(octype, rct->contentType)) {
  242. ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_MISMATCH);
  243. goto err;
  244. }
  245. /* Get original receipt request details */
  246. if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
  247. ERR_raise(ERR_LIB_CMS, CMS_R_NO_RECEIPT_REQUEST);
  248. goto err;
  249. }
  250. if (ASN1_STRING_cmp(rr->signedContentIdentifier,
  251. rct->signedContentIdentifier)) {
  252. ERR_raise(ERR_LIB_CMS, CMS_R_CONTENTIDENTIFIER_MISMATCH);
  253. goto err;
  254. }
  255. r = 1;
  256. err:
  257. CMS_ReceiptRequest_free(rr);
  258. M_ASN1_free_of(rct, CMS_Receipt);
  259. return r;
  260. }
  261. /*
  262. * Encode a Receipt into an OCTET STRING read for including into content of a
  263. * SignedData ContentInfo.
  264. */
  265. ASN1_OCTET_STRING *ossl_cms_encode_Receipt(CMS_SignerInfo *si)
  266. {
  267. CMS_Receipt rct;
  268. CMS_ReceiptRequest *rr = NULL;
  269. ASN1_OBJECT *ctype;
  270. ASN1_OCTET_STRING *os = NULL;
  271. /* Get original receipt request */
  272. /* Get original receipt request details */
  273. if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
  274. ERR_raise(ERR_LIB_CMS, CMS_R_NO_RECEIPT_REQUEST);
  275. goto err;
  276. }
  277. /* Get original content type */
  278. ctype = CMS_signed_get0_data_by_OBJ(si,
  279. OBJ_nid2obj(NID_pkcs9_contentType),
  280. -3, V_ASN1_OBJECT);
  281. if (!ctype) {
  282. ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT_TYPE);
  283. goto err;
  284. }
  285. rct.version = 1;
  286. rct.contentType = ctype;
  287. rct.signedContentIdentifier = rr->signedContentIdentifier;
  288. rct.originatorSignatureValue = si->signature;
  289. os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
  290. err:
  291. CMS_ReceiptRequest_free(rr);
  292. return os;
  293. }
  294. /*
  295. * Add signer certificate's V2 digest |sc| to a SignerInfo structure |si|
  296. */
  297. int ossl_cms_add1_signing_cert_v2(CMS_SignerInfo *si, ESS_SIGNING_CERT_V2 *sc)
  298. {
  299. ASN1_STRING *seq = NULL;
  300. unsigned char *p, *pp = NULL;
  301. int len;
  302. /* Add SigningCertificateV2 signed attribute to the signer info. */
  303. len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
  304. if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
  305. goto err;
  306. p = pp;
  307. i2d_ESS_SIGNING_CERT_V2(sc, &p);
  308. if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
  309. goto err;
  310. OPENSSL_free(pp);
  311. pp = NULL;
  312. if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificateV2,
  313. V_ASN1_SEQUENCE, seq, -1))
  314. goto err;
  315. ASN1_STRING_free(seq);
  316. return 1;
  317. err:
  318. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  319. ASN1_STRING_free(seq);
  320. OPENSSL_free(pp);
  321. return 0;
  322. }
  323. /*
  324. * Add signer certificate's digest |sc| to a SignerInfo structure |si|
  325. */
  326. int ossl_cms_add1_signing_cert(CMS_SignerInfo *si, ESS_SIGNING_CERT *sc)
  327. {
  328. ASN1_STRING *seq = NULL;
  329. unsigned char *p, *pp = NULL;
  330. int len;
  331. /* Add SigningCertificate signed attribute to the signer info. */
  332. len = i2d_ESS_SIGNING_CERT(sc, NULL);
  333. if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
  334. goto err;
  335. p = pp;
  336. i2d_ESS_SIGNING_CERT(sc, &p);
  337. if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
  338. goto err;
  339. OPENSSL_free(pp);
  340. pp = NULL;
  341. if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificate,
  342. V_ASN1_SEQUENCE, seq, -1))
  343. goto err;
  344. ASN1_STRING_free(seq);
  345. return 1;
  346. err:
  347. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  348. ASN1_STRING_free(seq);
  349. OPENSSL_free(pp);
  350. return 0;
  351. }