Skip to content

Commit e4d77de

Browse files
committed
Copy buffers to JS memory and free
Signed-off-by: jamshale <[email protected]>
1 parent 86dba40 commit e4d77de

File tree

1 file changed

+71
-48
lines changed

1 file changed

+71
-48
lines changed

packages/askar-nodejs/src/NodeJSAskar.ts

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import type {
1+
import {
2+
AeadParams,
23
AeadParamsOptions,
34
Askar,
5+
AskarError,
46
AskarErrorObject,
57
EncryptedBuffer,
68
EntryListCountOptions,
@@ -9,6 +11,7 @@ import type {
911
EntryListGetNameOptions,
1012
EntryListGetTagsOptions,
1113
EntryListGetValueOptions,
14+
EntryListHandle,
1215
KeyAeadDecryptOptions,
1316
KeyAeadEncryptOptions,
1417
KeyAeadGetPaddingOptions,
@@ -27,6 +30,7 @@ import type {
2730
KeyEntryListGetMetadataOptions,
2831
KeyEntryListGetNameOptions,
2932
KeyEntryListGetTagsOptions,
33+
KeyEntryListHandle,
3034
KeyEntryListLoadLocalOptions,
3135
KeyFreeOptions,
3236
KeyFromJwkOptions,
@@ -46,8 +50,10 @@ import type {
4650
KeyUnwrapKeyOptions,
4751
KeyVerifySignatureOptions,
4852
KeyWrapKeyOptions,
53+
LocalKeyHandle,
4954
MigrateIndySdkOptions,
5055
ScanFreeOptions,
56+
ScanHandle,
5157
ScanNextOptions,
5258
ScanStartOptions,
5359
SessionCloseOptions,
@@ -56,6 +62,7 @@ import type {
5662
SessionFetchAllOptions,
5763
SessionFetchKeyOptions,
5864
SessionFetchOptions,
65+
SessionHandle,
5966
SessionInsertKeyOptions,
6067
SessionRemoveAllOptions,
6168
SessionRemoveKeyOptions,
@@ -70,46 +77,34 @@ import type {
7077
StoreGenerateRawKeyOptions,
7178
StoreGetDefaultProfileOptions,
7279
StoreGetProfileNameOptions,
80+
StoreHandle,
7381
StoreListProfilesOptions,
7482
StoreOpenOptions,
7583
StoreProvisionOptions,
7684
StoreRekeyOptions,
7785
StoreRemoveOptions,
7886
StoreRemoveProfileOptions,
7987
StoreSetDefaultProfileOptions,
80-
} from '@openwallet-foundation/askar-shared'
81-
import {
82-
AeadParams,
83-
AskarError,
84-
EntryListHandle,
85-
KeyEntryListHandle,
86-
LocalKeyHandle,
87-
ScanHandle,
88-
SessionHandle,
89-
StoreHandle,
9088
handleInvalidNullResponse,
9189
} from '@openwallet-foundation/askar-shared'
92-
import type {
90+
import {
9391
ByteBufferType,
9492
EncryptedBufferType,
95-
NativeCallback,
96-
NativeCallbackWithResponse,
97-
SecretBufferType,
98-
} from './ffi'
99-
import {
10093
FFI_ENTRY_LIST_HANDLE,
101-
FFI_INT8,
10294
FFI_INT64,
95+
FFI_INT8,
10396
FFI_KEY_ENTRY_LIST_HANDLE,
10497
FFI_SCAN_HANDLE,
10598
FFI_SESSION_HANDLE,
10699
FFI_STORE_HANDLE,
107100
FFI_STRING,
108101
FFI_STRING_LIST_HANDLE,
102+
NativeCallback,
103+
NativeCallbackWithResponse,
109104
allocateAeadParams,
110105
allocateEncryptedBuffer,
111-
allocateInt8Buffer,
112106
allocateInt32Buffer,
107+
allocateInt8Buffer,
113108
allocatePointer,
114109
allocateSecretBuffer,
115110
allocateStringBuffer,
@@ -120,7 +115,7 @@ import {
120115
serializeArguments,
121116
toNativeCallback,
122117
toNativeCallbackWithResponse,
123-
toNativeLogCallback,
118+
toNativeLogCallback
124119
} from './ffi'
125120
import { getNativeAskar } from './library'
126121

@@ -317,9 +312,11 @@ export class NodeJSAskar implements Askar {
317312

318313
const errorCode = this.nativeAskar.askar_entry_list_get_value(entryListHandle, index, ret)
319314
this.handleError(errorCode)
320-
321315
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
322-
return new Uint8Array(secretBufferToBuffer(byteBuffer))
316+
const bufferArray= new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
317+
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
318+
319+
return bufferArray
323320
}
324321

325322
public keyAeadDecrypt(options: KeyAeadDecryptOptions): Uint8Array {
@@ -328,9 +325,11 @@ export class NodeJSAskar implements Askar {
328325

329326
const errorCode = this.nativeAskar.askar_key_aead_decrypt(localKeyHandle, ciphertext, nonce, tag, aad, ret)
330327
this.handleError(errorCode)
331-
332328
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
333-
return new Uint8Array(secretBufferToBuffer(byteBuffer))
329+
const bufferArray= new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
330+
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
331+
332+
return bufferArray
334333
}
335334

336335
public keyAeadEncrypt(options: KeyAeadEncryptOptions): EncryptedBuffer {
@@ -339,9 +338,11 @@ export class NodeJSAskar implements Askar {
339338

340339
const errorCode = this.nativeAskar.askar_key_aead_encrypt(localKeyHandle, message, nonce, aad, ret)
341340
this.handleError(errorCode)
342-
343341
const encryptedBuffer = handleReturnPointer<EncryptedBufferType>(ret)
344-
return encryptedBufferStructToClass(encryptedBuffer)
342+
const encryptedBufferClass = encryptedBufferStructToClass(encryptedBuffer)
343+
this.nativeAskar.askar_buffer_free(encryptedBuffer.secretBuffer as unknown as Buffer)
344+
345+
return encryptedBufferClass
345346
}
346347

347348
public keyAeadGetPadding(options: KeyAeadGetPaddingOptions): number {
@@ -370,9 +371,11 @@ export class NodeJSAskar implements Askar {
370371

371372
const errorCode = this.nativeAskar.askar_key_aead_random_nonce(localKeyHandle, ret)
372373
this.handleError(errorCode)
374+
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
375+
const bufferArray= new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
376+
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
373377

374-
const secretBuffer = handleReturnPointer<SecretBufferType>(ret)
375-
return new Uint8Array(secretBufferToBuffer(secretBuffer))
378+
return bufferArray
376379
}
377380

378381
public keyConvert(options: KeyConvertOptions): LocalKeyHandle {
@@ -392,9 +395,11 @@ export class NodeJSAskar implements Askar {
392395

393396
const errorCode = this.nativeAskar.askar_key_crypto_box(recipientKey, senderKey, message, nonce, ret)
394397
this.handleError(errorCode)
398+
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
399+
const bufferArray= new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
400+
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
395401

396-
const secretBuffer = handleReturnPointer<SecretBufferType>(ret)
397-
return new Uint8Array(secretBufferToBuffer(secretBuffer))
402+
return bufferArray
398403
}
399404

400405
public keyCryptoBoxOpen(options: KeyCryptoBoxOpenOptions): Uint8Array {
@@ -403,19 +408,23 @@ export class NodeJSAskar implements Askar {
403408

404409
const errorCode = this.nativeAskar.askar_key_crypto_box_open(recipientKey, senderKey, message, nonce, ret)
405410
this.handleError(errorCode)
411+
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
412+
const bufferArray= new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
413+
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
406414

407-
const secretBuffer = handleReturnPointer<SecretBufferType>(ret)
408-
return new Uint8Array(secretBufferToBuffer(secretBuffer))
415+
return bufferArray
409416
}
410417

411418
public keyCryptoBoxRandomNonce(): Uint8Array {
412419
const ret = allocateSecretBuffer()
413420

414421
const errorCode = this.nativeAskar.askar_key_crypto_box_random_nonce(ret)
415422
this.handleError(errorCode)
423+
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
424+
const bufferArray= new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
425+
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
416426

417-
const secretBuffer = handleReturnPointer<SecretBufferType>(ret)
418-
return new Uint8Array(secretBufferToBuffer(secretBuffer))
427+
return bufferArray
419428
}
420429

421430
public keyCryptoBoxSeal(options: KeyCryptoBoxSealOptions): Uint8Array {
@@ -424,9 +433,11 @@ export class NodeJSAskar implements Askar {
424433

425434
const errorCode = this.nativeAskar.askar_key_crypto_box_seal(localKeyHandle, message, ret)
426435
this.handleError(errorCode)
436+
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
437+
const bufferArray= new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
438+
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
427439

428-
const secretBuffer = handleReturnPointer<SecretBufferType>(ret)
429-
return new Uint8Array(secretBufferToBuffer(secretBuffer))
440+
return bufferArray
430441
}
431442

432443
public keyCryptoBoxSealOpen(options: KeyCryptoBoxSealOpenOptions): Uint8Array {
@@ -435,9 +446,11 @@ export class NodeJSAskar implements Askar {
435446

436447
const errorCode = this.nativeAskar.askar_key_crypto_box_seal_open(localKeyHandle, ciphertext, ret)
437448
this.handleError(errorCode)
449+
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
450+
const bufferArray= new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
451+
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
438452

439-
const secretBuffer = handleReturnPointer<SecretBufferType>(ret)
440-
return new Uint8Array(secretBufferToBuffer(secretBuffer))
453+
return bufferArray
441454
}
442455

443456
public keyDeriveEcdh1pu(options: KeyDeriveEcdh1puOptions): LocalKeyHandle {
@@ -659,9 +672,11 @@ export class NodeJSAskar implements Askar {
659672

660673
const errorCode = this.nativeAskar.askar_key_get_jwk_secret(localKeyHandle, ret)
661674
this.handleError(errorCode)
675+
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
676+
const bufferArray= new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
677+
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
662678

663-
const secretBuffer = handleReturnPointer<SecretBufferType>(ret)
664-
return new Uint8Array(secretBufferToBuffer(secretBuffer))
679+
return bufferArray
665680
}
666681

667682
public keyGetJwkThumbprint(options: KeyGetJwkThumbprintOptions): string {
@@ -680,9 +695,11 @@ export class NodeJSAskar implements Askar {
680695

681696
const errorCode = this.nativeAskar.askar_key_get_public_bytes(localKeyHandle, ret)
682697
this.handleError(errorCode)
698+
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
699+
const bufferArray= new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
700+
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
683701

684-
const secretBuffer = handleReturnPointer<SecretBufferType>(ret)
685-
return new Uint8Array(secretBufferToBuffer(secretBuffer))
702+
return bufferArray
686703
}
687704

688705
public keyGetSecretBytes(options: KeyGetSecretBytesOptions): Uint8Array {
@@ -691,9 +708,11 @@ export class NodeJSAskar implements Askar {
691708

692709
const errorCode = this.nativeAskar.askar_key_get_secret_bytes(localKeyHandle, ret)
693710
this.handleError(errorCode)
711+
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
712+
const bufferArray= new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
713+
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
694714

695-
const secretBuffer = handleReturnPointer<SecretBufferType>(ret)
696-
return new Uint8Array(secretBufferToBuffer(secretBuffer))
715+
return bufferArray
697716
}
698717

699718
public keySignMessage(options: KeySignMessageOptions): Uint8Array {
@@ -702,9 +721,11 @@ export class NodeJSAskar implements Askar {
702721

703722
const errorCode = this.nativeAskar.askar_key_sign_message(localKeyHandle, message, sigType, ret)
704723
this.handleError(errorCode)
724+
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
725+
const bufferArray= new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
726+
this.nativeAskar.askar_buffer_free(byteBuffer as unknown as Buffer)
705727

706-
const secretBuffer = handleReturnPointer<SecretBufferType>(ret)
707-
return new Uint8Array(secretBufferToBuffer(secretBuffer))
728+
return bufferArray
708729
}
709730

710731
public keyUnwrapKey(options: KeyUnwrapKeyOptions): LocalKeyHandle {
@@ -734,9 +755,11 @@ export class NodeJSAskar implements Askar {
734755

735756
const errorCode = this.nativeAskar.askar_key_wrap_key(localKeyHandle, other, nonce, ret)
736757
this.handleError(errorCode)
737-
738758
const encryptedBuffer = handleReturnPointer<EncryptedBufferType>(ret)
739-
return encryptedBufferStructToClass(encryptedBuffer)
759+
const encryptedBufferClass = encryptedBufferStructToClass(encryptedBuffer)
760+
this.nativeAskar.askar_buffer_free(encryptedBuffer.secretBuffer as unknown as Buffer)
761+
762+
return encryptedBufferClass
740763
}
741764

742765
public keyGetSupportedBackends(): string[] {

0 commit comments

Comments
 (0)