How to use the node-opcua-secure-channel.getCryptoFactory function in node-opcua-secure-channel

To help you get started, we’ve selected a few node-opcua-secure-channel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github node-opcua / node-opcua / packages / node-opcua-client / src / opcua_client.js View on Github external
// then the password only contains the UTF-8 encoded password.
    // note: this means that password is sent in clear text to the server
    // note: OPCUA specification discourages use of unencrypted password
    //       but some old OPCUA server may only provide this policy and we
    //       still have to support in the client?
    if (securityPolicy === SecurityPolicy.None) {
        identityToken = new UserNameIdentityToken({
            encryptionAlgorithm: null,
            password: Buffer.from(password, "utf-8"),
            policyId: userTokenPolicy.policyId,
            userName
        });
        return identityToken;
    }
    // see Release 1.02 155 OPC Unified Architecture, Part 4
    const cryptoFactory = getCryptoFactory(securityPolicy);

    // istanbul ignore next
    if (!cryptoFactory) {
        throw new Error(" Unsupported security Policy");
    }

    identityToken = new UserNameIdentityToken({
        encryptionAlgorithm: cryptoFactory.asymmetricEncryptionAlgorithm,
        password: Buffer.from(password, "utf-8"),
        policyId: userTokenPolicy.policyId,
        userName: userName,
    });


    // now encrypt password as requested
    const lenBuf = createFastUninitializedBuffer(4);
github node-opcua / node-opcua / packages / node-opcua-server / src / opcua_server.js View on Github external
const securityPolicy = adjustSecurityPolicy(channel, userTokenPolicy.securityPolicyUri);

    const userName = userIdentityToken.userName;
    let password = userIdentityToken.password;

    // decrypt password if necessary
    if (securityPolicy === SecurityPolicy.None) {
        password = password.toString();
    } else {
        const serverPrivateKey = self.getPrivateKey();

        const serverNonce = session.nonce;
        assert(serverNonce instanceof Buffer);

        const cryptoFactory = getCryptoFactory(securityPolicy);
        if (!cryptoFactory) {
            return done(new Error(" Unsupported security Policy"));
        }
        const buff = cryptoFactory.asymmetricDecrypt(password, serverPrivateKey);
        const length = buff.readUInt32LE(0) - serverNonce.length;
        password = buff.slice(4, 4 + length).toString("utf-8");
    }

    if (_.isFunction(self.userManager.isValidUserAsync)) {
        self.userManager.isValidUserAsync.call(session, userName, password, done);
    } else {
        const authorized = self.userManager.isValidUser.call(session, userName, password);
        async.setImmediate(function () {
            done(null, authorized)
        });
    }
github node-opcua / node-opcua / packages / node-opcua-server / src / opcua_server.js View on Github external
OPCUAServer.prototype.isValidUserNameIdentityToken = function (channel, session, userTokenPolicy, userIdentityToken) {

    assert(userIdentityToken instanceof UserNameIdentityToken);

    const securityPolicy = adjustSecurityPolicy(channel, userTokenPolicy.securityPolicyUri);
    if (securityPolicy === SecurityPolicy.None) {
        return StatusCodes.Good;
    }
    const cryptoFactory = getCryptoFactory(securityPolicy);
    if (!cryptoFactory) {
        throw new Error(" Unsupported security Policy");
    }

    if (userIdentityToken.encryptionAlgorithm !== cryptoFactory.asymmetricEncryptionAlgorithm) {
        console.log("invalid encryptionAlgorithm");
        console.log("userTokenPolicy", userTokenPolicy.toString());
        console.log("userTokenPolicy", userIdentityToken.toString());
        return false;
    }
    const userName = userIdentityToken.userName;
    const password = userIdentityToken.password;
    if (!userName || !password) {
        return false;
    }
    return true;