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
22
23
24
25
26
27
28
29
30 public class TradiseSignature implements Signature, Serializable {
31
32 protected byte[] signatureBytes = null;
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
44
45
46
47
48
49
50
51
52
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
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
107
108
109
110 public String getCertCaName() {
111 return impl.getCertCaName();
112 }
113
114
115
116
117
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
132
133
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
175
176
177
178 public boolean verifyAPosterioriTimestamp(InputStream contentStream)
179 throws SignatureProviderException, IOException, SignatureVerifyException
180 {
181
182
183
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 }