Skip to content

Commit 44d1110

Browse files
committed
PGSC: JCE implementation
1 parent 0fb6c73 commit 44d1110

18 files changed

Lines changed: 1034 additions & 57 deletions

‎build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ configure(subprojects.findAll {it.name != 'bom'}) {
297297
}
298298

299299
tasks.withType(JavaCompile).configureEach {
300-
options.debug = releaseDebug;
300+
options.debug = true;
301301
}
302302

303303
tasks.withType(Test).configureEach {

‎pg/src/main/java/org/bouncycastle/openpgp/operator/bc/RFC6637KDFCalculator.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Calculator for the EC based KDF algorithm described in RFC 6637
1717
*/
18-
class RFC6637KDFCalculator
18+
public class RFC6637KDFCalculator
1919
{
2020
// "Anonymous Sender ", which is the octet sequence
2121
private static final byte[] ANONYMOUS_SENDER = Hex.decode("416E6F6E796D6F75732053656E64657220202020");

‎pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcaPGPKeyConverter.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ public BCPGKey getBCPGKey(byte[] key)
669669
}
670670
}
671671

672-
private BCPGKey getPublicBCPGKey(int algorithm, PGPAlgorithmParameters algorithmParameters, PublicKey pubKey)
672+
public BCPGKey getPublicBCPGKey(int algorithm, PGPAlgorithmParameters algorithmParameters, PublicKey pubKey)
673673
throws PGPException
674674
{
675675
switch (algorithm)

‎pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JceExternalPublicKeyDataDecryptorFactoryBuilder.java‎

Lines changed: 397 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.bouncycastle.openpgp.operator.jcajce;
2+
3+
public class JcePublicKeyDataDecryptorFactory {
4+
}

‎pg/src/test/java/org/bouncycastle/bcpg/test/ExternalSecretKeyPacketTest.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.bouncycastle.bcpg.test;
22

33
import org.bouncycastle.bcpg.ArmoredInputStream;
4-
import org.bouncycastle.bcpg.ArmoredOutputStream;
54
import org.bouncycastle.bcpg.BCPGInputStream;
65
import org.bouncycastle.bcpg.BCPGOutputStream;
76
import org.bouncycastle.bcpg.ContainedPacket;

‎pgsc/src/main/java/org/bouncycastle/openpgp/smartcard/OpenPGPSmartCardBackend.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.bouncycastle.openpgp.smartcard;
22

3-
import org.bouncycastle.bcpg.KeyIdentifier;
43
import org.bouncycastle.bcpg.PublicKeyPacket;
54
import org.bouncycastle.openpgp.PGPException;
65
import org.bouncycastle.openpgp.PGPPublicKey;

‎pgsc/src/main/java/org/bouncycastle/openpgp/smartcard/yubikey/YubikeyOpenPGPSmartCard.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public YubikeyOpenPGPSmartCard(YubikeySmartCardBackend backend,
6161
}
6262
catch (ApduException | IOException | BadResponseException e)
6363
{
64-
throw new CardException("Cannot perform initial read from Yubikey", e);
64+
throw new CardException("Cannot perform initial read from Yubikey " + getSerialNumber(), e);
6565
}
6666
}
6767

‎pgsc/src/main/java/org/bouncycastle/openpgp/smartcard/yubikey/YubikeySmartCardBackend.java‎

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.bouncycastle.jce.provider.BouncyCastleProvider;
1212
import org.bouncycastle.openpgp.PGPException;
1313
import org.bouncycastle.openpgp.PGPKeyPair;
14-
import org.bouncycastle.openpgp.PGPPrivateKey;
1514
import org.bouncycastle.openpgp.PGPPublicKey;
1615
import org.bouncycastle.openpgp.api.KeyPassphraseProvider;
1716
import org.bouncycastle.openpgp.api.OpenPGPKey;
@@ -20,7 +19,8 @@
2019
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyConverter;
2120
import org.bouncycastle.openpgp.smartcard.OpenPGPSmartCardBackend;
2221
import org.bouncycastle.openpgp.smartcard.card.CardException;
23-
import org.bouncycastle.openpgp.smartcard.yubikey.operator.YubikeyPublicKeyDataDecryptorFactory;
22+
import org.bouncycastle.openpgp.smartcard.yubikey.operator.bc.BcYubikeyPublicKeyDataDecryptorFactory;
23+
import org.bouncycastle.openpgp.smartcard.yubikey.operator.jcajce.JceYubikeyPublicKeyDataDecryptorFactoryBuilder;
2424
import org.bouncycastle.util.Arrays;
2525

2626
import java.security.NoSuchAlgorithmException;
@@ -40,42 +40,57 @@ public class YubikeySmartCardBackend
4040
{
4141
private static final int X25519_SCALAR_SIZE = 32;
4242

43+
private final YubikeyDecryptorFactoryProvider decryptorFactoryProvider;
4344
private final JcaPGPKeyConverter converter;
4445
private final JcaKeyFingerprintCalculator fingerprintCalculator;
4546
private final Set<Integer> allowedCardSerials = new HashSet<>();
4647
private final YubiKitManager manager;
4748

4849
public static YubikeySmartCardBackend createInstance()
4950
{
50-
return createInstance(new YubiKitManager());
51+
return createInstance(bcImpl());
5152
}
5253

53-
public static YubikeySmartCardBackend createInstance(YubiKitManager yubiKitManager)
54+
public static YubikeySmartCardBackend createInstance(YubikeyDecryptorFactoryProvider decryptorFactoryProvider)
5455
{
55-
return createInstance(yubiKitManager, new BouncyCastleProvider());
56+
return createInstance(new YubiKitManager(),
57+
decryptorFactoryProvider);
5658
}
5759

5860
public static YubikeySmartCardBackend createInstance(YubiKitManager yubiKitManager,
59-
BouncyCastleProvider provider)
61+
YubikeyDecryptorFactoryProvider decryptorFactoryProvider)
6062
{
61-
return new YubikeySmartCardBackend(yubiKitManager,
63+
return createInstance(yubiKitManager,
64+
new BouncyCastleProvider(),
65+
decryptorFactoryProvider);
66+
}
67+
68+
public static YubikeySmartCardBackend createInstance(YubiKitManager yubiKitManager,
69+
BouncyCastleProvider provider,
70+
YubikeyDecryptorFactoryProvider decryptorFactoryProvider)
71+
{
72+
return new YubikeySmartCardBackend(
73+
yubiKitManager,
6274
new JcaPGPKeyConverter().setProvider(provider),
63-
new JcaKeyFingerprintCalculator().setProvider(provider));
75+
new JcaKeyFingerprintCalculator().setProvider(provider),
76+
decryptorFactoryProvider);
6477
}
6578

6679
public YubikeySmartCardBackend(YubiKitManager yubiKitManager,
6780
JcaPGPKeyConverter keyConverter,
68-
JcaKeyFingerprintCalculator fingerprintCalculator)
81+
JcaKeyFingerprintCalculator fingerprintCalculator,
82+
YubikeyDecryptorFactoryProvider decryptorFactoryProvider)
6983
{
7084
this.manager = yubiKitManager;
7185
this.converter = keyConverter;
7286
this.fingerprintCalculator = fingerprintCalculator;
87+
this.decryptorFactoryProvider = decryptorFactoryProvider;
7388
}
7489

7590
@Override
7691
public String getName()
7792
{
78-
return "Yubikit";
93+
return "Yubikit " + decryptorFactoryProvider.getName();
7994
}
8095

