When some files are re-generated, the old copy is now kept as -old.
Ylian Saint-Hilaire committed
Aug 26, 2022 at 14:19 UTC
15ee1feca5ca24b2e6590382b0f8414c5f898030
3 files changed
+21
certoperations.js
+8
@@ -815,12 +815,14 @@ module.exports.CertificateOperations = function (parent) {
815
var xext = xroot.getExtension('keyUsage');
816
if ((xext == null) || (xext.keyCertSign !== true) || (xroot.serialNumber == '')) {
817
// We need to fix this certificate
818
+ parent.common.moveOldFiles(['root-cert-public-backup.crt']);
819
obj.fs.writeFileSync(parent.getConfigFilePath('root-cert-public-backup.crt'), rootCertificate);
820
if (xroot.serialNumber == '') { console.log("Fixing root certificate to add serial number..."); xroot.serialNumber = '' + require('crypto').randomBytes(4).readUInt32BE(0); }
821
if ((xext == null) || (xext.keyCertSign !== true)) { console.log("Fixing root certificate to add signing key usage..."); xroot.setExtensions([{ name: 'basicConstraints', cA: true }, { name: 'subjectKeyIdentifier' }, { name: 'keyUsage', keyCertSign: true }]); }
822
var xrootPrivateKey = obj.pki.privateKeyFromPem(rootPrivateKey);
823
xroot.sign(xrootPrivateKey, obj.forge.md.sha384.create());
824
r.root.cert = obj.pki.certificateToPem(xroot);
825
+ parent.common.moveOldFiles([parent.getConfigFilePath('root-cert-public.crt')]);
826
try { obj.fs.writeFileSync(parent.getConfigFilePath('root-cert-public.crt'), r.root.cert); } catch (ex) { }
827
}
828
}
@@ -1036,6 +1038,7 @@ module.exports.CertificateOperations = function (parent) {
1038
}
1039
rootCertificate = obj.pki.certificateToPem(rootCertAndKey.cert);
1040
rootPrivateKey = obj.pki.privateKeyToPem(rootCertAndKey.key);
1041
+ parent.common.moveOldFiles([parent.getConfigFilePath('root-cert-public.crt'), parent.getConfigFilePath('root-cert-private.key')]);
1042
obj.fs.writeFileSync(parent.getConfigFilePath('root-cert-public.crt'), rootCertificate);
1043
obj.fs.writeFileSync(parent.getConfigFilePath('root-cert-private.key'), rootPrivateKey);
1044
} else {
@@ -1053,6 +1056,7 @@ module.exports.CertificateOperations = function (parent) {
1056
webCertAndKey = obj.IssueWebServerCertificate(rootCertAndKey, false, commonName, country, organization, null, strongCertificate);
1057
webCertificate = obj.pki.certificateToPem(webCertAndKey.cert);
1058
webPrivateKey = obj.pki.privateKeyToPem(webCertAndKey.key);
1059
+ parent.common.moveOldFiles([parent.getConfigFilePath('webserver-cert-public.crt'), parent.getConfigFilePath('webserver-cert-private.key')]);
1060
obj.fs.writeFileSync(parent.getConfigFilePath('webserver-cert-public.crt'), webCertificate);
1061
obj.fs.writeFileSync(parent.getConfigFilePath('webserver-cert-private.key'), webPrivateKey);
1062
} else {
@@ -1075,6 +1079,7 @@ module.exports.CertificateOperations = function (parent) {
1079
agentCertAndKey = obj.IssueWebServerCertificate(rootCertAndKey, true, 'MeshCentralAgentServer', country, organization, { }, strongCertificate);
1080
agentCertificate = obj.pki.certificateToPem(agentCertAndKey.cert);
1081
agentPrivateKey = obj.pki.privateKeyToPem(agentCertAndKey.key);
1082
+ parent.common.moveOldFiles([parent.getConfigFilePath('agentserver-cert-public.crt'), parent.getConfigFilePath('agentserver-cert-private.key')]);
1083
obj.fs.writeFileSync(parent.getConfigFilePath('agentserver-cert-public.crt'), agentCertificate);
1084
obj.fs.writeFileSync(parent.getConfigFilePath('agentserver-cert-private.key'), agentPrivateKey);
1085
} else {
@@ -1091,6 +1096,7 @@ module.exports.CertificateOperations = function (parent) {
1096
codesignCertAndKey = obj.IssueWebServerCertificate(rootCertAndKey, true, commonName, country, organization, { codeSign: true }, strongCertificate);
1097
codesignCertificate = obj.pki.certificateToPem(codesignCertAndKey.cert);
1098
codesignPrivateKey = obj.pki.privateKeyToPem(codesignCertAndKey.key);
1099
+ parent.common.moveOldFiles([parent.getConfigFilePath('codesign-cert-public.crt'), parent.getConfigFilePath('codesign-cert-private.key')]);
1100
obj.fs.writeFileSync(parent.getConfigFilePath('codesign-cert-public.crt'), codesignCertificate);
1101
obj.fs.writeFileSync(parent.getConfigFilePath('codesign-cert-private.key'), codesignPrivateKey);
1102
} else {
@@ -1107,6 +1113,7 @@ module.exports.CertificateOperations = function (parent) {
1113
mpsCertAndKey = obj.IssueWebServerCertificate(rootCertAndKey, false, mpsCommonName, mpsCountry, mpsOrganization, null, false);
1114
mpsCertificate = obj.pki.certificateToPem(mpsCertAndKey.cert);
1115
mpsPrivateKey = obj.pki.privateKeyToPem(mpsCertAndKey.key);
1116
+ parent.common.moveOldFiles([parent.getConfigFilePath('mpsserver-cert-public.crt'), parent.getConfigFilePath('mpsserver-cert-private.key')]);
1117
obj.fs.writeFileSync(parent.getConfigFilePath('mpsserver-cert-public.crt'), mpsCertificate);
1118
obj.fs.writeFileSync(parent.getConfigFilePath('mpsserver-cert-private.key'), mpsPrivateKey);
1119
} else {
@@ -1155,6 +1162,7 @@ module.exports.CertificateOperations = function (parent) {
1162
var xwebCertAndKey = obj.IssueWebServerCertificate(rootCertAndKey, false, dnsname, country, organization, null, strongCertificate);
1163
var xwebCertificate = obj.pki.certificateToPem(xwebCertAndKey.cert);
1164
var xwebPrivateKey = obj.pki.privateKeyToPem(xwebCertAndKey.key);
1165
+ parent.common.moveOldFiles([ parent.getConfigFilePath('webserver-' + i + '-cert-public.crt'), parent.getConfigFilePath('webserver-' + i + '-cert-private.key') ]);
1166
obj.fs.writeFileSync(parent.getConfigFilePath('webserver-' + i + '-cert-public.crt'), xwebCertificate);
1167
obj.fs.writeFileSync(parent.getConfigFilePath('webserver-' + i + '-cert-private.key'), xwebPrivateKey);
1168
r.dns[i] = { cert: xwebCertificate, key: xwebPrivateKey };
common.js
+12
@@ -358,3 +358,15 @@ module.exports.parseVersion = function (verstr) {
358
}
359
return r;
360
}
361
+
362
+// Move old files. If we are about to overwrite a file, we can move if first just in case the change needs to be reverted
363
+module.exports.moveOldFiles = function (filelist) {
364
+ // Fine an old extension that works for all files in the file list
365
+ var oldFileExt, oldFileExtCount = 0, extOk;
366
+ do {
367
+ extOk = true;
368
+ if (++oldFileExtCount == 1) { oldFileExt = '-old'; } else { oldFileExt = '-old' + oldFileExtCount; }
369
+ for (var i in filelist) { if (fs.existsSync(filelist[i] + oldFileExt) == true) { extOk = false; } }
370
+ } while (extOk == false);
371
+ for (var i in filelist) { try { fs.renameSync(filelist[i], filelist[i] + oldFileExt); } catch (ex) { } }
372
+}
\ No newline at end of file
meshcentral.js
+1
@@ -1785,6 +1785,7 @@ function CreateMeshCentralServer(config, args) {
1785
if ((vapidKeys == null) || (typeof vapidKeys.publicKey != 'string') || (typeof vapidKeys.privateKey != 'string')) {
1786
console.log("Generating web push VAPID keys...");
1787
vapidKeys = obj.webpush.generateVAPIDKeys();
1788
+ obj.common.moveOldFiles([obj.path.join(obj.datapath, 'vapid.json')]);
1789
obj.fs.writeFileSync(obj.path.join(obj.datapath, 'vapid.json'), JSON.stringify(vapidKeys));
1790
}
1791
obj.webpush.vapidPublicKey = vapidKeys.publicKey;