Skip to content

Commit

Permalink
TritonDataCenter/node-sshpk-agent#9 want to sign certificates using a…
Browse files Browse the repository at this point in the history
…gent keys

Reviewed by: Trent Mick <[email protected]>
  • Loading branch information
Alex Wilson committed Mar 9, 2017
1 parent 3bd4c38 commit 1479eb2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
31 changes: 30 additions & 1 deletion lib/formats/openssh-cert.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright 2016 Joyent, Inc.
// Copyright 2017 Joyent, Inc.

module.exports = {
read: read,
verify: verify,
sign: sign,
signAsync: signAsync,
write: write,

/* Internal private API */
Expand Down Expand Up @@ -188,6 +189,34 @@ function sign(cert, key) {
return (true);
}

function signAsync(cert, signer, done) {
if (cert.signatures.openssh === undefined)
cert.signatures.openssh = {};
try {
var blob = toBuffer(cert, true);
} catch (e) {
delete (cert.signatures.openssh);
done(e);
return;
}
var sig = cert.signatures.openssh;

signer(blob, function (err, signature) {
if (err) {
done(err);
return;
}
if ((signature.type === 'rsa' || signature.type === 'dsa') &&
signature.hashAlgorithm !== 'sha1') {
done(new Error('RSA/DSA keys can only sign with ' +
'SHA-1 for OpenSSH certificates'));
return;
}
sig.signature = signature;
done();
});
}

function write(cert, options) {
if (options === undefined)
options = {};
Expand Down
29 changes: 28 additions & 1 deletion lib/formats/x509.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright 2016 Joyent, Inc.
// Copyright 2017 Joyent, Inc.

module.exports = {
read: read,
verify: verify,
sign: sign,
signAsync: signAsync,
write: write
};

Expand Down Expand Up @@ -451,6 +452,32 @@ function sign(cert, key) {
return (true);
}

function signAsync(cert, signer, done) {
if (cert.signatures.x509 === undefined)
cert.signatures.x509 = {};
var sig = cert.signatures.x509;

var der = new asn1.BerWriter();
writeTBSCert(cert, der);
var blob = der.buffer;
sig.cache = blob;

signer(blob, function (err, signature) {
if (err) {
done(err);
return;
}
sig.algo = signature.type + '-' + signature.hashAlgorithm;
if (SIGN_ALGS[sig.algo] === undefined) {
done(new Error('Invalid signing algorithm "' +
sig.algo + '"'));
return;
}
sig.signature = signature;
done();
});
}

function write(cert, options) {
var sig = cert.signatures.x509;
assert.object(sig, 'x509 signature');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sshpk",
"version": "1.11.0",
"version": "1.12.0",
"description": "A library for finding and using SSH public keys",
"main": "lib/index.js",
"scripts": {
Expand Down

0 comments on commit 1479eb2

Please sign in to comment.