Class DerOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.ByteArrayOutputStream
-
- org.mozilla.jss.netscape.security.util.DerOutputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable,DerEncoder
public class DerOutputStream extends java.io.ByteArrayOutputStream implements DerEncoder
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.
-
-
Constructor Summary
Constructors Constructor Description DerOutputStream()Construct an DER output stream.DerOutputStream(int size)Construct an DER output stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidderEncode(java.io.OutputStream out)Write the current contents of thisDerOutputStreamto anOutputStream.byte[]getDateBytes(java.util.Date d, boolean UTC)voidputBitString(byte[] bits)Marshals a DER bit string on the output stream.voidputBMPString(java.lang.String s)Marshals a string which is consists of BMP (unicode) charactersvoidputBoolean(boolean val)Marshals a DER boolean on the output stream.voidputDerValue(DerValue val)Marshals pre-encoded DER value onto the output stream.voidputEnumerated(int i)Marshals a DER enumerated value on the output stream.voidputGeneralizedTime(java.util.Date d)Marshals a DER Generalized Time/date value.voidputGeneralString(java.lang.String s)voidputIA5String(java.lang.String s)Marshals a string which is consists of IA5(ASCII) charactersvoidputInteger(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.voidputPrintableString(java.lang.String s)Converts string to printable and writes to der output stream.voidputSequence(DerValue[] seq)Marshals a sequence on the output stream.voidputSet(byte tag, DerEncoder[] set)NSCP : Like putOrderSetOf, except not sorted.voidputSet(DerValue[] set)Marshals the contents of a set on the output stream without ordering the elements.voidputStringType(byte tag, java.lang.String s)voidputTag(byte tagClass, boolean form, byte val)Put the tag of the attribute in the stream.voidputUnalignedBitString(boolean[] bitString)Marshals a DER bit string on the output stream.voidputUnalignedBitString(byte[] bitString)Marshals a DER bit string on the output stream.voidputUnalignedBitString(BitArray ba)Marshals a DER bit string on the output stream.voidputUnsignedInteger(byte[] integerBytes)Marshals a DER unsigned integer on the output stream.voidputUTCTime(java.util.Date d)Marshals a DER UTC time/date value.voidputUTF8String(java.lang.String s)voidputVisibleString(java.lang.String s)voidwrite(byte tag, byte[] buf)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.
-
-
-
Method Detail
-
write
public void write(byte tag, byte[] buf) throws java.io.IOExceptionWrites 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_Sequencebuf- buffered data, which must be DER-encoded- Throws:
java.io.IOException
-
write
public void write(byte tag, DerOutputStream out) throws java.io.IOExceptionWrites 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- Throws:
java.io.IOException
-
writeImplicit
public void writeImplicit(byte tag, DerOutputStream value) throws java.io.IOExceptionWrites 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- Throws:
java.io.IOException
-
putDerValue
public void putDerValue(DerValue val) throws java.io.IOException
Marshals pre-encoded DER value onto the output stream.- Throws:
java.io.IOException
-
putBoolean
public void putBoolean(boolean val) throws java.io.IOExceptionMarshals a DER boolean on the output stream.- Throws:
java.io.IOException
-
putInteger
public void putInteger(BigInt i) throws java.io.IOException
Marshals a DER unsigned integer on the output stream.- Throws:
java.io.IOException
-
putUnsignedInteger
public void putUnsignedInteger(byte[] integerBytes) throws java.io.IOExceptionMarshals a DER unsigned integer on the output stream.- Throws:
java.io.IOException
-
putEnumerated
public void putEnumerated(int i) throws java.io.IOExceptionMarshals a DER enumerated value on the output stream.- Throws:
java.io.IOException
-
putBitString
public void putBitString(byte[] bits) throws java.io.IOExceptionMarshals a DER bit string on the output stream. The bit string must be byte-aligned.- Parameters:
bits- the bit string, MSB first- Throws:
java.io.IOException
-
putUnalignedBitString
public void putUnalignedBitString(BitArray ba) throws java.io.IOException
Marshals a DER bit string on the output stream. The bit strings need not be byte-aligned.- Parameters:
ba- the bit string, MSB first- Throws:
java.io.IOException
-
putUnalignedBitString
public void putUnalignedBitString(byte[] bitString) throws java.io.IOExceptionMarshals a DER bit string on the output stream. All trailing 0 bits will be stripped off in accordance with DER encoding.- Parameters:
bitString- the bit string, MSB first- Throws:
java.io.IOException
-
putUnalignedBitString
public void putUnalignedBitString(boolean[] bitString) throws java.io.IOExceptionMarshals a DER bit string on the output stream. All trailing 0 bits will be stripped off in accordance with DER encoding.- Parameters:
bitString- the bit string as an array of booleans.- Throws:
java.io.IOException
-
putOctetString
public void putOctetString(byte[] octets) throws java.io.IOExceptionDER-encodes an ASN.1 OCTET STRING value on the output stream.- Parameters:
octets- the octet string- Throws:
java.io.IOException
-
putNull
public void putNull() throws java.io.IOExceptionMarshals a DER "null" value on the output stream. These are often used to indicate optional values which have been omitted.- Throws:
java.io.IOException
-
putOID
public void putOID(ObjectIdentifier oid) throws java.io.IOException
Marshals an object identifier (OID) on the output stream. Corresponds to the ASN.1 "OBJECT IDENTIFIER" construct.- Throws:
java.io.IOException
-
putSequence
public void putSequence(DerValue[] seq) throws java.io.IOException
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.- Throws:
java.io.IOException
-
putSet
public void putSet(DerValue[] set) throws java.io.IOException
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().- Throws:
java.io.IOException
-
putSet
public void putSet(byte tag, DerEncoder[] set) throws java.io.IOExceptionNSCP : Like putOrderSetOf, except not sorted. This may defy DER encoding but is needed for compatibility with communicator.- Throws:
java.io.IOException
-
putOrderedSetOf
public void putOrderedSetOf(byte tag, DerEncoder[] set) throws java.io.IOExceptionMarshals 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.- Throws:
java.io.IOException
-
putOrderedSet
public void putOrderedSet(byte tag, DerEncoder[] set) throws java.io.IOExceptionMarshals 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.- Throws:
java.io.IOException
-
putPrintableString
public void putPrintableString(java.lang.String s) throws java.io.IOExceptionConverts string to printable and writes to der output stream.- Throws:
java.io.IOException
-
putVisibleString
public void putVisibleString(java.lang.String s) throws java.io.IOException- Throws:
java.io.IOException
-
putBMPString
public void putBMPString(java.lang.String s) throws java.io.IOExceptionMarshals a string which is consists of BMP (unicode) characters- Throws:
java.io.IOException
-
putGeneralString
public void putGeneralString(java.lang.String s) throws java.io.IOException- Throws:
java.io.IOException
-
putIA5String
public void putIA5String(java.lang.String s) throws java.io.IOExceptionMarshals a string which is consists of IA5(ASCII) characters- Throws:
java.io.IOException
-
putUTF8String
public void putUTF8String(java.lang.String s) throws java.io.IOException- Throws:
java.io.IOException
-
putStringType
public void putStringType(byte tag, java.lang.String s) throws java.io.IOException- Throws:
java.io.IOException
-
getDateBytes
public byte[] getDateBytes(java.util.Date d, boolean UTC)
-
putUTCTime
public void putUTCTime(java.util.Date d) throws java.io.IOExceptionMarshals a DER UTC time/date value.YYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time and with seconds (even if seconds=0) as per IETF-PKIX partI.
- Throws:
java.io.IOException
-
putGeneralizedTime
public void putGeneralizedTime(java.util.Date d) throws java.io.IOExceptionMarshals a DER Generalized Time/date value.YYYYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time and with seconds (even if seconds=0) as per IETF-PKIX partI.
- Throws:
java.io.IOException
-
putLength
public void putLength(int len) throws java.io.IOExceptionPut the encoding of the length in the stream.- Parameters:
len- the length of the attribute.- Throws:
java.io.IOException- on writing errors.
-
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
public void derEncode(java.io.OutputStream out) throws java.io.IOExceptionWrite the current contents of thisDerOutputStreamto anOutputStream.- Specified by:
derEncodein interfaceDerEncoder- Parameters:
out- the stream on which the DER encoding is written.- Throws:
java.io.IOException- on output error.
-
-