Add pem-api module: PEMDecoder/PEMEncoder (JEP 538 preview on JDK 27, JEP 542 final in JDK 28) for JWT keys, TLS, encrypted keys and BouncyCastle migration
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01CifR2BE7PBvRy5hT8TS9oz
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import java.security.*;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
/** Everything that goes wrong when you feed the decoder something it does not expect. */
|
||||
public class Failures {
|
||||
public static void main(String[] args) throws Exception {
|
||||
PEMDecoder d = PEMDecoder.of();
|
||||
|
||||
Fx.h("the file holds a different kind of object than the class you asked for");
|
||||
Fx.tryIt("certificate as PublicKey", () -> d.decode(Fx.read("server.pem"), PublicKey.class));
|
||||
Fx.tryIt("private key as PublicKey", () -> d.decode(Fx.read("rsa-private-pkcs8.pem"), PublicKey.class));
|
||||
Fx.tryIt("RSA private key as KeyPair", () -> d.decode(Fx.read("rsa-private-pkcs8.pem"), KeyPair.class));
|
||||
|
||||
Fx.h("untyped decode: the result type depends on what the file contains");
|
||||
Fx.tryIt("RSA PKCS#8", () -> d.decode(Fx.read("rsa-private-pkcs8.pem")));
|
||||
Fx.tryIt("EC PKCS#8 (openssl embeds the public key)", () -> d.decode(Fx.read("ec-private-pkcs8.pem")));
|
||||
Fx.tryIt("EC PKCS#8, asked for PrivateKey", () -> d.decode(Fx.read("ec-private-pkcs8.pem"), PrivateKey.class));
|
||||
Fx.tryIt("EC PKCS#8, asked for KeyPair", () -> d.decode(Fx.read("ec-private-pkcs8.pem"), KeyPair.class));
|
||||
|
||||
Fx.h("the old OpenSSL formats");
|
||||
Fx.tryIt("RSA PRIVATE KEY (PKCS#1)", () -> d.decode(Fx.read("rsa-private-pkcs1.pem"), PrivateKey.class));
|
||||
Fx.tryIt("EC PRIVATE KEY (SEC1), untyped", () -> d.decode(Fx.read("ec-private-sec1.pem")));
|
||||
Fx.tryIt("EC PRIVATE KEY (SEC1), asked for PrivateKey", () -> d.decode(Fx.read("ec-private-sec1.pem"), PrivateKey.class));
|
||||
PEM raw = d.decode(Fx.read("ec-private-sec1.pem"), PEM.class);
|
||||
System.out.println("as a PEM object: type=" + raw.type() + ", decoded bytes=" + raw.decode().length);
|
||||
|
||||
Fx.h("malformed text");
|
||||
String pub = Fx.read("rsa-public.pem");
|
||||
Fx.tryIt("empty string", () -> d.decode(""));
|
||||
Fx.tryIt("no PEM in the text", () -> d.decode("hello"));
|
||||
Fx.tryIt("footer missing", () -> d.decode(pub.replace("-----END PUBLIC KEY-----", "")));
|
||||
Fx.tryIt("body is not Base64 of a key", () -> d.decode("-----BEGIN PUBLIC KEY-----\n!!!!\n-----END PUBLIC KEY-----\n"));
|
||||
Fx.tryIt("Windows line endings (CRLF)", () -> d.decode(pub.replace("\n", "\r\n")));
|
||||
Fx.tryIt("comment lines before the header", () -> d.decode("# my key\n" + pub));
|
||||
|
||||
Fx.h("several objects in one string: only the first is returned");
|
||||
String chain = Fx.read("server-chain.pem");
|
||||
X509Certificate first = d.decode(chain, X509Certificate.class);
|
||||
System.out.println("decode(String) on a 2-certificate bundle -> " + first.getSubjectX500Principal());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user