public final class PemReader extends Object
The following modifications have been made to the original source code:
| Modifier and Type | Method and Description |
|---|---|
static KeyStore |
loadKeyStore(String certificateChainContents,
String privateKeyContents,
Optional<String> keyPassword)
Loads a KeyStore from PEM file contents.
|
static PrivateKey |
loadPrivateKey(String privateKey,
Optional<String> keyPassword)
Loads a private key from PEM-formatted content.
|
static List<X509Certificate> |
readCertificateChain(String certificateChainContents)
Reads a chain of X.509 certificates from PEM-formatted content.
|
public static KeyStore loadKeyStore(String certificateChainContents, String privateKeyContents, Optional<String> keyPassword) throws IOException, GeneralSecurityException
This method reads a private key and certificate chain from PEM-formatted content and stores them in a JKS KeyStore. The certificate file must contain at least one certificate.
certificateChainContents - the PEM-formatted content containing the certificate chainprivateKeyContents - the PEM-formatted content containing the private keykeyPassword - optional password for the private key; if the key is encrypted, this
password will be used to decrypt itIOException - if an I/O error occurs while reading the PEM contentGeneralSecurityException - if a security-related error occurs, such as:
CertificateException - if the certificate file does not contain any certificatespublic static List<X509Certificate> readCertificateChain(String certificateChainContents) throws CertificateException
This method extracts all certificates found in the provided PEM content. Certificates are identified by BEGIN CERTIFICATE and END CERTIFICATE markers. The certificates are returned in the order they appear in the input.
certificateChainContents - the PEM-formatted content containing one or more certificatesCertificateException - if any certificate cannot be parsed or generatedpublic static PrivateKey loadPrivateKey(String privateKey, Optional<String> keyPassword) throws IOException, GeneralSecurityException
This method supports both encrypted and unencrypted private keys. The key must be in PKCS#8
format. To convert a key to PKCS#8 format, use: openssl pkcs8 -topk8 ...
The method attempts to load the key using RSA, EC, and DSA algorithms in that order.
privateKey - the PEM-formatted content containing the private keykeyPassword - optional password for decrypting an encrypted private key; if empty, the key
is assumed to be unencryptedIOException - if an I/O error occurs while reading the keyGeneralSecurityException - if a security-related error occurs, such as:
Copyright © 2026 Broadcom Inc. and its subsidiaries.. All rights reserved.