HandlerFactory { private static ClientKeyExchangeHandler getClientKeyExchangeHandler(TlsContext context) { CipherSuite cs = context.getChooser().getSelectedCipherSuite(); KeyExchangeAlgorithm algorithm = AlgorithmResolver.getKeyExchangeAlgorithm(cs); switch (algorithm) { case RSA: return new RSAClientKeyExchangeHandler(context); case ECDHE_ECDSA: case ECDH_ECDSA: case ECDH_RSA: case ECDHE_RSA: return new ECDHClientKeyExchangeHandler(context); case DHE_DSS: case DHE_RSA: case DH_ANON: case DH_DSS: case DH_RSA: return new DHClientKeyExchangeHandler(context); case DHE_PSK: return new PskDhClientKeyExchangeHandler(context); case ECDHE_PSK: return new PskEcDhClientKeyExchangeHandler(context); case PSK_RSA: return new PskRsaClientKeyExchangeHandler(context); case PSK: return new PskClientKeyExchangeHandler(context); case SRP_SHA_DSS: case SRP_SHA_RSA: case SRP_SHA: return new SrpClientKeyExchangeHandler(context); case VKO_GOST01: case VKO_GOST12: return new GOSTClientKeyExchangeHandler(context); case ECCPWD: return new PWDClientKeyExchangeHandler(context); default: throw new UnsupportedOperationException("Algorithm " + algorithm + " NOT supported yet."); } } private  HandlerFactory(); static ProtocolMessageHandler getHandler(TlsContext context, ProtocolMessageType protocolType,
            HandshakeMessageType handshakeType); static HandshakeMessageHandler getHandshakeHandler(TlsContext context, HandshakeMessageType type); static ExtensionHandler getExtensionHandler(TlsContext context, ExtensionType type,
            HandshakeMessageType handshakeMessageType);  }
@Test public void getClientKeyExchangeHandler() { context.setSelectedCipherSuite(CipherSuite.TLS_RSA_WITH_NULL_MD5); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof RSAClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_ECDHE_RSA_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof ECDHClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_ECDH_RSA_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof ECDHClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_ECDH_ECDSA_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof ECDHClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_ECDHE_ECDSA_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof ECDHClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof DHClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof DHClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_DH_anon_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof DHClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_DH_DSS_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof DHClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_DH_RSA_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof DHClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof PskDhClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_ECDHE_PSK_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof PskEcDhClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_RSA_PSK_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof PskRsaClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_PSK_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof PskClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof SrpClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof SrpClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_SRP_SHA_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof SrpClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_GOSTR341001_WITH_28147_CNT_IMIT); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof GOSTClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_GOSTR341112_256_WITH_28147_CNT_IMIT); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof GOSTClientKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_ECCPWD_WITH_AES_128_GCM_SHA256); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.CLIENT_KEY_EXCHANGE) instanceof PWDClientKeyExchangeHandler); }