CMSSignedData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509.Store;
  13. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  14. {
  15. /**
  16. * general class for handling a pkcs7-signature message.
  17. *
  18. * A simple example of usage - note, in the example below the validity of
  19. * the certificate isn't verified, just the fact that one of the certs
  20. * matches the given signer...
  21. *
  22. * <pre>
  23. * IX509Store certs = s.GetCertificates();
  24. * SignerInformationStore signers = s.GetSignerInfos();
  25. *
  26. * foreach (SignerInformation signer in signers.GetSigners())
  27. * {
  28. * ArrayList certList = new ArrayList(certs.GetMatches(signer.SignerID));
  29. * X509Certificate cert = (X509Certificate) certList[0];
  30. *
  31. * if (signer.Verify(cert.GetPublicKey()))
  32. * {
  33. * verified++;
  34. * }
  35. * }
  36. * </pre>
  37. */
  38. public class CmsSignedData
  39. {
  40. private static readonly CmsSignedHelper Helper = CmsSignedHelper.Instance;
  41. private readonly CmsProcessable signedContent;
  42. private SignedData signedData;
  43. private ContentInfo contentInfo;
  44. private SignerInformationStore signerInfoStore;
  45. private IX509Store attrCertStore;
  46. private IX509Store certificateStore;
  47. private IX509Store crlStore;
  48. private IDictionary hashes;
  49. private CmsSignedData(
  50. CmsSignedData c)
  51. {
  52. this.signedData = c.signedData;
  53. this.contentInfo = c.contentInfo;
  54. this.signedContent = c.signedContent;
  55. this.signerInfoStore = c.signerInfoStore;
  56. }
  57. public CmsSignedData(
  58. byte[] sigBlock)
  59. : this(CmsUtilities.ReadContentInfo(new MemoryStream(sigBlock, false)))
  60. {
  61. }
  62. public CmsSignedData(
  63. CmsProcessable signedContent,
  64. byte[] sigBlock)
  65. : this(signedContent, CmsUtilities.ReadContentInfo(new MemoryStream(sigBlock, false)))
  66. {
  67. }
  68. /**
  69. * Content with detached signature, digests precomputed
  70. *
  71. * @param hashes a map of precomputed digests for content indexed by name of hash.
  72. * @param sigBlock the signature object.
  73. */
  74. public CmsSignedData(
  75. IDictionary hashes,
  76. byte[] sigBlock)
  77. : this(hashes, CmsUtilities.ReadContentInfo(sigBlock))
  78. {
  79. }
  80. /**
  81. * base constructor - content with detached signature.
  82. *
  83. * @param signedContent the content that was signed.
  84. * @param sigData the signature object.
  85. */
  86. public CmsSignedData(
  87. CmsProcessable signedContent,
  88. Stream sigData)
  89. : this(signedContent, CmsUtilities.ReadContentInfo(sigData))
  90. {
  91. }
  92. /**
  93. * base constructor - with encapsulated content
  94. */
  95. public CmsSignedData(
  96. Stream sigData)
  97. : this(CmsUtilities.ReadContentInfo(sigData))
  98. {
  99. }
  100. public CmsSignedData(
  101. CmsProcessable signedContent,
  102. ContentInfo sigData)
  103. {
  104. this.signedContent = signedContent;
  105. this.contentInfo = sigData;
  106. this.signedData = SignedData.GetInstance(contentInfo.Content);
  107. }
  108. public CmsSignedData(
  109. IDictionary hashes,
  110. ContentInfo sigData)
  111. {
  112. this.hashes = hashes;
  113. this.contentInfo = sigData;
  114. this.signedData = SignedData.GetInstance(contentInfo.Content);
  115. }
  116. public CmsSignedData(
  117. ContentInfo sigData)
  118. {
  119. this.contentInfo = sigData;
  120. this.signedData = SignedData.GetInstance(contentInfo.Content);
  121. //
  122. // this can happen if the signed message is sent simply to send a
  123. // certificate chain.
  124. //
  125. if (signedData.EncapContentInfo.Content != null)
  126. {
  127. this.signedContent = new CmsProcessableByteArray(
  128. ((Asn1OctetString)(signedData.EncapContentInfo.Content)).GetOctets());
  129. }
  130. // else
  131. // {
  132. // this.signedContent = null;
  133. // }
  134. }
  135. /// <summary>Return the version number for this object.</summary>
  136. public int Version
  137. {
  138. get { return signedData.Version.IntValueExact; }
  139. }
  140. internal IX509Store GetCertificates()
  141. {
  142. return Helper.GetCertificates(signedData.Certificates);
  143. }
  144. /**
  145. * return the collection of signers that are associated with the
  146. * signatures for the message.
  147. */
  148. public SignerInformationStore GetSignerInfos()
  149. {
  150. if (signerInfoStore == null)
  151. {
  152. IList signerInfos = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  153. Asn1Set s = signedData.SignerInfos;
  154. foreach (object obj in s)
  155. {
  156. SignerInfo info = SignerInfo.GetInstance(obj);
  157. DerObjectIdentifier contentType = signedData.EncapContentInfo.ContentType;
  158. if (hashes == null)
  159. {
  160. signerInfos.Add(new SignerInformation(info, contentType, signedContent, null));
  161. }
  162. else
  163. {
  164. byte[] hash = (byte[])hashes[info.DigestAlgorithm.Algorithm.Id];
  165. signerInfos.Add(new SignerInformation(info, contentType, null, new BaseDigestCalculator(hash)));
  166. }
  167. }
  168. signerInfoStore = new SignerInformationStore(signerInfos);
  169. }
  170. return signerInfoStore;
  171. }
  172. /**
  173. * return a X509Store containing the attribute certificates, if any, contained
  174. * in this message.
  175. *
  176. * @param type type of store to create
  177. * @return a store of attribute certificates
  178. * @exception NoSuchStoreException if the store type isn't available.
  179. * @exception CmsException if a general exception prevents creation of the X509Store
  180. */
  181. public IX509Store GetAttributeCertificates(
  182. string type)
  183. {
  184. if (attrCertStore == null)
  185. {
  186. attrCertStore = Helper.CreateAttributeStore(type, signedData.Certificates);
  187. }
  188. return attrCertStore;
  189. }
  190. /**
  191. * return a X509Store containing the public key certificates, if any, contained
  192. * in this message.
  193. *
  194. * @param type type of store to create
  195. * @return a store of public key certificates
  196. * @exception NoSuchStoreException if the store type isn't available.
  197. * @exception CmsException if a general exception prevents creation of the X509Store
  198. */
  199. public IX509Store GetCertificates(
  200. string type)
  201. {
  202. if (certificateStore == null)
  203. {
  204. certificateStore = Helper.CreateCertificateStore(type, signedData.Certificates);
  205. }
  206. return certificateStore;
  207. }
  208. /**
  209. * return a X509Store containing CRLs, if any, contained
  210. * in this message.
  211. *
  212. * @param type type of store to create
  213. * @return a store of CRLs
  214. * @exception NoSuchStoreException if the store type isn't available.
  215. * @exception CmsException if a general exception prevents creation of the X509Store
  216. */
  217. public IX509Store GetCrls(
  218. string type)
  219. {
  220. if (crlStore == null)
  221. {
  222. crlStore = Helper.CreateCrlStore(type, signedData.CRLs);
  223. }
  224. return crlStore;
  225. }
  226. public string SignedContentTypeOid
  227. {
  228. get { return signedData.EncapContentInfo.ContentType.Id; }
  229. }
  230. /// <summary>
  231. /// Return the <c>DerObjectIdentifier</c> associated with the encapsulated
  232. /// content info structure carried in the signed data.
  233. /// </summary>
  234. public DerObjectIdentifier SignedContentType
  235. {
  236. get { return signedData.EncapContentInfo.ContentType; }
  237. }
  238. public CmsProcessable SignedContent
  239. {
  240. get { return signedContent; }
  241. }
  242. /**
  243. * return the ContentInfo
  244. */
  245. public ContentInfo ContentInfo
  246. {
  247. get { return contentInfo; }
  248. }
  249. /**
  250. * return the ASN.1 encoded representation of this object.
  251. */
  252. public byte[] GetEncoded()
  253. {
  254. return contentInfo.GetEncoded();
  255. }
  256. /**
  257. * return the ASN.1 encoded representation of this object using the specified encoding.
  258. *
  259. * @param encoding the ASN.1 encoding format to use ("BER" or "DER").
  260. */
  261. public byte[] GetEncoded(string encoding)
  262. {
  263. return contentInfo.GetEncoded(encoding);
  264. }
  265. /**
  266. * Replace the signerinformation store associated with this
  267. * CmsSignedData object with the new one passed in. You would
  268. * probably only want to do this if you wanted to change the unsigned
  269. * attributes associated with a signer, or perhaps delete one.
  270. *
  271. * @param signedData the signed data object to be used as a base.
  272. * @param signerInformationStore the new signer information store to use.
  273. * @return a new signed data object.
  274. */
  275. public static CmsSignedData ReplaceSigners(
  276. CmsSignedData signedData,
  277. SignerInformationStore signerInformationStore)
  278. {
  279. //
  280. // copy
  281. //
  282. CmsSignedData cms = new CmsSignedData(signedData);
  283. //
  284. // replace the store
  285. //
  286. cms.signerInfoStore = signerInformationStore;
  287. //
  288. // replace the signers in the SignedData object
  289. //
  290. Asn1EncodableVector digestAlgs = new Asn1EncodableVector();
  291. Asn1EncodableVector vec = new Asn1EncodableVector();
  292. foreach (SignerInformation signer in signerInformationStore.GetSigners())
  293. {
  294. digestAlgs.Add(Helper.FixAlgID(signer.DigestAlgorithmID));
  295. vec.Add(signer.ToSignerInfo());
  296. }
  297. Asn1Set digests = new DerSet(digestAlgs);
  298. Asn1Set signers = new DerSet(vec);
  299. Asn1Sequence sD = (Asn1Sequence)signedData.signedData.ToAsn1Object();
  300. //
  301. // signers are the last item in the sequence.
  302. //
  303. vec = new Asn1EncodableVector(
  304. sD[0], // version
  305. digests);
  306. for (int i = 2; i != sD.Count - 1; i++)
  307. {
  308. vec.Add(sD[i]);
  309. }
  310. vec.Add(signers);
  311. cms.signedData = SignedData.GetInstance(new BerSequence(vec));
  312. //
  313. // replace the contentInfo with the new one
  314. //
  315. cms.contentInfo = new ContentInfo(cms.contentInfo.ContentType, cms.signedData);
  316. return cms;
  317. }
  318. /**
  319. * Replace the certificate and CRL information associated with this
  320. * CmsSignedData object with the new one passed in.
  321. *
  322. * @param signedData the signed data object to be used as a base.
  323. * @param x509Certs the new certificates to be used.
  324. * @param x509Crls the new CRLs to be used.
  325. * @return a new signed data object.
  326. * @exception CmsException if there is an error processing the stores
  327. */
  328. public static CmsSignedData ReplaceCertificatesAndCrls(
  329. CmsSignedData signedData,
  330. IX509Store x509Certs,
  331. IX509Store x509Crls,
  332. IX509Store x509AttrCerts)
  333. {
  334. if (x509AttrCerts != null)
  335. throw BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateNotImplementedException("Currently can't replace attribute certificates");
  336. //
  337. // copy
  338. //
  339. CmsSignedData cms = new CmsSignedData(signedData);
  340. //
  341. // replace the certs and crls in the SignedData object
  342. //
  343. Asn1Set certs = null;
  344. try
  345. {
  346. Asn1Set asn1Set = CmsUtilities.CreateBerSetFromList(
  347. CmsUtilities.GetCertificatesFromStore(x509Certs));
  348. if (asn1Set.Count != 0)
  349. {
  350. certs = asn1Set;
  351. }
  352. }
  353. catch (X509StoreException e)
  354. {
  355. throw new CmsException("error getting certificates from store", e);
  356. }
  357. Asn1Set crls = null;
  358. try
  359. {
  360. Asn1Set asn1Set = CmsUtilities.CreateBerSetFromList(
  361. CmsUtilities.GetCrlsFromStore(x509Crls));
  362. if (asn1Set.Count != 0)
  363. {
  364. crls = asn1Set;
  365. }
  366. }
  367. catch (X509StoreException e)
  368. {
  369. throw new CmsException("error getting CRLs from store", e);
  370. }
  371. //
  372. // replace the CMS structure.
  373. //
  374. SignedData old = signedData.signedData;
  375. cms.signedData = new SignedData(
  376. old.DigestAlgorithms,
  377. old.EncapContentInfo,
  378. certs,
  379. crls,
  380. old.SignerInfos);
  381. //
  382. // replace the contentInfo with the new one
  383. //
  384. cms.contentInfo = new ContentInfo(cms.contentInfo.ContentType, cms.signedData);
  385. return cms;
  386. }
  387. }
  388. }
  389. #pragma warning restore
  390. #endif