Coverage Report

Created: 2025-10-31 09:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/node/src/crypto/crypto_sig.h
Line
Count
Source
1
#ifndef SRC_CRYPTO_CRYPTO_SIG_H_
2
#define SRC_CRYPTO_CRYPTO_SIG_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "base_object.h"
7
#include "crypto/crypto_keys.h"
8
#include "crypto/crypto_util.h"
9
#include "env.h"
10
#include "memory_tracker.h"
11
12
namespace node {
13
namespace crypto {
14
static const unsigned int kNoDsaSignature = static_cast<unsigned int>(-1);
15
16
enum class DSASigEnc { DER, P1363, Invalid };
17
18
class SignBase : public BaseObject {
19
 public:
20
  enum class Error {
21
    Ok,
22
    UnknownDigest,
23
    Init,
24
    NotInitialised,
25
    Update,
26
    PrivateKey,
27
    PublicKey,
28
    MalformedSignature,
29
    ContextUnsupported,
30
  };
31
32
  SignBase(Environment* env, v8::Local<v8::Object> wrap);
33
34
  Error Init(const char* digest);
35
  Error Update(const char* data, size_t len);
36
37
  // TODO(joyeecheung): track the memory used by OpenSSL types
38
  void MemoryInfo(MemoryTracker* tracker) const override;
39
  SET_MEMORY_INFO_NAME(SignBase)
40
  SET_SELF_SIZE(SignBase)
41
42
 protected:
43
  ncrypto::EVPMDCtxPointer mdctx_;
44
};
45
46
class Sign final : public SignBase {
47
 public:
48
  static void Initialize(Environment* env, v8::Local<v8::Object> target);
49
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
50
51
  struct SignResult {
52
    Error error;
53
    std::unique_ptr<v8::BackingStore> signature;
54
55
    inline explicit SignResult(
56
        Error err, std::unique_ptr<v8::BackingStore>&& sig = nullptr)
57
0
        : error(err), signature(std::move(sig)) {}
58
  };
59
60
  SignResult SignFinal(const ncrypto::EVPKeyPointer& pkey,
61
                       int padding,
62
                       std::optional<int> saltlen,
63
                       DSASigEnc dsa_sig_enc);
64
65
  static void SignSync(const v8::FunctionCallbackInfo<v8::Value>& args);
66
67
 protected:
68
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
69
  static void SignInit(const v8::FunctionCallbackInfo<v8::Value>& args);
70
  static void SignUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
71
  static void SignFinal(const v8::FunctionCallbackInfo<v8::Value>& args);
72
73
  Sign(Environment* env, v8::Local<v8::Object> wrap);
74
};
75
76
class Verify final : public SignBase {
77
 public:
78
  static void Initialize(Environment* env, v8::Local<v8::Object> target);
79
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
80
81
  Error VerifyFinal(const ncrypto::EVPKeyPointer& key,
82
                    const ByteSource& sig,
83
                    int padding,
84
                    std::optional<int> saltlen,
85
                    bool* verify_result);
86
87
  static void VerifySync(const v8::FunctionCallbackInfo<v8::Value>& args);
88
89
 protected:
90
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
91
  static void VerifyInit(const v8::FunctionCallbackInfo<v8::Value>& args);
92
  static void VerifyUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
93
  static void VerifyFinal(const v8::FunctionCallbackInfo<v8::Value>& args);
94
95
  Verify(Environment* env, v8::Local<v8::Object> wrap);
96
};
97
98
struct SignConfiguration final : public MemoryRetainer {
99
  enum class Mode { Sign, Verify };
100
  enum Flags {
101
    kHasNone = 0,
102
    kHasSaltLength = 1,
103
    kHasPadding = 2,
104
    kHasContextString = 4
105
  };
106
107
  CryptoJobMode job_mode;
108
  Mode mode;
109
  KeyObjectData key;
110
  ByteSource data;
111
  ByteSource signature;
112
  ncrypto::Digest digest;
113
  int flags = SignConfiguration::kHasNone;
114
  int padding = 0;
115
  int salt_length = 0;
116
  DSASigEnc dsa_encoding = DSASigEnc::DER;
117
  ByteSource context_string;
118
119
0
  SignConfiguration() = default;
120
121
  explicit SignConfiguration(SignConfiguration&& other) noexcept;
122
123
  SignConfiguration& operator=(SignConfiguration&& other) noexcept;
124
125
  void MemoryInfo(MemoryTracker* tracker) const override;
126
  SET_MEMORY_INFO_NAME(SignConfiguration)
127
  SET_SELF_SIZE(SignConfiguration)
128
};
129
130
struct SignTraits final {
131
  using AdditionalParameters = SignConfiguration;
132
  static constexpr const char* JobName = "SignJob";
133
134
  static constexpr AsyncWrap::ProviderType Provider =
135
      AsyncWrap::PROVIDER_SIGNREQUEST;
136
137
  static v8::Maybe<void> AdditionalConfig(
138
      CryptoJobMode mode,
139
      const v8::FunctionCallbackInfo<v8::Value>& args,
140
      unsigned int offset,
141
      SignConfiguration* params);
142
143
  static bool DeriveBits(Environment* env,
144
                         const SignConfiguration& params,
145
                         ByteSource* out,
146
                         CryptoJobMode mode);
147
148
  static v8::MaybeLocal<v8::Value> EncodeOutput(Environment* env,
149
                                                const SignConfiguration& params,
150
                                                ByteSource* out);
151
};
152
153
using SignJob = DeriveBitsJob<SignTraits>;
154
155
}  // namespace crypto
156
}  // namespace node
157
158
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
159
#endif  // SRC_CRYPTO_CRYPTO_SIG_H_