@@ -35,6 +35,9 @@ Welcome to the PKCS11 Java Wrapper! This comprehensive Java library provides a r
35
35
- 🔐 Data encryption and decryption
36
36
- 🧰 Utility functions for common PKCS #11 operations
37
37
- 🛡️ Comprehensive exception handling for robust error management
38
+ - 📱 Multi-device support with hot-plug capabilities
39
+ - 🔄 Automatic device state monitoring
40
+ - 🎯 Device filtering by capabilities and state
38
41
- 🧪 Extensive test coverage ensuring reliability
39
42
40
43
## 📂 Project Structure
54
57
│ │ │ └───pkcs11
55
58
│ │ │ │ PKCS11Crypto.java
56
59
│ │ │ │ PKCS11Initializer.java
60
+ │ │ │ │ PKCS11DeviceManager.java
57
61
│ │ │ │ PKCS11Manager.java
58
62
│ │ │ │ PKCS11Session.java
59
63
│ │ │ │ PKCS11Signer.java
@@ -62,7 +66,11 @@ Root:.
62
66
│ │ │ │ // Various exception classes
63
67
│ │ │ └───model
64
68
│ │ │ CertificateInfo.java
69
+ │ │ │ DeviceCapability.java
70
+ │ │ │ DeviceChangeListener.java
71
+ │ │ │ DeviceState.java
65
72
│ │ │ KeyCertificatePair.java
73
+ │ │ │ PKCS11Device.java
66
74
│ │ │ SupportedAlgorithm.java
67
75
│ └───test
68
76
│ └───java
77
85
78
86
### Prerequisites
79
87
80
- - Java Development Kit (JDK) 17 or higher
88
+ - Java Development Kit (JDK) 21 or higher
81
89
- Maven 4.0.0 or higher
82
90
- A PKCS #11 compatible hardware security module or smart card
83
91
- The appropriate PKCS #11 library for your device (e.g., opensc-pkcs11.dll)
@@ -111,12 +119,46 @@ import java.nio.file.Paths;
111
119
112
120
public class PKCS11Example {
113
121
public static void main (String [] args ) {
114
- String userDir = System . getProperty(" user.dir" );
115
- PKCS11 example = new PKCS11 (
116
- Paths . get(userDir, " lib" , " opensc-pkcs11.dll" ),
117
- " your_pin_here"
118
- );
119
- example. run();
122
+ try (PKCS11Manager manager = new PKCS11Manager (Paths . get(" path/to/pkcs11/library" ))) {
123
+ // Register device change listener
124
+ manager. registerDeviceChangeListener(new DeviceChangeListener () {
125
+ @Override
126
+ public void onDeviceConnected (PKCS11Device device ) {
127
+ System . out. println(" Device connected: " + device. getLabel());
128
+ }
129
+
130
+ @Override
131
+ public void onDeviceDisconnected (PKCS11Device device ) {
132
+ System . out. println(" Device disconnected: " + device. getLabel());
133
+ }
134
+
135
+ @Override
136
+ public void onDeviceStateChanged (PKCS11Device device , DeviceState oldState ) {
137
+ System . out. println(" Device state changed: " + device. getLabel() +
138
+ " from " + oldState + " to " + device. getState());
139
+ }
140
+
141
+ @Override
142
+ public void onDeviceError (PKCS11Device device , Exception error ) {
143
+ System . err. println(" Device error: " + device. getLabel() +
144
+ " - " + error. getMessage());
145
+ }
146
+ });
147
+
148
+ // List available devices
149
+ List<PKCS11Device > devices = manager. listDevices();
150
+ System . out. println(" Available devices: " + devices. size());
151
+
152
+ // Select device and perform operations
153
+ if (! devices. isEmpty()) {
154
+ PKCS11Device selectedDevice = devices. get(0 );
155
+ String pin = " your_pin_here" ;
156
+
157
+ try (PKCS11Session session = manager. openSession(selectedDevice, pin)) {
158
+ // Perform operations with session...
159
+ }
160
+ }
161
+ }
120
162
}
121
163
}
122
164
```
@@ -361,7 +403,43 @@ classDiagram
361
403
-AlgorithmType type
362
404
+enum AlgorithmType
363
405
}
406
+ class PKCS11DeviceManager {
407
+ -Pkcs11 pkcs11
408
+ -Map<NativeLong, PKCS11Device> devices
409
+ -Set<DeviceChangeListener> listeners
410
+ -ScheduledExecutorService deviceMonitor
411
+ +listDevices(): List<PKCS11Device>
412
+ +listDevicesByState(DeviceState): List<PKCS11Device>
413
+ +listDevicesByCapability(DeviceCapability): List<PKCS11Device>
414
+ +getDevice(NativeLong): Optional<PKCS11Device>
415
+ +registerDeviceChangeListener(DeviceChangeListener)
416
+ +unregisterDeviceChangeListener(DeviceChangeListener)
417
+ }
418
+
419
+ class PKCS11Device {
420
+ -NativeLong slotId
421
+ -String label
422
+ -String manufacturer
423
+ -String model
424
+ -String serialNumber
425
+ -Set<DeviceCapability> capabilities
426
+ -DeviceState state
427
+ +getDetailedInfo(): Map<String, String>
428
+ +updateState(): boolean
429
+ +isReady(): boolean
430
+ }
431
+
432
+ class DeviceChangeListener {
433
+ <<interface>>
434
+ +onDeviceConnected(PKCS11Device)
435
+ +onDeviceDisconnected(PKCS11Device)
436
+ +onDeviceStateChanged(PKCS11Device, DeviceState)
437
+ +onDeviceError(PKCS11Device, Exception)
438
+ }
364
439
440
+ PKCS11Manager --> PKCS11DeviceManager : uses
441
+ PKCS11DeviceManager --> PKCS11Device : manages
442
+ PKCS11DeviceManager --> DeviceChangeListener : notifies
365
443
PKCS11Manager --> PKCS11Initializer : uses
366
444
PKCS11Manager --> PKCS11Session : creates
367
445
PKCS11Manager --> Pkcs11 : manages
@@ -606,6 +684,12 @@ Our test suite covers various scenarios, including:
606
684
- Encryption and decryption
607
685
- Digital signature creation and verification
608
686
- Error handling and exception scenarios
687
+ - Device detection and management
688
+ - Device state monitoring
689
+ - Hot-plug capability testing
690
+ - Device capability filtering
691
+ - Device change event handling
692
+ - Multi-device operations
609
693
610
694
## 🤝 Contributing
611
695
0 commit comments