Class DEROutputStream
java.lang.Object
java.io.OutputStream
java.io.ByteArrayOutputStream
com.github.toolarium.security.pki.impl.rsa.der.DEROutputStream
- All Implemented Interfaces:
DEREncoder,Closeable,Flushable,AutoCloseable
Output stream marshaling DER-encoded data. This is eventually provided in the form of a byte array; there is no advance limit on the size of
that byte array.
At this time, this class supports only a subset of the types of DER data encodings which are defined. That subset is sufficient for generating most X.509 certificates.
-
Field Summary
Fields inherited from class java.io.ByteArrayOutputStream
buf, count -
Constructor Summary
ConstructorsConstructorDescriptionConstruct an DER output stream.DEROutputStream(int size) Construct an DER output stream. -
Method Summary
Modifier and TypeMethodDescriptionvoidderEncode(OutputStream out) Write the current contents of thisDerOutputStreamto anOutputStream.voidputBitString(byte[] bits) Marshals a DER bit string on the output stream.voidMarshals a string as a DER encoded BMPString.voidputBoolean(boolean val) Marshals a DER boolean on the output stream.voidputDERValue(DERValue val) Marshals pre-encoded DER value onto the output stream.voidMarshals a DER enumerated on the output stream.voidMarshals a DER Generalized Time/date value.voidMarshals a string as a DER encoded IA5String.voidputInteger(BigInt i) Marshals a DER unsigned integer on the output stream.voidputLength(int len) Put the encoding of the length in the stream.voidputNull()Marshals a DER "null" value on the output stream.voidputOctetString(byte[] octets) DER-encodes an ASN.1 OCTET STRING value on the output stream.voidputOID(ObjectIdentifier oid) Marshals an object identifier (OID) on the output stream.voidputOrderedSet(byte tag, DEREncoder[] set) Marshals the contents of a set on the output stream.voidputOrderedSetOf(byte tag, DEREncoder[] set) Marshals the contents of a set on the output stream.voidMarshals a string as a DER encoded PrintableString.voidputSequence(DERValue[] seq) Marshals a sequence on the output stream.voidMarshals the contents of a set on the output stream without ordering the elements.voidMarshals a string as a DER encoded T61String.voidputTag(byte tagClass, boolean form, byte val) Put the tag of the attribute in the stream.voidMarshals a DER bit string on the output stream.voidputUTCTime(Date d) Marshals a DER UTC time/date value.voidMarshals a string as a DER encoded UTF8String.voidwrite(byte tag, byte[] b) Writes tagged, pre-marshaled data.voidwrite(byte tag, DEROutputStream out) Writes tagged data using buffer-to-buffer copy.voidwriteImplicit(byte tag, DEROutputStream value) Writes implicitly tagged data using buffer-to-buffer copy.Methods inherited from class java.io.ByteArrayOutputStream
close, reset, size, toByteArray, toString, toString, toString, toString, write, write, writeBytes, writeToMethods inherited from class java.io.OutputStream
flush, nullOutputStream, write
-
Constructor Details
-
DEROutputStream
public DEROutputStream(int size) Construct an DER output stream.- Parameters:
size- how large a buffer to preallocate.
-
DEROutputStream
public DEROutputStream()Construct an DER output stream.
-
-
Method Details
-
write
public void write(byte tag, byte[] b) Writes tagged, pre-marshaled data. This calcuates and encodes the length, so that the output data is the standard triple of { tag, length, data } used by all DER values.- Parameters:
tag- the DER value tag for the data, such as DerValue.tag_Sequenceb- buffered data, which must be DER-encoded
-
write
Writes tagged data using buffer-to-buffer copy. As above, this writes a standard DER record. This is often used when efficiently encapsulating values in sequences.- Parameters:
tag- the DER value tag for the data, such as DERValue.tag_Sequenceout- buffered data
-
writeImplicit
Writes implicitly tagged data using buffer-to-buffer copy. As above, this writes a standard DER record. This is often used when efficiently encapsulating implicitly tagged values.- Parameters:
tag- the DER value of the context-specific tag that replaces original tag of the value in the output, such as in [field] [N] IMPLICIT [type] For example, FooLength [1] IMPLICIT INTEGER, with value=4; would be encoded as "81 01 04" whereas in explicit tagging it would be encoded as "A1 03 02 01 04". Notice that the tag is A1 and not 81, this is because with explicit tagging the form is always constructed.value- original value being implicitly tagged
-
putDERValue
Marshals pre-encoded DER value onto the output stream.- Parameters:
val- the value- Throws:
IOException- in case of error
-
putBoolean
public void putBoolean(boolean val) Marshals a DER boolean on the output stream.- Parameters:
val- the value
-
putInteger
Marshals a DER unsigned integer on the output stream.- Parameters:
i- the value
-
putEnumerated
Marshals a DER enumerated on the output stream.- Parameters:
i- the value
-
putBitString
Marshals a DER bit string on the output stream. The bit string must be byte-aligned.- Parameters:
bits- the bit string, MSB first- Throws:
IOException- in case of error
-
putUnalignedBitString
Marshals a DER bit string on the output stream. The bit strings need not be byte-aligned.- Parameters:
b- the bit string, MSB first- Throws:
IOException- in case of error
-
putOctetString
public void putOctetString(byte[] octets) DER-encodes an ASN.1 OCTET STRING value on the output stream.- Parameters:
octets- the octet string
-
putNull
public void putNull()Marshals a DER "null" value on the output stream. These are often used to indicate optional values which have been omitted. -
putOID
Marshals an object identifier (OID) on the output stream. Corresponds to the ASN.1 "OBJECT IDENTIFIER" construct.- Parameters:
oid- the id
-
putSequence
Marshals a sequence on the output stream. This supports both the ASN.1 "SEQUENCE" (zero to N values) and "SEQUENCE OF" (one to N values) constructs.- Parameters:
seq- the sequence- Throws:
IOException- in case of error
-
putSet
Marshals the contents of a set on the output stream without ordering the elements. Ok for BER encoding, but not for DER encoding. For DER encoding, use orderedPutSet() or orderedPutSetOf().- Parameters:
set- the values- Throws:
IOException- in case of error
-
putOrderedSetOf
Marshals the contents of a set on the output stream. Sets are semantically unordered, but DER requires that encodings of set elements be sorted into ascending lexicographical order before being output. Hence sets with the same tags and elements have the same DER encoding. This method supports the ASN.1 "SET OF" construct, but not "SET", which uses a different order.- Parameters:
tag- the tagset- the set- Throws:
IOException- in case of error
-
putOrderedSet
Marshals the contents of a set on the output stream. Sets are semantically unordered, but DER requires that encodings of set elements be sorted into ascending tag order before being output. Hence sets with the same tags and elements have the same DER encoding. This method supports the ASN.1 "SET" construct, but not "SET OF", which uses a different order.- Parameters:
tag- the tagset- the set- Throws:
IOException- in case of error
-
putUTF8String
Marshals a string as a DER encoded UTF8String.- Parameters:
s- the string- Throws:
IOException- in case of error
-
putPrintableString
Marshals a string as a DER encoded PrintableString.- Parameters:
s- the string- Throws:
IOException- in case of error
-
putT61String
Marshals a string as a DER encoded T61String.- Parameters:
s- the string- Throws:
IOException- in case of error
-
putIA5String
Marshals a string as a DER encoded IA5String.- Parameters:
s- the string- Throws:
IOException- in case of error
-
putBMPString
Marshals a string as a DER encoded BMPString.- Parameters:
s- the string- Throws:
IOException- in case of error
-
putUTCTime
Marshals a DER UTC time/date value.YYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time and with seconds (even if seconds=0) as per RFC 2459.
- Parameters:
d- the date- Throws:
IOException- in case of error
-
putGeneralizedTime
Marshals a DER Generalized Time/date value.YYYYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time and with seconds (even if seconds=0) as per RFC 2459.
- Parameters:
d- the date- Throws:
IOException- in case of error
-
putLength
public void putLength(int len) Put the encoding of the length in the stream.- Parameters:
len- the length of the attribute.
-
putTag
public void putTag(byte tagClass, boolean form, byte val) Put the tag of the attribute in the stream.- Parameters:
tagClass- the tag class type, one of UNIVERSAL, CONTEXT, APPLICATION or PRIVATEform- if true, the value is constructed, otherwise it is primitive.val- the tag value
-
derEncode
Write the current contents of thisDerOutputStreamto anOutputStream.- Specified by:
derEncodein interfaceDEREncoder- Parameters:
out- the stream- Throws:
IOException- on output error.
-