All Downloads are FREE. Search and download functionalities are using the official Maven repository.

es.gob.afirma.standalone.SimpleKeyStoreManager Maven / Gradle / Ivy

There is a newer version: 1.8.2
Show newest version
/* Copyright (C) 2011 [Gobierno de Espana]
 * This file is part of "Cliente @Firma".
 * "Cliente @Firma" is free software; you can redistribute it and/or modify it under the terms of:
 *   - the GNU General Public License as published by the Free Software Foundation;
 *     either version 2 of the License, or (at your option) any later version.
 *   - or The European Software License; either version 1.1 or (at your option) any later version.
 * You may contact the copyright holder at: [email protected]
 */

package es.gob.afirma.standalone;

import java.awt.Component;
import java.io.IOException;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import es.gob.afirma.core.misc.Platform;
import es.gob.afirma.core.misc.Platform.OS;
import es.gob.afirma.core.ui.AOUIFactory;
import es.gob.afirma.keystores.AOKeyStore;
import es.gob.afirma.keystores.AOKeyStoreManager;
import es.gob.afirma.keystores.AOKeyStoreManagerException;
import es.gob.afirma.keystores.AOKeyStoreManagerFactory;
import es.gob.afirma.keystores.AOKeystoreAlternativeException;
import es.gob.afirma.keystores.SmartCardLockedException;
import es.gob.afirma.keystores.mozilla.MozillaKeyStoreUtilities;
import es.gob.afirma.standalone.ui.preferences.PreferencesManager;

/** Gestor simple de KeyStores. Obtiene o un KeyStore de DNIe
 * vía controlador 100% Java o el KeyStore por defecto del sistema operativo.
 * @author Tomás García-Merás. */
public final class SimpleKeyStoreManager {

    private SimpleKeyStoreManager() { /* No permitimos la instanciacion */ }

    /** Obtiene un KeyStore.
     * @param dnie true si desea obtenerse un KeyStore para DNIe, false si desea obtenerse
     *        el KeyStore por defecto del sistema operativo.
     * @param parent Componente padre para la modalidad.
     * @return KeyStore apropiado.
     * @throws AOKeyStoreManagerException Si ocurre cualquier problema durante la obtención del KeyStore. */
    static AOKeyStoreManager getKeyStore(final boolean dnie, final Component parent) throws AOKeyStoreManagerException {

        if (dnie) {
            try {
            	return getKeyStoreManager(AOKeyStore.DNIEJAVA, parent);
            }
            catch(final SmartCardLockedException e) {
            	AOUIFactory.showErrorMessage(
    				parent,
    				SimpleAfirmaMessages.getString("SimpleKeyStoreManager.9"), //$NON-NLS-1$
    				SimpleAfirmaMessages.getString("SimpleKeyStoreManager.10"), //$NON-NLS-1$
    				JOptionPane.WARNING_MESSAGE
				);
            }
            catch (final Exception e) {
            	String exClassName = e.getClass().getName();
            	Throwable t = e.getCause();
            	if (t != null) {
            		t = t.getCause();
            		if (t != null) {
            			t = t.getCause();
            			if (t != null) {
            				exClassName = t.getClass().getName();
            			}
            		}
            	}
            	switch(exClassName) {
            		case "es.gob.jmulticard.apdu.connection.CardNotPresentException": //$NON-NLS-1$
                		if (JOptionPane.YES_OPTION == AOUIFactory.showConfirmDialog(
            				parent,
            				SimpleAfirmaMessages.getString("SimpleKeyStoreManager.1"),  //$NON-NLS-1$
            				SimpleAfirmaMessages.getString("SimpleKeyStoreManager.2"),  //$NON-NLS-1$
            				JOptionPane.YES_NO_OPTION,
            				JOptionPane.WARNING_MESSAGE
        				)) {
                			return getKeyStore(true, parent);
                		}
                		break;
            		case "es.gob.jmulticard.card.InvalidCardException": //$NON-NLS-1$
            			if (JOptionPane.YES_OPTION == AOUIFactory.showConfirmDialog(
                				parent,
                				SimpleAfirmaMessages.getString("SimpleKeyStoreManager.3"),  //$NON-NLS-1$
                				SimpleAfirmaMessages.getString("SimpleKeyStoreManager.4"),  //$NON-NLS-1$
                				JOptionPane.YES_NO_OPTION,
                				JOptionPane.WARNING_MESSAGE
            				)) {
                    			return getKeyStore(true, parent);
                    		}
                    		break;

            		case "es.gob.jmulticard.card.dnie.BurnedDnieCardException": //$NON-NLS-1$
	            		AOUIFactory.showErrorMessage(
            				parent,
            				SimpleAfirmaMessages.getString("SimpleKeyStoreManager.5"), //$NON-NLS-1$
            				SimpleAfirmaMessages.getString("SimpleKeyStoreManager.6"), //$NON-NLS-1$
            				JOptionPane.WARNING_MESSAGE
        				);
	            		break;
            		default:
                		Logger.getLogger("es.gob.afirma").severe( //$NON-NLS-1$
            				"No se ha podido inicializar el controlador 100% Java del DNIe: " + e //$NON-NLS-1$
        				);
                		AOUIFactory.showErrorMessage(
            				parent,
            				SimpleAfirmaMessages.getString("SimpleKeyStoreManager.7"), //$NON-NLS-1$
            				SimpleAfirmaMessages.getString("SimpleKeyStoreManager.8"), //$NON-NLS-1$
            				JOptionPane.ERROR_MESSAGE
        				);
            	}
            }
        }

        // El por defecto
        final AOKeyStore aoks = getDefaultKeyStoreType();
        try {
			return getKeyStoreManager(
				aoks,
				parent
			);
		}
        catch (final Exception e) {
            throw new AOKeyStoreManagerException(
        		"No se ha podido incializar el almacen '" +  aoks  + "': " + e, e //$NON-NLS-1$ //$NON-NLS-2$
    		);
		}
    }

