SubjectPublicKeyInfo.cs 2.8 KB

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