aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-03-03 15:06:26 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2024-03-03 15:06:26 -0500
commita8603e00e390b07c036384285412eda5e07beda7 (patch)
treee0cdaffc3eff61502731829c7906d6e5962b22a1
parente4b5b937a05a9869249619a3b17a7269648d93bd (diff)
fix: fix library handle leak in hot-reload
-rw-r--r--back-end/plugins/nvault/src/ManagedCryptoprovider.cs11
-rw-r--r--lib/NVault.Crypto.Noscrypt/src/NostrCrypto.cs20
2 files changed, 20 insertions, 11 deletions
diff --git a/back-end/plugins/nvault/src/ManagedCryptoprovider.cs b/back-end/plugins/nvault/src/ManagedCryptoprovider.cs
index fe585cc..e239248 100644
--- a/back-end/plugins/nvault/src/ManagedCryptoprovider.cs
+++ b/back-end/plugins/nvault/src/ManagedCryptoprovider.cs
@@ -28,7 +28,7 @@ using NVault.Crypto.Noscrypt;
namespace NVault.Plugins.Vault
{
[ConfigurationName("crypto")]
- internal class ManagedCryptoprovider : INostrCryptoProvider
+ internal class ManagedCryptoprovider : INostrCryptoProvider, IDisposable
{
private readonly INostrCryptoProvider _provider;
@@ -99,5 +99,14 @@ namespace NVault.Plugins.Vault
{
_provider.GetRandomBytes(bytes);
}
+
+ public void Dispose()
+ {
+ //Dont leak the library
+ if(_provider is IDisposable noscrypt)
+ {
+ noscrypt.Dispose();
+ }
+ }
}
}
diff --git a/lib/NVault.Crypto.Noscrypt/src/NostrCrypto.cs b/lib/NVault.Crypto.Noscrypt/src/NostrCrypto.cs
index e4d4574..1bfccb0 100644
--- a/lib/NVault.Crypto.Noscrypt/src/NostrCrypto.cs
+++ b/lib/NVault.Crypto.Noscrypt/src/NostrCrypto.cs
@@ -15,11 +15,11 @@
using System;
using System.Runtime.CompilerServices;
-using System.Xml;
using VNLib.Utils;
using NCResult = System.Int64;
+using static NVault.Crypto.Noscrypt.LibNoscrypt;
namespace NVault.Crypto.Noscrypt
{
@@ -56,10 +56,10 @@ namespace NVault.Crypto.Noscrypt
//Copy nonce to struct memory buffer
Unsafe.CopyBlock(
- ref Unsafe.AsRef<byte>(data.nonce),
- in nonce,
- LibNoscrypt.NC_ENCRYPTION_NONCE_SIZE
- );
+ ref Unsafe.AsRef<byte>(data.nonce),
+ in nonce,
+ NC_ENCRYPTION_NONCE_SIZE
+ );
fixed (NCSecretKey* pSecKey = &secretKey)
fixed (NCPublicKey* pPubKey = &publicKey)
@@ -96,7 +96,7 @@ namespace NVault.Crypto.Noscrypt
Unsafe.CopyBlock(
ref Unsafe.AsRef<byte>(data.nonce),
in nonce,
- LibNoscrypt.NC_ENCRYPTION_NONCE_SIZE
+ NC_ENCRYPTION_NONCE_SIZE
);
fixed (NCSecretKey* pSecKey = &secretKey)
@@ -187,7 +187,7 @@ namespace NVault.Crypto.Noscrypt
NCResult result = Functions.NCVerifyData.Invoke(libCtx, pPubKey, pData, dataSize, pSig);
NCUtil.CheckResult<FunctionTable.NCVerifyDataDelegate>(result, false);
- return result == LibNoscrypt.NC_SUCCESS;
+ return result == NC_SUCCESS;
}
}
@@ -230,14 +230,14 @@ namespace NVault.Crypto.Noscrypt
Unsafe.CopyBlock(
ref Unsafe.AsRef<byte>(args.nonce),
in nonce32,
- LibNoscrypt.NC_ENCRYPTION_NONCE_SIZE
+ NC_ENCRYPTION_NONCE_SIZE
);
//Copy mac to struct memory buffer
Unsafe.CopyBlock(
ref Unsafe.AsRef<byte>(args.mac),
in mac32,
- LibNoscrypt.NC_ENCRYPTION_MAC_SIZE
+ NC_ENCRYPTION_MAC_SIZE
);
fixed(NCSecretKey* pSecKey = &secretKey)
@@ -251,7 +251,7 @@ namespace NVault.Crypto.Noscrypt
NCUtil.CheckResult<FunctionTable.NCVerifyMacDelegate>(result, false);
//Result should be success if the hmac is valid
- return result == LibNoscrypt.NC_SUCCESS;
+ return result == NC_SUCCESS;
}
}