]> WPIA git - gigi.git/blob - src/club/wpia/gigi/crypto/key/KeyCheck.java
Merge "add: show more certificates on the "roots" page"
[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() < 3) {
28             // Mandatory checks are registered here
29             register(new KeyCheckPublicKeyFormat());
30             register(new KeyCheckSmallFactors());
31             register(new KeyCheckROCA());
32         }
33
34         if (key == null) {
35             throw new GigiApiException("Failed key sanity check: No key given!");
36         }
37
38         for (KeyCheck kc : checks) {
39             kc.check(key);
40         }
41
42     }
43
44     @Override
45     public boolean equals(Object o) {
46         if (o == null) {
47             return false;
48         }
49
50         if (o == this) {
51             return true;
52         }
53
54         return getClass().equals(o.getClass());
55     }
56
57     @Override
58     public int hashCode() {
59         return getClass().hashCode();
60     }
61
62 }