Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(options: MessageBuilderOptions) {
super(options);
options = options || {};
this.id = (options.name ? options.name : "Id") + counter++;
this.privateKey = options.privateKey || invalidPrivateKey;
this.cryptoFactory = null;
this.securityPolicy = SecurityPolicy.Invalid; // not known yet
this.securityMode = options.securityMode || MessageSecurityMode.Invalid; // not known yet
this.objectFactory = options.objectFactory || defaultObjectFactory;
assert(_.isFunction(this.objectFactory.constructObject), " the objectFactory must provide a constructObject method");
this._previousSequenceNumber = -1; // means unknown
assert(_.isFinite(this._previousSequenceNumber));
this._tokenStack = [];
}
private _open_secure_channel_request(isInitial: boolean, callback: ErrorCallback) {
assert(this.securityMode !== MessageSecurityMode.Invalid, "invalid security mode");
// from the specs:
// The OpenSecureChannel Messages are not signed or encrypted if the SecurityMode is None. The
// nonces are ignored and should be set to null. The SecureChannelId and the TokenId are still
// assigned but no security is applied to Messages exchanged via the channel.
const msgType = "OPN";
const requestType = (isInitial) ? SecurityTokenRequestType.Issue : SecurityTokenRequestType.Renew;
this.clientNonce = this._build_client_nonce();
this._isOpened = !isInitial;
// OpenSecureChannel
const msg = new OpenSecureChannelRequest({
clientNonce: this.clientNonce, //
clientProtocolVersion: this.protocolVersion,
securityPolicyUri: "http://opcfoundation.org/UA/SecurityPolicy#None",
senderCertificate: null
});
this.messageChunker = new MessageChunker({
securityHeader: this.securityHeader // for OPN
});
this._tick0 = 0;
this._tick1 = 0;
this._tick2 = 0;
this._tick3 = 0;
this._bytesRead_before = 0;
this._bytesWritten_before = 0;
this.securityMode = MessageSecurityMode.Invalid;
this.timeoutId = null;
this._securityTokenTimeout = null;
this._transactionsCount = 0;
this.sessionTokens = {};
// xx #422 self.setMaxListeners(200); // increase the number of max listener
}
private _decrypt_MSG(binaryStream: BinaryStream): boolean {
assert(this.securityHeader instanceof SymmetricAlgorithmSecurityHeader);
assert(this.securityMode !== MessageSecurityMode.None);
assert(this.securityMode !== MessageSecurityMode.Invalid);
assert(this.securityPolicy !== SecurityPolicy.None);
assert(this.securityPolicy !== SecurityPolicy.Invalid);
const symmetricAlgorithmSecurityHeader = this.securityHeader as SymmetricAlgorithmSecurityHeader;
// Check security token
// securityToken may have been renewed
const securityTokenData = this._select_matching_token(symmetricAlgorithmSecurityHeader.tokenId);
if (!securityTokenData) {
this._report_error("Security token data for token " + symmetricAlgorithmSecurityHeader.tokenId + " doesn't exist");
return false;
}
assert(securityTokenData.hasOwnProperty("derivedKeys"));
// SecurityToken may have expired, in this case the MessageBuilder shall reject the message
if (securityTokenData.securityToken.expired) {
export function getOptionsForSymmetricSignAndEncrypt(
securityMode: MessageSecurityMode,
derivedKeys: DerivedKeys
): SecureMessageChunkManagerOptions {
assert(derivedKeys.hasOwnProperty("signatureLength"));
assert(securityMode !== MessageSecurityMode.None && securityMode !== MessageSecurityMode.Invalid);
let options = {
signBufferFunc: (chunk: Buffer) => makeMessageChunkSignatureWithDerivedKeys(chunk, derivedKeys),
signatureLength: derivedKeys.signatureLength,
};
if (securityMode === MessageSecurityMode.SignAndEncrypt) {
options = _.extend(options, {
cipherBlockSize: derivedKeys.encryptingBlockSize,
encryptBufferFunc: (chunk: Buffer) => encryptBufferWithDerivedKeys(chunk, derivedKeys),
plainBlockSize: derivedKeys.encryptingBlockSize,
});
}
return options as SecureMessageChunkManagerOptions;
}
public setSecurity(securityMode: MessageSecurityMode, securityPolicy: SecurityPolicy) {
assert(this.securityMode === MessageSecurityMode.Invalid, "security already set");
this.securityPolicy = coerceSecurityPolicy(securityPolicy);
this.securityMode = coerceMessageSecurityMode(securityMode);
assert(this.securityPolicy !== SecurityPolicy.Invalid);
assert(this.securityMode !== MessageSecurityMode.Invalid);
}