Skip to content

Commit 611dc08

Browse files
committed
Initial GnuDivertToCard format support. Relates to github #116
1 parent eb86764 commit 611dc08

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

CONTRIBUTORS.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@
564564
<li>zhsnew &lt;https://github.com/zhsnew&gt; - correct AsconCXof128 implementation and add test vectors</li>
565565
<li>mt-johan &lt;https://github.com/mt-johan&gt; - patch to preserve PRF on initializing from protectionAlgorithm with PBMAC1.</li>
566566
<li>oscerd &lt;https://github.com/oscerd&gt; - comment corrections in GMSSRootSig.java.</li>
567+
<li>Léonard Dallot &lt;[email protected]&gt; - initial patches for GNU PG Divert to card format support.</li>
567568
</ul>
568569
</body>
569570
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.bouncycastle.bcpg;
2+
3+
/**
4+
* Add a constructor fort GNU-extended S2K
5+
* <p>
6+
* This extension is documented on GnuPG documentation DETAILS file,
7+
* section "GNU extensions to the S2K algorithm". Its support is
8+
* already present in S2K class but lack for a constructor.
9+
*/
10+
public class GnuExtendedS2K
11+
extends S2K
12+
{
13+
public GnuExtendedS2K(int mode)
14+
{
15+
super(0x0);
16+
this.type = GNU_DUMMY_S2K;
17+
this.protectionMode = mode;
18+
}
19+
}

pg/src/main/java/org/bouncycastle/openpgp/PGPSecretKey.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.bouncycastle.bcpg.X25519SecretBCPGKey;
3737
import org.bouncycastle.bcpg.X448SecretBCPGKey;
3838
import org.bouncycastle.gpg.SExprParser;
39+
import org.bouncycastle.openpgp.operator.GnuDivertToCardSecretKeyEncryptor;
3940
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
4041
import org.bouncycastle.openpgp.operator.PBEProtectionRemoverFactory;
4142
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
@@ -552,7 +553,8 @@ public Iterator<PGPUserAttributeSubpacketVector> getUserAttributes()
552553
return pub.getUserAttributes();
553554
}
554555

555-
private byte[] extractKeyData(PBESecretKeyDecryptor decryptorFactory) throws PGPException
556+
private byte[] extractKeyData(PBESecretKeyDecryptor decryptorFactory)
557+
throws PGPException
556558
{
557559
byte[] encData = secret.getSecretKeyData();
558560

@@ -887,7 +889,8 @@ public static PGPSecretKey copyWithNewPassword(
887889
byte[] keyData;
888890
int newEncAlgorithm = SymmetricKeyAlgorithmTags.NULL;
889891

890-
if (newKeyEncryptor == null || newKeyEncryptor.getAlgorithm() == SymmetricKeyAlgorithmTags.NULL)
892+
if (newKeyEncryptor == null
893+
|| (newKeyEncryptor.getAlgorithm() == SymmetricKeyAlgorithmTags.NULL && !(newKeyEncryptor instanceof GnuDivertToCardSecretKeyEncryptor)))
891894
{
892895
s2kUsage = SecretKeyPacket.USAGE_NONE;
893896
if (key.secret.getS2KUsage() == SecretKeyPacket.USAGE_SHA1) // SHA-1 hash, need to rewrite checksum
@@ -997,7 +1000,7 @@ public static PGPSecretKey copyWithNewPassword(
9971000

9981001
SecretKeyPacket secret;
9991002

1000-
if (newKeyEncryptor!= null && newKeyEncryptor.getAeadAlgorithm() > 0)
1003+
if (newKeyEncryptor != null && newKeyEncryptor.getAeadAlgorithm() > 0)
10011004
{
10021005
s2kUsage = SecretKeyPacket.USAGE_AEAD;
10031006
secret = generateSecretKeyPacket(!(key.secret instanceof SecretSubkeyPacket), key.secret.getPublicKeyPacket(), newEncAlgorithm, newKeyEncryptor.getAeadAlgorithm(), s2kUsage, s2k, iv, keyData);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.bouncycastle.openpgp.operator;
2+
3+
import org.bouncycastle.bcpg.GnuExtendedS2K;
4+
import org.bouncycastle.bcpg.S2K;
5+
import org.bouncycastle.openpgp.PGPException;
6+
7+
/**
8+
* Secret key encryptor that allows to represent a secret key embedded
9+
* on a smartcard, using GNU S2K extensions.
10+
* <p>
11+
* This extension is documented on GnuPG documentation DETAILS file,
12+
* section "GNU extensions to the S2K algorithm".
13+
*/
14+
public class GnuDivertToCardSecretKeyEncryptor
15+
extends PBESecretKeyEncryptor
16+
{
17+
private byte[] serial;
18+
19+
public GnuDivertToCardSecretKeyEncryptor(PGPDigestCalculator s2kDigestCalculator, byte[] serial)
20+
{
21+
super(0, s2kDigestCalculator, 0, null, null);
22+
this.s2k = new GnuExtendedS2K(S2K.GNU_PROTECTION_MODE_DIVERT_TO_CARD);
23+
this.serial = new byte[serial.length + 1];
24+
this.serial[0] = (byte)serial.length;
25+
System.arraycopy(serial, 0, this.serial, 1, serial.length);
26+
}
27+
28+
@Override
29+
public byte[] encryptKeyData(byte[] key, byte[] keyData, int keyOff,
30+
int keyLen)
31+
throws PGPException
32+
{
33+
if (serial != null && serial.length > 16)
34+
{
35+
byte[] result = new byte[17];
36+
System.arraycopy(serial, 0, result, 0, result.length);
37+
return result;
38+
}
39+
return serial;
40+
}
41+
42+
@Override
43+
public byte[] getKey()
44+
throws PGPException
45+
{
46+
return null;
47+
}
48+
49+
@Override
50+
public byte[] getCipherIV()
51+
{
52+
return new byte[0];
53+
}
54+
}

0 commit comments

Comments
 (0)