You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by hu...@apache.org on 2022/05/27 13:25:19 UTC

[plc4x] branch develop updated: fix(opcua): Fixed a few issues with the OPCUA Encryption

This is an automated email from the ASF dual-hosted git repository.

hutcheb pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 49512816f6 fix(opcua): Fixed a few issues with the OPCUA Encryption
     new 6daf53c83c Merge branch 'develop' of github.com:apache/plc4x into develop
49512816f6 is described below

commit 49512816f666f58d7fcca4e08ce2d70e32d5d2b8
Author: hutcheb <be...@gmail.com>
AuthorDate: Fri May 27 13:24:06 2022 +0000

    fix(opcua): Fixed a few issues with the OPCUA Encryption
---
 .../apache/plc4x/java/opcua/OpcuaPlcDriver.java    |  1 -
 .../java/opcua/context/EncryptionHandler.java      |  1 +
 .../plc4x/java/opcua/context/SecureChannel.java    | 41 +++++++++++++---------
 .../java/opcua/protocol/OpcuaProtocolLogic.java    |  3 ++
 4 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java
index 0610ca835e..f3d9793460 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java
@@ -125,7 +125,6 @@ public class OpcuaPlcDriver extends GeneratedDriverBase<OpcuaAPU> {
     @Override
     protected ProtocolStackConfigurer<OpcuaAPU> getStackConfigurer() {
         return SingleProtocolStackConfigurer.builder(OpcuaAPU.class, OpcuaAPU::staticParse)
-            .withEncryptionHandler(new EncryptionHandler())
             .withProtocol(OpcuaProtocolLogic.class)
             .withPacketSizeEstimator(ByteLengthEstimator.class)
             .withParserArgs(true)
diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/EncryptionHandler.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/EncryptionHandler.java
index fb7b47374e..06b385f049 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/EncryptionHandler.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/EncryptionHandler.java
@@ -37,6 +37,7 @@ import java.security.*;
 import java.security.cert.CertificateFactory;
 import java.security.cert.X509Certificate;
 
+
 public class EncryptionHandler {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(OpcuaProtocolLogic.class);
diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannel.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannel.java
index 1816f11bae..5e638b615e 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannel.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannel.java
@@ -526,7 +526,7 @@ public class SecureChannel {
             expandedNodeId,
             null,
             activateSessionRequest,
-            true);
+            false);
 
         try {
             WriteBufferByteBased buffer = new WriteBufferByteBased(extObject.getLengthInBytes(), org.apache.plc4x.java.spi.generation.ByteOrder.LITTLE_ENDIAN);
@@ -618,7 +618,7 @@ public class SecureChannel {
             expandedNodeId,
             null,
             closeSessionRequest,
-            true);
+            false);
 
         try {
             WriteBufferByteBased buffer = new WriteBufferByteBased(extObject.getLengthInBytes(), org.apache.plc4x.java.spi.generation.ByteOrder.LITTLE_ENDIAN);
@@ -702,7 +702,7 @@ public class SecureChannel {
                 expandedNodeId,
                 null,
                 closeSecureChannelRequest,
-                true));
+                false));
 
         Consumer<Integer> requestConsumer = t -> {
             context.sendRequest(new OpcuaAPU(closeRequest, false))
@@ -740,9 +740,12 @@ public class SecureChannel {
                 onDiscoverOpenSecureChannel(context, opcuaAcknowledgeResponse);
             });
 
-        channelTransactionManager.submit(requestConsumer, 1);
+        channelTransactionManager.submit(requestConsumer, channelTransactionManager.getTransactionIdentifier());
+
     }
 
+
+
     public void onDiscoverOpenSecureChannel(ConversationContext<OpcuaAPU> context, OpcuaAcknowledgeResponse opcuaAcknowledgeResponse) {
         int transactionId = channelTransactionManager.getTransactionIdentifier();
 
@@ -762,20 +765,22 @@ public class SecureChannel {
             NULL_BYTE_STRING,
             lifetime);
 
+
         ExpandedNodeId expandedNodeId = new ExpandedNodeId(false,           //Namespace Uri Specified
             false,            //Server Index Specified
             new NodeIdFourByte((short) 0, Integer.parseInt(openSecureChannelRequest.getIdentifier())),
             null,
             null);
 
+        ExtensionObject extObject = new ExtensionObject(
+            expandedNodeId,
+            null,
+            openSecureChannelRequest,
+            false);
 
         try {
-            WriteBufferByteBased buffer = new WriteBufferByteBased(openSecureChannelRequest.getLengthInBytes(), org.apache.plc4x.java.spi.generation.ByteOrder.LITTLE_ENDIAN);
-            new ExtensionObject(
-                expandedNodeId,
-                null,
-                openSecureChannelRequest,
-                true).serialize(buffer);
+            WriteBufferByteBased buffer = new WriteBufferByteBased(extObject.getLengthInBytes(), org.apache.plc4x.java.spi.generation.ByteOrder.LITTLE_ENDIAN);
+            extObject.serialize(buffer);
 
             OpcuaOpenRequest openRequest = new OpcuaOpenRequest(FINAL_CHUNK,
                 0,
@@ -852,13 +857,15 @@ public class SecureChannel {
             null,
             null);
 
+        ExtensionObject extObject = new ExtensionObject(
+            expandedNodeId,
+            null,
+            endpointsRequest,
+            false);
+
         try {
-            WriteBufferByteBased buffer = new WriteBufferByteBased(endpointsRequest.getLengthInBytes(), org.apache.plc4x.java.spi.generation.ByteOrder.LITTLE_ENDIAN);
-            new ExtensionObject(
-                expandedNodeId,
-                null,
-                endpointsRequest,
-                true).serialize(buffer);
+            WriteBufferByteBased buffer = new WriteBufferByteBased(extObject.getLengthInBytes(), org.apache.plc4x.java.spi.generation.ByteOrder.LITTLE_ENDIAN);
+            extObject.serialize(buffer);
 
             OpcuaMessageRequest messageRequest = new OpcuaMessageRequest(FINAL_CHUNK,
                 channelId.get(),
@@ -940,7 +947,7 @@ public class SecureChannel {
                 expandedNodeId,
                 null,
                 closeSecureChannelRequest,
-                true));
+                false));
 
         Consumer<Integer> requestConsumer = t -> context.sendRequest(new OpcuaAPU(closeRequest, false))
             .expectResponse(OpcuaAPU.class, REQUEST_TIMEOUT)
diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java
index 66ce36354a..be3c7cca4d 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java
@@ -115,6 +115,9 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
     public void onDiscover(ConversationContext<OpcuaAPU> context) {
         // Only the TCP transport supports login.
         LOGGER.debug("Opcua Driver running in ACTIVE mode, discovering endpoints");
+        if (this.channel == null) {
+            this.channel = new SecureChannel(driverContext, this.configuration);
+        }
         channel.onDiscover(context);
     }