Esempio n. 1
0
        static bool IsMediaMounted(Java.IO.File location) =>
        Platform.HasApiLevel(BuildVersionCodes.Lollipop)
                                ? AndroidEnvironment.GetExternalStorageState(location) == AndroidEnvironment.MediaMounted
#pragma warning disable CS0618 // Type or member is obsolete
                                : AndroidEnvironment.GetStorageState(location) == AndroidEnvironment.MediaMounted;
        ISecretKey GetKey()
        {
            // check to see if we need to get our key from past-versions or newer versions.
            // we want to use symmetric if we are >= 23 or we didn't set it previously.

            useSymmetric = Preferences.Get(useSymmetricPreferenceKey, Platform.HasApiLevel(BuildVersionCodes.M), SecureStorage.Alias);

            // If >= API 23 we can use the KeyStore's symmetric key
            if (useSymmetric && !alwaysUseAsymmetricKey)
            {
                return(GetSymmetricKey());
            }

            // NOTE: KeyStore in < API 23 can only store asymmetric keys
            // specifically, only RSA/ECB/PKCS1Padding
            // So we will wrap our symmetric AES key we just generated
            // with this and save the encrypted/wrapped key out to
            // preferences for future use.
            // ECB should be fine in this case as the AES key should be
            // contained in one block.

            // Get the asymmetric key pair
            var keyPair = GetAsymmetricKeyPair();

            var existingKeyStr = Preferences.Get(prefsMasterKey, null, alias);

            if (!string.IsNullOrEmpty(existingKeyStr))
            {
                try
                {
                    var wrappedKey = Convert.FromBase64String(existingKeyStr);

                    var unwrappedKey = UnwrapKey(wrappedKey, keyPair.Private);
                    var kp           = unwrappedKey.JavaCast <ISecretKey>();

                    return(kp);
                }
                catch (InvalidKeyException ikEx)
                {
                    System.Diagnostics.Debug.WriteLine($"Unable to unwrap key: Invalid Key. This may be caused by system backup or upgrades. All secure storage items will now be removed. {ikEx.Message}");
                }
                catch (IllegalBlockSizeException ibsEx)
                {
                    System.Diagnostics.Debug.WriteLine($"Unable to unwrap key: Illegal Block Size. This may be caused by system backup or upgrades. All secure storage items will now be removed. {ibsEx.Message}");
                }
                catch (BadPaddingException paddingEx)
                {
                    System.Diagnostics.Debug.WriteLine($"Unable to unwrap key: Bad Padding. This may be caused by system backup or upgrades. All secure storage items will now be removed. {paddingEx.Message}");
                }
                SecureStorage.RemoveAll();
            }

            var keyGenerator    = KeyGenerator.GetInstance(aesAlgorithm);
            var defSymmetricKey = keyGenerator.GenerateKey();

            var newWrappedKey = WrapKey(defSymmetricKey, keyPair.Public);

            Preferences.Set(prefsMasterKey, Convert.ToBase64String(newWrappedKey), alias);

            return(defSymmetricKey);
        }
Esempio n. 3
0
        static Task ToggleTorchAsync(bool switchOn)
        {
            return(Task.Run(() =>
            {
                lock (locker)
                {
                    if (Platform.HasApiLevel(BuildVersionCodes.M) && !AlwaysUseCameraApi)
                    {
                        var cameraManager = Platform.CameraManager;
                        foreach (var id in cameraManager.GetCameraIdList())
                        {
                            var hasFlash = cameraManager.GetCameraCharacteristics(id).Get(CameraCharacteristics.FlashInfoAvailable);
                            if (Java.Lang.Boolean.True.Equals(hasFlash))
                            {
                                try
                                {
                                    cameraManager.SetTorchMode(id, switchOn);
                                    break;
                                }
                                catch (Exception ex)
                                {
                                    System.Diagnostics.Debug.WriteLine($"Unable to SetTorchMode on {id}: {ex.Message}");
                                }
                            }
                        }
                    }
                    else
                    {
                        if (camera == null)
                        {
                            if (surface == null)
                            {
                                surface = new SurfaceTexture(0);
                            }

#pragma warning disable CS0618 // Camera types are deprecated in Android 10+
                            camera = Camera.Open();

                            // Nexus 5 and some devices require a preview texture
                            camera.SetPreviewTexture(surface);
                        }

                        var param = camera.GetParameters();

                        // Deprecated in an earlier android version
                        param.FlashMode = switchOn ? Camera.Parameters.FlashModeTorch : Camera.Parameters.FlashModeOff;

                        camera.SetParameters(param);

                        if (switchOn)
                        {
                            camera.StartPreview();
                        }
                        else
                        {
                            camera.StopPreview();
                            camera.Release();
                            camera.Dispose();
#pragma warning restore CS0618 // Type or member is obsolete
                            camera = null;
                            surface.Dispose();
                            surface = null;
                        }
                    }
                }
            }));
        }