Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/util/DerIndefLenConverter.java
41159 views
1
/*
2
* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.util;
27
28
import java.io.IOException;
29
import java.io.InputStream;
30
import java.util.ArrayList;
31
import java.util.Arrays;
32
33
/**
34
* A package private utility class to convert indefinite length DER
35
* encoded byte arrays to definite length DER encoded byte arrays.
36
*
37
* This assumes that the basic data structure is "tag, length, value"
38
* triplet. In the case where the length is "indefinite", terminating
39
* end-of-contents bytes are expected.
40
*
41
* @author Hemma Prafullchandra
42
*/
43
class DerIndefLenConverter {
44
45
private static final int TAG_MASK = 0x1f; // bits 5-1
46
private static final int FORM_MASK = 0x20; // bits 6
47
private static final int CLASS_MASK = 0xC0; // bits 8 and 7
48
49
private static final int LEN_LONG = 0x80; // bit 8 set
50
private static final int LEN_MASK = 0x7f; // bits 7 - 1
51
private static final int SKIP_EOC_BYTES = 2;
52
53
private byte[] data, newData;
54
private int newDataPos, dataPos, dataSize, index;
55
private int unresolved = 0;
56
57
private ArrayList<Object> ndefsList = new ArrayList<Object>();
58
59
private int numOfTotalLenBytes = 0;
60
61
private boolean isEOC(int tag) {
62
return (((tag & TAG_MASK) == 0x00) && // EOC
63
((tag & FORM_MASK) == 0x00) && // primitive
64
((tag & CLASS_MASK) == 0x00)); // universal
65
}
66
67
// if bit 8 is set then it implies either indefinite length or long form
68
static boolean isLongForm(int lengthByte) {
69
return ((lengthByte & LEN_LONG) == LEN_LONG);
70
}
71
72
/*
73
* Default package private constructor
74
*/
75
DerIndefLenConverter() { }
76
77
/**
78
* Checks whether the given length byte is of the form
79
* <em>Indefinite</em>.
80
*
81
* @param lengthByte the length byte from a DER encoded
82
* object.
83
* @return true if the byte is of Indefinite form otherwise
84
* returns false.
85
*/
86
static boolean isIndefinite(int lengthByte) {
87
return (isLongForm(lengthByte) && ((lengthByte & LEN_MASK) == 0));
88
}
89
90
/**
91
* Parse the tag and if it is an end-of-contents tag then
92
* add the current position to the <code>eocList</code> vector.
93
*/
94
private void parseTag() throws IOException {
95
if (isEOC(data[dataPos]) && (data[dataPos + 1] == 0)) {
96
int numOfEncapsulatedLenBytes = 0;
97
Object elem = null;
98
int index;
99
for (index = ndefsList.size()-1; index >= 0; index--) {
100
// Determine the first element in the vector that does not
101
// have a matching EOC
102
elem = ndefsList.get(index);
103
if (elem instanceof Integer) {
104
break;
105
} else {
106
numOfEncapsulatedLenBytes += ((byte[])elem).length - 3;
107
}
108
}
109
if (index < 0) {
110
throw new IOException("EOC does not have matching " +
111
"indefinite-length tag");
112
}
113
int sectionLen = dataPos - ((Integer)elem).intValue() +
114
numOfEncapsulatedLenBytes;
115
byte[] sectionLenBytes = getLengthBytes(sectionLen);
116
ndefsList.set(index, sectionLenBytes);
117
unresolved--;
118
119
// Add the number of bytes required to represent this section
120
// to the total number of length bytes,
121
// and subtract the indefinite-length tag (1 byte) and
122
// EOC bytes (2 bytes) for this section
123
numOfTotalLenBytes += (sectionLenBytes.length - 3);
124
}
125
dataPos++;
126
}
127
128
/**
129
* Write the tag and if it is an end-of-contents tag
130
* then skip the tag and its 1 byte length of zero.
131
*/
132
private void writeTag() {
133
if (dataPos == dataSize)
134
return;
135
int tag = data[dataPos++];
136
if (isEOC(tag) && (data[dataPos] == 0)) {
137
dataPos++; // skip length
138
writeTag();
139
} else
140
newData[newDataPos++] = (byte)tag;
141
}
142
143
/**
144
* Parse the length and if it is an indefinite length then add
145
* the current position to the <code>ndefsList</code> vector.
146
*
147
* @return the length of definite length data next, or -1 if there is
148
* not enough bytes to determine it
149
* @throws IOException if invalid data is read
150
*/
151
private int parseLength() throws IOException {
152
int curLen = 0;
153
if (dataPos == dataSize)
154
return curLen;
155
int lenByte = data[dataPos++] & 0xff;
156
if (isIndefinite(lenByte)) {
157
ndefsList.add(dataPos);
158
unresolved++;
159
return curLen;
160
}
161
if (isLongForm(lenByte)) {
162
lenByte &= LEN_MASK;
163
if (lenByte > 4) {
164
throw new IOException("Too much data");
165
}
166
if ((dataSize - dataPos) < (lenByte + 1)) {
167
return -1;
168
}
169
for (int i = 0; i < lenByte; i++) {
170
curLen = (curLen << 8) + (data[dataPos++] & 0xff);
171
}
172
if (curLen < 0) {
173
throw new IOException("Invalid length bytes");
174
}
175
} else {
176
curLen = (lenByte & LEN_MASK);
177
}
178
return curLen;
179
}
180
181
/**
182
* Write the length and if it is an indefinite length
183
* then calculate the definite length from the positions
184
* of the indefinite length and its matching EOC terminator.
185
* Then, write the value.
186
*/
187
private void writeLengthAndValue() throws IOException {
188
if (dataPos == dataSize)
189
return;
190
int curLen = 0;
191
int lenByte = data[dataPos++] & 0xff;
192
if (isIndefinite(lenByte)) {
193
byte[] lenBytes = (byte[])ndefsList.get(index++);
194
System.arraycopy(lenBytes, 0, newData, newDataPos,
195
lenBytes.length);
196
newDataPos += lenBytes.length;
197
return;
198
}
199
if (isLongForm(lenByte)) {
200
lenByte &= LEN_MASK;
201
for (int i = 0; i < lenByte; i++) {
202
curLen = (curLen << 8) + (data[dataPos++] & 0xff);
203
}
204
if (curLen < 0) {
205
throw new IOException("Invalid length bytes");
206
}
207
} else {
208
curLen = (lenByte & LEN_MASK);
209
}
210
writeLength(curLen);
211
writeValue(curLen);
212
}
213
214
private void writeLength(int curLen) {
215
if (curLen < 128) {
216
newData[newDataPos++] = (byte)curLen;
217
218
} else if (curLen < (1 << 8)) {
219
newData[newDataPos++] = (byte)0x81;
220
newData[newDataPos++] = (byte)curLen;
221
222
} else if (curLen < (1 << 16)) {
223
newData[newDataPos++] = (byte)0x82;
224
newData[newDataPos++] = (byte)(curLen >> 8);
225
newData[newDataPos++] = (byte)curLen;
226
227
} else if (curLen < (1 << 24)) {
228
newData[newDataPos++] = (byte)0x83;
229
newData[newDataPos++] = (byte)(curLen >> 16);
230
newData[newDataPos++] = (byte)(curLen >> 8);
231
newData[newDataPos++] = (byte)curLen;
232
233
} else {
234
newData[newDataPos++] = (byte)0x84;
235
newData[newDataPos++] = (byte)(curLen >> 24);
236
newData[newDataPos++] = (byte)(curLen >> 16);
237
newData[newDataPos++] = (byte)(curLen >> 8);
238
newData[newDataPos++] = (byte)curLen;
239
}
240
}
241
242
private byte[] getLengthBytes(int curLen) {
243
byte[] lenBytes;
244
int index = 0;
245
246
if (curLen < 128) {
247
lenBytes = new byte[1];
248
lenBytes[index++] = (byte)curLen;
249
250
} else if (curLen < (1 << 8)) {
251
lenBytes = new byte[2];
252
lenBytes[index++] = (byte)0x81;
253
lenBytes[index++] = (byte)curLen;
254
255
} else if (curLen < (1 << 16)) {
256
lenBytes = new byte[3];
257
lenBytes[index++] = (byte)0x82;
258
lenBytes[index++] = (byte)(curLen >> 8);
259
lenBytes[index++] = (byte)curLen;
260
261
} else if (curLen < (1 << 24)) {
262
lenBytes = new byte[4];
263
lenBytes[index++] = (byte)0x83;
264
lenBytes[index++] = (byte)(curLen >> 16);
265
lenBytes[index++] = (byte)(curLen >> 8);
266
lenBytes[index++] = (byte)curLen;
267
268
} else {
269
lenBytes = new byte[5];
270
lenBytes[index++] = (byte)0x84;
271
lenBytes[index++] = (byte)(curLen >> 24);
272
lenBytes[index++] = (byte)(curLen >> 16);
273
lenBytes[index++] = (byte)(curLen >> 8);
274
lenBytes[index++] = (byte)curLen;
275
}
276
277
return lenBytes;
278
}
279
280
// Returns the number of bytes needed to represent the given length
281
// in ASN.1 notation
282
private int getNumOfLenBytes(int len) {
283
int numOfLenBytes = 0;
284
285
if (len < 128) {
286
numOfLenBytes = 1;
287
} else if (len < (1 << 8)) {
288
numOfLenBytes = 2;
289
} else if (len < (1 << 16)) {
290
numOfLenBytes = 3;
291
} else if (len < (1 << 24)) {
292
numOfLenBytes = 4;
293
} else {
294
numOfLenBytes = 5;
295
}
296
return numOfLenBytes;
297
}
298
299
/**
300
* Parse the value;
301
*/
302
private void parseValue(int curLen) {
303
dataPos += curLen;
304
}
305
306
/**
307
* Write the value;
308
*/
309
private void writeValue(int curLen) {
310
for (int i=0; i < curLen; i++)
311
newData[newDataPos++] = data[dataPos++];
312
}
313
314
/**
315
* Converts a indefinite length DER encoded byte array to
316
* a definte length DER encoding.
317
*
318
* @param indefData the byte array holding the indefinite
319
* length encoding.
320
* @return the byte array containing the definite length
321
* DER encoding, or null if there is not enough data.
322
* @exception IOException on parsing or re-writing errors.
323
*/
324
byte[] convertBytes(byte[] indefData) throws IOException {
325
data = indefData;
326
dataPos=0; index=0;
327
dataSize = data.length;
328
int len=0;
329
int unused = 0;
330
331
// parse and set up the vectors of all the indefinite-lengths
332
while (dataPos < dataSize) {
333
if (dataPos + 2 > dataSize) {
334
// There should be at least one tag and one length
335
return null;
336
}
337
parseTag();
338
len = parseLength();
339
if (len < 0) {
340
return null;
341
}
342
parseValue(len);
343
if (unresolved == 0) {
344
unused = dataSize - dataPos;
345
dataSize = dataPos;
346
break;
347
}
348
}
349
350
if (unresolved != 0) {
351
return null;
352
}
353
354
newData = new byte[dataSize + numOfTotalLenBytes + unused];
355
dataPos=0; newDataPos=0; index=0;
356
357
// write out the new byte array replacing all the indefinite-lengths
358
// and EOCs
359
while (dataPos < dataSize) {
360
writeTag();
361
writeLengthAndValue();
362
}
363
System.arraycopy(indefData, dataSize,
364
newData, dataSize + numOfTotalLenBytes, unused);
365
366
return newData;
367
}
368
369
/**
370
* Read the input stream into a DER byte array. If an indef len BER is
371
* not resolved this method will try to read more data until EOF is reached.
372
* This may block.
373
*
374
* @param in the input stream with tag and lenByte already read
375
* @param tag the tag to remember
376
* @return a DER byte array
377
* @throws IOException if not all indef len BER
378
* can be resolved or another I/O error happens
379
*/
380
public static byte[] convertStream(InputStream in, byte tag)
381
throws IOException {
382
int offset = 2; // for tag and length bytes
383
int readLen = in.available();
384
byte[] indefData = new byte[readLen + offset];
385
indefData[0] = tag;
386
indefData[1] = (byte)0x80;
387
while (true) {
388
int bytesRead = in.readNBytes(indefData, offset, readLen);
389
if (bytesRead != readLen) {
390
readLen = bytesRead;
391
indefData = Arrays.copyOf(indefData, offset + bytesRead);
392
}
393
DerIndefLenConverter derIn = new DerIndefLenConverter();
394
byte[] result = derIn.convertBytes(indefData);
395
if (result == null) {
396
int next = in.read(); // This could block, but we need more
397
if (next == -1) {
398
throw new IOException("not all indef len BER resolved");
399
}
400
int more = in.available();
401
// expand array to include next and more
402
indefData = Arrays.copyOf(indefData, offset + readLen + 1 + more);
403
indefData[offset + readLen] = (byte)next;
404
offset = offset + readLen + 1;
405
readLen = more;
406
} else {
407
return result;
408
}
409
}
410
}
411
}
412
413