    001/*
    002 * $HeadURL: http://juliusdavies.ca/svn/not-yet-commons-ssl/tags/commons-ssl-0.3.15/src/java/org/apache/commons/ssl/SSLEchoServer.java $
    003 * $Revision: 138 $
    004 * $Date: 2008-03-03 23:50:07 -0800 (Mon, 03 Mar 2008) $
    005 *
    006 * ====================================================================
    007 * Licensed to the Apache Software Foundation (ASF) under one
    008 * or more contributor license agreements.  See the NOTICE file
    009 * distributed with this work for additional information
    010 * regarding copyright ownership.  The ASF licenses this file
    011 * to you under the Apache License, Version 2.0 (the
    012 * "License"); you may not use this file except in compliance
    013 * with the License.  You may obtain a copy of the License at
    014 *
    015 *   http://www.apache.org/licenses/LICENSE-2.0
    016 *
    017 * Unless required by applicable law or agreed to in writing,
    018 * software distributed under the License is distributed on an
    019 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    020 * KIND, either express or implied.  See the License for the
    021 * specific language governing permissions and limitations
    022 * under the License.
    023 * ====================================================================
    024 *
    025 * This software consists of voluntary contributions made by many
    026 * individuals on behalf of the Apache Software Foundation.  For more
    027 * information on the Apache Software Foundation, please see
    028 * <http://www.apache.org/>.
    029 *
    030 */
    031
    032package org.apache.commons.ssl;
    033
    034import org.apache.commons.ssl.util.ReadLine;
    035
    036import javax.net.ssl.SSLPeerUnverifiedException;
    037import javax.net.ssl.SSLServerSocket;
    038import javax.net.ssl.SSLSession;
    039import javax.net.ssl.SSLSocket;
    040import java.io.IOException;
    041import java.io.InputStream;
    042import java.io.InterruptedIOException;
    043import java.io.OutputStream;
    044import java.security.cert.Certificate;
    045import java.security.cert.X509Certificate;
    046
    047/**
    048 * @author Credit Union Central of British Columbia
    049 * @author <a href="http://www.cucbc.com/">www.cucbc.com</a>
    050 * @author <a href="mailto:juliusdavies@cucbc.com">juliusdavies@cucbc.com</a>
    051 * @since 2-May-2006
    052 */
    053public class SSLEchoServer {
    054
    055    public static void main(String[] args) throws Exception {
    056        int port = 7443;
    057        if (args.length >= 1) {
    058            port = Integer.parseInt(args[0]);
    059        }
    060
    061        SSLServer ssl = new SSLServer();
    062        ssl.setTrustMaterial(TrustMaterial.TRUST_ALL);
    063        ssl.setCheckExpiry(false);
    064        ssl.setCheckCRL(false);
    065        ssl.setCheckHostname(false);
    066        ssl.setWantClientAuth(true);
    067        ssl.useDefaultJavaCiphers();
    068
    069        SSLServerSocket ss = (SSLServerSocket) ssl.createServerSocket(port, 3);
    070        System.out.println("SSL Echo server listening on port: " + port);
    071        while (true) {
    072            SSLSocket s = (SSLSocket) ss.accept();
    073            s.setSoTimeout(30000);
    074            EchoRunnable r = new EchoRunnable(s);
    075            new Thread(r).start();
    076        }
    077
    078    }
    079
    080    public static class EchoRunnable implements Runnable {
    081        private SSLSocket s;
    082
    083        public EchoRunnable(SSLSocket s) {
    084            this.s = s;
    085        }
    086
    087        public void run() {
    088            InputStream in = null;
    089            OutputStream out = null;
    090            System.out.println("Socket accepted!");
    091            try {
    092                SSLSession session = s.getSession();
    093
    094                try {
    095                    Certificate[] certs = JavaImpl.getPeerCertificates(session);
    096                    if (certs != null) {
    097                        for (int i = 0; i < certs.length; i++) {
    098                            // log client cert info
    099                            X509Certificate cert = (X509Certificate) certs[i];
    100                            String s = "client cert " + i + ":";
    101                            s += JavaImpl.getSubjectX500(cert);
    102                            System.out.println(s);
    103                            System.out.println(Certificates.toString(cert));
    104                        }
    105                    }
    106                }
    107                catch (SSLPeerUnverifiedException sslpue) {
    108                    // oh well, no client cert for us
    109                    System.out.println(sslpue);
    110                }
    111
    112                in = s.getInputStream();
    113                out = s.getOutputStream();
    114                ReadLine readLine = new ReadLine(in);
    115                String line = readLine.next();
    116                if (line != null && line.indexOf("HTTP") > 0) {
    117                    out.write("HTTP/1.1 200 OK\r\n\r\n".getBytes());
    118                    out.flush();
    119                }
    120                while (line != null) {
    121                    String echo = "ECHO:>" + line + "\n";
    122                    out.write(echo.getBytes());
    123                    out.flush();
    124                    line = readLine.next();
    125                }
    126            }
    127            catch (IOException ioe) {
    128                try {
    129                    if (out != null) {
    130                        out.close();
    131                    }
    132                    if (in != null) {
    133                        in.close();
    134                    }
    135                    s.close();
    136                }
    137                catch (Exception e) {
    138                }
    139
    140                if (ioe instanceof InterruptedIOException) {
    141                    System.out.println("Socket closed after 30 second timeout.");
    142                } else {
    143                    ioe.printStackTrace();
    144                }
    145
    146            }
    147        }
    148    }
    149
    150}