The app follows the instruction on node-jose 2.0.0
for import .pem
key. Here is the documentation:
To import and existing Key from a PEM or DER:
// input is either a:
// * String serialization of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER
// * Buffer of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER
// form is either a:
// * "json" for a JSON stringified JWK
// * "private" for a DER encoded 'raw' private key
// * "pkcs8" for a DER encoded (unencrypted!) PKCS8 private key
// * "public" for a DER encoded SPKI public key (alternate to 'spki')
// * "spki" for a DER encoded SPKI public key
// * "pkix" for a DER encoded PKIX X.509 certificate
// * "x509" for a DER encoded PKIX X.509 certificate
// * "pem" for a PEM encoded of PKCS8 / SPKI / PKIX //<<=="pem"
keystore.add(input, form).
then(function(result) {
// {result} is a jose.JWK.Key
});
The key was generated already with .pem
form and its content is stored in nodejs config file as following in variable process.env.josePrivateKey
:
-----BEGIN PRIVATE KEY-----
NC4CAQAwBQYcK2VwBCIEIIWUb0/MoKaBxQkmmPlHIGyPfDQb/U3D6jQ+gMUGtvpa
-----END PRIVATE KEY-----
Here is the code to add the pem
key to keystore:
const jose = require('node-jose');
let keystore = jose.JWK.createKeyStore();
let privkey = await keystore.add(process.env.josePrivateKey, "pem"); //<<==this code throws error
However there is an error:
(node:11572) UnhandledPromiseRejectionWarning: Error: no importer for key
at JWKStore.value (C:\d\code\js\xyz\node_modules\node-jose\lib\jwk\keystore.js:305:21)
at initKeystore (C:\d\code\js\xyz\startup\accessstorageinfo.js:9:34) //<<==code as above
at Object.<anonymous> (C:\d\code\js\xyz\startup\accessstorageinfo.js:14:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\d\code\js\xyz\server.js:13:1)
What is missing here for key importing?
(as of March 2021)
node-jose
does not support the following keys: Ed25519, Ed448, X25519, or X448. It also does not support thesecp256k1
EC curve. For any of those it will return the error you're encountering. As a result it does not support the JWS AlgorithmsEdDSA
orES256K
.On the other hand https://github.com/panva/jose supports all of the above in Node.js runtime.