2005-09-26 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System / System.Security.Cryptography / AsnEncodedData.cs
1 //
2 // AsnEncodedData.cs - System.Security.Cryptography.AsnEncodedData
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 #if NET_2_0
31
32 using System.Security.Cryptography.X509Certificates;
33 using System.Text;
34
35 using Mono.Security;
36 using Mono.Security.Cryptography;
37
38 namespace System.Security.Cryptography {
39
40         internal enum AsnDecodeStatus {
41                 NotDecoded = -1,
42                 Ok = 0,
43                 BadAsn = 1,
44                 BadTag = 2,
45                 BadLength = 3,
46                 InformationNotAvailable = 4
47         }
48
49         public class AsnEncodedData {
50
51                 internal Oid _oid;
52                 internal byte[] _raw;
53
54                 // constructors
55
56                 protected AsnEncodedData ()
57                 {
58                 }
59         
60                 public AsnEncodedData (string oid, byte[] rawData)
61                 {
62                         _oid = new Oid (oid);
63                         RawData = rawData;
64                 }
65
66                 public AsnEncodedData (Oid oid, byte[] rawData)
67                 {
68                         Oid = oid;
69                         RawData = rawData;
70
71                         // yes, here oid == null is legal (by design), 
72                         // but no, it would not be legal for an oid string
73                         // see MSDN FDBK11479
74                 }
75
76                 public AsnEncodedData (AsnEncodedData asnEncodedData)
77                 {
78                         if (asnEncodedData == null)
79                                 throw new ArgumentNullException ("asnEncodedData");
80
81                         Oid = new Oid (asnEncodedData._oid);
82                         RawData = asnEncodedData._raw;
83                 }
84
85                 public AsnEncodedData (byte[] rawData)
86                 {
87                         RawData = rawData;
88                 }
89
90                 // properties
91
92                 public Oid Oid {
93                         get { return _oid; }
94                         set {
95                                 if (value == null)
96                                         _oid = null;
97                                 else
98                                         _oid = new Oid (value);
99                         }
100                 }
101
102                 public byte[] RawData { 
103                         get { return _raw; }
104                         set {
105                                 if (value == null)
106                                         throw new ArgumentNullException ("RawData");
107                                 _raw = (byte[])value.Clone ();
108                         }
109                 }
110
111                 // methods
112
113                 public virtual void CopyFrom (AsnEncodedData asnEncodedData)
114                 {
115                         if (asnEncodedData == null)
116                                 throw new ArgumentNullException ("asnEncodedData");
117
118                         if (asnEncodedData._oid == null)
119                                 Oid = null;
120                         else
121                                 Oid = new Oid (asnEncodedData._oid);
122
123                         RawData = asnEncodedData._raw;
124                 }
125
126                 public virtual string Format (bool multiLine) 
127                 {
128                         if (_raw == null)
129                                 return String.Empty;
130
131                         if (_oid == null)
132                                 return Default (multiLine);
133
134                         return ToString (multiLine);
135                 }
136
137                 // internal decoding/formatting methods
138
139                 internal virtual string ToString (bool multiLine)
140                 {
141                         switch (_oid.Value) {
142                         // fx supported objects
143                         case X509BasicConstraintsExtension.oid:
144                                 return BasicConstraintsExtension (multiLine);
145                         case X509EnhancedKeyUsageExtension.oid:
146                                 return EnhancedKeyUsageExtension (multiLine);
147                         case X509KeyUsageExtension.oid:
148                                 return KeyUsageExtension (multiLine);
149                         case X509SubjectKeyIdentifierExtension.oid:
150                                 return SubjectKeyIdentifierExtension (multiLine);
151                         // other known objects (i.e. supported structure) - 
152                         // but without any corresponding framework class
153                         case Oid.oidSubjectAltName:
154                                 return SubjectAltName (multiLine);
155                         case Oid.oidNetscapeCertType:
156                                 return NetscapeCertType (multiLine);
157                         default:
158                                 return Default (multiLine);
159                         }
160                 }
161
162                 internal string Default (bool multiLine)
163                 {
164                         StringBuilder sb = new StringBuilder ();
165                         for (int i=0; i < _raw.Length; i++) {
166                                 sb.Append (_raw [i].ToString ("x2"));
167                                 if (i != _raw.Length - 1)
168                                         sb.Append (" ");
169                         }
170                         return sb.ToString ();
171                 }
172
173                 // Indirectly (undocumented but) supported extensions
174
175                 internal string BasicConstraintsExtension (bool multiLine)
176                 {
177                         try {
178                                 X509BasicConstraintsExtension bc = new X509BasicConstraintsExtension  (this, false);
179                                 return bc.ToString (multiLine);
180                         }
181                         catch {
182                                 return String.Empty;
183                         }
184                 }
185
186                 internal string EnhancedKeyUsageExtension (bool multiLine)
187                 {
188                         try {
189                                 X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension  (this, false);
190                                 return eku.ToString (multiLine);
191                         }
192                         catch {
193                                 return String.Empty;
194                         }
195                 }
196
197                 internal string KeyUsageExtension (bool multiLine)
198                 {
199                         try {
200                                 X509KeyUsageExtension ku = new X509KeyUsageExtension  (this, false);
201                                 return ku.ToString (multiLine);
202                         }
203                         catch {
204                                 return String.Empty;
205                         }
206                 }
207
208                 internal string SubjectKeyIdentifierExtension (bool multiLine)
209                 {
210                         try {
211                                 X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension  (this, false);
212                                 return ski.ToString (multiLine);
213                         }
214                         catch {
215                                 return String.Empty;
216                         }
217                 }
218
219                 // Indirectly (undocumented but) supported extensions
220
221                 internal string SubjectAltName (bool multiLine)
222                 {
223                         if (_raw.Length < 5)
224                                 return "Information Not Available";
225
226                         try {
227                                 ASN1 ex = new ASN1 (_raw);
228                                 StringBuilder sb = new StringBuilder ();
229                                 for (int i=0; i < ex.Count; i++) {
230                                         ASN1 el = ex [i];
231
232                                         string type = null;
233                                         string name = null;
234
235                                         switch (el.Tag) {
236                                         case 0x81:
237                                                 type = "RFC822 Name=";
238                                                 name = Encoding.ASCII.GetString (el.Value);
239                                                 break;
240                                         case 0x82:
241                                                 type = "DNS Name=";
242                                                 name = Encoding.ASCII.GetString (el.Value);
243                                                 break;
244                                         default:
245                                                 type = String.Format ("Unknown ({0})=", el.Tag);
246                                                 name = CryptoConvert.ToHex (el.Value);
247                                                 break;
248                                         }
249
250                                         sb.Append (type);
251                                         sb.Append (name);
252                                         if (multiLine) {
253                                                 sb.Append (Environment.NewLine);
254                                         } else if (i < ex.Count - 1) {
255                                                 sb.Append (", ");
256                                         }
257                                 }
258                                 return sb.ToString ();
259                         }
260                         catch {
261                                 return String.Empty;
262                         }
263                 }
264
265                 internal string NetscapeCertType (bool multiLine)
266                 {
267                         // 4 byte long, BITSTRING (0x03), Value length of 2
268                         if ((_raw.Length < 4) || (_raw [0] != 0x03) || (_raw [1] != 0x02))
269                                 return "Information Not Available";
270                         // first value byte is the number of unused bits
271                         int value = (_raw [3] >> _raw [2]) << _raw [2];
272
273                         StringBuilder sb = new StringBuilder ();
274
275                         if ((value & 0x80) == 0x80) {
276                                 sb.Append ("SSL Client Authentication");
277                         }
278                         if ((value & 0x40) == 0x40) {
279                                 if (sb.Length > 0)
280                                         sb.Append (", ");
281                                 sb.Append ("SSL Server Authentication");
282                         }
283                         if ((value & 0x20) == 0x20) {
284                                 if (sb.Length > 0)
285                                         sb.Append (", ");
286                                 sb.Append ("SMIME");
287                         }
288                         if ((value & 0x10) == 0x10) {
289                                 if (sb.Length > 0)
290                                         sb.Append (", ");
291                                 sb.Append ("Signature"); // a.k.a. Object Signing / Code Signing
292                         }
293                         if ((value & 0x08) == 0x08) {
294                                 if (sb.Length > 0)
295                                         sb.Append (", ");
296                                 sb.Append ("Unknown cert type");
297                         }
298                         if ((value & 0x04) == 0x04) {
299                                 if (sb.Length > 0)
300                                         sb.Append (", ");
301                                 sb.Append ("SSL CA");   // CA == Certificate Authority
302                         }
303                         if ((value & 0x02) == 0x02) {
304                                 if (sb.Length > 0)
305                                         sb.Append (", ");
306                                 sb.Append ("SMIME CA");
307                         }
308                         if ((value & 0x01) == 0x01) {
309                                 if (sb.Length > 0)
310                                         sb.Append (", ");
311                                 sb.Append ("Signature CA");
312                         }
313                         sb.AppendFormat (" ({0})", value.ToString ("x2"));
314                         return sb.ToString ();
315                 }
316         }
317 }
318
319 #endif