Closed bomgar closed 1 year ago
Without the -traditional
parameter, openssl
generates a private key in PKCS#8 format. The PEM file does not contain a public key. Generating a matching public key from that private key is out of scope for KeyPairUtils
as a simple utility class. The main purpose of readKeyPair()
is to read a PEM file that was written by writeKeyPair()
.
acme4j client itself only requires a standard java.security.KeyPair
object. If you find a way to read a key pair from the PKCS#8 file, you can just use that KeyPair
object.
Ok thank you.
If anyone reads this and wants to know what I did (only works for RSA keys):
fun readKeyPair(r: Reader): KeyPair {
PEMParser(r).use { parser ->
return when(val o = parser.readObject()) {
is PEMKeyPair -> {
println("Read old style RSA PRIVATE KEY")
JcaPEMKeyConverter().getKeyPair(o)
}
is PrivateKeyInfo -> {
println("Read PRIVATE KEY. Assume it is RSA.")
val privateKey = JcaPEMKeyConverter().getPrivateKey(o) as RSAPrivateCrtKey
val spec = RSAPublicKeySpec(privateKey.modulus, privateKey.publicExponent)
val publicKey = KeyFactory.getInstance("RSA").generatePublic(spec)
return KeyPair(publicKey, privateKey)
}
else -> {
throw IOException("Invalid PEM file")
}
}
}
}
We call this code in our application:
It reads files generated by this command:
Without the
-traditional
it causes problems:The typecast here seems to be the problem: