BasicConstraints.cs 3.2 KB

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