SignedData.cs 4.2 KB

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