JWK from PEM-encoded objects
Developers working with JOSE and JWT may occasionally may need to create a public JWK or a public / private JWK from a PEM-encoded X.509 certificate, a public key, a private key, or a matching pair thereof.
The JWK.parseFromPEMEncodedObject
method can take care of that. It parses a
string of one or more of the following PEM-encoded objects to create an RSA or
EC JWK:
- X.509 certificate (PEM header:
BEGIN CERTIFICATE
) - PKCS#1 RSAPublicKey (PEM header:
BEGIN RSA PUBLIC KEY
) - X.509 SubjectPublicKeyInfo (PEM header:
BEGIN PUBLIC KEY
) - PKCS#1 RSAPrivateKey (PEM header:
BEGIN RSA PRIVATE KEY
) - PKCS#8 PrivateKeyInfo (PEM header:
BEGIN PRIVATE KEY
) - Matching pair of the above, e.g. X.509 certificate with PKCS#8 encoded private key.
Requires Nimbus JOSE+JWT 6.2+.
// PEM-encoded private RSA key generated with
// openssl genpkey -algorithm RSA -out priv.pem -pkeyopt rsa_keygen_bits:2048
String pemEncodedRSAPrivateKey =
"-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCjknN/LTKuPvXU
bvLvyAwIs7I189CC+JErIOWwYzE1zRRLn7zNbG+UQJFqVorWm/84OqX/iC9HYudo
r4qFfT67Fynfs5/OMzbqp7ECX24oKF1G919bUHY8KrJY4CUJhgo4FOda3PuzbmEd
/POB6OcQ2vyJg1SsGY5REfheIaZ+AHVlTaze6HvejNJWE29iVIBxd0a4cWy9T4xt
DS5+Z9juoN+l8QvPjFaYenPzWgOqyw5Fsgkulv++M4A/htPX3iFWQNV1Mix9Y/aS
jbi8ft5+rlt6+llVT87fSgdeT9ZCmceIjwtZW+Zk6dlknXRoTI5q6PZfAFVDKhFN
RKdbSP03AgMBAAECggEAE2aCVlopAausAocqPzBN5RZTE70YRQBwT1o2g+Yv5v6s
4o9OmFq9HStPU/pxuySDb8rc92LSoLflVHBFkLGbKBuGNucaFB3U7J35C5v/97lZ
3tnmMHFppJc30fy7x9ZjDeXEzu0Y2V5FHIZs50K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LUx3lpr32I+zijs25AQ==
-----END PRIVATE KEY-----";
// Parse PEM-encoded key to RSA public / private JWK
JWK jwk = JWK.parseFromPEMEncodedObjects(pemEncodedRSAPrivateKey);
assertEquals(KeyType.RSA, jwk.getKeyType());
assertTrue(jwk.isPrivate());