|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--iaik.asn1.ASN1
This class is responsible for converting ASN.1 objects between internal (ASN1Object) and external (PEM, DER encoded arrays) representation.
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
Field Summary | |
static int |
DER
Global value for ASN.1 coding format DER. |
static int |
PEM
Global value for ASN.1 coding format PEM. |
static String |
startLine
First line of a file in PEM format. |
Constructor Summary | |
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(InputStream is)
Creates an ASN1 object from an InputStream. |
Method Summary | |
void |
clearASN1Object()
Clear the stored ASN1 object to save memory. |
void |
clearByteArray()
Clear the stored encoding to save memory. |
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 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. |
String |
toString()
Returns a string that represents the contents of this ASN1Object. |
void |
writeTo(OutputStream os)
DER encodes and writes the ASN1Object represented by this class to an OutputStream. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final int DER
public static final int PEM
public static final String startLine
Constructor Detail |
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(InputStream is) throws 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 dataIOException
- 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 decodedMethod Detail |
public 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(OutputStream os) throws IOException
The data written to the given output stream is DER encoded.
os
- the output stream to which to write the dataIOException
- if there an I/O error occurspublic int getFormat()
public String toString()
toString
in class Object
public static 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: byte[] pem_array = Util.Base64Encode(der_array);
public ASN1Object toASN1Object()
public void clearASN1Object()
public void clearByteArray()
public byte[] fingerprint()
|
This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note). | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |