Class DerOutputStream
java.lang.Object
java.io.OutputStream
java.io.ByteArrayOutputStream
org.mozilla.jss.netscape.security.util.DerOutputStream
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
,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.
- Version:
- 1.32
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Calendar
private static ByteArrayLexOrder
Lexicographical order comparison on byte arrays, for ordering elements of a SET OF objects in DER encoding.private static ByteArrayTagOrder
Tag order comparison on byte arrays, for ordering elements of SET objects in DER encoding.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 TypeMethodDescriptionvoid
derEncode
(OutputStream out) Write the current contents of thisDerOutputStream
to anOutputStream
.byte[]
getDateBytes
(Date d, boolean UTC) private Calendar
private void
put2DateBytes
(byte[] buffer, int value, int offset) void
putBitString
(byte[] bits) Marshals a DER bit string on the output stream.void
Marshals a string which is consists of BMP (unicode) charactersvoid
putBoolean
(boolean val) Marshals a DER boolean on the output stream.void
putDerValue
(DerValue val) Marshals pre-encoded DER value onto the output stream.void
putEnumerated
(int i) Marshals a DER enumerated value on the output stream.void
Marshals a DER Generalized Time/date value.void
void
Marshals a string which is consists of IA5(ASCII) charactersvoid
putInteger
(BigInt i) Marshals a DER unsigned integer on the output stream.void
putLength
(int len) Put the encoding of the length in the stream.void
putNull()
Marshals a DER "null" value on the output stream.void
putOctetString
(byte[] octets) DER-encodes an ASN.1 OCTET STRING value on the output stream.void
putOID
(ObjectIdentifier oid) Marshals an object identifier (OID) on the output stream.void
putOrderedSet
(byte tag, DerEncoder[] set) Marshals the contents of a set on the output stream.private void
putOrderedSet
(byte tag, DerEncoder[] set, Comparator<byte[]> order) Marshals a the contents of a set on the output stream with the encodings of its sorted in increasing order.void
putOrderedSetOf
(byte tag, DerEncoder[] set) Marshals the contents of a set on the output stream.void
Converts string to printable and writes to der output stream.void
putSequence
(DerValue[] seq) Marshals a sequence on the output stream.void
putSet
(byte tag, DerEncoder[] set) NSCP : Like putOrderSetOf, except not sorted.void
Marshals the contents of a set on the output stream without ordering the elements.void
putStringType
(byte tag, String s) void
putTag
(byte tagClass, boolean form, byte val) Put the tag of the attribute in the stream.void
putUnalignedBitString
(boolean[] bitString) Marshals a DER bit string on the output stream.void
putUnalignedBitString
(byte[] bitString) Marshals a DER bit string on the output stream.void
Marshals a DER bit string on the output stream.void
putUnsignedInteger
(byte[] integerBytes) Marshals a DER unsigned integer on the output stream.void
putUTCTime
(Date d) Marshals a DER UTC time/date value.void
void
private static BitArray
toBitArray
(boolean[] bitString) Converts a boolean array to a BitArray.private static BitArray
toBitArray
(byte[] bitString) Converts bit string to a BitArray, stripping off trailing 0 bits.void
write
(byte tag, byte[] buf) Writes tagged, pre-marshaled data.void
write
(byte tag, DerOutputStream out) Writes tagged data using buffer-to-buffer copy.void
writeImplicit
(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, writeTo
Methods inherited from class java.io.OutputStream
flush, nullOutputStream, write
-
Field Details
-
lexOrder
Lexicographical order comparison on byte arrays, for ordering elements of a SET OF objects in DER encoding. -
tagOrder
Tag order comparison on byte arrays, for ordering elements of SET objects in DER encoding. -
gmtGregorianCalendar
-
-
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
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_Sequencebuf
- buffered data, which must be DER-encoded- Throws:
IOException
-
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- Throws:
IOException
-
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- Throws:
IOException
-
putDerValue
Marshals pre-encoded DER value onto the output stream.- Throws:
IOException
-
putBoolean
Marshals a DER boolean on the output stream.- Throws:
IOException
-
putInteger
Marshals a DER unsigned integer on the output stream.- Throws:
IOException
-
putUnsignedInteger
Marshals a DER unsigned integer on the output stream.- Throws:
IOException
-
putEnumerated
Marshals a DER enumerated value on the output stream.- Throws:
IOException
-
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
-
toBitArray
Converts a boolean array to a BitArray. Trims trailing 0 bits in accordance with DER encoding standard. We assume the input is not null. -
toBitArray
Converts bit string to a BitArray, stripping off trailing 0 bits. We assume that the bit string is not null. -
putUnalignedBitString
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:
IOException
-
putUnalignedBitString
Marshals 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:
IOException
-
putUnalignedBitString
Marshals 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:
IOException
-
putOctetString
DER-encodes an ASN.1 OCTET STRING value on the output stream.- Parameters:
octets
- the octet string- Throws:
IOException
-
putNull
Marshals a DER "null" value on the output stream. These are often used to indicate optional values which have been omitted.- Throws:
IOException
-
putOID
Marshals an object identifier (OID) on the output stream. Corresponds to the ASN.1 "OBJECT IDENTIFIER" construct.- Throws:
IOException
-
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.- Throws:
IOException
-
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().- Throws:
IOException
-
putSet
NSCP : Like putOrderSetOf, except not sorted. This may defy DER encoding but is needed for compatibility with communicator.- Throws:
IOException
-
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.- Throws:
IOException
-
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.- Throws:
IOException
-
putOrderedSet
Marshals a the contents of a set on the output stream with the encodings of its sorted in increasing order.- Parameters:
order
- the order to use when sorting encodings of components.- Throws:
IOException
-
putPrintableString
Converts string to printable and writes to der output stream.- Throws:
IOException
-
putVisibleString
- Throws:
IOException
-
putBMPString
Marshals a string which is consists of BMP (unicode) characters- Throws:
IOException
-
putGeneralString
- Throws:
IOException
-
putIA5String
Marshals a string which is consists of IA5(ASCII) characters- Throws:
IOException
-
putUTF8String
- Throws:
IOException
-
putStringType
- Throws:
IOException
-
put2DateBytes
private void put2DateBytes(byte[] buffer, int value, int offset) -
getGMTGregorianCalendar
-
getDateBytes
-
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 IETF-PKIX partI.
- Throws:
IOException
-
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 IETF-PKIX partI.
- Throws:
IOException
-
putLength
Put the encoding of the length in the stream.- Parameters:
len
- the length of the attribute.- Throws:
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
Write the current contents of thisDerOutputStream
to anOutputStream
.- Specified by:
derEncode
in interfaceDerEncoder
- Parameters:
out
- the stream on which the DER encoding is written.- Throws:
IOException
- on output error.
-