**** Merged from MCS ****
[mono-project.git] / mcs / class / Mono.Security / Mono.Security.X509.Extensions / BasicConstraintsExtension.cs
blob8bf4e3ab0bc06014f7dd19474ca2a5f64f4d7ab2
1 //
2 // BasicConstraintsExtension.cs: Handles X.509 BasicConstrains extensions.
3 //
4 // Author:
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
9 //
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:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
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.
32 using System;
33 using System.Globalization;
34 using System.Text;
36 using Mono.Security;
37 using Mono.Security.X509;
39 namespace Mono.Security.X509.Extensions {
41 // References:
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
50 * }
52 #if INSIDE_CORLIB
53 internal
54 #else
55 public
56 #endif
57 class BasicConstraintsExtension : X509Extension {
59 private bool cA;
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 ()
73 // default values
74 cA = false;
75 pathLenConstraint = 0; // no constraint
77 ASN1 sequence = new ASN1 (extnValue.Value);
78 if (sequence.Tag != 0x30)
79 throw new ArgumentException ("Invalid BasicConstraints extension");
80 int n = 0;
81 ASN1 a = sequence [n++];
82 if ((a != null) && (a.Tag == 0x01)) {
83 cA = (a.Value [0] == 0xFF);
84 a = sequence [n++];
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);
94 if (cA)
95 extnValue.Add (new ASN1 (0x01, new byte[] { 0xFF }));
96 if (pathLenConstraint > 0)
97 extnValue.Add (ASN1Convert.FromInt32 (pathLenConstraint));
101 public bool CertificateAuthority {
102 get { return cA; }
103 set { cA = value; }
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)
123 sb.Append ("None");
124 else
125 sb.Append (pathLenConstraint.ToString (CultureInfo.InvariantCulture));
126 sb.Append (Environment.NewLine);
127 return sb.ToString ();