Merge pull request #645 from knocte/nitpicks
[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 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Globalization;
32 using System.Text;
33
34 using Mono.Security;
35 using Mono.Security.X509;
36
37 namespace Mono.Security.X509.Extensions {
38
39         // References:
40         // 1.   RFC 3280: Internet X.509 Public Key Infrastructure, Section 4.2.1.10
41         //      http://www.ietf.org/rfc/rfc3280.txt
42
43         /* id-ce-basicConstraints OBJECT IDENTIFIER ::=  { id-ce 19 }
44          * 
45          * BasicConstraints ::= SEQUENCE {
46          *      cA                      BOOLEAN DEFAULT FALSE,
47          *      pathLenConstraint       INTEGER (0..MAX) OPTIONAL 
48          * }
49          */
50 #if INSIDE_CORLIB
51         internal
52 #else
53         public 
54 #endif
55         class BasicConstraintsExtension : X509Extension {
56
57                 public const int NoPathLengthConstraint = -1;
58
59                 private bool cA;
60                 private int pathLenConstraint;
61
62                 public BasicConstraintsExtension () : base () 
63                 {
64                         extnOid = "2.5.29.19";
65                         pathLenConstraint = NoPathLengthConstraint;
66                 }
67
68                 public BasicConstraintsExtension (ASN1 asn1) : base (asn1) {}
69
70                 public BasicConstraintsExtension (X509Extension extension) : base (extension) {}
71
72                 protected override void Decode () 
73                 {
74                         // default values
75                         cA = false;
76                         pathLenConstraint = NoPathLengthConstraint;
77
78                         ASN1 sequence = new ASN1 (extnValue.Value);
79                         if (sequence.Tag != 0x30)
80                                 throw new ArgumentException ("Invalid BasicConstraints extension");
81                         int n = 0;
82                         ASN1 a = sequence [n++];
83                         if ((a != null) && (a.Tag == 0x01)) {
84                                 cA = (a.Value [0] == 0xFF);
85                                 a = sequence [n++];
86                         }
87                         if ((a != null) && (a.Tag == 0x02))
88                                 pathLenConstraint = ASN1Convert.ToInt32 (a);
89                 }
90
91                 protected override void Encode () 
92                 {
93                         ASN1 seq = new ASN1 (0x30);
94                         if (cA)
95                                 seq.Add (new ASN1 (0x01, new byte[] { 0xFF }));
96                         // CAs MUST NOT include the pathLenConstraint field unless the cA boolean is asserted
97                         if (cA && (pathLenConstraint >= 0))
98                                 seq.Add (ASN1Convert.FromInt32 (pathLenConstraint));
99
100                         extnValue = new ASN1 (0x04);
101                         extnValue.Add (seq);
102                 }
103
104                 public bool CertificateAuthority {
105                         get { return cA; }
106                         set { cA = value; }
107                 }
108
109                 public override string Name {
110                         get { return "Basic Constraints"; }
111                 }
112
113                 public int PathLenConstraint {
114                         get { return pathLenConstraint; }
115                         set {
116                                 if (value < NoPathLengthConstraint) {
117                                         string msg = Locale.GetText ("PathLenConstraint must be positive or -1 for none ({0}).", value);
118                                         throw new ArgumentOutOfRangeException (msg);
119                                 }
120                                 pathLenConstraint = value;
121                         }
122                 }
123
124                 public override string ToString () 
125                 {
126                         StringBuilder sb = new StringBuilder ();
127                         sb.Append ("Subject Type=");
128                         sb.Append ((cA) ? "CA" : "End Entity");
129                         sb.Append (Environment.NewLine);
130                         sb.Append ("Path Length Constraint=");
131                         if (pathLenConstraint == NoPathLengthConstraint)
132                                 sb.Append ("None");
133                         else
134                                 sb.Append (pathLenConstraint.ToString (CultureInfo.InvariantCulture));
135                         sb.Append (Environment.NewLine);
136                         return sb.ToString ();
137                 }
138         }
139 }