Program: 80248
Project: 80248
Buggy source:
/** 
 * M&eacute;todo que genera la parte que contiene la informaci&oacute;n del Usuario. Se generan los atributos que se necesitan para generar la firma.
 * @param cert Certificado de firma.
 * @param digestAlgorithmName Algoritmo Firmado.
 * @param digestAlgorithmOID
 * @param datos Datos firmados.
 * @param policyIdentifier
 * @param policyQualifier
 * @param signingCertificateV2
 * @param dataType Identifica el tipo del contenido a firmar.
 * @param messageDigest
 * @return Los datos necesarios para generar la firma referente a los datosdel usuario.
 * @throws java.security.NoSuchAlgorithmException
 * @throws java.io.IOException
 * @throws CertificateEncodingException 
 */
static ASN1EncodableVector generateSignerInfo(final X509Certificate cert,final String digestAlgorithmName,final byte[] datos,final String policyIdentifier,final String policyQualifier,final boolean signingCertificateV2,final byte[] messageDigest) throws NoSuchAlgorithmException, IOException, CertificateEncodingException {
  final AlgorithmIdentifier digestAlgorithmOID;
  try {
    digestAlgorithmOID=SigUtils.makeAlgId(AOAlgorithmID.getOID(digestAlgorithmName));
  }
 catch (  final Exception e) {
    throw new IOException("String_Node_Str" + e);
  }
  final ASN1EncodableVector ContexExpecific=initContexExpecific(digestAlgorithmName,datos,PKCSObjectIdentifiers.data.getId(),messageDigest);
  ContexExpecific.add(new Attribute(RFC4519Style.serialNumber,new DERSet(new DERPrintableString(cert.getSerialNumber().toString()))));
  if (signingCertificateV2) {
    final TBSCertificateStructure tbs=TBSCertificateStructure.getInstance(ASN1Object.fromByteArray(cert.getTBSCertificate()));
    final GeneralName gn=new GeneralName(tbs.getIssuer());
    final GeneralNames gns=new GeneralNames(gn);
    final IssuerSerial isuerSerial=new IssuerSerial(gns,tbs.getSerialNumber());
    final MessageDigest md=MessageDigest.getInstance(digestAlgorithmName);
    final byte[] certHash=md.digest(cert.getEncoded());
    final ESSCertIDv2[] essCertIDv2={new ESSCertIDv2(digestAlgorithmOID,certHash,isuerSerial)};
    PolicyInformation[] pI;
    SigningCertificateV2 scv2=null;
    if (policyQualifier != null) {
      final DERObjectIdentifier oidQualifier=new DERObjectIdentifier(policyQualifier.toString());
      if (policyIdentifier.equals("String_Node_Str")) {
        pI=new PolicyInformation[]{new PolicyInformation(oidQualifier)};
      }
 else {
        final PolicyQualifierInfo pqInfo=new PolicyQualifierInfo(policyIdentifier);
        pI=new PolicyInformation[]{new PolicyInformation(oidQualifier,new DERSequence(pqInfo))};
      }
      scv2=new SigningCertificateV2(essCertIDv2,pI);
    }
 else {
      scv2=new SigningCertificateV2(essCertIDv2);
    }
    ContexExpecific.add(new Attribute(PKCSObjectIdentifiers.id_aa_signingCertificateV2,new DERSet(scv2)));
  }
 else {
    final TBSCertificateStructure tbs=TBSCertificateStructure.getInstance(ASN1Object.fromByteArray(cert.getTBSCertificate()));
    final GeneralName gn=new GeneralName(tbs.getIssuer());
    final GeneralNames gns=new GeneralNames(gn);
    final IssuerSerial isuerSerial=new IssuerSerial(gns,tbs.getSerialNumber());
    final MessageDigest md=MessageDigest.getInstance(digestAlgorithmName);
    final byte[] certHash=md.digest(cert.getEncoded());
    final ESSCertID essCertID=new ESSCertID(certHash,isuerSerial);
    PolicyInformation[] pI;
    SigningCertificate scv=null;
    if (policyQualifier != null) {
      final DERObjectIdentifier oidQualifier=new DERObjectIdentifier(policyQualifier.toString());
      if (policyIdentifier.equals("String_Node_Str")) {
        pI=new PolicyInformation[]{new PolicyInformation(oidQualifier)};
      }
 else {
        final PolicyQualifierInfo pqInfo=new PolicyQualifierInfo(policyIdentifier);
        pI=new PolicyInformation[]{new PolicyInformation(oidQualifier,new DERSequence(pqInfo))};
      }
      final ASN1EncodableVector v=new ASN1EncodableVector();
      v.add(new DERSequence(essCertID));
      v.add(new DERSequence(pI));
      scv=new SigningCertificate(new DERSequence(v));
    }
 else {
      scv=new SigningCertificate(essCertID);
    }
    ContexExpecific.add(new Attribute(PKCSObjectIdentifiers.id_aa_signingCertificate,new DERSet(scv)));
  }
  if (policyQualifier != null) {
    final DERObjectIdentifier DOISigPolicyId=new DERObjectIdentifier(policyQualifier.toString());
    final MessageDigest mdgest=MessageDigest.getInstance(digestAlgorithmName);
    final byte[] hashed=mdgest.digest(policyIdentifier.getBytes());
    final DigestInfo OtherHashAlgAndValue=new DigestInfo(digestAlgorithmOID,hashed);
    final SigPolicyQualifierInfo spqInfo=new SigPolicyQualifierInfo(policyIdentifier);
    final ASN1EncodableVector v=new ASN1EncodableVector();
    v.add(DOISigPolicyId);
    v.add(OtherHashAlgAndValue.toASN1Object());
    v.add(spqInfo.toASN1Object());
    final DERSequence ds=new DERSequence(v);
    ContexExpecific.add(new Attribute(PKCSObjectIdentifiers.id_aa_ets_sigPolicyId,new DERSet(ds.toASN1Object())));
  }
  return ContexExpecific;
}

Fixed version (ground truth):
/** 
 * M&eacute;todo que genera la parte que contiene la informaci&oacute;n del Usuario. Se generan los atributos que se necesitan para generar la firma.
 * @param cert Certificado de firma.
 * @param digestAlgorithmName Algoritmo Firmado.
 * @param digestAlgorithmOID
 * @param datos Datos firmados.
 * @param policyIdentifier
 * @param policyQualifier
 * @param signingCertificateV2
 * @param dataType Identifica el tipo del contenido a firmar.
 * @param messageDigest
 * @return Los datos necesarios para generar la firma referente a los datosdel usuario.
 * @throws java.security.NoSuchAlgorithmException
 * @throws java.io.IOException
 * @throws CertificateEncodingException 
 */
static ASN1EncodableVector generateSignerInfo(final X509Certificate cert,final String digestAlgorithmName,final byte[] datos,final String policyIdentifier,final String policyQualifier,final boolean signingCertificateV2,final byte[] messageDigest) throws NoSuchAlgorithmException, IOException, CertificateEncodingException {
  final AlgorithmIdentifier digestAlgorithmOID;
  try {
    digestAlgorithmOID=SigUtils.makeAlgId(AOAlgorithmID.getOID(digestAlgorithmName));
  }
 catch (  final Exception e) {
    throw new IOException("String_Node_Str" + e);
  }
  final ASN1EncodableVector contexExpecific=initContexExpecific(digestAlgorithmName,datos,PKCSObjectIdentifiers.data.getId(),messageDigest);
  contexExpecific.add(new Attribute(RFC4519Style.serialNumber,new DERSet(new DERPrintableString(cert.getSerialNumber().toString()))));
  if (signingCertificateV2) {
    final TBSCertificateStructure tbs=TBSCertificateStructure.getInstance(ASN1Object.fromByteArray(cert.getTBSCertificate()));
    final GeneralName gn=new GeneralName(tbs.getIssuer());
    final GeneralNames gns=new GeneralNames(gn);
    final IssuerSerial isuerSerial=new IssuerSerial(gns,tbs.getSerialNumber());
    final MessageDigest md=MessageDigest.getInstance(digestAlgorithmName);
    final byte[] certHash=md.digest(cert.getEncoded());
    final ESSCertIDv2[] essCertIDv2={new ESSCertIDv2(digestAlgorithmOID,certHash,isuerSerial)};
    PolicyInformation[] pI;
    SigningCertificateV2 scv2=null;
    if (policyQualifier != null) {
      final DERObjectIdentifier oidQualifier=new DERObjectIdentifier(policyQualifier.toString());
      if (policyIdentifier.equals("String_Node_Str")) {
        pI=new PolicyInformation[]{new PolicyInformation(oidQualifier)};
      }
 else {
        final PolicyQualifierInfo pqInfo=new PolicyQualifierInfo(policyIdentifier);
        pI=new PolicyInformation[]{new PolicyInformation(oidQualifier,new DERSequence(pqInfo))};
      }
      scv2=new SigningCertificateV2(essCertIDv2,pI);
    }
 else {
      scv2=new SigningCertificateV2(essCertIDv2);
    }
    contexExpecific.add(new Attribute(PKCSObjectIdentifiers.id_aa_signingCertificateV2,new DERSet(scv2)));
  }
 else {
    final TBSCertificateStructure tbs=TBSCertificateStructure.getInstance(ASN1Object.fromByteArray(cert.getTBSCertificate()));
    final GeneralName gn=new GeneralName(tbs.getIssuer());
    final GeneralNames gns=new GeneralNames(gn);
    final IssuerSerial isuerSerial=new IssuerSerial(gns,tbs.getSerialNumber());
    final MessageDigest md=MessageDigest.getInstance(digestAlgorithmName);
    final byte[] certHash=md.digest(cert.getEncoded());
    final ESSCertID essCertID=new ESSCertID(certHash,isuerSerial);
    PolicyInformation[] pI;
    SigningCertificate scv=null;
    if (policyQualifier != null) {
      final DERObjectIdentifier oidQualifier=new DERObjectIdentifier(policyQualifier.toString());
      if (policyIdentifier.equals("String_Node_Str")) {
        pI=new PolicyInformation[]{new PolicyInformation(oidQualifier)};
      }
 else {
        final PolicyQualifierInfo pqInfo=new PolicyQualifierInfo(policyIdentifier);
        pI=new PolicyInformation[]{new PolicyInformation(oidQualifier,new DERSequence(pqInfo))};
      }
      final ASN1EncodableVector v=new ASN1EncodableVector();
      v.add(new DERSequence(essCertID));
      v.add(new DERSequence(pI));
      scv=new SigningCertificate(new DERSequence(v));
    }
 else {
      scv=new SigningCertificate(essCertID);
    }
    contexExpecific.add(new Attribute(PKCSObjectIdentifiers.id_aa_signingCertificate,new DERSet(scv)));
  }
  if (policyQualifier != null) {
    final DERObjectIdentifier DOISigPolicyId=new DERObjectIdentifier(policyQualifier.toString());
    final MessageDigest mdgest=MessageDigest.getInstance(digestAlgorithmName);
    final byte[] hashed=mdgest.digest(policyIdentifier.getBytes());
    final DigestInfo OtherHashAlgAndValue=new DigestInfo(digestAlgorithmOID,hashed);
    final SigPolicyQualifierInfo spqInfo=new SigPolicyQualifierInfo(policyIdentifier);
    final ASN1EncodableVector v=new ASN1EncodableVector();
    v.add(DOISigPolicyId);
    v.add(OtherHashAlgAndValue.toASN1Object());
    v.add(spqInfo.toASN1Object());
    final DERSequence ds=new DERSequence(v);
    contexExpecific.add(new Attribute(PKCSObjectIdentifiers.id_aa_ets_sigPolicyId,new DERSet(ds.toASN1Object())));
  }
  return contexExpecific;
}