1 /*
2  * @(#) $Id: KeyUsage.java,v 1.3 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk.cert;
11
12import java.util.*;
13import java.util.logging.Logger;
14import java.security.*;
15
16
17import org.jstk.*;
18
19
20public class KeyUsage {
21    public static final int MAX_KEYUSAGE_INDEX = 8;
22
23    public static final int DIGITAL_SIGNATURE = 0;
24    public static final int NON_REPUDIATION   = 1;
25    public static final int KEY_ENCIPHERMENT  = 2;
26    public static final int DATA_ENCIPHERMENT = 3;
27    public static final int KEY_AGREEMENT = 4;
28    public static final int KEY_CERTSIGN = 5;
29    public static final int CRL_SIGN = 6;
30    public static final int ENCIPHER_ONLY = 7;
31    public static final int DECIPHER_ONLY = 8;
32    public static final String[] keyUsageString = new String[] {
33        "digitalSignature",
34        "nonRepudiation",
35        "keyEncipherment",
36        "dataEncipherment",
37        "keyAgreement",
38        "keyCertSign",
39        "cRLSign",
40        "encipherOnly",
41        "decipherOnly" };
42
43    private boolean[] keyUsage = new boolean[MAX_KEYUSAGE_INDEX + 1];
44
45    public KeyUsage(){
46        // No key usage set by default.
47    }
48
49    public KeyUsage(boolean[] keyUsage){
50        if (keyUsage == null)
51            return;
52        for (int i = 0; (i < this.keyUsage.length) || (i < keyUsage.length); i++){
53            this.keyUsage[i] = keyUsage[i];
54        }
55    }
56
57    public void setKeyUsage(String kuString, boolean flag){
58        for (int i = 0; i < keyUsage.length; i++){
59            if (keyUsageString[i].equalsIgnoreCase(kuString)){
60                keyUsage[i] = flag;
61            }
62        }
63    }
64
65    public void setKeyUsage(int index, boolean flag){
66        if (index >= 0 && index <= MAX_KEYUSAGE_INDEX)
67            keyUsage[index] = flag;
68    }
69
70    public String getKeyUsageString(){
71        StringBuffer sb = new StringBuffer();
72        boolean first = true;
73        for (int i = 0; i < this.keyUsage.length; i++){
74            if (keyUsage[i]){
75                if (first){
76                    first = false;
77                } else {
78                    sb.append(", ");
79                }
80                sb.append(keyUsageString[i]);
81            }
82        }
83        return sb.toString();
84    }
85
86    public static String getKeyUsageString(int index){
87        if (index >= 0 && index <= MAX_KEYUSAGE_INDEX)
88            return keyUsageString[index];
89        return null;
90    }
91
92    public byte[] getBitString(){
93        byte[] value = new byte[2];
94        int mask = 0x80;
95        for (int i = 0; i < 8; i++){
96            if (keyUsage[i])
97                value[0] |= (byte)mask;
98            mask >>= 1;
99        }
00        mask = 0x80;
01        if (keyUsage[8])
02            value[1] = (byte)mask;
03        return value;
04    }
05
06    public int getNumUnusedBits(){
07        return 7;
08    }
09
10    public static void main(String[] args) throws Exception {
11        KeyUsage ku = new KeyUsage();
12        ku.setKeyUsage("crlSign", true);
13        System.out.println("KeyUsage: " + ku.getKeyUsageString());
14        ku.setKeyUsage("nonRepudiation", true);
15        System.out.println("KeyUsage: " + ku.getKeyUsageString());
16    }
17}