get-certificate.js 12.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
/*!
 * letiny
 * Copyright(c) 2015 Anatol Sommer <anatol@anatol.at>
 * Some code used from https://github.com/letsencrypt/boulder/tree/master/test/js
 * MPL 2.0
*/
'use strict';

function _toStandardBase64(str) {
  var b64 = str.replace(/-/g, "+").replace(/_/g, "/").replace(/=/g, "");

  switch (b64.length % 4) {
    case 2: b64 += "=="; break;
    case 3: b64 += "="; break;
  }

  return b64;
}

function certBufferToPem(cert) {
  cert = _toStandardBase64(cert.toString('base64'));
  cert = cert.match(/.{1,64}/g).join('\r\n');
  return '-----BEGIN CERTIFICATE-----\r\n'+cert+'\r\n-----END CERTIFICATE-----\r\n';
}

module.exports.create = function (deps) {
  var acmeRequest = deps.acmeRequest;
  var Acme = deps.Acme;
  var RSA = deps.RSA;

  // getCertificate // returns "pems", meaning "certs"
  function getCert(options, cb) {

    function bodyToError(res, body) {
      var err;

      if (!body) {
        err = new Error("[Error] letiny-core: no request body");
        err.code = "E_NO_RESPONSE_BODY";
        throw err;
      }

      if ('{' === body[0] || '{' === String.fromCharCode(body[0])) {
        try {
          body = JSON.parse(body.toString('utf8'));
        } catch(e) {
          err = new Error("[Error] letiny-core: body could not be parsed");
          err.code = "E_BODY_PARSE";
          err.description = body;
          throw err;
        }
      }

      if (Math.floor(res.statusCode / 100) !== 2) {
        err = new Error("[Error] letiny-core: not 200 ok");
        err.code = "E_STATUS_CODE";
        err.type = body.type;
        err.description = body;
        err.detail = body.detail;
        console.error("TODO: modules which depend on this module should expose this error properly but since some of them don't, I expose it here directly:");
        console.error(err.stack);
        console.error(body);
        throw err;
      }

      if (body.type && body.detail) {
        err = new Error("[Error] letiny-core: " + body.detail);
        err.code = body.type;
        err.type = body.type;
        err.description = body.detail;
        err.detail = body.detail;
        throw err;
      }

      return body;
    }

    function nextDomain() {
      if (state.domains.length > 0) {
        getChallenges(state.domains.shift());
        return;
      } else {
        getCertificate();
      }
    }

    function getChallenges(domain) {
      state.domain = domain;

      state.acme.post(state.newAuthzUrl, {
        resource: 'new-authz',
        identifier: {
          type: 'dns',
          value: state.domain,
        }
      }, function (err, res, body) {
        if (!err && res.body) {
          try {
            body = bodyToError(res, body);
          } catch(e) {
            err = e;
          }
        }

        getReadyToValidate(err, res, body);
      });
    }

    function getReadyToValidate(err, res, body) {
      var links;
      var authz;
      var httpChallenges;
      var challenge;
      var thumbprint;
      var keyAuthorization;

      function challengeDone(err) {
        if (err) {
          console.error('[letiny-core] setChallenge Error:');
          console.error(err && err.stack || err);
          ensureValidation(err, null, null, function () {
            options.removeChallenge(state.domain, challenge.token, function () {
              // ignore
            });
          });
          return;
        }

        state.acme.post(state.responseUrl, {
          resource: 'challenge',
          keyAuthorization: keyAuthorization
        }, function(err, res, body) {
          if (!err && res.body) {
            try {
              body = bodyToError(res, body);
            } catch(e) {
              err = e;
            }
          }

          ensureValidation(err, res, body, function unlink() {
            options.removeChallenge(state.domain, challenge.token, function () {
              // ignore
            });
          });
        });
      }

      if (err) {
        return handleErr(err);
      }

      if (Math.floor(res.statusCode/100)!==2) {
        return handleErr(null, 'Authorization request failed ('+res.statusCode+')');
      }

      links = Acme.parseLink(res.headers.link);
      if (!links || !('next' in links)) {
        return handleErr(err, 'Server didn\'t provide information to proceed (2)');
      }

      state.authorizationUrl = res.headers.location;
      state.newCertUrl = links.next;

      authz = body;

      httpChallenges = authz.challenges.filter(function(x) {
        return x.type === options.challengeType;
      });
      if (httpChallenges.length === 0) {
        return handleErr(null, 'Server didn\'t offer any challenge we can handle.');
      }
      challenge = httpChallenges[0];

      thumbprint = RSA.thumbprint(state.accountKeypair);
      keyAuthorization = challenge.token + '.' + thumbprint;

      state.responseUrl = challenge.uri;

      options.setChallenge(state.domain, challenge.token, keyAuthorization, challengeDone);
    }

    function ensureValidation(err, res, body, unlink) {
      var authz, challengesState;

      if (err || Math.floor(res.statusCode/100)!==2) {
        unlink();
        return handleErr(err, 'Authorization status request failed ('
          + (res && res.statusCode || err.code || err.message || err) + ')');
      }

      authz=body;

      if (authz.status==='pending') {
        setTimeout(function() {
          acmeRequest.create()({
            method: 'GET'
          , url: state.authorizationUrl
          }, function(err, res, body) {
            if (!err && res.body) {
              try {
                body = bodyToError(res, body);
              } catch(e) {
                err = e;
              }
            }

            ensureValidation(err, res, body, unlink);
          });
        }, 1000);
      } else if (authz.status==='valid') {
        log('Validating domain ... done');
        state.validatedDomains.push(state.domain);
        state.validAuthorizationUrls.push(state.authorizationUrl);
        unlink();
        nextDomain();
      } else if (authz.status==='invalid') {
        unlink();
        challengesState = (authz.challenges || []).map(function (challenge) {
          var result =  ' - ' + challenge.uri + ' [' + challenge.status + ']';
          if (challenge.error) {
            result += '\n   ' + challenge.error.detail;
          }
          return result;
        }).join('\n');
        return handleErr(null,
            'The CA was unable to validate the file you provisioned. '
          + (authz.detail ? 'Details: ' + authz.detail : '')
          + (challengesState ? '\n' + challengesState : ''), body);
      } else {
        unlink();
        return handleErr(null, 'CA returned an authorization in an unexpected state' + authz.detail, authz);
      }
    }

    function getCertificate() {
      var csr=RSA.generateCsrWeb64(state.certKeypair, state.validatedDomains);
      log('Requesting certificate...');
      state.acme.post(state.newCertUrl, {
        resource:'new-cert',
        csr:csr,
        authorizations:state.validAuthorizationUrls
      }, function (err, res, body ) {
        if (!err && res.body) {
          try {
            body = bodyToError(res, body);
          } catch(e) {
            err = e;
          }
        }

        downloadCertificate(err, res, body);
      });
    }

    function downloadCertificate(err, res, body) {
      var links, certUrl;

      if (err) {
        handleErr(err, 'Certificate request failed');
        return;
      }

      if (Math.floor(res.statusCode/100)!==2) {
        err = new Error("invalid status code: " + res.statusCode);
        err.code = "E_STATUS_CODE";
        err.description = body;
        handleErr(err);
        return;
      }

      links=Acme.parseLink(res.headers.link);
      if (!links || !('up' in links)) {
        return handleErr(err, 'Failed to fetch issuer certificate');
      }

      log('Requesting certificate: done');

      state.certificate=body;
      certUrl=res.headers.location;
      acmeRequest.create()({
        method: 'GET'
      , url: certUrl
      , encoding: null
      }, function(err, res, body) {
        if (!err) {
          try {
            body = bodyToError(res, body);
          } catch(e) {
            err = e;
          }
        }

        if (err) {
          return handleErr(err, 'Failed to fetch cert from '+certUrl);
        }

        if (res.statusCode!==200) {
          return handleErr(err, 'Failed to fetch cert from '+certUrl, res.body.toString());
        }

        if (body.toString()!==state.certificate.toString()) {
          return handleErr(null, 'Cert at '+certUrl+' did not match returned cert');
        }

        log('Successfully verified cert at '+certUrl);
        downloadIssuerCert(links);
      });
    }

    function downloadIssuerCert(links) {
      log('Requesting issuer certificate...');
      acmeRequest.create()({
        method: 'GET'
      , url: links.up
      , encoding: null
      }, function(err, res, body) {
        if (!err) {
          try {
            body = bodyToError(res, body);
          } catch(e) {
            err = e;
          }
        }

        if (err || res.statusCode!==200) {
          return handleErr(err, 'Failed to fetch issuer certificate');
        }

        state.chainPem = certBufferToPem(body);
        log('Requesting issuer certificate: done');
        done();
      });
    }

    function done() {
      var privkeyPem = RSA.exportPrivatePem(state.certKeypair);

      cb(null, {
        cert: certBufferToPem(state.certificate)
      , privkey: privkeyPem
      , chain: state.chainPem
        // TODO nix backwards compat
      , key: privkeyPem
      , ca: state.chainPem
      });
    }

    function handleErr(err, text, info) {
      log(text, err, info);
      cb(err || new Error(text));
    }

    var NOOP = function () {};
    var log = options.debug ? console.log : NOOP;
    var state={
      validatedDomains:[]
    , validAuthorizationUrls:[]
    , newAuthzUrl: options.newAuthzUrl
    , newCertUrl: options.newCertUrl
    };

    if (!options.challengeType) {
      options.challengeType = 'http-01';
    }
    if (-1 === [ 'http-01', 'tls-sni-01', 'dns-01' ].indexOf(options.challengeType)) {
      return handleErr(new Error("options.challengeType '" + options.challengeType + "' is not yet supported"));
    }
    if (!options.newAuthzUrl) {
      return handleErr(new Error("options.newAuthzUrl must be the authorization url"));
    }
    if (!options.newCertUrl) {
      return handleErr(new Error("options.newCertUrl must be the new certificate url"));
    }
    if (!options.accountKeypair) {
      if (!options.accountPrivateKeyPem) {
        return handleErr(new Error("options.accountKeypair must be an object with `privateKeyPem` and/or `privateKeyJwk`"));
      }
      console.warn("'accountPrivateKeyPem' is deprecated. Use options.accountKeypair.privateKeyPem instead.");
      options.accountKeypair = RSA.import({ privateKeyPem: options.accountPrivateKeyPem });
    }
    if (!options.domainKeypair) {
      if (!options.domainPrivateKeyPem) {
        return handleErr(new Error("options.domainKeypair must be an object with `privateKeyPem` and/or `privateKeyJwk`"));
      }
      console.warn("'domainPrivateKeyPem' is deprecated. Use options.domainKeypair.privateKeyPem instead.");
      options.domainKeypair = RSA.import({ privateKeyPem: options.domainPrivateKeyPem });
    }
    if (!options.setChallenge) {
      return handleErr(new Error("options.setChallenge must be function(hostname, challengeKey, tokenValue, done) {}"));
    }
    if (!options.removeChallenge) {
      return handleErr(new Error("options.removeChallenge must be function(hostname, challengeKey, done) {}"));
    }
    if (!(options.domains && options.domains.length)) {
      return handleErr(new Error("options.domains must be an array of domains such as ['example.com', 'www.example.com']"));
    }

    state.domains = options.domains.slice(0); // copy array
    try {
      state.accountKeypair = options.accountKeypair;
      state.certKeypair = options.domainKeypair;
      state.acme = new Acme(state.accountKeypair);
    } catch(err) {
      return handleErr(err, 'Failed to parse privateKey');
    }

    nextDomain();
  }

  return getCert;
};