Skip to content

Error: "Zugriff verweigert" when creating new DB #6

@404-Code-404

Description

@404-Code-404

Hello,
when trying to use this plugin i got the Error: "Zugriff verweigert" (see details below) i was able to prevent the error by making a small change to the isSuccessOrMissingKeyPredicate.

The error happens when the program tries to access computer certificates.

Windows 11
KeePass: 2.61.1
Plugin: 1.3.2

running KeePass with -debug:

Smartcard Encrypted Key File Provider

Zugriff verweigert
Win32Exception (0x80004005) @ PinvokeResult`1 DoPinvokeDetailedWithException[T](System.Func`1[T], System.Func`2[EpiSource.KeePass.Ekf.Util.PinvokeUtil+PinvokeResult`1[T],System.Boolean], System.Func`2[EpiSource.KeePass.Ekf.Util.PinvokeUtil+PinvokeResult`1[T],System.Exception])
   bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   bei EpiSource.Unblocker.Tasks.TaskExtensions.<TransformResult>d__0`2.MoveNext()
--- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
   bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   bei EpiSource.Unblocker.Tasks.TaskLike`1.<WaitAsync>d__0.MoveNext()
--- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
   bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   bei EpiSource.Unblocker.Tasks.TaskLikeExtensions.<AsAwaitable>d__0`1.MoveNext()
--- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
   bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   bei EpiSource.Unblocker.UnblockerHost.<InvokeMutableAsync>d__28`2.MoveNext()
--- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
   bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   bei EpiSource.KeePass.Ekf.UI.SmartcardOperationDialogFactory.<DoCryptoImpl>d__10`2.MoveNext()
--- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
   bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   bei EpiSource.KeePass.Ekf.UI.SmartcardOperationDialogFactory.<DoCryptoAsync>d__6`1.MoveNext()
--- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
   bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   bei EpiSource.KeePass.Ekf.Util.TaskExtensions.AwaitWithMessagePump[T](Task`1 task)
   bei EpiSource.KeePass.Ekf.UI.SmartcardOperationDialogFactory.DoCryptoWithMessagePumpShort[TReturn](Expression`1 cryptoOperation, CancellationToken ct)
   bei EpiSource.KeePass.Ekf.UI.EditEncryptedKeyFileDialogFactory.AskForNewEncryptedKeyFile(IOConnectionInfo dbPath, IUserKey activeDbKey)
   bei EpiSource.KeePass.Ekf.Plugin.SmartcardEncryptedKeyProvider.CreateNewKey(KeyProviderQueryContext ctx)
   bei EpiSource.KeePass.Ekf.Plugin.SmartcardEncryptedKeyProvider.GetKey(KeyProviderQueryContext ctx)
   bei KeePass.Util.KeyUtil.CreateKey(Byte[] pbPasswordUtf8, String strKeyFile, Boolean bUserAccount, IOConnectionInfo ioc, Boolean bNewKey, Boolean bSecureDesktop)
EpiSourceKeePassEkf

After debugging, I was able to determine that the Crash happens inside:

NativeCapi.QueryCertificatePrivateKey

Specifically:

            PinvokeUtil.DoPinvokeWithException(() => NativeCertPinvoke.CryptAcquireCertificatePrivateKey(cert.Handle,
                    CryptAcquireCertificatePrivateKeyFlags.CRYPT_ACQUIRE_COMPARE_KEY_FLAG
                    | CryptAcquireCertificatePrivateKeyFlags.CRYPT_ACQUIRE_USE_PROV_INFO_FLAG
                    | CryptAcquireCertificatePrivateKeyFlags.CRYPT_ACQUIRE_NO_HEALING
                    | CryptAcquireCertificatePrivateKeyFlags.CRYPT_ACQUIRE_PREFER_NCRYPT_KEY_FLAG
                    | CryptAcquireCertificatePrivateKeyFlags.CRYPT_ACQUIRE_SILENT_FLAG,
                    ref optOwner, out keyHandleRaw, out keySpec, out mustFreeHandle),
                isSuccessOrMissingKeyPredicate);

Whenever it encounters a certificate in the computer store

Fix/Workaround:
changing isSuccessOrMissingKeyPredicate to:

        private static readonly Func<PinvokeUtil.PinvokeResult<bool>, bool> isSuccessOrMissingKeyPredicate = r =>
            r.Result || r.Win32ErrorCode == unchecked((int) CryptoResult.NTE_NO_KEY)
                     || r.Win32ErrorCode == unchecked((int) CryptoResult.CRYPT_E_NOT_FOUND)
                     || r.Win32ErrorCode == unchecked((int) CryptoResult.CRYPT_E_NO_KEY_PROPERTY)
                     || r.Win32ErrorCode == unchecked((int) CryptoResult.NTE_BAD_KEY)
                     || r.Win32ErrorCode == unchecked((int) CryptoResult.NTE_BAD_KEYSET)
                     || r.Win32ErrorCode == unchecked((int) CryptoResult.SCARD_E_NO_SMARTCARD)
                     || r.Win32ErrorCode == unchecked((int) CryptoResult.SCARD_E_NO_READERS_AVAILABLE)
                     || r.Win32ErrorCode == unchecked((int) CryptoResult.NTE_PERM);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions