SubjectKeyIdentifier.cs 3.9 KB

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