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(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_bytes(rr->signedContentIdentifier->data, 32) <= 0)
  103. goto err;
  104. }
  105. sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
  106. rr->receiptsTo = receiptsTo;
  107. if (receiptList) {
  108. rr->receiptsFrom->type = 1;
  109. rr->receiptsFrom->d.receiptList = receiptList;
  110. } else {
  111. rr->receiptsFrom->type = 0;
  112. rr->receiptsFrom->d.allOrFirstTier = allorfirst;
  113. }
  114. return rr;
  115. merr:
  116. CMSerr(CMS_F_CMS_RECEIPTREQUEST_CREATE0, ERR_R_MALLOC_FAILURE);
  117. err:
  118. if (rr)
  119. CMS_ReceiptRequest_free(rr);
  120. return NULL;
  121. }
  122. int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
  123. {
  124. unsigned char *rrder = NULL;
  125. int rrderlen, r = 0;
  126. rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
  127. if (rrderlen < 0)
  128. goto merr;
  129. if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
  130. V_ASN1_SEQUENCE, rrder, rrderlen))
  131. goto merr;
  132. r = 1;
  133. merr:
  134. if (!r)
  135. CMSerr(CMS_F_CMS_ADD1_RECEIPTREQUEST, ERR_R_MALLOC_FAILURE);
  136. if (rrder)
  137. OPENSSL_free(rrder);
  138. return r;
  139. }
  140. void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
  141. ASN1_STRING **pcid,
  142. int *pallorfirst,
  143. STACK_OF(GENERAL_NAMES) **plist,
  144. STACK_OF(GENERAL_NAMES) **prto)
  145. {
  146. if (pcid)
  147. *pcid = rr->signedContentIdentifier;
  148. if (rr->receiptsFrom->type == 0) {
  149. if (pallorfirst)
  150. *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
  151. if (plist)
  152. *plist = NULL;
  153. } else {
  154. if (pallorfirst)
  155. *pallorfirst = -1;
  156. if (plist)
  157. *plist = rr->receiptsFrom->d.receiptList;
  158. }
  159. if (prto)
  160. *prto = rr->receiptsTo;
  161. }
  162. /* Digest a SignerInfo structure for msgSigDigest attribute processing */
  163. static int cms_msgSigDigest(CMS_SignerInfo *si,
  164. unsigned char *dig, unsigned int *diglen)
  165. {
  166. const EVP_MD *md;
  167. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  168. if (md == NULL)
  169. return 0;
  170. if (!ASN1_item_digest(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
  171. si->signedAttrs, dig, diglen))
  172. return 0;
  173. return 1;
  174. }
  175. /* Add a msgSigDigest attribute to a SignerInfo */
  176. int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
  177. {
  178. unsigned char dig[EVP_MAX_MD_SIZE];
  179. unsigned int diglen;
  180. if (!cms_msgSigDigest(src, dig, &diglen)) {
  181. CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, CMS_R_MSGSIGDIGEST_ERROR);
  182. return 0;
  183. }
  184. if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
  185. V_ASN1_OCTET_STRING, dig, diglen)) {
  186. CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, ERR_R_MALLOC_FAILURE);
  187. return 0;
  188. }
  189. return 1;
  190. }
  191. /* Verify signed receipt after it has already passed normal CMS verify */
  192. int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
  193. {
  194. int r = 0, i;
  195. CMS_ReceiptRequest *rr = NULL;
  196. CMS_Receipt *rct = NULL;
  197. STACK_OF(CMS_SignerInfo) *sis, *osis;
  198. CMS_SignerInfo *si, *osi = NULL;
  199. ASN1_OCTET_STRING *msig, **pcont;
  200. ASN1_OBJECT *octype;
  201. unsigned char dig[EVP_MAX_MD_SIZE];
  202. unsigned int diglen;
  203. /* Get SignerInfos, also checks SignedData content type */
  204. osis = CMS_get0_SignerInfos(req_cms);
  205. sis = CMS_get0_SignerInfos(cms);
  206. if (!osis || !sis)
  207. goto err;
  208. if (sk_CMS_SignerInfo_num(sis) != 1) {
  209. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NEED_ONE_SIGNER);
  210. goto err;
  211. }
  212. /* Check receipt content type */
  213. if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
  214. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NOT_A_SIGNED_RECEIPT);
  215. goto err;
  216. }
  217. /* Extract and decode receipt content */
  218. pcont = CMS_get0_content(cms);
  219. if (!pcont || !*pcont) {
  220. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT);
  221. goto err;
  222. }
  223. rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
  224. if (!rct) {
  225. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_RECEIPT_DECODE_ERROR);
  226. goto err;
  227. }
  228. /* Locate original request */
  229. for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
  230. osi = sk_CMS_SignerInfo_value(osis, i);
  231. if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
  232. break;
  233. }
  234. if (i == sk_CMS_SignerInfo_num(osis)) {
  235. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MATCHING_SIGNATURE);
  236. goto err;
  237. }
  238. si = sk_CMS_SignerInfo_value(sis, 0);
  239. /* Get msgSigDigest value and compare */
  240. msig = CMS_signed_get0_data_by_OBJ(si,
  241. OBJ_nid2obj
  242. (NID_id_smime_aa_msgSigDigest), -3,
  243. V_ASN1_OCTET_STRING);
  244. if (!msig) {
  245. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MSGSIGDIGEST);
  246. goto err;
  247. }
  248. if (!cms_msgSigDigest(osi, dig, &diglen)) {
  249. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_ERROR);
  250. goto err;
  251. }
  252. if (diglen != (unsigned int)msig->length) {
  253. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
  254. goto err;
  255. }
  256. if (memcmp(dig, msig->data, diglen)) {
  257. CMSerr(CMS_F_CMS_RECEIPT_VERIFY,
  258. CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
  259. goto err;
  260. }
  261. /* Compare content types */
  262. octype = CMS_signed_get0_data_by_OBJ(osi,
  263. OBJ_nid2obj(NID_pkcs9_contentType),
  264. -3, V_ASN1_OBJECT);
  265. if (!octype) {
  266. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT_TYPE);
  267. goto err;
  268. }
  269. /* Compare details in receipt request */
  270. if (OBJ_cmp(octype, rct->contentType)) {
  271. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENT_TYPE_MISMATCH);
  272. goto err;
  273. }
  274. /* Get original receipt request details */
  275. if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
  276. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_RECEIPT_REQUEST);
  277. goto err;
  278. }
  279. if (ASN1_STRING_cmp(rr->signedContentIdentifier,
  280. rct->signedContentIdentifier)) {
  281. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENTIDENTIFIER_MISMATCH);
  282. goto err;
  283. }
  284. r = 1;
  285. err:
  286. if (rr)
  287. CMS_ReceiptRequest_free(rr);
  288. if (rct)
  289. M_ASN1_free_of(rct, CMS_Receipt);
  290. return r;
  291. }
  292. /*
  293. * Encode a Receipt into an OCTET STRING read for including into content of a
  294. * SignedData ContentInfo.
  295. */
  296. ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si)
  297. {
  298. CMS_Receipt rct;
  299. CMS_ReceiptRequest *rr = NULL;
  300. ASN1_OBJECT *ctype;
  301. ASN1_OCTET_STRING *os = NULL;
  302. /* Get original receipt request */
  303. /* Get original receipt request details */
  304. if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
  305. CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_RECEIPT_REQUEST);
  306. goto err;
  307. }
  308. /* Get original content type */
  309. ctype = CMS_signed_get0_data_by_OBJ(si,
  310. OBJ_nid2obj(NID_pkcs9_contentType),
  311. -3, V_ASN1_OBJECT);
  312. if (!ctype) {
  313. CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_CONTENT_TYPE);
  314. goto err;
  315. }
  316. rct.version = 1;
  317. rct.contentType = ctype;
  318. rct.signedContentIdentifier = rr->signedContentIdentifier;
  319. rct.originatorSignatureValue = si->signature;
  320. os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
  321. err:
  322. if (rr)
  323. CMS_ReceiptRequest_free(rr);
  324. return os;
  325. }