SignerInfo.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms
  7. {
  8. public class SignerInfo
  9. : Asn1Encodable
  10. {
  11. private DerInteger version;
  12. private SignerIdentifier sid;
  13. private AlgorithmIdentifier digAlgorithm;
  14. private Asn1Set authenticatedAttributes;
  15. private AlgorithmIdentifier digEncryptionAlgorithm;
  16. private Asn1OctetString encryptedDigest;
  17. private Asn1Set unauthenticatedAttributes;
  18. public static SignerInfo GetInstance(object obj)
  19. {
  20. if (obj == null || obj is SignerInfo)
  21. return (SignerInfo) obj;
  22. if (obj is Asn1Sequence)
  23. return new SignerInfo((Asn1Sequence) obj);
  24. throw new ArgumentException("Unknown object in factory: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  25. }
  26. public SignerInfo(
  27. SignerIdentifier sid,
  28. AlgorithmIdentifier digAlgorithm,
  29. Asn1Set authenticatedAttributes,
  30. AlgorithmIdentifier digEncryptionAlgorithm,
  31. Asn1OctetString encryptedDigest,
  32. Asn1Set unauthenticatedAttributes)
  33. {
  34. this.version = new DerInteger(sid.IsTagged ? 3 : 1);
  35. this.sid = sid;
  36. this.digAlgorithm = digAlgorithm;
  37. this.authenticatedAttributes = authenticatedAttributes;
  38. this.digEncryptionAlgorithm = digEncryptionAlgorithm;
  39. this.encryptedDigest = encryptedDigest;
  40. this.unauthenticatedAttributes = unauthenticatedAttributes;
  41. }
  42. public SignerInfo(
  43. SignerIdentifier sid,
  44. AlgorithmIdentifier digAlgorithm,
  45. Attributes authenticatedAttributes,
  46. AlgorithmIdentifier digEncryptionAlgorithm,
  47. Asn1OctetString encryptedDigest,
  48. Attributes unauthenticatedAttributes)
  49. {
  50. this.version = new DerInteger(sid.IsTagged ? 3 : 1);
  51. this.sid = sid;
  52. this.digAlgorithm = digAlgorithm;
  53. this.authenticatedAttributes = Asn1Set.GetInstance(authenticatedAttributes);
  54. this.digEncryptionAlgorithm = digEncryptionAlgorithm;
  55. this.encryptedDigest = encryptedDigest;
  56. this.unauthenticatedAttributes = Asn1Set.GetInstance(unauthenticatedAttributes);
  57. }
  58. private SignerInfo(Asn1Sequence seq)
  59. {
  60. var e = seq.GetEnumerator();
  61. e.MoveNext();
  62. version = (DerInteger)e.Current;
  63. e.MoveNext();
  64. sid = SignerIdentifier.GetInstance(e.Current.ToAsn1Object());
  65. e.MoveNext();
  66. digAlgorithm = AlgorithmIdentifier.GetInstance(e.Current.ToAsn1Object());
  67. e.MoveNext();
  68. var obj = e.Current.ToAsn1Object();
  69. if (obj is Asn1TaggedObject tagged)
  70. {
  71. authenticatedAttributes = Asn1Set.GetInstance(tagged, false);
  72. e.MoveNext();
  73. digEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(e.Current.ToAsn1Object());
  74. }
  75. else
  76. {
  77. authenticatedAttributes = null;
  78. digEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(obj);
  79. }
  80. e.MoveNext();
  81. encryptedDigest = Asn1OctetString.GetInstance(e.Current.ToAsn1Object());
  82. if (e.MoveNext())
  83. {
  84. unauthenticatedAttributes = Asn1Set.GetInstance((Asn1TaggedObject)e.Current.ToAsn1Object(), false);
  85. }
  86. else
  87. {
  88. unauthenticatedAttributes = null;
  89. }
  90. }
  91. public DerInteger Version
  92. {
  93. get { return version; }
  94. }
  95. public SignerIdentifier SignerID
  96. {
  97. get { return sid; }
  98. }
  99. public Asn1Set AuthenticatedAttributes
  100. {
  101. get { return authenticatedAttributes; }
  102. }
  103. public AlgorithmIdentifier DigestAlgorithm
  104. {
  105. get { return digAlgorithm; }
  106. }
  107. public Asn1OctetString EncryptedDigest
  108. {
  109. get { return encryptedDigest; }
  110. }
  111. public AlgorithmIdentifier DigestEncryptionAlgorithm
  112. {
  113. get { return digEncryptionAlgorithm; }
  114. }
  115. public Asn1Set UnauthenticatedAttributes
  116. {
  117. get { return unauthenticatedAttributes; }
  118. }
  119. /**
  120. * Produce an object suitable for an Asn1OutputStream.
  121. * <pre>
  122. * SignerInfo ::= Sequence {
  123. * version Version,
  124. * SignerIdentifier sid,
  125. * digestAlgorithm DigestAlgorithmIdentifier,
  126. * authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL,
  127. * digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
  128. * encryptedDigest EncryptedDigest,
  129. * unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
  130. * }
  131. *
  132. * EncryptedDigest ::= OCTET STRING
  133. *
  134. * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
  135. *
  136. * DigestEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
  137. * </pre>
  138. */
  139. public override Asn1Object ToAsn1Object()
  140. {
  141. Asn1EncodableVector v = new Asn1EncodableVector(version, sid, digAlgorithm);
  142. v.AddOptionalTagged(false, 0, authenticatedAttributes);
  143. v.Add(digEncryptionAlgorithm, encryptedDigest);
  144. v.AddOptionalTagged(false, 1, unauthenticatedAttributes);
  145. return new DerSequence(v);
  146. }
  147. }
  148. }
  149. #pragma warning restore
  150. #endif