X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fping%2FSSLPinger.java;h=d6ebe71c2a3b3f3dd60f8463f82a6f51097202fe;hb=923c2987f262d77a2c7ec4d3558de77320750e77;hp=78adc12ac9e6a76bc4d08ec34675c77445ff25b9;hpb=2824d1c165c501e2f3a8809044788b33b81f478a;p=gigi.git diff --git a/src/org/cacert/gigi/ping/SSLPinger.java b/src/org/cacert/gigi/ping/SSLPinger.java index 78adc12a..d6ebe71c 100644 --- a/src/org/cacert/gigi/ping/SSLPinger.java +++ b/src/org/cacert/gigi/ping/SSLPinger.java @@ -22,173 +22,171 @@ import javax.security.cert.X509Certificate; public class SSLPinger extends DomainPinger { - @Override - public void ping(String domain, String configuration, String expToken) { - try { - SocketChannel sch = SocketChannel.open(); - String[] parts = configuration.split(":", 2); - sch.connect(new InetSocketAddress(domain, Integer.parseInt(parts[0]))); - if (parts.length == 2) { - switch (parts[1]) { - case "xmpp": - startXMPP(sch, false, domain); - break; - case "server-xmpp": - startXMPP(sch, true, domain); - break; - case "smtp": - startSMTP(sch); - break; - case "imap": - startIMAP(sch); - break; + @Override + public void ping(String domain, String configuration, String expToken) { + try { + SocketChannel sch = SocketChannel.open(); + String[] parts = configuration.split(":", 2); + sch.connect(new InetSocketAddress(domain, Integer.parseInt(parts[0]))); + if (parts.length == 2) { + switch (parts[1]) { + case "xmpp": + startXMPP(sch, false, domain); + break; + case "server-xmpp": + startXMPP(sch, true, domain); + break; + case "smtp": + startSMTP(sch); + break; + case "imap": + startIMAP(sch); + break; - } - } - test(sch, domain); - } catch (IOException e) { - e.printStackTrace(); - } + } + } + test(sch, domain); + } catch (IOException e) { + e.printStackTrace(); + } - } + } - private void startIMAP(SocketChannel sch) throws IOException { - Socket s = sch.socket(); - InputStream is = s.getInputStream(); - OutputStream os = s.getOutputStream(); - scanFor(is, "\n"); - os.write("ENABLE STARTTLS\r\n".getBytes()); - os.flush(); - scanFor(is, "\n"); - } + private void startIMAP(SocketChannel sch) throws IOException { + Socket s = sch.socket(); + InputStream is = s.getInputStream(); + OutputStream os = s.getOutputStream(); + scanFor(is, "\n"); + os.write("ENABLE STARTTLS\r\n".getBytes()); + os.flush(); + scanFor(is, "\n"); + } - private void startXMPP(SocketChannel sch, boolean server, String domain) throws IOException { - Socket s = sch.socket(); - InputStream is = s.getInputStream(); - OutputStream os = s.getOutputStream(); - os.write(("") - .getBytes()); - os.flush(); - os.write("".getBytes()); - os.flush(); - scanFor(is, ""); + private void startXMPP(SocketChannel sch, boolean server, String domain) throws IOException { + Socket s = sch.socket(); + InputStream is = s.getInputStream(); + OutputStream os = s.getOutputStream(); + os.write(("").getBytes()); + os.flush(); + os.write("".getBytes()); + os.flush(); + scanFor(is, ""); - } + } - private void scanFor(InputStream is, String scanFor) throws IOException { - int pos = 0; - while (pos < scanFor.length()) { - if (is.read() == scanFor.charAt(pos)) { - pos++; - } else { - pos = 0; - } - } - } + private void scanFor(InputStream is, String scanFor) throws IOException { + int pos = 0; + while (pos < scanFor.length()) { + if (is.read() == scanFor.charAt(pos)) { + pos++; + } else { + pos = 0; + } + } + } - private void startSMTP(SocketChannel sch) throws IOException { - Socket s = sch.socket(); - InputStream is = s.getInputStream(); - readSMTP(is); - s.getOutputStream().write("EHLO ssl.pinger\r\n".getBytes()); - s.getOutputStream().flush(); - readSMTP(is); - s.getOutputStream().write("HELP\r\n".getBytes()); - s.getOutputStream().flush(); - readSMTP(is); - s.getOutputStream().write("STARTTLS\r\n".getBytes()); - s.getOutputStream().flush(); - readSMTP(is); - } + private void startSMTP(SocketChannel sch) throws IOException { + Socket s = sch.socket(); + InputStream is = s.getInputStream(); + readSMTP(is); + s.getOutputStream().write("EHLO ssl.pinger\r\n".getBytes()); + s.getOutputStream().flush(); + readSMTP(is); + s.getOutputStream().write("HELP\r\n".getBytes()); + s.getOutputStream().flush(); + readSMTP(is); + s.getOutputStream().write("STARTTLS\r\n".getBytes()); + s.getOutputStream().flush(); + readSMTP(is); + } - private void readSMTP(InputStream is) throws IOException { - int counter = 0; - boolean finish = true; - while (true) { - char c = (char) is.read(); - if (counter == 3) { - if (c == ' ') { - finish = true; - } else if (c == '-') { - finish = false; - } else { - throw new Error("Invalid smtp: " + c); - } - } - if (c == '\n') { - if (finish) { - return; - } - counter = 0; - } else { - counter++; - } - } - } + private void readSMTP(InputStream is) throws IOException { + int counter = 0; + boolean finish = true; + while (true) { + char c = (char) is.read(); + if (counter == 3) { + if (c == ' ') { + finish = true; + } else if (c == '-') { + finish = false; + } else { + throw new Error("Invalid smtp: " + c); + } + } + if (c == '\n') { + if (finish) { + return; + } + counter = 0; + } else { + counter++; + } + } + } - private void test(SocketChannel sch, String domain) { - try { - SSLContext sc = SSLContext.getDefault(); - SSLEngine se = sc.createSSLEngine(); - ByteBuffer enc_in = ByteBuffer.allocate(se.getSession().getPacketBufferSize()); - ByteBuffer enc_out = ByteBuffer.allocate(se.getSession().getPacketBufferSize()); - ByteBuffer dec_in = ByteBuffer.allocate(se.getSession().getApplicationBufferSize()); - ByteBuffer dec_out = ByteBuffer.allocate(se.getSession().getApplicationBufferSize()); - se.setUseClientMode(true); - SSLParameters sp = se.getSSLParameters(); - sp.setServerNames(Arrays. asList(new SNIHostName(domain))); - se.setSSLParameters(sp); - se.beginHandshake(); - enc_in.limit(0); - while (se.getHandshakeStatus() != HandshakeStatus.FINISHED - && se.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING) { - switch (se.getHandshakeStatus()) { - case NEED_WRAP: - dec_out.limit(0); - se.wrap(dec_out, enc_out); - enc_out.flip(); - while (enc_out.remaining() > 0) { - sch.write(enc_out); - } - enc_out.clear(); - break; - case NEED_UNWRAP: - if (enc_in.remaining() == 0) { - enc_in.clear(); - sch.read(enc_in); - enc_in.flip(); - } - while (se.unwrap(enc_in, dec_in).getStatus() == Status.BUFFER_UNDERFLOW) { - enc_in.position(enc_in.limit()); - enc_in.limit(enc_in.capacity()); - sch.read(enc_in); - enc_in.flip(); - } - enc_in.compact(); - enc_in.flip(); - break; - case NEED_TASK: - se.getDelegatedTask().run(); - break; - case NOT_HANDSHAKING: - case FINISHED: + private void test(SocketChannel sch, String domain) { + try { + SSLContext sc = SSLContext.getDefault(); + SSLEngine se = sc.createSSLEngine(); + ByteBuffer enc_in = ByteBuffer.allocate(se.getSession().getPacketBufferSize()); + ByteBuffer enc_out = ByteBuffer.allocate(se.getSession().getPacketBufferSize()); + ByteBuffer dec_in = ByteBuffer.allocate(se.getSession().getApplicationBufferSize()); + ByteBuffer dec_out = ByteBuffer.allocate(se.getSession().getApplicationBufferSize()); + se.setUseClientMode(true); + SSLParameters sp = se.getSSLParameters(); + sp.setServerNames(Arrays.asList(new SNIHostName(domain))); + se.setSSLParameters(sp); + se.beginHandshake(); + enc_in.limit(0); + while (se.getHandshakeStatus() != HandshakeStatus.FINISHED && se.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING) { + switch (se.getHandshakeStatus()) { + case NEED_WRAP: + dec_out.limit(0); + se.wrap(dec_out, enc_out); + enc_out.flip(); + while (enc_out.remaining() > 0) { + sch.write(enc_out); + } + enc_out.clear(); + break; + case NEED_UNWRAP: + if (enc_in.remaining() == 0) { + enc_in.clear(); + sch.read(enc_in); + enc_in.flip(); + } + while (se.unwrap(enc_in, dec_in).getStatus() == Status.BUFFER_UNDERFLOW) { + enc_in.position(enc_in.limit()); + enc_in.limit(enc_in.capacity()); + sch.read(enc_in); + enc_in.flip(); + } + enc_in.compact(); + enc_in.flip(); + break; + case NEED_TASK: + se.getDelegatedTask().run(); + break; + case NOT_HANDSHAKING: + case FINISHED: - } + } - } - System.out.println("completed"); - System.out.println(se.getSession().getCipherSuite()); - X509Certificate[] peerCertificateChain = se.getSession().getPeerCertificateChain(); - for (X509Certificate x509Certificate : peerCertificateChain) { - System.out.println(x509Certificate.getSubjectDN().getName()); - } - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (SSLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } + } + System.out.println("completed"); + System.out.println(se.getSession().getCipherSuite()); + X509Certificate[] peerCertificateChain = se.getSession().getPeerCertificateChain(); + for (X509Certificate x509Certificate : peerCertificateChain) { + System.out.println(x509Certificate.getSubjectDN().getName()); + } + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (SSLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } }