1package org.bouncycastle.asn1.x509;
2
3import org.bouncycastle.asn1.DERBitString;
4
5/**
6 * The KeyUsage object.
7 * <pre>
8 *    id-ce-keyUsage OBJECT IDENTIFIER ::=  { id-ce 15 }
9 *
10 *    KeyUsage ::= BIT STRING {
11 *         digitalSignature        (0),
12 *         nonRepudiation          (1),
13 *         keyEncipherment         (2),
14 *         dataEncipherment        (3),
15 *         keyAgreement            (4),
16 *         keyCertSign             (5),
17 *         cRLSign                 (6),
18 *         encipherOnly            (7),
19 *         decipherOnly            (8) }
20 * </pre>
21 */
22public class KeyUsage
23    extends DERBitString
24{
25    public static final int        digitalSignature = (1 << 7);
26    public static final int        nonRepudiation   = (1 << 6);
27    public static final int        keyEncipherment  = (1 << 5);
28    public static final int        dataEncipherment = (1 << 4);
29    public static final int        keyAgreement     = (1 << 3);
30    public static final int        keyCertSign      = (1 << 2);
31    public static final int        cRLSign          = (1 << 1);
32    public static final int        encipherOnly     = (1 << 0);
33    public static final int        decipherOnly     = (1 << 15);
34
35    /**
36     * Basic constructor.
37     *
38     * @param usage - the bitwise OR of the Key Usage flags giving the
39     * allowed uses for the key.
40     * e.g. (KeyUsage.keyEncipherment | KeyUsage.dataEncipherment)
41     */
42    public KeyUsage(
43        int usage)
44    {
45        super(getBytes(usage), getPadBits(usage));
46    }
47
48    public KeyUsage(
49        DERBitString usage)
50    {
51        super(usage.getBytes(), usage.getPadBits());
52    }
53
54    public String toString()
55    {
56        if (data.length == 1)
57        {
58            return "KeyUsage: 0x" + Integer.toHexString(data[0] & 0xff);
59        }
60        return "KeyUsage: 0x" + Integer.toHexString((data[1] & 0xff) << 8 | (data[0] & 0xff));
61    }
62}
63