mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
8297065: DerOutputStream operations should not throw IOExceptions
Reviewed-by: mullan, valeriep
This commit is contained in:
parent
d83a07b72c
commit
2deb318c9f
109 changed files with 725 additions and 1112 deletions
|
@ -25,8 +25,6 @@
|
|||
|
||||
package sun.security.util;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Interface to an object that knows how to write its own DER
|
||||
* encoding to an output stream.
|
||||
|
@ -40,7 +38,6 @@ public interface DerEncoder {
|
|||
*
|
||||
* @param out the stream on which the DER encoding is written.
|
||||
*/
|
||||
void encode(DerOutputStream out)
|
||||
throws IOException;
|
||||
void encode(DerOutputStream out);
|
||||
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
package sun.security.util;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.Charset;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
@ -55,8 +54,8 @@ import static java.nio.charset.StandardCharsets.UTF_8;
|
|||
* @author Amit Kapoor
|
||||
* @author Hemma Prafullchandra
|
||||
*/
|
||||
public class DerOutputStream
|
||||
extends ByteArrayOutputStream implements DerEncoder {
|
||||
public final class DerOutputStream
|
||||
extends ByteArrayOutputStream implements DerEncoder {
|
||||
/**
|
||||
* Construct a DER output stream.
|
||||
*
|
||||
|
@ -78,10 +77,10 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* <em>DerValue.tag_Sequence</em>
|
||||
* @param buf buffered data, which must be DER-encoded
|
||||
*/
|
||||
public DerOutputStream write(byte tag, byte[] buf) throws IOException {
|
||||
public DerOutputStream write(byte tag, byte[] buf) {
|
||||
write(tag);
|
||||
putLength(buf.length);
|
||||
write(buf, 0, buf.length);
|
||||
writeBytes(buf);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -94,7 +93,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* <em>DerValue.tag_Sequence</em>
|
||||
* @param out buffered data
|
||||
*/
|
||||
public DerOutputStream write(byte tag, DerOutputStream out) throws IOException {
|
||||
public DerOutputStream write(byte tag, DerOutputStream out) {
|
||||
write(tag);
|
||||
putLength(out.count);
|
||||
write(out.buf, 0, out.count);
|
||||
|
@ -118,8 +117,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* explicit tagging the form is always constructed.
|
||||
* @param value original value being implicitly tagged
|
||||
*/
|
||||
public DerOutputStream writeImplicit(byte tag, DerOutputStream value)
|
||||
throws IOException {
|
||||
public DerOutputStream writeImplicit(byte tag, DerOutputStream value) {
|
||||
write(tag);
|
||||
write(value.buf, 1, value.count-1);
|
||||
return this;
|
||||
|
@ -128,7 +126,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
/**
|
||||
* Marshals pre-encoded DER value onto the output stream.
|
||||
*/
|
||||
public DerOutputStream putDerValue(DerValue val) throws IOException {
|
||||
public DerOutputStream putDerValue(DerValue val) {
|
||||
val.encode(this);
|
||||
return this;
|
||||
}
|
||||
|
@ -144,7 +142,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
/**
|
||||
* Marshals a DER boolean on the output stream.
|
||||
*/
|
||||
public DerOutputStream putBoolean(boolean val) throws IOException {
|
||||
public DerOutputStream putBoolean(boolean val) {
|
||||
write(DerValue.tag_Boolean);
|
||||
putLength(1);
|
||||
if (val) {
|
||||
|
@ -159,7 +157,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* Marshals a DER enumerated on the output stream.
|
||||
* @param i the enumerated value.
|
||||
*/
|
||||
public DerOutputStream putEnumerated(int i) throws IOException {
|
||||
public DerOutputStream putEnumerated(int i) {
|
||||
write(DerValue.tag_Enumerated);
|
||||
putIntegerContents(i);
|
||||
return this;
|
||||
|
@ -170,11 +168,11 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
*
|
||||
* @param i the integer in the form of a BigInteger.
|
||||
*/
|
||||
public DerOutputStream putInteger(BigInteger i) throws IOException {
|
||||
public DerOutputStream putInteger(BigInteger i) {
|
||||
write(DerValue.tag_Integer);
|
||||
byte[] buf = i.toByteArray(); // least number of bytes
|
||||
putLength(buf.length);
|
||||
write(buf, 0, buf.length);
|
||||
writeBytes(buf);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -183,10 +181,10 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
*
|
||||
* @param buf the integer in bytes, equivalent to BigInteger::toByteArray.
|
||||
*/
|
||||
public DerOutputStream putInteger(byte[] buf) throws IOException {
|
||||
public DerOutputStream putInteger(byte[] buf) {
|
||||
write(DerValue.tag_Integer);
|
||||
putLength(buf.length);
|
||||
write(buf, 0, buf.length);
|
||||
writeBytes(buf);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -194,7 +192,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* Marshals a DER integer on the output stream.
|
||||
* @param i the integer in the form of an Integer.
|
||||
*/
|
||||
public DerOutputStream putInteger(Integer i) throws IOException {
|
||||
public DerOutputStream putInteger(Integer i) {
|
||||
return putInteger(i.intValue());
|
||||
}
|
||||
|
||||
|
@ -202,13 +200,13 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* Marshals a DER integer on the output stream.
|
||||
* @param i the integer.
|
||||
*/
|
||||
public DerOutputStream putInteger(int i) throws IOException {
|
||||
public DerOutputStream putInteger(int i) {
|
||||
write(DerValue.tag_Integer);
|
||||
putIntegerContents(i);
|
||||
return this;
|
||||
}
|
||||
|
||||
private void putIntegerContents(int i) throws IOException {
|
||||
private void putIntegerContents(int i) {
|
||||
|
||||
byte[] bytes = new byte[4];
|
||||
int start = 0;
|
||||
|
@ -258,11 +256,11 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
*
|
||||
* @param bits the bit string, MSB first
|
||||
*/
|
||||
public DerOutputStream putBitString(byte[] bits) throws IOException {
|
||||
public DerOutputStream putBitString(byte[] bits) {
|
||||
write(DerValue.tag_BitString);
|
||||
putLength(bits.length + 1);
|
||||
write(0); // all of last octet is used
|
||||
write(bits);
|
||||
writeBytes(bits);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -272,13 +270,13 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
*
|
||||
* @param ba the bit string, MSB first
|
||||
*/
|
||||
public DerOutputStream putUnalignedBitString(BitArray ba) throws IOException {
|
||||
public DerOutputStream putUnalignedBitString(BitArray ba) {
|
||||
byte[] bits = ba.toByteArray();
|
||||
|
||||
write(DerValue.tag_BitString);
|
||||
putLength(bits.length + 1);
|
||||
write(bits.length*8 - ba.length()); // excess bits in last octet
|
||||
write(bits);
|
||||
writeBytes(bits);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -288,7 +286,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
*
|
||||
* @param ba the bit string, MSB first
|
||||
*/
|
||||
public DerOutputStream putTruncatedUnalignedBitString(BitArray ba) throws IOException {
|
||||
public DerOutputStream putTruncatedUnalignedBitString(BitArray ba) {
|
||||
return putUnalignedBitString(ba.truncate());
|
||||
}
|
||||
|
||||
|
@ -297,7 +295,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
*
|
||||
* @param octets the octet string
|
||||
*/
|
||||
public DerOutputStream putOctetString(byte[] octets) throws IOException {
|
||||
public DerOutputStream putOctetString(byte[] octets) {
|
||||
return write(DerValue.tag_OctetString, octets);
|
||||
}
|
||||
|
||||
|
@ -305,7 +303,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* Marshals a DER "null" value on the output stream. These are
|
||||
* often used to indicate optional values which have been omitted.
|
||||
*/
|
||||
public DerOutputStream putNull() throws IOException {
|
||||
public DerOutputStream putNull() {
|
||||
write(DerValue.tag_Null);
|
||||
putLength(0);
|
||||
return this;
|
||||
|
@ -315,7 +313,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* Marshals an object identifier (OID) on the output stream.
|
||||
* Corresponds to the ASN.1 "OBJECT IDENTIFIER" construct.
|
||||
*/
|
||||
public DerOutputStream putOID(ObjectIdentifier oid) throws IOException {
|
||||
public DerOutputStream putOID(ObjectIdentifier oid) {
|
||||
oid.encode(this);
|
||||
return this;
|
||||
}
|
||||
|
@ -325,7 +323,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* the ASN.1 "SEQUENCE" (zero to N values) and "SEQUENCE OF"
|
||||
* (one to N values) constructs.
|
||||
*/
|
||||
public DerOutputStream putSequence(DerValue[] seq) throws IOException {
|
||||
public DerOutputStream putSequence(DerValue[] seq) {
|
||||
DerOutputStream bytes = new DerOutputStream();
|
||||
int i;
|
||||
|
||||
|
@ -342,7 +340,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
*
|
||||
* For DER encoding, use orderedPutSet() or orderedPutSetOf().
|
||||
*/
|
||||
public DerOutputStream putSet(DerValue[] set) throws IOException {
|
||||
public DerOutputStream putSet(DerValue[] set) {
|
||||
DerOutputStream bytes = new DerOutputStream();
|
||||
int i;
|
||||
|
||||
|
@ -362,7 +360,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* This method supports the ASN.1 "SET OF" construct, but not
|
||||
* "SET", which uses a different order.
|
||||
*/
|
||||
public DerOutputStream putOrderedSetOf(byte tag, DerEncoder[] set) throws IOException {
|
||||
public DerOutputStream putOrderedSetOf(byte tag, DerEncoder[] set) {
|
||||
return putOrderedSet(tag, set, lexOrder);
|
||||
}
|
||||
|
||||
|
@ -376,7 +374,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* This method supports the ASN.1 "SET" construct, but not
|
||||
* "SET OF", which uses a different order.
|
||||
*/
|
||||
public DerOutputStream putOrderedSet(byte tag, DerEncoder[] set) throws IOException {
|
||||
public DerOutputStream putOrderedSet(byte tag, DerEncoder[] set) {
|
||||
return putOrderedSet(tag, set, tagOrder);
|
||||
}
|
||||
|
||||
|
@ -399,7 +397,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* @param order the order to use when sorting encodings of components.
|
||||
*/
|
||||
private DerOutputStream putOrderedSet(byte tag, DerEncoder[] set,
|
||||
Comparator<byte[]> order) throws IOException {
|
||||
Comparator<byte[]> order) {
|
||||
DerOutputStream[] streams = new DerOutputStream[set.length];
|
||||
|
||||
for (int i = 0; i < set.length; i++) {
|
||||
|
@ -416,7 +414,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
|
||||
DerOutputStream bytes = new DerOutputStream();
|
||||
for (int i = 0; i < streams.length; i++) {
|
||||
bytes.write(bufs[i]);
|
||||
bytes.writeBytes(bufs[i]);
|
||||
}
|
||||
return write(tag, bytes);
|
||||
}
|
||||
|
@ -424,21 +422,21 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
/**
|
||||
* Marshals a string as a DER encoded UTF8String.
|
||||
*/
|
||||
public DerOutputStream putUTF8String(String s) throws IOException {
|
||||
public DerOutputStream putUTF8String(String s) {
|
||||
return writeString(s, DerValue.tag_UTF8String, UTF_8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marshals a string as a DER encoded PrintableString.
|
||||
*/
|
||||
public DerOutputStream putPrintableString(String s) throws IOException {
|
||||
public DerOutputStream putPrintableString(String s) {
|
||||
return writeString(s, DerValue.tag_PrintableString, US_ASCII);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marshals a string as a DER encoded T61String.
|
||||
*/
|
||||
public DerOutputStream putT61String(String s) throws IOException {
|
||||
public DerOutputStream putT61String(String s) {
|
||||
/*
|
||||
* Works for characters that are defined in both ASCII and
|
||||
* T61.
|
||||
|
@ -449,21 +447,21 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
/**
|
||||
* Marshals a string as a DER encoded IA5String.
|
||||
*/
|
||||
public DerOutputStream putIA5String(String s) throws IOException {
|
||||
public DerOutputStream putIA5String(String s) {
|
||||
return writeString(s, DerValue.tag_IA5String, US_ASCII);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marshals a string as a DER encoded BMPString.
|
||||
*/
|
||||
public DerOutputStream putBMPString(String s) throws IOException {
|
||||
public DerOutputStream putBMPString(String s) {
|
||||
return writeString(s, DerValue.tag_BMPString, UTF_16BE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marshals a string as a DER encoded GeneralString.
|
||||
*/
|
||||
public DerOutputStream putGeneralString(String s) throws IOException {
|
||||
public DerOutputStream putGeneralString(String s) {
|
||||
return writeString(s, DerValue.tag_GeneralString, US_ASCII);
|
||||
}
|
||||
|
||||
|
@ -475,13 +473,12 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* @param charset the charset that should be used corresponding to
|
||||
* the above tag.
|
||||
*/
|
||||
private DerOutputStream writeString(String s, byte stringTag, Charset charset)
|
||||
throws IOException {
|
||||
private DerOutputStream writeString(String s, byte stringTag, Charset charset) {
|
||||
|
||||
byte[] data = s.getBytes(charset);
|
||||
write(stringTag);
|
||||
putLength(data.length);
|
||||
write(data);
|
||||
writeBytes(data);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -491,7 +488,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* <P>YYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time
|
||||
* and with seconds (even if seconds=0) as per RFC 5280.
|
||||
*/
|
||||
public DerOutputStream putUTCTime(Date d) throws IOException {
|
||||
public DerOutputStream putUTCTime(Date d) {
|
||||
return putTime(d, DerValue.tag_UtcTime);
|
||||
}
|
||||
|
||||
|
@ -501,7 +498,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* <P>YYYYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time
|
||||
* and with seconds (even if seconds=0) as per RFC 5280.
|
||||
*/
|
||||
public DerOutputStream putGeneralizedTime(Date d) throws IOException {
|
||||
public DerOutputStream putGeneralizedTime(Date d) {
|
||||
return putTime(d, DerValue.tag_GeneralizedTime);
|
||||
}
|
||||
|
||||
|
@ -512,7 +509,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* @param d the date to be marshalled
|
||||
* @param tag the tag for UTC Time or Generalized Time
|
||||
*/
|
||||
private DerOutputStream putTime(Date d, byte tag) throws IOException {
|
||||
private DerOutputStream putTime(Date d, byte tag) {
|
||||
|
||||
/*
|
||||
* Format the date.
|
||||
|
@ -538,7 +535,7 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
|
||||
write(tag);
|
||||
putLength(time.length);
|
||||
write(time);
|
||||
writeBytes(time);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -546,9 +543,8 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
* Put the encoding of the length in the stream.
|
||||
*
|
||||
* @param len the length of the attribute.
|
||||
* @exception IOException on writing errors.
|
||||
*/
|
||||
public void putLength(int len) throws IOException {
|
||||
public void putLength(int len) {
|
||||
if (len < 128) {
|
||||
write((byte)len);
|
||||
|
||||
|
@ -579,20 +575,17 @@ extends ByteArrayOutputStream implements DerEncoder {
|
|||
/**
|
||||
* Write the current contents of this <code>DerOutputStream</code>
|
||||
* to an <code>OutputStream</code>.
|
||||
*
|
||||
* @exception IOException on output error.
|
||||
*/
|
||||
@Override
|
||||
public void encode(DerOutputStream out) throws IOException {
|
||||
out.write(toByteArray());
|
||||
public void encode(DerOutputStream out) {
|
||||
out.writeBytes(toByteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a DerEncoder onto the output stream.
|
||||
* @param encoder the DerEncoder
|
||||
* @throws IOException on output error
|
||||
*/
|
||||
public DerOutputStream write(DerEncoder encoder) throws IOException {
|
||||
public DerOutputStream write(DerEncoder encoder) {
|
||||
encoder.encode(this);
|
||||
return this;
|
||||
}
|
||||
|
|
|
@ -493,7 +493,7 @@ public class DerValue {
|
|||
/**
|
||||
* Encode an ASN1/DER encoded datum onto a DER output stream.
|
||||
*/
|
||||
public void encode(DerOutputStream out) throws IOException {
|
||||
public void encode(DerOutputStream out) {
|
||||
out.write(tag);
|
||||
out.putLength(end - start);
|
||||
out.write(buffer, start, end - start);
|
||||
|
@ -774,7 +774,7 @@ public class DerValue {
|
|||
* Helper routine to return all the bytes contained in the
|
||||
* DerInputStream associated with this object.
|
||||
*/
|
||||
public byte[] getDataBytes() throws IOException {
|
||||
public byte[] getDataBytes() {
|
||||
data.pos = data.end; // Compatibility. Reach end.
|
||||
return Arrays.copyOfRange(buffer, start, end);
|
||||
}
|
||||
|
@ -1133,7 +1133,7 @@ public class DerValue {
|
|||
*
|
||||
* @return DER-encoded value, including tag and length.
|
||||
*/
|
||||
public byte[] toByteArray() throws IOException {
|
||||
public byte[] toByteArray() {
|
||||
data.pos = data.start; // Compatibility. At head.
|
||||
// Minimize content duplication by writing out tag and length only
|
||||
DerOutputStream out = new DerOutputStream();
|
||||
|
|
|
@ -25,7 +25,6 @@
|
|||
|
||||
package sun.security.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.security.spec.ECParameterSpec;
|
||||
import java.security.spec.ECPoint;
|
||||
|
@ -59,11 +58,7 @@ public final class NamedCurve extends ECParameterSpec {
|
|||
this.oid = ko.value();
|
||||
|
||||
DerOutputStream out = new DerOutputStream();
|
||||
try {
|
||||
out.putOID(ObjectIdentifier.of(ko));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Internal error", e);
|
||||
}
|
||||
out.putOID(ObjectIdentifier.of(ko));
|
||||
encoded = out.toByteArray();
|
||||
}
|
||||
|
||||
|
|
|
@ -324,7 +324,7 @@ public final class ObjectIdentifier implements Serializable {
|
|||
/*
|
||||
* n.b. the only public interface is DerOutputStream.putOID()
|
||||
*/
|
||||
void encode(DerOutputStream out) throws IOException {
|
||||
void encode(DerOutputStream out) {
|
||||
out.write (DerValue.tag_ObjectId, encoding);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue