SubjectKeyIdentifier.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  8. {
  9. /**
  10. * The SubjectKeyIdentifier object.
  11. * <pre>
  12. * SubjectKeyIdentifier::= OCTET STRING
  13. * </pre>
  14. */
  15. public class SubjectKeyIdentifier
  16. : Asn1Encodable
  17. {
  18. public static SubjectKeyIdentifier GetInstance(Asn1TaggedObject obj, bool explicitly)
  19. {
  20. return GetInstance(Asn1OctetString.GetInstance(obj, explicitly));
  21. }
  22. public static SubjectKeyIdentifier GetInstance(object obj)
  23. {
  24. if (obj is SubjectKeyIdentifier)
  25. return (SubjectKeyIdentifier)obj;
  26. if (obj is SubjectPublicKeyInfo)
  27. return new SubjectKeyIdentifier((SubjectPublicKeyInfo)obj);
  28. if (obj is X509Extension)
  29. return GetInstance(X509Extension.ConvertValueToObject((X509Extension)obj));
  30. if (obj == null)
  31. return null;
  32. return new SubjectKeyIdentifier(Asn1OctetString.GetInstance(obj));
  33. }
  34. public static SubjectKeyIdentifier FromExtensions(X509Extensions extensions)
  35. {
  36. return GetInstance(X509Extensions.GetExtensionParsedValue(extensions, X509Extensions.SubjectKeyIdentifier));
  37. }
  38. private readonly byte[] keyIdentifier;
  39. public SubjectKeyIdentifier(
  40. byte[] keyID)
  41. {
  42. if (keyID == null)
  43. throw new ArgumentNullException("keyID");
  44. this.keyIdentifier = Arrays.Clone(keyID);
  45. }
  46. public SubjectKeyIdentifier(
  47. Asn1OctetString keyID)
  48. : this(keyID.GetOctets())
  49. {
  50. }
  51. /**
  52. * Calculates the keyIdentifier using a SHA1 hash over the BIT STRING
  53. * from SubjectPublicKeyInfo as defined in RFC3280.
  54. *
  55. * @param spki the subject public key info.
  56. */
  57. public SubjectKeyIdentifier(
  58. SubjectPublicKeyInfo spki)
  59. {
  60. this.keyIdentifier = GetDigest(spki);
  61. }
  62. public byte[] GetKeyIdentifier()
  63. {
  64. return Arrays.Clone(keyIdentifier);
  65. }
  66. public override Asn1Object ToAsn1Object()
  67. {
  68. return new DerOctetString(GetKeyIdentifier());
  69. }
  70. /**
  71. * Return a RFC 3280 type 1 key identifier. As in:
  72. * <pre>
  73. * (1) The keyIdentifier is composed of the 160-bit SHA-1 hash of the
  74. * value of the BIT STRING subjectPublicKey (excluding the tag,
  75. * length, and number of unused bits).
  76. * </pre>
  77. * @param keyInfo the key info object containing the subjectPublicKey field.
  78. * @return the key identifier.
  79. */
  80. public static SubjectKeyIdentifier CreateSha1KeyIdentifier(
  81. SubjectPublicKeyInfo keyInfo)
  82. {
  83. return new SubjectKeyIdentifier(keyInfo);
  84. }
  85. /**
  86. * Return a RFC 3280 type 2 key identifier. As in:
  87. * <pre>
  88. * (2) The keyIdentifier is composed of a four bit type field with
  89. * the value 0100 followed by the least significant 60 bits of the
  90. * SHA-1 hash of the value of the BIT STRING subjectPublicKey.
  91. * </pre>
  92. * @param keyInfo the key info object containing the subjectPublicKey field.
  93. * @return the key identifier.
  94. */
  95. public static SubjectKeyIdentifier CreateTruncatedSha1KeyIdentifier(
  96. SubjectPublicKeyInfo keyInfo)
  97. {
  98. byte[] dig = GetDigest(keyInfo);
  99. byte[] id = new byte[8];
  100. Array.Copy(dig, dig.Length - 8, id, 0, id.Length);
  101. id[0] &= 0x0f;
  102. id[0] |= 0x40;
  103. return new SubjectKeyIdentifier(id);
  104. }
  105. private static byte[] GetDigest(
  106. SubjectPublicKeyInfo spki)
  107. {
  108. IDigest digest = new Sha1Digest();
  109. byte[] resBuf = new byte[digest.GetDigestSize()];
  110. byte[] bytes = spki.PublicKeyData.GetBytes();
  111. digest.BlockUpdate(bytes, 0, bytes.Length);
  112. digest.DoFinal(resBuf, 0);
  113. return resBuf;
  114. }
  115. }
  116. }
  117. #pragma warning restore
  118. #endif