697a55e79c9af71f7fc233eee94e59b9e778ad96
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509 / X509Extension.cs
1 //
2 // X509Extension.cs: Base class for all X.509 extensions.
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 using System;
31 using System.Globalization;
32 using System.Text;
33
34 using Mono.Security;
35
36 namespace Mono.Security.X509 {
37         /*
38          * Extension  ::=  SEQUENCE  {
39          *      extnID      OBJECT IDENTIFIER,
40          *      critical    BOOLEAN DEFAULT FALSE,
41          *      extnValue   OCTET STRING  
42          * }
43          */
44 #if INSIDE_CORLIB
45         internal
46 #else
47         public 
48 #endif
49         class X509Extension {
50
51                 protected string extnOid;
52                 protected bool extnCritical;
53                 protected ASN1 extnValue;
54
55                 protected X509Extension () 
56                 {
57                         extnCritical = false;
58                 }
59
60                 public X509Extension (ASN1 asn1) 
61                 {
62                         if ((asn1.Tag != 0x30) || (asn1.Count < 2))
63                                 throw new ArgumentException (Locale.GetText ("Invalid X.509 extension."));
64                         if (asn1[0].Tag != 0x06)
65                                 throw new ArgumentException (Locale.GetText ("Invalid X.509 extension."));
66
67                         extnOid = ASN1Convert.ToOid (asn1[0]);
68                         extnCritical = ((asn1[1].Tag == 0x01) && (asn1[1].Value[0] == 0xFF));
69                         extnValue = asn1 [asn1.Count - 1]; // last element
70                         Decode ();
71                 }
72
73                 public X509Extension (X509Extension extension)
74                 {
75                         if (extension == null)
76                                 throw new ArgumentNullException ("extension");
77                         if ((extension.Value == null) || (extension.Value.Tag != 0x04) || (extension.Value.Count != 1))
78                                 throw new ArgumentException (Locale.GetText ("Invalid X.509 extension."));
79
80                         extnOid = extension.Oid;
81                         extnCritical = extension.Critical;
82                         extnValue = extension.Value;
83                         Decode ();
84                 }
85
86                 // encode the extension *into* an OCTET STRING
87                 protected virtual void Decode () 
88                 {
89                 }
90
91                 // decode the extension from *inside* an OCTET STRING
92                 protected virtual void Encode ()
93                 {
94                 }
95
96                 public ASN1 ASN1 {
97                         get {
98                                 ASN1 extension = new ASN1 (0x30);
99                                 extension.Add (ASN1Convert.FromOid (extnOid));
100                                 if (extnCritical)
101                                         extension.Add (new ASN1 (0x01, new byte [1] { 0xFF }));
102                                 Encode ();
103                                 extension.Add (extnValue);
104                                 return extension;
105                         }
106                 }
107
108                 public string Oid {
109                         get { return extnOid; }
110                 }
111
112                 public bool Critical {
113                         get { return extnCritical; }
114                         set { extnCritical = value; }
115                 }
116
117                 // this gets overrided with more meaningful names
118                 public virtual string Name {
119                         get { return extnOid; }
120                 }
121
122                 public ASN1 Value {
123                         get {
124                                 if (extnValue == null) {
125                                         Encode ();
126                                 }
127                                 return extnValue;
128                         }
129                 }
130
131                 public override bool Equals (object obj) 
132                 {
133                         if (obj == null)
134                                 return false;
135                         
136                         X509Extension ex = (obj as X509Extension);
137                         if (ex == null)
138                                 return false;
139
140                         if (extnCritical != ex.extnCritical)
141                                 return false;
142                         if (extnOid != ex.extnOid)
143                                 return false;
144                         if (extnValue.Length != ex.extnValue.Length)
145                                 return false;
146                         
147                         for (int i=0; i < extnValue.Length; i++) {
148                                 if (extnValue [i] != ex.extnValue [i])
149                                         return false;
150                         }
151                         return true;
152                 }
153
154                 public byte[] GetBytes () 
155                 {
156                         return ASN1.GetBytes ();
157                 }
158
159                 public override int GetHashCode () 
160                 {
161                         // OID should be unique in a collection of extensions
162                         return extnOid.GetHashCode ();
163                 }
164
165                 private void WriteLine (StringBuilder sb, int n, int pos) 
166                 {
167                         byte[] value = extnValue.Value;
168                         int p = pos;
169                         for (int j=0; j < 8; j++) {
170                                 if (j < n) {
171                                         sb.Append (value [p++].ToString ("X2", CultureInfo.InvariantCulture));
172                                         sb.Append (" ");
173                                 }
174                                 else
175                                         sb.Append ("   ");
176                         }
177                         sb.Append ("  ");
178                         p = pos;
179                         for (int j=0; j < n; j++) {
180                                 byte b = value [p++];
181                                 if (b < 0x20)
182                                         sb.Append (".");
183                                 else
184                                         sb.Append (Convert.ToChar (b));
185                         }
186                         sb.Append (Environment.NewLine);
187                 }
188
189                 public override string ToString () 
190                 {
191                         StringBuilder sb = new StringBuilder ();
192                         int div = (extnValue.Length >> 3);
193                         int rem = (extnValue.Length - (div << 3));
194                         int x = 0;
195                         for (int i=0; i < div; i++) {
196                                 WriteLine (sb, 8, x);
197                                 x += 8;
198                         }
199                         WriteLine (sb, rem, x);
200                         return sb.ToString ();
201                 }
202         }
203 }