Java Code Examples for javax.net.ssl.KeyManagerFactory

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project avro, under directory /lang/java/ipc/src/test/java/org/apache/avro/ipc/.

Source file: TestNettyServerWithSSL.java

  23 
vote

private SSLContext createServerSSLContext(){
  try {
    KeyStore ks=KeyStore.getInstance("PKCS12");
    ks.load(TestNettyServer.class.getResource(TEST_CERTIFICATE).openStream(),TEST_CERTIFICATE_PASSWORD.toCharArray());
    KeyManagerFactory kmf=KeyManagerFactory.getInstance(getAlgorithm());
    kmf.init(ks,TEST_CERTIFICATE_PASSWORD.toCharArray());
    SSLContext serverContext=SSLContext.getInstance("TLS");
    serverContext.init(kmf.getKeyManagers(),null,null);
    return serverContext;
  }
 catch (  Exception e) {
    throw new Error("Failed to initialize the server-side SSLContext",e);
  }
}
 

Example 2

From project caseconductor-platform, under directory /utest-webservice/utest-webservice-client/src/main/java/com/utest/webservice/client/rest/.

Source file: AuthSSLProtocolSocketFactory.java

  23 
vote

private static KeyManager[] createKeyManagers(final KeyStore keystore,final String password) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
  if (keystore == null) {
    throw new IllegalArgumentException("Keystore may not be null");
  }
  KeyManagerFactory kmfactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmfactory.init(keystore,password != null ? password.toCharArray() : null);
  return kmfactory.getKeyManagers();
}
 

Example 3

From project components-ness-httpclient, under directory /client/src/main/java/com/nesscomputing/httpclient/internal/.

Source file: HttpClientTrustManagerFactory.java

  22 
vote

@Nonnull private static X509KeyManager getKeyManagerForKeystore(@Nonnull KeyStore keyStore,@Nonnull String password) throws GeneralSecurityException {
  KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance("SunX509","SunJSSE");
  keyManagerFactory.init(keyStore,password.toCharArray());
  for (  KeyManager keyManager : keyManagerFactory.getKeyManagers()) {
    if (keyManager instanceof X509KeyManager) {
      return (X509KeyManager)keyManager;
    }
  }
  throw new IllegalStateException("Couldn't find an X509KeyManager");
}
 

Example 4

From project dcm4che, under directory /dcm4che-net/src/main/java/org/dcm4che/net/.

Source file: SSLManagerFactory.java

  22 
vote

public static KeyManager createKeyManager(KeyStore ks,char[] password) throws UnrecoverableKeyException, KeyStoreException {
  try {
    KeyManagerFactory kmf=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(ks,password);
    KeyManager[] kms=kmf.getKeyManagers();
    return kms.length > 0 ? kms[0] : null;
  }
 catch (  NoSuchAlgorithmException e) {
    throw new AssertionError(e);
  }
}
 

Example 5

From project hqapi, under directory /hqapi1/src/main/java/org/hyperic/hq/hqapi1/.

Source file: HQConnection.java

  22 
vote

private KeyManagerFactory getKeyManagerFactory(final KeyStore keystore,final String password) throws KeyStoreException {
  try {
    KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keystore,password.toCharArray());
    return keyManagerFactory;
  }
 catch (  NoSuchAlgorithmException e) {
    throw new KeyStoreException(e);
  }
catch (  UnrecoverableKeyException e) {
    throw new KeyStoreException(e);
  }
}
 

Example 6

From project httpcore, under directory /httpcore-nio/src/test/java/org/apache/http/testserver/.

Source file: SSLTestContexts.java

  22 
vote

public static SSLContext createServerSSLContext() throws Exception {
  ClassLoader cl=SSLTestContexts.class.getClassLoader();
  URL url=cl.getResource("test.keystore");
  Assert.assertNotNull("Keystore URL should not be null",url);
  KeyStore keystore=KeyStore.getInstance("jks");
  keystore.load(url.openStream(),"nopassword".toCharArray());
  KeyManagerFactory kmfactory=createKeyManagerFactory();
  kmfactory.init(keystore,"nopassword".toCharArray());
  KeyManager[] keymanagers=kmfactory.getKeyManagers();
  SSLContext sslcontext=SSLContext.getInstance("TLS");
  sslcontext.init(keymanagers,null,null);
  return sslcontext;
}
 

Example 7

From project jftp, under directory /src/main/java/com/myjavaworld/jftp/ssl/.

Source file: JFTPKeyManager.java

  22 
vote

public JFTPKeyManager() throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
  super();
  KeyManagerFactory kmf=KeyManagerFactory.getInstance("SunX509");
  kmf.init(KeyStoreManager.getClientCertificateStore(),JFTP.prefs.getClientCertificateStorePassword());
  km=(X509KeyManager)kmf.getKeyManagers()[0];
}
 

Example 8

From project karaf, under directory /jaas/config/src/main/java/org/apache/karaf/jaas/config/impl/.

Source file: ResourceKeystoreInstance.java

  22 
vote

public KeyManager[] getKeyManager(String algorithm,String keyAlias) throws KeystoreIsLocked, NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException {
  if (isKeystoreLocked()) {
    throw new KeystoreIsLocked("Keystore '" + name + "' is locked.");
  }
  if (!loadKeystoreData()) {
    return null;
  }
  KeyManagerFactory keyFactory=KeyManagerFactory.getInstance(algorithm);
  keyFactory.init(keystore,(char[])keyPasswords.get(keyAlias));
  return keyFactory.getKeyManagers();
}
 

Example 9

From project litle-sdk-for-java, under directory /lib/apache-cxf-2.5.2/samples/jax_rs/basic_https/src/main/java/org/apache/commons/httpclient/contrib/ssl/.

Source file: AuthSSLProtocolSocketFactory.java

  22 
vote

private static KeyManager[] createKeyManagers(final KeyStore keystore,final String password) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
  if (keystore == null) {
    throw new IllegalArgumentException("Keystore may not be null");
  }
  LOG.debug("Initializing key manager");
  KeyManagerFactory kmfactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmfactory.init(keystore,password != null ? password.toCharArray() : null);
  return kmfactory.getKeyManagers();
}
 

Example 10

From project netty-socketio, under directory /src/main/java/com/corundumstudio/socketio/.

Source file: SocketIOPipelineFactory.java

  22 
vote

private SSLContext createSSLContext(InputStream keyStoreFile,String keyStoreFilePassword) throws Exception {
  String algorithm=Security.getProperty("ssl.KeyManagerFactory.algorithm");
  if (algorithm == null) {
    algorithm="SunX509";
  }
  KeyStore ks=KeyStore.getInstance("JKS");
  ks.load(keyStoreFile,keyStoreFilePassword.toCharArray());
  KeyManagerFactory kmf=KeyManagerFactory.getInstance(algorithm);
  kmf.init(ks,keyStoreFilePassword.toCharArray());
  SSLContext serverContext=SSLContext.getInstance("TLS");
  serverContext.init(kmf.getKeyManagers(),null,null);
  return serverContext;
}
 

Example 11

From project openclaws, under directory /cat/WEB-INF/src/edu/rit/its/claws/cat/.

Source file: CatSecureSocketFactory.java

  22 
vote

/** 
 * Create a new CatSecureSocketFactory.
 * @param attrs     attributes for the connection (Ignored at this point)
 * @throws Exception
 */
@SuppressWarnings("unchecked") public CatSecureSocketFactory(Hashtable attrs) throws Exception {
  if (ks == null)   throw new Exception("The CAT secure socket factory has not been initialized with a keystore");
  TrustManagerFactory tmf=TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  tmf.init(ks);
  KeyManagerFactory kmf=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmf.init(ks,ksPass.toCharArray());
  SSLContext ctx=SSLContext.getInstance("SSLv3");
  ctx.init(kmf.getKeyManagers(),tmf.getTrustManagers(),null);
  ssf=ctx.getSocketFactory();
}
 

