Browse Source

Fix #45 Restore GPG sharedPreference store mode

isundil 7 years ago
parent
commit
a832ae2615

+ 13 - 9
app/src/main/java/info/knacki/pass/io/FileUtils.java

@@ -1,8 +1,8 @@
 package info.knacki.pass.io;
 
-import java.io.ByteArrayOutputStream;
+import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileInputStream;
+import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -22,14 +22,18 @@ public class FileUtils {
         }
     }
 
-    public static byte[] ReadAllStream(InputStream str) throws IOException {
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        pipe(str, out);
-        return out.toByteArray();
-    }
-
     public static String ReadAllFile(File file) throws IOException {
-        return CharsetHelper.ByteArrayToString(ReadAllStream(new FileInputStream(file)));
+        BufferedReader buf = new BufferedReader(new FileReader(file));
+        StringBuilder result = new StringBuilder();
+        String tmp;
+
+        while ((tmp = buf.readLine()) != null) {
+            if (result.length() > 0)
+                result.append("\n");
+            result.append(tmp);
+        }
+        buf.close();
+        return result.toString();
     }
 
     public static void pipe(InputStream in, OutputStream out) throws IOException {

+ 0 - 3
app/src/main/java/info/knacki/pass/io/PathUtils.java

@@ -7,7 +7,6 @@ public class PathUtils {
     @SuppressWarnings("SpellCheckingInspection")
     private static final String DATA_GIT_LOCAL = "gitfiles";
     private static final String DATA_FINGER_LOCAL = "fingerprint";
-    private static final String DATA_GPG_FILE = "gpgKey";
 
     private static String GetAppRootDir(Context ctx) {
         return ctx.getFilesDir().getAbsolutePath();
@@ -25,8 +24,6 @@ public class PathUtils {
         return GetAppRootDir(ctx) +"/" +DATA_FINGER_LOCAL;
     }
 
-    public static String GetGPGKeyFile(Context ctx) { return GetAppRootDir(ctx) +"/" +DATA_GPG_FILE; }
-
     public static final String TRASH_SUFFIX = ".trash";
 
     public static boolean IsHidden(String path) {

+ 3 - 3
app/src/main/java/info/knacki/pass/io/pgp/GPGFileInterface.java

@@ -32,7 +32,7 @@ public class GPGFileInterface implements IFileInterface {
             return;
         }
         try {
-            if (!GPGStorage.HasGPGKey(fContext))
+            if (!GPGStorageEngine.GetDefaultEngine(fContext).HasGPGKey())
                 throw new FileNotFoundException("GPG key not set");
             GPGUtil.DecryptFile(fContext, fPasswordGetter, fFile, new OnResponseListener<byte[]>() {
                 @Override
@@ -53,7 +53,7 @@ public class GPGFileInterface implements IFileInterface {
     @Override
     public void WriteFile(String content, OnResponseListener<Void> resp) {
         try {
-            if (!GPGStorage.HasGPGKey(fContext))
+            if (!GPGStorageEngine.GetDefaultEngine(fContext).HasGPGKey())
                 throw new FileNotFoundException("GPG key not set");
             GPGUtil.CryptFile(fContext, new FileOutputStream(fFile), CharsetHelper.StringToByteArray(content), resp);
         }
@@ -66,7 +66,7 @@ public class GPGFileInterface implements IFileInterface {
     public String GetMethodName() {
         Resources r = fContext.getResources();
         String details;
-        if (GPGStorage.HasGPGKey(fContext)) {
+        if (GPGStorageEngine.GetDefaultEngine(fContext).HasGPGKey()) {
             try {
                 details = r.getString(GPGUtil.CheckIsPasswordProtected(fContext) ? R.string.protected_key : R.string.unprotected_key_short);
             } catch (IOException e) {

+ 0 - 161
app/src/main/java/info/knacki/pass/io/pgp/GPGStorage.java

@@ -1,161 +0,0 @@
-package info.knacki.pass.io.pgp;
-
-import android.content.Context;
-import android.support.annotation.NonNull;
-
-import org.bouncycastle.openpgp.PGPException;
-import org.bouncycastle.openpgp.PGPSecretKey;
-import org.bouncycastle.openpgp.PGPSecretKeyRing;
-import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
-import org.bouncycastle.openpgp.PGPUtil;
-import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
-
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.security.KeyStore;
-import java.security.KeyStoreException;
-import java.security.NoSuchAlgorithmException;
-import java.security.NoSuchProviderException;
-import java.security.UnrecoverableKeyException;
-import java.security.cert.CertificateException;
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import javax.crypto.SecretKey;
-import javax.crypto.spec.SecretKeySpec;
-
-import info.knacki.pass.io.FileUtils;
-import info.knacki.pass.io.PathUtils;
-
-public class GPGStorage {
-    private static final Logger log = Logger.getLogger(GPGStorage.class.getName());
-    private GPGStorage(){}
-
-    private final static String KEY_NAME = GPGStorage.class.getName()+"_PGP_PRIVATE";
-    private final static String KEY_PASS = GPGStorage.class.getName()+"___PASSWORD";
-
-    static SecretKey FindSecretKey(@NonNull byte[] in) throws IOException {
-        final PGPSecretKeyRingCollection pgpSec;
-
-        try {
-            pgpSec = new PGPSecretKeyRingCollection(in, new JcaKeyFingerprintCalculator());
-        } catch (PGPException e) {
-            throw new GPGUtil.MalformedKeyException(e);
-        }
-        Iterator<PGPSecretKeyRing> rIt = pgpSec.getKeyRings();
-        if (rIt.hasNext()) {
-            PGPSecretKey bcSecKey = rIt.next().getSecretKey();
-            return new SecretKeySpec(bcSecKey.getEncoded(), "AES");
-        }
-        throw new NoSuchElementException("GPG key not found");
-    }
-
-    private static KeyStore GetKeystore(Context ctx, boolean read) {
-        KeyStore ks;
-
-        try {
-            ks = KeyStore.getInstance("UBER", "BC");
-            if (read) {
-                File gpgKeyFile = new File(PathUtils.GetGPGKeyFile(ctx));
-                if (!gpgKeyFile.exists())
-                    throw new FileNotFoundException(gpgKeyFile.getName());
-                ks.load(new FileInputStream(gpgKeyFile), KEY_PASS.toCharArray());
-            } else {
-                ks.load(null, null);
-            }
-        } catch (KeyStoreException | NoSuchProviderException | IOException | CertificateException | NoSuchAlgorithmException e) {
-            log.log(Level.SEVERE, "Cannot retreive KeyStore", e);
-            ks = null;
-        }
-        return ks;
-    }
-
-    private static void SaveKeystore(Context ctx, KeyStore ks) {
-        try {
-            File f = new File(PathUtils.GetGPGKeyFile(ctx));
-            if (!f.exists() && !f.createNewFile())
-                throw new FileNotFoundException(f.getName());
-            ks.store(new FileOutputStream(f), KEY_PASS.toCharArray());
-        }
-        catch (IOException | CertificateException | NoSuchAlgorithmException | KeyStoreException e) {
-            log.log(Level.SEVERE, "Cannot save keystore", e);
-        }
-    }
-
-    public static boolean HasGPGKey(Context ctx) {
-        try {
-            KeyStore ks = GetKeystore(ctx, true);
-            return ks != null && ks.containsAlias(KEY_NAME);
-        } catch (KeyStoreException e) {
-            log.log(Level.SEVERE, "KeyStore Exception: " +e.getMessage(), e);
-            return false;
-        }
-    }
-
-    public static boolean SetGPGKeyContent(Context ctx, InputStream content) {
-        KeyStore ks = GetKeystore(ctx, false);
-        if (ks == null)
-            return false;
-        try {
-            SecretKey secret = FindSecretKey(FileUtils.ReadAllStream(content));
-            KeyStore.Entry e = new KeyStore.SecretKeyEntry(secret);
-            ks.setEntry(KEY_NAME, e, new KeyStore.PasswordProtection(null));
-            SaveKeystore(ctx, ks);
-        } catch (KeyStoreException | IOException e) {
-            log.log(Level.SEVERE, "Cannot set GPG key content", e);
-            return false;
-        }
-        return true;
-    }
-
-    private static @NonNull InputStream GetGPGKeyContent(Context ctx) throws FileNotFoundException {
-        if (!HasGPGKey(ctx))
-            throw new FileNotFoundException("GPG key");
-        KeyStore ks = GetKeystore(ctx, true);
-        if (ks == null)
-            throw new FileNotFoundException("GPG key store");
-        try {
-            return new ByteArrayInputStream(ks.getKey(KEY_NAME, null).getEncoded());
-        } catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException e) {
-            log.log(Level.SEVERE, "KeyStore Exception: " +e.getMessage(), e);
-            throw new FileNotFoundException("GPG key store");
-        }
-    }
-
-    public static void ExportGPGKeyContent(Context ctx, OutputStream out) throws IOException {
-        FileUtils.pipe(GetGPGKeyContent(ctx), out);
-    }
-
-    public static @NonNull PGPSecretKeyRing GetGPGKeyRing(Context ctx) throws IOException {
-        final PGPSecretKeyRingCollection pgpSec;
-
-        try {
-            pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(GetGPGKeyContent(ctx)), new JcaKeyFingerprintCalculator());
-        } catch (PGPException e) {
-            throw new GPGUtil.MalformedKeyException(e);
-        }
-        Iterator<PGPSecretKeyRing> rIt = pgpSec.getKeyRings();
-        if (rIt.hasNext())
-            return rIt.next();
-        throw new NoSuchElementException("GPG key not found");
-    }
-
-    public static @NonNull PGPSecretKey GetGPGSecretKey(Context ctx) throws IOException {
-        return GetGPGKeyRing(ctx).getSecretKey();
-    }
-
-    public static void RemoveGpgKey(Context ctx) {
-        File f = new File(PathUtils.GetGPGKeyFile(ctx));
-        if (f.exists() && !f.delete()) {
-            log.severe("Cannot remove GPG file");
-        }
-    }
-}

+ 34 - 0
app/src/main/java/info/knacki/pass/io/pgp/GPGStorageEngine.java

@@ -0,0 +1,34 @@
+package info.knacki.pass.io.pgp;
+
+import android.content.Context;
+import android.support.annotation.NonNull;
+
+import org.bouncycastle.openpgp.PGPSecretKey;
+import org.bouncycastle.openpgp.PGPSecretKeyRing;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+public class GPGStorageEngine {
+    private static IGPGStorage fDefaultEngine = null;
+
+    public static IGPGStorage GetDefaultEngine(Context ctx) {
+        if (fDefaultEngine == null) {
+            synchronized (GPGStorageEngine.class) {
+                return fDefaultEngine = new SharedPreferenceGPGStorage(ctx);
+            }
+        }
+        return fDefaultEngine;
+    }
+
+    public interface IGPGStorage {
+        boolean HasGPGKey();
+        boolean SetGPGKeyContent(InputStream content);
+        void ExportGPGKeyContent(OutputStream out) throws IOException;
+        @NonNull PGPSecretKeyRing GetGPGKeyRing() throws IOException;
+        @NonNull PGPSecretKey GetGPGSecretKey() throws IOException;
+        @NonNull PGPSecretKey GetGPGSecretKey(long keyId) throws IOException;
+        void RemoveGpgKey();
+    }
+}

+ 8 - 8
app/src/main/java/info/knacki/pass/io/pgp/GPGUtil.java

@@ -49,6 +49,7 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -63,12 +64,11 @@ import java.util.logging.Logger;
 
 import info.knacki.pass.io.FileInterfaceFactory;
 import info.knacki.pass.io.OnResponseListener;
+import info.knacki.pass.settings.SettingsManager;
 
 public class GPGUtil {
     private final static Logger log = Logger.getLogger(GPGUtil.class.getName());
 
-    private GPGUtil(){}
-
     public static class MalformedKeyException extends IOException {
         final Throwable fCause;
 
@@ -221,7 +221,7 @@ public class GPGUtil {
 
             FindPassword(
                     passwordGetter,
-                    GPGStorage.GetGPGSecretKey(ctx),
+                    GPGStorageEngine.GetDefaultEngine(ctx).GetGPGSecretKey(pbe.getKeyID()),
                     new OnResponseListener<PGPPrivateKeyAndPass>() {
                         @Override
                         public void OnResponse(PGPPrivateKeyAndPass sKey) {
@@ -260,7 +260,7 @@ public class GPGUtil {
 
             final byte[] compressedData = compressedDataStream.toByteArray();
             final OutputStream out = new ArmoredOutputStream(fileOutStream);
-            final PGPSecretKey sKey = GPGStorage.GetGPGSecretKey(ctx);
+            final PGPSecretKey sKey = GPGStorageEngine.GetDefaultEngine(ctx).GetGPGSecretKey();
 
             PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(PGPEncryptedDataGenerator.AES_128).setWithIntegrityPacket(true).setSecureRandom(new SecureRandom()).setProvider("BC"));
             encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(sKey.getPublicKey()).setProvider("BC"));
@@ -281,7 +281,7 @@ public class GPGUtil {
     }
 
     public static void ChangePassword(final Context ctx, final FileInterfaceFactory.ChangePasswordGetter passwordGetter, final OnResponseListener<Void> onDone) throws IOException {
-        final PGPSecretKeyRing secretKeyring = GPGStorage.GetGPGKeyRing(ctx);
+        final PGPSecretKeyRing secretKeyring = GPGStorageEngine.GetDefaultEngine(ctx).GetGPGKeyRing();
         final PGPSecretKey signingKey = secretKeyring.getSecretKey();
 
         FindPassword(
@@ -299,7 +299,7 @@ public class GPGUtil {
                                         keyringCollection.add(DoChangePassword(secretKeyring, sKey.fPass, result));
                                         ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                         new PGPSecretKeyRingCollection(keyringCollection).encode(stream);
-                                        GPGStorage.SetGPGKeyContent(ctx, new ByteArrayInputStream(stream.toByteArray()));
+                                        GPGStorageEngine.GetDefaultEngine(ctx).SetGPGKeyContent(new ByteArrayInputStream(stream.toByteArray()));
                                         onDone.OnResponse(null);
                                         return true;
                                     } catch (PGPException | IOException e) {
@@ -324,7 +324,7 @@ public class GPGUtil {
 
     public static String GetGPGKeyName(Context ctx) {
         try {
-            PGPSecretKey key = GPGStorage.GetGPGSecretKey(ctx);
+            PGPSecretKey key = GPGStorageEngine.GetDefaultEngine(ctx).GetGPGSecretKey();
             Iterator<String> users = key.getUserIDs();
             if (users.hasNext())
                 return users.next();
@@ -370,6 +370,6 @@ public class GPGUtil {
     }
 
     public static boolean CheckIsPasswordProtected(Context ctx) throws IOException {
-        return TryPassword(GPGStorage.GetGPGSecretKey(ctx), "") == null;
+        return TryPassword(GPGStorageEngine.GetDefaultEngine(ctx).GetGPGSecretKey(), "") == null;
     }
 }

+ 110 - 0
app/src/main/java/info/knacki/pass/io/pgp/SharedPreferenceGPGStorage.java

@@ -0,0 +1,110 @@
+package info.knacki.pass.io.pgp;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.support.annotation.NonNull;
+import android.util.Base64;
+
+import org.bouncycastle.openpgp.PGPException;
+import org.bouncycastle.openpgp.PGPSecretKey;
+import org.bouncycastle.openpgp.PGPSecretKeyRing;
+import org.bouncycastle.openpgp.PGPUtil;
+import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import info.knacki.pass.io.FileUtils;
+import info.knacki.pass.settings.SettingsManager;
+
+public class SharedPreferenceGPGStorage implements GPGStorageEngine.IGPGStorage {
+    private final static Logger log = Logger.getLogger(SharedPreferenceGPGStorage.class.getName());
+    private static final String GPG_KEY_FILE = "GPGKeyFile";
+    private final Context fContext;
+
+    SharedPreferenceGPGStorage(Context context) {
+        fContext = context;
+    }
+
+    private SharedPreferences GetPrefManager() {
+        return fContext.getSharedPreferences(SettingsManager.SHARED_PREF_FILE, Context.MODE_PRIVATE);
+    }
+
+    @Override
+    public boolean HasGPGKey() {
+        return GetPrefManager().contains(GPG_KEY_FILE);
+    }
+
+    @Override
+    public boolean SetGPGKeyContent(InputStream content) {
+        if (content == null) {
+            GetPrefManager().edit().remove(GPG_KEY_FILE).apply();
+            return false;
+        }
+        try {
+            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+            final int bufLen = 1024;
+            byte[] buffer = new byte[bufLen];
+            int len;
+            do {
+                len = content.read(buffer);
+                bytes.write(buffer, 0, len);
+            } while (len == bufLen);
+            GetPrefManager().edit().putString(GPG_KEY_FILE, Base64.encodeToString(bytes.toByteArray(), 0)).apply();
+        } catch (IOException e) {
+            log.log(Level.SEVERE, "Cannot read file", e);
+            RemoveGpgKey();
+            return false;
+        }
+        return true;    }
+
+    @Override
+    public void ExportGPGKeyContent(OutputStream out) throws IOException {
+        InputStream keyContent = GetGPGKeyContent();
+        if (keyContent != null)
+            FileUtils.pipe(keyContent, out);
+    }
+
+    @NonNull @Override public PGPSecretKeyRing GetGPGKeyRing() throws IOException {
+        try {
+            InputStream keyContent = GetGPGKeyContent();
+            if (keyContent == null) {
+                log.log(Level.WARNING, "Cannot read key stream input");
+                throw new FileNotFoundException();
+            }
+            return new PGPSecretKeyRing(PGPUtil.getDecoderStream(keyContent), new JcaKeyFingerprintCalculator());
+        } catch (PGPException e) {
+            log.log(Level.WARNING, "Cannot create Keyring: " +e.getMessage(), e);
+            throw new FileNotFoundException();
+        }
+    }
+
+    @NonNull @Override public PGPSecretKey GetGPGSecretKey() throws IOException {
+        return GetGPGKeyRing().getSecretKey();
+    }
+
+    @NonNull @Override public PGPSecretKey GetGPGSecretKey(long keyId) throws IOException {
+        PGPSecretKey key = GetGPGKeyRing().getSecretKey(keyId);
+        if (key == null)
+            throw new FileNotFoundException(String.format("key %X", keyId));
+        return key;
+    }
+
+    @Override
+    public void RemoveGpgKey() {
+        GetPrefManager().edit().remove(GPG_KEY_FILE).apply();
+    }
+
+    private InputStream GetGPGKeyContent() {
+        String content = GetPrefManager().getString(GPG_KEY_FILE, null);
+        if (content != null)
+            return new ByteArrayInputStream(Base64.decode(content, 0));
+        return null;
+    }
+}

+ 5 - 1
app/src/main/java/info/knacki/pass/settings/SettingsManager.java

@@ -2,10 +2,14 @@ package info.knacki.pass.settings;
 
 import android.content.Context;
 import android.content.SharedPreferences;
+import android.util.Base64;
 import android.util.JsonReader;
 import android.util.MalformedJsonException;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.StringReader;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -190,7 +194,7 @@ public class SettingsManager {
         }
     }
 
-    private final static String SHARED_PREF_FILE = "pass_prefs";
+    public final static String SHARED_PREF_FILE = "pass_prefs";
     @SuppressWarnings("SpellCheckingInspection") private static final String ENCRYPTION_PASSWORD = "encPasswd";
     private static final String FINGERPRINT_ACTIVE = "FingerprintEnabled";
 

+ 9 - 7
app/src/main/java/info/knacki/pass/settings/ui/SettingsActivity.java

@@ -31,6 +31,7 @@ import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -43,9 +44,10 @@ import info.knacki.pass.git.GitInterfaceFactory;
 import info.knacki.pass.git.entities.GitRef;
 import info.knacki.pass.io.FileInterfaceFactory;
 import info.knacki.pass.io.FileMigratoryUtils;
-import info.knacki.pass.io.OnResponseListener;
-import info.knacki.pass.io.pgp.GPGStorage;
+import info.knacki.pass.io.FileUtils;
+import info.knacki.pass.io.pgp.GPGStorageEngine;
 import info.knacki.pass.io.pgp.GPGUtil;
+import info.knacki.pass.io.OnResponseListener;
 import info.knacki.pass.settings.SettingsManager;
 import info.knacki.pass.ui.GitPullActivity;
 import info.knacki.pass.ui.alertPrompt.AlertPromptGenerator;
@@ -494,7 +496,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
                         .setTitle(R.string.pref_gpg_title_username_mail)
                         .setPositiveButton(R.string.ok, (dialogInterface, v) -> {
                             byte[] keyData = GPGUtil.Generate(((UsernameAndEmail) v).GetUserAndEmail());
-                            GPGStorage.SetGPGKeyContent(getActivity(), new ByteArrayInputStream(keyData));
+                            GPGStorageEngine.GetDefaultEngine(getActivity()).SetGPGKeyContent(new ByteArrayInputStream(keyData));
                             updateGpgFileLabel();
                             GPGSetPassword();
                         })
@@ -551,7 +553,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
             try {
                 outFile.createNewFile();
                 FileOutputStream fileOut = new FileOutputStream(outFile);
-                GPGStorage.ExportGPGKeyContent(getActivity(), fileOut);
+                GPGStorageEngine.GetDefaultEngine(getActivity()).ExportGPGKeyContent(fileOut);
                 fileOut.close();
             } catch (IOException e) {
                 Toast.makeText(getActivity(), "Cannot prepare key for sharing: " + e.getMessage(), Toast.LENGTH_LONG).show();
@@ -578,20 +580,20 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
                     final Uri intentData = data.getData();
                     boolean gpgImportStatus;
                     if (intentData != null)
-                        gpgImportStatus = GPGStorage.SetGPGKeyContent(getActivity(), getActivity().getContentResolver().openInputStream(intentData));
+                        gpgImportStatus = GPGStorageEngine.GetDefaultEngine(getActivity()).SetGPGKeyContent(getActivity().getContentResolver().openInputStream(intentData));
                     else
                         gpgImportStatus = false;
                     Toast.makeText(getActivity(), getResources().getString(gpgImportStatus ? R.string.gpg_import_ok : R.string.gpg_import_ko), Toast.LENGTH_LONG).show();
                 } catch (FileNotFoundException e) {
                     Toast.makeText(getActivity(), getResources().getString(R.string.file_not_found), Toast.LENGTH_LONG).show();
-                    GPGStorage.RemoveGpgKey(getActivity());
+                    GPGStorageEngine.GetDefaultEngine(getActivity()).RemoveGpgKey();
                 }
                 updateGpgFileLabel();
             }
         }
 
         protected void updateGpgFileLabel() {
-            if (GPGStorage.HasGPGKey(getActivity())) {
+            if (GPGStorageEngine.GetDefaultEngine(getActivity()).HasGPGKey()) {
                 findPreference(getResources().getString(R.string.id_gpg_key_file)).setSummary(GPGUtil.GetGPGKeyName(getActivity()));
                 findPreference(getResources().getString(R.string.id_gpg_password)).setEnabled(true);
                 findPreference(getResources().getString(R.string.id_gpg_export)).setEnabled(true);