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