01: package org.bouncycastle.asn1.x509;
02:
03: import org.bouncycastle.asn1.ASN1Encodable;
04: import org.bouncycastle.asn1.ASN1OctetString;
05: import org.bouncycastle.asn1.ASN1TaggedObject;
06: import org.bouncycastle.asn1.DERObject;
07: import org.bouncycastle.asn1.DEROctetString;
08: import org.bouncycastle.crypto.Digest;
09: import org.bouncycastle.crypto.digests.SHA1Digest;
10:
11: /**
12: * The SubjectKeyIdentifier object.
13: * <pre>
14: * SubjectKeyIdentifier::= OCTET STRING
15: * </pre>
16: */
17: public class SubjectKeyIdentifier extends ASN1Encodable {
18: private byte[] keyidentifier;
19:
20: public static SubjectKeyIdentifier getInstance(
21: ASN1TaggedObject obj, boolean explicit) {
22: return getInstance(ASN1OctetString.getInstance(obj, explicit));
23: }
24:
25: public static SubjectKeyIdentifier getInstance(Object obj) {
26: if (obj instanceof SubjectKeyIdentifier) {
27: return (SubjectKeyIdentifier) obj;
28: }
29:
30: if (obj instanceof SubjectPublicKeyInfo) {
31: return new SubjectKeyIdentifier((SubjectPublicKeyInfo) obj);
32: }
33:
34: if (obj instanceof ASN1OctetString) {
35: return new SubjectKeyIdentifier((ASN1OctetString) obj);
36: }
37:
38: if (obj instanceof X509Extension) {
39: return getInstance(X509Extension
40: .convertValueToObject((X509Extension) obj));
41: }
42:
43: throw new IllegalArgumentException(
44: "Invalid SubjectKeyIdentifier: "
45: + obj.getClass().getName());
46: }
47:
48: public SubjectKeyIdentifier(byte[] keyid) {
49: this .keyidentifier = keyid;
50: }
51:
52: public SubjectKeyIdentifier(ASN1OctetString keyid) {
53: this .keyidentifier = keyid.getOctets();
54:
55: }
56:
57: /**
58: *
59: * Calulates the keyidentifier using a SHA1 hash over the BIT STRING
60: * from SubjectPublicKeyInfo as defined in RFC2459.
61: *
62: **/
63: public SubjectKeyIdentifier(SubjectPublicKeyInfo spki) {
64: Digest digest = new SHA1Digest();
65: byte[] resBuf = new byte[digest.getDigestSize()];
66:
67: byte[] bytes = spki.getPublicKeyData().getBytes();
68: digest.update(bytes, 0, bytes.length);
69: digest.doFinal(resBuf, 0);
70: this .keyidentifier = resBuf;
71: }
72:
73: public byte[] getKeyIdentifier() {
74: return keyidentifier;
75: }
76:
77: public DERObject toASN1Object() {
78: return new DEROctetString(keyidentifier);
79: }
80: }
|