Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Occasional corrupt private key, breaks when loaded with asKey with "RangeError: Attempt to access memory outside buffer bounds" #416

@james-bowers

Description

@james-bowers

I created this private key using node-jose, and then when I load it back into node jose using the asKey function, I get an error;

RangeError: Attempt to access memory outside buffer bounds
const privatePem = `-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIOeCAAAgHeSEY/fn0OYjJh49LKmVReGQmJcQx78u0sVCoAoGCCqGSM49
AwEHoUQDQgAEPwrrzWtZSsEb6EgldGFCnqbykydVPbvtP3H1UKs6EPDCl8mzGMHg
kksAw85JhXLAhy7b4WP0clQCXMDvJGanqg==
-----END EC PRIVATE KEY-----`;

await nodeJose.JWK.asKey(privatePem, 'pem');

This doesn't always happen, more often than not a private key is generated that works completely correctly.

The private key looks valid when I use the command

openssl ec -in broken-pem.pem -noout -text to validate it.

I'm on node v16.14.2 but i've tried it on a lot of different node versions using runkit, and using version 2.2.0 of node-jose.

I'm generating the keys using;

  const key = await jose.JWK.createKey('EC', 'P-256', { alg: '' });

  const privatePem = key.toPEM(true);
  const publicPem = key.toPEM(false);

which creates keys that work 99% of the time.

With the broken key

Screenshot 2023-04-19 at 18 16 04

With a valid key generated in the same way;

Screenshot 2023-04-19 at 18 16 49

This can happen for both public and private keys.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions