]> WPIA git - gigi.git/blob - src/club/wpia/gigi/crypto/key/KeyCheck.java
add: public key check testing for ROCA (Return of Coppersmith Attack) vulnerability
[gigi.git] / src / club / wpia / gigi / crypto / key / KeyCheck.java
1 package club.wpia.gigi.crypto.key;
2
3 import java.security.PublicKey;
4 import java.util.Collections;
5 import java.util.LinkedHashSet;
6 import java.util.List;
7 import java.util.Set;
8
9 import club.wpia.gigi.GigiApiException;
10
11 public abstract class KeyCheck {
12
13     protected static final Set<KeyCheck> checks = new LinkedHashSet<KeyCheck>();
14
15     public static List<KeyCheck> getChecks() {
16         return Collections.list(Collections.enumeration(checks));
17     }
18
19     public static void register(KeyCheck check) {
20         checks.add(check);
21     }
22
23     public abstract void check(PublicKey key) throws GigiApiException;
24
25     public static void checkKey(PublicKey key) throws GigiApiException {
26
27         if (checks.isEmpty() || checks.size() < 2) {
28             // Mandatory checks are registered here
29             register(new KeyCheckSmallFactors());
30             register(new KeyCheckROCA());
31         }
32
33         if (key == null) {
34             throw new GigiApiException("Failed key sanity check: No key given!");
35         }
36
37         for (KeyCheck kc : checks) {
38             kc.check(key);
39         }
40
41     }
42
43     @Override
44     public boolean equals(Object o) {
45         if (o == null) {
46             return false;
47         }
48
49         if (o == this) {
50             return true;
51         }
52
53         return getClass().equals(o.getClass());
54     }
55
56     @Override
57     public int hashCode() {
58         return getClass().hashCode();
59     }
60
61 }