Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.base/share/classes/sun/security/util/DerIndefLenConverter.java
67773 views
1
/*
2
* Copyright (c) 1998, 2021, 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 BER
35
* encoded byte arrays to definite length DER encoded byte arrays.
36
* <p>
37
* Note: This class only substitute indefinite length octets to definite
38
* length octets. It does not update the contents even if they are not DER.
39
* <p>
40
* This assumes that the basic data structure is "tag, length, value"
41
* triplet. In the case where the length is "indefinite", terminating
42
* end-of-contents bytes are expected.
43
*
44
* @author Hemma Prafullchandra
45
*/
46
class DerIndefLenConverter {
47
48
private static final int LEN_LONG = 0x80; // bit 8 set
49
private static final int LEN_MASK = 0x7f; // bits 7 - 1
50
51
private byte[] data, newData;
52
private int newDataPos, dataPos, dataSize, index;
53
private int unresolved = 0;
54
55
// A list to store each indefinite length occurrence. Whenever an indef
56
// length is seen, the position after the 0x80 byte is appended to the
57
// list as an integer. Whenever its matching EOC is seen, we know the
58
// actual length and the position value is substituted with a calculated
59
// length octets. At the end, the new DER encoding is a concatenation of
60
// all existing tags, existing definite length octets, existing contents,
61
// and the newly created definte length octets in this list.
62
private ArrayList<Object> ndefsList = new ArrayList<Object>();
63
64
// Length of extra bytes needed to convert indefinite encoding to definite.
65
// For each resolved indefinite length encoding, the starting 0x80 byte
66
// and the ending 00 00 bytes will be removed and a new definite length
67
// octets will be added. This value might be positive or negative.
68
private int numOfTotalLenBytes = 0;
69
70
private static boolean isEOC(byte[] data, int pos) {
71
return data[pos] == 0 && data[pos + 1] == 0;
72
}
73
74
// if bit 8 is set then it implies either indefinite length or long form
75
static boolean isLongForm(int lengthByte) {
76
return ((lengthByte & LEN_LONG) == LEN_LONG);
77
}
78
79
/*
80
* Private constructor
81
*/
82
private DerIndefLenConverter() { }
83
84
/**
85
* Checks whether the given length byte is of the form
86
* <em>Indefinite</em>.
87
*
88
* @param lengthByte the length byte from a DER encoded
89
* object.
90
* @return true if the byte is of Indefinite form otherwise
91
* returns false.
92
*/
93
static boolean isIndefinite(int lengthByte) {
94
return (isLongForm(lengthByte) && ((lengthByte & LEN_MASK) == 0));
95
}
96
97
/**
98
* Consumes the tag at {@code dataPos}.
99
* <p>
100
* If it is EOC then replace the matching start position (i.e. the previous
101
* {@code dataPos} where an indefinite length was found by #parseLength)
102
* in {@code ndefsList} with a length octets for this section.
103
*/
104
private void parseTag() throws IOException {
105
if (isEOC(data, dataPos)) {
106
int numOfEncapsulatedLenBytes = 0;
107
Object elem = null;
108
int index;
109
for (index = ndefsList.size()-1; index >= 0; index--) {
110
// Determine the first element in the vector that does not
111
// have a matching EOC
112
elem = ndefsList.get(index);
113
if (elem instanceof Integer) {
114
break;
115
} else {
116
// For each existing converted part, 3 bytes (80 at the
117
// beginning and 00 00 at the end) are removed and a
118
// new length octets is added.
119
numOfEncapsulatedLenBytes += ((byte[])elem).length - 3;
120
}
121
}
122
if (index < 0) {
123
throw new IOException("EOC does not have matching " +
124
"indefinite-length tag");
125
}
126
int sectionLen = dataPos - ((Integer)elem).intValue() +
127
numOfEncapsulatedLenBytes;
128
byte[] sectionLenBytes = getLengthBytes(sectionLen);
129
ndefsList.set(index, sectionLenBytes);
130
assert unresolved > 0;
131
unresolved--;
132
133
// Add the number of bytes required to represent this section
134
// to the total number of length bytes,
135
// and subtract the indefinite-length tag (1 byte) and
136
// EOC bytes (2 bytes) for this section
137
numOfTotalLenBytes += (sectionLenBytes.length - 3);
138
}
139
dataPos++;
140
}
141
142
/**
143
* Write the tag and if it is an end-of-contents tag
144
* then skip the tag and its 1 byte length of zero.
145
*/
146
private void writeTag() {
147
while (dataPos < dataSize) {
148
assert dataPos + 1 < dataSize;
149
if (isEOC(data, dataPos)) {
150
dataPos += 2; // skip tag and length
151
} else {
152
newData[newDataPos++] = data[dataPos++];
153
break;
154
}
155
}
156
}
157
158
/**
159
* Parse the length octets started at {@code dataPos}. After this method
160
* is called, {@code dataPos} is placed after the length octets except
161
* -1 is returned.
162
*
163
* @return a) the length of definite length data next
164
* b) -1, if it is a definite length data next but the length
165
* octets is not complete to determine the actual length
166
* c) 0, if it is an indefinite length. Also, append the current
167
* position to the {@code ndefsList} vector.
168
* @throws IOException if invalid data is read
169
*/
170
private int parseLength() throws IOException {
171
if (dataPos == dataSize) {
172
return 0;
173
}
174
int lenByte = data[dataPos++] & 0xff;
175
if (isIndefinite(lenByte)) {
176
ndefsList.add(dataPos);
177
unresolved++;
178
return 0;
179
}
180
int curLen = 0;
181
if (isLongForm(lenByte)) {
182
lenByte &= LEN_MASK;
183
if (lenByte > 4) {
184
throw new IOException("Too much data");
185
}
186
if ((dataSize - dataPos) < (lenByte + 1)) {
187
return -1;
188
}
189
for (int i = 0; i < lenByte; i++) {
190
curLen = (curLen << 8) + (data[dataPos++] & 0xff);
191
}
192
if (curLen < 0) {
193
throw new IOException("Invalid length bytes");
194
}
195
} else {
196
curLen = (lenByte & LEN_MASK);
197
}
198
return curLen;
199
}
200
201
/**
202
* Write the length and value.
203
* <p>
204
* If it was definite length, just re-write the length and copy the value.
205
* If it was an indefinite length, copy the precalculated definite octets
206
* from {@code ndefsList}. There is no values here because they will be
207
* sub-encodings of a constructed encoding.
208
*/
209
private void writeLengthAndValue() throws IOException {
210
if (dataPos == dataSize) {
211
return;
212
}
213
int curLen = 0;
214
int lenByte = data[dataPos++] & 0xff;
215
if (isIndefinite(lenByte)) {
216
byte[] lenBytes = (byte[])ndefsList.get(index++);
217
System.arraycopy(lenBytes, 0, newData, newDataPos,
218
lenBytes.length);
219
newDataPos += lenBytes.length;
220
} else {
221
if (isLongForm(lenByte)) {
222
lenByte &= LEN_MASK;
223
for (int i = 0; i < lenByte; i++) {
224
curLen = (curLen << 8) + (data[dataPos++] & 0xff);
225
}
226
if (curLen < 0) {
227
throw new IOException("Invalid length bytes");
228
}
229
} else {
230
curLen = (lenByte & LEN_MASK);
231
}
232
writeLength(curLen);
233
writeValue(curLen);
234
}
235
}
236
237
private void writeLength(int curLen) {
238
if (curLen < 128) {
239
newData[newDataPos++] = (byte)curLen;
240
241
} else if (curLen < (1 << 8)) {
242
newData[newDataPos++] = (byte)0x81;
243
newData[newDataPos++] = (byte)curLen;
244
245
} else if (curLen < (1 << 16)) {
246
newData[newDataPos++] = (byte)0x82;
247
newData[newDataPos++] = (byte)(curLen >> 8);
248
newData[newDataPos++] = (byte)curLen;
249
250
} else if (curLen < (1 << 24)) {
251
newData[newDataPos++] = (byte)0x83;
252
newData[newDataPos++] = (byte)(curLen >> 16);
253
newData[newDataPos++] = (byte)(curLen >> 8);
254
newData[newDataPos++] = (byte)curLen;
255
256
} else {
257
newData[newDataPos++] = (byte)0x84;
258
newData[newDataPos++] = (byte)(curLen >> 24);
259
newData[newDataPos++] = (byte)(curLen >> 16);
260
newData[newDataPos++] = (byte)(curLen >> 8);
261
newData[newDataPos++] = (byte)curLen;
262
}
263
}
264
265
private byte[] getLengthBytes(int curLen) {
266
byte[] lenBytes;
267
int index = 0;
268
269
if (curLen < 128) {
270
lenBytes = new byte[1];
271
lenBytes[index++] = (byte)curLen;
272
273
} else if (curLen < (1 << 8)) {
274
lenBytes = new byte[2];
275
lenBytes[index++] = (byte)0x81;
276
lenBytes[index++] = (byte)curLen;
277
278
} else if (curLen < (1 << 16)) {
279
lenBytes = new byte[3];
280
lenBytes[index++] = (byte)0x82;
281
lenBytes[index++] = (byte)(curLen >> 8);
282
lenBytes[index++] = (byte)curLen;
283
284
} else if (curLen < (1 << 24)) {
285
lenBytes = new byte[4];
286
lenBytes[index++] = (byte)0x83;
287
lenBytes[index++] = (byte)(curLen >> 16);
288
lenBytes[index++] = (byte)(curLen >> 8);
289
lenBytes[index++] = (byte)curLen;
290
291
} else {
292
lenBytes = new byte[5];
293
lenBytes[index++] = (byte)0x84;
294
lenBytes[index++] = (byte)(curLen >> 24);
295
lenBytes[index++] = (byte)(curLen >> 16);
296
lenBytes[index++] = (byte)(curLen >> 8);
297
lenBytes[index++] = (byte)curLen;
298
}
299
300
return lenBytes;
301
}
302
303
// Returns the number of bytes needed to represent the given length
304
// in ASN.1 notation
305
private int getNumOfLenBytes(int len) {
306
int numOfLenBytes = 0;
307
308
if (len < 128) {
309
numOfLenBytes = 1;
310
} else if (len < (1 << 8)) {
311
numOfLenBytes = 2;
312
} else if (len < (1 << 16)) {
313
numOfLenBytes = 3;
314
} else if (len < (1 << 24)) {
315
numOfLenBytes = 4;
316
} else {
317
numOfLenBytes = 5;
318
}
319
return numOfLenBytes;
320
}
321
322
/**
323
* Write the value;
324
*/
325
private void writeValue(int curLen) {
326
System.arraycopy(data, dataPos, newData, newDataPos, curLen);
327
dataPos += curLen;
328
newDataPos += curLen;
329
}
330
331
/**
332
* Converts a indefinite length DER encoded byte array to
333
* a definte length DER encoding.
334
*
335
* @param indefData the byte array holding the indefinite
336
* length encoding.
337
* @return the byte array containing the definite length
338
* DER encoding, or null if there is not enough data.
339
* @exception IOException on parsing or re-writing errors.
340
*/
341
byte[] convertBytes(byte[] indefData) throws IOException {
342
data = indefData;
343
dataPos = 0;
344
dataSize = data.length;
345
346
// parse and set up the vectors of all the indefinite-lengths
347
while (dataPos < dataSize) {
348
if (dataPos + 2 > dataSize) {
349
// There should be at least one tag and one length
350
return null;
351
}
352
parseTag();
353
int len = parseLength();
354
if (len < 0) {
355
return null;
356
}
357
dataPos += len;
358
if (dataPos < 0) {
359
// overflow
360
throw new IOException("Data overflow");
361
}
362
if (unresolved == 0) {
363
assert !ndefsList.isEmpty() && ndefsList.get(0) instanceof byte[];
364
break;
365
}
366
}
367
368
if (unresolved != 0) {
369
return null;
370
}
371
372
int unused = dataSize - dataPos;
373
assert unused >= 0;
374
dataSize = dataPos;
375
376
newData = new byte[dataSize + numOfTotalLenBytes + unused];
377
dataPos = 0; newDataPos = 0; index = 0;
378
379
// write out the new byte array replacing all the indefinite-lengths
380
// and EOCs
381
while (dataPos < dataSize) {
382
writeTag();
383
writeLengthAndValue();
384
}
385
System.arraycopy(indefData, dataSize,
386
newData, dataSize + numOfTotalLenBytes, unused);
387
388
return newData;
389
}
390
391
/**
392
* Read the input stream into a DER byte array. If an indef len BER is
393
* not resolved this method will try to read more data until EOF is reached.
394
* This may block.
395
*
396
* @param in the input stream with tag and lenByte already read
397
* @param tag the tag to remember
398
* @return a DER byte array
399
* @throws IOException if not all indef len BER
400
* can be resolved or another I/O error happens
401
*/
402
public static byte[] convertStream(InputStream in, byte tag)
403
throws IOException {
404
int offset = 2; // for tag and length bytes
405
int readLen = in.available();
406
byte[] indefData = new byte[readLen + offset];
407
indefData[0] = tag;
408
indefData[1] = (byte)0x80;
409
while (true) {
410
int bytesRead = in.readNBytes(indefData, offset, readLen);
411
if (bytesRead != readLen) {
412
readLen = bytesRead;
413
indefData = Arrays.copyOf(indefData, offset + bytesRead);
414
}
415
DerIndefLenConverter derIn = new DerIndefLenConverter();
416
byte[] result = derIn.convertBytes(indefData);
417
if (result == null) {
418
int next = in.read(); // This could block, but we need more
419
if (next == -1) {
420
throw new IOException("not enough data to resolve indef len BER");
421
}
422
int more = in.available();
423
// expand array to include next and more
424
indefData = Arrays.copyOf(indefData, offset + readLen + 1 + more);
425
indefData[offset + readLen] = (byte)next;
426
offset = offset + readLen + 1;
427
readLen = more;
428
} else {
429
return result;
430
}
431
}
432
}
433
}
434
435