@@ -261,8 +261,8 @@ private static class ParseDerAnyResult {
261
261
DerTagClass tagClass ;
262
262
boolean constructed ;
263
263
byte tagValue ;
264
- byte [] content ;
265
- int nextOffset ;
264
+ int valueStart ;
265
+ int valueEnd ;
266
266
}
267
267
268
268
@ Value
@@ -342,14 +342,15 @@ private static ParseDerAnyResult parseDerAny(@NonNull byte[] der, int offset) {
342
342
DerTagClass .parse (tag ),
343
343
(tag & 0x20 ) != 0 ,
344
344
(byte ) (tag & 0x1f ),
345
- Arrays . copyOfRange ( der , contentLen .nextOffset , contentEnd ) ,
345
+ contentLen .nextOffset ,
346
346
contentEnd );
347
347
}
348
348
}
349
349
350
350
/**
351
- * Parse a DER header with the given tag value, constructed bit and tag class, and return a copy
352
- * of the value octets. If any of the three criteria do not match, return empty instead.
351
+ * Parse a DER header with the given tag value, constructed bit and tag class, and return the
352
+ * start and end offsets of the value octets. If any of the three criteria do not match, return
353
+ * empty instead.
353
354
*
354
355
* @param der DER source to read from.
355
356
* @param offset The offset in <code>der</code> from which to start reading.
@@ -359,11 +360,12 @@ private static ParseDerAnyResult parseDerAny(@NonNull byte[] der, int offset) {
359
360
* bit) of the tag octet.
360
361
* @param expectTagClass The expected tag class. This is the 2 most significant bits of the tag
361
362
* octet.
362
- * @return A copy of the value octets, if the parsed tag matches <code>expectTag</code>, <code>
363
+ * @return The start and end offsets of the value octets, if the parsed tag matches <code>
364
+ * expectTag</code>, <code>
363
365
* constructed</code> and <code>expectTagClass</code>, otherwise empty. {@link
364
366
* ParseDerResult#nextOffset} is always returned.
365
367
*/
366
- public static ParseDerResult <Optional <byte [] >> parseDerTaggedOrSkip (
368
+ public static ParseDerResult <Optional <Integer >> parseDerTaggedOrSkip (
367
369
@ NonNull byte [] der ,
368
370
int offset ,
369
371
byte expectTag ,
@@ -373,16 +375,16 @@ public static ParseDerResult<Optional<byte[]>> parseDerTaggedOrSkip(
373
375
if (result .tagValue == expectTag
374
376
&& result .constructed == constructed
375
377
&& result .tagClass == expectTagClass ) {
376
- return new ParseDerResult <>(Optional .of (result .content ), result .nextOffset );
378
+ return new ParseDerResult <>(Optional .of (result .valueStart ), result .valueEnd );
377
379
} else {
378
- return new ParseDerResult <>(Optional .empty (), result .nextOffset );
380
+ return new ParseDerResult <>(Optional .empty (), result .valueEnd );
379
381
}
380
382
}
381
383
382
384
/**
383
- * Parse a DER header with the given tag value, constructed bit and tag class, and return a copy
384
- * of the value octets. If any of the three criteria do not match, throw an {@link
385
- * IllegalArgumentException}.
385
+ * Parse a DER header with the given tag value, constructed bit and tag class, and return the
386
+ * start and end offsets of the value octets. If any of the three criteria do not match, throw an
387
+ * {@link IllegalArgumentException}.
386
388
*
387
389
* @param der DER source to read from.
388
390
* @param offset The offset in <code>der</code> from which to start reading.
@@ -392,11 +394,12 @@ public static ParseDerResult<Optional<byte[]>> parseDerTaggedOrSkip(
392
394
* bit) of the tag octet.
393
395
* @param expectTagClass The expected tag class. This is the 2 most significant bits of the tag
394
396
* octet.
395
- * @return A copy of the value octets, if the parsed tag matches <code>expectTag</code>, <code>
397
+ * @return The start and end offsets of the value octets, if the parsed tag matches <code>
398
+ * expectTag</code>, <code>
396
399
* constructed</code> and <code>expectTagClass</code>, otherwise empty. {@link
397
400
* ParseDerResult#nextOffset} is always returned.
398
401
*/
399
- private static ParseDerResult <byte [] > parseDerTagged (
402
+ private static ParseDerResult <Integer > parseDerTagged (
400
403
@ NonNull byte [] der ,
401
404
int offset ,
402
405
byte expectTag ,
@@ -406,7 +409,7 @@ private static ParseDerResult<byte[]> parseDerTagged(
406
409
if (result .tagValue == expectTag ) {
407
410
if (result .constructed == constructed ) {
408
411
if (result .tagClass == expectTagClass ) {
409
- return new ParseDerResult <>(result .content , result .nextOffset );
412
+ return new ParseDerResult <>(result .valueStart , result .valueEnd );
410
413
} else {
411
414
throw new IllegalArgumentException (
412
415
String .format (
@@ -476,16 +479,19 @@ public static <T> ParseDerResult<List<T>> parseDerSequenceContents(
476
479
*/
477
480
public static <T > ParseDerResult <List <T >> parseDerSequence (
478
481
@ NonNull byte [] der , int offset , @ NonNull ParseDerSequenceElementFunction <T > parseElement ) {
479
- final ParseDerResult <byte [] > seq =
482
+ final ParseDerResult <Integer > seq =
480
483
parseDerTagged (der , offset , (byte ) 0x10 , true , DerTagClass .UNIVERSAL );
481
484
final ParseDerResult <List <T >> res =
482
- parseDerSequenceContents (seq .result , 0 , seq .result . length , parseElement );
485
+ parseDerSequenceContents (der , seq .result , seq .nextOffset , parseElement );
483
486
return new ParseDerResult <>(res .result , seq .nextOffset );
484
487
}
485
488
486
489
/** Parse an Octet String. */
487
490
public static ParseDerResult <byte []> parseDerOctetString (@ NonNull byte [] der , int offset ) {
488
- return parseDerTagged (der , offset , (byte ) 0x04 , false , DerTagClass .UNIVERSAL );
491
+ ParseDerResult <Integer > res =
492
+ parseDerTagged (der , offset , (byte ) 0x04 , false , DerTagClass .UNIVERSAL );
493
+ return new ParseDerResult <>(
494
+ Arrays .copyOfRange (der , res .result , res .nextOffset ), res .nextOffset );
489
495
}
490
496
491
497
public static byte [] encodeDerObjectId (@ NonNull byte [] oid ) {
0 commit comments