diff options
Diffstat (limited to 'org_apg/src/org/thialfihar/android/apg/helper')
4 files changed, 91 insertions, 16 deletions
diff --git a/org_apg/src/org/thialfihar/android/apg/helper/OtherHelper.java b/org_apg/src/org/thialfihar/android/apg/helper/OtherHelper.java index 516d7fccb..0d2f060dd 100644 --- a/org_apg/src/org/thialfihar/android/apg/helper/OtherHelper.java +++ b/org_apg/src/org/thialfihar/android/apg/helper/OtherHelper.java @@ -27,6 +27,9 @@ import java.util.Set; import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.util.Log; +import com.actionbarsherlock.app.ActionBar; +import com.actionbarsherlock.app.SherlockFragmentActivity; + import android.content.Context; import android.os.Bundle; @@ -108,4 +111,24 @@ public class OtherHelper { } } } + + /** + * Set actionbar without home button if called from another app + * + * @param activity + */ + public static void setActionBarBackButton(SherlockFragmentActivity activity) { + // set actionbar without home button if called from another app + final ActionBar actionBar = activity.getSupportActionBar(); + Log.d(Constants.TAG, "calling package (only set when using startActivityForResult)=" + + activity.getCallingPackage()); + if (activity.getCallingPackage() != null + && activity.getCallingPackage().equals(Constants.PACKAGE_NAME)) { + actionBar.setDisplayHomeAsUpEnabled(true); + actionBar.setHomeButtonEnabled(true); + } else { + actionBar.setDisplayHomeAsUpEnabled(false); + actionBar.setHomeButtonEnabled(false); + } + } } diff --git a/org_apg/src/org/thialfihar/android/apg/helper/PGPConversionHelper.java b/org_apg/src/org/thialfihar/android/apg/helper/PGPConversionHelper.java index 72294e8ce..c9a7b33a0 100644 --- a/org_apg/src/org/thialfihar/android/apg/helper/PGPConversionHelper.java +++ b/org_apg/src/org/thialfihar/android/apg/helper/PGPConversionHelper.java @@ -43,9 +43,7 @@ public class PGPConversionHelper { try { key.encode(os); } catch (IOException e) { - Log.e(Constants.TAG, - "Error while converting PGPSecretKey to byte[]: " + e.getMessage()); - e.printStackTrace(); + Log.e(Constants.TAG, "Error while converting PGPSecretKey to byte[]!", e); } } @@ -68,7 +66,7 @@ public class PGPConversionHelper { Log.e(Constants.TAG, "No keys given!"); } } catch (IOException e) { - e.printStackTrace(); + Log.e(Constants.TAG, "Error while converting to PGPSecretKeyRing!", e); } return keyRing; @@ -88,7 +86,7 @@ public class PGPConversionHelper { Log.e(Constants.TAG, "No keys given!"); } } catch (IOException e) { - e.printStackTrace(); + Log.e(Constants.TAG, "Error while converting to PGPPublicKeyRing!", e); } return keyRing; @@ -135,7 +133,7 @@ public class PGPConversionHelper { try { return key.getEncoded(); } catch (IOException e) { - Log.e(Constants.TAG, "Encoding failed: ", e); + Log.e(Constants.TAG, "Encoding failed", e); return null; } @@ -151,7 +149,7 @@ public class PGPConversionHelper { try { return keyRing.getEncoded(); } catch (IOException e) { - Log.e(Constants.TAG, "Encoding failed: ", e); + Log.e(Constants.TAG, "Encoding failed", e); return null; } diff --git a/org_apg/src/org/thialfihar/android/apg/helper/PGPHelper.java b/org_apg/src/org/thialfihar/android/apg/helper/PGPHelper.java index 6d5a07f56..65b782c96 100644 --- a/org_apg/src/org/thialfihar/android/apg/helper/PGPHelper.java +++ b/org_apg/src/org/thialfihar/android/apg/helper/PGPHelper.java @@ -233,16 +233,16 @@ public class PGPHelper { public static String getMainUserIdSafe(Context context, PGPPublicKey key) { String userId = getMainUserId(key); - if (userId == null) { - userId = context.getResources().getString(R.string.unknownUserId); + if (userId == null || userId.equals("")) { + userId = context.getString(R.string.unknownUserId); } return userId; } public static String getMainUserIdSafe(Context context, PGPSecretKey key) { String userId = getMainUserId(key); - if (userId == null) { - userId = context.getResources().getString(R.string.unknownUserId); + if (userId == null || userId.equals("")) { + userId = context.getString(R.string.unknownUserId); } return userId; } @@ -384,7 +384,7 @@ public class PGPHelper { } - public static String getPubkeyAsArmoredString(long keyId) { + public static String getPubkeyAsArmoredString(Context context, long keyId) { PGPPublicKey key = PGPMain.getPublicKey(keyId); // if it is no public key get it from your own keys... if (key == null) { @@ -398,6 +398,8 @@ public class PGPHelper { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ArmoredOutputStream aos = new ArmoredOutputStream(bos); + aos.setHeader("Version", PGPMain.getFullVersion(context)); + String armouredKey = null; try { aos.write(key.getEncoded()); diff --git a/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java b/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java index 960baea72..2fd63bda1 100644 --- a/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java +++ b/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java @@ -51,6 +51,7 @@ import org.spongycastle.openpgp.PGPSignature; import org.spongycastle.openpgp.PGPSignatureGenerator; import org.spongycastle.openpgp.PGPSignatureList; import org.spongycastle.openpgp.PGPSignatureSubpacketGenerator; +import org.spongycastle.openpgp.PGPSignatureSubpacketVector; import org.spongycastle.openpgp.PGPUtil; import org.spongycastle.openpgp.PGPV3SignatureGenerator; import org.spongycastle.openpgp.operator.PBEDataDecryptorFactory; @@ -88,7 +89,6 @@ import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Id; import org.thialfihar.android.apg.R; -import android.app.Activity; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager.NameNotFoundException; @@ -750,6 +750,7 @@ public class PGPMain { throw new GeneralException(context.getString(R.string.error_externalStorageNotReady)); } ArmoredOutputStream out = new ArmoredOutputStream(outStream); + out.setHeader("Version", getFullVersion(context)); int numKeys = 0; for (int i = 0; i < keyRingIds.size(); ++i) { @@ -771,7 +772,7 @@ public class PGPMain { ++numKeys; } out.close(); - returnData.putInt("exported", numKeys); + returnData.putInt(ApgService.RESULT_EXPORT, numKeys); if (progress != null) progress.setProgress(R.string.progress_done, 100, 100); @@ -1048,22 +1049,26 @@ public class PGPMain { PGPPrivateKey signaturePrivateKey = null; if (signatureKeyId == 0) { + armorOut.close(); throw new GeneralException(context.getString(R.string.error_noSignatureKey)); } signingKeyRing = getSecretKeyRing(signatureKeyId); signingKey = PGPHelper.getSigningKey(signatureKeyId); if (signingKey == null) { + armorOut.close(); throw new GeneralException(context.getString(R.string.error_signatureFailed)); } if (signaturePassPhrase == null) { + armorOut.close(); throw new GeneralException(context.getString(R.string.error_noSignaturePassPhrase)); } PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider( BOUNCY_CASTLE_PROVIDER_NAME).build(signaturePassPhrase.toCharArray()); signaturePrivateKey = signingKey.extractPrivateKey(keyDecryptor); if (signaturePrivateKey == null) { + armorOut.close(); throw new GeneralException(context.getString(R.string.error_couldNotExtractPrivateKey)); } if (progress != null) @@ -1147,8 +1152,10 @@ public class PGPMain { throws GeneralException, PGPException, IOException, NoSuchAlgorithmException, SignatureException { - ArmoredOutputStream armorOut = null; OutputStream out = null; + + // Ascii Armor (Base64) + ArmoredOutputStream armorOut = null; if (armored) { armorOut = new ArmoredOutputStream(outStream); armorOut.setHeader("Version", getFullVersion(context)); @@ -1174,6 +1181,7 @@ public class PGPMain { if (signaturePassPhrase == null) { throw new GeneralException(context.getString(R.string.error_noSignaturePassPhrase)); } + PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider( BOUNCY_CASTLE_PROVIDER_NAME).build(signaturePassPhrase.toCharArray()); signaturePrivateKey = signingKey.extractPrivateKey(keyDecryptor); @@ -1202,7 +1210,6 @@ public class PGPMain { if (forceV3Signature) { signatureV3Generator = new PGPV3SignatureGenerator(contentSignerBuilder); signatureV3Generator.init(type, signaturePrivateKey); - } else { signatureGenerator = new PGPSignatureGenerator(contentSignerBuilder); signatureGenerator.init(type, signaturePrivateKey); @@ -1261,6 +1268,51 @@ public class PGPMain { progress.setProgress(R.string.progress_done, 100, 100); } + public static PGPPublicKeyRing signKey(Context context, long masterKeyId, long pubKeyId) + throws GeneralException, NoSuchAlgorithmException, NoSuchProviderException, + PGPException, SignatureException { + String signaturePassPhrase = PGPMain.getCachedPassPhrase(masterKeyId); + if (signaturePassPhrase == null || signaturePassPhrase.length() <= 0) { + throw new GeneralException("Unable to obtain passphrase"); + } else { + PGPPublicKeyRing pubring = PGPMain.getPublicKeyRing(pubKeyId); + + PGPSecretKey signingKey = PGPHelper.getSigningKey(masterKeyId); + if (signingKey == null) { + throw new GeneralException(context.getString(R.string.error_signatureFailed)); + } + + PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider( + BOUNCY_CASTLE_PROVIDER_NAME).build(signaturePassPhrase.toCharArray()); + PGPPrivateKey signaturePrivateKey = signingKey.extractPrivateKey(keyDecryptor); + if (signaturePrivateKey == null) { + throw new GeneralException( + context.getString(R.string.error_couldNotExtractPrivateKey)); + } + + // TODO: SHA256 fixed? + JcaPGPContentSignerBuilder contentSignerBuilder = new JcaPGPContentSignerBuilder( + signingKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256) + .setProvider(BOUNCY_CASTLE_PROVIDER_NAME); + + PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator( + contentSignerBuilder); + + signatureGenerator.init(PGPSignature.DIRECT_KEY, signaturePrivateKey); + + PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); + + PGPSignatureSubpacketVector packetVector = spGen.generate(); + signatureGenerator.setHashedSubpackets(packetVector); + + PGPPublicKey signedKey = PGPPublicKey.addCertification(pubring.getPublicKey(pubKeyId), + signatureGenerator.generate()); + pubring = PGPPublicKeyRing.insertPublicKey(pubring, signedKey); + + return pubring; + } + } + public static long getDecryptionKeyId(Context context, InputStream inputStream) throws GeneralException, NoAsymmetricEncryptionException, IOException { InputStream in = PGPUtil.getDecoderStream(inputStream); |