Exemple #1
0
        public async Task Legacy_Key(string key, string data)
        {
            var ks            = new AndroidKeyStore(Platform.AppContext, SecureStorage.Alias, SecureStorage.AlwaysUseAsymmetricKeyStorage);
            var encryptedData = ks.Encrypt(data);

            var encStr = System.Convert.ToBase64String(encryptedData);

            Preferences.Set(SecureStorage.Md5Hash(key), encStr, SecureStorage.Alias);

            // Ensure we read back out the right key
            var c = await SecureStorage.GetAsync(key);

            Assert.Equal(data, c);
        }
        /// <summary>
        /// Gets value from the AndroidKeyStore
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public string GetValue(string key, string defaultValue = null)
        {
            string encStr;

            using (var prefs = AppContext.GetSharedPreferences(SecurePreferenceName, FileCreationMode.Private))
                encStr = prefs.GetString(GetMD5Hash(key), defaultValue);

            string decryptedData = null;

            if (!string.IsNullOrEmpty(encStr))
            {
                var encData = Convert.FromBase64String(encStr);
                var ks      = new AndroidKeyStore(AppContext, SecurePreferenceName);
                decryptedData = ks.Decrypt(encData);

                return(decryptedData);
            }

            return(defaultValue);
        }
        /// <summary>
        /// Sets value in the AndroidKeystore
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetValue(string key, string value)
        {
            try
            {
                var ks            = new AndroidKeyStore(AppContext, SecurePreferenceName);
                var encryptedData = ks.Encrypt(value);

                using (var prefs = AppContext.GetSharedPreferences(SecurePreferenceName, FileCreationMode.Private))
                    using (var prefsEditor = prefs.Edit())
                    {
                        var encStr = Convert.ToBase64String(encryptedData);
                        prefsEditor.PutString(GetMD5Hash(key), encStr);
                        prefsEditor.Commit();

                        return(true);
                    }
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemple #4
0
 private void OnEnable()
 {
     m_Target = (AndroidKeyStore)target;
 }
Exemple #5
0
        internal static partial IStorePal FromSystemStore(string storeName, StoreLocation storeLocation, OpenFlags openFlags)
        {
            bool isReadWrite = (openFlags & OpenFlags.ReadWrite) == OpenFlags.ReadWrite;

            if (isReadWrite && storeLocation == StoreLocation.LocalMachine)
            {
                // All LocalMachine stores are read-only from an Android application's perspective
                throw new CryptographicException(
                          SR.Cryptography_Unix_X509_MachineStoresReadOnly,
                          new PlatformNotSupportedException(SR.Cryptography_Unix_X509_MachineStoresReadOnly));
            }

            StringComparer ordinalIgnoreCase = StringComparer.OrdinalIgnoreCase;

            switch (storeLocation)
            {
            case StoreLocation.CurrentUser:
            {
                // Matches Unix behaviour of getting a disallowed store that is always empty.
                if (ordinalIgnoreCase.Equals(X509Store.DisallowedStoreName, storeName))
                {
                    return(new UnsupportedDisallowedStore(openFlags));
                }

                if (ordinalIgnoreCase.Equals(X509Store.MyStoreName, storeName))
                {
                    return(AndroidKeyStore.OpenDefault(openFlags));
                }

                if (ordinalIgnoreCase.Equals(X509Store.RootStoreName, storeName))
                {
                    // Android only allows updating the trusted store through the built-in settings application
                    if (isReadWrite)
                    {
                        throw new CryptographicException(SR.Security_AccessDenied);
                    }

                    return(new TrustedStore(storeLocation));
                }
                break;
            }

            case StoreLocation.LocalMachine:
            {
                if (ordinalIgnoreCase.Equals(X509Store.RootStoreName, storeName))
                {
                    return(new TrustedStore(storeLocation));
                }

                break;
            }
            }

            if ((openFlags & OpenFlags.OpenExistingOnly) == OpenFlags.OpenExistingOnly)
            {
                throw new CryptographicException(SR.Cryptography_X509_StoreNotFound);
            }

            string message = SR.Format(SR.Cryptography_X509_StoreCannotCreate, storeName, storeLocation);

            throw new CryptographicException(message, new PlatformNotSupportedException(message));
        }