cms_ess.c 12 KB

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