@Test public void testAddSSLSettings() throws Exception { PowerMockito.mockStatic(SSLContext.class); PowerMockito.doReturn(sslContextMock).when(SSLContext.class, "getInstance", anyString()); PowerMockito.whenNew(EasyX509TrustManager.class).withNoArguments().thenReturn(easyX509TrustManagerMock); PowerMockito.mockStatic(SSLUtils.class); PowerMockito.whenNew(URL.class).withArguments(anyString()).thenReturn(urlMock); PowerMockito.doReturn(keyStoreMock).when(SSLUtils.class, "createKeyStore", anyObject(), anyString()); PowerMockito.doReturn(null).when(SSLUtils.class, "createAuthTrustManagers", anyObject()); PowerMockito.doReturn(null).when(SSLUtils.class, "createKeyManagers", anyObject(), anyObject()); PowerMockito.whenNew(SecureRandom.class).withNoArguments().thenReturn(secureRandomMock); doNothing().when(sslContextMock).init(null, null, secureRandomMock); PowerMockito.doNothing().when(SSLContext.class, "setDefault", sslContextMock); PowerMockito.doCallRealMethod().when(SSLUtils.class, "addSSLSettings", anyBoolean(), anyString(), anyString(), anyString(), anyString()); SSLUtils.addSSLSettings(true, "HDD:\\keystore", "keystorePass", "HDD:\\trustore", "truststorePass"); PowerMockito.verifyStatic(); SSLContext.getInstance(anyString()); PowerMockito.verifyNew(EasyX509TrustManager.class).withNoArguments(); PowerMockito.verifyStatic(); SSLContext.getInstance(anyString()); SSLContext.setDefault(sslContextMock); SSLUtils.createKeyStore(Matchers.<URL>any(), anyString()); SSLUtils.createAuthTrustManagers(Matchers.<KeyStore>anyObject()); SSLUtils.createKeyManagers(Matchers.<KeyStore>any(), anyString()); SSLContext.setDefault(sslContextMock); PowerMockito.verifyNew(SecureRandom.class).withNoArguments(); }
public static void addSSLSettings(boolean trustAllRoots, String keystore, String keystorePassword, String trustKeystore, String trustPassword) throws Exception { boolean useClientCert = false; boolean useTrustCert = false; String separator = System.getProperty(PropNames.FILE_SEPARATOR); String javaKeystore = System.getProperty(PropNames.JAVA_HOME) + separator + "lib" + separator + "security" + separator + "cacerts"; if (keystore.length() == 0 && !trustAllRoots) { boolean storeExists = new File(javaKeystore).exists(); keystore = (storeExists) ? Constants.FILE + javaKeystore : null; if (null != keystorePassword) { if (StringUtils.EMPTY.equals(keystorePassword)) { keystorePassword = SecurityConstants.DEFAULT_PASSWORD_FOR_STORE; } } useClientCert = storeExists; } else { if (!trustAllRoots) { if (!keystore.startsWith(Constants.HTTP)) keystore = Constants.FILE + keystore; useClientCert = true; } } if (trustKeystore.length() == 0 && !trustAllRoots) { boolean storeExists = new File(javaKeystore).exists(); trustKeystore = (storeExists) ? Constants.FILE + javaKeystore : null; if (storeExists) { if (isEmpty(trustPassword)) { trustPassword = SecurityConstants.DEFAULT_PASSWORD_FOR_STORE; } } else { trustPassword = null; } useTrustCert = storeExists; } else { if (!trustAllRoots) { if (!trustKeystore.startsWith(Constants.HTTP)) trustKeystore = Constants.FILE + trustKeystore; useTrustCert = true; } } TrustManager[] trustManagers = null; KeyManager[] keyManagers = null; if (trustAllRoots) { trustManagers = new TrustManager[]{new EasyX509TrustManager()}; } if (useTrustCert) { KeyStore trustKeyStore = SSLUtils.createKeyStore(new URL(trustKeystore), trustPassword); trustManagers = SSLUtils.createAuthTrustManagers(trustKeyStore); } if (useClientCert) { KeyStore clientKeyStore = SSLUtils.createKeyStore(new URL(keystore), keystorePassword); keyManagers = SSLUtils.createKeyManagers(clientKeyStore, keystorePassword); } SSLContext context = SSLContext.getInstance(SecurityConstants.SSL); context.init(keyManagers, trustManagers, new SecureRandom()); SSLContext.setDefault(context); }
SSLUtils { public static void addSSLSettings(boolean trustAllRoots, String keystore, String keystorePassword, String trustKeystore, String trustPassword) throws Exception { boolean useClientCert = false; boolean useTrustCert = false; String separator = System.getProperty(PropNames.FILE_SEPARATOR); String javaKeystore = System.getProperty(PropNames.JAVA_HOME) + separator + "lib" + separator + "security" + separator + "cacerts"; if (keystore.length() == 0 && !trustAllRoots) { boolean storeExists = new File(javaKeystore).exists(); keystore = (storeExists) ? Constants.FILE + javaKeystore : null; if (null != keystorePassword) { if (StringUtils.EMPTY.equals(keystorePassword)) { keystorePassword = SecurityConstants.DEFAULT_PASSWORD_FOR_STORE; } } useClientCert = storeExists; } else { if (!trustAllRoots) { if (!keystore.startsWith(Constants.HTTP)) keystore = Constants.FILE + keystore; useClientCert = true; } } if (trustKeystore.length() == 0 && !trustAllRoots) { boolean storeExists = new File(javaKeystore).exists(); trustKeystore = (storeExists) ? Constants.FILE + javaKeystore : null; if (storeExists) { if (isEmpty(trustPassword)) { trustPassword = SecurityConstants.DEFAULT_PASSWORD_FOR_STORE; } } else { trustPassword = null; } useTrustCert = storeExists; } else { if (!trustAllRoots) { if (!trustKeystore.startsWith(Constants.HTTP)) trustKeystore = Constants.FILE + trustKeystore; useTrustCert = true; } } TrustManager[] trustManagers = null; KeyManager[] keyManagers = null; if (trustAllRoots) { trustManagers = new TrustManager[]{new EasyX509TrustManager()}; } if (useTrustCert) { KeyStore trustKeyStore = SSLUtils.createKeyStore(new URL(trustKeystore), trustPassword); trustManagers = SSLUtils.createAuthTrustManagers(trustKeyStore); } if (useClientCert) { KeyStore clientKeyStore = SSLUtils.createKeyStore(new URL(keystore), keystorePassword); keyManagers = SSLUtils.createKeyManagers(clientKeyStore, keystorePassword); } SSLContext context = SSLContext.getInstance(SecurityConstants.SSL); context.init(keyManagers, trustManagers, new SecureRandom()); SSLContext.setDefault(context); } }
SSLUtils { public static void addSSLSettings(boolean trustAllRoots, String keystore, String keystorePassword, String trustKeystore, String trustPassword) throws Exception { boolean useClientCert = false; boolean useTrustCert = false; String separator = System.getProperty(PropNames.FILE_SEPARATOR); String javaKeystore = System.getProperty(PropNames.JAVA_HOME) + separator + "lib" + separator + "security" + separator + "cacerts"; if (keystore.length() == 0 && !trustAllRoots) { boolean storeExists = new File(javaKeystore).exists(); keystore = (storeExists) ? Constants.FILE + javaKeystore : null; if (null != keystorePassword) { if (StringUtils.EMPTY.equals(keystorePassword)) { keystorePassword = SecurityConstants.DEFAULT_PASSWORD_FOR_STORE; } } useClientCert = storeExists; } else { if (!trustAllRoots) { if (!keystore.startsWith(Constants.HTTP)) keystore = Constants.FILE + keystore; useClientCert = true; } } if (trustKeystore.length() == 0 && !trustAllRoots) { boolean storeExists = new File(javaKeystore).exists(); trustKeystore = (storeExists) ? Constants.FILE + javaKeystore : null; if (storeExists) { if (isEmpty(trustPassword)) { trustPassword = SecurityConstants.DEFAULT_PASSWORD_FOR_STORE; } } else { trustPassword = null; } useTrustCert = storeExists; } else { if (!trustAllRoots) { if (!trustKeystore.startsWith(Constants.HTTP)) trustKeystore = Constants.FILE + trustKeystore; useTrustCert = true; } } TrustManager[] trustManagers = null; KeyManager[] keyManagers = null; if (trustAllRoots) { trustManagers = new TrustManager[]{new EasyX509TrustManager()}; } if (useTrustCert) { KeyStore trustKeyStore = SSLUtils.createKeyStore(new URL(trustKeystore), trustPassword); trustManagers = SSLUtils.createAuthTrustManagers(trustKeyStore); } if (useClientCert) { KeyStore clientKeyStore = SSLUtils.createKeyStore(new URL(keystore), keystorePassword); keyManagers = SSLUtils.createKeyManagers(clientKeyStore, keystorePassword); } SSLContext context = SSLContext.getInstance(SecurityConstants.SSL); context.init(keyManagers, trustManagers, new SecureRandom()); SSLContext.setDefault(context); }  }
SSLUtils { public static void addSSLSettings(boolean trustAllRoots, String keystore, String keystorePassword, String trustKeystore, String trustPassword) throws Exception { boolean useClientCert = false; boolean useTrustCert = false; String separator = System.getProperty(PropNames.FILE_SEPARATOR); String javaKeystore = System.getProperty(PropNames.JAVA_HOME) + separator + "lib" + separator + "security" + separator + "cacerts"; if (keystore.length() == 0 && !trustAllRoots) { boolean storeExists = new File(javaKeystore).exists(); keystore = (storeExists) ? Constants.FILE + javaKeystore : null; if (null != keystorePassword) { if (StringUtils.EMPTY.equals(keystorePassword)) { keystorePassword = SecurityConstants.DEFAULT_PASSWORD_FOR_STORE; } } useClientCert = storeExists; } else { if (!trustAllRoots) { if (!keystore.startsWith(Constants.HTTP)) keystore = Constants.FILE + keystore; useClientCert = true; } } if (trustKeystore.length() == 0 && !trustAllRoots) { boolean storeExists = new File(javaKeystore).exists(); trustKeystore = (storeExists) ? Constants.FILE + javaKeystore : null; if (storeExists) { if (isEmpty(trustPassword)) { trustPassword = SecurityConstants.DEFAULT_PASSWORD_FOR_STORE; } } else { trustPassword = null; } useTrustCert = storeExists; } else { if (!trustAllRoots) { if (!trustKeystore.startsWith(Constants.HTTP)) trustKeystore = Constants.FILE + trustKeystore; useTrustCert = true; } } TrustManager[] trustManagers = null; KeyManager[] keyManagers = null; if (trustAllRoots) { trustManagers = new TrustManager[]{new EasyX509TrustManager()}; } if (useTrustCert) { KeyStore trustKeyStore = SSLUtils.createKeyStore(new URL(trustKeystore), trustPassword); trustManagers = SSLUtils.createAuthTrustManagers(trustKeyStore); } if (useClientCert) { KeyStore clientKeyStore = SSLUtils.createKeyStore(new URL(keystore), keystorePassword); keyManagers = SSLUtils.createKeyManagers(clientKeyStore, keystorePassword); } SSLContext context = SSLContext.getInstance(SecurityConstants.SSL); context.init(keyManagers, trustManagers, new SecureRandom()); SSLContext.setDefault(context); }  static KeyStore createKeyStore(final URL url, final String password); static KeyManager[] createKeyManagers(final KeyStore keystore, final String password); static TrustManager[] createAuthTrustManagers(final KeyStore keystore); static Store createMessageStore(GetMailInput input); static void addSSLSettings(boolean trustAllRoots, String keystore,
                                      String keystorePassword, String trustKeystore, String trustPassword); static Store configureStoreWithSSL(Properties props, Authenticator auth, MailInput input); static Store tryTLSOtherwiseTrySSL(Authenticator auth, Properties props, GetMailInput input); static Store configureStoreWithTLS(Properties props, Authenticator auth, MailInput input); static void configureWithTLS(Properties props, final MailInput input); static void clearTLSProperties(Properties props, MailInput input); }
