Search in sources :

Example 71 with KeyManagementException

use of java.security.KeyManagementException in project leopard by tanhaichao.

the class Https method initSSLContext.

// @SuppressLint("TrulyRandom")
public static void initSSLContext() {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
        SSLContext.setDefault(ctx);
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}
Also used : SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException)

Example 72 with KeyManagementException

use of java.security.KeyManagementException in project vcell by virtualcell.

the class RemoteRegistrationService method sendLostPassword.

@Override
public void sendLostPassword(String userid) throws DataAccessException, RemoteProxyException {
    // e.g. vcell.serverhost=vcellapi.cam.uchc.edu:8080
    String serverHost = PropertyLoader.getRequiredProperty(PropertyLoader.vcellServerHost);
    String[] parts = serverHost.split(":");
    String host = parts[0];
    int port = Integer.parseInt(parts[1]);
    boolean bIgnoreCertProblems = false;
    boolean bIgnoreHostMismatch = false;
    VCellApiClient apiClient;
    try {
        apiClient = new VCellApiClient(host, port, bIgnoreCertProblems, bIgnoreHostMismatch);
    } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
        e.printStackTrace();
        throw new RemoteProxyException("failure in send lost password request: " + e.getMessage(), e);
    }
    try {
        apiClient.sendLostPassword(userid);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RemoteProxyException("failed to request lost password: " + e.getMessage(), e);
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) VCellApiClient(org.vcell.api.client.VCellApiClient) KeyManagementException(java.security.KeyManagementException) RemoteProxyException(cbit.vcell.message.server.bootstrap.client.RemoteProxyVCellConnectionFactory.RemoteProxyException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) DataAccessException(org.vcell.util.DataAccessException) UseridIDExistsException(org.vcell.util.UseridIDExistsException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RemoteProxyException(cbit.vcell.message.server.bootstrap.client.RemoteProxyVCellConnectionFactory.RemoteProxyException)

Example 73 with KeyManagementException

use of java.security.KeyManagementException in project vcell by virtualcell.

the class RemoteRegistrationService method insertUserInfo.

@Override
public UserInfo insertUserInfo(UserInfo newUserInfo, boolean bUpdate) throws RemoteProxyException, DataAccessException, UseridIDExistsException {
    // e.g. vcell.serverhost=vcellapi.cam.uchc.edu:8080
    String serverHost = PropertyLoader.getRequiredProperty(PropertyLoader.vcellServerHost);
    String[] parts = serverHost.split(":");
    String host = parts[0];
    int port = Integer.parseInt(parts[1]);
    boolean bIgnoreCertProblems = false;
    boolean bIgnoreHostMismatch = false;
    VCellApiClient apiClient;
    try {
        apiClient = new VCellApiClient(host, port, bIgnoreCertProblems, bIgnoreHostMismatch);
    } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
        e.printStackTrace();
        throw new RemoteProxyException("failure inserting user: " + e.getMessage(), e);
    }
    org.vcell.api.common.UserInfo apiUserInfo;
    try {
        apiUserInfo = apiClient.insertUserInfo(newUserInfo.getApiUserInfo());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RemoteProxyException("failed to insert user: " + e.getMessage(), e);
    }
    return UserInfo.fromApiUserInfo(apiUserInfo);
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) VCellApiClient(org.vcell.api.client.VCellApiClient) KeyManagementException(java.security.KeyManagementException) RemoteProxyException(cbit.vcell.message.server.bootstrap.client.RemoteProxyVCellConnectionFactory.RemoteProxyException)

Example 74 with KeyManagementException

use of java.security.KeyManagementException in project opentheso by miledrousset.

the class HandleClient method deleteHandle.

/**
 * Permet de supprimer l'identifiant Handle d'une resource
 * @param pass
 * @param pathKey
 * @param pathCert
 * @param urlHandle
 * @param idHandle
 * @return
 */
