edhoc 1.2.3 → 1.3.1
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
|
@@ -7,6 +7,8 @@ extern "C" {
|
|
|
7
7
|
#include "edhoc.h"
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
class RunningContext;
|
|
11
|
+
|
|
10
12
|
/**
|
|
11
13
|
* @class EdhocCryptoManager
|
|
12
14
|
* @brief Manages cryptographic operations for EDHOC protocol.
|
|
@@ -22,20 +24,20 @@ class EdhocCryptoManager {
|
|
|
22
24
|
|
|
23
25
|
/**
|
|
24
26
|
* @struct edhoc_crypto
|
|
25
|
-
* @brief
|
|
27
|
+
* @brief Edhoc's bind structure for cryptographics operations.
|
|
26
28
|
*/
|
|
27
29
|
struct edhoc_crypto crypto;
|
|
28
30
|
|
|
29
31
|
/**
|
|
30
32
|
* @struct edhoc_keys
|
|
31
|
-
* @brief
|
|
33
|
+
* @brief Edhoc's bind structure for cryptographic key identifiers.
|
|
32
34
|
*/
|
|
33
35
|
struct edhoc_keys keys;
|
|
34
36
|
|
|
35
37
|
/**
|
|
36
38
|
* @brief Constructs an EdhocCryptoManager object.
|
|
37
39
|
*/
|
|
38
|
-
EdhocCryptoManager(Napi::Object& jsCryptoManager);
|
|
40
|
+
EdhocCryptoManager(Napi::Object& jsCryptoManager, Napi::Object& jsEdhoc);
|
|
39
41
|
|
|
40
42
|
/**
|
|
41
43
|
* @brief Destroys the EdhocCryptoManager object.
|
|
@@ -265,14 +267,14 @@ class EdhocCryptoManager {
|
|
|
265
267
|
/**
|
|
266
268
|
* @brief Calls the ImportKey function.
|
|
267
269
|
*
|
|
268
|
-
* @param
|
|
270
|
+
* @param runningContext The running context.
|
|
269
271
|
* @param key_type The type of the key to import.
|
|
270
272
|
* @param raw_key The raw key data.
|
|
271
273
|
* @param raw_key_length The length of the raw key data.
|
|
272
274
|
* @param key_id The imported key ID.
|
|
273
275
|
* @return int The result code.
|
|
274
276
|
*/
|
|
275
|
-
int callImportKey(
|
|
277
|
+
int callImportKey(RunningContext* runningContext,
|
|
276
278
|
enum edhoc_key_type key_type,
|
|
277
279
|
const uint8_t* raw_key,
|
|
278
280
|
size_t raw_key_length,
|
|
@@ -285,12 +287,12 @@ class EdhocCryptoManager {
|
|
|
285
287
|
* @param key_id The key ID to destroy.
|
|
286
288
|
* @return int The result code.
|
|
287
289
|
*/
|
|
288
|
-
int callDestroyKey(
|
|
290
|
+
int callDestroyKey(RunningContext* runningContext, void* key_id);
|
|
289
291
|
|
|
290
292
|
/**
|
|
291
293
|
* @brief Calls the MakeKeyPair function.
|
|
292
294
|
*
|
|
293
|
-
* @param
|
|
295
|
+
* @param runningContext The running context.
|
|
294
296
|
* @param key_id The key ID.
|
|
295
297
|
* @param private_key The buffer to store the private key.
|
|
296
298
|
* @param private_key_size The size of the private key buffer.
|
|
@@ -300,7 +302,7 @@ class EdhocCryptoManager {
|
|
|
300
302
|
* @param public_key_length The length of the generated public key.
|
|
301
303
|
* @return int The result code.
|
|
302
304
|
*/
|
|
303
|
-
int callMakeKeyPair(
|
|
305
|
+
int callMakeKeyPair(RunningContext* runningContext,
|
|
304
306
|
const void* key_id,
|
|
305
307
|
uint8_t* private_key,
|
|
306
308
|
size_t private_key_size,
|
|
@@ -312,7 +314,7 @@ class EdhocCryptoManager {
|
|
|
312
314
|
/**
|
|
313
315
|
* @brief Calls the KeyAgreement function.
|
|
314
316
|
*
|
|
315
|
-
* @param
|
|
317
|
+
* @param runningContext The running context.
|
|
316
318
|
* @param key_id The key ID.
|
|
317
319
|
* @param peer_public_key The peer's public key.
|
|
318
320
|
* @param peer_public_key_length The length of the peer's public key.
|
|
@@ -321,7 +323,7 @@ class EdhocCryptoManager {
|
|
|
321
323
|
* @param shared_secret_length The length of the generated shared secret.
|
|
322
324
|
* @return int The result code.
|
|
323
325
|
*/
|
|
324
|
-
int callKeyAgreement(
|
|
326
|
+
int callKeyAgreement(RunningContext* runningContext,
|
|
325
327
|
const void* key_id,
|
|
326
328
|
const uint8_t* peer_public_key,
|
|
327
329
|
size_t peer_public_key_length,
|
|
@@ -332,7 +334,7 @@ class EdhocCryptoManager {
|
|
|
332
334
|
/**
|
|
333
335
|
* @brief Calls the Sign function.
|
|
334
336
|
*
|
|
335
|
-
* @param
|
|
337
|
+
* @param runningContext The running context.
|
|
336
338
|
* @param key_id The key ID.
|
|
337
339
|
* @param input The input data to sign.
|
|
338
340
|
* @param input_length The length of the input data.
|
|
@@ -341,7 +343,7 @@ class EdhocCryptoManager {
|
|
|
341
343
|
* @param signature_length The length of the generated signature.
|
|
342
344
|
* @return int The result code.
|
|
343
345
|
*/
|
|
344
|
-
int callSign(
|
|
346
|
+
int callSign(RunningContext* runningContext,
|
|
345
347
|
const void* key_id,
|
|
346
348
|
const uint8_t* input,
|
|
347
349
|
size_t input_length,
|
|
@@ -352,7 +354,7 @@ class EdhocCryptoManager {
|
|
|
352
354
|
/**
|
|
353
355
|
* @brief Calls the Verify function.
|
|
354
356
|
*
|
|
355
|
-
* @param
|
|
357
|
+
* @param runningContext The running context.
|
|
356
358
|
* @param key_id The key ID.
|
|
357
359
|
* @param input The input data.
|
|
358
360
|
* @param input_length The length of the input data.
|
|
@@ -360,7 +362,7 @@ class EdhocCryptoManager {
|
|
|
360
362
|
* @param signature_length The length of the signature.
|
|
361
363
|
* @return int The result code.
|
|
362
364
|
*/
|
|
363
|
-
int callVerify(
|
|
365
|
+
int callVerify(RunningContext* runningContext,
|
|
364
366
|
const void* key_id,
|
|
365
367
|
const uint8_t* input,
|
|
366
368
|
size_t input_length,
|
|
@@ -370,7 +372,7 @@ class EdhocCryptoManager {
|
|
|
370
372
|
/**
|
|
371
373
|
* @brief Calls the Extract function.
|
|
372
374
|
*
|
|
373
|
-
* @param
|
|
375
|
+
* @param runningContext The running context.
|
|
374
376
|
* @param key_id The key ID.
|
|
375
377
|
* @param salt The salt data.
|
|
376
378
|
* @param salt_len The length of the salt data.
|
|
@@ -380,7 +382,7 @@ class EdhocCryptoManager {
|
|
|
380
382
|
* key.
|
|
381
383
|
* @return int The result code.
|
|
382
384
|
*/
|
|
383
|
-
int callExtract(
|
|
385
|
+
int callExtract(RunningContext* runningContext,
|
|
384
386
|
const void* key_id,
|
|
385
387
|
const uint8_t* salt,
|
|
386
388
|
size_t salt_len,
|
|
@@ -391,7 +393,7 @@ class EdhocCryptoManager {
|
|
|
391
393
|
/**
|
|
392
394
|
* @brief Calls the Expand function.
|
|
393
395
|
*
|
|
394
|
-
* @param
|
|
396
|
+
* @param runningContext The running context.
|
|
395
397
|
* @param key_id The key ID.
|
|
396
398
|
* @param info The info data.
|
|
397
399
|
* @param info_length The length of the info data.
|
|
@@ -401,7 +403,7 @@ class EdhocCryptoManager {
|
|
|
401
403
|
* keying material.
|
|
402
404
|
* @return int The result code.
|
|
403
405
|
*/
|
|
404
|
-
int callExpand(
|
|
406
|
+
int callExpand(RunningContext* runningContext,
|
|
405
407
|
const void* key_id,
|
|
406
408
|
const uint8_t* info,
|
|
407
409
|
size_t info_length,
|
|
@@ -411,7 +413,7 @@ class EdhocCryptoManager {
|
|
|
411
413
|
/**
|
|
412
414
|
* @brief Calls the Encrypt function.
|
|
413
415
|
*
|
|
414
|
-
* @param
|
|
416
|
+
* @param runningContext The running context.
|
|
415
417
|
* @param key_id The key ID.
|
|
416
418
|
* @param nonce The nonce data.
|
|
417
419
|
* @param nonce_length The length of the nonce data.
|
|
@@ -424,7 +426,7 @@ class EdhocCryptoManager {
|
|
|
424
426
|
* @param ciphertext_length The length of the generated ciphertext.
|
|
425
427
|
* @return int The result code.
|
|
426
428
|
*/
|
|
427
|
-
int callEncrypt(
|
|
429
|
+
int callEncrypt(RunningContext* runningContext,
|
|
428
430
|
const void* key_id,
|
|
429
431
|
const uint8_t* nonce,
|
|
430
432
|
size_t nonce_length,
|
|
@@ -439,7 +441,7 @@ class EdhocCryptoManager {
|
|
|
439
441
|
/**
|
|
440
442
|
* @brief Calls the Decrypt function.
|
|
441
443
|
*
|
|
442
|
-
* @param
|
|
444
|
+
* @param runningContext The running context.
|
|
443
445
|
* @param key_id The key ID.
|
|
444
446
|
* @param nonce The nonce data.
|
|
445
447
|
* @param nonce_length The length of the nonce data.
|
|
@@ -452,7 +454,7 @@ class EdhocCryptoManager {
|
|
|
452
454
|
* @param plaintext_length The length of the generated plaintext.
|
|
453
455
|
* @return int The result code.
|
|
454
456
|
*/
|
|
455
|
-
int callDecrypt(
|
|
457
|
+
int callDecrypt(RunningContext* runningContext,
|
|
456
458
|
const void* key_id,
|
|
457
459
|
const uint8_t* nonce,
|
|
458
460
|
size_t nonce_length,
|
|
@@ -467,7 +469,7 @@ class EdhocCryptoManager {
|
|
|
467
469
|
/**
|
|
468
470
|
* @brief Calls the Hash function.
|
|
469
471
|
*
|
|
470
|
-
* @param
|
|
472
|
+
* @param runningContext The running context.
|
|
471
473
|
* @param input The input data to hash.
|
|
472
474
|
* @param input_length The length of the input data.
|
|
473
475
|
* @param hash The buffer to store the hash.
|
|
@@ -475,37 +477,19 @@ class EdhocCryptoManager {
|
|
|
475
477
|
* @param hash_length The length of the generated hash.
|
|
476
478
|
* @return int The result code.
|
|
477
479
|
*/
|
|
478
|
-
int callHash(
|
|
480
|
+
int callHash(RunningContext* runningContext,
|
|
479
481
|
const uint8_t* input,
|
|
480
482
|
size_t input_length,
|
|
481
483
|
uint8_t* hash,
|
|
482
484
|
size_t hash_size,
|
|
483
485
|
size_t* hash_length);
|
|
484
|
-
/**
|
|
485
|
-
* @brief Sets up the async functions.
|
|
486
|
-
*/
|
|
487
|
-
void SetupAsyncFunctions();
|
|
488
|
-
|
|
489
|
-
/**
|
|
490
|
-
* @brief Releases the async functions.
|
|
491
|
-
*/
|
|
492
|
-
void CleanupAsyncFunctions();
|
|
493
486
|
|
|
494
487
|
private:
|
|
495
488
|
Napi::ObjectReference cryptoManagerRef; ///< Reference to the JS object
|
|
489
|
+
Napi::ObjectReference edhocRef;
|
|
496
490
|
|
|
497
491
|
std::vector<Napi::Reference<Napi::Buffer<uint8_t>>> bufferReferences; ///< References to the JS buffers
|
|
498
492
|
|
|
499
|
-
/* Thread-safe functions */
|
|
500
|
-
Napi::ThreadSafeFunction importTsfn, destroyTsfn, makeKeyPairTsfn, keyAgreementTsfn, signTsfn, verifyTsfn,
|
|
501
|
-
extractTsfn, expandTsfn, encryptTsfn, decryptTsfn, hashTsfn;
|
|
502
|
-
|
|
503
|
-
/**
|
|
504
|
-
* @brief Assosciates the thread-safe function with the JS object function.
|
|
505
|
-
* @param name The name of the function in the JS credential manager object.
|
|
506
|
-
* @param tsfn The thread-safe function.
|
|
507
|
-
*/
|
|
508
|
-
void SetFunction(const char* name, Napi::ThreadSafeFunction& tsfn);
|
|
509
493
|
};
|
|
510
494
|
|
|
511
495
|
#endif // EDHOC_CRYPTO_MANAGER_H
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
#define EDHOC_EAD_MANAGER_H
|
|
3
3
|
|
|
4
4
|
#include <napi.h>
|
|
5
|
-
|
|
6
5
|
#include <map>
|
|
7
6
|
#include <vector>
|
|
8
7
|
|
|
@@ -30,7 +29,7 @@ class EdhocEadManager {
|
|
|
30
29
|
public:
|
|
31
30
|
/**
|
|
32
31
|
* @struct edhoc_ead
|
|
33
|
-
* @brief
|
|
32
|
+
* @brief Edhoc's bind structure for EAD operations.
|
|
34
33
|
*/
|
|
35
34
|
struct edhoc_ead ead;
|
|
36
35
|
|
|
@@ -87,8 +86,8 @@ class EdhocEadManager {
|
|
|
87
86
|
void ClearEadByMessage(enum edhoc_message message);
|
|
88
87
|
|
|
89
88
|
private:
|
|
90
|
-
EadBufferMap
|
|
91
|
-
|
|
89
|
+
EadBufferMap eadBuffers_; ///< Map to store the EAD buffers for different
|
|
90
|
+
///< EDHOC messages.
|
|
92
91
|
|
|
93
92
|
/**
|
|
94
93
|
* @brief Static callback function for composing an EAD token.
|
|
@@ -2,36 +2,27 @@
|
|
|
2
2
|
#define EDHOC_EXPORT_OSCORE_ASYNC_WORKER_H
|
|
3
3
|
|
|
4
4
|
#include <napi.h>
|
|
5
|
-
|
|
6
5
|
#include <functional>
|
|
7
6
|
#include <vector>
|
|
8
7
|
|
|
8
|
+
#include "RunningContext.h"
|
|
9
|
+
|
|
9
10
|
extern "C" {
|
|
10
11
|
#include "edhoc.h"
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* @class EdhocExportOscoreAsyncWorker
|
|
15
|
-
* @brief A class that represents an asynchronous worker for exporting
|
|
16
|
-
* context.
|
|
17
|
-
*
|
|
18
|
-
* This class inherits from Napi::AsyncWorker and provides methods for executing
|
|
19
|
-
* the export operation asynchronously and handling the result or error.
|
|
16
|
+
* @brief A class that represents an asynchronous worker for exporting OSCORE context.
|
|
20
17
|
*/
|
|
21
18
|
class EdhocExportOscoreAsyncWorker : public Napi::AsyncWorker {
|
|
22
19
|
public:
|
|
23
|
-
/**
|
|
24
|
-
* @brief The type definition for the callback function.
|
|
25
|
-
*/
|
|
26
|
-
using CallbackType = std::function<void(Napi::Env&)>;
|
|
27
|
-
|
|
28
20
|
/**
|
|
29
21
|
* @brief Constructs a new EdhocExportOscoreAsyncWorker object.
|
|
30
22
|
*
|
|
31
|
-
* @param
|
|
32
|
-
* @param context The reference to the edhoc_context structure.
|
|
23
|
+
* @param runningContext The pointer to the running context.
|
|
33
24
|
*/
|
|
34
|
-
EdhocExportOscoreAsyncWorker(
|
|
25
|
+
EdhocExportOscoreAsyncWorker(RunningContext* runningContext);
|
|
35
26
|
|
|
36
27
|
/**
|
|
37
28
|
* @brief Destroys the EdhocExportOscoreAsyncWorker object.
|
|
@@ -55,20 +46,12 @@ class EdhocExportOscoreAsyncWorker : public Napi::AsyncWorker {
|
|
|
55
46
|
*/
|
|
56
47
|
void OnError(const Napi::Error& error) override;
|
|
57
48
|
|
|
58
|
-
/**
|
|
59
|
-
* @brief Returns the promise object.
|
|
60
|
-
* @return The promise object.
|
|
61
|
-
*/
|
|
62
|
-
Napi::Promise GetPromise();
|
|
63
|
-
|
|
64
49
|
private:
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
std::vector<uint8_t>
|
|
68
|
-
std::vector<uint8_t>
|
|
69
|
-
std::vector<uint8_t>
|
|
70
|
-
std::vector<uint8_t> recipientId; ///< The recipient ID.
|
|
71
|
-
CallbackType callback; ///< The callback function to be executed.
|
|
50
|
+
RunningContext* runningContext_; ///< The pointer to the running context.
|
|
51
|
+
std::vector<uint8_t> masterSecret_; ///< The master secret.
|
|
52
|
+
std::vector<uint8_t> masterSalt_; ///< The master salt.
|
|
53
|
+
std::vector<uint8_t> senderId_; ///< The sender ID.
|
|
54
|
+
std::vector<uint8_t> recipientId_; ///< The recipient ID.
|
|
72
55
|
};
|
|
73
56
|
|
|
74
57
|
#endif // EDHOC_EXPORT_OSCORE_ASYNC_WORKER_H
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
#define EDHOC_KEY_EXPORTER_ASYNC_WORKER_H
|
|
3
3
|
|
|
4
4
|
#include <napi.h>
|
|
5
|
-
|
|
6
5
|
#include <vector>
|
|
7
6
|
|
|
7
|
+
#include "RunningContext.h"
|
|
8
|
+
|
|
8
9
|
extern "C" {
|
|
9
10
|
#include "edhoc.h"
|
|
10
11
|
}
|
|
@@ -12,19 +13,9 @@ extern "C" {
|
|
|
12
13
|
/**
|
|
13
14
|
* @class EdhocKeyExporterAsyncWorker
|
|
14
15
|
* @brief A class that represents an asynchronous worker for exporting PRKs.
|
|
15
|
-
*
|
|
16
|
-
* This class inherits from the Napi::AsyncWorker class and is used to perform
|
|
17
|
-
* PRK export in a separate thread. It takes an Edhoc context, a label, a
|
|
18
|
-
* desired length, and a callback function as input parameters. The export is
|
|
19
|
-
* performed in the Execute() method, and the result is returned through the
|
|
20
|
-
* OnOK() method or an error is handled through the OnError() method.
|
|
21
16
|
*/
|
|
22
17
|
class EdhocKeyExporterAsyncWorker : public Napi::AsyncWorker {
|
|
23
18
|
public:
|
|
24
|
-
/**
|
|
25
|
-
* @brief The type definition for the callback function.
|
|
26
|
-
*/
|
|
27
|
-
using CallbackType = std::function<void(Napi::Env&)>;
|
|
28
19
|
|
|
29
20
|
/**
|
|
30
21
|
* @brief Constructs a new EdhocKeyExporterAsyncWorker object.
|
|
@@ -33,13 +24,10 @@ class EdhocKeyExporterAsyncWorker : public Napi::AsyncWorker {
|
|
|
33
24
|
* @param context The reference to the edhoc_context structure.
|
|
34
25
|
* @param label The label of the key to export.
|
|
35
26
|
* @param desiredLength The desired length of the key to export.
|
|
36
|
-
* @param callback The callback function to be called after the export.
|
|
37
27
|
*/
|
|
38
|
-
EdhocKeyExporterAsyncWorker(
|
|
39
|
-
struct edhoc_context& context,
|
|
28
|
+
EdhocKeyExporterAsyncWorker(RunningContext* runningContext,
|
|
40
29
|
uint16_t label,
|
|
41
|
-
uint8_t desiredLength
|
|
42
|
-
CallbackType callback);
|
|
30
|
+
uint8_t desiredLength);
|
|
43
31
|
|
|
44
32
|
/**
|
|
45
33
|
* @brief Executes the asynchronous worker task.
|
|
@@ -58,19 +46,11 @@ class EdhocKeyExporterAsyncWorker : public Napi::AsyncWorker {
|
|
|
58
46
|
*/
|
|
59
47
|
void OnError(const Napi::Error& error) override;
|
|
60
48
|
|
|
61
|
-
/**
|
|
62
|
-
* @brief Returns the promise object.
|
|
63
|
-
* @return The promise object.
|
|
64
|
-
*/
|
|
65
|
-
Napi::Promise GetPromise();
|
|
66
|
-
|
|
67
49
|
private:
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
uint8_t
|
|
72
|
-
std::vector<uint8_t> output;
|
|
73
|
-
CallbackType callback;
|
|
50
|
+
RunningContext* runningContext_; ///< Pointer to the running context.
|
|
51
|
+
uint16_t label_; ///< The label of the key to export.
|
|
52
|
+
uint8_t desiredLength_; ///< The desired length of the key to export.
|
|
53
|
+
std::vector<uint8_t> output_; ///< The output vector.
|
|
74
54
|
};
|
|
75
55
|
|
|
76
56
|
#endif // EDHOC_KEY_EXPORTER_ASYNC_WORKER_H
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
#define EDHOC_KEY_UPDATE_ASYNC_WORKER_H
|
|
3
3
|
|
|
4
4
|
#include <napi.h>
|
|
5
|
-
|
|
6
5
|
#include <vector>
|
|
7
6
|
|
|
7
|
+
#include "RunningContext.h"
|
|
8
|
+
|
|
8
9
|
extern "C" {
|
|
9
10
|
#include "edhoc.h"
|
|
10
11
|
}
|
|
@@ -12,12 +13,6 @@ extern "C" {
|
|
|
12
13
|
/**
|
|
13
14
|
* @class EdhocKeyUpdateAsyncWorker
|
|
14
15
|
* @brief A class that represents an asynchronous worker for key update.
|
|
15
|
-
*
|
|
16
|
-
* This class inherits from the Napi::AsyncWorker class and is used to perform
|
|
17
|
-
* key update in a separate thread. It takes an Edhoc context, a context buffer,
|
|
18
|
-
* and a callback function as input parameters. The key update is performed
|
|
19
|
-
* in the Execute() method, and the result is returned through the OnOK() method
|
|
20
|
-
* or an error is handled through the OnError() method.
|
|
21
16
|
*/
|
|
22
17
|
class EdhocKeyUpdateAsyncWorker : public Napi::AsyncWorker {
|
|
23
18
|
public:
|
|
@@ -29,15 +24,11 @@ class EdhocKeyUpdateAsyncWorker : public Napi::AsyncWorker {
|
|
|
29
24
|
/**
|
|
30
25
|
* @brief Constructs a new EdhocKeyUpdateAsyncWorker object.
|
|
31
26
|
*
|
|
32
|
-
* @param
|
|
33
|
-
* @param context The reference to the edhoc_context structure.
|
|
27
|
+
* @param runningContext The running context.
|
|
34
28
|
* @param contextBuffer The context buffer.
|
|
35
|
-
* @param callback The callback function to be called after the key update.
|
|
36
29
|
*/
|
|
37
|
-
EdhocKeyUpdateAsyncWorker(
|
|
38
|
-
|
|
39
|
-
std::vector<uint8_t> contextBuffer,
|
|
40
|
-
CallbackType callback);
|
|
30
|
+
EdhocKeyUpdateAsyncWorker(RunningContext *runningContext,
|
|
31
|
+
std::vector<uint8_t> contextBuffer);
|
|
41
32
|
|
|
42
33
|
/**
|
|
43
34
|
* @brief Executes the asynchronous worker task.
|
|
@@ -56,17 +47,9 @@ class EdhocKeyUpdateAsyncWorker : public Napi::AsyncWorker {
|
|
|
56
47
|
*/
|
|
57
48
|
void OnError(const Napi::Error& error) override;
|
|
58
49
|
|
|
59
|
-
/**
|
|
60
|
-
* @brief Returns the promise object.
|
|
61
|
-
* @return The promise object.
|
|
62
|
-
*/
|
|
63
|
-
Napi::Promise GetPromise();
|
|
64
|
-
|
|
65
50
|
private:
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
std::vector<uint8_t> contextBuffer;
|
|
69
|
-
CallbackType callback;
|
|
51
|
+
RunningContext* runningContext_; ///< Pointer to the running context.
|
|
52
|
+
std::vector<uint8_t> contextBuffer_; ///< The context buffer.
|
|
70
53
|
};
|
|
71
54
|
|
|
72
55
|
#endif // EDHOC_KEY_UPDATE_ASYNC_WORKER_H
|
|
@@ -2,47 +2,31 @@
|
|
|
2
2
|
#define EDHOC_PROCESS_ASYNC_WORKER_H
|
|
3
3
|
|
|
4
4
|
#include <napi.h>
|
|
5
|
-
|
|
6
5
|
#include <vector>
|
|
7
6
|
|
|
7
|
+
#include "RunningContext.h"
|
|
8
|
+
|
|
8
9
|
extern "C" {
|
|
9
10
|
#include "edhoc.h"
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* @class EdhocProcessAsyncWorker
|
|
14
|
-
* @brief A class that represents an asynchronous worker for processing Edhoc
|
|
15
|
-
* messages.
|
|
16
|
-
*
|
|
17
|
-
* This class inherits from the Napi::AsyncWorker class and is used to perform
|
|
18
|
-
* Edhoc message processing in a separate thread. It takes an Edhoc context,
|
|
19
|
-
* message number, message buffer, and a callback function as input parameters.
|
|
20
|
-
* The processing is performed in the Execute() method, and the result is
|
|
21
|
-
* returned through the OnOK() method or an error is handled through the
|
|
22
|
-
* OnError() method.
|
|
15
|
+
* @brief A class that represents an asynchronous worker for processing Edhoc messages.
|
|
23
16
|
*/
|
|
24
17
|
class EdhocProcessAsyncWorker : public Napi::AsyncWorker {
|
|
25
18
|
public:
|
|
26
|
-
/**
|
|
27
|
-
* @brief The type definition for the callback function.
|
|
28
|
-
*/
|
|
29
|
-
using CallbackType = std::function<Napi::Array(Napi::Env&)>;
|
|
30
19
|
|
|
31
20
|
/**
|
|
32
21
|
* @brief Constructs a new instance of the EdhocProcessAsyncWorker class.
|
|
33
22
|
*
|
|
34
|
-
* @param
|
|
35
|
-
* @param context The reference to the edhoc_context structure.
|
|
23
|
+
* @param runningContext The running context.
|
|
36
24
|
* @param messageNumber The message number.
|
|
37
|
-
* @param buffer The Napi::Buffer<uint8_t> object containing the message
|
|
38
|
-
* buffer.
|
|
39
|
-
* @param callback The callback function to be called after processing.
|
|
25
|
+
* @param buffer The Napi::Buffer<uint8_t> object containing the message buffer.
|
|
40
26
|
*/
|
|
41
|
-
EdhocProcessAsyncWorker(
|
|
42
|
-
struct edhoc_context& context,
|
|
27
|
+
EdhocProcessAsyncWorker(RunningContext* runningContext,
|
|
43
28
|
int messageNumber,
|
|
44
|
-
Napi::Buffer<uint8_t> buffer
|
|
45
|
-
CallbackType callback);
|
|
29
|
+
Napi::Buffer<uint8_t> buffer);
|
|
46
30
|
|
|
47
31
|
/**
|
|
48
32
|
* @brief Executes the asynchronous worker task.
|
|
@@ -61,20 +45,11 @@ class EdhocProcessAsyncWorker : public Napi::AsyncWorker {
|
|
|
61
45
|
*/
|
|
62
46
|
void OnError(const Napi::Error& error) override;
|
|
63
47
|
|
|
64
|
-
/**
|
|
65
|
-
* @brief Returns the promise object.
|
|
66
|
-
* @return The promise object.
|
|
67
|
-
*/
|
|
68
|
-
Napi::Promise GetPromise();
|
|
69
|
-
|
|
70
48
|
private:
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
std::vector<uint8_t> messageBuffer; ///< The message buffer.
|
|
76
|
-
CallbackType callback; ///< The callback function to be called after processing.
|
|
77
|
-
std::vector<uint8_t> peerCipherSuites; ///< The peer cipher suites.
|
|
49
|
+
RunningContext* runningContext_; ///< The pointer to the running context.
|
|
50
|
+
int messageNumber_; ///< The message number.
|
|
51
|
+
std::vector<uint8_t> messageBuffer_; ///< The message buffer.
|
|
52
|
+
std::vector<uint8_t> peerCipherSuites_; ///< The peer cipher suites.
|
|
78
53
|
};
|
|
79
54
|
|
|
80
55
|
#endif // EDHOC_PROCESS_ASYNC_WORKER_H
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
#ifndef RUNNING_CONTEXT_H
|
|
2
|
+
#define RUNNING_CONTEXT_H
|
|
3
|
+
|
|
4
|
+
#include <napi.h>
|
|
5
|
+
#include <future>
|
|
6
|
+
|
|
7
|
+
#include "EdhocCredentialManager.h"
|
|
8
|
+
#include "EdhocCryptoManager.h"
|
|
9
|
+
#include "EdhocEadManager.h"
|
|
10
|
+
|
|
11
|
+
extern "C" {
|
|
12
|
+
#include "edhoc.h"
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @brief RunningContext is a class that manages the running context of the Edhoc library.
|
|
17
|
+
* It is used to handle the asynchronous calls to the Edhoc library.
|
|
18
|
+
*/
|
|
19
|
+
class RunningContext {
|
|
20
|
+
public:
|
|
21
|
+
/**
|
|
22
|
+
* @brief ArgumentsHandler is a function that takes an Napi::Env and returns a vector of Napi::Value.
|
|
23
|
+
*/
|
|
24
|
+
using ArgumentsHandler = std::function<std::vector<napi_value>(Napi::Env)>;
|
|
25
|
+
/**
|
|
26
|
+
* @brief CompletionHandler is a function that takes an Napi::Env and a Napi::Value and returns an int.
|
|
27
|
+
*/
|
|
28
|
+
using CompletionHandler = std::function<int(Napi::Env, Napi::Value)>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @brief Constructs a new RunningContext object.
|
|
32
|
+
* @param env The Napi::Env object.
|
|
33
|
+
* @param edhoc_context The pointer to the edhoc_context object.
|
|
34
|
+
* @param cryptoManager The pointer to the cryptoManager object.
|
|
35
|
+
* @param eadManager The pointer to the eadManager object.
|
|
36
|
+
* @param credentialManager The pointer to the credentialManager object.
|
|
37
|
+
*/
|
|
38
|
+
RunningContext(Napi::Env env,
|
|
39
|
+
struct edhoc_context* edhoc_context,
|
|
40
|
+
EdhocCryptoManager* cryptoManager,
|
|
41
|
+
EdhocEadManager* eadManager,
|
|
42
|
+
EdhocCredentialManager* credentialManager,
|
|
43
|
+
const Napi::Function& logger);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @brief Get the Napi::Env object.
|
|
47
|
+
* @return The Napi::Env object.
|
|
48
|
+
*/
|
|
49
|
+
Napi::Env GetEnv() const { return deferred_.Env(); }
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @brief ThreadSafeBlockingCall is a function that takes an Napi::ObjectReference, a std::string, an ArgumentsHandler, and a CompletionHandler and returns an int.
|
|
53
|
+
* @param jsObjectRef The Napi::ObjectReference object.
|
|
54
|
+
* @param jsFunctionName The name of the function to call.
|
|
55
|
+
* @param argumentsHandler The ArgumentsHandler object.
|
|
56
|
+
* @param completionHandler The CompletionHandler object.
|
|
57
|
+
* @return The int.
|
|
58
|
+
*/
|
|
59
|
+
int ThreadSafeBlockingCall(Napi::ObjectReference& jsObjectRef,
|
|
60
|
+
const std::string& jsFunctionName,
|
|
61
|
+
ArgumentsHandler argumentsHandler,
|
|
62
|
+
CompletionHandler completionHandler);
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @brief Resolve is a function that takes an Napi::Value and resolves the promise.
|
|
66
|
+
* @param value The Napi::Value object.
|
|
67
|
+
*/
|
|
68
|
+
void Resolve(Napi::Value value);
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @brief Reject is a function that takes an Napi::Value and rejects the promise.
|
|
72
|
+
* @param value The Napi::Value object.
|
|
73
|
+
*/
|
|
74
|
+
void Reject(Napi::Value value);
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @brief GetPromise is a function that returns the promise.
|
|
78
|
+
* @return The Napi::Promise object.
|
|
79
|
+
*/
|
|
80
|
+
Napi::Promise GetPromise() const;
|
|
81
|
+
|
|
82
|
+
// Raw pointer getters
|
|
83
|
+
Napi::ThreadSafeFunction GetTsfn() const { return tsfn_; }
|
|
84
|
+
EdhocCryptoManager* GetCryptoManager() const { return cryptoManager_; }
|
|
85
|
+
EdhocEadManager* GetEadManager() const { return eadManager_; }
|
|
86
|
+
EdhocCredentialManager* GetCredentialManager() const { return credentialManager_; }
|
|
87
|
+
struct edhoc_context* GetEdhocContext() const { return edhoc_context_; }
|
|
88
|
+
Napi::FunctionReference& GetLoggerRef() { return loggerRef_; }
|
|
89
|
+
|
|
90
|
+
private:
|
|
91
|
+
struct edhoc_context* edhoc_context_; ///< Pointer to the edhoc_context object.
|
|
92
|
+
EdhocCryptoManager* cryptoManager_; ///< Pointer to the cryptoManager object.
|
|
93
|
+
EdhocEadManager* eadManager_; ///< Pointer to the eadManager object.
|
|
94
|
+
EdhocCredentialManager* credentialManager_; ///< Pointer to the credentialManager object.
|
|
95
|
+
Napi::ThreadSafeFunction tsfn_; ///< ThreadSafeFunction object.
|
|
96
|
+
Napi::Promise::Deferred deferred_; ///< Deferred object.
|
|
97
|
+
Napi::FunctionReference loggerRef_; ///< Reference to the logger function.
|
|
98
|
+
bool isResolved_; ///< Boolean to check if the promise is resolved.
|
|
99
|
+
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
#endif // RUNNING_CONTEXT_H
|