From cb82418841c0fcbd11b7a292baedbae3178e7ed7 Mon Sep 17 00:00:00 2001 From: crypt32 Date: Fri, 19 Oct 2018 19:45:08 +0300 Subject: [PATCH] Another fix for https://github.com/Crypt32/PSPKI/issues/56 --- PKI/Structs/nCrypt2.cs | 1 + PKI/Tools/MessageOperations/MessageSigner.cs | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/PKI/Structs/nCrypt2.cs b/PKI/Structs/nCrypt2.cs index 62dfcaa..8026dfa 100644 --- a/PKI/Structs/nCrypt2.cs +++ b/PKI/Structs/nCrypt2.cs @@ -16,6 +16,7 @@ static class nCrypt2 { public const UInt32 NCRYPT_ASYMMETRIC_ENCRYPTION_OPERATION = 0x00000004; public const UInt32 NCRYPT_SECRET_AGREEMENT_OPERATION = 0x00000008; public const UInt32 NCRYPT_SIGNATURE_OPERATION = 0x00000010; + public const UInt32 NCRYPT_MACHINE_KEY_FLAG = 0x00000020; // Key Storage Property Identifiers public const String NCRYPT_NAME_PROPERTY = "Name"; diff --git a/PKI/Tools/MessageOperations/MessageSigner.cs b/PKI/Tools/MessageOperations/MessageSigner.cs index 7d5412a..0a4674b 100644 --- a/PKI/Tools/MessageOperations/MessageSigner.cs +++ b/PKI/Tools/MessageOperations/MessageSigner.cs @@ -297,7 +297,11 @@ void acquirePrivateKeyFromKeyBuilder() { openLegacyPrivateKey(); return; } - hresult = NCrypt.NCryptOpenKey(phProv, out phPrivKey, _keyInfo.KeyContainerName, (UInt32)_keyInfo.KeySpec, 0); + UInt32 dwFlags = 0; + if (_keyInfo.MachineContext) { + dwFlags = nCrypt2.NCRYPT_MACHINE_KEY_FLAG; + } + hresult = NCrypt.NCryptOpenKey(phProv, out phPrivKey, _keyInfo.KeyContainerName, (UInt32)_keyInfo.KeySpec, dwFlags); if (hresult != 0) { throw new CryptographicException(hresult); }