1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package es.accv.arangi.base.device.model;
22
23 import iaik.pkcs.pkcs11.Module;
24 import iaik.pkcs.pkcs11.Session;
25 import iaik.pkcs.pkcs11.Token;
26 import iaik.pkcs.pkcs11.TokenInfo;
27 import iaik.pkcs.pkcs11.objects.X509PublicKeyCertificate;
28
29 import java.security.cert.X509Certificate;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 import org.apache.log4j.Logger;
36
37 import es.accv.arangi.base.device.AbstractPkcs11Manager;
38 import es.accv.arangi.base.device.util.pkcs11.Pkcs11Util;
39 import es.accv.arangi.base.exception.device.LoadingObjectException;
40 import es.accv.arangi.base.exception.device.OpeningDeviceException;
41 import es.accv.arangi.base.exception.device.SearchingException;
42 import es.accv.arangi.base.util.Util;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class Pkcs11Device {
63
64
65
66
67 Logger logger = Logger.getLogger(Pkcs11Device.class);
68
69 private Pkcs11Manufacturer manufacturer;
70 private String moduleName;
71 private Module module;
72 private Token token;
73 private TokenInfo tokenInfo;
74 private Session session;
75 private X509Certificate[] certificates;
76 private String[] aliases;
77 private Map<String,X509Certificate> mapCertificates;
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public Pkcs11Device (boolean isOpened, Pkcs11Manufacturer manufacturer, String moduleName, Module module, Token token, TokenInfo tokenInfo, Session session) {
92 this.manufacturer = manufacturer;
93 this.moduleName = moduleName;
94 this.module = module;
95 this.token = token;
96 this.tokenInfo = tokenInfo;
97
98
99
100 if (isOpened) {
101 this.session = session;
102 } else {
103 try {
104 loadAliasAndCertificates (session);
105 session.closeSession();
106 } catch (Throwable e) {
107 logger.info("[Pkcs11Manufacturer.open]::Excepción durante cierre de dispositivo. Ignorando...", e);
108 }
109 }
110 }
111
112
113
114
115
116
117 public long getId() {
118 return token.getTokenID();
119 }
120
121
122
123
124
125
126 public String getModuleName() {
127 return moduleName;
128 }
129
130
131
132
133
134
135 public Module getModule() {
136 return module;
137 }
138
139
140
141
142
143
144 public Token getToken() {
145 return token;
146 }
147
148
149
150
151
152
153 public TokenInfo getTokenInfo() {
154 return tokenInfo;
155 }
156
157
158
159
160
161
162
163 public Session getSession() {
164 return session;
165 }
166
167
168
169
170
171
172 public void setSession(Session session) {
173 this.session = session;
174 }
175
176
177
178
179
180
181
182 public X509Certificate[] getCertificates() {
183 return certificates;
184 }
185
186
187
188
189
190
191
192 public X509Certificate getCertificate (String alias) {
193 return mapCertificates.get(alias);
194 }
195
196
197
198
199
200
201
202 public String[] getAliases() {
203 return aliases;
204 }
205
206
207
208
209
210
211
212 public String getLabel() {
213 return tokenInfo.getLabel().trim();
214 }
215
216
217
218
219
220
221 public long getTotalMemory () {
222 return tokenInfo.getTotalPublicMemory();
223 }
224
225
226
227
228
229
230 public long getFreeMemory () {
231 return tokenInfo.getFreePublicMemory();
232 }
233
234
235
236
237
238
239 public Pkcs11Manufacturer getManufacturer() {
240 return manufacturer;
241 }
242
243
244
245
246
247
248
249
250 public String getManufacturerId() {
251 return tokenInfo.getManufacturerID().trim();
252 }
253
254
255
256
257
258
259 public String getModel() {
260 return tokenInfo.getModel().trim();
261 }
262
263
264
265
266
267
268 public String getSerialNumber() {
269 return tokenInfo.getSerialNumber().trim();
270 }
271
272
273
274
275
276
277 public String getFirmwareVersion() {
278 return tokenInfo.getFirmwareVersion().getMajor() + "." + tokenInfo.getFirmwareVersion().getMinor();
279 }
280
281
282
283
284
285
286 private void loadAliasAndCertificates(Session session) throws SearchingException {
287
288 mapCertificates = new HashMap<String, X509Certificate>();
289 try {
290 X509PublicKeyCertificate template = new X509PublicKeyCertificate();
291 List result = Pkcs11Util.findAllObjects(session, template);
292 List<X509Certificate> lX509Certificates = new ArrayList<X509Certificate> ();
293 List<String> lAliases = new ArrayList<String> ();
294 for (int f=0;f<result.size();f++) {
295 X509PublicKeyCertificate iaikCert = (X509PublicKeyCertificate)result.get(f);
296 X509Certificate certificate = Util.getCertificate(iaikCert.getValue().getByteArrayValue());
297 String alias = iaikCert.getLabel().toString();
298
299 lX509Certificates.add (certificate);
300 lAliases.add(alias);
301 mapCertificates.put(alias, certificate);
302 }
303 this.certificates = lX509Certificates.toArray(new X509Certificate[0]);
304 this.aliases = lAliases.toArray(new String[0]);
305 } catch (Exception e) {
306 throw new SearchingException ("No ha sido posible obtener la lista de certificados del dispositivo", e);
307 }
308
309 }
310
311 }