1 package es.caib.signatura.impl;
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
22
23
24
25
26
27
28
29
30 public class CMSSignature 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
41
42
43
44
45
46
47
48
49
50
51
52
53 public CMSSignature(byte pkcs7[], String contentType)
54 throws SignatureDataException {
55 signatureBytes = pkcs7;
56 this.contentType = contentType;
57 try {
58 init();
59 } catch (IOException e) {
60 throw new SignatureDataException(e);
61 }
62 }
63
64 public void setContentType(String contentType){
65 this.contentType = contentType;
66 }
67
68 public void setPkcs7(byte pkcs7[]){
69 signatureBytes = pkcs7;
70 }
71
72 public CMSSignature (SignatureProviderInterface impl)
73 {
74 this.impl = impl;
75 signatureBytes = impl.getPkcs7();
76 contentType = impl.getContentType();
77 }
78
79 private void init() throws IOException {
80 if (impl == null) {
81 try {
82 ClassLoader cl = ClassLoaderFactory.getFactory().getMasterClassLoader();
83 Class clazz = cl .loadClass( getInternalClassName() );
84 impl =(SignatureProviderInterface)clazz.newInstance();
85 } catch (InstantiationException e) {
86 throw new RuntimeException(e);
87 } catch (IllegalAccessException e) {
88 throw new RuntimeException(e);
89 } catch (ClassNotFoundException e) {
90 throw new RuntimeException(e);
91 }
92 }
93 try {
94 impl.setContentType(contentType);
95 impl.setSignedData(signatureBytes);
96 } catch (Exception e) {
97 throw new IOException("Unable to parse signature");
98 }
99 }
100
101
102
103
104 protected String getInternalClassName() {
105 return "es.caib.signatura.provider.impl.common.CMSSignatureImpl";
106 }
107
108
109
110
111
112
113 public String getCertCaName() {
114 return impl.getCertCaName();
115 }
116
117
118
119
120
121
122 public String getCertSubjectCommonName() {
123 return impl.getCertSubjectCommonName();
124 }
125
126 public String getCertSubjectAlternativeNames() {
127 return impl.getCertSubjectAlternativeNames();
128 }
129
130 public byte[] getPkcs7() {
131 return this.signatureBytes;
132 }
133
134
135
136
137 private void readObject(java.io.ObjectInputStream in) throws IOException,
138 ClassNotFoundException {
139 in.defaultReadObject();
140 init();
141 }
142
143 public Date getDate() throws SignatureTimestampException {
144 return impl.getDate();
145 }
146
147 public boolean verify() throws SignatureVerifyException {
148 return impl.verify();
149 }
150
151 public String getContentType() {
152 return contentType;
153 }
154
155 public X509Certificate getCert() {
156 return impl.getCert();
157 }
158
159 public X509Certificate[] getCertificateChain() throws Exception {
160 return impl.getCertificateChain();
161 }
162
163 public ParsedCertificate getParsedCertificate() {
164 return impl.getParsedCertificate();
165 }
166
167
168
169
170
171
172
173 public boolean verify(InputStream contentStream)
174 throws SignatureProviderException, IOException,
175 SignatureVerifyException {
176 boolean isVerified = true;
177 try{
178
179 isVerified = isVerified && impl.verify(contentStream);
180
181 }catch(SignatureVerifyException e){
182 throw e;
183 } catch(Exception e){
184 throw new SignatureVerifyException(e);
185 }
186 return isVerified;
187 }
188
189
190 public boolean verifyAPosterioriTimestamp(InputStream contentStream) throws SignatureProviderException, IOException,
191 SignatureVerifyException {
192 boolean isVerified = true;
193 try{
194
195 isVerified = isVerified && impl.verifyAPosterioriTimestamp(contentStream);
196 /
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249 }