2 // AsnEncodedData.cs - System.Security.Cryptography.AsnEncodedData
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell Inc. (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 extern alias MonoSecurity;
34 using System.Security.Cryptography.X509Certificates;
37 using MonoSecurity::Mono.Security;
38 using MonoSecurity::Mono.Security.Cryptography;
40 namespace System.Security.Cryptography {
42 internal enum AsnDecodeStatus {
48 InformationNotAvailable = 4
51 public class AsnEncodedData {
58 protected AsnEncodedData ()
62 public AsnEncodedData (string oid, byte[] rawData)
68 public AsnEncodedData (Oid oid, byte[] rawData)
73 // yes, here oid == null is legal (by design),
74 // but no, it would not be legal for an oid string
78 public AsnEncodedData (AsnEncodedData asnEncodedData)
80 if (asnEncodedData == null)
81 throw new ArgumentNullException ("asnEncodedData");
83 if (asnEncodedData._oid != null)
84 Oid = new Oid (asnEncodedData._oid);
85 RawData = asnEncodedData._raw;
88 public AsnEncodedData (byte[] rawData)
101 _oid = new Oid (value);
105 public byte[] RawData {
109 throw new ArgumentNullException ("RawData");
110 _raw = (byte[])value.Clone ();
116 public virtual void CopyFrom (AsnEncodedData asnEncodedData)
118 if (asnEncodedData == null)
119 throw new ArgumentNullException ("asnEncodedData");
121 if (asnEncodedData._oid == null)
124 Oid = new Oid (asnEncodedData._oid);
126 RawData = asnEncodedData._raw;
129 public virtual string Format (bool multiLine)
135 return Default (multiLine);
137 return ToString (multiLine);
140 // internal decoding/formatting methods
142 internal virtual string ToString (bool multiLine)
144 switch (_oid.Value) {
145 // fx supported objects
146 case X509BasicConstraintsExtension.oid:
147 return BasicConstraintsExtension (multiLine);
148 case X509EnhancedKeyUsageExtension.oid:
149 return EnhancedKeyUsageExtension (multiLine);
150 case X509KeyUsageExtension.oid:
151 return KeyUsageExtension (multiLine);
152 case X509SubjectKeyIdentifierExtension.oid:
153 return SubjectKeyIdentifierExtension (multiLine);
154 // other known objects (i.e. supported structure) -
155 // but without any corresponding framework class
156 case Oid.oidSubjectAltName:
157 return SubjectAltName (multiLine);
158 case Oid.oidNetscapeCertType:
159 return NetscapeCertType (multiLine);
161 return Default (multiLine);
165 internal string Default (bool multiLine)
167 StringBuilder sb = new StringBuilder ();
168 for (int i=0; i < _raw.Length; i++) {
169 sb.Append (_raw [i].ToString ("x2"));
170 if (i != _raw.Length - 1)
173 return sb.ToString ();
176 // Indirectly (undocumented but) supported extensions
178 internal string BasicConstraintsExtension (bool multiLine)
181 X509BasicConstraintsExtension bc = new X509BasicConstraintsExtension (this, false);
182 return bc.ToString (multiLine);
189 internal string EnhancedKeyUsageExtension (bool multiLine)
192 X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (this, false);
193 return eku.ToString (multiLine);
200 internal string KeyUsageExtension (bool multiLine)
203 X509KeyUsageExtension ku = new X509KeyUsageExtension (this, false);
204 return ku.ToString (multiLine);
211 internal string SubjectKeyIdentifierExtension (bool multiLine)
214 X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (this, false);
215 return ski.ToString (multiLine);
222 // Indirectly (undocumented but) supported extensions
224 internal string SubjectAltName (bool multiLine)
227 return "Information Not Available";
230 ASN1 ex = new ASN1 (_raw);
231 StringBuilder sb = new StringBuilder ();
232 for (int i=0; i < ex.Count; i++) {
240 type = "RFC822 Name=";
241 name = Encoding.ASCII.GetString (el.Value);
245 name = Encoding.ASCII.GetString (el.Value);
248 type = String.Format ("Unknown ({0})=", el.Tag);
249 name = CryptoConvert.ToHex (el.Value);
256 sb.Append (Environment.NewLine);
257 } else if (i < ex.Count - 1) {
261 return sb.ToString ();
268 internal string NetscapeCertType (bool multiLine)
270 // 4 byte long, BITSTRING (0x03), Value length of 2
271 if ((_raw.Length < 4) || (_raw [0] != 0x03) || (_raw [1] != 0x02))
272 return "Information Not Available";
273 // first value byte is the number of unused bits
274 int value = (_raw [3] >> _raw [2]) << _raw [2];
276 StringBuilder sb = new StringBuilder ();
278 if ((value & 0x80) == 0x80) {
279 sb.Append ("SSL Client Authentication");
281 if ((value & 0x40) == 0x40) {
284 sb.Append ("SSL Server Authentication");
286 if ((value & 0x20) == 0x20) {
291 if ((value & 0x10) == 0x10) {
294 sb.Append ("Signature"); // a.k.a. Object Signing / Code Signing
296 if ((value & 0x08) == 0x08) {
299 sb.Append ("Unknown cert type");
301 if ((value & 0x04) == 0x04) {
304 sb.Append ("SSL CA"); // CA == Certificate Authority
306 if ((value & 0x02) == 0x02) {
309 sb.Append ("SMIME CA");
311 if ((value & 0x01) == 0x01) {
314 sb.Append ("Signature CA");
316 sb.AppendFormat (" ({0})", value.ToString ("x2"));
317 return sb.ToString ();