public class ASN1
extends java.lang.Object
When creating an encoded ASN.1 object from an input stream
or from a byte array, the given input data automatically is decoded
properly depending on whether it is supplied in DER or PEM encoding format. Supposing,
for instance, some DER encoded ASN.1 object supplied as a byte array, first use the
ASN1(byte[] array) constructor for obtaining and decoding
the data, and subsequently call the toASN1Object method for
getting the delivered ASN1Object, e.g.:
//the byte array supplying the encoding byte[] encoding = ...; ASN1 asn1 = new ASN1(encoding); ASN1Object asn1_object = asn1.toASN1Object();If you are already aware to receive DER encoded data you alternatively may use one of the static
decode methods of the DerCoder
class. If you expect to deal with large amounts of data, it may be preferable to
take advantage of the DerInputStream utility for
parsing the incoming data.
Base64InputStream/Base64Encode and DerInputStream/DerCoder
utilities may be used for first Base64 decoding the PEM data, and subsequently DER decoding
the result from the first step.
When writing an ASN.1 Object to a byte array by calling the toByteArray method, the data is returned in DER encoded format. It may be preferable
to use one of the encode methods of the DerCoder
class for performing the DER encoding of some ASN1Object.
For PEM (Base64 DER) encoding DER encoded data, use the Base64Encode method of the iaik.utils.Util class, e.g.:
Don´t forget the BEGIN - END clauses when writing a PEM message, e.g.://create an ASN1 object from a byte array supplying the data in DER or //PEM encoded format: ASN1 asn1 = new ASN1(array); //Get the internal representation: ASN1Object asn1_obj = asn1.toASN1Object(); //DER encode the ASN1 object byte[] der_array = asn1.toByteArray(); //Base64 encode the DER encoded byte array just created to get the //PEM encoding: byte[] pem_array = Util.Base64Encode(der_array);
PrintWriter pw = new PrintWriter(new FileOutputStream("test.pem"));
pw.println("-----BEGIN PRIVACY-ENHANCED MESSAGE-----");
pw.println(new String(pem_array));
pw.println("-----END PRIVACY-ENHANCED MESSAGE-----");
For writing DER encoded data Base64 encoded to a stream, use the
Base64OutputStream class.
ASN1Object,
DerCoder,
DerInputStream,
Base64InputStream,
Base64OutputStream| Modifier and Type | Field and Description |
|---|---|
static int |
DER
Global value for ASN.1 coding format DER.
|
static int |
PEM
Global value for ASN.1 coding format PEM.
|
static java.lang.String |
startLine
First line of a file in PEM format.
|
| Constructor and Description |
|---|
ASN1()
Default constructor.
|
ASN1(ASN1Object obj)
Creates an ASN1 object from the supplied ASN1Object.
|
ASN1(byte[] array)
Creates an ASN1 object from a byte array.
|
ASN1(java.io.InputStream is)
Creates an ASN1 object from an InputStream.
|
| Modifier and Type | Method and Description |
|---|---|
int |
countComponents()
Returns the number of components in this ASN1 Object.
|
static byte[] |
extractFirstEncodedComponent(byte[] ba)
Extracts the DER encoded first component from a DER encoded ASN.1
structure (must be a Constructed type).
|
byte[] |
fingerprint()
Returns a fingerprint (MD5 Hash of the whole ASN1Object).
|
ASN1Object |
getComponentAt(int index)
Returns the ASN1Object at the given index if the ASN1Object
represented by this ASN1 object is of constructed type (e.g.
|
byte[] |
getFirstObject()
Returns the first SEQUENCE of a SEQUENCE ASN1 object as DER encoded byte array.
|
int |
getFormat()
Returns the format: ASN1.DER, ASN1.PEM.
|
static java.lang.String |
print(ASN1Object o)
Returns a string that represents the contents of the supplied ASN1Object.
|
ASN1Object |
toASN1Object()
Returns the ASN1Object represented by this ASN1 object.
|
byte[] |
toByteArray()
Returns the ASN1Object represented by this class as DER encoded byte array.
|
java.lang.String |
toString()
Returns a string that represents the contents of this ASN1Object.
|
void |
writeTo(java.io.OutputStream os)
DER encodes and writes the ASN1Object represented by this class to an OutputStream.
|
public static final int DER
public static final int PEM
public static java.lang.String startLine
public ASN1()
public ASN1(ASN1Object obj) throws CodingException
toByteArray or writeTo(OutputStream os) method.
You alternatively may use one of the encode methods of
the DerCoder class for DER encoding an
ASN1Object.
obj - the ASN1Object to be DER encodedCodingException - if the ASN1Object could not be DER encodedpublic ASN1(java.io.InputStream is)
throws java.io.IOException,
CodingException
The data can be in DER or PEM format. To decide if the data is DER or PEM encoded this method uses the first byte of data: If the first byte has the value: 65-77, 103-122 the format is PEM. Otherwise the format is DER. DER uses the tags 1-24, 48, 49, and 128-. These values PEM encoded result in (65-77, 103-122) and that´s why the algorithm should work :).
Use the toASN1Object() method for obtaining
the ASN1Object decoded from the supplied input stream data.
is - the InputStream containing the encoded datajava.io.IOException - if there is a problem with the InputStreamCodingException - if the object could not be decodedpublic ASN1(byte[] array)
throws CodingException
Use the toASN1Object() method for obtaining
the ASN1Object decoded from the supplied byte array data.
array - the byte array containing encoded ASN.1 objectCodingException - if the object could not be decodedpublic ASN1Object getComponentAt(int index) throws CodingException
index - the position of the component to be obtained from the constructed
ASN.1 objectCodingException - if this ASN1Object is not of constructed type or
the index is illegalConstructedTypepublic int countComponents()
throws CodingException
CodingException - if this ASN1Object does not support countComponents()public byte[] getFirstObject()
throws CodingException
This method only may be used for a SEQUENCE ASN1 object which contains some other SEQUENCE, e.g:
asn1SEQ ::= SEQUENCE {
field1 subSEQ,
...
}
subSEQ ::= SEQUENCE {
...
}
The first sub-sequence is returned as DER encoded byte array. Note that this
method searches the raw encoding for the first sub-sequence. This may be useful
in situations when doing some cryptographic operation where it is essential
that the original encoding format is preserved (e.g. verifying a hash, signature).
A X.509 certificate, for instance, holds the tbsCertificate structure to be verified in its first component:
Certificate ::= SEQUENCE {
tbsCertificate TBSCertificate,
signatureAlgorithm AlgorithmIdentifier,
signature BIT STRING }
Using getFistObject for extracting the tbsStructure will give
the raw DER bytes parsed from the original encoding.CodingException - if there is no sub-SEQUENCE in this SEQUENCEpublic static byte[] extractFirstEncodedComponent(byte[] ba)
throws CodingException
This is useful when dealing with signed data structures where the signature is calculated over the DER encoding of the first component in the ASN.1 structure. For example, X.509 Certificate and CRLs use this type of structure. When dealing with signed data, it is essential that the signed data is not modified (otherwise signature verification would fail). This API will provide access to the raw bytes over which the signature was calculated without needing to decode then re-encode (doing so could result in unintended modifications to the signed data by the encoding routine).
ba - a DER encoded ASN.1 structureCodingException - if the encoding is invalid (does not represent an DER encoded
ASN structure of a Constructed type)public byte[] toByteArray()
If you want to get a PEM (Base64 DER) encoding of the ASN1 object,
call Base64Encode
thereby supplying the DER encoded data returned by this method as parameter value,
e.g.:
ASN1 asn1 = ...; byte[] der_array = asn1.toByteArray(); //Base64 encode the DER encoded byte array just created to get the //PEM encoding: byte[] pem_array = Util.Base64Encode(der_array);
public void writeTo(java.io.OutputStream os)
throws java.io.IOException
The data written to the given output stream is DER encoded.
os - the output stream to which to write the datajava.io.IOException - if there an I/O error occurspublic int getFormat()
public java.lang.String toString()
toString in class java.lang.Objectpublic static java.lang.String print(ASN1Object o)
o - the ASN1Object about which information shall be printedpublic ASN1Object toASN1Object()
public byte[] fingerprint()