SSLUtils { public static void addSSLSettings(boolean trustAllRoots, String keystore, String keystorePassword, String trustKeystore, String trustPassword) throws Exception { boolean useClientCert = false; boolean useTrustCert = false; String separator = System.getProperty(PropNames.FILE_SEPARATOR); String javaKeystore = System.getProperty(PropNames.JAVA_HOME) + separator + "lib" + separator + "security" + separator + "cacerts"; if (keystore.length() == 0 && !trustAllRoots) { boolean storeExists = new File(javaKeystore).exists(); keystore = (storeExists) ? Constants.FILE + javaKeystore : null; if (null != keystorePassword) { if (StringUtils.EMPTY.equals(keystorePassword)) { keystorePassword = SecurityConstants.DEFAULT_PASSWORD_FOR_STORE; } } useClientCert = storeExists; } else { if (!trustAllRoots) { if (!keystore.startsWith(Constants.HTTP)) keystore = Constants.FILE + keystore; useClientCert = true; } } if (trustKeystore.length() == 0 && !trustAllRoots) { boolean storeExists = new File(javaKeystore).exists(); trustKeystore = (storeExists) ? Constants.FILE + javaKeystore : null; if (storeExists) { if (isEmpty(trustPassword)) { trustPassword = SecurityConstants.DEFAULT_PASSWORD_FOR_STORE; } } else { trustPassword = null; } useTrustCert = storeExists; } else { if (!trustAllRoots) { if (!trustKeystore.startsWith(Constants.HTTP)) trustKeystore = Constants.FILE + trustKeystore; useTrustCert = true; } } TrustManager[] trustManagers = null; KeyManager[] keyManagers = null; if (trustAllRoots) { trustManagers = new TrustManager[]{new EasyX509TrustManager()}; } if (useTrustCert) { KeyStore trustKeyStore = SSLUtils.createKeyStore(new URL(trustKeystore), trustPassword); trustManagers = SSLUtils.createAuthTrustManagers(trustKeyStore); } if (useClientCert) { KeyStore clientKeyStore = SSLUtils.createKeyStore(new URL(keystore), keystorePassword); keyManagers = SSLUtils.createKeyManagers(clientKeyStore, keystorePassword); } SSLContext context = SSLContext.getInstance(SecurityConstants.SSL); context.init(keyManagers, trustManagers, new SecureRandom()); SSLContext.setDefault(context); }  static KeyStore createKeyStore(final URL url, final String password); static KeyManager[] createKeyManagers(final KeyStore keystore, final String password); static TrustManager[] createAuthTrustManagers(final KeyStore keystore); static Store createMessageStore(GetMailInput input); static void addSSLSettings(boolean trustAllRoots, String keystore,
                                      String keystorePassword, String trustKeystore, String trustPassword); static Store configureStoreWithSSL(Properties props, Authenticator auth, MailInput input); static Store tryTLSOtherwiseTrySSL(Authenticator auth, Properties props, GetMailInput input); static Store configureStoreWithTLS(Properties props, Authenticator auth, MailInput input); static void configureWithTLS(Properties props, final MailInput input); static void clearTLSProperties(Properties props, MailInput input);  }