public class ASN1
extends java.lang.Object
implements java.lang.Cloneable
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);
For writing DER encoded data Base64 encoded to a stream, use thePrintWriter 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-----");
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 |
---|---|
void |
clearASN1Object()
Clear the stored ASN1 object to save memory.
|
void |
clearByteArray()
Clear the stored encoding to save memory.
|
java.lang.Object |
clone()
Returns a clone of this ASN1 object.
|
int |
countComponents()
Returns the number of components in this ASN1 Object.
|
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.
|
static byte[] |
readEncoded(java.io.InputStream is)
Reads the encoding of an ASN.1 object from the given stream.
|
ASN1Object |
toASN1Object()
Returns the ASN1Object represented by this ASN1 object.
|
byte[] |
toByteArray()
Returns the ASN1Object represented by this class as DER encoded byte array.
|
byte[] |
toByteArray(boolean createClone)
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 final 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 static byte[] readEncoded(java.io.InputStream is) throws java.io.IOException, CodingException
This methods allows to parse an ASN.1 object without keeping the internal ASN.1 structure in memory.
is
- the stream from which to read the encodingjava.io.IOException
- if an error occurs when reading from the streamCodingException
- if a coding error occurspublic 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
illegalConstructedType
public 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 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.Object
public static java.lang.String print(ASN1Object o)
o
- the ASN1Object about which information shall be printedpublic 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: if (der_array != null) { byte[] pem_array = Util.Base64Encode(der_array); }Note that the byte array returned by this method is not cloned! If you need a clone you must clone the array after calling this method or may use method
toByteArray(true)
.null
if internal ASN1Object and ASN1 array have been clearedpublic byte[] toByteArray(boolean createClone)
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: if (der_array != null) { byte[] pem_array = Util.Base64Encode(der_array); }
null
if internal ASN1Object and ASN1 array have been clearedpublic ASN1Object toASN1Object()
public void clearASN1Object()
public void clearByteArray()
public java.lang.Object clone()
clone
in class java.lang.Object
public byte[] fingerprint()