summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/certsrv/key/KeyClient.java
blob: bdb84fddbf020dd9633575d5e967335e1596c3cf (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//--- BEGIN COPYRIGHT BLOCK ---
//This program is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; version 2 of the License.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License along
//with this program; if not, write to the Free Software Foundation, Inc.,
//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
//(C) 2012 Red Hat, Inc.
//All rights reserved.
//--- END COPYRIGHT BLOCK ---
package com.netscape.certsrv.key;

import java.net.URISyntaxException;
import java.util.List;

import javax.ws.rs.core.Response;

import com.netscape.certsrv.base.ResourceMessage;
import com.netscape.certsrv.client.Client;
import com.netscape.certsrv.client.PKIClient;
import com.netscape.certsrv.dbs.keydb.KeyId;
import com.netscape.certsrv.request.RequestId;
import com.netscape.cmsutil.util.Utils;

/**
 * @author Endi S. Dewata
 */
public class KeyClient extends Client {

    public KeyResource keyClient;
    public KeyRequestResource keyRequestClient;

    public KeyClient(PKIClient client, String subsystem) throws URISyntaxException {
        super(client, subsystem, "key");
        init();
    }

    public void init() throws URISyntaxException {
        keyClient = createProxy(KeyResource.class);
        keyRequestClient = createProxy(KeyRequestResource.class);
    }

    public KeyInfoCollection findKeys(String clientID, String status, Integer maxSize, Integer maxTime,
            Integer start, Integer size) {
        Response response = keyClient.listKeys(clientID, status, maxSize, maxTime, start, size);
        return client.getEntity(response, KeyInfoCollection.class);
    }

    public KeyInfo getActiveKeyInfo(String clientID) {
        Response response = keyClient.getActiveKeyInfo(clientID);
        return client.getEntity(response, KeyInfo.class);
    }

    public KeyData retrieveKey(KeyId keyId, RequestId requestId, byte[] rpwd, byte[] rkey, byte[] nonceData) {
        // create recovery request
        KeyRecoveryRequest data = new KeyRecoveryRequest();
        data.setKeyId(keyId);
        data.setRequestId(requestId);
        if (rkey != null) {
            data.setTransWrappedSessionKey(Utils.base64encode(rkey));
        }
        if (rpwd != null) {
            data.setSessionWrappedPassphrase(Utils.base64encode(rpwd));
        }

        if (nonceData != null) {
            data.setNonceData(Utils.base64encode(nonceData));
        }

        return retrieveKey(data);
    }

    public KeyData retrieveKey(KeyRecoveryRequest data) {
        Response response = keyClient.retrieveKey(data);
        return client.getEntity(response, KeyData.class);
    }

    public KeyRequestInfoCollection findRequests(String requestState, String requestType) {
        return findRequests(
                requestState,
                requestType,
                null,
                new RequestId(0),
                100,
                100,
                10
        );
    }

    public KeyRequestInfoCollection findRequests(
            String requestState,
            String requestType,
            String clientID,
            RequestId start,
            Integer pageSize,
            Integer maxResults,
            Integer maxTime) {
        return keyRequestClient.listRequests(
                requestState,
                requestType,
                clientID,
                start,
                pageSize,
                maxResults,
                maxTime);
    }

    public KeyRequestInfo getRequestInfo(RequestId id) {
        return keyRequestClient.getRequestInfo(id);
    }

    public KeyRequestResponse archiveSecurityData(byte[] encoded, String clientId, String dataType, String algorithm, int strength) {
        // create archival request
        KeyArchivalRequest data = new KeyArchivalRequest();
        String req1 = Utils.base64encode(encoded);
        data.setWrappedPrivateData(req1);
        data.setClientId(clientId);
        data.setDataType(dataType);
        data.setKeyAlgorithm(algorithm);
        data.setKeySize(strength);

        return createRequest(data);
    }

    public KeyRequestResponse requestRecovery(KeyId keyId, byte[] rpwd, byte[] rkey, byte[] nonceData) {
        // create recovery request
        KeyRecoveryRequest data = new KeyRecoveryRequest();
        data.setKeyId(keyId);
        if (rpwd != null) {
            data.setSessionWrappedPassphrase(Utils.base64encode(rpwd));
        }
        if (rkey != null) {
            data.setTransWrappedSessionKey(Utils.base64encode(rkey));
        }

        if (nonceData != null) {
            data.setNonceData(Utils.base64encode(nonceData));
        }

        return createRequest(data);
    }

    public KeyRequestResponse requestKeyRecovery(String keyId, String b64Certificate) {
        // create key recovery request
        KeyRecoveryRequest data = new KeyRecoveryRequest();
        data.setKeyId(new KeyId(keyId));
        data.setCertificate(b64Certificate);

        return createRequest(data);
    }

    public KeyRequestResponse generateKey(String clientId, String keyAlgorithm, int keySize, List<String> usages) {
        SymKeyGenerationRequest data = new SymKeyGenerationRequest();
        data.setClientId(clientId);
        data.setKeyAlgorithm(keyAlgorithm);
        data.setKeySize(new Integer(keySize));
        data.setUsages(usages);

        return createRequest(data);
    }

    public KeyRequestResponse createRequest(ResourceMessage data) {
        Response response = keyRequestClient.createRequest(data);
        return client.getEntity(response, KeyRequestResponse.class);
    }

    public void approveRequest(RequestId id) {
        keyRequestClient.approveRequest(id);
    }

    public void rejectRequest(RequestId id) {
        keyRequestClient.rejectRequest(id);
    }

    public void cancelRequest(RequestId id) {
        keyRequestClient.cancelRequest(id);
    }
}