8294987: Streamline DerOutputStream write

Reviewed-by: jnimeh, ascarpino
This commit is contained in:
Weijun Wang 2022-10-07 20:34:08 +00:00
parent 6a266c4de1
commit 823b53d895

View file

@ -26,8 +26,8 @@
package sun.security.util; package sun.security.util;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -79,10 +79,11 @@ extends ByteArrayOutputStream implements DerEncoder {
* <em>DerValue.tag_Sequence</em> * <em>DerValue.tag_Sequence</em>
* @param buf buffered data, which must be DER-encoded * @param buf buffered data, which must be DER-encoded
*/ */
public void write(byte tag, byte[] buf) throws IOException { public DerOutputStream write(byte tag, byte[] buf) throws IOException {
write(tag); write(tag);
putLength(buf.length); putLength(buf.length);
write(buf, 0, buf.length); write(buf, 0, buf.length);
return this;
} }
/** /**
@ -94,10 +95,11 @@ extends ByteArrayOutputStream implements DerEncoder {
* <em>DerValue.tag_Sequence</em> * <em>DerValue.tag_Sequence</em>
* @param out buffered data * @param out buffered data
*/ */
public void write(byte tag, DerOutputStream out) throws IOException { public DerOutputStream write(byte tag, DerOutputStream out) throws IOException {
write(tag); write(tag);
putLength(out.count); putLength(out.count);
write(out.buf, 0, out.count); write(out.buf, 0, out.count);
return this;
} }
/** /**
@ -117,17 +119,19 @@ extends ByteArrayOutputStream implements DerEncoder {
* explicit tagging the form is always constructed. * explicit tagging the form is always constructed.
* @param value original value being implicitly tagged * @param value original value being implicitly tagged
*/ */
public void writeImplicit(byte tag, DerOutputStream value) public DerOutputStream writeImplicit(byte tag, DerOutputStream value)
throws IOException { throws IOException {
write(tag); write(tag);
write(value.buf, 1, value.count-1); write(value.buf, 1, value.count-1);
return this;
} }
/** /**
* Marshals pre-encoded DER value onto the output stream. * Marshals pre-encoded DER value onto the output stream.
*/ */
public void putDerValue(DerValue val) throws IOException { public DerOutputStream putDerValue(DerValue val) throws IOException {
val.encode(this); val.encode(this);
return this;
} }
/* /*
@ -141,7 +145,7 @@ extends ByteArrayOutputStream implements DerEncoder {
/** /**
* Marshals a DER boolean on the output stream. * Marshals a DER boolean on the output stream.
*/ */
public void putBoolean(boolean val) throws IOException { public DerOutputStream putBoolean(boolean val) throws IOException {
write(DerValue.tag_Boolean); write(DerValue.tag_Boolean);
putLength(1); putLength(1);
if (val) { if (val) {
@ -149,15 +153,17 @@ extends ByteArrayOutputStream implements DerEncoder {
} else { } else {
write(0); write(0);
} }
return this;
} }
/** /**
* Marshals a DER enumerated on the output stream. * Marshals a DER enumerated on the output stream.
* @param i the enumerated value. * @param i the enumerated value.
*/ */
public void putEnumerated(int i) throws IOException { public DerOutputStream putEnumerated(int i) throws IOException {
write(DerValue.tag_Enumerated); write(DerValue.tag_Enumerated);
putIntegerContents(i); putIntegerContents(i);
return this;
} }
/** /**
@ -165,11 +171,12 @@ extends ByteArrayOutputStream implements DerEncoder {
* *
* @param i the integer in the form of a BigInteger. * @param i the integer in the form of a BigInteger.
*/ */
public void putInteger(BigInteger i) throws IOException { public DerOutputStream putInteger(BigInteger i) throws IOException {
write(DerValue.tag_Integer); write(DerValue.tag_Integer);
byte[] buf = i.toByteArray(); // least number of bytes byte[] buf = i.toByteArray(); // least number of bytes
putLength(buf.length); putLength(buf.length);
write(buf, 0, buf.length); write(buf, 0, buf.length);
return this;
} }
/** /**
@ -177,27 +184,29 @@ extends ByteArrayOutputStream implements DerEncoder {
* *
* @param buf the integer in bytes, equivalent to BigInteger::toByteArray. * @param buf the integer in bytes, equivalent to BigInteger::toByteArray.
*/ */
public void putInteger(byte[] buf) throws IOException { public DerOutputStream putInteger(byte[] buf) throws IOException {
write(DerValue.tag_Integer); write(DerValue.tag_Integer);
putLength(buf.length); putLength(buf.length);
write(buf, 0, buf.length); write(buf, 0, buf.length);
return this;
} }
/** /**
* Marshals a DER integer on the output stream. * Marshals a DER integer on the output stream.
* @param i the integer in the form of an Integer. * @param i the integer in the form of an Integer.
*/ */
public void putInteger(Integer i) throws IOException { public DerOutputStream putInteger(Integer i) throws IOException {
putInteger(i.intValue()); return putInteger(i.intValue());
} }
/** /**
* Marshals a DER integer on the output stream. * Marshals a DER integer on the output stream.
* @param i the integer. * @param i the integer.
*/ */
public void putInteger(int i) throws IOException { public DerOutputStream putInteger(int i) throws IOException {
write(DerValue.tag_Integer); write(DerValue.tag_Integer);
putIntegerContents(i); putIntegerContents(i);
return this;
} }
private void putIntegerContents(int i) throws IOException { private void putIntegerContents(int i) throws IOException {
@ -250,11 +259,12 @@ extends ByteArrayOutputStream implements DerEncoder {
* *
* @param bits the bit string, MSB first * @param bits the bit string, MSB first
*/ */
public void putBitString(byte[] bits) throws IOException { public DerOutputStream putBitString(byte[] bits) throws IOException {
write(DerValue.tag_BitString); write(DerValue.tag_BitString);
putLength(bits.length + 1); putLength(bits.length + 1);
write(0); // all of last octet is used write(0); // all of last octet is used
write(bits); write(bits);
return this;
} }
/** /**
@ -263,13 +273,14 @@ extends ByteArrayOutputStream implements DerEncoder {
* *
* @param ba the bit string, MSB first * @param ba the bit string, MSB first
*/ */
public void putUnalignedBitString(BitArray ba) throws IOException { public DerOutputStream putUnalignedBitString(BitArray ba) throws IOException {
byte[] bits = ba.toByteArray(); byte[] bits = ba.toByteArray();
write(DerValue.tag_BitString); write(DerValue.tag_BitString);
putLength(bits.length + 1); putLength(bits.length + 1);
write(bits.length*8 - ba.length()); // excess bits in last octet write(bits.length*8 - ba.length()); // excess bits in last octet
write(bits); write(bits);
return this;
} }
/** /**
@ -278,8 +289,8 @@ extends ByteArrayOutputStream implements DerEncoder {
* *
* @param ba the bit string, MSB first * @param ba the bit string, MSB first
*/ */
public void putTruncatedUnalignedBitString(BitArray ba) throws IOException { public DerOutputStream putTruncatedUnalignedBitString(BitArray ba) throws IOException {
putUnalignedBitString(ba.truncate()); return putUnalignedBitString(ba.truncate());
} }
/** /**
@ -287,25 +298,27 @@ extends ByteArrayOutputStream implements DerEncoder {
* *
* @param octets the octet string * @param octets the octet string
*/ */
public void putOctetString(byte[] octets) throws IOException { public DerOutputStream putOctetString(byte[] octets) throws IOException {
write(DerValue.tag_OctetString, octets); return write(DerValue.tag_OctetString, octets);
} }
/** /**
* Marshals a DER "null" value on the output stream. These are * Marshals a DER "null" value on the output stream. These are
* often used to indicate optional values which have been omitted. * often used to indicate optional values which have been omitted.
*/ */
public void putNull() throws IOException { public DerOutputStream putNull() throws IOException {
write(DerValue.tag_Null); write(DerValue.tag_Null);
putLength(0); putLength(0);
return this;
} }
/** /**
* Marshals an object identifier (OID) on the output stream. * Marshals an object identifier (OID) on the output stream.
* Corresponds to the ASN.1 "OBJECT IDENTIFIER" construct. * Corresponds to the ASN.1 "OBJECT IDENTIFIER" construct.
*/ */
public void putOID(ObjectIdentifier oid) throws IOException { public DerOutputStream putOID(ObjectIdentifier oid) throws IOException {
oid.encode(this); oid.encode(this);
return this;
} }
/** /**
@ -313,14 +326,14 @@ extends ByteArrayOutputStream implements DerEncoder {
* the ASN.1 "SEQUENCE" (zero to N values) and "SEQUENCE OF" * the ASN.1 "SEQUENCE" (zero to N values) and "SEQUENCE OF"
* (one to N values) constructs. * (one to N values) constructs.
*/ */
public void putSequence(DerValue[] seq) throws IOException { public DerOutputStream putSequence(DerValue[] seq) throws IOException {
DerOutputStream bytes = new DerOutputStream(); DerOutputStream bytes = new DerOutputStream();
int i; int i;
for (i = 0; i < seq.length; i++) for (i = 0; i < seq.length; i++)
seq[i].encode(bytes); seq[i].encode(bytes);
write(DerValue.tag_Sequence, bytes); return write(DerValue.tag_Sequence, bytes);
} }
/** /**
@ -330,14 +343,14 @@ extends ByteArrayOutputStream implements DerEncoder {
* *
* For DER encoding, use orderedPutSet() or orderedPutSetOf(). * For DER encoding, use orderedPutSet() or orderedPutSetOf().
*/ */
public void putSet(DerValue[] set) throws IOException { public DerOutputStream putSet(DerValue[] set) throws IOException {
DerOutputStream bytes = new DerOutputStream(); DerOutputStream bytes = new DerOutputStream();
int i; int i;
for (i = 0; i < set.length; i++) for (i = 0; i < set.length; i++)
set[i].encode(bytes); set[i].encode(bytes);
write(DerValue.tag_Set, bytes); return write(DerValue.tag_Set, bytes);
} }
/** /**
@ -350,8 +363,8 @@ extends ByteArrayOutputStream implements DerEncoder {
* This method supports the ASN.1 "SET OF" construct, but not * This method supports the ASN.1 "SET OF" construct, but not
* "SET", which uses a different order. * "SET", which uses a different order.
*/ */
public void putOrderedSetOf(byte tag, DerEncoder[] set) throws IOException { public DerOutputStream putOrderedSetOf(byte tag, DerEncoder[] set) throws IOException {
putOrderedSet(tag, set, lexOrder); return putOrderedSet(tag, set, lexOrder);
} }
/** /**
@ -364,8 +377,8 @@ extends ByteArrayOutputStream implements DerEncoder {
* This method supports the ASN.1 "SET" construct, but not * This method supports the ASN.1 "SET" construct, but not
* "SET OF", which uses a different order. * "SET OF", which uses a different order.
*/ */
public void putOrderedSet(byte tag, DerEncoder[] set) throws IOException { public DerOutputStream putOrderedSet(byte tag, DerEncoder[] set) throws IOException {
putOrderedSet(tag, set, tagOrder); return putOrderedSet(tag, set, tagOrder);
} }
/** /**
@ -386,7 +399,7 @@ extends ByteArrayOutputStream implements DerEncoder {
* *
* @param order the order to use when sorting encodings of components. * @param order the order to use when sorting encodings of components.
*/ */
private void putOrderedSet(byte tag, DerEncoder[] set, private DerOutputStream putOrderedSet(byte tag, DerEncoder[] set,
Comparator<byte[]> order) throws IOException { Comparator<byte[]> order) throws IOException {
DerOutputStream[] streams = new DerOutputStream[set.length]; DerOutputStream[] streams = new DerOutputStream[set.length];
@ -406,54 +419,53 @@ extends ByteArrayOutputStream implements DerEncoder {
for (int i = 0; i < streams.length; i++) { for (int i = 0; i < streams.length; i++) {
bytes.write(bufs[i]); bytes.write(bufs[i]);
} }
write(tag, bytes); return write(tag, bytes);
} }
/** /**
* Marshals a string as a DER encoded UTF8String. * Marshals a string as a DER encoded UTF8String.
*/ */
public void putUTF8String(String s) throws IOException { public DerOutputStream putUTF8String(String s) throws IOException {
writeString(s, DerValue.tag_UTF8String, UTF_8); return writeString(s, DerValue.tag_UTF8String, UTF_8);
} }
/** /**
* Marshals a string as a DER encoded PrintableString. * Marshals a string as a DER encoded PrintableString.
*/ */
public void putPrintableString(String s) throws IOException { public DerOutputStream putPrintableString(String s) throws IOException {
writeString(s, DerValue.tag_PrintableString, US_ASCII); return writeString(s, DerValue.tag_PrintableString, US_ASCII);
} }
/** /**
* Marshals a string as a DER encoded T61String. * Marshals a string as a DER encoded T61String.
*/ */
public void putT61String(String s) throws IOException { public DerOutputStream putT61String(String s) throws IOException {
/* /*
* Works for characters that are defined in both ASCII and * Works for characters that are defined in both ASCII and
* T61. * T61.
*/ */
writeString(s, DerValue.tag_T61String, ISO_8859_1); return writeString(s, DerValue.tag_T61String, ISO_8859_1);
} }
/** /**
* Marshals a string as a DER encoded IA5String. * Marshals a string as a DER encoded IA5String.
*/ */
public void putIA5String(String s) throws IOException { public DerOutputStream putIA5String(String s) throws IOException {
writeString(s, DerValue.tag_IA5String, US_ASCII); return writeString(s, DerValue.tag_IA5String, US_ASCII);
} }
/** /**
* Marshals a string as a DER encoded BMPString. * Marshals a string as a DER encoded BMPString.
*/ */
public void putBMPString(String s) throws IOException { public DerOutputStream putBMPString(String s) throws IOException {
writeString(s, DerValue.tag_BMPString, UTF_16BE); return writeString(s, DerValue.tag_BMPString, UTF_16BE);
} }
/** /**
* Marshals a string as a DER encoded GeneralString. * Marshals a string as a DER encoded GeneralString.
*/ */
public void putGeneralString(String s) throws IOException { public DerOutputStream putGeneralString(String s) throws IOException {
writeString(s, DerValue.tag_GeneralString, US_ASCII); return writeString(s, DerValue.tag_GeneralString, US_ASCII);
} }
/** /**
@ -464,13 +476,14 @@ extends ByteArrayOutputStream implements DerEncoder {
* @param charset the charset that should be used corresponding to * @param charset the charset that should be used corresponding to
* the above tag. * the above tag.
*/ */
private void writeString(String s, byte stringTag, Charset charset) private DerOutputStream writeString(String s, byte stringTag, Charset charset)
throws IOException { throws IOException {
byte[] data = s.getBytes(charset); byte[] data = s.getBytes(charset);
write(stringTag); write(stringTag);
putLength(data.length); putLength(data.length);
write(data); write(data);
return this;
} }
/** /**
@ -479,8 +492,8 @@ extends ByteArrayOutputStream implements DerEncoder {
* <P>YYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time * <P>YYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time
* and with seconds (even if seconds=0) as per RFC 5280. * and with seconds (even if seconds=0) as per RFC 5280.
*/ */
public void putUTCTime(Date d) throws IOException { public DerOutputStream putUTCTime(Date d) throws IOException {
putTime(d, DerValue.tag_UtcTime); return putTime(d, DerValue.tag_UtcTime);
} }
/** /**
@ -489,8 +502,8 @@ extends ByteArrayOutputStream implements DerEncoder {
* <P>YYYYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time * <P>YYYYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time
* and with seconds (even if seconds=0) as per RFC 5280. * and with seconds (even if seconds=0) as per RFC 5280.
*/ */
public void putGeneralizedTime(Date d) throws IOException { public DerOutputStream putGeneralizedTime(Date d) throws IOException {
putTime(d, DerValue.tag_GeneralizedTime); return putTime(d, DerValue.tag_GeneralizedTime);
} }
/** /**
@ -500,7 +513,7 @@ extends ByteArrayOutputStream implements DerEncoder {
* @param d the date to be marshalled * @param d the date to be marshalled
* @param tag the tag for UTC Time or Generalized Time * @param tag the tag for UTC Time or Generalized Time
*/ */
private void putTime(Date d, byte tag) throws IOException { private DerOutputStream putTime(Date d, byte tag) throws IOException {
/* /*
* Format the date. * Format the date.
@ -527,6 +540,7 @@ extends ByteArrayOutputStream implements DerEncoder {
write(tag); write(tag);
putLength(time.length); putLength(time.length);
write(time); write(time);
return this;
} }
/** /**
@ -563,23 +577,6 @@ extends ByteArrayOutputStream implements DerEncoder {
} }
} }
/**
* Put the tag of the attribute in the stream.
*
* @param tagClass the tag class type, one of UNIVERSAL, CONTEXT,
* APPLICATION or PRIVATE
* @param form if true, the value is constructed, otherwise it is
* primitive.
* @param val the tag value
*/
public void putTag(byte tagClass, boolean form, byte val) {
byte tag = (byte)(tagClass | val);
if (form) {
tag |= (byte)0x20;
}
write(tag);
}
/** /**
* Write the current contents of this <code>DerOutputStream</code> * Write the current contents of this <code>DerOutputStream</code>
* to an <code>OutputStream</code>. * to an <code>OutputStream</code>.
@ -590,6 +587,16 @@ extends ByteArrayOutputStream implements DerEncoder {
out.write(toByteArray()); out.write(toByteArray());
} }
/**
* Write a DerEncoder onto the output stream.
* @param encoder the DerEncoder
* @throws IOException on output error
*/
public DerOutputStream write(DerEncoder encoder) throws IOException {
encoder.derEncode(this);
return this;
}
byte[] buf() { byte[] buf() {
return buf; return buf;
} }