OtherCertID.cs 2.9 KB

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