OtherCertID.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Ess
  8. {
  9. [Obsolete("Use version in Asn1.Esf instead")]
  10. public class OtherCertID
  11. : Asn1Encodable
  12. {
  13. private Asn1Encodable otherCertHash;
  14. private IssuerSerial issuerSerial;
  15. public static OtherCertID GetInstance(
  16. object o)
  17. {
  18. if (o == null || o is OtherCertID)
  19. {
  20. return (OtherCertID) o;
  21. }
  22. if (o is Asn1Sequence)
  23. {
  24. return new OtherCertID((Asn1Sequence) o);
  25. }
  26. throw new ArgumentException(
  27. "unknown object in 'OtherCertID' factory : "
  28. + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(o) + ".");
  29. }
  30. /**
  31. * constructor
  32. */
  33. public OtherCertID(
  34. Asn1Sequence seq)
  35. {
  36. if (seq.Count < 1 || seq.Count > 2)
  37. {
  38. throw new ArgumentException("Bad sequence size: " + seq.Count);
  39. }
  40. if (seq[0].ToAsn1Object() is Asn1OctetString)
  41. {
  42. otherCertHash = Asn1OctetString.GetInstance(seq[0]);
  43. }
  44. else
  45. {
  46. otherCertHash = DigestInfo.GetInstance(seq[0]);
  47. }
  48. if (seq.Count > 1)
  49. {
  50. issuerSerial = IssuerSerial.GetInstance(Asn1Sequence.GetInstance(seq[1]));
  51. }
  52. }
  53. public OtherCertID(
  54. AlgorithmIdentifier algId,
  55. byte[] digest)
  56. {
  57. this.otherCertHash = new DigestInfo(algId, digest);
  58. }
  59. public OtherCertID(
  60. AlgorithmIdentifier algId,
  61. byte[] digest,
  62. IssuerSerial issuerSerial)
  63. {
  64. this.otherCertHash = new DigestInfo(algId, digest);
  65. this.issuerSerial = issuerSerial;
  66. }
  67. public AlgorithmIdentifier AlgorithmHash
  68. {
  69. get
  70. {
  71. if (otherCertHash.ToAsn1Object() is Asn1OctetString)
  72. {
  73. // SHA-1
  74. return new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1);
  75. }
  76. return DigestInfo.GetInstance(otherCertHash).AlgorithmID;
  77. }
  78. }
  79. public byte[] GetCertHash()
  80. {
  81. if (otherCertHash.ToAsn1Object() is Asn1OctetString)
  82. {
  83. // SHA-1
  84. return ((Asn1OctetString) otherCertHash.ToAsn1Object()).GetOctets();
  85. }
  86. return DigestInfo.GetInstance(otherCertHash).GetDigest();
  87. }
  88. public IssuerSerial IssuerSerial
  89. {
  90. get { return issuerSerial; }
  91. }
  92. /**
  93. * <pre>
  94. * OtherCertID ::= SEQUENCE {
  95. * otherCertHash OtherHash,
  96. * issuerSerial IssuerSerial OPTIONAL }
  97. *
  98. * OtherHash ::= CHOICE {
  99. * sha1Hash OCTET STRING,
  100. * otherHash OtherHashAlgAndValue }
  101. *
  102. * OtherHashAlgAndValue ::= SEQUENCE {
  103. * hashAlgorithm AlgorithmIdentifier,
  104. * hashValue OCTET STRING }
  105. *
  106. * </pre>
  107. */
  108. public override Asn1Object ToAsn1Object()
  109. {
  110. Asn1EncodableVector v = new Asn1EncodableVector(otherCertHash);
  111. if (issuerSerial != null)
  112. {
  113. v.Add(issuerSerial);
  114. }
  115. return new DerSequence(v);
  116. }
  117. }
  118. }
  119. #pragma warning restore
  120. #endif