home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Notes / 50b2wic.exe / DATA1.CAB / NotesProgramFilesJavaSupport / rt.jar / sun / security / x509 / X509Key.class (.txt) < prev   
Encoding:
Java Class File  |  1998-04-23  |  5.1 KB  |  193 lines

  1. package sun.security.x509;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.security.InvalidKeyException;
  10. import java.security.Key;
  11. import java.security.MessageDigest;
  12. import java.security.Provider;
  13. import java.security.PublicKey;
  14. import java.security.Security;
  15. import java.util.Properties;
  16. import sun.misc.CharacterEncoder;
  17. import sun.misc.HexDumpEncoder;
  18. import sun.security.util.DerOutputStream;
  19. import sun.security.util.DerValue;
  20.  
  21. public class X509Key implements PublicKey {
  22.    protected AlgorithmId algid;
  23.    protected byte[] key;
  24.    private byte[] encodedKey;
  25.  
  26.    protected X509Key() {
  27.    }
  28.  
  29.    private X509Key(AlgorithmId var1, byte[] var2) throws InvalidKeyException {
  30.       this.algid = var1;
  31.       this.key = var2;
  32.       this.encode();
  33.    }
  34.  
  35.    public static X509Key parse(DerValue var0) throws IOException {
  36.       if (var0.tag != 48) {
  37.          throw new IOException("corrupt subject key");
  38.       } else {
  39.          AlgorithmId var1 = AlgorithmId.parse(var0.data.getDerValue());
  40.  
  41.          X509Key var2;
  42.          try {
  43.             var2 = buildX509Key(var1, var0.data.getBitString());
  44.          } catch (InvalidKeyException var4) {
  45.             throw new IOException("subject key, " + ((Throwable)var4).toString());
  46.          }
  47.  
  48.          if (var0.data.available() != 0) {
  49.             throw new IOException("excess subject key");
  50.          } else {
  51.             return var2;
  52.          }
  53.       }
  54.    }
  55.  
  56.    protected void parseKeyBits() throws IOException, InvalidKeyException {
  57.       this.encode();
  58.    }
  59.  
  60.    static String getSunProperty(String var0) {
  61.       Provider var1 = Security.getProvider("SUN");
  62.       return ((Properties)var1).getProperty(var0);
  63.    }
  64.  
  65.    static X509Key buildX509Key(AlgorithmId var0, byte[] var1) throws IOException, InvalidKeyException {
  66.       String var2 = getSunProperty("PublicKey.X.509." + var0.getName());
  67.       if (var2 == null) {
  68.          var2 = "sun.security.x509.X509Key";
  69.       }
  70.  
  71.       try {
  72.          Class var3 = Class.forName(var2);
  73.          Object var4 = var3.newInstance();
  74.          if (var4 instanceof X509Key) {
  75.             X509Key var5 = (X509Key)var4;
  76.             var5.algid = var0;
  77.             var5.key = var1;
  78.             var5.parseKeyBits();
  79.             return var5;
  80.          } else {
  81.             System.err.println("Misconfiguration:  faulty key config, " + var2);
  82.             return new X509Key(var0, var1);
  83.          }
  84.       } catch (ClassNotFoundException var6) {
  85.          System.err.println("Misconfiguration:  unknown key class, " + var2);
  86.          return new X509Key(var0, var1);
  87.       } catch (IllegalAccessException var7) {
  88.          ((Throwable)var7).printStackTrace();
  89.          throw new IOException(var2 + " [internal error]");
  90.       } catch (InstantiationException var8) {
  91.          System.err.println("Misconfiguration:  faulty key class, " + var2);
  92.          return new X509Key(var0, var1);
  93.       }
  94.    }
  95.  
  96.    public String getAlgorithm() {
  97.       return this.algid.getName();
  98.    }
  99.  
  100.    public AlgorithmId getAlgorithmId() {
  101.       return this.algid;
  102.    }
  103.  
  104.    public final void emit(DerOutputStream var1) throws IOException {
  105.       DerOutputStream var2 = new DerOutputStream();
  106.       this.algid.emit(var2);
  107.       var2.putBitString(this.key);
  108.       var1.write((byte)48, var2);
  109.    }
  110.  
  111.    public synchronized byte[] getEncoded() {
  112.       if (this.encodedKey == null) {
  113.          try {
  114.             this.encode();
  115.          } catch (InvalidKeyException var1) {
  116.          }
  117.       }
  118.  
  119.       return this.encodedKey;
  120.    }
  121.  
  122.    public String getFormat() {
  123.       return "X.509";
  124.    }
  125.  
  126.    public byte[] encode() throws InvalidKeyException {
  127.       if (this.encodedKey == null) {
  128.          try {
  129.             DerOutputStream var1 = new DerOutputStream();
  130.             this.emit(var1);
  131.             this.encodedKey = ((ByteArrayOutputStream)var1).toByteArray();
  132.          } catch (IOException var2) {
  133.             throw new InvalidKeyException("IOException : " + ((Throwable)var2).getMessage());
  134.          }
  135.       }
  136.  
  137.       return this.encodedKey;
  138.    }
  139.  
  140.    public String toString() {
  141.       HexDumpEncoder var1 = new HexDumpEncoder();
  142.       return "algorithm = " + this.algid.toString() + ", unparsed keybits = \n" + ((CharacterEncoder)var1).encodeBuffer(this.key);
  143.    }
  144.  
  145.    public void decode(InputStream var1) throws InvalidKeyException {
  146.       try {
  147.          DerValue var2 = new DerValue(var1);
  148.          if (var2.tag != 48) {
  149.             throw new InvalidKeyException("invalid key format");
  150.          } else {
  151.             this.algid = AlgorithmId.parse(var2.data.getDerValue());
  152.             this.key = var2.data.getBitString();
  153.             this.parseKeyBits();
  154.             if (var2.data.available() != 0) {
  155.                throw new InvalidKeyException("excess key data");
  156.             }
  157.          }
  158.       } catch (IOException var4) {
  159.          throw new InvalidKeyException(((Throwable)var4).toString());
  160.       }
  161.    }
  162.  
  163.    public void decode(byte[] var1) throws InvalidKeyException {
  164.       this.decode((InputStream)(new ByteArrayInputStream(var1)));
  165.    }
  166.  
  167.    private synchronized void writeObject(ObjectOutputStream var1) throws IOException {
  168.       var1.write(this.getEncoded());
  169.    }
  170.  
  171.    private synchronized void readObject(ObjectInputStream var1) throws IOException {
  172.       try {
  173.          this.decode((InputStream)var1);
  174.       } catch (InvalidKeyException var3) {
  175.          ((Throwable)var3).printStackTrace();
  176.          throw new IOException("deserialized key is invalid: " + ((Throwable)var3).getMessage());
  177.       }
  178.    }
  179.  
  180.    public boolean equals(Object var1) {
  181.       if (this == var1) {
  182.          return true;
  183.       } else if (var1 instanceof Key) {
  184.          Key var2 = (Key)var1;
  185.          byte[] var3 = this.getEncoded();
  186.          byte[] var4 = var2.getEncoded();
  187.          return MessageDigest.isEqual(var3, var4);
  188.       } else {
  189.          return false;
  190.       }
  191.    }
  192. }
  193.