CertificateID.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Ocsp;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  11. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Ocsp
  12. {
  13. public class CertificateID
  14. {
  15. public const string HashSha1 = "1.3.14.3.2.26";
  16. private readonly CertID id;
  17. public CertificateID(
  18. CertID id)
  19. {
  20. if (id == null)
  21. throw new ArgumentNullException("id");
  22. this.id = id;
  23. }
  24. /**
  25. * create from an issuer certificate and the serial number of the
  26. * certificate it signed.
  27. * @exception OcspException if any problems occur creating the id fields.
  28. */
  29. public CertificateID(
  30. string hashAlgorithm,
  31. X509Certificate issuerCert,
  32. BigInteger serialNumber)
  33. {
  34. AlgorithmIdentifier hashAlg = new AlgorithmIdentifier(
  35. new DerObjectIdentifier(hashAlgorithm), DerNull.Instance);
  36. this.id = CreateCertID(hashAlg, issuerCert, new DerInteger(serialNumber));
  37. }
  38. public string HashAlgOid
  39. {
  40. get { return id.HashAlgorithm.Algorithm.Id; }
  41. }
  42. public byte[] GetIssuerNameHash()
  43. {
  44. return id.IssuerNameHash.GetOctets();
  45. }
  46. public byte[] GetIssuerKeyHash()
  47. {
  48. return id.IssuerKeyHash.GetOctets();
  49. }
  50. /**
  51. * return the serial number for the certificate associated
  52. * with this request.
  53. */
  54. public BigInteger SerialNumber
  55. {
  56. get { return id.SerialNumber.Value; }
  57. }
  58. public bool MatchesIssuer(
  59. X509Certificate issuerCert)
  60. {
  61. return CreateCertID(id.HashAlgorithm, issuerCert, id.SerialNumber).Equals(id);
  62. }
  63. public CertID ToAsn1Object()
  64. {
  65. return id;
  66. }
  67. public override bool Equals(
  68. object obj)
  69. {
  70. if (obj == this)
  71. return true;
  72. CertificateID other = obj as CertificateID;
  73. if (other == null)
  74. return false;
  75. return id.ToAsn1Object().Equals(other.id.ToAsn1Object());
  76. }
  77. public override int GetHashCode()
  78. {
  79. return id.ToAsn1Object().GetHashCode();
  80. }
  81. /**
  82. * Create a new CertificateID for a new serial number derived from a previous one
  83. * calculated for the same CA certificate.
  84. *
  85. * @param original the previously calculated CertificateID for the CA.
  86. * @param newSerialNumber the serial number for the new certificate of interest.
  87. *
  88. * @return a new CertificateID for newSerialNumber
  89. */
  90. public static CertificateID DeriveCertificateID(CertificateID original, BigInteger newSerialNumber)
  91. {
  92. return new CertificateID(new CertID(original.id.HashAlgorithm, original.id.IssuerNameHash,
  93. original.id.IssuerKeyHash, new DerInteger(newSerialNumber)));
  94. }
  95. private static CertID CreateCertID(
  96. AlgorithmIdentifier hashAlg,
  97. X509Certificate issuerCert,
  98. DerInteger serialNumber)
  99. {
  100. try
  101. {
  102. String hashAlgorithm = hashAlg.Algorithm.Id;
  103. X509Name issuerName = PrincipalUtilities.GetSubjectX509Principal(issuerCert);
  104. byte[] issuerNameHash = DigestUtilities.CalculateDigest(
  105. hashAlgorithm, issuerName.GetEncoded());
  106. AsymmetricKeyParameter issuerKey = issuerCert.GetPublicKey();
  107. SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(issuerKey);
  108. byte[] issuerKeyHash = DigestUtilities.CalculateDigest(
  109. hashAlgorithm, info.PublicKeyData.GetBytes());
  110. return new CertID(hashAlg, new DerOctetString(issuerNameHash),
  111. new DerOctetString(issuerKeyHash), serialNumber);
  112. }
  113. catch (Exception e)
  114. {
  115. throw new OcspException("problem creating ID: " + e, e);
  116. }
  117. }
  118. }
  119. }
  120. #pragma warning restore
  121. #endif