AlgorithmResolver { public static CipherAlgorithm getCipher(CipherSuite cipherSuite) { String cipher = cipherSuite.toString().toUpperCase(); if (cipher.contains("NULL")) { return CipherAlgorithm.NULL; } else if (cipher.contains("IDEA")) { return CipherAlgorithm.IDEA_128; } else if (cipher.contains("RC2")) { return CipherAlgorithm.RC2_128; } else if (cipher.contains("RC4")) { return CipherAlgorithm.RC4_128; } else if (cipher.contains("DES_EDE_CBC")) { return CipherAlgorithm.DES_EDE_CBC; } else if (cipher.contains("AES_128_CBC")) { return CipherAlgorithm.AES_128_CBC; } else if (cipher.contains("AES_256_CBC")) { return CipherAlgorithm.AES_256_CBC; } else if (cipher.contains("AES_128_GCM")) { return CipherAlgorithm.AES_128_GCM; } else if (cipher.contains("AES_256_GCM")) { return CipherAlgorithm.AES_256_GCM; } else if (cipher.contains("AES_128_CCM")) { return CipherAlgorithm.AES_128_CCM; } else if (cipher.contains("AES_256_CCM")) { return CipherAlgorithm.AES_256_CCM; } else if (cipher.contains("CAMELLIA_128_CBC")) { return CipherAlgorithm.CAMELLIA_128_CBC; } else if (cipher.contains("CAMELLIA_256_CBC")) { return CipherAlgorithm.CAMELLIA_256_CBC; } else if (cipher.contains("CAMELLIA_128_GCM")) { return CipherAlgorithm.CAMELLIA_128_GCM; } else if (cipher.contains("CAMELLIA_256_GCM")) { return CipherAlgorithm.CAMELLIA_256_GCM; } else if (cipher.contains("SEED_CBC")) { return CipherAlgorithm.SEED_CBC; } else if (cipher.contains("DES40_CBC")) { return CipherAlgorithm.DES40_CBC; } else if (cipher.contains("DES_CBC")) { return CipherAlgorithm.DES_CBC; } else if (cipher.contains("WITH_FORTEZZA_CBC")) { return CipherAlgorithm.FORTEZZA_CBC; } else if (cipher.contains("ARIA_128_CBC")) { return CipherAlgorithm.ARIA_128_CBC; } else if (cipher.contains("ARIA_256_CBC")) { return CipherAlgorithm.ARIA_256_CBC; } else if (cipher.contains("ARIA_128_GCM")) { return CipherAlgorithm.ARIA_128_GCM; } else if (cipher.contains("ARIA_256_GCM")) { return CipherAlgorithm.ARIA_256_GCM; } else if (cipher.contains("28147_CNT")) { return CipherAlgorithm.GOST_28147_CNT; } else if (cipher.contains("CHACHA20_POLY1305")) { return CipherAlgorithm.ChaCha20Poly1305; } if (cipherSuite == CipherSuite.TLS_FALLBACK_SCSV || cipherSuite == CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV) { throw new UnsupportedOperationException("The CipherSuite:" + cipherSuite.name() + " does not specify a Cipher"); } throw new UnsupportedOperationException("The cipher algorithm in " + cipherSuite + " 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 testGetCipher() { assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_NULL_WITH_NULL_NULL) == CipherAlgorithm.NULL); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_RSA_WITH_IDEA_CBC_SHA) == CipherAlgorithm.IDEA_128); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5) == CipherAlgorithm.RC2_128); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_RSA_WITH_RC4_128_SHA) == CipherAlgorithm.RC4_128); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA) == CipherAlgorithm.DES_EDE_CBC); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA) == CipherAlgorithm.AES_128_CBC); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA) == CipherAlgorithm.AES_256_CBC); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_RSA_WITH_AES_128_CCM) == CipherAlgorithm.AES_128_CCM); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_RSA_WITH_AES_256_CCM) == CipherAlgorithm.AES_256_CCM); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256) == CipherAlgorithm.AES_128_GCM); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_DH_anon_WITH_AES_256_GCM_SHA384) == CipherAlgorithm.AES_256_GCM); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256) == CipherAlgorithm.CAMELLIA_128_CBC); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256) == CipherAlgorithm.CAMELLIA_128_GCM); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384) == CipherAlgorithm.CAMELLIA_256_CBC); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384) == CipherAlgorithm.CAMELLIA_256_GCM); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_RSA_WITH_SEED_CBC_SHA) == CipherAlgorithm.SEED_CBC); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA) == CipherAlgorithm.DES40_CBC); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_RSA_WITH_DES_CBC_SHA) == CipherAlgorithm.DES_CBC); assertTrue(AlgorithmResolver.getCipher(CipherSuite.SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA) == CipherAlgorithm.FORTEZZA_CBC); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_RSA_WITH_ARIA_128_CBC_SHA256) == CipherAlgorithm.ARIA_128_CBC); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_RSA_WITH_ARIA_128_GCM_SHA256) == CipherAlgorithm.ARIA_128_GCM); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_DH_anon_WITH_ARIA_256_CBC_SHA384) == CipherAlgorithm.ARIA_256_CBC); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_RSA_WITH_ARIA_256_GCM_SHA384) == CipherAlgorithm.ARIA_256_GCM); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_GOSTR341094_WITH_28147_CNT_IMIT) == CipherAlgorithm.GOST_28147_CNT); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256) == CipherAlgorithm.ChaCha20Poly1305); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256) == CipherAlgorithm.ChaCha20Poly1305); assertTrue(AlgorithmResolver.getCipher(CipherSuite.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) == CipherAlgorithm.ChaCha20Poly1305); }
@Test public void testGetAllCipher() { for (CipherSuite suite : CipherSuite.values()) { try { AlgorithmResolver.getCipher(suite); } catch (UnsupportedOperationException E) { } } }
@Test(expected = UnsupportedOperationException.class) public void testUnresolvableCipherUnknown() { AlgorithmResolver.getCipher(CipherSuite.TLS_FALLBACK_SCSV); }
@Test(expected = UnsupportedOperationException.class) public void testUnresolvableCipherReno() { AlgorithmResolver.getCipher(CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV); }
@Test(expected = UnsupportedOperationException.class) public void testUnresolvableCipherFallback() { AlgorithmResolver.getCipher(CipherSuite.TLS_FALLBACK_SCSV); }