Skip to content

createPublicKey Fails with PEM Certificate in Deno but Works in Node.js #25681

Closed

Description

@kechako

When attempting to use createPublicKey from the node:crypto module to parse a PEM certificate in Deno, an error is thrown: TypeError: unsupported PEM label: CERTIFICATE. This issue does not occur in Node.js, where the same code executes without error, correctly creating a PublicKeyObject.

Steps to Reproduce

  1. Create a JavaScript file (e.g., main.mjs) with the following content:
import { createPublicKey } from 'node:crypto';

const certificate = `-----BEGIN CERTIFICATE-----
MIIC8zCCAdugAwIBAgIBATANBgkqhkiG9w0BAQsFADAbMRkwFwYDVQQDExB0ZXN0
LWNlcnRpZmljYXRlMB4XDTI0MDkxNzA5MTczNVoXDTI3MDkxNzA5MTczNVowGzEZ
MBcGA1UEAxMQdGVzdC1jZXJ0aWZpY2F0ZTCCASIwDQYJKoZIhvcNAQEBBQADggEP
ADCCAQoCggEBAMOzTIrwvbUbPIrxCr5DO1XMd3tH37pID0no4cOUq1hxNEnB4l1j
2201atvmXwzWI3xtPzfwOYUtE/DGagFh805/nod5yXwR6liGd9RjkABxPi0UF7jl
lWHfBLyILUHVR9hEOl65vUpKx5ORNgbO9L7WsL/FKH9pvCbWjdI29+pQnZ4gOoWZ
YC6auoKfG7TcbaFb9AubolcIlofC2MHP+cWjPA+iX6ezUqqN1Ug5xGiF/sC79M0o
5d6E83zdXxyyFwydUWUv3EKgmVTLln/2hYQFKCRhy72n6L7y9JNcieOauQK0efJB
+2HwaWeAr2xkhSnWVCRl4nEgiu/E0nL/zNUCAwEAAaNCMEAwDgYDVR0PAQH/BAQD
AgGGMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFIAeXho137l8V6daKI33IvRb
N6CyMA0GCSqGSIb3DQEBCwUAA4IBAQAQU1Sast6VsD4uTJiSz/lSEkLZ6wC/6v+R
az0YSnbNmQ5YczBLdTLs07hBC1tDvv0vfopRXvNxP7AxkopX5O7Lc15zf5JdTZnY
/tJwO62jZiaLsfAo2JzrZ31h2cFFFRTYPTx+8E4djgdmwKtaECeQFXqdpOHOJCGv
NfwVlZ7Z/cd8fI8oiNtvJDAhPa/UZXAhFV74hT0DiuMwPiJvsG83rutvAYpZ8lPu
yG6QSsxPnxzEHIKR+vgxUHKwTdv0sWt3XBmpIY5CGXFR2eIQP1jv0ohtcnLMJe8N
z6TExWlQMjt66nV7R8cRAkzmABrG+NW3e8Zpac7Lkuv+zu0S+K7c
-----END CERTIFICATE-----`

console.debug(createPublicKey(certificate));
  1. Run this code using Deno:
deno run main.mjs
  1. Observe the error: TypeError: unsupported PEM label: CERTIFICATE.

Version: Deno 1.46.3

Activity

  1. kt3k commented on Sep 19, 2024

    @kt3k
    Contributor

    https://nodejs.org/api/crypto.html#cryptocreatepublickeykey

    If the format is 'pem', the 'key' may also be an X.509 certificate.

    It looks like createPublicKey extracts public key from X509 cert and we don't handle that case.

    ref: https://github.com/nodejs/node/blob/6031a4bc7cab01623d93e75d17c5b796b5bb831e/src/crypto/crypto_keys.cc#L130

    It looks discussed around here: nodejs/node#24234 (comment)

  2. self-assigned this
    on Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working correctlycryptoRelated to node:crypto or WebCryptonode compat

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions