Example usage for org.bouncycastle.asn1.ess ContentHints toASN1Primitive

List of usage examples for org.bouncycastle.asn1.ess ContentHints toASN1Primitive

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.ess ContentHints toASN1Primitive.

Prototype

public ASN1Primitive toASN1Primitive() 

Source Link

Document

 ContentHints ::= SEQUENCE { contentDescription UTF8String (SIZE (1..MAX)) OPTIONAL, contentType ContentType } 

Usage

From source file:es.gob.afirma.signers.cades.CAdESUtils.java

License:Open Source License

/** Genera la parte que contiene la información del Usuario.
 * Se generan los atributos que se necesitan para generar la firma.
 *
 * <pre>/*w  w w  .j ava  2  s  .c om*/
 * SignerInfo ::= SEQUENCE {
 *   version CMSVersion,
 *   sid SignerIdentifier,
 *   digestAlgorithm DigestAlgorithmIdentifier,
 *   signedAttrs [0] IMPLICIT SignedAttributes OPTIONAL,
 *   signatureAlgorithm SignatureAlgorithmIdentifier,
 *   signature SignatureValue,
 *   unsignedAttrs [1] IMPLICIT UnsignedAttributes OPTIONAL
 * }
 *
 * SignerIdentifier ::= CHOICE {
 *   issuerAndSerialNumber IssuerAndSerialNumber,
 *   subjectKeyIdentifier [0] SubjectKeyIdentifier
 * }
 *
 * SignedAttributes ::= SET SIZE (1..MAX) OF Attribute
 *
 * UnsignedAttributes ::= SET SIZE (1..MAX) OF Attribute
 *
 * Attribute ::= SEQUENCE {
 *   attrType OBJECT IDENTIFIER,
 *   attrValues SET OF AttributeValue
 * }
 *
 * AttributeValue ::= ANY
 *
 * SignatureValue ::= OCTET STRING
 * </pre>
 *
 * @param cert Certificado del firmante
 * @param digestAlgorithmName Nombre del algoritmo de huella digital a usar
 * @param data Datos firmados
 * @param policy Pol&iacute;tica de firma
 * @param signingCertificateV2 {@code true} para utilizar la versi&oacute;n 2 del campo
 *                             signingCertificate, {@code false} para utilizar la versi&oacute;n 1.
 * @param dataDigest Huella digital de los datos firmados
 * @param signDate Fecha de la firma (debe establecerse externamente para evitar desincronismos en la firma trif&aacute;sica)
 * @param padesMode <code>true</code> para generar una firma CAdES compatible PAdES, <code>false</code> para generar una firma CAdES normal
 * @param contentType Tipo de contenido definido por su OID.
 * @param contentDescription Descripci&oacute;n textual del tipo de contenido firmado.
 * @param ctis Lista de compromisos adquiridos con esta firma
 * @param csm Metadatos sobre el firmante
 * @return Los datos necesarios para generar la firma referente a los datos del usuario.
 * @throws java.security.NoSuchAlgorithmException Cuando se introduce un algoritmo no v&aacute;lido.
 * @throws java.io.IOException Cuando se produce un error de entrada/salida.
 * @throws CertificateEncodingException Error de codificaci&oacute;n en el certificado. */
public static ASN1EncodableVector generateSignerInfo(final Certificate cert, final String digestAlgorithmName,
        final byte[] data, final AdESPolicy policy, final boolean signingCertificateV2, final byte[] dataDigest,
        final Date signDate, final boolean padesMode, final String contentType, final String contentDescription,
        final List<CommitmentTypeIndicationBean> ctis, final CAdESSignerMetadata csm)
        throws NoSuchAlgorithmException, IOException, CertificateEncodingException {
    // // ATRIBUTOS

    // authenticatedAttributes (http://tools.ietf.org/html/rfc3852#section-11)
    final ASN1EncodableVector contexExpecific = initContexExpecific(digestAlgorithmName, data, dataDigest,
            signDate, padesMode);

    if (signingCertificateV2) {
        contexExpecific.add(getSigningCertificateV2((X509Certificate) cert, digestAlgorithmName, policy));
    } else {
        contexExpecific.add(getSigningCertificateV1((X509Certificate) cert, digestAlgorithmName, policy));
    }

    // SIGPOLICYID ATTRIBUTE

    if (policy != null && policy.getPolicyIdentifier() != null) {
        contexExpecific.add(getSigPolicyId(digestAlgorithmName, policy));
    }

    /** Secuencia con el tipo de contenido firmado. No se agrega en firmas PAdES.
     *
     * ContentHints ::= SEQUENCE {
     *     contentDescription UTF8String (SIZE (1..MAX)) OPTIONAL,
     *     contentType ContentType
     * } */

    if (contentType != null && !padesMode) {
        final ContentHints contentHints;
        if (contentDescription != null) {
            contentHints = new ContentHints(new ASN1ObjectIdentifier(contentType),
                    new DERUTF8String(contentDescription));
        } else {
            contentHints = new ContentHints(new ASN1ObjectIdentifier(contentType));
        }
        contexExpecific.add(new Attribute(PKCSObjectIdentifiers.id_aa_contentHint,
                new DERSet(contentHints.toASN1Primitive())));
    }

    // Atributos adicionales segun seccion 5.11 de RFC 5126

    // commitment-type-indication
    if (ctis != null && ctis.size() > 0) {
        for (final CommitmentTypeIndicationBean ctib : ctis) {
            contexExpecific.add(new Attribute(PKCSObjectIdentifiers.id_aa_ets_commitmentType, new DERSet(
                    CommitmentTypeIndicationsHelper.generateCommitmentTypeIndication(ctib).toASN1Primitive())));
        }
    }

    // id-aa-ets-signerLocation
    if (csm != null && CAdESSignerMetadataHelper.getSignerLocation(csm.getSignerLocation()) != null) {
        contexExpecific.add(new Attribute(PKCSObjectIdentifiers.id_aa_ets_signerLocation,
                new DERSet(CAdESSignerMetadataHelper.getSignerLocation(csm.getSignerLocation()))));
    }

    return contexExpecific;
}