aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java
diff options
context:
space:
mode:
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java126
1 files changed, 74 insertions, 52 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java
index ab00db13a..4df86ee9b 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java
@@ -37,12 +37,16 @@ import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSecretKeyRing;
import org.spongycastle.openpgp.PGPSignature;
-import org.spongycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.spongycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider;
import org.sufficientlysecure.keychain.Constants;
+import org.sufficientlysecure.keychain.pgp.KeyRing;
+import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing;
+import org.sufficientlysecure.keychain.pgp.WrappedPublicKeyRing;
import org.sufficientlysecure.keychain.pgp.PgpConversionHelper;
import org.sufficientlysecure.keychain.pgp.PgpHelper;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
+import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
+import org.sufficientlysecure.keychain.pgp.UncachedSecretKeyRing;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiApps;
import org.sufficientlysecure.keychain.provider.KeychainContract.Certs;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRingData;
@@ -63,7 +67,6 @@ import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
-import java.util.Map;
import java.util.Set;
public class ProviderHelper {
@@ -141,35 +144,10 @@ public class ProviderHelper {
public HashMap<String, Object> getUnifiedData(long masterKeyId, String[] proj, int[] types)
throws NotFoundException {
- return getGenericData(KeyRings.buildUnifiedKeyRingUri(Long.toString(masterKeyId)), proj, types);
- }
-
- /**
- * Find the master key id related to a given query. The id will either be extracted from the
- * query, which should work for all specific /key_rings/ queries, or will be queried if it can't.
- */
- public long extractOrGetMasterKeyId(Uri queryUri)
- throws NotFoundException {
- // try extracting from the uri first
- String firstSegment = queryUri.getPathSegments().get(1);
- if (!firstSegment.equals("find")) try {
- return Long.parseLong(firstSegment);
- } catch (NumberFormatException e) {
- // didn't work? oh well.
- Log.d(Constants.TAG, "Couldn't get masterKeyId from URI, querying...");
- }
- return getMasterKeyId(queryUri);
- }
-
- public long getMasterKeyId(Uri queryUri) throws NotFoundException {
- Object data = getGenericData(queryUri, KeyRings.MASTER_KEY_ID, FIELD_TYPE_INTEGER);
- if (data != null) {
- return (Long) data;
- } else {
- throw new NotFoundException();
- }
+ return getGenericData(KeyRings.buildUnifiedKeyRingUri(masterKeyId), proj, types);
}
+ @Deprecated
public LongSparseArray<PGPKeyRing> getPGPKeyRings(Uri queryUri) {
Cursor cursor = mContentResolver.query(queryUri,
new String[]{KeyRingData.MASTER_KEY_ID, KeyRingData.KEY_RING_DATA},
@@ -193,6 +171,55 @@ public class ProviderHelper {
return result;
}
+ public CachedPublicKeyRing getCachedPublicKeyRing(Uri queryUri) {
+ return new CachedPublicKeyRing(this, queryUri);
+ }
+
+ public WrappedPublicKeyRing getWrappedPublicKeyRing(long id) throws NotFoundException {
+ return (WrappedPublicKeyRing) getWrappedKeyRing(KeyRings.buildUnifiedKeyRingUri(id), false);
+ }
+
+ public WrappedPublicKeyRing getWrappedPublicKeyRing(Uri queryUri) throws NotFoundException {
+ return (WrappedPublicKeyRing) getWrappedKeyRing(queryUri, false);
+ }
+
+ public WrappedSecretKeyRing getWrappedSecretKeyRing(long id) throws NotFoundException {
+ return (WrappedSecretKeyRing) getWrappedKeyRing(KeyRings.buildUnifiedKeyRingUri(id), true);
+ }
+
+ public WrappedSecretKeyRing getWrappedSecretKeyRing(Uri queryUri) throws NotFoundException {
+ return (WrappedSecretKeyRing) getWrappedKeyRing(queryUri, true);
+ }
+
+
+ private KeyRing getWrappedKeyRing(Uri queryUri, boolean secret) throws NotFoundException {
+ Cursor cursor = mContentResolver.query(queryUri,
+ new String[] {
+ // we pick from cache only information that is not easily available from keyrings
+ KeyRings.HAS_ANY_SECRET, KeyRings.VERIFIED,
+ // and of course, ring data
+ secret ? KeyRings.PRIVKEY_DATA : KeyRings.PUBKEY_DATA
+ }, null, null, null);
+ try {
+ if (cursor != null && cursor.moveToFirst()) {
+
+ boolean hasAnySecret = cursor.getInt(0) > 0;
+ int verified = cursor.getInt(1);
+ byte[] blob = cursor.getBlob(2);
+ return secret
+ ? new WrappedSecretKeyRing(blob, hasAnySecret, verified)
+ : new WrappedPublicKeyRing(blob, hasAnySecret, verified);
+ } else {
+ throw new NotFoundException("Key not found!");
+ }
+ } finally {
+ if (cursor != null) {
+ cursor.close();
+ }
+ }
+ }
+
+ @Deprecated
public PGPKeyRing getPGPKeyRing(Uri queryUri) throws NotFoundException {
LongSparseArray<PGPKeyRing> result = getPGPKeyRings(queryUri);
if (result.size() == 0) {
@@ -202,31 +229,10 @@ public class ProviderHelper {
}
}
- public PGPPublicKeyRing getPGPPublicKeyRingWithKeyId(long keyId)
- throws NotFoundException {
- Uri uri = KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId));
- long masterKeyId = getMasterKeyId(uri);
- return getPGPPublicKeyRing(masterKeyId);
- }
-
- public PGPSecretKeyRing getPGPSecretKeyRingWithKeyId(long keyId)
- throws NotFoundException {
- Uri uri = KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId));
- long masterKeyId = getMasterKeyId(uri);
- return getPGPSecretKeyRing(masterKeyId);
- }
-
- /**
- * Retrieves the actual PGPPublicKeyRing object from the database blob based on the masterKeyId
- */
- public PGPPublicKeyRing getPGPPublicKeyRing(long masterKeyId) throws NotFoundException {
- Uri queryUri = KeyRingData.buildPublicKeyRingUri(Long.toString(masterKeyId));
- return (PGPPublicKeyRing) getPGPKeyRing(queryUri);
- }
-
/**
* Retrieves the actual PGPSecretKeyRing object from the database blob based on the maserKeyId
*/
+ @Deprecated
public PGPSecretKeyRing getPGPSecretKeyRing(long masterKeyId) throws NotFoundException {
Uri queryUri = KeyRingData.buildSecretKeyRingUri(Long.toString(masterKeyId));
return (PGPSecretKeyRing) getPGPKeyRing(queryUri);
@@ -395,8 +401,18 @@ public class ProviderHelper {
* Saves a PGPSecretKeyRing in the DB. This will only work if a corresponding public keyring
* is already in the database!
*/
+ public void saveKeyRing(UncachedSecretKeyRing wrappedRing) throws IOException {
+ // TODO split up getters
+ PGPSecretKeyRing keyRing = wrappedRing.getSecretKeyRing();
+ saveKeyRing(keyRing);
+ }
+
+ /**
+ * Saves a PGPSecretKeyRing in the DB. This will only work if a corresponding public keyring
+ * is already in the database!
+ */
public void saveKeyRing(PGPSecretKeyRing keyRing) throws IOException {
- long masterKeyId = keyRing.getPublicKey().getKeyID();
+ long masterKeyId = keyRing.getSecretKey().getKeyID();
{
Uri uri = Keys.buildKeysUri(Long.toString(masterKeyId));
@@ -433,6 +449,12 @@ public class ProviderHelper {
}
+ public void saveKeyRing(UncachedKeyRing wrappedRing) throws IOException {
+ PGPPublicKeyRing pubRing = wrappedRing.getPublicRing();
+ PGPSecretKeyRing secRing = wrappedRing.getSecretRing();
+ saveKeyRing(pubRing, secRing);
+ }
+
/**
* Saves (or updates) a pair of public and secret KeyRings in the database
*/