コード例 #1
0
        /// <summary>
        /// Stores the key data to the given alias.
        /// </summary>
        /// <param name="alias">The alias.</param>
        /// <param name="data">The key data.</param>
        /// <exception cref="DuplicateKeyException"></exception>
        public void Save(string alias, byte[] data)
        {
            this.ValidateAlias(alias);
            this.ValidateData(data);

            if (this.Exists(alias))
            {
                throw new DuplicateKeyException(alias);
            }

            var keyEntry = new KeyStore.SecretKeyEntry(new KeyEntry(data));

            keyStorage.SetEntry(alias, keyEntry, new KeyStore.PasswordProtection(this.password));

            try
            {
                using (var stream = new IsolatedStorageFileStream(StorageIdentity, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    keyStorage.Store(stream, this.password);
                }
            }
            catch (Exception)
            {
                throw new SecureStorageException($"The key under alias '{alias}' can't be saved.");
            }
        }
コード例 #2
0
        private async Task <byte[]> GetEncryptionKeyLockedAsync(string keyName)
        {
            byte[] key = null;

            try
            {
                char[]   deviceId = GetDeviceId().ToCharArray();
                KeyStore keyStore = await GetOrCreateKeyStoreAsync(deviceId).ConfigureAwait(false);

                KeyStore.IProtectionParameter protectionParameter = new KeyStore.PasswordProtection(deviceId);
                KeyStore.SecretKeyEntry       secretKeyEntry      = (KeyStore.SecretKeyEntry)keyStore.GetEntry(keyName, protectionParameter);

                if (secretKeyEntry != null)
                {
                    ISecretKey secretKey = secretKeyEntry.SecretKey;
                    if (secretKey != null)
                    {
                        key = secretKey.GetEncoded();
                    }
                }
            }
            catch (FileNotFoundException)
            {
                // If the file isn't found, it's not a big deal and should mean it's just the first run.
                // The caller or the GetOrCreate method above will need to create it if we don't find it here.
            }

            return(key);
        }
コード例 #3
0
 public void Save(string pin, string serviceId)
 {
     var alias = MakeAlias(serviceId);
     var secretKey = new SecretAccount(pin);
     var entry = new KeyStore.SecretKeyEntry(secretKey);
     ks.SetEntry(alias, entry, prot);
     Save();
 }
コード例 #4
0
        public void Save <T>(string key, T data)
        {
            var secretKey = new SecretData <T>(data);

            KeyStore.SecretKeyEntry entry = new KeyStore.SecretKeyEntry(secretKey);
            ks.SetEntry(key.Trim(), entry, prot);

            Save();
        }
コード例 #5
0
ファイル: Utility.cs プロジェクト: denniskorir/Sport
		static internal void SetSecured(string key, string value, string clientId, string service, string sharedGrou = null)
		{
			var ks = LoadKeyStore(clientId, service);

			var entry = new KeyStore.SecretKeyEntry(new SecretAccount(value));
			var protect = new KeyStore.PasswordProtection(service.ToCharArray());
			ks.SetEntry("{0} - {1}".Fmt(clientId, key), entry, protect);
			Save(clientId, service, ks);
		}
コード例 #6
0
		public void Add(string key, string value)
		{
			PBEKeySpec keyspec = new PBEKeySpec(value.ToCharArray());
			SecretKeyFactory fk = SecretKeyFactory.GetInstance("PBEWithMD5andDES");
			ISecretKey mysec = fk.GenerateSecret(keyspec);
			KeyStore.SecretKeyEntry entry = new KeyStore.SecretKeyEntry(mysec);

			keyStore.SetEntry(key, entry, Password);
			Save();
		}
コード例 #7
0
        static internal void SetSecured(string key, string value, string clientId, string service, string sharedGrou = null)
        {
            var ks = LoadKeyStore(clientId, service);

            var entry   = new KeyStore.SecretKeyEntry(new SecretAccount(value));
            var protect = new KeyStore.PasswordProtection(service.ToCharArray());

            ks.SetEntry("{0} - {1}".Fmt(clientId, key), entry, protect);
            Save(clientId, service, ks);
        }
コード例 #8
0
        void Save(LoginDetails account, string serviceId)
        {
            var alias = MakeAlias(account, serviceId);

            var secretKey = new SecretAccount(account);
            var entry     = new KeyStore.SecretKeyEntry(secretKey);

            _keyStore.SetEntry(alias, entry, _passwordProtection);

            Save();
        }
コード例 #9
0
        public override void Save(Account account, string serviceId)
        {
            var alias = MakeAlias(account, serviceId);

            var secretKey = new SecretAccount(account);
            var entry     = new KeyStore.SecretKeyEntry(secretKey);

            ks.SetEntry(alias, entry, prot);

            Save();
        }
コード例 #10
0
        public void Add(
            string secret,
            string serviceId)
        {
            string        alias     = MakeAlias(serviceId);
            SecretAccount secretKey = new SecretAccount(secret);

            KeyStore.SecretKeyEntry entry = new KeyStore.SecretKeyEntry(secretKey);
            _keyStore.SetEntry(alias, entry, _passProtection);
            Save();
        }
コード例 #11
0
        public void Save(string stringToSave, string identifier)
        {
            var alias = MakeAlias(identifier);

            var secretKey = new SecretString(stringToSave);
            var entry     = new KeyStore.SecretKeyEntry(secretKey);

            _keyStore.SetEntry(alias, entry, _passwordProtection);

            Save();
        }
コード例 #12
0
        /// <summary>
        /// Save a Key (or a Password) to the KeyChain
        /// </summary>
        /// <returns><c>true</c>, if key was saved, <c>false</c> otherwise.</returns>
        /// <param name="keyName">Key name or username.</param>
        /// <param name="keyValue">Key value or password.</param>
        public bool SetKey(string keyName, string keyValue)
        {
            var alias     = MakeAlias(keyName, _serviceId);
            var secretKey = new SecretAccount(keyValue);
            var entry     = new KeyStore.SecretKeyEntry(secretKey);

            _androidKeyStore.SetEntry(alias, entry, _passwordProtection);

            Save();
            return(true);
        }
コード例 #13
0
        public void Add(string key, string value)
        {
            PBEKeySpec       keyspec = new PBEKeySpec(value.ToCharArray());
            SecretKeyFactory fk      = SecretKeyFactory.GetInstance("PBEWithMD5andDES");
            ISecretKey       mysec   = fk.GenerateSecret(keyspec);

            KeyStore.SecretKeyEntry entry = new KeyStore.SecretKeyEntry(mysec);

            keyStore.SetEntry(key, entry, Password);
            Save();
        }
コード例 #14
0
        public void Save(Dictionary <string, string> values, string serviceId)
        {
            var alias = MakeAlias(serviceId);

            var secretKey = new SecretAccount(values);
            var entry     = new KeyStore.SecretKeyEntry(secretKey);

            ks.SetEntry(alias, entry, prot);

            Save();
        }
        public override Task SaveAsync(Account account, string serviceId)
        {
            string alias = MakeAlias(account, serviceId);

            SecretAccount secretKey = new SecretAccount(account);

            Java.Security.KeyStore.SecretKeyEntry entry = new KeyStore.SecretKeyEntry(secretKey);
            ks.SetEntry(alias, entry, prot);

            Save();

            return(Task.FromResult(true));
        }
コード例 #16
0
        /// <summary>
        /// Sets value in the password protected file
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetValue(string key, string value)
        {
            // create entry
            var secKeyEntry = new KeyStore.SecretKeyEntry(new StringKeyEntry(value));

            // save it in the KeyStore
            _store.SetEntry(key, secKeyEntry, _passwordProtection);

            // save the store
            Save();

            return(true);
        }
コード例 #17
0
        /// <inheritdoc />
        public Task Save(string identifier, string stringToSave)
        {
            var alias = MakeAlias(identifier);

            var secretKey = new SecretString(stringToSave);
            var entry     = new KeyStore.SecretKeyEntry(secretKey);

            _keyStore.SetEntry(alias, entry, _passwordProtection);

            Save();

            return(Task.CompletedTask);
        }
コード例 #18
0
ファイル: AuthService.cs プロジェクト: pingzing/Philosopher
        public void Save(Account account, string serviceId)
        {
            if (_keyStore == null)
            {
                InitializeStore();
            }

            string alias = $"{account.Username}-{serviceId}";

            var secretKey = new SecretAccount(account);
            var entry = new KeyStore.SecretKeyEntry(secretKey);
            _keyStore.SetEntry(alias, entry, _prot);
            SaveCurrent();
        }
コード例 #19
0
        /// <summary>
        /// Gets value from the password protected file
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public string GetValue(string key, string defaultValue = null)
        {
            // get the entry from the store
            // if it does not exist, return the default value
            KeyStore.SecretKeyEntry entry = GetSecretKeyEntry(key);

            if (entry != null)
            {
                var encodedBytes = entry.SecretKey.GetEncoded();
                return(Encoding.UTF8.GetString(encodedBytes));
            }

            return(defaultValue);
        }
コード例 #20
0
        public override void Save(ISalesforceUser account, string serviceId)
        {
            var alias = MakeAlias(account, serviceId);

            var secretKey = new SecretAccount(account);
            var entry     = new KeyStore.SecretKeyEntry(secretKey);

            ks.SetEntry(alias, entry, prot);

            lock (fileLock) {
                using (var s = context.OpenFileOutput(FileName, FileCreationMode.Private)) {
                    ks.Store(s, Password);
                }
            }
        }
コード例 #21
0
        /// <summary>
        /// Retrieves the value from storage.
        /// If value with the given key does not exist,
        /// returns default value
        /// </summary>
        /// <returns>The value.</returns>
        /// <param name="key">Key.</param>
        /// <param name="defaultValue">Default value.</param>
        public override string GetValue(string key, string defaultValue)
        {
            // validate using base class
            base.GetValue(key, defaultValue);

            // get the entry from the store
            // if it does not exist, return the default value
            KeyStore.SecretKeyEntry entry = GetSecretKeyEntry(key);

            if (entry != null)
            {
                var encodedBytes = entry.SecretKey.GetEncoded();
                return(Encoding.ASCII.GetString(encodedBytes));
            }

            return(defaultValue);
        }
コード例 #22
0
        public void SetKey(string keyName, string keyValue)
        {
            var existing = GetKey(keyName);

            if (!string.IsNullOrEmpty(existing))
            {
                DeleteKey(keyName);
            }

            var alias     = MakeAlias(keyName, _serviceId);
            var secretKey = new SecretAccount(keyValue);
            var entry     = new KeyStore.SecretKeyEntry(secretKey);

            _androidKeyStore.SetEntry(alias, entry, new KeyStore.PasswordProtection(_serviceId.ToCharArray()));

            Save();
        }
コード例 #23
0
        /// <summary>
        /// Deletes a value from the password protected file
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool DeleteKey(string key)
        {
            // retrieve the entry
            KeyStore.SecretKeyEntry entry = GetSecretKeyEntry(key);

            // if entry exists, delete from store, save the store and return true
            if (entry != null)
            {
                _store.DeleteEntry(key);

                Save();

                return(true);
            }

            return(false);
        }
コード例 #24
0
        /// <summary>
        /// Deletes the key and corresponding value from the storage
        /// </summary>
        public override bool DeleteKey(string key)
        {
            // valdiate using base class
            base.DeleteKey(key);

            // retrieve the entry
            KeyStore.SecretKeyEntry entry = GetSecretKeyEntry(key);

            // if entry exists, delete from store, save the store and return true
            if (entry != null)
            {
                _keyStore.DeleteEntry(key);
                Save();
                return(true);
            }

            return(false);
        }
コード例 #25
0
        private async Task StoreKeyAsync(string keyName, ISecretKey key)
        {
            char[]   deviceId = GetDeviceId().ToCharArray();
            KeyStore keyStore = await GetOrCreateKeyStoreAsync(deviceId).ConfigureAwait(false);

            KeyStore.IProtectionParameter protectionParameter = new KeyStore.PasswordProtection(deviceId);

            // Create the SecretKeyEntry wrapper around the SecretKey
            KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(key);

            // Store the SecretKeyEntry in the KeyStore
            keyStore.SetEntry(keyName, secretKeyEntry, protectionParameter);

            // Write the KeyStore to the app's directory with appropriate permissions
            // Note: we're using the same key to protect the KeyStore as we are to protect the entry inside it.
            using (Stream fileStream = await OpenKeyStoreFileForOutputAsync().ConfigureAwait(false))
            {
                keyStore.Store(fileStream, deviceId);
            }
        }
コード例 #26
0
        public void Store(string key, string value)
        {
            if (!IsUnlocked)
            {
                throw new Exception("Please unlock the key store prior to using it.");
            }

            if (String.IsNullOrEmpty(value))
            {
                _keyStore.DeleteEntry(key);
            }
            else
            {
                var secretKey = new SecretValue(value);
                Java.Security.KeyStore.SecretKeyEntry entry = new KeyStore.SecretKeyEntry(secretKey);
                _keyStore.SetEntry(key, entry, _passwordProtection);

                Save();
            }
        }
コード例 #27
0
        public override Task <bool> SaveAsync()
        {
            var secureKey = GetKeyFromPreferences(context);
            var keyStore  = LoadKeyStore(context, secureKey);
            var password  = new KeyStore.PasswordProtection(secureKey);

            var secretValue = new SecretValue(System.Text.Encoding.UTF8.GetBytes(DictionaryToJson(data)));

            var secretKeyEntry = new KeyStore.SecretKeyEntry(secretValue);

            keyStore.SetEntry(FILENAME, secretKeyEntry, password);

            lock (fileLock)
            {
                using (var stream = context.OpenFileOutput(FILENAME, FileCreationMode.Private))
                {
                    keyStore.Store(stream, secureKey);
                    stream.Flush();
                    stream.Close();
                }
            }
            return(Task.FromResult(true));
        }
コード例 #28
0
        bool saveItemToKeychain(string service, string account, string privateKey)
        {
            var context = Android.App.Application.Context;

            var password = service.ToCharArray();

            var serviceId = $"{context.PackageName}.nomadcode.botframework-{service}";

            var keystore = getKeystore(service);

            var item = new KeychainItem(privateKey);

            var secretEntry = new KeyStore.SecretKeyEntry(item);

            keystore.SetEntry(account, secretEntry, new KeyStore.PasswordProtection(password));

            using (var stream = context.OpenFileOutput(serviceId, FileCreationMode.Private))
            {
                keystore.Store(stream, password);
            }

            return(true);
        }
コード例 #29
0
        public IEnumerable <string> FindAccountsForService(string serviceId)
        {
            List <string> r       = new List <string>();
            string        postfix = "-" + serviceId;
            IEnumeration  aliases = _keyStore.Aliases();

            while (aliases.HasMoreElements)
            {
                string alias = aliases.NextElement().ToString();
                if (alias.EndsWith(postfix))
                {
                    KeyStore.SecretKeyEntry e = _keyStore.GetEntry(
                        alias,
                        _passProtection) as KeyStore.SecretKeyEntry;
                    if (e != null)
                    {
                        byte[] bytes    = e.SecretKey.GetEncoded();
                        string password = System.Text.Encoding.UTF8.GetString(bytes);
                        r.Add(password);
                    }
                }
            }
            return(r);
        }
コード例 #30
0
		public override void Save (ISalesforceUser account, string serviceId)
		{
			var alias = MakeAlias (account, serviceId);

			var secretKey = new SecretAccount (account);
			var entry = new KeyStore.SecretKeyEntry (secretKey);
			ks.SetEntry (alias, entry, prot);

			this.Save();
		}
コード例 #31
0
		public override void Save (Account account, string serviceId)
		{
			var alias = MakeAlias (account, serviceId);

			var secretKey = new SecretAccount (account);
			var entry = new KeyStore.SecretKeyEntry (secretKey);
			ks.SetEntry (alias, entry, prot);

			lock (fileLock) {
				using (var s = context.OpenFileOutput (FileName, FileCreationMode.Private)) {
					ks.Store (s, Password);
				}
			}
		}
コード例 #32
0
        /// <summary>
        /// Sets the value for the given key. If value exists, overwrites it
        /// Else creates new entry.
        /// Does not accept null value.
        /// </summary>
        /// <returns>true</returns>
        /// <c>false</c>
        /// <param name="key">Key.</param>
        /// <param name="value">Value.</param>
        public override bool SetValue(string key, string value)
        {
            // validate the parameters
            base.SetValue(key, value);

            // create entry
            var secKeyEntry = new KeyStore.SecretKeyEntry(new StringKeyEntry(value));

            // save it in the KeyStore
            _store.SetEntry(key, secKeyEntry, _passwordProtection);

            // save the store
            Save();

            return true;
        }