Asn1Set.cs
10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
using System;
using System.Collections;
using System.IO;
#if PORTABLE
using System.Collections.Generic;
using System.Linq;
#endif
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.Collections;
namespace Org.BouncyCastle.Asn1
{
abstract public class Asn1Set
: Asn1Object, IEnumerable
{
private readonly IList _set;
/**
* return an ASN1Set from the given object.
*
* @param obj the object we want converted.
* @exception ArgumentException if the object cannot be converted.
*/
public static Asn1Set GetInstance(
object obj)
{
if (obj == null || obj is Asn1Set)
{
return (Asn1Set)obj;
}
else if (obj is Asn1SetParser)
{
return Asn1Set.GetInstance(((Asn1SetParser)obj).ToAsn1Object());
}
else if (obj is byte[])
{
try
{
return Asn1Set.GetInstance(FromByteArray((byte[])obj));
}
catch (IOException e)
{
throw new ArgumentException("failed to construct set from byte[]: " + e.Message);
}
}
else if (obj is Asn1Encodable)
{
Asn1Object primitive = ((Asn1Encodable)obj).ToAsn1Object();
if (primitive is Asn1Set)
{
return (Asn1Set)primitive;
}
}
throw new ArgumentException("Unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj");
}
/**
* Return an ASN1 set from a tagged object. There is a special
* case here, if an object appears to have been explicitly tagged on
* reading but we were expecting it to be implicitly tagged in the
* normal course of events it indicates that we lost the surrounding
* set - so we need to add it back (this will happen if the tagged
* object is a sequence that contains other sequences). If you are
* dealing with implicitly tagged sets you really <b>should</b>
* be using this method.
*
* @param obj the tagged object.
* @param explicitly true if the object is meant to be explicitly tagged
* false otherwise.
* @exception ArgumentException if the tagged object cannot
* be converted.
*/
public static Asn1Set GetInstance(
Asn1TaggedObject obj,
bool explicitly)
{
Asn1Object inner = obj.GetObject();
if (explicitly)
{
if (!obj.IsExplicit())
throw new ArgumentException("object implicit - explicit expected.");
return (Asn1Set) inner;
}
//
// constructed object which appears to be explicitly tagged
// and it's really implicit means we have to add the
// surrounding sequence.
//
if (obj.IsExplicit())
{
return new DerSet(inner);
}
if (inner is Asn1Set)
{
return (Asn1Set) inner;
}
//
// in this case the parser returns a sequence, convert it
// into a set.
//
if (inner is Asn1Sequence)
{
Asn1EncodableVector v = new Asn1EncodableVector();
Asn1Sequence s = (Asn1Sequence) inner;
foreach (Asn1Encodable ae in s)
{
v.Add(ae);
}
// TODO Should be able to construct set directly from sequence?
return new DerSet(v, false);
}
throw new ArgumentException("Unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj");
}
protected internal Asn1Set(
int capacity)
{
_set = Platform.CreateArrayList(capacity);
}
public virtual IEnumerator GetEnumerator()
{
return _set.GetEnumerator();
}
[Obsolete("Use GetEnumerator() instead")]
public IEnumerator GetObjects()
{
return GetEnumerator();
}
/**
* return the object at the set position indicated by index.
*
* @param index the set number (starting at zero) of the object
* @return the object at the set position indicated by index.
*/
public virtual Asn1Encodable this[int index]
{
get { return (Asn1Encodable) _set[index]; }
}
[Obsolete("Use 'object[index]' syntax instead")]
public Asn1Encodable GetObjectAt(
int index)
{
return this[index];
}
[Obsolete("Use 'Count' property instead")]
public int Size
{
get { return Count; }
}
public virtual int Count
{
get { return _set.Count; }
}
public virtual Asn1Encodable[] ToArray()
{
Asn1Encodable[] values = new Asn1Encodable[this.Count];
for (int i = 0; i < this.Count; ++i)
{
values[i] = this[i];
}
return values;
}
private class Asn1SetParserImpl
: Asn1SetParser
{
private readonly Asn1Set outer;
private readonly int max;
private int index;
public Asn1SetParserImpl(
Asn1Set outer)
{
this.outer = outer;
this.max = outer.Count;
}
public IAsn1Convertible ReadObject()
{
if (index == max)
return null;
Asn1Encodable obj = outer[index++];
if (obj is Asn1Sequence)
return ((Asn1Sequence)obj).Parser;
if (obj is Asn1Set)
return ((Asn1Set)obj).Parser;
// NB: Asn1OctetString implements Asn1OctetStringParser directly
// if (obj is Asn1OctetString)
// return ((Asn1OctetString)obj).Parser;
return obj;
}
public virtual Asn1Object ToAsn1Object()
{
return outer;
}
}
public Asn1SetParser Parser
{
get { return new Asn1SetParserImpl(this); }
}
protected override int Asn1GetHashCode()
{
int hc = Count;
foreach (object o in this)
{
hc *= 17;
if (o == null)
{
hc ^= DerNull.Instance.GetHashCode();
}
else
{
hc ^= o.GetHashCode();
}
}
return hc;
}
protected override bool Asn1Equals(
Asn1Object asn1Object)
{
Asn1Set other = asn1Object as Asn1Set;
if (other == null)
return false;
if (Count != other.Count)
{
return false;
}
IEnumerator s1 = GetEnumerator();
IEnumerator s2 = other.GetEnumerator();
while (s1.MoveNext() && s2.MoveNext())
{
Asn1Object o1 = GetCurrent(s1).ToAsn1Object();
Asn1Object o2 = GetCurrent(s2).ToAsn1Object();
if (!o1.Equals(o2))
return false;
}
return true;
}
private Asn1Encodable GetCurrent(IEnumerator e)
{
Asn1Encodable encObj = (Asn1Encodable)e.Current;
// unfortunately null was allowed as a substitute for DER null
if (encObj == null)
return DerNull.Instance;
return encObj;
}
protected internal void Sort()
{
if (_set.Count < 2)
return;
#if PORTABLE
var sorted = _set.Cast<Asn1Encodable>()
.Select(a => new { Item = a, Key = a.GetEncoded(Asn1Encodable.Der) })
.OrderBy(t => t.Key, new DerComparer())
.Select(t => t.Item)
.ToList();
for (int i = 0; i < _set.Count; ++i)
{
_set[i] = sorted[i];
}
#else
Asn1Encodable[] items = new Asn1Encodable[_set.Count];
byte[][] keys = new byte[_set.Count][];
for (int i = 0; i < _set.Count; ++i)
{
Asn1Encodable item = (Asn1Encodable)_set[i];
items[i] = item;
keys[i] = item.GetEncoded(Asn1Encodable.Der);
}
Array.Sort(keys, items, new DerComparer());
for (int i = 0; i < _set.Count; ++i)
{
_set[i] = items[i];
}
#endif
}
protected internal void AddObject(Asn1Encodable obj)
{
_set.Add(obj);
}
public override string ToString()
{
return CollectionUtilities.ToString(_set);
}
#if PORTABLE
private class DerComparer
: IComparer<byte[]>
{
public int Compare(byte[] x, byte[] y)
{
byte[] a = x, b = y;
#else
private class DerComparer
: IComparer
{
public int Compare(object x, object y)
{
byte[] a = (byte[])x, b = (byte[])y;
#endif
int len = System.Math.Min(a.Length, b.Length);
for (int i = 0; i != len; ++i)
{
byte ai = a[i], bi = b[i];
if (ai != bi)
return ai < bi ? -1 : 1;
}
if (a.Length > b.Length)
return AllZeroesFrom(a, len) ? 0 : 1;
if (a.Length < b.Length)
return AllZeroesFrom(b, len) ? 0 : -1;
return 0;
}
private bool AllZeroesFrom(byte[] bs, int pos)
{
while (pos < bs.Length)
{
if (bs[pos++] != 0)
return false;
}
return true;
}
}
}
}