Added tests for Task.WhenAll w/ empty list
[mono.git] / mcs / class / corlib / Mono.Security / ASN1.cs
1 //
2 // ASN1.cs: Abstract Syntax Notation 1 - micro-parser and generator
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //      Jesper Pedersen  <jep@itplus.dk>
7 //
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // (C) 2004 IT+ A/S (http://www.itplus.dk)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Collections;
34 using System.IO;
35 using System.Text;
36
37 namespace Mono.Security {
38
39         // References:
40         // a.   ITU ASN.1 standards (free download)
41         //      http://www.itu.int/ITU-T/studygroups/com17/languages/
42 #if INSIDE_CORLIB
43         internal
44 #else
45         public
46 #endif
47         class ASN1 {
48
49                 private byte m_nTag;
50                 private byte[] m_aValue;
51                 private ArrayList elist;
52
53                 public ASN1 () : this (0x00, null) {}
54
55                 public ASN1 (byte tag) : this (tag, null) {}
56
57                 public ASN1 (byte tag, byte[] data) 
58                 {
59                         m_nTag = tag;
60                         m_aValue = data;
61                 }
62
63                 public ASN1 (byte[] data) 
64                 {
65                         m_nTag = data [0];
66
67                         int nLenLength = 0;
68                         int nLength = data [1];
69
70                         if (nLength > 0x80) {
71                                 // composed length
72                                 nLenLength = nLength - 0x80;
73                                 nLength = 0;
74                                 for (int i = 0; i < nLenLength; i++) {
75                                         nLength *= 256;
76                                         nLength += data [i + 2];
77                                 }
78                         }
79                         else if (nLength == 0x80) {
80                                 // undefined length encoding
81                                 throw new NotSupportedException ("Undefined length encoding.");
82                         }
83
84                         m_aValue = new byte [nLength];
85                         Buffer.BlockCopy (data, (2 + nLenLength), m_aValue, 0, nLength);
86
87                         if ((m_nTag & 0x20) == 0x20) {
88                                 int nStart = (2 + nLenLength);
89                                 Decode (data, ref nStart, data.Length);
90                         }
91                 }
92
93                 public int Count {
94                         get { 
95                                 if (elist == null)
96                                         return 0;
97                                 return elist.Count; 
98                         }
99                 }
100
101                 public byte Tag {
102                         get { return m_nTag; }
103                 }
104
105                 public int Length {
106                         get { 
107                                 if (m_aValue != null)
108                                         return m_aValue.Length; 
109                                 else
110                                         return 0;
111                         }
112                 }
113
114                 public byte[] Value {
115                         get { 
116                                 if (m_aValue == null)
117                                         GetBytes ();
118                                 return (byte[]) m_aValue.Clone (); 
119                         }
120                         set { 
121                                 if (value != null)
122                                         m_aValue = (byte[]) value.Clone (); 
123                         }
124                 }
125
126                 private bool CompareArray (byte[] array1, byte[] array2)
127                 {
128                         bool bResult = (array1.Length == array2.Length);
129                         if (bResult) {
130                                 for (int i = 0; i < array1.Length; i++) {
131                                         if (array1[i] != array2[i])
132                                                 return false;
133                                 }
134                         }
135                         return bResult;
136                 }
137
138                 public bool Equals (byte[] asn1) 
139                 {
140                         return CompareArray (this.GetBytes (), asn1);
141                 }
142
143                 public bool CompareValue (byte[] value) 
144                 {
145                         return CompareArray (m_aValue, value);
146                 }
147
148                 public ASN1 Add (ASN1 asn1) 
149                 {
150                         if (asn1 != null) {
151                                 if (elist == null)
152                                         elist = new ArrayList ();
153                                 elist.Add (asn1);
154                         }
155                         return asn1;
156                 }
157
158                 public virtual byte[] GetBytes () 
159                 {
160                         byte[] val = null;
161                         
162                         if (Count > 0) {
163                                 int esize = 0;
164                                 ArrayList al = new ArrayList ();
165                                 foreach (ASN1 a in elist) {
166                                         byte[] item = a.GetBytes ();
167                                         al.Add (item);
168                                         esize += item.Length;
169                                 }
170                                 val = new byte [esize];
171                                 int pos = 0;
172                                 for (int i=0; i < elist.Count; i++) {
173                                         byte[] item = (byte[]) al[i];
174                                         Buffer.BlockCopy (item, 0, val, pos, item.Length);
175                                         pos += item.Length;
176                                 }
177                         } else if (m_aValue != null) {
178                                 val = m_aValue;
179                         }
180
181                         byte[] der;
182                         int nLengthLen = 0;
183
184                         if (val != null) {
185                                 int nLength = val.Length;
186                                 // special for length > 127
187                                 if (nLength > 127) {
188                                         if (nLength <= Byte.MaxValue) {
189                                                 der = new byte [3 + nLength];
190                                                 Buffer.BlockCopy (val, 0, der, 3, nLength);
191                                                 nLengthLen = 0x81;
192                                                 der[2] = (byte)(nLength);
193                                         }
194                                         else if (nLength <= UInt16.MaxValue) {
195                                                 der = new byte [4 + nLength];
196                                                 Buffer.BlockCopy (val, 0, der, 4, nLength);
197                                                 nLengthLen = 0x82;
198                                                 der[2] = (byte)(nLength >> 8);
199                                                 der[3] = (byte)(nLength);
200                                         }
201                                         else if (nLength <= 0xFFFFFF) {
202                                                 // 24 bits
203                                                 der = new byte [5 + nLength];
204                                                 Buffer.BlockCopy (val, 0, der, 5, nLength);
205                                                 nLengthLen = 0x83;
206                                                 der [2] = (byte)(nLength >> 16);
207                                                 der [3] = (byte)(nLength >> 8);
208                                                 der [4] = (byte)(nLength);
209                                         }
210                                         else {
211                                                 // max (Length is an integer) 32 bits
212                                                 der = new byte [6 + nLength];
213                                                 Buffer.BlockCopy (val, 0, der, 6, nLength);
214                                                 nLengthLen = 0x84;
215                                                 der [2] = (byte)(nLength >> 24);
216                                                 der [3] = (byte)(nLength >> 16);
217                                                 der [4] = (byte)(nLength >> 8);
218                                                 der [5] = (byte)(nLength);
219                                         }
220                                 }
221                                 else {
222                                         // basic case (no encoding)
223                                         der = new byte [2 + nLength];
224                                         Buffer.BlockCopy (val, 0, der, 2, nLength);
225                                         nLengthLen = nLength;
226                                 }
227                                 if (m_aValue == null)
228                                         m_aValue = val;
229                         }
230                         else
231                                 der = new byte[2];
232
233                         der[0] = m_nTag;
234                         der[1] = (byte)nLengthLen;
235
236                         return der;
237                 }
238
239                 // Note: Recursive
240                 protected void Decode (byte[] asn1, ref int anPos, int anLength) 
241                 {
242                         byte nTag;
243                         int nLength;
244                         byte[] aValue;
245
246                         // minimum is 2 bytes (tag + length of 0)
247                         while (anPos < anLength - 1) {
248                                 DecodeTLV (asn1, ref anPos, out nTag, out nLength, out aValue);
249                                 // sometimes we get trailing 0
250                                 if (nTag == 0)
251                                         continue;
252
253                                 ASN1 elm = Add (new ASN1 (nTag, aValue));
254
255                                 if ((nTag & 0x20) == 0x20) {
256                                         int nConstructedPos = anPos;
257                                         elm.Decode (asn1, ref nConstructedPos, nConstructedPos + nLength);
258                                 }
259                                 anPos += nLength; // value length
260                         }
261                 }
262
263                 // TLV : Tag - Length - Value
264                 protected void DecodeTLV (byte[] asn1, ref int pos, out byte tag, out int length, out byte[] content) 
265                 {
266                         tag = asn1 [pos++];
267                         length = asn1 [pos++];
268
269                         // special case where L contains the Length of the Length + 0x80
270                         if ((length & 0x80) == 0x80) {
271                                 int nLengthLen = length & 0x7F;
272                                 length = 0;
273                                 for (int i = 0; i < nLengthLen; i++)
274                                         length = length * 256 + asn1 [pos++];
275                         }
276
277                         content = new byte [length];
278                         Buffer.BlockCopy (asn1, pos, content, 0, length);
279                 }
280
281                 public ASN1 this [int index] {
282                         get {           
283                                 try {
284                                         if ((elist == null) || (index >= elist.Count))
285                                                 return null;
286                                         return (ASN1) elist [index];
287                                 }
288                                 catch (ArgumentOutOfRangeException) {
289                                         return null;
290                                 }
291                         }
292                 }
293
294                 public ASN1 Element (int index, byte anTag) 
295                 {
296                         try {
297                                 if ((elist == null) || (index >= elist.Count))
298                                         return null;
299
300                                 ASN1 elm = (ASN1) elist [index];
301                                 if (elm.Tag == anTag)
302                                         return elm;
303                                 else
304                                         return null;
305                         }
306                         catch (ArgumentOutOfRangeException) {
307                                 return null;
308                         }
309                 }
310
311                 public override string ToString()
312                 {
313                         StringBuilder hexLine = new StringBuilder ();
314             
315                         // Add tag
316                         hexLine.AppendFormat ("Tag: {0} {1}", m_nTag.ToString ("X2"), Environment.NewLine);
317
318                         // Add length
319                         hexLine.AppendFormat ("Length: {0} {1}", Value.Length, Environment.NewLine);
320
321                         // Add value
322                         hexLine.Append ("Value: ");
323                         hexLine.Append (Environment.NewLine);
324                         for (int i = 0; i < Value.Length; i++) {
325                                 hexLine.AppendFormat ("{0} ", Value [i].ToString ("X2"));
326                                 if ((i+1) % 16 == 0)
327                                         hexLine.AppendFormat (Environment.NewLine);
328                         }
329                         return hexLine.ToString ();
330                 }
331
332                 public void SaveToFile (string filename)
333                 {
334                         if (filename == null)
335                                 throw new ArgumentNullException ("filename");
336
337                         using (FileStream fs = File.Create (filename)) {
338                                 byte[] data = GetBytes ();
339                                 fs.Write (data, 0, data.Length);
340                         }
341                 }
342         }
343 }