Skip to content

Commit

Permalink
8297065: DerOutputStream operations should not throw IOExceptions
Browse files Browse the repository at this point in the history
Reviewed-by: mullan, valeriep
  • Loading branch information
wangweij committed Nov 29, 2022
1 parent d83a07b commit 2deb318
Show file tree
Hide file tree
Showing 109 changed files with 720 additions and 1,107 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -32,7 +32,6 @@
import java.security.KeyRep;
import java.security.PrivateKey;
import java.security.InvalidKeyException;
import java.security.ProviderException;
import javax.crypto.spec.DHParameterSpec;
import sun.security.util.*;

Expand All @@ -44,7 +43,7 @@
*
*
* @see DHPublicKey
* @see java.security.KeyAgreement
* @see javax.crypto.KeyAgreement
*/
final class DHPrivateKey implements PrivateKey,
javax.crypto.interfaces.DHPrivateKey, Serializable {
Expand Down Expand Up @@ -80,8 +79,6 @@ final class DHPrivateKey implements PrivateKey,
* @param x the private value
* @param p the prime modulus
* @param g the base generator
*
* @throws ProviderException if the key cannot be encoded
*/
DHPrivateKey(BigInteger x, BigInteger p, BigInteger g)
throws InvalidKeyException {
Expand All @@ -97,24 +94,18 @@ final class DHPrivateKey implements PrivateKey,
* @param p the prime modulus
* @param g the base generator
* @param l the private-value length
*
* @throws ProviderException if the key cannot be encoded
*/
DHPrivateKey(BigInteger x, BigInteger p, BigInteger g, int l) {
this.x = x;
this.p = p;
this.g = g;
this.l = l;
try {
byte[] xbytes = x.toByteArray();
DerValue val = new DerValue(DerValue.tag_Integer, xbytes);
this.key = val.toByteArray();
val.clear();
Arrays.fill(xbytes, (byte)0);
encode();
} catch (IOException e) {
throw new ProviderException("Cannot produce ASN.1 encoding", e);
}
byte[] xbytes = x.toByteArray();
DerValue val = new DerValue(DerValue.tag_Integer, xbytes);
this.key = val.toByteArray();
val.clear();
Arrays.fill(xbytes, (byte) 0);
encode();
}

/**
Expand Down Expand Up @@ -221,46 +212,42 @@ public synchronized byte[] getEncoded() {
*/
private void encode() {
if (this.encodedKey == null) {
try {
DerOutputStream tmp = new DerOutputStream();

//
// version
//
tmp.putInteger(PKCS8_VERSION);

//
// privateKeyAlgorithm
//
DerOutputStream algid = new DerOutputStream();

// store OID
algid.putOID(DHPublicKey.DH_OID);
// encode parameters
DerOutputStream params = new DerOutputStream();
params.putInteger(this.p);
params.putInteger(this.g);
if (this.l != 0) {
params.putInteger(this.l);
}
// wrap parameters into SEQUENCE
DerValue paramSequence = new DerValue(DerValue.tag_Sequence,
params.toByteArray());
// store parameter SEQUENCE in algid
algid.putDerValue(paramSequence);
// wrap algid into SEQUENCE
tmp.write(DerValue.tag_Sequence, algid);

// privateKey
tmp.putOctetString(this.key);

// make it a SEQUENCE
DerValue val = DerValue.wrap(DerValue.tag_Sequence, tmp);
this.encodedKey = val.toByteArray();
val.clear();
} catch (IOException e) {
throw new AssertionError(e);
DerOutputStream tmp = new DerOutputStream();

//
// version
//
tmp.putInteger(PKCS8_VERSION);

//
// privateKeyAlgorithm
//
DerOutputStream algid = new DerOutputStream();

// store OID
algid.putOID(DHPublicKey.DH_OID);
// encode parameters
DerOutputStream params = new DerOutputStream();
params.putInteger(this.p);
params.putInteger(this.g);
if (this.l != 0) {
params.putInteger(this.l);
}
// wrap parameters into SEQUENCE
DerValue paramSequence = new DerValue(DerValue.tag_Sequence,
params.toByteArray());
// store parameter SEQUENCE in algid
algid.putDerValue(paramSequence);
// wrap algid into SEQUENCE
tmp.write(DerValue.tag_Sequence, algid);

// privateKey
tmp.putOctetString(this.key);

// make it a SEQUENCE
DerValue val = DerValue.wrap(DerValue.tag_Sequence, tmp);
this.encodedKey = val.toByteArray();
val.clear();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -30,7 +30,6 @@
import java.math.BigInteger;
import java.security.KeyRep;
import java.security.InvalidKeyException;
import java.security.ProviderException;
import java.security.PublicKey;
import javax.crypto.spec.DHParameterSpec;
import sun.security.util.*;
Expand Down Expand Up @@ -97,21 +96,15 @@ final class DHPublicKey implements PublicKey,
* @param p the prime modulus
* @param g the base generator
* @param l the private-value length
*
* @exception ProviderException if the key cannot be encoded
*/
DHPublicKey(BigInteger y, BigInteger p, BigInteger g, int l) {
this.y = y;
this.p = p;
this.g = g;
this.l = l;
try {
this.key = new DerValue(DerValue.tag_Integer,
this.y.toByteArray()).toByteArray();
this.encodedKey = getEncoded();
} catch (IOException e) {
throw new ProviderException("Cannot produce ASN.1 encoding", e);
}
this.key = new DerValue(DerValue.tag_Integer,
this.y.toByteArray()).toByteArray();
this.encodedKey = getEncoded();
}

/**
Expand Down Expand Up @@ -201,39 +194,35 @@ public String getAlgorithm() {
*/
public synchronized byte[] getEncoded() {
if (this.encodedKey == null) {
try {
DerOutputStream algid = new DerOutputStream();

// store oid in algid
algid.putOID(DH_OID);

// encode parameters
DerOutputStream params = new DerOutputStream();
params.putInteger(this.p);
params.putInteger(this.g);
if (this.l != 0) {
params.putInteger(this.l);
}
// wrap parameters into SEQUENCE
DerValue paramSequence = new DerValue(DerValue.tag_Sequence,
params.toByteArray());
// store parameter SEQUENCE in algid
algid.putDerValue(paramSequence);

// wrap algid into SEQUENCE, and store it in key encoding
DerOutputStream tmpDerKey = new DerOutputStream();
tmpDerKey.write(DerValue.tag_Sequence, algid);

// store key data
tmpDerKey.putBitString(this.key);

// wrap algid and key into SEQUENCE
DerOutputStream derKey = new DerOutputStream();
derKey.write(DerValue.tag_Sequence, tmpDerKey);
this.encodedKey = derKey.toByteArray();
} catch (IOException e) {
return null;
DerOutputStream algid = new DerOutputStream();

// store oid in algid
algid.putOID(DH_OID);

// encode parameters
DerOutputStream params = new DerOutputStream();
params.putInteger(this.p);
params.putInteger(this.g);
if (this.l != 0) {
params.putInteger(this.l);
}
// wrap parameters into SEQUENCE
DerValue paramSequence = new DerValue(DerValue.tag_Sequence,
params.toByteArray());
// store parameter SEQUENCE in algid
algid.putDerValue(paramSequence);

// wrap algid into SEQUENCE, and store it in key encoding
DerOutputStream tmpDerKey = new DerOutputStream();
tmpDerKey.write(DerValue.tag_Sequence, algid);

// store key data
tmpDerKey.putBitString(this.key);

// wrap algid and key into SEQUENCE
DerOutputStream derKey = new DerOutputStream();
derKey.write(DerValue.tag_Sequence, tmpDerKey);
this.encodedKey = derKey.toByteArray();
}
return this.encodedKey.clone();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ byte[] getEncryptedData() {
/**
* Returns the ASN.1 encoding of this class.
*/
byte[] getEncoded()
throws IOException
{
byte[] getEncoded() {
if (this.encoded != null) return this.encoded.clone();

DerOutputStream out = new DerOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @author Benjamin Renaud
*/

public class ContentInfo {
public class ContentInfo implements DerEncoder {

// pkcs7 pre-defined content types
public static ObjectIdentifier PKCS7_OID =
Expand Down Expand Up @@ -166,7 +166,8 @@ public byte[] getData() throws IOException {
throw new IOException("content type is not DATA: " + contentType);
}

public void encode(DerOutputStream out) throws IOException {
@Override
public void encode(DerOutputStream out) {
DerOutputStream contentDerCode;
DerOutputStream seq;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ public byte[] getEncryptedData() {
/**
* Returns the ASN.1 encoding of this class.
*/
public byte[] getEncoded()
throws IOException
{
public byte[] getEncoded() {
if (this.encoded != null) return this.encoded.clone();

DerOutputStream out = new DerOutputStream();
Expand All @@ -141,20 +139,16 @@ public boolean equals(Object other) {
return true;
if (!(other instanceof EncryptedPrivateKeyInfo))
return false;
try {
byte[] thisEncrInfo = this.getEncoded();
byte[] otherEncrInfo
= ((EncryptedPrivateKeyInfo)other).getEncoded();
byte[] thisEncrInfo = this.getEncoded();
byte[] otherEncrInfo
= ((EncryptedPrivateKeyInfo) other).getEncoded();

if (thisEncrInfo.length != otherEncrInfo.length)
return false;
for (int i = 0; i < thisEncrInfo.length; i++)
if (thisEncrInfo[i] != otherEncrInfo[i])
return false;
return true;
} catch (IOException e) {
if (thisEncrInfo.length != otherEncrInfo.length)
return false;
}
for (int i = 0; i < thisEncrInfo.length; i++)
if (thisEncrInfo[i] != otherEncrInfo[i])
return false;
return true;
}

/**
Expand Down
14 changes: 1 addition & 13 deletions src/java.base/share/classes/sun/security/pkcs/PKCS7.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,18 +436,6 @@ private void parseOldSignedData(DerValue val) throws IOException {
}
}

/**
* Encodes the signed data to an output stream.
*
* @param out the output stream to write the encoded data to.
* @exception IOException on encoding errors.
*/
public void encodeSignedData(OutputStream out) throws IOException {
DerOutputStream derout = new DerOutputStream();
encodeSignedData(derout);
out.write(derout.toByteArray());
}

/**
* Encodes the signed data to a DerOutputStream.
*
Expand Down Expand Up @@ -850,7 +838,7 @@ private static byte[] constructToken(byte[] signature,
: new ContentInfo(content);
PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
signerChain, signerInfos);
ByteArrayOutputStream p7out = new ByteArrayOutputStream();
DerOutputStream p7out = new DerOutputStream();
pkcs7.encodeSignedData(p7out);

return p7out.toByteArray();
Expand Down
23 changes: 9 additions & 14 deletions src/java.base/share/classes/sun/security/pkcs/PKCS8Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ public AlgorithmId getAlgorithmId () {
* or {@code null} if an encoding error occurs.
*/
public byte[] getEncoded() {
byte[] b = getEncodedInternal();
return (b == null) ? null : b.clone();
return getEncodedInternal().clone();
}

/**
Expand All @@ -213,21 +212,17 @@ public String getFormat() {
* DER-encodes this key as a byte array stored inside this object
* and return it.
*
* @return the encoding, or null if there is an I/O error.
* @return the encoding
*/
private synchronized byte[] getEncodedInternal() {
if (encodedKey == null) {
try {
DerOutputStream tmp = new DerOutputStream();
tmp.putInteger(V1);
algid.encode(tmp);
tmp.putOctetString(key);
DerValue out = DerValue.wrap(DerValue.tag_Sequence, tmp);
encodedKey = out.toByteArray();
out.clear();
} catch (IOException e) {
// encodedKey is still null
}
DerOutputStream tmp = new DerOutputStream();
tmp.putInteger(V1);
algid.encode(tmp);
tmp.putOctetString(key);
DerValue out = DerValue.wrap(DerValue.tag_Sequence, tmp);
encodedKey = out.toByteArray();
out.clear();
}
return encodedKey;
}
Expand Down

1 comment on commit 2deb318

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.