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

πŸš€ Feature Request β€” crypto.subtle.importKey should support large RSA public exponent #2120

Open
thibmeu opened this issue May 13, 2024 · 1 comment
Labels
api crypto feature request Request for Workers team to add a feature

Comments

@thibmeu
Copy link

thibmeu commented May 13, 2024

It would be great if crypto.subtle.importKey would support large RSA public key exponent.

The following worker (playground) highlights the curent error path

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  try {
    await crypto.subtle.importKey('jwk', {
      "alg": "PS384",
      "e": "K932_NQuz0qCWG0SZbKm97-6NG1Oy2VFbwAb9Mh1OcHS0jXlZCCa-jJWd9li6aYd_bLkcR26K70eFxIIxSY8bZElSeidBcpBewjHUd5wNmubxI9MInQPgsPUT9gEHXYeRC1o0vBUOUPH7hDNfCEC9ijSOiTFDT01oZakoc_XoTc",
      "ext": true,
      "key_ops": [
          "verify"
      ],
      "kty": "RSA",
      "n": "1pMIIPcf5Re_MlnRTUAgmwKlwNPWGZHHMd19o5-NaYIVUuIxjWya2JfmA4h6R26jFiwSBdqayW8C7fMd8Em9VfFCE0wX1DgqDnjidTRfFl--jknNymz1xybFmd054J514PMwozEh5zl25PrLqc-gAcKLfJb4E0-ZgdtnULQ6QXEPUdpCQP4DEGwSrLHnu1PXXsclbaP93QcYuJw2VBD85hvHyZsRX7TDwxgIH6fhtlo3d06OUMlujOKyzGs7NnmCNmor-ZJMS6_bP_XnIiWKtwXHbUPl8fEhuYSBTpjqKyuHJc2byQXAvD11wqjbcKcVMhPDmuNxsrXcHa_LGdb66Q"
    }, {name: 'RSA-PSS', hash: 'SHA-384'}, true, ['verify'])
    return new Response('crypto.subtle.importKey succeded')
  } catch (e) {
    return new Response(`crypto.subtle.importKey failed: ${e.message}`, { status: 500 })
  }
}

This key import works in Node.js v20 and Firefox.

While there might be a performance penalty, the extra CPU usage would be reflected on the worker bill.

The relevant code is

// Now check the public exponent for allow-listed values.
// First see if we can convert the public exponent to an unsigned number. Unfortunately OpenSSL
// doesn't have convenient APIs to do this (since these are bignums) so we have to do it by hand.
// Since the problematic BIGNUMs are within the range of an unsigned int (& technicall an
// unsigned short) we can treat an out-of-range issue as valid input.
KJ_IF_SOME(v, fromBignum<unsigned>(publicExponent)) {
if (!isImport) {
JSG_REQUIRE(v == 3 || v == 65537, DOMOperationError,
"The \"publicExponent\" must be either 3 or 65537, but got ", v, ".");
} else if (strictCrypto) {
// While we have long required the exponent to be 3 or 65537 when generating keys, handle
// imported keys more permissively and allow additional exponents that are considered safe
// and commonly used.
JSG_REQUIRE(v == 3 || v == 17 || v == 37 || v == 65537, DOMOperationError,
"Imported RSA key has invalid publicExponent ", v, ".");
}
} else {
JSG_FAIL_REQUIRE(DOMOperationError, "The \"publicExponent\" must be either 3 or 65537, but "
"got a number larger than 2^32.");
}

@jasnell
Copy link
Member

jasnell commented May 14, 2024

@irvinebroque @mikenomitch ... this is related to recent discussions around compute limits and crypto operations. We should discuss and figure out how to prioritize.

@jasnell jasnell added api crypto feature request Request for Workers team to add a feature labels May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api crypto feature request Request for Workers team to add a feature
Projects
None yet
Development

No branches or pull requests

2 participants