2017-01-14 17:36:33 +00:00
|
|
|
import * as q from 'q'
|
|
|
|
|
|
|
|
import * as plugins from './smartacme.plugins'
|
|
|
|
import * as helpers from './smartacme.helpers'
|
|
|
|
|
|
|
|
import { SmartAcme, IRsaKeypair } from './smartacme.classes.smartacme'
|
|
|
|
import { AcmeAccount } from './smartacme.classes.acmeaccount'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* types of challenges supported by letsencrypt and this module
|
|
|
|
*/
|
|
|
|
export type TChallengeType = 'dns-01' | 'http-01'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* values that a challenge's status can have
|
|
|
|
*/
|
|
|
|
export type TChallengeStatus = 'pending'
|
|
|
|
|
|
|
|
export interface ISmartAcmeChallenge {
|
|
|
|
uri: string
|
|
|
|
status: TChallengeStatus
|
|
|
|
type: TChallengeType
|
|
|
|
token: string
|
|
|
|
keyAuthorization: string
|
|
|
|
}
|
|
|
|
|
2017-01-25 01:45:48 +00:00
|
|
|
export interface ISmartAcmeChallengeChosen extends ISmartAcmeChallenge {
|
2017-01-15 21:30:33 +00:00
|
|
|
dnsKeyHash: string
|
|
|
|
domainName: string
|
|
|
|
domainNamePrefixed: string
|
2017-01-14 17:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface IAcmeCsrConstructorOptions {
|
|
|
|
bit: number,
|
|
|
|
key: string,
|
|
|
|
domain: string,
|
|
|
|
country: string,
|
|
|
|
country_short: string,
|
|
|
|
locality: string,
|
|
|
|
organization: string,
|
|
|
|
organization_short: string,
|
|
|
|
password: string,
|
|
|
|
unstructured: string,
|
|
|
|
subject_alt_names: string[]
|
|
|
|
}
|
2017-01-01 23:18:51 +00:00
|
|
|
|
2017-01-15 21:30:33 +00:00
|
|
|
// Dnsly instance (we really just need one)
|
|
|
|
let myDnsly = new plugins.dnsly.Dnsly('google')
|
|
|
|
|
2017-01-14 13:14:50 +00:00
|
|
|
/**
|
|
|
|
* class AcmeCert represents a cert for domain
|
|
|
|
*/
|
2017-01-01 23:18:51 +00:00
|
|
|
export class AcmeCert {
|
2017-01-15 11:21:29 +00:00
|
|
|
domainName: string
|
2017-01-14 17:36:33 +00:00
|
|
|
attributes
|
2017-01-14 13:14:50 +00:00
|
|
|
fullchain: string
|
2017-01-14 17:36:33 +00:00
|
|
|
parentAcmeAccount: AcmeAccount
|
|
|
|
csr
|
|
|
|
validFrom: Date
|
|
|
|
validTo: Date
|
|
|
|
keypair: IRsaKeypair
|
|
|
|
keyPairFinal: IRsaKeypair
|
2017-01-25 01:45:48 +00:00
|
|
|
chosenChallenge: ISmartAcmeChallengeChosen
|
|
|
|
dnsKeyHash: string
|
2017-01-15 11:21:29 +00:00
|
|
|
constructor(optionsArg: IAcmeCsrConstructorOptions, parentAcmeAccount: AcmeAccount) {
|
|
|
|
this.domainName = optionsArg.domain
|
|
|
|
this.parentAcmeAccount = parentAcmeAccount
|
2017-01-14 17:36:33 +00:00
|
|
|
this.keypair = helpers.createKeypair(optionsArg.bit)
|
|
|
|
let privateKeyForged = plugins.nodeForge.pki.privateKeyFromPem(this.keypair.privateKey)
|
|
|
|
let publicKeyForged = plugins.nodeForge.pki.publicKeyToPem(
|
|
|
|
plugins.nodeForge.pki.setRsaPublicKey(privateKeyForged.n, privateKeyForged.e)
|
|
|
|
)
|
|
|
|
this.keyPairFinal = {
|
|
|
|
privateKey: privateKeyForged,
|
|
|
|
publicKey: publicKeyForged
|
|
|
|
}
|
|
|
|
|
|
|
|
// set dates
|
|
|
|
this.validFrom = new Date()
|
|
|
|
this.validTo = new Date()
|
|
|
|
this.validTo.setDate(this.validFrom.getDate() + 90)
|
|
|
|
|
|
|
|
// set attributes
|
|
|
|
this.attributes = [
|
|
|
|
{ name: 'commonName', value: optionsArg.domain },
|
|
|
|
{ name: 'countryName', value: optionsArg.country },
|
|
|
|
{ shortName: 'ST', value: optionsArg.country_short },
|
|
|
|
{ name: 'localityName', value: optionsArg.locality },
|
|
|
|
{ name: 'organizationName', value: optionsArg.organization },
|
|
|
|
{ shortName: 'OU', value: optionsArg.organization_short },
|
|
|
|
{ name: 'challengePassword', value: optionsArg.password },
|
|
|
|
{ name: 'unstructuredName', value: optionsArg.unstructured }
|
|
|
|
]
|
|
|
|
|
|
|
|
// set up csr
|
|
|
|
this.csr = plugins.nodeForge.pki.createCertificationRequest()
|
|
|
|
this.csr.setSubject(this.attributes)
|
|
|
|
this.csr.setAttributes(this.attributes)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* requests a challenge for a domain
|
|
|
|
* @param domainNameArg - the domain name to request a challenge for
|
|
|
|
* @param challengeType - the challenge type to request
|
|
|
|
*/
|
2017-01-15 11:21:29 +00:00
|
|
|
requestChallenge(challengeTypeArg: TChallengeType = 'dns-01') {
|
2017-01-25 01:45:48 +00:00
|
|
|
let done = q.defer<ISmartAcmeChallengeChosen>()
|
2017-01-14 17:36:33 +00:00
|
|
|
this.parentAcmeAccount.parentSmartAcme.rawacmeClient.newAuthz(
|
|
|
|
{
|
|
|
|
identifier: {
|
|
|
|
type: 'dns',
|
2017-01-15 11:21:29 +00:00
|
|
|
value: this.domainName
|
2017-01-14 17:36:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
this.parentAcmeAccount.parentSmartAcme.keyPair,
|
|
|
|
(err, res) => {
|
|
|
|
if (err) {
|
|
|
|
console.error('smartacme: something went wrong:')
|
|
|
|
console.log(err)
|
|
|
|
done.reject(err)
|
|
|
|
}
|
2017-01-25 01:45:48 +00:00
|
|
|
let preChosenChallenge = res.body.challenges.filter(x => {
|
2017-01-14 17:36:33 +00:00
|
|
|
return x.type === challengeTypeArg
|
|
|
|
})[0]
|
2017-01-25 01:45:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the key is needed to accept the challenge
|
|
|
|
*/
|
|
|
|
let authKey: string = plugins.rawacme.keyAuthz(
|
|
|
|
preChosenChallenge.token,
|
|
|
|
this.parentAcmeAccount.parentSmartAcme.keyPair.publicKey
|
|
|
|
)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* needed in case selected challenge is of type dns-01
|
|
|
|
*/
|
|
|
|
this.dnsKeyHash = plugins.rawacme.dnsKeyAuthzHash(authKey) // needed if dns challenge is chosen
|
|
|
|
/**
|
|
|
|
* the return challenge
|
|
|
|
*/
|
|
|
|
this.chosenChallenge = {
|
|
|
|
uri: preChosenChallenge.uri,
|
|
|
|
type: preChosenChallenge.type,
|
|
|
|
token: preChosenChallenge.token,
|
|
|
|
keyAuthorization: authKey,
|
|
|
|
status: preChosenChallenge.status,
|
|
|
|
dnsKeyHash: this.dnsKeyHash,
|
|
|
|
domainName: this.domainName,
|
|
|
|
domainNamePrefixed: helpers.prefixName(this.domainName)
|
|
|
|
}
|
|
|
|
done.resolve(this.chosenChallenge)
|
2017-01-14 17:36:33 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
return done.promise
|
|
|
|
}
|
|
|
|
|
2017-01-15 21:30:33 +00:00
|
|
|
/**
|
2017-01-15 21:59:58 +00:00
|
|
|
* checks if DNS records are set, will go through a max of 30 cycles
|
2017-01-15 21:30:33 +00:00
|
|
|
*/
|
2017-01-15 21:59:58 +00:00
|
|
|
async checkDns(cycleArg = 1) {
|
2017-01-25 01:45:48 +00:00
|
|
|
let redoCheck = async (err?) => {
|
2017-01-15 21:59:58 +00:00
|
|
|
if (cycleArg < 30) {
|
|
|
|
cycleArg++
|
2017-01-22 20:50:04 +00:00
|
|
|
await plugins.smartdelay.delayFor(2000)
|
|
|
|
return await this.checkDns(cycleArg)
|
2017-01-15 21:59:58 +00:00
|
|
|
} else {
|
|
|
|
console.log('failed permanently...')
|
|
|
|
throw err
|
|
|
|
}
|
2017-01-15 21:30:33 +00:00
|
|
|
}
|
2017-01-25 01:45:48 +00:00
|
|
|
console.log(`checkDns failed ${cycleArg} times and has ${30 - cycleArg} cycles to go before it fails permanently!`)
|
|
|
|
let myRecord
|
|
|
|
try {
|
|
|
|
myRecord = await myDnsly.getRecord(helpers.prefixName(this.domainName), 'TXT')
|
|
|
|
myRecord = myRecord[0][0]
|
|
|
|
if (myRecord === this.dnsKeyHash) {
|
|
|
|
console.log('and matches the required dnsKeyHash')
|
|
|
|
} else {
|
|
|
|
console.log('but does not match required dns keyHash!')
|
|
|
|
return redoCheck()
|
|
|
|
}
|
|
|
|
console.log('DNS is set!')
|
|
|
|
return myRecord
|
|
|
|
} catch (err) {
|
|
|
|
return redoCheck()
|
|
|
|
}
|
2017-01-15 21:30:33 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:33 +00:00
|
|
|
/**
|
|
|
|
* validates a challenge, only call after you have set the challenge at the expected location
|
|
|
|
*/
|
2017-01-22 20:50:04 +00:00
|
|
|
async requestValidation() {
|
2017-01-25 01:45:48 +00:00
|
|
|
await plugins.smartdelay.delayFor(20000)
|
2017-01-22 20:50:04 +00:00
|
|
|
let makeRequest = () => {
|
|
|
|
let done = q.defer()
|
2017-01-25 01:45:48 +00:00
|
|
|
this.parentAcmeAccount.parentSmartAcme.rawacmeClient.poll(this.chosenChallenge.uri, async (err, res) => {
|
2017-01-22 20:50:04 +00:00
|
|
|
if (err) {
|
|
|
|
console.log(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log(`Validation response:`)
|
|
|
|
console.log(JSON.stringify(res.body))
|
|
|
|
if (res.body.status === 'pending' || 'invalid') {
|
2017-01-25 01:45:48 +00:00
|
|
|
console.log('retry in 6 minutes!')
|
|
|
|
await plugins.smartdelay.delayFor(3000)
|
2017-01-22 20:50:04 +00:00
|
|
|
makeRequest().then((x: any) => { done.resolve(x) })
|
|
|
|
} else {
|
|
|
|
done.resolve(res.body)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return done.promise
|
|
|
|
}
|
|
|
|
await makeRequest()
|
2017-01-14 17:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* requests a certificate
|
|
|
|
*/
|
|
|
|
requestCert() {
|
|
|
|
let done = q.defer()
|
|
|
|
let payload = {
|
|
|
|
csr: plugins.rawacme.base64.encode(
|
|
|
|
plugins.rawacme.toDer(
|
|
|
|
plugins.nodeForge.pki.certificationRequestToPem(
|
|
|
|
this.csr
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
notBefore: this.validFrom.toISOString(),
|
|
|
|
notAfter: this.validTo.toISOString()
|
|
|
|
}
|
|
|
|
this.parentAcmeAccount.parentSmartAcme.rawacmeClient.newCert(
|
|
|
|
payload,
|
|
|
|
helpers.createKeypair(),
|
|
|
|
(err, res) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err)
|
|
|
|
done.reject(err)
|
|
|
|
}
|
2017-01-15 12:33:55 +00:00
|
|
|
console.log(res.body)
|
|
|
|
done.resolve(res.body)
|
2017-01-14 17:36:33 +00:00
|
|
|
})
|
|
|
|
return done.promise
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getCertificate - takes care of cooldown, validation polling and certificate retrieval
|
|
|
|
*/
|
|
|
|
getCertificate() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* accept a challenge - for private use only
|
|
|
|
*/
|
2017-01-25 01:45:48 +00:00
|
|
|
acceptChallenge() {
|
2017-01-14 17:36:33 +00:00
|
|
|
let done = q.defer()
|
|
|
|
this.parentAcmeAccount.parentSmartAcme.rawacmeClient.post(
|
2017-01-25 01:45:48 +00:00
|
|
|
this.chosenChallenge.uri,
|
2017-01-14 17:36:33 +00:00
|
|
|
{
|
|
|
|
resource: 'challenge',
|
2017-01-25 01:45:48 +00:00
|
|
|
keyAuthorization: this.chosenChallenge.keyAuthorization
|
2017-01-14 17:36:33 +00:00
|
|
|
},
|
|
|
|
this.parentAcmeAccount.parentSmartAcme.keyPair,
|
|
|
|
(err, res) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err)
|
|
|
|
done.reject(err)
|
|
|
|
}
|
2017-01-25 01:45:48 +00:00
|
|
|
done.resolve(res.body)
|
2017-01-14 17:36:33 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
return done.promise
|
|
|
|
}
|
|
|
|
}
|