X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=tests%2Forg%2Fcacert%2Fgigi%2FtestUtils%2FPemKey.java;h=1bf2c2508eac84b5a9224c28e68ccb675a1dbac6;hb=63d97b58fdda853ec712cba3e821a16e8daec401;hp=14d282fdce4e60c6566dea16a6ee908223589900;hpb=26adba2db53b0a4cb7e965219e51175845407be2;p=gigi.git diff --git a/tests/org/cacert/gigi/testUtils/PemKey.java b/tests/org/cacert/gigi/testUtils/PemKey.java index 14d282fd..1bf2c250 100644 --- a/tests/org/cacert/gigi/testUtils/PemKey.java +++ b/tests/org/cacert/gigi/testUtils/PemKey.java @@ -1,5 +1,7 @@ package org.cacert.gigi.testUtils; +import java.io.IOException; +import java.io.InputStreamReader; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; @@ -10,10 +12,21 @@ import java.util.Base64; public class PemKey { public static PrivateKey parsePEMPrivateKey(String privKeyPEM) throws NoSuchAlgorithmException, InvalidKeySpecException { - privKeyPEM = privKeyPEM.replaceAll("-----BEGIN (RSA )?PRIVATE KEY-----", "").replace("\n", ""); + 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 (RSA )?PRIVATE KEY-----", ""); - + privKeyPEM = privKeyPEM.replaceAll("-----END PRIVATE KEY-----", ""); + System.out.println(privKeyPEM); // Base64 decode the data byte[] encoded = Base64.getDecoder().decode(privKeyPEM);