/*
 * Copyright (C) 2009 Libreria para Firma Digital development team.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 */

package com.fp.firma.keystore;

import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import com.fp.firma.common.FirmasException;

/**
 * Implementaci&oacute;n de KeyStoreProvider para acceder al keystore del sistema operativo
 * Microsoft Windows.
 * 
 * Utiliza funcionalidad disponible desde el JDK6 en adelante para acceder al MS CAPI.
 * 
 */
public class WindowsJDK6KeyStoreProvider implements KeyStoreProvider {

	public KeyStore getKeystore(char[] password) throws KeyStoreException {
		try {
			KeyStore keyStore = KeyStore.getInstance("Windows-MY");
			keyStore.load(null, password);
			return keyStore;
		} catch (NoSuchAlgorithmException e) {
			throw new KeyStoreException(e);
		} catch (CertificateException e) {
			throw new KeyStoreException(e);
		} catch (IOException e) {
			if(e.getCause() instanceof UnrecoverableKeyException){
				throw new FirmasException(e);
			} else{
				throw new KeyStoreException(e);
			}
		} catch (Throwable e) {
			if ((e instanceof UnrecoverableKeyException)) {
		        throw new FirmasException(e);
		    } else{
		    	throw new KeyStoreException(e);
		    }
		}
	}
}