AlgorithmResolver { public static MacAlgorithm getMacAlgorithm(ProtocolVersion protocolVersion, CipherSuite cipherSuite) { MacAlgorithm result = null; if (getCipherType(cipherSuite) == CipherType.AEAD) { result = MacAlgorithm.AEAD; } else { String cipher = cipherSuite.toString(); if (cipher.contains("MD5")) { if (protocolVersion.isSSL()) { result = MacAlgorithm.SSLMAC_MD5; } else { result = MacAlgorithm.HMAC_MD5; } } else if (cipher.endsWith("SHA")) { if (protocolVersion.isSSL()) { result = MacAlgorithm.SSLMAC_SHA1; } else { result = MacAlgorithm.HMAC_SHA1; } } else if (cipher.contains("SHA256")) { result = MacAlgorithm.HMAC_SHA256; } else if (cipher.contains("SHA384")) { result = MacAlgorithm.HMAC_SHA384; } else if (cipher.contains("SHA512")) { result = MacAlgorithm.HMAC_SHA512; } else if (cipher.endsWith("NULL")) { result = MacAlgorithm.NULL; } else if (cipher.endsWith("IMIT")) { result = MacAlgorithm.IMIT_GOST28147; } else if (cipherSuite.usesGOSTR3411()) { result = MacAlgorithm.HMAC_GOSTR3411; } else if (cipherSuite.usesGOSTR34112012()) { result = MacAlgorithm.HMAC_GOSTR3411_2012_256; } } if (cipherSuite == CipherSuite.TLS_FALLBACK_SCSV || cipherSuite == CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV) { throw new UnsupportedOperationException("The CipherSuite:" + cipherSuite.name() + " does not specify a MAC-Algorithm"); } if (result != null) { LOGGER.debug("Using the following Mac Algorithm: {}", result); return result; } else { throw new UnsupportedOperationException("The Mac algorithm for cipher " + 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 testGetMacAlgorithm() { assertEquals(AlgorithmResolver.getMacAlgorithm(ProtocolVersion.TLS12, CipherSuite.TLS_GOSTR341094_WITH_28147_CNT_IMIT), MacAlgorithm.IMIT_GOST28147); assertEquals(AlgorithmResolver.getMacAlgorithm(ProtocolVersion.TLS12, CipherSuite.TLS_GOSTR341001_WITH_NULL_GOSTR3411), MacAlgorithm.HMAC_GOSTR3411); assertEquals(AlgorithmResolver.getMacAlgorithm(ProtocolVersion.TLS12, CipherSuite.TLS_GOSTR341112_256_WITH_NULL_GOSTR3411), MacAlgorithm.HMAC_GOSTR3411_2012_256); }
@Test public void getAllMacAlgorithms() { for (CipherSuite suite : CipherSuite.values()) { try { AlgorithmResolver.getMacAlgorithm(ProtocolVersion.SSL3, suite); AlgorithmResolver.getMacAlgorithm(ProtocolVersion.TLS12, suite); } catch (UnsupportedOperationException E) { } } }
@Test(expected = UnsupportedOperationException.class) public void testUnresolvableMACUnknown() { AlgorithmResolver.getMacAlgorithm(ProtocolVersion.TLS12, CipherSuite.TLS_FALLBACK_SCSV); }
@Test(expected = UnsupportedOperationException.class) public void testUnresolvableMACReno() { AlgorithmResolver.getMacAlgorithm(ProtocolVersion.TLS12, CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV); }
@Test(expected = UnsupportedOperationException.class) public void testUnresolvableMACFallback() { AlgorithmResolver.getMacAlgorithm(ProtocolVersion.TLS12, CipherSuite.TLS_FALLBACK_SCSV); }