2004-09-17 Sebastien Pouliot <sebastien@ximian.com>
[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 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                 internal 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 ("Invalid X.509 extension");
64                         if (asn1[0].Tag != 0x06)
65                                 throw new ArgumentException ("Invalid X.509 extension");
66                         extnOid = ASN1Convert.ToOid (asn1 [0]);
67                         extnCritical = ((asn1[1].Tag == 0x01) && (asn1[1].Value[0] == 0xFF));
68                         extnValue = asn1 [asn1.Count - 1]; // last element
69                         Decode ();
70                 }
71
72                 public X509Extension (X509Extension extension) : this () 
73                 {
74                         if (extension == null)
75                                 throw new ArgumentNullException ("extension");
76                         if ((extension.Value.Tag != 0x04) || (extension.Value.Count != 0))
77                                 throw new ArgumentException ("Invalid extension");
78                         extnOid = extension.Oid;
79                         extnCritical = extension.Critical;
80                         extnValue = extension.Value;
81                         Decode ();
82                 }
83
84                 protected virtual void Decode () 
85                 {
86                 }
87
88                 protected virtual void Encode ()
89                 {
90                 }
91
92                 public ASN1 ASN1 {
93                         get {
94                                 ASN1 extension = new ASN1 (0x30);
95                                 extension.Add (ASN1Convert.FromOid (extnOid));
96                                 if (extnCritical)
97                                         extension.Add (new ASN1 (0x01, new byte [1] { 0x01 }));
98                                 ASN1 os = extension.Add (new ASN1 (0x04));
99                                 Encode ();
100                                 os.Add (extnValue);
101                                 return extension;
102                         }
103                 }
104
105                 public string Oid {
106                         get { return extnOid; }
107                 }
108
109                 public bool Critical {
110                         get { return extnCritical; }
111                 }
112
113                 // this gets overrided with more meaningful names
114                 public virtual string Name {
115                         get { return extnOid; }
116                 }
117
118                 public ASN1 Value {
119                         get { return extnValue; }
120                 }
121
122                 public override bool Equals (object obj) 
123                 {
124                         if (obj == null)
125                                 return false;
126                         
127                         X509Extension ex = (obj as X509Extension);
128                         if (ex == null)
129                                 return false;
130
131                         if (extnCritical != ex.extnCritical)
132                                 return false;
133                         if (extnOid != ex.extnOid)
134                                 return false;
135                         if (extnValue.Length != ex.extnValue.Length)
136                                 return false;
137                         
138                         for (int i=0; i < extnValue.Length; i++) {
139                                 if (extnValue [i] != ex.extnValue [i])
140                                         return false;
141                         }
142                         return true;
143                 }
144
145                 public byte[] GetBytes () 
146                 {
147                         return ASN1.GetBytes ();
148                 }
149
150                 public override int GetHashCode () 
151                 {
152                         // OID should be unique in a collection of extensions
153                         return extnOid.GetHashCode ();
154                 }
155
156                 private void WriteLine (StringBuilder sb, int n, int pos) 
157                 {
158                         byte[] value = extnValue.Value;
159                         int p = pos;
160                         for (int j=0; j < 8; j++) {
161                                 if (j < n) {
162                                         sb.Append (value [p++].ToString ("X2", CultureInfo.InvariantCulture));
163                                         sb.Append (" ");
164                                 }
165                                 else
166                                         sb.Append ("   ");
167                         }
168                         sb.Append ("  ");
169                         p = pos;
170                         for (int j=0; j < n; j++) {
171                                 byte b = value [p++];
172                                 if (b < 0x20)
173                                         sb.Append (".");
174                                 else
175                                         sb.Append (Convert.ToChar (b));
176                         }
177                         sb.Append (Environment.NewLine);
178                 }
179
180                 public override string ToString () 
181                 {
182                         StringBuilder sb = new StringBuilder ();
183                         int div = (extnValue.Length >> 3);
184                         int rem = (extnValue.Length - (div << 3));
185                         int x = 0;
186                         for (int i=0; i < div; i++) {
187                                 WriteLine (sb, 8, x);
188                                 x += 8;
189                         }
190                         WriteLine (sb, rem, x);
191                         return sb.ToString ();
192                 }
193         }
194 }