Program: 5305
Project: 5305
Buggy source:
/** 
 * Generate an X.509 certificate
 * @param dn Distinguished name for the owner of the certificate, it will also be the signer of the certificate.
 * @param pair Key pair used for signing the certificate.
 * @param days Validity of the certificate.
 * @param algorithm Name of the signature algorithm used.
 * @return A X.509 certificate
 */
private static X509Certificate getCertificate(String dn,KeyPair pair,int days,String algorithm) throws IOException, CertificateException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
  Date from=new Date();
  Date to=DateUtils.addDays(from,days);
  CertificateValidity interval=new CertificateValidity(from,to);
  BigInteger sn=new BigInteger(64,new SecureRandom());
  X500Name owner=new X500Name(dn);
  X509CertInfo info=new X509CertInfo();
  info.set(X509CertInfo.VALIDITY,interval);
  info.set(X509CertInfo.SERIAL_NUMBER,new CertificateSerialNumber(sn));
  info.set(X509CertInfo.SUBJECT,new CertificateSubjectName(owner));
  info.set(X509CertInfo.ISSUER,new CertificateIssuerName(owner));
  info.set(X509CertInfo.KEY,new CertificateX509Key(pair.getPublic()));
  info.set(X509CertInfo.VERSION,new CertificateVersion(CertificateVersion.V3));
  AlgorithmId algo=new AlgorithmId(AlgorithmId.md5WithRSAEncryption_oid);
  info.set(X509CertInfo.ALGORITHM_ID,new CertificateAlgorithmId(algo));
  X509CertImpl cert=new X509CertImpl(info);
  PrivateKey privateKey=pair.getPrivate();
  cert.sign(privateKey,algorithm);
  return cert;
}

Fixed version (ground truth):
/** 
 * Generate an X.509 certificate
 * @param dn Distinguished name for the owner of the certificate, it will also be the signer of the certificate.
 * @param pair Key pair used for signing the certificate.
 * @param days Validity of the certificate.
 * @param algorithm Name of the signature algorithm used.
 * @return A X.509 certificate
 */
private static X509Certificate getCertificate(String dn,KeyPair pair,int days,String algorithm) throws IOException, CertificateException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
  Date from=new Date();
  Date to=DateUtils.addDays(from,days);
  CertificateValidity interval=new CertificateValidity(from,to);
  BigInteger sn=new BigInteger(64,new SecureRandom());
  X500Name owner=new X500Name(dn);
  X509CertInfo info=new X509CertInfo();
  info.set(X509CertInfo.VALIDITY,interval);
  info.set(X509CertInfo.SERIAL_NUMBER,new CertificateSerialNumber(sn));
  Field subjectField=null;
  try {
    subjectField=info.getClass().getDeclaredField("String_Node_Str");
    if (subjectField.getType().equals(X500Name.class)) {
      info.set(X509CertInfo.SUBJECT,owner);
      info.set(X509CertInfo.ISSUER,owner);
    }
 else {
      info.set(X509CertInfo.SUBJECT,new CertificateSubjectName(owner));
      info.set(X509CertInfo.ISSUER,new CertificateIssuerName(owner));
    }
  }
 catch (  NoSuchFieldException e) {
    info.set(X509CertInfo.SUBJECT,owner);
    info.set(X509CertInfo.ISSUER,owner);
  }
  info.set(X509CertInfo.KEY,new CertificateX509Key(pair.getPublic()));
  info.set(X509CertInfo.VERSION,new CertificateVersion(CertificateVersion.V3));
  AlgorithmId algo=new AlgorithmId(AlgorithmId.md5WithRSAEncryption_oid);
  info.set(X509CertInfo.ALGORITHM_ID,new CertificateAlgorithmId(algo));
  X509CertImpl cert=new X509CertImpl(info);
  PrivateKey privateKey=pair.getPrivate();
  cert.sign(privateKey,algorithm);
  return cert;
}