Skip to content

Commit ec1a2cd

Browse files
ligefeiBouncycastledghgit
authored andcommitted
AbstractOpenPGPDocumentSignatureGenerator.getPreferredHashAlgorithm
1 parent 146c93a commit ec1a2cd

File tree

5 files changed

+71
-18
lines changed

5 files changed

+71
-18
lines changed

ant/jdk15+.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272

7373
</copy>
7474
<copy todir="${src.dir}" overwrite="true">
75+
<fileset dir="core/src/main/jdk1.5" includes="**/*.java" />
7576
<fileset dir="prov/src/main/jdk1.5" includes="**/*.java" />
7677
<fileset dir="prov/src/test/jdk1.5" includes="**/*.java" />
7778
<fileset dir="pkix/src/main/jdk1.5" includes="**/*.java" />
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.bouncycastle.util;
2+
3+
import java.io.IOException;
4+
5+
public class Exceptions
6+
{
7+
public static IllegalArgumentException illegalArgumentException(String message, Throwable cause)
8+
{
9+
return new IllegalArgumentException(message, cause);
10+
}
11+
12+
public static IllegalStateException illegalStateException(String message, Throwable cause)
13+
{
14+
return new IllegalStateException(message, cause);
15+
}
16+
17+
public static IOException ioException(String message, Throwable cause)
18+
{
19+
return new IOException(message + "-" + cause.getMessage());
20+
}
21+
22+
}

pg/src/main/java/org/bouncycastle/openpgp/api/AbstractOpenPGPDocumentSignatureGenerator.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package org.bouncycastle.openpgp.api;
22

33
import java.util.ArrayList;
4-
import java.util.Arrays;
54
import java.util.Date;
65
import java.util.Iterator;
76
import java.util.List;
8-
import java.util.function.IntPredicate;
97

108
import org.bouncycastle.bcpg.sig.PreferredAlgorithms;
119
import org.bouncycastle.openpgp.PGPException;
@@ -263,22 +261,41 @@ private int getPreferredHashAlgorithm(OpenPGPCertificate.OpenPGPComponentKey key
263261
PreferredAlgorithms hashPreferences = key.getHashAlgorithmPreferences();
264262
if (hashPreferences != null)
265263
{
266-
int[] pref = Arrays.stream(hashPreferences.getPreferences())
267-
.filter(new IntPredicate()
268-
{ // Replace lambda with anonymous class for IntPredicate
269-
@Override
270-
public boolean test(int it)
271-
{
272-
return policy.isAcceptableDocumentSignatureHashAlgorithm(it, new Date());
273-
}
274-
})
275-
.toArray();
276-
if (pref.length != 0)
264+
int[] prefs = hashPreferences.getPreferences();
265+
List<Integer> acceptablePrefs = new ArrayList<Integer>();
266+
for (int i = 0; i < prefs.length; i++)
277267
{
278-
return pref[0];
268+
int algo = prefs[i];
269+
if (policy.isAcceptableDocumentSignatureHashAlgorithm(algo, new Date()))
270+
{
271+
acceptablePrefs.add(algo);
272+
}
273+
}
274+
if (!acceptablePrefs.isEmpty())
275+
{
276+
return acceptablePrefs.get(0);
279277
}
280278
}
281279
return policy.getDefaultDocumentSignatureHashAlgorithm();
280+
// PreferredAlgorithms hashPreferences = key.getHashAlgorithmPreferences();
281+
// if (hashPreferences != null)
282+
// {
283+
// int[] pref = Arrays.stream(hashPreferences.getPreferences())
284+
// .filter(new IntPredicate()
285+
// { // Replace lambda with anonymous class for IntPredicate
286+
// @Override
287+
// public boolean test(int it)
288+
// {
289+
// return policy.isAcceptableDocumentSignatureHashAlgorithm(it, new Date());
290+
// }
291+
// })
292+
// .toArray();
293+
// if (pref.length != 0)
294+
// {
295+
// return pref[0];
296+
// }
297+
// }
298+
// return policy.getDefaultDocumentSignatureHashAlgorithm();
282299
}
283300

284301
/**

pg/src/test/java/org/bouncycastle/openpgp/api/test/OpenPGPDetachedSignatureProcessorTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,21 @@ public String getName()
3535
protected void performTestWith(OpenPGPApi api)
3636
throws PGPException, IOException
3737
{
38+
String javaVersion = System.getProperty("java.version");
39+
boolean oldJDK = javaVersion.startsWith("1.5") || javaVersion.startsWith("1.6");
40+
3841
createVerifyV4Signature(api);
3942
createVerifyV6Signature(api);
4043

4144
keyPassphrasesArePairedUpProperly_keyAddedFirst(api);
4245
keyPassphrasesArePairedUpProperly_passphraseAddedFirst(api);
4346

4447
missingPassphraseThrows(api);
45-
wrongPassphraseThrows(api);
48+
49+
if (!oldJDK)
50+
{
51+
wrongPassphraseThrows(api);
52+
}
4653

4754
withoutSigningSubkeyFails(api);
4855
nonSigningSubkeyFails(api);

pg/src/test/java/org/bouncycastle/openpgp/api/test/OpenPGPMessageProcessorTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public String getName()
4848
protected void performTestWith(OpenPGPApi api)
4949
throws PGPException, IOException
5050
{
51+
String javaVersion = System.getProperty("java.version");
52+
boolean oldJDK = javaVersion.startsWith("1.5") || javaVersion.startsWith("1.6");
53+
5154
testVerificationOfSEIPD1MessageWithTamperedCiphertext(api);
5255

5356
roundtripUnarmoredPlaintextMessage(api);
@@ -64,9 +67,12 @@ protected void performTestWith(OpenPGPApi api)
6467
encryptWithV4V6KeyDecryptWithV4(api);
6568
encryptWithV4V6KeyDecryptWithV6(api);
6669

67-
encryptDecryptWithLockedKey(api);
68-
encryptDecryptWithMissingKey(api);
69-
70+
if (!oldJDK)
71+
{
72+
encryptDecryptWithLockedKey(api);
73+
encryptDecryptWithMissingKey(api);
74+
}
75+
7076
inlineSignWithV4KeyAlice(api);
7177
inlineSignWithV4KeyBob(api);
7278
inlineSignWithV6Key(api);

0 commit comments

Comments
 (0)