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();
}
}
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);
}
}
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);
}
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;
}
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;
}
Aggregations