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;
27
28import org.apache.harmony.security.asn1.ASN1Implicit;
29import org.apache.harmony.security.asn1.ASN1Integer;
30import org.apache.harmony.security.asn1.ASN1OctetString;
31import org.apache.harmony.security.asn1.ASN1Sequence;
32import org.apache.harmony.security.asn1.ASN1Type;
33import org.apache.harmony.security.asn1.BerInputStream;
34import org.apache.harmony.security.utils.Array;
35
36/**
37 * The class encapsulates the ASN.1 DER encoding/decoding work
38 * with Authority Key Identifier Extension (OID = 2.5.29.35).
39 * (as specified in RFC 3280 -
40 *  Internet X.509 Public Key Infrastructure.
41 *  Certificate and Certificate Revocation List (CRL) Profile.
42 *  http://www.ietf.org/rfc/rfc3280.txt):
43 *
44 * <pre>
45 *   id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::=  { id-ce 35 }
46 *
47 *   AuthorityKeyIdentifier ::= SEQUENCE {
48 *      keyIdentifier             [0] KeyIdentifier           OPTIONAL,
49 *      authorityCertIssuer       [1] GeneralNames            OPTIONAL,
50 *      authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL  }
51 *
52 *   KeyIdentifier ::= OCTET STRING
53 * </pre>
54 */
55public class AuthorityKeyIdentifier extends ExtensionValue {
56
57    private final byte[] keyIdentifier;
58    private final GeneralNames authorityCertIssuer;
59    private final BigInteger authorityCertSerialNumber;
60
61    public AuthorityKeyIdentifier(byte[] keyIdentifier,
62            GeneralNames authorityCertIssuer,
63            BigInteger authorityCertSerialNumber) {
64        this.keyIdentifier = keyIdentifier;
65        this.authorityCertIssuer = authorityCertIssuer;
66        this.authorityCertSerialNumber = authorityCertSerialNumber;
67    }
68
69    public static AuthorityKeyIdentifier decode(byte[] encoding)
70            throws IOException {
71        AuthorityKeyIdentifier aki =
72            (AuthorityKeyIdentifier) ASN1.decode(encoding);
73        aki.encoding = encoding;
74        return aki;
75    }
76
77    public byte[] getEncoded() {
78        if (encoding == null) {
79            encoding = ASN1.encode(this);
80        }
81        return encoding;
82    }
83
84    /**
85     * Places the string representation of extension value
86     * into the StringBuffer object.
87     */
88    public void dumpValue(StringBuffer buffer, String prefix) {
89        buffer.append(prefix).append("AuthorityKeyIdentifier [\n"); //$NON-NLS-1$
90        if (keyIdentifier != null) {
91            buffer.append(prefix).append("  keyIdentifier:\n"); //$NON-NLS-1$
92            buffer.append(Array.toString(keyIdentifier, prefix + "    ")); //$NON-NLS-1$
93        }
94        if (authorityCertIssuer != null) {
95            buffer.append(prefix).append("  authorityCertIssuer: [\n"); //$NON-NLS-1$
96            authorityCertIssuer.dumpValue(buffer, prefix + "    "); //$NON-NLS-1$
97            buffer.append(prefix).append("  ]\n"); //$NON-NLS-1$
98        }
99        if (authorityCertSerialNumber != null) {
100            buffer.append(prefix).append("  authorityCertSerialNumber: ") //$NON-NLS-1$
101                .append(authorityCertSerialNumber).append('\n');
102        }
103        buffer.append(prefix).append("]\n"); //$NON-NLS-1$
104    }
105
106    public static final ASN1Type ASN1 = new ASN1Sequence(
107            new ASN1Type[] {
108                new ASN1Implicit(0, ASN1OctetString.getInstance()),
109                new ASN1Implicit(1, GeneralNames.ASN1),
110                new ASN1Implicit(2, ASN1Integer.getInstance()),
111            }) {
112        {
113            setOptional(0);
114            setOptional(1);
115            setOptional(2);
116        }
117
118        protected Object getDecodedObject(BerInputStream in) throws IOException {
119            Object[] values = (Object[]) in.content;
120
121            byte[] enc = (byte[]) values[2];
122            BigInteger authorityCertSerialNumber = null;
123            if (enc != null) {
124                authorityCertSerialNumber = new BigInteger(enc);
125            }
126
127            return new AuthorityKeyIdentifier((byte[]) values[0],
128                    (GeneralNames) values[1], authorityCertSerialNumber);
129        }
130
131        protected void getValues(Object object, Object[] values) {
132
133            AuthorityKeyIdentifier akid = (AuthorityKeyIdentifier) object;
134
135            values[0] = akid.keyIdentifier;
136            values[1] = akid.authorityCertIssuer;
137            if (akid.authorityCertSerialNumber != null) {
138                values[2] = akid.authorityCertSerialNumber.toByteArray();
139            }
140        }
141    };
142}
143
144