summaryrefslogtreecommitdiffstats
path: root/pki/base/console/src/com/netscape/admin/certsrv/security/Response.java
blob: 5d311d33bdcff1d478e47df49d4077df00741636 (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// --- 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) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.admin.certsrv.security;

/**
 *
 *  Parse the response that was sent back by the cgi
 *
 */

import java.util.*;
import java.io.*;
import com.netscape.management.client.util.Debug;

//this class need some optimization....

class Response {

    String _response;
    String _cert = "";
    Vector _messages = new Vector();
    Vector _certList = null;
    CertInfo _certInfo = null;
    Hashtable _certInstInfo = null;

    Hashtable _ssl2Preference = null;
    Hashtable _ssl3Preference = null;

    String startCert = "-----BEGIN NEW CERTIFICATE REQUEST-----";
    String endCert = "-----END NEW CERTIFICATE REQUEST-----";
    String startCertList = "-----BEGIN CERT LIST-----";
    String endCertList = "-----END CERT LIST-----";
    String startCertInfo = "-----BEGIN CERTIFICATE INFO-----";
    String endCertInfo = "-----END CERTIFICATE INFO-----";
    String startCRLCertInfo = "-----BEGIN CRL INFO-----";
    String endCRLCertInfo = "-----END CRL INFO-----";

    String startCertInstInfo = "-----BEGIN CERTIFICATE INSTALL INFO-----";
    String endCertInstInfo = "-----END CERTIFICATE INSTALL INFO-----";
    String startCRLCertInstInfo = "-----BEGIN CRL INSTALL INFO-----";
    String endCRLCertInstInfo = "-----END CRL INSTALL INFO-----";

    boolean _fCert = false, _fCertList = false, _fCertInfo = false,
    _fCertInstInfo = false;
    boolean _fsecurityDomestic = false, _fsecurityFortezza = false;

    void parseCertificate(String response) {
        if (response.indexOf(startCert) != -1) {
            _cert = response.substring(response.indexOf(startCert),
                    response.indexOf(endCert) + endCert.length());
            _fCert = true;
        }
    }

    void parseCertificateList(String response) {
        if (response.indexOf(startCertList) != -1) {
            _certList = new Vector();

            try {
                BufferedReader stream =
                        new BufferedReader(new StringReader(response));
                while (!(stream.readLine().equals(startCertList))) {
                }

                String line;
                while (!((line = stream.readLine()).equals(endCertList))) {
                    //need to hack the string that return by the NS secutiry code.
                    //it assumes we are working with html
                    line = urlDecode(line);
                    StringTokenizer token =
                            new StringTokenizer(line, "=;\n", false);
                    Debug.print(line);

                    String certName = "", certType = "", certExpire = "";
                    try {
                        certName = token.nextToken();
                        certType = token.nextToken();
                        certExpire = token.nextToken();
                    } catch (NoSuchElementException noToken) { }

                    _certList.addElement(
                            new CertBasicInfo(certName, certType,
                            certExpire));
                }
            } catch (IOException e) {
                Debug.println(e.getMessage());
            }

            _fCertList = true;
        }

    }

    void parseCertificateInfo(String response) {
        if ((response.indexOf(startCertInfo) != -1) ||
                (response.indexOf(startCRLCertInfo) != -1)) {

            try {
                BufferedReader stream =
                        new BufferedReader(new StringReader(response));
                String line;
                while (! (((line =
                        stream.readLine()).equals(startCertInfo)) ||
                        (line.equals(startCRLCertInfo)))) {
                }

                String issuer = "", subject = "", serialNumber = "",
                version = "", validFrom = "", validTo = "";
                String fingerPrint = "", trustCert = "", certName = "",
                certDeleted = "0", certTitle = "";

                while (!((line = stream.readLine()).equals(endCertInfo))
                        && !(line.equals(endCRLCertInfo))) {

                    //need to hack the string that was returned by the NS secutiry code.
                    //it assumes we are working with html
                    line = urlDecode(line);
                    StringTokenizer token =
                            new StringTokenizer(line, "=\n", false);
                    Debug.print(line);


                    try {
                        String keyWord = token.nextToken();
                        if (keyWord.equals("ISSUER")) {
                            //have to hack again because of the stupid html in the data
                            issuer = KeyCertUtility.replace(
                                    token.nextToken(), "<br>", "\n");
                            ;
                        } else if (keyWord.equals("SUBJECT")) {
                            subject = KeyCertUtility.replace(
                                    token.nextToken(), "<br>", "\n");
                            ;
                        } else if (keyWord.equals("SERIALNUMBER")) {
                            serialNumber = token.nextToken();
                        } else if (keyWord.equals("VERSION")) {
                            version = token.nextToken();
                        } else if (keyWord.equals("NOTBEFORE")) {
                            validFrom = token.nextToken();
                        } else if (keyWord.equals("NOTAFTER")) {
                            validTo = token.nextToken();
                        } else if (keyWord.equals("FINGERPRINT")) {
                            fingerPrint = token.nextToken();
                        } else if (keyWord.equals("TRUSTED")) {
                            trustCert = token.nextToken();
                        } else if (keyWord.equals("CERTNAME")) {
                            certName = token.nextToken();
                        } else if (keyWord.equals("CERTDELETED")) {
                            certDeleted = token.nextToken();
                        } else if (keyWord.equals("CERTTITLE")) {
                            certTitle = token.nextToken();
                        }
                    } catch (NoSuchElementException noToken) {
                        Debug.print(noToken.getMessage());
                    }

                }

                _certInfo = new CertInfo(certName, issuer, subject,
                        serialNumber, version, validFrom, validTo,
                        fingerPrint, trustCert, certDeleted, certTitle);
            } catch (IOException e) {
                Debug.println(e.getMessage());
            }

            _fCertInfo = true;
        }

    }

    void parseCertificateInstInfo(String response) {
        if ((response.indexOf(startCertInstInfo) != -1) ||
                (response.indexOf(startCRLCertInstInfo) != -1)) {
            _certInstInfo = new Hashtable();

            try {
                BufferedReader stream =
                        new BufferedReader(new StringReader(response));
                String line;

                while (! (((line =
                        stream.readLine()).equals(startCertInstInfo))
                        || (line.equals(startCRLCertInstInfo)))) {
                }


                while (! ((line =
                        stream.readLine()).equals(endCertInstInfo)) &&
                        !(line.equals(endCRLCertInstInfo))) {
                    StringTokenizer token =
                            new StringTokenizer(line, "=\n", false);
                    Debug.print(line);
                    try {
                        String key = token.nextToken();
                        String val = token.nextToken();
                        _certInstInfo.put(key, val);
                    } catch (NoSuchElementException noToken) {
                        Debug.print(noToken.getMessage());
                    }
                }
            } catch (IOException e) {
                Debug.println(e.getMessage());
            }
            _fCertInstInfo = true;
        }
    }

    public static String urlDecode(String urlString) {
        ByteArrayOutputStream out =
                new ByteArrayOutputStream(urlString.length());

        for (int i = 0; i < urlString.length(); i++) {
            int c = (int) urlString.charAt(i);
            if (c == '+') {
                out.write(' ');
            } else if (c == '%') {
                int c1 = Character.digit(urlString.charAt(++i), 16);
                int c2 = Character.digit(urlString.charAt(++i), 16);
                out.write((char)(c1 * 16 + c2));
            } else {
                out.write(c);
            }
        }

        return out.toString();
    }

    Vector familyList;
    public Vector parseFamilyList(String response) {
        familyList = new Vector();
        _fsecurityFortezza = false;
        _fsecurityDomestic = false;
        try {
            BufferedReader stream =
                    new BufferedReader(new StringReader(response));
            String line = null;

            while (!(((line = stream.readLine()).startsWith("NULL")))) {
                String cipherName = line.substring(0, line.indexOf("="));

                StringTokenizer st = new StringTokenizer(
                        line.substring(line.indexOf("=") + 1,
                        line.length()), ",\n", false);
                Vector tokenList = new Vector();
                Hashtable tokenCertList = new Hashtable();
                while (st.hasMoreTokens()) {
                    String token = st.nextToken();
                    tokenList.addElement(token);
                    tokenCertList.put(token, "");
                }

                Enumeration e = tokenList.elements();
                while (e.hasMoreElements()) {
                    String token = (String)(e.nextElement());
                    line = stream.readLine();
                    String certListString = line.substring(
                            (token + "-certs=").length(), line.length());
                    StringTokenizer certNames =
                            new StringTokenizer(certListString, ",\n",
                            false);
                    Vector certList = new Vector();
                    while (certNames.hasMoreTokens()) {
                        certList.addElement(certNames.nextToken());
                    }
                    tokenCertList.put(token, certList);
                }

                familyList.addElement(
                        new CipherEntry(cipherName, tokenCertList));
            }
            if ((line = stream.readLine()).startsWith("security")) {
                if (line.endsWith("fortezza")) {
                    _fsecurityFortezza = true;
                    _fsecurityDomestic = true;
                }
                if (line.endsWith("domestic")) {
                    _fsecurityDomestic = true;
                }
            }
        } catch (Exception e) {
            Debug.println("com.netscape.admin.certsrv.security.response:"+
                          e.toString());
        }
        return familyList;
    }


    Vector moduleList;
    public Vector parseModuleList(String response) {

        moduleList = new Vector();

        try {
            BufferedReader stream =
                    new BufferedReader(new StringReader(response));
            String line = stream.readLine();

            StringTokenizer st = new StringTokenizer(
                    line.substring(line.indexOf("=") + 1,
                    line.length()), ",\n", false);
            while (st.hasMoreTokens())
                moduleList.addElement(st.nextToken());
        } catch (Exception e) {/*System.out.println(e);*/
        }
        return moduleList;
    }


    public Response(String response) {

        //Debug.print(response);
        if (response == null) {
            return;
        }

        _response = response;

        int beginIndex = 0, endIndex = 0;
        while (true) {
            beginIndex = response.indexOf(Message.NMC_STATUS, endIndex);
            endIndex = response.indexOf(Message.NMC_STATUS,
                    beginIndex + Message.NMC_STATUS.length());
            if ((endIndex == -1) && (beginIndex == -1)) {
                break;
            }
            if (endIndex != -1) {
                _messages.addElement( new Message( KeyCertUtility.replace(
                        response.substring(beginIndex, endIndex), "\r",
                        "")));
            } else {
                _messages.addElement( new Message( KeyCertUtility.replace(
                        response.substring(beginIndex,
                        response.length()), "\r", "")));
                break;
            }
        }
    }

    public Vector getFamilyList() {
        return parseFamilyList(_response);
    }

    public Vector getModuleList() {
        return parseModuleList(_response);
    }

    public boolean isSecurityDomestic() {
        return _fsecurityDomestic;
    }

    public boolean isSecurityFortezza() {
        return _fsecurityFortezza;
    }
    public boolean hasCert() {
        parseCertificate(_response);
        return _fCert;
    }
    public boolean hasMessage() {
        return (_messages.size() > 0);
    }

    public boolean hasCertList() {
        parseCertificateList(_response);
        return _fCertList;
    }
    public boolean hasCertInfo() {
        parseCertificateInfo(_response);
        return _fCertInfo;
    }
    public boolean hasCertInstInfo() {
        parseCertificateInstInfo(_response);
        return _fCertInstInfo;
    }
    public String getCert() {
        return _fCert ? _cert : "";
    }
    public Vector getMessages() {
        return _messages;
    }
    public Vector getCertList() {
        return _fCertList ? _certList : (new Vector());
    }
    public CertInfo getCertInfo() {
        return _certInfo;
    }
    public Hashtable getCertInstInfo() {
        return _certInstInfo;
    }
    public String getServerResponse() {
        return _response;
    }
}