Handling of certificate user tokens with OPC UA

Frequently Asked Questions. Please look here first before you ask questions in one of the forums.
Post Reply
User avatar
Support Team
Hero Member
Hero Member
Posts: 2522
Joined: 18 Mar 2011, 15:09

Handling of certificate user tokens with OPC UA

Post by Support Team »

The following code provides an example for using certificate user tokens in an OPC UA client with the C++ SDK

Code: Select all

    UaStatus            ret;
    UaSession*          pSession;
    UaString            sUrl;
    UaString            sCertificateFile; // Set path and file name for public key
    UaString            sPrivateKeyFile; // Set path and file name for private key
    SessionConnectInfo  connectInfo;
    SessionSecurityInfo securityInfo;

    UaPkiCertificate userCertificate = UaPkiCertificate::fromDERFile(sCertificateFile.toUtf8());
    UaPkiRsaKeyPair userPrivateKey = UaPkiRsaKeyPair::fromPEMFile(sPrivateKeyFile.toUtf8(), OpcUa_Null);

    if (userCertificate.isNull())
    {
        // Error handling
    }
    if (!userPrivateKey.isValid())
    {
        // Error handling
    }

    UaByteArray baUserCertificate = userCertificate.toDER();
    UaByteArray baUserPrivateKey = userPrivateKey.toDER();

    UaByteString bsUserCertificate(baUserCertificate.size(), (OpcUa_Byte*)baUserCertificate.data());
    UaByteString bsUserPrivateKey(baUserPrivateKey.size(), (OpcUa_Byte*)baUserPrivateKey.data());

    securityInfo.setCertificateUserIdentity(bsUserCertificate, bsUserPrivateKey);


    // Connect to server with certificate as user token
    ret = pSession->connect(sUrl, connectInfo, securityInfo, g_pCallback);
 
Best regards
Unified Automation Support Team

Post Reply