DHDomainParameters.cs 2.6 KB

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