-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDEREncoder.java
More file actions
415 lines (348 loc) · 13.7 KB
/
DEREncoder.java
File metadata and controls
415 lines (348 loc) · 13.7 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package der.tools;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
/**
* Class that produces a DER encoded byte array based on a passed in encoding.
*/
public class DEREncoder
{
/**
* Flags are designed to be '|'ed together
*/
public static final int NO_ENFORCEMENT = 0; // return bytes as is
public static final int LENGTHS = 1; // enforce length encoding limitations
public static final int SORT_SETS = 2; // sort sets correctly
private final int enforcementFlags;
public DEREncoder(int enforcementFlags)
{
this.enforcementFlags = enforcementFlags;
}
public byte[] toDER(byte[] input)
throws IOException
{
ByteArrayInputStream bIn = new ByteArrayInputStream(input);
if (enforcementFlags == NO_ENFORCEMENT)
{
return input;
}
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
process(bOut, input, 0);
return bOut.toByteArray();
}
private int process(OutputStream out, byte[] data, int offset)
throws IOException
{
ByteArrayInputStream bIn = new ByteArrayInputStream(data, offset, data.length - offset);
int baseTag = bIn.read();
int flags = baseTag & 0xe0;
int t = readTagNumber(bIn, baseTag & 0x1f);
int l = readLength(bIn, data.length - offset);
byte[] v = new byte[l];
bIn.read(v); // this will always read the full amount or fail as backed by byte array.
int totalRead = data.length - offset - bIn.available();
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
if ((flags & BERTags.CONSTRUCTED) != 0)
{
int index = 0;
switch (t)
{
case BERTags.SEQUENCE:
while (index < v.length)
{
index += process(bOut, v, index);
}
break;
case BERTags.SET:
List<byte[]> elements = new ArrayList<byte[]>();
ByteArrayOutputStream eltOut = new ByteArrayOutputStream();
while (index < v.length)
{
index += process(eltOut, v, index);
elements.add(eltOut.toByteArray());
eltOut.reset();
}
if ((enforcementFlags & SORT_SETS) != 0)
{
sort(elements);
}
for (byte[] encoding : elements)
{
bOut.write(encoding);
}
break;
default:
if ((flags & BERTags.TAGGED) != 0)
{
while (index < v.length)
{
index += process(bOut, v, index);
}
}
else
{
throw new IllegalArgumentException("found constructed primitive");
}
}
}
else
{
bOut.write(v);
}
byte[] derResult = bOut.toByteArray();
writeTag(out, flags, t);
writeLength(out, derResult.length);
out.write(derResult);
return totalRead;
}
static int readTagNumber(InputStream s, int baseTag)
throws IOException
{
int tag = baseTag;
int tagNo = tag & 0x1f;
//
// with tagged object tag number is bottom 5 bits, or stored at the start of the content
//
if (tagNo == 0x1f)
{
int b = s.read();
if (b < 31)
{
if (b < 0)
{
throw new EOFException("EOF found inside tag value.");
}
throw new IOException("corrupted stream - high tag number < 31 found");
}
tagNo = b & 0x7f;
// X.690-0207 8.1.2.4.2
// "c) bits 7 to 1 of the first subsequent octet shall not all be zero."
if (0 == tagNo)
{
throw new IOException("corrupted stream - invalid high tag number found");
}
while ((b & 0x80) != 0)
{
if ((tagNo >>> 24) != 0)
{
throw new IOException("Tag number more than 31 bits");
}
tagNo <<= 7;
b = s.read();
if (b < 0)
{
throw new EOFException("EOF found inside tag value.");
}
tagNo |= (b & 0x7f);
}
}
return tagNo;
}
private static int readLength(InputStream s, int limit)
throws IOException
{
int length = s.read();
if (0 == (length >>> 7))
{
// definite-length short form
return length;
}
if (0x80 == length)
{
// indefinite-length
return -1;
}
if (length < 0)
{
throw new EOFException("EOF found when length expected");
}
if (0xFF == length)
{
throw new IOException("invalid long form definite-length 0xFF");
}
int octetsCount = length & 0x7F, octetsPos = 0;
length = 0;
do
{
int octet = s.read();
if (octet < 0)
{
throw new EOFException("EOF found reading length");
}
if ((length >>> 23) != 0)
{
throw new IOException("long form definite-length more than 31 bits");
}
length = (length << 8) + octet;
}
while (++octetsPos < octetsCount);
if (length >= limit) // after all we must have read at least 1 byte
{
throw new IOException("corrupted stream - out of bounds length found: " + length + " >= " + limit);
}
return length;
}
final void writeTag(OutputStream out, int flags, int tag)
throws IOException
{
if (tag < 31)
{
out.write(flags | tag);
}
else
{
byte[] stack = new byte[6];
int pos = stack.length;
stack[--pos] = (byte)(tag & 0x7F);
while (tag > 127)
{
tag >>>= 7;
stack[--pos] = (byte)(tag & 0x7F | 0x80);
}
stack[--pos] = (byte)(flags | 0x1F);
out.write(stack, pos, stack.length - pos);
}
}
final void writeLength(OutputStream out, int length)
throws IOException
{
if (length < 128)
{
out.write(length);
}
else
{
byte[] stack = new byte[5];
int pos = stack.length;
do
{
stack[--pos] = (byte)length;
length >>>= 8;
}
while (length != 0);
int count = stack.length - pos;
stack[--pos] = (byte)(0x80 | count);
out.write(stack, pos, count + 1);
}
}
private static void sort(List<byte[]> t)
{
int count = t.size();
if (count < 2)
{
return;
}
byte[] bh = t.get(0), bi = t.get(1);
if (lessThanOrEqual(bi, bh))
{
byte[] bt = bi;
bi = bh;
bh = bt;
}
for (int i = 2; i < count; ++i)
{
byte[] b2 = t.get(i);
if (lessThanOrEqual(bi, b2))
{
t.set(i - 2, bh);
bh = bi;
bi = b2;
continue;
}
if (lessThanOrEqual(bh, b2))
{
t.set(i - 2, bh);
bh = b2;
continue;
}
int j = i - 1;
while (--j > 0)
{
byte[] b1 = t.get(j - 1);
if (lessThanOrEqual(b1, b2))
{
break;
}
t.set(j, b1);
}
t.set(j, b2);
}
t.set(count - 2, bh);
t.set(count - 1, bi);
}
private static boolean lessThanOrEqual(byte[] a, byte[] b)
{
/*
* NOTE: Set elements in DER encodings are ordered first according to their tags (class and
* number); the CONSTRUCTED bit is not part of the tag.
*
* For SET-OF, this is unimportant. All elements have the same tag and DER requires them to
* either all be in constructed form or all in primitive form, according to that tag. The
* elements are effectively ordered according to their content octets.
*
* For SET, the elements will have distinct tags, and each will be in constructed or
* primitive form accordingly. Failing to ignore the CONSTRUCTED bit could therefore lead to
* ordering inversions.
*/
int a0 = a[0] & (~BERTags.CONSTRUCTED & 0xff);
int b0 = b[0] & (~BERTags.CONSTRUCTED & 0xff);
if (a0 != b0)
{
return a0 < b0;
}
int last = Math.min(a.length, b.length) - 1;
for (int i = 1; i < last; ++i)
{
if (a[i] != b[i])
{
return (a[i] & 0xFF) < (b[i] & 0xFF);
}
}
return (a[last] & 0xFF) <= (b[last] & 0xFF);
}
private static byte[] baseCert = Base64.getDecoder().decode(
"MIIDbDCCAtWgAwIBAgIBADANBgkqh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7zEc9aLnX5lH65jVJ6ZVUxvownx8jYU=");
private static byte[] cert_multiv_dn_der = Base64.getDecoder().decode(
"MIICjzCCAjmgAwIBAgIBATANBgkqhkiG9w0BAQQFADCBrTELMAkGA1UEBhMCQVUxKDAmBgNVBAoMH1RoZSBMZWdpb24gb2YgdGhlIEJvdW5jeSBDYXN0bGUxEjAQBgNVBAcMCU1lbGJvdXJuZTERMA8GA1UECAwIVmljdG9yaWExTTAcBgNVBAUTFUNWUjoxMjM0NTY3OC1VSUQ6MTExMTAtBgkqhkiG9w0BCQEWIGZlZWRiYWNrLWNyeXB0b0Bib3VuY3ljYXN0bGUub3JnMB4XDTIzMDcyMDA3MzA1NFoXDTIzMDcyMDA3MzIzNFowga0xCzAJBgNVBAYTAkFVMSgwJgYDVQQKDB9UaGUgTGVnaW9uIG9mIHRoZSBCb3VuY3kgQ2FzdGxlMRIwEAYDVQQHDAlNZWxib3VybmUxETAPBgNVBAgMCFZpY3RvcmlhMU0wHAYDVQQFExVDVlI6MTIzNDU2NzgtVUlEOjExMTEwLQYJKoZIhvcNAQkBFiBmZWVkYmFjay1jcnlwdG9AYm91bmN5Y2FzdGxlLm9yZzBaMA0GCSqGSIb3DQEBAQUAA0kAMEYCQQC0p+RhcFdPFqlwgrIr5YtqKmKXmEGb4ShypL26Ymz66ZAPdqv7EhOdzl3lZWT6srZUMWWgQMYGiHQg4z2R7X7XAgERo0QwQjAOBgNVHQ8BAf8EBAMCAAEwEgYDVR0lAQH/BAgwBgYEVR0lADAcBgNVHREBAf8EEjAQgQ50ZXN0QHRlc3QudGVzdDANBgkqhkiG9w0BAQQFAANBAIhylDesUsTXE5nOV5hnGxnIamkLXNqJGBvNTOboDnxKF7bEi6mswBhk7zEc9aLnX5lH65jVJ6ZVUxvownx8jYU=");
public static void main(String[] args)
throws Exception
{
DEREncoder derEnc = new DEREncoder(DEREncoder.LENGTHS | DEREncoder.SORT_SETS);
byte[] derOut = derEnc.toDER(baseCert);
System.out.println(Arrays.equals(derOut, baseCert));
derOut = derEnc.toDER(cert_multiv_dn_der);
System.out.println(Arrays.equals(derOut, cert_multiv_dn_der));
derOut = derEnc.toDER(cert_multiv_dn_ber);
System.out.println(Arrays.equals(derOut, cert_multiv_dn_der));
derOut = derEnc.toDER(cert_multiv_dn_ber);
System.out.println(Arrays.equals(derOut, cert_multiv_dn_der));
System.out.println(Arrays.equals(cert_multiv_dn_ber, cert_multiv_dn_der));
}
}