- java.lang.Object
-
- org.apache.xml.security.utils.DERDecoderUtils
-
public class DERDecoderUtils extends java.lang.ObjectProvides the means to navigate through a DER-encoded byte array, to help in decoding the contents.It maintains a "current position" in the array that advances with each operation, providing a simple means to handle the type-length-value encoding of DER. For example
decoder.expect(TYPE); int length = decoder.getLength(); byte[] value = decoder.getBytes(len);
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.System.LoggerLOGstatic byteTYPE_BIT_STRINGDER type identifier for a bit string valuestatic byteTYPE_OBJECT_IDENTIFIERDER type identifier for ASN.1 "OBJECT IDENTIFIER" value.static byteTYPE_OCTET_STRINGDER type identifier for a octet string valuestatic byteTYPE_SEQUENCEDER type identifier for a sequence value
-
Constructor Summary
Constructors Constructor Description DERDecoderUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static longdecodeBytes(byte[] inBytes, int iOffset, int iLength)Decode a byte array into a long value.static java.lang.StringdecodeOID(byte[] oidBytes)The first two nodes of the OID are encoded onto a single byte.static byte[]getAlgorithmIdBytes(java.io.InputStream derEncodedIS)Simple method parses an ASN.1 encoded byte array.static java.lang.StringgetAlgorithmIdFromPublicKey(java.security.PublicKey publicKey)The method extracts the algorithm OID from the public key and returns it as "dot encoded" OID string.static intreadLength(java.io.InputStream derEncodedIs)Get the DER length at the current position.static byte[]readObjectIdentifier(java.io.InputStream derEncodedIS)Read the next object identifier from the given DER-encoded input stream.private static voidvalidateType(byte type, byte expectedType)private static voidvalidateType(int iType, byte expectedType)
-
-
-
Field Detail
-
LOG
private static final java.lang.System.Logger LOG
-
TYPE_BIT_STRING
public static final byte TYPE_BIT_STRING
DER type identifier for a bit string value- See Also:
- Constant Field Values
-
TYPE_OCTET_STRING
public static final byte TYPE_OCTET_STRING
DER type identifier for a octet string value- See Also:
- Constant Field Values
-
TYPE_SEQUENCE
public static final byte TYPE_SEQUENCE
DER type identifier for a sequence value- See Also:
- Constant Field Values
-
TYPE_OBJECT_IDENTIFIER
public static final byte TYPE_OBJECT_IDENTIFIER
DER type identifier for ASN.1 "OBJECT IDENTIFIER" value.- See Also:
- Constant Field Values
-
-
Method Detail
-
getAlgorithmIdBytes
public static byte[] getAlgorithmIdBytes(java.io.InputStream derEncodedIS) throws DERDecodingException, java.io.IOExceptionSimple method parses an ASN.1 encoded byte array. The encoding uses "DER", a BER/1 subset, that means a triple { typeId, length, data }. with the following structure:PublicKeyInfo ::= SEQUENCE { algorithm AlgorithmIdentifier, PublicKey BIT STRING }Where AlgorithmIdentifier is formatted as:
AlgorithmIdentifier ::= SEQUENCE { algorithm OBJECT IDENTIFIER, parameters ANY DEFINED BY algorithm OPTIONAL }- Parameters:
derEncodedIS- the DER-encoded input stream to decode.- Throws:
DERDecodingException- in case of decoding error or if given InputStream is null or empty.java.io.IOException- if an I/O error occurs.
-
readObjectIdentifier
public static byte[] readObjectIdentifier(java.io.InputStream derEncodedIS) throws DERDecodingExceptionRead the next object identifier from the given DER-encoded input stream.- Parameters:
derEncodedIS- the DER-encoded input stream to decode.- Returns:
- the object identifier as a byte array.
- Throws:
DERDecodingException- if parse error occurs.
-
getAlgorithmIdFromPublicKey
public static java.lang.String getAlgorithmIdFromPublicKey(java.security.PublicKey publicKey) throws DERDecodingExceptionThe method extracts the algorithm OID from the public key and returns it as "dot encoded" OID string.- Parameters:
publicKey- the public key for which method returns algorithm ID.- Returns:
- String representing the algorithm ID.
- Throws:
DERDecodingException- if the algorithm ID cannot be determined.
-
validateType
private static void validateType(int iType, byte expectedType) throws DERDecodingException- Throws:
DERDecodingException
-
validateType
private static void validateType(byte type, byte expectedType) throws DERDecodingException- Throws:
DERDecodingException
-
readLength
public static int readLength(java.io.InputStream derEncodedIs) throws DERDecodingException, java.io.IOExceptionGet the DER length at the current position.DER length is encoded as
- If the first byte is 0x00 to 0x7F, it describes the actual length.
- If the first byte is 0x80 + n with 0
The length value 0x80, used only in constructed types, is defined as "indefinite length".
- Returns:
- the length, -1 for indefinite length.
- Throws:
DERDecodingException- if the current position is at the end of the array or there is an incomplete length specification.java.io.IOException- if an I/O error occurs.
-
decodeOID
public static java.lang.String decodeOID(byte[] oidBytes)
The first two nodes of the OID are encoded onto a single byte. The first node is multiplied by the decimal 40 and the result is added to the value of the second node. Node values less than or equal to 127 are encoded in one byte. Node values greater than or equal to 128 are encoded on multiple bytes. Bit 7 of the leftmost byte is set to one. Bits 0 through 6 of each byte contains the encoded value.- Parameters:
oidBytes- the byte array containing the OID- Returns:
- the decoded OID as a string
-
decodeBytes
private static long decodeBytes(byte[] inBytes, int iOffset, int iLength)Decode a byte array into a long value. The most significant bit of each byte is ignored because it is used as a continuation flag. Bits are shifted to the left so that the most significant 7 bits are in first byte next 7 bits in second byte and so on.- Parameters:
inBytes- the input byte arrayiOffset- start point insideinBytesiLength- number of bytes to decode- Returns:
- long value decoded from the byte array.
-
-