es.mityc.firmaJava.libreria.pkcs7.FirmaPkcs7.java Source code

Java tutorial

Introduction

Here is the source code for es.mityc.firmaJava.libreria.pkcs7.FirmaPkcs7.java

Source

/**
 * LICENCIA LGPL:
 * 
 * Esta librera es Software Libre; Usted puede redistribuirlo y/o modificarlo
 * bajo los trminos de la GNU Lesser General Public License (LGPL)
 * tal y como ha sido publicada por la Free Software Foundation; o
 * bien la versin 2.1 de la Licencia, o (a su eleccin) cualquier versin posterior.
 * 
 * Esta librera se distribuye con la esperanza de que sea til, pero SIN NINGUNA
 * GARANT?A; tampoco las implcitas garantas de MERCANTILIDAD o ADECUACIN A UN
 * PROPSITO PARTICULAR. Consulte la GNU Lesser General Public License (LGPL) para ms
 * detalles
 * 
 * Usted debe recibir una copia de la GNU Lesser General Public License (LGPL)
 * junto con esta librera; si no es as, escriba a la Free Software Foundation Inc.
 * 51 Franklin Street, 5 Piso, Boston, MA 02110-1301, USA o consulte
 * <http://www.gnu.org/licenses/>.
 *
 * Copyright 2008 Ministerio de Industria, Turismo y Comercio
 * 
 */

package es.mityc.firmaJava.libreria.pkcs7;

import java.awt.Frame;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertStore;
import java.security.cert.CertStoreException;
import java.security.cert.Certificate;
import java.security.cert.CollectionCertStoreParameters;
import java.security.cert.X509Certificate;
import java.util.Arrays;

import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.CMSProcessable;
import org.bouncycastle.cms.CMSProcessableByteArray;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSSignedDataGenerator;

import es.mityc.firmaJava.libreria.ConstantesXADES;

/**
 * @author  Ministerio de Industria, Turismo y Comercio
 * @version 0.9 beta
 */

public final class FirmaPkcs7 implements ConstantesXADES {
    //private static FirmaPkcs7 firma   = null;

    private FirmaPkcs7() {
        // Creates the one and only instance of the class
    }

    //   public static FirmaPkcs7 getInstance()
    //   {
    //      if(firma == null)
    //      {
    //         firma = new FirmaPkcs7();
    //      }
    //      return firma;
    //   }

    public static byte[] firmar(Frame padre, X509Certificate cert, byte[] datos) throws KeyStoreException,
            NoSuchAlgorithmException, UnrecoverableKeyException, InvalidAlgorithmParameterException,
            NoSuchProviderException, CertStoreException, CMSException, IOException {
        byte[] bytesFirma = null;

        // Valida la tarjeta criptogrfica

        ValidaTarjeta vt = new ValidaTarjeta(padre);
        vt.setVisible(true);
        // Esperamos mientras se valida la tarjeta...
        vt.setVisible(false);

        // Genera la firma PKCS#7

        PrivateKey privateKey = vt.getPrivateKey(cert);

        CMSProcessable msg = new CMSProcessableByteArray(datos);
        CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
        gen.addSigner(privateKey, cert, CMSSignedDataGenerator.DIGEST_SHA1);

        KeyStore ks = vt.getKeyStore();
        String alias = vt.getAlias(cert);

        Certificate[] certChain = ks.getCertificateChain(alias);

        CertStore certs = CertStore.getInstance(COLLECTION,
                new CollectionCertStoreParameters(Arrays.asList(certChain)), BC);
        gen.addCertificatesAndCRLs(certs);

        CMSSignedData s = gen.generate(CMSSignedDataGenerator.DATA, msg, true, SUNPCKS11_TOKEN, true);
        bytesFirma = s.getEncoded();
        return bytesFirma;
    }

    public static byte[] firmar(ValidaTarjeta vt, X509Certificate cert, byte[] datos) throws KeyStoreException,
            NoSuchAlgorithmException, UnrecoverableKeyException, InvalidAlgorithmParameterException,
            NoSuchProviderException, CertStoreException, CMSException, IOException {
        byte[] bytesFirma = null;

        PrivateKey privateKey = vt.getPrivateKey(cert);

        CMSProcessable msg = new CMSProcessableByteArray(datos);
        CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
        gen.addSigner(privateKey, cert, CMSSignedDataGenerator.DIGEST_SHA1);

        KeyStore ks = vt.getKeyStore();
        String alias = vt.getAlias(cert);

        Certificate[] certChain = ks.getCertificateChain(alias);

        CertStore certs = CertStore.getInstance(COLLECTION,
                new CollectionCertStoreParameters(Arrays.asList(certChain)), BC);
        gen.addCertificatesAndCRLs(certs);

        CMSSignedData s = gen.generate(CMSSignedDataGenerator.DATA, msg, true, SUNPCKS11_TOKEN, true);
        bytesFirma = s.getEncoded();
        return bytesFirma;
    }
}