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.device.model;
22
23 import iaik.pkcs.pkcs11.Module;
24 import iaik.pkcs.pkcs11.Token;
25 import iaik.pkcs.pkcs11.TokenException;
26
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Set;
33
34 import org.apache.log4j.Logger;
35
36 import es.accv.arangi.base.device.model.Pkcs11Device;
37 import es.accv.arangi.base.device.model.Pkcs11Manufacturer;
38 import es.accv.arangi.base.exception.device.DeviceNotFoundException;
39 import es.accv.arangi.base.exception.device.IAIKDLLNotFoundException;
40 import es.accv.arangi.base.exception.device.IncorrectPINException;
41 import es.accv.arangi.base.exception.device.IncorrectPUKException;
42 import es.accv.arangi.base.exception.device.LockedPINException;
43 import es.accv.arangi.base.exception.device.ModuleNotFoundException;
44 import es.accv.arangi.base.exception.device.NoSuitableDriversException;
45 import es.accv.arangi.base.exception.device.OpeningDeviceException;
46
47
48
49
50
51
52 public class Pkcs11SiemensManufacturer extends Pkcs11Manufacturer {
53
54
55
56
57 Logger logger = Logger.getLogger(Pkcs11SiemensManufacturer.class);
58
59
60
61
62 public static final String MANUFACTURER_NAME = "siemens";
63
64
65
66
67 public static final String SIEMENS_2_2_MODULE_NAME = "CardOS_PKCS11.dll";
68
69
70
71
72 public static final String SIEMENS_3_2_MODULE_NAME = "siecap11.dll";
73
74
75
76
77 private boolean version22Needed = false;
78
79
80
81
82 private boolean version32Needed = false;
83
84
85
86
87
88
89
90
91 public Pkcs11SiemensManufacturer() throws IAIKDLLNotFoundException, NoSuitableDriversException {
92 super(MANUFACTURER_NAME, SIEMENS_2_2_MODULE_NAME);
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public Pkcs11Device getInstance (String pin, boolean isPUK) throws DeviceNotFoundException, ModuleNotFoundException, IncorrectPINException, IncorrectPUKException, LockedPINException, OpeningDeviceException {
115 return getInstance(-1, pin, isPUK);
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137 public Pkcs11Device getInstance (long deviceId, String pin, boolean isPUK) throws DeviceNotFoundException, ModuleNotFoundException, IncorrectPINException, IncorrectPUKException, LockedPINException, OpeningDeviceException {
138 logger.debug("[Pkcs11SiemensManufacturer.getInstance]::Entrada::" + Arrays.asList (new Object [] { deviceId, isPUK, iaikDLLFile } ));
139
140
141 this.pkcs11Lib = SIEMENS_2_2_MODULE_NAME;
142 Pkcs11Device device22 = null;
143 try {
144 device22 = super.getInstance(deviceId, pin, isPUK);
145 if (testWrite (device22)) {
146 this.pkcs11LibPath = getPkcs11LibPaths().get(SIEMENS_2_2_MODULE_NAME);
147 return device22;
148 } else {
149 logger.debug("[Pkcs11SiemensManufacturer.getInstance]::El módulo " + SIEMENS_2_2_MODULE_NAME + " no es capaz de escribir en el dispositivo");
150 }
151 } catch (ModuleNotFoundException e) {
152
153 logger.debug("[Pkcs11SiemensManufacturer.getInstance]::El módulo " + SIEMENS_2_2_MODULE_NAME + " no está instalado");
154 } catch (DeviceNotFoundException e) {
155 logger.debug("[Pkcs11SiemensManufacturer.getInstance]::No hay ningún dispositivo para la versión 2.2 del driver de Siemens");
156 }
157
158
159 this.pkcs11Lib = SIEMENS_3_2_MODULE_NAME;
160 Pkcs11Device device32 = null;
161 try {
162 device32 = super.getInstance(deviceId, pin, isPUK);
163 if (testWrite (device32)) {
164 this.pkcs11LibPath = getPkcs11LibPaths().get(SIEMENS_3_2_MODULE_NAME);
165 return device32;
166 } else {
167 logger.debug("[Pkcs11SiemensManufacturer.getInstance]::El módulo " + SIEMENS_3_2_MODULE_NAME + " no es capaz de escribir en el dispositivo");
168 }
169 } catch (ModuleNotFoundException e) {
170
171 logger.debug("[Pkcs11SiemensManufacturer.getInstance]::El módulo " + SIEMENS_3_2_MODULE_NAME + " no está instalado");
172 throw new ModuleNotFoundException ("No hay instalado ningún módulo PKCS#11 de Siemens", e);
173 }
174
175
176 logger.info("[Pkcs11SiemensManufacturer.getInstance]::Ninguno de los módulos es capaz de escribir en el dispositivo");
177 if (device22 != null) {
178 logger.info("[Pkcs11SiemensManufacturer.getInstance]::Es necesario instalar la versión 3.2 de los módulos PKCS#11 Siemens");
179 version32Needed = true;
180 return device22;
181 }
182 if (device32 != null) {
183 logger.info("[Pkcs11SiemensManufacturer.getInstance]::Es necesario instalar la versión 2.2 de los módulos PKCS#11 Siemens");
184 version22Needed = true;
185 return device32;
186 }
187
188
189 return null;
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 public Pkcs11Device getInstance () throws DeviceNotFoundException, ModuleNotFoundException, OpeningDeviceException {
211 return getInstance(-1);
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232 public Pkcs11Device getInstance (long deviceId) throws DeviceNotFoundException, ModuleNotFoundException, OpeningDeviceException {
233 logger.debug("[Pkcs11SiemensManufacturer.getInstance]::Entrada::" + Arrays.asList (new Object [] { deviceId } ));
234
235
236 this.pkcs11Lib = SIEMENS_2_2_MODULE_NAME;
237 try {
238 return super.getInstance(deviceId);
239 } catch (ModuleNotFoundException e) {
240
241 logger.debug("[Pkcs11SiemensManufacturer.getInstance]::El módulo " + SIEMENS_2_2_MODULE_NAME + " no está instalado");
242 }
243
244
245 this.pkcs11Lib = SIEMENS_3_2_MODULE_NAME;
246 try {
247 return super.getInstance(deviceId);
248 } catch (ModuleNotFoundException e) {
249
250 logger.debug("[Pkcs11SiemensManufacturer.getInstance]::El módulo " + SIEMENS_3_2_MODULE_NAME + " no está instalado");
251 throw new ModuleNotFoundException ("No hay instalado ningún módulo PKCS#11 de Siemens", e);
252 }
253
254 }
255
256
257
258
259
260
261 public boolean isModulePresent () {
262
263
264 this.pkcs11Lib = SIEMENS_2_2_MODULE_NAME;
265 if (super.isModulePresent()) {
266 return true;
267 } else {
268
269 this.pkcs11Lib = SIEMENS_3_2_MODULE_NAME;
270 return super.isModulePresent();
271 }
272 }
273
274
275
276
277
278
279
280 public boolean isVersion22Needed() {
281 return version22Needed;
282 }
283
284
285
286
287
288
289
290 public boolean isVersion32Needed() {
291 return version32Needed;
292 }
293
294
295
296
297
298
299
300
301
302
303 public List getConnectedDevices () throws ModuleNotFoundException, DeviceNotFoundException, OpeningDeviceException {
304
305 logger.debug("[Pkcs11Manufacturer.getConnectedDevices]::Entrada::" + this.pkcs11Lib);
306
307
308 List lDevices = new ArrayList();
309
310
311 Module module22 = null;
312 try {
313
314 module22 = Module.getInstance(SIEMENS_2_2_MODULE_NAME, this.iaikDLLFile.getAbsolutePath());
315 this.pkcs11Lib = SIEMENS_2_2_MODULE_NAME;
316 logger.debug ("[Pkcs11Manufacturer.getConnectedDevices]::Se ha cargado el módulo '" + SIEMENS_2_2_MODULE_NAME + "'");
317 } catch (IOException e) {
318 logger.debug ("[Pkcs11Manufacturer.getConnectedDevices]::No ha sido posible cargar el módulo '" + SIEMENS_2_2_MODULE_NAME + "'");
319 }
320
321 if (module22 != null) {
322
323 Token[] tokens = null;
324 try {
325 tokens = getTokens(module22, getTreatableManufacturerIds());
326 } catch (DeviceNotFoundException e) {
327 logger.debug ("[Pkcs11Manufacturer.getConnectedDevices]::No ha sido posible obtener la lista de dispositivos conectados para el módulo '" + this.pkcs11Lib + "'::" + e.getMessage());
328 }
329
330 if (tokens != null) {
331
332 for (int i = 0; i < tokens.length; i++) {
333 try {
334 lDevices.add (new Pkcs11Device (false, this, this.pkcs11Lib, module22, tokens[i], tokens[i].getTokenInfo(), getSession(tokens[i])));
335 } catch (TokenException e) {
336
337 logger.debug("[Pkcs11Manufacturer.getConnectedDevices]::No se puede obtener información del token", e);
338 }
339 }
340 }
341 }
342
343 Module module32 = null;
344 try {
345
346 module32 = Module.getInstance(SIEMENS_3_2_MODULE_NAME, this.iaikDLLFile.getAbsolutePath());
347 this.pkcs11Lib = SIEMENS_3_2_MODULE_NAME;
348 } catch (IOException e1) {
349 logger.debug ("[Pkcs11Manufacturer.getConnectedDevices]::No ha sido posible cargar el módulo '" + SIEMENS_3_2_MODULE_NAME + "'");
350 }
351
352 if (module22 == null && module32 == null) {
353 throw new ModuleNotFoundException ("Ninguno de los módulos de Siemens ha podido ser cargado.");
354 }
355
356 if (module32 != null) {
357
358 Token[] tokens = null;
359 try {
360 tokens = getTokens(module32, getTreatableManufacturerIds());
361 } catch (DeviceNotFoundException e) {
362 logger.debug ("[Pkcs11Manufacturer.getConnectedDevices]::No ha sido posible obtener la lista de dispositivos conectados para el módulo '" + this.pkcs11Lib + "'::" + e.getMessage());
363 }
364
365 if (tokens != null) {
366
367 for (int i = 0; i < tokens.length; i++) {
368
369 try {
370 if (!existeTokenEnLista (tokens[i], lDevices)) {
371 lDevices.add (new Pkcs11Device (false, this, this.pkcs11Lib, module22, tokens[i], tokens[i].getTokenInfo(), getSession(tokens[i])));
372 }
373 } catch (TokenException e) {
374
375 logger.debug("[Pkcs11Manufacturer.getConnectedDevices]::No se puede obtener información del token", e);
376 }
377 }
378 }
379 }
380
381
382 if (lDevices.isEmpty()) {
383 logger.debug ("[Pkcs11Manufacturer.getConnectedDevices]::No ha sido posible obtener la lista de dispositivos conectados para Siemens");
384 throw new DeviceNotFoundException("No hay dispositivos conectados para Siemens");
385 }
386
387
388 return lDevices;
389
390 }
391
392
393
394 @Override
395 protected String[] getX86LibrariesNames() {
396 return new String[] {
397
398
399 "siecadu8.dll",
400 "siecacrd.dll",
401 "gmp4_2_1.dll",
402 "siecaces.dll",
403 "siecap15.dll",
404 "siecap11.dll"
405 };
406 }
407
408 @Override
409 protected String[] getX64LibrariesNames() {
410 return new String[] {
411 };
412 }
413
414 @Override
415 protected String[] getX86ResourcesNames() {
416 return new String[] {
417
418 };
419 }
420
421 @Override
422 protected String[] getX64ResourcesNames() {
423 return new String[] {
424
425 };
426 }
427
428 @Override
429 protected Set getTreatableManufacturerIds() {
430 return null;
431 }
432
433 @Override
434 public int getPinLength() {
435 return 8;
436 }
437
438 @Override
439 public int getPukLength() {
440 return 10;
441 }
442
443 @Override
444 protected List<String> getPkcs11Libraries() {
445 List<String> lPkcs11Libraries = new ArrayList<String>();
446 lPkcs11Libraries.add(SIEMENS_2_2_MODULE_NAME);
447 lPkcs11Libraries.add(SIEMENS_3_2_MODULE_NAME);
448
449 return lPkcs11Libraries;
450 }
451
452
453
454
455
456
457
458
459
460
461
462 private boolean testWrite(Pkcs11Device device) {
463 return !device.getTokenInfo().isWriteProtected();
464 }
465
466
467
468
469
470
471 private boolean existeTokenEnLista(Token token, List lDevices) {
472 for (Iterator iterator = lDevices.iterator(); iterator.hasNext();) {
473 Pkcs11Device device = (Pkcs11Device) iterator.next();
474 long a = device.getId();
475 long b = token.getSlot().getSlotID();
476 if(device.getId() == token.getSlot().getSlotID()) {
477 return true;
478 }
479 }
480
481 return false;
482 }
483
484 @Override
485 protected boolean runInX64() {
486 return false;
487 }
488
489 @Override
490 protected boolean runInX86() {
491 return true;
492 }
493
494
495 }