Skip to content

Commit 5341167

Browse files
author
Olesja Aarma
committed
Merge branch 'RM-5019' into 'master'
RM-5019: lib: add method to load keys from smart-card with pre-saved pin See merge request cdoc2/cdoc2-java-ref-impl!112
2 parents 460f375 + 6b2d65b commit 5341167

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

cdoc2-lib/README.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,20 +279,32 @@ with `cdoc2-lib` verify that you can access id-card with [DigiDoc4](https://gith
279279
Path destDir = Paths.get("/tmp");
280280
Integer slot = 0;
281281
String alias = "Isikutuvastus";
282-
DecryptionKeyMaterial dkm = DecryptionKeyMaterial.fromKeyPair(
283-
Pkcs11Tools.loadFromPKCS11Interactively(
284-
"/usr/lib/x86_64-linux-gnu/opensc-pkcs11.so", // pkcs11 driver location, differs on different platforms
285-
slot,
286-
alias
287-
)
282+
283+
// load keys by asking pin code interactively
284+
KeyPair keyPair = Pkcs11Tools.loadFromPKCS11Interactively(
285+
"/usr/lib/x86_64-linux-gnu/opensc-pkcs11.so", // pkcs11 driver location, differs on different platforms
286+
slot,
287+
alias
288288
);
289289

290+
// or load keys with a given pin code
291+
char[] pin;
292+
KeyPair keyPair = Pkcs11Tools.loadFromPKCS11WithPin(
293+
"/usr/lib/x86_64-linux-gnu/opensc-pkcs11.so", // pkcs11 driver location, differs on different platforms
294+
slot,
295+
new PasswordProtection(pin),
296+
alias
297+
);
298+
299+
DecryptionKeyMaterial dkm = DecryptionKeyMaterial.fromKeyPair(keyPair);
300+
290301
List<String> extractedFiles = new CDocDecrypter()
291-
.withCDoc(cdoc2FileToDecrypt.toFile())
292-
.withRecipient(dkm)
293-
.withDestinationDirectory(destDir.toFile())
294-
.decrypt();
302+
.withCDoc(cdoc2FileToDecrypt.toFile())
303+
.withRecipient(dkm)
304+
.withDestinationDirectory(destDir.toFile())
305+
.decrypt();
295306
```
307+
296308
`/usr/lib/x86_64-linux-gnu/opensc-pkcs11.so` is location of OpenSC pkcs11 driver library. Some info
297309
on setting up pcks11 on Ubuntu can be found in [pkcs11.README](https://github.com/open-eid/cdoc2-java-ref-impl/blob/master/cdoc2-lib/pkcs11.README)
298310

@@ -379,8 +391,6 @@ Similar to previous example, to decrypt cdoc2 with server recipient,
379391
[cdoc2-capsule-server](https://github.com/open-eid/cdoc2-capsule-server)client needs to be configured.
380392

381393
```java
382-
383-
384394
Path cdoc2FileToDecrypt = Paths.get("/tmp/second.cdoc2");
385395
Path destDir = Paths.get("/tmp");
386396
Integer slot = 0;

cdoc2-lib/src/main/java/ee/cyber/cdoc2/crypto/Pkcs11Tools.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private Pkcs11Tools() {
6262
*
6363
* @param pkcs11LibPath pkcs11 provider library location, defaults described above if null
6464
* @param slot the slot number with the keys
65-
* @param keyAlias key alias (optional) to use in case the are more than one entry in the
65+
* @param keyAlias key alias (optional) to use in case there are more than one entry in the
6666
* keystore
6767
* @return KeyPair
6868
*
@@ -88,6 +88,34 @@ public static KeyPair loadFromPKCS11Interactively(String pkcs11LibPath, Integer
8888
return new KeyPair(entry.getValue().getPublicKey(), entry.getKey());
8989
}
9090

91+
/**
92+
* Load KeyPair using automatically generated SunPKCS11 configuration with given pin code.
93+
*
94+
* @param pkcs11LibPath pkcs11 provider library location, defaults described above if null
95+
* @param slot the slot number with the keys
96+
* @param pin pin code wrapped into PasswordProtection object
97+
* @param keyAlias key alias (optional) to use in case there are more than one entry in the
98+
* keystore
99+
* @return KeyPair
100+
*
101+
* @see <a href="https://docs.oracle.com/en/java/javase/17/security/pkcs11-reference-guide1.html">
102+
* SunPKCS11 documentation Table 5-1</a>
103+
*/
104+
public static KeyPair loadFromPKCS11WithPin(
105+
String pkcs11LibPath,
106+
Integer slot,
107+
KeyStore.PasswordProtection pin,
108+
@Nullable String keyAlias
109+
) throws GeneralSecurityException, IOException {
110+
var entry = loadFromPKCS11(
111+
Pkcs11Tools.createSunPkcsConfigurationFile(null, pkcs11LibPath, slot),
112+
pin,
113+
keyAlias
114+
);
115+
116+
return new KeyPair(entry.getValue().getPublicKey(), entry.getKey());
117+
}
118+
91119
/**
92120
* Init OpenSC based KeyStore (like EST-EID). OpenSC must be installed. Creates configuration file for SunPKCS11,
93121
* configures SunPkcs11 Provider and loads and configures PKCS11 KeyStore from SunPkcs11 Provider.

0 commit comments

Comments
 (0)