How to use the node-opcua-crypto.extractPublicKeyFromCertificate function in node-opcua-crypto

To help you get started, we’ve selected a few node-opcua-crypto 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.create = function (endpointUrl, callback) {

    assert(_.isFunction(callback));
    const self = this;

    if (self.securityMode !== MessageSecurityMode.NONE) {


        if (!self.serverCertificate) {
            return callback(new Error("ClientSecureChannelLayer#create : expecting a  server certificate when securityMode is not NONE"));
        }

        // take the opportunity of this async method to perform some async pre-processing
        if (_.isUndefined(self.receiverPublicKey)) {

            crypto_utils.extractPublicKeyFromCertificate(self.serverCertificate, function (err, publicKey) {
                /* istanbul ignore next */
                if (err) {
                    return callback(err);
                }
                self.receiverPublicKey = publicKey;
                assert(!_.isUndefined(self.receiverPublicKey)); // make sure we wont go into infinite recursion calling create again.
                self.create(endpointUrl, callback);
            });
            return;
        }
        assert(typeof self.receiverPublicKey === "string");
    }


    self.endpointUrl = endpointUrl;
    const transport = new ClientTCP_transport();
github node-opcua / node-opcua / packages / node-opcua-secure-channel / source / client / client_secure_channel_layer.ts View on Github external
public create(endpointUrl: string, callback: ErrorCallback) {

        assert(_.isFunction(callback));

        if (this.securityMode !== MessageSecurityMode.None) {

            if (!this.serverCertificate) {
                return callback(new Error("ClientSecureChannelLayer#create : expecting a  server certificate when securityMode is not None"));
            }

            // take the opportunity of this async method to perform some async pre-processing
            if (!this.receiverPublicKey) {

                extractPublicKeyFromCertificate(this.serverCertificate, (err: Error | null, publicKey?: PublicKeyPEM) => {
                    /* istanbul ignore next */
                    if (err) {
                        return callback(err);
                    }
                    /* istanbul ignore next */
                    if (!publicKey) {
                        throw new Error("Internal Error");
                    }
                    this.receiverPublicKey = publicKey;

                    this.create(endpointUrl, callback);
                });
                return;
            }
        }
github node-opcua / node-opcua / packages / node-opcua-secure-channel / source / server / server_secure_channel_layer.ts View on Github external
this.receiverPublicKey = null;
            this.receiverPublicKeyLength = 0;
            this.receiverCertificate = asymmSecurityHeader ? asymmSecurityHeader.senderCertificate : null;
            // get the clientCertificate for convenience
            this.clientCertificate = this.receiverCertificate;

            // ignore receiverCertificate that have a zero length
            /* istanbul ignore next */
            if (this.receiverCertificate && this.receiverCertificate.length === 0) {
                this.receiverCertificate = null;
            }

            if (this.receiverCertificate) {
                // extract public key
                extractPublicKeyFromCertificate(this.receiverCertificate, (err, key) => {
                    if (!err) {
                        if (key) {
                            this.receiverPublicKey = key;
                            this.receiverPublicKeyLength = rsa_length(key);
                        }
                        callback(null, StatusCodes.Good);
                    } else {
                        callback(err);
                    }
                });
            } else {
                this.receiverPublicKey = null;
                callback(null, StatusCodes.Good);
            }
        });
    }
github node-opcua / node-opcua / packages / node-opcua-secure-channel / src / server / server_secure_channel_layer.js View on Github external
ServerSecureChannelLayer.prototype._process_certificates = function(message, callback) {
    const self = this;

    self.receiverPublicKey = null;
    self.receiverPublicKeyLength = 0;
    self.receiverCertificate = message.securityHeader ? message.securityHeader.senderCertificate : null;

    // ignore receiverCertificate that have a zero length
    /* istanbul ignore next */
    if (self.receiverCertificate && self.receiverCertificate.length === 0) {
        self.receiverCertificate = null;
    }

    if (self.receiverCertificate) {
        // extract public key
        crypto_utils.extractPublicKeyFromCertificate(self.receiverCertificate, function(err, key) {
            if (!err) {
                self.receiverPublicKey = key;
                self.receiverPublicKeyLength = crypto_utils.rsa_length(key);
            }
            callback(err);
        });
    } else {
        self.receiverPublicKey = null;
        callback();
    }
};