home *** CD-ROM | disk | FTP | other *** search
- package sun.security.x509;
-
- 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.security.InvalidKeyException;
- import java.security.Key;
- import java.security.MessageDigest;
- import java.security.Provider;
- import java.security.PublicKey;
- import java.security.Security;
- import java.util.Properties;
- import sun.misc.CharacterEncoder;
- import sun.misc.HexDumpEncoder;
- import sun.security.util.DerOutputStream;
- import sun.security.util.DerValue;
-
- public class X509Key implements PublicKey {
- protected AlgorithmId algid;
- protected byte[] key;
- private byte[] encodedKey;
-
- protected X509Key() {
- }
-
- private X509Key(AlgorithmId var1, byte[] var2) throws InvalidKeyException {
- this.algid = var1;
- this.key = var2;
- this.encode();
- }
-
- public static X509Key parse(DerValue var0) throws IOException {
- if (var0.tag != 48) {
- throw new IOException("corrupt subject key");
- } else {
- AlgorithmId var1 = AlgorithmId.parse(var0.data.getDerValue());
-
- X509Key var2;
- try {
- var2 = buildX509Key(var1, var0.data.getBitString());
- } catch (InvalidKeyException var4) {
- throw new IOException("subject key, " + ((Throwable)var4).toString());
- }
-
- if (var0.data.available() != 0) {
- throw new IOException("excess subject 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 X509Key buildX509Key(AlgorithmId var0, byte[] var1) throws IOException, InvalidKeyException {
- String var2 = getSunProperty("PublicKey.X.509." + var0.getName());
- if (var2 == null) {
- var2 = "sun.security.x509.X509Key";
- }
-
- try {
- Class var3 = Class.forName(var2);
- Object var4 = var3.newInstance();
- if (var4 instanceof X509Key) {
- X509Key var5 = (X509Key)var4;
- var5.algid = var0;
- var5.key = var1;
- var5.parseKeyBits();
- return var5;
- } else {
- System.err.println("Misconfiguration: faulty key config, " + var2);
- return new X509Key(var0, var1);
- }
- } catch (ClassNotFoundException var6) {
- System.err.println("Misconfiguration: unknown key class, " + var2);
- return new X509Key(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 X509Key(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();
- this.algid.emit(var2);
- var2.putBitString(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 "X.509";
- }
-
- 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 {
- this.algid = AlgorithmId.parse(var2.data.getDerValue());
- this.key = var2.data.getBitString();
- this.parseKeyBits();
- if (var2.data.available() != 0) {
- throw new InvalidKeyException("excess key data");
- }
- }
- } catch (IOException var4) {
- throw new InvalidKeyException(((Throwable)var4).toString());
- }
- }
-
- 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());
- }
- }
-
- public boolean equals(Object var1) {
- if (this == var1) {
- return true;
- } else if (var1 instanceof Key) {
- Key var2 = (Key)var1;
- byte[] var3 = this.getEncoded();
- byte[] var4 = var2.getEncoded();
- return MessageDigest.isEqual(var3, var4);
- } else {
- return false;
- }
- }
- }
-