A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
9.4 KiB

  1. /*
  2. * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "cms_lcl.h"
  17. IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest)
  18. /* ESS services: for now just Signed Receipt related */
  19. int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr)
  20. {
  21. ASN1_STRING *str;
  22. CMS_ReceiptRequest *rr = NULL;
  23. if (prr)
  24. *prr = NULL;
  25. str = CMS_signed_get0_data_by_OBJ(si,
  26. OBJ_nid2obj
  27. (NID_id_smime_aa_receiptRequest), -3,
  28. V_ASN1_SEQUENCE);
  29. if (!str)
  30. return 0;
  31. rr = ASN1_item_unpack(str, ASN1_ITEM_rptr(CMS_ReceiptRequest));
  32. if (!rr)
  33. return -1;
  34. if (prr)
  35. *prr = rr;
  36. else
  37. CMS_ReceiptRequest_free(rr);
  38. return 1;
  39. }
  40. CMS_ReceiptRequest *CMS_ReceiptRequest_create0(unsigned char *id, int idlen,
  41. int allorfirst,
  42. STACK_OF(GENERAL_NAMES)
  43. *receiptList, STACK_OF(GENERAL_NAMES)
  44. *receiptsTo)
  45. {
  46. CMS_ReceiptRequest *rr = NULL;
  47. rr = CMS_ReceiptRequest_new();
  48. if (rr == NULL)
  49. goto merr;
  50. if (id)
  51. ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
  52. else {
  53. if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
  54. goto merr;
  55. if (RAND_bytes(rr->signedContentIdentifier->data, 32) <= 0)
  56. goto err;
  57. }
  58. sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
  59. rr->receiptsTo = receiptsTo;
  60. if (receiptList) {
  61. rr->receiptsFrom->type = 1;
  62. rr->receiptsFrom->d.receiptList = receiptList;
  63. } else {
  64. rr->receiptsFrom->type = 0;
  65. rr->receiptsFrom->d.allOrFirstTier = allorfirst;
  66. }
  67. return rr;
  68. merr:
  69. CMSerr(CMS_F_CMS_RECEIPTREQUEST_CREATE0, ERR_R_MALLOC_FAILURE);
  70. err:
  71. CMS_ReceiptRequest_free(rr);
  72. return NULL;
  73. }
  74. int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
  75. {
  76. unsigned char *rrder = NULL;
  77. int rrderlen, r = 0;
  78. rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
  79. if (rrderlen < 0)
  80. goto merr;
  81. if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
  82. V_ASN1_SEQUENCE, rrder, rrderlen))
  83. goto merr;
  84. r = 1;
  85. merr:
  86. if (!r)
  87. CMSerr(CMS_F_CMS_ADD1_RECEIPTREQUEST, ERR_R_MALLOC_FAILURE);
  88. OPENSSL_free(rrder);
  89. return r;
  90. }
  91. void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
  92. ASN1_STRING **pcid,
  93. int *pallorfirst,
  94. STACK_OF(GENERAL_NAMES) **plist,
  95. STACK_OF(GENERAL_NAMES) **prto)
  96. {
  97. if (pcid)
  98. *pcid = rr->signedContentIdentifier;
  99. if (rr->receiptsFrom->type == 0) {
  100. if (pallorfirst)
  101. *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
  102. if (plist)
  103. *plist = NULL;
  104. } else {
  105. if (pallorfirst)
  106. *pallorfirst = -1;
  107. if (plist)
  108. *plist = rr->receiptsFrom->d.receiptList;
  109. }
  110. if (prto)
  111. *prto = rr->receiptsTo;
  112. }
  113. /* Digest a SignerInfo structure for msgSigDigest attribute processing */
  114. static int cms_msgSigDigest(CMS_SignerInfo *si,
  115. unsigned char *dig, unsigned int *diglen)
  116. {
  117. const EVP_MD *md;
  118. md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
  119. if (md == NULL)
  120. return 0;
  121. if (!ASN1_item_digest(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
  122. si->signedAttrs, dig, diglen))
  123. return 0;
  124. return 1;
  125. }
  126. /* Add a msgSigDigest attribute to a SignerInfo */
  127. int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
  128. {
  129. unsigned char dig[EVP_MAX_MD_SIZE];
  130. unsigned int diglen;
  131. if (!cms_msgSigDigest(src, dig, &diglen)) {
  132. CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, CMS_R_MSGSIGDIGEST_ERROR);
  133. return 0;
  134. }
  135. if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
  136. V_ASN1_OCTET_STRING, dig, diglen)) {
  137. CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, ERR_R_MALLOC_FAILURE);
  138. return 0;
  139. }
  140. return 1;
  141. }
  142. /* Verify signed receipt after it has already passed normal CMS verify */
  143. int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
  144. {
  145. int r = 0, i;
  146. CMS_ReceiptRequest *rr = NULL;
  147. CMS_Receipt *rct = NULL;
  148. STACK_OF(CMS_SignerInfo) *sis, *osis;
  149. CMS_SignerInfo *si, *osi = NULL;
  150. ASN1_OCTET_STRING *msig, **pcont;
  151. ASN1_OBJECT *octype;
  152. unsigned char dig[EVP_MAX_MD_SIZE];
  153. unsigned int diglen;
  154. /* Get SignerInfos, also checks SignedData content type */
  155. osis = CMS_get0_SignerInfos(req_cms);
  156. sis = CMS_get0_SignerInfos(cms);
  157. if (!osis || !sis)
  158. goto err;
  159. if (sk_CMS_SignerInfo_num(sis) != 1) {
  160. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NEED_ONE_SIGNER);
  161. goto err;
  162. }
  163. /* Check receipt content type */
  164. if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
  165. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NOT_A_SIGNED_RECEIPT);
  166. goto err;
  167. }
  168. /* Extract and decode receipt content */
  169. pcont = CMS_get0_content(cms);
  170. if (!pcont || !*pcont) {
  171. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT);
  172. goto err;
  173. }
  174. rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
  175. if (!rct) {
  176. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_RECEIPT_DECODE_ERROR);
  177. goto err;
  178. }
  179. /* Locate original request */
  180. for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
  181. osi = sk_CMS_SignerInfo_value(osis, i);
  182. if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
  183. break;
  184. }
  185. if (i == sk_CMS_SignerInfo_num(osis)) {
  186. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MATCHING_SIGNATURE);
  187. goto err;
  188. }
  189. si = sk_CMS_SignerInfo_value(sis, 0);
  190. /* Get msgSigDigest value and compare */
  191. msig = CMS_signed_get0_data_by_OBJ(si,
  192. OBJ_nid2obj
  193. (NID_id_smime_aa_msgSigDigest), -3,
  194. V_ASN1_OCTET_STRING);
  195. if (!msig) {
  196. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MSGSIGDIGEST);
  197. goto err;
  198. }
  199. if (!cms_msgSigDigest(osi, dig, &diglen)) {
  200. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_ERROR);
  201. goto err;
  202. }
  203. if (diglen != (unsigned int)msig->length) {
  204. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
  205. goto err;
  206. }
  207. if (memcmp(dig, msig->data, diglen)) {
  208. CMSerr(CMS_F_CMS_RECEIPT_VERIFY,
  209. CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
  210. goto err;
  211. }
  212. /* Compare content types */
  213. octype = CMS_signed_get0_data_by_OBJ(osi,
  214. OBJ_nid2obj(NID_pkcs9_contentType),
  215. -3, V_ASN1_OBJECT);
  216. if (!octype) {
  217. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT_TYPE);
  218. goto err;
  219. }
  220. /* Compare details in receipt request */
  221. if (OBJ_cmp(octype, rct->contentType)) {
  222. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENT_TYPE_MISMATCH);
  223. goto err;
  224. }
  225. /* Get original receipt request details */
  226. if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
  227. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_RECEIPT_REQUEST);
  228. goto err;
  229. }
  230. if (ASN1_STRING_cmp(rr->signedContentIdentifier,
  231. rct->signedContentIdentifier)) {
  232. CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENTIDENTIFIER_MISMATCH);
  233. goto err;
  234. }
  235. r = 1;
  236. err:
  237. CMS_ReceiptRequest_free(rr);
  238. M_ASN1_free_of(rct, CMS_Receipt);
  239. return r;
  240. }
  241. /*
  242. * Encode a Receipt into an OCTET STRING read for including into content of a
  243. * SignedData ContentInfo.
  244. */
  245. ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si)
  246. {
  247. CMS_Receipt rct;
  248. CMS_ReceiptRequest *rr = NULL;
  249. ASN1_OBJECT *ctype;
  250. ASN1_OCTET_STRING *os = NULL;
  251. /* Get original receipt request */
  252. /* Get original receipt request details */
  253. if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
  254. CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_RECEIPT_REQUEST);
  255. goto err;
  256. }
  257. /* Get original content type */
  258. ctype = CMS_signed_get0_data_by_OBJ(si,
  259. OBJ_nid2obj(NID_pkcs9_contentType),
  260. -3, V_ASN1_OBJECT);
  261. if (!ctype) {
  262. CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_CONTENT_TYPE);
  263. goto err;
  264. }
  265. rct.version = 1;
  266. rct.contentType = ctype;
  267. rct.signedContentIdentifier = rr->signedContentIdentifier;
  268. rct.originatorSignatureValue = si->signature;
  269. os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
  270. err:
  271. CMS_ReceiptRequest_free(rr);
  272. return os;
  273. }