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.parseInt(parts[0])));
31 if (parts.length == 2) {
34 startXMPP(sch, false, domain);
37 startXMPP(sch, true, domain);
49 } 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());
65 private void startXMPP(SocketChannel sch, boolean server, String domain) throws IOException {
66 Socket s = sch.socket();
67 InputStream is = s.getInputStream();
68 OutputStream os = s.getOutputStream();
69 os.write(("<stream:stream to=\"" + domain + "\" xmlns=\"jabber:" + (server ? "server" : "client") + "\"" + " xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\">").getBytes());
71 os.write("<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>".getBytes());
73 scanFor(is, "<proceed");
78 private void scanFor(InputStream is, String scanFor) throws IOException {
80 while (pos < scanFor.length()) {
81 if (is.read() == scanFor.charAt(pos)) {
89 private void startSMTP(SocketChannel sch) throws IOException {
90 Socket s = sch.socket();
91 InputStream is = s.getInputStream();
93 s.getOutputStream().write("EHLO ssl.pinger\r\n".getBytes());
94 s.getOutputStream().flush();
96 s.getOutputStream().write("HELP\r\n".getBytes());
97 s.getOutputStream().flush();
99 s.getOutputStream().write("STARTTLS\r\n".getBytes());
100 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);
129 private void test(SocketChannel sch, String domain) {
131 SSLContext sc = SSLContext.getDefault();
132 SSLEngine se = sc.createSSLEngine();
133 ByteBuffer enc_in = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
134 ByteBuffer enc_out = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
135 ByteBuffer dec_in = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
136 ByteBuffer dec_out = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
137 se.setUseClientMode(true);
138 SSLParameters sp = se.getSSLParameters();
139 sp.setServerNames(Arrays.<SNIServerName>asList(new SNIHostName(domain)));
140 se.setSSLParameters(sp);
143 while (se.getHandshakeStatus() != HandshakeStatus.FINISHED && se.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING) {
144 switch (se.getHandshakeStatus()) {
147 se.wrap(dec_out, enc_out);
149 while (enc_out.remaining() > 0) {
155 if (enc_in.remaining() == 0) {
160 while (se.unwrap(enc_in, dec_in).getStatus() == Status.BUFFER_UNDERFLOW) {
161 enc_in.position(enc_in.limit());
162 enc_in.limit(enc_in.capacity());
170 se.getDelegatedTask().run();
172 case NOT_HANDSHAKING:
178 System.out.println("completed");
179 System.out.println(se.getSession().getCipherSuite());
180 X509Certificate[] peerCertificateChain = se.getSession().getPeerCertificateChain();
181 for (X509Certificate x509Certificate : peerCertificateChain) {
182 System.out.println(x509Certificate.getSubjectDN().getName());
184 } catch (NoSuchAlgorithmException e) {
186 } catch (SSLException e) {
188 } catch (IOException e) {