01: package org.bouncycastle.ocsp;
02:
03: import org.bouncycastle.asn1.ASN1InputStream;
04: import org.bouncycastle.asn1.ASN1OctetString;
05: import org.bouncycastle.asn1.DEROctetString;
06: import org.bouncycastle.asn1.ocsp.ResponderID;
07: import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
08: import org.bouncycastle.jce.X509Principal;
09:
10: import javax.security.auth.x500.X500Principal;
11: import java.io.IOException;
12: import java.security.MessageDigest;
13: import java.security.PublicKey;
14:
15: /**
16: * Carrier for a ResponderID.
17: */
18: public class RespID {
19: ResponderID id;
20:
21: public RespID(ResponderID id) {
22: this .id = id;
23: }
24:
25: public RespID(X500Principal name) {
26: try {
27: this .id = new ResponderID(new X509Principal(name
28: .getEncoded()));
29: } catch (IOException e) {
30: throw new IllegalArgumentException("can't decode name.");
31: }
32: }
33:
34: public RespID(PublicKey key) throws OCSPException {
35: try {
36: MessageDigest digest = MessageDigest.getInstance("SHA1");
37:
38: ASN1InputStream aIn = new ASN1InputStream(key.getEncoded());
39: SubjectPublicKeyInfo info = SubjectPublicKeyInfo
40: .getInstance(aIn.readObject());
41:
42: digest.update(info.getPublicKeyData().getBytes());
43:
44: ASN1OctetString keyHash = new DEROctetString(digest
45: .digest());
46:
47: this .id = new ResponderID(keyHash);
48: } catch (Exception e) {
49: throw new OCSPException("problem creating ID: " + e, e);
50: }
51: }
52:
53: public ResponderID toASN1Object() {
54: return id;
55: }
56:
57: public boolean equals(Object o) {
58: if (!(o instanceof RespID)) {
59: return false;
60: }
61:
62: RespID obj = (RespID) o;
63:
64: return id.equals(obj.id);
65: }
66:
67: public int hashCode() {
68: return id.hashCode();
69: }
70: }
|