Skip to content

Commit 4137974

Browse files
committed
Merge branch 'RM-3762' into 'master'
RM-3762: fix reading of PKCS11 properties when sent via CLI See merge request cdoc2/cdoc2-java-ref-impl!50
2 parents 4122319 + 618b99f commit 4137974

File tree

3 files changed

+66
-29
lines changed

3 files changed

+66
-29
lines changed

cdoc2-cli/src/main/java/ee/cyber/cdoc2/cli/commands/CDocCreateCmd.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ public Void call() throws Exception {
123123
LabeledPassword labeledPassword = null;
124124
if (this.recipient.labeledPasswordParam != null) {
125125
labeledPassword = (this.recipient.labeledPasswordParam.isEmpty())
126-
? InteractiveCommunicationUtil.readPasswordAndLabelInteractively(true)
127-
: this.recipient.labeledPasswordParam.labeledPassword();
126+
? InteractiveCommunicationUtil.readPasswordAndLabelInteractively(true)
127+
: this.recipient.labeledPasswordParam.labeledPassword();
128128
}
129129

130130
List<EncryptionKeyMaterial> recipients = EncryptionKeyMaterial.collectionBuilder()

cdoc2-lib/src/test/java/ee/cyber/cdoc2/crypto/Pkcs11DeviceConfiguration.java

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,52 @@
1414
/**
1515
* Test configuration for running PKCS11 tests using a hardware device.
1616
*/
17-
public record Pkcs11DeviceConfiguration(
18-
// full path to the PKCS11 provider library
19-
String pkcs11Library,
17+
public class Pkcs11DeviceConfiguration {
2018

21-
// the PKCS11 device slot
22-
int slot,
19+
private static final Logger log = LoggerFactory.getLogger(Pkcs11DeviceConfiguration.class);
2320

24-
// alias of the key in the keystore to use (if multiple keys in the keystore)
25-
@Nullable String keyAlias,
21+
public Pkcs11DeviceConfiguration() {
22+
load();
23+
}
2624

27-
// the keystore pin
28-
char[] pin,
25+
private String pkcs11Library;
26+
// the PKCS11 device slot
27+
private int slot;
28+
// alias of the key in the keystore to use (if multiple keys in the keystore)
29+
private @Nullable String keyAlias;
30+
// the keystore pin
31+
private char[] pin;
32+
// part of the CN field in the certificate
33+
private String certCn;
2934

30-
// part of the CN field in the certificate
31-
String certCn) {
35+
public String getPkcs11Library() {
36+
return pkcs11Library;
37+
}
3238

33-
private static final Logger log = LoggerFactory.getLogger(Pkcs11DeviceConfiguration.class);
39+
public int getSlot() {
40+
return slot;
41+
}
42+
43+
public char[] getPin() {
44+
return pin;
45+
}
46+
47+
@Nullable
48+
public String getKeyAlias() {
49+
return keyAlias;
50+
}
51+
52+
public String getCertCn() {
53+
return certCn;
54+
}
3455

3556
/**
3657
* Loads the PKCS11 device configuration from a file on the classpath.
3758
* <p>
3859
* The properties file can be specified with the system property cdoc2.pkcs11.test-configuration
3960
* e.g -D cdoc2.pkcs11.test-configuration=pkcs11-test-idcard.properties
40-
*
4161
*/
42-
public static Pkcs11DeviceConfiguration load() {
62+
private void load() {
4363
final String classpath = "classpath:";
4464
String filename = System.getProperty(
4565
"cdoc2.pkcs11.conf-file",
@@ -51,18 +71,18 @@ public static Pkcs11DeviceConfiguration load() {
5171
} else {
5272
propertyFileName = new File(filename).getAbsolutePath();
5373
}
54-
return loadFromPropertiesFile(propertyFileName);
74+
loadFromPropertiesFile(propertyFileName);
5575
}
5676

57-
private static Pkcs11DeviceConfiguration loadFromPropertiesFile(String filename) {
77+
private void loadFromPropertiesFile(String filename) {
5878
log.info("Loading PKCS11 device configuration from {}", filename);
5979

6080
try (InputStream is
6181
= Resources.getResourceAsStream(filename, Pkcs11Test.class.getClassLoader())) {
6282
var properties = new Properties();
6383
properties.load(is);
6484

65-
return new Pkcs11DeviceConfiguration(
85+
init(
6686
getRequiredProperty(properties, "pkcs11.library"),
6787
Integer.parseInt(getRequiredProperty(properties, "pkcs11.slot")),
6888
properties.getProperty("pkcs11.key-alias"),
@@ -75,7 +95,21 @@ private static Pkcs11DeviceConfiguration loadFromPropertiesFile(String filename)
7595
}
7696
}
7797

78-
private static String getRequiredProperty(Properties properties, String property) {
98+
private void init(
99+
String xPkcs11Library,
100+
int xSlot,
101+
String xKeyAlias,
102+
char[] xPin,
103+
String xCertCn
104+
) {
105+
this.pkcs11Library = xPkcs11Library;
106+
this.slot = xSlot;
107+
this.keyAlias = xKeyAlias;
108+
this.pin = xPin;
109+
this.certCn = xCertCn;
110+
}
111+
112+
private String getRequiredProperty(Properties properties, String property) {
79113
return Optional.ofNullable(properties.getProperty(property))
80114
.orElseThrow(() -> new IllegalArgumentException("Required property '" + property + "' not found"));
81115
}

cdoc2-lib/src/test/java/ee/cyber/cdoc2/crypto/Pkcs11Test.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
2222
import static org.junit.jupiter.api.Assertions.assertTrue;
2323

24+
2425
/**
2526
* These tests will fail without a PKCS11 device (smart card, usb token).
2627
* The device and its details can be configured using a properties file under src/test/resources/
@@ -31,15 +32,15 @@ class Pkcs11Test {
3132
private static final Logger log = LoggerFactory.getLogger(Pkcs11Test.class);
3233

3334
// load pkcs11 device properties
34-
private static final Pkcs11DeviceConfiguration CONF = Pkcs11DeviceConfiguration.load();
35+
private Pkcs11DeviceConfiguration conf = new Pkcs11DeviceConfiguration();
3536

3637
@Test
3738
@Tag("pkcs11")
3839
void testLoadKeyInteractively() throws Exception {
3940
// seems that when pin has already been provided to SunPKCS11, then pin is not asked again
4041
// so running this test with other tests doesn't make much sense
4142
KeyPair keyPair = Pkcs11Tools.loadFromPKCS11Interactively(
42-
CONF.pkcs11Library(), CONF.slot(), CONF.keyAlias()
43+
conf.getPkcs11Library(), conf.getSlot(), conf.getKeyAlias()
4344
);
4445

4546
if (Crypto.isECPKCS11Key(keyPair.getPrivate())) {
@@ -51,9 +52,9 @@ void testLoadKeyInteractively() throws Exception {
5152
@Tag("pkcs11")
5253
void testLoadCert() throws Exception {
5354
var pair = Pkcs11Tools.loadFromPKCS11(
54-
Pkcs11Tools.createSunPkcsConfigurationFile(null, CONF.pkcs11Library(), CONF.slot()),
55-
new KeyStore.PasswordProtection(CONF.pin()),
56-
CONF.keyAlias()
55+
Pkcs11Tools.createSunPkcsConfigurationFile(null, conf.getPkcs11Library(), conf.getSlot()),
56+
new KeyStore.PasswordProtection(conf.getPin()),
57+
conf.getKeyAlias()
5758
);
5859

5960
X509Certificate cert = pair.getValue();
@@ -73,7 +74,7 @@ void testLoadCert() throws Exception {
7374
log.debug("CN {}", cn);
7475

7576
assertEquals(1, cn.size());
76-
assertTrue(cn.get(0).contains(CONF.certCn()));
77+
assertTrue(cn.get(0).contains(conf.getCertCn()));
7778
}
7879

7980
@Test
@@ -89,11 +90,13 @@ void testContainerUsingPKCS11Key(@TempDir Path tempDir) throws Exception {
8990
"testContainerUsingPKCS11Key", null);
9091
}
9192

92-
private static KeyPair loadFromPKCS11() throws Exception {
93-
Path confPath = Pkcs11Tools.createSunPkcsConfigurationFile("OpenSC", CONF.pkcs11Library(), CONF.slot());
93+
private KeyPair loadFromPKCS11() throws Exception {
94+
Path confPath = Pkcs11Tools.createSunPkcsConfigurationFile(
95+
"OpenSC", conf.getPkcs11Library(), conf.getSlot());
9496
var entry = Pkcs11Tools.loadFromPKCS11(
95-
confPath, new KeyStore.PasswordProtection(CONF.pin()), CONF.keyAlias()
97+
confPath, new KeyStore.PasswordProtection(conf.getPin()), conf.getKeyAlias()
9698
);
9799
return new KeyPair(entry.getValue().getPublicKey(), entry.getKey());
98100
}
101+
99102
}

0 commit comments

Comments
 (0)