DHDomainParameters.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9
  7. {
  8. public class DHDomainParameters
  9. : Asn1Encodable
  10. {
  11. private readonly DerInteger p, g, q, j;
  12. private readonly DHValidationParms validationParms;
  13. public static DHDomainParameters GetInstance(Asn1TaggedObject obj, bool isExplicit)
  14. {
  15. return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
  16. }
  17. public static DHDomainParameters GetInstance(object obj)
  18. {
  19. if (obj == null || obj is DHDomainParameters)
  20. return (DHDomainParameters)obj;
  21. if (obj is Asn1Sequence)
  22. return new DHDomainParameters((Asn1Sequence)obj);
  23. throw new ArgumentException("Invalid DHDomainParameters: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  24. }
  25. public DHDomainParameters(DerInteger p, DerInteger g, DerInteger q, DerInteger j,
  26. DHValidationParms validationParms)
  27. {
  28. if (p == null)
  29. throw new ArgumentNullException("p");
  30. if (g == null)
  31. throw new ArgumentNullException("g");
  32. if (q == null)
  33. throw new ArgumentNullException("q");
  34. this.p = p;
  35. this.g = g;
  36. this.q = q;
  37. this.j = j;
  38. this.validationParms = validationParms;
  39. }
  40. private DHDomainParameters(Asn1Sequence seq)
  41. {
  42. if (seq.Count < 3 || seq.Count > 5)
  43. throw new ArgumentException("Bad sequence size: " + seq.Count, "seq");
  44. IEnumerator e = seq.GetEnumerator();
  45. this.p = DerInteger.GetInstance(GetNext(e));
  46. this.g = DerInteger.GetInstance(GetNext(e));
  47. this.q = DerInteger.GetInstance(GetNext(e));
  48. Asn1Encodable next = GetNext(e);
  49. if (next != null && next is DerInteger)
  50. {
  51. this.j = DerInteger.GetInstance(next);
  52. next = GetNext(e);
  53. }
  54. if (next != null)
  55. {
  56. this.validationParms = DHValidationParms.GetInstance(next.ToAsn1Object());
  57. }
  58. }
  59. private static Asn1Encodable GetNext(IEnumerator e)
  60. {
  61. return e.MoveNext() ? (Asn1Encodable)e.Current : null;
  62. }
  63. public DerInteger P
  64. {
  65. get { return this.p; }
  66. }
  67. public DerInteger G
  68. {
  69. get { return this.g; }
  70. }
  71. public DerInteger Q
  72. {
  73. get { return this.q; }
  74. }
  75. public DerInteger J
  76. {
  77. get { return this.j; }
  78. }
  79. public DHValidationParms ValidationParms
  80. {
  81. get { return this.validationParms; }
  82. }
  83. public override Asn1Object ToAsn1Object()
  84. {
  85. Asn1EncodableVector v = new Asn1EncodableVector(p, g, q);
  86. v.AddOptional(j, validationParms);
  87. return new DerSequence(v);
  88. }
  89. }
  90. }
  91. #pragma warning restore
  92. #endif