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 |
|
|
static void RegisterExternalReferences(ExternalReferenceRegistry* registry); |
53 |
|
|
|
54 |
|
|
struct SignResult { |
55 |
|
|
Error error; |
56 |
|
|
std::unique_ptr<v8::BackingStore> signature; |
57 |
|
|
|
58 |
|
112 |
explicit SignResult( |
59 |
|
|
Error err, |
60 |
|
|
std::unique_ptr<v8::BackingStore>&& sig = nullptr) |
61 |
|
112 |
: error(err), signature(std::move(sig)) {} |
62 |
|
|
}; |
63 |
|
|
|
64 |
|
|
SignResult SignFinal( |
65 |
|
|
const ManagedEVPPKey& pkey, |
66 |
|
|
int padding, |
67 |
|
|
const v8::Maybe<int>& saltlen, |
68 |
|
|
DSASigEnc dsa_sig_enc); |
69 |
|
|
|
70 |
|
|
static void SignSync(const v8::FunctionCallbackInfo<v8::Value>& args); |
71 |
|
|
|
72 |
|
|
protected: |
73 |
|
|
static void New(const v8::FunctionCallbackInfo<v8::Value>& args); |
74 |
|
|
static void SignInit(const v8::FunctionCallbackInfo<v8::Value>& args); |
75 |
|
|
static void SignUpdate(const v8::FunctionCallbackInfo<v8::Value>& args); |
76 |
|
|
static void SignFinal(const v8::FunctionCallbackInfo<v8::Value>& args); |
77 |
|
|
|
78 |
|
|
Sign(Environment* env, v8::Local<v8::Object> wrap); |
79 |
|
|
}; |
80 |
|
|
|
81 |
|
|
class Verify : public SignBase { |
82 |
|
|
public: |
83 |
|
|
static void Initialize(Environment* env, v8::Local<v8::Object> target); |
84 |
|
|
static void RegisterExternalReferences(ExternalReferenceRegistry* registry); |
85 |
|
|
|
86 |
|
|
Error VerifyFinal(const ManagedEVPPKey& key, |
87 |
|
|
const ByteSource& sig, |
88 |
|
|
int padding, |
89 |
|
|
const v8::Maybe<int>& saltlen, |
90 |
|
|
bool* verify_result); |
91 |
|
|
|
92 |
|
|
static void VerifySync(const v8::FunctionCallbackInfo<v8::Value>& args); |
93 |
|
|
|
94 |
|
|
protected: |
95 |
|
|
static void New(const v8::FunctionCallbackInfo<v8::Value>& args); |
96 |
|
|
static void VerifyInit(const v8::FunctionCallbackInfo<v8::Value>& args); |
97 |
|
|
static void VerifyUpdate(const v8::FunctionCallbackInfo<v8::Value>& args); |
98 |
|
|
static void VerifyFinal(const v8::FunctionCallbackInfo<v8::Value>& args); |
99 |
|
|
|
100 |
|
|
Verify(Environment* env, v8::Local<v8::Object> wrap); |
101 |
|
|
}; |
102 |
|
|
|
103 |
|
|
struct SignConfiguration final : public MemoryRetainer { |
104 |
|
|
enum Mode { |
105 |
|
|
kSign, |
106 |
|
|
kVerify |
107 |
|
|
}; |
108 |
|
|
enum Flags { |
109 |
|
|
kHasNone = 0, |
110 |
|
|
kHasSaltLength = 1, |
111 |
|
|
kHasPadding = 2 |
112 |
|
|
}; |
113 |
|
|
|
114 |
|
|
CryptoJobMode job_mode; |
115 |
|
|
Mode mode; |
116 |
|
|
ManagedEVPPKey key; |
117 |
|
|
ByteSource data; |
118 |
|
|
ByteSource signature; |
119 |
|
|
const EVP_MD* digest = nullptr; |
120 |
|
|
int flags = SignConfiguration::kHasNone; |
121 |
|
|
int padding = 0; |
122 |
|
|
int salt_length = 0; |
123 |
|
|
DSASigEnc dsa_encoding = kSigEncDER; |
124 |
|
|
|
125 |
|
1502 |
SignConfiguration() = default; |
126 |
|
|
|
127 |
|
|
explicit SignConfiguration(SignConfiguration&& other) noexcept; |
128 |
|
|
|
129 |
|
|
SignConfiguration& operator=(SignConfiguration&& other) noexcept; |
130 |
|
|
|
131 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
132 |
|
|
SET_MEMORY_INFO_NAME(SignConfiguration) |
133 |
|
|
SET_SELF_SIZE(SignConfiguration) |
134 |
|
|
}; |
135 |
|
|
|
136 |
|
|
struct SignTraits final { |
137 |
|
|
using AdditionalParameters = SignConfiguration; |
138 |
|
|
static constexpr const char* JobName = "SignJob"; |
139 |
|
|
|
140 |
|
|
// TODO(@jasnell): Sign request vs. Verify request |
141 |
|
|
|
142 |
|
|
static constexpr AsyncWrap::ProviderType Provider = |
143 |
|
|
AsyncWrap::PROVIDER_SIGNREQUEST; |
144 |
|
|
|
145 |
|
|
static v8::Maybe<bool> AdditionalConfig( |
146 |
|
|
CryptoJobMode mode, |
147 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
148 |
|
|
unsigned int offset, |
149 |
|
|
SignConfiguration* params); |
150 |
|
|
|
151 |
|
|
static bool DeriveBits( |
152 |
|
|
Environment* env, |
153 |
|
|
const SignConfiguration& params, |
154 |
|
|
ByteSource* out); |
155 |
|
|
|
156 |
|
|
static v8::Maybe<bool> EncodeOutput( |
157 |
|
|
Environment* env, |
158 |
|
|
const SignConfiguration& params, |
159 |
|
|
ByteSource* out, |
160 |
|
|
v8::Local<v8::Value>* result); |
161 |
|
|
}; |
162 |
|
|
|
163 |
|
|
using SignJob = DeriveBitsJob<SignTraits>; |
164 |
|
|
|
165 |
|
|
} // namespace crypto |
166 |
|
|
} // namespace node |
167 |
|
|
|
168 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
169 |
|
|
#endif // SRC_CRYPTO_CRYPTO_SIG_H_ |