Package org.postgresql.ssl
Class Pk8OrPemKeyManager
java.lang.Object
org.postgresql.ssl.Pk8OrPemKeyManager
- All Implemented Interfaces:
KeyManager,X509KeyManager
A 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 X509KeyManager
method, so this works regardless of which method the TLS engine calls first.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate X509KeyManagerprivate final Stringprivate static final intprivate final PEMKeyManagerprivate static final byte[]private static final intprivate PSQLExceptionprivate final LazyKeyManager -
Constructor Summary
ConstructorsConstructorDescriptionPk8OrPemKeyManager(String keyFilePath, PEMKeyManager pem, LazyKeyManager pk8) -
Method Summary
Modifier and TypeMethodDescriptionchooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) chooseServerAlias(String keyType, Principal[] issuers, Socket socket) private X509KeyManagerdelegate()Resolves which key manager to use by probing the key file format, ornullif the key file has insecure permissions.getCertificateChain(String alias) String[]getClientAliases(String keyType, Principal[] issuers) getPrivateKey(String alias) String[]getServerAliases(String keyType, Principal[] issuers) private booleanReturnstrueif the key file is a PKCS#8 PEM file, detected by theBEGIN PRIVATE KEYmarker within the firstMAX_PEM_SCAN_BYTESbytes.voidPropagates any exception from the resolved delegate key manager, including an insecure-permission error detected while probing the key file.
-
Field Details
-
PEM_HEADER
private static final byte[] PEM_HEADER -
MAX_PEM_SCAN_BYTES
private static final int MAX_PEM_SCAN_BYTES- See Also:
-
PEM_SCAN_CHUNK_BYTES
private static final int PEM_SCAN_CHUNK_BYTES- See Also:
-
keyFilePath
-
pem
-
pk8
-
delegate
-
permissionError
-
-
Constructor Details
-
Pk8OrPemKeyManager
-
-
Method Details
-
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
- Specified by:
getClientAliasesin interfaceX509KeyManager
-
chooseClientAlias
- Specified by:
chooseClientAliasin interfaceX509KeyManager
-
getServerAliases
- Specified by:
getServerAliasesin interfaceX509KeyManager
-
chooseServerAlias
- Specified by:
chooseServerAliasin interfaceX509KeyManager
-
getCertificateChain
- Specified by:
getCertificateChainin interfaceX509KeyManager
-
getPrivateKey
- Specified by:
getPrivateKeyin interfaceX509KeyManager
-
throwKeyManagerException
Propagates 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
-