home *** CD-ROM | disk | FTP | other *** search
- package sun.security.pkcs;
-
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.math.BigInteger;
- import java.security.InvalidKeyException;
- import java.security.PrivateKey;
- import java.security.Provider;
- import java.security.Security;
- import java.util.Properties;
- import sun.misc.CharacterEncoder;
- import sun.misc.HexDumpEncoder;
- import sun.security.util.BigInt;
- import sun.security.util.DerOutputStream;
- import sun.security.util.DerValue;
- import sun.security.x509.AlgorithmId;
-
- public class PKCS8Key implements PrivateKey {
- protected AlgorithmId algid;
- protected byte[] key;
- private byte[] encodedKey;
- public static final BigInteger version = BigInteger.valueOf(0L);
-
- protected PKCS8Key() {
- }
-
- private PKCS8Key(AlgorithmId var1, byte[] var2) throws InvalidKeyException {
- this.algid = var1;
- this.key = var2;
- this.encode();
- }
-
- public static PKCS8Key parse(DerValue var0) throws IOException {
- if (var0.tag != 48) {
- throw new IOException("corrupt private key");
- } else {
- BigInteger var3 = var0.data.getInteger().toBigInteger();
- if (!version.equals(var3)) {
- throw new IOException("version mismatch: (supported: " + version + ", parsed: " + var3);
- } else {
- AlgorithmId var1 = AlgorithmId.parse(var0.data.getDerValue());
-
- PKCS8Key var2;
- try {
- var2 = buildPKCS8Key(var1, var0.data.getOctetString());
- } catch (InvalidKeyException var4) {
- throw new IOException("corrupt private key");
- }
-
- if (var0.data.available() != 0) {
- throw new IOException("excess private key");
- } else {
- return var2;
- }
- }
- }
- }
-
- protected void parseKeyBits() throws IOException, InvalidKeyException {
- this.encode();
- }
-
- static String getSunProperty(String var0) {
- Provider var1 = Security.getProvider("SUN");
- return ((Properties)var1).getProperty(var0);
- }
-
- static PKCS8Key buildPKCS8Key(AlgorithmId var0, byte[] var1) throws IOException, InvalidKeyException {
- String var2 = getSunProperty("PrivateKey.PKCS#8." + var0.getName());
- if (var2 == null) {
- var2 = "sun.security.x509.PKCS8Key";
- }
-
- try {
- Class var3 = Class.forName(var2);
- Object var4 = var3.newInstance();
- if (var4 instanceof PKCS8Key) {
- PKCS8Key var5 = (PKCS8Key)var4;
- var5.algid = var0;
- var5.key = var1;
- var5.parseKeyBits();
- return var5;
- } else {
- System.err.println("Misconfiguration: faulty key config, " + var2);
- return new PKCS8Key(var0, var1);
- }
- } catch (ClassNotFoundException var6) {
- System.err.println("Misconfiguration: unknown key class, " + var2);
- return new PKCS8Key(var0, var1);
- } catch (IllegalAccessException var7) {
- ((Throwable)var7).printStackTrace();
- throw new IOException(var2 + " [internal error]");
- } catch (InstantiationException var8) {
- System.err.println("Misconfiguration: faulty key class, " + var2);
- return new PKCS8Key(var0, var1);
- }
- }
-
- public String getAlgorithm() {
- return this.algid.getName();
- }
-
- public AlgorithmId getAlgorithmId() {
- return this.algid;
- }
-
- public final void emit(DerOutputStream var1) throws IOException {
- DerOutputStream var2 = new DerOutputStream();
- var2.putInteger(new BigInt(version.toByteArray()));
- this.algid.emit(var2);
- var2.putOctetString(this.key);
- var1.write((byte)48, var2);
- }
-
- public synchronized byte[] getEncoded() {
- if (this.encodedKey == null) {
- try {
- this.encode();
- } catch (InvalidKeyException var1) {
- }
- }
-
- return this.encodedKey;
- }
-
- public String getFormat() {
- return "PKCS#8";
- }
-
- public byte[] encode() throws InvalidKeyException {
- if (this.encodedKey == null) {
- try {
- DerOutputStream var1 = new DerOutputStream();
- this.emit(var1);
- this.encodedKey = ((ByteArrayOutputStream)var1).toByteArray();
- } catch (IOException var2) {
- throw new InvalidKeyException("IOException : " + ((Throwable)var2).getMessage());
- }
- }
-
- return this.encodedKey;
- }
-
- public String toString() {
- HexDumpEncoder var1 = new HexDumpEncoder();
- return "algorithm = " + this.algid.toString() + ", unparsed keybits = \n" + ((CharacterEncoder)var1).encodeBuffer(this.key);
- }
-
- public void decode(InputStream var1) throws InvalidKeyException {
- try {
- DerValue var2 = new DerValue(var1);
- if (var2.tag != 48) {
- throw new InvalidKeyException("invalid key format");
- } else {
- BigInteger var3 = var2.data.getInteger().toBigInteger();
- if (!var3.equals(version)) {
- throw new IOException("version mismatch: (supported: " + version + ", parsed: " + var3);
- } else {
- this.algid = AlgorithmId.parse(var2.data.getDerValue());
- this.key = var2.data.getOctetString();
- this.parseKeyBits();
- if (var2.data.available() != 0) {
- throw new InvalidKeyException("excess key data");
- }
- }
- }
- } catch (IOException var4) {
- throw new InvalidKeyException();
- }
- }
-
- public void decode(byte[] var1) throws InvalidKeyException {
- this.decode((InputStream)(new ByteArrayInputStream(var1)));
- }
-
- private synchronized void writeObject(ObjectOutputStream var1) throws IOException {
- var1.write(this.getEncoded());
- }
-
- private synchronized void readObject(ObjectInputStream var1) throws IOException {
- try {
- this.decode((InputStream)var1);
- } catch (InvalidKeyException var3) {
- ((Throwable)var3).printStackTrace();
- throw new IOException("deserialized key is invalid: " + ((Throwable)var3).getMessage());
- }
- }
- }
-