bcpkix-jdk15on-147

mPEMReader, String paramString)
  {
    provider = paramString;
  }
  
  public Object parseObject(PemObject paramPemObject)
    throws IOException
  {
    ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(paramPemObject.getContent());
    try
    {
      CertificateFactory localCertificateFactory = CertificateFactory.getInstance("X.509", provider);
      return localCertificateFactory.generateCertificate(localByteArrayInputStream);
    }
    catch (Exception localException)
    {
      throw new PEMException("problem parsing cert: " + localException.toString(), localException);
    }
  }
}

/* Location:
 * Qualified Name:     org.bouncycastle.openssl.PEMReader.X509CertificateParser
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.bouncycastle.openssl;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Reader;
import java.math.BigInteger;
import java.security.AlgorithmParameters;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PublicKey;
import java.security.cert.CertificateFactory;
import java.security.spec.DSAPrivateKeySpec;
import java.security.spec.DSAPublicKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.DERInteger;
import org.bouncycastle.asn1.DERObjectIdentifier;
import org.bouncycastle.asn1.cms.ContentInfo;
import org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo;
import org.bouncycastle.asn1.pkcs.EncryptionScheme;
import org.bouncycastle.asn1.pkcs.KeyDerivationFunc;
import org.bouncycastle.asn1.pkcs.PBEParameter;
import org.bouncycastle.asn1.pkcs.PBES2Parameters;
import org.bouncycastle.asn1.pkcs.PBKDF2Params;
import org.bouncycastle.asn1.pkcs.PKCS12PBEParams;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.pkcs.RSAPrivateKey;
import org.bouncycastle.asn1.pkcs.RSAPublicKey;
import org.bouncycastle.asn1.sec.ECPrivateKey;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.PKCS10CertificationRequest;
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.util.io.pem.PemHeader;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemObjectParser;
import org.bouncycastle.util.io.pem.PemReader;
import org.bouncycastle.x509.X509V2AttributeCertificate;

public class PEMReader
  extends PemReader
{
  private final Map parsers = new HashMap();
  private PasswordFinder pFinder;
  
  public PEMReader(Reader paramReader)
  {
    this(paramReader, null, "BC");
  }
  
  public PEMReader(Reader paramReader, PasswordFinder paramPasswordFinder)
  {
    this(paramReader, paramPasswordFinder, "BC");
  }
  
  public PEMReader(Reader paramReader, PasswordFinder paramPasswordFinder, String paramString)
  {
    this(paramReader, paramPasswordFinder, paramString, paramString);
  }
  
  public PEMReader(Reader paramReader, PasswordFinder paramPasswordFinder, String paramString1, String paramString2)
  {
    super(paramReader);
    pFinder = paramPasswordFinder;
    parsers.put("CERTIFICATE REQUEST", new PKCS10CertificationRequestParser(null));
    parsers.put("NEW CERTIFICATE REQUEST", new PKCS10CertificationRequestParser(null));
    parsers.put("CERTIFICATE", new X509CertificateParser(paramString2));
    parsers.put("X509 CERTIFICATE", new X509CertificateParser(paramString2));
    parsers.put("X509 CRL", new X509CRLParser(paramString2));
    parsers.put("PKCS7", new PKCS7Parser(null));
    parsers.put("ATTRIBUTE CERTIFICATE", new X509AttributeCertificateParser(null));
    parsers.put("EC PARAMETERS", new ECNamedCurveSpecParser(null));
    parsers.put("PUBLIC KEY", new PublicKeyParser(paramString2));
    parsers.put("RSA PUBLIC KEY", new RSAPublicKeyParser(paramString2));
    parsers.put("RSA PRIVATE KEY", new RSAKeyPairParser(paramString2));
    parsers.put("DSA PRIVATE KEY", new DSAKeyPairParser(paramString2));
    parsers.put("EC PRIVATE KEY", new ECDSAKeyPairParser(paramString2));
    parsers.put("ENCRYPTED PRIVATE KEY", new EncryptedPrivateKeyParser(paramString1, paramString2));
    parsers.put("PRIVATE KEY", new PrivateKeyParser(paramString2));
  }
  
  public Object readObject()
    throws IOException
  {
    PemObject localPemObject = readPemObject();
    if (localPemObject != null)
    {
      String str = localPemObject.getType();
      if (parsers.containsKey(str)) {
        return ((PemObjectParser)parsers.get(str)).parseObject(localPemObject);
      }
      throw new IOException("unrecognised object: " + str);
    }
    return null;
  }
  
  private class DSAKeyPairParser
    extends PEMReader.KeyPairParser
  {
    public DSAKeyPairParser(String paramString)
    {
      super(paramString);
    }
    
    public Object parseObject(PemObject paramPemObject)
      throws IOException
    {
      try
      {
        ASN1Sequence localASN1Sequence = readKeyPair(paramPemObject);
        if (localASN1Sequence.size() != 6) {
          throw new PEMException("malformed sequence in DSA private key");
        }
        DERInteger localDERInteger1 = (DERInteger)localASN1Sequence.getObjectAt(1);
        DERInteger localDERInteger2 = (DERInteger)localASN1Sequence.getObjectAt(2);
        DERInteger localDERInteger3 = (DERInteger)localASN1Sequence.getObjectAt(3);
        DERInteger localDERInteger4 = (DERInteger)localASN1Sequence.getObjectAt(4);
        DERInteger localDERInteger5 = (DERInteger)localASN1Sequence.getObjectAt(5);
        DSAPrivateKeySpec localDSAPrivateKeySpec = new DSAPrivateKeySpec(localDERInteger5.getValue(), localDERInteger1.getValue(), localDERInteger2.getValue(), localDERInteger3.getValue());
        DSAPublicKeySpec localDSAPublicKeySpec = new DSAPublicKeySpec(localDERInteger4.getValue(), localDERInteger1.getValue(), localDERInteger2.getValue(), localDERInteger3.getValue());
        KeyFactory localKeyFactory = KeyFactory.getInstance("DSA", provider);
        return new KeyPair(localKeyFactory.generatePublic(localDSAPublicKeySpec), localKeyFactory.generatePrivate(localDSAPrivateKeySpec));
      }
      catch (IOException localIOException)
      {
        throw localIOException;
      }
      catch (Exception localException)
      {
        throw new PEMException("problem creating DSA private key: " + localException.toString(), localException);
      }
    }
  }
  
  private class ECDSAKeyPairParser
    extends PEMReader.KeyPairParser
  {
    public ECDSAKeyPairParser(String paramString)
    {
      super(paramString);
    }
    
    public Object parseObject(PemObject paramPemObject)
      throws IOException
    {
      try
      {
        ASN1Sequence localASN1Sequence = readKeyPair(paramPemObject);
        ECPrivateKey localECPrivateKey = ECPrivateKey.getInstance(localASN1Sequence);
        AlgorithmIdentifier localAlgorithmIdentifier = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, localECPrivateKey.getParameters());
        PrivateKeyInfo localPrivateKeyInfo = new PrivateKeyInfo(localAlgorithmIdentifier, localECPrivateKey);
        SubjectPublicKeyInfo localSubjectPublicKeyInfo = new SubjectPublicKeyInfo(localAlgorithmIdentifier, localECPrivateKey.getPublicKey().getBytes());
        PKCS8EncodedKeySpec localPKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(localPrivateKeyInfo.getEncoded());
        X509EncodedKeySpec localX509EncodedKeySpec = new X509EncodedKeySpec(localSubjectPublicKeyInfo.getEncoded());
        KeyFactory localKeyFactory = KeyFactory.getInstance("ECDSA", provider);
        return new KeyPair(localKeyFactory.generatePublic(localX509EncodedKeySpec), localKeyFactory.generatePrivate(localPKCS8EncodedKeySpec));
      }
      catch (IOException localIOException)
      {
        throw localIOException;
      }
      catch (Exception localException)
      {
        throw new PEMException("problem creating EC private key: " + localException.toString(), localException);
      }
    }
  }
  
  private class ECNamedCurveSpecParser
    implements PemObjectParser
  {
    private ECNamedCurveSpecParser() {}
    
    public Object parseObject(PemObject paramPemObject)
      throws IOException
    {
      try
      {
        DERObjectIdentifier localDERObjectIdentifier = (DERObjectIdentifier)ASN1Primitive.fromByteArray(paramPemObject.getContent());
        ECNamedCurveParameterSpec localECNamedCurveParameterSpec = ECNamedCurveTable.getParameterSpec(localDERObjectIdentifier.getId());
        if (localECNamedCurveParameterSpec == null) {
          throw new IOException("object ID not found in EC curve table");
        }
        return localECNamedCurveParameterSpec;
      }
      catch (IOException localIOException)
      {
        throw localIOException;
      }
      catch (Exception localException)
      {
        throw new PEMException("exception extracting EC named curve: " + localException.toString());
      }
    }
  }
  
  private class EncryptedPrivateKeyParser
    implements PemObjectParser
  {
    private String symProvider;
    private String asymProvider;
    
    public EncryptedPrivateKeyParser(String paramString1, String paramString2)
    {
      symProvider = paramString1;
      asymProvider = paramString2;
    }
    
    public Object parseObject(PemObject paramPemObject)
      throws IOException
    {
      try
      {
        EncryptedPrivateKeyInfo localEncryptedPrivateKeyInfo = EncryptedPrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(paramPemObject.getContent()));
        AlgorithmIdentifier localAlgorithmIdentifier = localEncryptedPrivateKeyInfo.getEncryptionAlgorithm();
        if (pFinder == null) {
          throw new PEMException("no PasswordFinder specified");
        }
        Object localObject1;
        Object localObject2;
        Object localObject3;
        Object localObject4;
        Object localObject5;
        Object localObject6;
        Object localObject7;
        Object localObject8;
        if (PEMUtilities.isPKCS5Scheme2(localAlgorithmIdentifier.getAlgorithm()))
        {
          localObject1 = PBES2Parameters.getInstance(localAlgorithmIdentifier.getParameters());
          localObject2 = ((PBES2Parameters)localObject1).getKeyDerivationFunc();
          localObject3 = ((PBES2Parameters)localObject1).getEncryptionScheme();
          localObject4 = (PBKDF2Params)((KeyDerivationFunc)localObject2).getParameters();
          int i = ((PBKDF2Params)localObject4).getIterationCount().intValue();
          localObject5 = ((PBKDF2Params)localObject4).getSalt();
          localObject6 = ((EncryptionScheme)localObject3).getAlgorithm().getId();
          localObject7 = PEMUtilities.generateSecretKeyForPKCS5Scheme2((String)localObject6, pFinder.getPassword(), (byte[])localObject5, i);
          localObject8 = Cipher.getInstance((String)localObject6, symProvider);
          AlgorithmParameters localAlgorithmParameters = AlgorithmParameters.getInstance((String)localObject6, symProvider);
          localAlgorithmParameters.init(((EncryptionScheme)localObject3).getParameters().toASN1Primitive().getEncoded());
          ((Cipher)localObject8).init(2, (Key)localObject7, localAlgorithmParameters);
          PrivateKeyInfo localPrivateKeyInfo = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(((Cipher)localObject8).doFinal(localEncryptedPrivateKeyInfo.getEncryptedData())));
          PKCS8EncodedKeySpec localPKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(localPrivateKeyInfo.getEncoded());
          KeyFactory localKeyFactory = KeyFactory.getInstance(localPrivateKeyInfo.getPrivateKeyAlgorithm().getAlgorithm().getId(), asymProvider);
          return localKeyFactory.generatePrivate(localPKCS8EncodedKeySpec);
        }
        PBEParameterSpec localPBEParameterSpec;
        if (PEMUtilities.isPKCS12(localAlgorithmIdentifier.getAlgorithm()))
        {
          localObject1 = PKCS12PBEParams.getInstance(localAlgorithmIdentifier.getParameters());
          localObject2 = localAlgorithmIdentifier.getAlgorithm().getId();
          localObject3 = new PBEKeySpec(pFinder.getPassword());
          localObject4 = SecretKeyFactory.getInstance((String)localObject2, symProvider);
          localPBEParameterSpec = new PBEParameterSpec(((PKCS12PBEParams)localObject1).getIV(), ((PKCS12PBEParams)localObject1).getIterations().intValue());
          localObject5 = Cipher.getInstance((String)localObject2, symProvider);
          ((Cipher)localObject5).init(2, ((SecretKeyFactory)localObject4).generateSecret((KeySpec)localObject3), localPBEParameterSpec);
          localObject6 = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(((Cipher)localObject5).doFinal(localEncryptedPrivateKeyInfo.getEncryptedData())));
          localObject7 = new PKCS8EncodedKeySpec(((PrivateKeyInfo)localObject6).getEncoded());
          localObject8 = KeyFactory.getInstance(((PrivateKeyInfo)localObject6).getAlgorithmId().getAlgorithm().getId(), asymProvider);
          return ((KeyFactory)localObject8).generatePrivate((KeySpec)localObject7);
        }
        if (PEMUtilities.isPKCS5Scheme1(localAlgorithmIdentifier.getAlgorithm()))
        {
          localObject1 = PBEParameter.getInstance(localAlgorithmIdentifier.getParameters());
          localObject2 = localAlgorithmIdentifier.getAlgorithm().getId();
          localObject3 = new PBEKeySpec(pFinder.getPassword());
          localObject4 = SecretKeyFactory.getInstance((String)localObject2, symProvider);
          localPBEParameterSpec = new PBEParameterSpec(((PBEParameter)localObject1).getSalt(), ((PBEParameter)localObject1).getIterationCount().intValue());
          localObject5 = Cipher.getInstance((String)localObject2, symProvider);
          ((Cipher)localObject5).init(2, ((SecretKeyFactory)localObject4).generateSecret((KeySpec)localObject3), localPBEParameterSpec);
          localObject6 = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(((Cipher)localObject5).doFinal(localEncryptedPrivateKeyInfo.getEncryptedData())));
          localObject7 = new PKCS8EncodedKeySpec(((PrivateKeyInfo)localObject6).getEncoded());
          localObject8 = KeyFactory.getInstance(((PrivateKeyInfo)localObject6).getAlgorithmId().getAlgorithm().getId(), asymProvider);
          return ((KeyFactory)localObject8).generatePrivate((KeySpec)localObject7);
        }
        throw new PEMException("Unknown algorithm: " + localAlgorithmIdentifier.getAlgorithm());
      }
      catch (IOException localIOException)
      {
        throw localIOException;
      }
      catch (Exception localException)
      {
        throw new PEMException("problem parsing ENCRYPTED PRIVATE KEY: " + localException.toString(), localException);
      }
    }
  }
  
  private abstract class KeyPairParser
    implements PemObjectParser
  {
    protected String provider;
    
    public KeyPairParser(String paramString)
    {
      provider = paramString;
    }
    
    protected ASN1Sequence readKeyPair(PemObject paramPemObject)
      throws IOException
    {
      int i = 0;
      String str1 = null;
      List localList = paramPemObject.getHeaders();
      Object localObject1 = localList.iterator();
      Object localObject2;
      while (((Iterator)localObject1).hasNext())
      {
        localObject2 = (PemHeader)((Iterator)localObject1).next();
        if ((((PemHeader)localObject2).getName().equals("Proc-Type")) && (((PemHeader)localObject2).getValue().equals("4,ENCRYPTED"))) {
          i = 1;
        } else if (((PemHeader)localObject2).getName().equals("DEK-Info")) {
          str1 = ((PemHeader)localObject2).getValue();
        }
      }
      localObject1 = paramPemObject.getContent();
      if (i != 0)
      {
        if (pFinder == null) {
          throw new PasswordException("No password finder specified, but a password is required");
        }
        localObject2 = pFinder.getPassword();
        if (localObject2 == null) {
          throw new PasswordException("Password is null, but a password is required");
        }
        StringTokenizer localStringTokenizer = new StringTokenizer(str1, ",");
        String str2 = localStringTokenizer.nextToken();
        byte[] arrayOfByte = Hex.decode(localStringTokenizer.nextToken());
        localObject1 = PEMUtilities.crypt(false, provider, (byte[])localObject1, (char[])localObject2, str2, arrayOfByte);
      }
      try
      {
        return ASN1Sequence.getInstance(ASN1Primitive.fromByteArray((byte[])localObject1));
      }
      catch (IOException localIOException)
      {
        if (i != 0) {
          throw new PEMException("exception decoding - please check password and data.", localIOException);
        }
        throw new PEMException(localIOException.getMessage(), localIOException);
      }
      catch (IllegalArgumentException localIllegalArgumentException)
      {
        if (i != 0) {
          throw new PEMException("exception decoding - please check password and data.", localIllegalArgumentException);
        }
        throw new PEMException(localIllegalArgumentException.getMessage(), localIllegalArgumentException);
      }
    }
  }
  
  private class PKCS10CertificationRequestParser
    implements PemObjectParser
  {
    private PKCS10CertificationRequestParser() {}
    
    public Object parseObject(PemObject paramPemObject)
      throws IOException
    {
      try
      {
        return new PKCS10CertificationRequest(paramPemObject.getContent());
      }
      catch (Exception localException)
      {
        throw new PEMException("problem parsing certrequest: " + localException.toString(), localException);
      }
    }
  }
  
  private class PKCS7Parser
    implements PemObjectParser
  {
    private PKCS7Parser() {}
    
    public Object parseObject(PemObject paramPemObject)
      throws IOException
    {
      try
      {
        ASN1InputStream localASN1InputStream = new ASN1InputStream(paramPemObject.getContent());
        return ContentInfo.getInstance(localASN1InputStream.readObject());
      }
      catch (Exception localException)
      {
        throw new PEMException("problem parsing PKCS7 object: " + localException.toString(), localException);
      }
    }
  }
  
  private class PrivateKeyParser
    implements PemObjectParser
  {
    private String provider;
    
    public PrivateKeyParser(String paramString)
    {
      provider = paramString;
    }
    
    public Object parseObject(PemObject paramPemObject)
      throws IOException
    {
      try
      {
        PrivateKeyInfo localPrivateKeyInfo = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(paramPemObject.getContent()));
        PKCS8EncodedKeySpec localPKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(paramPemObject.getContent());
        KeyFactory localKeyFactory = KeyFactory.getInstance(localPrivateKeyInfo.getAlgorithmId().getAlgorithm().getId(), provider);
        return localKeyFactory.generatePrivate(localPKCS8EncodedKeySpec);
      }
      catch (Exception localException)
      {
        throw new PEMException("problem parsing PRIVATE KEY: " + localException.toString(), localException);
      }
    }
  }
  
  private class PublicKeyParser
    implements PemObjectParser
  {
    private String provider;
    
    public PublicKeyParser(String paramString)
    {
      provider = paramString;
    }
    
    public Object parseObject(PemObject paramPemObject)
      throws IOException
    {
      X509EncodedKeySpec localX509EncodedKeySpec = new X509EncodedKeySpec(paramPemObject.getContent());
      String[] arrayOfString = { "DSA", "RSA" };
      for (int i = 0; i < arrayOfString.length; i++) {
        try
        {
          KeyFactory localKeyFactory = KeyFactory.getInstance(arrayOfString[i], provider);
          PublicKey localPublicKey = localKeyFactory.generatePublic(localX509EncodedKeySpec);
          return localPublicKey;
        }
        catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {}catch (InvalidKeySpecException localInvalidKeySpecException) {}catch (NoSuchProviderException localNoSuchProviderException)
        {
          throw new RuntimeException("can't find provider " + provider);
        }
      }
      return null;
    }
  }
  
  private class RSAKeyPairParser
    extends PEMReader.KeyPairParser
  {
    public RSAKeyPairParser(String paramString)
    {
      super(paramString);
    }
    
    public Object parseObject(PemObject paramPemObject)
      throws IOException
    {
      try
      {
        ASN1Sequence localASN1Sequence = readKeyPair(paramPemObject);
        if (localASN1Sequence.size() != 9) {
          throw new PEMException("malformed sequence in RSA private key");
        }
        RSAPrivateKey localRSAPrivateKey = RSAPrivateKey.getInstance(localASN1Sequence);
        RSAPublicKeySpec localRSAPublicKeySpec = new RSAPublicKeySpec(localRSAPrivateKey.getModulus(), localRSAPrivateKey.getPublicExponent());
        RSAPrivateCrtKeySpec localRSAPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(localRSAPrivateKey.getModulus(), localRSAPrivateKey.getPublicExponent(), localRSAPrivateKey.getPrivateExponent(), localRSAPrivateKey.getPrime1(), localRSAPrivateKey.getPrime2(), localRSAPrivateKey.getExponent1(), localRSAPrivateKey.getExponent2(), localRSAPrivateKey.getCoefficient());
        KeyFactory localKeyFactory = KeyFactory.getInstance("RSA", provider);
        return new KeyPair(localKeyFactory.generatePublic(localRSAPublicKeySpec), localKeyFactory.generatePrivate(localRSAPrivateCrtKeySpec));
      }
      catch (IOException localIOException)
      {
        throw localIOException;
      }
      catch (Exception localException)
      {
        throw new PEMException("problem creating RSA private key: " + localException.toString(), localException);
      }
    }
  }
  
  private class RSAPublicKeyParser
    implements PemObjectParser
  {
    private String provider;
    
    public RSAPublicKeyParser(String paramString)
    {
      provider = paramString;
    }
    
    public Object parseObject(PemObject paramPemObject)
      throws IOException
    {
      try
      {
        ASN1InputStream localASN1InputStream = new ASN1InputStream(paramPemObject.getContent());
        ASN1Primitive localASN1Primitive = localASN1InputStream.readObject();
        ASN1Sequence localASN1Sequence = (ASN1Sequence)localASN1Primitive;
        RSAPublicKey localRSAPublicKey = RSAPublicKey.getInstance(localASN1Sequence);
        RSAPublicKeySpec localRSAPublicKeySpec = new RSAPublicKeySpec(localRSAPublicKey.getModulus(), localRSAPublicKey.getPublicExponent());
        KeyFactory localKeyFactory = KeyFactory.getInstance("RSA", provider);
        return localKeyFactory.generatePublic(localRSAPublicKeySpec);
      }
      catch (IOException localIOException)
      {
        throw localIOException;
      }
      catch (NoSuchProviderException localNoSuchProviderException)
      {
        throw new IOException("can't find provider " + provider);
      }
      catch (Exception localException)
      {
        throw new PEMException("problem extracting key: " + localException.toString(), localException);
      }
    }
  }
  
  private class X509AttributeCertificateParser
    implements PemObjectParser
  {
    private X509AttributeCertificateParser() {}
    
    public Object parseObject(PemObject paramPemObject)
      throws IOException
    {
      return new X509V2AttributeCertificate(paramPemObject.getContent());
    }
  }
  
  private class X509CRLParser
    implements PemObjectParser
  {
    private String provider;
    
    public X509CRLParser(String paramString)
    {
      provider = paramString;
    }
    
    public Object parseObject(PemObject paramPemObject)
      throws IOException
    {
      ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(paramPemObject.getContent());
      try
      {
        CertificateFactory localCertificateFactory = CertificateFactory.getInstance("X.509", provider);
        return localCertificateFactory.generateCRL(localByteArrayInputStream);
      }
      catch (Exception localException)
      {
        throw new PEMException("problem parsing cert: " + localException.toString(), localException);
      }
    }
  }
  
  private class X509CertificateParser
    implements PemObjectParser
  {
    private String provider;
    
    public X509CertificateParser(String paramString)
    {
      provider = paramString;
    }
    
    public Object parseObject(PemObject paramPemObject)
      throws IOException
    {
      ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(paramPemObject.getContent());
      try
      {
        CertificateFactory localCertificateFactory = CertificateFactory.getInstance("X.509", provider);
        return localCertificateFactory.generateCertificate(localByteArrayInputStream);
      }
      catch (Exception localException)
      {
        throw new PEMException("problem parsing cert: " + localException.toString(), localException);
      }
    }
  }
}

/* Location:
 * Qualified Name:     org.bouncycastle.openssl.PEMReader
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.bouncycastle.openssl;

import java.io.IOException;
import java.security.Provider;
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.RC2ParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.DERObjectIdentifier;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.crypto.PBEParametersGenerator;
import org.bouncycastle.crypto.generators.OpenSSLPBEParametersGenerator;
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator;
import org.bouncycastle.crypto.params.KeyParameter;

final class PEMUtilities
{
  private static final Map KEYSIZES = new HashMap();
  private static final Set PKCS5_SCHEME_1 = new HashSet();
  private static final Set PKCS5_SCHEME_2 = new HashSet();
  
  static int getKeySize(String paramString)
  {
    if (!KEYSIZES.containsKey(paramString)) {
      throw new IllegalStateException("no key size for algorithm: " + paramString);
    }
    return ((Integer)KEYSIZES.get(paramString)).intValue();
  }
  
  static boolean isPKCS5Scheme1(DERObjectIdentifier paramDERObjectIdentifier)
  {
    return PKCS5_SCHEME_1.contains(paramDERObjectIdentifier);
  }
  
  static boolean isPKCS5Scheme2(ASN1ObjectIdentifier paramASN1ObjectIdentifier)
  {
    return PKCS5_SCHEME_2.contains(paramASN1ObjectIdentifier);
  }
  
  static boolean isPKCS12(DERObjectIdentifier paramDERObjectIdentifier)
  {
    return paramDERObjectIdentifier.getId().startsWith(PKCSObjectIdentifiers.pkcs_12PbeIds.getId());
  }
  
  static SecretKey generateSecretKeyForPKCS5Scheme2(String paramString, char[] paramArrayOfChar, byte[] paramArrayOfByte, int paramInt)
  {
    PKCS5S2ParametersGenerator localPKCS5S2ParametersGenerator = new PKCS5S2ParametersGenerator();
    localPKCS5S2ParametersGenerator.init(PBEParametersGenerator.PKCS5PasswordToBytes(paramArrayOfChar), paramArrayOfByte, paramInt);
    return new SecretKeySpec(((KeyParameter)localPKCS5S2ParametersGenerator.generateDerivedParameters(getKeySize(paramString))).getKey(), paramString);
  }
  
  static byte[] crypt(boolean paramBoolean, String paramString1, byte[] paramArrayOfByte1, char[] paramArrayOfChar, String paramString2, byte[] paramArrayOfByte2)
    throws IOException
  {
    Provider localProvider = null;
    if (paramString1 != null)
    {
      localProvider = Security.getProvider(paramString1);
      if (localProvider == null) {
        throw new EncryptionException("cannot find provider: " + paramString1);
      }
    }
    return crypt(paramBoolean, localProvider, paramArrayOfByte1, paramArrayOfChar, paramString2, paramArrayOfByte2);
  }
  
  static byte[] crypt(boolean paramBoolean, Provider paramProvider, byte[] paramArrayOfByte1, char[] paramArrayOfChar, String paramString, byte[] paramArrayOfByte2)
    throws IOException
  {
    Object localObject1 = new IvParameterSpec(paramArrayOfByte2);
    String str2 = "CBC";
    String str3 = "PKCS5Padding";
    if (paramString.endsWith("-CFB"))
    {
      str2 = "CFB";
      str3 = "NoPadding";
    }
    if ((paramString.endsWith("-ECB")) || ("DES-EDE".equals(paramString)) || ("DES-EDE3".equals(paramString)))
    {
      str2 = "ECB";
      localObject1 = null;
    }
    if (paramString.endsWith("-OFB"))
    {
      str2 = "OFB";
      str3 = "NoPadding";
    }
    String str1;
    SecretKey localSecretKey;
    if (paramString.startsWith("DES-EDE"))
    {
      str1 = "DESede";
      boolean bool = !paramString.startsWith("DES-EDE3");
      localSecretKey = getKey(paramArrayOfChar, str1, 24, paramArrayOfByte2, bool);
    }
    else if (paramString.startsWith("DES-"))
    {
      str1 = "DES";
      localSecretKey = getKey(paramArrayOfChar, str1, 8, paramArrayOfByte2);
    }
    else if (paramString.startsWith("BF-"))
    {
      str1 = "Blowfish";
      localSecretKey = getKey(paramArrayOfChar, str1, 16, paramArrayOfByte2);
    }
    else if (paramString.startsWith("RC2-"))
    {
      str1 = "RC2";
      int i = 128;
      if (paramString.startsWith("RC2-40-")) {
        i = 40;
      } else if (paramString.startsWith("RC2-64-")) {
        i = 64;
      }
      localSecretKey = getKey(paramArrayOfChar, str1, i / 8, paramArrayOfByte2);
      if (localObject1 == null) {
        localObject1 = new RC2ParameterSpec(i);
      } else {
        localObject1 = new RC2ParameterSpec(i, paramArrayOfByte2);
      }
    }
    else if (paramString.startsWith("AES-"))
    {
      str1 = "AES";
      localObject2 = paramArrayOfByte2;
      if (localObject2.length > 8)
      {
        localObject2 = new byte[8];
        System.arraycopy(paramArrayOfByte2, 0, localObject2, 0, 8);
      }
      int j;
      if (paramString.startsWith("AES-128-")) {
        j = 128;
      } else if (paramString.startsWith("AES-192-")) {
        j = 192;
      } else if (paramString.startsWith("AES-256-")) {
        j = 256;
      } else {
        throw new EncryptionException("unknown AES encryption with private key");
      }
      localSecretKey = getKey(paramArrayOfChar, "AES", j / 8, (byte[])localObject2);
    }
    else
    {
      throw new EncryptionException("unknown encryption with private key");
    }
    Object localObject2 = str1 + "/" + str2 + "/" + str3;
    try
    {
      Cipher localCipher = Cipher.getInstance((String)localObject2, paramProvider);
      int k = paramBoolean ? 1 : 2;
      if (localObject1 == null) {
        localCipher.init(k, localSecretKey);
      } else {
        localCipher.init(k, localSecretKey, (AlgorithmParameterSpec)localObject1);
      }
      return localCipher.doFinal(paramArrayOfByte1);
    }
    catch (Exception localException)
    {
      throw new EncryptionException("exception using cipher - please check password and data.", localException);
    }
  }
  
  private static SecretKey getKey(char[] paramArrayOfChar, String paramString, int paramInt, byte[] paramArrayOfByte)
  {
    return getKey(paramArrayOfChar, paramString, paramInt, paramArrayOfByte, false);
  }
  
  private static SecretKey getKey(char[] paramArrayOfChar, String paramString, int paramInt, byte[] paramArrayOfByte, boolean paramBoolean)
  {
    OpenSSLPBEParametersGenerator localOpenSSLPBEParametersGenerator = new OpenSSLPBEParametersGenerator();
    localOpenSSLPBEParametersGenerator.init(PBEParametersGenerator.PKCS5PasswordToBytes(paramArrayOfChar), paramArrayOfByte);
    KeyParameter localKeyParameter = (KeyParameter)localOpenSSLPBEParametersGenerator.generateDerivedParameters(paramInt * 8);
    byte[] arrayOfByte = localKeyParameter.getKey();
    if ((paramBoolean) && (arrayOfByte.length >= 24)) {
      System.arraycopy(arrayOfByte, 0, arrayOfByte, 16, 8);
    }
    return new SecretKeySpec(arrayOfByte, paramString);
  }
  
  static
  {
    PKCS5_SCHEME_1.add(PKCSObjectIdentifiers.pbeWithMD2AndDES_CBC);
    PKCS5_SCHEME_1.add(PKCSObjectIdentifiers.pbeWithMD2AndRC2_CBC);
    PKCS5_SCHEME_1.add(PKCSObjectIdentifiers.pbeWithMD5AndDES_CBC);
    PKCS5_SCHEME_1.add(PKCSObjectIdentifiers.pbeWithMD5AndRC2_CBC);
    PKCS5_SCHEME_1.add(PKCSObjectIdentifiers.pbeWithSHA1AndDES_CBC);
    PKCS5_SCHEME_1.add(PKCSObjectIdentifiers.pbeWithSHA1AndRC2_CBC);
    PKCS5_SCHEME_2.add(PKCSObjectIdentifiers.id_PBES2);
    PKCS5_SCHEME_2.add(PKCSObjectIdentifiers.des_EDE3_CBC);
    PKCS5_SCHEME_2.add(NISTObjectIdentifiers.id_aes128_CBC);
    PKCS5_SCHEME_2.add(NISTObjectIdentifiers.id_aes192_CBC);
    PKCS5_SCHEME_2.add(NISTObjectIdentifiers.id_aes256_CBC);
    KEYSIZES.put(PKCSObjectIdentifiers.des_EDE3_CBC.getId(), new Integer(192));
    KEYSIZES.put(NISTObjectIdentifiers.id_aes128_CBC.getId(), new Integer(128));
    KEYSIZES.put(NISTObjectIdentifiers.id_aes192_CBC.getId(), new Integer(192));
    KEYSIZES.put(NISTObjectIdentifiers.id_aes256_CBC.getId(), new Integer(256));
  }
}

/* Location:
 * Qualified Name:     org.bouncycastle.openssl.PEMUtilities
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.bouncycastle.openssl;

import java.io.IOException;
import java.io.Writer;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import org.bouncycastle.util.io.pem.PemGenerationException;
import org.bouncycastle.util.io.pem.PemObjectGenerator;
import org.bouncycastle.util.io.pem.PemWriter;

public class PEMWriter
  extends PemWriter
{
  private String provider;
  
  public PEMWriter(Writer paramWriter)
  {
    this(paramWriter, "BC");
  }
  
  public PEMWriter(Writer paramWriter, String paramString)
  {
    super(paramWriter);
    provider = paramString;
  }
  
  public void writeObject(Object paramObject)
    throws IOException
  {
    try
    {
      super.writeObject(new MiscPEMGenerator(paramObject));
    }
    catch (PemGenerationException localPemGenerationException)
    {
      if ((localPemGenerationException.getCause() instanceof IOException)) {
        throw ((IOException)localPemGenerationException.getCause());
      }
      throw localPemGenerationException;
    }
  }
  
  public void writeObject(PemObjectGenerator paramPemObjectGenerator)
    throws IOException
  {
    super.writeObject(paramPemObjectGenerator);
  }
  
  public void writeObject(Object paramObject, String paramString, char[] paramArrayOfChar, SecureRandom paramSecureRandom)
    throws IOException
  {
    try
    {
      super.writeObject(new MiscPEMGenerator(paramObject, paramString, paramArrayOfChar, paramSecureRandom, provider));
    }
    catch (NoSuchProviderException localNoSuchProviderException)
    {
      throw new EncryptionException(localNoSuchProviderException.getMessage(), localNoSuchProviderException);
    }
  }
}

/* Location:
 * Qualified Name:     org.bouncycastle.openssl.PEMWriter
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.bouncycastle.openssl;

import java.io.IOException;
import java.security.AlgorithmParameterGenerator;
import java.security.AlgorithmParameters;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo;
import org.bouncycastle.asn1.pkcs.EncryptionScheme;
import org.bouncycastle.asn1.pkcs.KeyDerivationFunc;
import org.bouncycastle.asn1.pkcs.PBES2Parameters;
import org.bouncycastle.asn1.pkcs.PBKDF2Params;
import org.bouncycastle.asn1.pkcs.PKCS12PBEParams;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.util.io.pem.PemGenerationException;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemObjectGenerator;

public class PKCS8Generator
  implements PemObjectGenerator
{
  public static final String AES_128_CBC = NISTObjectIdentifiers.id_aes128_CBC.getId();
  public static final String AES_192_CBC = NISTObjectIdentifiers.id_aes192_CBC.getId();
  public static final String AES_256_CBC = NISTObjectIdentifiers.id_aes256_CBC.getId();
  public static final String DES3_CBC = PKCSObjectIdentifiers.des_EDE3_CBC.getId();
  public static final String PBE_SHA1_RC4_128 = PKCSObjectIdentifiers.pbeWithSHAAnd128BitRC4.getId();
  public static final String PBE_SHA1_RC4_40 = PKCSObjectIdentifiers.pbeWithSHAAnd40BitRC4.getId();
  public static final String PBE_SHA1_3DES = PKCSObjectIdentifiers.pbeWithSHAAnd3_KeyTripleDES_CBC.getId();
  public static final String PBE_SHA1_2DES = PKCSObjectIdentifiers.pbeWithSHAAnd2_KeyTripleDES_CBC.getId();
  public static final String PBE_SHA1_RC2_128 = PKCSObjectIdentifiers.pbeWithSHAAnd128BitRC2_CBC.getId();
  public static final String PBE_SHA1_RC2_40 = PKCSObjectIdentifiers.pbeWithSHAAnd40BitRC2_CBC.getId();
  private char[] password;
  private String algorithm;
  private int iterationCount;
  private PrivateKey key;
  private Cipher cipher;
  private SecureRandom random;
  private AlgorithmParameterGenerator paramGen;
  private SecretKeyFactory secKeyFact;
  
  public PKCS8Generator(PrivateKey paramPrivateKey)
  {
    key = paramPrivateKey;
  }
  
  public PKCS8Generator(PrivateKey paramPrivateKey, String paramString1, String paramString2)
    throws NoSuchProviderException, NoSuchAlgorithmException
  {
    Provider localProvider = Security.getProvider(paramString2);
    if (localProvider == null) {
      throw new NoSuchProviderException("cannot find provider: " + paramString2);
    }
    init(paramPrivateKey, paramString1, localProvider);
  }
  
  public PKCS8Generator(PrivateKey paramPrivateKey, String paramString, Provider paramProvider)
    throws NoSuchAlgorithmException
  {
    init(paramPrivateKey, paramString, paramProvider);
  }
  
  private void init(PrivateKey paramPrivateKey, String paramString, Provider paramProvider)
    throws NoSuchAlgorithmException
  {
    key = p
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd