Vale in generale quanto già detto per l'HTTPsServer.

CountServer.java:

import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.*;

import java.security.*;
import java.security.cert.X509Certificate;

import iaik.security.ssl.*;
import iaik.asn1.structures.Name;

import com.entrust.security.provider.*;
import com.entrust.toolkit.*;
import com.entrust.util.*;
import com.entrust.x509.directory.*;

public class CountServer {
static final String NAME = "CountServer";
static final String VERSION = "1.0";
static final int SERVERPORT = 8089;

  public CountServer() {

super();   }

  public void displayVersionInfo(){ System.out.println("CountServer version "+VERSION);   }

  public static void main(String args[]){ CountServer server = new CountServer();
server.run();
  }

  public void run () {

displayVersionInfo();

// Inizializzazione ciphers.
com.entrust.util.Util.initCiphers();

EntrustProfile entrustProfile = new EntrustProfile();

entrustProfile.setPKIXVersion(EntrustProfile.PKIXforEntrust3);

try {

String filename="WebAndrea.epf";
String password="SnorkMuffin";

entrustProfile.logon( new FileInputStream(filename), new StringBuffer(password));

if ( entrustProfile.randomSeedRequired() ) { System.out.println( "The profile " + filename + " requires a valid random seed.\nUse the ProfileRandomSeed program to add a seed to your profile." );
System.exit( 1 );
}
}
catch (Exception e){ e.printStackTrace();
System.out.println("Logon failed.");
}

System.out.println("EntrustProfile done.");

SSLServerContext sslServerContext = new SSLServerContext();

// Si accettano solo questi ciphers forti
CipherSuite [] suites = {

CipherSuite.SSL_RSA_WITH_3DES_EDE_CBC_SHA,
CipherSuite.SSL_RSA_WITH_RC4_SHA,
CipherSuite.SSL_RSA_WITH_RC4_MD5
};

sslServerContext.setEnabledCipherSuites( suites );

try {

System.out.println("Enter the IP address of the directory: ");

String addressDir = stdIn.readLine();

JNDIDirectory directory = new JNDIDirectory( addressDir, 389);
directory.connect();

ETKCertificateVerifier verifier =
new ETKCertificateVerifier( directory, entrustProfile );

sslServerContext.setTrustDecider(verifier);

}
catch (Exception ex) { ex.printStackTrace(); }

System.out.println("ETKCertificateVerifier done.");

try {

// Si genera un coppia di chiavi RSA temporanea.
X509Certificate[] certchain=new X509Certificate[2];
certchain[0]= entrustProfile.getEncryptionCertificate();
certchain[1]= entrustProfile.getCaCertificate();
sslServerContext.setRSACertificate(certchain,entrustProfile.getDecryptionKey());

// Si genera un coppia di chiavi RSA temporanea.
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(512,Util.secureRandom());
sslServerContext.setRSATempKeyPair(kpg.generateKeyPair());
// Si abilitano le suite cipher valide.
CipherSuite[] enabledCS = serverContext.updateCipherSuites();

} catch (Exception ex) { System.out.println("Unable to set RSA server certificate.");
System.out.println("RSA cipher-suites can not be used.");
}

byte[] types = { ClientTrustDecider.rsa_sign, };
Name[] cas=null;

cas = new Name[1];

cas[0]=Util.getSubject(entrustProfile.getCaCertificate());
sslServerContext.setRequireClientCertificate(types,cas);

try {

SSLServerSocket server = new SSLServerSocket( SERVERPORT, sslServerContext);
SSLSocket client = null;
System.out.println(NAME+" is listening on port "+SERVERPORT+".");
do { client = (SSLSocket) server.accept();
(new CountServerThread(client)).start();
} while(true);
} catch(IOException ex) { System.out.println("Unable to listen on "+SERVERPORT+".");
System.exit(0);
}
  }
}

class CountServerThread extends Thread {
SSLSocket client;

  public CountServerThread(SSLSocket client) {

this.client = (SSLSocket) client;   }

  public String startTiming() { return String.valueOf( System.currentTimeMillis());   }

  public String stopTiming() { return String.valueOf( System.currentTimeMillis() );   }

  public void run() {

try { describeConnection(client);
FileOutputStream fileStream = new FileOutputStream("Utenti.txt");
DataInputStream inStream = new DataInputStream(client.getInputStream());
DataOutputStream outStream = new DataOutputStream(client.getOutputStream());

/* Il codice che segue è puramente indicativo.
In linea di principio si suppone che ogni applicazione comunichi a questo Server quando inizia o termina la propria esecuzione. Il Server rimane in attesa di questi messaggi. Se il messaggio :

  • è 10, si conferma all'applicazione che può continuare la propria esecuzione e si scrive su file quando il cliente X ( conosciuto per via del suo certificato ) ha iniziato il suo utilizzo;
  • è 20, l'applicazione ha terminato la propria esecuzione quindi è possibile scrivere su un file che il cliente X l'ha utilizzata per Tot tempo. Una apposita applicazione leggerà poi mensilmente tale file preparando le fatture per i clienti.
*/
int Start = inStream.readInt();
if(Start == 10) { outStream.writeInt(10);
startTiming();
/* Scrivo_su_file (Applicazione, Nome_Utente, startTiming)*/
}
else outStream.writeInt(0);

int Stop = inStream.readInt();
if(Stop == 20) {
outStream.writeInt(20);
stopTiming();
/* Scrivo_su_file (Applicazione, Nome_Utente, stopTiming)*/
}
/********************************************************/ System.out.println("Request completed. Closing connection.");
}catch(IOException ex) { System.out.println("IOException occured when processing request."); }
try { client.close(); }catch(IOException ex) { System.out.println("IOException occured when closing SSLSocket."); }
  }

  void describeConnection(SSLSocket client) {

String destName = client.getInetAddress().getHostName();
String destAddr = client.getInetAddress().getHostAddress();
int destPort = client.getPort();
System.out.println("Accepted connection to "+destName+" ("
+destAddr+")"+" on port "+destPort+".");
  }
}

b6.gif - 3872 Bytes