Browse Source

SecureChannel: Fix the case where instantiating the SecurityPolicy fails

Julius Pfrommer 5 years ago
parent
commit
d8b0a250cf
1 changed files with 12 additions and 7 deletions
  1. 12 7
      src/ua_securechannel.c

+ 12 - 7
src/ua_securechannel.c

@@ -54,17 +54,16 @@ UA_SecureChannel_setSecurityPolicy(UA_SecureChannel *channel,
     if(channel->securityPolicy)
     if(channel->securityPolicy)
         return UA_STATUSCODE_BADINTERNALERROR;
         return UA_STATUSCODE_BADINTERNALERROR;
 
 
-    channel->securityPolicy = securityPolicy;
     UA_StatusCode retval;
     UA_StatusCode retval;
-    if(channel->securityPolicy->certificateVerification != NULL) {
-        retval = channel->securityPolicy->certificateVerification->
-            verifyCertificate(channel->securityPolicy->certificateVerification->context,
+    if(securityPolicy->certificateVerification != NULL) {
+        retval = securityPolicy->certificateVerification->
+            verifyCertificate(securityPolicy->certificateVerification->context,
                               remoteCertificate);
                               remoteCertificate);
 
 
         if(retval != UA_STATUSCODE_GOOD)
         if(retval != UA_STATUSCODE_GOOD)
             return retval;
             return retval;
     } else {
     } else {
-        UA_LOG_WARNING(channel->securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
+        UA_LOG_WARNING(securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY,
                        "No PKI plugin set. Accepting all certificates");
                        "No PKI plugin set. Accepting all certificates");
     }
     }
 
 
@@ -82,6 +81,9 @@ UA_SecureChannel_setSecurityPolicy(UA_SecureChannel *channel,
         makeCertificateThumbprint(securityPolicy, &channel->remoteCertificate,
         makeCertificateThumbprint(securityPolicy, &channel->remoteCertificate,
                                   &remoteCertificateThumbprint);
                                   &remoteCertificateThumbprint);
 
 
+    if(retval == UA_STATUSCODE_GOOD)
+        channel->securityPolicy = securityPolicy;
+
     return retval;
     return retval;
 }
 }
 
 
@@ -128,12 +130,15 @@ UA_SecureChannel_deleteMembers(UA_SecureChannel *channel) {
     UA_ChannelSecurityToken_deleteMembers(&channel->nextSecurityToken);
     UA_ChannelSecurityToken_deleteMembers(&channel->nextSecurityToken);
 
 
     /* Delete the channel context for the security policy */
     /* Delete the channel context for the security policy */
-    if(channel->securityPolicy)
+    if(channel->securityPolicy) {
         channel->securityPolicy->channelModule.deleteContext(channel->channelContext);
         channel->securityPolicy->channelModule.deleteContext(channel->channelContext);
-
+        channel->securityPolicy = NULL;
+    }
 
 
     /* Remove the buffered messages */
     /* Remove the buffered messages */
     UA_SecureChannel_deleteMessages(channel);
     UA_SecureChannel_deleteMessages(channel);
+
+    UA_SecureChannel_init(channel);
 }
 }
 
 
 void
 void