Program: 52370
Project: 52370
Buggy source:
public PrivateKey getPrivateKey(PGPPrivateKey privKey) throws PGPException {
  if (privKey instanceof JcaPGPPrivateKey) {
    return ((JcaPGPPrivateKey)privKey).getPrivateKey();
  }
  PublicKeyPacket pubPk=privKey.getPublicKeyPacket();
  BCPGKey privPk=privKey.getPrivateKeyDataPacket();
  try {
    KeyFactory fact;
switch (pubPk.getAlgorithm()) {
case PGPPublicKey.RSA_ENCRYPT:
case PGPPublicKey.RSA_GENERAL:
case PGPPublicKey.RSA_SIGN:
      RSAPublicBCPGKey rsaPub=(RSAPublicBCPGKey)pubPk.getKey();
    RSASecretBCPGKey rsaPriv=(RSASecretBCPGKey)privPk;
  RSAPrivateCrtKeySpec rsaPrivSpec=new RSAPrivateCrtKeySpec(rsaPriv.getModulus(),rsaPub.getPublicExponent(),rsaPriv.getPrivateExponent(),rsaPriv.getPrimeP(),rsaPriv.getPrimeQ(),rsaPriv.getPrimeExponentP(),rsaPriv.getPrimeExponentQ(),rsaPriv.getCrtCoefficient());
fact=helper.createKeyFactory("String_Node_Str");
return fact.generatePrivate(rsaPrivSpec);
case PGPPublicKey.DSA:
DSAPublicBCPGKey dsaPub=(DSAPublicBCPGKey)pubPk.getKey();
DSASecretBCPGKey dsaPriv=(DSASecretBCPGKey)privPk;
DSAPrivateKeySpec dsaPrivSpec=new DSAPrivateKeySpec(dsaPriv.getX(),dsaPub.getP(),dsaPub.getQ(),dsaPub.getG());
fact=helper.createKeyFactory("String_Node_Str");
return fact.generatePrivate(dsaPrivSpec);
case PublicKeyAlgorithmTags.ECDH:
ECDHPublicBCPGKey ecdhPub=(ECDHPublicBCPGKey)pubPk.getKey();
ECSecretBCPGKey ecdhK=(ECSecretBCPGKey)privPk;
ECPrivateKeySpec ecDhSpec=new ECPrivateKeySpec(ecdhK.getX(),convertX9Parameters(ecdhPub.getCurveOID(),NISTNamedCurves.getByOID(ecdhPub.getCurveOID())));
fact=helper.createKeyFactory("String_Node_Str");
return fact.generatePrivate(ecDhSpec);
case PublicKeyAlgorithmTags.ECDSA:
ECDSAPublicBCPGKey ecdsaPub=(ECDSAPublicBCPGKey)pubPk.getKey();
ECSecretBCPGKey ecdsaK=(ECSecretBCPGKey)privPk;
ECPrivateKeySpec ecDsaSpec=new ECPrivateKeySpec(ecdsaK.getX(),convertX9Parameters(ecdsaPub.getCurveOID(),NISTNamedCurves.getByOID(ecdsaPub.getCurveOID())));
fact=helper.createKeyFactory("String_Node_Str");
return fact.generatePrivate(ecDsaSpec);
case PGPPublicKey.ELGAMAL_ENCRYPT:
case PGPPublicKey.ELGAMAL_GENERAL:
ElGamalPublicBCPGKey elPub=(ElGamalPublicBCPGKey)pubPk.getKey();
ElGamalSecretBCPGKey elPriv=(ElGamalSecretBCPGKey)privPk;
ElGamalPrivateKeySpec elSpec=new ElGamalPrivateKeySpec(elPriv.getX(),new ElGamalParameterSpec(elPub.getP(),elPub.getG()));
fact=helper.createKeyFactory("String_Node_Str");
return fact.generatePrivate(elSpec);
default :
throw new PGPException("String_Node_Str");
}
}
 catch (PGPException e) {
throw e;
}
catch (Exception e) {
throw new PGPException("String_Node_Str",e);
}
}

