001/* 002 * $HeadURL: http://juliusdavies.ca/svn/not-yet-commons-ssl/tags/commons-ssl-0.3.9/src/java/org/apache/commons/ssl/SSLEchoServer.java $ 003 * $Revision: 121 $ 004 * $Date: 2007-11-13 21:26:57 -0800 (Tue, 13 Nov 2007) $ 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 javax.net.ssl.SSLPeerUnverifiedException; 035import javax.net.ssl.SSLServerSocket; 036import javax.net.ssl.SSLSession; 037import javax.net.ssl.SSLSocket; 038import java.io.IOException; 039import java.io.InputStream; 040import java.io.InterruptedIOException; 041import java.io.OutputStream; 042import java.security.cert.Certificate; 043import java.security.cert.X509Certificate; 044 045/** 046 * @author Credit Union Central of British Columbia 047 * @author <a href="http://www.cucbc.com/">www.cucbc.com</a> 048 * @author <a href="mailto:juliusdavies@cucbc.com">juliusdavies@cucbc.com</a> 049 * @since 2-May-2006 050 */ 051public class SSLEchoServer { 052 053 public static void main(String[] args) throws Exception { 054 int port = 7443; 055 if (args.length >= 1) { 056 port = Integer.parseInt(args[0]); 057 } 058 059 SSLServer ssl = new SSLServer(); 060 // ssl.setCheckExpiry( false ); 061 // ssl.setNeedClientAuth( true ); 062 ssl.addTrustMaterial(TrustMaterial.TRUST_ALL); 063 SSLServerSocket ss = (SSLServerSocket) ssl.createServerSocket(port, 3); 064 System.out.println("SSL Echo server listening on port: " + port); 065 while (true) { 066 SSLSocket s = (SSLSocket) ss.accept(); 067 s.setSoTimeout(30000); 068 EchoRunnable r = new EchoRunnable(s); 069 new Thread(r).start(); 070 } 071 072 } 073 074 public static class EchoRunnable implements Runnable { 075 private SSLSocket s; 076 077 public EchoRunnable(SSLSocket s) { 078 this.s = s; 079 } 080 081 public void run() { 082 InputStream in = null; 083 OutputStream out = null; 084 System.out.println("Socket accepted!"); 085 try { 086 SSLSession session = s.getSession(); 087 088 try { 089 Certificate[] certs = JavaImpl.getPeerCertificates(session); 090 if (certs != null) { 091 for (int i = 0; i < certs.length; i++) { 092 // log client cert info 093 X509Certificate cert = (X509Certificate) certs[i]; 094 String s = "client cert " + i + ":"; 095 s += JavaImpl.getSubjectX500(cert); 096 System.out.println(s); 097 System.out.println(Certificates.toString(cert)); 098 } 099 } 100 } 101 catch (SSLPeerUnverifiedException sslpue) { 102 // oh well, no client cert for us 103 } 104 105 in = s.getInputStream(); 106 out = s.getOutputStream(); 107 String line = Util.readLine(in); 108 if (line != null && line.indexOf("HTTP") > 0) { 109 out.write("HTTP/1.1 200 OK\r\n\r\n".getBytes()); 110 out.flush(); 111 } 112 while (line != null) { 113 String echo = "ECHO:>" + line + "\n"; 114 out.write(echo.getBytes()); 115 out.flush(); 116 line = Util.readLine(in); 117 } 118 } 119 catch (IOException ioe) { 120 try { 121 if (out != null) { 122 out.close(); 123 } 124 if (in != null) { 125 in.close(); 126 } 127 s.close(); 128 } 129 catch (Exception e) { 130 } 131 132 if (ioe instanceof InterruptedIOException) { 133 System.out.println("Socket closed after 30 second timeout."); 134 } else { 135 ioe.printStackTrace(); 136 } 137 138 } 139 } 140 } 141 142}