* WebService.cs: Added SoapVersion property.
[mono.git] / mcs / class / corlib / 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 // (C) 2004 Novell (http://www.novell.com)
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Globalization;
36 using System.Text;
37
38 using Mono.Security;
39
40 namespace Mono.Security.X509 {
41         /*
42          * Extension  ::=  SEQUENCE  {
43          *      extnID      OBJECT IDENTIFIER,
44          *      critical    BOOLEAN DEFAULT FALSE,
45          *      extnValue   OCTET STRING  
46          * }
47          */
48 #if INSIDE_CORLIB
49         internal
50 #else
51         public 
52 #endif
53         class X509Extension {
54
55                 protected string extnOid;
56                 protected bool extnCritical;
57                 protected ASN1 extnValue;
58
59                 internal X509Extension () 
60                 {
61                         extnCritical = false;
62                 }
63
64                 public X509Extension (ASN1 asn1) 
65                 {
66                         if ((asn1.Tag != 0x30) || (asn1.Count < 2))
67                                 throw new ArgumentException ("Invalid X.509 extension");
68                         if (asn1[0].Tag != 0x06)
69                                 throw new ArgumentException ("Invalid X.509 extension");
70                         extnOid = ASN1Convert.ToOid (asn1 [0]);
71                         extnCritical = ((asn1[1].Tag == 0x01) && (asn1[1].Value[0] == 0xFF));
72                         extnValue = asn1 [asn1.Count - 1]; // last element
73                         Decode ();
74                 }
75
76                 public X509Extension (X509Extension extension) : this () 
77                 {
78                         if (extension == null)
79                                 throw new ArgumentNullException ("extension");
80                         if ((extension.Value.Tag != 0x04) || (extension.Value.Count != 0))
81                                 throw new ArgumentException ("Invalid extension");
82                         extnOid = extension.Oid;
83                         extnCritical = extension.Critical;
84                         extnValue = extension.Value;
85                         Decode ();
86                 }
87
88                 protected virtual void Decode () 
89                 {
90                 }
91
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] { 0x01 }));
102                                 ASN1 os = extension.Add (new ASN1 (0x04));
103                                 Encode ();
104                                 os.Add (extnValue);
105                                 return extension;
106                         }
107                 }
108
109                 public string Oid {
110                         get { return extnOid; }
111                 }
112
113                 public bool Critical {
114                         get { return extnCritical; }
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 { return extnValue; }
124                 }
125
126                 public override bool Equals (object obj) 
127                 {
128                         if (obj == null)
129                                 return false;
130                         
131                         X509Extension ex = (obj as X509Extension);
132                         if (ex == null)
133                                 return false;
134
135                         if (extnCritical != ex.extnCritical)
136                                 return false;
137                         if (extnOid != ex.extnOid)
138                                 return false;
139                         if (extnValue.Length != ex.extnValue.Length)
140                                 return false;
141                         
142                         for (int i=0; i < extnValue.Length; i++) {
143                                 if (extnValue [i] != ex.extnValue [i])
144                                         return false;
145                         }
146                         return true;
147                 }
148
149                 public byte[] GetBytes () 
150                 {
151                         return ASN1.GetBytes ();
152                 }
153
154                 public override int GetHashCode () 
155                 {
156                         // OID should be unique in a collection of extensions
157                         return extnOid.GetHashCode ();
158                 }
159
160                 private void WriteLine (StringBuilder sb, int n, int pos) 
161                 {
162                         byte[] value = extnValue.Value;
163                         int p = pos;
164                         StringBuilder preview = new StringBuilder ();
165                         for (int j=0; j < 8; j++) {
166                                 if (j < n) {
167                                         sb.Append (value [p++].ToString ("X2", CultureInfo.InvariantCulture));
168                                         sb.Append (" ");
169                                 }
170                                 else
171                                         sb.Append ("   ");
172                         }
173                         sb.Append ("  ");
174                         p = pos;
175                         for (int j=0; j < n; j++) {
176                                 byte b = value [p++];
177                                 if (b < 0x20)
178                                         sb.Append (".");
179                                 else
180                                         sb.Append (Convert.ToChar (b));
181                         }
182                         sb.Append (Environment.NewLine);
183                 }
184
185                 public override string ToString () 
186                 {
187                         StringBuilder sb = new StringBuilder ();
188                         int div = (extnValue.Length >> 3);
189                         int rem = (extnValue.Length - (div << 3));
190                         int x = 0;
191                         for (int i=0; i < div; i++) {
192                                 WriteLine (sb, 8, x);
193                                 x += 8;
194                         }
195                         WriteLine (sb, rem, x);
196                         return sb.ToString ();
197                 }
198         }
199 }