le-acme-core.js/lib/client.js

480 lines
13 KiB
JavaScript
Raw Normal View History

2015-12-13 16:50:04 +00:00
/*!
* 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';
var _DEBUG, NOOP=new Function(), log=NOOP,
mkdirp=require('mkdirp').sync, request=require('request'),
forge=require('node-forge'), pki=forge.pki,
cryptoUtil=require('./crypto-util'), util=require('./acme-util'),
fs=require('fs'), path=require('path'), child=require('child_process');
2015-12-13 16:50:04 +00:00
function Acme(privateKey) {
this.privateKey=privateKey;
this.nonces=[];
}
Acme.prototype.getNonce=function(url, cb) {
var self=this;
request.head({
url:url,
}, function(err, res, body) {
if (err) {
return cb(err);
}
if (res && 'replay-nonce' in res.headers) {
log('Storing nonce: '+res.headers['replay-nonce']);
self.nonces.push(res.headers['replay-nonce']);
cb();
return;
}
cb(new Error('Failed to get nonce for request'));
});
};
Acme.prototype.post=function(url, body, cb) {
var self=this, payload, jws, signed;
if (this.nonces.length===0) {
this.getNonce(url, function(err) {
if (err) {
return cb(err);
}
self.post(url, body, cb);
});
return;
}
log('Using nonce: '+this.nonces[0]);
payload=JSON.stringify(body, null, 2);
jws=cryptoUtil.generateSignature(
this.privateKey, new Buffer(payload), this.nonces.shift()
);
signed=JSON.stringify(jws, null, 2);
log('Posting to '+url);
log(signed.green);
log('Payload:'+payload.blue);
return request.post({
url:url,
body:signed,
encoding:null
}, function(err, res, body) {
var parsed;
if (err) {
console.error(err);
return cb(err);
}
if (res) {
log(('HTTP/1.1 '+res.statusCode).yellow);
}
Object.keys(res.headers).forEach(function(key) {
var value, upcased;
value=res.headers[key];
upcased=key.charAt(0).toUpperCase()+key.slice(1);
log((upcased+': '+value).yellow);
});
if (body && !body.toString().match(/[^\x00-\x7F]/)) {
try {
parsed=JSON.parse(body);
log(JSON.stringify(parsed, null, 2).cyan);
} catch(err) {
log(body.toString().cyan);
}
}
if ('replay-nonce' in res.headers) {
log('Storing nonce: '+res.headers['replay-nonce']);
self.nonces.push(res.headers['replay-nonce']);
}
cb(err, res, body);
});
};
2015-12-15 12:47:14 +00:00
function registerNewAccount(options, cb) {
2015-12-15 12:42:56 +00:00
var state = {};
if (!options.accountPrivateKeyPem) {
return handleErr(new Error("options.accountPrivateKeyPem must be an ascii private key pem"));
}
2015-12-15 11:02:19 +00:00
if (!options.agreeToTerms) {
cb(new Error("options.agreeToTerms must be function (tosUrl, fn => (err, true))"));
return;
}
2015-12-15 11:02:19 +00:00
if (!options.newReg) {
cb(new Error("options.newReg must be the a new registration url"));
return;
}
2015-12-15 11:02:19 +00:00
if (!options.email) {
cb(new Error("options.email must be an email"));
return;
2015-12-13 16:50:04 +00:00
}
2015-12-15 12:42:56 +00:00
state.accountKeyPem=options.accountPrivateKeyPem;
2015-12-15 13:23:30 +00:00
state.accountKeyPair=cryptoUtil.importPemPrivateKey(state.accountKeyPem);
2015-12-15 12:42:56 +00:00
state.acme=new Acme(state.accountKeyPair);
2015-12-15 11:02:19 +00:00
register();
2015-12-13 16:50:04 +00:00
function register() {
2015-12-15 11:02:19 +00:00
state.acme.post(options.newReg, {
2015-12-13 16:50:04 +00:00
resource:'new-reg',
contact:['mailto:'+options.email]
}, getTerms);
}
function getTerms(err, res) {
var links;
if (err || Math.floor(res.statusCode/100)!==2) {
2015-12-15 13:23:30 +00:00
return handleErr(err, 'Registration request failed: ' + res.body.toString('utf8'));
2015-12-13 16:50:04 +00:00
}
links=parseLink(res.headers['link']);
if (!links || !('next' in links)) {
return handleErr(err, 'Server didn\'t provide information to proceed (1)');
}
state.registrationURL=res.headers['location'];
state.newAuthorizationURL=links['next'];
state.termsRequired=('terms-of-service' in links);
if (state.termsRequired) {
state.termsURL=links['terms-of-service'];
options.agreeToTerms(state.termsURL, function (err, agree) {
2015-12-15 11:02:19 +00:00
if (err) {
return handleErr(err);
}
if (!agree) {
return handleErr(new Error("You must agree to the terms of use at '" + state.termsURL + "'"));
}
state.agreeTerms = agree;
state.termsURL=links['terms-of-service'];
log(state.termsURL);
request.get(state.termsURL, getAgreement);
});
2015-12-13 16:50:04 +00:00
} else {
2015-12-15 11:02:19 +00:00
cb();
2015-12-13 16:50:04 +00:00
}
}
2015-12-15 11:02:19 +00:00
function getAgreement(err/*, res, body*/) {
2015-12-13 16:50:04 +00:00
if (err) {
return handleErr(err, 'Couldn\'t get agreement');
}
log('The CA requires your agreement to terms:\n'+state.termsURL);
sendAgreement();
}
function sendAgreement() {
2015-12-15 11:02:19 +00:00
if (state.termsRequired && !state.agreeTerms) {
2015-12-13 16:50:04 +00:00
return handleErr(null, 'The CA requires your agreement to terms: '+state.termsURL);
}
log('Posting agreement to: '+state.registrationURL);
2015-12-15 11:02:19 +00:00
state.acme.post(state.registrationURL, {
2015-12-13 16:50:04 +00:00
resource:'reg',
agreement:state.termsURL
}, function(err, res, body) {
if (err || Math.floor(res.statusCode/100)!==2) {
return handleErr(err, 'Couldn\'t POST agreement back to server', body);
} else {
2015-12-15 11:02:19 +00:00
cb(null, body);
2015-12-13 16:50:04 +00:00
}
});
}
2015-12-15 11:02:19 +00:00
function handleErr(err, text, info) {
log(text, err, info);
cb(err || new Error(text));
}
}
function getCert(options, cb) {
var state={
validatedDomains:[],
validAuthorizationURLs:[]
};
if (!options.accountPrivateKeyPem) {
return handleErr(new Error("options.accountPrivateKeyPem must be an ascii private key pem"));
}
if (!options.domainPrivateKeyPem) {
return handleErr(new Error("options.domainPrivateKeyPem must be an ascii private key pem"));
}
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) {}"));
}
2015-12-15 12:42:56 +00:00
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']"));
}
2015-12-15 11:02:19 +00:00
2015-12-15 12:42:56 +00:00
state.domains = options.domains.slice(0); // copy array
2015-12-15 11:02:19 +00:00
try {
state.accountKeyPem=options.accountPrivateKeyPem;
2015-12-15 13:23:30 +00:00
state.accountKeyPair=cryptoUtil.importPemPrivateKey(state.accountKeyPem);
2015-12-15 11:02:19 +00:00
state.acme=new Acme(state.accountKeyPair);
2015-12-15 13:23:30 +00:00
state.certPrivateKeyPem=options.domainPrivateKeyPem;
state.certPrivateKey=cryptoUtil.importPemPrivateKey(state.certPrivateKeyPem);
2015-12-15 11:02:19 +00:00
} catch(err) {
return handleErr(err, 'Failed to parse privateKey');
}
nextDomain();
2015-12-13 16:50:04 +00:00
function nextDomain() {
if (state.domains.length > 0) {
getChallenges(state.domains.shift());
return;
} else {
getCertificate();
}
}
function getChallenges(domain) {
state.domain=domain;
2015-12-15 11:02:19 +00:00
state.acme.post(state.newAuthorizationURL, {
2015-12-13 16:50:04 +00:00
resource:'new-authz',
identifier:{
type:'dns',
value:state.domain,
}
}, getReadyToValidate);
}
function getReadyToValidate(err, res, body) {
var links, authz, httpChallenges, challenge, thumbprint, keyAuthorization, challengePath;
if (err || Math.floor(res.statusCode/100)!==2) {
2015-12-13 17:22:16 +00:00
return handleErr(err, 'Authorization request failed ('+res.statusCode+')');
2015-12-13 16:50:04 +00:00
}
links=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.newCertificateURL=links['next'];
authz=JSON.parse(body);
httpChallenges=authz.challenges.filter(function(x) {
return x.type==='http-01';
});
if (httpChallenges.length===0) {
return handleErr(null, 'Server didn\'t offer any challenge we can handle.');
}
challenge=httpChallenges[0];
thumbprint=cryptoUtil.thumbprint(state.accountKeyPair.publicKey);
keyAuthorization=challenge.token+'.'+thumbprint;
challengePath='.well-known/acme-challenge/'+challenge.token;
state.responseURL=challenge['uri'];
state.path=challengePath;
2015-12-15 11:02:19 +00:00
options.setChallenge(state.domain, '/'+challengePath, keyAuthorization, challengeDone);
2015-12-13 16:50:04 +00:00
function challengeDone() {
2015-12-15 11:02:19 +00:00
state.acme.post(state.responseURL, {
2015-12-13 16:50:04 +00:00
resource:'challenge',
keyAuthorization:keyAuthorization
}, function(err, res, body) {
ensureValidation(err, res, body, function unlink() {
2015-12-15 11:02:19 +00:00
options.removeChallenge(state.domain, '/'+challengePath, function () {
// ignore
});
2015-12-13 16:50:04 +00:00
});
});
}
}
function ensureValidation(err, res, body, unlink) {
var authz;
if (err || Math.floor(res.statusCode/100)!==2) {
unlink();
return handleErr(err, 'Authorization status request failed ('+res.statusCode+')');
}
authz=JSON.parse(body);
if (authz.status==='pending') {
setTimeout(function() {
request.get(state.authorizationURL, {}, function(err, res, body) {
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();
return handleErr(null, 'The CA was unable to validate the file you provisioned', body);
} else {
unlink();
return handleErr(null, 'CA returned an authorization in an unexpected state', authz);
}
}
function getCertificate() {
var csr=cryptoUtil.generateCSR(state.certPrivateKey, state.validatedDomains);
log('Requesting certificate...');
2015-12-15 11:02:19 +00:00
state.acme.post(state.newCertificateURL, {
2015-12-13 16:50:04 +00:00
resource:'new-cert',
csr:csr,
authorizations:state.validAuthorizationURLs
}, downloadCertificate);
}
function downloadCertificate(err, res, body) {
var links, certURL;
if (err || Math.floor(res.statusCode/100)!==2) {
log('Certificate request failed with error ', err);
if (body) {
log(body.toString());
}
return handleErr(err, 'Certificate request failed');
}
links=parseLink(res.headers['link']);
if (!links || !('up' in links)) {
2015-12-13 17:22:16 +00:00
return handleErr(err, 'Failed to fetch issuer certificate');
2015-12-13 16:50:04 +00:00
}
log('Requesting certificate: done');
state.certificate=body;
certURL=res.headers['location'];
request.get({
url:certURL,
encoding:null
}, function(err, res, body) {
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()) {
handleErr(null, 'Cert at '+certURL+' did not match returned cert');
} else {
log('Successfully verified cert at '+certURL);
2015-12-13 17:22:16 +00:00
log('Requesting issuer certificate...');
2015-12-13 16:50:04 +00:00
request.get({
url:links['up'],
encoding:null
}, function(err, res, body) {
if (err || res.statusCode!==200) {
2015-12-13 17:22:16 +00:00
return handleErr(err, 'Failed to fetch issuer certificate');
2015-12-13 16:50:04 +00:00
}
state.caCert=certBufferToPEM(body);
2015-12-13 17:22:16 +00:00
log('Requesting issuer certificate: done');
2015-12-13 16:50:04 +00:00
done();
});
}
});
}
function done() {
2015-12-15 11:02:19 +00:00
var cert;
2015-12-13 16:50:04 +00:00
try {
cert=certBufferToPEM(state.certificate);
2015-12-15 11:02:19 +00:00
} catch(e) {
console.error(e.stack);
//cb(new Error("Could not write output files. Please check permissions!"));
handleErr(e, 'Could not write output files. Please check permissions!');
return;
2015-12-13 16:50:04 +00:00
}
2015-12-15 11:02:19 +00:00
cb(null, {
cert: cert
2015-12-15 13:23:30 +00:00
, key: state.certPrivateKeyPem
2015-12-15 11:02:19 +00:00
, ca: state.caCert
});
2015-12-13 16:50:04 +00:00
}
function handleErr(err, text, info) {
log(text, err, info);
cb(err || new Error(text));
}
}
function certBufferToPEM(cert) {
cert=util.toStandardB64(cert.toString('base64'));
cert=cert.match(/.{1,64}/g).join('\n');
return '-----BEGIN CERTIFICATE-----\n'+cert+'\n-----END CERTIFICATE-----';
}
function parseLink(link) {
var links;
try {
links=link.split(',').map(function(link) {
var parts, url, info;
parts=link.trim().split(';');
url=parts.shift().replace(/[<>]/g, '');
info=parts.reduce(function(acc, p) {
var m=p.trim().match(/(.+) *= *"(.+)"/);
if (m) {
acc[m[1]]=m[2];
}
return acc;
}, {});
info['url']=url;
return info;
}).reduce(function(acc, link) {
if ('rel' in link) {
acc[link['rel']]=link['url'];
}
return acc;
}, {});
return links;
} catch(err) {
return null;
}
}
if (~process.argv.indexOf('--letiny-fork')) {
process.on('message', function(msg) {
if (msg.request) {
getCert(msg.request.options, function(err, cert, key, ca) {
process.send({
result:{
err:err ? err.stack : null,
cert:cert,
key:key,
ca:ca
}
});
});
}
});
}
2015-12-15 11:02:19 +00:00
exports.registerNewAccount=registerNewAccount;
2015-12-13 16:50:04 +00:00
exports.getCert=getCert;