edhoc 1.2.2 → 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.
Files changed (39) hide show
  1. package/dist/edhoc.d.ts +4 -0
  2. package/dist/edhoc.d.ts.map +1 -1
  3. package/include/{LibEDHOC.h → Binding.h} +60 -40
  4. package/include/EdhocComposeAsyncWorker.h +8 -22
  5. package/include/EdhocCredentialManager.h +9 -25
  6. package/include/EdhocCryptoManager.h +27 -43
  7. package/include/EdhocEadManager.h +3 -4
  8. package/include/EdhocExportOscoreAsyncWorker.h +10 -27
  9. package/include/EdhocKeyExporterAsyncWorker.h +8 -28
  10. package/include/EdhocKeyUpdateAsyncWorker.h +7 -24
  11. package/include/EdhocProcessAsyncWorker.h +11 -36
  12. package/include/RunningContext.h +102 -0
  13. package/include/Utils.h +0 -43
  14. package/package.json +1 -2
  15. package/prebuilds/android-arm/edhoc.armv7.node +0 -0
  16. package/prebuilds/android-arm64/edhoc.armv8.node +0 -0
  17. package/prebuilds/darwin-arm64/edhoc.node +0 -0
  18. package/prebuilds/darwin-x64/edhoc.node +0 -0
  19. package/prebuilds/linux-arm/edhoc.armv6.node +0 -0
  20. package/prebuilds/linux-arm/edhoc.armv7.node +0 -0
  21. package/prebuilds/linux-arm64/edhoc.armv8.node +0 -0
  22. package/prebuilds/linux-x64/edhoc.glibc.node +0 -0
  23. package/prebuilds/linux-x64/edhoc.musl.node +0 -0
  24. package/prebuilds/win32-ia32/edhoc.node +0 -0
  25. package/prebuilds/win32-x64/edhoc.node +0 -0
  26. package/src/Binding.cpp +434 -0
  27. package/src/EdhocComposeAsyncWorker.cpp +15 -26
  28. package/src/EdhocCredentialManager.cpp +50 -69
  29. package/src/EdhocCryptoManager.cpp +158 -332
  30. package/src/EdhocEadManager.cpp +11 -11
  31. package/src/EdhocExportOscoreAsyncWorker.cpp +18 -28
  32. package/src/EdhocKeyExporterAsyncWorker.cpp +11 -21
  33. package/src/EdhocKeyUpdateAsyncWorker.cpp +8 -18
  34. package/src/EdhocProcessAsyncWorker.cpp +28 -35
  35. package/src/RunningContext.cpp +95 -0
  36. package/src/Utils.cpp +1 -38
  37. package/test/basic.test.ts +57 -3
  38. package/include/UserContext.h +0 -77
  39. package/src/LibEDHOC.cpp +0 -408
@@ -5,7 +5,7 @@
5
5
  #include <iostream>
6
6
  #include <stdexcept>
7
7
 
