Package org.postgresql.ssl
Class Pk8OrPemKeyManager
- java.lang.Object
-
- org.postgresql.ssl.Pk8OrPemKeyManager
-
- All Implemented Interfaces:
javax.net.ssl.KeyManager,javax.net.ssl.X509KeyManager
public class Pk8OrPemKeyManager extends java.lang.Object implements javax.net.ssl.X509KeyManagerA key manager that selects PEM when its header is found within the first 64 KiB of the key file, and PK8/DER otherwise. This preserves libpq's preference for PEM before DER, while using a bounded marker scan rather than parsing the key during format detection.The delegate is resolved lazily on the first call to any
X509KeyManagermethod, so this works regardless of which method the TLS engine calls first.
-
-
Field Summary
Fields Modifier and Type Field Description private javax.net.ssl.X509KeyManagerdelegateprivate java.lang.StringkeyFilePathprivate static intMAX_PEM_SCAN_BYTESprivate PEMKeyManagerpemprivate static byte[]PEM_HEADERprivate static intPEM_SCAN_CHUNK_BYTESprivate PSQLExceptionpermissionErrorprivate LazyKeyManagerpk8
-
Constructor Summary
Constructors Constructor Description Pk8OrPemKeyManager(java.lang.String keyFilePath, PEMKeyManager pem, LazyKeyManager pk8)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringchooseClientAlias(java.lang.String[] keyType, java.security.Principal[] issuers, java.net.Socket socket)java.lang.StringchooseServerAlias(java.lang.String keyType, java.security.Principal[] issuers, java.net.Socket socket)private javax.net.ssl.X509KeyManagerdelegate()Resolves which key manager to use by probing the key file format, ornullif the key file has insecure permissions.java.security.cert.X509Certificate[]getCertificateChain(java.lang.String alias)java.lang.String[]getClientAliases(java.lang.String keyType, java.security.Principal[] issuers)java.security.PrivateKeygetPrivateKey(java.lang.String alias)java.lang.String[]getServerAliases(java.lang.String keyType, java.security.Principal[] issuers)private booleanlooksLikePem()Returnstrueif the key file is a PKCS#8 PEM file, detected by theBEGIN PRIVATE KEYmarker within the firstMAX_PEM_SCAN_BYTESbytes.voidthrowKeyManagerException()Propagates any exception from the resolved delegate key manager, including an insecure-permission error detected while probing the key file.
-
-
-
Field Detail
-
PEM_HEADER
private static final byte[] PEM_HEADER
-
MAX_PEM_SCAN_BYTES
private static final int MAX_PEM_SCAN_BYTES
- See Also:
- Constant Field Values
-
PEM_SCAN_CHUNK_BYTES
private static final int PEM_SCAN_CHUNK_BYTES
- See Also:
- Constant Field Values
-
keyFilePath
private final java.lang.String keyFilePath
-
pem
private final PEMKeyManager pem
-
pk8
private final LazyKeyManager pk8
-
delegate
private javax.net.ssl.X509KeyManager delegate
-
permissionError
private PSQLException permissionError
-
-
Constructor Detail
-
Pk8OrPemKeyManager
public Pk8OrPemKeyManager(java.lang.String keyFilePath, PEMKeyManager pem, LazyKeyManager pk8)
-
-
Method Detail
-
delegate
private javax.net.ssl.X509KeyManager delegate()
Resolves which key manager to use by probing the key file format, ornullif the key file has insecure permissions.
-
looksLikePem
private boolean looksLikePem()
Returnstrueif the key file is a PKCS#8 PEM file, detected by theBEGIN PRIVATE KEYmarker within the firstMAX_PEM_SCAN_BYTESbytes. Binary (DER) or unreadable content is not PEM.The marker is matched with a streaming scan, so it is found even when it straddles a read boundary, while memory stays bounded to one chunk. The marker's first byte (
'B') does not recur in it, so a mismatch can only start a fresh match and no KMP-style backtracking is needed.
-
getClientAliases
public java.lang.String[] getClientAliases(java.lang.String keyType, java.security.Principal[] issuers)- Specified by:
getClientAliasesin interfacejavax.net.ssl.X509KeyManager
-
chooseClientAlias
public java.lang.String chooseClientAlias(java.lang.String[] keyType, java.security.Principal[] issuers, java.net.Socket socket)- Specified by:
chooseClientAliasin interfacejavax.net.ssl.X509KeyManager
-
getServerAliases
public java.lang.String[] getServerAliases(java.lang.String keyType, java.security.Principal[] issuers)- Specified by:
getServerAliasesin interfacejavax.net.ssl.X509KeyManager
-
chooseServerAlias
public java.lang.String chooseServerAlias(java.lang.String keyType, java.security.Principal[] issuers, java.net.Socket socket)- Specified by:
chooseServerAliasin interfacejavax.net.ssl.X509KeyManager
-
getCertificateChain
public java.security.cert.X509Certificate[] getCertificateChain(java.lang.String alias)
- Specified by:
getCertificateChainin interfacejavax.net.ssl.X509KeyManager
-
getPrivateKey
public java.security.PrivateKey getPrivateKey(java.lang.String alias)
- Specified by:
getPrivateKeyin interfacejavax.net.ssl.X509KeyManager
-
throwKeyManagerException
public void throwKeyManagerException() throws PSQLExceptionPropagates any exception from the resolved delegate key manager, including an insecure-permission error detected while probing the key file.- Throws:
PSQLException- if the delegate key manager has a stored exception
-
-