mattosaurus / PgpCore

.NET Core class library for using PGP
MIT License
238 stars 100 forks source link

Could not find any signing keys in keyring #289

Open Phoenix01001010 opened 5 months ago

Phoenix01001010 commented 5 months ago

Hello, I have used the Pgpcore 6.3.1 library in my project to sign and encrypt a string. The code works fine locally, but when deployed as a web app on Azure, I am encountering an error: "Could not find any signing keys in the keyring." I have been reading the keys from the appsettings file for now and passing them as a string (I also tried using a memory stream), but none of these methods are working. Could someone please help me identify the possible root cause?

string? publicKey = Helper.GetAppSettings("KeyConfig:PublicKey"); string? privateKey = Helper.GetAppSettings("KeyConfig:PrivateKey"); EncryptionKeys signingKey = new(publicKey, privateKey, passphrase); -Error line

mattosaurus commented 5 months ago

Hi, the issue is likely because the path to your appsettings file will be different on your local machine than in Azure so you won't be loading any values in Azure.

I'd suggest using the inbuilt configuration provider rather than a custom helper class and making it available via DI.

        public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();
Phoenix01001010 commented 5 months ago

Hello @mattosaurus Thank you for your speedy response. I can confirm that it's not an issue with values not being loaded in Azure. With additional logging, I could confirm this.

Full stack trace:

Error: Encryption failed. [Could not find any signing keys in keyring at PgpCore.Utilities.FindBestSigningKey(PgpSecretKeyRingBundle secretKeyRingBundle) at PgpCore.EncryptionKeys.<>c__DisplayClass48_0.b__8() at System.Lazy1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) at System.Lazy1.CreateValue() at System.Lazy1.get_Value() at PgpCore.EncryptionKeys.get_SigningSecretKey() at PgpCore.EncryptionKeys.InitializeKeys(IEnumerable`1 publicKeyRings) at PgpCore.EncryptionKeys..ctor(String publicKey, String privateKey, String passPhrase)

ustaalon commented 3 weeks ago

@mattosaurus The same issue happens to me as well. Using the same code from the examples. When downgrading to previous version its working fine (5.2.0 - the version I used before upgrading to 6.5.1).

mattosaurus commented 2 weeks ago

Hi, @ustaalon / @Phoenix01001010. Do you have an example project that you can share with me that I can use to investigate further?

Pd-26 commented 5 days ago

Hi @mattosaurus I am also facing the same issue. Any update on this ? I am passing Publickeystream and private keystream from azure keyvault. I am using below code from your repository. My input output files are also in stream format. I would like to use Encyrption and signing using streams.

`public PGPEncryptionKeys2(Stream publicKeyStream, Stream privateKeyStream, string passPhrase) { if (publicKeyStream == null) throw new ArgumentException("PublicKeyStream"); if (privateKeyStream == null) throw new ArgumentException("PrivateKeyStream"); if (passPhrase == null) throw new ArgumentNullException(nameof(passPhrase), "Invalid Pass Phrase.");

  var keyRings = Utilities.ReadAllKeyRings(publicKeyStream);

  _secretKeys = new Lazy<PgpSecretKeyRingBundle>(() => Utilities.ReadSecretKeyRingBundle(privateKeyStream));
  _passPhrase = passPhrase;
  InitializeKeys(keyRings);

}`

mattosaurus commented 4 days ago

Hi, I've created a basic example here and all works as expected, both locally and when deployed to Azure.

https://github.com/mattosaurus/PgpCore.Api

If someon's able to share a project with me that replicates this issue then I'll take another look.

ustaalon commented 2 days ago

Hey @mattosaurus sorry for not sharing any more details. I will try to do something over this weekend or the next one. I will try to make simple project to reproduce it, as I can't share the original code.

mattosaurus commented 1 day ago

Hi @ustaalon, no worries. Does the above example help you out or can you fork that to demonstrate?