SubjectPublicKeyInfo.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  7. {
  8. /**
  9. * The object that contains the public key stored in a certficate.
  10. * <p>
  11. * The GetEncoded() method in the public keys in the JCE produces a DER
  12. * encoded one of these.</p>
  13. */
  14. public class SubjectPublicKeyInfo
  15. : Asn1Encodable
  16. {
  17. private readonly AlgorithmIdentifier algID;
  18. private readonly DerBitString keyData;
  19. public static SubjectPublicKeyInfo GetInstance(
  20. Asn1TaggedObject obj,
  21. bool explicitly)
  22. {
  23. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  24. }
  25. public static SubjectPublicKeyInfo GetInstance(
  26. object obj)
  27. {
  28. if (obj is SubjectPublicKeyInfo)
  29. return (SubjectPublicKeyInfo) obj;
  30. if (obj != null)
  31. return new SubjectPublicKeyInfo(Asn1Sequence.GetInstance(obj));
  32. return null;
  33. }
  34. public SubjectPublicKeyInfo(
  35. AlgorithmIdentifier algID,
  36. Asn1Encodable publicKey)
  37. {
  38. this.keyData = new DerBitString(publicKey);
  39. this.algID = algID;
  40. }
  41. public SubjectPublicKeyInfo(
  42. AlgorithmIdentifier algID,
  43. byte[] publicKey)
  44. {
  45. this.keyData = new DerBitString(publicKey);
  46. this.algID = algID;
  47. }
  48. private SubjectPublicKeyInfo(
  49. Asn1Sequence seq)
  50. {
  51. if (seq.Count != 2)
  52. throw new ArgumentException("Bad sequence size: " + seq.Count, "seq");
  53. this.algID = AlgorithmIdentifier.GetInstance(seq[0]);
  54. this.keyData = DerBitString.GetInstance(seq[1]);
  55. }
  56. public AlgorithmIdentifier AlgorithmID
  57. {
  58. get { return algID; }
  59. }
  60. /**
  61. * for when the public key is an encoded object - if the bitstring
  62. * can't be decoded this routine raises an IOException.
  63. *
  64. * @exception IOException - if the bit string doesn't represent a Der
  65. * encoded object.
  66. */
  67. public Asn1Object ParsePublicKey()
  68. {
  69. return Asn1Object.FromByteArray(keyData.GetOctets());
  70. }
  71. /**
  72. * for when the public key is an encoded object - if the bitstring
  73. * can't be decoded this routine raises an IOException.
  74. *
  75. * @exception IOException - if the bit string doesn't represent a Der
  76. * encoded object.
  77. */
  78. public Asn1Object GetPublicKey()
  79. {
  80. return Asn1Object.FromByteArray(keyData.GetOctets());
  81. }
  82. /**
  83. * for when the public key is raw bits...
  84. */
  85. public DerBitString PublicKeyData
  86. {
  87. get { return keyData; }
  88. }
  89. /**
  90. * Produce an object suitable for an Asn1OutputStream.
  91. * <pre>
  92. * SubjectPublicKeyInfo ::= Sequence {
  93. * algorithm AlgorithmIdentifier,
  94. * publicKey BIT STRING }
  95. * </pre>
  96. */
  97. public override Asn1Object ToAsn1Object()
  98. {
  99. return new DerSequence(algID, keyData);
  100. }
  101. }
  102. }
  103. #pragma warning restore
  104. #endif