summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/certsrv/key/SymKeyGenerationRequest.java
blob: 19e6aa67c01c46c081a16d4a6bf1b2de465e56dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.netscape.certsrv.key;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.ws.rs.core.MultivaluedMap;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.commons.lang.StringUtils;

/**
 * @author alee
 *
 */
@XmlRootElement(name="SymKeyGenerationRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class SymKeyGenerationRequest extends KeyRequest {

    private static final String CLIENT_ID = "clientID";
    private static final String KEY_SIZE = "keySize";
    private static final String KEY_ALGORITHM = "keyAlgorithm";
    private static final String KEY_USAGE = "keyUsage";

    // usages
    public static final String ENCRYPT_USAGE = "encrypt";
    public static final String DECRYPT_USAGE = "decrypt";
    public static final String SIGN_USAGE = "sign";
    public static final String VERIFY_USAGE = "verify";
    public static final String WRAP_USAGE = "wrap";
    public static final String UWRAP_USAGE = "unwrap";

    public List<String> getUsages() {
        String usageString = properties.get(KEY_USAGE);
        if (! StringUtils.isBlank(usageString)) {
            return new ArrayList<String>(Arrays.asList(usageString.split(",")));
        }
        return new ArrayList<String>();
    }

    public void setUsages(List<String> usages) {
        this.properties.put(KEY_USAGE, StringUtils.join(usages, ","));
    }

    public void addUsage(String usage) {
        List<String> usages = getUsages();
        for (String u: usages) {
            if (u.equals(usage)) return;
        }
        usages.add(usage);
        setUsages(usages);
    }

    public SymKeyGenerationRequest() {
        // required for JAXB (defaults)
    }

    public SymKeyGenerationRequest(MultivaluedMap<String, String> form) {
        this.properties.put(CLIENT_ID, form.getFirst(CLIENT_ID));
        this.properties.put(KEY_SIZE, form.getFirst(KEY_SIZE));
        this.properties.put(KEY_ALGORITHM, form.getFirst(KEY_ALGORITHM));
        this.properties.put(KEY_USAGE, form.getFirst(KEY_USAGE));

        String usageString = properties.get(KEY_USAGE);
        if (! StringUtils.isBlank(usageString)) {
            setUsages(new ArrayList<String>(Arrays.asList(usageString.split(","))));
        }
    }

    /**
     * @return the clientId
     */
    public String getClientId() {
        return properties.get(CLIENT_ID);
    }

    /**
     * @param clientId the clientId to set
     */
    public void setClientId(String clientId) {
        this.properties.put(CLIENT_ID, clientId);
    }

    /**
     * @return the keySize
     */
    public int getKeySize() {
        return Integer.parseInt(this.properties.get(KEY_SIZE));
    }

    /**
     * @param keySize the key size to set
     */
    public void setKeySize(int keySize) {
        this.properties.put(KEY_SIZE, Integer.toString(keySize));
    }

    /**
     * @return the keyAlgorithm
     */
    public String getKeyAlgorithm() {
        return this.properties.get(KEY_ALGORITHM);
    }

    /**
     * @param keyAlgorithm the key algorithm to set
     */
    public void setKeyAlgorithm(String keyAlgorithm) {
        this.properties.put(KEY_ALGORITHM, keyAlgorithm);
    }

    public String toString() {
        try {
            return KeyRequest.marshal(this, SymKeyGenerationRequest.class);
        } catch (Exception e) {
            return super.toString();
        }
    }

    public static SymKeyGenerationRequest valueOf(String string) throws Exception {
        try {
            return KeyRequest.unmarshal(string, SymKeyGenerationRequest.class);
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String args[]) throws Exception {

        SymKeyGenerationRequest before = new SymKeyGenerationRequest();
        before.setClientId("vek 12345");
        before.setKeyAlgorithm("aes");
        before.setKeySize(128);
        before.setRequestType(KeyRequestResource.KEY_GENERATION_REQUEST);
        before.addUsage(SymKeyGenerationRequest.DECRYPT_USAGE);
        before.addUsage(SymKeyGenerationRequest.ENCRYPT_USAGE);
        before.addUsage(SymKeyGenerationRequest.SIGN_USAGE);

        String string = before.toString();
        System.out.println(string);

        SymKeyGenerationRequest after = SymKeyGenerationRequest.valueOf(string);
        System.out.println(before.equals(after));
    }

}