Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kechako opened this issue Sep 17, 2024 · 1 comment · Fixed by #25731
Closed

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

kechako opened this issue Sep 17, 2024 · 1 comment · Fixed by #25731
Assignees
Labels
bug Something isn't working correctly crypto Related to node:crypto or WebCrypto node compat

Comments

@kechako
Copy link

kechako commented Sep 17, 2024

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

@marvinhagemeister marvinhagemeister added bug Something isn't working correctly node compat crypto Related to node:crypto or WebCrypto labels Sep 17, 2024
@kt3k
Copy link
Member

kt3k commented Sep 19, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly crypto Related to node:crypto or WebCrypto node compat
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants