1 package org.cacert.gigi.ping;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.math.BigInteger;
7 import java.net.InetSocketAddress;
8 import java.net.Socket;
9 import java.nio.ByteBuffer;
10 import java.nio.channels.SocketChannel;
11 import java.security.KeyManagementException;
12 import java.security.KeyStore;
13 import java.security.KeyStoreException;
14 import java.security.NoSuchAlgorithmException;
15 import java.security.SecureRandom;
16 import java.util.Arrays;
18 import javax.net.ssl.SNIHostName;
19 import javax.net.ssl.SNIServerName;
20 import javax.net.ssl.SSLContext;
21 import javax.net.ssl.SSLEngine;
22 import javax.net.ssl.TrustManagerFactory;
23 import javax.net.ssl.SSLEngineResult.Status;
24 import javax.net.ssl.SSLException;
25 import javax.net.ssl.SSLEngineResult.HandshakeStatus;
26 import javax.net.ssl.SSLParameters;
27 import javax.security.cert.X509Certificate;
29 import org.cacert.gigi.dbObjects.Certificate;
30 import org.cacert.gigi.dbObjects.Domain;
31 import org.cacert.gigi.dbObjects.User;
33 public class SSLPinger extends DomainPinger {
35 public static final String[] TYPES = new String[] {
36 "xmpp", "server-xmpp", "smtp", "imap"
39 private KeyStore truststore;
41 public SSLPinger(KeyStore truststore) {
42 this.truststore = truststore;
46 public String ping(Domain domain, String configuration, User u) {
47 try (SocketChannel sch = SocketChannel.open()) {
48 String[] parts = configuration.split(":", 2);
49 sch.connect(new InetSocketAddress(domain.getSuffix(), Integer.parseInt(parts[0])));
50 if (parts.length == 2) {
53 startXMPP(sch, false, domain.getSuffix());
56 startXMPP(sch, true, domain.getSuffix());
67 return test(sch, domain.getSuffix(), u);
68 } catch (IOException e) {
69 return "Connecton failed";
74 private void startIMAP(SocketChannel sch) throws IOException {
75 Socket s = sch.socket();
76 InputStream is = s.getInputStream();
77 OutputStream os = s.getOutputStream();
79 os.write("ENABLE STARTTLS\r\n".getBytes());
84 private void startXMPP(SocketChannel sch, boolean server, String domain) throws IOException {
85 Socket s = sch.socket();
86 InputStream is = s.getInputStream();
87 OutputStream os = s.getOutputStream();
88 os.write(("<stream:stream to=\"" + domain + "\" xmlns=\"jabber:" + (server ? "server" : "client") + "\"" + " xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\">").getBytes());
90 os.write("<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>".getBytes());
92 scanFor(is, "<proceed");
97 private void scanFor(InputStream is, String scanFor) throws IOException {
99 while (pos < scanFor.length()) {
100 if (is.read() == scanFor.charAt(pos)) {
108 private void startSMTP(SocketChannel sch) throws IOException {
109 Socket s = sch.socket();
110 InputStream is = s.getInputStream();
112 s.getOutputStream().write("EHLO ssl.pinger\r\n".getBytes());
113 s.getOutputStream().flush();
115 s.getOutputStream().write("HELP\r\n".getBytes());
116 s.getOutputStream().flush();
118 s.getOutputStream().write("STARTTLS\r\n".getBytes());
119 s.getOutputStream().flush();
123 private void readSMTP(InputStream is) throws IOException {
125 boolean finish = true;
127 char c = (char) is.read();
131 } else if (c == '-') {
134 throw new Error("Invalid smtp: " + c);
148 private String test(SocketChannel sch, String domain, User subject) {
150 SSLContext sc = SSLContext.getInstance("SSL");
152 TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
153 tmf.init(truststore);
154 sc.init(null, tmf.getTrustManagers(), new SecureRandom());
155 } catch (KeyManagementException e) {
157 } catch (KeyStoreException e) {
160 SSLEngine se = sc.createSSLEngine();
161 ByteBuffer enc_in = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
162 ByteBuffer enc_out = ByteBuffer.allocate(se.getSession().getPacketBufferSize());
163 ByteBuffer dec_in = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
164 ByteBuffer dec_out = ByteBuffer.allocate(se.getSession().getApplicationBufferSize());
165 se.setUseClientMode(true);
166 SSLParameters sp = se.getSSLParameters();
167 sp.setServerNames(Arrays.<SNIServerName>asList(new SNIHostName(domain)));
168 se.setSSLParameters(sp);
171 while (se.getHandshakeStatus() != HandshakeStatus.FINISHED && se.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING) {
172 switch (se.getHandshakeStatus()) {
175 se.wrap(dec_out, enc_out);
177 while (enc_out.remaining() > 0) {
183 if (enc_in.remaining() == 0) {
188 while (se.unwrap(enc_in, dec_in).getStatus() == Status.BUFFER_UNDERFLOW) {
189 enc_in.position(enc_in.limit());
190 enc_in.limit(enc_in.capacity());
198 se.getDelegatedTask().run();
200 case NOT_HANDSHAKING:
206 X509Certificate[] peerCertificateChain = se.getSession().getPeerCertificateChain();
207 X509Certificate first = peerCertificateChain[0];
209 BigInteger serial = first.getSerialNumber();
210 Certificate c = Certificate.getBySerial(serial.toString(16));
211 if (c.getOwnerId() != subject.getId()) {
212 return "Owner mismatch";
214 return PING_SUCCEDED;
215 } catch (NoSuchAlgorithmException e) {
216 // e.printStackTrace(); TODO log for user debugging?
217 return "Security failed";
218 } catch (SSLException e) {
219 // e.printStackTrace(); TODO log for user debugging?
220 return "Security failed";
221 } catch (IOException e) {
222 // e.printStackTrace(); TODO log for user debugging?
223 return "Connection closed";