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 #if MONO_SECURITY_ALIAS
33 extern alias MonoSecurity;
34 using MonoSecurity::Mono.Security;
35 using MonoSecurity::Mono.Security.Cryptography;
38 using Mono.Security.Cryptography;
41 using System.Security.Cryptography.X509Certificates;
44 namespace System.Security.Cryptography {
46 internal enum AsnDecodeStatus {
52 InformationNotAvailable = 4
55 public class AsnEncodedData {
62 protected AsnEncodedData ()
66 public AsnEncodedData (string oid, byte[] rawData)
72 public AsnEncodedData (Oid oid, byte[] rawData)
77 // yes, here oid == null is legal (by design),
78 // but no, it would not be legal for an oid string
82 public AsnEncodedData (AsnEncodedData asnEncodedData)
84 if (asnEncodedData == null)
85 throw new ArgumentNullException ("asnEncodedData");
87 if (asnEncodedData._oid != null)
88 Oid = new Oid (asnEncodedData._oid);
89 RawData = asnEncodedData._raw;
92 public AsnEncodedData (byte[] rawData)
105 _oid = new Oid (value);
109 public byte[] RawData {
113 throw new ArgumentNullException ("RawData");
114 _raw = (byte[])value.Clone ();
120 public virtual void CopyFrom (AsnEncodedData asnEncodedData)
122 if (asnEncodedData == null)
123 throw new ArgumentNullException ("asnEncodedData");
125 if (asnEncodedData._oid == null)
128 Oid = new Oid (asnEncodedData._oid);
130 RawData = asnEncodedData._raw;
133 public virtual string Format (bool multiLine)
139 return Default (multiLine);
141 return ToString (multiLine);
144 // internal decoding/formatting methods
146 internal virtual string ToString (bool multiLine)
148 switch (_oid.Value) {
149 // fx supported objects
150 case X509BasicConstraintsExtension.oid:
151 return BasicConstraintsExtension (multiLine);
152 case X509EnhancedKeyUsageExtension.oid:
153 return EnhancedKeyUsageExtension (multiLine);
154 case X509KeyUsageExtension.oid:
155 return KeyUsageExtension (multiLine);
156 case X509SubjectKeyIdentifierExtension.oid:
157 return SubjectKeyIdentifierExtension (multiLine);
158 // other known objects (i.e. supported structure) -
159 // but without any corresponding framework class
160 case Oid.oidSubjectAltName:
161 return SubjectAltName (multiLine);
162 case Oid.oidNetscapeCertType:
163 return NetscapeCertType (multiLine);
165 return Default (multiLine);
169 internal string Default (bool multiLine)
171 StringBuilder sb = new StringBuilder ();
172 for (int i=0; i < _raw.Length; i++) {
173 sb.Append (_raw [i].ToString ("x2"));
174 if (i != _raw.Length - 1)
177 return sb.ToString ();
180 // Indirectly (undocumented but) supported extensions
182 internal string BasicConstraintsExtension (bool multiLine)
185 X509BasicConstraintsExtension bc = new X509BasicConstraintsExtension (this, false);
186 return bc.ToString (multiLine);
193 internal string EnhancedKeyUsageExtension (bool multiLine)
196 X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (this, false);
197 return eku.ToString (multiLine);
204 internal string KeyUsageExtension (bool multiLine)
207 X509KeyUsageExtension ku = new X509KeyUsageExtension (this, false);
208 return ku.ToString (multiLine);
215 internal string SubjectKeyIdentifierExtension (bool multiLine)
218 X509SubjectKeyIdentifierExtension ski = new X509SubjectKeyIdentifierExtension (this, false);
219 return ski.ToString (multiLine);
226 // Indirectly (undocumented but) supported extensions
228 internal string SubjectAltName (bool multiLine)
231 return "Information Not Available";
234 ASN1 ex = new ASN1 (_raw);
235 StringBuilder sb = new StringBuilder ();
236 for (int i=0; i < ex.Count; i++) {
244 type = "RFC822 Name=";
245 name = Encoding.ASCII.GetString (el.Value);
249 name = Encoding.ASCII.GetString (el.Value);
252 type = String.Format ("Unknown ({0})=", el.Tag);
253 name = CryptoConvert.ToHex (el.Value);
260 sb.Append (Environment.NewLine);
261 } else if (i < ex.Count - 1) {
265 return sb.ToString ();
272 internal string NetscapeCertType (bool multiLine)
274 // 4 byte long, BITSTRING (0x03), Value length of 2
275 if ((_raw.Length < 4) || (_raw [0] != 0x03) || (_raw [1] != 0x02))
276 return "Information Not Available";
277 // first value byte is the number of unused bits
278 int value = (_raw [3] >> _raw [2]) << _raw [2];
280 StringBuilder sb = new StringBuilder ();
282 if ((value & 0x80) == 0x80) {
283 sb.Append ("SSL Client Authentication");
285 if ((value & 0x40) == 0x40) {
288 sb.Append ("SSL Server Authentication");
290 if ((value & 0x20) == 0x20) {
295 if ((value & 0x10) == 0x10) {
298 sb.Append ("Signature"); // a.k.a. Object Signing / Code Signing
300 if ((value & 0x08) == 0x08) {
303 sb.Append ("Unknown cert type");
305 if ((value & 0x04) == 0x04) {
308 sb.Append ("SSL CA"); // CA == Certificate Authority
310 if ((value & 0x02) == 0x02) {
313 sb.Append ("SMIME CA");
315 if ((value & 0x01) == 0x01) {
318 sb.Append ("Signature CA");
320 sb.AppendFormat (" ({0})", value.ToString ("x2"));
321 return sb.ToString ();