Skip to content

Commit

Permalink
Implement Node.js compat KeyObject asymmetricKeyType property (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell authored May 16, 2023
1 parent b5d5066 commit 6ac0b16
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/workerd/api/node/crypto-keys.c++
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "crypto.h"
#include <workerd/api/crypto-impl.h>
#include <openssl/crypto.h>
#include <map>

namespace workerd::api::node {

Expand Down Expand Up @@ -71,7 +72,23 @@ CryptoImpl::AsymmetricKeyDetails CryptoImpl::getAsymmetricKeyDetail(
}

kj::StringPtr CryptoImpl::getAsymmetricKeyType(jsg::Lock& js, jsg::Ref<CryptoKey> key) {
KJ_UNIMPLEMENTED("not implemented");
static std::map<kj::StringPtr, kj::StringPtr> mapping{
{"RSASSA-PKCS1-v1_5", "rsa"},
{"RSA-PSS", "rsa"},
{"RSA-OAEP", "rsa"},
{"ECDSA", "ec"},
{"Ed25519", "ed25519"},
{"NODE-ED25519", "ed25519"},
{"ECDH", "ecdh"},
{"X25519", "x25519"},
};
JSG_REQUIRE(key->getType() != "secret"_kj, TypeError,
"Secret key does not have an asymmetric type");
auto found = mapping.find(key->getAlgorithmName());
if (found != mapping.end()) {
return found->second;
}
return key->getAlgorithmName();
}

CryptoKeyPair CryptoImpl::generateKeyPair(
Expand Down
3 changes: 3 additions & 0 deletions src/workerd/api/node/crypto_keys-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ export const asymmetric_key_equals_test = {
const rsa_ko = KeyObject.from(rsa);
const rsa2_ko = KeyObject.from(rsa2);

strictEqual(jwk1_ko.asymmetricKeyType, 'ec');
strictEqual(rsa_ko.asymmetricKeyType, 'rsa');

ok(jwk1_ko.equals(jwk1_ko));
ok(jwk1_ko.equals(jwk2_ko));
ok(!rsa_ko.equals(jwk1_ko));
Expand Down

0 comments on commit 6ac0b16

Please # to comment.