Importing private key using jose/web crypto api in cloudflare workers returns an undefined CryptoKey

Hi, im trying to import a PKCS8 private key in my worker. I have attempted to do this using both the jose library (panva/jose) and my own implementation using crypto.subtle.importKey(). In both cases, I get an undefined crypto key:
const pk = await importPKCS8(key, "RS256");
console.log(pk);
const pk = await importPKCS8(key, "RS256");
console.log(pk);
CryptoKey {
usages: undefined,
algorithm: undefined,
extractable: undefined,
type: undefined
}
CryptoKey {
usages: undefined,
algorithm: undefined,
extractable: undefined,
type: undefined
}
I have read through the docs on cloudflare referencing Web Crypto (https://developers.cloudflare.com/workers/runtime-apis/web-crypto) and I believe this type of operation should be supported. Any help is appreciated.
Web Crypto · Cloudflare Workers docs
The Web Crypto API provides a set of low-level functions for common cryptographic tasks. The Workers Runtime implements the full surface of this API, …
1 Reply
Hyper
Hyper12mo ago
Yeah you're right. I tested that behavior with this code:
app.get('/test', async (c) => {
const encoder = new TextEncoder();
const secretKeyData = encoder.encode("my secret symmetric key");
const key = await crypto.subtle.importKey(
"raw",
secretKeyData,
{ name: "HMAC", hash: "SHA-256" },
false,
["verify"]
);

// Returns the undefined crypto key
console.log(key);

// Returns the correct key
return c.json(key)
})
app.get('/test', async (c) => {
const encoder = new TextEncoder();
const secretKeyData = encoder.encode("my secret symmetric key");
const key = await crypto.subtle.importKey(
"raw",
secretKeyData,
{ name: "HMAC", hash: "SHA-256" },
false,
["verify"]
);

// Returns the undefined crypto key
console.log(key);

// Returns the correct key
return c.json(key)
})
Thanks!