|
@@ -1,4 +1,4 @@
|
|
|
-package info.knacki.pass.io;
|
|
|
|
|
|
|
+package info.knacki.pass.io.pgp;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
import android.content.Context;
|
|
|
import android.support.annotation.NonNull;
|
|
import android.support.annotation.NonNull;
|
|
@@ -49,7 +49,6 @@ import java.io.ByteArrayInputStream;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
import java.io.FileInputStream;
|
|
|
-import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStream;
|
|
@@ -62,11 +61,14 @@ import java.util.NoSuchElementException;
|
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Level;
|
|
|
import java.util.logging.Logger;
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
-import info.knacki.pass.settings.SettingsManager;
|
|
|
|
|
|
|
+import info.knacki.pass.io.FileInterfaceFactory;
|
|
|
|
|
+import info.knacki.pass.io.OnResponseListener;
|
|
|
|
|
|
|
|
public class GPGUtil {
|
|
public class GPGUtil {
|
|
|
private final static Logger log = Logger.getLogger(GPGUtil.class.getName());
|
|
private final static Logger log = Logger.getLogger(GPGUtil.class.getName());
|
|
|
|
|
|
|
|
|
|
+ private GPGUtil(){}
|
|
|
|
|
+
|
|
|
public static class MalformedKeyException extends IOException {
|
|
public static class MalformedKeyException extends IOException {
|
|
|
final Throwable fCause;
|
|
final Throwable fCause;
|
|
|
|
|
|
|
@@ -184,34 +186,6 @@ public class GPGUtil {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static @NonNull
|
|
|
|
|
- InputStream getKeyInputStream(Context ctx) throws FileNotFoundException {
|
|
|
|
|
- InputStream ss = SettingsManager.GetGPGKeyContent(ctx);
|
|
|
|
|
- if (ss == null)
|
|
|
|
|
- throw new FileNotFoundException("GPG key");
|
|
|
|
|
- return ss;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static @NonNull
|
|
|
|
|
- PGPSecretKeyRing findSecretKeyring(@NonNull InputStream in) throws IOException {
|
|
|
|
|
- final PGPSecretKeyRingCollection pgpSec;
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(in), new JcaKeyFingerprintCalculator());
|
|
|
|
|
- } catch (PGPException e) {
|
|
|
|
|
- throw new MalformedKeyException(e);
|
|
|
|
|
- }
|
|
|
|
|
- Iterator<PGPSecretKeyRing> rIt = pgpSec.getKeyRings();
|
|
|
|
|
- if (rIt.hasNext())
|
|
|
|
|
- return rIt.next();
|
|
|
|
|
- throw new NoSuchElementException("GPG key not found");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static @NonNull
|
|
|
|
|
- PGPSecretKey findSecretKey(@NonNull InputStream in) throws IOException {
|
|
|
|
|
- return findSecretKeyring(in).getSecretKey();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
private static void DoDecryptFile(PGPPrivateKeyAndPass sKey, InputStream dataStream, OnResponseListener<byte[]> resp) {
|
|
private static void DoDecryptFile(PGPPrivateKeyAndPass sKey, InputStream dataStream, OnResponseListener<byte[]> resp) {
|
|
|
byte[] output;
|
|
byte[] output;
|
|
|
try {
|
|
try {
|
|
@@ -247,7 +221,7 @@ public class GPGUtil {
|
|
|
|
|
|
|
|
FindPassword(
|
|
FindPassword(
|
|
|
passwordGetter,
|
|
passwordGetter,
|
|
|
- findSecretKey(getKeyInputStream(ctx)),
|
|
|
|
|
|
|
+ GPGStorage.GetGPGSecretKey(ctx),
|
|
|
new OnResponseListener<PGPPrivateKeyAndPass>() {
|
|
new OnResponseListener<PGPPrivateKeyAndPass>() {
|
|
|
@Override
|
|
@Override
|
|
|
public void OnResponse(PGPPrivateKeyAndPass sKey) {
|
|
public void OnResponse(PGPPrivateKeyAndPass sKey) {
|
|
@@ -286,7 +260,7 @@ public class GPGUtil {
|
|
|
|
|
|
|
|
final byte[] compressedData = compressedDataStream.toByteArray();
|
|
final byte[] compressedData = compressedDataStream.toByteArray();
|
|
|
final OutputStream out = new ArmoredOutputStream(fileOutStream);
|
|
final OutputStream out = new ArmoredOutputStream(fileOutStream);
|
|
|
- final PGPSecretKey sKey = findSecretKey(getKeyInputStream(ctx));
|
|
|
|
|
|
|
+ final PGPSecretKey sKey = GPGStorage.GetGPGSecretKey(ctx);
|
|
|
|
|
|
|
|
PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(PGPEncryptedDataGenerator.AES_128).setWithIntegrityPacket(true).setSecureRandom(new SecureRandom()).setProvider("BC"));
|
|
PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(PGPEncryptedDataGenerator.AES_128).setWithIntegrityPacket(true).setSecureRandom(new SecureRandom()).setProvider("BC"));
|
|
|
encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(sKey.getPublicKey()).setProvider("BC"));
|
|
encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(sKey.getPublicKey()).setProvider("BC"));
|
|
@@ -307,7 +281,7 @@ public class GPGUtil {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static void ChangePassword(final Context ctx, final FileInterfaceFactory.ChangePasswordGetter passwordGetter, final OnResponseListener<Void> onDone) throws IOException {
|
|
public static void ChangePassword(final Context ctx, final FileInterfaceFactory.ChangePasswordGetter passwordGetter, final OnResponseListener<Void> onDone) throws IOException {
|
|
|
- final PGPSecretKeyRing secretKeyring = findSecretKeyring(getKeyInputStream(ctx));
|
|
|
|
|
|
|
+ final PGPSecretKeyRing secretKeyring = GPGStorage.GetGPGKeyRing(ctx);
|
|
|
final PGPSecretKey signingKey = secretKeyring.getSecretKey();
|
|
final PGPSecretKey signingKey = secretKeyring.getSecretKey();
|
|
|
|
|
|
|
|
FindPassword(
|
|
FindPassword(
|
|
@@ -325,7 +299,7 @@ public class GPGUtil {
|
|
|
keyringCollection.add(DoChangePassword(secretKeyring, sKey.fPass, result));
|
|
keyringCollection.add(DoChangePassword(secretKeyring, sKey.fPass, result));
|
|
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
|
new PGPSecretKeyRingCollection(keyringCollection).encode(stream);
|
|
new PGPSecretKeyRingCollection(keyringCollection).encode(stream);
|
|
|
- SettingsManager.SetGPGKeyContent(ctx, new ByteArrayInputStream(stream.toByteArray()));
|
|
|
|
|
|
|
+ GPGStorage.SetGPGKeyContent(ctx, new ByteArrayInputStream(stream.toByteArray()));
|
|
|
onDone.OnResponse(null);
|
|
onDone.OnResponse(null);
|
|
|
return true;
|
|
return true;
|
|
|
} catch (PGPException | IOException e) {
|
|
} catch (PGPException | IOException e) {
|
|
@@ -350,7 +324,7 @@ public class GPGUtil {
|
|
|
|
|
|
|
|
public static String GetGPGKeyName(Context ctx) {
|
|
public static String GetGPGKeyName(Context ctx) {
|
|
|
try {
|
|
try {
|
|
|
- PGPSecretKey key = findSecretKey(getKeyInputStream(ctx));
|
|
|
|
|
|
|
+ PGPSecretKey key = GPGStorage.GetGPGSecretKey(ctx);
|
|
|
Iterator<String> users = key.getUserIDs();
|
|
Iterator<String> users = key.getUserIDs();
|
|
|
if (users.hasNext())
|
|
if (users.hasNext())
|
|
|
return users.next();
|
|
return users.next();
|
|
@@ -396,6 +370,6 @@ public class GPGUtil {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static boolean CheckIsPasswordProtected(Context ctx) throws IOException {
|
|
public static boolean CheckIsPasswordProtected(Context ctx) throws IOException {
|
|
|
- return TryPassword(findSecretKey(getKeyInputStream(ctx)), "") == null;
|
|
|
|
|
|
|
+ return TryPassword(GPGStorage.GetGPGSecretKey(ctx), "") == null;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|