{ // Create OPC UA client Object UA_Client *clientclient = UA_Client_new(); UA_ClientConfig_setDefault (UA_Client_getConfig((client))); // ShowEndPoints(endpointURL.c_str()); // Show all endpoints & tokens UA_ClientConfig * clientConfig = UA_Client_getConfig(client); clientConfig->securityMode = UA_MESSAGESECURITYMODE_NONE; // Has to match with the endpoint clientConfig->securityPolicyUri = UA_STRING ("http://opcfoundation.org/UA/SecurityPolicy#None"); // The SecurityPolicy has to match the end point // Have to include in the security Policies the same security policy as the Token // If not added return error: 'Could not find the required SecurityPolicy for the UserToken' // if the security police is not good could return: // - 'Could not create securityContext' // - 'Could not instantiate the SecurityPolicy for the UserToken' cout << " Adding Security Policy in the Client for Token" << endl; clientConfig->securityPoliciesSize = 2; UA_SecurityPolicy *tmp = (UA_SecurityPolicy *) UA_realloc(clientConfig->securityPolicies, sizeof(UA_SecurityPolicy) * clientConfig->securityPoliciesSize ); clientConfig->securityPolicies = tmp; // Get the Policy memory created cout << "[OPC UA] Adding Security Policy: None" << endl; UA_SecurityPolicy_None(&clientConfig->securityPolicies[0], NULL, UA_BYTESTRING_NULL, &clientConfig->logger); // Load certificate & key from local folder cout << "[OPC UA] Adding Security Policy: Basic128Rsa15" << endl; UA_ByteString cert = LoadFile ("ca.crt"); // UA_ByteString key = LoadFile ("ca.key"); // UA_SecurityPolicy_Basic128Rsa15(&clientConfig->securityPolicies[1], NULL, cert, key, &clientConfig->logger); // Delete certificate files UA_ByteString_deleteMembers(&cert); UA_ByteString_deleteMembers(&key); cout << " Configured Security Policy : " << clientConfig->securityPoliciesSize << endl; // Adding Security Policy for the UserToken (user name & password) that is separated from client security policy // This token was prepared based on the Discovery Endpoints clientConfig->userTokenPolicy.policyId = UA_STRING_STATIC("UserName"); clientConfig->userTokenPolicy.tokenType = UA_USERTOKENTYPE_USERNAME; clientConfig->userTokenPolicy.issuedTokenType = UA_STRING_NULL; clientConfig->userTokenPolicy.issuerEndpointUrl = UA_STRING_NULL; clientConfig->userTokenPolicy.securityPolicyUri = UA_STRING ("http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15"); // Basic128Rsa15 cout << " [OPC UA] Try to connect to " << endpointURL.c_str() << endl; status = UA_Client_connect_username (client,endpointURL.c_str(),username.c_str(), password.c_str());// Set Security Policy = NONE, Message Security Mode = NONE }