This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509.Extensions / BasicConstraintsExtension.cs
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 //
10
11 //
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.
30 //
31
32 using System;
33 using System.Globalization;
34 using System.Text;
35
36 using Mono.Security;
37 using Mono.Security.X509;
38
39 namespace Mono.Security.X509.Extensions {
40
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
44
45         /* id-ce-basicConstraints OBJECT IDENTIFIER ::=  { id-ce 19 }
46          * 
47          * BasicConstraints ::= SEQUENCE {
48          *      cA                      BOOLEAN DEFAULT FALSE,
49          *      pathLenConstraint       INTEGER (0..MAX) OPTIONAL 
50          * }
51          */
52 #if INSIDE_CORLIB
53         internal
54 #else
55         public 
56 #endif
57         class BasicConstraintsExtension : X509Extension {
58
59                 private bool cA;
60                 private int pathLenConstraint;
61
62                 public BasicConstraintsExtension () : base () 
63                 {
64                         extnOid = "2.5.29.19";
65                 }
66
67                 public BasicConstraintsExtension (ASN1 asn1) : base (asn1) {}
68
69                 public BasicConstraintsExtension (X509Extension extension) : base (extension) {}
70
71                 protected override void Decode () 
72                 {
73                         // default values
74                         cA = false;
75                         pathLenConstraint = 0; // no constraint
76
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++];
85                         }
86                         if ((a != null) && (a.Tag == 0x02))
87                                 pathLenConstraint = ASN1Convert.ToInt32 (a);
88                 }
89
90                 protected override void Encode () 
91                 {
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));
98                         }
99                 }
100
101                 public bool CertificateAuthority {
102                         get { return cA; }
103                         set { cA = value; }
104                 }
105
106                 public override string Name {
107                         get { return "Basic Constraints"; }
108                 }
109
110                 public int PathLenConstraint {
111                         get { return pathLenConstraint; }
112                         set { pathLenConstraint = value; }
113                 }
114
115                 public override string ToString () 
116                 {
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 ();
128                 }
129         }
130 }