2 // BasicConstraintsExtension.cs: Handles X.509 BasicConstrains extensions.
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
12 using System.Globalization;
16 using Mono.Security.X509;
18 namespace Mono.Security.X509.Extensions {
21 // 1. RFC 3280: Internet X.509 Public Key Infrastructure, Section 4.2.1.10
22 // http://www.ietf.org/rfc/rfc3280.txt
24 /* id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 }
26 * BasicConstraints ::= SEQUENCE {
27 * cA BOOLEAN DEFAULT FALSE,
28 * pathLenConstraint INTEGER (0..MAX) OPTIONAL
36 class BasicConstraintsExtension : X509Extension {
39 private int pathLenConstraint;
41 public BasicConstraintsExtension () : base ()
43 extnOid = "2.5.29.19";
46 public BasicConstraintsExtension (ASN1 asn1) : base (asn1) {}
48 public BasicConstraintsExtension (X509Extension extension) : base (extension) {}
50 protected override void Decode ()
54 pathLenConstraint = 0; // no constraint
56 ASN1 sequence = new ASN1 (extnValue.Value);
57 if (sequence.Tag != 0x30)
58 throw new ArgumentException ("Invalid BasicConstraints extension");
60 ASN1 a = sequence [n++];
61 if ((a != null) && (a.Tag == 0x01)) {
62 cA = (a.Value [0] == 0xFF);
65 if ((a != null) && (a.Tag == 0x02))
66 pathLenConstraint = ASN1Convert.ToInt32 (a);
69 protected override void Encode ()
71 if (extnValue == null) {
72 extnValue = new ASN1 (0x30);
74 extnValue.Add (new ASN1 (0x01, new byte[] { 0xFF }));
75 if (pathLenConstraint > 0)
76 extnValue.Add (ASN1Convert.FromInt32 (pathLenConstraint));
80 public bool CertificateAuthority {
85 public override string Name {
86 get { return "Basic Constraints"; }
89 public int PathLenConstraint {
90 get { return pathLenConstraint; }
91 set { pathLenConstraint = value; }
94 public override string ToString ()
96 StringBuilder sb = new StringBuilder ();
97 sb.Append ("Subject Type=");
98 sb.Append ((cA) ? "CA" : "End Entity");
99 sb.Append (Environment.NewLine);
100 sb.Append ("Path Length Constraint=");
101 if (pathLenConstraint == 0)
104 sb.Append (pathLenConstraint.ToString (CultureInfo.InvariantCulture));
105 sb.Append (Environment.NewLine);
106 return sb.ToString ();