11cb0ef41Sopenharmony_ci#ifndef SRC_CRYPTO_CRYPTO_SIG_H_
21cb0ef41Sopenharmony_ci#define SRC_CRYPTO_CRYPTO_SIG_H_
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#include "base_object.h"
71cb0ef41Sopenharmony_ci#include "crypto/crypto_keys.h"
81cb0ef41Sopenharmony_ci#include "crypto/crypto_util.h"
91cb0ef41Sopenharmony_ci#include "env.h"
101cb0ef41Sopenharmony_ci#include "memory_tracker.h"
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cinamespace node {
131cb0ef41Sopenharmony_cinamespace crypto {
141cb0ef41Sopenharmony_cistatic const unsigned int kNoDsaSignature = static_cast<unsigned int>(-1);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cienum DSASigEnc {
171cb0ef41Sopenharmony_ci  kSigEncDER,
181cb0ef41Sopenharmony_ci  kSigEncP1363
191cb0ef41Sopenharmony_ci};
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciclass SignBase : public BaseObject {
221cb0ef41Sopenharmony_ci public:
231cb0ef41Sopenharmony_ci  enum Error {
241cb0ef41Sopenharmony_ci    kSignOk,
251cb0ef41Sopenharmony_ci    kSignUnknownDigest,
261cb0ef41Sopenharmony_ci    kSignInit,
271cb0ef41Sopenharmony_ci    kSignNotInitialised,
281cb0ef41Sopenharmony_ci    kSignUpdate,
291cb0ef41Sopenharmony_ci    kSignPrivateKey,
301cb0ef41Sopenharmony_ci    kSignPublicKey,
311cb0ef41Sopenharmony_ci    kSignMalformedSignature
321cb0ef41Sopenharmony_ci  };
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  SignBase(Environment* env, v8::Local<v8::Object> wrap);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  Error Init(const char* sign_type);
371cb0ef41Sopenharmony_ci  Error Update(const char* data, size_t len);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  // TODO(joyeecheung): track the memory used by OpenSSL types
401cb0ef41Sopenharmony_ci  void MemoryInfo(MemoryTracker* tracker) const override;
411cb0ef41Sopenharmony_ci  SET_MEMORY_INFO_NAME(SignBase)
421cb0ef41Sopenharmony_ci  SET_SELF_SIZE(SignBase)
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci protected:
451cb0ef41Sopenharmony_ci  EVPMDPointer mdctx_;
461cb0ef41Sopenharmony_ci};
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciclass Sign : public SignBase {
491cb0ef41Sopenharmony_ci public:
501cb0ef41Sopenharmony_ci  static void Initialize(Environment* env, v8::Local<v8::Object> target);
511cb0ef41Sopenharmony_ci  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  struct SignResult {
541cb0ef41Sopenharmony_ci    Error error;
551cb0ef41Sopenharmony_ci    std::unique_ptr<v8::BackingStore> signature;
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci    explicit SignResult(
581cb0ef41Sopenharmony_ci        Error err,
591cb0ef41Sopenharmony_ci        std::unique_ptr<v8::BackingStore>&& sig = nullptr)
601cb0ef41Sopenharmony_ci      : error(err), signature(std::move(sig)) {}
611cb0ef41Sopenharmony_ci  };
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  SignResult SignFinal(
641cb0ef41Sopenharmony_ci      const ManagedEVPPKey& pkey,
651cb0ef41Sopenharmony_ci      int padding,
661cb0ef41Sopenharmony_ci      const v8::Maybe<int>& saltlen,
671cb0ef41Sopenharmony_ci      DSASigEnc dsa_sig_enc);
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  static void SignSync(const v8::FunctionCallbackInfo<v8::Value>& args);
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci protected:
721cb0ef41Sopenharmony_ci  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
731cb0ef41Sopenharmony_ci  static void SignInit(const v8::FunctionCallbackInfo<v8::Value>& args);
741cb0ef41Sopenharmony_ci  static void SignUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
751cb0ef41Sopenharmony_ci  static void SignFinal(const v8::FunctionCallbackInfo<v8::Value>& args);
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  Sign(Environment* env, v8::Local<v8::Object> wrap);
781cb0ef41Sopenharmony_ci};
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ciclass Verify : public SignBase {
811cb0ef41Sopenharmony_ci public:
821cb0ef41Sopenharmony_ci  static void Initialize(Environment* env, v8::Local<v8::Object> target);
831cb0ef41Sopenharmony_ci  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  Error VerifyFinal(const ManagedEVPPKey& key,
861cb0ef41Sopenharmony_ci                    const ByteSource& sig,
871cb0ef41Sopenharmony_ci                    int padding,
881cb0ef41Sopenharmony_ci                    const v8::Maybe<int>& saltlen,
891cb0ef41Sopenharmony_ci                    bool* verify_result);
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci  static void VerifySync(const v8::FunctionCallbackInfo<v8::Value>& args);
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci protected:
941cb0ef41Sopenharmony_ci  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
951cb0ef41Sopenharmony_ci  static void VerifyInit(const v8::FunctionCallbackInfo<v8::Value>& args);
961cb0ef41Sopenharmony_ci  static void VerifyUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
971cb0ef41Sopenharmony_ci  static void VerifyFinal(const v8::FunctionCallbackInfo<v8::Value>& args);
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  Verify(Environment* env, v8::Local<v8::Object> wrap);
1001cb0ef41Sopenharmony_ci};
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_cistruct SignConfiguration final : public MemoryRetainer {
1031cb0ef41Sopenharmony_ci  enum Mode {
1041cb0ef41Sopenharmony_ci    kSign,
1051cb0ef41Sopenharmony_ci    kVerify
1061cb0ef41Sopenharmony_ci  };
1071cb0ef41Sopenharmony_ci  enum Flags {
1081cb0ef41Sopenharmony_ci    kHasNone = 0,
1091cb0ef41Sopenharmony_ci    kHasSaltLength = 1,
1101cb0ef41Sopenharmony_ci    kHasPadding = 2
1111cb0ef41Sopenharmony_ci  };
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci  CryptoJobMode job_mode;
1141cb0ef41Sopenharmony_ci  Mode mode;
1151cb0ef41Sopenharmony_ci  ManagedEVPPKey key;
1161cb0ef41Sopenharmony_ci  ByteSource data;
1171cb0ef41Sopenharmony_ci  ByteSource signature;
1181cb0ef41Sopenharmony_ci  const EVP_MD* digest = nullptr;
1191cb0ef41Sopenharmony_ci  int flags = SignConfiguration::kHasNone;
1201cb0ef41Sopenharmony_ci  int padding = 0;
1211cb0ef41Sopenharmony_ci  int salt_length = 0;
1221cb0ef41Sopenharmony_ci  DSASigEnc dsa_encoding = kSigEncDER;
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci  SignConfiguration() = default;
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci  explicit SignConfiguration(SignConfiguration&& other) noexcept;
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci  SignConfiguration& operator=(SignConfiguration&& other) noexcept;
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  void MemoryInfo(MemoryTracker* tracker) const override;
1311cb0ef41Sopenharmony_ci  SET_MEMORY_INFO_NAME(SignConfiguration)
1321cb0ef41Sopenharmony_ci  SET_SELF_SIZE(SignConfiguration)
1331cb0ef41Sopenharmony_ci};
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_cistruct SignTraits final {
1361cb0ef41Sopenharmony_ci  using AdditionalParameters = SignConfiguration;
1371cb0ef41Sopenharmony_ci  static constexpr const char* JobName = "SignJob";
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci// TODO(@jasnell): Sign request vs. Verify request
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci  static constexpr AsyncWrap::ProviderType Provider =
1421cb0ef41Sopenharmony_ci      AsyncWrap::PROVIDER_SIGNREQUEST;
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci  static v8::Maybe<bool> AdditionalConfig(
1451cb0ef41Sopenharmony_ci      CryptoJobMode mode,
1461cb0ef41Sopenharmony_ci      const v8::FunctionCallbackInfo<v8::Value>& args,
1471cb0ef41Sopenharmony_ci      unsigned int offset,
1481cb0ef41Sopenharmony_ci      SignConfiguration* params);
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci  static bool DeriveBits(
1511cb0ef41Sopenharmony_ci      Environment* env,
1521cb0ef41Sopenharmony_ci      const SignConfiguration& params,
1531cb0ef41Sopenharmony_ci      ByteSource* out);
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  static v8::Maybe<bool> EncodeOutput(
1561cb0ef41Sopenharmony_ci      Environment* env,
1571cb0ef41Sopenharmony_ci      const SignConfiguration& params,
1581cb0ef41Sopenharmony_ci      ByteSource* out,
1591cb0ef41Sopenharmony_ci      v8::Local<v8::Value>* result);
1601cb0ef41Sopenharmony_ci};
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ciusing SignJob = DeriveBitsJob<SignTraits>;
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci}  // namespace crypto
1651cb0ef41Sopenharmony_ci}  // namespace node
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
1681cb0ef41Sopenharmony_ci#endif  // SRC_CRYPTO_CRYPTO_SIG_H_
169