Update PointConverter.cs
[mono.git] / mcs / class / System / System.Security.Cryptography.X509Certificates / X509BasicConstraintsExtension.cs
1 //
2 // System.Security.Cryptography.X509BasicConstraintsExtension
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //      Tim Coleman (tim@timcoleman.com)
7 //
8 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) Tim Coleman, 2004
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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 #if SECURITY_DEP
33
34 extern alias MonoSecurity;
35
36 using System.Text;
37
38 using MonoSecurity::Mono.Security;
39
40 namespace System.Security.Cryptography.X509Certificates {
41
42         public sealed class X509BasicConstraintsExtension : X509Extension {
43
44                 internal const string oid = "2.5.29.19";
45                 internal const string friendlyName = "Basic Constraints";
46
47                 private bool _certificateAuthority;
48                 private bool _hasPathLengthConstraint;
49                 private int _pathLengthConstraint;
50                 private AsnDecodeStatus _status;
51
52                 // constructors
53
54                 public X509BasicConstraintsExtension ()
55                 {
56                         _oid = new Oid (oid, friendlyName);
57                 }
58
59                 public X509BasicConstraintsExtension (AsnEncodedData encodedBasicConstraints, bool critical)
60                 {
61                         // ignore the Oid provided by encodedKeyUsage (our rules!)
62                         _oid = new Oid (oid, friendlyName);
63                         _raw = encodedBasicConstraints.RawData;
64                         base.Critical = critical;
65                         _status = Decode (this.RawData);
66                 }
67
68                 public X509BasicConstraintsExtension (bool certificateAuthority, bool hasPathLengthConstraint, int pathLengthConstraint, bool critical)
69                 {
70                         if (hasPathLengthConstraint) {
71                                 if (pathLengthConstraint < 0)
72                                         throw new ArgumentOutOfRangeException ("pathLengthConstraint");
73                                 _pathLengthConstraint = pathLengthConstraint;
74                         }
75                         _hasPathLengthConstraint = hasPathLengthConstraint;
76                         _certificateAuthority = certificateAuthority;
77                         _oid = new Oid (oid, friendlyName);
78                         base.Critical = critical;
79                         RawData = Encode ();
80                 }
81
82                 // properties
83
84                 public bool CertificateAuthority {
85                         get {
86                                 switch (_status) {
87                                 case AsnDecodeStatus.Ok:
88                                 case AsnDecodeStatus.InformationNotAvailable:
89                                         return _certificateAuthority;
90                                 default:
91                                         throw new CryptographicException ("Badly encoded extension.");
92                                 }
93                         }
94                 }
95
96                 public bool HasPathLengthConstraint {
97                         get {
98                                 switch (_status) {
99                                 case AsnDecodeStatus.Ok:
100                                 case AsnDecodeStatus.InformationNotAvailable:
101                                         return _hasPathLengthConstraint;
102                                 default:
103                                         throw new CryptographicException ("Badly encoded extension.");
104                                 }
105                         }
106                 }
107
108                 public int PathLengthConstraint {
109                         get {
110                                 switch (_status) {
111                                 case AsnDecodeStatus.Ok:
112                                 case AsnDecodeStatus.InformationNotAvailable:
113                                         return _pathLengthConstraint;
114                                 default:
115                                         throw new CryptographicException ("Badly encoded extension.");
116                                 }
117                         }
118                 }
119
120                 // methods
121
122                 public override void CopyFrom (AsnEncodedData asnEncodedData) 
123                 {
124                         if (asnEncodedData == null)
125                                 throw new ArgumentNullException ("asnEncodedData");
126
127                         X509Extension ex = (asnEncodedData as X509Extension);
128                         if (ex == null)
129                                 throw new ArgumentException (Locale.GetText ("Wrong type."), "asnEncodedData");
130
131                         if (ex._oid == null)
132                                 _oid = new Oid (oid, friendlyName);
133                         else 
134                                 _oid = new Oid (ex._oid);
135
136                         RawData = ex.RawData;
137                         base.Critical = ex.Critical;
138                         // and we deal with the rest later
139                         _status = Decode (this.RawData);
140                 }
141
142                 // internal
143
144                 internal AsnDecodeStatus Decode (byte[] extension)
145                 {
146                         if ((extension == null) || (extension.Length == 0))
147                                 return AsnDecodeStatus.BadAsn;
148                         if (extension [0] != 0x30)
149                                 return AsnDecodeStatus.BadTag;
150                         if (extension.Length < 3) {
151                                 if (!((extension.Length == 2) && (extension [1] == 0x00)))
152                                         return AsnDecodeStatus.BadLength;
153                         }
154
155                         try {
156                                 ASN1 sequence = new ASN1 (extension);
157                                 int n = 0;
158                                 ASN1 a = sequence [n++];
159                                 if ((a != null) && (a.Tag == 0x01)) {
160                                         _certificateAuthority = (a.Value [0] == 0xFF);
161                                         a = sequence [n++];
162                                 }
163                                 if ((a != null) && (a.Tag == 0x02)) {
164                                         _hasPathLengthConstraint = true;
165                                         _pathLengthConstraint = ASN1Convert.ToInt32 (a);
166                                 }
167                         }
168                         catch {
169                                 return AsnDecodeStatus.BadAsn;
170                         }
171
172                         return AsnDecodeStatus.Ok;
173                 }
174
175                 internal byte[] Encode ()
176                 {
177                         ASN1 ex = new ASN1 (0x30);
178
179                         if (_certificateAuthority)
180                                 ex.Add (new ASN1 (0x01, new byte[] { 0xFF }));
181                         if (_hasPathLengthConstraint) {
182                                 // MS encodes the 0 (pathLengthConstraint is OPTIONAL)
183                                 // and in a long form (02 00 versus 02 01 00)
184                                 if (_pathLengthConstraint == 0)
185                                         ex.Add (new ASN1 (0x02, new byte[] { 0x00 }));
186                                 else
187                                         ex.Add (ASN1Convert.FromInt32 (_pathLengthConstraint));
188                         }
189
190                         return ex.GetBytes ();
191                 }
192
193                 internal override string ToString (bool multiLine)
194                 {
195                         switch (_status) {
196                         case AsnDecodeStatus.BadAsn:
197                                 return String.Empty;
198                         case AsnDecodeStatus.BadTag:
199                         case AsnDecodeStatus.BadLength:
200                                 return FormatUnkownData (_raw);
201                         case AsnDecodeStatus.InformationNotAvailable:
202                                 return "Information Not Available";
203                         }
204
205                         if (_oid.Value != oid)
206                                 return String.Format ("Unknown Key Usage ({0})", _oid.Value);
207
208                         StringBuilder sb = new StringBuilder ();
209
210                         sb.Append ("Subject Type=");
211                         if (_certificateAuthority)
212                                 sb.Append ("CA");
213                         else
214                                 sb.Append ("End Entity");
215                         if (multiLine)
216                                 sb.Append (Environment.NewLine);
217                         else
218                                 sb.Append (", ");
219
220                         sb.Append ("Path Length Constraint=");
221                         if (_hasPathLengthConstraint) 
222                                 sb.Append (_pathLengthConstraint);
223                         else
224                                 sb.Append ("None");
225                         if (multiLine)
226                                 sb.Append (Environment.NewLine);
227
228                         return sb.ToString ();
229                 }
230         }
231 }
232
233 #endif