SignedData.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using Org.BouncyCastle.Asn1;
  5. namespace Org.BouncyCastle.Asn1.Pkcs
  6. {
  7. /**
  8. * a Pkcs#7 signed data object.
  9. */
  10. public class SignedData
  11. : Asn1Encodable
  12. {
  13. private readonly DerInteger version;
  14. private readonly Asn1Set digestAlgorithms;
  15. private readonly ContentInfo contentInfo;
  16. private readonly Asn1Set certificates;
  17. private readonly Asn1Set crls;
  18. private readonly Asn1Set signerInfos;
  19. public static SignedData GetInstance(object obj)
  20. {
  21. if (obj == null)
  22. return null;
  23. SignedData existing = obj as SignedData;
  24. if (existing != null)
  25. return existing;
  26. return new SignedData(Asn1Sequence.GetInstance(obj));
  27. }
  28. public SignedData(
  29. DerInteger _version,
  30. Asn1Set _digestAlgorithms,
  31. ContentInfo _contentInfo,
  32. Asn1Set _certificates,
  33. Asn1Set _crls,
  34. Asn1Set _signerInfos)
  35. {
  36. version = _version;
  37. digestAlgorithms = _digestAlgorithms;
  38. contentInfo = _contentInfo;
  39. certificates = _certificates;
  40. crls = _crls;
  41. signerInfos = _signerInfos;
  42. }
  43. private SignedData(
  44. Asn1Sequence seq)
  45. {
  46. IEnumerator e = seq.GetEnumerator();
  47. e.MoveNext();
  48. version = (DerInteger) e.Current;
  49. e.MoveNext();
  50. digestAlgorithms = (Asn1Set) e.Current;
  51. e.MoveNext();
  52. contentInfo = ContentInfo.GetInstance(e.Current);
  53. while (e.MoveNext())
  54. {
  55. Asn1Object o = (Asn1Object) e.Current;
  56. //
  57. // an interesting feature of SignedData is that there appear to be varying implementations...
  58. // for the moment we ignore anything which doesn't fit.
  59. //
  60. if (o is DerTaggedObject)
  61. {
  62. DerTaggedObject tagged = (DerTaggedObject) o;
  63. switch (tagged.TagNo)
  64. {
  65. case 0:
  66. certificates = Asn1Set.GetInstance(tagged, false);
  67. break;
  68. case 1:
  69. crls = Asn1Set.GetInstance(tagged, false);
  70. break;
  71. default:
  72. throw new ArgumentException("unknown tag value " + tagged.TagNo);
  73. }
  74. }
  75. else
  76. {
  77. signerInfos = (Asn1Set) o;
  78. }
  79. }
  80. }
  81. public DerInteger Version
  82. {
  83. get { return version; }
  84. }
  85. public Asn1Set DigestAlgorithms
  86. {
  87. get { return digestAlgorithms; }
  88. }
  89. public ContentInfo ContentInfo
  90. {
  91. get { return contentInfo; }
  92. }
  93. public Asn1Set Certificates
  94. {
  95. get { return certificates; }
  96. }
  97. public Asn1Set Crls
  98. {
  99. get { return crls; }
  100. }
  101. public Asn1Set SignerInfos
  102. {
  103. get { return signerInfos; }
  104. }
  105. /**
  106. * Produce an object suitable for an Asn1OutputStream.
  107. * <pre>
  108. * SignedData ::= Sequence {
  109. * version Version,
  110. * digestAlgorithms DigestAlgorithmIdentifiers,
  111. * contentInfo ContentInfo,
  112. * certificates
  113. * [0] IMPLICIT ExtendedCertificatesAndCertificates
  114. * OPTIONAL,
  115. * crls
  116. * [1] IMPLICIT CertificateRevocationLists OPTIONAL,
  117. * signerInfos SignerInfos }
  118. * </pre>
  119. */
  120. public override Asn1Object ToAsn1Object()
  121. {
  122. Asn1EncodableVector v = new Asn1EncodableVector(
  123. version, digestAlgorithms, contentInfo);
  124. if (certificates != null)
  125. {
  126. v.Add(new DerTaggedObject(false, 0, certificates));
  127. }
  128. if (crls != null)
  129. {
  130. v.Add(new DerTaggedObject(false, 1, crls));
  131. }
  132. v.Add(signerInfos);
  133. return new BerSequence(v);
  134. }
  135. }
  136. }
  137. #endif