AuthorityKeyIdentifier.java revision 8216dc1fd9d31867770439985c3d66570330e4c7
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    @Override public void dumpValue(StringBuilder sb, String prefix) {
81        sb.append(prefix).append("AuthorityKeyIdentifier [\n");
82        if (keyIdentifier != null) {
83            sb.append(prefix).append("  keyIdentifier:\n");
84            sb.append(Array.toString(keyIdentifier, prefix + "    "));
85        }
86        if (authorityCertIssuer != null) {
87            sb.append(prefix).append("  authorityCertIssuer: [\n");
88            authorityCertIssuer.dumpValue(sb, prefix + "    ");
89            sb.append(prefix).append("  ]\n");
90        }
91        if (authorityCertSerialNumber != null) {
92            sb.append(prefix).append("  authorityCertSerialNumber: ");
93            sb.append(authorityCertSerialNumber).append('\n');
94        }
95        sb.append(prefix).append("]\n");
96    }
97
98    public static final ASN1Type ASN1 = new ASN1Sequence(
99            new ASN1Type[] {
100                new ASN1Implicit(0, ASN1OctetString.getInstance()),
101                new ASN1Implicit(1, GeneralNames.ASN1),
102                new ASN1Implicit(2, ASN1Integer.getInstance()),
103            }) {
104        {
105            setOptional(0);
106            setOptional(1);
107            setOptional(2);
108        }
109
110        @Override protected Object getDecodedObject(BerInputStream in) throws IOException {
111            Object[] values = (Object[]) in.content;
112
113            byte[] enc = (byte[]) values[2];
114            BigInteger authorityCertSerialNumber = null;
115            if (enc != null) {
116                authorityCertSerialNumber = new BigInteger(enc);
117            }
118
119            return new AuthorityKeyIdentifier((byte[]) values[0],
120                    (GeneralNames) values[1], authorityCertSerialNumber);
121        }
122
123        @Override protected void getValues(Object object, Object[] values) {
124            AuthorityKeyIdentifier akid = (AuthorityKeyIdentifier) object;
125            values[0] = akid.keyIdentifier;
126            values[1] = akid.authorityCertIssuer;
127            if (akid.authorityCertSerialNumber != null) {
128                values[2] = akid.authorityCertSerialNumber.toByteArray();
129            }
130        }
131    };
132}
133