wrapped-key-ring: split up CachedKeyRing and WrappedKeyRing

This commit is contained in:
Vincent Breitmoser
2014-05-21 21:07:32 +02:00
parent 2f95100d88
commit 761d87b661
23 changed files with 370 additions and 211 deletions

View File

@@ -1,68 +0,0 @@
package org.sufficientlysecure.keychain.pgp;
public abstract class CachedKeyRing {
private final long mMasterKeyId;
private final String mUserId;
private final boolean mHasAnySecret;
private final boolean mIsRevoked;
private final boolean mCanCertify;
private final long mHasEncryptId;
private final long mHasSignId;
private final int mVerified;
protected CachedKeyRing(long masterKeyId, String userId, boolean hasAnySecret,
boolean isRevoked, boolean canCertify, long hasEncryptId, long hasSignId,
int verified)
{
mMasterKeyId = masterKeyId;
mUserId = userId;
mHasAnySecret = hasAnySecret;
mIsRevoked = isRevoked;
mCanCertify = canCertify;
mHasEncryptId = hasEncryptId;
mHasSignId = hasSignId;
mVerified = verified;
}
public long getMasterKeyId() {
return mMasterKeyId;
}
public String getPrimaryUserId() {
return mUserId;
}
public boolean hasAnySecret() {
return mHasAnySecret;
}
public boolean isRevoked() {
return mIsRevoked;
}
public boolean canCertify() {
return mCanCertify;
}
public long getEncryptId() {
return mHasEncryptId;
}
public boolean hasEncrypt() {
return mHasEncryptId != 0;
}
public long getSignId() {
return mHasSignId;
}
public boolean hasSign() {
return mHasSignId != 0;
}
public int getVerified() {
return mVerified;
}
}

View File

@@ -0,0 +1,25 @@
package org.sufficientlysecure.keychain.pgp;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
public abstract class KeyRing {
abstract public long getMasterKeyId() throws PgpGeneralException;
abstract public String getPrimaryUserId() throws PgpGeneralException;
abstract public boolean isRevoked() throws PgpGeneralException;
abstract public boolean canCertify() throws PgpGeneralException;
abstract public long getEncryptId() throws PgpGeneralException;
abstract public boolean hasEncrypt() throws PgpGeneralException;
abstract public long getSignId() throws PgpGeneralException;
abstract public boolean hasSign() throws PgpGeneralException;
abstract public int getVerified() throws PgpGeneralException;
}

View File

