letsencrypt.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * @description MeshCentral letsEncrypt module, uses ACME-Client to do all the work.
  3. * @author Ylian Saint-Hilaire
  4. * @copyright Intel Corporation 2018-2022
  5. * @license Apache-2.0
  6. * @version v0.0.2
  7. */
  8. /*xjslint node: true */
  9. /*xjslint plusplus: true */
  10. /*xjslint maxlen: 256 */
  11. /*jshint node: true */
  12. /*jshint strict: false */
  13. /*jshint esversion: 6 */
  14. 'use strict';
  15. // ACME-Client Implementation
  16. var globalLetsEncrypt = null;
  17. module.exports.CreateLetsEncrypt = function (parent) {
  18. const acme = require('acme-client');
  19. var obj = {};
  20. obj.fs = require('fs');
  21. obj.path = require('path');
  22. obj.parent = parent;
  23. obj.forge = obj.parent.certificateOperations.forge;
  24. obj.leDomains = null;
  25. obj.challenges = {};
  26. obj.runAsProduction = false;
  27. obj.redirWebServerHooked = false;
  28. obj.zerossl = false;
  29. obj.csr = null;
  30. obj.configErr = null;
  31. obj.configOk = false;
  32. obj.pendingRequest = false;
  33. // Let's Encrypt debug logging
  34. obj.log = function (str) {
  35. parent.debug('cert', 'LE: ' + str);
  36. var d = new Date();
  37. obj.events.push(d.toLocaleDateString() + ' ' + d.toLocaleTimeString() + ' - ' + str);
  38. while (obj.events.length > 200) { obj.events.shift(); } // Keep only 200 last events.
  39. }
  40. obj.events = [];
  41. // Setup the certificate storage paths
  42. obj.certPath = obj.path.join(obj.parent.datapath, 'letsencrypt-certs');
  43. try { obj.parent.fs.mkdirSync(obj.certPath); } catch (e) { }
  44. // Hook up GreenLock to the redirection server
  45. if (obj.parent.config.settings.rediraliasport === 80) { obj.redirWebServerHooked = true; }
  46. else if ((obj.parent.config.settings.rediraliasport == null) && (obj.parent.redirserver.port == 80)) { obj.redirWebServerHooked = true; }
  47. // Deal with HTTP challenges
  48. function challengeCreateFn(authz, challenge, keyAuthorization) { if (challenge.type === 'http-01') { obj.challenges[challenge.token] = keyAuthorization; } }
  49. function challengeRemoveFn(authz, challenge, keyAuthorization) { if (challenge.type === 'http-01') { delete obj.challenges[challenge.token]; } }
  50. obj.challenge = function (token, hostname, func) { if (obj.challenges[token] != null) { obj.log("Succesful response to challenge."); } else { obj.log("Failed to respond to challenge, token: " + token + ", table: " + JSON.stringify(obj.challenges) + "."); } func(obj.challenges[token]); }
  51. // Get the current certificate
  52. obj.getCertificate = function(certs, func) {
  53. obj.runAsProduction = (obj.parent.config.letsencrypt.production === true);
  54. obj.zerossl = ((typeof obj.parent.config.letsencrypt.zerossl == 'object') ? obj.parent.config.letsencrypt.zerossl : false);
  55. obj.log("Getting certs from local store (" + (obj.runAsProduction ? "Production" : "Staging") + ")");
  56. if (certs.CommonName.indexOf('.') == -1) { obj.configErr = "Add \"cert\" value to settings in config.json before using Let's Encrypt."; parent.addServerWarning(obj.configErr); obj.log("WARNING: " + obj.configErr); func(certs); return; }
  57. if (obj.parent.config.letsencrypt == null) { obj.configErr = "No Let's Encrypt configuration"; parent.addServerWarning(obj.configErr); obj.log("WARNING: " + obj.configErr); func(certs); return; }
  58. if (obj.parent.config.letsencrypt.email == null) { obj.configErr = "Let's Encrypt email address not specified."; parent.addServerWarning(obj.configErr); obj.log("WARNING: " + obj.configErr); func(certs); return; }
  59. if ((obj.parent.redirserver == null) || ((typeof obj.parent.config.settings.rediraliasport === 'number') && (obj.parent.config.settings.rediraliasport !== 80)) || ((obj.parent.config.settings.rediraliasport == null) && (obj.parent.redirserver.port !== 80))) { obj.configErr = "Redirection web server must be active on port 80 for Let's Encrypt to work."; parent.addServerWarning(obj.configErr); obj.log("WARNING: " + obj.configErr); func(certs); return; }
  60. if (obj.redirWebServerHooked !== true) { obj.configErr = "Redirection web server not setup for Let's Encrypt to work."; parent.addServerWarning(obj.configErr); obj.log("WARNING: " + obj.configErr); func(certs); return; }
  61. if ((obj.parent.config.letsencrypt.rsakeysize != null) && (obj.parent.config.letsencrypt.rsakeysize !== 2048) && (obj.parent.config.letsencrypt.rsakeysize !== 3072)) { obj.configErr = "Invalid Let's Encrypt certificate key size, must be 2048 or 3072."; parent.addServerWarning(obj.configErr); obj.log("WARNING: " + obj.configErr); func(certs); return; }
  62. if (obj.checkInterval == null) { obj.checkInterval = setInterval(obj.checkRenewCertificate, 86400000); } // Call certificate check every 24 hours.
  63. obj.configOk = true;
  64. // Get the list of domains
  65. obj.leDomains = [ certs.CommonName ];
  66. if (obj.parent.config.letsencrypt.names != null) {
  67. if (typeof obj.parent.config.letsencrypt.names == 'string') { obj.parent.config.letsencrypt.names = obj.parent.config.letsencrypt.names.split(','); }
  68. obj.parent.config.letsencrypt.names.map(function (s) { return s.trim(); }); // Trim each name
  69. if ((typeof obj.parent.config.letsencrypt.names != 'object') || (obj.parent.config.letsencrypt.names.length == null)) { console.log("ERROR: Let's Encrypt names must be an array in config.json."); func(certs); return; }
  70. obj.leDomains = obj.parent.config.letsencrypt.names;
  71. }
  72. // Read TLS certificate from the configPath
  73. var certFile = obj.path.join(obj.certPath, (obj.runAsProduction ? 'production.crt' : 'staging.crt'));
  74. var keyFile = obj.path.join(obj.certPath, (obj.runAsProduction ? 'production.key' : 'staging.key'));
  75. if (obj.fs.existsSync(certFile) && obj.fs.existsSync(keyFile)) {
  76. obj.log("Reading certificate files");
  77. // Read the certificate and private key
  78. var certPem = obj.fs.readFileSync(certFile).toString('utf8');
  79. var cert = obj.forge.pki.certificateFromPem(certPem);
  80. var keyPem = obj.fs.readFileSync(keyFile).toString('utf8');
  81. var key = obj.forge.pki.privateKeyFromPem(keyPem);
  82. // Decode the certificate common and alt names
  83. obj.certNames = [cert.subject.getField('CN').value];
  84. var altNames = cert.getExtension('subjectAltName');
  85. if (altNames) { for (var i = 0; i < altNames.altNames.length; i++) { var acn = altNames.altNames[i].value.toLowerCase(); if (obj.certNames.indexOf(acn) == -1) { obj.certNames.push(acn); } } }
  86. // Decode the certificate expire time
  87. obj.certExpire = cert.validity.notAfter;
  88. // Use this certificate when possible on any domain
  89. if (obj.certNames.indexOf(certs.CommonName) >= 0) {
  90. obj.log("Setting LE cert for default domain.");
  91. certs.web.cert = certPem;
  92. certs.web.key = keyPem;
  93. //certs.web.ca = [results.pems.chain];
  94. }
  95. for (var i in obj.parent.config.domains) {
  96. if ((obj.parent.config.domains[i].dns != null) && (obj.parent.certificateOperations.compareCertificateNames(obj.certNames, obj.parent.config.domains[i].dns))) {
  97. obj.log("Setting LE cert for domain " + i + ".");
  98. certs.dns[i].cert = certPem;
  99. certs.dns[i].key = keyPem;
  100. //certs.dns[i].ca = [results.pems.chain];
  101. }
  102. }
  103. } else {
  104. obj.log("No certificate files found");
  105. }
  106. func(certs);
  107. setTimeout(obj.checkRenewCertificate, 5000); // Hold 5 seconds and check if we need to request a certificate.
  108. }
  109. // Check if we need to get a new certificate
  110. // Return 0 = CertOK, 1 = Request:NoCert, 2 = Request:Expire, 3 = Request:MissingNames
  111. obj.checkRenewCertificate = function () {
  112. if (obj.pendingRequest == true) { obj.log("Request for certificate is in process."); return 4; }
  113. if (obj.certNames == null) {
  114. obj.log("Got no certificates, asking for one now.");
  115. obj.requestCertificate();
  116. return 1;
  117. } else {
  118. // Look at the existing certificate to see if we need to renew it
  119. var daysLeft = Math.floor((obj.certExpire - new Date()) / 86400000);
  120. obj.log("Certificate has " + daysLeft + " day(s) left.");
  121. if (daysLeft < 45) {
  122. obj.log("Asking for new certificate because of expire time.");
  123. obj.requestCertificate();
  124. return 2;
  125. } else {
  126. var missingDomain = false;
  127. for (var i in obj.leDomains) {
  128. if (obj.parent.certificateOperations.compareCertificateNames(obj.certNames, obj.leDomains[i]) == false) {
  129. obj.log("Missing name \"" + obj.leDomains[i] + "\".");
  130. missingDomain = true;
  131. }
  132. }
  133. if (missingDomain) {
  134. obj.log("Asking for new certificate because of missing names.");
  135. obj.requestCertificate();
  136. return 3;
  137. } else {
  138. obj.log("Certificate is ok.");
  139. }
  140. }
  141. }
  142. return 0;
  143. }
  144. obj.requestCertificate = function () {
  145. if (obj.pendingRequest == true) return;
  146. if (obj.configOk == false) { obj.log("Can't request cert, invalid configuration."); return; }
  147. if (acme.forge == null) { obj.log("Forge not setup in ACME, unable to continue."); return; }
  148. obj.pendingRequest = true;
  149. // Create a private key
  150. obj.log("Generating private key...");
  151. acme.forge.createPrivateKey().then(function (accountKey) {
  152. // Create the ACME client
  153. obj.log("Setting up ACME client...");
  154. if (obj.zerossl) {
  155. if (obj.zerossl.kid == "") { obj.log("EAB KID hasn't been set, invalid configuration."); return; }
  156. if (obj.zerossl.hmackey == "") { obj.log("EAB HMAC KEY hasn't been set, invalid configuration."); return; }
  157. obj.client = new acme.Client({
  158. directoryUrl: acme.directory.zerossl.production,
  159. accountKey: accountKey,
  160. externalAccountBinding: {
  161. kid: obj.zerossl.kid,
  162. hmacKey: obj.zerossl.hmackey
  163. }
  164. });
  165. } else {
  166. obj.client = new acme.Client({
  167. directoryUrl: obj.runAsProduction ? acme.directory.letsencrypt.production : acme.directory.letsencrypt.staging,
  168. accountKey: accountKey
  169. });
  170. }
  171. // Create Certificate Request (CSR)
  172. obj.log("Creating certificate request...");
  173. var certRequest = { commonName: obj.leDomains[0] };
  174. if (obj.leDomains.length > 1) { certRequest.altNames = obj.leDomains; }
  175. acme.forge.createCsr(certRequest).then(function (r) {
  176. obj.csr = r[1];
  177. obj.tempPrivateKey = r[0];
  178. if(obj.zerossl) { obj.log("Requesting certificate from ZeroSSL..."); } else { obj.log("Requesting certificate from Let's Encrypt..."); }
  179. obj.client.auto({
  180. csr: obj.csr,
  181. email: obj.parent.config.letsencrypt.email,
  182. termsOfServiceAgreed: true,
  183. skipChallengeVerification: (obj.parent.config.letsencrypt.skipchallengeverification === true),
  184. challengeCreateFn,
  185. challengeRemoveFn
  186. }).then(function (cert) {
  187. obj.log("Got certificate.");
  188. // Save certificate and private key to PEM files
  189. var certFile = obj.path.join(obj.certPath, (obj.runAsProduction ? 'production.crt' : 'staging.crt'));
  190. var keyFile = obj.path.join(obj.certPath, (obj.runAsProduction ? 'production.key' : 'staging.key'));
  191. obj.fs.writeFileSync(certFile, cert);
  192. obj.fs.writeFileSync(keyFile, obj.tempPrivateKey);
  193. delete obj.tempPrivateKey;
  194. // Cause a server restart
  195. obj.log("Performing server restart...");
  196. obj.parent.performServerCertUpdate();
  197. }, function (err) {
  198. obj.log("Failed to obtain certificate: " + err.message);
  199. obj.pendingRequest = false;
  200. delete obj.client;
  201. });
  202. }, function (err) {
  203. obj.log("Failed to generate certificate request: " + err.message);
  204. obj.pendingRequest = false;
  205. delete obj.client;
  206. });
  207. }, function (err) {
  208. obj.log("Failed to generate private key: " + err.message);
  209. obj.pendingRequest = false;
  210. delete obj.client;
  211. });
  212. }
  213. // Return the status of this module
  214. obj.getStats = function () {
  215. var r = {
  216. configOk: obj.configOk,
  217. leDomains: obj.leDomains,
  218. challenges: obj.challenges,
  219. production: obj.runAsProduction,
  220. webServer: obj.redirWebServerHooked,
  221. certPath: obj.certPath,
  222. skipChallengeVerification: (obj.parent.config.letsencrypt.skipchallengeverification == true)
  223. };
  224. if (obj.configErr) { r.error = "WARNING: " + obj.configErr; }
  225. if (obj.certExpire) { r.cert = 'Present'; r.daysLeft = Math.floor((obj.certExpire - new Date()) / 86400000); } else { r.cert = 'None'; }
  226. return r;
  227. }
  228. return obj;
  229. }