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

Java tutorial

Introduction

Here is the source code for es.mityc.firmaJava.libreria.pkcs7.ConexionTarjeta.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.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.ProviderException;
import java.security.Security;
import java.security.cert.CertificateException;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import sun.security.pkcs11.SunPKCS11;
import sun.security.pkcs11.wrapper.PKCS11Exception;
import es.mityc.firmaJava.libreria.ConstantesXADES;

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

public final class ConexionTarjeta implements ConstantesXADES {

    private static ConexionTarjeta conexion = null;

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

    public static ConexionTarjeta getInstance() {
        if (conexion == null) {
            conexion = new ConexionTarjeta();
        }
        return conexion;
    }

    /**
     * @param args
     */
    public static KeyStore conectar(char[] pin, String libreria) throws PKCS11Exception, ProviderException {
        KeyStore ks = null;
        try {
            if (Security.getProvider(SUNPCKS11_TOKEN) != null)
                Security.removeProvider(SUNPCKS11_TOKEN);
            String pkcs11config = NAME_IGUAL_TOKEN + LIBRARY_IGUAL + libreria;
            byte[] pkcs11configBytes = pkcs11config.getBytes();
            ByteArrayInputStream configStream = new ByteArrayInputStream(pkcs11configBytes);
            Provider pkcs11Provider = new SunPKCS11(configStream);
            Security.addProvider(new BouncyCastleProvider());
            Security.addProvider(pkcs11Provider);

            ks = KeyStore.getInstance(PKCS11, pkcs11Provider);
            ks.load(null, pin);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            Throwable t = null;
            if (e.getCause() != null) {
                t = e.getCause();

                if (t.getCause() != null && t.getCause() instanceof PKCS11Exception) {
                    PKCS11Exception pke = (PKCS11Exception) t.getCause();
                    throw new PKCS11Exception(pke.getErrorCode());
                }

            }
        } catch (KeyStoreException e) {
            throw new PKCS11Exception(-1);
        } catch (ProviderException e) {
            if (e.getCause() != null) {
                Throwable t = e.getCause();
                if (t instanceof PKCS11Exception) {
                    PKCS11Exception pke = (PKCS11Exception) t;
                    throw new PKCS11Exception(pke.getErrorCode());
                } else
                    throw e;
            } else {
                throw new ProviderException(e.getMessage());
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return ks;
    }
}