AuthorityKeyIdentifier.java revision 5c27fb80ffd335aa45dc8829ba3ecbc18c01e4e8
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18/**
19* @author Alexander Y. Kleymenov
20* @version $Revision$
21*/
22
23package org.apache.harmony.security.x509;
24
25import java.io.IOException;
26import java.math.BigInteger;
27import org.apache.harmony.security.asn1.ASN1Implicit;
28import org.apache.harmony.security.asn1.ASN1Integer;
29import org.apache.harmony.security.asn1.ASN1OctetString;
30import org.apache.harmony.security.asn1.ASN1Sequence;
31import org.apache.harmony.security.asn1.ASN1Type;
32import org.apache.harmony.security.asn1.BerInputStream;
33import org.apache.harmony.security.utils.Array;
34
35/**
36 * The class encapsulates the ASN.1 DER encoding/decoding work
37 * with Authority Key Identifier Extension (OID = 2.5.29.35).
38 * (as specified in RFC 3280 -
39 *  Internet X.509 Public Key Infrastructure.
40 *  Certificate and Certificate Revocation List (CRL) Profile.
41 *  http://www.ietf.org/rfc/rfc3280.txt):
42 *
43 * <pre>
44 *   id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::=  { id-ce 35 }
45 *
46 *   AuthorityKeyIdentifier ::= SEQUENCE {
47 *      keyIdentifier             [0] KeyIdentifier           OPTIONAL,
48 *      authorityCertIssuer       [1] GeneralNames            OPTIONAL,
49 *      authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL  }
50 *
51 *   KeyIdentifier ::= OCTET STRING
52 * </pre>
53 */
54public final class AuthorityKeyIdentifier extends ExtensionValue {
55    private final byte[] keyIdentifier;
56    private final GeneralNames authorityCertIssuer;
57    private final BigInteger authorityCertSerialNumber;
58
59    public AuthorityKeyIdentifier(byte[] keyIdentifier,
60            GeneralNames authorityCertIssuer,
61            BigInteger authorityCertSerialNumber) {
62        this.keyIdentifier = keyIdentifier;
63        this.authorityCertIssuer = authorityCertIssuer;
64        this.authorityCertSerialNumber = authorityCertSerialNumber;
65    }
66
67    public static AuthorityKeyIdentifier decode(byte[] encoding) throws IOException {
68        AuthorityKeyIdentifier aki = (AuthorityKeyIdentifier) ASN1.decode(encoding);
69        aki.encoding = encoding;
70        return aki;
71    }
72
73    @Override public byte[] getEncoded() {
74        if (encoding == null) {
75            encoding = ASN1.encode(this);
76        }
77        return encoding;
78    }
79
80    /**
81     * Places the string representation of extension value
82     * into the StringBuffer object.
83     */
84    @Override public void dumpValue(StringBuffer buffer, String prefix) {
85        buffer.append(prefix).append("AuthorityKeyIdentifier [\n");
86        if (keyIdentifier != null) {
87            buffer.append(prefix).append("  keyIdentifier:\n");
88            buffer.append(Array.toString(keyIdentifier, prefix + "    "));
89        }
90        if (authorityCertIssuer != null) {
91            buffer.append(prefix).append("  authorityCertIssuer: [\n");
92            authorityCertIssuer.dumpValue(buffer, prefix + "    ");
93            buffer.append(prefix).append("  ]\n");
94        }
95        if (authorityCertSerialNumber != null) {
96            buffer.append(prefix).append("  authorityCertSerialNumber: ")
97                .append(authorityCertSerialNumber).append('\n');
98        }
99        buffer.append(prefix).append("]\n");
100    }
101
102    public static final ASN1Type ASN1 = new ASN1Sequence(
103            new ASN1Type[] {
104                new ASN1Implicit(0, ASN1OctetString.getInstance()),
105                new ASN1Implicit(1, GeneralNames.ASN1),
106                new ASN1Implicit(2, ASN1Integer.getInstance()),
107            }) {
108        {
109            setOptional(0);
110            setOptional(1);
111            setOptional(2);
112        }
113
114        @Override protected Object getDecodedObject(BerInputStream in) throws IOException {
115            Object[] values = (Object[]) in.content;
116
117            byte[] enc = (byte[]) values[2];
118            BigInteger authorityCertSerialNumber = null;
119            if (enc != null) {
120                authorityCertSerialNumber = new BigInteger(enc);
121            }
122
123            return new AuthorityKeyIdentifier((byte[]) values[0],
124                    (GeneralNames) values[1], authorityCertSerialNumber);
125        }
126
127        @Override protected void getValues(Object object, Object[] values) {
128            AuthorityKeyIdentifier akid = (AuthorityKeyIdentifier) object;
129            values[0] = akid.keyIdentifier;
130            values[1] = akid.authorityCertIssuer;
131            if (akid.authorityCertSerialNumber != null) {
132                values[2] = akid.authorityCertSerialNumber.toByteArray();
133            }
134        }
135    };
136}
137
138