1 |
|
|
#ifndef SRC_CRYPTO_CRYPTO_EC_H_ |
2 |
|
|
#define SRC_CRYPTO_CRYPTO_EC_H_ |
3 |
|
|
|
4 |
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
5 |
|
|
|
6 |
|
|
#include "crypto/crypto_keys.h" |
7 |
|
|
#include "crypto/crypto_keygen.h" |
8 |
|
|
#include "crypto/crypto_util.h" |
9 |
|
|
#include "allocated_buffer.h" |
10 |
|
|
#include "async_wrap.h" |
11 |
|
|
#include "base_object.h" |
12 |
|
|
#include "env.h" |
13 |
|
|
#include "memory_tracker.h" |
14 |
|
|
#include "node_internals.h" |
15 |
|
|
#include "v8.h" |
16 |
|
|
|
17 |
|
|
namespace node { |
18 |
|
|
namespace crypto { |
19 |
|
|
int GetCurveFromName(const char* name); |
20 |
|
|
int GetOKPCurveFromName(const char* name); |
21 |
|
|
|
22 |
|
|
class ECDH final : public BaseObject { |
23 |
|
|
public: |
24 |
|
|
~ECDH() override; |
25 |
|
|
|
26 |
|
|
static void Initialize(Environment* env, v8::Local<v8::Object> target); |
27 |
|
|
static ECPointPointer BufferToPoint(Environment* env, |
28 |
|
|
const EC_GROUP* group, |
29 |
|
|
v8::Local<v8::Value> buf); |
30 |
|
|
|
31 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
32 |
|
|
SET_MEMORY_INFO_NAME(ECDH) |
33 |
|
|
SET_SELF_SIZE(ECDH) |
34 |
|
|
|
35 |
|
|
static void ConvertKey(const v8::FunctionCallbackInfo<v8::Value>& args); |
36 |
|
|
|
37 |
|
|
static void GetCurves(const v8::FunctionCallbackInfo<v8::Value>& args); |
38 |
|
|
|
39 |
|
|
protected: |
40 |
|
|
ECDH(Environment* env, v8::Local<v8::Object> wrap, ECKeyPointer&& key); |
41 |
|
|
|
42 |
|
|
static void New(const v8::FunctionCallbackInfo<v8::Value>& args); |
43 |
|
|
static void GenerateKeys(const v8::FunctionCallbackInfo<v8::Value>& args); |
44 |
|
|
static void ComputeSecret(const v8::FunctionCallbackInfo<v8::Value>& args); |
45 |
|
|
static void GetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args); |
46 |
|
|
static void SetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args); |
47 |
|
|
static void GetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args); |
48 |
|
|
static void SetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args); |
49 |
|
|
|
50 |
|
|
bool IsKeyPairValid(); |
51 |
|
|
bool IsKeyValidForCurve(const BignumPointer& private_key); |
52 |
|
|
|
53 |
|
|
ECKeyPointer key_; |
54 |
|
|
const EC_GROUP* group_; |
55 |
|
|
}; |
56 |
|
|
|
57 |
|
|
struct ECDHBitsConfig final : public MemoryRetainer { |
58 |
|
|
int id_; |
59 |
|
|
std::shared_ptr<KeyObjectData> private_; |
60 |
|
|
std::shared_ptr<KeyObjectData> public_; |
61 |
|
|
|
62 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
63 |
|
|
SET_MEMORY_INFO_NAME(ECDHBitsConfig); |
64 |
|
|
SET_SELF_SIZE(ECDHBitsConfig); |
65 |
|
|
}; |
66 |
|
|
|
67 |
|
|
struct ECDHBitsTraits final { |
68 |
|
|
using AdditionalParameters = ECDHBitsConfig; |
69 |
|
|
static constexpr const char* JobName = "ECDHBitsJob"; |
70 |
|
|
static constexpr AsyncWrap::ProviderType Provider = |
71 |
|
|
AsyncWrap::PROVIDER_DERIVEBITSREQUEST; |
72 |
|
|
|
73 |
|
|
static v8::Maybe<bool> AdditionalConfig( |
74 |
|
|
CryptoJobMode mode, |
75 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
76 |
|
|
unsigned int offset, |
77 |
|
|
ECDHBitsConfig* params); |
78 |
|
|
|
79 |
|
|
static bool DeriveBits( |
80 |
|
|
Environment* env, |
81 |
|
|
const ECDHBitsConfig& params, |
82 |
|
|
ByteSource* out_); |
83 |
|
|
|
84 |
|
|
static v8::Maybe<bool> EncodeOutput( |
85 |
|
|
Environment* env, |
86 |
|
|
const ECDHBitsConfig& params, |
87 |
|
|
ByteSource* out, |
88 |
|
|
v8::Local<v8::Value>* result); |
89 |
|
|
}; |
90 |
|
|
|
91 |
|
|
using ECDHBitsJob = DeriveBitsJob<ECDHBitsTraits>; |
92 |
|
|
|
93 |
|
|
struct EcKeyPairParams final : public MemoryRetainer { |
94 |
|
|
int curve_nid; |
95 |
|
|
int param_encoding; |
96 |
|
|
SET_NO_MEMORY_INFO() |
97 |
|
|
SET_MEMORY_INFO_NAME(EcKeyPairParams) |
98 |
|
|
SET_SELF_SIZE(EcKeyPairParams) |
99 |
|
|
}; |
100 |
|
|
|
101 |
|
|
using EcKeyPairGenConfig = KeyPairGenConfig<EcKeyPairParams>; |
102 |
|
|
|
103 |
|
|
struct EcKeyGenTraits final { |
104 |
|
|
using AdditionalParameters = EcKeyPairGenConfig; |
105 |
|
|
static constexpr const char* JobName = "EcKeyPairGenJob"; |
106 |
|
|
|
107 |
|
|
static EVPKeyCtxPointer Setup(EcKeyPairGenConfig* params); |
108 |
|
|
|
109 |
|
|
static v8::Maybe<bool> AdditionalConfig( |
110 |
|
|
CryptoJobMode mode, |
111 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
112 |
|
|
unsigned int* offset, |
113 |
|
|
EcKeyPairGenConfig* params); |
114 |
|
|
}; |
115 |
|
|
|
116 |
|
|
using ECKeyPairGenJob = KeyGenJob<KeyPairGenTraits<EcKeyGenTraits>>; |
117 |
|
|
|
118 |
|
|
// There is currently no additional information that the |
119 |
|
|
// ECKeyExport needs to collect, but we need to provide |
120 |
|
|
// the base struct anyway. |
121 |
|
|
struct ECKeyExportConfig final : public MemoryRetainer { |
122 |
|
|
SET_NO_MEMORY_INFO() |
123 |
|
|
SET_MEMORY_INFO_NAME(ECKeyExportConfig) |
124 |
|
|
SET_SELF_SIZE(ECKeyExportConfig) |
125 |
|
|
}; |
126 |
|
|
|
127 |
|
|
struct ECKeyExportTraits final { |
128 |
|
|
static constexpr const char* JobName = "ECKeyExportJob"; |
129 |
|
|
using AdditionalParameters = ECKeyExportConfig; |
130 |
|
|
|
131 |
|
|
static v8::Maybe<bool> AdditionalConfig( |
132 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
133 |
|
|
unsigned int offset, |
134 |
|
|
ECKeyExportConfig* config); |
135 |
|
|
|
136 |
|
|
static WebCryptoKeyExportStatus DoExport( |
137 |
|
|
std::shared_ptr<KeyObjectData> key_data, |
138 |
|
|
WebCryptoKeyFormat format, |
139 |
|
|
const ECKeyExportConfig& params, |
140 |
|
|
ByteSource* out); |
141 |
|
|
}; |
142 |
|
|
|
143 |
|
|
using ECKeyExportJob = KeyExportJob<ECKeyExportTraits>; |
144 |
|
|
|
145 |
|
|
v8::Maybe<bool> ExportJWKEcKey( |
146 |
|
|
Environment* env, |
147 |
|
|
std::shared_ptr<KeyObjectData> key, |
148 |
|
|
v8::Local<v8::Object> target); |
149 |
|
|
|
150 |
|
|
v8::Maybe<bool> ExportJWKEdKey( |
151 |
|
|
Environment* env, |
152 |
|
|
std::shared_ptr<KeyObjectData> key, |
153 |
|
|
v8::Local<v8::Object> target); |
154 |
|
|
|
155 |
|
|
std::shared_ptr<KeyObjectData> ImportJWKEcKey( |
156 |
|
|
Environment* env, |
157 |
|
|
v8::Local<v8::Object> jwk, |
158 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
159 |
|
|
unsigned int offset); |
160 |
|
|
|
161 |
|
|
v8::Maybe<bool> GetEcKeyDetail( |
162 |
|
|
Environment* env, |
163 |
|
|
std::shared_ptr<KeyObjectData> key, |
164 |
|
|
v8::Local<v8::Object> target); |
165 |
|
|
|
166 |
|
|
ByteSource ConvertToWebCryptoSignature( |
167 |
|
|
ManagedEVPPKey key, |
168 |
|
|
const ByteSource& signature); |
169 |
|
|
|
170 |
|
|
ByteSource ConvertFromWebCryptoSignature( |
171 |
|
|
ManagedEVPPKey key, |
172 |
|
|
const ByteSource& signature); |
173 |
|
|
|
174 |
|
|
} // namespace crypto |
175 |
|
|
} // namespace node |
176 |
|
|
|
177 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
178 |
|
|
#endif // SRC_CRYPTO_CRYPTO_EC_H_ |