Skip to content

Commit

Permalink
Update: export RSA keys as PEM
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxwolf committed Dec 11, 2015
2 parents b55d60f + 654ccb7 commit e6ef2ef
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

Matthew A. Miller <linuxwolf@outer-planes.net>
Ian W. Remmel <design@ianwremmel.com>
Joe Hildebrand <joe-github@cursive.net>
26 changes: 26 additions & 0 deletions lib/jwk/basekey.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,32 @@ var JWKBaseKeyObject = function(kty, ks, props, cfg) {
}
});

/**
* @method JWK.Key#toPEM
* @description
* Returns the PEM representation of this Key as a string.
*
* @param {Boolean} [isPrivate=false] `true` if private parameters should be
* included.
* @returns {string} The PEM-encoded string
*/
Object.defineProperty(this, "toPEM", {
value: function(isPrivate) {
if (isPrivate === null) {
isPrivate = false;
}

if (!cfg.convertToPEM) {
throw new Error("Unsupported key type for PEM encoding");
}
var k = (isPrivate) ? keys.private : keys.public;
if (!k) {
throw new Error("Invalid key");
}
return cfg.convertToPEM.call(this, k, isPrivate);
}
});

/**
* @method JWK.Key#toObject
* @description
Expand Down
9 changes: 9 additions & 0 deletions lib/jwk/rsakey.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"use strict";

var forge = require("../deps/forge.js");
var rsau = require("../algorithms/rsa-util");

var JWK = {
BaseKey: require("./basekey.js"),
Expand Down Expand Up @@ -95,6 +96,14 @@ var JWKRsaCfg = {
},
verifyKey: function(alg, keys) {
return keys.public;
},

convertToPEM: function(key, isPrivate) {
var k = rsau.convertToForge(key, !isPrivate);
if (!isPrivate) {
return forge.pki.publicKeyToPem(k);
}
return forge.pki.privateKeyToPem(k);
}
};

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"author": "Cisco Systems, Inc. <https://www.cisco.com>",
"contributors": [
"Matthew A. Miller <linuxwolf@outer-planes.net>",
"Ian W. Remmel <design@ianwremmel.com>"
"Ian W. Remmel <design@ianwremmel.com>",
"Joe Hildebrand <joe-github@cursive.net>"
],
"license": "Apache-2.0",
"dependencies": {
Expand Down
11 changes: 11 additions & 0 deletions test/jwk/rsakey-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,17 @@ describe("jwk/RSA", function() {
kid: "someid"
});
assert.deepEqual(key.toJSON(true), json);
var priv_pem = key.toPEM(true);
assert.isString(priv_pem);
assert.match(priv_pem, /^-----BEGIN RSA PRIVATE KEY-----\r\n/);
assert.match(priv_pem, /\r\n-----END RSA PRIVATE KEY-----\r\n$/);

var pub_pem = key.toPEM();
assert.isString(pub_pem);
assert.match(pub_pem, /^-----BEGIN PUBLIC KEY-----\r\n/);
assert.match(pub_pem, /\r\n-----END PUBLIC KEY-----\r\n$/);

assert.equal(pub_pem, key.toPEM(false));
});

return promise;
Expand Down

0 comments on commit e6ef2ef

Please sign in to comment.