use of javax.net.ssl.SSLSession 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 javax.net.ssl.SSLSession 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;
}
use of javax.net.ssl.SSLSession in project yamcs-studio by yamcs.
the class ResourceUtil method openRawURLStream.
/**
* Open URL Stream from remote.
*
* @param url
* @return
* @throws IOException
*/
private static InputStream openRawURLStream(final URL url) throws IOException {
if (url.getProtocol().equals("https")) {
// $NON-NLS-1$
// The code to support https protocol is provided by Eric Berryman (eric.berryman@gmail.com) from Frib
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
// Install the all-trusting trust manager
SSLContext sc = null;
try {
sc = SSLContext.getInstance("SSL");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
try {
sc.init(null, trustAllCerts, new java.security.SecureRandom());
} catch (KeyManagementException e) {
e.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}
URLConnection connection = url.openConnection();
connection.setReadTimeout(PreferencesHelper.getURLFileLoadingTimeout());
return connection.getInputStream();
}
use of javax.net.ssl.SSLSession in project athenz by yahoo.
the class ZTSClientTest method testHostNameVerifierVerifyCertNull.
@Test
public void testHostNameVerifierVerifyCertNull() throws SSLPeerUnverifiedException {
ZTSRDLClientMock ztsClientMock = new ZTSRDLClientMock();
Principal principal = SimplePrincipal.create("user_domain", "user", "v=S1;d=user_domain;n=user;s=sig", PRINCIPAL_AUTHORITY);
ZTSClient client = new ZTSClient("http://localhost:4080", principal);
client.setZTSRDLGeneratedClient(ztsClientMock);
ZTSClient.AWSHostNameVerifier hostnameVerifier = client.new AWSHostNameVerifier("host1");
SSLSession session = Mockito.mock(SSLSession.class);
Mockito.when(session.getPeerCertificates()).thenReturn(null);
assertFalse(hostnameVerifier.verify("host1", session));
System.out.println("hashCode:" + client.hashCode());
client.close();
}
use of javax.net.ssl.SSLSession in project athenz by yahoo.
the class ZTSClientTest method testHostNameVerifierVerifyCert.
@Test
public void testHostNameVerifierVerifyCert() throws CertificateException, IOException {
ZTSRDLClientMock ztsClientMock = new ZTSRDLClientMock();
Principal principal = SimplePrincipal.create("user_domain", "user", "v=S1;d=user_domain;n=user;s=sig", PRINCIPAL_AUTHORITY);
ZTSClient client = new ZTSClient("http://localhost:4080", principal);
client.setZTSRDLGeneratedClient(ztsClientMock);
ZTSClient.AWSHostNameVerifier hostnameVerifier = client.new AWSHostNameVerifier("host1");
InputStream is = new ByteArrayInputStream(test_cert.getBytes("utf-8"));
CertificateFactory cf = CertificateFactory.getInstance("X.509");
java.security.cert.Certificate cert = cf.generateCertificate(is);
is.close();
Certificate[] certs = new Certificate[1];
certs[0] = cert;
SSLSession session = Mockito.mock(SSLSession.class);
Mockito.when(session.getPeerCertificates()).thenReturn(certs);
assertFalse(hostnameVerifier.verify("unknown", session));
client.close();
}
Aggregations