/** * Replace the passed the public key on the passed in secret key. * * @param secretKey secret key to change * @param publicKey new public key. * @return a new secret key. * @throws IllegalArgumentException if keyIDs do not match. */ public static PGPSecretKey replacePublicKey(PGPSecretKey secretKey, PGPPublicKey publicKey) { if (publicKey.getKeyID() != secretKey.getKeyID()) { throw new IllegalArgumentException("keyIDs do not match"); } return new PGPSecretKey(secretKey.secret, publicKey); }
/** * Checks if key has a passphrase * * @param secretKeyId * @return true if it has a passphrase */ private static boolean hasPassphrase(Context context, long secretKeyId) { // check if the key has no passphrase try { PGPSecretKey secretKey = PGPHelper.getMasterKey(ProviderHelper.getPGPSecretKeyRingByKeyId(context, secretKeyId)); // PGPSecretKey secretKey = // PGPHelper.getMasterKey(PGPMain.getSecretKeyRing(secretKeyId)); Log.d(Constants.TAG, "Check if key has no passphrase..."); PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider("SC").build("".toCharArray()); PGPPrivateKey testKey = secretKey.extractPrivateKey(keyDecryptor); if (testKey != null) { Log.d(Constants.TAG, "Key has no passphrase! Caches empty passphrase!"); // cache empty passphrase PassphraseCacheService.addCachedPassphrase(context, secretKey.getKeyID(), ""); return false; } } catch (PGPException e) { // silently catch } return true; }
/** * If an Intent gives a signatureMasterKeyId and/or encryptionMasterKeyIds, preselect those! * * @param preselectedSignatureKeyId * @param preselectedEncryptionKeyIds */ private void preselectKeys( long preselectedSignatureKeyId, long[] preselectedEncryptionKeyIds, ProviderHelper providerHelper) { if (preselectedSignatureKeyId != 0) { // TODO: don't use bouncy castle objects! try { PGPSecretKeyRing keyRing = providerHelper.getPGPSecretKeyRingWithKeyId(preselectedSignatureKeyId); PGPSecretKey masterKey = keyRing.getSecretKey(); if (masterKey != null) { PGPSecretKey signKey = PgpKeyHelper.getFirstSigningSubkey(keyRing); if (signKey != null) { setSignatureKeyId(masterKey.getKeyID()); } } } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "key not found!", e); } } if (preselectedEncryptionKeyIds != null) { Vector<Long> goodIds = new Vector<Long>(); for (int i = 0; i < preselectedEncryptionKeyIds.length; ++i) { // TODO One query per selected key?! wtf try { long id = providerHelper.getMasterKeyId( KeyRings.buildUnifiedKeyRingsFindBySubkeyUri( Long.toString(preselectedEncryptionKeyIds[i]))); goodIds.add(id); } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "key not found!", e); } } if (goodIds.size() > 0) { long[] keyIds = new long[goodIds.size()]; for (int i = 0; i < goodIds.size(); ++i) { keyIds[i] = goodIds.get(i); } setEncryptionKeyIds(keyIds); } } }
public static long getDecryptionKeyId(Context context, InputStream inputStream) throws PgpGeneralException, NoAsymmetricEncryptionException, IOException { InputStream in = PGPUtil.getDecoderStream(inputStream); PGPObjectFactory pgpF = new PGPObjectFactory(in); PGPEncryptedDataList enc; Object o = pgpF.nextObject(); // the first object might be a PGP marker packet. if (o instanceof PGPEncryptedDataList) { enc = (PGPEncryptedDataList) o; } else { enc = (PGPEncryptedDataList) pgpF.nextObject(); } if (enc == null) { throw new PgpGeneralException(context.getString(R.string.error_invalid_data)); } // TODO: currently we always only look at the first known key // find the secret key PGPSecretKey secretKey = null; Iterator<?> it = enc.getEncryptedDataObjects(); boolean gotAsymmetricEncryption = false; while (it.hasNext()) { Object obj = it.next(); if (obj instanceof PGPPublicKeyEncryptedData) { gotAsymmetricEncryption = true; PGPPublicKeyEncryptedData pbe = (PGPPublicKeyEncryptedData) obj; secretKey = ProviderHelper.getPGPSecretKeyByKeyId(context, pbe.getKeyID()); if (secretKey != null) { break; } } } if (!gotAsymmetricEncryption) { throw new NoAsymmetricEncryptionException(); } if (secretKey == null) { return Id.key.none; } return secretKey.getKeyID(); }