FileDocCategorySizeDatePackage
KeyManager.javaAPI DocExample6202Thu Apr 09 12:17:28 BST 1998oreilly.jonathan.security

KeyManager

public class KeyManager extends IdentityScope

Fields Summary
protected PrivateKey
mPrivateKey
protected Hashtable
mIdentities
protected transient String
mKeyFile
Constructors Summary
protected KeyManager(String name, KeyPair pair)

    super(name);
    try { setPublicKey(pair.getPublic()); }
    catch (KeyManagementException kme) {}
    mPrivateKey = pair.getPrivate();
    mIdentities = new Hashtable();
  
Methods Summary
public voidaddIdentity(java.lang.String name, java.security.PublicKey key)

    Identity i = new KeyManagerIdentity(name);
    i.setPublicKey(key);
    addIdentity(i);
  
public synchronized voidaddIdentity(java.security.Identity identity)

    if (mIdentities.contains(identity))
      throw new KeyManagementException("This KeyManager already contains "
          + identity.getName() + ".");
    if (mIdentities.containsKey(identity.getPublicKey()))
      throw new KeyManagementException("This KeyManager already contains "
          + identity.getName() + "'s key.");
    mIdentities.put(identity.getPublicKey(), identity);
  
public static oreilly.jonathan.security.KeyManagercreate(java.lang.String file, java.lang.String name, java.security.KeyPair pair)

    KeyManager km = new KeyManager(name, pair);
    km.mKeyFile = file;
    return km;
  
public synchronized java.security.IdentitygetIdentity(java.lang.String name)

    Enumeration e = mIdentities.elements();
    while (e.hasMoreElements()) {
      Identity i = (Identity)e.nextElement();
      if (i.getName().equals(name))
        return i;
    }
    return null;
  
public java.security.IdentitygetIdentity(java.security.PublicKey key)

    return (Identity)mIdentities.get(key);
  
public static oreilly.jonathan.security.KeyManagergetInstance(java.lang.String file)

    ObjectInputStream in = new ObjectInputStream(
        new FileInputStream(file));
    KeyManager km = (KeyManager)in.readObject();
    in.close();
    km.mKeyFile = file;
    return km;
  
public java.security.PrivateKeygetPrivateKey()

 return mPrivateKey; 
public synchronized java.security.PublicKeygetPublicKey(java.lang.String name)

    if (name.equals(getName()))
      return getPublicKey();
    return getIdentity(name).getPublicKey();
  
public java.util.Enumerationidentities()

 return mIdentities.elements(); 
public static voidmain(java.lang.String[] args)

    if (args.length < 2) {
      usage();
      return;
    }
    String option = args[0];
    String keyfile = args[1];

    if (option.indexOf("c") != -1) {
      if (args.length < 5) { usage(); return; }
      String signer = args[2];
      String algorithm = args[3];
      int strength = NumberFormat.getInstance().parse(args[4]).intValue();
      System.out.println("Initializing the KeyPairGenerator...");
      KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithm);
      kpg.initialize(strength);
      System.out.println("Generating the key pair...");
      KeyPair pair = kpg.genKeyPair();
      KeyManager km = create(keyfile, signer, pair);
      km.save();
      System.out.println("Done.");
    }
    else if (option.indexOf("e") != -1) {
      if (args.length < 4) { usage(); return; }
      String idname = args[2];
      String outfile = args[3];
      KeyManager km = getInstance(keyfile);
      ObjectOutputStream out = new ObjectOutputStream(
          new FileOutputStream(outfile));
      PublicKey key = km.getPublicKey(idname);
      out.writeObject(idname);
      out.writeObject(key);
      out.close();
      System.out.println("Done.");
    }
    else if (option.indexOf("i") != -1) {
      if (args.length < 3) { usage(); return; }
      String infile = args[2];
      KeyManager km = getInstance(keyfile);
      ObjectInputStream in = new ObjectInputStream(
          new FileInputStream(infile));
      String idname = (String)in.readObject();
      PublicKey key = (PublicKey)in.readObject();
      in.close();
      km.addIdentity(idname, key);
      km.save();
      System.out.println("Done.");
    }
    else if (option.indexOf("r") != -1) {
      if (args.length < 3) { usage(); return; }
      String idname = args[2];
      KeyManager km = getInstance(keyfile);
      Identity i = km.getIdentity(idname);
      km.removeIdentity(i);
      km.save();
      System.out.println("Done.");
    }
    else if (option.indexOf("l") != -1) {
      if (args.length < 2) { usage(); return; }
      KeyManager km = getInstance(keyfile);
      System.out.println("KeyManager contents of " + keyfile + ":");
      System.out.println("  public and private key for " + km.getName());
      Enumeration e = km.identities();
      while (e.hasMoreElements()) {
        Identity i = (Identity)e.nextElement();
        System.out.println("  public key for " + i.getName());
      }
    }
  
public synchronized voidremoveIdentity(java.security.Identity identity)

    PublicKey key = identity.getPublicKey();
    if (mIdentities.containsKey(key))
      mIdentities.remove(key);
    else
      throw new KeyManagementException("This KeyManager does not contain "
          + identity.getName() + ".");
  
public synchronized voidsave()

    try {
      ObjectOutputStream out = new ObjectOutputStream(
          new FileOutputStream(mKeyFile));
      out.writeObject(this);
      out.close();
    }
    catch (Exception e) {
      System.out.println("KeyManager.save: " + e.toString());
    }
  
public intsize()

 return mIdentities.size(); 
protected static voidusage()

    System.out.println("Options:");
    System.out.println("  create: -c keyfile signer algorithm strength");
    System.out.println("  export: -e keyfile idname outfile");
    System.out.println("  import: -i keyfile infile");
    System.out.println("  remove: -r keyfile idname");
    System.out.println("  list  : -l keyfile");