From 36331ed4aad523f6075576c3b8a404313ed6ff35 Mon Sep 17 00:00:00 2001 From: chubtub <43381989+chubtub@users.noreply.github.com> Date: Thu, 4 Jan 2024 14:02:51 -0500 Subject: [PATCH] WIP: add support for EC private keys --- .../src/main/java/hirs/swid/CredentialParser.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/CredentialParser.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/CredentialParser.java index 013095d8..946d48f5 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/CredentialParser.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/CredentialParser.java @@ -29,7 +29,7 @@ public class CredentialParser { private static final String JKS = "JKS"; private static final String PEM = "PEM"; private static final String PKCS1_HEADER = "-----BEGIN RSA PRIVATE KEY-----"; - private static final String PKCS1_FOOTER = "-----END RSA PRIVATE KEY-----"; + private static final String EC_HEADER = "-----BEGIN EC PRIVATE KEY-----"; private static final String PKCS8_HEADER = "-----BEGIN PRIVATE KEY-----"; private static final String PKCS8_FOOTER = "-----END PRIVATE KEY-----"; private static final String CERTIFICATE_HEADER = "-----BEGIN CERTIFICATE-----"; @@ -175,8 +175,9 @@ public class CredentialParser { dis.close(); String privateKeyStr = new String(key); - if (privateKeyStr.contains(PKCS1_HEADER)) { - privateKey = getPKCS1KeyPair(filename).getPrivate(); + if (privateKeyStr.contains(PKCS1_HEADER) || + privateKeyStr.contains(EC_HEADER)) { + privateKey = parseBase64KeyPair(filename).getPrivate(); } else if (privateKeyStr.contains(PKCS8_HEADER)) { privateKeyStr = privateKeyStr.replace(PKCS8_HEADER, ""); privateKeyStr = privateKeyStr.replace(PKCS8_FOOTER, ""); @@ -217,11 +218,11 @@ public class CredentialParser { } /** - * This method reads a PKCS1 keypair from a PEM file. + * This method reads a base64 PEM string to parse a key pair. * @param filename * @return */ - private KeyPair getPKCS1KeyPair(String filename) throws IOException { + private KeyPair parseBase64KeyPair(String filename) throws IOException { Security.addProvider(new BouncyCastleProvider()); PEMParser pemParser = new PEMParser(new FileReader(filename)); JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");