home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VPage / Java.bin / CLASSES.ZIP / sun / security / provider / IdentityDatabase.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-07-08  |  6.9 KB  |  271 lines

  1. package sun.security.provider;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InvalidClassException;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.io.OutputStream;
  12. import java.io.Serializable;
  13. import java.io.StreamCorruptedException;
  14. import java.security.Identity;
  15. import java.security.IdentityScope;
  16. import java.security.InvalidParameterException;
  17. import java.security.Key;
  18. import java.security.KeyManagementException;
  19. import java.security.MessageDigest;
  20. import java.security.PublicKey;
  21. import java.security.Security;
  22. import java.security.Signer;
  23. import java.util.Enumeration;
  24. import java.util.Hashtable;
  25.  
  26. public class IdentityDatabase extends IdentityScope implements Serializable {
  27.    private static boolean debug;
  28.    private static boolean error = true;
  29.    File sourceFile;
  30.    Hashtable identities;
  31.  
  32.    IdentityDatabase() throws InvalidParameterException {
  33.       this("restoring...");
  34.    }
  35.  
  36.    public IdentityDatabase(File var1) throws InvalidParameterException {
  37.       this(var1.getName());
  38.       this.sourceFile = var1;
  39.    }
  40.  
  41.    public IdentityDatabase(String var1) throws InvalidParameterException {
  42.       super(var1);
  43.       this.identities = new Hashtable();
  44.    }
  45.  
  46.    public static IdentityDatabase fromStream(InputStream var0) throws IOException {
  47.       IdentityDatabase var1 = null;
  48.  
  49.       try {
  50.          ObjectInputStream var2 = new ObjectInputStream(var0);
  51.          var1 = (IdentityDatabase)var2.readObject();
  52.       } catch (ClassNotFoundException var3) {
  53.          debug("This should not be happening.", var3);
  54.          error("The version of the database is obsolete. Cannot initialize.");
  55.       } catch (InvalidClassException var4) {
  56.          debug("This should not be happening.", var4);
  57.          error("Unable to initialize system identity scope:  InvalidClassException. \nThis is most likely due to a serialization versioning problem: a class used in key management was obsoleted");
  58.       } catch (StreamCorruptedException var5) {
  59.          debug("The serialization stream is corrupted. Unable to load.", var5);
  60.          error("Unable to initialize system identity scope. StreamCorrputedException.");
  61.       }
  62.  
  63.       if (var1 == null) {
  64.          var1 = new IdentityDatabase("uninitialized");
  65.       }
  66.  
  67.       return var1;
  68.    }
  69.  
  70.    public static IdentityDatabase fromFile(File var0) throws IOException {
  71.       FileInputStream var1 = new FileInputStream(var0);
  72.       IdentityDatabase var2 = fromStream(var1);
  73.       var2.sourceFile = var0;
  74.       return var2;
  75.    }
  76.  
  77.    public int size() {
  78.       return this.identities.size();
  79.    }
  80.  
  81.    public Identity getIdentity(String var1) {
  82.       Identity var2 = (Identity)this.identities.get(var1);
  83.       if (var2 instanceof Signer) {
  84.          this.localCheck("get.signer");
  85.       }
  86.  
  87.       return var2;
  88.    }
  89.  
  90.    public Identity getIdentity(PublicKey var1) {
  91.       if (var1 == null) {
  92.          return null;
  93.       } else {
  94.          Enumeration var2 = this.identities();
  95.  
  96.          while(var2.hasMoreElements()) {
  97.             Identity var3 = (Identity)var2.nextElement();
  98.             PublicKey var4 = var3.getPublicKey();
  99.             if (var4 != null && this.keyEqual(var4, var1)) {
  100.                if (var3 instanceof Signer) {
  101.                   this.localCheck("get.signer");
  102.                }
  103.  
  104.                return var3;
  105.             }
  106.          }
  107.  
  108.          return null;
  109.       }
  110.    }
  111.  
  112.    private boolean keyEqual(Key var1, Key var2) {
  113.       return var1 == var2 ? true : MessageDigest.isEqual(var1.getEncoded(), var2.getEncoded());
  114.    }
  115.  
  116.    public void addIdentity(Identity var1) throws KeyManagementException {
  117.       this.localCheck("add.identity");
  118.       Identity var2 = this.getIdentity(var1.getName());
  119.       Identity var3 = this.getIdentity(var1.getPublicKey());
  120.       String var4 = null;
  121.       if (var2 != null) {
  122.          var4 = "name conflict";
  123.       }
  124.  
  125.       if (var3 != null) {
  126.          var4 = "key conflict";
  127.       }
  128.  
  129.       if (var4 != null) {
  130.          throw new KeyManagementException(var4);
  131.       } else {
  132.          this.identities.put(var1.getName(), var1);
  133.       }
  134.    }
  135.  
  136.    public void removeIdentity(Identity var1) throws KeyManagementException {
  137.       this.localCheck("remove.identity");
  138.       String var2 = var1.getName();
  139.       if (this.identities.get(var2) == null) {
  140.          throw new KeyManagementException("there is no identity named " + var2 + " in " + this);
  141.       } else {
  142.          this.identities.remove(var2);
  143.       }
  144.    }
  145.  
  146.    public Enumeration identities() {
  147.       return this.identities.elements();
  148.    }
  149.  
  150.    void setSourceFile(File var1) {
  151.       this.sourceFile = var1;
  152.    }
  153.  
  154.    File getSourceFile() {
  155.       return this.sourceFile;
  156.    }
  157.  
  158.    public void save(OutputStream var1) throws IOException {
  159.       try {
  160.          ObjectOutputStream var2 = new ObjectOutputStream(var1);
  161.          var2.writeObject(this);
  162.          var2.flush();
  163.       } catch (InvalidClassException var3) {
  164.          debug("This should not be happening.", var3);
  165.       }
  166.    }
  167.  
  168.    void save(File var1) throws IOException {
  169.       this.setSourceFile(var1);
  170.       FileOutputStream var2 = new FileOutputStream(var1);
  171.       this.save((OutputStream)var2);
  172.    }
  173.  
  174.    public void save() throws IOException {
  175.       if (this.sourceFile == null) {
  176.          throw new IOException("this database has no source file");
  177.       } else {
  178.          this.save(this.sourceFile);
  179.       }
  180.    }
  181.  
  182.    private static File systemDatabaseFile() {
  183.       String var0 = Security.getProperty("identity.database");
  184.       if (var0 == null) {
  185.          var0 = System.getProperty("user.home") + File.separatorChar + "identitydb.obj";
  186.       }
  187.  
  188.       return new File(var0);
  189.    }
  190.  
  191.    private static void initializeSystem() {
  192.       File var1 = systemDatabaseFile();
  193.  
  194.       try {
  195.          IdentityDatabase var0;
  196.          if (var1.exists()) {
  197.             debug("loading system database from file: " + var1);
  198.             var0 = fromFile(var1);
  199.          } else {
  200.             var0 = new IdentityDatabase(var1);
  201.          }
  202.  
  203.          IdentityScope.setSystemScope(var0);
  204.          debug("System database initialized: " + var0);
  205.       } catch (IOException var3) {
  206.          debug("Error initializing identity database: " + var1, var3);
  207.       } catch (InvalidParameterException var4) {
  208.          debug("Error trying to instantiate a system identities db in " + var1, var4);
  209.       }
  210.    }
  211.  
  212.    private static File securityPropFile(String var0) {
  213.       String var1 = File.separator;
  214.       return new File(System.getProperty("java.home") + var1 + "lib" + var1 + "security" + var1 + var0);
  215.    }
  216.  
  217.    public String toString() {
  218.       return "sun.security.IdentityDatabase, source file: " + this.sourceFile;
  219.    }
  220.  
  221.    private static void debug(String var0) {
  222.       if (debug) {
  223.          System.err.println(var0);
  224.       }
  225.  
  226.    }
  227.  
  228.    private static void debug(String var0, Throwable var1) {
  229.       if (debug) {
  230.          var1.printStackTrace();
  231.          System.err.println(var0);
  232.       }
  233.  
  234.    }
  235.  
  236.    private static void error(String var0) {
  237.       if (error) {
  238.          System.err.println(var0);
  239.       }
  240.  
  241.    }
  242.  
  243.    void localCheck(String var1) {
  244.       SecurityManager var2 = System.getSecurityManager();
  245.       if (var2 != null) {
  246.          var1 = this.getClass().getName() + "." + var1 + "." + this.localFullName();
  247.          var2.checkSecurityAccess(var1);
  248.       }
  249.  
  250.    }
  251.  
  252.    String localFullName() {
  253.       String var1 = ((Identity)this).getName();
  254.       if (((Identity)this).getScope() != null) {
  255.          var1 = var1 + "." + ((Identity)this).getScope().getName();
  256.       }
  257.  
  258.       return var1;
  259.    }
  260.  
  261.    private synchronized void writeObject(ObjectOutputStream var1) throws IOException {
  262.       this.localCheck("serialize.identity.database");
  263.       var1.writeObject(this.identities);
  264.       var1.writeObject(this.sourceFile);
  265.    }
  266.  
  267.    static {
  268.       initializeSystem();
  269.    }
  270. }
  271.