Skip to content

Commit 81a5be7

Browse files
committed
github:115 fix to s2kUsage if previously none
1 parent e099d55 commit 81a5be7

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,16 @@ public long getKeyID()
323323
return pub.getKeyID();
324324
}
325325

326+
/**
327+
* Return the S2K usage associated with this key.
328+
*
329+
* @return the key's S2K usage
330+
*/
331+
public int getS2KUsage()
332+
{
333+
return secret.getS2KUsage();
334+
}
335+
326336
/**
327337
* Return the S2K used to process this key
328338
*
@@ -682,6 +692,10 @@ public static PGPSecretKey copyWithNewPassword(
682692
}
683693
else
684694
{
695+
if (s2kUsage == SecretKeyPacket.USAGE_NONE)
696+
{
697+
s2kUsage = SecretKeyPacket.USAGE_CHECKSUM;
698+
}
685699
if (key.secret.getPublicKeyPacket().getVersion() < 4)
686700
{
687701
// Version 2 or 3 - RSA Keys only

pg/src/test/java/org/bouncycastle/openpgp/test/PGPKeyRingTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javax.crypto.Cipher;
1212

1313
import org.bouncycastle.bcpg.HashAlgorithmTags;
14+
import org.bouncycastle.bcpg.SecretKeyPacket;
1415
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
1516
import org.bouncycastle.jce.provider.BouncyCastleProvider;
1617
import org.bouncycastle.jce.spec.ElGamalParameterSpec;
@@ -2355,9 +2356,9 @@ private void rewrapTest()
23552356
// this should succeed
23562357
PGPPrivateKey privTmp = pgpKey.extractPrivateKey(null);
23572358

2358-
if (pgpKey.getKeyID() != oldKeyID)
2359+
if (pgpKey.getKeyID() != oldKeyID || pgpKey.getS2KUsage() != SecretKeyPacket.USAGE_NONE)
23592360
{
2360-
fail("key ID mismatch");
2361+
fail("usage/key ID mismatch");
23612362
}
23622363
}
23632364

@@ -2379,9 +2380,9 @@ private void rewrapTest()
23792380
// this should succeed
23802381
PGPPrivateKey privTmp = pgpKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(newPass));
23812382

2382-
if (pgpKey.getKeyID() != oldKeyID)
2383+
if (pgpKey.getKeyID() != oldKeyID || pgpKey.getS2KUsage() != SecretKeyPacket.USAGE_CHECKSUM)
23832384
{
2384-
fail("key ID mismatch");
2385+
fail("usage/key ID mismatch");
23852386
}
23862387
}
23872388
}

0 commit comments

Comments
 (0)