How to use the node-opcua-service-secure-channel.MessageSecurityMode.SIGN function in node-opcua-service-secure-channel

To help you get started, we’ve selected a few node-opcua-service-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-secure-channel / src / client / client_secure_channel_layer.js View on Github external
ClientSecureChannelLayer.prototype._construct_security_header = function () {

    const self = this;
    assert(self.hasOwnProperty("securityMode"));
    assert(self.hasOwnProperty("securityPolicy"));


    self.receiverCertificate = self.serverCertificate;

    let securityHeader = null;
    switch (self.securityMode.value) {
        case MessageSecurityMode.SIGN.value:
        case MessageSecurityMode.SIGNANDENCRYPT.value:
            assert(self.securityPolicy !== SecurityPolicy.None);
            // get the thumbprint of the client certificate
            const thumbprint = self.receiverCertificate ? crypto_utils.makeSHA1Thumbprint(self.receiverCertificate) : null;
            securityHeader = new AsymmetricAlgorithmSecurityHeader({
                securityPolicyUri: securityPolicy_m.toURI(self.securityPolicy),
                senderCertificate: self.getCertificateChain(),  // certificate of the private key used to sign the message
                receiverCertificateThumbprint: thumbprint       // thumbprint of the public key used to encrypt the message
            });
            break;
        default:
            /* istanbul ignore next */
            assert(false, "invalid security mode");
    }
    //xx console.log("xxxx security Header",securityHeader.toJSON());
    //xx console.log("xxxx receiverCertificate",self.receiverCertificate.toString("base64").cyan);
github node-opcua / node-opcua / packages / node-opcua-secure-channel / src / server / server_secure_channel_layer.js View on Github external
ServerSecureChannelLayer.prototype._get_security_options_for_OPN = function() {
    const self = this;
    const cryptoFactory = self.messageBuilder.cryptoFactory;
    const options = {};
    // install sign & sign-encrypt behavior
    if (self.securityMode === MessageSecurityMode.SIGN || self.securityMode === MessageSecurityMode.SIGNANDENCRYPT) {
        assert(cryptoFactory, "ServerSecureChannelLayer must have a crypto strategy");

        options.signatureLength = self.getSignatureLength();

        options.signingFunc = function(chunk) {
            const signed = cryptoFactory.asymmetricSign(chunk, self.getPrivateKey());
            assert(signed.length === options.signatureLength);
            return signed;
        };

        assert(self.receiverPublicKeyLength >= 0);
        options.plainBlockSize = self.receiverPublicKeyLength - cryptoFactory.blockPaddingSize;
        options.cipherBlockSize = self.receiverPublicKeyLength;

        options.encrypt_buffer = function(chunk) {
            return cryptoFactory.asymmetricEncrypt(chunk, self.receiverPublicKey);
github node-opcua / node-opcua / packages / node-opcua-secure-channel / src / server / server_secure_channel_layer.js View on Github external
//    This indicates what public key was used to encrypt the MessageChunk
    //   This field shall be null if the message is not encrypted.
    switch (request.securityMode.value) {
        case MessageSecurityMode.NONE.value:
            assert(
                !message.securityHeader ||
                    message.securityHeader.securityPolicyUri === "http://opcfoundation.org/UA/SecurityPolicy#None"
            );
            securityHeader = new AsymmetricAlgorithmSecurityHeader({
                securityPolicyUri: "http://opcfoundation.org/UA/SecurityPolicy#None",
                senderCertificate: null, // message not signed
                receiverCertificateThumbprint: null // message not encrypted
            });

            break;
        case MessageSecurityMode.SIGN.value:
        case MessageSecurityMode.SIGNANDENCRYPT.value:
            // get the thumbprint of the client certificate
            const thumbprint = self.receiverCertificate
                ? crypto_utils.makeSHA1Thumbprint(self.receiverCertificate)
                : null;

            securityHeader = new AsymmetricAlgorithmSecurityHeader({
                securityPolicyUri: self.clientSecurityHeader.securityPolicyUri,
                senderCertificate: self.getCertificateChain(), // certificate of the private key used to sign the message
                receiverCertificateThumbprint: thumbprint // message not encrypted (????)
            });
            break;
    }
    return securityHeader;
}