1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-24 02:12:15 -05:00

extracted method filterBySupport

This commit is contained in:
András Veres-Szentkirályi 2013-10-20 10:30:33 +02:00
parent dbc47c7297
commit 77d43fb7bd

View File

@ -25,43 +25,49 @@ public class TrustedSocketFactory implements LayeredSocketFactory {
protected static final String ENABLED_CIPHERS[]; protected static final String ENABLED_CIPHERS[];
static { static {
List<String> enabledCiphers = new ArrayList<String>(); String preferredCiphers[] = {
try { "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
String preferredCiphers[] = { "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", "TLS_DHE_RSA_WITH_AES_128_CBC_SHA",
"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", "TLS_DHE_RSA_WITH_AES_256_CBC_SHA",
"TLS_DHE_RSA_WITH_AES_128_CBC_SHA", "TLS_DHE_DSS_WITH_AES_128_CBC_SHA",
"TLS_DHE_RSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_RSA_WITH_RC4_128_SHA",
"TLS_DHE_DSS_WITH_AES_128_CBC_SHA", "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA",
"TLS_ECDHE_RSA_WITH_RC4_128_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA",
"TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", "TLS_RSA_WITH_AES_256_CBC_SHA",
"TLS_RSA_WITH_AES_128_CBC_SHA", "SSL_RSA_WITH_3DES_EDE_CBC_SHA",
"TLS_RSA_WITH_AES_256_CBC_SHA", "SSL_RSA_WITH_RC4_128_SHA",
"SSL_RSA_WITH_3DES_EDE_CBC_SHA", "SSL_RSA_WITH_RC4_128_MD5",
"SSL_RSA_WITH_RC4_128_SHA", };
"SSL_RSA_WITH_RC4_128_MD5",
};
String[] supportedCiphers = null;
try {
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, new SecureRandom()); sslContext.init(null, null, new SecureRandom());
SSLSocketFactory sf = sslContext.getSocketFactory(); SSLSocketFactory sf = sslContext.getSocketFactory();
Set<String> supportedCiphers = new HashSet<String>(); supportedCiphers = sf.getSupportedCipherSuites();
Collections.addAll(supportedCiphers, sf.getSupportedCipherSuites());
for (String preferredCipher : preferredCiphers) {
if (supportedCiphers.contains(preferredCipher)) {
enabledCiphers.add(preferredCipher);
}
}
} catch (KeyManagementException kme) { } catch (KeyManagementException kme) {
kme.printStackTrace(); kme.printStackTrace();
} catch (NoSuchAlgorithmException nsae) { } catch (NoSuchAlgorithmException nsae) {
nsae.printStackTrace(); nsae.printStackTrace();
} }
ENABLED_CIPHERS = enabledCiphers.isEmpty() ? null :
enabledCiphers.toArray(new String[enabledCiphers.size()]); ENABLED_CIPHERS = supportedCiphers == null ? null :
filterBySupport(preferredCiphers, supportedCiphers);
}
protected static String[] filterBySupport(String[] preferred, String[] supported) {
List<String> enabled = new ArrayList<String>();
Set<String> available = new HashSet<String>();
Collections.addAll(available, supported);
for (String item : preferred) {
if (available.contains(item)) enabled.add(item);
}
return enabled.toArray(new String[enabled.size()]);
} }
protected static final String ENABLED_PROTOCOLS[] = { protected static final String ENABLED_PROTOCOLS[] = {