AlgorithmResolver { public static KeyExchangeAlgorithm getKeyExchangeAlgorithm(CipherSuite cipherSuite) { if (cipherSuite.isTLS13()) { return null; } String cipher = cipherSuite.toString().toUpperCase(); if (cipher.contains("TLS_RSA_WITH") || cipher.contains("TLS_RSA_EXPORT")) { return KeyExchangeAlgorithm.RSA; } else if (cipher.contains("TLS_RSA_PSK_")) { return KeyExchangeAlgorithm.PSK_RSA; } else if (cipher.startsWith("TLS_DH_DSS_")) { return KeyExchangeAlgorithm.DH_DSS; } else if (cipher.startsWith("TLS_DH_RSA_")) { return KeyExchangeAlgorithm.DH_RSA; } else if (cipher.startsWith("TLS_DHE_DSS_")) { return KeyExchangeAlgorithm.DHE_DSS; } else if (cipher.contains("TLS_DHE_RSA_")) { return KeyExchangeAlgorithm.DHE_RSA; } else if (cipher.contains("TLS_DHE_PSK") || cipher.contains("TLS_PSK_DHE")) { return KeyExchangeAlgorithm.DHE_PSK; } else if (cipher.startsWith("TLS_DH_ANON_")) { return KeyExchangeAlgorithm.DH_ANON; } else if (cipher.contains("TLS_ECDHE_RSA")) { return KeyExchangeAlgorithm.ECDHE_RSA; } else if (cipher.contains("TLS_ECDHE_ECDSA")) { return KeyExchangeAlgorithm.ECDHE_ECDSA; } else if (cipher.contains("TLS_ECDH_ANON")) { return KeyExchangeAlgorithm.ECDH_ANON; } else if (cipher.contains("TLS_ECDH_ECDSA")) { return KeyExchangeAlgorithm.ECDH_ECDSA; } else if (cipher.contains("TLS_ECDH_RSA")) { return KeyExchangeAlgorithm.ECDH_RSA; } else if (cipher.contains("TLS_ECDHE_PSK")) { return KeyExchangeAlgorithm.ECDHE_PSK; } else if (cipher.startsWith("TLS_NULL_")) { return KeyExchangeAlgorithm.NULL; } else if (cipher.startsWith("TLS_KRB5_")) { return KeyExchangeAlgorithm.KRB5; } else if (cipher.contains("TLS_PSK_")) { return KeyExchangeAlgorithm.PSK; } else if (cipher.startsWith("TLS_SRP_SHA_RSA")) { return KeyExchangeAlgorithm.SRP_SHA_RSA; } else if (cipher.startsWith("TLS_SRP_SHA_DSS")) { return KeyExchangeAlgorithm.SRP_SHA_DSS; } else if (cipher.startsWith("TLS_SRP_SHA")) { return KeyExchangeAlgorithm.SRP_SHA; } else if (cipher.startsWith("TLS_GOSTR341001_")) { return KeyExchangeAlgorithm.VKO_GOST01; } else if (cipher.startsWith("TLS_GOSTR341112_")) { return KeyExchangeAlgorithm.VKO_GOST12; } else if (cipher.startsWith("TLS_CECPQ1_")) { return KeyExchangeAlgorithm.CECPQ1_ECDSA; } else if (cipher.contains("SSL_FORTEZZA_KEA")) { return KeyExchangeAlgorithm.FORTEZZA_KEA; } else if (cipher.contains("ECMQV_ECNRA")) { return KeyExchangeAlgorithm.ECMQV_ECNRA; } else if (cipher.contains("ECMQV_ECDSA")) { return KeyExchangeAlgorithm.ECMQV_ECDSA; } else if (cipher.contains("ECDH_ECNRA")) { return KeyExchangeAlgorithm.ECDH_ECNRA; } else if (cipher.contains("ECCPWD")) { return KeyExchangeAlgorithm.ECCPWD; } if (cipherSuite == CipherSuite.TLS_FALLBACK_SCSV || cipherSuite == CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV) { throw new UnsupportedOperationException("The CipherSuite:" + cipherSuite.name() + " does not specify a KeyExchangeAlgorithm"); } throw new UnsupportedOperationException("The key exchange algorithm in " + cipherSuite.toString() + " is not supported yet."); } private  AlgorithmResolver(); static PRFAlgorithm getPRFAlgorithm(ProtocolVersion protocolVersion, CipherSuite cipherSuite); static DigestAlgorithm getDigestAlgorithm(ProtocolVersion protocolVersion, CipherSuite cipherSuite); static KeyExchangeAlgorithm getKeyExchangeAlgorithm(CipherSuite cipherSuite); static Set<PublicKeyAlgorithm> getRequiredKeystoreAlgorithms(CipherSuite cipherSuite); static CipherAlgorithm getCipher(CipherSuite cipherSuite); static BulkCipherAlgorithm getBulkCipherAlgorithm(CipherSuite cipherSuite); static CipherType getCipherType(CipherSuite cipherSuite); static MacAlgorithm getMacAlgorithm(ProtocolVersion protocolVersion, CipherSuite cipherSuite); static HKDFAlgorithm getHKDFAlgorithm(CipherSuite cipherSuite);  }
@Test public void testGetKeyExchangeAlgorithm() { assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256) == KeyExchangeAlgorithm.DHE_RSA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.SSL_FORTEZZA_KEA_WITH_NULL_SHA) == KeyExchangeAlgorithm.FORTEZZA_KEA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_CECPQ1_ECDSA_WITH_AES_256_GCM_SHA384) == KeyExchangeAlgorithm.CECPQ1_ECDSA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA) == KeyExchangeAlgorithm.DHE_DSS); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA) == KeyExchangeAlgorithm.DHE_DSS); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA) == KeyExchangeAlgorithm.DHE_PSK); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA256) == KeyExchangeAlgorithm.DHE_RSA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA) == KeyExchangeAlgorithm.DHE_RSA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_DH_DSS_WITH_AES_256_CBC_SHA) == KeyExchangeAlgorithm.DH_DSS); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA) == KeyExchangeAlgorithm.DH_RSA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA) == KeyExchangeAlgorithm.DH_ANON); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8) == KeyExchangeAlgorithm.ECDHE_ECDSA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384) == KeyExchangeAlgorithm.ECDHE_PSK); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256) == KeyExchangeAlgorithm.ECDHE_RSA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA) == KeyExchangeAlgorithm.ECDH_ECDSA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA) == KeyExchangeAlgorithm.ECDH_RSA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_ECDH_anon_WITH_AES_128_CBC_SHA) == KeyExchangeAlgorithm.ECDH_ANON); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_GOSTR341001_WITH_28147_CNT_IMIT) == KeyExchangeAlgorithm.VKO_GOST01); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5) == KeyExchangeAlgorithm.KRB5); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_KRB5_WITH_DES_CBC_SHA) == KeyExchangeAlgorithm.KRB5); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_NULL_WITH_NULL_NULL) == KeyExchangeAlgorithm.NULL); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_PSK_DHE_WITH_AES_128_CCM_8) == KeyExchangeAlgorithm.DHE_PSK); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_PSK_WITH_AES_128_CCM) == KeyExchangeAlgorithm.PSK); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_RSA_EXPORT1024_WITH_RC4_56_MD5) == KeyExchangeAlgorithm.RSA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_RSA_PSK_WITH_AES_256_CBC_SHA) == KeyExchangeAlgorithm.PSK_RSA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256) == KeyExchangeAlgorithm.RSA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA) == KeyExchangeAlgorithm.SRP_SHA_DSS); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA) == KeyExchangeAlgorithm.SRP_SHA_RSA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA) == KeyExchangeAlgorithm.SRP_SHA); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.UNOFFICIAL_TLS_ECMQV_ECNRA_WITH_DES_CBC_SHA) == KeyExchangeAlgorithm.ECMQV_ECNRA); assertTrue(AlgorithmResolver .getKeyExchangeAlgorithm(CipherSuite.UNOFFICIAL_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA) == KeyExchangeAlgorithm.ECDH_ECDSA); assertTrue(AlgorithmResolver .getKeyExchangeAlgorithm(CipherSuite.UNOFFICIAL_TLS_ECDH_anon_EXPORT_WITH_RC4_40_SHA) == KeyExchangeAlgorithm.ECDH_ANON); assertTrue(AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_AES_128_GCM_SHA256) == null); }
@Test(expected = UnsupportedOperationException.class) public void testUnresolvableKeyExchangeUnknown() { AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_FALLBACK_SCSV); }
@Test(expected = UnsupportedOperationException.class) public void testUnresolvableKeyExchangeReno() { AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV); }
@Test(expected = UnsupportedOperationException.class) public void testUnresolvableKeyExchangeFallback() { AlgorithmResolver.getKeyExchangeAlgorithm(CipherSuite.TLS_FALLBACK_SCSV); }
@Test public void testAllCipherSuitesGetKeyExchange() { for (CipherSuite suite : CipherSuite.values()) { try { AlgorithmResolver.getKeyExchangeAlgorithm(suite); } catch (UnsupportedOperationException E) { } } }