Example usage for org.bouncycastle.asn1 ASN1UTCTime ASN1UTCTime

List of usage examples for org.bouncycastle.asn1 ASN1UTCTime ASN1UTCTime

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 ASN1UTCTime ASN1UTCTime.

Prototype

ASN1UTCTime(byte[] time) 

Source Link

Usage

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

License:Open Source License

private static ASN1EncodableVector initContexExpecific(final String dataDigestAlgorithmName, final byte[] data,
        final byte[] dataDigest, final Date signDate, final boolean padesMode) throws NoSuchAlgorithmException {
    // authenticatedAttributes
    final ASN1EncodableVector contexExpecific = new ASN1EncodableVector();

    // ContentType es obligatorio, y debe tener siempre el valor "id-data"
    contexExpecific.add(new Attribute(CMSAttributes.contentType, new DERSet(PKCSObjectIdentifiers.data)));

    // fecha de firma, no se anade en modo PAdES, pero es obligatorio en CAdES
    if (!padesMode) {
        contexExpecific.add(new Attribute(CMSAttributes.signingTime, new DERSet(new ASN1UTCTime(signDate))));
    }// www . j  a va  2 s.c  o  m

    // MessageDigest
    contexExpecific.add(new Attribute(CMSAttributes.messageDigest,
            new DERSet(new DEROctetString(dataDigest != null ? dataDigest
                    : MessageDigest.getInstance(dataDigestAlgorithmName).digest(data)))));

    return contexExpecific;
}

From source file:es.gob.afirma.signers.cms.CoSigner.java

License:Open Source License

/** Método que genera la parte que contiene la información del
 * Usuario. Se generan los atributos que se necesitan para generar la firma.
 * @param digestAlgorithm Algoritmo de huella digital.
 * @param datos Datos firmados./*from w  w w. java2s .com*/
 * @param dataType Identifica el tipo del contenido a firmar.
 * @param atrib Lista de atributos firmados que se insertarán dentro
 *              del archivo de firma.
 * @return Los atributos firmados de la firma.
 * @throws java.security.NoSuchAlgorithmException Cuando el JRE no soporta algún
 *                                                algoritmo necesario */
private ASN1Set generateSignerInfo(final String digestAlgorithm, final byte[] datos, final String dataType,
        final Map<String, byte[]> atrib) throws NoSuchAlgorithmException {

    // // ATRIBUTOS

    // authenticatedAttributes
    final ASN1EncodableVector contexExpecific = new ASN1EncodableVector();

    // tipo de contenido
    contexExpecific
            .add(new Attribute(CMSAttributes.contentType, new DERSet(new ASN1ObjectIdentifier(dataType))));

    // fecha de firma
    contexExpecific.add(new Attribute(CMSAttributes.signingTime, new DERSet(new ASN1UTCTime(new Date()))));

    // Si nos viene el hash de fuera no lo calculamos
    final byte[] md = MessageDigest.getInstance(AOSignConstants.getDigestAlgorithmName(digestAlgorithm))
            .digest(datos);

    // MessageDigest
    contexExpecific.add(new Attribute(CMSAttributes.messageDigest, new DERSet(new DEROctetString(md.clone()))));

    // agregamos la lista de atributos a mayores.
    if (atrib.size() != 0) {
        final Iterator<Map.Entry<String, byte[]>> it = atrib.entrySet().iterator();
        while (it.hasNext()) {
            final Map.Entry<String, byte[]> e = it.next();
            contexExpecific.add(new Attribute(
                    // el oid
                    new ASN1ObjectIdentifier(e.getKey().toString()),
                    // el array de bytes en formato string
                    new DERSet(new DERPrintableString(new String(e.getValue())))));
        }
    }

    this.signedAttr2 = SigUtils.getAttributeSet(new AttributeTable(contexExpecific));

    return SigUtils.getAttributeSet(new AttributeTable(contexExpecific));

}

From source file:es.gob.afirma.signers.cms.CoSigner.java

License:Open Source License

/** 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.
 * En este caso se introduce el hash directamente.
 * @param cert Certificado necesario para la firma.
 * @param datos Datos firmados./*from   ww  w . j  ava 2s .  c o m*/
 * @param dataType Identifica el tipo del contenido a firmar.
 * @param atrib Lista de atributos firmados que se insertar&aacute;n dentro
 *              del archivo de firma.
 * @return Atributos firmados de la firma. */
private ASN1Set generateSignerInfoFromHash(final X509Certificate cert, final byte[] datos,
        final String dataType, final Map<String, byte[]> atrib) {

    // // ATRIBUTOS

    // authenticatedAttributes
    final ASN1EncodableVector contexExpecific = new ASN1EncodableVector();

    // tipo de contenido
    contexExpecific
            .add(new Attribute(CMSAttributes.contentType, new DERSet(new ASN1ObjectIdentifier(dataType))));

    // fecha de firma
    contexExpecific.add(new Attribute(CMSAttributes.signingTime, new DERSet(new ASN1UTCTime(new Date()))));

    // MessageDigest
    contexExpecific.add(new Attribute(CMSAttributes.messageDigest, new DERSet(new DEROctetString(datos))));

    // Serial Number
    contexExpecific.add(new Attribute(RFC4519Style.serialNumber,
            new DERSet(new DERPrintableString(cert.getSerialNumber().toString()))));

    // agregamos la lista de atributos a mayores.
    if (atrib.size() != 0) {
        final Iterator<Map.Entry<String, byte[]>> it = atrib.entrySet().iterator();
        while (it.hasNext()) {
            final Map.Entry<String, byte[]> e = it.next();
            contexExpecific.add(new Attribute(
                    // el oid
                    new ASN1ObjectIdentifier(e.getKey().toString()),
                    // el array de bytes en formato string
                    new DERSet(new DERPrintableString(new String(e.getValue())))));
        }
    }

    this.signedAttr2 = SigUtils.getAttributeSet(new AttributeTable(contexExpecific));

    return SigUtils.getAttributeSet(new AttributeTable(contexExpecific));

}

