Skip to content

Commit 05f043f

Browse files
author
Enver Cicak
committed
Allow keys without BEGIN/END boundaries to be loaded
1 parent 5e4806c commit 05f043f

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/utils.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,18 @@ module.exports._ = {
9191
* themselves.
9292
*/
9393
module.exports.trimSurroundingText = function (data, opening, closing) {
94+
let trimStartIndex = 0;
95+
let trimEndIndex = data.length;
96+
9497
let openingBoundaryIndex = data.indexOf(opening);
95-
if (openingBoundaryIndex < 0) {
96-
throw Error('Missing BEGIN line');
98+
if (openingBoundaryIndex >= 0) {
99+
trimStartIndex = openingBoundaryIndex + opening.length;
97100
}
98101

99102
let closingBoundaryIndex = data.indexOf(closing, openingBoundaryIndex);
100-
if (closingBoundaryIndex < 0) {
101-
throw Error('Missing END line');
103+
if (closingBoundaryIndex >= 0) {
104+
trimEndIndex = closingBoundaryIndex;
102105
}
103106

104-
return data.substring(openingBoundaryIndex + opening.length, closingBoundaryIndex);
107+
return data.substring(trimStartIndex, trimEndIndex);
105108
}

test/tests.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,40 @@ describe('NodeRSA', function () {
366366
assert(!publicNodeRSA.isPrivate());
367367
});
368368

369+
it('should handle data without begin/end encapsulation boundaries for pkcs1 private keys', function () {
370+
let privateFile = fs.readFileSync(keysFolder + 'private_pkcs1.pem', "utf8");
371+
let privateFileNoBoundaries = privateFile.substring("-----BEGIN RSA PRIVATE KEY-----".length, privateFile.indexOf("-----END RSA PRIVATE KEY-----"));
372+
let key = new NodeRSA(privateFileNoBoundaries, "pkcs1-private-pem");
373+
assert.equal(key.exportKey(), fileKeyPKCS1);
374+
});
375+
376+
it('should handle data without begin/end encapsulation boundaries for pkcs1 public keys', function () {
377+
let publicFile = fs.readFileSync(keysFolder + 'public_pkcs1.pem', "utf8");
378+
let publicFileNoBoundaries = publicFile.substring("-----BEGIN RSA PUBLIC KEY-----".length, publicFile.indexOf("-----END RSA PUBLIC KEY-----"));
379+
let publicNodeRSA = new NodeRSA(publicFileNoBoundaries, "pkcs1-public-pem");
380+
assert.instanceOf(publicNodeRSA.keyPair, Object);
381+
assert(publicNodeRSA.isPublic());
382+
assert(publicNodeRSA.isPublic(true));
383+
assert(!publicNodeRSA.isPrivate());
384+
});
385+
386+
it('should handle data without begin/end encapsulation boundaries for pkcs8 private keys', function () {
387+
let privateFile = fs.readFileSync(keysFolder + 'private_pkcs8.pem', "utf8");
388+
let privateFileNoBoundaries = privateFile.substring('-----BEGIN PRIVATE KEY-----'.length, privateFile.indexOf('-----END PRIVATE KEY-----'));
389+
let key = new NodeRSA(privateFileNoBoundaries, "pkcs8-private-pem");
390+
assert.equal(key.exportKey(), fileKeyPKCS1);
391+
});
392+
393+
it('should handle data without begin/end encapsulation boundaries for pkcs8 public keys', function () {
394+
let publicFile = fs.readFileSync(keysFolder + 'public_pkcs8.pem', "utf8");
395+
let publicFileNoBoundaries = publicFile.substring("-----BEGIN PUBLIC KEY-----".length, publicFile.indexOf("-----END PUBLIC KEY-----"));
396+
let publicNodeRSA = new NodeRSA(publicFileNoBoundaries, "pkcs8-public-pem");
397+
assert.instanceOf(publicNodeRSA.keyPair, Object);
398+
assert(publicNodeRSA.isPublic());
399+
assert(publicNodeRSA.isPublic(true));
400+
assert(!publicNodeRSA.isPrivate());
401+
});
402+
369403
it('.importKey() from private components', function () {
370404
var key = new NodeRSA();
371405
key.importKey({

0 commit comments

Comments
 (0)