1 package org.cacert.gigi.ping;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.net.InetSocketAddress;
7 import java.net.Socket;
8 import java.nio.ByteBuffer;
9 import java.nio.channels.SocketChannel;
10 import java.security.NoSuchAlgorithmException;
11 import java.util.Arrays;
13 import javax.net.ssl.SNIHostName;
14 import javax.net.ssl.SNIServerName;
15 import javax.net.ssl.SSLContext;
16 import javax.net.ssl.SSLEngine;
17 import javax.net.ssl.SSLEngineResult.Status;
18 import javax.net.ssl.SSLException;
19 import javax.net.ssl.SSLEngineResult.HandshakeStatus;
20 import javax.net.ssl.SSLParameters;
21 import javax.security.cert.X509Certificate;
23 public class SSLPinger extends DomainPinger {
26 public void ping(String domain, String configuration, String expToken) {
28 SocketChannel sch = SocketChannel.open();
29 String[] parts = configuration.split(":", 2);
30 sch.connect(new InetSocketAddress(domain, Integer
31 .parseInt(parts[0])));
32 if (parts.length == 2) {
35 startXMPP(sch, false, domain);
38 startXMPP(sch, true, domain);
50 } catch (IOException e) {
55 private void startIMAP(SocketChannel sch) throws IOException {
56 Socket s = sch.socket();
57 InputStream is = s.getInputStream();
58 OutputStream os = s.getOutputStream();
60 os.write("ENABLE STARTTLS\r\n".getBytes());
64 private void startXMPP(SocketChannel sch, boolean server, String domain)
66 Socket s = sch.socket();
67 InputStream is = s.getInputStream();
68 OutputStream os = s.getOutputStream();
69 os.write(("<stream:stream to=\"" + domain + "\" xmlns=\"jabber:"
70 + (server ? "server" : "client") + "\"" + " xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\">")
73 os.write("<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>"
76 scanFor(is, "<proceed");
80 private void scanFor(InputStream is, String scanFor) throws IOException {
82 while (pos < scanFor.length()) {
83 if (is.read() == scanFor.charAt(pos)) {
90 private void startSMTP(SocketChannel sch) throws IOException {
91 Socket s = sch.socket();
92 InputStream is = s.getInputStream();
94 s.getOutputStream().write("EHLO ssl.pinger\r\n".getBytes());
95 s.getOutputStream().flush();
97 s.getOutputStream().write("HELP\r\n".getBytes());
98 s.getOutputStream().flush();
100 s.getOutputStream().write("STARTTLS\r\n".getBytes());
101 s.getOutputStream().flush();
104 private void readSMTP(InputStream is) throws IOException {
106 boolean finish = true;
108 char c = (char) is.read();
112 } else if (c == '-') {
115 throw new Error("Invalid smtp: " + c);
128 private void test(SocketChannel sch, String domain) {
130 SSLContext sc = SSLContext.getDefault();
131 SSLEngine se = sc.createSSLEngine();
132 ByteBuffer enc_in = ByteBuffer.allocate(se.getSession()
133 .getPacketBufferSize());
134 ByteBuffer enc_out = ByteBuffer.allocate(se.getSession()
135 .getPacketBufferSize());
136 ByteBuffer dec_in = ByteBuffer.allocate(se.getSession()
137 .getApplicationBufferSize());
138 ByteBuffer dec_out = ByteBuffer.allocate(se.getSession()
139 .getApplicationBufferSize());
140 se.setUseClientMode(true);
141 SSLParameters sp = se.getSSLParameters();
142 sp.setServerNames(Arrays.<SNIServerName> asList(new SNIHostName(
144 se.setSSLParameters(sp);
147 while (se.getHandshakeStatus() != HandshakeStatus.FINISHED
148 && se.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING) {
149 switch (se.getHandshakeStatus()) {
152 se.wrap(dec_out, enc_out);
154 while (enc_out.remaining() > 0) {
160 if (enc_in.remaining() == 0) {
165 while (se.unwrap(enc_in, dec_in).getStatus() == Status.BUFFER_UNDERFLOW) {
166 enc_in.position(enc_in.limit());
167 enc_in.limit(enc_in.capacity());
175 se.getDelegatedTask().run();
177 case NOT_HANDSHAKING :
183 System.out.println("completed");
184 System.out.println(se.getSession().getCipherSuite());
185 X509Certificate[] peerCertificateChain = se.getSession()
186 .getPeerCertificateChain();
187 for (X509Certificate x509Certificate : peerCertificateChain) {
188 System.out.println(x509Certificate.getSubjectDN().getName());
190 } catch (NoSuchAlgorithmException e) {
192 } catch (SSLException e) {
194 } catch (IOException e) {