cms_ess.c 12 KB

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