summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/servlet/csadmin/SizePanel.java
blob: 03f0e186d3a9d437e9715b65025a59bb38adeaba (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
// --- 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.cms.servlet.csadmin;

import java.io.IOException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPublicKey;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.context.Context;
import org.mozilla.jss.CryptoManager;
import org.mozilla.jss.NoSuchTokenException;
import org.mozilla.jss.crypto.TokenException;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.property.Descriptor;
import com.netscape.certsrv.property.IDescriptor;
import com.netscape.certsrv.property.PropertySet;
import com.netscape.certsrv.util.HttpInput;
import com.netscape.cms.servlet.wizard.WizardServlet;
import com.netscape.cmsutil.crypto.CryptoUtil;

public class SizePanel extends WizardPanelBase {
    private Vector<Cert> mCerts = null;
    private WizardServlet mServlet = null;

    private String default_ecc_curve_name;
    private String default_rsa_key_size;
    private boolean mShowSigning = false;

    public SizePanel() {
    }

    /**
     * Initializes this panel.
     */
    public void init(WizardServlet servlet, ServletConfig config, int panelno, String id)
            throws ServletException {
        setPanelNo(panelno);
        setName("Key Pairs");
        setId(id);
        mServlet = servlet;
    }

    public PropertySet getUsage() {
        PropertySet set = new PropertySet();

        Descriptor choiceDesc =
                new Descriptor(
                        IDescriptor.CHOICE,
                        "default,custom",
                        null, /* no default parameter */
                        "If 'default', the key size will be configured automatically. If 'custom', the key size will be set to the value of the parameter 'custom_size'.");

        set.add("choice", choiceDesc);

        Descriptor customSizeDesc = new Descriptor(IDescriptor.STRING, null, /* no constraint */
                null, /* no default parameter */
                "Custom Key Size");

        set.add("custom_size", customSizeDesc);

        return set;
    }

    public void cleanUp() throws IOException {
        IConfigStore cs = CMS.getConfigStore();
        /* clean up if necessary*/
        try {
            @SuppressWarnings("unused")
            boolean done = cs.getBoolean("preop.SizePanel.done"); // check for errors
            cs.putBoolean("preop.SizePanel.done", false);
            cs.commit(false);
        } catch (Exception e) {
        }
    }

    public boolean isPanelDone() {
        IConfigStore cs = CMS.getConfigStore();
        try {
            boolean s = cs.getBoolean("preop.SizePanel.done", false);
            if (s != true) {
                return false;
            } else {
                return true;
            }
        } catch (EBaseException e) {
        }

        return false;
    }

    /**
     * Display the panel.
     */
    public void display(HttpServletRequest request,
            HttpServletResponse response,
            Context context) {
        CMS.debug("SizePanel: display()");
        try {
            initParams(request, context);
        } catch (IOException e) {
        }

        context.put("firsttime", "false");
        String errorString = "";
        mCerts = new Vector<Cert>();

        IConfigStore config = CMS.getConfigStore();
        try {
            @SuppressWarnings("unused")
            boolean done = config.getBoolean("preop.SizePanel.done"); // check whether it's first time
        } catch (Exception e) {
            context.put("firsttime", "true");
        }

        try {
            default_ecc_curve_name = config.getString("keys.ecc.curve.default", "nistp256");
        } catch (Exception e) {
        }

        try {
            default_rsa_key_size = config.getString("keys.rsa.keysize.default", "2048");
        } catch (Exception e) {
        }

        try {
            // same token for now
            String token = config.getString(PRE_CONF_CA_TOKEN);
            String certTags = config.getString("preop.cert.list");
            StringTokenizer st = new StringTokenizer(certTags, ",");
            mShowSigning = false;

            while (st.hasMoreTokens()) {
                String certTag = st.nextToken();
                String nn = config.getString(
                        PCERT_PREFIX + certTag + ".nickname");
                Cert c = new Cert(token, nn, certTag);

                String s = config.getString(
                        PCERT_PREFIX + certTag + ".keysize.select", "default");

                if (s.equals("default")) {
                    c.setKeyOption("default");
                }
                if (s.equals("custom")) {
                    c.setKeyOption("custom");
                }

                s = config.getString(
                        PCERT_PREFIX + certTag + ".keysize.custom_size",
                        default_rsa_key_size);
                c.setCustomKeysize(s);

                s = config.getString(
                        PCERT_PREFIX + certTag + ".curvename.custom_name",
                        default_ecc_curve_name);
                c.setCustomCurvename(s);

                boolean signingRequired = config.getBoolean(
                        PCERT_PREFIX + certTag + ".signing.required",
                        false);
                c.setSigningRequired(signingRequired);
                if (signingRequired)
                    mShowSigning = true;

                String userfriendlyname = config.getString(
                        PCERT_PREFIX + certTag + ".userfriendlyname");
                c.setUserFriendlyName(userfriendlyname);
                boolean enable = config.getBoolean(PCERT_PREFIX + certTag + ".enable", true);
                c.setEnable(enable);
                mCerts.addElement(c);
            }// while
        } catch (Exception e) {
            CMS.debug("SizePanel: display() " + e.toString());
        }
        CMS.debug("SizePanel: display() 1");

        context.put("show_signing", mShowSigning ? "true" : "false");
        context.put("certs", mCerts);
        context.put("errorString", errorString);
        context.put("default_keysize", default_rsa_key_size);
        context.put("default_ecc_curvename", default_ecc_curve_name);
        context.put("panel", "admin/console/config/sizepanel.vm");
    }

    /**
     * Checks if the given parameters are valid.
     */
    public void validate(HttpServletRequest request,
            HttpServletResponse response,
            Context context) throws IOException {
    }

    /**
     * Commit parameter changes
     */
    public void update(HttpServletRequest request,
            HttpServletResponse response,
            Context context) throws IOException, NumberFormatException {
        CMS.debug("SizePanel: update()");
        boolean hasErr = false;
        IConfigStore config = CMS.getConfigStore();
        String select1 = "";
        String val1 = null;
        boolean hasChanged = false;
        try {
            select1 = config.getString("preop.subsystem.select", "");
        } catch (Exception e) {
        }

        context.put("firsttime", "false");
        try {
            @SuppressWarnings("unused")
            boolean done = config.getBoolean("preop.SizePanel.done"); // check whether it's first time
        } catch (Exception e) {
            context.put("firsttime", "true");
            if (select1.equals("clone")) {
                // preset the sslserver dn for cloning case
                try {
                    String val = config.getString("preop.cert.sslserver.dn", "");
                    config.putString("preop.cert.sslserver.dn", val + ",o=clone");
                } catch (Exception ee) {
                }
            }
        }

        String token = "";
        try {
            token = config.getString(PRE_CONF_CA_TOKEN, "");
            Enumeration<Cert> c = mCerts.elements();

            while (c.hasMoreElements()) {
                Cert cert = c.nextElement();
                String ct = cert.getCertTag();
                boolean enable = config.getBoolean(PCERT_PREFIX + ct + ".enable", true);
                if (!enable)
                    continue;

                String keytype = HttpInput.getKeyType(request, ct + "_keytype"); // rsa or ecc

                String keyalgorithm = HttpInput.getString(request, ct + "_keyalgorithm");
                if (keyalgorithm == null) {
                    if (keytype != null && keytype.equals("ecc")) {
                        keyalgorithm = "SHA256withEC";
                    } else {
                        keyalgorithm = "SHA256withRSA";
                    }
                }

                String signingalgorithm = HttpInput.getString(request, ct + "_signingalgorithm");
                if (signingalgorithm == null) {
                    signingalgorithm = keyalgorithm;
                }

                String select = HttpInput.getID(request, ct + "_choice");

                if (select == null) {
                    CMS.debug("SizePanel: " + ct + "_choice not found");
                    throw new IOException(
                            "SizePanel: " + ct + "_choice not found");
                }
                CMS.debug(
                        "SizePanel: update() keysize choice selected:" + select);
                String oldkeysize =
                        config.getString(PCERT_PREFIX + ct + ".keysize.size", "");
                String oldkeytype =
                        config.getString(PCERT_PREFIX + ct + ".keytype", "");
                String oldkeyalgorithm =
                        config.getString(PCERT_PREFIX + ct + ".keyalgorithm", "");
                String oldsigningalgorithm =
                        config.getString(PCERT_PREFIX + ct + ".signingalgorithm", "");
                String oldcurvename =
                        config.getString(PCERT_PREFIX + ct + ".curvename.name", "");

                if (select.equals("default")) {
                    // XXXrenaming these...keep for now just in case
                    config.putString("preop.keysize.select", "default");
                    if (keytype != null && keytype.equals("ecc")) {
                        config.putString("preop.curvename.custom_name",
                                default_ecc_curve_name);
                        config.putString("preop.curvename.name", default_ecc_curve_name);
                    } else {
                        config.putString("preop.keysize.custom_size",
                                default_rsa_key_size);
                        config.putString("preop.keysize.size", default_rsa_key_size);
                    }

                    config.putString(PCERT_PREFIX + ct + ".keytype", keytype);
                    config.putString(PCERT_PREFIX + ct + ".keyalgorithm", keyalgorithm);
                    config.putString(PCERT_PREFIX + ct + ".signingalgorithm", signingalgorithm);
                    config.putString(PCERT_PREFIX + ct + ".keysize.select",
                            "default");

                    if (keytype != null && keytype.equals("ecc")) {
                        config.putString(PCERT_PREFIX + ct +
                                ".curvename.custom_name",
                                default_ecc_curve_name);
                        config.putString(PCERT_PREFIX + ct + ".curvename.name",
                                default_ecc_curve_name);
                    } else {
                        config.putString(PCERT_PREFIX + ct +
                                ".keysize.custom_size",
                                default_rsa_key_size);
                        config.putString(PCERT_PREFIX + ct + ".keysize.size",
                                default_rsa_key_size);
                    }
                } else if (select.equals("custom")) {
                    // XXXrenaming these...keep for now just in case
                    config.putString("preop.keysize.select", "custom");
                    if (keytype != null && keytype.equals("ecc")) {
                        config.putString("preop.curvename.name",
                                HttpInput.getString(request, ct + "_custom_curvename"));
                        config.putString("preop.curvename.custom_name",
                                HttpInput.getString(request, ct + "_custom_curvename"));
                    } else {
                        config.putString("preop.keysize.size",
                                HttpInput.getKeySize(request, ct + "_custom_size", keytype));
                        config.putString("preop.keysize.custom_size",
                                HttpInput.getKeySize(request, ct + "_custom_size", keytype));
                    }

                    config.putString(PCERT_PREFIX + ct + ".keytype", keytype);
                    config.putString(PCERT_PREFIX + ct + ".keyalgorithm", keyalgorithm);
                    config.putString(PCERT_PREFIX + ct + ".signingalgorithm", signingalgorithm);
                    config.putString(PCERT_PREFIX + ct + ".keysize.select",
                            "custom");

                    if (keytype != null && keytype.equals("ecc")) {
                        config.putString(PCERT_PREFIX + ct + ".curvename.custom_name",
                                HttpInput.getString(request, ct + "_custom_curvename"));
                        config.putString(PCERT_PREFIX + ct + ".curvename.name",
                                HttpInput.getString(request, ct + "_custom_curvename"));
                    } else {
                        config.putString(PCERT_PREFIX + ct + ".keysize.custom_size",
                                HttpInput.getKeySize(request, ct + "_custom_size"));
                        config.putString(PCERT_PREFIX + ct + ".keysize.size",
                                HttpInput.getKeySize(request, ct + "_custom_size"));
                    }
                } else {
                    CMS.debug("SizePanel: invalid choice " + select);
                    throw new IOException("invalid choice " + select);
                }

                String newkeysize =
                        config.getString(PCERT_PREFIX + ct + ".keysize.size", "");
                String newkeytype =
                        config.getString(PCERT_PREFIX + ct + ".keytype", "");
                String newkeyalgorithm =
                        config.getString(PCERT_PREFIX + ct + ".keyalgorithm", "");
                String newsigningalgorithm =
                        config.getString(PCERT_PREFIX + ct + ".signingalgorithm", "");
                String newcurvename =
                        config.getString(PCERT_PREFIX + ct + ".curvename.name", "");

                if (!oldkeysize.equals(newkeysize) ||
                        !oldkeytype.equals(newkeytype) ||
                        !oldkeyalgorithm.equals(newkeyalgorithm) ||
                        !oldsigningalgorithm.equals(newsigningalgorithm) ||
                        !oldcurvename.equals(newcurvename))
                    hasChanged = true;
            }// while

            try {
                config.commit(false);
            } catch (EBaseException e) {
                CMS.debug("SizePanel: update() Exception caught at config commit: " + e.toString());
            }

            val1 = HttpInput.getID(request, "generateKeyPair");

            if (hasChanged || (val1 != null && !val1.equals(""))) {
                mServlet.cleanUpFromPanel(mServlet.getPanelNo(request));
            } else if (isPanelDone()) {
                context.put("updateStatus", "success");
                return;
            }
        } catch (IOException e) {
            CMS.debug("SizePanel: update() IOException caught: " + e.toString());
            context.put("updateStatus", "failure");
            throw e;
        } catch (NumberFormatException e) {
            CMS.debug("SizePanel: update() NumberFormatException caught: " + e.toString());
            context.put("updateStatus", "failure");
            throw e;
        } catch (Exception e) {
            CMS.debug("SizePanel: update() Exception caught: " + e.toString());
        }

        // generate key pair 
        Enumeration<Cert> c = mCerts.elements();

        while (c.hasMoreElements()) {
            Cert cert = c.nextElement();
            String ct = cert.getCertTag();
            String friendlyName = ct;
            boolean enable = true;
            try {
                enable = config.getBoolean(PCERT_PREFIX + ct + ".enable", true);
                friendlyName = config.getString(PCERT_PREFIX + ct + ".userfriendlyname", ct);
            } catch (Exception e) {
            }

            if (!enable)
                continue;

            try {
                String keytype = config.getString(PCERT_PREFIX + ct + ".keytype");

                if (keytype.equals("rsa")) {
                    int keysize = config.getInteger(
                            PCERT_PREFIX + ct + ".keysize.size");

                    createRSAKeyPair(token, keysize, config, ct);
                } else {
                    String curveName = config.getString(
                            PCERT_PREFIX + ct + ".curvename.name", default_ecc_curve_name);
                    createECCKeyPair(token, curveName, config, ct);
                }
                config.commit(false);
            } catch (Exception e) {
                CMS.debug(e);
                CMS.debug("SizePanel: key generation failure: " + e.toString());
                context.put("updateStatus", "failure");
                throw new IOException("key generation failure for the certificate: " + friendlyName +
                                      ".  See the logs for details.");
            }
        } // while

        if (hasErr == false) {
            config.putBoolean("preop.SizePanel.done", true);
            try {
                config.commit(false);
            } catch (EBaseException e) {
                CMS.debug(
                        "SizePanel: update() Exception caught at config commit: "
                                + e.toString());
            }
        }
        CMS.debug("SizePanel: update() done");
        context.put("updateStatus", "success");

    }

    public void createECCKeyPair(String token, String curveName, IConfigStore config, String ct)
            throws NoSuchAlgorithmException, NoSuchTokenException, TokenException,
            CryptoManager.NotInitializedException {
        CMS.debug("Generating ECC key pair with curvename=" + curveName +
                    ", token=" + token);
        KeyPair pair = null;
        /*
         * default ssl server cert to ECDHE unless stated otherwise
         * note: IE only supports "ECDHE", but "ECDH" is more efficient
         *
         * for "ECDHE", server.xml should have the following for ciphers:
         * +TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
         * -TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
         *
         * for "ECDH", server.xml should have the following for ciphers:
         * -TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
         * +TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
         */
        String sslType = "ECDHE";
        try {
            sslType = config.getString(PCERT_PREFIX + ct + "ec.type", "ECDHE");
        } catch (Exception e) {
            CMS.debug("SizePanel: createECCKeyPair() Exception caught at config.getString for ec type");
        }

        // ECDHE needs "SIGN" but no "DERIVE"
        org.mozilla.jss.crypto.KeyPairGeneratorSpi.Usage usages_mask[] = {
                org.mozilla.jss.crypto.KeyPairGeneratorSpi.Usage.DERIVE
        };

        // ECDH needs "DERIVE" but no any kind of "SIGN"
        org.mozilla.jss.crypto.KeyPairGeneratorSpi.Usage ECDH_usages_mask[] = {
                org.mozilla.jss.crypto.KeyPairGeneratorSpi.Usage.SIGN,
                org.mozilla.jss.crypto.KeyPairGeneratorSpi.Usage.SIGN_RECOVER,
        };

        do {
            if (ct.equals("sslserver") && sslType.equalsIgnoreCase("ECDH")) {
                CMS.debug("SizePanel: createECCKeypair: sslserver cert for ECDH. Make sure server.xml is set properly with -TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,+TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA");
                pair = CryptoUtil.generateECCKeyPair(token, curveName,
                        null,
                        ECDH_usages_mask);
            } else {
                if (ct.equals("sslserver")) {
                    CMS.debug("SizePanel: createECCKeypair: sslserver cert for ECDHE. Make sure server.xml is set properly with +TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,-TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA");
                }
                pair = CryptoUtil.generateECCKeyPair(token, curveName,
                        null,
                        usages_mask);
            }

            // XXX - store curve , w
            byte id[] = ((org.mozilla.jss.crypto.PrivateKey) pair.getPrivate()).getUniqueID();
            String kid = CryptoUtil.byte2string(id);
            config.putString(PCERT_PREFIX + ct + ".privkey.id", kid);

            // try to locate the private key
            org.mozilla.jss.crypto.PrivateKey privk =
                    CryptoUtil.findPrivateKeyFromID(CryptoUtil.string2byte(kid));
            if (privk == null) {
                CMS.debug("Found bad ECC key id " + kid);
                pair = null;
            }
        } while (pair == null);

        CMS.debug("Public key class " + pair.getPublic().getClass().getName());
        byte encoded[] = pair.getPublic().getEncoded();
        config.putString(PCERT_PREFIX + ct + ".pubkey.encoded",
                CryptoUtil.byte2string(encoded));

        String keyAlgo = "";
        try {
            keyAlgo = config.getString(PCERT_PREFIX + ct + ".signingalgorithm");
        } catch (Exception e1) {
        }

        setSigningAlgorithm(ct, keyAlgo, config);
    }

    public void createRSAKeyPair(String token, int keysize, IConfigStore config, String ct)
            throws NoSuchAlgorithmException, NoSuchTokenException, TokenException,
            CryptoManager.NotInitializedException {
        /* generate key pair */
        KeyPair pair = null;
        do {
            pair = CryptoUtil.generateRSAKeyPair(token, keysize);
            byte id[] = ((org.mozilla.jss.crypto.PrivateKey) pair.getPrivate()).getUniqueID();
            String kid = CryptoUtil.byte2string(id);
            config.putString(PCERT_PREFIX + ct + ".privkey.id", kid);
            // try to locate the private key
            org.mozilla.jss.crypto.PrivateKey privk =
                    CryptoUtil.findPrivateKeyFromID(CryptoUtil.string2byte(kid));
            if (privk == null) {
                CMS.debug("Found bad RSA key id " + kid);
                pair = null;
            }
        } while (pair == null);

        byte modulus[] = ((RSAPublicKey) pair.getPublic()).getModulus().toByteArray();
        byte exponent[] = ((RSAPublicKey) pair.getPublic()).getPublicExponent().toByteArray();

        config.putString(PCERT_PREFIX + ct + ".pubkey.modulus",
                CryptoUtil.byte2string(modulus));
        config.putString(PCERT_PREFIX + ct + ".pubkey.exponent",
                CryptoUtil.byte2string(exponent));

        String keyAlgo = "";
        try {
            keyAlgo = config.getString(PCERT_PREFIX + ct + ".signingalgorithm");
        } catch (Exception e1) {
        }

        setSigningAlgorithm(ct, keyAlgo, config);
    }

    public void setSigningAlgorithm(String ct, String keyAlgo, IConfigStore config) {
        String systemType = "";
        try {
            systemType = config.getString("preop.system.name");
        } catch (Exception e1) {
        }
        if (systemType.equalsIgnoreCase("CA")) {
            if (ct.equals("signing")) {
                config.putString("ca.signing.defaultSigningAlgorithm",
                           keyAlgo);
                config.putString("ca.crl.MasterCRL.signingAlgorithm",
                           keyAlgo);
            } else if (ct.equals("ocsp_signing")) {
                config.putString("ca.ocsp_signing.defaultSigningAlgorithm",
                           keyAlgo);
            }
        } else if (systemType.equalsIgnoreCase("OCSP")) {
            if (ct.equals("signing")) {
                config.putString("ocsp.signing.defaultSigningAlgorithm",
                           keyAlgo);
            }
        } else if (systemType.equalsIgnoreCase("KRA") ||
                systemType.equalsIgnoreCase("DRM")) {
            if (ct.equals("transport")) {
                config.putString("kra.transportUnit.signingAlgorithm", keyAlgo);
            }
        }
    }

    public void initParams(HttpServletRequest request, Context context)
                   throws IOException {
        IConfigStore config = CMS.getConfigStore();
        String s = "";
        try {
            context.put("title", "Key Pairs");

            s = config.getString("preop.subsystem.select", "");
            context.put("select", s);

            s = config.getString("preop.hierarchy.select", "root");
            context.put("hselect", s);

            s = config.getString("preop.ecc.algorithm.list", "SHA256withEC,SHA1withEC,SHA384withEC,SHA512withEC");
            context.put("ecclist", s);

            s =
                    config.getString("preop.rsa.algorithm.list",
                            "SHA256withRSA,SHA1withRSA,SHA512withRSA,MD5withRSA,MD2withRSA");
            context.put("rsalist", s);

            s = config.getString("keys.ecc.curve.list", "nistp256");
            context.put("curvelist", s);

            s = config.getString("keys.ecc.curve.display.list", "nistp256");
            context.put("displaycurvelist", s);

            s = config.getString("pkicreate.subsystem_type");
            context.put("subsystemtype", s);

        } catch (Exception e) {
            CMS.debug("SizePanel(): initParams: unable to set all initial parameters:" + e);
        }
    }

    /**
     * If validiate() returns false, this method will be called.
     */
    public void displayError(HttpServletRequest request,
            HttpServletResponse response,
            Context context) {
        try {
            initParams(request, context);
        } catch (IOException e) {
        }

        context.put("certs", mCerts);
        context.put("show_signing", mShowSigning ? "true" : "false");
        context.put("default_keysize", default_rsa_key_size);
        context.put("default_ecc_curvename", default_ecc_curve_name);

        context.put("panel", "admin/console/config/sizepanel.vm");
    }
}