GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_sig.h Lines: 3 7 42.9 %
Date: 2021-09-20 04:12:42 Branches: 0 0 - %

Line Branch Exec 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 "crypto/crypto_keys.h"
7
#include "crypto/crypto_util.h"
8
#include "allocated_buffer.h"
9
#include "base_object.h"
10
#include "env.h"
11
#include "memory_tracker.h"
12
13
namespace node {
14
namespace crypto {
15
static const unsigned int kNoDsaSignature = static_cast<unsigned int>(-1);
16
17
enum DSASigEnc {
18
  kSigEncDER,
19
  kSigEncP1363
20
};
21
22
class SignBase : public BaseObject {
23
 public:
24
  typedef enum {
25
    kSignOk,
26
    kSignUnknownDigest,
27
    kSignInit,
28
    kSignNotInitialised,
29
    kSignUpdate,
30
    kSignPrivateKey,
31
    kSignPublicKey,
32
    kSignMalformedSignature
33
  } Error;
34
35
  SignBase(Environment* env, v8::Local<v8::Object> wrap);
36
37
  Error Init(const char* sign_type);
38
  Error Update(const char* data, size_t len);
39
40
  // TODO(joyeecheung): track the memory used by OpenSSL types
41
  void MemoryInfo(MemoryTracker* tracker) const override;
42
  SET_MEMORY_INFO_NAME(SignBase)
43
  SET_SELF_SIZE(SignBase)
44
45
 protected:
46
  EVPMDPointer mdctx_;
47
};
48
49
class Sign : public SignBase {
50
 public:
51
  static void Initialize(Environment* env, v8::Local<v8::Object> target);
52
53
  struct SignResult {
54
    Error error;
55
    AllocatedBuffer signature;
56
57
112
    explicit SignResult(
58
        Error err,
59
        AllocatedBuffer&& sig = AllocatedBuffer())
60
112
      : error(err), signature(std::move(sig)) {}
61
  };
62
63
  SignResult SignFinal(
64
      const ManagedEVPPKey& pkey,
65
      int padding,
66
      const v8::Maybe<int>& saltlen,
67
      DSASigEnc dsa_sig_enc);
68
69
  static void SignSync(const v8::FunctionCallbackInfo<v8::Value>& args);
70
71
 protected:
72
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
73
  static void SignInit(const v8::FunctionCallbackInfo<v8::Value>& args);
74
  static void SignUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
75
  static void SignFinal(const v8::FunctionCallbackInfo<v8::Value>& args);
76
77
  Sign(Environment* env, v8::Local<v8::Object> wrap);
78
};
79
80
class Verify : public SignBase {
81
 public:
82
  static void Initialize(Environment* env, v8::Local<v8::Object> target);
83
84
  Error VerifyFinal(const ManagedEVPPKey& key,
85
                    const ByteSource& sig,
86
                    int padding,
87
                    const v8::Maybe<int>& saltlen,
88
                    bool* verify_result);
89
90
  static void VerifySync(const v8::FunctionCallbackInfo<v8::Value>& args);
91
92
 protected:
93
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
94
  static void VerifyInit(const v8::FunctionCallbackInfo<v8::Value>& args);
95
  static void VerifyUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
96
  static void VerifyFinal(const v8::FunctionCallbackInfo<v8::Value>& args);
97
98
  Verify(Environment* env, v8::Local<v8::Object> wrap);
99
};
100
101
struct SignConfiguration final : public MemoryRetainer {
102
  enum Mode {
103
    kSign,
104
    kVerify
105
  };
106
  enum Flags {
107
    kHasNone = 0,
108
    kHasSaltLength = 1,
109
    kHasPadding = 2
110
  };
111
112
  CryptoJobMode job_mode;
113
  Mode mode;
114
  ManagedEVPPKey key;
115
  ByteSource data;
116
  ByteSource signature;
117
  const EVP_MD* digest = nullptr;
118
  int flags = SignConfiguration::kHasNone;
119
  int padding = 0;
120
  int salt_length = 0;
121
  DSASigEnc dsa_encoding = kSigEncDER;
122
123
1501
  SignConfiguration() = default;
124
125
  explicit SignConfiguration(SignConfiguration&& other) noexcept;
126
127
  SignConfiguration& operator=(SignConfiguration&& other) noexcept;
128
129
  void MemoryInfo(MemoryTracker* tracker) const override;
130
  SET_MEMORY_INFO_NAME(SignConfiguration)
131
  SET_SELF_SIZE(SignConfiguration)
132
};
133
134
struct SignTraits final {
135
  using AdditionalParameters = SignConfiguration;
136
  static constexpr const char* JobName = "SignJob";
137
138
// TODO(@jasnell): Sign request vs. Verify request
139
140
  static constexpr AsyncWrap::ProviderType Provider =
141
      AsyncWrap::PROVIDER_SIGNREQUEST;
142
143
  static v8::Maybe<bool> AdditionalConfig(
144
      CryptoJobMode mode,
145
      const v8::FunctionCallbackInfo<v8::Value>& args,
146
      unsigned int offset,
147
      SignConfiguration* params);
148
149
  static bool DeriveBits(
150
      Environment* env,
151
      const SignConfiguration& params,
152
      ByteSource* out);
153
154
  static v8::Maybe<bool> EncodeOutput(
155
      Environment* env,
156
      const SignConfiguration& params,
157
      ByteSource* out,
158
      v8::Local<v8::Value>* result);
159
};
160
161
using SignJob = DeriveBitsJob<SignTraits>;
162
163
}  // namespace crypto
164
}  // namespace node
165
166
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
167
#endif  // SRC_CRYPTO_CRYPTO_SIG_H_