001: package org.bouncycastle.asn1.x509;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1OctetString;
006: import org.bouncycastle.asn1.ASN1Sequence;
007: import org.bouncycastle.asn1.ASN1TaggedObject;
008: import org.bouncycastle.asn1.DERInteger;
009: import org.bouncycastle.asn1.DERObject;
010: import org.bouncycastle.asn1.DEROctetString;
011: import org.bouncycastle.asn1.DERSequence;
012: import org.bouncycastle.asn1.DERTaggedObject;
013: import org.bouncycastle.crypto.Digest;
014: import org.bouncycastle.crypto.digests.SHA1Digest;
015:
016: import java.math.BigInteger;
017: import java.util.Enumeration;
018:
019: /**
020: * The AuthorityKeyIdentifier object.
021: * <pre>
022: * id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 35 }
023: *
024: * AuthorityKeyIdentifier ::= SEQUENCE {
025: * keyIdentifier [0] IMPLICIT KeyIdentifier OPTIONAL,
026: * authorityCertIssuer [1] IMPLICIT GeneralNames OPTIONAL,
027: * authorityCertSerialNumber [2] IMPLICIT CertificateSerialNumber OPTIONAL }
028: *
029: * KeyIdentifier ::= OCTET STRING
030: * </pre>
031: *
032: */
033: public class AuthorityKeyIdentifier extends ASN1Encodable {
034: ASN1OctetString keyidentifier = null;
035: GeneralNames certissuer = null;
036: DERInteger certserno = null;
037:
038: public static AuthorityKeyIdentifier getInstance(
039: ASN1TaggedObject obj, boolean explicit) {
040: return getInstance(ASN1Sequence.getInstance(obj, explicit));
041: }
042:
043: public static AuthorityKeyIdentifier getInstance(Object obj) {
044: if (obj instanceof AuthorityKeyIdentifier) {
045: return (AuthorityKeyIdentifier) obj;
046: }
047: if (obj instanceof ASN1Sequence) {
048: return new AuthorityKeyIdentifier((ASN1Sequence) obj);
049: }
050: if (obj instanceof X509Extension) {
051: return getInstance(X509Extension
052: .convertValueToObject((X509Extension) obj));
053: }
054:
055: throw new IllegalArgumentException("unknown object in factory");
056: }
057:
058: public AuthorityKeyIdentifier(ASN1Sequence seq) {
059: Enumeration e = seq.getObjects();
060:
061: while (e.hasMoreElements()) {
062: ASN1TaggedObject o = DERTaggedObject.getInstance(e
063: .nextElement());
064:
065: switch (o.getTagNo()) {
066: case 0:
067: this .keyidentifier = ASN1OctetString.getInstance(o,
068: false);
069: break;
070: case 1:
071: this .certissuer = GeneralNames.getInstance(o, false);
072: break;
073: case 2:
074: this .certserno = DERInteger.getInstance(o, false);
075: break;
076: default:
077: throw new IllegalArgumentException("illegal tag");
078: }
079: }
080: }
081:
082: /**
083: *
084: * Calulates the keyidentifier using a SHA1 hash over the BIT STRING
085: * from SubjectPublicKeyInfo as defined in RFC2459.
086: *
087: * Example of making a AuthorityKeyIdentifier:
088: * <pre>
089: * SubjectPublicKeyInfo apki = new SubjectPublicKeyInfo((ASN1Sequence)new ASN1InputStream(
090: * publicKey.getEncoded()).readObject());
091: * AuthorityKeyIdentifier aki = new AuthorityKeyIdentifier(apki);
092: * </pre>
093: *
094: **/
095: public AuthorityKeyIdentifier(SubjectPublicKeyInfo spki) {
096: Digest digest = new SHA1Digest();
097: byte[] resBuf = new byte[digest.getDigestSize()];
098:
099: byte[] bytes = spki.getPublicKeyData().getBytes();
100: digest.update(bytes, 0, bytes.length);
101: digest.doFinal(resBuf, 0);
102: this .keyidentifier = new DEROctetString(resBuf);
103: }
104:
105: /**
106: * create an AuthorityKeyIdentifier with the GeneralNames tag and
107: * the serial number provided as well.
108: */
109: public AuthorityKeyIdentifier(SubjectPublicKeyInfo spki,
110: GeneralNames name, BigInteger serialNumber) {
111: Digest digest = new SHA1Digest();
112: byte[] resBuf = new byte[digest.getDigestSize()];
113:
114: byte[] bytes = spki.getPublicKeyData().getBytes();
115: digest.update(bytes, 0, bytes.length);
116: digest.doFinal(resBuf, 0);
117:
118: this .keyidentifier = new DEROctetString(resBuf);
119: this .certissuer = GeneralNames.getInstance(name.toASN1Object());
120: this .certserno = new DERInteger(serialNumber);
121: }
122:
123: /**
124: * create an AuthorityKeyIdentifier with the GeneralNames tag and
125: * the serial number provided.
126: */
127: public AuthorityKeyIdentifier(GeneralNames name,
128: BigInteger serialNumber) {
129: this .keyidentifier = null;
130: this .certissuer = GeneralNames.getInstance(name.toASN1Object());
131: this .certserno = new DERInteger(serialNumber);
132: }
133:
134: /**
135: * create an AuthorityKeyIdentifier with a precomupted key identifier
136: */
137: public AuthorityKeyIdentifier(byte[] keyIdentifier) {
138: this .keyidentifier = new DEROctetString(keyIdentifier);
139: this .certissuer = null;
140: this .certserno = null;
141: }
142:
143: /**
144: * create an AuthorityKeyIdentifier with a precomupted key identifier
145: * and the GeneralNames tag and the serial number provided as well.
146: */
147: public AuthorityKeyIdentifier(byte[] keyIdentifier,
148: GeneralNames name, BigInteger serialNumber) {
149: this .keyidentifier = new DEROctetString(keyIdentifier);
150: this .certissuer = GeneralNames.getInstance(name.toASN1Object());
151: this .certserno = new DERInteger(serialNumber);
152: }
153:
154: public byte[] getKeyIdentifier() {
155: if (keyidentifier != null) {
156: return keyidentifier.getOctets();
157: }
158:
159: return null;
160: }
161:
162: public GeneralNames getAuthorityCertIssuer() {
163: return certissuer;
164: }
165:
166: public BigInteger getAuthorityCertSerialNumber() {
167: if (certserno != null) {
168: return certserno.getValue();
169: }
170:
171: return null;
172: }
173:
174: /**
175: * Produce an object suitable for an ASN1OutputStream.
176: */
177: public DERObject toASN1Object() {
178: ASN1EncodableVector v = new ASN1EncodableVector();
179:
180: if (keyidentifier != null) {
181: v.add(new DERTaggedObject(false, 0, keyidentifier));
182: }
183:
184: if (certissuer != null) {
185: v.add(new DERTaggedObject(false, 1, certissuer));
186: }
187:
188: if (certserno != null) {
189: v.add(new DERTaggedObject(false, 2, certserno));
190: }
191:
192: return new DERSequence(v);
193: }
194:
195: public String toString() {
196: return ("AuthorityKeyIdentifier: KeyID("
197: + this .keyidentifier.getOctets() + ")");
198: }
199: }
|