From 9e37522bddc7c5a048aca7fc60153b6f10ce8f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 13 Sep 2014 19:05:53 +0200 Subject: Get certificates by raw user ids, be more liberal about accepted user id encodings --- .../keychain/pgp/UncachedPublicKey.java | 34 +++++++++++++++------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java index 404dbc0fb..e27190bc7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java @@ -28,11 +28,13 @@ import org.spongycastle.openpgp.PGPPublicKey; import org.spongycastle.openpgp.PGPSignature; import org.spongycastle.openpgp.PGPSignatureSubpacketVector; import org.spongycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider; +import org.spongycastle.util.Strings; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.util.IterableIterator; import org.sufficientlysecure.keychain.util.Log; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -126,14 +128,14 @@ public class UncachedPublicKey { * */ public String getPrimaryUserId() { - String found = null; + byte[] found = null; PGPSignature foundSig = null; // noinspection unchecked - for (String userId : new IterableIterator(mPublicKey.getUserIDs())) { + for (byte[] rawUserId : new IterableIterator(mPublicKey.getRawUserIDs())) { PGPSignature revocation = null; @SuppressWarnings("unchecked") - Iterator signaturesIt = mPublicKey.getSignaturesForID(userId); + Iterator signaturesIt = mPublicKey.getSignaturesForID(rawUserId); // no signatures for this User ID if (signaturesIt == null) { continue; @@ -147,10 +149,10 @@ public class UncachedPublicKey { // make sure it's actually valid sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider( Constants.BOUNCY_CASTLE_PROVIDER_NAME), mPublicKey); - if (!sig.verifyCertification(userId, mPublicKey)) { + if (!sig.verifyCertification(rawUserId, mPublicKey)) { continue; } - if (found != null && found.equals(userId)) { + if (found != null && Arrays.equals(found, rawUserId)) { found = null; } revocation = sig; @@ -169,8 +171,8 @@ public class UncachedPublicKey { // make sure it's actually valid sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider( Constants.BOUNCY_CASTLE_PROVIDER_NAME), mPublicKey); - if (sig.verifyCertification(userId, mPublicKey)) { - found = userId; + if (sig.verifyCertification(rawUserId, mPublicKey)) { + found = rawUserId; foundSig = sig; // this one can't be relevant anymore at this point revocation = null; @@ -182,7 +184,11 @@ public class UncachedPublicKey { } } } - return found; + if (found != null) { + return Strings.fromUTF8ByteArray(found); + } else { + return null; + } } /** @@ -204,6 +210,14 @@ public class UncachedPublicKey { return userIds; } + public ArrayList getUnorderedRawUserIds() { + ArrayList userIds = new ArrayList(); + for (byte[] userId : new IterableIterator(mPublicKey.getRawUserIDs())) { + userIds.add(userId); + } + return userIds; + } + public boolean isElGamalEncrypt() { return getAlgorithm() == PGPPublicKey.ELGAMAL_ENCRYPT; } @@ -320,8 +334,8 @@ public class UncachedPublicKey { }; } - public Iterator getSignaturesForId(String userId) { - final Iterator it = mPublicKey.getSignaturesForID(userId); + public Iterator getSignaturesForRawId(byte[] rawUserId) { + final Iterator it = mPublicKey.getSignaturesForID(rawUserId); if (it != null) { return new Iterator() { public void remove() { -- cgit v1.2.3