1 package es.caib.signatura.impl;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.Serializable;
8 import java.lang.reflect.Method;
9 import java.security.cert.X509Certificate;
10 import java.util.Date;
11
12 import es.caib.signatura.api.ParsedCertificate;
13 import es.caib.signatura.api.Signature;
14 import es.caib.signatura.api.SignatureDataException;
15 import es.caib.signatura.api.SignatureProviderException;
16 import es.caib.signatura.api.SignatureTimestampException;
17 import es.caib.signatura.api.SignatureVerifyException;
18 import es.caib.signatura.api.Signer;
19 import es.caib.signatura.impl.ClassLoaderFactory;
20 import es.caib.signatura.impl.SignatureProviderInterface;
21
22
23
24
25
26
27
28
29
30 public class WebSignature implements Signature, Serializable {
31
32 protected byte[] signatureBytes = null;
33
34 private String contentType = null;
35
36 private static final long serialVersionUID = 1;
37
38 private transient SignatureProviderInterface impl = null;
39
40 private static final int INTERNET_EXPLORER = 1;
41 private static final int MOZILLA_FIREFOX = 2;
42 private int origin;
43
44
45
46
47
48
49
50
51
52
53 public WebSignature(String firma) throws SignatureDataException {
54 if (firma.startsWith("FF:"))
55 origin = MOZILLA_FIREFOX;
56 else if (firma.startsWith("IE:"))
57 origin = INTERNET_EXPLORER;
58 else
59 throw new SignatureDataException();
60
61 int len = firma.indexOf(":", 3);
62
63 try {
64 Class base64Class = ClassLoaderFactory.getFactory()
65 .getMasterClassLoader().loadClass(
66 "org.bouncycastle.util.encoders.Base64");
67 Method m = base64Class.getMethod("decode",
68 new Class[] { String.class });
69 signatureBytes = (byte[]) m.invoke(null, new Object[] { firma
70 .substring(len + 1) });
71 contentType = firma.substring(3, len);
72 init();
73 } catch (Exception e) {
74 throw new SignatureDataException(e);
75 }
76 }
77
78 public void setContentType(String contentType) {
79 this.contentType = contentType;
80 }
81
82 public void setPkcs7(byte pkcs7[]) {
83 signatureBytes = pkcs7;
84 }
85
86 private void init() throws IOException {
87 if (impl == null) {
88 try {
89 ClassLoader cl = ClassLoaderFactory.getFactory()
90 .getMasterClassLoader();
91 Class clazz = cl.loadClass(getInternalClassName());
92 impl = (SignatureProviderInterface) clazz.newInstance();
93 } catch (InstantiationException e) {
94 throw new RuntimeException(e);
95 } catch (IllegalAccessException e) {
96 throw new RuntimeException(e);
97 } catch (ClassNotFoundException e) {
98 throw new RuntimeException(e);
99 }
100 }
101 try {
102 impl.setContentType(contentType);
103 impl.setSignedData(signatureBytes);
104 } catch (Exception e) {
105 throw new IOException("Unable to parse signature");
106 }
107 }
108
109
110
111
112
113
114 protected String getInternalClassName() {
115 if (origin == INTERNET_EXPLORER)
116 return "es.caib.signatura.provider.impl.common.CapicomSignatureImpl";
117 else if (origin == MOZILLA_FIREFOX)
118 return "es.caib.signatura.provider.impl.common.CMSSignatureImplv2";
119 else
120 throw new RuntimeException("Signature type unknown " + origin);
121 }
122
123
124
125
126 public String getCertCaName() {
127 return impl.getCertCaName();
128 }
129
130
131
132
133 public String getCertSubjectCommonName() {
134 return impl.getCertSubjectCommonName();
135 }
136
137
138
139
140 public String getCertSubjectAlternativeNames() {
141 return impl.getCertSubjectAlternativeNames();
142 }
143
144
145
146
147 public byte[] getPkcs7() {
148 return this.signatureBytes;
149 }
150
151
152
153
154 private void readObject(java.io.ObjectInputStream in) throws IOException,
155 ClassNotFoundException {
156 in.defaultReadObject();
157 init();
158 }
159
160
161
162
163 public Date getDate() throws SignatureTimestampException {
164 return impl.getDate();
165 }
166
167
168
169
170 public boolean verify() throws SignatureVerifyException {
171 return impl.verify();
172 }
173
174
175
176
177 public String getContentType() {
178 return contentType;
179 }
180
181
182
183
184 public X509Certificate getCert() {
185 return impl.getCert();
186 }
187
188
189
190
191 public X509Certificate[] getCertificateChain() throws Exception {
192 return impl.getCertificateChain();
193 }
194
195
196
197
198 public ParsedCertificate getParsedCertificate() {
199 return impl.getParsedCertificate();
200 }
201
202
203
204
205 public boolean verify(InputStream contentStream)
206 throws SignatureProviderException, IOException,
207 SignatureVerifyException {
208 boolean isVerified = true;
209 try {
210
211 isVerified = isVerified && impl.verify(contentStream);
212
213 } catch (SignatureVerifyException e) {
214 throw e;
215 } catch (Exception e) {
216 throw new SignatureVerifyException(e);
217 }
218 return isVerified;
219 }
220
221
222
223
224 public boolean verifyAPosterioriTimestamp(InputStream contentStream)
225 throws SignatureProviderException, IOException,
226 SignatureVerifyException {
227 boolean isVerified = true;
228 try {
229
230 isVerified = isVerified
231 && impl.verifyAPosterioriTimestamp(contentStream);
232
233
234
235
236 if (isVerified) {
237 this.setPkcs7(impl.getPkcs7());
238 }
239
240 } catch (SignatureVerifyException e) {
241 throw e;
242 } catch (Exception e) {
243 throw new SignatureVerifyException(e);
244 }
245 return isVerified;
246 }
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270 }