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