View Javadoc

1   package es.caib.signatura.impl;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.lang.reflect.InvocationTargetException;
7   import java.lang.reflect.Method;
8   import java.security.cert.X509Certificate;
9   import java.util.Properties;
10  
11  import es.caib.signatura.api.SignatureVerifyException;
12  
13  
14  
15  /**
16   * Clase del lado del cliente que se utiliza para conectarse con la aplicacion de
17   * verificacion de certificados enviar peticiones de validacoin de certificados o cadenas de certificacion
18   * y devolvera el XML con la respuesta.
19   *
20   * Esta clase necesita el archivo de configuracion validator.properties con los siguientes parametros
21   *
22   * URL_SERVIDOR = Indica la URL donde se encuentra el servidor de validacion de certificados
23   * ENTORNO = Indica el entorno en el que se quiere verificar (DESARROLLO o PRODUCCION)
24   *
25   *
26   */
27  public class ValidadorProxy {
28  
29      private static String entorno;
30  	private static Method validadorMethod;
31  	
32      public ValidadorProxy() {
33      }
34      
35      static {
36  		ClassLoaderFactory factory;
37  		Properties p;
38  		try {
39  			factory = ClassLoaderFactory.getFactory();
40  			InputStream inputStream = factory.getMasterClassLoader().getResourceAsStream("es/caib/signatura/comun/comun.properties");
41  			if (inputStream ==null) 
42  				throw new RuntimeException (  "Resource /es/caib/signatura/comun/comun.properties not found" );
43  
44  			p = new Properties();
45  			p.load(inputStream);
46  			inputStream.close();
47  		} catch (IOException e1) {
48  			throw new RuntimeException ( e1 );
49  		}
50  		entorno = p.getProperty("ENTORNO");
51  
52  		if (entorno == null) {
53  			throw new RuntimeException( "El fichero comun.properties no es correcto");
54  		}
55  		try {
56  	    		Class validador = factory.getMasterClassLoader().loadClass( "es.caib.signatura.cliente.ValidadorCertificados2" );
57  	    		
58  	    		Method initmethod = validador.getMethod("init", new Class [] {File.class, String.class} );
59  	    		initmethod.invoke(null, new Object [] {factory.getLibraryDir(),entorno});
60  	    		validadorMethod = validador.getMethod("validate", new Class[] {
61  	    				(new X509Certificate[0]).getClass(), String.class } );		    		
62  		} catch ( Exception e ) {
63  			System.err.println ("WARNING: Cannot check certificate revocation status.");
64  			// No hay validador => Simplemente no se valida
65  		}
66      }
67  
68  
69      public boolean isValidadorInstalado() {
70      	return getMethod() != null;
71      }
72      
73  	private static Method getMethod() {
74  		return validadorMethod;
75  	}
76  
77  	public boolean validarAutenticacion(X509Certificate[] certificateChain)
78  		throws SignatureVerifyException
79  	{
80  		return invoke(certificateChain, "AUTENTIC");
81  	}
82  
83  	/**
84  	 * @param certificateChain
85  	 * @param result
86  	 * @return
87  	 * @throws SignatureVerifyException
88  	 */
89  	private static boolean invoke(X509Certificate[] certificateChain, String proposito ) throws SignatureVerifyException {
90  		try {
91  			Boolean result = (Boolean) getMethod().invoke(null, new Object [] {
92  					certificateChain,
93  					proposito });
94  			return result.booleanValue ();
95  		} catch (IllegalArgumentException e) {
96  			throw new RuntimeException(e);
97  		} catch (IllegalAccessException e) {
98  			throw new RuntimeException(e);
99  		} catch (InvocationTargetException e) {
100 			throw new SignatureVerifyException(e.getTargetException());
101 		}
102 	}
103 
104 	public boolean validarFirma(X509Certificate[] certificateChain) throws SignatureVerifyException {
105 		return invoke(certificateChain, "FIRMA");
106 	}
107 
108 	public boolean isEnDesenvolupament() {
109 		return "DESARROLLO".equals(entorno);
110 	}
111 	
112 	
113 
114 }