summaryrefslogtreecommitdiffstats
path: root/base/tps/src/org/dogtagpki/server/tps/cms/KRARemoteRequestHandler.java
blob: 1f7347ddd001a18cb6214c25876883dd0753753f (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// --- 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) 2014 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

package org.dogtagpki.server.tps.cms;

import java.math.BigInteger;
import java.util.Hashtable;

import org.dogtagpki.server.connector.IRemoteRequest;
import org.dogtagpki.server.tps.TPSSubsystem;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.cmscore.connector.HttpConnector;
import com.netscape.cmsutil.http.HttpResponse;

/**
 * KRARemoteRequestHandler is a class representing remote requests
 * offered by the Key Recovery Authority (KRA)
 * On a successful return, name/value pairs are provided in a Hashtable where
 * all contents are URL decoded if needed
 *
 * @author cfu
 */
public class KRARemoteRequestHandler extends RemoteRequestHandler
{
    public KRARemoteRequestHandler(String connID)
            throws EBaseException {
        if (connID == null) {
            throw new EBaseException("KRARemoteRequestHandler: KRARemoteRequestHandler(): connID null.");
        }

        connid = connID;
    }

    /**
     * serverSideKeyGen generates key pairs on the KRA
     *
     * @param cuid is the token id
     * @param userid is the user id
     * @param sDesKey is the des key provided by the TKS for key encryption
     * @param archive true or false
     *
     * @returns KRAServerSideKeyGenResponse
     */
    public KRAServerSideKeyGenResponse serverSideKeyGen(
            boolean isECC,
            int keysize,
            String cuid,
            String userid,
            String sDesKey,
            boolean archive)
            throws EBaseException {

        CMS.debug("KRARemoteRequestHandler: serverSideKeyGen(): begins.");
        if (cuid == null || userid == null || sDesKey == null) {
            throw new EBaseException("KRARemoteRequestHandler: serverSideKeyGen(): input parameter null.");
        }

        TPSSubsystem subsystem =
                (TPSSubsystem) CMS.getSubsystem(TPSSubsystem.ID);
        HttpConnector conn =
                (HttpConnector) subsystem.getConnectionManager().getConnector(connid);
        CMS.debug("KRARemoteRequestHandler: serverSideKeyGen(): sending request to KRA");
        HttpResponse resp;
        String request;
        if (isECC) {
            String eckeycurve;
            if (keysize == 521) {
                eckeycurve = "nistp521";
            } else if (keysize == 384) {
                eckeycurve = "nistp384";
            } else if (keysize == 256) {
                eckeycurve = "nistp256";
            } else {
                CMS.debug("KRARemoteRequestHandler: serverSideKeyGen(): unrecognized ECC keysize" + keysize
                        + ", setting to nistp256");
                keysize = 256;
                eckeycurve = "nistp256";
            }

            request = IRemoteRequest.KRA_KEYGEN_Archive + "=" +
                    archive +
                    "&" + IRemoteRequest.TOKEN_CUID + "=" +
                    cuid +
                    "&" + IRemoteRequest.KRA_UserId + "=" +
                    userid +
                    "&" + IRemoteRequest.KRA_KEYGEN_KeyType + "=" +
                    "EC" +
                    "&" + IRemoteRequest.KRA_KEYGEN_EC_KeyCurve + "=" +
                    eckeycurve +
                    "&" + IRemoteRequest.KRA_Trans_DesKey + "=" +
                    sDesKey;

            CMS.debug("KRARemoteRequestHandler: outgoing request for ECC: " + request);

            resp =
                    conn.send("GenerateKeyPair",
                            request);
        } else { // RSA

            request = IRemoteRequest.KRA_KEYGEN_Archive + "=" +
                    archive +
                    "&" + IRemoteRequest.TOKEN_CUID + "=" +
                    cuid +
                    "&" + IRemoteRequest.KRA_UserId + "=" +
                    userid +
                    "&" + IRemoteRequest.KRA_KEYGEN_KeyType + "=" +
                    "RSA" +
                    "&" + IRemoteRequest.KRA_KEYGEN_KeySize + "=" +
                    keysize +
                    "&" + IRemoteRequest.KRA_Trans_DesKey + "=" +
                    sDesKey;

            CMS.debug("KRARemoteRequestHandler: outgoing request for RSA: " + request);

            resp =
                    conn.send("GenerateKeyPair",
                            request);
        }

        //For some reason the send method can return null and not throw an exception.
        // Check here;

        if (resp == null) {
            throw new EBaseException(
                    "KRARemoteRequestHandler: serverSideKeyGen(): No response object returned from connection.");
        }

        String content = resp.getContent();

        CMS.debug("KRARemoteRequestHandler: serverSideKeyGen(): got content = " + content);
        if (content != null && !content.equals("")) {
            Hashtable<String, Object> response =
                    parseResponse(content);

            /**
             * When a value is not found in response, keep going so we know
             * what else is missing
             * Note: response values "missing" might not be bad for some cases
             */
            Integer ist = new Integer(IRemoteRequest.RESPONSE_STATUS_NOT_FOUND);
            String value = (String) response.get(IRemoteRequest.RESPONSE_STATUS);

            if (value == null) {
                throw new EBaseException("KRARemoteRequestHandler: serverSideKeyGen(): Invalide status returned!");
            }

            CMS.debug("KRARemoteRequestHandler: serverSideKeyGen(): got status = " + value);
            ist = Integer.parseInt(value);
            if (ist != 0) {
                CMS.debug("KRARemoteRequestHandler: serverSideKeyGen(): status not 0, getting error string... ");
                value = (String) response.get(IRemoteRequest.RESPONSE_ERROR_STRING);
                if (value == null) {
                    CMS.debug("KRARemoteRequestHandler: serverSideKeyGen(): response missing name-value pair for: " +
                            IRemoteRequest.RESPONSE_ERROR_STRING);
                } else {
                    CMS.debug("KRARemoteRequestHandler: serverSideKeyGen(): got IRemoteRequest.RESPONSE_ERROR_STRING = "
                            + value);
                    response.put(IRemoteRequest.RESPONSE_ERROR_STRING, value);
                }
            }
            response.put(IRemoteRequest.RESPONSE_STATUS, ist);

            value = (String) response.get(IRemoteRequest.KRA_RESPONSE_PublicKey);
            if (value == null) {
                CMS.debug("KRARemoteRequestHandler: serverSideKeyGen(): response missing name-value pair for: " +
                        IRemoteRequest.KRA_RESPONSE_PublicKey);
            } else {
                CMS.debug("KRARemoteRequestHandler:serverSideKeyGen(): got IRemoteRequest.KRA_RESPONSE_PublicKey= "
                        + value);
                response.put(IRemoteRequest.KRA_RESPONSE_PublicKey, value);
            }

            value = (String) response.get(IRemoteRequest.KRA_RESPONSE_Wrapped_PrivKey);
            if (value == null) {
                CMS.debug("KRARemoteRequestHandler: serverSideKeyGen(): response missing name-value pair for: " +
                        IRemoteRequest.KRA_RESPONSE_Wrapped_PrivKey);
            } else {
                CMS.debug("KRARemoteRequestHandler:serverSideKeyGen(): got IRemoteRequest.KRA_RESPONSE_Wrapped_PrivKey= "
                        + value);
                response.put(IRemoteRequest.KRA_RESPONSE_Wrapped_PrivKey, value);
            }

            value = (String) response.get(IRemoteRequest.KRA_RESPONSE_IV_Param);
            if (value == null) {
                CMS.debug("KRARemoteRequestHandler: serverSideKeyGen(): response missing name-value pair for: " +
                        IRemoteRequest.KRA_RESPONSE_IV_Param);
            } else {
                CMS.debug("KRARemoteRequestHandler:serverSideKeyGen(): got IRemoteRequest.KRA_RESPONSE_IV_Param= "
                        + value);
                response.put(IRemoteRequest.KRA_RESPONSE_IV_Param, value);
            }

            CMS.debug("KRARemoteRequestHandler: serverSideKeyGen(): ends.");
            return new KRAServerSideKeyGenResponse(connid, response);
        } else {
            CMS.debug("KRARemoteRequestHandler: serverSideKeyGen(): no response content.");
            throw new EBaseException("KRARemoteRequestHandler: serverSideKeyGen(): no response content.");
        }

    }

    /**
     * recoverKey recovers keys from KRA
     *
     * @param cuid is the token id
     * @param userid is the user id
     * @param sDesKey is the des key provided by the TKS for key encryption
     * @param b64cert is the Base64 encoding of a certificate used to recover
     *
     * @returns KRARecoverKeyResponse
     */
    public KRARecoverKeyResponse recoverKey(
            String cuid,
            String userid,
            String sDesKey,
            String b64cert)
            throws EBaseException {
        return recoverKey(cuid, userid, sDesKey, b64cert, BigInteger.valueOf(0));
    }

    public KRARecoverKeyResponse recoverKey(
            String cuid,
            String userid,
            String sDesKey,
            String b64cert,
            BigInteger keyid)
            throws EBaseException {

        CMS.debug("KRARemoteRequestHandler: recoverKey(): begins.");
        if (b64cert == null && keyid == BigInteger.valueOf(0)) {
            throw new EBaseException("KRARemoteRequestHandler: recoverKey(): one of b64cert or kid has to be a valid value");
        }
        if (cuid == null || userid == null || sDesKey == null) {
            throw new EBaseException("KRARemoteRequestHandler: recoverKey(): input parameter null.");
        }

        TPSSubsystem subsystem =
                (TPSSubsystem) CMS.getSubsystem(TPSSubsystem.ID);
        CMS.debug("KRARemoteRequestHandler: getting conn id: " + connid);
        HttpConnector conn =
                (HttpConnector) subsystem.getConnectionManager().getConnector(connid);
        if (conn == null) {
            CMS.debug("KRARemoteRequestHandler: recoverKey(): conn null");
            throw new EBaseException("KRARemoteRequestHandler: recoverKey(): conn null");
        }
        CMS.debug("KRARemoteRequestHandler: recoverKey(): sending request to KRA");

        String sendMsg = null;
        if (b64cert != null) { // recover by cert
            sendMsg = IRemoteRequest.TOKEN_CUID + "=" +
                    cuid +
                    "&" + IRemoteRequest.KRA_UserId + "=" +
                    userid +
                    "&" + IRemoteRequest.KRA_RECOVERY_CERT + "=" +
                    b64cert +
                    "&" + IRemoteRequest.KRA_Trans_DesKey + "=" +
                    sDesKey;
        } else if (keyid != BigInteger.valueOf(0)){ // recover by keyid ... keyid != BigInteger.valueOf(0)
            CMS.debug("KRARemoteRequestHandler: recoverKey(): keyid = " + keyid);
            sendMsg = IRemoteRequest.TOKEN_CUID + "=" +
                    cuid +
                    "&" + IRemoteRequest.KRA_UserId + "=" +
                    userid +
                    "&" + IRemoteRequest.KRA_RECOVERY_KEYID + "=" +
                    keyid.toString() +
                    "&" + IRemoteRequest.KRA_Trans_DesKey + "=" +
                    sDesKey;
        }
        CMS.debug("KRARemoteRequestHandler: recoverKey(): sendMsg =" + sendMsg);
        HttpResponse resp =
                conn.send("TokenKeyRecovery",
                        sendMsg);
        if (resp == null) {
            throw new EBaseException(
                    "KRARemoteRequestHandler: recoverKey(): No response object returned from connection.");
        }

        String content = resp.getContent();

        CMS.debug("KRARemoteRequestHandler: recoverKey(): got content = " + content);
        if (content != null && !content.equals("")) {
            Hashtable<String, Object> response =
                    parseResponse(content);

            /**
             * When a value is not found in response, keep going so we know
             * what else is missing
             * Note: response values "missing" might not be bad for some cases
             */
            Integer ist = new Integer(IRemoteRequest.RESPONSE_STATUS_NOT_FOUND);
            String value = (String) response.get(IRemoteRequest.RESPONSE_STATUS);

            CMS.debug("KRARemoteRequestHandler: recoverKey(): got status = " + value);
            ist = Integer.parseInt(value);
            if (ist != 0) {
                CMS.debug("KRARemoteRequestHandler: recoverKey(): status not 0, getting error string... ");
                value = (String) response.get(IRemoteRequest.RESPONSE_ERROR_STRING);
                if (value == null) {
                    CMS.debug("KRARemoteRequestHandler: recoverKey(): response missing name-value pair for: " +
                            IRemoteRequest.RESPONSE_ERROR_STRING);
                } else {
                    CMS.debug("KRARemoteRequestHandler: recoverKey(): got IRemoteRequest.RESPONSE_ERROR_STRING = "
                            + value);
                    response.put(IRemoteRequest.RESPONSE_ERROR_STRING, value);
                }
            }
            response.put(IRemoteRequest.RESPONSE_STATUS, ist);

            value = (String) response.get(IRemoteRequest.KRA_RESPONSE_PublicKey);
            if (value == null) {
                CMS.debug("KRARemoteRequestHandler: recoverKey(): response missing name-value pair for: " +
                        IRemoteRequest.KRA_RESPONSE_PublicKey);
            } else {
                CMS.debug("KRARemoteRequestHandler:recoverKey(): got IRemoteRequest.KRA_RESPONSE_PublicKey= " + value);
                response.put(IRemoteRequest.KRA_RESPONSE_PublicKey, value);
            }

            value = (String) response.get(IRemoteRequest.KRA_RESPONSE_Wrapped_PrivKey);
            if (value == null) {
                CMS.debug("KRARemoteRequestHandler: recoverKey(): response missing name-value pair for: " +
                        IRemoteRequest.KRA_RESPONSE_Wrapped_PrivKey);
            } else {
                CMS.debug("KRARemoteRequestHandler:recoverKey(): got IRemoteRequest.KRA_RESPONSE_Wrapped_PrivKey= "
                        + value);
                response.put(IRemoteRequest.KRA_RESPONSE_Wrapped_PrivKey, value);
            }

            value = (String) response.get(IRemoteRequest.KRA_RESPONSE_IV_Param);
            if (value == null) {
                CMS.debug("KRARemoteRequestHandler: recoverKey(): response missing name-value pair for: " +
                        IRemoteRequest.KRA_RESPONSE_IV_Param);
            } else {
                CMS.debug("KRARemoteRequestHandler:recoverKey(): got IRemoteRequest.KRA_RESPONSE_IV_Param= " + value);
                response.put(IRemoteRequest.KRA_RESPONSE_IV_Param, value);
            }

            CMS.debug("KRARemoteRequestHandler: recoverKey(): ends.");
            return new KRARecoverKeyResponse(connid, response);
        } else {
            CMS.debug("KRARemoteRequestHandler: recoverKey(): no response content.");
            throw new EBaseException("KRARemoteRequestHandler: recoverKey(): no response content.");
        }
    }
}