9285c96c283de9fc42bea6190e229830dce1c59b
[org.ibex.core.git] / src / org / bouncycastle / asn1 / x509 / AuthorityKeyIdentifier.java
1 package org.bouncycastle.asn1.x509;
2
3 import java.math.BigInteger;
4
5 import java.util.Enumeration;
6
7 import org.bouncycastle.crypto.Digest;
8 import org.bouncycastle.crypto.digests.SHA1Digest;
9 import org.bouncycastle.asn1.*;
10
11 /**
12  * <pre>
13  * id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::=  { id-ce 35 }
14  *
15  *   AuthorityKeyIdentifier ::= SEQUENCE {
16  *      keyIdentifier             [0] IMPLICIT KeyIdentifier           OPTIONAL,
17  *      authorityCertIssuer       [1] IMPLICIT GeneralNames            OPTIONAL,
18  *      authorityCertSerialNumber [2] IMPLICIT CertificateSerialNumber OPTIONAL  }
19  *
20  *   KeyIdentifier ::= OCTET STRING
21  * </pre>
22  *
23  */
24 public class AuthorityKeyIdentifier
25     implements DEREncodable
26 {
27     DEROctetString keyidentifier=null;
28     GeneralNames certissuer=null;
29     DERInteger certserno=null;
30
31     public AuthorityKeyIdentifier(
32         DERConstructedSequence   seq)
33     {
34         Enumeration e = seq.getObjects();
35
36         while (e.hasMoreElements())
37         {
38             DERTaggedObject o = (DERTaggedObject)e.nextElement();
39
40             switch (o.getTagNo())
41             {
42             case 0:
43                 this.keyidentifier= (DEROctetString)o.getObject();
44                 break;
45
46             case 1:
47                 if (o.getObject() instanceof DERConstructedSequence)
48                 {
49                     this.certissuer = new GeneralNames((DERConstructedSequence)o.getObject());
50                 }
51                 else   
52                 {
53                     // as it's implicitly tagged we can loose the"sequence"
54                     // if there is only one object.
55                     //
56                     DERConstructedSequence s = new DERConstructedSequence();
57
58                     s.addObject(o.getObject());
59
60                     this.certissuer = new GeneralNames(s);
61                 }
62                 break;
63             case 2:
64                 //
65                 // implicit tagging again...
66                 //
67                 DEROctetString          oct = (DEROctetString)o.getObject();
68
69                 this.certserno = new DERInteger(new BigInteger(oct.getOctets()));
70                 break;
71             default:
72                 throw new IllegalArgumentException("illegal tag");
73             }
74         }
75     }
76
77     /**
78      *
79      * Calulates the keyidentifier using a SHA1 hash over the BIT STRING
80      * from SubjectPublicKeyInfo as defined in RFC2459.
81      *
82      * Example of making a AuthorityKeyIdentifier:
83      * <pre>
84      *   SubjectPublicKeyInfo apki = new SubjectPublicKeyInfo((DERConstructedSequence)new DERInputStream(
85      *       new ByteArrayInputStream(publicKey.getEncoded())).readObject());
86      *   AuthorityKeyIdentifier aki = new AuthorityKeyIdentifier(apki);
87      * </pre>
88      *
89      **/
90     public AuthorityKeyIdentifier(
91         SubjectPublicKeyInfo    spki)
92     {
93         Digest  digest = new SHA1Digest();
94         byte[]  resBuf = new byte[digest.getDigestSize()];
95
96         DERBitString derpk = new DERBitString(spki.getPublicKey());
97         byte[] bytes = derpk.getBytes();
98         digest.update(bytes, 0, bytes.length);
99         digest.doFinal(resBuf, 0);
100         this.keyidentifier=new DEROctetString(resBuf);
101     }
102
103     /**
104      * create an AuthorityKeyIdentifier with the GeneralNames tag and
105      * the serial number provided as well.
106      */
107     public AuthorityKeyIdentifier(
108         SubjectPublicKeyInfo    spki,
109         GeneralNames            name,
110         BigInteger              serialNumber)
111     {
112         Digest  digest = new SHA1Digest();
113         byte[]  resBuf = new byte[digest.getDigestSize()];
114
115         DERBitString derpk = new DERBitString(spki.getPublicKey());
116         byte[] bytes = derpk.getBytes();
117         digest.update(bytes, 0, bytes.length);
118         digest.doFinal(resBuf, 0);
119
120         this.keyidentifier = new DEROctetString(resBuf);
121         this.certissuer = name;
122         this.certserno = new DERInteger(serialNumber);
123     }
124
125     public byte[] getKeyIdentifier()
126     {
127         if (keyidentifier != null)
128         {
129             return keyidentifier.getOctets();
130         }
131
132         return null;
133     }
134
135      /**
136      * <pre>
137      *   AuthorityKeyIdentifier ::= SEQUENCE {
138      *      keyIdentifier             [0] IMPLICIT KeyIdentifier           OPTIONAL,
139      *      authorityCertIssuer       [1] IMPLICIT GeneralNames            OPTIONAL,
140      *      authorityCertSerialNumber [2] IMPLICIT CertificateSerialNumber OPTIONAL  }
141      *
142      *   KeyIdentifier ::= OCTET STRING
143      * </pre>
144      */
145     public DERObject getDERObject()
146     {
147         DERConstructedSequence  seq = new DERConstructedSequence();
148
149         if (keyidentifier != null)
150         {
151             seq.addObject(new DERTaggedObject(false, 0, keyidentifier));
152         }
153
154         if (certissuer != null)
155         {
156             seq.addObject(new DERTaggedObject(false, 1, certissuer));
157         }
158
159         if (certserno != null)
160         {
161             seq.addObject(new DERTaggedObject(false, 2, certserno));
162         }
163
164
165         return seq;
166     }
167
168     public String toString()
169     {
170         return ("AuthorityKeyIdentifier: KeyID(" + this.keyidentifier.getOctets() + ")");
171     }
172 }