edhoc 1.2.3 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/binding.gyp +2 -2
- package/dist/edhoc.d.ts +4 -0
- package/dist/edhoc.d.ts.map +1 -1
- package/include/{LibEDHOC.h → Binding.h} +60 -40
- package/include/EdhocComposeAsyncWorker.h +8 -22
- package/include/EdhocCredentialManager.h +9 -25
- package/include/EdhocCryptoManager.h +27 -43
- package/include/EdhocEadManager.h +3 -4
- package/include/EdhocExportOscoreAsyncWorker.h +10 -27
- package/include/EdhocKeyExporterAsyncWorker.h +8 -28
- package/include/EdhocKeyUpdateAsyncWorker.h +7 -24
- package/include/EdhocProcessAsyncWorker.h +11 -36
- package/include/RunningContext.h +102 -0
- package/include/Utils.h +2 -46
- package/package.json +1 -2
- package/prebuilds/android-arm/edhoc.armv7.node +0 -0
- package/prebuilds/android-arm64/edhoc.armv8.node +0 -0
- package/prebuilds/darwin-arm64/edhoc.node +0 -0
- package/prebuilds/darwin-x64/edhoc.node +0 -0
- package/prebuilds/linux-arm/edhoc.armv6.node +0 -0
- package/prebuilds/linux-arm/edhoc.armv7.node +0 -0
- package/prebuilds/linux-arm64/edhoc.armv8.node +0 -0
- package/prebuilds/linux-x64/edhoc.glibc.node +0 -0
- package/prebuilds/linux-x64/edhoc.musl.node +0 -0
- package/prebuilds/win32-ia32/edhoc.node +0 -0
- package/prebuilds/win32-x64/edhoc.node +0 -0
- package/src/Binding.cpp +434 -0
- package/src/EdhocComposeAsyncWorker.cpp +39 -57
- package/src/EdhocCredentialManager.cpp +58 -93
- package/src/EdhocCryptoManager.cpp +181 -400
- package/src/EdhocEadManager.cpp +13 -13
- package/src/EdhocExportOscoreAsyncWorker.cpp +29 -45
- package/src/EdhocKeyExporterAsyncWorker.cpp +19 -37
- package/src/EdhocKeyUpdateAsyncWorker.cpp +15 -33
- package/src/EdhocProcessAsyncWorker.cpp +82 -96
- package/src/RunningContext.cpp +95 -0
- package/src/Utils.cpp +2 -34
- package/test/basic.test.ts +57 -3
- package/include/UserContext.h +0 -78
- package/src/LibEDHOC.cpp +0 -418
- package/test/errors.test.ts +0 -129
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
#include <iostream>
|
|
6
6
|
#include <stdexcept>
|
|
7
7
|
|
|
8
|
-
#include "
|
|
8
|
+
#include "RunningContext.h"
|
|
9
9
|
#include "Utils.h"
|
|
10
10
|
|
|
11
11
|
static constexpr const char* kFormat = "format";
|
|
@@ -26,9 +26,6 @@ static constexpr const char* kInvalidInputDataErrorKid = "Invalid input data for
|
|
|
26
26
|
static constexpr const char* kInvalidInputDataErrorX509Chain = "Invalid input data for X.509 chain";
|
|
27
27
|
static constexpr const char* kInvalidInputDataErrorX509Hash = "Invalid input data for X.509 hash";
|
|
28
28
|
static constexpr const char* kErrorObjectExpected = "Object expected";
|
|
29
|
-
static constexpr const char* kErrorFunctionExpected = "Function expected";
|
|
30
|
-
static constexpr const char* kFetch = "fetch";
|
|
31
|
-
static constexpr const char* kVerify = "verify";
|
|
32
29
|
|
|
33
30
|
/*
|
|
34
31
|
* Convert a JavaScript object to an edhoc_auth_cred_key_id
|
|
@@ -36,7 +33,7 @@ static constexpr const char* kVerify = "verify";
|
|
|
36
33
|
void convert_js_to_edhoc_kid(const Napi::Object& jsObject, struct edhoc_auth_creds* credentials) {
|
|
37
34
|
Napi::Object kidObj = jsObject.Get(kKid).As<Napi::Object>();
|
|
38
35
|
if (!kidObj.Has(kIsCBOR) || !kidObj.Has(kKid) || !kidObj.Has(kCredentials)) {
|
|
39
|
-
|
|
36
|
+
throw std::runtime_error(kInvalidInputDataErrorKid);
|
|
40
37
|
}
|
|
41
38
|
|
|
42
39
|
credentials->label = EDHOC_COSE_HEADER_KID;
|
|
@@ -58,7 +55,7 @@ void convert_js_to_edhoc_kid(const Napi::Object& jsObject, struct edhoc_auth_cre
|
|
|
58
55
|
credentials->key_id.key_id_bstr_length = buffer.Length();
|
|
59
56
|
memcpy(credentials->key_id.key_id_bstr, buffer.Data(), buffer.Length());
|
|
60
57
|
} else {
|
|
61
|
-
|
|
58
|
+
throw std::runtime_error(kInvalidInputDataErrorKid);
|
|
62
59
|
}
|
|
63
60
|
|
|
64
61
|
Napi::Buffer<uint8_t> credBuffer = kidObj.Get(kCredentials).As<Napi::Buffer<uint8_t>>();
|
|
@@ -73,7 +70,7 @@ void convert_js_to_edhoc_kid(const Napi::Object& jsObject, struct edhoc_auth_cre
|
|
|
73
70
|
void convert_js_to_edhoc_x5chain(const Napi::Object& jsObject, struct edhoc_auth_creds* credentials) {
|
|
74
71
|
Napi::Object x5chainObj = jsObject.Get(kX5chain).As<Napi::Object>();
|
|
75
72
|
if (!x5chainObj.Has(kCertificates)) {
|
|
76
|
-
|
|
73
|
+
throw std::runtime_error(kInvalidInputDataErrorX509Chain);
|
|
77
74
|
}
|
|
78
75
|
|
|
79
76
|
credentials->label = EDHOC_COSE_HEADER_X509_CHAIN;
|
|
@@ -95,7 +92,7 @@ void convert_js_to_edhoc_x5chain(const Napi::Object& jsObject, struct edhoc_auth
|
|
|
95
92
|
void convert_js_to_edhoc_x5t(const Napi::Object& jsObject, struct edhoc_auth_creds* credentials) {
|
|
96
93
|
Napi::Object x5tObj = jsObject.Get(kX5t).As<Napi::Object>();
|
|
97
94
|
if (!x5tObj.Has(kCertificate) || !x5tObj.Has(kHash) || !x5tObj.Has(kHashAlgorithm)) {
|
|
98
|
-
|
|
95
|
+
throw std::runtime_error(kInvalidInputDataErrorX509Hash);
|
|
99
96
|
}
|
|
100
97
|
|
|
101
98
|
credentials->label = EDHOC_COSE_HEADER_X509_HASH;
|
|
@@ -155,73 +152,68 @@ Napi::Object convert_edhoc_x5t_to_js(const Napi::Env& env, const struct edhoc_au
|
|
|
155
152
|
return obj;
|
|
156
153
|
}
|
|
157
154
|
|
|
158
|
-
|
|
159
|
-
|
|
155
|
+
/*
|
|
156
|
+
* EdhocCredentialManager constructor
|
|
157
|
+
*/
|
|
158
|
+
EdhocCredentialManager::EdhocCredentialManager(Napi::Object& jsCredentialManager, Napi::Object& jsEdhoc) {
|
|
159
|
+
if (!jsCredentialManager.IsObject() || !jsEdhoc.IsObject()) {
|
|
160
160
|
Napi::Error::New(jsCredentialManager.Env(), kErrorObjectExpected).ThrowAsJavaScriptException();
|
|
161
161
|
}
|
|
162
|
-
|
|
162
|
+
credentialManagerRef_ = Napi::Persistent(jsCredentialManager);
|
|
163
|
+
edhocRef_ = Napi::Weak(jsEdhoc);
|
|
163
164
|
|
|
164
165
|
credentials.fetch = FetchCredentials;
|
|
165
166
|
credentials.verify = VerifyCredentials;
|
|
166
167
|
}
|
|
167
168
|
|
|
169
|
+
/*
|
|
170
|
+
* EdhocCredentialManager destructor
|
|
171
|
+
*/
|
|
168
172
|
EdhocCredentialManager::~EdhocCredentialManager() {
|
|
169
|
-
|
|
170
|
-
|
|
173
|
+
credentialManagerRef_.Reset();
|
|
174
|
+
edhocRef_.Reset();
|
|
175
|
+
for (auto& ref : credentialReferences_) {
|
|
171
176
|
ref.Reset();
|
|
172
177
|
}
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
void EdhocCredentialManager::SetupAsyncFunctions() {
|
|
177
|
-
SetFunction(kFetch, fetchTsfn);
|
|
178
|
-
SetFunction(kVerify, verifyTsfn);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
void EdhocCredentialManager::CleanupAsyncFunctions() {
|
|
182
|
-
fetchTsfn.Release();
|
|
183
|
-
verifyTsfn.Release();
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
void EdhocCredentialManager::SetFunction(const char* name, Napi::ThreadSafeFunction& tsfn) {
|
|
187
|
-
Napi::Env env = credentialManagerRef.Env();
|
|
188
|
-
Napi::HandleScope scope(env);
|
|
189
|
-
Napi::Function jsFunction = credentialManagerRef.Value().Get(name).As<Napi::Function>();
|
|
190
|
-
if (!jsFunction.IsFunction()) {
|
|
191
|
-
Napi::Error::New(env, kErrorFunctionExpected).ThrowAsJavaScriptException();
|
|
192
|
-
}
|
|
193
|
-
tsfn = Napi::ThreadSafeFunction::New(env, jsFunction, name, 0, 1);
|
|
178
|
+
credentialReferences_.clear();
|
|
194
179
|
}
|
|
195
180
|
|
|
181
|
+
/*
|
|
182
|
+
* Static method to fetch credentials
|
|
183
|
+
*/
|
|
196
184
|
int EdhocCredentialManager::FetchCredentials(void* user_context, struct edhoc_auth_creds* credentials) {
|
|
197
|
-
|
|
185
|
+
RunningContext* context = static_cast<RunningContext*>(const_cast<void*>(user_context));
|
|
198
186
|
EdhocCredentialManager* manager = context->GetCredentialManager();
|
|
199
187
|
return manager->callFetchCredentials(context, credentials);
|
|
200
188
|
}
|
|
201
189
|
|
|
190
|
+
/*
|
|
191
|
+
* Static method to verify credentials
|
|
192
|
+
*/
|
|
202
193
|
int EdhocCredentialManager::VerifyCredentials(void* user_context,
|
|
203
194
|
struct edhoc_auth_creds* credentials,
|
|
204
195
|
const uint8_t** public_key_reference,
|
|
205
196
|
size_t* public_key_length) {
|
|
206
|
-
|
|
197
|
+
RunningContext* context = static_cast<RunningContext*>(const_cast<void*>(user_context));
|
|
207
198
|
EdhocCredentialManager* manager = context->GetCredentialManager();
|
|
208
199
|
return manager->callVerifyCredentials(context, credentials, public_key_reference, public_key_length);
|
|
209
200
|
}
|
|
210
201
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
202
|
+
/*
|
|
203
|
+
* Method to fetch credentials
|
|
204
|
+
*/
|
|
205
|
+
int EdhocCredentialManager::callFetchCredentials(RunningContext* runningContext, struct edhoc_auth_creds* credentials) {
|
|
214
206
|
|
|
215
|
-
|
|
216
|
-
auto successHandler = [this, &promise, &credentials, &context](Napi::Env env, Napi::Value result) {
|
|
207
|
+
auto successHandler = [this, &credentials](Napi::Env env, Napi::Value result) {
|
|
217
208
|
Napi::HandleScope scope(env);
|
|
218
209
|
auto credsObj = result.As<Napi::Object>();
|
|
219
|
-
credentialReferences.push_back(Napi::Persistent(credsObj));
|
|
220
210
|
|
|
221
211
|
if (credsObj.IsObject() == false || credsObj.Has(kFormat) == false) {
|
|
222
|
-
|
|
223
|
-
return promise.set_value(EDHOC_ERROR_GENERIC_ERROR);
|
|
212
|
+
throw std::runtime_error(kInvalidInputCredentialTypeError);
|
|
224
213
|
}
|
|
214
|
+
|
|
215
|
+
credentialReferences_.push_back(Napi::Persistent(credsObj));
|
|
216
|
+
|
|
225
217
|
int label = credsObj.Get(kFormat).As<Napi::Number>().Int32Value();
|
|
226
218
|
|
|
227
219
|
switch (label) {
|
|
@@ -236,54 +228,39 @@ int EdhocCredentialManager::callFetchCredentials(const void* user_context, struc
|
|
|
236
228
|
break;
|
|
237
229
|
case EDHOC_COSE_ANY:
|
|
238
230
|
default:
|
|
239
|
-
|
|
240
|
-
return promise.set_value(EDHOC_ERROR_GENERIC_ERROR);
|
|
231
|
+
throw Napi::Error::New(env, kUnsupportedCredentialTypeError);
|
|
241
232
|
}
|
|
242
233
|
|
|
243
234
|
if (credsObj.Has(kPrivateKeyId) && !credsObj.Get(kPrivateKeyId).IsNull()) {
|
|
244
235
|
Napi::Buffer<uint8_t> privKeyIdBuffer = credsObj.Get(kPrivateKeyId).As<Napi::Buffer<uint8_t>>();
|
|
245
236
|
memcpy(credentials->priv_key_id, privKeyIdBuffer.Data(), privKeyIdBuffer.Length());
|
|
246
237
|
}
|
|
247
|
-
else {
|
|
248
|
-
context->error = Napi::Error::New(env, kInvalidInputCredentialTypeError);
|
|
249
|
-
return promise.set_value(EDHOC_ERROR_GENERIC_ERROR);
|
|
250
|
-
}
|
|
251
238
|
|
|
252
|
-
|
|
239
|
+
return EDHOC_SUCCESS;
|
|
253
240
|
};
|
|
254
241
|
|
|
255
|
-
auto
|
|
256
|
-
|
|
257
|
-
Napi::HandleScope scope(env);
|
|
258
|
-
std::vector<napi_value> arguments = {context->parent.Value()};
|
|
259
|
-
auto errorHandler = Utils::CreatePromiseErrorHandler<int>(promise, EDHOC_ERROR_GENERIC_ERROR, context->error);
|
|
260
|
-
Utils::InvokeJSFunctionWithPromiseHandling(env, credentialManagerRef.Value(), jsCallback, arguments, successHandler,
|
|
261
|
-
errorHandler);
|
|
242
|
+
auto argumentsHandler = [this](Napi::Env env) {
|
|
243
|
+
return std::vector<napi_value> { this->edhocRef_.Value() };
|
|
262
244
|
};
|
|
263
245
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
future.wait();
|
|
267
|
-
return future.get();
|
|
246
|
+
return runningContext->ThreadSafeBlockingCall(credentialManagerRef_, "fetch", argumentsHandler, successHandler);
|
|
268
247
|
}
|
|
269
248
|
|
|
270
|
-
|
|
249
|
+
/*
|
|
250
|
+
* Method to verify credentials
|
|
251
|
+
*/
|
|
252
|
+
int EdhocCredentialManager::callVerifyCredentials(RunningContext* runningContext,
|
|
271
253
|
struct edhoc_auth_creds* credentials,
|
|
272
254
|
const uint8_t** public_key_reference,
|
|
273
255
|
size_t* public_key_length) {
|
|
274
|
-
std::promise<int> promise;
|
|
275
|
-
std::future<int> future = promise.get_future();
|
|
276
|
-
|
|
277
|
-
UserContext* context = static_cast<UserContext*>(const_cast<void*>(user_context));
|
|
278
256
|
|
|
279
|
-
auto successHandler = [this, &
|
|
280
|
-
Napi::Value result) {
|
|
257
|
+
auto successHandler = [this, &credentials, &public_key_reference, &public_key_length](Napi::Env env, Napi::Value result) {
|
|
281
258
|
Napi::HandleScope scope(env);
|
|
282
259
|
Napi::Object credsObj = result.As<Napi::Object>();
|
|
283
|
-
|
|
260
|
+
credentialReferences_.push_back(Napi::Persistent(credsObj));
|
|
261
|
+
|
|
284
262
|
if (credsObj.IsObject() == false) {
|
|
285
|
-
|
|
286
|
-
return promise.set_value(EDHOC_ERROR_GENERIC_ERROR);
|
|
263
|
+
throw std::runtime_error(kInvalidInputCredentialTypeError);
|
|
287
264
|
}
|
|
288
265
|
|
|
289
266
|
int label = credsObj.Get(kFormat).As<Napi::Number>().Int32Value();
|
|
@@ -298,8 +275,7 @@ int EdhocCredentialManager::callVerifyCredentials(const void* user_context,
|
|
|
298
275
|
convert_js_to_edhoc_x5t(credsObj, credentials);
|
|
299
276
|
break;
|
|
300
277
|
default:
|
|
301
|
-
|
|
302
|
-
return promise.set_value(EDHOC_ERROR_GENERIC_ERROR);
|
|
278
|
+
throw std::runtime_error(kUnsupportedCredentialTypeError);
|
|
303
279
|
}
|
|
304
280
|
|
|
305
281
|
if (credsObj.Has(kPublicKey) && !credsObj.Get(kPublicKey).IsNull()) {
|
|
@@ -307,17 +283,11 @@ int EdhocCredentialManager::callVerifyCredentials(const void* user_context,
|
|
|
307
283
|
*public_key_reference = publicKeyBuffer.Data();
|
|
308
284
|
*public_key_length = publicKeyBuffer.Length();
|
|
309
285
|
}
|
|
310
|
-
else {
|
|
311
|
-
context->error = Napi::Error::New(env, kInvalidInputCredentialTypeError);
|
|
312
|
-
return promise.set_value(EDHOC_ERROR_GENERIC_ERROR);
|
|
313
|
-
}
|
|
314
286
|
|
|
315
|
-
|
|
287
|
+
return EDHOC_SUCCESS;
|
|
316
288
|
};
|
|
317
289
|
|
|
318
|
-
auto
|
|
319
|
-
Napi::Function jsCallback) {
|
|
320
|
-
Napi::HandleScope scope(env);
|
|
290
|
+
auto argumentsHandler = [this, &credentials](Napi::Env env) {
|
|
321
291
|
Napi::Object resultObject = Napi::Object::New(env);
|
|
322
292
|
resultObject.Set(kFormat, Napi::Number::New(env, credentials->label));
|
|
323
293
|
|
|
@@ -332,18 +302,13 @@ int EdhocCredentialManager::callVerifyCredentials(const void* user_context,
|
|
|
332
302
|
resultObject.Set(kX5t, convert_edhoc_x5t_to_js(env, credentials->x509_hash));
|
|
333
303
|
break;
|
|
334
304
|
default:
|
|
335
|
-
|
|
336
|
-
return promise.set_value(EDHOC_ERROR_GENERIC_ERROR);
|
|
305
|
+
throw std::runtime_error(kUnsupportedCredentialTypeError);
|
|
337
306
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
errorHandler);
|
|
307
|
+
return std::vector<napi_value> {
|
|
308
|
+
this->edhocRef_.Value(),
|
|
309
|
+
resultObject
|
|
310
|
+
};
|
|
343
311
|
};
|
|
344
312
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
future.wait();
|
|
348
|
-
return future.get();
|
|
313
|
+
return runningContext->ThreadSafeBlockingCall(credentialManagerRef_, "verify", argumentsHandler, successHandler);
|
|
349
314
|
}
|