celld icon indicating copy to clipboard operation
celld copied to clipboard

crypto.subtle: EC public keys cannot use the raw format (export returns SPKI, import throws)

Open carlulsoe opened this issue 1 month ago • 0 comments

crypto.subtle cannot move an EC public key (P-256, and presumably P-384/P-521) in the raw format, in either direction:

  • exportKey("raw", ecPublicKey) returns the key's SPKI document (91 bytes for P-256, first byte 0x30) instead of the 65-byte uncompressed point. No error is raised.
  • importKey("raw", point, { name: "ECDH" | "ECDSA", namedCurve: "P-256" }, …) throws NotSupportedError: unsupported key import.

Both work on Cloudflare Workers, and raw is the spec's format for EC public keys (WebCrypto §23.7). JWK and SPKI work on celld.

celld version: 0.6.2 (celld dev, compatibility_date 2026-09-01, nodejs_compat).

Why it matters

Web Push message encryption (RFC 8291) needs both: the subscriber's p256dh key arrives as a raw point, and the sender's ephemeral public key goes into the message header as a raw point. On celld the import throws, so no notification is sent. Had the import worked, the export would have put 91 bytes of SPKI into the header and phones would have failed to decrypt with no error on the server. We worked around it by going through JWK in both directions.

Repro

const first = (e: unknown) => String(e).split("\n")[0];
async function probe(name: "ECDH" | "ECDSA") {
  const algo = { name, namedCurve: "P-256" };
  const usages: KeyUsage[] = name === "ECDH" ? ["deriveBits"] : ["sign", "verify"];
  const pair = (await crypto.subtle.generateKey(algo, true, usages)) as CryptoKeyPair;
  const raw = new Uint8Array((await crypto.subtle.exportKey("raw", pair.publicKey)) as ArrayBuffer);
  const row: Record<string, unknown> = { algorithm: `${name} P-256`, exportRawLength: raw.length, exportRawFirstByte: raw[0] };
  // The 65-byte uncompressed point, rebuilt from the JWK export.
  const jwk = (await crypto.subtle.exportKey("jwk", pair.publicKey)) as JsonWebKey;
  const b = (s: string) => Uint8Array.from(atob(s.replace(/-/g, "+").replace(/_/g, "/")), (c) => c.charCodeAt(0));
  const point = new Uint8Array([4, ...b(jwk.x!), ...b(jwk.y!)]);
  try {
    await crypto.subtle.importKey("raw", point, algo, true, name === "ECDH" ? [] : ["verify"]);
    row.importRaw = "ok";
  } catch (e) { row.importRaw = first(e); }
  return row;
}
export default { fetch: async () => Response.json([await probe("ECDH"), await probe("ECDSA")]) };

celld 0.6.2:

{"algorithm":"ECDH P-256","exportRawLength":91,"exportRawFirstByte":48,"importRaw":"NotSupportedError: unsupported key import"}
{"algorithm":"ECDSA P-256","exportRawLength":91,"exportRawFirstByte":48,"importRaw":"NotSupportedError: unsupported key import"}

Expected (Workers, browsers): exportRawLength: 65, exportRawFirstByte: 4, importRaw: "ok".

Where

In crates/celld/js/crypto.js:

  • importKey has a raw arm for HKDF/PBKDF2, secret keys, and the Secure Curves (_OKP_CURVES), but none for ECDH/ECDSA, so a raw EC point falls through to unsupported key import.
  • exportKey special-cases raw for Ed25519/X25519 public keys (the comment there describes this same problem for X25519), then answers every other raw request from the stored bytes, which for an EC key are its SPKI.

The fix is probably the EC counterpart of the Secure Curves arms: import a raw point (uncompressed, and ideally compressed too) through the host's key import, and export a public EC key's point rather than its DER.

carlulsoe avatar Oct 13 '26 17:10 carlulsoe