Example 12

From project org.openscada.atlantis, under directory /org.openscada.core.net/src/org/openscada/core/net/.

Source file: ConnectionHelper.java

  22 
vote

private static KeyManager[] getKeyManagers(final ConnectionInformation connectionInformation,final boolean isClient) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException, CertificateException, IOException {
  if (isClient) {
    return null;
  }
  final KeyStore keyStore;
  keyStore=createKeyStore(connectionInformation);
  final String keyManagerFactory=KeyManagerFactory.getDefaultAlgorithm();
  final KeyManagerFactory kmf=KeyManagerFactory.getInstance(keyManagerFactory);
  kmf.init(keyStore,getPassword(connectionInformation,"sslCertPassword"));
  return kmf.getKeyManagers();
}
 

Example 13

From project Orweb, under directory /src/info/guardianproject/browser/.

Source file: ModSSLSocketFactory.java

  22 
vote

private static KeyManager[] createKeyManagers(final KeyStore keystore,final String password) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
  if (keystore == null) {
    throw new IllegalArgumentException("Keystore may not be null");
  }
  KeyManagerFactory kmfactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmfactory.init(keystore,password != null ? password.toCharArray() : null);
  return kmfactory.getKeyManagers();
}
 

Example 14

From project OWASP-WebScarab, under directory /src/org/owasp/webscarab/plugin/.

Source file: SSLKeyManager.java

  22 
vote

public synchronized void addKeyStore(String description,KeyStore ks,char[] password) throws KeyStoreException, UnrecoverableKeyException {
  try {
    KeyManagerFactory kmf=KeyManagerFactory.getInstance("SunX509");
    kmf.init(ks,password);
    KeyManager km=kmf.getKeyManagers()[0];
    if (!(km instanceof X509KeyManager))     throw new KeyStoreException("KeyManager for " + description + "is not X509!");
    _stores.put(description,ks);
    _managers.put(description,(X509KeyManager)km);
  }
 catch (  NoSuchAlgorithmException nsae) {
    _logger.severe("This should never happen! SunX509 algorithm not found: " + nsae.getMessage());
  }
  _changeSupport.firePropertyChange(KEY_PROPERTY,null,null);
}
 

Example 15

From project PenguinCMS, under directory /PenguinCMS/tests/vendor/sahi/src/net/sf/sahi/ssl/.

Source file: SSLHelper.java

  22 
vote

private SSLSocketFactory createSocketFactory(final String fileWithPath,final String password){
  SSLSocketFactory factory=null;
  try {
    KeyManagerFactory keyManagerFactory=getKeyManagerFactory(fileWithPath,password,"JKS");
    SSLContext sslContext=SSLContext.getInstance("SSLv3");
    sslContext.init(keyManagerFactory.getKeyManagers(),getAllTrustingManager(),new SecureRandom());
    factory=sslContext.getSocketFactory();
    return factory;
  }
 catch (  Exception e) {
    e.printStackTrace();
  }
  return (SSLSocketFactory)SSLSocketFactory.getDefault();
}
 

Example 16

From project platform_external_apache-http, under directory /src/org/apache/http/conn/ssl/.

Source file: SSLSocketFactory.java

  22 
vote

private static KeyManager[] createKeyManagers(final KeyStore keystore,final String password) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
  if (keystore == null) {
    throw new IllegalArgumentException("Keystore may not be null");
  }
  KeyManagerFactory kmfactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmfactory.init(keystore,password != null ? password.toCharArray() : null);
  return kmfactory.getKeyManagers();
}
 

Example 17

From project Red5, under directory /src/org/red5/server/net/rtmps/.

Source file: RTMPSMinaIoHandler.java

  22 
vote

/** 
 * {@inheritDoc} 
 */
@Override public void sessionOpened(IoSession session) throws Exception {
  if (password == null || keystore == null) {
    throw new NotActiveException("Keystore or password are null");
  }
  SSLContext context=SSLContext.getInstance("TLS");
  KeyManagerFactory kmf=KeyManagerFactory.getInstance("SunX509");
  kmf.init(getKeyStore(),password);
  context.init(kmf.getKeyManagers(),null,null);
  SslFilter sslFilter=new SslFilter(context);
  if (sslFilter != null) {
    session.getFilterChain().addFirst("sslFilter",sslFilter);
  }
  super.sessionOpened(session);
}
 

Example 18

From project twistDemo, under directory /twist-libs/com.thoughtworks.webdriver.recorder_1.0.0.11288/sahi/src/net/sf/sahi/ssl/.

Source file: SSLHelper.java

  22 
vote

private SSLSocketFactory createSocketFactory(final String fileWithPath,final String password){
  SSLSocketFactory factory=null;
  try {
    KeyManagerFactory keyManagerFactory=getKeyManagerFactory(fileWithPath,password,"JKS");
    SSLContext sslContext=SSLContext.getInstance("SSLv3");
    sslContext.init(keyManagerFactory.getKeyManagers(),getAllTrustingManager(),new SecureRandom());
    factory=sslContext.getSocketFactory();
    return factory;
  }
 catch (  Exception e) {
    e.printStackTrace();
  }
  return (SSLSocketFactory)SSLSocketFactory.getDefault();
}
 

Example 19

From project voms-api-java, under directory /src/main/java/org/glite/voms/.

Source file: VOMSKeyManager.java

  22 
vote

public VOMSKeyManager(UserCredentials creds) throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, IOException, CertificateException {
  if (creds.getUserKey() == null) {
    throw new UnrecoverableKeyException("Passed Credential does not hold a private key!");
  }
  KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance("SunX509");
  KeyStore keyStore=KeyStore.getInstance("JKS");
  keyStore.load(null,"".toCharArray());
  logger.debug("ABOUT to set key entry");
  keyStore.setKeyEntry("alias",creds.getUserKey(),"".toCharArray(),creds.getUserChain());
  logger.debug("STORETYPE: " + keyStore.getType());
  keyManagerFactory.init(keyStore,"".toCharArray());
  manager=(X509KeyManager)keyManagerFactory.getKeyManagers()[0];
}
 

Example 20

From project xnio_1, under directory /api/src/test/java/org/xnio/.

Source file: XnioTestCase.java

  22 
vote

@Test public void retrieveSslProviderWithTrustAndKeyManagers() throws GeneralSecurityException, FileNotFoundException, IOException {
  final Xnio xnio=Xnio.getInstance();
  final OptionMap optionMap=OptionMap.create(Options.SSL_CLIENT_AUTH_MODE,SslClientAuthMode.REQUIRED,Options.SSL_STARTTLS,true);
  final KeyStore keyStore=KeyStore.getInstance("JKS");
  final String keyStorePath=XnioTestCase.class.getClassLoader().getResource(DEFAULT_KEY_STORE).getFile();
  keyStore.load(new FileInputStream(keyStorePath),DEFAULT_KEY_STORE_PASSWORD.toCharArray());
  final KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  keyManagerFactory.init(keyStore,DEFAULT_KEY_STORE_PASSWORD.toCharArray());
  TrustManagerFactory trustManagerFactory=TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  trustManagerFactory.init(keyStore);
  final XnioSsl sslProvider=xnio.getSslProvider(keyManagerFactory.getKeyManagers(),trustManagerFactory.getTrustManagers(),optionMap);
  assertNotNull(sslProvider);
}
 

Example 21

From project AmDroid, under directory /httpclientandroidlib/src/ch/boye/httpclientandroidlib/conn/ssl/.

Source file: SSLSocketFactory.java

  21 
vote

