Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

non-interactive mode #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 65 additions & 8 deletions src/de/duenndns/ssl/MemorizingTrustManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ public HostnameVerifier wrapHostnameVerifier(final HostnameVerifier defaultVerif
return new MemorizingHostnameVerifier(defaultVerifier);
}

public HostnameVerifier wrapHostnameVerifierNonInteractive(final HostnameVerifier defaultVerifier) {
if (defaultVerifier == null)
throw new IllegalArgumentException("The default verifier may not be null");

return new NonInteractiveMemorizingHostnameVerifier(defaultVerifier);
}

X509TrustManager getTrustManager(KeyStore ks) {
try {
TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
Expand Down Expand Up @@ -382,7 +389,7 @@ private boolean isExpiredException(Throwable e) {
return false;
}

public void checkCertTrusted(X509Certificate[] chain, String authType, boolean isServer)
public void checkCertTrusted(X509Certificate[] chain, String authType, boolean isServer, boolean interactive)
throws CertificateException
{
LOGGER.log(Level.FINE, "checkCertTrusted(" + chain + ", " + authType + ", " + isServer + ")");
Expand Down Expand Up @@ -412,22 +419,26 @@ public void checkCertTrusted(X509Certificate[] chain, String authType, boolean i
else
defaultTrustManager.checkClientTrusted(chain, authType);
} catch (CertificateException e) {
LOGGER.log(Level.FINER, "checkCertTrusted: defaultTrustManager failed", e);
interactCert(chain, authType, e);
e.printStackTrace();
if (interactive) {
interactCert(chain, authType, e);
} else {
throw e;
}
}
}
}

public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException
{
checkCertTrusted(chain, authType, false);
checkCertTrusted(chain, authType, false,true);
}

public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException
{
checkCertTrusted(chain, authType, true);
checkCertTrusted(chain, authType, true,true);
}

public X509Certificate[] getAcceptedIssuers()
Expand Down Expand Up @@ -650,8 +661,7 @@ public MemorizingHostnameVerifier(HostnameVerifier wrapped) {
defaultVerifier = wrapped;
}

@Override
public boolean verify(String hostname, SSLSession session) {
protected boolean verify(String hostname, SSLSession session, boolean interactive) {
LOGGER.log(Level.FINE, "hostname verifier for " + hostname + ", trying default verifier first");
// if the default verifier accepts the hostname, we are done
if (defaultVerifier.verify(hostname, session)) {
Expand All @@ -667,12 +677,59 @@ public boolean verify(String hostname, SSLSession session) {
return true;
} else {
LOGGER.log(Level.FINE, "server " + hostname + " provided wrong certificate, asking user.");
return interactHostname(cert, hostname);
if (interactive) {
return interactHostname(cert, hostname);
} else {
return false;
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

@Override
public boolean verify(String hostname, SSLSession session) {
return verify(hostname, session, true);
}
}

class NonInteractiveMemorizingHostnameVerifier extends MemorizingHostnameVerifier {

public NonInteractiveMemorizingHostnameVerifier(HostnameVerifier wrapped) {
super(wrapped);
}
@Override
public boolean verify(String hostname, SSLSession session) {
return verify(hostname, session, true);
}


}

public X509TrustManager getNonInteractive() {
return new NonInteractiveMemorizingTrustManager();
}

private class NonInteractiveMemorizingTrustManager implements X509TrustManager {

@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
MemorizingTrustManager.this.checkCertTrusted(chain, authType, false, false);
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
MemorizingTrustManager.this.checkCertTrusted(chain, authType, true, false);
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return MemorizingTrustManager.this.getAcceptedIssuers();
}

}
}