ProofOfPossession.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Crmf
  6. {
  7. public class ProofOfPossession
  8. : Asn1Encodable, IAsn1Choice
  9. {
  10. public const int TYPE_RA_VERIFIED = 0;
  11. public const int TYPE_SIGNING_KEY = 1;
  12. public const int TYPE_KEY_ENCIPHERMENT = 2;
  13. public const int TYPE_KEY_AGREEMENT = 3;
  14. private readonly int tagNo;
  15. private readonly Asn1Encodable obj;
  16. private ProofOfPossession(Asn1TaggedObject tagged)
  17. {
  18. tagNo = tagged.TagNo;
  19. switch (tagNo)
  20. {
  21. case 0:
  22. obj = DerNull.Instance;
  23. break;
  24. case 1:
  25. obj = PopoSigningKey.GetInstance(tagged, false);
  26. break;
  27. case 2:
  28. case 3:
  29. obj = PopoPrivKey.GetInstance(tagged, false);
  30. break;
  31. default:
  32. throw new ArgumentException("unknown tag: " + tagNo, "tagged");
  33. }
  34. }
  35. public static ProofOfPossession GetInstance(object obj)
  36. {
  37. if (obj is ProofOfPossession)
  38. return (ProofOfPossession)obj;
  39. if (obj is Asn1TaggedObject)
  40. return new ProofOfPossession((Asn1TaggedObject)obj);
  41. throw new ArgumentException("Invalid object: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  42. }
  43. /** Creates a ProofOfPossession with type raVerified. */
  44. public ProofOfPossession()
  45. {
  46. tagNo = TYPE_RA_VERIFIED;
  47. obj = DerNull.Instance;
  48. }
  49. /** Creates a ProofOfPossession for a signing key. */
  50. public ProofOfPossession(PopoSigningKey Poposk)
  51. {
  52. tagNo = TYPE_SIGNING_KEY;
  53. obj = Poposk;
  54. }
  55. /**
  56. * Creates a ProofOfPossession for key encipherment or agreement.
  57. * @param type one of TYPE_KEY_ENCIPHERMENT or TYPE_KEY_AGREEMENT
  58. */
  59. public ProofOfPossession(int type, PopoPrivKey privkey)
  60. {
  61. tagNo = type;
  62. obj = privkey;
  63. }
  64. public virtual int Type
  65. {
  66. get { return tagNo; }
  67. }
  68. public virtual Asn1Encodable Object
  69. {
  70. get { return obj; }
  71. }
  72. /**
  73. * <pre>
  74. * ProofOfPossession ::= CHOICE {
  75. * raVerified [0] NULL,
  76. * -- used if the RA has already verified that the requester is in
  77. * -- possession of the private key
  78. * signature [1] PopoSigningKey,
  79. * keyEncipherment [2] PopoPrivKey,
  80. * keyAgreement [3] PopoPrivKey }
  81. * </pre>
  82. * @return a basic ASN.1 object representation.
  83. */
  84. public override Asn1Object ToAsn1Object()
  85. {
  86. return new DerTaggedObject(false, tagNo, obj);
  87. }
  88. }
  89. }
  90. #pragma warning restore
  91. #endif