private static SSLContext createSSLContext(String algorithm,final KeyStore keystore,final String keystorePassword,final KeyStore truststore,final SecureRandom random,final TrustStrategy trustStrategy) throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException {
  if (algorithm == null) {
    algorithm=TLS;
  }
  KeyManagerFactory kmfactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmfactory.init(keystore,keystorePassword != null ? keystorePassword.toCharArray() : null);
  KeyManager[] keymanagers=kmfactory.getKeyManagers();
  TrustManagerFactory tmfactory=TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  tmfactory.init(truststore);
  TrustManager[] trustmanagers=tmfactory.getTrustManagers();
  if (trustmanagers != null && trustStrategy != null) {
    for (int i=0; i < trustmanagers.length; i++) {
      TrustManager tm=trustmanagers[i];
      if (tm instanceof X509TrustManager) {
        trustmanagers[i]=new TrustManagerDecorator((X509TrustManager)tm,trustStrategy);
      }
    }
  }
  SSLContext sslcontext=SSLContext.getInstance(algorithm);
  sslcontext.init(keymanagers,trustmanagers,random);
  return sslcontext;
}
 

Example 22

From project androidpn, under directory /androidpn-server-bin-tomcat/src/org/androidpn/server/xmpp/ssl/.

Source file: SSLKeyManagerFactory.java

  21 
vote

public static KeyManager[] getKeyManagers(String storeType,String keystore,String keypass) throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException {
  KeyManager[] keyManagers;
  if (keystore == null) {
    keyManagers=null;
  }
 else {
    if (keypass == null) {
      keypass="";
    }
    KeyStore keyStore=KeyStore.getInstance(storeType);
    keyStore.load(new FileInputStream(keystore),keypass.toCharArray());
    KeyManagerFactory keyFactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyFactory.init(keyStore,keypass.toCharArray());
    keyManagers=keyFactory.getKeyManagers();
  }
  return keyManagers;
}
 

Example 23

From project apjp, under directory /APJP_LOCAL_JAVA/src/main/java/APJP/HTTPS/.

Source file: HTTPS.java

  21 
vote

public static synchronized SSLSocket createSSLSocket() throws HTTPSException {
  try {
    KeyStore defaultKeyStore=getDefaultKeyStore();
    PrivateKey privateKey=(PrivateKey)defaultKeyStore.getKey("APJP","APJP".toCharArray());
    Certificate certificateAuthority=defaultKeyStore.getCertificate("APJP");
    Certificate[] certificateArray=new Certificate[1];
    certificateArray[0]=certificateAuthority;
    KeyStore keyStore=KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(null,"APJP".toCharArray());
    keyStore.setCertificateEntry("APJP",certificateAuthority);
    keyStore.setKeyEntry("APJP",privateKey,"APJP".toCharArray(),certificateArray);
    SSLContext sslContext=SSLContext.getInstance("TLS");
    KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keyStore,"APJP".toCharArray());
    TrustManagerFactory trustManagerFactory=TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(keyStore);
    sslContext.init(keyManagerFactory.getKeyManagers(),trustManagerFactory.getTrustManagers(),null);
    SSLSocketFactory sslSocketFactory=(SSLSocketFactory)sslContext.getSocketFactory();
    return (SSLSocket)sslSocketFactory.createSocket();
  }
 catch (  Exception e) {
    logger.log(2,"HTTPS/CREATE_SSL_SOCKET: EXCEPTION",e);
    throw new HTTPSException("HTTPS/CREATE_SSL_SOCKET",e);
  }
}
 

Example 24

From project chililog-server, under directory /src/main/java/org/chililog/server/pubsub/jsonhttp/.

Source file: JsonHttpSslContextManager.java

  21 
vote

/** 
 * Constructor for singleton
 */
private JsonHttpSslContextManager(){
  try {
    String algorithm=Security.getProperty("ssl.KeyManagerFactory.algorithm");
    if (algorithm == null) {
      algorithm="SunX509";
    }
    SSLContext serverContext=null;
    try {
      KeyStore ks=KeyStore.getInstance("JKS");
      FileInputStream fin=new FileInputStream(AppProperties.getInstance().getPubSubJsonHttpKeyStorePath());
      ks.load(fin,AppProperties.getInstance().getPubSubJsonHttpKeyStorePassword().toCharArray());
      KeyManagerFactory kmf=KeyManagerFactory.getInstance(algorithm);
      kmf.init(ks,AppProperties.getInstance().getPubSubJsonHttpKeyStoreKeyPassword().toCharArray());
      serverContext=SSLContext.getInstance(PROTOCOL);
      serverContext.init(kmf.getKeyManagers(),null,null);
    }
 catch (    Exception e) {
      throw new Error("Failed to initialize the server-side SSLContext",e);
    }
    _serverContext=serverContext;
    SSLContext clientContext=null;
    try {
      clientContext=SSLContext.getInstance(PROTOCOL);
      clientContext.init(null,JsonHttpSSLTrustManager.getInstance().getTrustManagers(),null);
    }
 catch (    Exception e) {
      throw new Error("Failed to initialize the client-side SSLContext",e);
    }
    _clientContext=clientContext;
    return;
  }
 catch (  Exception ex) {
    _logger.error("Error initializing SslContextManager. " + ex.getMessage(),ex);
    System.exit(1);
  }
}
 

Example 25

From project cloudify, under directory /esc/src/main/java/org/cloudifysource/esc/driver/provisioning/azure/client/.

Source file: MicrosoftAzureSSLHelper.java

  21 
vote

/** 
 * @return .
 * @throws NoSuchAlgorithmException .
 * @throws KeyStoreException .
 * @throws CertificateException .
 * @throws IOException .
 * @throws UnrecoverableKeyException .
 * @throws KeyManagementException .
 */
public SSLContext createSSLContext() throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, UnrecoverableKeyException, KeyManagementException {
  InputStream pfxFile=null;
  SSLContext context=null;
  try {
    pfxFile=new FileInputStream(new File(pathToPfxFile));
    KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance(SUN_X_509_ALGORITHM);
    KeyStore keyStore=KeyStore.getInstance(KEY_STORE_CONTEXT);
    keyStore.load(pfxFile,pfxPassword.toCharArray());
    pfxFile.close();
    keyManagerFactory.init(keyStore,pfxPassword.toCharArray());
    context=SSLContext.getInstance("SSL");
    context.init(keyManagerFactory.getKeyManagers(),null,new SecureRandom());
    return context;
  }
  finally {
    if (pfxFile != null) {
      pfxFile.close();
    }
  }
}
 

Example 26

From project Cours-3eme-ann-e, under directory /Java/FerryInpres/src/identity_server/.

Source file: IdentityServer.java

  21 
vote

private void verifId(ObjectInputStream in,ObjectOutputStream out,Cipher cryptor,Cipher decryptor) throws IOException, ClassNotFoundException, IllegalBlockSizeException, BadPaddingException, SQLException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, KeyManagementException {
  for (; ; ) {
    VerifId query=(VerifId)Utils.decryptObject((byte[])in.readObject(),decryptor);
    Protocol valide;
    if ("BE".equals(query.getNationalite())) {
      PreparedStatement instruc=this._con.prepareStatement("SELECT COUNT(*) AS existe " + "FROM voyageur " + "WHERE id_national = ? AND nom = ? AND prenom = ?");
      instruc.setInt(1,query.getClientNationalId());
      instruc.setString(2,query.getClientName());
      instruc.setString(3,query.getClientSurname());
      ResultSet rs=instruc.executeQuery();
      rs.next();
      if (rs.getInt("existe") != 0)       valide=new Ack();
 else       valide=new Fail();
    }
 else {
      System.out.println("Consultation du registre international");
      KeyStore store=KeyStore.getInstance("JKS");
      store.load(new FileInputStream("client_keystore.jks"),"pwdpwd".toCharArray());
      KeyManagerFactory kmf=KeyManagerFactory.getInstance("SunX509");
      kmf.init(store,"pwdpwd".toCharArray());
      TrustManagerFactory tmf=TrustManagerFactory.getInstance("SunX509");
      tmf.init(store);
      SSLContext context=SSLContext.getInstance("SSLv3");
      context.init(kmf.getKeyManagers(),tmf.getTrustManagers(),null);
      SSLSocketFactory factory=context.getSocketFactory();
      SSLSocket sock=(SSLSocket)factory.createSocket(IdentityServer.prop.getProperty("INTERNATIONAL_SERVER"),Integer.parseInt(IdentityServer.prop.getProperty("INTERNATIONAL_PORT")));
      ObjectInputStream ssl_in=new ObjectInputStream(sock.getInputStream());
      ObjectOutputStream ssl_out=new ObjectOutputStream(sock.getOutputStream());
      ssl_out.writeObject(query);
      ssl_out.flush();
      valide=(Protocol)ssl_in.readObject();
      sock.close();
    }
    out.writeObject(Utils.cryptObject(valide,cryptor));
    out.flush();
  }
}
 

Example 27

From project Gibberbot, under directory /src/info/guardianproject/otr/app/im/plugin/xmpp/.

Source file: XmppConnection.java

  21 
vote

private void initSSLContext(String domain,String requestedServer,ConnectionConfiguration config) throws Exception {
  ks=KeyStore.getInstance(TRUSTSTORE_TYPE);
  try {
    ks.load(new FileInputStream(TRUSTSTORE_PATH),TRUSTSTORE_PASS.toCharArray());
  }
 catch (  Exception e) {
    ks=null;
  }
  KeyManagerFactory kmf=KeyManagerFactory.getInstance(KEYMANAGER_TYPE);
  try {
    kmf.init(ks,TRUSTSTORE_PASS.toCharArray());
    kms=kmf.getKeyManagers();
  }
 catch (  NullPointerException npe) {
    kms=null;
  }
  sslContext=SSLContext.getInstance(SSLCONTEXT_TYPE);
  sTrustManager=new ServerTrustManager(aContext,domain,requestedServer,config);
  sslContext.init(kms,new javax.net.ssl.TrustManager[]{sTrustManager},new java.security.SecureRandom());
  config.setCustomSSLContext(sslContext);
  config.setCallbackHandler(this);
}
 

Example 28

From project gmc, under directory /src/org.gluster.storage.management.client/src/org/gluster/storage/management/client/.

Source file: AbstractClient.java

  21 
vote

private SSLContext initializeSSLContext(){
  SSLContext context=null;
  try {
    context=SSLContext.getInstance(PROTOCOL_TLS);
    KeyStore keyStore=KeyStore.getInstance(KEYSTORE_TYPE_JKS);
    keyStore.load(loadResource(TRUSTED_KEYSTORE),TRUSTED_KEYSTORE_ACCESS.toCharArray());
    KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance(ALGORITHM_SUNX509);
    keyManagerFactory.init(keyStore,TRUSTED_KEYSTORE_ACCESS.toCharArray());
    TrustManagerFactory trustManagerFactory=TrustManagerFactory.getInstance(ALGORITHM_SUNX509);
    trustManagerFactory.init(keyStore);
    context.init(keyManagerFactory.getKeyManagers(),trustManagerFactory.getTrustManagers(),null);
  }
 catch (  Exception e) {
    throw new GlusterRuntimeException("Couldn't initialize SSL Context with Gluster Management Gateway! Error: " + e,e);
  }
  return context;
}
 

Example 29

From project Haven-and-Hearth-client-modified-by-Ender, under directory /src/haven/.

Source file: SslHelper.java

  21 
vote

private synchronized SSLContext ctx(){
  if (ctx == null) {
    TrustManagerFactory tmf;
    KeyManagerFactory kmf;
    try {
      ctx=SSLContext.getInstance("TLS");
      tmf=TrustManagerFactory.getInstance("PKIX");
      kmf=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
      KeyManager[] kms=null;
      tmf.init(trusted);
      if (creds != null) {
        kmf.init(creds,pw);
        kms=kmf.getKeyManagers();
      }
      ctx.init(kms,tmf.getTrustManagers(),new SecureRandom());
    }
 catch (    NoSuchAlgorithmException e) {
      throw (new Error(e));
    }
catch (    KeyStoreException e) {
      throw (new RuntimeException(e));
    }
catch (    UnrecoverableKeyException e) {
      throw (new RuntimeException(e));
    }
catch (    KeyManagementException e) {
      throw (new RuntimeException(e));
    }
  }
  return (ctx);
}
 

Example 30

From project hotpotato, under directory /src/main/java/com/biasedbit/hotpotato/util/.

Source file: DummyHttpServer.java

  21 
vote

public static SslHandler createSelfSignedSslHandler() throws Exception {
  String algorithm="SunX509";
  String password="password";
  KeyStore keyStore=KeyStore.getInstance("JKS");
  InputStream keyStoreAsStream=null;
  try {
    keyStoreAsStream=new BufferedInputStream(new FileInputStream("src/main/resources/dummyserver/selfsigned.jks"));
    keyStore.load(keyStoreAsStream,password.toCharArray());
  }
  finally {
    if (keyStoreAsStream != null) {
      try {
        keyStoreAsStream.close();
      }
 catch (      Exception e) {
      }
    }
  }
  KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance(algorithm);
  TrustManagerFactory trustManagerFactory=TrustManagerFactory.getInstance(algorithm);
  keyManagerFactory.init(keyStore,password.toCharArray());
  trustManagerFactory.init(keyStore);
  SSLContext context=SSLContext.getInstance("TLS");
  context.init(keyManagerFactory.getKeyManagers(),trustManagerFactory.getTrustManagers(),new SecureRandom());
  SSLEngine engine=context.createSSLEngine();
  engine.setUseClientMode(false);
  return new SslHandler(engine,true);
}
 

Example 31

From project http-client, under directory /src/main/java/com/biasedbit/http/ssl/.

Source file: BogusSslContextFactory.java

  21 
vote

public BogusSslContextFactory(){
  String algorithm=Security.getProperty("ssl.KeyManagerFactory.algorithm");
  if (algorithm == null) {
    algorithm="X509";
  }
  SSLContext tmpServerContext;
  SSLContext tmpClientContext;
  try {
    KeyStore ks=KeyStore.getInstance("JKS");
    ks.load(BogusKeyStore.asInputStream(),BogusKeyStore.getKeyStorePassword());
    KeyManagerFactory kmf=KeyManagerFactory.getInstance(algorithm);
    kmf.init(ks,BogusKeyStore.getCertificatePassword());
    tmpServerContext=SSLContext.getInstance(PROTOCOL);
    tmpServerContext.init(kmf.getKeyManagers(),BogusTrustManagerFactory.getTrustManagers(),null);
  }
 catch (  Exception e) {
    throw new Error("Failed to initialize the server-side SSLContext",e);
  }
  try {
    tmpClientContext=SSLContext.getInstance(PROTOCOL);
    tmpClientContext.init(null,BogusTrustManagerFactory.getTrustManagers(),null);
  }
 catch (  Exception e) {
    throw new Error("Failed to initialize the client-side SSLContext",e);
  }
  serverContext=tmpServerContext;
  clientContext=tmpClientContext;
}
 

Example 32

From project http-testing-harness, under directory /server-provider/src/test/java/org/sonatype/tests/http/server/jetty/impl/.

Source file: ClientSideCertTest.java

  21 
vote

