Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function onCertificateChange(server: OPCUAServer) {
debugLog("on CertificateChanged");
const _server = server as any as OPCUAServerPartial;
_server.$$privateKeyPEM = fs.readFileSync(server.serverCertificateManager.privateKey, "utf8");
const certificateFile = path.join(server.serverCertificateManager.rootDir, "own/certs/certificate.pem");
const certificatePEM = fs.readFileSync(certificateFile, "utf8");
const privateKeyFile = server.serverCertificateManager.privateKey;
const privateKeyPEM = fs.readFileSync(privateKeyFile, "utf8");
// also reread the private key
_server.$$certificateChain = convertPEMtoDER(certificatePEM);
_server.$$privateKeyPEM = privateKeyPEM;
// note : $$certificate will be reconstructed on demand
_server.$$certificate = undefined;
setTimeout(async () => {
try {
debugLog(chalk.yellow(" onCertificateChange => shutting down channels"));
await server.shutdownChannels();
debugLog(chalk.yellow(" onCertificateChange => channels shut down"));
debugLog(chalk.yellow(" onCertificateChange => resuming end points"));
await server.resumeEndPoints();
debugLog(chalk.yellow(" onCertificateChange => end points resumed"));
debugLog(chalk.yellow("channels have been closed -> client should reconnect "));
startDate: new Date(),
validity: 365 * 5, // five year
/* */
outputFile: certificateFile
};
debugLog("creating self signed certificate", options);
await this.serverCertificateManager.createSelfSignedCertificate(options);
}
const certificatePEM =
await promisify(fs.readFile)(certificateFile, "utf8");
this.$$certificateChain = convertPEMtoDER(certificatePEM);
}
}
}
// todo : at this time regenerate PrivateKey is not supported
if (regeneratePrivateKey) {
debugLog(" regeneratePrivateKey = true not supported yet");
return {
statusCode: StatusCodes.BadInvalidArgument
};
}
const options = {
subject: subjectName
};
const csrfile = await certificateManager.createCertificateRequest(options);
const csrPEM = await promisify(fs.readFile)(csrfile, "utf8");
const certificateSigningRequest = convertPEMtoDER(csrPEM);
return {
certificateSigningRequest,
statusCode: StatusCodes.Good
};
}
public toString(): string {
const privateKey1 = convertPEMtoDER(this.getPrivateKey());
const txt =
" end point" + this._counter +
" port = " + this.port +
" l = " + this._endpoints.length +
" " + makeSHA1Thumbprint(this.getCertificateChain()).toString("hex") +
" " + makeSHA1Thumbprint(privateKey1).toString("hex");
return txt;
}
public toString(): string {
const privateKey1 = convertPEMtoDER(this.getPrivateKey());
const txt =
" end point" + this._counter +
" port = " + this.port +
" l = " + this._endpoints.length +
" " + makeSHA1Thumbprint(this.getCertificateChain()).toString("hex") +
" " + makeSHA1Thumbprint(privateKey1).toString("hex");
return txt;
}
function getCertificateChainEP(this: OPCUAServerEndPoint): Certificate {
const certificateFile = path.join(this.certificateManager.rootDir, "own/certs/certificate.pem");
const certificatePEM = fs.readFileSync(certificateFile, "utf8");
const $$certificateChain = convertPEMtoDER(certificatePEM);
const thumbprint = makeSHA1Thumbprint($$certificateChain);
return $$certificateChain;
}