View Javadoc

1   package es.caib.signatura.provider.tradise;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.io.Serializable;
6   import java.security.cert.X509Certificate;
7   import java.util.Date;
8   
9   import es.caib.signatura.api.SignatureVerifyException;
10  
11  import es.caib.signatura.api.ParsedCertificate;
12  import es.caib.signatura.api.Signature;
13  import es.caib.signatura.api.SignatureDataException;
14  import es.caib.signatura.api.SignatureProviderException;
15  import es.caib.signatura.api.SignatureTimestampException;
16  import es.caib.signatura.api.Signer;
17  import es.caib.signatura.impl.ClassLoaderFactory;
18  import es.caib.signatura.impl.SignatureProviderInterface;
19  
20  /**
21   * Implementación de la interfaz <code>SignatureData</code> para usar con la
22   * entidad certificadora Tradisea
23   * 
24   * @author 3digits
25   * @version 0.98
26   * @see Signer
27   * @see Signature
28   * 
29   */
30  public class TradiseSignature implements Signature, Serializable {
31  
32  	protected byte[] signatureBytes = null; // array de bytes con la firma
33  
34  	private static final long serialVersionUID = 1;
35  
36  	private String contentType = null;
37  
38  	private static final String CLASS_LOADER = "tradise";
39  
40  	private transient SignatureProviderInterface impl = null;
41  
42  	/**
43  	 * Crea un nuevo objeto a partir de los atributos de la clase. Es el
44  	 * constructor que debe usar cada implementación de la interfaz
45  	 * <code>Signature</code> para crear una firma. Se extrae el certificado
46  	 * de la firma y se guarda en la propiedad <code>transient</code>
47  	 * certificate para usarla en los métodos que dan información concreta del
48  	 * certificado
49  	 * 
50  	 * @param signatureBytes
51  	 *            array de bytes con la firma digital generada por la api del
52  	 *            proveedor de firma electrónica
53  	 * 
54  	 */
55  	public TradiseSignature(byte pkcs7[], String contentType)
56  			throws SignatureDataException {
57  		signatureBytes = pkcs7;
58  		this.contentType = contentType;
59  		try {
60  			init();
61  		} catch (IOException e) {
62  			throw new SignatureDataException(e);
63  		}
64  	}
65  
66  	public TradiseSignature (SignatureProviderInterface impl)
67  	{
68  		this.impl = impl;
69  		signatureBytes = impl.getPkcs7();
70  		contentType = impl.getContentType();
71  	}
72  
73  	protected String getClassLoaderName() {
74  		return CLASS_LOADER;
75  	}
76  
77  	protected String getImplClassName()
78  	{
79  		return "es.caib.signatura.provider.impl.tradise.TradiseSignatureImpl";
80  	}
81  
82  	private void init() throws IOException {
83  		if (impl == null) {
84  			try {
85  				ClassLoader cl = ClassLoaderFactory.getFactory().getClassLoader(getClassLoaderName());
86  				Class clazz = cl.loadClass( getImplClassName() );
87  					//[-3d] "es.caib.signatura.provider.impl.tradise.TradiseSignatureImpl"); 
88  				impl = (SignatureProviderInterface) clazz.newInstance();
89  			} catch (InstantiationException e) {
90  				throw new RuntimeException(e);
91  			} catch (IllegalAccessException e) {
92  				throw new RuntimeException(e);
93  			} catch (ClassNotFoundException e) {
94  				throw new RuntimeException(e);
95  			}
96  		}
97  		try {
98  			impl.setContentType(contentType);
99  			impl.setSignedData(signatureBytes);
100 		} catch (Exception e) {
101 			throw new IOException("Unable to parse signature");
102 		}
103 	}
104 
105 	/**
106 	 * Obtiene el nombre de la entidad certificadora usada en la firma
107 	 * 
108 	 * @return nombre de la entidad certificadora
109 	 */
110 	public String getCertCaName() {
111 		return impl.getCertCaName();
112 	}
113 
114 	/**
115 	 * Obtiene el nombre del certificado usado en la firma
116 	 * 
117 	 * @return nombre del certificado (CommonName)
118 	 */
119 	public String getCertSubjectCommonName() {
120 		return impl.getCertSubjectCommonName();
121 	}
122 
123 	public String getCertSubjectAlternativeNames() {
124 		return impl.getCertSubjectAlternativeNames();
125 	}
126 
127 	public byte[] getPkcs7() {
128 		return this.signatureBytes;
129 	}
130 
131 	// Sobreescribimos el método readObject que deserializa el objeto para
132 	// llamar posteriormente al método privado de inicialización del objetco
133 	// cuyo cometido es extraer el certificado de usuario de la firma
134 
135 	private void readObject(java.io.ObjectInputStream in) 
136 		throws IOException, ClassNotFoundException 
137 	{
138 		in.defaultReadObject();
139 		init();
140 	}
141 
142 	public Date getDate() 
143 		throws SignatureTimestampException 
144 	{
145 		return impl.getDate();
146 	}
147 
148 	public boolean verify() 
149 		throws SignatureVerifyException 
150 	{
151 		return impl.verify();
152 	}
153 
154 	public String getContentType() {
155 		return contentType;
156 	}
157 
158 	public X509Certificate getCert() {
159 		return impl.getCert();
160 	}
161 
162 
163     public X509Certificate[] getCertificateChain()
164                     throws Exception
165     {
166             return impl.getCertificateChain();
167     }
168 
169 	public ParsedCertificate getParsedCertificate() {
170 		return impl.getParsedCertificate();
171 	}
172 
173 	/*
174 	 * (non-Javadoc)
175 	 * 
176 	 * @see es.caib.signatura.api.Signature#verify(java.io.InputStream)
177 	 */
178 	public  boolean verifyAPosterioriTimestamp(InputStream contentStream)
179 		throws SignatureProviderException, IOException, SignatureVerifyException
180 	{
181 		/*
182 		 * Para tradise no se implementa la oparación
183 		 * de time stamp a posteriori
184 		 */
185 		return verify( contentStream);
186 	}
187 	
188 	public boolean verify(InputStream contentStream)
189 		throws SignatureProviderException, IOException,
190 			SignatureVerifyException 
191 	{
192 		return impl.verify(contentStream);
193 	}
194 }