Skip to content

Commit e6ef2ef

Browse files
committed
Update: export RSA keys as PEM
2 parents b55d60f + 654ccb7 commit e6ef2ef

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33

44
Matthew A. Miller <[email protected]>
55
Ian W. Remmel <[email protected]>
6+
Joe Hildebrand <[email protected]>

lib/jwk/basekey.js

+26
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,32 @@ var JWKBaseKeyObject = function(kty, ks, props, cfg) {
321321
}
322322
});
323323

324+
/**
325+
* @method JWK.Key#toPEM
326+
* @description
327+
* Returns the PEM representation of this Key as a string.
328+
*
329+
* @param {Boolean} [isPrivate=false] `true` if private parameters should be
330+
* included.
331+
* @returns {string} The PEM-encoded string
332+
*/
333+
Object.defineProperty(this, "toPEM", {
334+
value: function(isPrivate) {
335+
if (isPrivate === null) {
336+
isPrivate = false;
337+
}
338+
339+
if (!cfg.convertToPEM) {
340+
throw new Error("Unsupported key type for PEM encoding");
341+
}
342+
var k = (isPrivate) ? keys.private : keys.public;
343+
if (!k) {
344+
throw new Error("Invalid key");
345+
}
346+
return cfg.convertToPEM.call(this, k, isPrivate);
347+
}
348+
});
349+
324350
/**
325351
* @method JWK.Key#toObject
326352
* @description

lib/jwk/rsakey.js

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"use strict";
77

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

1011
var JWK = {
1112
BaseKey: require("./basekey.js"),
@@ -95,6 +96,14 @@ var JWKRsaCfg = {
9596
},
9697
verifyKey: function(alg, keys) {
9798
return keys.public;
99+
},
100+
101+
convertToPEM: function(key, isPrivate) {
102+
var k = rsau.convertToForge(key, !isPrivate);
103+
if (!isPrivate) {
104+
return forge.pki.publicKeyToPem(k);
105+
}
106+
return forge.pki.privateKeyToPem(k);
98107
}
99108
};
100109

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"author": "Cisco Systems, Inc. <https://www.cisco.com>",
2222
"contributors": [
2323
"Matthew A. Miller <[email protected]>",
24-
"Ian W. Remmel <[email protected]>"
24+
"Ian W. Remmel <[email protected]>",
25+
"Joe Hildebrand <[email protected]>"
2526
],
2627
"license": "Apache-2.0",
2728
"dependencies": {

test/jwk/rsakey-test.js

+11
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,17 @@ describe("jwk/RSA", function() {
347347
kid: "someid"
348348
});
349349
assert.deepEqual(key.toJSON(true), json);
350+
var priv_pem = key.toPEM(true);
351+
assert.isString(priv_pem);
352+
assert.match(priv_pem, /^-----BEGIN RSA PRIVATE KEY-----\r\n/);
353+
assert.match(priv_pem, /\r\n-----END RSA PRIVATE KEY-----\r\n$/);
354+
355+
var pub_pem = key.toPEM();
356+
assert.isString(pub_pem);
357+
assert.match(pub_pem, /^-----BEGIN PUBLIC KEY-----\r\n/);
358+
assert.match(pub_pem, /\r\n-----END PUBLIC KEY-----\r\n$/);
359+
360+
assert.equal(pub_pem, key.toPEM(false));
350361
});
351362

352363
return promise;

0 commit comments

Comments
 (0)