8
- #include "UserContext.h"
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
@@ -155,71 +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
- EdhocCredentialManager::EdhocCredentialManager(Napi::Object& jsCredentialManager) {
159
- if (!jsCredentialManager.IsObject()) {
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
- credentialManagerRef = Napi::Persistent(jsCredentialManager);
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
- credentialManagerRef.Reset();
170
- for (auto& ref : credentialReferences) {
173
+ credentialManagerRef_.Reset();
174
+ edhocRef_.Reset();
175
+ for (auto& ref : credentialReferences_) {
171
176
  ref.Reset();
172
177
  }
173
- credentialReferences.clear();
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
- UserContext* context = static_cast<UserContext*>(user_context);
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
- UserContext* context = static_cast<UserContext*>(user_context);
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
- int EdhocCredentialManager::callFetchCredentials(const void* user_context, struct edhoc_auth_creds* credentials) {
212
- std::promise<int> promise;
213
- std::future<int> future = promise.get_future();
202
+ /*
203
+ * Method to fetch credentials
204
+ */
205
+ int EdhocCredentialManager::callFetchCredentials(RunningContext* runningContext, struct edhoc_auth_creds* credentials) {
214
206
 
215
- auto successHandler = [this, &promise, &credentials](Napi::Env env, Napi::Value result) {
207
+ auto successHandler = [this, &credentials](Napi::Env env, Napi::Value result) {
216
208
  Napi::HandleScope scope(env);
217
209
  auto credsObj = result.As<Napi::Object>();
218
- credentialReferences.push_back(Napi::Persistent(credsObj));
219
210
 
220
211
  if (credsObj.IsObject() == false || credsObj.Has(kFormat) == false) {
221
212
  throw std::runtime_error(kInvalidInputCredentialTypeError);
222
213
  }
214
+
215
+ credentialReferences_.push_back(Napi::Persistent(credsObj));
216
+
223
217
  int label = credsObj.Get(kFormat).As<Napi::Number>().Int32Value();
224
218
 
225
219
  switch (label) {
@@ -234,7 +228,7 @@ int EdhocCredentialManager::callFetchCredentials(const void* user_context, struc
234
228
  break;
235
229
  case EDHOC_COSE_ANY:
236
230
  default:
237
- throw std::runtime_error(kUnsupportedCredentialTypeError);
231
+ throw Napi::Error::New(env, kUnsupportedCredentialTypeError);
238
232
  }
239
233
 
240
234
  if (credsObj.Has(kPrivateKeyId) && !credsObj.Get(kPrivateKeyId).IsNull()) {
@@ -242,36 +236,29 @@ int EdhocCredentialManager::callFetchCredentials(const void* user_context, struc
242
236
  memcpy(credentials->priv_key_id, privKeyIdBuffer.Data(), privKeyIdBuffer.Length());
243
237
  }
244
238
 
245
- promise.set_value(EDHOC_SUCCESS);
239
+ return EDHOC_SUCCESS;
246
240
  };
247
241
 
248
- auto blockingCallHandler = [this, &user_context, &successHandler, &promise](Napi::Env env,
249
- Napi::Function jsCallback) {
250
- Napi::HandleScope scope(env);
251
- std::vector<napi_value> arguments = {static_cast<const UserContext*>(user_context)->parent.Value()};
252
- auto errorHandler = Utils::CreatePromiseErrorHandler<int>(promise, EDHOC_ERROR_GENERIC_ERROR);
253
- Utils::InvokeJSFunctionWithPromiseHandling(env, credentialManagerRef.Value(), jsCallback, arguments, successHandler,
254
- errorHandler);
242
+ auto argumentsHandler = [this](Napi::Env env) {
243
+ return std::vector<napi_value> { this->edhocRef_.Value() };
255
244
  };
256
245
 
257
- fetchTsfn.BlockingCall(blockingCallHandler);
258
-
259
- future.wait();
260
- return future.get();
246
+ return runningContext->ThreadSafeBlockingCall(credentialManagerRef_, "fetch", argumentsHandler, successHandler);
261
247
  }
262
248
 
263
- int EdhocCredentialManager::callVerifyCredentials(const void* user_context,
249
+ /*
250
+ * Method to verify credentials
251
+ */
252
+ int EdhocCredentialManager::callVerifyCredentials(RunningContext* runningContext,
264
253
  struct edhoc_auth_creds* credentials,
265
254
  const uint8_t** public_key_reference,
266
255
  size_t* public_key_length) {
267
- std::promise<int> promise;
268
- std::future<int> future = promise.get_future();
269
256
 
270
- auto successHandler = [this, &promise, &credentials, &public_key_reference, &public_key_length](Napi::Env env,
271
- Napi::Value result) {
257
+ auto successHandler = [this, &credentials, &public_key_reference, &public_key_length](Napi::Env env, Napi::Value result) {
272
258
  Napi::HandleScope scope(env);
273
259
  Napi::Object credsObj = result.As<Napi::Object>();
274
- credentialReferences.push_back(Napi::Persistent(credsObj));
260
+ credentialReferences_.push_back(Napi::Persistent(credsObj));
261
+
275
262
  if (credsObj.IsObject() == false) {
276
263
  throw std::runtime_error(kInvalidInputCredentialTypeError);
277
264
  }
@@ -297,12 +284,10 @@ int EdhocCredentialManager::callVerifyCredentials(const void* user_context,
297
284
  *public_key_length = publicKeyBuffer.Length();
298
285
  }
299
286
 
300
- promise.set_value(EDHOC_SUCCESS);
287
+ return EDHOC_SUCCESS;
301
288
  };
302
289
 
303
- auto blockingCallHandler = [this, &user_context, &credentials, &successHandler, &promise](Napi::Env env,
304
- Napi::Function jsCallback) {
305
- Napi::HandleScope scope(env);
290
+ auto argumentsHandler = [this, &credentials](Napi::Env env) {
306
291
  Napi::Object resultObject = Napi::Object::New(env);
307
292
  resultObject.Set(kFormat, Napi::Number::New(env, credentials->label));
308
293
 
@@ -319,15 +304,11 @@ int EdhocCredentialManager::callVerifyCredentials(const void* user_context,
319
304
  default:
320
305
  throw std::runtime_error(kUnsupportedCredentialTypeError);
321
306
  }
322
-
323
- std::vector<napi_value> arguments = {static_cast<const UserContext*>(user_context)->parent.Value(), resultObject};
324
- auto errorHandler = Utils::CreatePromiseErrorHandler<int>(promise, EDHOC_ERROR_GENERIC_ERROR);
325
- Utils::InvokeJSFunctionWithPromiseHandling(env, credentialManagerRef.Value(), jsCallback, arguments, successHandler,
326
- errorHandler);
307
+ return std::vector<napi_value> {
308
+ this->edhocRef_.Value(),
309
+ resultObject
310
+ };
327
311
  };
328
312
 
329
- verifyTsfn.BlockingCall(blockingCallHandler);
330
-
331
- future.wait();
332
- return future.get();
313
+ return runningContext->ThreadSafeBlockingCall(credentialManagerRef_, "verify", argumentsHandler, successHandler);
333
314
  }