Red Hat Application Migration Toolkit
package iaik.pkcs.pkcs8;
import iaik.asn1.ASN1;
import iaik.asn1.ASN1Object;
import iaik.asn1.ASN1Type;
import iaik.asn1.CodingException;
import iaik.asn1.OCTET_STRING;
import iaik.asn1.SEQUENCE;
import iaik.asn1.structures.AlgorithmID;
import iaik.pkcs.pkcs8.PrivateKeyInfo;
import iaik.security.cipher.PBEKey;
import iaik.security.cipher.PBEKeyBMP;
import iaik.security.pbe.PBEGenParameterSpec;
import iaik.utils.InternalErrorException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.security.AlgorithmParameters;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.spec.InvalidParameterSpecException;
import javax.crypto.Cipher;
import javax.crypto.spec.PBEParameterSpec;
public class EncryptedPrivateKeyInfo implements ASN1Type, Serializable, PrivateKey {
// $FF: synthetic field
static Class d;
ASN1 c;
AlgorithmID b;
PrivateKey a;
static Class a(String var0) {
try {
return Class.forName(var0);
} catch (ClassNotFoundException var2) {
throw new NoClassDefFoundError(var2.getMessage());
}
}
public void writeTo(OutputStream var1) throws IOException {
this.c.writeTo(var1);
}
private void writeObject(ObjectOutputStream var1) throws IOException {
if(this.c == null) {
throw new IOException("Private key not encrypted yet.");
} else {
var1.write(this.getEncoded());
}
}
public String toString() {
StringBuffer var1 = new StringBuffer();
if(this.b != null) {
var1.append("Private key is encrypted with algorithm: " + this.b.getName());
} else {
var1.append(this.a.getAlgorithm() + " private key is not encrypted yet.");
}
return var1.toString();
}
public ASN1Object toASN1Object() {
return this.c.toASN1Object();
}
private Key a(char[] var1) {
return (Key)(this.b.equals(AlgorithmID.pbeWithMD5AndDES_CBC)?new PBEKey(var1):new PBEKeyBMP(var1));
}
private void readObject(ObjectInputStream var1) throws ClassNotFoundException, IOException {
try {
this.c = new ASN1(var1);
this.a();
} catch (CodingException var3) {
throw new IOException("Unable to restore PrivateKeyInfo: " + var3.toString());
} catch (InvalidKeyException var4) {
throw new IOException("Unable to restore PrivateKeyInfo: " + var4.toString());
}
}
public PrivateKey getPrivateKeyInfo() {
return this.a;
}
public String getFormat() {
return "PKCS#8";
}
public byte[] getEncoded() {
return this.c.toByteArray();
}
public String getAlgorithm() {
return "ENCRYPTED";
}
public void encrypt(char[] var1, AlgorithmID var2, SecureRandom var3, int var4) throws NoSuchAlgorithmException {
this.b = (AlgorithmID)var2.clone();
byte[] var5;
Cipher var6;
try {
var6 = this.b.getCipherInstance();
PBEGenParameterSpec var7 = new PBEGenParameterSpec(8, var4);
var6.init(1, this.a(var1), var7, var3);
var5 = var6.doFinal(this.a.getEncoded());
this.b.setAlgorithmParameters(var6.getParameters());
} catch (InvalidKeyException var9) {
throw new InternalErrorException(var9);
} catch (GeneralSecurityException var10) {
throw new InternalErrorException(var10);
}
var6 = null;
try {
SEQUENCE var11 = new SEQUENCE();
var11.addComponent(this.b.toASN1Object());
var11.addComponent(new OCTET_STRING(var5));
this.c = new ASN1(var11);
} catch (CodingException var8) {
throw new InternalErrorException(var8);
}
}
public void encrypt(char[] var1, AlgorithmID var2, SecureRandom var3) throws NoSuchAlgorithmException {
this.encrypt(var1, var2, var3, 1);
}
public void encrypt(String var1, AlgorithmID var2, SecureRandom var3) throws NoSuchAlgorithmException {
this.encrypt(var1.toCharArray(), var2, var3);
}
public PrivateKey decrypt(char[] var1) throws GeneralSecurityException, NoSuchAlgorithmException {
PBEParameterSpec var2 = null;
try {
AlgorithmParameters var3 = this.b.getAlgorithmParameters("PBE");
var2 = (PBEParameterSpec)var3.getParameterSpec(d != null?d:(d = a("javax.crypto.spec.PBEParameterSpec")));
} catch (InvalidParameterSpecException var10) {
throw new InternalErrorException(var10);
}
Cipher var11 = this.b.getCipherInstance();
try {
var11.init(2, this.a(var1), var2);
} catch (InvalidKeyException var9) {
throw new InternalErrorException(var9);
}
try {
byte[] var4 = (byte[])this.c.getComponentAt(1).getValue();
byte[] var5 = var11.doFinal(var4);
this.a = PrivateKeyInfo.getPrivateKey(var5);
return this.a;
} catch (InvalidKeyException var6) {
throw new GeneralSecurityException("Unable to parse decrypted private key: " + var6.toString());
} catch (GeneralSecurityException var7) {
throw new GeneralSecurityException("Unable to decrypt private key: " + var7.toString());
} catch (CodingException var8) {
throw new GeneralSecurityException("Unable to parse decrypted private key: " + var8.toString());
}
}
public PrivateKey decrypt(String var1) throws GeneralSecurityException, NoSuchAlgorithmException {
return this.decrypt(var1.toCharArray());
}
public void decode(ASN1Object var1) throws CodingException {
this.c = new ASN1(var1);
try {
this.a();
} catch (InvalidKeyException var3) {
throw new CodingException(var3.toString());
}
}
private void a() throws InvalidKeyException {
try {
this.b = new AlgorithmID(this.c.getComponentAt(0));
} catch (CodingException var2) {
throw new InvalidKeyException(var2.toString());
}
}
public EncryptedPrivateKeyInfo(byte[] var1) throws InvalidKeyException {
try {
this.c = new ASN1(var1);
this.a();
} catch (CodingException var3) {
throw new InvalidKeyException("No PrivateKeyInfo: " + var3.toString());
}
}
public EncryptedPrivateKeyInfo(PrivateKey var1) {
this.a = var1;
}
public EncryptedPrivateKeyInfo(InputStream var1) throws IOException, InvalidKeyException {
try {
this.c = new ASN1(var1);
this.a();
} catch (CodingException var3) {
throw new InvalidKeyException("No PrivateKeyInfo: " + var3.toString());
}
}
public EncryptedPrivateKeyInfo(ASN1Object var1) throws InvalidKeyException {
try {
this.c = new ASN1(var1);
this.a();
} catch (CodingException var3) {
throw new InvalidKeyException("No PrivateKeyInfo: " + var3.toString());
}
}
}