summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/profile/def/CertificatePoliciesExtDefault.java
blob: 7cb5e72b1aa352551b42b34fa3d297f6f9f77075 (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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760

// --- 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.profile.def;


import java.io.*;
import java.security.cert.*;
import java.util.*;
import com.netscape.certsrv.base.*;
import com.netscape.certsrv.common.*;
import com.netscape.certsrv.profile.*;
import com.netscape.certsrv.request.*;
import com.netscape.certsrv.property.*;
import com.netscape.certsrv.apps.*;

import netscape.security.x509.*;
import netscape.security.extensions.*;
import netscape.security.util.*;
import com.netscape.cms.profile.common.*;

/**
 * This class implements an enrollment default policy
 * that populates a policy mappings extension
 * into the certificate template.
 *
 * @version $Revision$, $Date$
 */
public class CertificatePoliciesExtDefault extends EnrollExtDefault {

    public static final String CONFIG_CRITICAL = "Critical";
    public static final String CONFIG_PREFIX = "PoliciesExt.certPolicy";
    public static final String CONFIG_PREFIX1 = "PolicyQualifiers";
    public static final String CONFIG_POLICY_ENABLE = "enable";
    public static final String CONFIG_POLICY_NUM = "PoliciesExt.num";
    public static final String CONFIG_POLICY_ID = "policyId";
    public static final String CONFIG_POLICY_QUALIFIERS_NUM = "PolicyQualifiers.num";
    public static final String CONFIG_CPSURI_ENABLE = "CPSURI.enable";
    public static final String CONFIG_USERNOTICE_ENABLE = "usernotice.enable";
    public static final String CONFIG_CPSURI_VALUE = "CPSURI.value";
    public static final String CONFIG_USERNOTICE_ORG = "usernotice.noticeReference.organization";
    public static final String CONFIG_USERNOTICE_NUMBERS = "usernotice.noticeReference.noticeNumbers";
    public static final String CONFIG_USERNOTICE_TEXT = "usernotice.explicitText.value";

    public static final String VAL_CRITICAL = "Critical";
    public static final String VAL_POLICY_QUALIFIERS = "policyQualifiers";

    private static final String SEPARATOR = ".";
    private static final int DEF_NUM_POLICIES = 5;
    private static final int DEF_NUM_QUALIFIERS = 1;
    private static final int MAX_NUM_POLICIES = 20;
    private static final String POLICY_ID_ENABLE = "Enable";
    private static final String POLICY_ID = "Policy Id";
    private static final String POLICY_QUALIFIER_CPSURI_ENABLE = "CPSuri Enable";
    private static final String POLICY_QUALIFIER_USERNOTICE_ENABLE = "UserNotice Enable";
    private static final String USERNOTICE_REF_ORG = "UserNoticeReference Organization";
    private static final String USERNOTICE_REF_NUMBERS = "UserNoticeReference Numbers";
    private static final String USERNOTICE_EXPLICIT_TEXT = "UserNoticeReference Explicit Text";
    private static final String CPSURI = "CPS uri";

    public CertificatePoliciesExtDefault() {
        super();
    }

    protected int getNumPolicies() {
        int num = DEF_NUM_POLICIES;
        String numPolicies = getConfig(CONFIG_POLICY_NUM);

        if (numPolicies != null) {
            try {
                num = Integer.parseInt(numPolicies);
            } catch (NumberFormatException e) {
                // ignore
            }
        }

        if (num >= MAX_NUM_POLICIES)
            num = DEF_NUM_POLICIES;
        return num;
    }

    protected int getNumQualifiers() {
        int num = DEF_NUM_QUALIFIERS;
        String numQualifiers = getConfig(CONFIG_POLICY_QUALIFIERS_NUM);
        if (numQualifiers != null) {
            try {
                num = Integer.parseInt(numQualifiers);
            } catch (NumberFormatException e) {
                // ignore
            }
        }
        return num;
    }

    public void init(IProfile profile, IConfigStore config)
        throws EProfileException {
        super.init(profile, config);

        refreshConfigAndValueNames();
    }

    public void setConfig(String name, String value)
        throws EPropertyException {
        int num = 0;
        if (name.equals(CONFIG_POLICY_NUM)) {
          try {
            num = Integer.parseInt(value);

            if (num >= MAX_NUM_POLICIES || num < 0) {
                throw new EPropertyException(CMS.getUserMessage(
                            "CMS_INVALID_PROPERTY", CONFIG_POLICY_NUM));
            }

          } catch (Exception e) {
                throw new EPropertyException(CMS.getUserMessage(
                            "CMS_INVALID_PROPERTY", CONFIG_POLICY_NUM));
          }
        }
        super.setConfig(name, value);
    }


    public Enumeration getConfigNames() {
        refreshConfigAndValueNames();
        return super.getConfigNames();
    }

    protected void refreshConfigAndValueNames() {

        super.refreshConfigAndValueNames();

        addValueName(VAL_CRITICAL);
        addValueName(VAL_POLICY_QUALIFIERS);

        addConfigName(CONFIG_CRITICAL);
        int num = getNumPolicies();
        int numQualifiers = getNumQualifiers();

        addConfigName(CONFIG_POLICY_NUM);
        
        for (int i = 0; i < num; i++) {
            addConfigName(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_ID);
            addConfigName(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_ENABLE);
            for (int j=0; j<numQualifiers; j++) {
                addConfigName(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_CPSURI_ENABLE);
                addConfigName(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_USERNOTICE_ENABLE);
                addConfigName(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_CPSURI_VALUE);
                addConfigName(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_USERNOTICE_ORG);
                addConfigName(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_USERNOTICE_NUMBERS);
                addConfigName(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_USERNOTICE_TEXT);
            }
        }
    }

    public IDescriptor getConfigDescriptor(Locale locale, String name) { 

        if (name.equals(CONFIG_CRITICAL)) {
            return new Descriptor(IDescriptor.BOOLEAN, null,
                    "false",
                    CMS.getUserMessage(locale, "CMS_PROFILE_CRITICAL"));
        } else if (name.indexOf(CONFIG_POLICY_ID) >= 0) {
            return new Descriptor(IDescriptor.STRING, null,
              null,
              CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_ID"));
        } else if (name.indexOf(CONFIG_CPSURI_ENABLE) >= 0) {
            return new Descriptor(IDescriptor.BOOLEAN, null,
              "false",
              CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_QUALIFIER_CPSURI_ENABLE"));
        } else if (name.indexOf(CONFIG_USERNOTICE_ENABLE) >= 0) {
            return new Descriptor(IDescriptor.BOOLEAN, null,
              "false",
              CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_QUALIFIER_USERNOTICE_ENABLE"));
        } else if (name.indexOf(CONFIG_POLICY_ENABLE) >= 0) {
            return new Descriptor(IDescriptor.BOOLEAN, null,
                    "false",
                    CMS.getUserMessage(locale, "CMS_PROFILE_CERTIFICATE_POLICY_ENABLE"));
        } else if (name.indexOf(CONFIG_POLICY_QUALIFIERS_NUM) >= 0) {
            return new Descriptor(IDescriptor.INTEGER, null,
                    "1",
                    CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_QUALIFIER_NUM"));
        } else if (name.indexOf(CONFIG_USERNOTICE_ORG) >= 0) {
            return new Descriptor(IDescriptor.STRING, null,
                    null,
                    CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_USERNOTICE_REF_ORG"));
        } else if (name.indexOf(CONFIG_USERNOTICE_NUMBERS) >= 0) {
            return new Descriptor(IDescriptor.STRING, null,
                    null,
                    CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_USERNOTICE_REF_NUMBERS"));
        } else if (name.indexOf(CONFIG_USERNOTICE_TEXT) >= 0) {
            return new Descriptor(IDescriptor.STRING, null,
                    null,
                    CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_USERNOTICE_EXPLICIT_TEXT"));
        } else if (name.indexOf(CONFIG_CPSURI_VALUE) >= 0) {
            return new Descriptor(IDescriptor.STRING, null,
                    null,
                    CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_CPSURI"));
        } else if (name.indexOf(CONFIG_POLICY_NUM) >= 0) {
            return new Descriptor(IDescriptor.INTEGER, null,
                   "5",
                   CMS.getUserMessage(locale, "CMS_PROFILE_NUM_POLICIES"));
        }
        return null;
    }

    public IDescriptor getValueDescriptor(Locale locale, String name) {

        if (name.equals(VAL_CRITICAL)) {
            return new Descriptor(IDescriptor.BOOLEAN, null, 
                    "false",
                    CMS.getUserMessage(locale, "CMS_PROFILE_CRITICAL"));
        } else if (name.equals(VAL_POLICY_QUALIFIERS)) {
            return new Descriptor(IDescriptor.STRING_LIST, null,
                    null,
                    CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_QUALIFIERS"));
        }
        return null;
    }

    private Hashtable buildRecords(String value) throws EPropertyException {
        StringTokenizer st = new StringTokenizer(value, "\r\n");
        Hashtable table = new Hashtable();
        while (st.hasMoreTokens()) {
            String token = (String) st.nextToken();
            int index = token.indexOf(":");
            if (index <= 0)
                throw new EPropertyException(CMS.getUserMessage(
                  "CMS_INVALID_PROPERTY", token));
            String name = token.substring(0, index);
            String val = "";
            if ((token.length()-1) > index) {
                val = token.substring(index+1);
            }
            table.put(name, val);
        }        
    
        return table;
    }

    public void setValue(String name, Locale locale,
        X509CertInfo info, String value)
        throws EPropertyException {
        try {
            CertificatePoliciesExtension ext = null;

            if (name == null) {
                throw new EPropertyException(CMS.getUserMessage( 
                            locale, "CMS_INVALID_PROPERTY", name));
            }
            if (name.equals(VAL_CRITICAL)) {
                ext = (CertificatePoliciesExtension)
                        getExtension(PKIXExtensions.CertificatePolicies_Id.toString(), 
                            info);
                boolean val = Boolean.valueOf(value).booleanValue();

                ext.setCritical(val); 
            } else if (name.equals(VAL_POLICY_QUALIFIERS)) { 
                ext = (CertificatePoliciesExtension)
                        getExtension(PKIXExtensions.CertificatePolicies_Id.toString(), 
                            info);
                
                Hashtable h = buildRecords(value);

                String numStr = (String)h.get(CONFIG_POLICY_NUM);
                int size = Integer.parseInt(numStr);

                Vector certificatePolicies = new Vector();
                for (int i = 0; i < size; i++) {
                    String enable = (String)h.get(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_ENABLE);
                    CertificatePolicyInfo cinfo = null;
                    if (enable != null && enable.equals("true")) {
                        String policyId = (String)h.get(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_ID);

                        if (policyId == null || policyId.length() == 0) 
                            throw new EPropertyException(CMS.getUserMessage( 
                                        locale, "CMS_PROFILE_CERTIFICATE_POLICIES_EMPTY_POLICYID"));
                        CertificatePolicyId cpolicyId = getPolicyId(policyId);

                        String qualifersNum = (String)h.get(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_QUALIFIERS_NUM);
                        PolicyQualifiers policyQualifiers = new PolicyQualifiers();
                        int num = 0;
                        if (qualifersNum != null && qualifersNum.length() > 0)
                            num = Integer.parseInt(qualifersNum);
                        for (int j=0; j<num; j++) {
                            String cpsuriEnable = (String)h.get(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_CPSURI_ENABLE);
                            String usernoticeEnable = (String)h.get(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_USERNOTICE_ENABLE);
                            if (cpsuriEnable != null && cpsuriEnable.equals("true")) {
                                String cpsuri = (String)h.get(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_CPSURI_VALUE);
                                netscape.security.x509.PolicyQualifierInfo qualifierInfo = createCPSuri(cpsuri);
                                if (qualifierInfo != null)
                                  policyQualifiers.add(qualifierInfo);
                            } else if (usernoticeEnable != null && enable.equals("true")) {
                                String org = (String)h.get(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_USERNOTICE_ORG);
                                String noticenumbers = (String)h.get(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_USERNOTICE_NUMBERS);
                                String explicitText = (String)h.get(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_USERNOTICE_TEXT);
                                netscape.security.x509.PolicyQualifierInfo qualifierInfo = createUserNotice(org,
                                  noticenumbers, explicitText);
                                if (qualifierInfo != null)
                                  policyQualifiers.add(qualifierInfo);
                            }
                        }

                        if (policyQualifiers.size() <= 0) {
                            cinfo =
                              new CertificatePolicyInfo(cpolicyId);
                        } else {
                            cinfo =
                              new CertificatePolicyInfo(cpolicyId, policyQualifiers);
                        }
                        if (cinfo != null)
                          certificatePolicies.addElement(cinfo);
                    }
                }

                ext.set(CertificatePoliciesExtension.INFOS, certificatePolicies);
            } else {
                throw new EPropertyException(CMS.getUserMessage( 
                            locale, "CMS_INVALID_PROPERTY", name));
            }

            replaceExtension(PKIXExtensions.CertificatePolicies_Id.toString(),
                ext, info);
        } catch (EProfileException e) {
            CMS.debug("CertificatePoliciesExtDefault: setValue " + e.toString());
            throw new EPropertyException(CMS.getUserMessage( 
                        locale, "CMS_INVALID_PROPERTY", name));
        } catch (IOException e) {
            CMS.debug("CertificatePoliciesExtDefault: setValue " + e.toString());
            throw new EPropertyException(CMS.getUserMessage( 
                        locale, "CMS_INVALID_PROPERTY", name));
        }
    }

    public String getValue(String name, Locale locale,
        X509CertInfo info)
        throws EPropertyException {
        CertificatePoliciesExtension ext = null;

        if (name == null) {
            throw new EPropertyException(CMS.getUserMessage( 
                        locale, "CMS_INVALID_PROPERTY", name));
        }

        if (name.equals(VAL_CRITICAL)) {
            ext = (CertificatePoliciesExtension)
                    getExtension(PKIXExtensions.CertificatePolicies_Id.toString(), 
                        info);

            if (ext == null) {
                return null;
            }
            if (ext.isCritical()) {
                return "true";
            } else {
                return "false";
            }
        } else if (name.equals(VAL_POLICY_QUALIFIERS)) { 
            ext = (CertificatePoliciesExtension)
                    getExtension(PKIXExtensions.CertificatePolicies_Id.toString(), 
                        info);

            if (ext == null)
                return "";

            StringBuffer sb = new StringBuffer();
            int num_policies = getNumPolicies();
            int num_qualifiers = DEF_NUM_QUALIFIERS;
            sb.append(CONFIG_POLICY_NUM);
            sb.append(":");
            sb.append(num_policies);
            sb.append("\n");
            Vector infos = null;
            try {
                infos = (Vector)(ext.get(CertificatePoliciesExtension.INFOS)); 
            } catch (IOException ee) {
            }
            Enumeration policies = ext.getElements();

            for (int i = 0; i < num_policies; i++) {
                int qSize = 0;
                String policyId = "";
                String policyEnable = "false";
                PolicyQualifiers qualifiers = null;
                if (infos.size() > 0) { 
                    CertificatePolicyInfo cinfo = 
                        (CertificatePolicyInfo) infos.elementAt(0);
                
                    CertificatePolicyId id1 = cinfo.getPolicyIdentifier();               
                    policyId = id1.getIdentifier().toString();
                    policyEnable = "true";
                    qualifiers = cinfo.getPolicyQualifiers();
                    if (qualifiers != null)
                      qSize = qualifiers.size();
                    infos.removeElementAt(0);
                }
                sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_ENABLE);
                sb.append(":");
                sb.append(policyEnable);
                sb.append("\n");
                sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_ID);
                sb.append(":");
                sb.append(policyId);
                sb.append("\n");
          
                if (qSize == 0) {
                    sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_QUALIFIERS_NUM);
                    sb.append(":");
                    sb.append(DEF_NUM_QUALIFIERS);
                    sb.append("\n");
                } else {
                    sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_QUALIFIERS_NUM);
                    sb.append(":");
                    sb.append(qSize);
                    sb.append("\n");
                }
                if (qSize == 0) {
                    sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+"0"+SEPARATOR+CONFIG_CPSURI_ENABLE);
                    sb.append(":");
                    sb.append("false");
                    sb.append("\n");
                    sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+"0"+SEPARATOR+CONFIG_CPSURI_VALUE);
                    sb.append(":");
                    sb.append("");
                    sb.append("\n");
                    sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+"0"+SEPARATOR+CONFIG_USERNOTICE_ENABLE);
                    sb.append(":");
                    sb.append("false");
                    sb.append("\n");
                    sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+"0"+SEPARATOR+CONFIG_USERNOTICE_ORG);
                    sb.append(":");
                    sb.append("");
                    sb.append("\n");
                    sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+"0"+SEPARATOR+CONFIG_USERNOTICE_NUMBERS);
                    sb.append(":");
                    sb.append("");
                    sb.append("\n");
                    sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+"0"+SEPARATOR+CONFIG_USERNOTICE_TEXT);
                    sb.append(":");
                    sb.append("");
                    sb.append("\n");
                }

                for (int j=0; j<qSize; j++) {
                    netscape.security.x509.PolicyQualifierInfo qinfo = qualifiers.getInfoAt(j);
                    ObjectIdentifier oid = qinfo.getId();
                    Qualifier qualifier = qinfo.getQualifier();
                        
                    String cpsuriEnable = "false";
                    String usernoticeEnable = "false";
                    String cpsuri = "";
                    String org = "";
                    StringBuffer noticeNum = new StringBuffer();
                    String explicitText = "";

                    if (oid.toString().equals(netscape.security.x509.PolicyQualifierInfo.QT_CPS.toString())) {
                        cpsuriEnable = "true";
                        CPSuri content = (CPSuri)qualifier;
                        cpsuri = content.getURI(); 
                    } else if (oid.toString().equals(netscape.security.x509.PolicyQualifierInfo.QT_UNOTICE.toString())) {
                        usernoticeEnable = "true";
                        UserNotice content = (UserNotice)qualifier;
                        NoticeReference ref = content.getNoticeReference();
                        if (ref != null) {
                            org = ref.getOrganization().getText();
                            int[] nums = ref.getNumbers();
                            for (int k=0; k<nums.length; k++) {
                                if (k != 0) {
                                    noticeNum.append(",");
                                    noticeNum.append(nums[k]);
                                } else
                                    noticeNum.append(nums[k]);
                            }
                        }
                        DisplayText displayText = content.getDisplayText();
                        if (displayText != null)
                            explicitText = displayText.getText();
                    }

                    sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_CPSURI_ENABLE);
                    sb.append(":");
                    sb.append(cpsuriEnable);
                    sb.append("\n");
                    sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_CPSURI_VALUE);
                    sb.append(":");
                    sb.append(cpsuri);
                    sb.append("\n");
                    sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_USERNOTICE_ENABLE);
                    sb.append(":");
                    sb.append(usernoticeEnable);
                    sb.append("\n");
                    sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_USERNOTICE_ORG);
                    sb.append(":");
                    sb.append(org);
                    sb.append("\n");
                    sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_USERNOTICE_NUMBERS);
                    sb.append(":");
                    sb.append(noticeNum.toString());
                    sb.append("\n");
                    sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+j+SEPARATOR+CONFIG_USERNOTICE_TEXT);
                    sb.append(":");
                    sb.append(explicitText);
                    sb.append("\n");
                }
            } // end of for loop
            return sb.toString();
        } else {
            throw new EPropertyException(CMS.getUserMessage( 
                        locale, "CMS_INVALID_PROPERTY", name));
        }
    }

    public String getText(Locale locale) {
        StringBuffer sb = new StringBuffer();
        String numPolicies = getConfig(CONFIG_POLICY_NUM);
        int num = getNumPolicies();
        int num1 = getNumQualifiers();

        try {
            IConfigStore basesubstore = getConfigStore().getSubStore("params");
            sb.append("{");
            sb.append(CONFIG_POLICY_NUM + ":");
            sb.append(num);
            sb.append(",");
            for (int i = 0; i < num; i++) {
                sb.append("{");
                IConfigStore substore = basesubstore.getSubStore(CONFIG_PREFIX+i);
                String enable = substore.getString(CONFIG_POLICY_ENABLE, "");
                sb.append(POLICY_ID_ENABLE + ":");
                sb.append(enable);
                sb.append(",");
                String policyId = substore.getString(CONFIG_POLICY_ID, "");
                sb.append(POLICY_ID + ":");
                sb.append(policyId);
                sb.append(",");
                String qualifiersNum = substore.getString(CONFIG_POLICY_QUALIFIERS_NUM, "");
                sb.append(CONFIG_POLICY_QUALIFIERS_NUM+":");
                sb.append(qualifiersNum);
                sb.append(",");
                for (int j=0; j<num1; j++) {
                    IConfigStore substore1 = substore.getSubStore(CONFIG_PREFIX1+j);
                    sb.append("{");
                    String cpsuriEnable = substore1.getString(CONFIG_CPSURI_ENABLE, "");
                    sb.append(POLICY_QUALIFIER_CPSURI_ENABLE + ":");
                    sb.append(cpsuriEnable);
                    sb.append(",");
                    String usernoticeEnable = substore1.getString(CONFIG_USERNOTICE_ENABLE, "");
                    sb.append(POLICY_QUALIFIER_USERNOTICE_ENABLE+ ":");
                    sb.append(usernoticeEnable);
                    sb.append(",");
                    String org = substore1.getString(CONFIG_USERNOTICE_ORG, "");
                    sb.append(USERNOTICE_REF_ORG + ":");
                    sb.append(org);
                    sb.append(",");
                    String refNums = substore1.getString(CONFIG_USERNOTICE_NUMBERS, "");
                    sb.append(USERNOTICE_REF_NUMBERS + ":");
                    sb.append(refNums);
                    sb.append(",");
                    String explicitText = substore1.getString(CONFIG_USERNOTICE_TEXT, "");
                    sb.append(USERNOTICE_EXPLICIT_TEXT + ":");
                    sb.append(explicitText);
                    sb.append(",");
                    String cpsuri = substore1.getString(CONFIG_CPSURI_VALUE, "");
                    sb.append(CPSURI + ":");
                    sb.append(cpsuri);
                    sb.append("}");
                }
                sb.append("}");
            }
            sb.append("}");
            return CMS.getUserMessage(locale, 
                "CMS_PROFILE_DEF_CERTIFICATE_POLICIES_EXT", 
                getConfig(CONFIG_CRITICAL), sb.toString());
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * Populates the request with this policy default.
     */
    public void populate(IRequest request, X509CertInfo info)
        throws EProfileException {
        CertificatePoliciesExtension ext = createExtension();

        if (ext == null)
            return;
        addExtension(PKIXExtensions.CertificatePolicies_Id.toString(), 
            ext, info);
    }

    public CertificatePoliciesExtension createExtension() 
      throws EProfileException {
        CertificatePoliciesExtension ext = null; 

        try {
            boolean critical = getConfigBoolean(CONFIG_CRITICAL);
            Vector certificatePolicies = new Vector();
            int num = getNumPolicies();
            CMS.debug("CertificatePoliciesExtension: createExtension: number of policies="+num);
            IConfigStore config = getConfigStore();

            for (int i = 0; i < num; i++) { 
                IConfigStore basesubstore = config.getSubStore("params");
                IConfigStore substore = basesubstore.getSubStore(CONFIG_PREFIX+i);
                String enable = substore.getString(CONFIG_POLICY_ENABLE);

                CMS.debug("CertificatePoliciesExtension: createExtension: CertificatePolicy "+i+" enable="+enable);
                if (enable != null && enable.equals("true")) {
                    String policyId = substore.getString(CONFIG_POLICY_ID);
                    CertificatePolicyId cpolicyId = getPolicyId(policyId);
                    CMS.debug("CertificatePoliciesExtension: createExtension: CertificatePolicy "+i+" policyId="+policyId);
                    int qualifierNum = getNumQualifiers();
                    PolicyQualifiers policyQualifiers = new PolicyQualifiers();
                    for (int j=0; j<qualifierNum; j++) {
                        IConfigStore substore1 = substore.getSubStore(CONFIG_PREFIX1+j);
                        String cpsuriEnable = substore1.getString(CONFIG_CPSURI_ENABLE);
                        String usernoticeEnable = substore1.getString(CONFIG_USERNOTICE_ENABLE);

                        if (cpsuriEnable != null && cpsuriEnable.equals("true")) {
                            String cpsuri = substore1.getString(CONFIG_CPSURI_VALUE, "");
                            netscape.security.x509.PolicyQualifierInfo qualifierInfo = createCPSuri(cpsuri); 
                            if (qualifierInfo != null)
                              policyQualifiers.add(qualifierInfo);
                        } else if (usernoticeEnable != null && 
                                     usernoticeEnable.equals("true")) {

                            String org = substore1.getString(CONFIG_USERNOTICE_ORG);
                            String noticenumbers = substore1.getString(CONFIG_USERNOTICE_NUMBERS);
                            String explicitText = substore1.getString(CONFIG_USERNOTICE_TEXT);
                            netscape.security.x509.PolicyQualifierInfo qualifierInfo = createUserNotice(org,
                              noticenumbers, explicitText);
                            if (qualifierInfo != null)
                                policyQualifiers.add(qualifierInfo);
                        }
                    }
                
                    CertificatePolicyInfo info = null;
                    if (policyQualifiers.size() <= 0) {
                        info = 
                          new CertificatePolicyInfo(cpolicyId);
                    } else {
                        info = 
                          new CertificatePolicyInfo(cpolicyId, policyQualifiers);
                    }
             
                    if (info != null)
                      certificatePolicies.addElement(info);
                }
            }

            ext = new CertificatePoliciesExtension(critical, certificatePolicies);
        } catch (EPropertyException e) {
            throw new EProfileException(e.toString());
        } catch (EProfileException e) {
            throw e;
        } catch (Exception e) {
            CMS.debug("CertificatePoliciesExtDefault: createExtension " + 
                e.toString());
        }

        return ext;
    }

    private CertificatePolicyId getPolicyId (String policyId) throws EPropertyException {
        if (policyId == null || policyId.length() == 0)
            throw new EPropertyException(CMS.getUserMessage(
              "CMS_PROFILE_CERTIFICATE_POLICIES_EMPTY_POLICYID"));

        CertificatePolicyId cpolicyId = null;
        try {
            cpolicyId = new CertificatePolicyId(
              ObjectIdentifier.getObjectIdentifier(policyId));
            return cpolicyId;
        } catch (Exception e) {
            throw new EPropertyException(CMS.getUserMessage(
              "CMS_PROFILE_CERTIFICATE_POLICIES_POLICYID_ERROR", policyId));
        }
    }

    private netscape.security.x509.PolicyQualifierInfo createCPSuri(String uri) throws EPropertyException {
        if (uri == null || uri.length() == 0) 
            throw new EPropertyException(CMS.getUserMessage(
              "CMS_PROFILE_CERTIFICATE_POLICIES_EMPTY_CPSURI"));

        CPSuri cpsURI = new CPSuri(uri); 
        netscape.security.x509.PolicyQualifierInfo policyQualifierInfo2 =
          new netscape.security.x509.PolicyQualifierInfo(netscape.security.x509.PolicyQualifierInfo.QT_CPS, cpsURI);
 
        return policyQualifierInfo2;
    }

    private netscape.security.x509.PolicyQualifierInfo createUserNotice(String organization, 
      String noticeText, String noticeNums) throws EPropertyException {
   
        if ((organization == null || organization.length() == 0) && 
          (noticeNums == null || noticeNums.length() == 0) &&
          (noticeText == null || noticeText.length() == 0))
            return null;

        DisplayText explicitText = null;
        if (noticeText != null && noticeText.length() > 0) 
            explicitText = new DisplayText(DisplayText.tag_VisibleString, noticeText);

        int nums[] = null;
        if (noticeNums != null && noticeNums.length() > 0) {
            Vector numsVector = new Vector();
            StringTokenizer tokens = new StringTokenizer(noticeNums, ";");
            while (tokens.hasMoreTokens()) {
                String num = tokens.nextToken().trim();
                numsVector.addElement(num);
            }

            nums = new int[numsVector.size()];
            try {
                for (int i = 0; i < numsVector.size(); i++) {
                    Integer ii = new Integer((String) numsVector.elementAt(i));
                    nums[i] = ii.intValue();
                }
            } catch (Exception e) {
                throw new EPropertyException("Wrong notice numbers");
            }
        }

        DisplayText orgName = null;
        if (organization != null && organization.length() > 0) {
            orgName =
              new DisplayText(DisplayText.tag_VisibleString, organization);
        }

        NoticeReference noticeReference = null;

        if (orgName != null)
            noticeReference = new NoticeReference(orgName, nums);

        UserNotice userNotice = null;
        if (explicitText != null || noticeReference != null) {
            userNotice = new UserNotice (noticeReference, explicitText);

            netscape.security.x509.PolicyQualifierInfo policyQualifierInfo1 =
              new netscape.security.x509.PolicyQualifierInfo(netscape.security.x509.PolicyQualifierInfo.QT_UNOTICE, userNotice);
            return policyQualifierInfo1;
        }

        return null;
    }
}