Challenge.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cmp
  6. {
  7. /**
  8. * <pre>
  9. * Challenge ::= SEQUENCE {
  10. * owf AlgorithmIdentifier OPTIONAL,
  11. *
  12. * -- MUST be present in the first Challenge; MAY be omitted in
  13. * -- any subsequent Challenge in POPODecKeyChallContent (if
  14. * -- omitted, then the owf used in the immediately preceding
  15. * -- Challenge is to be used).
  16. *
  17. * witness OCTET STRING,
  18. * -- the result of applying the one-way function (owf) to a
  19. * -- randomly-generated INTEGER, A. [Note that a different
  20. * -- INTEGER MUST be used for each Challenge.]
  21. * challenge OCTET STRING
  22. * -- the encryption (under the public key for which the cert.
  23. * -- request is being made) of Rand, where Rand is specified as
  24. * -- Rand ::= SEQUENCE {
  25. * -- int INTEGER,
  26. * -- - the randomly-generated INTEGER A (above)
  27. * -- sender GeneralName
  28. * -- - the sender's name (as included in PKIHeader)
  29. * -- }
  30. * }
  31. * </pre>
  32. */
  33. public class Challenge
  34. : Asn1Encodable
  35. {
  36. public static Challenge GetInstance(object obj)
  37. {
  38. if (obj is Challenge challenge)
  39. return challenge;
  40. if (obj != null)
  41. return new Challenge(Asn1Sequence.GetInstance(obj));
  42. return null;
  43. }
  44. private readonly AlgorithmIdentifier m_owf;
  45. private readonly Asn1OctetString m_witness;
  46. private readonly Asn1OctetString m_challenge;
  47. private Challenge(Asn1Sequence seq)
  48. {
  49. int index = 0;
  50. if (seq.Count == 3)
  51. {
  52. m_owf = AlgorithmIdentifier.GetInstance(seq[index++]);
  53. }
  54. m_witness = Asn1OctetString.GetInstance(seq[index++]);
  55. m_challenge = Asn1OctetString.GetInstance(seq[index]);
  56. }
  57. public Challenge(byte[] witness, byte[] challenge)
  58. : this(null, witness, challenge)
  59. {
  60. }
  61. public Challenge(AlgorithmIdentifier owf, byte[] witness, byte[] challenge)
  62. {
  63. m_owf = owf;
  64. m_witness = new DerOctetString(witness);
  65. m_challenge = new DerOctetString(challenge);
  66. }
  67. public virtual AlgorithmIdentifier Owf => m_owf;
  68. public virtual Asn1OctetString Witness => m_witness;
  69. public virtual Asn1OctetString ChallengeValue => m_challenge;
  70. /**
  71. * <pre>
  72. * Challenge ::= SEQUENCE {
  73. * owf AlgorithmIdentifier OPTIONAL,
  74. *
  75. * -- MUST be present in the first Challenge; MAY be omitted in
  76. * -- any subsequent Challenge in POPODecKeyChallContent (if
  77. * -- omitted, then the owf used in the immediately preceding
  78. * -- Challenge is to be used).
  79. *
  80. * witness OCTET STRING,
  81. * -- the result of applying the one-way function (owf) to a
  82. * -- randomly-generated INTEGER, A. [Note that a different
  83. * -- INTEGER MUST be used for each Challenge.]
  84. * challenge OCTET STRING
  85. * -- the encryption (under the public key for which the cert.
  86. * -- request is being made) of Rand, where Rand is specified as
  87. * -- Rand ::= SEQUENCE {
  88. * -- int INTEGER,
  89. * -- - the randomly-generated INTEGER A (above)
  90. * -- sender GeneralName
  91. * -- - the sender's name (as included in PKIHeader)
  92. * -- }
  93. * }
  94. * </pre>
  95. * @return a basic ASN.1 object representation.
  96. */
  97. public override Asn1Object ToAsn1Object()
  98. {
  99. Asn1EncodableVector v = new Asn1EncodableVector();
  100. v.AddOptional(m_owf);
  101. v.Add(m_witness, m_challenge);
  102. return new DerSequence(v);
  103. }
  104. /**
  105. * Rand is the inner type
  106. */
  107. public class Rand
  108. : Asn1Encodable
  109. {
  110. public static Rand GetInstance(object obj)
  111. {
  112. if (obj is Rand rand)
  113. return rand;
  114. if (obj != null)
  115. return new Rand(Asn1Sequence.GetInstance(obj));
  116. return null;
  117. }
  118. private readonly DerInteger m_intVal;
  119. private readonly GeneralName m_sender;
  120. public Rand(DerInteger intVal, GeneralName sender)
  121. {
  122. m_intVal = intVal;
  123. m_sender = sender;
  124. }
  125. public Rand(Asn1Sequence seq)
  126. {
  127. if (seq.Count != 2)
  128. throw new ArgumentException("expected sequence size of 2");
  129. m_intVal = DerInteger.GetInstance(seq[0]);
  130. m_sender = GeneralName.GetInstance(seq[1]);
  131. }
  132. public virtual DerInteger IntVal => m_intVal;
  133. public virtual GeneralName Sender => m_sender;
  134. public override Asn1Object ToAsn1Object()
  135. {
  136. return new DerSequence(m_intVal, m_sender);
  137. }
  138. }
  139. }
  140. }
  141. #pragma warning restore
  142. #endif