FileDocCategorySizeDatePackage
CertAgent.javaAPI DocExample1703Mon Jul 07 12:43:22 BST 1997dcj.examples.security

CertAgent

public class CertAgent extends SimpleAgent

Fields Summary
Identity
remoteAgent
Constructors Summary
public CertAgent(String host, int port)

    
      
        

    super(host, port);
    DataInputStream din = new DataInputStream(inStream);

    // Try to authenticate the remote agent
    try {
      String agentId = din.readUTF();
      int sigLen = din.readInt();
      byte[] sigData = new byte[sigLen];
      din.readFully(sigData);

      if (!authenticate(agentId, sigData)) {
        // Failed to authenticate, write error message, close socket and
        // return
        System.out.println("Failed to authenticate remote agent " + agentId);
        closeConnection();
      }
      else {
        // Remote agent is authenticated, first message is a welcome
        addMsg("HELLO " + agentId);
      }
    }
    catch (Exception e) {
      closeConnection();
    }
  
Methods Summary
protected booleanauthenticate(java.lang.String id, byte[] sigMsg)

    boolean success = false;
    PublicKey key = lookupKey(id);
    try {
      // Set up a signature with the agent's public key
      Signature sig = Signature.getInstance("RSA");
      sig.initVerify(key);
      // Try to verify the signature message from the agent
      sig.update(id.getBytes());
      success = sig.verify(sigMsg);
      
      if (success) {
        // Agent checks out, so initialize an identity for it
        remoteAgent = new Identity(id);
        remoteAgent.setPublicKey(key);
      }
    }
    catch (Exception e) {}

    return success;