SignedData.cs 4.4 KB

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