From source file:es.gob.afirma.signers.cms.CounterSigner.java

License:Open Source License

/** Genera la parte que contiene la informaci&oacute;n del
 * Usuario. Se generan los atributos que se necesitan para generar la
 * firma.//w w w  .j  ava 2 s  .  com
 * @param cert Certificado necesario para la firma.
 * @param digestAlgorithm Algoritmo Firmado.
 * @param datos Datos firmados.
 * @return Los datos necesarios para generar la firma referente a los datos
 *         del usuario.
 * @throws java.security.NoSuchAlgorithmException Cuando el JRE no soporta alg&uacute;n algoritmo necesario. */
private ASN1Set generateSignerInfo(final X509Certificate cert, final String digestAlgorithm, final byte[] datos)
        throws NoSuchAlgorithmException {
    // // ATRIBUTOS

    // authenticatedAttributes
    final ASN1EncodableVector contexExpecific = new ASN1EncodableVector();

    // Las Contrafirmas CMS no tienen ContentType

    // fecha de firma
    contexExpecific.add(new Attribute(CMSAttributes.signingTime, new DERSet(new ASN1UTCTime(new Date()))));

    // MessageDigest
    contexExpecific.add(new Attribute(CMSAttributes.messageDigest, new DERSet(new DEROctetString(MessageDigest
            .getInstance(AOSignConstants.getDigestAlgorithmName(digestAlgorithm)).digest(datos)))));

    // Serial Number
    contexExpecific.add(new Attribute(RFC4519Style.serialNumber,
            new DERSet(new DERPrintableString(cert.getSerialNumber().toString()))));

    // agregamos la lista de atributos a mayores.
    if (this.atrib2.size() != 0) {
        final Iterator<Map.Entry<String, byte[]>> it = this.atrib2.entrySet().iterator();
        while (it.hasNext()) {
            final Map.Entry<String, byte[]> e = it.next();
            contexExpecific.add(new Attribute(new ASN1ObjectIdentifier(e.getKey().toString()), // el oid
                    new DERSet(new DERPrintableString(new String(e.getValue()))))); // el array de bytes en formato string
        }
    }

    this.signedAttr2 = SigUtils.getAttributeSet(new AttributeTable(contexExpecific));

    return SigUtils.getAttributeSet(new AttributeTable(contexExpecific));

}

From source file:es.gob.afirma.signers.cms.GenSignedData.java

License:Open Source License

/** Genera los atributos firmados.
 * @param digestAlgorithm Algoritmo Firmado.
 * @param datos Datos firmados./*from w  ww  .  j ava 2  s .c o  m*/
 * @param dataType Identifica el tipo del contenido a firmar.
 * @param timestamp Introducir el momento de la firma como atributo firmado (no confundir con un sello de tiempo reconocido)
 * @param atrib Lista de atributos firmados que se insertar&aacute;n dentro
 *              del archivo de firma.
 * @param messageDigest Huella digital.
 * @return Los atributos firmados de la firma.
 * @throws java.security.NoSuchAlgorithmException Cuando el JRE no soporta alg&uacute;n algoritmo necesario. */
private ASN1Set generateSignedInfo(final String digestAlgorithm, final byte[] datos, final String dataType,
        final boolean timestamp, final Map<String, byte[]> atrib, final byte[] messageDigest)
        throws NoSuchAlgorithmException {
    // // ATRIBUTOS

    // authenticatedAttributes
    final ASN1EncodableVector contexExpecific = new ASN1EncodableVector();

    // tipo de contenido
    contexExpecific
            .add(new Attribute(CMSAttributes.contentType, new DERSet(new ASN1ObjectIdentifier(dataType))));

    // fecha de firma
    if (timestamp) {
        contexExpecific.add(new Attribute(CMSAttributes.signingTime, new DERSet(new ASN1UTCTime(new Date()))));
    }

    // Si nos viene el hash de fuera no lo calculamos
    final byte[] md;
    if (messageDigest == null || messageDigest.length < 1) {
        md = MessageDigest.getInstance(AOSignConstants.getDigestAlgorithmName(digestAlgorithm)).digest(datos);
    } else {
        md = messageDigest;
    }

    // MessageDigest
    contexExpecific.add(new Attribute(CMSAttributes.messageDigest, new DERSet(new DEROctetString(md.clone()))));

    // agregamos la lista de atributos a mayores.
    if (atrib.size() != 0) {

        final Iterator<Map.Entry<String, byte[]>> it = atrib.entrySet().iterator();
        while (it.hasNext()) {
            final Map.Entry<String, byte[]> e = it.next();
            contexExpecific.add(new Attribute(new ASN1ObjectIdentifier(e.getKey()), // el oid
                    new DERSet(new DERPrintableString(new String(e.getValue()))) // el array de bytes en formato string
            ));
        }

    }

    this.signedAttr2 = SigUtils.getAttributeSet(new AttributeTable(contexExpecific));

    return SigUtils.getAttributeSet(new AttributeTable(contexExpecific));

}