Connection parameters for OPCUA with Basic256Sha256

80 Views Asked by At

I'm currently facing an issue with PLC4J OPC UA while attempting to establish an encrypted connection without using a discovery server. I've implemented the connection using the following Java code:

String connectionString = String.format("opcua:tcp://%s:%d?discovery=%b", host, port, discovery);
connectionString += (username.isBlank()) ? "" : String.format("&username=%s", username);
connectionString += (password.isBlank()) ? "" : String.format("&password=%s", password);
connectionString += (certDirectory.isBlank()) ? "" : String.format("&certDirectory=%s", certDirectory);
connectionString += (securityPolicy.isBlank()) ? "" : String.format("&securityPolicy=%s", securityPolicy);
connectionString += (keyStoreFile.isBlank()) ? "" : String.format("&keyStoreFile=%s", keyStoreFile);
connectionString += (keyStorePassword.isBlank()) ? "" : String.format("&keyStorePassword=%s", keyStorePassword);

try (PlcConnection connection = PlcDriverManager.getDefault().getConnectionManager().getConnection(connectionString)) {
        ...
}

However, when executing this code, I encounter the following exception:

io.netty.handler.codec.DecoderException: java.lang.NullPointerException: Cannot invoke "org.apache.plc4x.java.opcua.readwrite.PascalByteString.getLengthInBits()" because "this.receiverCertificateThumbprint" is null

I have ensured that all the parameters like host, port, discovery, etc., are correctly set. It seems like an issue with the OPC UA connection, and the receiverCertificateThumbprint not being set in certain scenarios.

Here are some additional details:

When using security None and providing a username and password, everything works correctly.

The keystore is found and opened successfully, but the receiverCertificateThumbprint is never set.

Upon further investigation, it seems that receiverCertificateThumbprint is only set when using a discovery server. I found its usage within the org.apache.plc4x.java.opcua.context.SecureChannel class, specifically in the onDiscoverGetEndpointsRequest() method.

onDiscoverGetEndpointsRequest() {
    // ...
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
        byte[] digest = messageDigest.digest(driverContext.getSenderCertificate());
        driverContext.setThumbprint(new PascalByteString(digest.length, digest));
    } catch (NoSuchAlgorithmException e) {
        LOGGER.error("Failed to find hashing algorithm");
    }
    // ...
}

Any guidance or suggestions on how to resolve this issue would be greatly appreciated. If additional information is needed, please let me know.

Thank you!

0

There are 0 best solutions below