cms_ess.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* crypto/cms/cms_ess.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. */
  54. #include "cryptlib.h"
  55. #include <openssl/asn1t.h>
  56. #include <openssl/pem.h>
  57. #include <openssl/rand.h>
  58. #include <openssl/x509v3.h>
  59. #include <openssl/err.h>
  60. #include <openssl/cms.h>
  61. #include "cms_lcl.h"
  62. DECLARE_ASN1_ITEM(CMS_ReceiptRequest)
  63. DECLARE_ASN1_ITEM(CMS_Receipt)
  64. IMPLEMENT_ASN1_FUNCTIONS_const(CMS_ReceiptRequest)
  65. /* ESS services: for now just Signed Receipt related */
  66. int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr)
  67. {
  68. ASN1_STRING *str;
  69. CMS_ReceiptRequest *rr = NULL;
  70. if (prr)
  71. *prr = NULL;
  72. str = CMS_signed_get0_data_by_OBJ(si,
  73. OBJ_nid2obj
  74. (NID_id_smime_aa_receiptRequest), -3,
  75. V_ASN1_SEQUENCE);
  76. if (!str)
  77. return 0;
  78. rr = ASN1_item_unpack(str, ASN1_ITEM_rptr(CMS_ReceiptRequest));
  79. if (!rr)
  80. return -1;
  81. if (prr)
  82. *prr = rr;
  83. else
  84. CMS_ReceiptRequest_free(rr);
  85. return 1;
  86. }
  87. CMS_ReceiptRequest *CMS_ReceiptRequest_create0(unsigned char *id, int idlen,
  88. int allorfirst,
  89. STACK_OF(GENERAL_NAMES)
  90. *receiptList, STACK_OF(GENERAL_NAMES)
  91. *receiptsTo)
  92. {
  93. CMS_ReceiptRequest *rr = NULL;
  94. rr = CMS_ReceiptRequest_new();
  95. if (!rr)
  96. goto merr;
  97. if (id)
  98. ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
  99. else {
  100. if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
  101. goto merr;
  102. if (RAND_pseudo_bytes(rr->signedContentIdentifier->data, 32)
  103. <= 0)
  104. goto err;
  105. }
  106. sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
  107. rr->receiptsTo = receiptsTo;
  108. if (receiptList) {
  109. rr->receiptsFrom->type = 1;
  110. rr->receiptsFrom->d.receiptList = receiptList;
  111. } else {
  112. rr->receiptsFrom->type = 0;
  113. rr->receiptsFrom->d.allOrFirstTier = allorfirst;
  114. }
  115. return rr;
  116. merr:
  117. CMSerr(CMS_F_CMS_RECEIPTREQUEST_CREATE0, ERR_R_MALLOC_FAILURE);
  118. err:
  119. if (rr)
  120. CMS_ReceiptRequest_free(rr);
  121. return NULL;
  122. }
  123. int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
  124. {
  125. unsigned char *rrder = NULL;
  126. int rrderlen, r = 0;
  127. rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
  128. if (rrderlen < 0)
  129. goto merr;
  130. if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
  131. V_ASN1_SEQUENCE, rrder, rrderlen))
  132. goto merr;
  133. r = 1;
  134. merr:
  135. if (!r)
  136. CMSerr(CMS_F_CMS_ADD1_RECEIPTREQUEST, ERR_R_MALLOC_FAILURE);
  137. if (rrder)
  138. OPENSSL_free(rrder);
  139. return r;
  140. }
  141. void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
  142. ASN1_STRING **pcid,
  143. int *pallorfirst,
  144. STACK_OF(GENERAL_NAMES) **plist,
  145. STACK_OF(GENERAL_NAMES) **prto)
  146. {
  147. if (pcid)
  148. *pcid = rr->signedContentIdentifier;
  149. if (rr->receiptsFrom->type == 0) {
  150. if (pallorfirst)
  151. *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
  152. if (plist)
  153. *plist = NULL;
  154. } else {
  155. if (pallorfirst)
  156. *pallorfirst = -1;
  157. if (plist)
  158. *plist = rr->receiptsFrom->d.receiptList;
  159. }
  160. if (prto)
  161. *prto = rr->receiptsTo;
  162. }
  163. /* Digest a SignerInfo structure for msgSigDigest attribute processing */
  164. static int cms_msgSigDigest(CMS_SignerInfo *si,
  165. unsigned char *dig, unsigned int *diglen)
  166. {
  167. const EVP_MD *md;
  168. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  169. if (md == NULL)
  170. return 0;
  171. if (!ASN1_item_digest(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
  172. si->signedAttrs, dig, diglen))
  173. return 0;
  174. return 1;
  175. }
  176. /* Add a msgSigDigest attribute to a SignerInfo */
  177. int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
  178. {
  179. unsigned char dig[EVP_MAX_MD_SIZE];
  180. unsigned int diglen;
  181. if (!cms_msgSigDigest(src, dig, &diglen)) {
  182. CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, CMS_R_MSGSIGDIGEST_ERROR);
  183. return 0;
  184. }
  185. if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
  186. V_ASN1_OCTET_STRING, dig, diglen)) {
  187. CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, ERR_R_MALLOC_FAILURE);
  188. return 0;
  189. }
  190. return 1;
  191. }
  192. /* Verify signed receipt after it has already passed normal CMS verify */
  193. int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
  194. {
  195. int r = 0, i;
  196. CMS_ReceiptRequest *rr = NULL;
  197. CMS_Receipt *rct = NULL;
  198. STACK_OF(CMS_SignerInfo) *sis, *osis;
  199. CMS_SignerInfo *si, *osi = NULL;
  200. ASN1_OCTET_STRING *msig, **pcont;
  201. ASN1_OBJECT *octype;
  202. unsigned char dig[EVP_MAX_MD_SIZE];
  203. unsigned int diglen;
  204. /* Get SignerInfos, also checks SignedData content type */
  205. osis = CMS_get0_SignerInfos(req_cms);
  206. sis = CMS_get0_SignerInfos(cms);
  207. if (!osis || !sis)
  208. goto err;
  209. if (sk_CMS_SignerInfo_num(sis) != 1) {
  210. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NEED_ONE_SIGNER);
  211. goto err;
  212. }
  213. /* Check receipt content type */
  214. if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
  215. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NOT_A_SIGNED_RECEIPT);
  216. goto err;
  217. }
  218. /* Extract and decode receipt content */
  219. pcont = CMS_get0_content(cms);
  220. if (!pcont || !*pcont) {
  221. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT);
  222. goto err;
  223. }
  224. rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
  225. if (!rct) {
  226. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_RECEIPT_DECODE_ERROR);
  227. goto err;
  228. }
  229. /* Locate original request */
  230. for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
  231. osi = sk_CMS_SignerInfo_value(osis, i);
  232. if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
  233. break;
  234. }
  235. if (i == sk_CMS_SignerInfo_num(osis)) {
  236. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MATCHING_SIGNATURE);
  237. goto err;
  238. }
  239. si = sk_CMS_SignerInfo_value(sis, 0);
  240. /* Get msgSigDigest value and compare */
  241. msig = CMS_signed_get0_data_by_OBJ(si,
  242. OBJ_nid2obj
  243. (NID_id_smime_aa_msgSigDigest), -3,
  244. V_ASN1_OCTET_STRING);
  245. if (!msig) {
  246. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MSGSIGDIGEST);
  247. goto err;
  248. }
  249. if (!cms_msgSigDigest(osi, dig, &diglen)) {
  250. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_ERROR);
  251. goto err;
  252. }
  253. if (diglen != (unsigned int)msig->length) {
  254. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
  255. goto err;
  256. }
  257. if (memcmp(dig, msig->data, diglen)) {
  258. CMSerr(CMS_F_CMS_RECEIPT_VERIFY,
  259. CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
  260. goto err;
  261. }
  262. /* Compare content types */
  263. octype = CMS_signed_get0_data_by_OBJ(osi,
  264. OBJ_nid2obj(NID_pkcs9_contentType),
  265. -3, V_ASN1_OBJECT);
  266. if (!octype) {
  267. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT_TYPE);
  268. goto err;
  269. }
  270. /* Compare details in receipt request */
  271. if (OBJ_cmp(octype, rct->contentType)) {
  272. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENT_TYPE_MISMATCH);
  273. goto err;
  274. }
  275. /* Get original receipt request details */
  276. if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
  277. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_RECEIPT_REQUEST);
  278. goto err;
  279. }
  280. if (ASN1_STRING_cmp(rr->signedContentIdentifier,
  281. rct->signedContentIdentifier)) {
  282. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENTIDENTIFIER_MISMATCH);
  283. goto err;
  284. }
  285. r = 1;
  286. err:
  287. if (rr)
  288. CMS_ReceiptRequest_free(rr);
  289. if (rct)
  290. M_ASN1_free_of(rct, CMS_Receipt);
  291. return r;
  292. }
  293. /*
  294. * Encode a Receipt into an OCTET STRING read for including into content of a
  295. * SignedData ContentInfo.
  296. */
  297. ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si)
  298. {
  299. CMS_Receipt rct;
  300. CMS_ReceiptRequest *rr = NULL;
  301. ASN1_OBJECT *ctype;
  302. ASN1_OCTET_STRING *os = NULL;
  303. /* Get original receipt request */
  304. /* Get original receipt request details */
  305. if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
  306. CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_RECEIPT_REQUEST);
  307. goto err;
  308. }
  309. /* Get original content type */
  310. ctype = CMS_signed_get0_data_by_OBJ(si,
  311. OBJ_nid2obj(NID_pkcs9_contentType),
  312. -3, V_ASN1_OBJECT);
  313. if (!ctype) {
  314. CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_CONTENT_TYPE);
  315. goto err;
  316. }
  317. rct.version = 1;
  318. rct.contentType = ctype;
  319. rct.signedContentIdentifier = rr->signedContentIdentifier;
  320. rct.originatorSignatureValue = si->signature;
  321. os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
  322. err:
  323. if (rr)
  324. CMS_ReceiptRequest_free(rr);
  325. return os;
  326. }