@@ -233,7 +233,7 @@ public class PgpDecryptVerify {
PGPPublicKeyEncryptedData encryptedDataAsymmetric = null;
PGPPBEEncryptedData encryptedDataSymmetric = null;
CachedSecretKey secretEncryptionKey = null;
WrappedSecretKey secretEncryptionKey = null;
Iterator<?> it = enc.getEncryptedDataObjects();
boolean asymmetricPacketFound = false;
boolean symmetricPacketFound = false;
@@ -245,10 +245,10 @@ public class PgpDecryptVerify {
PGPPublicKeyEncryptedData encData = (PGPPublicKeyEncryptedData) obj;
CachedSecretKeyRing secretKeyRing;
WrappedSecretKeyRing secretKeyRing;
try {
// get actual keyring object based on master key id
secretKeyRing = mProviderHelper.getCachedSecretKeyRing(
secretKeyRing = mProviderHelper.getWrappedSecretKeyRing(
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(
Long.toString(encData.getKeyID()))
);
@@ -368,8 +368,8 @@ public class PgpDecryptVerify {
Object dataChunk = plainFact.nextObject();
OpenPgpSignatureResultBuilder signatureResultBuilder = new OpenPgpSignatureResultBuilder();
int signatureIndex = -1;
CachedPublicKeyRing signingRing = null;
CachedPublicKey signingKey = null;
WrappedPublicKeyRing signingRing = null;
WrappedPublicKey signingKey = null;
if (dataChunk instanceof PGPCompressedData) {
updateProgress(R.string.progress_decompressing_data, currentProgress, 100);
@@ -393,7 +393,7 @@ public class PgpDecryptVerify {
for (int i = 0; i < sigList.size(); ++i) {
try {
long sigKeyId = sigList.get(i).getKeyID();
signingRing = mProviderHelper.getCachedPublicKeyRing(
signingRing = mProviderHelper.getWrappedPublicKeyRing(
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(
Long.toString(sigKeyId)
)
@@ -413,7 +413,11 @@ public class PgpDecryptVerify {
signatureResultBuilder.signatureAvailable(true);
signatureResultBuilder.knownKey(true);
signatureResultBuilder.keyId(signingRing.getMasterKeyId());
signatureResultBuilder.userId(signingRing.getPrimaryUserId());
try {
signatureResultBuilder.userId(signingRing.getPrimaryUserId());
} catch(PgpGeneralException e) {
Log.d(Constants.TAG, "No primary user id in key " + signingRing.getMasterKeyId());
}
signatureResultBuilder.signatureKeyCertified(signingRing.getVerified() > 0);
signingKey.initSignature(signature);
@@ -567,8 +571,8 @@ public class PgpDecryptVerify {
throw new InvalidDataException();
}
CachedPublicKeyRing signingRing = null;
CachedPublicKey signingKey = null;
WrappedPublicKeyRing signingRing = null;
WrappedPublicKey signingKey = null;
int signatureIndex = -1;
// go through all signatures
@@ -576,7 +580,7 @@ public class PgpDecryptVerify {
for (int i = 0; i < sigList.size(); ++i) {
try {
long sigKeyId = sigList.get(i).getKeyID();
signingRing = mProviderHelper.getCachedPublicKeyRing(
signingRing = mProviderHelper.getWrappedPublicKeyRing(
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(
Long.toString(sigKeyId)
)
@@ -598,7 +602,11 @@ public class PgpDecryptVerify {
signatureResultBuilder.signatureAvailable(true);
signatureResultBuilder.knownKey(true);
signatureResultBuilder.keyId(signingRing.getMasterKeyId());
signatureResultBuilder.userId(signingRing.getPrimaryUserId());
try {
signatureResultBuilder.userId(signingRing.getPrimaryUserId());
} catch(PgpGeneralException e) {
Log.d(Constants.TAG, "No primary user id in key " + signingRing.getMasterKeyId());
}
signatureResultBuilder.signatureKeyCertified(signingRing.getVerified() > 0);
signingKey.initSignature(signature);

View File

@@ -27,7 +27,6 @@ import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.PGPKeyRing;
import org.spongycastle.openpgp.PGPPublicKey;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSecretKeyRing;
import org.spongycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
import org.sufficientlysecure.keychain.Constants;
@@ -101,7 +100,7 @@ public class PgpImportExport {
}
}
public boolean uploadKeyRingToServer(HkpKeyServer server, CachedPublicKeyRing keyring) {
public boolean uploadKeyRingToServer(HkpKeyServer server, WrappedPublicKeyRing keyring) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ArmoredOutputStream aos = null;
try {
@@ -229,7 +228,7 @@ public class PgpImportExport {
updateProgress(progress * 100 / masterKeyIdsSize, 100);
try {
CachedPublicKeyRing ring = mProviderHelper.getCachedPublicKeyRing(
WrappedPublicKeyRing ring = mProviderHelper.getWrappedPublicKeyRing(
KeychainContract.KeyRings.buildGenericKeyRingUri(pubKeyMasterId)
);

View File

@@ -72,7 +72,6 @@ public class PgpKeyHelper {
}
@SuppressWarnings("unchecked")
@Deprecated
public static boolean isEncryptionKey(PGPPublicKey key) {
if (!key.isEncryptionKey()) {
return false;
@@ -114,7 +113,6 @@ public class PgpKeyHelper {
}
@SuppressWarnings("unchecked")
@Deprecated
public static boolean isSigningKey(PGPPublicKey key) {
if (key.getVersion() <= 3) {
return true;
@@ -146,7 +144,6 @@ public class PgpKeyHelper {
}
@SuppressWarnings("unchecked")
@Deprecated
public static boolean isCertificationKey(PGPPublicKey key) {
if (key.getVersion() <= 3) {
return true;

View File

@@ -337,8 +337,8 @@ public class PgpKeyOperation {
}
public UncachedKeyRing buildSecretKey(CachedSecretKeyRing wmKR,
CachedPublicKeyRing wpKR,
public UncachedKeyRing buildSecretKey(WrappedSecretKeyRing wmKR,
WrappedPublicKeyRing wpKR,
SaveKeyringParcel saveParcel)
throws PgpGeneralMsgIdException, PGPException, SignatureException, IOException {

View File

@@ -266,11 +266,11 @@ public class PgpSignEncrypt {
}
/* Get keys for signature generation for later usage */
CachedSecretKey signingKey = null;
WrappedSecretKey signingKey = null;
if (enableSignature) {
CachedSecretKeyRing signingKeyRing;
WrappedSecretKeyRing signingKeyRing;
try {
signingKeyRing = mProviderHelper.getCachedSecretKeyRing(mSignatureMasterKeyId);
signingKeyRing = mProviderHelper.getWrappedSecretKeyRing(mSignatureMasterKeyId);
} catch (ProviderHelper.NotFoundException e) {
throw new NoSigningKeyException();
}
@@ -316,9 +316,9 @@ public class PgpSignEncrypt {
// Asymmetric encryption
for (long id : mEncryptionMasterKeyIds) {
try {
CachedPublicKeyRing keyRing = mProviderHelper.getCachedPublicKeyRing(
WrappedPublicKeyRing keyRing = mProviderHelper.getWrappedPublicKeyRing(
KeyRings.buildUnifiedKeyRingUri(Long.toString(id)));
CachedPublicKey key = keyRing.getEncryptionSubKey();
WrappedPublicKey key = keyRing.getEncryptionSubKey();
cPk.addMethod(key.getPubKeyEncryptionGenerator());
} catch (PgpGeneralException e) {
Log.e(Constants.TAG, "key not found!", e);

View File

@@ -0,0 +1,81 @@
package org.sufficientlysecure.keychain.pgp;
import org.spongycastle.openpgp.PGPKeyRing;
import org.spongycastle.openpgp.PGPPublicKey;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.util.IterableIterator;
public abstract class WrappedKeyRing extends KeyRing {
private final boolean mHasAnySecret;
private final int mVerified;
WrappedKeyRing(boolean hasAnySecret, int verified) {
mHasAnySecret = hasAnySecret;
mVerified = verified;
}
public long getMasterKeyId() {
return getRing().getPublicKey().getKeyID();
}
public boolean hasAnySecret() {
return mHasAnySecret;
}
public int getVerified() {
return mVerified;
}
public String getPrimaryUserId() throws PgpGeneralException {
return (String) getRing().getPublicKey().getUserIDs().next();
};
public boolean isRevoked() throws PgpGeneralException {
// Is the master key revoked?
return getRing().getPublicKey().isRevoked();
}
public boolean canCertify() throws PgpGeneralException {
return getRing().getPublicKey().isEncryptionKey();
}
public long getEncryptId() throws PgpGeneralException {
for(PGPPublicKey key : new IterableIterator<PGPPublicKey>(getRing().getPublicKeys())) {
if(PgpKeyHelper.isEncryptionKey(key)) {
return key.getKeyID();
}
}
throw new PgpGeneralException("No valid encryption key found!");
}
public boolean hasEncrypt() throws PgpGeneralException {
try {
getEncryptId();
return true;
} catch(PgpGeneralException e) {
return false;
}
}
public long getSignId() throws PgpGeneralException {
for(PGPPublicKey key : new IterableIterator<PGPPublicKey>(getRing().getPublicKeys())) {
if(PgpKeyHelper.isSigningKey(key)) {
return key.getKeyID();
}
}
throw new PgpGeneralException("No valid signing key found!");
}
public boolean hasSign() throws PgpGeneralException {
try {
getSignId();
return true;
} catch (PgpGeneralException e) {
return false;
}
}
abstract PGPKeyRing getRing();
}

View File

@@ -11,12 +11,12 @@ import org.sufficientlysecure.keychain.util.IterableIterator;
import java.security.SignatureException;
public class CachedPublicKey extends UncachedPublicKey {
public class WrappedPublicKey extends UncachedPublicKey {
// this is the parent key ring
final CachedKeyRing mRing;
final KeyRing mRing;
CachedPublicKey(CachedKeyRing ring, PGPPublicKey key) {
WrappedPublicKey(KeyRing ring, PGPPublicKey key) {
super(key);
mRing = ring;
}
@@ -25,7 +25,7 @@ public class CachedPublicKey extends UncachedPublicKey {
return new IterableIterator<String>(mPublicKey.getUserIDs());
}
public CachedKeyRing getKeyRing() {
public KeyRing getKeyRing() {
return mRing;
}

View File

@@ -18,18 +18,13 @@ import java.security.SignatureException;
import java.util.Arrays;
import java.util.Iterator;
public class CachedPublicKeyRing extends CachedKeyRing {
public class WrappedPublicKeyRing extends WrappedKeyRing {
private PGPPublicKeyRing mRing;
private final byte[] mPubKey;
public CachedPublicKeyRing(long masterKeyId, String userId, boolean hasAnySecret,
boolean isRevoked, boolean canCertify, long hasEncryptId, long hasSignId,
int verified, byte[] blob)
{
super(masterKeyId, userId, hasAnySecret, isRevoked, canCertify,
hasEncryptId, hasSignId, verified);
public WrappedPublicKeyRing(byte[] blob, boolean hasAnySecret, int verified) {
super(hasAnySecret, verified);
mPubKey = blob;
}
@@ -44,19 +39,19 @@ public class CachedPublicKeyRing extends CachedKeyRing {
getRing().encode(stream);
}
public CachedPublicKey getSubkey() {
return new CachedPublicKey(this, getRing().getPublicKey());
public WrappedPublicKey getSubkey() {
return new WrappedPublicKey(this, getRing().getPublicKey());
}
public CachedPublicKey getSubkey(long id) {
return new CachedPublicKey(this, getRing().getPublicKey(id));
public WrappedPublicKey getSubkey(long id) {
return new WrappedPublicKey(this, getRing().getPublicKey(id));
}
/** Getter that returns the subkey that should be used for signing. */
CachedPublicKey getEncryptionSubKey() throws PgpGeneralException {
WrappedPublicKey getEncryptionSubKey() throws PgpGeneralException {
PGPPublicKey key = getRing().getPublicKey(getEncryptId());
if(key != null) {
CachedPublicKey cKey = new CachedPublicKey(this, key);
WrappedPublicKey cKey = new WrappedPublicKey(this, key);
if(!cKey.canEncrypt()) {
throw new PgpGeneralException("key error");
}
@@ -66,7 +61,7 @@ public class CachedPublicKeyRing extends CachedKeyRing {
throw new PgpGeneralException("no encryption key available");
}
public boolean verifySubkeyBinding(CachedPublicKey cachedSubkey) {
public boolean verifySubkeyBinding(WrappedPublicKey cachedSubkey) {
boolean validSubkeyBinding = false;
boolean validTempSubkeyBinding = false;
boolean validPrimaryKeyBinding = false;
@@ -161,17 +156,17 @@ public class CachedPublicKeyRing extends CachedKeyRing {
return validPrimaryKeyBinding;
}
public IterableIterator<CachedPublicKey> iterator() {
public IterableIterator<WrappedPublicKey> iterator() {
final Iterator<PGPPublicKey> it = getRing().getPublicKeys();
return new IterableIterator<CachedPublicKey>(new Iterator<CachedPublicKey>() {
return new IterableIterator<WrappedPublicKey>(new Iterator<WrappedPublicKey>() {
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public CachedPublicKey next() {
return new CachedPublicKey(CachedPublicKeyRing.this, it.next());
public WrappedPublicKey next() {
return new WrappedPublicKey(WrappedPublicKeyRing.this, it.next());
}
@Override

View File

@@ -26,18 +26,18 @@ import java.security.NoSuchProviderException;
import java.security.SignatureException;
import java.util.List;
public class CachedSecretKey extends CachedPublicKey {
public class WrappedSecretKey extends WrappedPublicKey {
private final PGPSecretKey mSecretKey;
private PGPPrivateKey mPrivateKey = null;
CachedSecretKey(CachedSecretKeyRing ring, PGPSecretKey key) {
WrappedSecretKey(WrappedSecretKeyRing ring, PGPSecretKey key) {
super(ring, key.getPublicKey());
mSecretKey = key;
}
public CachedSecretKeyRing getRing() {
return (CachedSecretKeyRing) mRing;
public WrappedSecretKeyRing getRing() {
return (WrappedSecretKeyRing) mRing;
}
/** Returns the wrapped PGPSecretKeyRing.
@@ -137,7 +137,7 @@ public class CachedSecretKey extends CachedPublicKey {
* @param userIds User IDs to certify, must not be null or empty
* @return A keyring with added certifications
*/
public UncachedKeyRing certifyUserIds(CachedPublicKeyRing publicKeyRing, List<String> userIds)
public UncachedKeyRing certifyUserIds(WrappedPublicKeyRing publicKeyRing, List<String> userIds)
throws PgpGeneralMsgIdException, NoSuchAlgorithmException, NoSuchProviderException,
PGPException, SignatureException {

View File

@@ -16,18 +16,13 @@ import java.io.IOException;
import java.security.NoSuchProviderException;
import java.util.Iterator;
public class CachedSecretKeyRing extends CachedKeyRing {
public class WrappedSecretKeyRing extends WrappedKeyRing {
private PGPSecretKeyRing mRing;
public CachedSecretKeyRing(long masterKeyId, String userId, boolean hasAnySecret,
boolean isRevoked, boolean canCertify, long hasEncryptId, long hasSignId,
int verified, byte[] blob)
public WrappedSecretKeyRing(byte[] blob, boolean isRevoked, int verified)
{
super(masterKeyId, userId, hasAnySecret,
isRevoked, canCertify, hasEncryptId, hasSignId,
verified);
super(isRevoked, verified);
mRing = (PGPSecretKeyRing) PgpConversionHelper.BytesToPGPKeyRing(blob);
}
@@ -35,19 +30,19 @@ public class CachedSecretKeyRing extends CachedKeyRing {
return mRing;
}
public CachedSecretKey getSubKey() {
return new CachedSecretKey(this, mRing.getSecretKey());
public WrappedSecretKey getSubKey() {
return new WrappedSecretKey(this, mRing.getSecretKey());
}
public CachedSecretKey getSubKey(long id) {
return new CachedSecretKey(this, mRing.getSecretKey(id));
public WrappedSecretKey getSubKey(long id) {
return new WrappedSecretKey(this, mRing.getSecretKey(id));
}
/** Getter that returns the subkey that should be used for signing. */
CachedSecretKey getSigningSubKey() throws PgpGeneralException {
WrappedSecretKey getSigningSubKey() throws PgpGeneralException {
PGPSecretKey key = mRing.getSecretKey(getSignId());
if(key != null) {
CachedSecretKey cKey = new CachedSecretKey(this, key);
WrappedSecretKey cKey = new WrappedSecretKey(this, key);
if(!cKey.canSign()) {
throw new PgpGeneralException("key error");
}
@@ -105,17 +100,17 @@ public class CachedSecretKeyRing extends CachedKeyRing {
}
public IterableIterator<CachedSecretKey> iterator() {
public IterableIterator<WrappedSecretKey> iterator() {
final Iterator<PGPSecretKey> it = mRing.getSecretKeys();
return new IterableIterator<CachedSecretKey>(new Iterator<CachedSecretKey>() {
return new IterableIterator<WrappedSecretKey>(new Iterator<WrappedSecretKey>() {
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public CachedSecretKey next() {
return new CachedSecretKey(CachedSecretKeyRing.this, it.next());
public WrappedSecretKey next() {
return new WrappedSecretKey(WrappedSecretKeyRing.this, it.next());
}
@Override

View File

@@ -27,4 +27,7 @@ public class PgpGeneralException extends Exception {
public PgpGeneralException(String message, Throwable cause) {
super(message, cause);
}
public PgpGeneralException(Throwable cause) {
super(cause);
}
}