Example #1
0
        public bool TryGetValue(string dbPath, out ProtectedKey protectedKey)
        {
            Data data;

            if (_keys.TryGetValue(dbPath, out data))
            {
                if (data.IsValid())
                {
                    protectedKey = data.ProtectedKey;
                    return(true);
                }
                _keys.Remove(dbPath);
            }

            protectedKey = null;
            return(false);
        }
Example #2
0
        public void OnDBClosing(object sender, FileClosingEventArgs e)
        {
            if (e == null)
            {
                Debug.Fail("Event is null");
                return;
            }

            if (e.Cancel || e.Database == null || e.Database.MasterKey == null || e.Database.IOConnectionInfo == null)
            {
                return;
            }

            try
            {
                string dbPath = e.Database.IOConnectionInfo.Path;
                if (!IsDBLocking(e) && Settings.Instance.GetAuthCacheType() == AuthCacheType.Local)
                {
                    _keyStorage.Remove(dbPath);
                }
                else if (Settings.Instance.Enabled)
                {
                    _keyStorage.AddOrUpdate(dbPath, ProtectedKey.Create(e.Database.MasterKey, _keyCipher));
                }
            }
            catch (AuthProviderInvalidKeyException ex)
            {
                // It's expected not to throw exceptions
                ClaimCurrentCacheType(AuthCacheType.Local);
                ErrorHandler.ShowError(ex, "For security reasons Credential Manager storage has been turned off. Use Options dialog to turn it on.");
            }
            catch (AuthProviderUserCancelledException)
            {
                // it's OK
            }
            catch (Exception ex)
            {
                ErrorHandler.ShowError(ex);
            }
        }
Example #3
0
        public bool TryGetValue(string dbPath, out ProtectedKey protectedKey)
        {
            protectedKey = null;
            IntPtr ncredPtr;

            if (!CredRead(GetTarget(dbPath), CRED_TYPE_GENERIC, 0, out ncredPtr).Result)
            {
                Debug.Assert(Marshal.GetLastWin32Error() == ERROR_NOT_FOUND);
                return(false);
            }

            byte[] data = null;
            try
            {
                var ncred = (CREDENTIAL)Marshal.PtrToStructure(ncredPtr, typeof(CREDENTIAL));
                if (IsExpired(ncred))
                {
                    return(false);
                }

                data = new byte[ncred.CredentialBlobSize];
                Marshal.Copy(ncred.CredentialBlob, data, 0, data.Length);

                protectedKey = ProtectedKey.Deserialize(data);
            }
            catch
            {
                CredDelete(GetTarget(dbPath), CRED_TYPE_GENERIC, 0);
                throw;
            }
            finally
            {
                CredFree(ncredPtr);
                if (data != null)
                {
                    MemUtil.ZeroByteArray(data);
                }
            }
            return(true);
        }
Example #4
0
        public void OnDBClosing(object sender, FileClosingEventArgs e)
        {
            if (e == null)
            {
                Debug.Fail("Event is null");
                return;
            }

            if (e.Cancel || e.Database == null || e.Database.MasterKey == null || e.Database.IOConnectionInfo == null)
            {
                return;
            }

            string dbPath = e.Database.IOConnectionInfo.Path;

            if (!IsDBLocking(e))
            {
                _keyStorage.Remove(dbPath);
            }
            else if (AuthProviderFactory.IsAvailable() && Settings.Instance.Enabled)
            {
                _keyStorage.AddOrUpdate(dbPath, ProtectedKey.Create(e.Database.MasterKey, _keyCipher));
            }
        }
Example #5
0
 public void AddOrUpdate(string dbPath, ProtectedKey protectedKey)
 {
     _keys[dbPath] = new Data(protectedKey);
 }