private static SSLSocketFactory getFactory(File pKeyFile,String pKeyPassword,String certAlias) throws Exception {
  KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance("SunX509");
  KeyStore keyStore=KeyStore.getInstance("JKS");
  InputStream keyInput=new FileInputStream(pKeyFile);
  keyStore.load(keyInput,pKeyPassword.toCharArray());
  keyInput.close();
  keyManagerFactory.init(keyStore,pKeyPassword.toCharArray());
  KeyManager[] kms=keyManagerFactory.getKeyManagers();
  for (int i=0; i < kms.length; i++) {
    if (kms[i] instanceof X509KeyManager) {
      kms[i]=new AliasForcingKeyManager((X509KeyManager)kms[i],certAlias);
    }
  }
  TrustManager[] _trustManagers=new TrustManager[]{new CustomTrustManager()};
  SSLContext context;
  try {
    context=SSLContext.getInstance("TLS");
    context.init(kms,_trustManagers,new SecureRandom());
  }
 catch (  GeneralSecurityException gse) {
    throw new IllegalStateException(gse.getMessage());
  }
  HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
  return context.getSocketFactory();
}
 

Example 33

From project httpClient, under directory /httpclient/src/main/java/org/apache/http/conn/ssl/.

Source file: SSLSocketFactory.java

  21 
vote

private static SSLContext createSSLContext(String algorithm,final KeyStore keystore,final String keystorePassword,final KeyStore truststore,final SecureRandom random,final TrustStrategy trustStrategy) throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException {
  if (algorithm == null) {
    algorithm=TLS;
  }
  KeyManagerFactory kmfactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmfactory.init(keystore,keystorePassword != null ? keystorePassword.toCharArray() : null);
  KeyManager[] keymanagers=kmfactory.getKeyManagers();
  TrustManagerFactory tmfactory=TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  tmfactory.init(truststore);
  TrustManager[] trustmanagers=tmfactory.getTrustManagers();
  if (trustmanagers != null && trustStrategy != null) {
    for (int i=0; i < trustmanagers.length; i++) {
      TrustManager tm=trustmanagers[i];
      if (tm instanceof X509TrustManager) {
        trustmanagers[i]=new TrustManagerDecorator((X509TrustManager)tm,trustStrategy);
      }
    }
  }
  SSLContext sslcontext=SSLContext.getInstance(algorithm);
  sslcontext.init(keymanagers,trustmanagers,random);
  return sslcontext;
}
 

Example 34

From project httpserver, under directory /src/test/java/.

Source file: SimpleSSLContext.java

  21 
vote

SimpleSSLContext(String dir) throws IOException {
  try {
    String file=dir + "/testkeys";
    char[] passphrase="passphrase".toCharArray();
    KeyStore ks=KeyStore.getInstance("JKS");
    ks.load(new FileInputStream(file),passphrase);
    KeyManagerFactory kmf=KeyManagerFactory.getInstance("SunX509");
    kmf.init(ks,passphrase);
    TrustManagerFactory tmf=TrustManagerFactory.getInstance("SunX509");
    tmf.init(ks);
    ssl=SSLContext.getInstance("TLS");
    ssl.init(kmf.getKeyManagers(),tmf.getTrustManagers(),null);
  }
 catch (  KeyManagementException e) {
    throw new RuntimeException(e.getMessage());
  }
catch (  KeyStoreException e) {
    throw new RuntimeException(e.getMessage());
  }
catch (  UnrecoverableKeyException e) {
    throw new RuntimeException(e.getMessage());
  }
catch (  CertificateException e) {
    throw new RuntimeException(e.getMessage());
  }
catch (  NoSuchAlgorithmException e) {
    throw new RuntimeException(e.getMessage());
  }
}
 

Example 35

From project james, under directory /protocols-library/src/main/java/org/apache/james/protocols/lib/netty/.

Source file: AbstractConfigurableAsyncServer.java

  21 
vote

/** 
 * Build the SSLEngine
 * @throws Exception
 */
private void buildSSLContext() throws Exception {
  if (useStartTLS || useSSL) {
    FileInputStream fis=null;
    try {
      KeyStore ks=KeyStore.getInstance("JKS");
      fis=new FileInputStream(fileSystem.getFile(keystore));
      ks.load(fis,secret.toCharArray());
      KeyManagerFactory kmf=KeyManagerFactory.getInstance(x509Algorithm);
      kmf.init(ks,secret.toCharArray());
      SSLContext context=SSLContext.getInstance("TLS");
      context.init(kmf.getKeyManagers(),null,null);
      if (useStartTLS) {
        encryption=Encryption.createStartTls(context,enabledCipherSuites);
      }
 else {
        encryption=Encryption.createTls(context,enabledCipherSuites);
      }
    }
  finally {
      if (fis != null) {
        fis.close();
      }
    }
  }
}
 

Example 36

From project JGlobus, under directory /jsse/src/main/java/org/globus/gsi/jsse/.

Source file: SSLConfigurator.java

  21 
vote

private KeyManager[] loadKeyManagers() throws GlobusSSLConfigurationException {
  try {
    KeyStore inputKeyStore;
    if (this.credentialStore == null) {
      if (this.credentialStoreLocation == null)       return null;
      inputKeyStore=GlobusSSLHelper.findCredentialStore(this.provider,this.credentialStoreType,this.credentialStoreLocation,this.credentialStorePassword);
    }
 else {
      inputKeyStore=this.credentialStore;
    }
    KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance(sslKeyManagerFactoryAlgorithm);
    keyManagerFactory.init(inputKeyStore,credentialStorePassword == null ? null : credentialStorePassword.toCharArray());
    return keyManagerFactory.getKeyManagers();
  }
 catch (  KeyStoreException e) {
    throw new GlobusSSLConfigurationException(e);
  }
catch (  NoSuchAlgorithmException e) {
    throw new GlobusSSLConfigurationException(e);
  }
catch (  UnrecoverableKeyException e) {
    throw new GlobusSSLConfigurationException(e);
  }
}
 

Example 37

From project jnrpe-lib, under directory /jnrpe-lib/src/main/java/it/jnrpe/.

Source file: JNRPEListenerThread.java

  21 
vote

/** 
 * Creates an SSLServerSocketFactory.
 * @return the newly creates SSL Server Socket Factory
 * @throws KeyStoreException
 * @throws CertificateException
 * @throws IOException
 * @throws UnrecoverableKeyException
 * @throws KeyManagementException
 */
private SSLServerSocketFactory getSSLSocketFactory() throws KeyStoreException, CertificateException, IOException, UnrecoverableKeyException, KeyManagementException {
  StreamManager h=new StreamManager();
  SSLContext ctx;
  KeyManagerFactory kmf;
  try {
    InputStream ksStream=getClass().getClassLoader().getResourceAsStream(KEYSTORE_NAME);
    h.handle(ksStream);
    ctx=SSLContext.getInstance("SSLv3");
    kmf=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    KeyStore ks=KeyStore.getInstance("JKS");
    char[] passphrase=KEYSTORE_PWD.toCharArray();
    ks.load(ksStream,passphrase);
    kmf.init(ks,passphrase);
    ctx.init(kmf.getKeyManagers(),null,new java.security.SecureRandom());
  }
 catch (  NoSuchAlgorithmException e) {
    throw new SSLException("Unable to initialize SSLSocketFactory.\n" + e.getMessage());
  }
 finally {
    h.closeAll();
  }
  return ctx.getServerSocketFactory();
}
 

Example 38

From project mina, under directory /examples/src/main/java/org/apache/mina/examples/http/.

Source file: BogusSslContextFactory.java

  21 
vote

