1 package es.caib.signatura.impl;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.security.cert.X509Certificate;
6 import java.util.Date;
7
8 import es.caib.signatura.api.ParsedCertificate;
9 import es.caib.signatura.api.Signature;
10 import es.caib.signatura.api.SignatureDataException;
11 import es.caib.signatura.api.SignatureProviderException;
12 import es.caib.signatura.api.SignatureTimestampException;
13 import es.caib.signatura.api.SignatureVerifyException;
14 import es.caib.signatura.impl.ClassLoaderFactory;
15 import es.caib.signatura.impl.SignatureProviderInterface;
16
17
18
19
20
21
22
23 public class SMIMESignatureProxy implements Signature {
24
25 protected byte[] signatureBytes = null;
26
27 private String contentType = null;
28
29 private static final long serialVersionUID = 1;
30
31 private transient SignatureProviderInterface impl = null;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public SMIMESignatureProxy(byte pkcs7[], String contentType)
47 throws SignatureDataException {
48 signatureBytes = pkcs7;
49 this.contentType = contentType;
50 try {
51 init();
52 } catch (IOException e) {
53 throw new SignatureDataException(e);
54 }
55 }
56
57
58
59 private void init() throws IOException {
60 if (impl == null) {
61 try {
62 ClassLoader cl = ClassLoaderFactory.getFactory().getMasterClassLoader();
63 Class clazz = cl .loadClass( getInternalClassName() );
64 impl =(SignatureProviderInterface)clazz.newInstance();
65 } catch (InstantiationException e) {
66 throw new RuntimeException(e);
67 } catch (IllegalAccessException e) {
68 throw new RuntimeException(e);
69 } catch (ClassNotFoundException e) {
70 throw new RuntimeException(e);
71 }
72 }
73 try {
74 impl.setContentType(contentType);
75 impl.setSignedData(signatureBytes);
76 } catch (Exception e) {
77 throw new IOException("Unable to parse signature");
78 }
79 }
80
81
82
83
84 protected String getInternalClassName() {
85 return "es.caib.signatura.provider.impl.common.SMIMESignatureImpl";
86 }
87
88
89
90
91
92
93 public String getCertCaName() {
94 return impl.getCertCaName();
95 }
96
97
98
99
100
101
102 public String getCertSubjectCommonName() {
103 return impl.getCertSubjectCommonName();
104 }
105
106 public String getCertSubjectAlternativeNames() {
107 return impl.getCertSubjectAlternativeNames();
108 }
109
110
111 public Date getDate() throws SignatureTimestampException {
112 return impl.getDate();
113 }
114
115 public boolean verify() throws SignatureVerifyException {
116 return impl.verify();
117 }
118
119 public String getContentType() {
120 return contentType;
121 }
122
123 public X509Certificate getCert() {
124 return impl.getCert();
125 }
126
127 public X509Certificate[] getCertificateChain() throws Exception {
128 return impl.getCertificateChain();
129 }
130
131 public ParsedCertificate getParsedCertificate() {
132 return impl.getParsedCertificate();
133 }
134
135
136
137 public boolean verify(InputStream contentStream)
138 throws SignatureProviderException, IOException,
139 SignatureVerifyException {
140 boolean isVerified = true;
141 try{
142
143
144 isVerified = isVerified && impl.verify(contentStream);
145
146 }catch(SignatureVerifyException e){
147 throw e;
148 } catch(Exception e){
149 throw new SignatureVerifyException(e);
150 }
151 return isVerified;
152 }
153
154
155
156 public byte[] getPkcs7() {
157 return null;
158 }
159
160
161
162 public boolean verifyAPosterioriTimestamp(InputStream contentStream) throws SignatureProviderException, IOException, SignatureVerifyException {
163 boolean isVerified = true;
164 try{
165
166
167 isVerified = isVerified && impl.verifyAPosterioriTimestamp(contentStream);
168
169 }catch(SignatureVerifyException e){
170 throw e;
171 } catch(Exception e){
172 throw new SignatureVerifyException(e);
173 }
174 return isVerified;
175 }
176
177
178
179 }