aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper
diff options
context:
space:
mode:
authormar-v-in <github@rvin.mooo.com>2014-07-01 00:15:30 +0200
committermar-v-in <github@rvin.mooo.com>2014-07-01 00:15:30 +0200
commitd1d414c006aa6b7b80b25b34f0c75a92a9603a85 (patch)
tree6375bd8a5436c866824190318d10d183d8548ab6 /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper
parent38823b73a31ae0fb82d7e385c141f1170127c4dd (diff)
parenta9010045889d307ede37d154b0a2fece566c1845 (diff)
downloadopen-keychain-d1d414c006aa6b7b80b25b34f0c75a92a9603a85.tar.gz
open-keychain-d1d414c006aa6b7b80b25b34f0c75a92a9603a85.tar.bz2
open-keychain-d1d414c006aa6b7b80b25b34f0c75a92a9603a85.zip
Merge branch 'master' into improve-file-more
Conflicts: OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFileFragment.java OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileFragment.java OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/FileDialogFragment.java
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java137
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java18
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/TlsHelper.java129
3 files changed, 282 insertions, 2 deletions
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 d8a7e8427..e639824ec 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java
@@ -22,8 +22,10 @@ import android.accounts.AccountManager;
import android.content.*;
import android.database.Cursor;
import android.net.Uri;
+import android.os.Build;
import android.provider.ContactsContract;
import android.util.Patterns;
+
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.pgp.KeyRing;
@@ -58,7 +60,27 @@ public class ContactHelper {
ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?";
public static final String ID_SELECTION = ContactsContract.RawContacts._ID + "=?";
- public static List<String> getMailAccounts(Context context) {
+ public static List<String> getPossibleUserEmails(Context context) {
+ Set<String> accountMails = getAccountEmails(context);
+ accountMails.addAll(getMainProfileContactEmails(context));
+ // now return the Set (without duplicates) as a List
+ return new ArrayList<String>(accountMails);
+ }
+
+ public static List<String> getPossibleUserNames(Context context) {
+ Set<String> accountMails = getAccountEmails(context);
+ Set<String> names = getContactNamesFromEmails(context, accountMails);
+ names.addAll(getMainProfileContactName(context));
+ return new ArrayList<String>(names);
+ }
+
+ /**
+ * Get emails from AccountManager
+ *
+ * @param context
+ * @return
+ */
+ private static Set<String> getAccountEmails(Context context) {
final Account[] accounts = AccountManager.get(context).getAccounts();
final Set<String> emailSet = new HashSet<String>();
for (Account account : accounts) {
@@ -66,7 +88,118 @@ public class ContactHelper {
emailSet.add(account.name);
}
}
- return new ArrayList<String>(emailSet);
+ return emailSet;
+ }
+
+ /**
+ * Search for contact names based on a list of emails (to find out the names of the
+ * device owner based on the email addresses from AccountsManager)
+ *
+ * @param context
+ * @param emails
+ * @return
+ */
+ private static Set<String> getContactNamesFromEmails(Context context, Set<String> emails) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
+ Set<String> names = new HashSet<String>();
+ for (String email : emails) {
+ ContentResolver resolver = context.getContentResolver();
+ Cursor profileCursor = resolver.query(
+ ContactsContract.CommonDataKinds.Email.CONTENT_URI,
+ new String[]{ContactsContract.CommonDataKinds.Email.ADDRESS,
+ ContactsContract.Contacts.DISPLAY_NAME},
+ ContactsContract.CommonDataKinds.Email.ADDRESS + "=?",
+ new String[]{email}, null
+ );
+ if (profileCursor == null) return null;
+
+ Set<String> currNames = new HashSet<String>();
+ while (profileCursor.moveToNext()) {
+ String name = profileCursor.getString(1);
+ Log.d(Constants.TAG, "name" + name);
+ if (name != null) {
+ currNames.add(name);
+ }
+ }
+ profileCursor.close();
+ names.addAll(currNames);
+ }
+ return names;
+ } else {
+ return new HashSet<String>();
+ }
+ }
+
+ /**
+ * Retrieves the emails of the primary profile contact
+ * http://developer.android.com/reference/android/provider/ContactsContract.Profile.html
+ *
+ * @param context
+ * @return
+ */
+ private static Set<String> getMainProfileContactEmails(Context context) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
+ ContentResolver resolver = context.getContentResolver();
+ Cursor profileCursor = resolver.query(
+ Uri.withAppendedPath(
+ ContactsContract.Profile.CONTENT_URI,
+ ContactsContract.Contacts.Data.CONTENT_DIRECTORY),
+ new String[]{ContactsContract.CommonDataKinds.Email.ADDRESS,
+ ContactsContract.CommonDataKinds.Email.IS_PRIMARY},
+
+ // Selects only email addresses
+ ContactsContract.Contacts.Data.MIMETYPE + "=?",
+ new String[]{
+ ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
+ },
+ // Show primary rows first. Note that there won't be a primary email address if the
+ // user hasn't specified one.
+ ContactsContract.Contacts.Data.IS_PRIMARY + " DESC"
+ );
+ if (profileCursor == null) return null;
+
+ Set<String> emails = new HashSet<String>();
+ while (profileCursor.moveToNext()) {
+ String email = profileCursor.getString(0);
+ if (email != null) {
+ emails.add(email);
+ }
+ }
+ profileCursor.close();
+ return emails;
+ } else {
+ return new HashSet<String>();
+ }
+ }
+
+ /**
+ * Retrieves the name of the primary profile contact
+ * http://developer.android.com/reference/android/provider/ContactsContract.Profile.html
+ *
+ * @param context
+ * @return
+ */
+ private static List<String> getMainProfileContactName(Context context) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
+ ContentResolver resolver = context.getContentResolver();
+ Cursor profileCursor = resolver.query(ContactsContract.Profile.CONTENT_URI,
+ new String[]{ContactsContract.Profile.DISPLAY_NAME},
+ null, null, null);
+ if (profileCursor == null) return null;
+
+ Set<String> names = new HashSet<String>();
+ // should only contain one entry!
+ while (profileCursor.moveToNext()) {
+ String name = profileCursor.getString(0);
+ if (name != null) {
+ names.add(name);
+ }
+ }
+ profileCursor.close();
+ return new ArrayList<String>(names);
+ } else {
+ return new ArrayList<String>();
+ }
}
public static List<String> getContactMails(Context context) {
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java
index c82c5e115..6f3d38ccd 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java
@@ -169,4 +169,22 @@ public class Preferences {
editor.putString(Constants.Pref.KEY_SERVERS, rawData);
editor.commit();
}
+
+ public void updateKeyServers() {
+ if (mSharedPreferences.getInt(Constants.Pref.KEY_SERVERS_DEFAULT_VERSION, 0) !=
+ Constants.Defaults.KEY_SERVERS_VERSION) {
+ String[] servers = getKeyServers();
+ for (int i = 0; i < servers.length; i++) {
+ if (servers[i].equals("pool.sks-keyservers.net")) {
+ servers[i] = "hkps://hkps.pool.sks-keyservers.net";
+ } else if (servers[i].equals("pgp.mit.edu")) {
+ servers[i] = "hkps://pgp.mit.edu";
+ }
+ }
+ setKeyServers(servers);
+ mSharedPreferences.edit()
+ .putInt(Constants.Pref.KEY_SERVERS_DEFAULT_VERSION, Constants.Defaults.KEY_SERVERS_VERSION)
+ .commit();
+ }
+ }
}
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/TlsHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/TlsHelper.java
new file mode 100644
index 000000000..4b09af04d
--- /dev/null
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/TlsHelper.java
@@ -0,0 +1,129 @@
+/*
+ * Copyright (C) 2013-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.res.AssetManager;
+import org.sufficientlysecure.keychain.Constants;
+import org.sufficientlysecure.keychain.util.Log;
+
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.security.KeyManagementException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.*;
+import java.util.HashMap;
+import java.util.Map;
+
+public class TlsHelper {
+
+ public static class TlsHelperException extends Exception {
+ public TlsHelperException(Exception e) {
+ super(e);
+ }
+ }
+
+ private static Map<String, byte[]> sStaticCA = new HashMap<String, byte[]>();
+
+ public static void addStaticCA(String domain, byte[] certificate) {
+ sStaticCA.put(domain, certificate);
+ }
+
+ public static void addStaticCA(String domain, AssetManager assetManager, String name) {
+ try {
+ InputStream is = assetManager.open(name);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ int reads = is.read();
+
+ while(reads != -1){
+ baos.write(reads);
+ reads = is.read();
+ }
+
+ is.close();
+
+ addStaticCA(domain, baos.toByteArray());
+ } catch (IOException e) {
+ Log.w(Constants.TAG, e);
+ }
+ }
+
+ public static URLConnection openConnection(URL url) throws IOException, TlsHelperException {
+ if (url.getProtocol().equals("https")) {
+ for (String domain : sStaticCA.keySet()) {
+ if (url.getHost().endsWith(domain)) {
+ return openCAConnection(sStaticCA.get(domain), url);
+ }
+ }
+ }
+ return url.openConnection();
+ }
+
+ /**
+ * Opens a Connection that will only accept certificates signed with a specific CA and skips common name check.
+ * This is required for some distributed Keyserver networks like sks-keyservers.net
+ *
+ * @param certificate The X.509 certificate used to sign the servers certificate
+ * @param url Connection target
+ */
+ public static HttpsURLConnection openCAConnection(byte[] certificate, URL url)
+ throws TlsHelperException, IOException {
+ try {
+ // Load CA
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ Certificate ca = cf.generateCertificate(new ByteArrayInputStream(certificate));
+
+ // Create a KeyStore containing our trusted CAs
+ String keyStoreType = KeyStore.getDefaultType();
+ KeyStore keyStore = KeyStore.getInstance(keyStoreType);
+ keyStore.load(null, null);
+ keyStore.setCertificateEntry("ca", ca);
+
+ // Create a TrustManager that trusts the CAs in our KeyStore
+ String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
+ TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
+ tmf.init(keyStore);
+
+ // Create an SSLContext that uses our TrustManager
+ SSLContext context = SSLContext.getInstance("TLS");
+ context.init(null, tmf.getTrustManagers(), null);
+
+ // Tell the URLConnection to use a SocketFactory from our SSLContext
+ HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
+ urlConnection.setSSLSocketFactory(context.getSocketFactory());
+
+ return urlConnection;
+ } catch (CertificateException e) {
+ throw new TlsHelperException(e);
+ } catch (NoSuchAlgorithmException e) {
+ throw new TlsHelperException(e);
+ } catch (KeyStoreException e) {
+ throw new TlsHelperException(e);
+ } catch (KeyManagementException e) {
+ throw new TlsHelperException(e);
+ }
+ }
+}