Exemple #1
0
        /// <summary>
        /// Handles deserialization of file from disk; virtual method allows customization (e.g., pre-load decryption and/or data merge).
        /// </summary>
        /// <param name="fileStream"><see cref="FileStream"/> used to deserialize data.</param>
        /// <returns>Deserialized file data.</returns>
        /// <remarks>
        /// Consumers overriding this method should not directly call <see cref="InterprocessCache.FileData"/> property to avoid potential dead-locks.
        /// </remarks>
        protected override byte[] LoadFileData(FileStream fileStream)
        {
            // Decrypt data that was encrypted local to this machine
            byte[] serializedUserDataTable = ProtectedData.Unprotect(fileStream.ReadStream(), null, DataProtectionScope.LocalMachine);
            Dictionary<string, UserData> userDataTable = DeserializeCache(serializedUserDataTable);

            // Wait for thread level lock on user data table
            lock (m_userDataTableLock)
            {
                // Merge new and existing key tables since new user information may have been queued for serialization, but not saved yet
                m_userDataTable = userDataTable.Merge(m_userDataTable);
            }

            return serializedUserDataTable;
        }
Exemple #2
0
            /// <summary>
            /// Handles deserialization of file from disk; virtual method allows customization (e.g., pre-load decryption and/or data merge).
            /// </summary>
            /// <param name="fileStream"><see cref="FileStream"/> used to deserialize data.</param>
            /// <returns>Deserialized file data.</returns>
            /// <remarks>
            /// Consumers overriding this method should not directly call <see cref="InterprocessCache.FileData"/> property to avoid potential dead-locks.
            /// </remarks>
            protected override byte[] LoadFileData(FileStream fileStream)
            {
                // Decrypt data that was encrypted local to this machine
                byte[] serializedKeyIVTable = ProtectedData.Unprotect(fileStream.ReadStream(), null, DataProtectionScope.LocalMachine);
                Dictionary<string, byte[][]> keyIVTable = Serialization.Deserialize<Dictionary<string, byte[][]>>(serializedKeyIVTable, SerializationFormat.Binary);

                // Wait for thread level lock on key table
                lock (m_keyIVTableLock)
                {
                    // Merge new and existing key tables since new keys may have been queued for serialization, but not saved yet
                    m_keyIVTable = keyIVTable.Merge(m_keyIVTable);
                }

                return serializedKeyIVTable;
            }
Exemple #3
0
        /// <summary>
        /// Handles deserialization of file from disk; virtual method allows customization (e.g., pre-load decryption and/or data merge).
        /// </summary>
        /// <param name="fileStream"><see cref="FileStream"/> used to deserialize data.</param>
        /// <returns>Deserialized file data.</returns>
        /// <remarks>
        /// Consumers overriding this method should not directly call <see cref="InterprocessCache.FileData"/> property to avoid potential dead-locks.
        /// </remarks>
        protected override byte[] LoadFileData(FileStream fileStream)
        {
            // Decrypt data that was encrypted local to this machine
            byte[] serializedUserRoles = ProtectedData.Unprotect(fileStream.ReadStream(), null, DataProtectionScope.LocalMachine);
            Dictionary<string, string[]> userRoles = Serialization.Deserialize<Dictionary<string, string[]>>(serializedUserRoles, SerializationFormat.Binary);

            // Wait for thread level lock on user role dictionary
            lock (m_userRolesLock)
            {
                // Merge new and existing dictionaries since new user roles may have been queued for serialization, but not saved yet
                m_userRoles = userRoles.Merge(m_userRoles);
            }

            return serializedUserRoles;
        }
Exemple #4
0
        /// <summary>
        /// Handles deserialization of file from disk; virtual method allows customization (e.g., pre-load decryption and/or data merge).
        /// </summary>
        /// <param name="fileStream"><see cref="FileStream"/> used to deserialize data.</param>
        /// <returns>Deserialized file data.</returns>
        /// <remarks>
        /// Consumers overriding this method should not directly call <see cref="InterprocessCache.FileData"/> property to avoid potential dead-locks.
        /// </remarks>
        protected override byte[] LoadFileData(FileStream fileStream)
        {
            // Decrypt data that was encrypted local to this machine
            byte[] serializedDataSet = ProtectedData.Unprotect(fileStream.ReadStream(), null, DataProtectionScope.LocalMachine);
            DataSet dataSet;

            using (MemoryStream stream = new MemoryStream(serializedDataSet))
            {
                dataSet = stream.DeserializeToDataSet();
            }

            // Wait for thread level lock on data set
            lock (m_dataSetLock)
            {
                m_dataSet = dataSet;
            }

            return serializedDataSet;
        }
Exemple #5
0
 /// <summary>
 /// Handles deserialization of file from disk; virtual method allows customization (e.g., pre-load decryption and/or data merge).
 /// </summary>
 /// <param name="fileStream"><see cref="FileStream"/> used to deserialize data.</param>
 /// <returns>Deserialized file data.</returns>
 /// <remarks>
 /// Consumers overriding this method should not directly call <see cref="FileData"/> property to avoid potential dead-locks.
 /// </remarks>
 protected virtual byte[] LoadFileData(FileStream fileStream)
 {
     return fileStream.ReadStream();
 }
Exemple #6
0
        /// <summary>
        /// Deserializes key and initialization vector table from local cache that was encrypted on local machine.
        /// </summary>
        private static Dictionary<string, byte[][]> DeserializeKeyIVTable()
        {
            Dictionary<string, byte[][]> keyIVTable;

            string keyIVCacheFilePath = FilePath.GetAbsolutePath(KeyIVCacheFileName);

            if (File.Exists(keyIVCacheFilePath))
            {
                // We need interprocess synchronization on the key & IV cache file so we create a system level mutex
                Mutex fileLockMutex = GetSystemLevelMutex(KeyIVCacheMutexName);
                
                try
                {
                    // Wait for system level mutex lock before we work with key & IV cache file
                    fileLockMutex.WaitOne();
                }
                catch (AbandonedMutexException)
                {
                    // Abnormal system terminations can leave mutexs abandoned, in this
                    // case we now own the mutex and can safely ignore this exception
                }

                try
                {
                    FileStream keyIVCacheFile = new FileStream(keyIVCacheFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                    byte[] serializedKeyIVTable = ProtectedData.Unprotect(keyIVCacheFile.ReadStream(), null, DataProtectionScope.LocalMachine);
                    keyIVTable = Serialization.GetObject<Dictionary<string, byte[][]>>(serializedKeyIVTable);
                }
                finally
                {
                    fileLockMutex.ReleaseMutex();
                }
            }
            else
            {
                // No crypto key cache file exists, create a blank crypto key table
                keyIVTable = new Dictionary<string, byte[][]>();
            }

            return keyIVTable;
        }