From c1d04c110f717521d0f6bed3e57fdcd39b5d0498 Mon Sep 17 00:00:00 2001 From: Bianca Schnalzer Date: Mon, 7 Aug 2017 08:13:09 +0200 Subject: Certificate Download Server + Searching for Placeholder --- .../java/at/asit/pdfover/gui/bku/BKUHelper.java | 43 +++++++++++++---- .../at/asit/pdfover/gui/bku/LocalBKUConnector.java | 1 + .../gui/bku/mobile/SimpleXMLTrustManager.java | 55 ++++++++++++++++++++-- 3 files changed, 84 insertions(+), 15 deletions(-) (limited to 'pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku') diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/BKUHelper.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/BKUHelper.java index 427afad5..82d6f07d 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/BKUHelper.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/BKUHelper.java @@ -15,9 +15,9 @@ */ package at.asit.pdfover.gui.bku; -// Imports import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.UsernamePasswordCredentials; +// Imports import org.apache.commons.httpclient.auth.AuthScope; import org.apache.http.client.config.CookieSpecs; import org.slf4j.Logger; @@ -35,6 +35,30 @@ public class BKUHelper { @SuppressWarnings("unused") private static final Logger log = LoggerFactory.getLogger(BKUHelper.class); + /* public static HttpClient getHttpClient(boolean useProxy) { + HttpClient client = new HttpClient(); + client.getParams().setParameter("http.useragent", //$NON-NLS-1$ + Constants.USER_AGENT_STRING); + + if (useProxy) { + String host = System.getProperty("http.proxyHost"); //$NON-NLS-1$ + String port = System.getProperty("http.proxyPort"); //$NON-NLS-1$ + if (host != null && !host.isEmpty() && + port != null && !port.isEmpty()) { + int p = Integer.parseInt(port); + client.getHostConfiguration().setProxy(host, p); + String user = System.getProperty("http.proxyUser"); //$NON-NLS-1$ + String pass = System.getProperty("http.proxyPassword"); //$NON-NLS-1$ + if (user != null && !user.isEmpty() && pass != null) { + client.getState().setProxyCredentials(new AuthScope(host, p), + new UsernamePasswordCredentials(user, pass)); + } + } + } + + return client; + }*/ + /** * Get a HTTP Client instance * @@ -42,25 +66,23 @@ public class BKUHelper { * whether to use a potentially set proxy * @return the HttpClient */ + @SuppressWarnings("deprecation") public static HttpClient getHttpClient(boolean useProxy) { HttpClient client = new HttpClient(); client.getParams().setParameter("http.useragent", //$NON-NLS-1$ Constants.USER_AGENT_STRING); - client.getParams().setParameter("http.protocol.cookie-policy", CookieSpecs.BROWSER_COMPATIBILITY); + + client.getParams().setParameter("http.protocol.cookie-policy", CookieSpecs.BROWSER_COMPATIBILITY); //$NON-NLS-1$ if (useProxy) { - String host = System.getProperty("http.proxyHost"); - // $NON-NLS-1$ - String port = System.getProperty("http.proxyPort"); - // $NON-NLS-1$ + String host = System.getProperty("http.proxyHost");//$NON-NLS-1$ + String port = System.getProperty("http.proxyPort");//$NON-NLS-1$ if (host != null && !host.isEmpty() && port != null && !port.isEmpty()) { int p = Integer.parseInt(port); client.getHostConfiguration().setProxy(host, p); - String user = System.getProperty("http.proxyUser"); - // $NON-NLS-1$ - String pass = System.getProperty("http.proxyPassword"); - // $NON-NLS-1$ + String user = System.getProperty("http.proxyUser");//$NON-NLS-1$ + String pass = System.getProperty("http.proxyPassword");//$NON-NLS-1$ if (user != null && !user.isEmpty() && pass != null) { client.getState().setProxyCredentials(new AuthScope(host, p), new UsernamePasswordCredentials(user, pass)); @@ -69,6 +91,7 @@ public class BKUHelper { } return client; + } /** diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/LocalBKUConnector.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/LocalBKUConnector.java index eb1b57d6..1c88499a 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/LocalBKUConnector.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/LocalBKUConnector.java @@ -18,6 +18,7 @@ package at.asit.pdfover.gui.bku; // Imports import java.io.IOException; + import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/SimpleXMLTrustManager.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/SimpleXMLTrustManager.java index bceb59fb..ec4b674b 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/SimpleXMLTrustManager.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/SimpleXMLTrustManager.java @@ -15,12 +15,15 @@ */ package at.asit.pdfover.gui.bku.mobile; +import java.io.File; +import java.io.FileInputStream; // Imports import java.security.KeyStore; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.util.Arrays; +import java.util.List; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; @@ -82,7 +85,7 @@ public class SimpleXMLTrustManager implements X509TrustManager { } /* - * A-Trust Certificates + *Certificates */ KeyStore myKeyStore = KeyStore.getInstance(KeyStore @@ -93,16 +96,25 @@ public class SimpleXMLTrustManager implements X509TrustManager { Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(this.getClass().getResourceAsStream(Constants.RES_CERT_LIST)); - + + + File added_cert = new File(Constants.RES_CERT_LIST_ADDED); + + Document doc_added = DocumentBuilderFactory.newInstance() + .newDocumentBuilder() + .parse(added_cert); + + Node certificates_added = doc_added.getFirstChild(); Node certificates = doc.getFirstChild(); - if (!certificates.getNodeName().equals("certificates")) { //$NON-NLS-1$ + if (!certificates.getNodeName().equals("certificates") && !certificates_added.getNodeName().equals("certificates")) { //$NON-NLS-1$ throw new Exception( "Used certificates xml is invalid! no certificates node"); //$NON-NLS-1$ } + NodeList certificates_added_list = certificates_added.getChildNodes(); NodeList certificateList = certificates.getChildNodes(); - + for (int i = 0; i < certificateList.getLength(); i++) { try { @@ -117,7 +129,7 @@ public class SimpleXMLTrustManager implements X509TrustManager { continue; } - String certResource = Constants.RES_CERT_PATH + certificateNode.getTextContent(); + String certResource = Constants.RES_CERT_PATH_ADDED + certificateNode.getTextContent(); X509Certificate cert = (X509Certificate) CertificateFactory .getInstance("X509"). //$NON-NLS-1$ @@ -133,6 +145,39 @@ public class SimpleXMLTrustManager implements X509TrustManager { log.error("Failed to load certificate [" + "]", ex); //$NON-NLS-1$ //$NON-NLS-2$ } } + + for (int i = 0; i < certificates_added_list.getLength(); i++) { + try { + + Node certificateNode = certificates_added_list.item(i); + + if (certificateNode.getNodeName().equals("#text")) { //$NON-NLS-1$ + continue; // Ignore dummy text node .. + } + + if (!certificateNode.getNodeName().equals("certificate")) { //$NON-NLS-1$ + log.warn("Ignoring XML node: " + certificateNode.getNodeName()); //$NON-NLS-1$ + continue; + } + + String certResource = Constants.RES_CERT_PATH_ADDED + certificateNode.getTextContent(); + + FileInputStream addedNode = new FileInputStream(certResource); + + X509Certificate cert = (X509Certificate) CertificateFactory + .getInstance("X509"). //$NON-NLS-1$ + generateCertificate( + addedNode); + + myKeyStore.setCertificateEntry(certificateNode.getTextContent(), cert); + + log.debug("Loaded certificate : " + certResource); //$NON-NLS-1$ + + } catch (Exception ex) { + log.error("Failed to load certificate [" + "]", ex); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + tmf.init(myKeyStore); -- cgit v1.2.3