]> WPIA git - gigi.git/blob - src/club/wpia/gigi/ocsp/OCSPIssuerId.java
add: implement OCSP serving
[gigi.git] / src / club / wpia / gigi / ocsp / OCSPIssuerId.java
1 package club.wpia.gigi.ocsp;
2
3 import java.security.MessageDigest;
4 import java.security.cert.X509Certificate;
5 import java.util.Arrays;
6
7 import javax.security.auth.x500.X500Principal;
8
9 import sun.security.provider.certpath.CertId;
10 import sun.security.x509.AlgorithmId;
11
12 /**
13  * Idenfies an {@link OCSPIssuer} by remembering its public key hash and its
14  * name hash together with the used hash algorithm. A {@link OCSPIssuer} can be
15  * identified by several {@link OCSPIssuerId}s when they use different hash
16  * algorithms.
17  */
18 public class OCSPIssuerId {
19
20     private final byte[] keyHash;
21
22     private final byte[] nameHash;
23
24     private final AlgorithmId alg;
25
26     /**
27      * Creates a new OCSPIssuerId for a given {@link OCSPIssuer}. The hash
28      * algorithm has to be specified twice, once for description purposes as
29      * {@link AlgorithmId} and once instantiated as {@link MessageDigest}.
30      * 
31      * @param alg
32      *            the description of the hash algorithm
33      * @param md
34      *            the instantiated hash algorithm
35      * @param iss
36      *            the issuer to hash.
37      */
38     public OCSPIssuerId(AlgorithmId alg, MessageDigest md, X509Certificate target) {
39         X500Principal dn = target.getSubjectX500Principal();
40         this.keyHash = OCSPResponder.calcKeyHash(target, md);
41         this.nameHash = md.digest(dn.getEncoded());
42         this.alg = alg;
43     }
44
45     /**
46      * Creates a new OCSPIssuerId from the {@link CertId} inside an OCSP
47      * request.
48      * 
49      * @param id
50      *            the {@link CertId}
51      */
52     public OCSPIssuerId(CertId id) {
53         keyHash = id.getIssuerKeyHash();
54         nameHash = id.getIssuerNameHash();
55         alg = id.getHashAlgorithm();
56     }
57
58     @Override
59     public int hashCode() {
60         final int prime = 31;
61         int result = 1;
62         result = prime * result + ((alg == null) ? 0 : alg.hashCode());
63         result = prime * result + Arrays.hashCode(keyHash);
64         result = prime * result + Arrays.hashCode(nameHash);
65         return result;
66     }
67
68     @Override
69     public boolean equals(Object obj) {
70         if (this == obj) {
71             return true;
72         }
73         if (obj == null) {
74             return false;
75         }
76         if (getClass() != obj.getClass()) {
77             return false;
78         }
79         OCSPIssuerId other = (OCSPIssuerId) obj;
80         if (alg == null) {
81             if (other.alg != null) {
82                 return false;
83             }
84         } else if ( !alg.equals(other.alg)) {
85             return false;
86         }
87         if ( !Arrays.equals(keyHash, other.keyHash)) {
88             return false;
89         }
90         if ( !Arrays.equals(nameHash, other.nameHash)) {
91             return false;
92         }
93         return true;
94     }
95
96     public AlgorithmId getAlg() {
97         return alg;
98     }
99 }