Reputation: 11
We are trying to make SSL connection to PostgreSQL DB through JDBC and for that we are required to pass key file in der format only. We are facing issue with the key file in DER format which is created from PEM String using below code.
String serverKeyString = "-----BEGIN RSA PRIVATE KEY-----\r\n"+"***************\r\n"+ "-----END RSA PRIVATE KEY-----";
serverKeyString = serverKeyString.replace("-----BEGIN RSA PRIVATE KEY-----", "").replace("-----END RSA PRIVATE KEY-----", "").replaceAll("\\s", "");
FileOutputStream osServerkey = new FileOutputStream(new File(".\\**server-key.der**"));
osServerkey.write(Base64.getDecoder().decode(serverKeyString));
osServerkey.close();
And we are passing this file in the properties object.
Class.forName("org.postgresql.Driver");
// JDBC connection parameters
String jdbcUrl = "jdbc:postgresql://<host>:<port>/<db>";
String username = "<username>";
String password = "<password>";
// SSL properties
Properties sslProperties = new Properties();
sslProperties.setProperty("user", username);
sslProperties.setProperty("password", password);
sslProperties.setProperty("ssl", "true");
sslProperties.setProperty("sslmode", "verify-ca");
sslProperties.setProperty("sslrootcert",".\\ca.crt");
sslProperties.setProperty("sslcert", ".\\server-cert.pem");
sslProperties.setProperty("sslkey", ".\\**server-key.der**");
// JDBC connection
try {
Connection connection = DriverManager.getConnection(jdbcUrl,sslProperties);
// Use the connection...
System.out.println("Connection created successfully!!!");
// Close the connection
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
We are getting below exception:
org.postgresql.util.PSQLException: SSL error: Received fatal alert: unexpected_message
at org.postgresql.ssl.MakeSSL.convert(MakeSSL.java:43)
at org.postgresql.core.v3.ConnectionFactoryImpl.enableSSL(ConnectionFactoryImpl.java:584)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:168)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:235)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:247)
at org.postgresql.Driver.makeConnection(Driver.java:434)
at org.postgresql.Driver.connect(Driver.java:291)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at com.postgres.main.PostgreSQLSSL.main(PostgreSQLSSL.java:219)
Caused by: javax.net.ssl.SSLException: Received fatal alert: unexpected_message
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2020)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1127)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379)
at org.postgresql.ssl.MakeSSL.convert(MakeSSL.java:41)
... 10 more
Note:
If we are converting key file from PEM to DER format using below openssl command then it is working fine and we are able to connect openssl pkcs8 -topk8 -inform PEM -in server-key.pem -outform DER -nocrypt -out server-key.der
We are using postgresql-42.5.0
Request you all to help us out. Thanks in advance!
We have mentioned in the description whatever we have tried till now.
Upvotes: 1
Views: 429