BasicConstraints.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  7. {
  8. public class BasicConstraints
  9. : Asn1Encodable
  10. {
  11. private readonly DerBoolean cA;
  12. private readonly DerInteger pathLenConstraint;
  13. public static BasicConstraints GetInstance(
  14. Asn1TaggedObject obj,
  15. bool explicitly)
  16. {
  17. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  18. }
  19. public static BasicConstraints GetInstance(
  20. object obj)
  21. {
  22. if (obj == null || obj is BasicConstraints)
  23. {
  24. return (BasicConstraints) obj;
  25. }
  26. if (obj is Asn1Sequence)
  27. {
  28. return new BasicConstraints((Asn1Sequence) obj);
  29. }
  30. if (obj is X509Extension)
  31. {
  32. return GetInstance(X509Extension.ConvertValueToObject((X509Extension) obj));
  33. }
  34. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  35. }
  36. private BasicConstraints(
  37. Asn1Sequence seq)
  38. {
  39. if (seq.Count > 0)
  40. {
  41. if (seq[0] is DerBoolean)
  42. {
  43. this.cA = DerBoolean.GetInstance(seq[0]);
  44. }
  45. else
  46. {
  47. this.pathLenConstraint = DerInteger.GetInstance(seq[0]);
  48. }
  49. if (seq.Count > 1)
  50. {
  51. if (this.cA == null)
  52. throw new ArgumentException("wrong sequence in constructor", "seq");
  53. this.pathLenConstraint = DerInteger.GetInstance(seq[1]);
  54. }
  55. }
  56. }
  57. public BasicConstraints(
  58. bool cA)
  59. {
  60. if (cA)
  61. {
  62. this.cA = DerBoolean.True;
  63. }
  64. }
  65. /**
  66. * create a cA=true object for the given path length constraint.
  67. *
  68. * @param pathLenConstraint
  69. */
  70. public BasicConstraints(
  71. int pathLenConstraint)
  72. {
  73. this.cA = DerBoolean.True;
  74. this.pathLenConstraint = new DerInteger(pathLenConstraint);
  75. }
  76. public bool IsCA()
  77. {
  78. return cA != null && cA.IsTrue;
  79. }
  80. public BigInteger PathLenConstraint
  81. {
  82. get { return pathLenConstraint == null ? null : pathLenConstraint.Value; }
  83. }
  84. /**
  85. * Produce an object suitable for an Asn1OutputStream.
  86. * <pre>
  87. * BasicConstraints := Sequence {
  88. * cA Boolean DEFAULT FALSE,
  89. * pathLenConstraint Integer (0..MAX) OPTIONAL
  90. * }
  91. * </pre>
  92. */
  93. public override Asn1Object ToAsn1Object()
  94. {
  95. Asn1EncodableVector v = new Asn1EncodableVector();
  96. if (cA != null)
  97. {
  98. v.Add(cA);
  99. }
  100. if (pathLenConstraint != null) // yes some people actually do this when cA is false...
  101. {
  102. v.Add(pathLenConstraint);
  103. }
  104. return new DerSequence(v);
  105. }
  106. public override string ToString()
  107. {
  108. if (pathLenConstraint == null)
  109. {
  110. return "BasicConstraints: isCa(" + this.IsCA() + ")";
  111. }
  112. return "BasicConstraints: isCa(" + this.IsCA() + "), pathLenConstraint = " + pathLenConstraint.Value;
  113. }
  114. }
  115. }
  116. #pragma warning restore
  117. #endif