Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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();
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;
}
}
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);
}
});
}
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();
}
};