X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;ds=sidebyside;f=tests%2Forg%2Fcacert%2Fgigi%2FtestUtils%2FPemKey.java;h=c790dd7250d45cd67ef2e104ddd3da1fa1b41b50;hb=2a55aad15a217b2410c79f23e42605186028ac38;hp=1bf2c2508eac84b5a9224c28e68ccb675a1dbac6;hpb=98b0a9f22e27177b810f6977e44dfa803cd0c4f0;p=gigi.git diff --git a/tests/org/cacert/gigi/testUtils/PemKey.java b/tests/org/cacert/gigi/testUtils/PemKey.java index 1bf2c250..c790dd72 100644 --- a/tests/org/cacert/gigi/testUtils/PemKey.java +++ b/tests/org/cacert/gigi/testUtils/PemKey.java @@ -10,30 +10,31 @@ import java.security.spec.PKCS8EncodedKeySpec; import java.util.Base64; public class PemKey { - public static PrivateKey parsePEMPrivateKey(String privKeyPEM) throws NoSuchAlgorithmException, - InvalidKeySpecException { - if (privKeyPEM.startsWith("-----BEGIN RSA PRIVATE KEY-----")) { - // key is pkcs1 convert to p8 - try { - Process p = Runtime.getRuntime().exec(new String[] { "openssl", "pkcs8", "-topk8", "-nocrypt" }); - p.getOutputStream().write(privKeyPEM.getBytes()); - p.getOutputStream().close(); - privKeyPEM = IOUtils.readURL(new InputStreamReader(p.getInputStream())); - } catch (IOException e) { - e.printStackTrace(); - } - } - privKeyPEM = privKeyPEM.replaceAll("-----BEGIN PRIVATE KEY-----", "").replace("\n", ""); - // Remove the first and last lines - privKeyPEM = privKeyPEM.replaceAll("-----END PRIVATE KEY-----", ""); - System.out.println(privKeyPEM); - // Base64 decode the data - byte[] encoded = Base64.getDecoder().decode(privKeyPEM); - // PKCS8 decode the encoded RSA private key - PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded); - KeyFactory kf = KeyFactory.getInstance("RSA"); - PrivateKey privKey = kf.generatePrivate(keySpec); - return privKey; - } + public static PrivateKey parsePEMPrivateKey(String privKeyPEM) throws NoSuchAlgorithmException, InvalidKeySpecException { + if (privKeyPEM.startsWith("-----BEGIN RSA PRIVATE KEY-----")) { + // key is pkcs1 convert to p8 + try { + Process p = Runtime.getRuntime().exec(new String[] { + "openssl", "pkcs8", "-topk8", "-nocrypt" + }); + p.getOutputStream().write(privKeyPEM.getBytes()); + p.getOutputStream().close(); + privKeyPEM = IOUtils.readURL(new InputStreamReader(p.getInputStream())); + } catch (IOException e) { + e.printStackTrace(); + } + } + privKeyPEM = privKeyPEM.replaceAll("-----BEGIN PRIVATE KEY-----", "").replace("\n", ""); + // Remove the first and last lines + privKeyPEM = privKeyPEM.replaceAll("-----END PRIVATE KEY-----", ""); + // Base64 decode the data + byte[] encoded = Base64.getDecoder().decode(privKeyPEM); + + // PKCS8 decode the encoded RSA private key + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded); + KeyFactory kf = KeyFactory.getInstance("RSA"); + PrivateKey privKey = kf.generatePrivate(keySpec); + return privKey; + } }