EncKeyWithID.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Crmf
  7. {
  8. public class EncKeyWithID
  9. : Asn1Encodable
  10. {
  11. private readonly PrivateKeyInfo privKeyInfo;
  12. private readonly Asn1Encodable identifier;
  13. public static EncKeyWithID GetInstance(object obj)
  14. {
  15. if (obj is EncKeyWithID)
  16. return (EncKeyWithID)obj;
  17. if (obj != null)
  18. return new EncKeyWithID(Asn1Sequence.GetInstance(obj));
  19. return null;
  20. }
  21. private EncKeyWithID(Asn1Sequence seq)
  22. {
  23. this.privKeyInfo = PrivateKeyInfo.GetInstance(seq[0]);
  24. if (seq.Count > 1)
  25. {
  26. if (!(seq[1] is DerUtf8String))
  27. {
  28. this.identifier = GeneralName.GetInstance(seq[1]);
  29. }
  30. else
  31. {
  32. this.identifier = (Asn1Encodable)seq[1];
  33. }
  34. }
  35. else
  36. {
  37. this.identifier = null;
  38. }
  39. }
  40. public EncKeyWithID(PrivateKeyInfo privKeyInfo)
  41. {
  42. this.privKeyInfo = privKeyInfo;
  43. this.identifier = null;
  44. }
  45. public EncKeyWithID(PrivateKeyInfo privKeyInfo, DerUtf8String str)
  46. {
  47. this.privKeyInfo = privKeyInfo;
  48. this.identifier = str;
  49. }
  50. public EncKeyWithID(PrivateKeyInfo privKeyInfo, GeneralName generalName)
  51. {
  52. this.privKeyInfo = privKeyInfo;
  53. this.identifier = generalName;
  54. }
  55. public virtual PrivateKeyInfo PrivateKey
  56. {
  57. get { return privKeyInfo; }
  58. }
  59. public virtual bool HasIdentifier
  60. {
  61. get { return identifier != null; }
  62. }
  63. public virtual bool IsIdentifierUtf8String
  64. {
  65. get { return identifier is DerUtf8String; }
  66. }
  67. public virtual Asn1Encodable Identifier
  68. {
  69. get { return identifier; }
  70. }
  71. /**
  72. * <pre>
  73. * EncKeyWithID ::= SEQUENCE {
  74. * privateKey PrivateKeyInfo,
  75. * identifier CHOICE {
  76. * string UTF8String,
  77. * generalName GeneralName
  78. * } OPTIONAL
  79. * }
  80. * </pre>
  81. * @return
  82. */
  83. public override Asn1Object ToAsn1Object()
  84. {
  85. Asn1EncodableVector v = new Asn1EncodableVector(privKeyInfo);
  86. v.AddOptional(identifier);
  87. return new DerSequence(v);
  88. }
  89. }
  90. }
  91. #pragma warning restore
  92. #endif