Fixed version (ground truth):
public PrivateKey getPrivateKey(PGPPrivateKey privKey) throws PGPException {
  if (privKey instanceof JcaPGPPrivateKey) {
    return ((JcaPGPPrivateKey)privKey).getPrivateKey();
  }
  PublicKeyPacket pubPk=privKey.getPublicKeyPacket();
  BCPGKey privPk=privKey.getPrivateKeyDataPacket();
  try {
    KeyFactory fact;
switch (pubPk.getAlgorithm()) {
case PGPPublicKey.RSA_ENCRYPT:
case PGPPublicKey.RSA_GENERAL:
case PGPPublicKey.RSA_SIGN:
      RSAPublicBCPGKey rsaPub=(RSAPublicBCPGKey)pubPk.getKey();
    RSASecretBCPGKey rsaPriv=(RSASecretBCPGKey)privPk;
  RSAPrivateCrtKeySpec rsaPrivSpec=new RSAPrivateCrtKeySpec(rsaPriv.getModulus(),rsaPub.getPublicExponent(),rsaPriv.getPrivateExponent(),rsaPriv.getPrimeP(),rsaPriv.getPrimeQ(),rsaPriv.getPrimeExponentP(),rsaPriv.getPrimeExponentQ(),rsaPriv.getCrtCoefficient());
fact=helper.createKeyFactory("String_Node_Str");
return fact.generatePrivate(rsaPrivSpec);
case PGPPublicKey.DSA:
DSAPublicBCPGKey dsaPub=(DSAPublicBCPGKey)pubPk.getKey();
DSASecretBCPGKey dsaPriv=(DSASecretBCPGKey)privPk;
DSAPrivateKeySpec dsaPrivSpec=new DSAPrivateKeySpec(dsaPriv.getX(),dsaPub.getP(),dsaPub.getQ(),dsaPub.getG());
fact=helper.createKeyFactory("String_Node_Str");
return fact.generatePrivate(dsaPrivSpec);
case PublicKeyAlgorithmTags.ECDH:
ECDHPublicBCPGKey ecdhPub=(ECDHPublicBCPGKey)pubPk.getKey();
ECSecretBCPGKey ecdhK=(ECSecretBCPGKey)privPk;
ECPrivateKeySpec ecDhSpec=new ECPrivateKeySpec(ecdhK.getX(),convertX9Parameters(ecdhPub.getCurveOID(),ECNamedCurveTable.getByOID(ecdhPub.getCurveOID())));
fact=helper.createKeyFactory("String_Node_Str");
return fact.generatePrivate(ecDhSpec);
case PublicKeyAlgorithmTags.ECDSA:
ECDSAPublicBCPGKey ecdsaPub=(ECDSAPublicBCPGKey)pubPk.getKey();
ECSecretBCPGKey ecdsaK=(ECSecretBCPGKey)privPk;
ECPrivateKeySpec ecDsaSpec=new ECPrivateKeySpec(ecdsaK.getX(),convertX9Parameters(ecdsaPub.getCurveOID(),ECNamedCurveTable.getByOID(ecdsaPub.getCurveOID())));
fact=helper.createKeyFactory("String_Node_Str");
return fact.generatePrivate(ecDsaSpec);
case PGPPublicKey.ELGAMAL_ENCRYPT:
case PGPPublicKey.ELGAMAL_GENERAL:
ElGamalPublicBCPGKey elPub=(ElGamalPublicBCPGKey)pubPk.getKey();
ElGamalSecretBCPGKey elPriv=(ElGamalSecretBCPGKey)privPk;
ElGamalPrivateKeySpec elSpec=new ElGamalPrivateKeySpec(elPriv.getX(),new ElGamalParameterSpec(elPub.getP(),elPub.getG()));
fact=helper.createKeyFactory("String_Node_Str");
return fact.generatePrivate(elSpec);
default :
throw new PGPException("String_Node_Str");
}
}
 catch (PGPException e) {
throw e;
}
catch (Exception e) {
throw new PGPException("String_Node_Str",e);
}
}