ycdch 2022-11-21 13:57 采纳率: 0%
浏览 185
已结题

62541库的密码登录和加密连接问题

oepn62541库开发opc ua 的客户端;密码登录和加密登录都未通过(UaExpert测试能通过)

网上的一般例子运行情况:https://wanghao1314.blog.csdn.net/article/details/104713548

[2022-11-21 02:26:28.000 (UTC-0500)] warn/userland      AcceptAll Certificate Verification. Any remote certificate will be accepted.
[2022-11-21 02:26:28.000 (UTC-0500)] info/securitypolicy        The Basic128Rsa15 security policy with openssl is added.
[2022-11-21 02:26:28.000 (UTC-0500)] info/securitypolicy        The basic256 security policy with openssl is added.
[2022-11-21 02:26:28.001 (UTC-0500)] info/securitypolicy        The basic256sha256 security policy with openssl is added.
[2022-11-21 02:26:28.001 (UTC-0500)] info/securitypolicy        The Aes128Sha256RsaOaep security policy with openssl is added.
[2022-11-21 02:26:28.001 (UTC-0500)] warn/client        The configured ApplicationURI does not match the URI specified in the certificate for the SecurityPolicy http://opcfoundation.org/UA/SecurityPolicy#None
[2022-11-21 02:26:28.001 (UTC-0500)] warn/client        The configured ApplicationURI does not match the URI specified in the certificate for the SecurityPolicy http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15
[2022-11-21 02:26:28.001 (UTC-0500)] warn/client        The configured ApplicationURI does not match the URI specified in the certificate for the SecurityPolicy http://opcfoundation.org/UA/SecurityPolicy#Basic256
[2022-11-21 02:26:28.001 (UTC-0500)] warn/client        The configured ApplicationURI does not match the URI specified in the certificate for the SecurityPolicy http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256
[2022-11-21 02:26:28.001 (UTC-0500)] warn/client        The configured ApplicationURI does not match the URI specified in the certificate for the SecurityPolicy http://opcfoundation.org/UA/SecurityPolicy#Aes128_Sha256_RsaOaep
[2022-11-21 02:26:28.007 (UTC-0500)] info/channel       Connection 3 | SecureChannel 3786802061 | SecureChannel opened with SecurityPolicy http://opcfoundation.org/UA/SecurityPolicy#None and a revised lifetime of 600.00s
[2022-11-21 02:26:28.007 (UTC-0500)] info/client        Client Status: ChannelState: Open, SessionState: Closed, ConnectStatus: Good
[2022-11-21 02:26:28.010 (UTC-0500)] info/client        Rejecting endpoint 0: security mode doesn't match
[2022-11-21 02:26:28.010 (UTC-0500)] info/client        Rejecting endpoint 1: security mode doesn't match
[2022-11-21 02:26:28.010 (UTC-0500)] info/client        Rejecting endpoint 2: security mode doesn't match
[2022-11-21 02:26:28.011 (UTC-0500)] error/client       No suitable endpoint found
[2022-11-21 02:26:28.011 (UTC-0500)] info/client        Client Status: ChannelState: Closed, SessionState: Closed, ConnectStatus: BadInternalError

c语言版本的;来个相关示例代码(客户端的),并详细说下当前原因(密码登录和加密登录2种)

  • 写回答

2条回答 默认 最新

  • Jackyin0720 2022-11-21 14:39
    关注

    这是讲解最详细,注解最清晰,62541库开发实例流程最具体的,你可参考一二:https://blog.csdn.net/weixin_40639467/article/details/108379781
    【下面是博文中配置服务器登录账户与密码的源码】

     第二步  配置服务器的登陆账户与密码
    源码片段 :  
      ret_var = Set_UserPasswer(opc_server,opc_server->Anonymous);  //这里调用了内部实现函数 Set_UserPasswer,我自己的内部函数 //
        if(0>ret_var)
        {
            UA_ServerConfig_delete(opc_server->config);
            opc_server->config = NULL;
            return -4;
        }
    static int Set_UserPasswer(OPC_SERVER *opc_server,UA_Boolean Anonymous)   //配置服务器用户密码认证//
    {
        if(NULL == opc_server)
            return -1;
        UA_ServerConfig *config = (opc_server->config);
        UA_UsernamePasswordLogin PasswordLogin[2] = {{{strlen((opc_server->Username)),(UA_Byte*)((opc_server->Username))},{ strlen((opc_server->Passwer)),(UA_Byte*)((opc_server->Passwer))}},{ UA_STRING_STATIC("Administrator"),UA_STRING_STATIC("administrator")}};  //定义设置服务器用户和密码的数组//
        config->accessControl = UA_AccessControl_default(Anonymous,2,PasswordLogin);  //配置服务器用户密码,可以有多个,这里配置了2个, 其中一个作为管理员被内置 //
        return 0;
    }
    解析:
    1、 变量   UA_UsernamePasswordLogin 是open62541提供的一个结构体变量,结构体变量用来描述一个服务器的用户及其登陆密码,opc ua的一个安全策略就是客户端访问服务器可以受到用户密码的管理; 一个服务器可以被多个使用相同用户登录的客户端访问。
    typedef struct {
        UA_String username;
        UA_String password;
    } UA_UsernamePasswordLogin;
    2、 UA_AccessControl  UA_AccessControl_default( UA_Boolean allowAnonymous,  size_t usernamePasswordLoginSize,  const UA_UsernamePasswordLogin *usernamePasswordLogin )  是open62541提供的函数接口,用来配置并返回一个服务器用户登录管理对象,open62541默认配置的服务器对应不允许匿名登陆,如果需要自由定义,通过调用这个函数接口可以修改。
    UA_AccessControl
    UA_AccessControl_default(UA_Boolean allowAnonymous, size_t usernamePasswordLoginSize,
                             const UA_UsernamePasswordLogin *usernamePasswordLogin) {
        AccessControlContext *context = (AccessControlContext*)
            UA_calloc(1, sizeof(AccessControlContext));
        
        UA_AccessControl ac;
        memset(&ac, 0, sizeof(ac));
        ac.context = context;
        ac.deleteMembers = deleteMembers_default;
        ac.activateSession = activateSession_default;
        ac.closeSession = closeSession_default;
        ac.getUserRightsMask = getUserRightsMask_default;
        ac.getUserAccessLevel = getUserAccessLevel_default;
        ac.getUserExecutable = getUserExecutable_default;
        ac.getUserExecutableOnObject = getUserExecutableOnObject_default;
        ac.allowAddNode = allowAddNode_default;
        ac.allowAddReference = allowAddReference_default;
        ac.allowDeleteNode = allowDeleteNode_default;
        ac.allowDeleteReference = allowDeleteReference_default;
     
        /* Allow anonymous? */
        context->allowAnonymous = allowAnonymous;
     
        /* Copy username/password to the access control plugin */
        if(usernamePasswordLoginSize > 0) {
            context->usernamePasswordLogin = (UA_UsernamePasswordLogin*)
                UA_malloc(usernamePasswordLoginSize * sizeof(UA_UsernamePasswordLogin));
            if(!context->usernamePasswordLogin)
                return ac;
            context->usernamePasswordLoginSize = usernamePasswordLoginSize;
            for(size_t i = 0; i < usernamePasswordLoginSize; i++) {
                UA_String_copy(&usernamePasswordLogin[i].username, &context->usernamePasswordLogin[i].username);
                UA_String_copy(&usernamePasswordLogin[i].password, &context->usernamePasswordLogin[i].password);
            }
        }
     
        /* Set the allowed policies */
        size_t policies = 0;
        if(allowAnonymous)
            policies++;
        if(usernamePasswordLoginSize > 0)
            policies++;
        ac.userTokenPoliciesSize = 0;
        ac.userTokenPolicies = (UA_UserTokenPolicy *)
            UA_Array_new(policies, &UA_TYPES[UA_TYPES_USERTOKENPOLICY]);
        if(!ac.userTokenPolicies)
            return ac;
        ac.userTokenPoliciesSize = policies;
     
        policies = 0;
        if(allowAnonymous) {
            ac.userTokenPolicies[policies].tokenType = UA_USERTOKENTYPE_ANONYMOUS;
            ac.userTokenPolicies[policies].policyId = UA_STRING_ALLOC(ANONYMOUS_POLICY);
            policies++;
        }
     
        if(usernamePasswordLoginSize > 0) {
            ac.userTokenPolicies[policies].tokenType = UA_USERTOKENTYPE_USERNAME;
            ac.userTokenPolicies[policies].policyId = UA_STRING_ALLOC(USERNAME_POLICY);
            /* No encryption of username/password supported at the moment */
            ac.userTokenPolicies[policies].securityPolicyUri =
                UA_STRING_ALLOC(" http://opcfoundation.org/UA/SecurityPolicy#None");
        }
        return ac;
    }
    
    评论

报告相同问题?

问题事件

  • 系统已结题 11月29日
  • 赞助了问题酬金50元 11月25日
  • 赞助了问题酬金50元 11月24日
  • 赞助了问题酬金50元 11月23日
  • 展开全部