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 using System.Security.Cryptography.X509Certificates;
36 using Mono.Security.Cryptography;
38 namespace System.Security.Cryptography {
40 internal enum AsnDecodeStatus {
46 InformationNotAvailable = 4
49 public class AsnEncodedData {
56 protected AsnEncodedData ()
60 public AsnEncodedData (string oid, byte[] rawData)
66 public AsnEncodedData (Oid oid, byte[] rawData)
71 // yes, here oid == null is legal (by design),
72 // but no, it would not be legal for an oid string
76 public AsnEncodedData (AsnEncodedData asnEncodedData)
78 if (asnEncodedData == null)
79 throw new ArgumentNullException ("asnEncodedData");
81 Oid = new Oid (asnEncodedData._oid);
82 RawData = asnEncodedData._raw;
85 public AsnEncodedData (byte[] rawData)
98 _oid = new Oid (value);
102 public byte[] RawData {
106 throw new ArgumentNullException ("RawData");
107 _raw = (byte[])value.Clone ();
113 public virtual void CopyFrom (AsnEncodedData asnEncodedData)
115 if (asnEncodedData == null)
116 throw new ArgumentNullException ("asnEncodedData");
118 if (asnEncodedData._oid == null)
121 Oid = new Oid (asnEncodedData._oid);
123 RawData = asnEncodedData._raw;
126 public virtual string Format (bool multiLine)
132 return Default (multiLine);
134 return ToString (multiLine);
137 // internal decoding/formatting methods
139 internal virtual string ToString (bool multiLine)
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);
158 return Default (multiLine);
162 internal string Default (bool multiLine)
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)
170 return sb.ToString ();
173 // Indirectly (undocumented but) supported extensions
175 internal string BasicConstraintsExtension (bool multiLine)
178 X509BasicConstraintsExtension bc = new X509BasicConstraintsExtension (this, false);
179 return bc.ToString (multiLine);
186 internal string EnhancedKeyUsageExtension (bool multiLine)
189 X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (this, false);
190 return eku.ToString (multiLine);
197 internal string KeyUsageExtension (bool multiLine)
200 X509KeyUsageExtension ku = new X509KeyUsageExtension (this, false);
201 return ku.ToString (multiLine);
208 internal string SubjectKeyIdentifierExtension (bool multiLine)
211 X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (this, false);
212 return ski.ToString (multiLine);
219 // Indirectly (undocumented but) supported extensions
221 internal string SubjectAltName (bool multiLine)
224 return "Information Not Available";
227 ASN1 ex = new ASN1 (_raw);
228 StringBuilder sb = new StringBuilder ();
229 for (int i=0; i < ex.Count; i++) {
237 type = "RFC822 Name=";
238 name = Encoding.ASCII.GetString (el.Value);
242 name = Encoding.ASCII.GetString (el.Value);
245 type = String.Format ("Unknown ({0})=", el.Tag);
246 name = CryptoConvert.ToHex (el.Value);
253 sb.Append (Environment.NewLine);
254 } else if (i < ex.Count - 1) {
258 return sb.ToString ();
265 internal string NetscapeCertType (bool multiLine)
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];
273 StringBuilder sb = new StringBuilder ();
275 if ((value & 0x80) == 0x80) {
276 sb.Append ("SSL Client Authentication");
278 if ((value & 0x40) == 0x40) {
281 sb.Append ("SSL Server Authentication");
283 if ((value & 0x20) == 0x20) {
288 if ((value & 0x10) == 0x10) {
291 sb.Append ("Signature"); // a.k.a. Object Signing / Code Signing
293 if ((value & 0x08) == 0x08) {
296 sb.Append ("Unknown cert type");
298 if ((value & 0x04) == 0x04) {
301 sb.Append ("SSL CA"); // CA == Certificate Authority
303 if ((value & 0x02) == 0x02) {
306 sb.Append ("SMIME CA");
308 if ((value & 0x01) == 0x01) {
311 sb.Append ("Signature CA");
313 sb.AppendFormat (" ({0})", value.ToString ("x2"));
314 return sb.ToString ();