AIDL API changes

This commit is contained in:
Dominik Schürmann
2012-12-14 18:22:03 +01:00
parent 2dcaad3d3b
commit be4e3a10b0
36 changed files with 1113 additions and 275 deletions

View File

@@ -18,17 +18,24 @@ package org.thialfihar.android.apg.helper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import org.spongycastle.bcpg.ArmoredOutputStream;
import org.spongycastle.openpgp.PGPKeyRing;
import org.spongycastle.openpgp.PGPObjectFactory;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSecretKeyRing;
import org.spongycastle.openpgp.PGPUtil;
import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.provider.ProviderHelper;
import org.thialfihar.android.apg.util.Log;
import android.content.Context;
public class PGPConversionHelper {
/**
@@ -134,4 +141,17 @@ public class PGPConversionHelper {
return null;
}
}
public static PGPKeyRing decodeKeyRing(InputStream is) throws IOException {
InputStream in = PGPUtil.getDecoderStream(is);
PGPObjectFactory objectFactory = new PGPObjectFactory(in);
Object obj = objectFactory.nextObject();
if (obj instanceof PGPKeyRing) {
return (PGPKeyRing) obj;
}
return null;
}
}

View File

@@ -20,9 +20,11 @@ package org.thialfihar.android.apg.helper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Vector;
import org.spongycastle.bcpg.ArmoredOutputStream;
@@ -38,24 +40,17 @@ import org.spongycastle.openpgp.PGPSignatureSubpacketVector;
import org.spongycastle.openpgp.PGPUtil;
import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.R;
import org.thialfihar.android.apg.provider.ApgContract.KeyRings;
import org.thialfihar.android.apg.provider.ProviderHelper;
import org.thialfihar.android.apg.util.IterableIterator;
import org.thialfihar.android.apg.util.Log;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
public class PGPHelper {
public static PGPKeyRing decodeKeyRing(InputStream is) throws IOException {
InputStream in = PGPUtil.getDecoderStream(is);
PGPObjectFactory objectFactory = new PGPObjectFactory(in);
Object obj = objectFactory.nextObject();
if (obj instanceof PGPKeyRing) {
return (PGPKeyRing) obj;
}
return null;
}
public static Date getCreationDate(PGPPublicKey key) {
return key.getCreationTime();
@@ -191,7 +186,8 @@ public class PGPHelper {
}
public static PGPPublicKey getEncryptPublicKey(Context context, long masterKeyId) {
PGPPublicKeyRing keyRing = ProviderHelper.getPGPPublicKeyRingByMasterKeyId(context, masterKeyId);
PGPPublicKeyRing keyRing = ProviderHelper.getPGPPublicKeyRingByMasterKeyId(context,
masterKeyId);
if (keyRing == null) {
Log.e(Constants.TAG, "keyRing is null!");
return null;
@@ -205,7 +201,8 @@ public class PGPHelper {
}
public static PGPSecretKey getSigningKey(Context context, long masterKeyId) {
PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRingByMasterKeyId(context, masterKeyId);
PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRingByMasterKeyId(context,
masterKeyId);
if (keyRing == null) {
return null;
}
@@ -374,7 +371,7 @@ public class PGPHelper {
} else if (i != 0 && i % 2 == 0) {
fingerPrint += " ";
}
String chunk = Integer.toHexString((fp[i] + 256) % 256).toUpperCase();
String chunk = Integer.toHexString((fp[i] + 256) % 256).toUpperCase(Locale.US);
while (chunk.length() < 2) {
chunk = "0" + chunk;
}
@@ -385,37 +382,6 @@ public class PGPHelper {
}
public static String getPubkeyAsArmoredString(Context context, long keyId) {
PGPPublicKey key = ProviderHelper.getPGPPublicKeyByKeyId(context, keyId);
// if it is no public key get it from your own keys...
if (key == null) {
PGPSecretKey secretKey = ProviderHelper.getPGPSecretKeyByKeyId(context, keyId);
if (secretKey == null) {
Log.e(Constants.TAG, "Key could not be found!");
return null;
}
key = secretKey.getPublicKey();
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ArmoredOutputStream aos = new ArmoredOutputStream(bos);
aos.setHeader("Version", PGPMain.getFullVersion(context));
String armouredKey = null;
try {
aos.write(key.getEncoded());
aos.close();
armouredKey = bos.toString("UTF-8");
} catch (IOException e) {
Log.e(Constants.TAG, "Problems while encoding key as armored string", e);
}
Log.d(Constants.TAG, "key:" + armouredKey);
return armouredKey;
}
public static String getFingerPrint(Context context, long keyId) {
PGPPublicKey key = ProviderHelper.getPGPPublicKeyByKeyId(context, keyId);
// if it is no public key get it from your own keys...
@@ -432,7 +398,7 @@ public class PGPHelper {
}
public static String getSmallFingerPrint(long keyId) {
String fingerPrint = Long.toHexString(keyId & 0xffffffffL).toUpperCase();
String fingerPrint = Long.toHexString(keyId & 0xffffffffL).toUpperCase(Locale.US);
while (fingerPrint.length() < 8) {
fingerPrint = "0" + fingerPrint;
}
@@ -449,4 +415,5 @@ public class PGPHelper {
String s1 = data.substring(0, len - 8);
return (Long.parseLong(s1, 16) << 32) | Long.parseLong(s2, 16);
}
}

View File

@@ -564,7 +564,7 @@ public class PGPMain {
int oldKeys = 0;
int badKeys = 0;
try {
PGPKeyRing keyring = PGPHelper.decodeKeyRing(bufferedInput);
PGPKeyRing keyring = PGPConversionHelper.decodeKeyRing(bufferedInput);
while (keyring != null) {
int status = Integer.MIN_VALUE; // out of bounds value
@@ -592,7 +592,7 @@ public class PGPMain {
// TODO: needed?
// obj = objectFactory.nextObject();
keyring = PGPHelper.decodeKeyRing(bufferedInput);
keyring = PGPConversionHelper.decodeKeyRing(bufferedInput);
}
} catch (EOFException e) {
// nothing to do, we are done
@@ -652,6 +652,42 @@ public class PGPMain {
return returnData;
}
public static Bundle getKeyRings(Context context, long[] masterKeyIds,
OutputStream outStream, ProgressDialogUpdater progress) throws ApgGeneralException,
FileNotFoundException, PGPException, IOException {
Bundle returnData = new Bundle();
ArmoredOutputStream out = new ArmoredOutputStream(outStream);
out.setHeader("Version", getFullVersion(context));
int numKeys = 0;
for (int i = 0; i < masterKeyIds.length; ++i) {
updateProgress(progress, i * 100 / masterKeyIds.length, 100);
// try to get it as a PGPPublicKeyRing, if that fails try to get it as a SecretKeyRing
PGPPublicKeyRing publicKeyRing = ProviderHelper.getPGPPublicKeyRingByRowId(context,
masterKeyIds[i]);
if (publicKeyRing != null) {
publicKeyRing.encode(out);
} else {
PGPSecretKeyRing secretKeyRing = ProviderHelper.getPGPSecretKeyRingByRowId(context,
masterKeyIds[i]);
if (secretKeyRing != null) {
secretKeyRing.encode(out);
} else {
continue;
}
}
++numKeys;
}
out.close();
returnData.putInt(ApgIntentService.RESULT_EXPORT, numKeys);
updateProgress(progress, R.string.progress_done, 100, 100);
return returnData;
}
/**
* Encrypt and Sign data
*