    private static AOKeyStoreManager getKeyStoreManager(final AOKeyStore aoks, final Component parent) throws IOException, AOKeystoreAlternativeException {
    	return AOKeyStoreManagerFactory.getAOKeyStoreManager(
    		aoks,
    		null,
    		null,
    		aoks.getStorePasswordCallback(parent),
    		parent
		);
    }

    /** Indica si está disponible el almacén de claves de Mozilla Firefox.
     * @return true si está disponible el almacén de claves de Mozilla Firefox,
     *         false en caso contrario. */
    public static boolean isFirefoxAvailable() {
		final String mozProfileDir;
		final String nssLibDir;
		try {
			mozProfileDir = MozillaKeyStoreUtilities.getMozillaUserProfileDirectory();
			nssLibDir = MozillaKeyStoreUtilities.getSystemNSSLibDir();
		}
		catch(final Exception e) {
			return false;
		}
		if (mozProfileDir != null && nssLibDir !=null) {
			return true;
		}
		return false;
    }

    private static AOKeyStore getDefaultKeyStoreTypeByOs(final OS os) {
    	if (Platform.OS.WINDOWS.equals(os)) {
    		return AOKeyStore.WINDOWS;
    	}
    	if (Platform.OS.MACOSX.equals(os)) {
    		return AOKeyStore.APPLE;
    	}
    	return AOKeyStore.SHARED_NSS;
    }

    /** Obtiene el almacén de claves por defecto de la aplicación.
     * @return Almacén de claves por defecto de la aplicación. */
    public static AOKeyStore getDefaultKeyStoreType() {
    	final String savedStoreName = PreferencesManager.get(PreferencesManager.PREFERENCE_KEYSTORE_DEFAULT_STORE);
    	final OS os = Platform.getOS();
    	if (savedStoreName != null) {
    		final AOKeyStore ks = AOKeyStore.getKeyStore(savedStoreName);
    		if (ks != null) {
    			// Si desinstalan Firefox que no se quede una seleccion mala
    			if (AOKeyStore.MOZ_UNI.equals(ks)) {
    				if (isFirefoxAvailable()) {
    					return ks;
    				}
					return getDefaultKeyStoreTypeByOs(os);
    			}
    			// No deberia pasar
    			if (AOKeyStore.WINDOWS.equals(ks)) {
    				if (OS.WINDOWS.equals(os)) {
    					return ks;
    				}
    				return getDefaultKeyStoreTypeByOs(os);
    			}
    			// No deberia pasar
    			if (AOKeyStore.APPLE.equals(ks)) {
    				if (OS.MACOSX.equals(os)) {
    					return ks;
    				}
    				return getDefaultKeyStoreTypeByOs(os);
    			}
    			return ks;
    		}
    	}
    	return getDefaultKeyStoreTypeByOs(os);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy