1 package org.bouncycastle.asn1.x509;
2 
3 import org.bouncycastle.asn1.ASN1Object;
4 import org.bouncycastle.asn1.ASN1Primitive;
5 import org.bouncycastle.asn1.DERBitString;
6 
7 /**
8  * The KeyUsage object.
9  * <pre>
10  *    id-ce-keyUsage OBJECT IDENTIFIER ::=  { id-ce 15 }
11  *
12  *    KeyUsage ::= BIT STRING {
13  *         digitalSignature        (0),
14  *         nonRepudiation          (1),
15  *         keyEncipherment         (2),
16  *         dataEncipherment        (3),
17  *         keyAgreement            (4),
18  *         keyCertSign             (5),
19  *         cRLSign                 (6),
20  *         encipherOnly            (7),
21  *         decipherOnly            (8) }
22  * </pre>
23  */
24 public class KeyUsage
25     extends ASN1Object
26 {
27     public static final int        digitalSignature = (1 << 7);
28     public static final int        nonRepudiation   = (1 << 6);
29     public static final int        keyEncipherment  = (1 << 5);
30     public static final int        dataEncipherment = (1 << 4);
31     public static final int        keyAgreement     = (1 << 3);
32     public static final int        keyCertSign      = (1 << 2);
33     public static final int        cRLSign          = (1 << 1);
34     public static final int        encipherOnly     = (1 << 0);
35     public static final int        decipherOnly     = (1 << 15);
36 
37     private DERBitString bitString;
38 
getInstance(Object obj)39     public static KeyUsage getInstance(Object obj)   // needs to be DERBitString for other VMs
40     {
41         if (obj instanceof KeyUsage)
42         {
43             return (KeyUsage)obj;
44         }
45         else if (obj != null)
46         {
47             return new KeyUsage(DERBitString.getInstance(obj));
48         }
49 
50         return null;
51     }
52 
fromExtensions(Extensions extensions)53     public static KeyUsage fromExtensions(Extensions extensions)
54     {
55         return KeyUsage.getInstance(extensions.getExtensionParsedValue(Extension.keyUsage));
56     }
57 
58     /**
59      * Basic constructor.
60      *
61      * @param usage - the bitwise OR of the Key Usage flags giving the
62      * allowed uses for the key.
63      * e.g. (KeyUsage.keyEncipherment | KeyUsage.dataEncipherment)
64      */
KeyUsage( int usage)65     public KeyUsage(
66         int usage)
67     {
68         this.bitString = new DERBitString(usage);
69     }
70 
KeyUsage( DERBitString bitString)71     private KeyUsage(
72         DERBitString bitString)
73     {
74         this.bitString = bitString;
75     }
76 
77     /**
78      * Return true if a given usage bit is set, false otherwise.
79      *
80      * @param usages combination of usage flags.
81      * @return true if all bits are set, false otherwise.
82      */
hasUsages(int usages)83     public boolean hasUsages(int usages)
84     {
85         return (bitString.intValue() & usages) == usages;
86     }
87 
getBytes()88     public byte[] getBytes()
89     {
90         return bitString.getBytes();
91     }
92 
getPadBits()93     public int getPadBits()
94     {
95         return bitString.getPadBits();
96     }
97 
toString()98     public String toString()
99     {
100         byte[] data = bitString.getBytes();
101 
102         if (data.length == 1)
103         {
104             return "KeyUsage: 0x" + Integer.toHexString(data[0] & 0xff);
105         }
106         return "KeyUsage: 0x" + Integer.toHexString((data[1] & 0xff) << 8 | (data[0] & 0xff));
107     }
108 
toASN1Primitive()109     public ASN1Primitive toASN1Primitive()
110     {
111         return bitString;
112     }
113 }
114