imported everything from my branch (which is slightly harmless).
[mono.git] / mcs / class / corlib / 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 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
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:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
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.
32 //
33
34 using System;
35 using System.Globalization;
36 using System.Text;
37
38 using Mono.Security;
39 using Mono.Security.X509;
40
41 namespace Mono.Security.X509.Extensions {
42
43         // References:
44         // 1.   RFC 3280: Internet X.509 Public Key Infrastructure, Section 4.2.1.10
45         //      http://www.ietf.org/rfc/rfc3280.txt
46
47         /* id-ce-basicConstraints OBJECT IDENTIFIER ::=  { id-ce 19 }
48          * 
49          * BasicConstraints ::= SEQUENCE {
50          *      cA                      BOOLEAN DEFAULT FALSE,
51          *      pathLenConstraint       INTEGER (0..MAX) OPTIONAL 
52          * }
53          */
54 #if INSIDE_CORLIB
55         internal
56 #else
57         public 
58 #endif
59         class BasicConstraintsExtension : X509Extension {
60
61                 private bool cA;
62                 private int pathLenConstraint;
63
64                 public BasicConstraintsExtension () : base () 
65                 {
66                         extnOid = "2.5.29.19";
67                 }
68
69                 public BasicConstraintsExtension (ASN1 asn1) : base (asn1) {}
70
71                 public BasicConstraintsExtension (X509Extension extension) : base (extension) {}
72
73                 protected override void Decode () 
74                 {
75                         // default values
76                         cA = false;
77                         pathLenConstraint = 0; // no constraint
78
79                         ASN1 sequence = new ASN1 (extnValue.Value);
80                         if (sequence.Tag != 0x30)
81                                 throw new ArgumentException ("Invalid BasicConstraints extension");
82                         int n = 0;
83                         ASN1 a = sequence [n++];
84                         if ((a != null) && (a.Tag == 0x01)) {
85                                 cA = (a.Value [0] == 0xFF);
86                                 a = sequence [n++];
87                         }
88                         if ((a != null) && (a.Tag == 0x02))
89                                 pathLenConstraint = ASN1Convert.ToInt32 (a);
90                 }
91
92                 protected override void Encode () 
93                 {
94                         if (extnValue == null) {
95                                 extnValue = new ASN1 (0x30);
96                                 if (cA)
97                                         extnValue.Add (new ASN1 (0x01, new byte[] { 0xFF }));
98                                 if (pathLenConstraint > 0)
99                                         extnValue.Add (ASN1Convert.FromInt32 (pathLenConstraint));
100                         }
101                 }
102
103                 public bool CertificateAuthority {
104                         get { return cA; }
105                         set { cA = value; }
106                 }
107
108                 public override string Name {
109                         get { return "Basic Constraints"; }
110                 }
111
112                 public int PathLenConstraint {
113                         get { return pathLenConstraint; }
114                         set { pathLenConstraint = value; }
115                 }
116
117                 public override string ToString () 
118                 {
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)
125                                 sb.Append ("None");
126                         else
127                                 sb.Append (pathLenConstraint.ToString (CultureInfo.InvariantCulture));
128                         sb.Append (Environment.NewLine);
129                         return sb.ToString ();
130                 }
131         }
132 }