private static SSLContext createBougusServerSslContext() throws GeneralSecurityException, IOException {
  KeyStore ks=KeyStore.getInstance("JKS");
  InputStream in=null;
  try {
    in=BogusSslContextFactory.class.getResourceAsStream(BOGUS_KEYSTORE);
    ks.load(in,BOGUS_PW);
  }
  finally {
    if (in != null) {
      try {
        in.close();
      }
 catch (      IOException ignored) {
      }
    }
  }
  KeyManagerFactory kmf=KeyManagerFactory.getInstance(KEY_MANAGER_FACTORY_ALGORITHM);
  kmf.init(ks,BOGUS_PW);
  SSLContext sslContext=SSLContext.getInstance(PROTOCOL);
  sslContext.init(kmf.getKeyManagers(),BogusTrustManagerFactory.X509_MANAGERS,null);
  return sslContext;
}
 

Example 39

From project netty, under directory /example/src/main/java/io/netty/example/http/websocketx/sslserver/.

Source file: WebSocketSslServerSslContext.java

  21 
vote

/** 
 * Constructor for singleton
 */
private WebSocketSslServerSslContext(){
  try {
    String algorithm=Security.getProperty("ssl.KeyManagerFactory.algorithm");
    if (algorithm == null) {
      algorithm="SunX509";
    }
    SSLContext serverContext;
    try {
      String keyStoreFilePath=System.getProperty("keystore.file.path");
      String keyStoreFilePassword=System.getProperty("keystore.file.password");
      KeyStore ks=KeyStore.getInstance("JKS");
      FileInputStream fin=new FileInputStream(keyStoreFilePath);
      ks.load(fin,keyStoreFilePassword.toCharArray());
      KeyManagerFactory kmf=KeyManagerFactory.getInstance(algorithm);
      kmf.init(ks,keyStoreFilePassword.toCharArray());
      serverContext=SSLContext.getInstance(PROTOCOL);
      serverContext.init(kmf.getKeyManagers(),null,null);
    }
 catch (    Exception e) {
      throw new Error("Failed to initialize the server-side SSLContext",e);
    }
    _serverContext=serverContext;
  }
 catch (  Exception ex) {
    if (logger.isErrorEnabled()) {
      logger.error("Error initializing SslContextManager. " + ex.getMessage(),ex);
    }
    System.exit(1);
  }
}
 

Example 40

From project pegadi, under directory /common/src/main/java/org/pegadi/server/.

Source file: RMISSLServerSocketFactory.java

  21 
vote

public ServerSocket createServerSocket(int port) throws IOException {
  log.info("createServerSocket: Port " + port);
  log.info("createServerSocket: keystore is: " + keystore);
  SSLServerSocketFactory ssf;
  try {
    SSLContext ctx;
    KeyManagerFactory kmf;
    KeyStore ks;
    ctx=SSLContext.getInstance("TLS");
    kmf=KeyManagerFactory.getInstance("SunX509");
    ks=KeyStore.getInstance("JKS");
    if (inDeveloperMode) {
      ks.load(getClass().getResourceAsStream("dummyssl.keys"),passphrase.toCharArray());
    }
 else {
      if (!keystore.exists()) {
        throw new IllegalArgumentException("File " + keystore + " does not exist");
      }
      ks.load(new FileInputStream(keystore),passphrase.toCharArray());
    }
    kmf.init(ks,passphrase.toCharArray());
    ctx.init(kmf.getKeyManagers(),null,null);
    ssf=ctx.getServerSocketFactory();
  }
 catch (  Exception e) {
    log.error("Error",e);
    throw new IOException("Exceptinon getting socket factory " + e.getClass() + e.getMessage());
  }
  return ssf.createServerSocket(port);
}
 

Example 41

From project Pitbull, under directory /pitbull-core/src/main/java/org/jboss/pitbull/server/.

Source file: HttpServerBuilder.java

  21 
vote

public T add() throws Exception {
  HttpConnector connector=new HttpConnector();
  connector.setPort(port);
  if (enableHttps) {
    KeyManagerFactory kmf=null;
    if (keyStore == null) {
      try {
        keyStore=KeyTools.generateKeyStore();
        kmf=KeyManagerFactory.getInstance("SunX509");
        keyStore=KeyTools.generateKeyStore();
        kmf.init(keyStore,new char[]{'p','a','s','s','w','o','r','d'});
      }
 catch (      Exception e) {
        throw new RuntimeException(e);
      }
    }
 else {
      kmf=KeyManagerFactory.getInstance("SunX509");
      kmf.init(keyStore,keyStorePassword.toCharArray());
    }
    SSLContext sslContext=SSLContext.getInstance("TLS");
    sslContext.init(kmf.getKeyManagers(),null,null);
    SSLContext serverContext=SSLContext.getInstance("TLS");
    serverContext.init(kmf.getKeyManagers(),null,null);
    connector.setSslContext(serverContext);
  }
  server.getConnectors().add(connector);
  return (T)HttpServerBuilder.this;
}
 

Example 42

From project red5-server, under directory /src/org/red5/server/net/rtmps/.

Source file: RTMPSMinaIoHandler.java

  21 
vote

/** 
 * {@inheritDoc} 
 */
@Override public void sessionOpened(IoSession session) throws Exception {
  if (password == null || keystore == null) {
    throw new NotActiveException("Keystore or password are null");
  }
  SSLContext context=null;
  SslFilter sslFilter=null;
  RTMP rtmp=(RTMP)session.getAttribute(ProtocolState.SESSION_KEY);
  if (rtmp.getMode() != RTMP.MODE_CLIENT) {
    context=SSLContext.getInstance("TLS");
    KeyManagerFactory kmf=KeyManagerFactory.getInstance("SunX509");
    kmf.init(getKeyStore(),password);
    context.init(kmf.getKeyManagers(),null,null);
    sslFilter=new SslFilter(context);
  }
 else {
    context=SSLContext.getInstance("SSL");
    context.init(null,trustAllCerts,new SecureRandom());
    sslFilter=new SslFilter(context);
    sslFilter.setUseClientMode(true);
  }
  if (sslFilter != null) {
    session.getFilterChain().addFirst("sslFilter",sslFilter);
  }
  super.sessionOpened(session);
}
 

Example 43

From project salem, under directory /src/haven/.

Source file: SslHelper.java

  21 
vote

private synchronized SSLContext ctx(){
  if (ctx == null) {
    TrustManagerFactory tmf;
    KeyManagerFactory kmf;
    try {
      ctx=SSLContext.getInstance("TLS");
      tmf=TrustManagerFactory.getInstance("PKIX");
      kmf=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
      KeyManager[] kms=null;
      tmf.init(trusted);
      if (creds != null) {
        kmf.init(creds,pw);
        kms=kmf.getKeyManagers();
      }
      ctx.init(kms,tmf.getTrustManagers(),new SecureRandom());
    }
 catch (    NoSuchAlgorithmException e) {
      throw (new Error(e));
    }
catch (    KeyStoreException e) {
      throw (new RuntimeException(e));
    }
catch (    UnrecoverableKeyException e) {
      throw (new RuntimeException(e));
    }
catch (    KeyManagementException e) {
      throw (new RuntimeException(e));
    }
  }
  return (ctx);
}
 

Example 44

From project sisu-goodies, under directory /crypto/src/main/java/org/sonatype/sisu/goodies/crypto/internal/.

Source file: CryptoHelperImpl.java

  21 
vote

@Override public KeyManagerFactory createKeyManagerFactory(final String algorithm) throws NoSuchAlgorithmException {
  checkNotNull(algorithm);
  KeyManagerFactory obj;
  try {
    obj=KeyManagerFactory.getInstance(algorithm,getProvider());
  }
 catch (  NoSuchAlgorithmException e) {
    logFallback(e);
    obj=KeyManagerFactory.getInstance(algorithm);
  }
  if (log.isTraceEnabled()) {
    log.trace("Created key-manager-factory: {} ({})",obj.getAlgorithm(),obj.getProvider().getName());
  }
  return obj;
}
 

Example 45

From project smsc-server, under directory /core/src/main/java/org/apache/smscserver/ssl/.

