Browse Source

Merge branch 'issue-21' of isundil/pass into master

isundil 7 years ago
parent
commit
64bfdb81b4

+ 8 - 0
app/src/main/java/info/knacki/pass/io/FileUtils.java

@@ -1,6 +1,8 @@
 package info.knacki.pass.io;
 
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
@@ -22,6 +24,12 @@ public class FileUtils {
         }
     }
 
+    public static byte[] ReadAllStream(InputStream s) throws IOException {
+        ByteArrayOutputStream str = new ByteArrayOutputStream();
+        pipe(s, str);
+        return str.toByteArray();
+    }
+
     public static String ReadAllFile(File file) throws IOException {
         BufferedReader buf = new BufferedReader(new FileReader(file));
         StringBuilder result = new StringBuilder();

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

@@ -7,6 +7,7 @@ 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 = "key";
 
     private static String GetAppRootDir(Context ctx) {
         return ctx.getFilesDir().getAbsolutePath();
@@ -24,6 +25,10 @@ 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) {

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

@@ -16,7 +16,7 @@ public class GPGStorageEngine {
     public static IGPGStorage GetDefaultEngine(Context ctx) {
         if (fDefaultEngine == null) {
             synchronized (GPGStorageEngine.class) {
-                return fDefaultEngine = new SharedPreferenceGPGStorage(ctx);
+                return fDefaultEngine = new UberGPGStorage(ctx);
             }
         }
         return fDefaultEngine;

+ 184 - 0
app/src/main/java/info/knacki/pass/io/pgp/UberGPGStorage.java

@@ -0,0 +1,184 @@
+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.UnrecoverableEntryException;
+import java.security.cert.CertificateException;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.crypto.spec.SecretKeySpec;
+
+import info.knacki.pass.io.FileUtils;
+import info.knacki.pass.io.PathUtils;
+
+public class UberGPGStorage implements GPGStorageEngine.IGPGStorage {
+    private static final Logger log = Logger.getLogger(UberGPGStorage.class.getName());
+    private final Context fContext;
+
+    private final static String KEY_NAME = "sbcKey";
+    private final static String KEY_PASS = UberGPGStorage.class.getName()+"___PASSWORD";
+
+    UberGPGStorage(Context ctx) {
+        fContext = ctx;
+    }
+
+    private PGPSecretKeyRingCollection FindKeyring(@NonNull byte[] in) throws IOException {
+        try {
+            return new PGPSecretKeyRingCollection(in, new JcaKeyFingerprintCalculator());
+        } catch (PGPException e) {
+            throw new GPGUtil.MalformedKeyException(e);
+        }
+    }
+
+    private KeyStore.SecretKeyEntry FindSecretKey(@NonNull byte[] in) throws IOException {
+        Iterator<PGPSecretKeyRing> kr = FindKeyring(in).iterator();
+        if (kr.hasNext())
+            return new KeyStore.SecretKeyEntry(new SecretKeySpec(kr.next().getEncoded(), "AES"));
+        return null;
+    }
+
+    private KeyStore GetKeystore(boolean read) {
+        KeyStore ks;
+
+        try {
+            ks = KeyStore.getInstance("UBER", "BC");
+            if (read) {
+                File gpgKeyFile = new File(PathUtils.GetGPGKeyFile(fContext));
+                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 void SaveKeystore(KeyStore ks) {
+        try {
+            File f = new File(PathUtils.GetGPGKeyFile(fContext));
+            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);
+        }
+    }
+
+    @Override
+    public boolean HasGPGKey() {
+        try {
+            KeyStore ks = GetKeystore(true);
+            return ks != null && ks.containsAlias(KEY_NAME);
+        } catch (KeyStoreException e) {
+            log.log(Level.SEVERE, "KeyStore Exception: " +e.getMessage(), e);
+            return false;
+        }
+    }
+
+    @Override
+    public boolean SetGPGKeyContent(InputStream content) {
+        KeyStore ks = GetKeystore(false);
+        if (ks == null)
+            return false;
+        try {
+            KeyStore.SecretKeyEntry keyring = FindSecretKey(FileUtils.ReadAllStream(content));
+            ks.setEntry(KEY_NAME, keyring, new KeyStore.PasswordProtection(null));
+            SaveKeystore(ks);
+        } catch (KeyStoreException | IOException e) {
+            log.log(Level.SEVERE, "Cannot set GPG key content", e);
+            return false;
+        }
+        return true;
+    }
+
+    private @NonNull KeyStore.SecretKeyEntry GetGPGKeyContent() throws FileNotFoundException {
+        if (!HasGPGKey())
+            throw new FileNotFoundException("GPG key");
+        KeyStore ks = GetKeystore(true);
+        if (ks == null)
+            throw new FileNotFoundException("GPG key store");
+        try {
+            return (KeyStore.SecretKeyEntry) ks.getEntry(KEY_NAME, null);
+        } catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableEntryException e) {
+            log.log(Level.SEVERE, "KeyStore Exception: " +e.getMessage(), e);
+            throw new FileNotFoundException("GPG key store");
+        }
+    }
+
+    @Override
+    public void ExportGPGKeyContent(OutputStream out) throws IOException {
+        try {
+            new PGPSecretKeyRingCollection(Collections.singleton(GetGPGKeyRing())).encode(out);
+        } catch (PGPException e) {
+            e.printStackTrace();
+        }
+    }
+
+    @Override
+    public @NonNull PGPSecretKeyRing GetGPGKeyRing() throws IOException {
+        KeyStore.SecretKeyEntry secretKey = GetGPGKeyContent();
+
+        try {
+            Iterator<PGPSecretKeyRing> keyrings = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(new ByteArrayInputStream(secretKey.getSecretKey().getEncoded())), new JcaKeyFingerprintCalculator()).iterator();
+            if (keyrings.hasNext())
+                return keyrings.next();
+        } catch (PGPException e) {
+            log.log(Level.WARNING, "Cannot load key: " + e.getMessage(), e);
+            throw new GPGUtil.MalformedKeyException(e);
+        }
+        throw new NoSuchElementException("GPG key not found");
+    }
+
+    @Override
+    public @NonNull PGPSecretKey GetGPGSecretKey() throws IOException {
+        PGPSecretKey key = GetGPGKeyRing().getSecretKey();
+        if (key == null)
+            throw new FileNotFoundException("Key");
+        return key;
+    }
+
+    @Override
+    public @NonNull 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() {
+        File f = new File(PathUtils.GetGPGKeyFile(fContext));
+        if (f.exists() && !f.delete()) {
+            log.severe("Cannot remove GPG file");
+        }
+    }
+}