Search code examples
javaautomationscreen-scrapinghtmlunit

Getting an exception "SSLPeerUnverifiedException: peer not authenticated", how can I avoid/ignore it?


I am trying to log in to a website using HtmlUnit (Java Library), and am getting an exception that I feel I could just ignore. The exception is "javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated". I've narrowed this down to the certificate supports "www.thesite.com", however it does use "somedomain.thesite.com". The error I believe is due to the certificate not naming the other subdomain it uses in some javascript functions. It's very strange as when using Firefox by hand, I do not see these exceptions, making me think there is something else happening.

Below is the code I am using:

// <editor-fold desc="User Credentials and site.">
String user = "auser";
String password = "apassword";

String mainUrl = "https://www.awebsite.com/";

// Setup Ajax & Cookies
AjaxController ajaxController = new AjaxController();
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiesEnabled(true);

// Setup WebClient
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setAppletEnabled(true);
webClient.setCssEnabled(true);
webClient.setJavaScriptEnabled(true);
webClient.setAjaxController(ajaxController);
webClient.setThrowExceptionOnScriptError(false);
webClient.setThrowExceptionOnFailingStatusCode(false);
webClient.setCookieManager(cookieManager);

/*
 * Provider p = new sun.security.pkcs11.SunPKCS11();
 * Security.addProvider(p);
 */

//webClient.set
HtmlPage page = null;

try {
    webClient.setUseInsecureSSL(true);
    page = webClient.getPage(mainUrl);

    // fill in form
    HtmlForm htmlForm = page.getFormByName("thisForm");
    HtmlInput userField = htmlForm.getInputByName("Username");
    HtmlInput passwordField = htmlForm.getInputByName("Password");

    // Enter login and password
    userField.setValueAttribute(user);
    passwordField.setValueAttribute(password);

    //HtmlPage mainMenuPage = htmlForm.click();
    HtmlElement element = htmlForm.getInputByValue("Login");

    page = element.click();
    webClient.waitForBackgroundJavaScript(5000); // pause if

    System.err.println(page.asText());
    System.err.println(page.asXml());

} catch (Exception ex) {
    ex.printStackTrace();
}

webClient.closeAllWindows();

}

As you can see, I have enabled 'webClient.setUseInsecureSSL(true);' in the HtmlUnit library. I figure this should do it. However, at this point I am stuck.

Here is the exception error I am getting:

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
    at com.sun.net.ssl.internal.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:352)
    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
    at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:397)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:573)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:776)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:152)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1439)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1358)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:307)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:373)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:358)
    at com.mycompany.extract.Program.main(Program.java:62)

Any ideas, or suggestions? The program simply doesn't continue after this error, I don't care if the certificate is bad (it is a valid cert, just using a different subdomain) as well, the certificate is out of my control.


Solution

  • The problem is that you need the valid cert for that submdomain. Get it, and add it to your jssecacerts in the version of Java you are using.

    I did this using InstallCert.java. There is some helpful documentation here: http://miteff.com/install-cert and possibly here: http://pinchii.com/home/tag/jssecacerts/

    A simple google search for "InstallCert.java" should give you plenty of results.

    Good Luck!