CMSSignedData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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.Value.IntValue; }
  139. }
  140. /**
  141. * return the collection of signers that are associated with the
  142. * signatures for the message.
  143. */
  144. public SignerInformationStore GetSignerInfos()
  145. {
  146. if (signerInfoStore == null)
  147. {
  148. IList signerInfos = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  149. Asn1Set s = signedData.SignerInfos;
  150. foreach (object obj in s)
  151. {
  152. SignerInfo info = SignerInfo.GetInstance(obj);
  153. DerObjectIdentifier contentType = signedData.EncapContentInfo.ContentType;
  154. if (hashes == null)
  155. {
  156. signerInfos.Add(new SignerInformation(info, contentType, signedContent, null));
  157. }
  158. else
  159. {
  160. byte[] hash = (byte[])hashes[info.DigestAlgorithm.Algorithm.Id];
  161. signerInfos.Add(new SignerInformation(info, contentType, null, new BaseDigestCalculator(hash)));
  162. }
  163. }
  164. signerInfoStore = new SignerInformationStore(signerInfos);
  165. }
  166. return signerInfoStore;
  167. }
  168. /**
  169. * return a X509Store containing the attribute certificates, if any, contained
  170. * in this message.
  171. *
  172. * @param type type of store to create
  173. * @return a store of attribute certificates
  174. * @exception NoSuchStoreException if the store type isn't available.
  175. * @exception CmsException if a general exception prevents creation of the X509Store
  176. */
  177. public IX509Store GetAttributeCertificates(
  178. string type)
  179. {
  180. if (attrCertStore == null)
  181. {
  182. attrCertStore = Helper.CreateAttributeStore(type, signedData.Certificates);
  183. }
  184. return attrCertStore;
  185. }
  186. /**
  187. * return a X509Store containing the public key certificates, if any, contained
  188. * in this message.
  189. *
  190. * @param type type of store to create
  191. * @return a store of public key certificates
  192. * @exception NoSuchStoreException if the store type isn't available.
  193. * @exception CmsException if a general exception prevents creation of the X509Store
  194. */
  195. public IX509Store GetCertificates(
  196. string type)
  197. {
  198. if (certificateStore == null)
  199. {
  200. certificateStore = Helper.CreateCertificateStore(type, signedData.Certificates);
  201. }
  202. return certificateStore;
  203. }
  204. /**
  205. * return a X509Store containing CRLs, if any, contained
  206. * in this message.
  207. *
  208. * @param type type of store to create
  209. * @return a store of CRLs
  210. * @exception NoSuchStoreException if the store type isn't available.
  211. * @exception CmsException if a general exception prevents creation of the X509Store
  212. */
  213. public IX509Store GetCrls(
  214. string type)
  215. {
  216. if (crlStore == null)
  217. {
  218. crlStore = Helper.CreateCrlStore(type, signedData.CRLs);
  219. }
  220. return crlStore;
  221. }
  222. [Obsolete("Use 'SignedContentType' property instead.")]
  223. public string SignedContentTypeOid
  224. {
  225. get { return signedData.EncapContentInfo.ContentType.Id; }
  226. }
  227. /// <summary>
  228. /// Return the <c>DerObjectIdentifier</c> associated with the encapsulated
  229. /// content info structure carried in the signed data.
  230. /// </summary>
  231. public DerObjectIdentifier SignedContentType
  232. {
  233. get { return signedData.EncapContentInfo.ContentType; }
  234. }
  235. public CmsProcessable SignedContent
  236. {
  237. get { return signedContent; }
  238. }
  239. /**
  240. * return the ContentInfo
  241. */
  242. public ContentInfo ContentInfo
  243. {
  244. get { return contentInfo; }
  245. }
  246. /**
  247. * return the ASN.1 encoded representation of this object.
  248. */
  249. public byte[] GetEncoded()
  250. {
  251. return contentInfo.GetEncoded();
  252. }
  253. /**
  254. * Replace the signerinformation store associated with this
  255. * CmsSignedData object with the new one passed in. You would
  256. * probably only want to do this if you wanted to change the unsigned
  257. * attributes associated with a signer, or perhaps delete one.
  258. *
  259. * @param signedData the signed data object to be used as a base.
  260. * @param signerInformationStore the new signer information store to use.
  261. * @return a new signed data object.
  262. */
  263. public static CmsSignedData ReplaceSigners(
  264. CmsSignedData signedData,
  265. SignerInformationStore signerInformationStore)
  266. {
  267. //
  268. // copy
  269. //
  270. CmsSignedData cms = new CmsSignedData(signedData);
  271. //
  272. // replace the store
  273. //
  274. cms.signerInfoStore = signerInformationStore;
  275. //
  276. // replace the signers in the SignedData object
  277. //
  278. Asn1EncodableVector digestAlgs = new Asn1EncodableVector();
  279. Asn1EncodableVector vec = new Asn1EncodableVector();
  280. foreach (SignerInformation signer in signerInformationStore.GetSigners())
  281. {
  282. digestAlgs.Add(Helper.FixAlgID(signer.DigestAlgorithmID));
  283. vec.Add(signer.ToSignerInfo());
  284. }
  285. Asn1Set digests = new DerSet(digestAlgs);
  286. Asn1Set signers = new DerSet(vec);
  287. Asn1Sequence sD = (Asn1Sequence)signedData.signedData.ToAsn1Object();
  288. //
  289. // signers are the last item in the sequence.
  290. //
  291. vec = new Asn1EncodableVector(
  292. sD[0], // version
  293. digests);
  294. for (int i = 2; i != sD.Count - 1; i++)
  295. {
  296. vec.Add(sD[i]);
  297. }
  298. vec.Add(signers);
  299. cms.signedData = SignedData.GetInstance(new BerSequence(vec));
  300. //
  301. // replace the contentInfo with the new one
  302. //
  303. cms.contentInfo = new ContentInfo(cms.contentInfo.ContentType, cms.signedData);
  304. return cms;
  305. }
  306. /**
  307. * Replace the certificate and CRL information associated with this
  308. * CmsSignedData object with the new one passed in.
  309. *
  310. * @param signedData the signed data object to be used as a base.
  311. * @param x509Certs the new certificates to be used.
  312. * @param x509Crls the new CRLs to be used.
  313. * @return a new signed data object.
  314. * @exception CmsException if there is an error processing the stores
  315. */
  316. public static CmsSignedData ReplaceCertificatesAndCrls(
  317. CmsSignedData signedData,
  318. IX509Store x509Certs,
  319. IX509Store x509Crls,
  320. IX509Store x509AttrCerts)
  321. {
  322. if (x509AttrCerts != null)
  323. throw BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateNotImplementedException("Currently can't replace attribute certificates");
  324. //
  325. // copy
  326. //
  327. CmsSignedData cms = new CmsSignedData(signedData);
  328. //
  329. // replace the certs and crls in the SignedData object
  330. //
  331. Asn1Set certs = null;
  332. try
  333. {
  334. Asn1Set asn1Set = CmsUtilities.CreateBerSetFromList(
  335. CmsUtilities.GetCertificatesFromStore(x509Certs));
  336. if (asn1Set.Count != 0)
  337. {
  338. certs = asn1Set;
  339. }
  340. }
  341. catch (X509StoreException e)
  342. {
  343. throw new CmsException("error getting certificates from store", e);
  344. }
  345. Asn1Set crls = null;
  346. try
  347. {
  348. Asn1Set asn1Set = CmsUtilities.CreateBerSetFromList(
  349. CmsUtilities.GetCrlsFromStore(x509Crls));
  350. if (asn1Set.Count != 0)
  351. {
  352. crls = asn1Set;
  353. }
  354. }
  355. catch (X509StoreException e)
  356. {
  357. throw new CmsException("error getting CRLs from store", e);
  358. }
  359. //
  360. // replace the CMS structure.
  361. //
  362. SignedData old = signedData.signedData;
  363. cms.signedData = new SignedData(
  364. old.DigestAlgorithms,
  365. old.EncapContentInfo,
  366. certs,
  367. crls,
  368. old.SignerInfos);
  369. //
  370. // replace the contentInfo with the new one
  371. //
  372. cms.contentInfo = new ContentInfo(cms.contentInfo.ContentType, cms.signedData);
  373. return cms;
  374. }
  375. }
  376. }
  377. #pragma warning restore
  378. #endif