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 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Globalization;
39 using Mono.Security.X509;
41 namespace Mono.Security.X509.Extensions {
44 // 1. RFC 3280: Internet X.509 Public Key Infrastructure, Section 4.2.1.10
45 // http://www.ietf.org/rfc/rfc3280.txt
47 /* id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 }
49 * BasicConstraints ::= SEQUENCE {
50 * cA BOOLEAN DEFAULT FALSE,
51 * pathLenConstraint INTEGER (0..MAX) OPTIONAL
59 class BasicConstraintsExtension : X509Extension {
62 private int pathLenConstraint;
64 public BasicConstraintsExtension () : base ()
66 extnOid = "2.5.29.19";
69 public BasicConstraintsExtension (ASN1 asn1) : base (asn1) {}
71 public BasicConstraintsExtension (X509Extension extension) : base (extension) {}
73 protected override void Decode ()
77 pathLenConstraint = 0; // no constraint
79 ASN1 sequence = new ASN1 (extnValue.Value);
80 if (sequence.Tag != 0x30)
81 throw new ArgumentException ("Invalid BasicConstraints extension");
83 ASN1 a = sequence [n++];
84 if ((a != null) && (a.Tag == 0x01)) {
85 cA = (a.Value [0] == 0xFF);
88 if ((a != null) && (a.Tag == 0x02))
89 pathLenConstraint = ASN1Convert.ToInt32 (a);
92 protected override void Encode ()
94 if (extnValue == null) {
95 extnValue = new ASN1 (0x30);
97 extnValue.Add (new ASN1 (0x01, new byte[] { 0xFF }));
98 if (pathLenConstraint > 0)
99 extnValue.Add (ASN1Convert.FromInt32 (pathLenConstraint));
103 public bool CertificateAuthority {
108 public override string Name {
109 get { return "Basic Constraints"; }
112 public int PathLenConstraint {
113 get { return pathLenConstraint; }
114 set { pathLenConstraint = value; }
117 public override string ToString ()
119 StringBuilder sb = new StringBuilder ();
120 sb.Append ("Subject Type=");
121 sb.Append ((cA) ? "CA" : "End Entity");
122 sb.Append (Environment.NewLine);
123 sb.Append ("Path Length Constraint=");
124 if (pathLenConstraint == 0)
127 sb.Append (pathLenConstraint.ToString (CultureInfo.InvariantCulture));
128 sb.Append (Environment.NewLine);
129 return sb.ToString ();