BasicConstraints.cs 3.3 KB

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