-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Cert signing with node-forge - fix: another signing bug
- Loading branch information
Showing
9 changed files
with
93 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
|
||
openssl req -subj '/' -new -nodes -x509 -days 3650 -extensions v3_ca -keyout cakey.pem -out cacert.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,53 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const read = (file, desc) => { | ||
if (!fs.existsSync(file)) | ||
throw new Error("Unable to find " + desc + " file " + JSON.stringify(file)) | ||
return fs.readFileSync(file).toString() | ||
} | ||
const forge = require('node-forge') | ||
const pki = forge.pki | ||
const debug = require('debug') | ||
const log = debug('nodetrust:ca:forge') | ||
|
||
module.exports = class ForgeCA { | ||
constructor(swarm, config) { | ||
this.swarm = swarm | ||
this.config = config | ||
|
||
this.cert = read(config.ca, 'Certificate Authority Certification') | ||
this.key = read(config.key, 'Certificate Authority Private Key') | ||
this.caKey = pki.privateKeyFromPem(this.key) | ||
this.caCert = pki.certificateFromPem(this.cert) | ||
} | ||
|
||
doCertRequest(csr, sig, cb) { | ||
|
||
doCertRequest(pem, id, cn, sig, cb) { | ||
log('reading csr') | ||
const csr = pki.certificationRequestFromPem(pem.toString()) | ||
if (!csr.verify()) return cb(new Error("Certification request invalid")) | ||
// const ext = csr.getAttribute({name: 'extensionRequest'}) | ||
const cn_req = csr.subject.getField({ | ||
name: 'commonName' | ||
}).value | ||
if (cn != cn_req) return cb(new Error("Rejecting request: commonName (" + cn + ") and requested commonName (" + cn_req + ") do not match!")) | ||
const cert = pki.createCertificate() | ||
|
||
cert.serialNumber = '02' | ||
|
||
cert.validity.notBefore = new Date() | ||
cert.validity.notAfter = new Date() | ||
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1) | ||
|
||
cert.setSubject(csr.subject.attributes) | ||
|
||
cert.setIssuer(this.caCert.subject.attributes) | ||
|
||
cert.publicKey = csr.publicKey | ||
|
||
cert.sign(this.caKey) | ||
|
||
log('signing csr for %s', cn) | ||
|
||
return cb(null, Buffer.from(pki.certificateToPem(cert))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters