【OpcUA开发笔记 3】Open62541证书登录方式

前言:

       OpcUA的登录方式有3种,分别是匿名,用户名+密码,证书。其中前两个比较简单,我们就从最复杂的说起,前两个顺带说下也就会了,属于抛砖引玉讲述解决思路,非保姆式教程。

       材料:open62541-1.4.0,cmake 3.29.2,openssl 3.3.0.9,visual studio2022,python3.8.6

一、代码生成

       匿名和用户名+密码方式不需要在代码生成时只需要勾选UA_ENABLE_AMALGAMATION。但是为了支持证书,则在CMake生成代码时,就要选择以下选项

  • UA_ENABLE_ENCRYPTION = OPENSSL

       有了这两个选项,再加之安装了OpenSSL,并正确设置了OpenSSL的环境变量。则代码中证书部分的功能才被支持,否则就是灰的。

/* #undef UA_ENABLE_MQTT */
/* #undef UA_ENABLE_NODESET_INJECTOR */
/* #undef UA_INFORMATION_MODEL_AUTOLOAD */
/* #undef UA_ENABLE_ENCRYPTION_MBEDTLS */
/* #undef UA_ENABLE_CERT_REJECTED_DIR */
/* #undef UA_ENABLE_TPM2_SECURITY */
#define UA_ENABLE_ENCRYPTION_OPENSSL
/* #undef UA_ENABLE_ENCRYPTION_LIBRESSL */
#if defined(UA_ENABLE_ENCRYPTION_MBEDTLS) || defined(UA_ENABLE_ENCRYPTION_OPENSSL) || defined(UA_ENABLE_ENCRYPTION_LIBRESSL)
#define UA_ENABLE_ENCRYPTION
#endif
#ifdef UA_ENABLE_ENCRYPTION

UA_EXPORT UA_StatusCode
UA_ServerConfig_setDefaultWithSecurityPolicies(UA_ServerConfig *conf,
                                               UA_UInt16 portNumber,
                                               const UA_ByteString *certificate,
                                               const UA_ByteString *privateKey,
                                               const UA_ByteString *trustList,
                                               size_t trustListSize,
                                               const UA_ByteString *issuerList,
                                               size_t issuerListSize,
                                               const UA_ByteString *revocationList,
                                               size_t revocationListSize);

        如果老是生成失败,可以考虑换个cmake,反正我一开始老是失败,后来重装了个最新版,代码就能顺利生成了,太坑了。

二、编译

       

        成功生成vs代码以后,用vs打开项目,此时examples里会相较没有勾选UA_ENABLE_ENCRYPTION时,多了 access_control_client_encrypt ,“client_encryptionserver_encryption用于演示证书使用

        1. 编译这两个例子的时候,会编译失败,那是因为缺少以来库,在工程的链接器输入里加入以下库文件,注意OpenSSL要引用MDd文件夹下的两个lib:

ws2_32.lib      #socket相关
iphlpapi.lib      #忘了
C:\Program Files\OpenSSL-Win64\lib\VC\x64\MDd\libssl.lib           #openssl相关
C:\Program Files\OpenSSL-Win64\lib\VC\x64\MDd\libcrypto.lib      #openssl相关

       2. 将OpenSSL的include文件夹路径包含到工程的VC++目录里的包含目录里去,记得要指定到Include这一层,因为代码里直接用的是#include <openssl/x509.h>这种路径,不指定到include,可能就报找不到openssl头文件

C:\Program Files\OpenSSL-Win64\include

三、登录方式

1. 匿名登录

    匿名登录就参考官方例子就好,没什么好说的。

2. 用户名+密码登录

    以下代码中,赋予user1所有权限,而user2没有权限删除节点和引用。

#include "open62541.h"

//record the UA_UsernamePasswordLogin object for delete on service closing.
typedef struct {
	UA_Boolean allowAnonymous;
	size_t usernamePasswordLoginSize;
	UA_UsernamePasswordLogin* usernamePasswordLogin;
	UA_UsernamePasswordLoginCallback loginCallback;
	void* loginContext;
	UA_CertificateVerification verifyX509;
} AccessControlContext;

typedef struct {
	UA_String userName;
	UA_Boolean fAllowAddNode;
	UA_Boolean fAllowAddReference;
	UA_Boolean fAllowDeleteNode;
	UA_Boolean fAllowDeleteReference;
} UserRight_t;

#define ANONYMOUS_POLICY "open62541-anonymous-policy"
#define CERTIFICATE_POLICY "open62541-certificate-policy"
#define USERNAME_POLICY "open62541-username-policy"
const UA_String anonymous_policy = UA_STRING_STATIC(ANONYMOUS_POLICY);
const UA_String certificate_policy = UA_STRING_STATIC(CERTIFICATE_POLICY);
const UA_String username_policy = UA_STRING_STATIC(USERNAME_POLICY);

#define USER_COUNT 2

UA_UsernamePasswor
上一篇:SQL Server重置自增序列初始值


下一篇:设计模式-策略模式