node-jose icon indicating copy to clipboard operation
node-jose copied to clipboard

Generate key with embedded signature

Open nick-bull opened this issue 4 years ago • 2 comments

When I run the following:

const jose = require('node-jose');

async function initialize() {
  const keystore = jose.JWK.createKeyStore();

  const props = {
    use: 'sig',
    alg: 'RS256',
  };
  const publicKey = await keystore.generate("RSA", 2048, props)
  console.log(publicKey);
}

initialize();

I end up with an output similar to the following:

JWKBaseKeyObject {
  keystore: JWKStore {},
  length: 2048,
  kty: 'RSA',
  kid: 'ONTcoIyZb10-HNvag6r1kYUKo6YYbzRj438W_6YvIjo',
  use: 'sig',
  alg: 'RS256'
}

However, what I would like to generate is something similar to the items in this jwks file.

Is it possible to generate keys with embedded signatures?

nick-bull avatar Jul 19 '20 17:07 nick-bull

The referenced jwks file has embedded X.509 certificates. node-jose does not support creating X.509 certificates itself, but it is possible to get there. Unfortunately, it is very involved:

  1. Generate the (private) key (like your example)
  2. Export private key in PEM encoding (pubkey = key.toPEM(false); privkey = key.toPEM(true))
  3. Obtain a certificate for the above public key (various mechanisms, depends on your CA)
  4. Copy cert, stripping whitespace and leading/trailing markers (e.g., -----BEGIN CERTIFICATE-----)
  5. re-import public key from (unmodified) cert, and specifying the "x5c" extra:
let publicKey = await jose.JWK.asKey(cert, "pem", { x5c: [certStripped] });

linuxwolf avatar Aug 27 '20 15:08 linuxwolf

Can you describe the procedure in more detail, I have a code example:

const jose = require('node-jose');

async function initialize() { const keystore = jose.JWK.createKeyStore();

const props = { use: 'sig', alg: 'RS256', }; const key = await keystore.generate("RSA", 2048, props);

const pubkey = key.toPEM(false); const privkey = key.toPEM(true) console.log('key', key, 'pubkey', pubkey, 'privkey', privkey); }

initialize();

How can I get the certificate for the above public key?

NikitaSharapov avatar May 12 '22 10:05 NikitaSharapov