PopoSigningKey.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Crmf
  7. {
  8. public class PopoSigningKey
  9. : Asn1Encodable
  10. {
  11. private readonly PopoSigningKeyInput poposkInput;
  12. private readonly AlgorithmIdentifier algorithmIdentifier;
  13. private readonly DerBitString signature;
  14. private PopoSigningKey(Asn1Sequence seq)
  15. {
  16. int index = 0;
  17. if (seq[index] is Asn1TaggedObject)
  18. {
  19. Asn1TaggedObject tagObj
  20. = (Asn1TaggedObject) seq[index++];
  21. if (tagObj.TagNo != 0)
  22. {
  23. throw new ArgumentException( "Unknown PopoSigningKeyInput tag: " + tagObj.TagNo, "seq");
  24. }
  25. poposkInput = PopoSigningKeyInput.GetInstance(tagObj.GetObject());
  26. }
  27. algorithmIdentifier = AlgorithmIdentifier.GetInstance(seq[index++]);
  28. signature = DerBitString.GetInstance(seq[index]);
  29. }
  30. public static PopoSigningKey GetInstance(object obj)
  31. {
  32. if (obj is PopoSigningKey)
  33. return (PopoSigningKey)obj;
  34. if (obj is Asn1Sequence)
  35. return new PopoSigningKey((Asn1Sequence)obj);
  36. throw new ArgumentException("Invalid object: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  37. }
  38. public static PopoSigningKey GetInstance(Asn1TaggedObject obj, bool isExplicit)
  39. {
  40. return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
  41. }
  42. /**
  43. * Creates a new Proof of Possession object for a signing key.
  44. * @param poposkIn the PopoSigningKeyInput structure, or null if the
  45. * CertTemplate includes both subject and publicKey values.
  46. * @param aid the AlgorithmIdentifier used to sign the proof of possession.
  47. * @param signature a signature over the DER-encoded value of poposkIn,
  48. * or the DER-encoded value of certReq if poposkIn is null.
  49. */
  50. public PopoSigningKey(
  51. PopoSigningKeyInput poposkIn,
  52. AlgorithmIdentifier aid,
  53. DerBitString signature)
  54. {
  55. this.poposkInput = poposkIn;
  56. this.algorithmIdentifier = aid;
  57. this.signature = signature;
  58. }
  59. public virtual PopoSigningKeyInput PoposkInput
  60. {
  61. get { return poposkInput; }
  62. }
  63. public virtual AlgorithmIdentifier AlgorithmIdentifier
  64. {
  65. get { return algorithmIdentifier; }
  66. }
  67. public virtual DerBitString Signature
  68. {
  69. get { return signature; }
  70. }
  71. /**
  72. * <pre>
  73. * PopoSigningKey ::= SEQUENCE {
  74. * poposkInput [0] PopoSigningKeyInput OPTIONAL,
  75. * algorithmIdentifier AlgorithmIdentifier,
  76. * signature BIT STRING }
  77. * -- The signature (using "algorithmIdentifier") is on the
  78. * -- DER-encoded value of poposkInput. NOTE: If the CertReqMsg
  79. * -- certReq CertTemplate contains the subject and publicKey values,
  80. * -- then poposkInput MUST be omitted and the signature MUST be
  81. * -- computed on the DER-encoded value of CertReqMsg certReq. If
  82. * -- the CertReqMsg certReq CertTemplate does not contain the public
  83. * -- key and subject values, then poposkInput MUST be present and
  84. * -- MUST be signed. This strategy ensures that the public key is
  85. * -- not present in both the poposkInput and CertReqMsg certReq
  86. * -- CertTemplate fields.
  87. * </pre>
  88. * @return a basic ASN.1 object representation.
  89. */
  90. public override Asn1Object ToAsn1Object()
  91. {
  92. Asn1EncodableVector v = new Asn1EncodableVector();
  93. if (poposkInput != null)
  94. {
  95. v.Add(new DerTaggedObject(false, 0, poposkInput));
  96. }
  97. v.Add(algorithmIdentifier);
  98. v.Add(signature);
  99. return new DerSequence(v);
  100. }
  101. }
  102. }
  103. #pragma warning restore
  104. #endif