SSLServer.java.sic
上传用户:huihesys
上传日期:2007-01-04
资源大小:3877k
文件大小:7k
- /* $Id: SSLServer.java.sic,v 1.1.1.1 2000/02/01 12:01:37 wastl Exp $ */
- package webmail.server.http;
- import java.io.*;
- import java.net.*;
- import java.util.*;
- import java.math.BigInteger;
- import java.security.*;
- import iaik.security.ssl.*;
- import iaik.security.rsa.*;
- import iaik.x509.*;
- import iaik.asn1.structures.Name;
- import iaik.asn1.ObjectID;
- import iaik.security.provider.IAIK;
- import webmail.config.Server;
- import webmail.config.ConfigurationListener;
- import webmail.server.*;
- import webmail.ui.html.*;
- import webmail.debug.ErrorHandler;
- /**
- * HTTPServer.java
- *
- *
- * Created: Tue Feb 2 12:15:48 1999
- *
- * @author Sebastian Schaffert
- * @version $Revision: 1.1.1.1 $
- */
- public class SSLServer extends HTTPServer implements ConfigurationListener {
- private class WebMailRSAPrivateKey extends RSAPrivateKey {
- WebMailRSAPrivateKey(String s) throws java.security.InvalidKeyException, IOException {
- super(s);
- }
- public BigInteger getPrivateExponent() {
- return getExponent();
- }
- }
- private boolean shutdown=false;
- private int port;
- private ConnectionTimer timer;
- private SSLServerSocket socket;
- private SSLContext serverContext;
- private WebMailServer parent;
- private Hashtable html_loaders;
- private long start_time;
- private long nr_connections=0;
- public SSLServer(WebMailServer parent) {
- super();
-
- System.err.print("- IAIK SSL Server ...");
- parent.getConfigScheme().configRegisterIntegerKey(this,"SSL PORT","6790","Port where the SSL server will accept connections");
- parent.getConfigScheme().configRegisterStringKey(this,"SSL CERTS","../data/ssl/certs/","Path to SSL certificates");
- parent.getConfigScheme().configRegisterStringKey(this,"SSL ADDRESS","0.0.0.0","Address for the SSL Server to listen on (default: all addresses)");
- parent.getConfigScheme().configRegisterIntegerKey(this,"SSL BACKLOG","50","SSL Server Socket Backlog (how many connections to hold in Queue)");
- parent.getConfigScheme().configRegisterIntegerKey(this,"SSL CONNECTION LIMIT","50","Maximum number of simultaneous connections (reduce to avoid server crash on machines with low memory, 20 should be ok for medium-high load machines)");
- parent.getConfigScheme().configRegisterYesNoKey(this,"SSL ENABLE","Enable the SSL server.");
- this.timer=parent.getConnectionTimer();
- this.parent=parent;
- int port=6790;
- try {
- port=Integer.parseInt(parent.getStorage().getConfig("SSL PORT"));
- } catch(NumberFormatException e) {
- }
- int max_connections=50;
- current_connections=0;
- try {
- max_connections=Integer.parseInt(parent.getStorage().getConfig("SSL CONNECTION LIMIT"));
- } catch(NumberFormatException e) {
- }
- int backlog=50;
- try {
- backlog=Integer.parseInt(parent.getStorage().getConfig("SSL BACKLOG"));
- } catch(NumberFormatException e) {
- }
- if(parent.getStorage().getConfig("SSL ENABLE").toUpperCase().equals("YES")) {
- IAIK provider = new IAIK();
- Security.addProvider(provider);
-
- SSLSocket ssl = null;
- SSLContext serverContext = new SSLContext();
-
- X509Certificate[] certList = new X509Certificate[2];
- RSAPrivateKey privateKey = null;
-
- KeyPair tempKeyPair = null;
-
- try {
- certList[0] = new X509Certificate(parent.getConfig("SSL CERTS")+"/serverCert1024.der");
- certList[1] = new X509Certificate(parent.getConfig("SSL CERTS")+"/caCert1024.der");
- privateKey = new WebMailRSAPrivateKey(parent.getConfig("SSL CERTS")+"/serverPrivateKey.der");
-
- RSAPrivateKey tsk = new WebMailRSAPrivateKey(parent.getConfig("SSL CERTS")+"/tempPrivateKey.der");
- PublicKey tpk = tsk.getPublicKey();
- tempKeyPair = new KeyPair(tpk, tsk);
-
-
- serverContext.setCertificate(certList, privateKey);
-
- serverContext.setTempKeyPair(tempKeyPair);
-
- Name[] cas = new Name[1];
- cas[0] = new Name();
- cas[0].addRDN(ObjectID.country, Locale.getDefault().getCountry().toUpperCase());
-
- // DefaultTrustDecider dtd = new DefaultTrustDecider(cas);
- // serverContext.setTrustDecider(dtd);
-
- CipherSuite[] cs = new CipherSuite[8];
- cs[0] = CipherSuite.SSL_RSA_WITH_3DES_EDE_CBC_SHA; // Netscape-Enterprise/2.0a
- cs[1] = CipherSuite.SSL_RSA_WITH_IDEA_CBC_SHA;
- cs[2] = CipherSuite.SSL_RSA_WITH_DES_CBC_SHA; // Netscape-Enterprise/2.0a
- cs[3] = CipherSuite.SSL_RSA_WITH_RC4_MD5; // Netscape-Enterprise/2.0a
- cs[4] = CipherSuite.SSL_RSA_WITH_RC4_SHA;
- cs[5] = CipherSuite.SSL_RSA_EXPORT_WITH_RC4_40_MD5; // Netscape-Enterprise/2.0a
- cs[6] = CipherSuite.SSL_RSA_EXPORT_WITH_DES40_CBC_SHA;
- cs[7] = CipherSuite.SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5;
- // enable this 8 cipher suites
- serverContext.setEnabledCipherSuites(cs);
-
- socket = new SSLServerSocket(port, backlog,
- InetAddress.getByName(parent.getStorage().getConfig("SSL ADDRESS")),
- serverContext);
-
- this.start();
- System.err.println(" initialization complete. Listening on port "+port+".");
- start_time=System.currentTimeMillis();
-
- } catch(Exception ex) {
- //new ErrorHandler(ex);
- System.err.println(" initialization failed! ("+ex.getMessage()+")");
- }
- } else {
- System.err.println(" SSL disabled in configuration. Server not started.");
- }
- }
-
- public void shutdown() {
- shutdown=true;
- System.err.print("- SSL Server shutdown requested ...");
- try {
- socket.close();
- } catch(Exception e) {}
- try {
- // Give connections enough time to terminate
- sleep(2000);
- } catch(InterruptedException ex) {}
- //this.stop();
- System.err.println("complete!");
- }
- public String getStatus() {
- String status;
- if(isAlive()) {
- status="SSL Server listening on "+socket.getInetAddress()+", Port "+port;
- long up=System.currentTimeMillis()-start_time;
- status+="nUptime: "+up/1000+" secondsn";
- status+="Number of connections so far: "+nr_connections+", average "+ (nr_connections*60000/up) +" conn/minn";
- status+="There are currently "+current_connections+" connections out of a maximum of "+max_connections+".n";
- } else {
- status="SSL Server disabled.";
- }
- return status;
- }
- public void notifyConfigurationChange(String key) {
- parent.reinitSSLServer();
- }
- public void run() {
- while(!shutdown) {
- /* Enter critical resource. Connections must release this on termination! */
- if(current_connections < max_connections) {
-
- try {
- if(Runtime.getRuntime().freeMemory() > required_free_memory) {
- SSLSocket client=(SSLSocket)socket.accept();
- //client.setDebugStream(System.out);
- //System.err.println(client.toString());
- Connection conn=new Connection(client,parent,this);
- nr_connections++;
- } else {
- parent.getStorage().log(Storage.LOG_ERR,"Error: Ran out of memory. Garbage collecting. You might want to increase the minimum memory size in webmail.sh to avoid this.");
- System.gc();
- }
- } catch(IOException ex) {
- if(!ex.getMessage().equals("Socket closed")) {
- new ErrorHandler(ex);
- }
- }
- } else {
- parent.getStorage().log(Storage.LOG_DEBUG,"SSL Server: Maximum number of SSL connections reached. You might want to increase the SSL CONNECTION LIMIT if your server is fast enough");
- try {
- synchronized(this) {
- wait();
- }
- } catch(InterruptedException ex) {}
- }
- }
- }
- } // Server