8196
/**
@@ -130,7 +145,7 @@ public PublicKeyDataDecryptorFactory providePublicKeyDataDecryptorFactory(
130145
KeyPassphraseProvider userPinProvider)
131146
throws PGPException
132147
{
133-
return new YubikeyPublicKeyDataDecryptorFactory(secretKey, card, userPinProvider);
148+
return decryptorFactoryProvider.provide(secretKey, card, userPinProvider);
134149
}
135150

136151
/**
@@ -276,4 +291,58 @@ private PGPPublicKey bruteForcePublicKey(PublicKey pk, Date creationTime,
276291
}
277292
return null;
278293
}
294+
295+
public interface YubikeyDecryptorFactoryProvider
296+
{
297+
PublicKeyDataDecryptorFactory provide(OpenPGPKey.OpenPGPSecretKey secretKey,
298+
YubikeyOpenPGPSmartCard card,
299+
KeyPassphraseProvider userPinProvider)
300+
throws PGPException;
301+
302+
String getName();
303+
}
304+
305+
public static YubikeyDecryptorFactoryProvider bcImpl()
306+
{
307+
return new YubikeyDecryptorFactoryProvider()
308+
{
309+
@Override
310+
public PublicKeyDataDecryptorFactory provide(OpenPGPKey.OpenPGPSecretKey secretKey,
311+
YubikeyOpenPGPSmartCard card,
312+
KeyPassphraseProvider userPinProvider)
313+
throws PGPException
314+
{
315+
return new BcYubikeyPublicKeyDataDecryptorFactory(secretKey, card, userPinProvider);
316+
}
317+
318+
@Override
319+
public String getName()
320+
{
321+
return "BCYK";
322+
}
323+
};
324+
}
325+
326+
public static YubikeyDecryptorFactoryProvider jceImpl()
327+
{
328+
return new YubikeyDecryptorFactoryProvider()
329+
{
330+
@Override
331+
public PublicKeyDataDecryptorFactory provide(OpenPGPKey.OpenPGPSecretKey secretKey,
332+
YubikeyOpenPGPSmartCard card,
333+
KeyPassphraseProvider userPinProvider)
334+
throws PGPException
335+
{
336+
return new JceYubikeyPublicKeyDataDecryptorFactoryBuilder(card, userPinProvider)
337+
.setProvider(new BouncyCastleProvider())
338+
.build(secretKey);
339+
}
340+
341+
@Override
342+
public String getName()
343+
{
344+
return "JCYK";
345+
}
346+
};
347+
}
279348
}

pgsc/src/main/java/org/bouncycastle/openpgp/smartcard/yubikey/operator/YubikeyPublicKeyDataDecryptorFactory.java renamed to pgsc/src/main/java/org/bouncycastle/openpgp/smartcard/yubikey/operator/bc/BcYubikeyPublicKeyDataDecryptorFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.bouncycastle.openpgp.smartcard.yubikey.operator;
1+
package org.bouncycastle.openpgp.smartcard.yubikey.operator.bc;
22

33
import com.yubico.yubikit.core.application.InvalidPinException;
44
import com.yubico.yubikit.core.keys.PublicKeyValues;
@@ -39,13 +39,13 @@
3939
* unwrap work stays in {@link org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory}.
4040
* ElGamal and X448 are not supported by the applet and are rejected.
4141
*/
42-
public class YubikeyPublicKeyDataDecryptorFactory
42+
public class BcYubikeyPublicKeyDataDecryptorFactory
4343
extends BcExternalPublicKeyDataDecryptorFactory
4444
{
4545
private final KeyPassphraseProvider userPinProvider;
4646
private final YubikeyOpenPGPSmartCard yubikey;
4747

48-
public YubikeyPublicKeyDataDecryptorFactory(OpenPGPKey.OpenPGPSecretKey secretKey,
48+
public BcYubikeyPublicKeyDataDecryptorFactory(OpenPGPKey.OpenPGPSecretKey secretKey,
4949
YubikeyOpenPGPSmartCard yubikey,
5050
KeyPassphraseProvider userPinProvider)
5151
throws PGPException

0 commit comments

Comments
 (0)