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
18package org.apache.harmony.security.x509;
19
20import java.io.IOException;
21import org.apache.harmony.security.asn1.ASN1OctetString;
22import org.apache.harmony.security.utils.Array;
23
24/**
25 * Subject Key Identifier Extension (OID = 2.5.29.14).
26 *
27 * The ASN.1 definition for extension is:
28 *
29 * <pre>
30 *  id-ce-subjectKeyIdentifier OBJECT IDENTIFIER ::=  { id-ce 14 }
31 *
32 *  SubjectKeyIdentifier ::= KeyIdentifier
33 *
34 *  KeyIdentifier ::= OCTET STRING
35 * </pre>
36 * (as specified in RFC 3280 http://www.ietf.org/rfc/rfc3280.txt)
37 */
38public final class SubjectKeyIdentifier extends ExtensionValue {
39
40    // the value of key identifier
41    private final byte[] keyIdentifier;
42
43    /**
44     * Creates the object on the base of the value of key identifier.
45     */
46    public SubjectKeyIdentifier(byte[] keyIdentifier) {
47        this.keyIdentifier = keyIdentifier;
48    }
49
50    /**
51     * Creates an object on the base of its encoded form.
52     */
53    public static SubjectKeyIdentifier decode(byte[] encoding)
54            throws IOException {
55        SubjectKeyIdentifier res = new SubjectKeyIdentifier((byte[])
56                ASN1OctetString.getInstance().decode(encoding));
57        res.encoding = encoding;
58        return res;
59    }
60
61    /**
62     * The key identifier for this subject.
63     */
64    public byte[] getKeyIdentifier() {
65        return keyIdentifier;
66    }
67
68    @Override public byte[] getEncoded() {
69        if (encoding == null) {
70            encoding = ASN1OctetString.getInstance().encode(keyIdentifier);
71        }
72        return encoding;
73    }
74
75    @Override public void dumpValue(StringBuilder sb, String prefix) {
76        sb.append(prefix).append("SubjectKeyIdentifier: [\n");
77        sb.append(Array.toString(keyIdentifier, prefix));
78        sb.append(prefix).append("]\n");
79    }
80}
81