public boolean deleteHandle(String pass, String pathKey, String pathCert, String urlHandle, String idHandle) {
    // exp : idHandle = (20.500.11942/LDx76olvIm)
    String output;
    String xmlRecord = "";
    try {
        KeyStore clientStore = KeyStore.getInstance("PKCS12");
        // "motdepasse" = le mot de passe saisie pour la génération des certificats.
        // clientStore.load(new FileInputStream("key.p12"), "motdepasse".toCharArray());
        clientStore.load(this.getClass().getResourceAsStream(pathKey), pass.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(clientStore, pass.toCharArray());
        KeyStore trustStore = KeyStore.getInstance("JKS");
        // trustStore.load(new FileInputStream("cacerts2"), pass.toCharArray());
        trustStore.load(this.getClass().getResourceAsStream(pathCert), pass.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(trustStore);
        SSLContext sslContext;
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());
        // URL url = new URL("https://cchum-isi-handle01.in2p3.fr:8001/api/handles/20.500.11942/opentheso443");
        URL url = new URL(urlHandle + idHandle);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setSSLSocketFactory(sslContext.getSocketFactory());
        conn.setRequestMethod("DELETE");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "Handle clientCert=\"true\"");
        conn.setHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }
        });
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        int status = conn.getResponseCode();
        InputStream in = status >= 400 ? conn.getErrorStream() : conn.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        while ((output = br.readLine()) != null) {
            xmlRecord += output;
        }
        byte[] bytes = xmlRecord.getBytes();
        xmlRecord = new String(bytes, Charset.forName("UTF-8"));
        if (status == 200) {
            message = "Suppression du Handle réussie";
        }
        if (status == 100) {
            message = "Handle n'existe pas";
        }
        message = message + "\n" + xmlRecord;
        message = message + "\n" + "status de la réponse : " + status;
        return true;
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (KeyStoreException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (CertificateException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnrecoverableKeyException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (KeyManagementException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (MalformedURLException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}
Also used : MalformedURLException(java.net.MalformedURLException) CertificateException(java.security.cert.CertificateException) JsonString(javax.json.JsonString) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URL(java.net.URL) KeyManagementException(java.security.KeyManagementException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SSLContext(javax.net.ssl.SSLContext) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) HostnameVerifier(javax.net.ssl.HostnameVerifier) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) BufferedReader(java.io.BufferedReader) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 75 with KeyManagementException

use of java.security.KeyManagementException in project opentheso by miledrousset.

the class HandleClient method updateHandle.

/**
 * Permet de mettre à jour l'URL et les données d'une resource Handle
 * cette fonction donne la même action que le putHandle
 * @param pass
 * @param pathKey
 * @param pathCert
 * @param urlHandle
 * @param idHandle
 * @param jsonData
 * @return
 */
public boolean updateHandle(String pass, String pathKey, String pathCert, String urlHandle, String idHandle, String jsonData) {
    String output;
    String xmlRecord = "";
    try {
        KeyStore clientStore = KeyStore.getInstance("PKCS12");
        // "motdepasse" = le mot de passe saisie pour la génération des certificats.
        // clientStore.load(new FileInputStream("key.p12"), "motdepasse".toCharArray());
        clientStore.load(this.getClass().getResourceAsStream(pathKey), pass.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(clientStore, pass.toCharArray());
        KeyStore trustStore = KeyStore.getInstance("JKS");
        // trustStore.load(new FileInputStream("cacerts2"), pass.toCharArray());
        trustStore.load(this.getClass().getResourceAsStream(pathCert), pass.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(trustStore);
        SSLContext sslContext;
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());
        // URL url = new URL("https://cchum-isi-handle01.in2p3.fr:8001/api/handles/20.500.11942/opentheso443");
        URL url = new URL(urlHandle + idHandle);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setSSLSocketFactory(sslContext.getSocketFactory());
        conn.setRequestMethod("PUT");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "Handle clientCert=\"true\"");
        conn.setHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }
        });
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        OutputStream os = conn.getOutputStream();
        OutputStreamWriter out = new OutputStreamWriter(os);
        out.write(jsonData);
        out.flush();
        int status = conn.getResponseCode();
        InputStream in = status >= 400 ? conn.getErrorStream() : conn.getInputStream();
        // status = 201 = création réussie
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        while ((output = br.readLine()) != null) {
            xmlRecord += output;
        }
        byte[] bytes = xmlRecord.getBytes();
        xmlRecord = new String(bytes, Charset.forName("UTF-8"));
        os.close();
        conn.disconnect();
        if (status == 200) {
            message = "Mise à jour du Handle réussie";
        }
        if (status == 100) {
            message = "Handle n'existe pas";
        }
        message = message + "\n" + xmlRecord;
        message = message + "\n" + "status de la réponse : " + status;
        return true;
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (KeyStoreException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (CertificateException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnrecoverableKeyException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (KeyManagementException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (MalformedURLException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(HandleClient.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}
Also used : MalformedURLException(java.net.MalformedURLException) OutputStream(java.io.OutputStream) CertificateException(java.security.cert.CertificateException) JsonString(javax.json.JsonString) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URL(java.net.URL) KeyManagementException(java.security.KeyManagementException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SSLContext(javax.net.ssl.SSLContext) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) HostnameVerifier(javax.net.ssl.HostnameVerifier) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

KeyManagementException (java.security.KeyManagementException)157 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)111 SSLContext (javax.net.ssl.SSLContext)83 KeyStoreException (java.security.KeyStoreException)60 IOException (java.io.IOException)55 TrustManager (javax.net.ssl.TrustManager)45 CertificateException (java.security.cert.CertificateException)35 X509TrustManager (javax.net.ssl.X509TrustManager)28 SecureRandom (java.security.SecureRandom)27 X509Certificate (java.security.cert.X509Certificate)26 UnrecoverableKeyException (java.security.UnrecoverableKeyException)24 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)24 KeyStore (java.security.KeyStore)22 KeyManager (javax.net.ssl.KeyManager)19 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)16 HostnameVerifier (javax.net.ssl.HostnameVerifier)15 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)15 InputStream (java.io.InputStream)12 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)11 SSLSession (javax.net.ssl.SSLSession)10