summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/servlet/csadmin/RestoreKeyCertPanel.java
blob: 80a887fd2d09fdb09bf988df55912678d8a56379 (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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
// --- 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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.Principal;
import java.security.PublicKey;
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 netscape.ldap.LDAPDN;
import netscape.security.x509.X509CertImpl;

import org.apache.velocity.context.Context;
import org.mozilla.jss.CryptoManager;
import org.mozilla.jss.asn1.ANY;
import org.mozilla.jss.asn1.BMPString;
import org.mozilla.jss.asn1.OBJECT_IDENTIFIER;
import org.mozilla.jss.asn1.OCTET_STRING;
import org.mozilla.jss.asn1.SEQUENCE;
import org.mozilla.jss.asn1.SET;
import org.mozilla.jss.crypto.Cipher;
import org.mozilla.jss.crypto.CryptoStore;
import org.mozilla.jss.crypto.CryptoToken;
import org.mozilla.jss.crypto.EncryptionAlgorithm;
import org.mozilla.jss.crypto.IVParameterSpec;
import org.mozilla.jss.crypto.InternalCertificate;
import org.mozilla.jss.crypto.KeyGenAlgorithm;
import org.mozilla.jss.crypto.KeyGenerator;
import org.mozilla.jss.crypto.KeyWrapAlgorithm;
import org.mozilla.jss.crypto.KeyWrapper;
import org.mozilla.jss.crypto.SymmetricKey;
import org.mozilla.jss.crypto.X509Certificate;
import org.mozilla.jss.pkcs11.PK11Store;
import org.mozilla.jss.pkcs12.AuthenticatedSafes;
import org.mozilla.jss.pkcs12.CertBag;
import org.mozilla.jss.pkcs12.PFX;
import org.mozilla.jss.pkcs12.PasswordConverter;
import org.mozilla.jss.pkcs12.SafeBag;
import org.mozilla.jss.pkix.primitive.Attribute;
import org.mozilla.jss.pkix.primitive.EncryptedPrivateKeyInfo;
import org.mozilla.jss.pkix.primitive.PrivateKeyInfo;
import org.mozilla.jss.util.Password;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.property.PropertySet;
import com.netscape.certsrv.util.HttpInput;
import com.netscape.cms.servlet.wizard.WizardServlet;

public class RestoreKeyCertPanel extends WizardPanelBase {

    public RestoreKeyCertPanel() {
    }

    /**
     * Initializes this panel.
     */
    public void init(ServletConfig config, int panelno)
            throws ServletException {
        setPanelNo(panelno);
        setName("Import Keys and Certificates");
    }

    public void init(WizardServlet servlet, ServletConfig config, int panelno, String id)
            throws ServletException {
        setPanelNo(panelno);
        setName("Import Keys and Certificates");
        setId(id);
    }

    /**
     * Should we skip this panel for the configuration.
     */
    public boolean shouldSkip() {
        CMS.debug("RestoreKeyCertPanel: should skip");

        IConfigStore cs = CMS.getConfigStore();
        // if we are root, no need to get the certificate chain.

        try {
            String select = cs.getString("preop.subsystem.select", "");
            if (select.equals("clone")) {
                return false;
            }
        } catch (EBaseException e) {
        }

        return true;
    }

    public boolean isSubPanel() {
        return true;
    }

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

    public boolean isPanelDone() {
        IConfigStore cs = CMS.getConfigStore();
        try {
            String s = cs.getString("preop.restorekeycert.done", "");
            if (s == null || s.equals("")) {
                return false;
            } else {
                return true;
            }
        } catch (EBaseException e) {
        }
        return false;
    }

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

        /* XXX */

        return set;
    }

    /**
     * Display the panel.
     */
    public void display(HttpServletRequest request,
            HttpServletResponse response,
            Context context) {
        context.put("title", "Import Keys and Certificates");
        IConfigStore config = CMS.getConfigStore();

        if (isPanelDone()) {

            try {
                String s = config.getString("preop.pk12.path", "");
                String type = config.getString("preop.subsystem.select", "");
                context.put("path", s);
            } catch (Exception e) {
                CMS.debug(e.toString());
            }
        } else {
            context.put("path", "");
        }

        context.put("password", "");
        context.put("panel", "admin/console/config/restorekeycertpanel.vm");
        context.put("errorString", "");
    }

    /**
     * Checks if the given parameters are valid.
     */
    public void validate(HttpServletRequest request,
            HttpServletResponse response,
            Context context) throws IOException {
        IConfigStore config = CMS.getConfigStore();
        String tokenname = "";
        try {
            tokenname = config.getString("preop.module.token", "");
        } catch (Exception e) {
        }

        if (!tokenname.equals("Internal Key Storage Token"))
            return;

        // Path can be empty. If this case, we just want to 
        // get to the next panel. Customer has HSM.
        String s = HttpInput.getString(request, "path");
        // if (s == null || s.equals("")) {
        //    CMS.debug("RestoreKeyCertPanel validate: path is empty");
        //    throw new IOException("Path is empty");
        // }

        if (s != null && !s.equals("")) {
            s = HttpInput.getPassword(request, "__password");
            if (s == null || s.equals("")) {
                CMS.debug("RestoreKeyCertPanel validate: password is empty");
                context.put("updateStatus", "validate-failure");
                throw new IOException("Empty password");
            }
        }
    }

    /**
     * Commit parameter changes
     */
    public void update(HttpServletRequest request,
            HttpServletResponse response,
            Context context) throws IOException {
        IConfigStore config = CMS.getConfigStore();
        String path = HttpInput.getString(request, "path");
        if (path == null || path.equals("")) {
            // skip to next panel
            config.putBoolean("preop.restorekeycert.done", true);
            try {
                config.commit(false);
            } catch (EBaseException e) {
            }
            getConfigEntriesFromMaster(request, response, context);
            context.put("updateStatus", "success");
            return;
        }
        String pwd = HttpInput.getPassword(request, "__password");

        String tokenn = "";
        String instanceRoot = "";

        try {
            tokenn = config.getString("preop.module.token");
            instanceRoot = config.getString("instanceRoot");
        } catch (Exception e) {
        }

        if (tokenn.equals("Internal Key Storage Token")) {
            byte b[] = new byte[1000000];
            FileInputStream fis = new FileInputStream(instanceRoot + "/alias/" + path);
            while (fis.available() > 0)
                fis.read(b);
            fis.close();

            ByteArrayInputStream bis = new ByteArrayInputStream(b);
            StringBuffer reason = new StringBuffer();
            Password password = new Password(pwd.toCharArray());
            PFX pfx = null;
            boolean verifypfx = false;
            try {
                pfx = (PFX) (new PFX.Template()).decode(bis);
                verifypfx = pfx.verifyAuthSafes(password, reason);
            } catch (Exception e) {
                CMS.debug("RestoreKeyCertPanel update: Exception=" + e.toString());
            }

            if (verifypfx) {
                CMS.debug("RestoreKeyCertPanel verify the PFX.");
                AuthenticatedSafes safes = pfx.getAuthSafes();
                Vector pkeyinfo_collection = new Vector();
                Vector cert_collection = new Vector();
                for (int i = 0; i < safes.getSize(); i++) {
                    try {
                        SEQUENCE scontent = safes.getSafeContentsAt(null, i);
                        for (int j = 0; j < scontent.size(); j++) {
                            SafeBag bag = (SafeBag) scontent.elementAt(j);
                            OBJECT_IDENTIFIER oid = bag.getBagType();
                            if (oid.equals(SafeBag.PKCS8_SHROUDED_KEY_BAG)) {
                                EncryptedPrivateKeyInfo privkeyinfo =
                                        (EncryptedPrivateKeyInfo) bag.getInterpretedBagContent();
                                PasswordConverter passConverter = new PasswordConverter();
                                PrivateKeyInfo pkeyinfo = privkeyinfo.decrypt(password, new PasswordConverter());
                                Vector pkeyinfo_v = new Vector();
                                pkeyinfo_v.addElement(pkeyinfo);
                                SET bagAttrs = bag.getBagAttributes();
                                for (int k = 0; k < bagAttrs.size(); k++) {
                                    Attribute attrs = (Attribute) bagAttrs.elementAt(k);
                                    OBJECT_IDENTIFIER aoid = attrs.getType();
                                    if (aoid.equals(SafeBag.FRIENDLY_NAME)) {
                                        SET val = attrs.getValues();
                                        ANY ss = (ANY) val.elementAt(0);
                                        ByteArrayInputStream bbis = new ByteArrayInputStream(ss.getEncoded());
                                        BMPString sss = (BMPString) (new BMPString.Template()).decode(bbis);
                                        String s = sss.toString();
                                        pkeyinfo_v.addElement(s);
                                    }
                                }
                                pkeyinfo_collection.addElement(pkeyinfo_v);
                            } else if (oid.equals(SafeBag.CERT_BAG)) {
                                CertBag cbag = (CertBag) bag.getInterpretedBagContent();
                                OCTET_STRING str = (OCTET_STRING) cbag.getInterpretedCert();
                                byte[] x509cert = str.toByteArray();
                                Vector cert_v = new Vector();
                                cert_v.addElement(x509cert);
                                SET bagAttrs = bag.getBagAttributes();

                                if (bagAttrs != null) {
                                    for (int k = 0; k < bagAttrs.size(); k++) {
                                        Attribute attrs = (Attribute) bagAttrs.elementAt(k);
                                        OBJECT_IDENTIFIER aoid = attrs.getType();
                                        if (aoid.equals(SafeBag.FRIENDLY_NAME)) {
                                            SET val = attrs.getValues();
                                            ANY ss = (ANY) val.elementAt(0);
                                            ByteArrayInputStream bbis = new ByteArrayInputStream(ss.getEncoded());
                                            BMPString sss = (BMPString) (new BMPString.Template()).decode(bbis);
                                            String s = sss.toString();
                                            cert_v.addElement(s);
                                        }
                                    }
                                }

                                cert_collection.addElement(cert_v);
                            }
                        }
                    } catch (Exception e) {
                        CMS.debug("RestoreKeyCertPanel update: Exception=" + e.toString());
                    }
                }

                importkeycert(pkeyinfo_collection, cert_collection);
            } else {
                context.put("updateStatus", "failure");
                throw new IOException("The pkcs12 file is not correct.");
            }
        }

        String subsystemtype = "";
        String cstype = "";
        try {
            subsystemtype = config.getString("preop.subsystem.select", "");
            cstype = config.getString("cs.type", "");
        } catch (Exception e) {
        }
        cstype = toLowerCaseSubsystemType(cstype);

        if (subsystemtype.equals("clone")) {
            CMS.debug("RestoreKeyCertPanel: this is the clone subsystem");
            boolean cloneReady = isCertdbCloned(request, context);
            if (!cloneReady) {
                CMS.debug("RestoreKeyCertPanel update: clone does not have all the certificates.");
                context.put("errorString", "Make sure you have copied the certificate database over to the clone");
                context.put("updateStatus", "failure");
                throw new IOException("Clone is not ready");
            }
        }

        config.putBoolean("preop.restorekeycert.done", true);
        try {
            config.commit(false);
        } catch (EBaseException e) {
        }

        getConfigEntriesFromMaster(request, response, context);
        context.put("updateStatus", "success");
    }

    private void getConfigEntriesFromMaster(HttpServletRequest request,
            HttpServletResponse response, Context context) throws IOException {
        try {
            IConfigStore config = CMS.getConfigStore();
            String cstype = "";
            try {
                cstype = config.getString("cs.type", "");
            } catch (Exception e) {
            }
            cstype = toLowerCaseSubsystemType(cstype);

            String session_id = CMS.getConfigSDSessionId();
            String sd_hostname = "";
            int sd_port = -1;
            String master_hostname = "";
            int master_port = -1;
            int master_ee_port = -1;
            try {
                sd_hostname = config.getString("securitydomain.host", "");
                sd_port = config.getInteger("securitydomain.httpsadminport", -1);
                master_hostname = config.getString("preop.master.hostname", "");
                master_port = config.getInteger("preop.master.httpsadminport", -1);
                master_ee_port = config.getInteger("preop.master.httpsport", -1);

                String content = "";
                if (cstype.equals("ca") || cstype.equals("kra")) {
                    content = "type=request&xmlOutput=true&sessionID=" + session_id;
                    CMS.debug("http content=" + content);
                    updateNumberRange(master_hostname, master_ee_port, true, content, "request", response);

                    content = "type=serialNo&xmlOutput=true&sessionID=" + session_id;
                    updateNumberRange(master_hostname, master_ee_port, true, content, "serialNo", response);

                    content = "type=replicaId&xmlOutput=true&sessionID=" + session_id;
                    updateNumberRange(master_hostname, master_ee_port, true, content, "replicaId", response);
                }

                String list = "";
                try {
                    list = config.getString("preop.cert.list", "");
                } catch (Exception e) {
                }

                StringBuffer c1 = new StringBuffer();
                StringBuffer s1 = new StringBuffer();
                StringTokenizer tok = new StringTokenizer(list, ",");
                while (tok.hasMoreTokens()) {
                    String t1 = tok.nextToken();
                    if (t1.equals("sslserver"))
                        continue;
                    c1.append(",");
                    c1.append("cloning.");
                    c1.append(t1);
                    c1.append(".nickname,");
                    c1.append("cloning.");
                    c1.append(t1);
                    c1.append(".dn,");
                    c1.append("cloning.");
                    c1.append(t1);
                    c1.append(".keytype,");
                    c1.append("cloning.");
                    c1.append(t1);
                    c1.append(".keyalgorithm,");
                    c1.append("cloning.");
                    c1.append(t1);
                    c1.append(".privkey.id,");
                    c1.append("cloning.");
                    c1.append(t1);
                    c1.append(".pubkey.exponent,");
                    c1.append("cloning.");
                    c1.append(t1);
                    c1.append(".pubkey.modulus,");
                    c1.append("cloning.");
                    c1.append(t1);
                    c1.append(".pubkey.encoded");

                    if (s1.length() != 0)
                        s1.append(",");

                    s1.append(cstype);
                    s1.append(".");
                    s1.append(t1);
                }

                if (!cstype.equals("ca")) {
                    c1.append(",cloning.ca.hostname,cloning.ca.httpport,cloning.ca.httpsport,cloning.ca.list,cloning.ca.pkcs7,cloning.ca.type");
                }

                if (cstype.equals("ca")) {
                    /* get ca connector details */
                    if (s1.length() != 0)
                        s1.append(",");
                    s1.append("ca.connector.KRA");
                }

                content =
                        "op=get&names=cloning.token,instanceId,internaldb.basedn,internaldb.ldapauth.password,internaldb.replication.password,internaldb.ldapconn.host,internaldb.ldapconn.port,internaldb.ldapauth.bindDN"
                                + c1.toString()
                                + "&substores="
                                + s1.toString()
                                + "&xmlOutput=true&sessionID="
                                + session_id;
                boolean success = updateConfigEntries(master_hostname, master_port, true,
                        "/" + cstype + "/admin/" + cstype + "/getConfigEntries", content, config, response);
                if (!success) {
                    context.put("errorString", "Failed to get configuration entries from the master");
                    throw new IOException("Failed to get configuration entries from the master");
                }
                config.putString("preop.clone.configuration", "true");
                try {
                    config.commit(false);
                } catch (Exception ee) {
                }
            } catch (IOException eee) {
                throw eee;
            } catch (Exception eee) {
                CMS.debug("RestoreKeyCertPanel: update exception caught:" + eee.toString());
            }

        } catch (IOException ee) {
            throw ee;
        } catch (Exception ee) {
        }
    }

    private void deleteExistingCerts() {
        IConfigStore cs = CMS.getConfigStore();
        try {
            String list = cs.getString("preop.cert.list", "");
            StringTokenizer st = new StringTokenizer(list, ",");
            while (st.hasMoreTokens()) {
                String s = st.nextToken();
                if (s.equals("sslserver"))
                    continue;
                String name = "preop.master." + s + ".nickname";
                String nickname = cs.getString(name, "");
                CryptoManager cm = CryptoManager.getInstance();
                X509Certificate xcert = null;
                try {
                    xcert = cm.findCertByNickname(nickname);
                } catch (Exception ee) {
                    CMS.debug("RestoreKeyCertPanel deleteExistingCerts: Exception=" + ee.toString());
                }
                CryptoToken ct = cm.getInternalKeyStorageToken();
                CryptoStore store = ct.getCryptoStore();
                try {
                    store.deleteCert(xcert);
                } catch (Exception ee) {
                    CMS.debug("RestoreKeyCertPanel deleteExistingCerts: Exception=" + ee.toString());
                }
            }
        } catch (Exception e) {
            CMS.debug("RestoreKeyCertPanel deleteExistingCerts: Exception=" + e.toString());
        }
    }

    private org.mozilla.jss.crypto.PrivateKey.Type getPrivateKeyType(PublicKey pubkey) {
        CMS.debug("Key Algorithm '" + pubkey.getAlgorithm() + "'");
        if (pubkey.getAlgorithm().equals("EC")) {
            return org.mozilla.jss.crypto.PrivateKey.Type.EC;
        }
        return org.mozilla.jss.crypto.PrivateKey.Type.RSA;
    }

    private void importkeycert(Vector pkeyinfo_collection,
            Vector cert_collection) throws IOException {
        CryptoManager cm = null;
        try {
            cm = CryptoManager.getInstance();
        } catch (Exception e) {
        }

        // delete all existing certificates first
        deleteExistingCerts();

        for (int i = 0; i < pkeyinfo_collection.size(); i++) {
            try {
                Vector pkeyinfo_v = (Vector) pkeyinfo_collection.elementAt(i);
                PrivateKeyInfo pkeyinfo = (PrivateKeyInfo) pkeyinfo_v.elementAt(0);
                String nickname = (String) pkeyinfo_v.elementAt(1);
                byte[] x509cert = getX509Cert(nickname, cert_collection);
                X509Certificate cert = cm.importCACertPackage(x509cert);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                pkeyinfo.encode(bos);
                byte[] pkey = bos.toByteArray();

                PublicKey publickey = cert.getPublicKey();
                CryptoToken token = cm.getInternalKeyStorageToken();
                CryptoStore store = token.getCryptoStore();
                CMS.debug("RestoreKeyCertPanel deleteCert: this is pk11store");
                try {
                    store.deleteCert(cert);
                } catch (Exception ee) {
                    CMS.debug("RestoreKeyCertPanel importKeyCert: Exception=" + ee.toString());
                }

                KeyGenerator kg = token.getKeyGenerator(KeyGenAlgorithm.DES3);
                SymmetricKey sk = kg.generate();
                byte iv[] = { 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1 };
                IVParameterSpec param = new IVParameterSpec(iv);
                Cipher c = token.getCipherContext(EncryptionAlgorithm.DES3_CBC_PAD);
                c.initEncrypt(sk, param);
                byte[] encpkey = c.doFinal(pkey);

                KeyWrapper wrapper = token.getKeyWrapper(KeyWrapAlgorithm.DES3_CBC_PAD);
                wrapper.initUnwrap(sk, param);
                org.mozilla.jss.crypto.PrivateKey pp =
                        wrapper.unwrapPrivate(encpkey, getPrivateKeyType(publickey), publickey);

            } catch (Exception e) {
                CMS.debug("RestoreKeyCertPanel importkeycert: Exception=" + e.toString());
            }
        }

        for (int i = 0; i < cert_collection.size(); i++) {
            try {
                Vector cert_v = (Vector) cert_collection.elementAt(i);
                byte[] cert = (byte[]) cert_v.elementAt(0);
                if (cert_v.size() > 1) {
                    String name = (String) cert_v.elementAt(1);
                    // we need to delete the trusted CA certificate if it is
                    // the same as the ca signing certificate
                    if (isCASigningCert(name)) {
                        X509Certificate certchain = getX509CertFromToken(cert);
                        if (certchain != null) {
                            CryptoToken token = cm.getInternalKeyStorageToken();
                            CryptoStore store = token.getCryptoStore();
                            CMS.debug("RestoreKeyCertPanel deleteCert: this is pk11store");
                            if (store instanceof PK11Store) {
                                try {
                                    PK11Store pk11store = (PK11Store) store;
                                    pk11store.deleteCertOnly(certchain);
                                } catch (Exception ee) {
                                    CMS.debug("RestoreKeyCertPanel importKeyCert: Exception=" + ee.toString());
                                }
                            }
                        }
                    }

                    X509Certificate xcert = cm.importUserCACertPackage(cert, name);
                    if (name.startsWith("caSigningCert")) {
                        // we need to change the trust attribute to CT
                        InternalCertificate icert = (InternalCertificate) xcert;
                        icert.setSSLTrust(InternalCertificate.TRUSTED_CA
                                | InternalCertificate.TRUSTED_CLIENT_CA
                                | InternalCertificate.VALID_CA);
                    } else if (name.startsWith("auditSigningCert")) {
                        InternalCertificate icert = (InternalCertificate) xcert;
                        icert.setObjectSigningTrust(InternalCertificate.USER
                                | InternalCertificate.VALID_PEER | InternalCertificate.TRUSTED_PEER);
                    }
                } else
                    cm.importCACertPackage(cert);
            } catch (Exception e) {
                CMS.debug("RestoreKeyCertPanel importkeycert: Exception=" + e.toString());
            }
        }
    }

    private boolean isCASigningCert(String name) {
        String n = "preop.master.signing.nickname";
        IConfigStore cs = CMS.getConfigStore();
        try {
            String nickname = cs.getString(n);
            if (nickname.equals(name))
                return true;
        } catch (Exception e) {
            return false;
        }

        return false;
    }

    private X509Certificate getX509CertFromToken(byte[] cert)
            throws IOException {
        try {
            X509CertImpl impl = new X509CertImpl(cert);
            String issuer_impl = impl.getIssuerDN().toString();
            BigInteger serial_impl = impl.getSerialNumber();
            CryptoManager cm = CryptoManager.getInstance();
            X509Certificate[] permcerts = cm.getPermCerts();
            for (int i = 0; i < permcerts.length; i++) {
                String issuer_p = permcerts[i].getSubjectDN().toString();
                BigInteger serial_p = permcerts[i].getSerialNumber();
                if (issuer_p.equals(issuer_impl) && serial_p.compareTo(serial_impl) == 0) {
                    return permcerts[i];
                }
            }
        } catch (Exception e) {
            CMS.debug("RestoreKeyCertPanel getX509CertFromToken: Exception=" + e.toString());
        }

        return null;
    }

    private byte[] getX509Cert(String nickname, Vector cert_collection)
            throws IOException {
        for (int i = 0; i < cert_collection.size(); i++) {
            Vector v = (Vector) cert_collection.elementAt(i);
            byte[] b = (byte[]) v.elementAt(0);
            X509CertImpl impl = null;
            try {
                impl = new X509CertImpl(b);
            } catch (Exception e) {
                CMS.debug("RestoreKeyCertPanel getX509Cert: Exception=" + e.toString());
                throw new IOException(e.toString());
            }
            Principal subjectdn = impl.getSubjectDN();
            if (LDAPDN.equals(subjectdn.toString(), nickname))
                return b;
        }

        return null;
    }

    /**
     * If validiate() returns false, this method will be called.
     */
    public void displayError(HttpServletRequest request,
            HttpServletResponse response,
            Context context) {
        context.put("title", "Import Keys and Certificates");
        context.put("password", "");
        context.put("path", "");
        context.put("panel", "admin/console/config/restorekeycertpanel.vm");
    }

    private boolean isCertdbCloned(HttpServletRequest request,
            Context context) {
        IConfigStore config = CMS.getConfigStore();
        String certList = "";
        try {
            CryptoManager cm = CryptoManager.getInstance();
            certList = config.getString("preop.cert.list");
            StringTokenizer st = new StringTokenizer(certList, ",");
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                if (token.equals("sslserver"))
                    continue;
                String tokenname = config.getString("preop.module.token", "");
                CryptoToken tok = cm.getTokenByName(tokenname);
                CryptoStore store = tok.getCryptoStore();
                String name1 = "preop.master." + token + ".nickname";
                String nickname = config.getString(name1, "");
                if (!tokenname.equals("Internal Key Storage Token") &&
                        !tokenname.equals("internal"))
                    nickname = tokenname + ":" + nickname;

                CMS.debug("RestoreKeyCertPanel isCertdbCloned: " + nickname);
                X509Certificate cert = cm.findCertByNickname(nickname);
                if (cert == null)
                    return false;
            }
        } catch (Exception e) {
            context.put("errorString", "Check your CS.cfg for cloning");
            return false;
        }

        return true;
    }
}