Skip to content

Refactor PemPrivateKeyParser to use DerElement #39162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -91,6 +92,36 @@ final class PemPrivateKeyParser {
*/
private static final int[] RSA_ALGORITHM = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01 };

/**
* ASN.1 encoded object identifier {@literal 1.2.840.113549.1.1.10}.
*/
private static final int[] RSASSA_PSS_ALGORITHM = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0a };

/**
* ASN.1 encoded object identifier {@literal 1.2.840.10040.4.1}.
*/
private static final int[] DSA_ALGORITHM = { 0x2a, 0x86, 0x48, 0xce, 0x38, 0x04, 0x01 };

/**
* ASN.1 encoded object identifier {@literal 1.3.101.110}.
*/
private static final int[] X25519_ALGORITHM = { 0x2b, 0x65, 0x6e };

/**
* ASN.1 encoded object identifier {@literal 1.3.101.111}.
*/
private static final int[] X448_ALGORITHM = { 0x2b, 0x65, 0x6f };

/**
* ASN.1 encoded object identifier {@literal 1.3.101.112}.
*/
private static final int[] ED448_ALGORITHM = { 0x2b, 0x65, 0x70 };

/**
* ASN.1 encoded object identifier {@literal 1.3.101.113}.
*/
private static final int[] ED25519_ALGORITHM = { 0x2b, 0x65, 0x71 };

/**
* ASN.1 encoded object identifier {@literal 1.2.840.10045.2.1}.
*/
Expand Down Expand Up @@ -132,10 +163,10 @@ private static int[] getEcParameters(DerElement parameters) {
DerElement contents = DerElement.of(parameters.getContents());
Assert.state(contents != null && contents.isType(ValueType.PRIMITIVE, TagType.OBJECT_IDENTIFIER),
"Key spec parameters should contain object identifier");
return getEcParameters(contents.getContents());
return getOid(contents.getContents());
}

private static int[] getEcParameters(ByteBuffer bytes) {
private static int[] getOid(ByteBuffer bytes) {
int[] result = new int[bytes.remaining()];
for (int i = 0; i < result.length; i++) {
result[i] = bytes.get() & 0xFF;
Expand All @@ -160,7 +191,55 @@ private static PKCS8EncodedKeySpec createKeySpecForAlgorithm(byte[] bytes, int[]
}

private static PKCS8EncodedKeySpec createKeySpecForPkcs8(byte[] bytes, String password) {
return new PKCS8EncodedKeySpec(bytes);
DerElement ecPrivateKey = DerElement.of(bytes);
Assert.state(ecPrivateKey.isType(ValueType.ENCODED, TagType.SEQUENCE),
"Key spec should be an ASN.1 encoded sequence");
DerElement version = DerElement.of(ecPrivateKey.getContents());
Assert.state(version != null && version.isType(ValueType.PRIMITIVE, TagType.INTEGER),
"Key spec should start with version");
DerElement sequence = DerElement.of(ecPrivateKey.getContents());
Assert.state(sequence != null && sequence.isType(ValueType.ENCODED, TagType.SEQUENCE),
"Key spec should contain private key");
DerElement algorithmIdentifier = DerElement.of(sequence.getContents());
Assert.state(
algorithmIdentifier != null
&& algorithmIdentifier.isType(ValueType.PRIMITIVE, TagType.OBJECT_IDENTIFIER),
"Key spec container object identifier");
int[] oid = getOid(algorithmIdentifier.getContents());
String algorithmName = getAlgorithm(oid);
if (algorithmName != null) {
return new PKCS8EncodedKeySpec(bytes, algorithmName);
}
else {
return new PKCS8EncodedKeySpec(bytes);
}
}

private static String getAlgorithm(int[] oid) {
if (oid == null) {
return null;
}
if (Arrays.equals(RSA_ALGORITHM, oid)) {
return "RSA";
}
else if (Arrays.equals(RSASSA_PSS_ALGORITHM, oid)) {
return "RSASSA-PSS";
}
else if (Arrays.equals(DSA_ALGORITHM, oid)) {
return "DSA";
}
else if (Arrays.equals(ED448_ALGORITHM, oid) || Arrays.equals(ED25519_ALGORITHM, oid)) {
return "EdDSA";
}
else if (Arrays.equals(X448_ALGORITHM, oid) || Arrays.equals(X25519_ALGORITHM, oid)) {
return "XDH";
}
else if (Arrays.equals(EC_ALGORITHM, oid)) {
return "EC";
}
else {
return null;
}
}

private static PKCS8EncodedKeySpec createKeySpecForPkcs8Encrypted(byte[] bytes, String password) {
Expand Down Expand Up @@ -231,6 +310,15 @@ private static byte[] decodeBase64(String content) {

private PrivateKey parse(byte[] bytes, String password) {
PKCS8EncodedKeySpec keySpec = this.keySpecFactory.apply(bytes, password);
if (keySpec.getAlgorithm() != null) {
try {
KeyFactory keyFactory = KeyFactory.getInstance(keySpec.getAlgorithm());
return keyFactory.generatePrivate(keySpec);
}
catch (InvalidKeySpecException | NoSuchAlgorithmException ex) {
// Ignore
}
}
for (String algorithm : this.algorithms) {
try {
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
Expand Down