Skip to content

Commit a892f9d

Browse files
committed
Ignore the OpenPGP encryption key if the card doesn't support the MANAGE SECURITY ENVIRONMENT command
1 parent b3ce6d5 commit a892f9d

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

jsign-core/src/main/java/net/jsign/jca/OpenPGPCard.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class OpenPGPCard {
5050
/** Data Object cache */
5151
private final Map<Integer, byte[]> dataObjectCache = new HashMap<>();
5252

53+
/** The extended capabilities flag list */
54+
private byte[] extendedCapabilities;
55+
5356
/** Information about the keys */
5457
private KeyInfo[] keyInfos;
5558

@@ -193,13 +196,13 @@ public float getVersion() throws CardException {
193196
}
194197

195198
/**
196-
* Return the available keys.
199+
* Return the keys available for signing.
197200
*/
198201
public Set<Key> getAvailableKeys() throws CardException {
199202
Set<Key> keys = new LinkedHashSet<>();
200203

201204
for (Key key : Key.values()) {
202-
if (getKeyInfo(key).isPresent()) {
205+
if (getKeyInfo(key).isPresent() && (key != Key.ENCRYPTION || supportsManageSecurityEnvironment())) {
203206
keys.add(key);
204207
}
205208
}
@@ -268,9 +271,30 @@ private KeyInfo[] getKeyInfo() throws CardException {
268271
}
269272
}
270273

274+
extendedCapabilities = relatedData.find("73", "C0").value();
275+
271276
return keyInfos;
272277
}
273278

279+
/**
280+
* Return the extended capabilities.
281+
*/
282+
private byte[] getExtendedCapabilities() throws CardException {
283+
if (extendedCapabilities == null) {
284+
TLV relatedData = TLV.parse(ByteBuffer.wrap(getData(0x6E)));
285+
System.out.println(relatedData);
286+
extendedCapabilities = relatedData.find("73", "C0").value();
287+
}
288+
return extendedCapabilities;
289+
}
290+
291+
/**
292+
* Tell if the MANAGE SECURITY ENVIRONMENT command is supported.
293+
*/
294+
protected boolean supportsManageSecurityEnvironment() throws CardException {
295+
return getVersion() > 3 && (getExtendedCapabilities()[9] & 0x01) != 0;
296+
}
297+
274298
/**
275299
* Put the specified data object on the card.
276300
*/

jsign-core/src/test/java/net/jsign/jca/OpenPGPCardTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public void testGetAvailableKeys() throws Exception {
144144

145145
Set<OpenPGPCard.Key> keys = pgpcard.getAvailableKeys();
146146
assertNotNull(keys);
147-
assertEquals("number of keys", 3, keys.size());
147+
assertEquals("number of keys", pgpcard.supportsManageSecurityEnvironment() ? 3 : 2, keys.size());
148148
}
149149

150150
@Test
@@ -212,4 +212,14 @@ public void testPutData() throws Exception {
212212

213213
assertArrayEquals("backup data", backup, pgpcard.getData(0x7F21));
214214
}
215+
216+
@Test
217+
public void testSupportsManageSecurityEnvironment() throws Exception {
218+
assumeCardPresent();
219+
220+
OpenPGPCard pgpcard = OpenPGPCard.getCard();
221+
assertNotNull("card not found", pgpcard);
222+
223+
assertTrue("MSE is not supported", pgpcard.supportsManageSecurityEnvironment());
224+
}
215225
}

0 commit comments

Comments
 (0)