AuthorityKeyIdentifierStructure.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.X509;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security.Certificates;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.X509.Extension
  10. {
  11. /// <remarks>A high level authority key identifier.</remarks>
  12. public class AuthorityKeyIdentifierStructure
  13. : AuthorityKeyIdentifier
  14. {
  15. /**
  16. * Constructor which will take the byte[] returned from getExtensionValue()
  17. *
  18. * @param encodedValue a DER octet encoded string with the extension structure in it.
  19. * @throws IOException on parsing errors.
  20. */
  21. // TODO Add a functional constructor from byte[]?
  22. public AuthorityKeyIdentifierStructure(
  23. Asn1OctetString encodedValue)
  24. : base((Asn1Sequence) X509ExtensionUtilities.FromExtensionValue(encodedValue))
  25. {
  26. }
  27. private static Asn1Sequence FromCertificate(
  28. X509Certificate certificate)
  29. {
  30. try
  31. {
  32. GeneralName genName = new GeneralName(
  33. PrincipalUtilities.GetIssuerX509Principal(certificate));
  34. if (certificate.Version == 3)
  35. {
  36. Asn1OctetString ext = certificate.GetExtensionValue(X509Extensions.SubjectKeyIdentifier);
  37. if (ext != null)
  38. {
  39. Asn1OctetString str = (Asn1OctetString) X509ExtensionUtilities.FromExtensionValue(ext);
  40. return (Asn1Sequence) new AuthorityKeyIdentifier(
  41. str.GetOctets(), new GeneralNames(genName), certificate.SerialNumber).ToAsn1Object();
  42. }
  43. }
  44. SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(
  45. certificate.GetPublicKey());
  46. return (Asn1Sequence) new AuthorityKeyIdentifier(
  47. info, new GeneralNames(genName), certificate.SerialNumber).ToAsn1Object();
  48. }
  49. catch (Exception e)
  50. {
  51. throw new CertificateParsingException("Exception extracting certificate details", e);
  52. }
  53. }
  54. private static Asn1Sequence FromKey(
  55. AsymmetricKeyParameter pubKey)
  56. {
  57. try
  58. {
  59. SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pubKey);
  60. return (Asn1Sequence) new AuthorityKeyIdentifier(info).ToAsn1Object();
  61. }
  62. catch (Exception e)
  63. {
  64. throw new InvalidKeyException("can't process key: " + e);
  65. }
  66. }
  67. /**
  68. * Create an AuthorityKeyIdentifier using the passed in certificate's public
  69. * key, issuer and serial number.
  70. *
  71. * @param certificate the certificate providing the information.
  72. * @throws CertificateParsingException if there is a problem processing the certificate
  73. */
  74. public AuthorityKeyIdentifierStructure(
  75. X509Certificate certificate)
  76. : base(FromCertificate(certificate))
  77. {
  78. }
  79. /**
  80. * Create an AuthorityKeyIdentifier using just the hash of the
  81. * public key.
  82. *
  83. * @param pubKey the key to generate the hash from.
  84. * @throws InvalidKeyException if there is a problem using the key.
  85. */
  86. public AuthorityKeyIdentifierStructure(
  87. AsymmetricKeyParameter pubKey)
  88. : base(FromKey(pubKey))
  89. {
  90. }
  91. }
  92. }
  93. #pragma warning restore
  94. #endif