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.ASN1Type;
22import org.apache.harmony.security.asn1.ASN1BitString;
23
24/**
25 * Key Usage Extension (OID = 2.5.29.15).
26 *
27 * The ASN.1 definition for Key Usage Extension is:
28 *
29 * <pre>
30 * id-ce-keyUsage OBJECT IDENTIFIER ::=  { id-ce 15 }
31 *
32 * KeyUsage ::= BIT STRING {
33 *     digitalSignature        (0),
34 *     nonRepudiation          (1),
35 *     keyEncipherment         (2),
36 *     dataEncipherment        (3),
37 *     keyAgreement            (4),
38 *     keyCertSign             (5),
39 *     cRLSign                 (6),
40 *     encipherOnly            (7),
41 *     decipherOnly            (8)
42 * }
43 * </pre>
44 * (as specified in RFC 3280 http://www.ietf.org/rfc/rfc3280.txt)
45 */
46public class KeyUsage extends ExtensionValue {
47
48    /**
49     * The names of the usages.
50     */
51    private static final String[] USAGES = {
52        "digitalSignature", //$NON-NLS-1$
53        "nonRepudiation", //$NON-NLS-1$
54        "keyEncipherment", //$NON-NLS-1$
55        "dataEncipherment", //$NON-NLS-1$
56        "keyAgreement", //$NON-NLS-1$
57        "keyCertSign", //$NON-NLS-1$
58        "cRLSign", //$NON-NLS-1$
59        "encipherOnly", //$NON-NLS-1$
60        "decipherOnly", //$NON-NLS-1$
61    };
62
63    // the value of extension
64    private final boolean[] keyUsage;
65
66    /**
67     * Creates the extension object corresponding to the given key usage.
68     */
69    public KeyUsage(boolean[] keyUsage) {
70        this.keyUsage = keyUsage;
71    }
72
73    /**
74     * Creates the extension object on the base of its encoded form.
75     */
76    public KeyUsage(byte[] encoding) throws IOException {
77        super(encoding);
78        this.keyUsage = (boolean[]) ASN1.decode(encoding);
79    }
80
81    public boolean[] getKeyUsage() {
82        return keyUsage;
83    }
84
85    /**
86     * Returns the encoded of the object.
87     * @return a byte array containing ASN.1 encoded form.
88     */
89    public byte[] getEncoded() {
90        if (encoding == null) {
91            encoding = ASN1.encode(keyUsage);
92        }
93        return encoding;
94    }
95
96    /**
97     * Places the string representation of extension value
98     * into the StringBuffer object.
99     */
100    public void dumpValue(StringBuffer buffer, String prefix) {
101        buffer.append(prefix).append("KeyUsage [\n"); //$NON-NLS-1$
102        for (int i=0; i<keyUsage.length; i++) {
103            if (keyUsage[i]) {
104                buffer.append(prefix).append("  ") //$NON-NLS-1$
105                    .append(USAGES[i]).append('\n');
106            }
107        }
108        buffer.append(prefix).append("]\n"); //$NON-NLS-1$
109    }
110
111    /**
112     * X.509 Extension value encoder/decoder.
113     */
114    private static final ASN1Type ASN1 = new ASN1BitString.ASN1NamedBitList(9);
115
116}
117