#include "crypto/crypto_keygen.h" #include "async_wrap-inl.h" #include "base_object-inl.h" #include "debug_utils-inl.h" #include "env-inl.h" #include "memory_tracker-inl.h" #include "threadpoolwork-inl.h" #include "v8.h" #include namespace node { using v8::FunctionCallbackInfo; using v8::Int32; using v8::Just; using v8::Local; using v8::Maybe; using v8::Object; using v8::Uint32; using v8::Value; namespace crypto { // NidKeyPairGenJob input arguments: // 1. CryptoJobMode // 2. NID // 3. Public Format // 4. Public Type // 5. Private Format // 6. Private Type // 7. Cipher // 8. Passphrase Maybe NidKeyPairGenTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int* offset, NidKeyPairGenConfig* params) { CHECK(args[*offset]->IsInt32()); params->params.id = args[*offset].As()->Value(); *offset += 1; return Just(true); } EVPKeyCtxPointer NidKeyPairGenTraits::Setup(NidKeyPairGenConfig* params) { EVPKeyCtxPointer ctx = EVPKeyCtxPointer(EVP_PKEY_CTX_new_id(params->params.id, nullptr)); if (!ctx || EVP_PKEY_keygen_init(ctx.get()) <= 0) return EVPKeyCtxPointer(); return ctx; } void SecretKeyGenConfig::MemoryInfo(MemoryTracker* tracker) const { if (out != nullptr) tracker->TrackFieldWithSize("out", length); } Maybe SecretKeyGenTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int* offset, SecretKeyGenConfig* params) { CHECK(args[*offset]->IsUint32()); uint32_t bits = args[*offset].As()->Value(); static_assert(std::numeric_limits::max() / CHAR_BIT <= INT_MAX); params->length = bits / CHAR_BIT; *offset += 1; return Just(true); } KeyGenJobStatus SecretKeyGenTraits::DoKeyGen( Environment* env, SecretKeyGenConfig* params) { CHECK_LE(params->length, INT_MAX); params->out = MallocOpenSSL(params->length); if (CSPRNG(reinterpret_cast(params->out), params->length).is_err()) { OPENSSL_clear_free(params->out, params->length); params->out = nullptr; params->length = 0; return KeyGenJobStatus::FAILED; } return KeyGenJobStatus::OK; } Maybe SecretKeyGenTraits::EncodeKey( Environment* env, SecretKeyGenConfig* params, Local* result) { ByteSource out = ByteSource::Allocated(params->out, params->length); std::shared_ptr data = KeyObjectData::CreateSecret(std::move(out)); return Just(KeyObjectHandle::Create(env, data).ToLocal(result)); } namespace Keygen { void Initialize(Environment* env, Local target) { NidKeyPairGenJob::Initialize(env, target); SecretKeyGenJob::Initialize(env, target); } void RegisterExternalReferences(ExternalReferenceRegistry* registry) { NidKeyPairGenJob::RegisterExternalReferences(registry); SecretKeyGenJob::RegisterExternalReferences(registry); } } // namespace Keygen } // namespace crypto } // namespace node