diff --git a/src/workerd/api/node/crypto-keys.c++ b/src/workerd/api/node/crypto-keys.c++ new file mode 100644 index 00000000000..f29c5134d54 --- /dev/null +++ b/src/workerd/api/node/crypto-keys.c++ @@ -0,0 +1,48 @@ +#include "crypto.h" + +namespace workerd::api::node { + +kj::OneOf, SubtleCrypto::JsonWebKey> CryptoImpl::exportKey( + jsg::Lock& js, + jsg::Ref key, + jsg::Optional options) { + KJ_UNIMPLEMENTED("not implemented"); +} + +bool CryptoImpl::equals(jsg::Lock& js, jsg::Ref key, jsg::Ref otherKey) { + KJ_UNIMPLEMENTED("not implemented"); +} + +CryptoImpl::AsymmetricKeyDetails CryptoImpl::getAsymmetricKeyDetail( + jsg::Lock& js, jsg::Ref key) { + KJ_UNIMPLEMENTED("not implemented"); +} + +kj::StringPtr CryptoImpl::getAsymmetricKeyType(jsg::Lock& js, jsg::Ref key) { + KJ_UNIMPLEMENTED("not implemented"); +} + +CryptoKeyPair CryptoImpl::generateKeyPair( + jsg::Lock& js, + kj::String type, + CryptoImpl::GenerateKeyPairOptions options) { + KJ_UNIMPLEMENTED("not implemented"); +} + +jsg::Ref CryptoImpl::createSecretKey(jsg::Lock& js, kj::Array) { + KJ_UNIMPLEMENTED("not implemented"); +} + +jsg::Ref CryptoImpl::createPrivateKey( + jsg::Lock& js, + CreateAsymmetricKeyOptions options) { + KJ_UNIMPLEMENTED("not implemented"); +} + +jsg::Ref CryptoImpl::createPublicKey( + jsg::Lock& js, + CreateAsymmetricKeyOptions options) { + KJ_UNIMPLEMENTED("not implemented"); +} + +} // namespace workerd::api::node diff --git a/src/workerd/api/node/crypto.h b/src/workerd/api/node/crypto.h index 911df0b6ff2..7959507618e 100644 --- a/src/workerd/api/node/crypto.h +++ b/src/workerd/api/node/crypto.h @@ -1,25 +1,131 @@ #pragma once #include +#include namespace workerd::api::node { class CryptoImpl final: public jsg::Object { public: + // Primes kj::Array randomPrime(uint32_t size, bool safe, jsg::Optional> add, jsg::Optional> rem); bool checkPrimeSync(kj::Array bufferView, uint32_t num_checks); + // Pbkdf2 kj::Array getPbkdf(kj::Array password, kj::Array salt, uint32_t num_iterations, uint32_t keylen, kj::String name); + + // Keys + struct KeyExportOptions { + jsg::Optional type; + jsg::Optional format; + jsg::Optional cipher; + jsg::Optional> passphrase; + JSG_STRUCT(type, format, cipher, passphrase); + }; + + struct AsymmetricKeyDetails { + jsg::Optional modulusLength; + jsg::Optional publicExponent; + jsg::Optional hashAlgorithm; + jsg::Optional mgf1HashAlgorithm; + jsg::Optional saltLength; + jsg::Optional divisorLength; + jsg::Optional namedCurve; + JSG_STRUCT(modulusLength, + publicExponent, + hashAlgorithm, + mgf1HashAlgorithm, + saltLength, + divisorLength, + namedCurve); + }; + + struct GenerateKeyPairOptions { + jsg::Optional modulusLength; + jsg::Optional publicExponent; + jsg::Optional hashAlgorithm; + jsg::Optional mgf1HashAlgorithm; + jsg::Optional saltLength; + jsg::Optional divisorLength; + jsg::Optional namedCurve; + jsg::Optional> prime; + jsg::Optional primeLength; + jsg::Optional generator; + jsg::Optional groupName; + jsg::Optional paramEncoding; // one of either 'named' or 'explicit' + jsg::Optional publicKeyEncoding; + jsg::Optional privateKeyEncoding; + + JSG_STRUCT(modulusLength, + publicExponent, + hashAlgorithm, + mgf1HashAlgorithm, + saltLength, + divisorLength, + namedCurve, + prime, + primeLength, + generator, + groupName, + paramEncoding, + publicKeyEncoding, + privateKeyEncoding); + }; + + struct CreateAsymmetricKeyOptions { + kj::OneOf, SubtleCrypto::JsonWebKey, jsg::Ref> key; + // For a PrivateKey, the key is one of either kj::Array or + // SubtleCrypto::JsonWebKey. For a PublicKey it can also be a CryptoKey + // containing a private key from which the public key will be derived. + jsg::Optional format; + jsg::Optional type; + jsg::Optional> passphrase; + // The passphrase is only used for private keys. The format, type, and passphrase + // options are only used if the key is a kj::Array. + JSG_STRUCT(key, format, type, passphrase); + }; + + kj::OneOf, SubtleCrypto::JsonWebKey> exportKey( + jsg::Lock& js, + jsg::Ref key, + jsg::Optional options); + + bool equals(jsg::Lock& js, jsg::Ref key, jsg::Ref otherKey); + + AsymmetricKeyDetails getAsymmetricKeyDetail(jsg::Lock& js, jsg::Ref key); + kj::StringPtr getAsymmetricKeyType(jsg::Lock& js, jsg::Ref key); + + CryptoKeyPair generateKeyPair(jsg::Lock& js, kj::String type, GenerateKeyPairOptions options); + + jsg::Ref createSecretKey(jsg::Lock& js, kj::Array); + jsg::Ref createPrivateKey(jsg::Lock& js, CreateAsymmetricKeyOptions options); + jsg::Ref createPublicKey(jsg::Lock& js, CreateAsymmetricKeyOptions options); + JSG_RESOURCE_TYPE(CryptoImpl) { + // Primes JSG_METHOD(randomPrime); JSG_METHOD(checkPrimeSync); + // Pbkdf2 JSG_METHOD(getPbkdf); + // Keys + JSG_METHOD(exportKey); + JSG_METHOD(equals); + JSG_METHOD(getAsymmetricKeyDetail); + JSG_METHOD(getAsymmetricKeyType); + JSG_METHOD(generateKeyPair); + JSG_METHOD(createSecretKey); + JSG_METHOD(createPrivateKey); + JSG_METHOD(createPublicKey); } }; -#define EW_NODE_CRYPTO_ISOLATE_TYPES \ - api::node::CryptoImpl +#define EW_NODE_CRYPTO_ISOLATE_TYPES \ + api::node::CryptoImpl, \ + api::node::CryptoImpl::KeyExportOptions, \ + api::node::CryptoImpl::AsymmetricKeyDetails, \ + api::node::CryptoImpl::GenerateKeyPairOptions, \ + api::node::CryptoImpl::CreateAsymmetricKeyOptions } // namespace workerd::api::node