Java tutorial
/** * 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.utilidades; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.Enumeration; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import es.mityc.firmaJava.configuracion.Configuracion; import es.mityc.firmaJava.libreria.ConstantesXADES; import es.mityc.firmaJava.libreria.errores.PKCS12Error; /** * @author Ministerio de Industria, Turismo y Comercio * @version 0.9 beta */ public class GetPKCS12Keys implements ConstantesXADES { static Log log = LogFactory.getLog(GetPKCS12Keys.class); private String contrasenia = CADENA_VACIA; private String fichero = CADENA_VACIA; private KeyStore ks = null; private String claveAlias = null; private Configuracion configuracion = new Configuracion(); /** Crea una nueva instancia de GetPKCS12Keys * @param fichero * @param contrasea */ public GetPKCS12Keys(String fichero, String contrasenia) throws PKCS12Error { InputStream fis = null; try { //Carga la configuracin configuracion.cargarConfiguracion(); //Establece el idioma segn la configuracin String locale = configuracion.getValor(LOCALE); // Configura el idioma I18n.setLocale(locale, locale.toUpperCase()); if (contrasenia == null) { throw new PKCS12Error(I18n.getResource(LIBRERIAXADES_GETPKCS12KEYS_TEXTO_1)); } if (fichero == null || fichero.trim().equals(CADENA_VACIA)) { throw new PKCS12Error(I18n.getResource(LIBRERIAXADES_GETPKCS12KEYS_TEXTO_2)); } this.contrasenia = contrasenia; this.fichero = fichero; ks = KeyStore.getInstance(PKCS12); fis = new FileInputStream(fichero); ks.load(fis, contrasenia.toCharArray()); Enumeration e = ks.aliases(); while (e.hasMoreElements()) { String alias = (String) e.nextElement(); if (ks.isKeyEntry(alias)) { claveAlias = alias; break; } } if (claveAlias == null) { throw new PKCS12Error(I18n.getResource(LIBRERIAXADES_GETPKCS12KEYS_TEXTO_3)); } } catch (KeyStoreException e) { throw new PKCS12Error(e.getMessage()); } catch (FileNotFoundException e) { throw new PKCS12Error(e.getMessage()); } catch (NoSuchAlgorithmException e) { throw new PKCS12Error(e.getMessage()); } catch (CertificateException e) { throw new PKCS12Error(e.getMessage()); } catch (IOException e) { throw new PKCS12Error(e.getMessage()); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { log.error(e); } } } } /** * Obtiene la clave privada * @return Devuelve la clave privada * @throws PKCS12Error */ public PrivateKey getPrivateKey() throws PKCS12Error { try { // Leer certificado PrivateKey clavePrivada = (PrivateKey) ks.getKey(claveAlias, contrasenia.toCharArray()); return clavePrivada; } catch (Exception e) { throw new PKCS12Error(e); } } /** * Obtiene el certificado * @return Devuelve el certificado * @throws PKCS12Error */ public X509Certificate getCertificate() throws PKCS12Error { try { X509Certificate cert = (X509Certificate) ks.getCertificate(claveAlias); return cert; } catch (Exception e) { throw new PKCS12Error(e); } } /** * Obtiene la contrasea. * @return Valor de la contrasea. */ public String getPassword() { return contrasenia; } /** * Obtiene el fichero. * @return Valor del fichero. */ public String getFile() { return fichero; } /** * Obtiene la claveAlias * @return Valor de la claveAlias. */ public String getKeyAlias() { return claveAlias; } }