Source file: SslConfigurationFactory.java

  21 
vote

/** 
 * Create an instance of  {@link SslConfiguration} based on the configuration of this factory.
 * @return The {@link SslConfiguration} instance
 */
public SslConfiguration createSslConfiguration(){
  try {
    this.LOG.debug("Loading key store from \"{}\", using the key store type \"{}\"",this.keystoreFile.getAbsolutePath(),this.keystoreType);
    KeyStore keyStore=this.loadStore(this.keystoreFile,this.keystoreType,this.keystorePass);
    KeyStore trustStore;
    if (this.trustStoreFile != null) {
      this.LOG.debug("Loading trust store from \"{}\", using the key store type \"{}\"",this.trustStoreFile.getAbsolutePath(),this.trustStoreType);
      trustStore=this.loadStore(this.trustStoreFile,this.trustStoreType,this.trustStorePass);
    }
 else {
      trustStore=keyStore;
    }
    String keyPassToUse;
    if (this.keyPass == null) {
      keyPassToUse=this.keystorePass;
    }
 else {
      keyPassToUse=this.keyPass;
    }
    KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance(this.keystoreAlgorithm);
    keyManagerFactory.init(keyStore,keyPassToUse.toCharArray());
    TrustManagerFactory trustManagerFactory=TrustManagerFactory.getInstance(this.trustStoreAlgorithm);
    trustManagerFactory.init(trustStore);
    return new DefaultSslConfiguration(keyManagerFactory,trustManagerFactory,this.clientAuth,this.sslProtocol,this.enabledCipherSuites,this.keyAlias);
  }
 catch (  Exception ex) {
    this.LOG.error("DefaultSsl.configure()",ex);
    throw new SmscServerConfigurationException("DefaultSsl.configure()",ex);
  }
}
 

Example 46

From project subsonic, under directory /subsonic-android/src/github/daneren2005/dsub/service/ssl/.

Source file: SSLSocketFactory.java

  21 
vote

private static SSLContext createSSLContext(String algorithm,final KeyStore keystore,final String keystorePassword,final KeyStore truststore,final SecureRandom random,final TrustStrategy trustStrategy) throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException {
  if (algorithm == null) {
    algorithm=TLS;
  }
  KeyManagerFactory kmfactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmfactory.init(keystore,keystorePassword != null ? keystorePassword.toCharArray() : null);
  KeyManager[] keymanagers=kmfactory.getKeyManagers();
  TrustManagerFactory tmfactory=TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  tmfactory.init(keystore);
  TrustManager[] trustmanagers=tmfactory.getTrustManagers();
  if (trustmanagers != null && trustStrategy != null) {
    for (int i=0; i < trustmanagers.length; i++) {
      TrustManager tm=trustmanagers[i];
      if (tm instanceof X509TrustManager) {
        trustmanagers[i]=new TrustManagerDecorator((X509TrustManager)tm,trustStrategy);
      }
    }
  }
  SSLContext sslcontext=SSLContext.getInstance(algorithm);
  sslcontext.init(keymanagers,trustmanagers,random);
  return sslcontext;
}
 

Example 47

From project Subsonic-Android, under directory /src/net/sourceforge/subsonic/androidapp/service/ssl/.

Source file: SSLSocketFactory.java

  21 
vote

private static SSLContext createSSLContext(String algorithm,final KeyStore keystore,final String keystorePassword,final KeyStore truststore,final SecureRandom random,final TrustStrategy trustStrategy) throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException {
  if (algorithm == null) {
    algorithm=TLS;
  }
  KeyManagerFactory kmfactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmfactory.init(keystore,keystorePassword != null ? keystorePassword.toCharArray() : null);
  KeyManager[] keymanagers=kmfactory.getKeyManagers();
  TrustManagerFactory tmfactory=TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  tmfactory.init(keystore);
  TrustManager[] trustmanagers=tmfactory.getTrustManagers();
  if (trustmanagers != null && trustStrategy != null) {
    for (int i=0; i < trustmanagers.length; i++) {
      TrustManager tm=trustmanagers[i];
      if (tm instanceof X509TrustManager) {
        trustmanagers[i]=new TrustManagerDecorator((X509TrustManager)tm,trustStrategy);
      }
    }
  }
  SSLContext sslcontext=SSLContext.getInstance(algorithm);
  sslcontext.init(keymanagers,trustmanagers,random);
  return sslcontext;
}
 

Example 48

From project subsonic_1, under directory /subsonic-android/src/net/sourceforge/subsonic/androidapp/service/ssl/.

Source file: SSLSocketFactory.java

  21 
vote

private static SSLContext createSSLContext(String algorithm,final KeyStore keystore,final String keystorePassword,final KeyStore truststore,final SecureRandom random,final TrustStrategy trustStrategy) throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException {
  if (algorithm == null) {
    algorithm=TLS;
  }
  KeyManagerFactory kmfactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmfactory.init(keystore,keystorePassword != null ? keystorePassword.toCharArray() : null);
  KeyManager[] keymanagers=kmfactory.getKeyManagers();
  TrustManagerFactory tmfactory=TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  tmfactory.init(keystore);
  TrustManager[] trustmanagers=tmfactory.getTrustManagers();
  if (trustmanagers != null && trustStrategy != null) {
    for (int i=0; i < trustmanagers.length; i++) {
      TrustManager tm=trustmanagers[i];
      if (tm instanceof X509TrustManager) {
        trustmanagers[i]=new TrustManagerDecorator((X509TrustManager)tm,trustStrategy);
      }
    }
  }
  SSLContext sslcontext=SSLContext.getInstance(algorithm);
  sslcontext.init(keymanagers,trustmanagers,random);
  return sslcontext;
}
 

Example 49

From project subsonic_2, under directory /subsonic-android/src/net/sourceforge/subsonic/androidapp/service/ssl/.

Source file: SSLSocketFactory.java

  21 
vote

private static SSLContext createSSLContext(String algorithm,final KeyStore keystore,final String keystorePassword,final KeyStore truststore,final SecureRandom random,final TrustStrategy trustStrategy) throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException {
  if (algorithm == null) {
    algorithm=TLS;
  }
  KeyManagerFactory kmfactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmfactory.init(keystore,keystorePassword != null ? keystorePassword.toCharArray() : null);
  KeyManager[] keymanagers=kmfactory.getKeyManagers();
  TrustManagerFactory tmfactory=TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  tmfactory.init(keystore);
  TrustManager[] trustmanagers=tmfactory.getTrustManagers();
  if (trustmanagers != null && trustStrategy != null) {
    for (int i=0; i < trustmanagers.length; i++) {
      TrustManager tm=trustmanagers[i];
      if (tm instanceof X509TrustManager) {
        trustmanagers[i]=new TrustManagerDecorator((X509TrustManager)tm,trustStrategy);
      }
    }
  }
  SSLContext sslcontext=SSLContext.getInstance(algorithm);
  sslcontext.init(keymanagers,trustmanagers,random);
  return sslcontext;
}
 

Example 50

From project Supersonic, under directory /subsonic-android/src/net/sourceforge/subsonic/androidapp/service/ssl/.

Source file: SSLSocketFactory.java

  21 
vote

private static SSLContext createSSLContext(String algorithm,final KeyStore keystore,final String keystorePassword,final KeyStore truststore,final SecureRandom random,final TrustStrategy trustStrategy) throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException {
  if (algorithm == null) {
    algorithm=TLS;
  }
  KeyManagerFactory kmfactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmfactory.init(keystore,keystorePassword != null ? keystorePassword.toCharArray() : null);
  KeyManager[] keymanagers=kmfactory.getKeyManagers();
  TrustManagerFactory tmfactory=TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  tmfactory.init(keystore);
  TrustManager[] trustmanagers=tmfactory.getTrustManagers();
  if (trustmanagers != null && trustStrategy != null) {
    for (int i=0; i < trustmanagers.length; i++) {
      TrustManager tm=trustmanagers[i];
      if (tm instanceof X509TrustManager) {
        trustmanagers[i]=new TrustManagerDecorator((X509TrustManager)tm,trustStrategy);
      }
    }
  }
  SSLContext sslcontext=SSLContext.getInstance(algorithm);
  sslcontext.init(keymanagers,trustmanagers,random);
  return sslcontext;
}
 

