Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,18 @@ module.exports._ = {
* themselves.
*/
module.exports.trimSurroundingText = function (data, opening, closing) {
let trimStartIndex = 0;
let trimEndIndex = data.length;

let openingBoundaryIndex = data.indexOf(opening);
if (openingBoundaryIndex < 0) {
throw Error('Missing BEGIN line');
if (openingBoundaryIndex >= 0) {
trimStartIndex = openingBoundaryIndex + opening.length;
}

let closingBoundaryIndex = data.indexOf(closing, openingBoundaryIndex);
if (closingBoundaryIndex < 0) {
throw Error('Missing END line');
if (closingBoundaryIndex >= 0) {
trimEndIndex = closingBoundaryIndex;
}

return data.substring(openingBoundaryIndex + opening.length, closingBoundaryIndex);
return data.substring(trimStartIndex, trimEndIndex);
}
34 changes: 34 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,40 @@ describe('NodeRSA', function () {
assert(!publicNodeRSA.isPrivate());
});

it('should handle data without begin/end encapsulation boundaries for pkcs1 private keys', function () {
let privateFile = fs.readFileSync(keysFolder + 'private_pkcs1.pem', "utf8");
let privateFileNoBoundaries = privateFile.substring("-----BEGIN RSA PRIVATE KEY-----".length, privateFile.indexOf("-----END RSA PRIVATE KEY-----"));
let key = new NodeRSA(privateFileNoBoundaries, "pkcs1-private-pem");
assert.equal(key.exportKey(), fileKeyPKCS1);
});

it('should handle data without begin/end encapsulation boundaries for pkcs1 public keys', function () {
let publicFile = fs.readFileSync(keysFolder + 'public_pkcs1.pem', "utf8");
let publicFileNoBoundaries = publicFile.substring("-----BEGIN RSA PUBLIC KEY-----".length, publicFile.indexOf("-----END RSA PUBLIC KEY-----"));
let publicNodeRSA = new NodeRSA(publicFileNoBoundaries, "pkcs1-public-pem");
assert.instanceOf(publicNodeRSA.keyPair, Object);
assert(publicNodeRSA.isPublic());
assert(publicNodeRSA.isPublic(true));
assert(!publicNodeRSA.isPrivate());
});

it('should handle data without begin/end encapsulation boundaries for pkcs8 private keys', function () {
let privateFile = fs.readFileSync(keysFolder + 'private_pkcs8.pem', "utf8");
let privateFileNoBoundaries = privateFile.substring('-----BEGIN PRIVATE KEY-----'.length, privateFile.indexOf('-----END PRIVATE KEY-----'));
let key = new NodeRSA(privateFileNoBoundaries, "pkcs8-private-pem");
assert.equal(key.exportKey(), fileKeyPKCS1);
});

it('should handle data without begin/end encapsulation boundaries for pkcs8 public keys', function () {
let publicFile = fs.readFileSync(keysFolder + 'public_pkcs8.pem', "utf8");
let publicFileNoBoundaries = publicFile.substring("-----BEGIN PUBLIC KEY-----".length, publicFile.indexOf("-----END PUBLIC KEY-----"));
let publicNodeRSA = new NodeRSA(publicFileNoBoundaries, "pkcs8-public-pem");
assert.instanceOf(publicNodeRSA.keyPair, Object);
assert(publicNodeRSA.isPublic());
assert(publicNodeRSA.isPublic(true));
assert(!publicNodeRSA.isPrivate());
});

it('.importKey() from private components', function () {
var key = new NodeRSA();
key.importKey({
Expand Down