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