HTTPS Zertifikat

Knut

Mitglied
Hallo

Ich würde gerne den Quellcode einer HTTPS Seite auslesen.
Nun benötige aber ein gültiges Zertifikat um eine verschlüsselte Verbindung aufzubauen.
Hab z.B. hier gelesen: keytool-Key and Certificate Management Tool
dass man mit diesem keytool ein Zertifikat erstellen kann.

Jetzt scheint es aber so zu sein, dass das Zertifikat zeitlich begrenzt ist.
Bzw wenn das Programm woanders ausgeführt wird, dann fehlt ja auch dort das Zertifikat.

Gibt es eine Möglichkeit ein Zertifikat automatisch erstellen zu lassen wenn keines da ist?
Also so wie es im Browser realisiert ist, mit einem einfachen Dialog bestätigen?

Warum muss man beim "keytool" ein PW angeben, im Browser aber nicht?

Danke für Eure Mühe

lg Lukas
 
Zuletzt bearbeitet von einem Moderator:
Wieso solltest DU als CLIENT ein Zertifikat brauchen?

Das Zertifikat besitzt der Server und weißt es dir vor. Nun musst du es als vertrauenswürdig akzeptieren.
 
hmm ok danke,dann hab ich das wohl falsch verstanden.

Wie kann ich das Zertifikat akzeptieren?

Kennt vielleicht jemand ein Beispiel Programm?

Java:
import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL oracle = new URL("http://www.oracle.com/");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

Wenn ich nun meinen HTTPS Server eintrage ->
"javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"

Danke für Eure Mühe

lg Lukas
 
Zuletzt bearbeitet:
Danke, ich denke ich hab nun eine Lösung gefunden:

Palaniappan's blog: HttpsUrlConnection Example Code
Java:
/**
* JavaHttpsUrlConnectionReader
*
*
*@author Palaniappan S
*/
import java.io.*;
import java.net.*;
import java.security.cert.X509Certificate;
import java.util.Properties;
import javax.net.ssl.*;


public class JavaHttpsUrlConnectionReader implements javax.net.ssl.X509TrustManager {

public static void main(String[] args) throws Exception {

String output = new JavaHttpsUrlConnectionReader().doHttpsUrlConnectionAction("https://somesecuresite.com");
System.out.println("Output : "+output);

}

public String doHttpsUrlConnectionAction(String desiredUrl) throws Exception {

URL url;
int responseCode=0;
StringBuffer responseData = new StringBuffer( );
//Uncomment below lines of code if any proxy enabled in the network you are using
//Properties props = System.getProperties();
//props.put("https.proxyHost", "127.0.0.1");
//props.put("https.proxyPort", "8080");

// ###########
SSLContext sc = SSLContext.getInstance("SSLv3");
TrustManager[] tma = { new JavaHttpsUrlConnectionReader() };
sc.init(null, tma, null);
SSLSocketFactory ssf = sc.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(ssf);
// ###########
HttpsURLConnection connection = null;

String nurl = desiredUrl;
HostnameVerifier hv = new HostnameVerifier()
{
public boolean verify(String urlHostName, SSLSession session)
{
System.out.println("Warning: URL Host: " + urlHostName + " vs. "
+ session.getPeerHost());
return true;
}
};
// Now you are telling the JRE to trust any https server.
// If you know the URL that you are connecting to then this should not be a problem
// trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);

try {
url = new URL(nurl);
connection = (HttpsURLConnection) url.openConnection();
System.out.println("Conn established "+connection);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
responseCode = connection.getResponseCode();
System.out.println("response code : "+responseCode);
connection.connect();
} catch(Exception e) {
System.err.println(e);
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String _ResData = null;
while ((_ResData = reader.readLine()) != null)
{
responseData.append(_ResData);
//_ResData = sckStream.readLine();
}
System.out.println("Received Data: "+ responseData.toString());

} catch (Exception e) {
System.err.println(e);
}
finally
{
// close the reader; this can throw an exception too, so
// wrap it in another try/catch block.
if (reader != null)
{
try
{
connection.disconnect();
reader.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
return responseData.toString();
}

// TrustManager Methods
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}

public void checkServerTrusted(X509Certificate[] chain, String authType) {
}

public X509Certificate[] getAcceptedIssuers() {
return null;
}

}

lg Lukas
 

Zurück
Oben