2 // BasicConstraintsExtension.cs: Handles X.509 BasicConstrains extensions.
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
14 using Mono.Security.X509;
16 namespace Mono.Security.X509.Extensions {
19 // 1. RFC 3280: Internet X.509 Public Key Infrastructure, Section 4.2.1.10
20 // http://www.ietf.org/rfc/rfc3280.txt
22 /* id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 }
24 * BasicConstraints ::= SEQUENCE {
25 * cA BOOLEAN DEFAULT FALSE,
26 * pathLenConstraint INTEGER (0..MAX) OPTIONAL
29 public class BasicConstraintsExtension : X509Extension {
32 private int pathLenConstraint;
34 public BasicConstraintsExtension () : base ()
36 extnOid = "2.5.29.19";
39 public BasicConstraintsExtension (ASN1 asn1) : base (asn1) {}
41 public BasicConstraintsExtension (X509Extension extension) : base (extension) {}
43 protected override void Decode ()
47 pathLenConstraint = 0; // no constraint
49 ASN1 sequence = new ASN1 (extnValue.Value);
50 if (sequence.Tag != 0x30)
51 throw new ArgumentException ("Invalid BasicConstraints extension");
53 ASN1 a = sequence [n++];
54 if ((a != null) && (a.Tag == 0x01)) {
55 cA = (a.Value [0] == 0xFF);
58 if ((a != null) && (a.Tag == 0x02))
59 pathLenConstraint = ASN1Convert.ToInt32 (a);
62 protected override void Encode ()
64 if (extnValue == null) {
65 extnValue = new ASN1 (0x30);
67 extnValue.Add (new ASN1 (0x01, new byte[] { 0xFF }));
68 if (pathLenConstraint > 0)
69 extnValue.Add (ASN1Convert.FromInt32 (pathLenConstraint));
73 public bool CertificateAuthority {
78 public override string Name {
79 get { return "Basic Constraints"; }
82 public int PathLenConstraint {
83 get { return pathLenConstraint; }
84 set { pathLenConstraint = value; }
87 public override string ToString ()
89 StringBuilder sb = new StringBuilder ();
90 sb.Append ("Subject Type=");
91 sb.Append ((cA) ? "CA" : "End Entity");
92 sb.Append (Environment.NewLine);
93 sb.Append ("Path Length Constraint=");
94 if (pathLenConstraint == 0)
97 sb.Append (pathLenConstraint.ToString ());
98 sb.Append (Environment.NewLine);
99 return sb.ToString ();