diff options
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure')
29 files changed, 2231 insertions, 380 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java index 0fd109484..c769da421 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java @@ -46,6 +46,8 @@ public final class Constants {      public static final String INTENT_PREFIX = PACKAGE_NAME + ".action."; +    public static final String CUSTOM_CONTACT_DATA_MIME_TYPE = "vnd.android.cursor.item/vnd.org.sufficientlysecure.keychain.key"; +      public static final class Path {          public static final String APP_DIR = Environment.getExternalStorageDirectory()                  + "/OpenKeychain"; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java index f911318a0..5d6a62f9c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java @@ -17,6 +17,8 @@  package org.sufficientlysecure.keychain; +import android.accounts.Account; +import android.accounts.AccountManager;  import android.app.Application;  import android.content.Context;  import android.graphics.PorterDuff; @@ -24,6 +26,7 @@ import android.graphics.drawable.Drawable;  import android.os.Environment;  import org.spongycastle.jce.provider.BouncyCastleProvider; +import org.sufficientlysecure.keychain.helper.ContactHelper;  import org.sufficientlysecure.keychain.util.Log;  import org.sufficientlysecure.keychain.util.PRNGFixes; @@ -76,6 +79,17 @@ public class KeychainApplication extends Application {          brandGlowEffect(getApplicationContext(),                  getApplicationContext().getResources().getColor(R.color.emphasis)); + +        setupAccountAsNeeded(this); +    } + +    public static void setupAccountAsNeeded(Context context) { +        AccountManager manager = AccountManager.get(context); +        Account[] accounts = manager.getAccountsByType(Constants.PACKAGE_NAME); +        if (accounts == null || accounts.length == 0) { +            Account dummy = new Account(context.getString(R.string.app_name), Constants.PACKAGE_NAME); +            manager.addAccountExplicitly(dummy, null, null); +        }      }      static void brandGlowEffect(Context context, int brandColor) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java index a92ea5408..f50ccf6f8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java @@ -19,8 +19,18 @@ package org.sufficientlysecure.keychain.helper;  import android.accounts.Account;  import android.accounts.AccountManager; -import android.content.Context; +import android.content.*; +import android.database.Cursor; +import android.net.Uri; +import android.os.RemoteException; +import android.provider.ContactsContract;  import android.util.Patterns; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.pgp.KeyRing; +import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; +import org.sufficientlysecure.keychain.provider.KeychainContract; +import org.sufficientlysecure.keychain.util.Log;  import java.util.ArrayList;  import java.util.HashSet; @@ -29,6 +39,15 @@ import java.util.Set;  public class ContactHelper { +    public static final String[] KEYS_TO_CONTACT_PROJECTION = new String[]{ +            KeychainContract.KeyRings.USER_ID, +            KeychainContract.KeyRings.FINGERPRINT, +            KeychainContract.KeyRings.KEY_ID, +            KeychainContract.KeyRings.MASTER_KEY_ID}; +    public static final String[] RAW_CONTACT_ID_PROJECTION = new String[]{ContactsContract.RawContacts._ID}; +    public static final String FIND_RAW_CONTACT_SELECTION = +            ContactsContract.RawContacts.ACCOUNT_TYPE + "=? AND " + ContactsContract.RawContacts.SOURCE_ID + "=?"; +      public static final List<String> getMailAccounts(Context context) {          final Account[] accounts = AccountManager.get(context).getAccounts();          final Set<String> emailSet = new HashSet<String>(); @@ -39,4 +58,92 @@ public class ContactHelper {          }          return new ArrayList<String>(emailSet);      } + +    public static List<String> getContactMails(Context context) { +        ContentResolver resolver = context.getContentResolver(); +        Cursor mailCursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, +                new String[]{ContactsContract.CommonDataKinds.Email.DATA}, +                null, null, null); +        if (mailCursor == null) return null; + +        Set<String> mails = new HashSet<String>(); +        while (mailCursor.moveToNext()) { +            String email = mailCursor.getString(0); +            if (email != null) { +                mails.add(email); +            } +        } +        mailCursor.close(); +        return new ArrayList<String>(mails); +    } + +    public static Uri dataUriFromContactUri(Context context, Uri contactUri) { +        Cursor contactMasterKey = context.getContentResolver().query(contactUri, new String[]{ContactsContract.Data.DATA2}, null, null, null, null); +        if (contactMasterKey != null) { +            if (contactMasterKey.moveToNext()) { +                return KeychainContract.KeyRings.buildGenericKeyRingUri(contactMasterKey.getLong(0)); +            } +            contactMasterKey.close(); +        } +        return null; +    } + +    public static void writeKeysToContacts(Context context) { +        ContentResolver resolver = context.getContentResolver(); +        Cursor cursor = resolver.query(KeychainContract.KeyRings.buildUnifiedKeyRingsUri(), KEYS_TO_CONTACT_PROJECTION, +                null, null, null); +        if (cursor != null) { +            while (cursor.moveToNext()) { +                String[] userId = KeyRing.splitUserId(cursor.getString(0)); +                String fingerprint = PgpKeyHelper.convertFingerprintToHex(cursor.getBlob(1)); +                String keyIdShort = PgpKeyHelper.convertKeyIdToHexShort(cursor.getLong(2)); +                long masterKeyId = cursor.getLong(3); +                int rawContactId = -1; +                Cursor raw = resolver.query(ContactsContract.RawContacts.CONTENT_URI, RAW_CONTACT_ID_PROJECTION, +                        FIND_RAW_CONTACT_SELECTION, new String[]{Constants.PACKAGE_NAME, fingerprint}, null, null); +                if (raw != null) { +                    if (raw.moveToNext()) { +                        rawContactId = raw.getInt(0); +                    } +                    raw.close(); +                } +                ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); +                if (rawContactId == -1) { +                    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) +                            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, context.getString(R.string.app_name)) +                            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, Constants.PACKAGE_NAME) +                            .withValue(ContactsContract.RawContacts.SOURCE_ID, fingerprint) +                            .build()); +                    if (userId[0] != null) { +                        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) +                                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) +                                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) +                                .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, userId[0]) +                                .build()); +                    } +                    if (userId[1] != null) { +                        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) +                                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) +                                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) +                                .withValue(ContactsContract.CommonDataKinds.Email.DATA, userId[1]) +                                .build()); +                    } +                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) +                            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) +                            .withValue(ContactsContract.Data.MIMETYPE, Constants.CUSTOM_CONTACT_DATA_MIME_TYPE) +                            .withValue(ContactsContract.Data.DATA1, String.format(context.getString(R.string.contact_show_key), keyIdShort)) +                            .withValue(ContactsContract.Data.DATA2, masterKeyId) +                            .build()); +                } +                try { +                    resolver.applyBatch(ContactsContract.AUTHORITY, ops); +                } catch (RemoteException e) { +                    e.printStackTrace(); +                } catch (OperationApplicationException e) { +                    e.printStackTrace(); +                } +            } +            cursor.close(); +        } +    }  } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/EmailKeyHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/EmailKeyHelper.java new file mode 100644 index 000000000..80f52f914 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/EmailKeyHelper.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program.  If not, see <http://www.gnu.org/licenses/>. + */ + +package org.sufficientlysecure.keychain.helper; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.Messenger; +import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; +import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; +import org.sufficientlysecure.keychain.keyimport.Keyserver; +import org.sufficientlysecure.keychain.service.KeychainIntentService; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class EmailKeyHelper { + +    public static void importContacts(Context context, Messenger messenger) { +        importAll(context, messenger, ContactHelper.getContactMails(context)); +    } + +    public static void importAll(Context context, Messenger messenger, List<String> mails) { +        Set<ImportKeysListEntry> keys = new HashSet<ImportKeysListEntry>(); +        for (String mail : mails) { +            keys.addAll(getEmailKeys(context, mail)); +        } +        importKeys(context, messenger, new ArrayList<ImportKeysListEntry>(keys)); +    } + +    public static List<ImportKeysListEntry> getEmailKeys(Context context, String mail) { +        Set<ImportKeysListEntry> keys = new HashSet<ImportKeysListEntry>(); + +        // Try _hkp._tcp SRV record first +        String[] mailparts = mail.split("@"); +        if (mailparts.length == 2) { +            HkpKeyserver hkp = HkpKeyserver.resolve(mailparts[1]); +            if (hkp != null) { +                keys.addAll(getEmailKeys(mail, hkp)); +            } +        } + +        // Most users don't have the SRV record, so ask a default server as well +        String[] servers = Preferences.getPreferences(context).getKeyServers(); +        if (servers != null && servers.length != 0) { +            HkpKeyserver hkp = new HkpKeyserver(servers[0]); +            keys.addAll(getEmailKeys(mail, hkp)); +        } +        return new ArrayList<ImportKeysListEntry>(keys); +    } + +    private static void importKeys(Context context, Messenger messenger, List<ImportKeysListEntry> keys) { +        Intent importIntent = new Intent(context, KeychainIntentService.class); +        importIntent.setAction(KeychainIntentService.ACTION_DOWNLOAD_AND_IMPORT_KEYS); +        Bundle importData = new Bundle(); +        importData.putParcelableArrayList(KeychainIntentService.DOWNLOAD_KEY_LIST, +                new ArrayList<ImportKeysListEntry>(keys)); +        importIntent.putExtra(KeychainIntentService.EXTRA_DATA, importData); +        importIntent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); + +        context.startService(importIntent); +    } + +    public static List<ImportKeysListEntry> getEmailKeys(String mail, Keyserver keyServer) { +        Set<ImportKeysListEntry> keys = new HashSet<ImportKeysListEntry>(); +        try { +            for (ImportKeysListEntry key : keyServer.search(mail)) { +                if (key.isRevoked() || key.isExpired()) continue; +                for (String userId : key.getUserIds()) { +                    if (userId.toLowerCase().contains(mail.toLowerCase())) { +                        keys.add(key); +                    } +                } +            } +        } catch (Keyserver.QueryFailedException ignored) { +        } catch (Keyserver.QueryNeedsRepairException ignored) { +        } +        return new ArrayList<ImportKeysListEntry>(keys); +    } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java index 5969455bd..2ec9e1c07 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java @@ -33,6 +33,10 @@ import org.sufficientlysecure.keychain.Constants;  import org.sufficientlysecure.keychain.pgp.PgpHelper;  import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;  import org.sufficientlysecure.keychain.util.Log; +import org.xbill.DNS.Lookup; +import org.xbill.DNS.Record; +import org.xbill.DNS.SRVRecord; +import org.xbill.DNS.Type;  import java.io.IOException;  import java.io.InputStream; @@ -45,6 +49,8 @@ import java.net.URLDecoder;  import java.net.URLEncoder;  import java.net.UnknownHostException;  import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator;  import java.util.GregorianCalendar;  import java.util.List;  import java.util.Locale; @@ -75,6 +81,7 @@ public class HkpKeyserver extends Keyserver {      private String mHost;      private short mPort; +    private boolean mSecure;      /**       * pub:%keyid%:%algo%:%keylen%:%creationdate%:%expirationdate%:%flags% @@ -109,7 +116,7 @@ public class HkpKeyserver extends Keyserver {       */      public static final Pattern PUB_KEY_LINE = Pattern              .compile("pub:([0-9a-fA-F]+):([0-9]+):([0-9]+):([0-9]+):([0-9]*):([rde]*)[ \n\r]*" // pub line -                    + "(uid:(.*):([0-9]+):([0-9]*):([rde]*))+", // one or more uid lines +                    + "((uid:([^:]*):([0-9]+):([0-9]*):([rde]*)[ \n\r]*)+)", // one or more uid lines                      Pattern.CASE_INSENSITIVE);      /** @@ -137,10 +144,11 @@ public class HkpKeyserver extends Keyserver {       * </ul>       */      public static final Pattern UID_LINE = Pattern -            .compile("uid:(.*):([0-9]+):([0-9]*):([rde]*)", +            .compile("uid:([^:]*):([0-9]+):([0-9]*):([rde]*)",                      Pattern.CASE_INSENSITIVE);      private static final short PORT_DEFAULT = 11371; +    private static final short PORT_DEFAULT_HKPS = 443;      /**       * @param hostAndPort may be just @@ -151,31 +159,68 @@ public class HkpKeyserver extends Keyserver {      public HkpKeyserver(String hostAndPort) {          String host = hostAndPort;          short port = PORT_DEFAULT; -        final int colonPosition = hostAndPort.lastIndexOf(':'); -        if (colonPosition > 0) { -            host = hostAndPort.substring(0, colonPosition); -            final String portStr = hostAndPort.substring(colonPosition + 1); -            port = Short.decode(portStr); +        boolean secure = false; +        String[] parts = hostAndPort.split(":"); +        if (parts.length > 1) { +            if (!parts[0].contains(".")) { // This is not a domain or ip, so it must be a protocol name +                if (parts[0].equalsIgnoreCase("hkps") || parts[0].equalsIgnoreCase("https")) { +                    secure = true; +                    port = PORT_DEFAULT_HKPS; +                } else if (!parts[0].equalsIgnoreCase("hkp") && !parts[0].equalsIgnoreCase("http")) { +                    throw new IllegalArgumentException("Protocol " + parts[0] + " is unknown"); +                } +                host = parts[1]; +                if (host.startsWith("//")) { // People tend to type https:// and hkps://, so we'll support that as well +                    host = host.substring(2); +                } +                if (parts.length > 2) { +                    port = Short.decode(parts[2]); +                } +            } else { +                host = parts[0]; +                port = Short.decode(parts[1]); +            }          }          mHost = host;          mPort = port; +        mSecure = secure;      }      public HkpKeyserver(String host, short port) { +        this(host, port, false); +    } + +    public HkpKeyserver(String host, short port, boolean secure) {          mHost = host;          mPort = port; +        mSecure = secure; +    } + +    private String getUrlPrefix() { +        return mSecure ? "https://" : "http://";      }      private String query(String request) throws QueryFailedException, HttpError { -        InetAddress ips[]; -        try { -            ips = InetAddress.getAllByName(mHost); -        } catch (UnknownHostException e) { -            throw new QueryFailedException(e.toString()); +        List<String> urls = new ArrayList<String>(); +        if (mSecure) { +            urls.add(getUrlPrefix() + mHost + ":" + mPort + request); +        } else { +            InetAddress ips[]; +            try { +                ips = InetAddress.getAllByName(mHost); +            } catch (UnknownHostException e) { +                throw new QueryFailedException(e.toString()); +            } +            for (InetAddress ip : ips) { +                // Note: This is actually not HTTP 1.1 compliant, as we hide the real "Host" value, +                //       but Android's HTTPUrlConnection does not support any other way to set +                //       Socket's remote IP address... +                urls.add(getUrlPrefix() + ip.getHostAddress() + ":" + mPort + request); +            }          } -        for (int i = 0; i < ips.length; ++i) { + +        for (String url : urls) {              try { -                String url = "http://" + ips[i].getHostAddress() + ":" + mPort + request;                  Log.d(Constants.TAG, "hkp keyserver query: " + url);                  URL realUrl = new URL(url);                  HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(); @@ -238,6 +283,7 @@ public class HkpKeyserver extends Keyserver {          while (matcher.find()) {              final ImportKeysListEntry entry = new ImportKeysListEntry();              entry.setQuery(query); +            entry.setOrigin(getUrlPrefix() + mHost + ":" + mPort);              entry.setBitStrength(Integer.parseInt(matcher.group(3))); @@ -262,6 +308,7 @@ public class HkpKeyserver extends Keyserver {              entry.setDate(tmpGreg.getTime());              entry.setRevoked(matcher.group(6).contains("r")); +            entry.setExpired(matcher.group(6).contains("e"));              ArrayList<String> userIds = new ArrayList<String>();              final String uidLines = matcher.group(7); @@ -290,7 +337,7 @@ public class HkpKeyserver extends Keyserver {      public String get(String keyIdHex) throws QueryFailedException {          HttpClient client = new DefaultHttpClient();          try { -            String query = "http://" + mHost + ":" + mPort + +            String query = getUrlPrefix() + mHost + ":" + mPort +                      "/pks/lookup?op=get&options=mr&search=" + keyIdHex;              Log.d(Constants.TAG, "hkp keyserver get: " + query);              HttpGet get = new HttpGet(query); @@ -319,7 +366,7 @@ public class HkpKeyserver extends Keyserver {      public void add(String armoredKey) throws AddKeyException {          HttpClient client = new DefaultHttpClient();          try { -            String query = "http://" + mHost + ":" + mPort + "/pks/add"; +            String query = getUrlPrefix() + mHost + ":" + mPort + "/pks/add";              HttpPost post = new HttpPost(query);              Log.d(Constants.TAG, "hkp keyserver add: " + query);              List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); @@ -336,4 +383,36 @@ public class HkpKeyserver extends Keyserver {              client.getConnectionManager().shutdown();          }      } + +    @Override +    public String toString() { +        return mHost + ":" + mPort; +    } + +    /** +     * Tries to find a server responsible for a given domain +     * +     * @return A responsible Keyserver or null if not found. +     */ +    public static HkpKeyserver resolve(String domain) { +        try { +            Record[] records = new Lookup("_hkp._tcp." + domain, Type.SRV).run(); +            if (records.length > 0) { +                Arrays.sort(records, new Comparator<Record>() { +                    @Override +                    public int compare(Record lhs, Record rhs) { +                        if (!(lhs instanceof SRVRecord)) return 1; +                        if (!(rhs instanceof SRVRecord)) return -1; +                        return ((SRVRecord) lhs).getPriority() - ((SRVRecord) rhs).getPriority(); +                    } +                }); +                Record record = records[0]; // This is our best choice +                if (record instanceof SRVRecord) { +                    return new HkpKeyserver(((SRVRecord) record).getTarget().toString(), (short) ((SRVRecord) record).getPort()); +                } +            } +        } catch (Exception ignored) { +        } +        return null; +    }  } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/ImportKeysListEntry.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/ImportKeysListEntry.java index c43f72235..47265c3aa 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/ImportKeysListEntry.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/ImportKeysListEntry.java @@ -32,18 +32,20 @@ import java.util.Date;  public class ImportKeysListEntry implements Serializable, Parcelable {      private static final long serialVersionUID = -7797972103284992662L; -    public ArrayList<String> userIds; -    public long keyId; -    public String keyIdHex; -    public boolean revoked; -    public Date date; // TODO: not displayed -    public String fingerprintHex; -    public int bitStrength; -    public String algorithm; -    public boolean secretKey; -    public String mPrimaryUserId; +    private ArrayList<String> mUserIds; +    private long mKeyId; +    private String mKeyIdHex; +    private boolean mRevoked; +    private boolean mExpired; +    private Date mDate; // TODO: not displayed +    private String mFingerprintHex; +    private int mBitStrength; +    private String mAlgorithm; +    private boolean mSecretKey; +    private String mPrimaryUserId;      private String mExtraData;      private String mQuery; +    private String mOrigin;      private boolean mSelected; @@ -54,35 +56,39 @@ public class ImportKeysListEntry implements Serializable, Parcelable {      @Override      public void writeToParcel(Parcel dest, int flags) {          dest.writeString(mPrimaryUserId); -        dest.writeStringList(userIds); -        dest.writeLong(keyId); -        dest.writeByte((byte) (revoked ? 1 : 0)); -        dest.writeSerializable(date); -        dest.writeString(fingerprintHex); -        dest.writeString(keyIdHex); -        dest.writeInt(bitStrength); -        dest.writeString(algorithm); -        dest.writeByte((byte) (secretKey ? 1 : 0)); +        dest.writeStringList(mUserIds); +        dest.writeLong(mKeyId); +        dest.writeByte((byte) (mRevoked ? 1 : 0)); +        dest.writeByte((byte) (mExpired ? 1 : 0)); +        dest.writeSerializable(mDate); +        dest.writeString(mFingerprintHex); +        dest.writeString(mKeyIdHex); +        dest.writeInt(mBitStrength); +        dest.writeString(mAlgorithm); +        dest.writeByte((byte) (mSecretKey ? 1 : 0));          dest.writeByte((byte) (mSelected ? 1 : 0));          dest.writeString(mExtraData); +        dest.writeString(mOrigin);      }      public static final Creator<ImportKeysListEntry> CREATOR = new Creator<ImportKeysListEntry>() {          public ImportKeysListEntry createFromParcel(final Parcel source) {              ImportKeysListEntry vr = new ImportKeysListEntry();              vr.mPrimaryUserId = source.readString(); -            vr.userIds = new ArrayList<String>(); -            source.readStringList(vr.userIds); -            vr.keyId = source.readLong(); -            vr.revoked = source.readByte() == 1; -            vr.date = (Date) source.readSerializable(); -            vr.fingerprintHex = source.readString(); -            vr.keyIdHex = source.readString(); -            vr.bitStrength = source.readInt(); -            vr.algorithm = source.readString(); -            vr.secretKey = source.readByte() == 1; +            vr.mUserIds = new ArrayList<String>(); +            source.readStringList(vr.mUserIds); +            vr.mKeyId = source.readLong(); +            vr.mRevoked = source.readByte() == 1; +            vr.mExpired = source.readByte() == 1; +            vr.mDate = (Date) source.readSerializable(); +            vr.mFingerprintHex = source.readString(); +            vr.mKeyIdHex = source.readString(); +            vr.mBitStrength = source.readInt(); +            vr.mAlgorithm = source.readString(); +            vr.mSecretKey = source.readByte() == 1;              vr.mSelected = source.readByte() == 1;              vr.mExtraData = source.readString(); +            vr.mOrigin = source.readString();              return vr;          } @@ -93,7 +99,7 @@ public class ImportKeysListEntry implements Serializable, Parcelable {      };      public String getKeyIdHex() { -        return keyIdHex; +        return mKeyIdHex;      }      public boolean isSelected() { @@ -104,72 +110,80 @@ public class ImportKeysListEntry implements Serializable, Parcelable {          this.mSelected = selected;      } +    public boolean isExpired() { +        return mExpired; +    } + +    public void setExpired(boolean expired) { +        this.mExpired = expired; +    } +      public long getKeyId() { -        return keyId; +        return mKeyId;      }      public void setKeyId(long keyId) { -        this.keyId = keyId; +        this.mKeyId = keyId;      }      public void setKeyIdHex(String keyIdHex) { -        this.keyIdHex = keyIdHex; +        this.mKeyIdHex = keyIdHex;      }      public boolean isRevoked() { -        return revoked; +        return mRevoked;      }      public void setRevoked(boolean revoked) { -        this.revoked = revoked; +        this.mRevoked = revoked;      }      public Date getDate() { -        return date; +        return mDate;      }      public void setDate(Date date) { -        this.date = date; +        this.mDate = date;      }      public String getFingerprintHex() { -        return fingerprintHex; +        return mFingerprintHex;      }      public void setFingerprintHex(String fingerprintHex) { -        this.fingerprintHex = fingerprintHex; +        this.mFingerprintHex = fingerprintHex;      }      public int getBitStrength() { -        return bitStrength; +        return mBitStrength;      }      public void setBitStrength(int bitStrength) { -        this.bitStrength = bitStrength; +        this.mBitStrength = bitStrength;      }      public String getAlgorithm() { -        return algorithm; +        return mAlgorithm;      }      public void setAlgorithm(String algorithm) { -        this.algorithm = algorithm; +        this.mAlgorithm = algorithm;      }      public boolean isSecretKey() { -        return secretKey; +        return mSecretKey;      }      public void setSecretKey(boolean secretKey) { -        this.secretKey = secretKey; +        this.mSecretKey = secretKey;      }      public ArrayList<String> getUserIds() { -        return userIds; +        return mUserIds;      }      public void setUserIds(ArrayList<String> userIds) { -        this.userIds = userIds; +        this.mUserIds = userIds;      }      public String getPrimaryUserId() { @@ -196,15 +210,23 @@ public class ImportKeysListEntry implements Serializable, Parcelable {          mQuery = query;      } +    public String getOrigin() { +        return mOrigin; +    } + +    public void setOrigin(String origin) { +        mOrigin = origin; +    } +      /**       * Constructor for later querying from keyserver       */      public ImportKeysListEntry() {          // keys from keyserver are always public keys; from keybase too -        secretKey = false; +        mSecretKey = false;          // do not select by default          mSelected = false; -        userIds = new ArrayList<String>(); +        mUserIds = new ArrayList<String>();      }      /** @@ -215,24 +237,24 @@ public class ImportKeysListEntry implements Serializable, Parcelable {          // selected is default          this.mSelected = true; -        secretKey = ring.isSecret(); +        mSecretKey = ring.isSecret();          UncachedPublicKey key = ring.getPublicKey();          mPrimaryUserId = key.getPrimaryUserId(); -        userIds = key.getUnorderedUserIds(); +        mUserIds = key.getUnorderedUserIds();          // if there was no user id flagged as primary, use the first one          if (mPrimaryUserId == null) { -            mPrimaryUserId = userIds.get(0); +            mPrimaryUserId = mUserIds.get(0);          } -        this.keyId = key.getKeyId(); -        this.keyIdHex = PgpKeyHelper.convertKeyIdToHex(keyId); +        this.mKeyId = key.getKeyId(); +        this.mKeyIdHex = PgpKeyHelper.convertKeyIdToHex(mKeyId); -        this.revoked = key.maybeRevoked(); -        this.fingerprintHex = PgpKeyHelper.convertFingerprintToHex(key.getFingerprint()); -        this.bitStrength = key.getBitStrength(); +        this.mRevoked = key.isRevoked(); +        this.mFingerprintHex = PgpKeyHelper.convertFingerprintToHex(key.getFingerprint()); +        this.mBitStrength = key.getBitStrength();          final int algorithm = key.getAlgorithm(); -        this.algorithm = PgpKeyHelper.getAlgorithmInfo(context, algorithm); +        this.mAlgorithm = PgpKeyHelper.getAlgorithmInfo(context, algorithm);      }  } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyserver.java index f9b6abf18..43557279f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/KeybaseKeyserver.java @@ -31,6 +31,7 @@ import java.net.URLEncoder;  import java.util.ArrayList;  public class KeybaseKeyserver extends Keyserver { +    public static final String ORIGIN = "keybase:keybase.io";      private String mQuery;      @Override @@ -87,6 +88,7 @@ public class KeybaseKeyserver extends Keyserver {          final ImportKeysListEntry entry = new ImportKeysListEntry();          entry.setQuery(mQuery); +        entry.setOrigin(ORIGIN);          String keybaseId = JWalk.getString(match, "components", "username", "val");          String fullName = JWalk.getString(match, "components", "full_name", "val"); @@ -144,7 +146,8 @@ public class KeybaseKeyserver extends Keyserver {                  try {                      JSONObject json = new JSONObject(text);                      if (JWalk.getInt(json, "status", "code") != 0) { -                        throw new QueryFailedException("Keybase autocomplete search failed"); +                        throw new QueryFailedException("Keybase.io query failed: " + path + "?" + +                                query);                      }                      return json;                  } catch (JSONException e) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java index 868f543f0..842e7d922 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java @@ -48,12 +48,12 @@ public abstract class Keyserver {          private static final long serialVersionUID = -507574859137295530L;      } -    abstract List<ImportKeysListEntry> search(String query) throws QueryFailedException, +    public abstract List<ImportKeysListEntry> search(String query) throws QueryFailedException,              QueryNeedsRepairException; -    abstract String get(String keyIdHex) throws QueryFailedException; +    public abstract String get(String keyIdHex) throws QueryFailedException; -    abstract void add(String armoredKey) throws AddKeyException; +    public abstract void add(String armoredKey) throws AddKeyException;      public static String readAll(InputStream in, String encoding) throws IOException {          ByteArrayOutputStream raw = new ByteArrayOutputStream(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/ParcelableKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/ParcelableKeyRing.java index 5da6c4cd3..fdf561aaf 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/ParcelableKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/ParcelableKeyRing.java @@ -3,7 +3,7 @@ package org.sufficientlysecure.keychain.keyimport;  import android.os.Parcel;  import android.os.Parcelable; -/** This is a trivial wrapper around UncachedKeyRing which implements Parcelable. It exists +/** This is a trivial wrapper around keyring bytes which implements Parcelable. It exists   * for the sole purpose of keeping spongycastle and android imports in separate packages.   */  public class ParcelableKeyRing implements Parcelable { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java index 14ec67e64..e1967429a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java @@ -33,6 +33,9 @@ import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;  import org.sufficientlysecure.keychain.provider.KeychainContract;  import org.sufficientlysecure.keychain.provider.ProviderHelper;  import org.sufficientlysecure.keychain.service.KeychainIntentService; +import org.sufficientlysecure.keychain.service.OperationResultParcel.OperationLog; +import org.sufficientlysecure.keychain.service.OperationResults.ImportResult; +import org.sufficientlysecure.keychain.service.OperationResults.SaveKeyringResult;  import org.sufficientlysecure.keychain.util.Log;  import java.io.ByteArrayOutputStream; @@ -55,10 +58,6 @@ public class PgpImportExport {      private ProviderHelper mProviderHelper; -    public static final int RETURN_OK = 0; -    public static final int RETURN_BAD = -2; -    public static final int RETURN_UPDATED = 1; -      public PgpImportExport(Context context, Progressable progressable) {          super();          this.mContext = context; @@ -115,26 +114,20 @@ public class PgpImportExport {                  if (aos != null) {                      aos.close();                  } -                if (bos != null) { -                    bos.close(); -                } +                bos.close();              } catch (IOException e) { +                // this is just a finally thing, no matter if it doesn't work out.              }          }      } -    /** -     * Imports keys from given data. If keyIds is given only those are imported -     */ -    public Bundle importKeyRings(List<ParcelableKeyRing> entries) +    /** Imports keys from given data. If keyIds is given only those are imported */ +    public ImportResult importKeyRings(List<ParcelableKeyRing> entries)              throws PgpGeneralException, PGPException, IOException { -        Bundle returnData = new Bundle();          updateProgress(R.string.progress_importing, 0, 100); -        int newKeys = 0; -        int oldKeys = 0; -        int badKeys = 0; +        int newKeys = 0, oldKeys = 0, badKeys = 0;          int position = 0;          for (ParcelableKeyRing entry : entries) { @@ -152,14 +145,19 @@ public class PgpImportExport {                      }                  } -                mProviderHelper.savePublicKeyRing(key); -                /*switch(status) { -                    case RETURN_UPDATED: oldKeys++; break; -                    case RETURN_OK: newKeys++; break; -                    case RETURN_BAD: badKeys++; break; -                }*/ -                // TODO proper import feedback -                newKeys += 1; +                SaveKeyringResult result; +                if (key.isSecret()) { +                    result = mProviderHelper.saveSecretKeyRing(key); +                } else { +                    result = mProviderHelper.savePublicKeyRing(key); +                } +                if (!result.success()) { +                    badKeys += 1; +                } else if (result.updated()) { +                    oldKeys += 1; +                } else { +                    newKeys += 1; +                }              } catch (PgpGeneralException e) {                  Log.e(Constants.TAG, "Encountered bad key on import!", e); @@ -170,11 +168,31 @@ public class PgpImportExport {              updateProgress(position / entries.size() * 100, 100);          } -        returnData.putInt(KeychainIntentService.RESULT_IMPORT_ADDED, newKeys); -        returnData.putInt(KeychainIntentService.RESULT_IMPORT_UPDATED, oldKeys); -        returnData.putInt(KeychainIntentService.RESULT_IMPORT_BAD, badKeys); +        OperationLog log = mProviderHelper.getLog(); +        int resultType = 0; +        // special return case: no new keys at all +        if (badKeys == 0 && newKeys == 0 && oldKeys == 0) { +            resultType = ImportResult.RESULT_FAIL_NOTHING; +        } else { +            if (newKeys > 0) { +                resultType |= ImportResult.RESULT_OK_NEWKEYS; +            } +            if (oldKeys > 0) { +                resultType |= ImportResult.RESULT_OK_UPDATED; +            } +            if (badKeys > 0) { +                resultType |= ImportResult.RESULT_WITH_ERRORS; +                if (newKeys == 0 && oldKeys == 0) { +                    resultType |= ImportResult.RESULT_ERROR; +                } +            } +            if (log.containsWarnings()) { +                resultType |= ImportResult.RESULT_WITH_WARNINGS; +            } +        } + +        return new ImportResult(resultType, log, newKeys, oldKeys, badKeys); -        return returnData;      }      public Bundle exportKeyRings(ArrayList<Long> publicKeyRingMasterIds, diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java index 02e5411ca..e309ed632 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -2,14 +2,23 @@ package org.sufficientlysecure.keychain.pgp;  import org.spongycastle.bcpg.ArmoredOutputStream;  import org.spongycastle.bcpg.S2K; +import org.spongycastle.bcpg.SignatureSubpacketTags; +import org.spongycastle.bcpg.sig.KeyFlags; +import org.spongycastle.openpgp.PGPKeyFlags;  import org.spongycastle.openpgp.PGPKeyRing;  import org.spongycastle.openpgp.PGPObjectFactory;  import org.spongycastle.openpgp.PGPPublicKey; +import org.spongycastle.openpgp.PGPPublicKeyRing;  import org.spongycastle.openpgp.PGPSecretKey;  import org.spongycastle.openpgp.PGPSecretKeyRing; +import org.spongycastle.openpgp.PGPSignature; +import org.spongycastle.openpgp.PGPSignatureList;  import org.spongycastle.openpgp.PGPUtil;  import org.sufficientlysecure.keychain.Constants;  import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.service.OperationResultParcel.OperationLog; +import org.sufficientlysecure.keychain.service.OperationResultParcel.LogLevel; +import org.sufficientlysecure.keychain.service.OperationResultParcel.LogType;  import org.sufficientlysecure.keychain.util.IterableIterator;  import org.sufficientlysecure.keychain.util.Log; @@ -18,7 +27,8 @@ import java.io.ByteArrayInputStream;  import java.io.IOException;  import java.io.InputStream;  import java.io.OutputStream; -import java.util.ArrayList; +import java.util.Date; +import java.util.HashSet;  import java.util.Iterator;  import java.util.List;  import java.util.Vector; @@ -149,13 +159,13 @@ public class UncachedKeyRing {          aos.close();      } -    public ArrayList<Long> getAvailableSubkeys() { +    public HashSet<Long> getAvailableSubkeys() {          if(!isSecret()) {              throw new RuntimeException("Tried to find available subkeys from non-secret keys. " +                      "This is a programming error and should never happen!");          } -        ArrayList<Long> result = new ArrayList<Long>(); +        HashSet<Long> result = new HashSet<Long>();          // then, mark exactly the keys we have available          for (PGPSecretKey sub : new IterableIterator<PGPSecretKey>(                  ((PGPSecretKeyRing) mRing).getSecretKeys())) { @@ -168,4 +178,411 @@ public class UncachedKeyRing {          return result;      } +    /** "Canonicalizes" a key, removing inconsistencies in the process. This operation can be +     * applied to public keyrings only. +     * +     * More specifically: +     *  - Remove all non-verifying self-certificates +     *  - Remove all "future" self-certificates +     *  - Remove all certificates flagged as "local" +     *  - Remove all certificates which are superseded by a newer one on the same target +     * +     * After this cleaning, a number of checks are done: TODO implement +     *  - See if each subkey retains a valid self certificate +     *  - See if each user id retains a valid self certificate +     * +     * This operation writes an OperationLog which can be used as part of a OperationResultParcel. +     * +     * @return A canonicalized key +     * +     */ +    public UncachedKeyRing canonicalize(OperationLog log, int indent) { +        if (isSecret()) { +            throw new RuntimeException("Tried to canonicalize non-secret keyring. " + +                    "This is a programming error and should never happen!"); +        } + +        log.add(LogLevel.START, LogType.MSG_KC, +                new String[]{PgpKeyHelper.convertKeyIdToHex(getMasterKeyId())}, indent); +        indent += 1; + +        final Date now = new Date(); + +        int removedCerts = 0; + +        PGPPublicKeyRing ring = (PGPPublicKeyRing) mRing; +        PGPPublicKey masterKey = mRing.getPublicKey(); +        final long masterKeyId = masterKey.getKeyID(); + +        { +            log.add(LogLevel.DEBUG, LogType.MSG_KC_MASTER, +                    new String[]{PgpKeyHelper.convertKeyIdToHex(masterKey.getKeyID())}, indent); +            indent += 1; + +            PGPPublicKey modified = masterKey; +            PGPSignature revocation = null; +            for (PGPSignature zert : new IterableIterator<PGPSignature>(masterKey.getSignatures())) { +                int type = zert.getSignatureType(); + +                // Disregard certifications on user ids, we will deal with those later +                if (type == PGPSignature.NO_CERTIFICATION +                        || type == PGPSignature.DEFAULT_CERTIFICATION +                        || type == PGPSignature.CASUAL_CERTIFICATION +                        || type == PGPSignature.POSITIVE_CERTIFICATION +                        || type == PGPSignature.CERTIFICATION_REVOCATION) { +                    continue; +                } +                WrappedSignature cert = new WrappedSignature(zert); + +                if (type != PGPSignature.KEY_REVOCATION) { +                    // Unknown type, just remove +                    log.add(LogLevel.WARN, LogType.MSG_KC_REVOKE_BAD_TYPE, new String[]{ +                            "0x" + Integer.toString(type, 16) +                    }, indent); +                    modified = PGPPublicKey.removeCertification(modified, zert); +                    removedCerts += 1; +                    continue; +                } + +                if (cert.getCreationTime().after(now)) { +                    // Creation date in the future? No way! +                    log.add(LogLevel.WARN, LogType.MSG_KC_REVOKE_BAD_TIME, null, indent); +                    modified = PGPPublicKey.removeCertification(modified, zert); +                    removedCerts += 1; +                    continue; +                } + +                if (cert.isLocal()) { +                    // Creation date in the future? No way! +                    log.add(LogLevel.WARN, LogType.MSG_KC_REVOKE_BAD_LOCAL, null, indent); +                    modified = PGPPublicKey.removeCertification(modified, zert); +                    removedCerts += 1; +                    continue; +                } + +                try { +                    cert.init(masterKey); +                    if (!cert.verifySignature(masterKey)) { +                        log.add(LogLevel.WARN, LogType.MSG_KC_REVOKE_BAD, null, indent); +                        modified = PGPPublicKey.removeCertification(modified, zert); +                        removedCerts += 1; +                        continue; +                    } +                } catch (PgpGeneralException e) { +                    log.add(LogLevel.WARN, LogType.MSG_KC_REVOKE_BAD_ERR, null, indent); +                    modified = PGPPublicKey.removeCertification(modified, zert); +                    removedCerts += 1; +                    continue; +                } + +                // first revocation? fine then. +                if (revocation == null) { +                    revocation = zert; +                    // more revocations? at least one is superfluous, then. +                } else if (revocation.getCreationTime().before(zert.getCreationTime())) { +                    modified = PGPPublicKey.removeCertification(modified, revocation); +                    removedCerts += 1; +                    log.add(LogLevel.INFO, LogType.MSG_KC_REVOKE_DUP, null, indent); +                    revocation = zert; +                } else { +                    modified = PGPPublicKey.removeCertification(modified, zert); +                    removedCerts += 1; +                    log.add(LogLevel.INFO, LogType.MSG_KC_REVOKE_DUP, null, indent); +                } +            } + +            for (String userId : new IterableIterator<String>(masterKey.getUserIDs())) { +                PGPSignature selfCert = null; +                revocation = null; + +                // look through signatures for this specific key +                for (PGPSignature zert : new IterableIterator<PGPSignature>( +                        masterKey.getSignaturesForID(userId))) { +                    WrappedSignature cert = new WrappedSignature(zert); +                    long certId = cert.getKeyId(); + +                    int type = zert.getSignatureType(); +                    if (type != PGPSignature.DEFAULT_CERTIFICATION +                            && type != PGPSignature.NO_CERTIFICATION +                            && type != PGPSignature.CASUAL_CERTIFICATION +                            && type != PGPSignature.POSITIVE_CERTIFICATION +                            && type != PGPSignature.CERTIFICATION_REVOCATION) { +                        log.add(LogLevel.WARN, LogType.MSG_KC_UID_BAD_TYPE, +                                new String[] { +                                        "0x" + Integer.toString(zert.getSignatureType(), 16) +                                }, indent); +                        modified = PGPPublicKey.removeCertification(modified, userId, zert); +                        removedCerts += 1; +                    } + +                    if (cert.getCreationTime().after(now)) { +                        // Creation date in the future? No way! +                        log.add(LogLevel.WARN, LogType.MSG_KC_REVOKE_BAD_TIME, null, indent); +                        modified = PGPPublicKey.removeCertification(modified, zert); +                        removedCerts += 1; +                        continue; +                    } + +                    if (cert.isLocal()) { +                        // Creation date in the future? No way! +                        log.add(LogLevel.WARN, LogType.MSG_KC_REVOKE_BAD_LOCAL, null, indent); +                        modified = PGPPublicKey.removeCertification(modified, zert); +                        removedCerts += 1; +                        continue; +                    } + +                    // If this is a foreign signature, never mind any further +                    if (certId != masterKeyId) { +                        continue; +                    } + +                    // Otherwise, first make sure it checks out +                    try { +                        cert.init(masterKey); +                        if (!cert.verifySignature(masterKey, userId)) { +                            log.add(LogLevel.WARN, LogType.MSG_KC_UID_BAD, +                                    new String[] { userId }, indent); +                            modified = PGPPublicKey.removeCertification(modified, userId, zert); +                            removedCerts += 1; +                            continue; +                        } +                    } catch (PgpGeneralException e) { +                        log.add(LogLevel.WARN, LogType.MSG_KC_UID_BAD_ERR, +                                new String[] { userId }, indent); +                        modified = PGPPublicKey.removeCertification(modified, userId, zert); +                        removedCerts += 1; +                        continue; +                    } + +                    switch (type) { +                        case PGPSignature.DEFAULT_CERTIFICATION: +                        case PGPSignature.NO_CERTIFICATION: +                        case PGPSignature.CASUAL_CERTIFICATION: +                        case PGPSignature.POSITIVE_CERTIFICATION: +                            if (selfCert == null) { +                                selfCert = zert; +                            } else if (selfCert.getCreationTime().before(cert.getCreationTime())) { +                                modified = PGPPublicKey.removeCertification(modified, userId, selfCert); +                                removedCerts += 1; +                                log.add(LogLevel.INFO, LogType.MSG_KC_UID_DUP, +                                        new String[] { userId }, indent); +                                selfCert = zert; +                            } else { +                                modified = PGPPublicKey.removeCertification(modified, userId, zert); +                                removedCerts += 1; +                                log.add(LogLevel.INFO, LogType.MSG_KC_UID_DUP, +                                        new String[] { userId }, indent); +                            } +                            // If there is a revocation certificate, and it's older than this, drop it +                            if (revocation != null +                                    && revocation.getCreationTime().before(selfCert.getCreationTime())) { +                                modified = PGPPublicKey.removeCertification(modified, userId, revocation); +                                revocation = null; +                                removedCerts += 1; +                                log.add(LogLevel.INFO, LogType.MSG_KC_UID_REVOKE_OLD, +                                        new String[] { userId }, indent); +                            } +                            break; + +                        case PGPSignature.CERTIFICATION_REVOCATION: +                            // If this is older than the (latest) self cert, drop it +                            if (selfCert != null && selfCert.getCreationTime().after(zert.getCreationTime())) { +                                modified = PGPPublicKey.removeCertification(modified, userId, zert); +                                removedCerts += 1; +                                log.add(LogLevel.INFO, LogType.MSG_KC_UID_REVOKE_OLD, +                                        new String[] { userId }, indent); +                                continue; +                            } +                            // first revocation? remember it. +                            if (revocation == null) { +                                revocation = zert; +                                // more revocations? at least one is superfluous, then. +                            } else if (revocation.getCreationTime().before(cert.getCreationTime())) { +                                modified = PGPPublicKey.removeCertification(modified, userId, revocation); +                                removedCerts += 1; +                                log.add(LogLevel.INFO, LogType.MSG_KC_UID_REVOKE_DUP, +                                        new String[] { userId }, indent); +                                revocation = zert; +                            } else { +                                modified = PGPPublicKey.removeCertification(modified, userId, zert); +                                removedCerts += 1; +                                log.add(LogLevel.INFO, LogType.MSG_KC_UID_REVOKE_DUP, +                                        new String[] { userId }, indent); +                            } +                            break; + +                    } + +                } +            } + +            // Replace modified key in the keyring +            ring = PGPPublicKeyRing.insertPublicKey(ring, modified); + +            log.add(LogLevel.DEBUG, LogType.MSG_KC_MASTER_SUCCESS, null, indent); +            indent -= 1; + +        } + +        // Process all keys +        for (PGPPublicKey key : new IterableIterator<PGPPublicKey>(ring.getPublicKeys())) { +            // Don't care about the master key here, that one gets special treatment above +            if (key.isMasterKey()) { +                continue; +            } +            log.add(LogLevel.DEBUG, LogType.MSG_KC_SUB, +                    new String[]{PgpKeyHelper.convertKeyIdToHex(key.getKeyID())}, indent); +            indent += 1; +            // A subkey needs exactly one subkey binding certificate, and optionally one revocation +            // certificate. +            PGPPublicKey modified = key; +            PGPSignature selfCert = null, revocation = null; +            uids: for (PGPSignature zig : new IterableIterator<PGPSignature>(key.getSignatures())) { +                // remove from keyring (for now) +                modified = PGPPublicKey.removeCertification(modified, zig); +                // add this too, easier than adding it for every single "continue" case +                removedCerts += 1; + +                WrappedSignature cert = new WrappedSignature(zig); +                int type = cert.getSignatureType(); + +                // filter out bad key types... +                if (cert.getKeyId() != masterKey.getKeyID()) { +                    log.add(LogLevel.WARN, LogType.MSG_KC_SUB_BAD_KEYID, null, indent); +                    continue; +                } + +                if (type != PGPSignature.SUBKEY_BINDING && type != PGPSignature.SUBKEY_REVOCATION) { +                    log.add(LogLevel.WARN, LogType.MSG_KC_SUB_BAD_TYPE, new String[]{ +                            "0x" + Integer.toString(type, 16) +                    }, indent); +                    continue; +                } + +                if (cert.getCreationTime().after(now)) { +                    // Creation date in the future? No way! +                    log.add(LogLevel.WARN, LogType.MSG_KC_SUB_BAD_TIME, null, indent); +                    continue; +                } + +                if (cert.isLocal()) { +                    // Creation date in the future? No way! +                    log.add(LogLevel.WARN, LogType.MSG_KC_SUB_BAD_LOCAL, null, indent); +                    continue; +                } + +                if (type == PGPSignature.SUBKEY_BINDING) { + +                    // make sure the certificate checks out +                    try { +                        cert.init(masterKey); +                        if (!cert.verifySignature(masterKey, key)) { +                            log.add(LogLevel.WARN, LogType.MSG_KC_SUB_BAD, null, indent); +                            continue; +                        } +                    } catch (PgpGeneralException e) { +                        log.add(LogLevel.WARN, LogType.MSG_KC_SUB_BAD_ERR, null, indent); +                        continue; +                    } + +                    if (zig.getHashedSubPackets().hasSubpacket(SignatureSubpacketTags.KEY_FLAGS)) { +                        int flags = ((KeyFlags) zig.getHashedSubPackets() +                                .getSubpacket(SignatureSubpacketTags.KEY_FLAGS)).getFlags(); +                        // If this subkey is allowed to sign data, +                        if ((flags & PGPKeyFlags.CAN_SIGN) == PGPKeyFlags.CAN_SIGN) { +                            try { +                                PGPSignatureList list = zig.getUnhashedSubPackets().getEmbeddedSignatures(); +                                boolean ok = false; +                                for (int i = 0; i < list.size(); i++) { +                                    WrappedSignature subsig = new WrappedSignature(list.get(i)); +                                    if (subsig.getSignatureType() == PGPSignature.PRIMARYKEY_BINDING) { +                                        subsig.init(key); +                                        if (subsig.verifySignature(masterKey, key)) { +                                            ok = true; +                                        } else { +                                            log.add(LogLevel.WARN, LogType.MSG_KC_SUB_PRIMARY_BAD, null, indent); +                                            continue uids; +                                        } +                                    } +                                } +                                if (!ok) { +                                    log.add(LogLevel.WARN, LogType.MSG_KC_SUB_PRIMARY_NONE, null, indent); +                                    continue; +                                } +                            } catch (Exception e) { +                                log.add(LogLevel.WARN, LogType.MSG_KC_SUB_PRIMARY_BAD_ERR, null, indent); +                                continue; +                            } +                        } +                    } + +                    // if we already have a cert, and this one is not newer: skip it +                    if (selfCert != null && selfCert.getCreationTime().before(cert.getCreationTime())) { +                        continue; +                    } + +                    selfCert = zig; +                    // if this is newer than a possibly existing revocation, drop that one +                    if (revocation != null && selfCert.getCreationTime().after(revocation.getCreationTime())) { +                        revocation = null; +                    } + +                // it must be a revocation, then (we made sure above) +                } else { + +                    // make sure the certificate checks out +                    try { +                        cert.init(masterKey); +                        if (!cert.verifySignature(key)) { +                            log.add(LogLevel.WARN, LogType.MSG_KC_SUB_REVOKE_BAD, null, indent); +                            continue; +                        } +                    } catch (PgpGeneralException e) { +                        log.add(LogLevel.WARN, LogType.MSG_KC_SUB_REVOKE_BAD_ERR, null, indent); +                        continue; +                    } + +                    // if there is no binding (yet), or the revocation is newer than the binding: keep it +                    if (selfCert == null || selfCert.getCreationTime().before(cert.getCreationTime())) { +                        revocation = zig; +                    } +                } +            } + +            // it is not properly bound? error! +            if (selfCert == null) { +                ring = PGPPublicKeyRing.removePublicKey(ring, modified); + +                log.add(LogLevel.ERROR, LogType.MSG_KC_SUB_NO_CERT, +                        new String[]{PgpKeyHelper.convertKeyIdToHex(key.getKeyID())}, indent); +                indent -= 1; +                continue; +            } + +            // re-add certification +            modified = PGPPublicKey.addCertification(modified, selfCert); +            removedCerts -= 1; +            // add revocation, if any +            if (revocation != null) { +                modified = PGPPublicKey.addCertification(modified, revocation); +                removedCerts -= 1; +            } +            // replace pubkey in keyring +            ring = PGPPublicKeyRing.insertPublicKey(ring, modified); + +            log.add(LogLevel.DEBUG, LogType.MSG_KC_SUB_SUCCESS, null, indent); +            indent -= 1; +        } + +        if (removedCerts > 0) { +            log.add(LogLevel.OK, LogType.MSG_KC_SUCCESS_REMOVED, +                    new String[] { Integer.toString(removedCerts) }, indent); +        } else { +            log.add(LogLevel.OK, LogType.MSG_KC_SUCCESS, null, indent); +        } + +        return new UncachedKeyRing(ring); +    } + +  } 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 108c8c8c3..33db7771b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java @@ -2,6 +2,7 @@ package org.sufficientlysecure.keychain.pgp;  import org.spongycastle.bcpg.SignatureSubpacketTags;  import org.spongycastle.bcpg.sig.KeyFlags; +import org.spongycastle.openpgp.PGPException;  import org.spongycastle.openpgp.PGPPublicKey;  import org.spongycastle.openpgp.PGPSignature;  import org.spongycastle.openpgp.PGPSignatureSubpacketVector; @@ -9,6 +10,7 @@ import org.spongycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProv  import org.sufficientlysecure.keychain.Constants;  import org.sufficientlysecure.keychain.util.IterableIterator; +import java.security.SignatureException;  import java.util.ArrayList;  import java.util.Calendar;  import java.util.Date; @@ -28,8 +30,13 @@ public class UncachedPublicKey {      }      /** The revocation signature is NOT checked here, so this may be false! */ -    public boolean maybeRevoked() { -        return mPublicKey.isRevoked(); +    public boolean isRevoked() { +        for (PGPSignature sig : new IterableIterator<PGPSignature>( +                mPublicKey.getSignaturesOfType(isMasterKey() ? PGPSignature.KEY_REVOCATION +                                                             : PGPSignature.SUBKEY_REVOCATION))) { +            return true; +        } +        return false;      }      public Date getCreationTime() { @@ -193,4 +200,5 @@ public class UncachedPublicKey {              }          };      } +  } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java index 1b7a5e8ba..be7f960a9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java @@ -35,7 +35,7 @@ public class WrappedSignature {      final PGPSignature mSig; -    protected WrappedSignature(PGPSignature sig) { +    WrappedSignature(PGPSignature sig) {          mSig = sig;      } @@ -88,7 +88,7 @@ public class WrappedSignature {          init(key.getPublicKey());      } -    protected void init(PGPPublicKey key) throws PgpGeneralException { +    void init(PGPPublicKey key) throws PgpGeneralException {          try {              JcaPGPContentVerifierBuilderProvider contentVerifierBuilderProvider =                      new JcaPGPContentVerifierBuilderProvider() @@ -125,7 +125,27 @@ public class WrappedSignature {          }      } -    protected boolean verifySignature(PGPPublicKey key, String uid) throws PgpGeneralException { +    boolean verifySignature(PGPPublicKey key) throws PgpGeneralException { +        try { +            return mSig.verifyCertification(key); +        } catch (SignatureException e) { +            throw new PgpGeneralException("Sign!", e); +        } catch (PGPException e) { +            throw new PgpGeneralException("Error!", e); +        } +    } + +    boolean verifySignature(PGPPublicKey masterKey, PGPPublicKey subKey) throws PgpGeneralException { +        try { +            return mSig.verifyCertification(masterKey, subKey); +        } catch (SignatureException e) { +            throw new PgpGeneralException("Sign!", e); +        } catch (PGPException e) { +            throw new PgpGeneralException("Error!", e); +        } +    } + +    boolean verifySignature(PGPPublicKey key, String uid) throws PgpGeneralException {          try {              return mSig.verifyCertification(uid, key);          } catch (SignatureException e) { @@ -158,4 +178,11 @@ public class WrappedSignature {          return new WrappedSignature(signatures.get(0));      } +    public boolean isLocal() { +        if (!mSig.getHashedSubPackets().hasSubpacket(SignatureSubpacketTags.EXPORTABLE)) { +            return false; +        } +        SignatureSubpacket p = mSig.getHashedSubPackets().getSubpacket(SignatureSubpacketTags.EXPORTABLE); +        return p.getData()[0] == 0; +    }  } 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 ca7e622bb..102c8e6d0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -29,6 +29,10 @@ import android.support.v4.util.LongSparseArray;  import org.sufficientlysecure.keychain.Constants;  import org.sufficientlysecure.keychain.pgp.KeyRing; +import org.sufficientlysecure.keychain.pgp.WrappedPublicKey; +import org.sufficientlysecure.keychain.service.OperationResultParcel.LogType; +import org.sufficientlysecure.keychain.service.OperationResultParcel.LogLevel; +import org.sufficientlysecure.keychain.service.OperationResultParcel.OperationLog;  import org.sufficientlysecure.keychain.pgp.PgpHelper;  import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;  import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; @@ -45,6 +49,7 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.Keys;  import org.sufficientlysecure.keychain.provider.KeychainContract.UserIds;  import org.sufficientlysecure.keychain.remote.AccountSettings;  import org.sufficientlysecure.keychain.remote.AppSettings; +import org.sufficientlysecure.keychain.service.OperationResults.SaveKeyringResult;  import org.sufficientlysecure.keychain.util.IterableIterator;  import org.sufficientlysecure.keychain.util.Log; @@ -58,13 +63,43 @@ import java.util.HashSet;  import java.util.List;  import java.util.Set; +/** This class contains high level methods for database access. Despite its + * name, it is not only a helper but actually the main interface for all + * synchronous database operations. + * + * Operations in this class write logs (TODO). These can be obtained from the + * OperationResultParcel return values directly, but are also accumulated over + * the lifetime of the executing ProviderHelper object unless the resetLog() + * method is called to start a new one specifically. + * + */  public class ProviderHelper { -    private Context mContext; -    private ContentResolver mContentResolver; +    private final Context mContext; +    private final ContentResolver mContentResolver; +    private OperationLog mLog; +    private int mIndent;      public ProviderHelper(Context context) { -        this.mContext = context; -        this.mContentResolver = context.getContentResolver(); +        this(context, new OperationLog(), 0); +    } + +    public ProviderHelper(Context context, OperationLog log, int indent) { +        mContext = context; +        mContentResolver = context.getContentResolver(); +        mLog = log; +        mIndent = indent; +    } + +    public void resetLog() { +        if(mLog != null) { +            // Start a new log (leaving the old one intact) +            mLog = new OperationLog(); +            mIndent = 0; +        } +    } + +    public OperationLog getLog() { +        return mLog;      }      public static class NotFoundException extends Exception { @@ -76,6 +111,17 @@ public class ProviderHelper {          }      } +    public void log(LogLevel level, LogType type) { +        if(mLog != null) { +            mLog.add(level, type, null, mIndent); +        } +    } +    public void log(LogLevel level, LogType type, String[] parameters) { +        if(mLog != null) { +            mLog.add(level, type, parameters, mIndent); +        } +    } +      // If we ever switch to api level 11, we can ditch this whole mess!      public static final int FIELD_TYPE_NULL = 1;      // this is called integer to stay coherent with the constants in Cursor (api level 11) @@ -126,36 +172,31 @@ public class ProviderHelper {          }      } -    public Object getUnifiedData(long masterKeyId, String column, int type) -            throws NotFoundException { -        return getUnifiedData(masterKeyId, new String[]{column}, new int[]{type}).get(column); -    } -      public HashMap<String, Object> getUnifiedData(long masterKeyId, String[] proj, int[] types)              throws NotFoundException {          return getGenericData(KeyRings.buildUnifiedKeyRingUri(masterKeyId), proj, types);      } -    private LongSparseArray<UncachedPublicKey> getUncachedMasterKeys(Uri queryUri) { -        Cursor cursor = mContentResolver.query(queryUri, -                new String[]{KeyRingData.MASTER_KEY_ID, KeyRingData.KEY_RING_DATA}, -                null, null, null); +    private LongSparseArray<WrappedPublicKey> getTrustedMasterKeys() { +        Cursor cursor = mContentResolver.query(KeyRings.buildUnifiedKeyRingsUri(), new String[] { +                KeyRings.MASTER_KEY_ID, +                // we pick from cache only information that is not easily available from keyrings +                KeyRings.HAS_ANY_SECRET, KeyRings.VERIFIED, +                // and of course, ring data +                KeyRings.PUBKEY_DATA +            }, KeyRings.HAS_ANY_SECRET + " = 1", null, null); -        LongSparseArray<UncachedPublicKey> result = -                new LongSparseArray<UncachedPublicKey>(cursor.getCount()); +        LongSparseArray<WrappedPublicKey> result = +                new LongSparseArray<WrappedPublicKey>(cursor.getCount());          try {              if (cursor != null && cursor.moveToFirst()) do {                  long masterKeyId = cursor.getLong(0); -                byte[] data = cursor.getBlob(1); -                if (data != null) { -                    try { -                        result.put(masterKeyId, -                                UncachedKeyRing.decodeFromData(data).getPublicKey()); -                    } catch(PgpGeneralException e) { -                        Log.e(Constants.TAG, "Error parsing keyring, skipping " + masterKeyId, e); -                    } catch(IOException e) { -                        Log.e(Constants.TAG, "IO error, skipping keyring" + masterKeyId, e); -                    } +                boolean hasAnySecret = cursor.getInt(1) > 0; +                int verified = cursor.getInt(2); +                byte[] blob = cursor.getBlob(3); +                if (blob != null) { +                    result.put(masterKeyId, +                            new WrappedPublicKeyRing(blob, hasAnySecret, verified).getSubkey());                  }              } while (cursor.moveToNext());          } finally { @@ -206,7 +247,7 @@ public class ProviderHelper {                      throw new NotFoundException("Secret key not available!");                  }                  return secret -                        ? new WrappedSecretKeyRing(blob, hasAnySecret, verified) +                        ? new WrappedSecretKeyRing(blob, true, verified)                          : new WrappedPublicKeyRing(blob, hasAnySecret, verified);              } else {                  throw new NotFoundException("Key not found!"); @@ -222,134 +263,269 @@ public class ProviderHelper {       * Saves PGPPublicKeyRing with its keys and userIds in DB       */      @SuppressWarnings("unchecked") -    public void savePublicKeyRing(UncachedKeyRing keyRing) throws IOException { +    public SaveKeyringResult savePublicKeyRing(UncachedKeyRing keyRing) {          if (keyRing.isSecret()) { -            throw new RuntimeException("Tried to save secret keyring as public! " + -                    "This is a bug, please file a bug report."); +            log(LogLevel.ERROR, LogType.MSG_IP_BAD_TYPE_SECRET); +            return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);          } +        // start with ok result +        int result = SaveKeyringResult.SAVED_PUBLIC; + +        long masterKeyId = keyRing.getMasterKeyId(); +        log(LogLevel.START, LogType.MSG_IP, +                new String[]{ PgpKeyHelper.convertKeyIdToHex(masterKeyId) }); +        mIndent += 1; + +        // Canonicalize this key, to assert a number of assumptions made about it. +        keyRing = keyRing.canonicalize(mLog, mIndent); +          UncachedPublicKey masterKey = keyRing.getPublicKey(); -        long masterKeyId = masterKey.getKeyId();          // IF there is a secret key, preserve it! -        UncachedKeyRing secretRing = null; +        UncachedKeyRing secretRing;          try {              secretRing = getWrappedSecretKeyRing(masterKeyId).getUncached(); +            log(LogLevel.DEBUG, LogType.MSG_IP_PRESERVING_SECRET);          } catch (NotFoundException e) { -            Log.e(Constants.TAG, "key not found!"); +            secretRing = null;          } -        // delete old version of this keyRing, which also deletes all keys and userIds on cascade +        ArrayList<ContentProviderOperation> operations;          try { -            mContentResolver.delete(KeyRingData.buildPublicKeyRingUri(Long.toString(masterKeyId)), null, null); -        } catch (UnsupportedOperationException e) { -            Log.e(Constants.TAG, "Key could not be deleted! Maybe we are creating a new one!", e); -        } -        // insert new version of this keyRing -        ContentValues values = new ContentValues(); -        values.put(KeyRingData.MASTER_KEY_ID, masterKeyId); -        values.put(KeyRingData.KEY_RING_DATA, keyRing.getEncoded()); -        Uri uri = KeyRingData.buildPublicKeyRingUri(Long.toString(masterKeyId)); -        mContentResolver.insert(uri, values); - -        // save all keys and userIds included in keyRing object in database -        ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); - -        int rank = 0; -        for (UncachedPublicKey key : new IterableIterator<UncachedPublicKey>(keyRing.getPublicKeys())) { -            operations.add(buildPublicKeyOperations(masterKeyId, key, rank)); -            ++rank; -        } +            log(LogLevel.DEBUG, LogType.MSG_IP_PREPARE); +            mIndent += 1; -        // get a list of owned secret keys, for verification filtering -        LongSparseArray<UncachedPublicKey> allKeyRings = -                getUncachedMasterKeys(KeyRingData.buildSecretKeyRingUri()); -        // special case: available secret keys verify themselves! -        if (secretRing != null) { -            allKeyRings.put(secretRing.getMasterKeyId(), secretRing.getPublicKey()); -        } +            // save all keys and userIds included in keyRing object in database +            operations = new ArrayList<ContentProviderOperation>(); -        // classify and order user ids. primary are moved to the front, revoked to the back, -        // otherwise the order in the keyfile is preserved. -        List<UserIdItem> uids = new ArrayList<UserIdItem>(); +            log(LogLevel.INFO, LogType.MSG_IP_INSERT_KEYRING); +            { // insert keyring +                // insert new version of this keyRing +                ContentValues values = new ContentValues(); +                values.put(KeyRingData.MASTER_KEY_ID, masterKeyId); +                try { +                    values.put(KeyRingData.KEY_RING_DATA, keyRing.getEncoded()); +                } catch (IOException e) { +                    log(LogLevel.ERROR, LogType.MSG_IP_ENCODE_FAIL); +                    return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); +                } -        for (String userId : new IterableIterator<String>( -                masterKey.getUnorderedUserIds().iterator())) { -            UserIdItem item = new UserIdItem(); -            uids.add(item); -            item.userId = userId; +                Uri uri = KeyRingData.buildPublicKeyRingUri(Long.toString(masterKeyId)); +                operations.add(ContentProviderOperation.newInsert(uri).withValues(values).build()); +            } -            // look through signatures for this specific key -            for (WrappedSignature cert : new IterableIterator<WrappedSignature>( -                    masterKey.getSignaturesForId(userId))) { -                long certId = cert.getKeyId(); -                try { -                    // self signature -                    if (certId == masterKeyId) { -                        cert.init(masterKey); -                        if (!cert.verifySignature(masterKey, userId)) { -                            // not verified?! dang! TODO notify user? this is kinda serious... -                            Log.e(Constants.TAG, "Could not verify self signature for " + userId + "!"); -                            continue; +            log(LogLevel.INFO, LogType.MSG_IP_INSERT_SUBKEYS); +            mIndent += 1; +            { // insert subkeys +                Uri uri = Keys.buildKeysUri(Long.toString(masterKeyId)); +                int rank = 0; +                for (UncachedPublicKey key : new IterableIterator<UncachedPublicKey>(keyRing.getPublicKeys())) { +                    log(LogLevel.DEBUG, LogType.MSG_IP_SUBKEY, new String[]{ +                            PgpKeyHelper.convertKeyIdToHex(key.getKeyId()) +                    }); +                    mIndent += 1; + +                    ContentValues values = new ContentValues(); +                    values.put(Keys.MASTER_KEY_ID, masterKeyId); +                    values.put(Keys.RANK, rank); + +                    values.put(Keys.KEY_ID, key.getKeyId()); +                    values.put(Keys.KEY_SIZE, key.getBitStrength()); +                    values.put(Keys.ALGORITHM, key.getAlgorithm()); +                    values.put(Keys.FINGERPRINT, key.getFingerprint()); + +                    boolean c = key.canCertify(), e = key.canEncrypt(), s = key.canSign(); +                    values.put(Keys.CAN_CERTIFY, c); +                    values.put(Keys.CAN_ENCRYPT, e); +                    values.put(Keys.CAN_SIGN, s); +                    values.put(Keys.IS_REVOKED, key.isRevoked()); +                    if (c) { +                        if (e) { +                            log(LogLevel.DEBUG, s ? LogType.MSG_IP_SUBKEY_FLAGS_CES +                                    : LogType.MSG_IP_SUBKEY_FLAGS_CEX, null); +                        } else { +                            log(LogLevel.DEBUG, s ? LogType.MSG_IP_SUBKEY_FLAGS_CXS +                                    : LogType.MSG_IP_SUBKEY_FLAGS_CXX, null);                          } -                        // is this the first, or a more recent certificate? -                        if (item.selfCert == null || -                                item.selfCert.getCreationTime().before(cert.getCreationTime())) { -                            item.selfCert = cert; -                            item.isPrimary = cert.isPrimaryUserId(); -                            item.isRevoked = cert.isRevocation(); +                    } else { +                        if (e) { +                            log(LogLevel.DEBUG, s ? LogType.MSG_IP_SUBKEY_FLAGS_XES +                                    : LogType.MSG_IP_SUBKEY_FLAGS_XEX, null); +                        } else { +                            log(LogLevel.DEBUG, s ? LogType.MSG_IP_SUBKEY_FLAGS_XXS +                                    : LogType.MSG_IP_SUBKEY_FLAGS_XXX, null);                          }                      } -                    // verify signatures from known private keys -                    if (allKeyRings.indexOfKey(certId) >= 0) { -                        cert.init(allKeyRings.get(certId)); -                        if (cert.verifySignature(masterKey, userId)) { -                            item.trustedCerts.add(cert); + +                    Date creation = key.getCreationTime(); +                    values.put(Keys.CREATION, creation.getTime() / 1000); +                    Date expiryDate = key.getExpiryTime(); +                    if (expiryDate != null) { +                        values.put(Keys.EXPIRY, expiryDate.getTime() / 1000); +                        if (key.isExpired()) { +                            log(LogLevel.DEBUG, LogType.MSG_IP_SUBKEY_EXPIRED, new String[]{ +                                    expiryDate.toString() +                            }); +                        } else { +                            log(LogLevel.DEBUG, LogType.MSG_IP_SUBKEY_EXPIRES, new String[]{ +                                    expiryDate.toString() +                            });                          }                      } -                } catch (PgpGeneralException e) { -                    Log.e(Constants.TAG, "Signature verification failed! " -                            + PgpKeyHelper.convertKeyIdToHex(masterKey.getKeyId()) -                            + " from " -                            + PgpKeyHelper.convertKeyIdToHex(cert.getKeyId()), e); + +                    operations.add(ContentProviderOperation.newInsert(uri).withValues(values).build()); +                    ++rank; +                    mIndent -= 1;                  }              } -        } +            mIndent -= 1; + +            // get a list of owned secret keys, for verification filtering +            LongSparseArray<WrappedPublicKey> trustedKeys = getTrustedMasterKeys(); + +            // classify and order user ids. primary are moved to the front, revoked to the back, +            // otherwise the order in the keyfile is preserved. +            log(LogLevel.INFO, LogType.MSG_IP_UID_CLASSIFYING, new String[]{ +                    Integer.toString(trustedKeys.size()) +            }); +            mIndent += 1; +            List<UserIdItem> uids = new ArrayList<UserIdItem>(); +            for (String userId : new IterableIterator<String>( +                    masterKey.getUnorderedUserIds().iterator())) { +                UserIdItem item = new UserIdItem(); +                uids.add(item); +                item.userId = userId; + +                int unknownCerts = 0; + +                log(LogLevel.INFO, LogType.MSG_IP_UID_PROCESSING, new String[]{ userId }); +                mIndent += 1; +                // look through signatures for this specific key +                for (WrappedSignature cert : new IterableIterator<WrappedSignature>( +                        masterKey.getSignaturesForId(userId))) { +                    long certId = cert.getKeyId(); +                    try { +                        // self signature +                        if (certId == masterKeyId) { + +                            // NOTE self-certificates are already verified during canonicalization, +                            // AND we know there is at most one cert plus at most one revocation +                            if (!cert.isRevocation()) { +                                item.selfCert = cert; +                                item.isPrimary = cert.isPrimaryUserId(); +                                log(LogLevel.DEBUG, LogType.MSG_IP_UID_SELF_GOOD); +                            } else { +                                item.isRevoked = true; +                                log(LogLevel.DEBUG, LogType.MSG_IP_UID_REVOKED); +                            } + +                        } + +                        // verify signatures from known private keys +                        if (trustedKeys.indexOfKey(certId) >= 0) { +                            WrappedPublicKey trustedKey = trustedKeys.get(certId); +                            cert.init(trustedKey); +                            if (cert.verifySignature(masterKey, userId)) { +                                item.trustedCerts.add(cert); +                                log(LogLevel.INFO, LogType.MSG_IP_UID_CERT_GOOD, new String[] { +                                        PgpKeyHelper.convertKeyIdToHexShort(trustedKey.getKeyId()), +                                        trustedKey.getPrimaryUserId() +                                }); +                            } else { +                                log(LogLevel.WARN, LogType.MSG_IP_UID_CERT_BAD); +                            } +                        } + +                        unknownCerts += 1; + +                    } catch (PgpGeneralException e) { +                        log(LogLevel.WARN, LogType.MSG_IP_UID_CERT_ERROR, new String[]{ +                                PgpKeyHelper.convertKeyIdToHex(cert.getKeyId()) +                        }); +                    } +                } + +                if (unknownCerts > 0) { +                    log(LogLevel.DEBUG, LogType.MSG_IP_UID_CERTS_UNKNOWN, new String[]{ +                            Integer.toString(unknownCerts) +                    }); +                } +                mIndent -= 1; -        // primary before regular before revoked (see UserIdItem.compareTo) -        // this is a stable sort, so the order of keys is otherwise preserved. -        Collections.sort(uids); -        // iterate and put into db -        for (int userIdRank = 0; userIdRank < uids.size(); userIdRank++) { -            UserIdItem item = uids.get(userIdRank); -            operations.add(buildUserIdOperations(masterKeyId, item, userIdRank)); -            // no self cert is bad, but allowed by the rfc... -            if (item.selfCert != null) { -                operations.add(buildCertOperations( -                        masterKeyId, userIdRank, item.selfCert, Certs.VERIFIED_SELF)); -            } -            // don't bother with trusted certs if the uid is revoked, anyways -            if (item.isRevoked) { -                continue;              } -            for (int i = 0; i < item.trustedCerts.size(); i++) { -                operations.add(buildCertOperations( -                        masterKeyId, userIdRank, item.trustedCerts.get(i), Certs.VERIFIED_SECRET)); +            mIndent -= 1; + +            log(LogLevel.DEBUG, LogType.MSG_IP_UID_REORDER); +            // primary before regular before revoked (see UserIdItem.compareTo) +            // this is a stable sort, so the order of keys is otherwise preserved. +            Collections.sort(uids); +            // iterate and put into db +            for (int userIdRank = 0; userIdRank < uids.size(); userIdRank++) { +                UserIdItem item = uids.get(userIdRank); +                operations.add(buildUserIdOperations(masterKeyId, item, userIdRank)); +                if (item.selfCert != null) { +                    operations.add(buildCertOperations(masterKeyId, userIdRank, item.selfCert, +                            secretRing != null ? Certs.VERIFIED_SECRET : Certs.VERIFIED_SELF)); +                } +                // don't bother with trusted certs if the uid is revoked, anyways +                if (item.isRevoked) { +                    continue; +                } +                for (int i = 0; i < item.trustedCerts.size(); i++) { +                    operations.add(buildCertOperations( +                            masterKeyId, userIdRank, item.trustedCerts.get(i), Certs.VERIFIED_SECRET)); +                }              } + +            log(LogLevel.DEBUG, LogType.MSG_IP_PREPARE_SUCCESS); +            mIndent -= 1; + +        } catch (IOException e) { +            log(LogLevel.ERROR, LogType.MSG_IP_FAIL_IO_EXC); +            Log.e(Constants.TAG, "IOException during import", e); +            mIndent -= 1; +            return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);          }          try { +            // delete old version of this keyRing, which also deletes all keys and userIds on cascade +            int deleted = mContentResolver.delete( +                    KeyRingData.buildPublicKeyRingUri(Long.toString(masterKeyId)), null, null); +            if (deleted > 0) { +                log(LogLevel.DEBUG, LogType.MSG_IP_DELETE_OLD_OK); +                result |= SaveKeyringResult.UPDATED; +            } else { +                log(LogLevel.DEBUG, LogType.MSG_IP_DELETE_OLD_FAIL); +            } + +            log(LogLevel.DEBUG, LogType.MSG_IP_APPLY_BATCH);              mContentResolver.applyBatch(KeychainContract.CONTENT_AUTHORITY, operations); + +            // Save the saved keyring (if any) +            if (secretRing != null) { +                log(LogLevel.DEBUG, LogType.MSG_IP_REINSERT_SECRET); +                mIndent += 1; +                saveSecretKeyRing(secretRing); +                result |= SaveKeyringResult.SAVED_SECRET; +                mIndent -= 1; +            } + +            mIndent -= 1; +            log(LogLevel.OK, LogType.MSG_IP_SUCCESS); +            return new SaveKeyringResult(result, mLog); +          } catch (RemoteException e) { -            Log.e(Constants.TAG, "applyBatch failed!", e); +            log(LogLevel.ERROR, LogType.MSG_IP_FAIL_REMOTE_EX); +            Log.e(Constants.TAG, "RemoteException during import", e); +            mIndent -= 1; +            return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);          } catch (OperationApplicationException e) { -            Log.e(Constants.TAG, "applyBatch failed!", e); -        } - -        // Save the saved keyring (if any) -        if (secretRing != null) { -            saveSecretKeyRing(secretRing); +            log(LogLevel.ERROR, LogType.MSG_IP_FAIL_OP_EX); +            Log.e(Constants.TAG, "OperationApplicationException during import", e); +            mIndent -= 1; +            return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);          }      } @@ -378,14 +554,37 @@ public class ProviderHelper {      /**       * Saves a PGPSecretKeyRing in the DB. This will only work if a corresponding public keyring       * is already in the database! +     * +     * TODO allow adding secret keys where no public key exists (ie, consolidate keys)       */ -    public void saveSecretKeyRing(UncachedKeyRing keyRing) throws IOException { +    public SaveKeyringResult saveSecretKeyRing(UncachedKeyRing keyRing) { +          if (!keyRing.isSecret()) { -            throw new RuntimeException("Tried to save publkc keyring as secret! " + -                    "This is a bug, please file a bug report."); +            log(LogLevel.ERROR, LogType.MSG_IS_BAD_TYPE_PUBLIC); +            return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog);          }          long masterKeyId = keyRing.getMasterKeyId(); +        log(LogLevel.START, LogType.MSG_IS, +                new String[]{ PgpKeyHelper.convertKeyIdToHex(masterKeyId) }); +        mIndent += 1; + +        // IF this is successful, it's a secret key +        int result = SaveKeyringResult.SAVED_SECRET; + +        // save secret keyring +        try { +            ContentValues values = new ContentValues(); +            values.put(KeyRingData.MASTER_KEY_ID, masterKeyId); +            values.put(KeyRingData.KEY_RING_DATA, keyRing.getEncoded()); +            // insert new version of this keyRing +            Uri uri = KeyRingData.buildSecretKeyRingUri(Long.toString(masterKeyId)); +            mContentResolver.insert(uri, values); +        } catch (IOException e) { +            Log.e(Constants.TAG, "Failed to encode key!", e); +            log(LogLevel.ERROR, LogType.MSG_IS_IO_EXCPTION); +            return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); +        }          {              Uri uri = Keys.buildKeysUri(Long.toString(masterKeyId)); @@ -397,24 +596,38 @@ public class ProviderHelper {              values.put(Keys.HAS_SECRET, 1);              // then, mark exactly the keys we have available -            for (Long sub : new IterableIterator<Long>(keyRing.getAvailableSubkeys().iterator())) { -                mContentResolver.update(uri, values, Keys.KEY_ID + " = ?", new String[] { -                    Long.toString(sub) -                }); +            log(LogLevel.INFO, LogType.MSG_IS_IMPORTING_SUBKEYS); +            mIndent += 1; +            Set<Long> available = keyRing.getAvailableSubkeys(); +            for (UncachedPublicKey sub : +                    new IterableIterator<UncachedPublicKey>(keyRing.getPublicKeys())) { +                long id = sub.getKeyId(); +                if(available.contains(id)) { +                    int upd = mContentResolver.update(uri, values, Keys.KEY_ID + " = ?", +                            new String[] { Long.toString(id) }); +                    if (upd == 1) { +                        log(LogLevel.DEBUG, LogType.MSG_IS_SUBKEY_OK, new String[]{ +                                PgpKeyHelper.convertKeyIdToHex(id) +                        }); +                    } else { +                        log(LogLevel.WARN, LogType.MSG_IS_SUBKEY_NONEXISTENT, new String[]{ +                                PgpKeyHelper.convertKeyIdToHex(id) +                        }); +                    } +                } else { +                    log(LogLevel.INFO, LogType.MSG_IS_SUBKEY_STRIPPED, new String[]{ +                            PgpKeyHelper.convertKeyIdToHex(id) +                    }); +                }              } +            mIndent -= 1; +              // this implicitly leaves all keys which were not in the secret key ring              // with has_secret = 0          } -        // save secret keyring -        { -            ContentValues values = new ContentValues(); -            values.put(KeyRingData.MASTER_KEY_ID, masterKeyId); -            values.put(KeyRingData.KEY_RING_DATA, keyRing.getEncoded()); -            // insert new version of this keyRing -            Uri uri = KeyRingData.buildSecretKeyRingUri(Long.toString(masterKeyId)); -            mContentResolver.insert(uri, values); -        } +        log(LogLevel.OK, LogType.MSG_IS_SUCCESS); +        return new SaveKeyringResult(result, mLog);      } @@ -436,37 +649,6 @@ public class ProviderHelper {       * Build ContentProviderOperation to add PGPPublicKey to database corresponding to a keyRing       */      private ContentProviderOperation -    buildPublicKeyOperations(long masterKeyId, UncachedPublicKey key, int rank) throws IOException { - -        ContentValues values = new ContentValues(); -        values.put(Keys.MASTER_KEY_ID, masterKeyId); -        values.put(Keys.RANK, rank); - -        values.put(Keys.KEY_ID, key.getKeyId()); -        values.put(Keys.KEY_SIZE, key.getBitStrength()); -        values.put(Keys.ALGORITHM, key.getAlgorithm()); -        values.put(Keys.FINGERPRINT, key.getFingerprint()); - -        values.put(Keys.CAN_CERTIFY, key.canCertify()); -        values.put(Keys.CAN_SIGN, key.canSign()); -        values.put(Keys.CAN_ENCRYPT, key.canEncrypt()); -        values.put(Keys.IS_REVOKED, key.maybeRevoked()); - -        values.put(Keys.CREATION, key.getCreationTime().getTime() / 1000); -        Date expiryDate = key.getExpiryTime(); -        if (expiryDate != null) { -            values.put(Keys.EXPIRY, expiryDate.getTime() / 1000); -        } - -        Uri uri = Keys.buildKeysUri(Long.toString(masterKeyId)); - -        return ContentProviderOperation.newInsert(uri).withValues(values).build(); -    } - -    /** -     * Build ContentProviderOperation to add PGPPublicKey to database corresponding to a keyRing -     */ -    private ContentProviderOperation      buildCertOperations(long masterKeyId, int rank, WrappedSignature cert, int verified)              throws IOException {          ContentValues values = new ContentValues(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java new file mode 100644 index 000000000..8db9294df --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program.  If not, see <http://www.gnu.org/licenses/>. + */ + +package org.sufficientlysecure.keychain.service; + +import android.accounts.Account; +import android.app.Service; +import android.content.AbstractThreadedSyncAdapter; +import android.content.ContentProviderClient; +import android.content.Intent; +import android.content.SyncResult; +import android.os.*; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.KeychainApplication; +import org.sufficientlysecure.keychain.helper.ContactHelper; +import org.sufficientlysecure.keychain.helper.EmailKeyHelper; +import org.sufficientlysecure.keychain.util.Log; + +public class ContactSyncAdapterService extends Service { + +    private class ContactSyncAdapter extends AbstractThreadedSyncAdapter { + +        public ContactSyncAdapter() { +            super(ContactSyncAdapterService.this, true); +        } + +        @Override +        public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, +                                  final SyncResult syncResult) { +            EmailKeyHelper.importContacts(getContext(), new Messenger(new Handler(Looper.getMainLooper(), +                    new Handler.Callback() { +                        @Override +                        public boolean handleMessage(Message msg) { +                            Bundle data = msg.getData(); +                            switch (msg.arg1) { +                                case KeychainIntentServiceHandler.MESSAGE_OKAY: +                                    return true; +                                case KeychainIntentServiceHandler.MESSAGE_UPDATE_PROGRESS: +                                    if (data.containsKey(KeychainIntentServiceHandler.DATA_PROGRESS) && +                                            data.containsKey(KeychainIntentServiceHandler.DATA_PROGRESS_MAX)) { +                                        Log.d(Constants.TAG, "Progress: " + +                                                data.getInt(KeychainIntentServiceHandler.DATA_PROGRESS) + "/" + +                                                data.getInt(KeychainIntentServiceHandler.DATA_PROGRESS_MAX)); +                                        return false; +                                    } +                                default: +                                    Log.d(Constants.TAG, "Syncing... " + msg.toString()); +                                    return false; +                            } +                        } +                    }))); +            KeychainApplication.setupAccountAsNeeded(ContactSyncAdapterService.this); +            ContactHelper.writeKeysToContacts(ContactSyncAdapterService.this); +        } +    } + +    @Override +    public IBinder onBind(Intent intent) { +        return new ContactSyncAdapter().getSyncAdapterBinder(); +    } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/DummyAccountService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/DummyAccountService.java new file mode 100644 index 000000000..008502ce7 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/DummyAccountService.java @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program.  If not, see <http://www.gnu.org/licenses/>. + */ + +package org.sufficientlysecure.keychain.service; + +import android.accounts.AbstractAccountAuthenticator; +import android.accounts.Account; +import android.accounts.AccountAuthenticatorResponse; +import android.accounts.NetworkErrorException; +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.Handler; +import android.os.IBinder; +import android.os.Message; +import android.widget.Toast; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.util.Log; + +/** + * This service actually does nothing, it's sole task is to show a Toast if the use tries to create an account. + */ +public class DummyAccountService extends Service { + +    private class Toaster { +        private static final String TOAST_MESSAGE = "toast_message"; +        private Context context; +        private Handler handler = new Handler(new Handler.Callback() { +            @Override +            public boolean handleMessage(Message msg) { +                Toast.makeText(context, msg.getData().getString(TOAST_MESSAGE), Toast.LENGTH_LONG).show(); +                return true; +            } +        }); + +        private Toaster(Context context) { +            this.context = context; +        } + +        public void toast(int resourceId) { +            toast(context.getString(resourceId)); +        } + +        public void toast(String message) { +            Message msg = new Message(); +            Bundle bundle = new Bundle(); +            bundle.putString(TOAST_MESSAGE, message); +            msg.setData(bundle); +            handler.sendMessage(msg); +        } +    } + +    private class Authenticator extends AbstractAccountAuthenticator { + +        public Authenticator() { +            super(DummyAccountService.this); +        } + +        @Override +        public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { +            Log.d(Constants.TAG, "DummyAccountService.editProperties"); +            return null; +        } + +        @Override +        public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, +                                 String[] requiredFeatures, Bundle options) throws NetworkErrorException { +            response.onResult(new Bundle()); +            toaster.toast(R.string.info_no_manual_account_creation); +            Log.d(Constants.TAG, "DummyAccountService.addAccount"); +            return null; +        } + +        @Override +        public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) +                throws NetworkErrorException { +            Log.d(Constants.TAG, "DummyAccountService.confirmCredentials"); +            return null; +        } + +        @Override +        public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, +                                   Bundle options) throws NetworkErrorException { +            Log.d(Constants.TAG, "DummyAccountService.getAuthToken"); +            return null; +        } + +        @Override +        public String getAuthTokenLabel(String authTokenType) { +            Log.d(Constants.TAG, "DummyAccountService.getAuthTokenLabel"); +            return null; +        } + +        @Override +        public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, +                                        Bundle options) throws NetworkErrorException { +            Log.d(Constants.TAG, "DummyAccountService.updateCredentials"); +            return null; +        } + +        @Override +        public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) +                throws NetworkErrorException { +            Log.d(Constants.TAG, "DummyAccountService.hasFeatures"); +            return null; +        } +    } + +    private Toaster toaster; + +    @Override +    public IBinder onBind(Intent intent) { +        toaster = new Toaster(this); +        return new Authenticator().getIBinder(); +    } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index 498b963f2..3ddcdfcf4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -32,6 +32,7 @@ import org.sufficientlysecure.keychain.helper.FileHelper;  import org.sufficientlysecure.keychain.helper.OtherHelper;  import org.sufficientlysecure.keychain.helper.Preferences;  import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; +import org.sufficientlysecure.keychain.keyimport.Keyserver;  import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;  import org.sufficientlysecure.keychain.keyimport.KeybaseKeyserver;  import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; @@ -174,14 +175,11 @@ public class KeychainIntentService extends IntentService      public static final String RESULT_DECRYPTED_BYTES = "decrypted_data";      public static final String RESULT_DECRYPT_VERIFY_RESULT = "signature"; -    // import -    public static final String RESULT_IMPORT_ADDED = "added"; -    public static final String RESULT_IMPORT_UPDATED = "updated"; -    public static final String RESULT_IMPORT_BAD = "bad"; -      // export      public static final String RESULT_EXPORT = "exported"; +    public static final String RESULT = "result"; +      Messenger mMessenger;      private boolean mIsCanceled; @@ -648,7 +646,10 @@ public class KeychainIntentService extends IntentService                  List<ParcelableKeyRing> entries = data.getParcelableArrayList(IMPORT_KEY_LIST);                  PgpImportExport pgpImportExport = new PgpImportExport(this, this); -                Bundle resultData = pgpImportExport.importKeyRings(entries); +                OperationResults.ImportResult result = pgpImportExport.importKeyRings(entries); + +                Bundle resultData = new Bundle(); +                resultData.putParcelable(RESULT, result);                  sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, resultData);              } catch (Exception e) { @@ -734,49 +735,30 @@ public class KeychainIntentService extends IntentService              } catch (Exception e) {                  sendErrorToHandler(e);              } -        } else if (ACTION_IMPORT_KEYBASE_KEYS.equals(action)) { -            ArrayList<ImportKeysListEntry> entries = data.getParcelableArrayList(DOWNLOAD_KEY_LIST); - -            try { -                KeybaseKeyserver server = new KeybaseKeyserver(); -                ArrayList<ParcelableKeyRing> keyRings = new ArrayList<ParcelableKeyRing>(entries.size()); -                for (ImportKeysListEntry entry : entries) { -                    // the keybase handle is in userId(1) -                    String keybaseId = entry.getExtraData(); -                    byte[] downloadedKeyBytes = server.get(keybaseId).getBytes(); - -                    // save key bytes in entry object for doing the -                    // actual import afterwards -                    keyRings.add(new ParcelableKeyRing(downloadedKeyBytes)); -                } - -                Intent importIntent = new Intent(this, KeychainIntentService.class); -                importIntent.setAction(ACTION_IMPORT_KEYRING); -                Bundle importData = new Bundle(); -                importData.putParcelableArrayList(IMPORT_KEY_LIST, keyRings); -                importIntent.putExtra(EXTRA_DATA, importData); -                importIntent.putExtra(EXTRA_MESSENGER, mMessenger); - -                // now import it with this service -                onHandleIntent(importIntent); - -                // result is handled in ACTION_IMPORT_KEYRING -            } catch (Exception e) { -                sendErrorToHandler(e); -            } -        } else if (ACTION_DOWNLOAD_AND_IMPORT_KEYS.equals(action)) { +        } else if (ACTION_DOWNLOAD_AND_IMPORT_KEYS.equals(action) || ACTION_IMPORT_KEYBASE_KEYS.equals(action)) {              try {                  ArrayList<ImportKeysListEntry> entries = data.getParcelableArrayList(DOWNLOAD_KEY_LIST);                  // this downloads the keys and places them into the ImportKeysListEntry entries                  String keyServer = data.getString(DOWNLOAD_KEY_SERVER); -                HkpKeyserver server = new HkpKeyserver(keyServer);                  ArrayList<ParcelableKeyRing> keyRings = new ArrayList<ParcelableKeyRing>(entries.size());                  for (ImportKeysListEntry entry : entries) { + +                    Keyserver server; +                    if (entry.getOrigin() == null) { +                        server = new HkpKeyserver(keyServer); +                    } else if (KeybaseKeyserver.ORIGIN.equals(entry.getOrigin())) { +                        server = new KeybaseKeyserver(); +                    } else { +                        server = new HkpKeyserver(entry.getOrigin()); +                    } +                      // if available use complete fingerprint for get request                      byte[] downloadedKeyBytes; -                    if (entry.getFingerprintHex() != null) { +                    if (KeybaseKeyserver.ORIGIN.equals(entry.getOrigin())) { +                        downloadedKeyBytes = server.get(entry.getExtraData()).getBytes(); +                    } else if (entry.getFingerprintHex() != null) {                          downloadedKeyBytes = server.get("0x" + entry.getFingerprintHex()).getBytes();                      } else {                          downloadedKeyBytes = server.get(entry.getKeyIdHex()).getBytes(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java new file mode 100644 index 000000000..b5f01ce4d --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -0,0 +1,250 @@ +package org.sufficientlysecure.keychain.service; + +import android.os.Parcel; +import android.os.Parcelable; + +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.util.IterableIterator; + +import java.util.ArrayList; + +/** Represent the result of an operation. + * + * This class holds a result and the log of an operation. It can be subclassed + * to include typed additional information specific to the operation. To keep + * the class structure (somewhat) simple, this class contains an exhaustive + * list (ie, enum) of all possible log types, which should in all cases be tied + * to string resource ids. + * + */ +public class OperationResultParcel implements Parcelable { +    /** Holds the overall result, the number specifying varying degrees of success. The first bit +     * is 0 on overall success, 1 on overall failure. All other bits may be used for more specific +     * conditions. */ +    final int mResult; + +    public static final int RESULT_OK = 0; +    public static final int RESULT_ERROR = 1; + +    /// A list of log entries tied to the operation result. +    final OperationLog mLog; + +    public OperationResultParcel(int result, OperationLog log) { +        mResult = result; +        mLog = log; +    } + +    public OperationResultParcel(Parcel source) { +        mResult = source.readInt(); +        mLog = new OperationLog(); +        mLog.addAll(source.createTypedArrayList(LogEntryParcel.CREATOR)); +    } + +    public int getResult() { +        return mResult; +    } + +    public boolean success() { +        return (mResult & 1) == 0; +    } + +    public OperationLog getLog() { +        return mLog; +    } + +    /** One entry in the log. */ +    public static class LogEntryParcel implements Parcelable { +        public final LogLevel mLevel; +        public final LogType mType; +        public final String[] mParameters; +        public final int mIndent; + +        public LogEntryParcel(LogLevel level, LogType type, String[] parameters, int indent) { +            mLevel = level; +            mType = type; +            mParameters = parameters; +            mIndent = indent; +        } +        public LogEntryParcel(LogLevel level, LogType type, String[] parameters) { +            this(level, type, parameters, 0); +        } + +        public LogEntryParcel(Parcel source) { +            mLevel = LogLevel.values()[source.readInt()]; +            mType = LogType.values()[source.readInt()]; +            mParameters = source.createStringArray(); +            mIndent = source.readInt(); +        } + +        @Override +        public int describeContents() { +            return 0; +        } + +        @Override +        public void writeToParcel(Parcel dest, int flags) { +            dest.writeInt(mLevel.ordinal()); +            dest.writeInt(mType.ordinal()); +            dest.writeStringArray(mParameters); +            dest.writeInt(mIndent); +        } + +        public static final Creator<LogEntryParcel> CREATOR = new Creator<LogEntryParcel>() { +            public LogEntryParcel createFromParcel(final Parcel source) { +                return new LogEntryParcel(source); +            } + +            public LogEntryParcel[] newArray(final int size) { +                return new LogEntryParcel[size]; +            } +        }; + +    } + +    public static enum LogType { + +        // import public +        MSG_IP(R.string.msg_ip), +        MSG_IP_APPLY_BATCH (R.string.msg_ip_apply_batch), +        MSG_IP_BAD_TYPE_SECRET (R.string.msg_ip_bad_type_secret), +        MSG_IP_DELETE_OLD_FAIL (R.string.msg_ip_delete_old_fail), +        MSG_IP_DELETE_OLD_OK (R.string.msg_ip_delete_old_ok), +        MSG_IP_ENCODE_FAIL (R.string.msg_ip_encode_fail), +        MSG_IP_FAIL_IO_EXC (R.string.msg_ip_fail_io_exc), +        MSG_IP_FAIL_OP_EX (R.string.msg_ip_fail_op_ex), +        MSG_IP_FAIL_REMOTE_EX (R.string.msg_ip_fail_remote_ex), +        MSG_IP_INSERT_KEYRING (R.string.msg_ip_insert_keyring), +        MSG_IP_INSERT_SUBKEYS (R.string.msg_ip_insert_subkeys), +        MSG_IP_PREPARE (R.string.msg_ip_prepare), +        MSG_IP_PREPARE_SUCCESS(R.string.msg_ip_prepare_success), +        MSG_IP_PRESERVING_SECRET (R.string.msg_ip_preserving_secret), +        MSG_IP_REINSERT_SECRET (R.string.msg_ip_reinsert_secret), +        MSG_IP_SUBKEY (R.string.msg_ip_subkey), +        MSG_IP_SUBKEY_EXPIRED (R.string.msg_ip_subkey_expired), +        MSG_IP_SUBKEY_EXPIRES (R.string.msg_ip_subkey_expires), +        MSG_IP_SUBKEY_FLAGS (R.string.msg_ip_subkey_flags), +        MSG_IP_SUBKEY_FLAGS_CES (R.string.msg_ip_subkey_flags_ces), +        MSG_IP_SUBKEY_FLAGS_CEX (R.string.msg_ip_subkey_flags_cex), +        MSG_IP_SUBKEY_FLAGS_CXS (R.string.msg_ip_subkey_flags_cxs), +        MSG_IP_SUBKEY_FLAGS_XES (R.string.msg_ip_subkey_flags_xes), +        MSG_IP_SUBKEY_FLAGS_CXX (R.string.msg_ip_subkey_flags_cxx), +        MSG_IP_SUBKEY_FLAGS_XEX (R.string.msg_ip_subkey_flags_xex), +        MSG_IP_SUBKEY_FLAGS_XXS (R.string.msg_ip_subkey_flags_xxs), +        MSG_IP_SUBKEY_FLAGS_XXX (R.string.msg_ip_subkey_flags_xxx), +        MSG_IP_SUCCESS (R.string.msg_ip_success), +        MSG_IP_UID_CERT_BAD (R.string.msg_ip_uid_cert_bad), +        MSG_IP_UID_CERT_ERROR (R.string.msg_ip_uid_cert_error), +        MSG_IP_UID_CERT_GOOD (R.string.msg_ip_uid_cert_good), +        MSG_IP_UID_CERTS_UNKNOWN (R.string.msg_ip_uid_certs_unknown), +        MSG_IP_UID_CLASSIFYING (R.string.msg_ip_uid_classifying), +        MSG_IP_UID_REORDER(R.string.msg_ip_uid_reorder), +        MSG_IP_UID_PROCESSING (R.string.msg_ip_uid_processing), +        MSG_IP_UID_REVOKED (R.string.msg_ip_uid_revoked), +        MSG_IP_UID_SELF_GOOD (R.string.msg_ip_uid_self_good), + +        // import secret +        MSG_IS(R.string.msg_is), +        MSG_IS_BAD_TYPE_PUBLIC (R.string.msg_is_bad_type_public), +        MSG_IS_IMPORTING_SUBKEYS (R.string.msg_is_importing_subkeys), +        MSG_IS_IO_EXCPTION (R.string.msg_is_io_excption), +        MSG_IS_SUBKEY_NONEXISTENT (R.string.msg_is_subkey_nonexistent), +        MSG_IS_SUBKEY_OK (R.string.msg_is_subkey_ok), +        MSG_IS_SUBKEY_STRIPPED (R.string.msg_is_subkey_stripped), +        MSG_IS_SUCCESS (R.string.msg_is_success), + +        // keyring canonicalization +        MSG_KC (R.string.msg_kc), +        MSG_KC_MASTER (R.string.msg_kc_master), +        MSG_KC_MASTER_SUCCESS (R.string.msg_kc_master_success), +        MSG_KC_REVOKE_BAD_ERR (R.string.msg_kc_revoke_bad_err), +        MSG_KC_REVOKE_BAD_LOCAL (R.string.msg_kc_revoke_bad_local), +        MSG_KC_REVOKE_BAD_TIME (R.string.msg_kc_revoke_bad_time), +        MSG_KC_REVOKE_BAD_TYPE (R.string.msg_kc_revoke_bad_type), +        MSG_KC_REVOKE_BAD (R.string.msg_kc_revoke_bad), +        MSG_KC_REVOKE_DUP (R.string.msg_kc_revoke_dup), +        MSG_KC_SUB (R.string.msg_kc_sub), +        MSG_KC_SUB_BAD(R.string.msg_kc_sub_bad), +        MSG_KC_SUB_BAD_ERR(R.string.msg_kc_sub_bad_err), +        MSG_KC_SUB_BAD_LOCAL(R.string.msg_kc_sub_bad_local), +        MSG_KC_SUB_BAD_KEYID(R.string.msg_kc_sub_bad_keyid), +        MSG_KC_SUB_BAD_TIME(R.string.msg_kc_sub_bad_time), +        MSG_KC_SUB_BAD_TYPE(R.string.msg_kc_sub_bad_type), +        MSG_KC_SUB_PRIMARY_BAD(R.string.msg_kc_sub_primary_bad), +        MSG_KC_SUB_PRIMARY_BAD_ERR(R.string.msg_kc_sub_primary_bad_err), +        MSG_KC_SUB_PRIMARY_NONE(R.string.msg_kc_sub_primary_none), +        MSG_KC_SUB_NO_CERT(R.string.msg_kc_sub_no_cert), +        MSG_KC_SUB_REVOKE_BAD_ERR (R.string.msg_kc_sub_revoke_bad_err), +        MSG_KC_SUB_REVOKE_BAD (R.string.msg_kc_sub_revoke_bad), +        MSG_KC_SUB_REVOKE_DUP (R.string.msg_kc_sub_revoke_dup), +        MSG_KC_SUB_SUCCESS (R.string.msg_kc_sub_success), +        MSG_KC_SUCCESS_REMOVED (R.string.msg_kc_success_removed), +        MSG_KC_SUCCESS (R.string.msg_kc_success), +        MSG_KC_UID_BAD_ERR (R.string.msg_kc_uid_bad_err), +        MSG_KC_UID_BAD_LOCAL (R.string.msg_kc_uid_bad_local), +        MSG_KC_UID_BAD_TIME (R.string.msg_kc_uid_bad_time), +        MSG_KC_UID_BAD_TYPE (R.string.msg_kc_uid_bad_type), +        MSG_KC_UID_BAD (R.string.msg_kc_uid_bad), +        MSG_KC_UID_DUP (R.string.msg_kc_uid_dup), +        MSG_KC_UID_REVOKE_DUP (R.string.msg_kc_uid_revoke_dup), +        MSG_KC_UID_REVOKE_OLD (R.string.msg_kc_uid_revoke_old), +        ; + +        private final int mMsgId; +        LogType(int msgId) { +            mMsgId = msgId; +        } +        public int getMsgId() { +            return mMsgId; +        } +    } + +    /** Enumeration of possible log levels. */ +    public static enum LogLevel { +        DEBUG, +        INFO, +        WARN, +        ERROR, // should occur once at the end of a failed operation +        START, // should occur once at the start of each independent operation +        OK, // should occur once at the end of a successful operation +    } + +    @Override +    public int describeContents() { +        return 0; +    } + +    @Override +    public void writeToParcel(Parcel dest, int flags) { +        dest.writeInt(mResult); +        dest.writeTypedList(mLog); +    } + +    public static final Creator<OperationResultParcel> CREATOR = new Creator<OperationResultParcel>() { +        public OperationResultParcel createFromParcel(final Parcel source) { +            return new OperationResultParcel(source); +        } + +        public OperationResultParcel[] newArray(final int size) { +            return new OperationResultParcel[size]; +        } +    }; + +    public static class OperationLog extends ArrayList<LogEntryParcel> { + +        /// Simple convenience method +        public void add(LogLevel level, LogType type, String[] parameters, int indent) { +            add(new OperationResultParcel.LogEntryParcel(level, type, parameters, indent)); +        } + +        public boolean containsWarnings() { +            for(LogEntryParcel entry : new IterableIterator<LogEntryParcel>(iterator())) { +                if (entry.mLevel == LogLevel.WARN || entry.mLevel == LogLevel.ERROR) { +                    return true; +                } +            } +            return false; +        } + +    } + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java new file mode 100644 index 000000000..6c44b01f1 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java @@ -0,0 +1,92 @@ +package org.sufficientlysecure.keychain.service; + +import android.os.Parcel; + +public abstract class OperationResults { + +    public static class ImportResult extends OperationResultParcel { + +        public final int mNewKeys, mUpdatedKeys, mBadKeys; + +        // At least one new key +        public static final int RESULT_OK_NEWKEYS = 2; +        // At least one updated key +        public static final int RESULT_OK_UPDATED = 4; +        // At least one key failed (might still be an overall success) +        public static final int RESULT_WITH_ERRORS = 8; +        // There are warnings in the log +        public static final int RESULT_WITH_WARNINGS = 16; + +        // No keys to import... +        public static final int RESULT_FAIL_NOTHING = 32 +1; + +        public boolean isOkBoth() { +            return (mResult & (RESULT_OK_NEWKEYS | RESULT_OK_UPDATED)) +                    == (RESULT_OK_NEWKEYS | RESULT_OK_UPDATED); +        } +        public boolean isOkNew() { +            return (mResult & RESULT_OK_NEWKEYS) == RESULT_OK_NEWKEYS; +        } +        public boolean isOkUpdated() { +            return (mResult & RESULT_OK_UPDATED) == RESULT_OK_UPDATED; +        } +        public boolean isFailNothing() { +            return (mResult & RESULT_FAIL_NOTHING) == RESULT_FAIL_NOTHING; +        } + +        public ImportResult(Parcel source) { +            super(source); +            mNewKeys = source.readInt(); +            mUpdatedKeys = source.readInt(); +            mBadKeys = source.readInt(); +        } + +        public ImportResult(int result, OperationLog log, +                            int newKeys, int updatedKeys, int badKeys) { +            super(result, log); +            mNewKeys = newKeys; +            mUpdatedKeys = updatedKeys; +            mBadKeys = badKeys; +        } + +        @Override +        public void writeToParcel(Parcel dest, int flags) { +            super.writeToParcel(dest, flags); +            dest.writeInt(mNewKeys); +            dest.writeInt(mUpdatedKeys); +            dest.writeInt(mBadKeys); +        } + +        public static Creator<ImportResult> CREATOR = new Creator<ImportResult>() { +            public ImportResult createFromParcel(final Parcel source) { +                return new ImportResult(source); +            } + +            public ImportResult[] newArray(final int size) { +                return new ImportResult[size]; +            } +        }; + +    } + +    public static class SaveKeyringResult extends OperationResultParcel { + +        public SaveKeyringResult(int result, OperationLog log) { +            super(result, log); +        } + +        // Some old key was updated +        public static final int UPDATED = 2; + +        // Public key was saved +        public static final int SAVED_PUBLIC = 8; +        // Secret key was saved (not exclusive with public!) +        public static final int SAVED_SECRET = 16; + +        public boolean updated() { +            return (mResult & UPDATED) == UPDATED; +        } + +    } + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/HelpActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/HelpActivity.java index 9edaff19a..bbc1e4b1f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/HelpActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/HelpActivity.java @@ -25,7 +25,7 @@ import android.support.v7.app.ActionBarActivity;  import org.sufficientlysecure.keychain.R;  import org.sufficientlysecure.keychain.ui.adapter.PagerTabStripAdapter; -import org.sufficientlysecure.keychain.util.SlidingTabLayout; +import org.sufficientlysecure.keychain.ui.widget.SlidingTabLayout;  public class HelpActivity extends ActionBarActivity {      public static final String EXTRA_SELECTED_TAB = "selected_tab"; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index 48602aaa1..f389726ff 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -38,7 +38,10 @@ import android.view.View;  import android.view.View.OnClickListener;  import android.widget.ArrayAdapter; -import com.devspark.appmsg.AppMsg; +import com.github.johnpersano.supertoasts.SuperCardToast; +import com.github.johnpersano.supertoasts.SuperToast; +import com.github.johnpersano.supertoasts.util.OnClickWrapper; +import com.github.johnpersano.supertoasts.util.Style;  import org.sufficientlysecure.keychain.Constants;  import org.sufficientlysecure.keychain.R; @@ -47,7 +50,7 @@ import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;  import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;  import org.sufficientlysecure.keychain.service.KeychainIntentService;  import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; -import org.sufficientlysecure.keychain.ui.dialog.BadImportKeyDialogFragment; +import org.sufficientlysecure.keychain.service.OperationResults.ImportResult;  import org.sufficientlysecure.keychain.util.Log;  import java.util.ArrayList; @@ -135,6 +138,7 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O          }          handleActions(savedInstanceState, getIntent()); +      }      protected void handleActions(Bundle savedInstanceState, Intent intent) { @@ -331,8 +335,11 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O      public void loadFromFingerprint(Bundle savedInstanceState, String fingerprint) {          if (fingerprint == null || fingerprint.length() < 40) { -            AppMsg.makeText(this, R.string.import_qr_code_too_short_fingerprint, -                    AppMsg.STYLE_ALERT).show(); +            SuperCardToast toast = SuperCardToast.create(this, +                    getString(R.string.import_qr_code_too_short_fingerprint), +                    SuperToast.Duration.LONG); +            toast.setBackground(SuperToast.Background.RED); +            toast.show();              return;          } @@ -368,39 +375,93 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O                  if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) {                      // get returned data bundle                      Bundle returnData = message.getData(); +                    final ImportResult result = +                            returnData.<ImportResult>getParcelable(KeychainIntentService.RESULT); + +                    int resultType = result.getResult(); + +                    String str; +                    int duration, color; + +                    // Not an overall failure +                    if ((resultType & ImportResult.RESULT_ERROR) == 0) { +                        String withWarnings; + +                        // Any warnings? +                        if ((resultType & ImportResult.RESULT_WITH_WARNINGS) > 0) { +                            duration = 0; +                            color = Style.ORANGE; +                            withWarnings = getResources().getString(R.string.import_with_warnings); +                        } else { +                            duration = SuperToast.Duration.LONG; +                            color = Style.GREEN; +                            withWarnings = ""; +                        } + +                        // New and updated keys +                        if (result.isOkBoth()) { +                            str = getResources().getQuantityString( +                                    R.plurals.import_keys_added_and_updated_1, result.mNewKeys, result.mNewKeys); +                            str += getResources().getQuantityString( +                                    R.plurals.import_keys_added_and_updated_2, result.mUpdatedKeys, result.mUpdatedKeys, withWarnings); +                        } else if (result.isOkUpdated()) { +                            str = getResources().getQuantityString( +                                    R.plurals.import_keys_updated, result.mUpdatedKeys, result.mUpdatedKeys, withWarnings); +                        } else if (result.isOkNew()) { +                            str = getResources().getQuantityString( +                                    R.plurals.import_keys_added, result.mNewKeys, result.mNewKeys, withWarnings); +                        } else { +                            duration = 0; +                            color = Style.RED; +                            str = "internal error"; +                        } -                    int added = returnData.getInt(KeychainIntentService.RESULT_IMPORT_ADDED); -                    int updated = returnData -                            .getInt(KeychainIntentService.RESULT_IMPORT_UPDATED); -                    int bad = returnData.getInt(KeychainIntentService.RESULT_IMPORT_BAD); -                    String toastMessage; -                    if (added > 0 && updated > 0) { -                        String addedStr = getResources().getQuantityString( -                                R.plurals.keys_added_and_updated_1, added, added); -                        String updatedStr = getResources().getQuantityString( -                                R.plurals.keys_added_and_updated_2, updated, updated); -                        toastMessage = addedStr + updatedStr; -                    } else if (added > 0) { -                        toastMessage = getResources().getQuantityString(R.plurals.keys_added, -                                added, added); -                    } else if (updated > 0) { -                        toastMessage = getResources().getQuantityString(R.plurals.keys_updated, -                                updated, updated);                      } else { -                        toastMessage = getString(R.string.no_keys_added_or_updated); +                        duration = 0; +                        color = Style.RED; +                        if (result.isFailNothing()) { +                            str = getString(R.string.import_error_nothing); +                        } else { +                            str = getString(R.string.import_error); +                        }                      } -                    AppMsg.makeText(ImportKeysActivity.this, toastMessage, AppMsg.STYLE_INFO) -                            .show(); + +                    SuperCardToast toast = new SuperCardToast(ImportKeysActivity.this, +                            SuperToast.Type.BUTTON, Style.getStyle(color, SuperToast.Animations.POPUP)); +                    toast.setText(str); +                    toast.setDuration(duration); +                    toast.setIndeterminate(duration == 0); +                    toast.setSwipeToDismiss(true); +                    toast.setButtonIcon(R.drawable.ic_action_view_as_list, +                            getResources().getString(R.string.import_view_log)); +                    toast.setButtonTextColor(getResources().getColor(R.color.black)); +                    toast.setTextColor(getResources().getColor(R.color.black)); +                    toast.setOnClickWrapper(new OnClickWrapper("supercardtoast", +                        new SuperToast.OnClickListener() { +                            @Override +                            public void onClick(View view, Parcelable token) { +                                Intent intent = new Intent( +                                        ImportKeysActivity.this, LogDisplayActivity.class); +                                intent.putExtra(LogDisplayFragment.EXTRA_RESULT, result); +                                startActivity(intent); +                            } +                    })); +                    toast.show(); + +                    /*                      if (bad > 0) {                          BadImportKeyDialogFragment badImportKeyDialogFragment =                                  BadImportKeyDialogFragment.newInstance(bad);                          badImportKeyDialogFragment.show(getSupportFragmentManager(), "badKeyDialog");                      } +                    */ +                    /*                      if (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN.equals(getIntent().getAction())) {                          ImportKeysActivity.this.setResult(Activity.RESULT_OK, mPendingIntentData);                          finish();                      } +                    */                  }              }          }; @@ -483,7 +544,11 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O              startService(intent);          } else { -            AppMsg.makeText(this, R.string.error_nothing_import, AppMsg.STYLE_ALERT).show(); +            SuperCardToast toast = SuperCardToast.create(this, +                    getString(R.string.error_nothing_import), +                    SuperToast.Duration.LONG); +            toast.setBackground(SuperToast.Background.RED); +            toast.show();          }      } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index 9cc0f4667..5eb8ecb8d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -252,7 +252,7 @@ public class KeyListFragment extends LoaderFragment      static final int INDEX_HAS_ANY_SECRET = 6;      static final String ORDER = -            KeyRings.HAS_ANY_SECRET + " DESC, " + KeyRings.USER_ID + " ASC"; +            KeyRings.HAS_ANY_SECRET + " DESC, UPPER(" + KeyRings.USER_ID + ") ASC";      @Override @@ -592,7 +592,7 @@ public class KeyListFragment extends LoaderFragment              String userId = mCursor.getString(KeyListFragment.INDEX_USER_ID);              String headerText = convertView.getResources().getString(R.string.user_id_no_name);              if (userId != null && userId.length() > 0) { -                headerText = "" + userId.subSequence(0, 1).charAt(0); +                headerText = "" + userId.charAt(0);              }              holder.mText.setText(headerText);              holder.mCount.setVisibility(View.GONE); @@ -621,7 +621,7 @@ public class KeyListFragment extends LoaderFragment              // otherwise, return the first character of the name as ID              String userId = mCursor.getString(KeyListFragment.INDEX_USER_ID);              if (userId != null && userId.length() > 0) { -                return userId.charAt(0); +                return Character.toUpperCase(userId.charAt(0));              } else {                  return Long.MAX_VALUE;              } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayActivity.java new file mode 100644 index 000000000..a0d449195 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayActivity.java @@ -0,0 +1,21 @@ +package org.sufficientlysecure.keychain.ui; + +import android.os.Bundle; +import android.support.v4.view.GestureDetectorCompat; +import android.support.v7.app.ActionBarActivity; +import android.view.GestureDetector; +import android.view.GestureDetector.SimpleOnGestureListener; +import android.view.MotionEvent; + +import org.sufficientlysecure.keychain.R; + +public class LogDisplayActivity extends ActionBarActivity { + +    @Override +    public void onCreate(Bundle savedInstanceState) { +        super.onCreate(savedInstanceState); + +        setContentView(R.layout.log_display_activity); +    } + +}
\ No newline at end of file diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java new file mode 100644 index 000000000..496e98c18 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java @@ -0,0 +1,175 @@ +package org.sufficientlysecure.keychain.ui; + +import android.content.Context; +import android.content.Intent; +import android.graphics.Color; +import android.os.Bundle; +import android.support.v4.app.ListFragment; +import android.util.TypedValue; +import android.view.GestureDetector; +import android.view.GestureDetector.SimpleOnGestureListener; +import android.view.LayoutInflater; +import android.view.MotionEvent; +import android.view.View; +import android.view.View.OnTouchListener; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.service.OperationResultParcel; +import org.sufficientlysecure.keychain.service.OperationResultParcel.LogEntryParcel; +import org.sufficientlysecure.keychain.service.OperationResultParcel.LogLevel; +import org.sufficientlysecure.keychain.util.Log; + +import java.util.ArrayList; +import java.util.HashMap; + +public class LogDisplayFragment extends ListFragment implements OnTouchListener { + +    HashMap<LogLevel,LogAdapter> mAdapters = new HashMap<LogLevel, LogAdapter>(); +    LogAdapter mAdapter; +    LogLevel mLevel = LogLevel.DEBUG; + +    OperationResultParcel mResult; + +    GestureDetector mDetector; + +    public static final String EXTRA_RESULT = "log"; + +    @Override +    public void onCreate(Bundle savedInstanceState) { +        super.onActivityCreated(savedInstanceState); + +        Intent intent = getActivity().getIntent(); +        if (intent.getExtras() == null || !intent.getExtras().containsKey(EXTRA_RESULT)) { +            getActivity().finish(); +            return; +        } + +        mResult = intent.<OperationResultParcel>getParcelableExtra(EXTRA_RESULT); +        if (mResult == null) { +            getActivity().finish(); +            return; +        } + +        mAdapter = new LogAdapter(getActivity(), mResult.getLog(), LogLevel.DEBUG); +        mAdapters.put(LogLevel.DEBUG, mAdapter); +        setListAdapter(mAdapter); + +        mDetector = new GestureDetector(getActivity(), new SimpleOnGestureListener() { +            @Override +            public boolean onFling(MotionEvent e1, MotionEvent e2, float vx, float vy) { +                Log.d(Constants.TAG, "x: " + vx + ", y: " + vy); +                if (vx < -2000) { +                    decreaseLogLevel(); +                } else if (vx > 2000) { +                    increaseLogLevel(); +                } +                return true; +            } +        }); + +    } + +    public void decreaseLogLevel() { +        switch (mLevel) { +            case DEBUG: mLevel = LogLevel.INFO; break; +            case INFO:  mLevel = LogLevel.WARN; break; +        } +        refreshLevel(); +    } + +    public void increaseLogLevel() { +        switch (mLevel) { +            case INFO: mLevel = LogLevel.DEBUG; break; +            case WARN: mLevel = LogLevel.INFO; break; +        } +        refreshLevel(); +    } + +    private void refreshLevel() { +        /* TODO not sure if this is a good idea +        if (!mAdapters.containsKey(mLevel)) { +            mAdapters.put(mLevel, new LogAdapter(getActivity(), mResult.getLog(), mLevel)); +        } +        mAdapter = mAdapters.get(mLevel); +        setListAdapter(mAdapter); +        */ +    } + +    @Override +    public void onActivityCreated(Bundle savedInstanceState) { +        super.onActivityCreated(savedInstanceState); +        getListView().setDividerHeight(0); +        getListView().setOnTouchListener(this); +    } + +    @Override +    public boolean onTouch(View v, MotionEvent event) { +        mDetector.onTouchEvent(event); +        return false; +    } + +    private class LogAdapter extends ArrayAdapter<LogEntryParcel> { + +        private LayoutInflater mInflater; +        private int dipFactor; + +        public LogAdapter(Context context, ArrayList<LogEntryParcel> log, LogLevel level) { +            super(context, R.layout.log_display_item); +            mInflater = LayoutInflater.from(getContext()); +            dipFactor = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, +                    (float) 8, getResources().getDisplayMetrics()); +            // we can't use addAll for a LogLevel.DEBUG shortcut here, unfortunately :( +            for (LogEntryParcel e : log) { +                if (e.mLevel.ordinal() >= level.ordinal()) { +                    add(e); +                } +            } +            notifyDataSetChanged(); +        } + +        private class ItemHolder { +            final TextView mText; +            final ImageView mImg; +            public ItemHolder(TextView text, ImageView image) { +                mText = text; +                mImg = image; +            } +        } + +        @Override +        public View getView(int position, View convertView, ViewGroup parent) { +            LogEntryParcel entry = getItem(position); +            ItemHolder ih; +            if (convertView == null) { +                convertView = mInflater.inflate(R.layout.log_display_item, parent, false); +                ih = new ItemHolder( +                        (TextView) convertView.findViewById(R.id.log_text), +                        (ImageView) convertView.findViewById(R.id.log_img) +                ); +                convertView.setTag(ih); +            } else { +                ih = (ItemHolder) convertView.getTag(); +            } + +            ih.mText.setText(getResources().getString(entry.mType.getMsgId(), (Object[]) entry.mParameters)); +            ih.mText.setTextColor(entry.mLevel == LogLevel.DEBUG ? Color.GRAY : Color.BLACK); +            convertView.setPadding((entry.mIndent) * dipFactor, 0, 0, 0); +            switch (entry.mLevel) { +                case DEBUG: ih.mImg.setBackgroundColor(Color.GRAY); break; +                case INFO: ih.mImg.setBackgroundColor(Color.BLACK); break; +                case WARN: ih.mImg.setBackgroundColor(Color.YELLOW); break; +                case ERROR: ih.mImg.setBackgroundColor(Color.RED); break; +                case START: ih.mImg.setBackgroundColor(Color.GREEN); break; +                case OK: ih.mImg.setBackgroundColor(Color.GREEN); break; +            } + +            return convertView; +        } + +    } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index f27a4ad27..463c800d2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.ui;  import android.annotation.TargetApi;  import android.app.Activity; +import android.content.Context;  import android.content.Intent;  import android.database.Cursor;  import android.net.Uri; @@ -32,6 +33,7 @@ import android.os.Build;  import android.os.Bundle;  import android.os.Handler;  import android.os.Message; +import android.provider.ContactsContract;  import android.support.v4.app.LoaderManager;  import android.support.v4.content.CursorLoader;  import android.support.v4.content.Loader; @@ -47,6 +49,7 @@ import com.devspark.appmsg.AppMsg;  import org.sufficientlysecure.keychain.Constants;  import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.helper.ContactHelper;  import org.sufficientlysecure.keychain.helper.ExportHelper;  import org.sufficientlysecure.keychain.pgp.KeyRing;  import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; @@ -54,7 +57,7 @@ import org.sufficientlysecure.keychain.provider.KeychainContract;  import org.sufficientlysecure.keychain.provider.ProviderHelper;  import org.sufficientlysecure.keychain.ui.adapter.PagerTabStripAdapter;  import org.sufficientlysecure.keychain.util.Log; -import org.sufficientlysecure.keychain.util.SlidingTabLayout; +import org.sufficientlysecure.keychain.ui.widget.SlidingTabLayout;  import java.util.Date;  import java.util.HashMap; @@ -92,6 +95,8 @@ public class ViewKeyActivity extends ActionBarActivity implements      private static final int LOADER_ID_UNIFIED = 0; +    private boolean mShowAdvancedTabs; +      @Override      protected void onCreate(Bundle savedInstanceState) { @@ -116,16 +121,13 @@ public class ViewKeyActivity extends ActionBarActivity implements          mViewPager = (ViewPager) findViewById(R.id.view_key_pager);          mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.view_key_sliding_tab_layout); -        mTabsAdapter = new PagerTabStripAdapter(this); -        mViewPager.setAdapter(mTabsAdapter); -          int switchToTab = TAB_MAIN;          Intent intent = getIntent();          if (intent.getExtras() != null && intent.getExtras().containsKey(EXTRA_SELECTED_TAB)) {              switchToTab = intent.getExtras().getInt(EXTRA_SELECTED_TAB);          } -        Uri dataUri = getIntent().getData(); +        Uri dataUri = getDataUri();          if (dataUri == null) {              Log.e(Constants.TAG, "Data missing. Should be Uri of key!");              finish(); @@ -136,6 +138,18 @@ public class ViewKeyActivity extends ActionBarActivity implements          initNfc(dataUri); +        mShowAdvancedTabs = false; + +        initTabs(dataUri); + +        // switch to tab selected by extra +        mViewPager.setCurrentItem(switchToTab); +    } + +    private void initTabs(Uri dataUri) { +        mTabsAdapter = new PagerTabStripAdapter(this); +        mViewPager.setAdapter(mTabsAdapter); +          Bundle mainBundle = new Bundle();          mainBundle.putParcelable(ViewKeyMainFragment.ARG_DATA_URI, dataUri);          mTabsAdapter.addTab(ViewKeyMainFragment.class, @@ -146,6 +160,11 @@ public class ViewKeyActivity extends ActionBarActivity implements          mTabsAdapter.addTab(ViewKeyShareFragment.class,                  mainBundle, getString(R.string.key_view_tab_share)); +        // update layout after operations +        mSlidingTabLayout.setViewPager(mViewPager); +    } + +    private void addAdvancedTabs(Uri dataUri) {          Bundle keyDetailsBundle = new Bundle();          keyDetailsBundle.putParcelable(ViewKeyKeysFragment.ARG_DATA_URI, dataUri);          mTabsAdapter.addTab(ViewKeyKeysFragment.class, @@ -156,11 +175,54 @@ public class ViewKeyActivity extends ActionBarActivity implements          mTabsAdapter.addTab(ViewKeyCertsFragment.class,                  certBundle, getString(R.string.key_view_tab_certs)); -        // NOTE: must be after adding the tabs! +        // update layout after operations          mSlidingTabLayout.setViewPager(mViewPager); +    } -        // switch to tab selected by extra -        mViewPager.setCurrentItem(switchToTab); +    private void removeAdvancedTabs() { +        // before removing, switch to the first tab if necessary +        if (mViewPager.getCurrentItem() >= TAB_KEYS) { +            // remove _after_ switching to the main tab +            mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { +                @Override +                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { +                } + +                @Override +                public void onPageSelected(int position) { +                } + +                @Override +                public void onPageScrollStateChanged(int state) { +                    if (ViewPager.SCROLL_STATE_SETTLING == state) { +                        mTabsAdapter.removeTab(TAB_CERTS); +                        mTabsAdapter.removeTab(TAB_KEYS); + +                        // update layout after operations +                        mSlidingTabLayout.setViewPager(mViewPager); + +                        // remove this listener again +//                        mViewPager.setOnPageChangeListener(null); +                    } +                } +            }); + +            mViewPager.setCurrentItem(TAB_MAIN); +        } else { +            mTabsAdapter.removeTab(TAB_CERTS); +            mTabsAdapter.removeTab(TAB_KEYS); +        } + +        // update layout after operations +        mSlidingTabLayout.setViewPager(mViewPager); +    } + +    private Uri getDataUri() { +        Uri dataUri = getIntent().getData(); +        if (dataUri != null && dataUri.getHost().equals(ContactsContract.AUTHORITY)) { +            dataUri = ContactHelper.dataUriFromContactUri(this, dataUri); +        } +        return dataUri;      }      private void loadData(Uri dataUri) { @@ -177,6 +239,9 @@ public class ViewKeyActivity extends ActionBarActivity implements      public boolean onCreateOptionsMenu(Menu menu) {          super.onCreateOptionsMenu(menu);          getMenuInflater().inflate(R.menu.key_view, menu); + +        MenuItem showAdvancedInfoItem = menu.findItem(R.id.menu_key_view_advanced); +        showAdvancedInfoItem.setChecked(mShowAdvancedTabs);          return true;      } @@ -184,24 +249,37 @@ public class ViewKeyActivity extends ActionBarActivity implements      public boolean onOptionsItemSelected(MenuItem item) {          try {              switch (item.getItemId()) { -                case android.R.id.home: +                case android.R.id.home: {                      Intent homeIntent = new Intent(this, KeyListActivity.class);                      homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);                      startActivity(homeIntent);                      return true; -                case R.id.menu_key_view_update: +                } +                case R.id.menu_key_view_update: {                      updateFromKeyserver(mDataUri, mProviderHelper);                      return true; -                case R.id.menu_key_view_export_keyserver: +                } +                case R.id.menu_key_view_export_keyserver: {                      uploadToKeyserver(mDataUri);                      return true; -                case R.id.menu_key_view_export_file: +                } +                case R.id.menu_key_view_export_file: {                      exportToFile(mDataUri, mExportHelper, mProviderHelper);                      return true; +                }                  case R.id.menu_key_view_delete: {                      deleteKey(mDataUri, mExportHelper);                      return true;                  } +                case R.id.menu_key_view_advanced: { +                    mShowAdvancedTabs = !mShowAdvancedTabs; +                    item.setChecked(mShowAdvancedTabs); +                    if (mShowAdvancedTabs) { +                        addAdvancedTabs(mDataUri); +                    } else { +                        removeAdvancedTabs(); +                    } +                }              }          } catch (ProviderHelper.NotFoundException e) {              AppMsg.makeText(this, R.string.error_key_not_found, AppMsg.STYLE_ALERT).show(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java index 0684efe0f..233b1fca8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java @@ -33,7 +33,6 @@ import android.widget.TextView;  import org.sufficientlysecure.keychain.R;  import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;  import org.sufficientlysecure.keychain.pgp.KeyRing; -import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;  import org.sufficientlysecure.keychain.util.Highlighter;  import java.util.ArrayList; @@ -120,13 +119,13 @@ public class ImportKeysAdapter extends ArrayAdapter<ImportKeysListEntry> {          }          // main user id -        String userId = entry.userIds.get(0); +        String userId = entry.getUserIds().get(0);          String[] userIdSplit = KeyRing.splitUserId(userId);          // name          if (userIdSplit[0] != null) {              // show red user id if it is a secret key -            if (entry.secretKey) { +            if (entry.isSecretKey()) {                  holder.mainUserId.setText(mActivity.getString(R.string.secret_key)                          + " " + userIdSplit[0]);                  holder.mainUserId.setTextColor(Color.RED); @@ -147,30 +146,26 @@ public class ImportKeysAdapter extends ArrayAdapter<ImportKeysListEntry> {              holder.mainUserIdRest.setVisibility(View.GONE);          } -        holder.keyId.setText(entry.keyIdHex); +        holder.keyId.setText(entry.getKeyIdHex()); -        if (entry.fingerprintHex != null) { -            holder.fingerprint.setVisibility(View.VISIBLE); -            holder.fingerprint.setText(PgpKeyHelper.colorizeFingerprint(entry.fingerprintHex)); -        } else { -            holder.fingerprint.setVisibility(View.GONE); -        } +        // don't show full fingerprint on key import +        holder.fingerprint.setVisibility(View.GONE); -        if (entry.bitStrength != 0 && entry.algorithm != null) { -            holder.algorithm.setText("" + entry.bitStrength + "/" + entry.algorithm); +        if (entry.getBitStrength() != 0 && entry.getAlgorithm() != null) { +            holder.algorithm.setText("" + entry.getBitStrength() + "/" + entry.getAlgorithm());              holder.algorithm.setVisibility(View.VISIBLE);          } else {              holder.algorithm.setVisibility(View.INVISIBLE);          } -        if (entry.revoked) { +        if (entry.isRevoked()) {              holder.status.setVisibility(View.VISIBLE);              holder.status.setText(R.string.revoked);          } else {              holder.status.setVisibility(View.GONE);          } -        if (entry.userIds.size() == 1) { +        if (entry.getUserIds().size() == 1) {              holder.userIdsList.setVisibility(View.GONE);          } else {              holder.userIdsList.setVisibility(View.VISIBLE); @@ -178,7 +173,7 @@ public class ImportKeysAdapter extends ArrayAdapter<ImportKeysListEntry> {              // clear view from holder              holder.userIdsList.removeAllViews(); -            Iterator<String> it = entry.userIds.iterator(); +            Iterator<String> it = entry.getUserIds().iterator();              // skip primary user id              it.next();              while (it.hasNext()) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/PagerTabStripAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/PagerTabStripAdapter.java index 977740567..3e3098b10 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/PagerTabStripAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/PagerTabStripAdapter.java @@ -20,8 +20,13 @@ package org.sufficientlysecure.keychain.ui.adapter;  import android.app.Activity;  import android.os.Bundle;  import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager;  import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.app.FragmentTransaction;  import android.support.v7.app.ActionBarActivity; +import android.view.ViewGroup; + +import org.sufficientlysecure.keychain.Constants;  import java.util.ArrayList; @@ -52,6 +57,11 @@ public class PagerTabStripAdapter extends FragmentPagerAdapter {          notifyDataSetChanged();      } +    public void removeTab(int index) { +        mTabs.remove(index); +        notifyDataSetChanged(); +    } +      @Override      public int getCount() {          return mTabs.size(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/SlidingTabLayout.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SlidingTabLayout.java index 065034be1..17471c86c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/SlidingTabLayout.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SlidingTabLayout.java @@ -14,7 +14,7 @@   * limitations under the License.   */ -package org.sufficientlysecure.keychain.util; +package org.sufficientlysecure.keychain.ui.widget;  import android.content.Context;  import android.graphics.Typeface; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/SlidingTabStrip.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SlidingTabStrip.java index 9ce6f943e..4c41e12c5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/SlidingTabStrip.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SlidingTabStrip.java @@ -14,7 +14,7 @@   * limitations under the License.   */ -package org.sufficientlysecure.keychain.util; +package org.sufficientlysecure.keychain.ui.widget;  import android.R;  import android.content.Context;  | 
