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 / pkcs / PKCS8Key.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-07-08  |  5.5 KB  |  193 lines

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