Example 51

From project thumbslug, under directory /src/main/java/org/candlepin/thumbslug/ssl/.

Source file: SslContextFactory.java

  21 
vote

public static SSLContext getServerContext(String keystoreUrl,String keystorePassword) throws SslKeystoreException {
  if (serverContext != null) {
    return serverContext;
  }
  String algorithm=Security.getProperty("ssl.KeyManagerFactory.algorithm");
  if (algorithm == null) {
    algorithm="SunX509";
  }
  FileInputStream fis=null;
  try {
    log.info("reading keystore");
    fis=new FileInputStream(new File(keystoreUrl));
    KeyStore ks=KeyStore.getInstance("PKCS12");
    ks.load(fis,keystorePassword.toCharArray());
    KeyManagerFactory kmf=KeyManagerFactory.getInstance(algorithm);
    kmf.init(ks,keystorePassword.toCharArray());
    serverContext=SSLContext.getInstance(PROTOCOL);
    serverContext.init(kmf.getKeyManagers(),ServerContextTrustManagerFactory.getTrustManagers(),null);
  }
 catch (  Exception e) {
    throw new SslKeystoreException("Failed to initialize the server-side SSLContext.",e);
  }
 finally {
    if (fis != null) {
      try {
        fis.close();
      }
 catch (      IOException e) {
        throw new Error("Failed to initialize the client-side SSLContext",e);
      }
    }
  }
  return serverContext;
}
 

Example 52

From project TiVo-Commander, under directory /src/com/arantius/tivocommander/rpc/.

Source file: MindRpc.java

  21 
vote

private static SSLSocketFactory createSocketFactory(final Activity originActivity){
  final String password=readPassword(originActivity);
  try {
    KeyStore keyStore=KeyStore.getInstance("PKCS12");
    KeyManagerFactory fac=KeyManagerFactory.getInstance("X509");
    InputStream keyInput=originActivity.getResources().openRawResource(R.raw.cdata);
    keyStore.load(keyInput,password.toCharArray());
    keyInput.close();
    fac.init(keyStore,password.toCharArray());
    SSLContext context=SSLContext.getInstance("TLS");
    TrustManager[] tm=new TrustManager[]{new AlwaysTrustManager()};
    context.init(fac.getKeyManagers(),tm,new SecureRandom());
    return context.getSocketFactory();
  }
 catch (  CertificateException e) {
    Log.e(LOG_TAG,"createSocketFactory: CertificateException!",e);
  }
catch (  IOException e) {
    Log.e(LOG_TAG,"createSocketFactory: IOException!",e);
  }
catch (  KeyManagementException e) {
    Log.e(LOG_TAG,"createSocketFactory: KeyManagementException!",e);
  }
catch (  KeyStoreException e) {
    Log.e(LOG_TAG,"createSocketFactory: KeyStoreException!",e);
  }
catch (  NoSuchAlgorithmException e) {
    Log.e(LOG_TAG,"createSocketFactory: NoSuchAlgorithmException!",e);
  }
catch (  UnrecoverableKeyException e) {
    Log.e(LOG_TAG,"createSocketFactory: UnrecoverableKeyException!",e);
  }
  return null;
}
 

Example 53

From project vysper, under directory /server/core/src/main/java/org/apache/vysper/xmpp/cryptography/.

Source file: AbstractTLSContextFactory.java

  21 
vote

private SSLContext createSSLContext() throws GeneralSecurityException, IOException {
  KeyStore ks=KeyStore.getInstance(keystoreType);
  InputStream in=null;
  try {
    in=getCertificateInputStream();
    ks.load(in,password.toCharArray());
  }
  finally {
    if (in != null) {
      try {
        in.close();
      }
 catch (      IOException ignored) {
        ;
      }
    }
  }
  KeyManagerFactory kmf=KeyManagerFactory.getInstance(KEY_MANAGER_FACTORY_ALGORITHM);
  kmf.init(ks,password.toCharArray());
  SSLContext sslContext=SSLContext.getInstance(PROTOCOL);
  sslContext.init(kmf.getKeyManagers(),trustManagerFactory.getTrustManagers(),null);
  return sslContext;
}
 

Example 54

From project winstone, under directory /src/java/winstone/ssl/.

Source file: HttpsListener.java

  21 
vote

/** 
 * Used to get the base ssl context in which to create the server socket. This is basically just so we can have a custom location for key stores.
 */
public SSLContext getSSLContext(){
  try {
    KeyManagerFactory kmf=KeyManagerFactory.getInstance(this.keyManagerType);
    kmf.init(keystore,password);
    Logger.log(Logger.FULL_DEBUG,SSL_RESOURCES,"HttpsListener.KeyCount",keystore.size() + "");
    for (Enumeration e=keystore.aliases(); e.hasMoreElements(); ) {
      String alias=(String)e.nextElement();
      Logger.log(Logger.FULL_DEBUG,SSL_RESOURCES,"HttpsListener.KeyFound",alias,keystore.getCertificate(alias) + "");
    }
    SSLContext context=SSLContext.getInstance("SSL");
    context.init(kmf.getKeyManagers(),null,null);
    return context;
  }
 catch (  Throwable err) {
    throw new WinstoneException(SSL_RESOURCES.getString("HttpsListener.ErrorGettingContext"),err);
  }
}
 

Example 55

From project QuasselDroid, under directory /src/com/iskrembilen/quasseldroid/io/.

Source file: CustomTrustManager.java

  19 
vote

CustomTrustManager(CoreConnection coreConnection) throws GeneralSecurityException {
  this.coreConnection=coreConnection;
  KeyStore ks=KeyStore.getInstance(KeyStore.getDefaultType());
  TrustManagerFactory tmf=TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  tmf.init(ks);
  TrustManager tms[]=tmf.getTrustManagers();
  for (int i=0; i < tms.length; i++) {
    if (tms[i] instanceof X509TrustManager) {
      defaultTrustManager=(X509TrustManager)tms[i];
      return;
    }
  }
  throw new GeneralSecurityException("Couldn't initialize certificate management!");
}
 

Example 56

From project WaarpCommon, under directory /src/main/java/org/waarp/common/crypto/ssl/.

Source file: WaarpSecureKeyStore.java

  19 
vote

/** 
 * Init KeyManagerFactory
 * @throws CryptoException
 */
void initKeyManagerFactory() throws CryptoException {
  try {
    keyManagerFactory=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  }
 catch (  NoSuchAlgorithmException e) {
    logger.error("Cannot create KeyManagerFactory Instance",e);
    throw new CryptoException("Cannot create KeyManagerFactory Instance",e);
  }
  try {
    keyManagerFactory.init(keyStore,getCertificatePassword());
  }
 catch (  UnrecoverableKeyException e) {
    logger.error("Cannot create KeyManagerFactory Instance",e);
    throw new CryptoException("Cannot create KeyManagerFactory Instance",e);
  }
catch (  KeyStoreException e) {
    logger.error("Cannot create KeyManagerFactory Instance",e);
    throw new CryptoException("Cannot create KeyManagerFactory Instance",e);
  }
catch (  NoSuchAlgorithmException e) {
    logger.error("Cannot create KeyManagerFactory Instance",e);
    throw new CryptoException("Cannot create KeyManagerFactory Instance",e);
  }
}