001/* 002 * $HeadURL: file:///opt/dev/not-yet-commons-ssl-SVN-repo/tags/commons-ssl-0.3.17/src/java/org/apache/commons/ssl/SSLEchoServer.java $ 003 * $Revision: 180 $ 004 * $Date: 2014-09-23 11:33:47 -0700 (Tue, 23 Sep 2014) $ 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 068 SSLServerSocket ss = (SSLServerSocket) ssl.createServerSocket(port, 3); 069 System.out.println("SSL Echo server listening on port: " + port); 070 while (true) { 071 SSLSocket s = (SSLSocket) ss.accept(); 072 s.setSoTimeout(30000); 073 EchoRunnable r = new EchoRunnable(s); 074 new Thread(r).start(); 075 } 076 077 } 078 079 public static class EchoRunnable implements Runnable { 080 private SSLSocket s; 081 082 public EchoRunnable(SSLSocket s) { 083 this.s = s; 084 } 085 086 public void run() { 087 InputStream in = null; 088 OutputStream out = null; 089 System.out.println("Socket accepted!"); 090 try { 091 SSLSession session = s.getSession(); 092 093 try { 094 Certificate[] certs = JavaImpl.getPeerCertificates(session); 095 if (certs != null) { 096 for (int i = 0; i < certs.length; i++) { 097 // log client cert info 098 X509Certificate cert = (X509Certificate) certs[i]; 099 String s = "client cert " + i + ":"; 100 s += JavaImpl.getSubjectX500(cert); 101 System.out.println(s); 102 System.out.println(Certificates.toString(cert)); 103 } 104 } 105 } 106 catch (SSLPeerUnverifiedException sslpue) { 107 // oh well, no client cert for us 108 System.out.println(sslpue); 109 } 110 111 in = s.getInputStream(); 112 out = s.getOutputStream(); 113 ReadLine readLine = new ReadLine(in); 114 String line = readLine.next(); 115 if (line != null && line.indexOf("HTTP") > 0) { 116 out.write("HTTP/1.1 200 OK\r\n\r\n".getBytes()); 117 out.flush(); 118 } 119 while (line != null) { 120 String echo = "ECHO:>" + line + "\n"; 121 out.write(echo.getBytes()); 122 out.flush(); 123 line = readLine.next(); 124 } 125 } 126 catch (IOException ioe) { 127 try { 128 if (out != null) { 129 out.close(); 130 } 131 if (in != null) { 132 in.close(); 133 } 134 s.close(); 135 } 136 catch (Exception e) { 137 } 138 139 if (ioe instanceof InterruptedIOException) { 140 System.out.println("Socket closed after 30 second timeout."); 141 } else { 142 ioe.printStackTrace(); 143 } 144 145 } 146 } 147 } 148 149}