Exemple #1
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));
        }