APIdia Launch Partner
Top Description Fields Constructors Methods
sun.security.util

pack-priv Class DerIndefLenConverter

extends Object
Class Inheritance
Imports
java.io.IOException, .InputStream, java.util.ArrayList, .Arrays

A package private utility class to convert indefinite length BER encoded byte arrays to definite length DER encoded byte arrays.

Note

This class only substitute indefinite length octets to definite length octets. It does not update the contents even if they are not DER.

This assumes that the basic data structure is "tag, length, value" triplet. In the case where the length is "indefinite", terminating end-of-contents bytes are expected.

Author
Hemma Prafullchandra

Field Summary

Modifier and TypeField and Description
private byte[]
private int
private int
private int
private static final int
private static final int
private final ArrayList<Object>
private byte[]
private int
private int
private int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv byte[]

Returns:

the byte array containing the definite length DER encoding, or null if there is not enough data.
convertBytes
(byte[]
the byte array holding the indefinite length encoding.
indefData
)

Converts an indefinite length DER encoded byte array to a definite length DER encoding.

public static byte[]

Returns:

a DER byte array
convertStream
(InputStream
the input stream with tag and lenByte already read
in
,
byte
the tag to remember
tag
)

Read the input stream into a DER byte array.

private byte[]
getLengthBytes(int curLen)

private int
private static boolean
isEOC(byte[] data, int pos)

pack-priv static boolean

Returns:

true if the byte is of Indefinite form otherwise returns false.
isIndefinite
(int
the length byte from a DER encoded object.
lengthByte
)

Checks whether the given length byte is of the form Indefinite.

pack-priv static boolean
isLongForm(int lengthByte)

private int

Returns:

a) the length of definite length data next b) -1, if it is a definite length data next but the length octets is not complete to determine the actual length c) 0, if it is an indefinite length. Also, append the current position to the ndefsList vector.
parseLength
()

Parse the length octets started at dataPos.

private void
parseTag()

Consumes the tag at dataPos.

private void
writeLength(int curLen)

private void
writeLengthAndValue()

Write the length and value.

private void
writeTag()

Write the tag and if it is an end-of-contents tag then skip the tag and its 1 byte length of zero.

private void
writeValue(int curLen)

Write the value;

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

databack to summary
private byte[] data
dataPosback to summary
private int dataPos
dataSizeback to summary
private int dataSize
indexback to summary
private int index
LEN_LONGback to summary
private static final int LEN_LONG
LEN_MASKback to summary
private static final int LEN_MASK
ndefsListback to summary
private final ArrayList<Object> ndefsList
newDataback to summary
private byte[] newData
newDataPosback to summary
private int newDataPos
numOfTotalLenBytesback to summary
private int numOfTotalLenBytes
unresolvedback to summary
private int unresolved

Constructor Detail

DerIndefLenConverterback to summary
private DerIndefLenConverter()

Method Detail

convertBytesback to summary
pack-priv byte[] convertBytes(byte[] indefData) throws IOException

Converts an indefinite length DER encoded byte array to a definite length DER encoding.

Parameters
indefData:byte[]

the byte array holding the indefinite length encoding.

Returns:byte[]

the byte array containing the definite length DER encoding, or null if there is not enough data.

Exceptions
IOException:
on parsing or re-writing errors.
convertStreamback to summary
public static byte[] convertStream(InputStream in, byte tag) throws IOException

Read the input stream into a DER byte array. If an indef len BER is not resolved this method will try to read more data until EOF is reached. This may block.

Parameters
in:InputStream

the input stream with tag and lenByte already read

tag:byte

the tag to remember

Returns:byte[]

a DER byte array

Exceptions
IOException:
if not all indef len BER can be resolved or another I/O error happens
getLengthBytesback to summary
private byte[] getLengthBytes(int curLen)
getNumOfLenBytesback to summary
private int getNumOfLenBytes(int len)
isEOCback to summary
private static boolean isEOC(byte[] data, int pos)
isIndefiniteback to summary
pack-priv static boolean isIndefinite(int lengthByte)

Checks whether the given length byte is of the form Indefinite.

Parameters
lengthByte:int

the length byte from a DER encoded object.

Returns:boolean

true if the byte is of Indefinite form otherwise returns false.

isLongFormback to summary
pack-priv static boolean isLongForm(int lengthByte)
parseLengthback to summary
private int parseLength() throws IOException

Parse the length octets started at dataPos. After this method is called, dataPos is placed after the length octets except -1 is returned.

Returns:int

a) the length of definite length data next b) -1, if it is a definite length data next but the length octets is not complete to determine the actual length c) 0, if it is an indefinite length. Also, append the current position to the ndefsList vector.

Exceptions
IOException:
if invalid data is read
parseTagback to summary
private void parseTag() throws IOException

Consumes the tag at dataPos.

If it is EOC then replace the matching start position (i.e. the previous dataPos where an indefinite length was found by #parseLength) in ndefsList with a length octets for this section.

writeLengthback to summary
private void writeLength(int curLen)
writeLengthAndValueback to summary
private void writeLengthAndValue() throws IOException

Write the length and value.

If it was definite length, just re-write the length and copy the value. If it was an indefinite length, copy the precalculated definite octets from ndefsList. There is no values here because they will be sub-encodings of a constructed encoding.

writeTagback to summary
private void writeTag()

Write the tag and if it is an end-of-contents tag then skip the tag and its 1 byte length of zero.

writeValueback to summary
private void writeValue(int curLen)

Write the value;