Java by API/javax.net.ssl/SSLSocketFactory — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:33, 31 мая 2010
ServerSocketFactory: createServerSocket(int arg0)
import java.io.IOException;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
public class MainClass {
public static void main(String[] args) {
int port = Integer.parseInt(args[0]);
try {
System.out.println("Locating server socket factory for SSL...");
SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
System.out.println("Creating a server socket on port " + port);
SSLServerSocket serverSocket = (SSLServerSocket) factory.createServerSocket(port);
String[] suites = serverSocket.getSupportedCipherSuites();
System.out.println("Support cipher suites are:");
for (int i = 0; i < suites.length; i++) {
System.out.println(suites[i]);
}
serverSocket.setEnabledCipherSuites(suites);
System.out.println("Support protocols are:");
String[] protocols = serverSocket.getSupportedProtocols();
for (int i = 0; i < protocols.length; i++) {
System.out.println(protocols[i]);
}
System.out.println("Waiting for client...");
SSLSocket socket = (SSLSocket) serverSocket.accept();
System.out.println("Starting handshake...");
socket.startHandshake();
System.out.println("Just connected to " + socket.getRemoteSocketAddress());
} catch (IOException e) {
e.printStackTrace();
}
}
}
SSLSocketFactory: createSocket(String arg0, int arg1)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.security.Principal;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class MainClass {
public static void main(String[] args) throws Exception {
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
String hostName = "hostName";
String fileName = "fileName";
SSLSocket sslsock = (SSLSocket) factory.createSocket(hostName, 443);
SSLSession session = sslsock.getSession();
X509Certificate cert;
try {
cert = (X509Certificate) session.getPeerCertificates()[0];
} catch (SSLPeerUnverifiedException e) {
System.err.println(session.getPeerHost() + " did not present a valid certificate.");
return;
}
System.out.println(session.getPeerHost() + " has presented a certificate belonging to:");
Principal p = cert.getSubjectDN();
System.out.println("\t[" + p.getName() + "]");
System.out.println("The certificate bears the valid signature of:");
System.out.println("\t[" + cert.getIssuerDN().getName() + "]");
System.out.print("Do you trust this certificate (y/n)? ");
System.out.flush();
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
if (Character.toLowerCase(console.readLine().charAt(0)) != "y")
return;
PrintWriter out = new PrintWriter(sslsock.getOutputStream());
out.print("GET " + fileName + " HTTP/1.0\r\n\r\n");
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(sslsock.getInputStream()));
String line;
while ((line = in.readLine()) != null)
System.out.println(line);
sslsock.close();
}
}
SSLSocketFactory: getDefault()
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.security.Principal;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class MainClass {
public static void main(String[] args) throws Exception {
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
String hostName = "hostName";
String fileName = "fileName";
SSLSocket sslsock = (SSLSocket) factory.createSocket(hostName, 443);
SSLSession session = sslsock.getSession();
X509Certificate cert;
try {
cert = (X509Certificate) session.getPeerCertificates()[0];
} catch (SSLPeerUnverifiedException e) {
System.err.println(session.getPeerHost() + " did not present a valid certificate.");
return;
}
System.out.println(session.getPeerHost() + " has presented a certificate belonging to:");
Principal p = cert.getSubjectDN();
System.out.println("\t[" + p.getName() + "]");
System.out.println("The certificate bears the valid signature of:");
System.out.println("\t[" + cert.getIssuerDN().getName() + "]");
System.out.print("Do you trust this certificate (y/n)? ");
System.out.flush();
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
if (Character.toLowerCase(console.readLine().charAt(0)) != "y")
return;
PrintWriter out = new PrintWriter(sslsock.getOutputStream());
out.print("GET " + fileName + " HTTP/1.0\r\n\r\n");
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(sslsock.getInputStream()));
String line;
while ((line = in.readLine()) != null)
System.out.println(line);
sslsock.close();
}
}