summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/profile/def/PolicyMappingsExtDefault.java
blob: d9ef59c1153d62ce03da0da1c6b767065a48f468 (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
// --- 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 PolicyMappingsExtDefault extends EnrollExtDefault {

    public static final String CONFIG_CRITICAL = "policyMappingsCritical";
    public static final String CONFIG_NUM_POLICY_MAPPINGS = "policyMappingsNum";
    public static final String CONFIG_ISSUER_DOMAIN_POLICY = "policyMappingsIssuerDomainPolicy_";
    public static final String CONFIG_SUBJECT_DOMAIN_POLICY = "policyMappingsSubjectDomainPolicy_";
    public static final String CONFIG_ENABLE = "policyMappingsEnable_";

    public static final String VAL_CRITICAL = "policyMappingsCritical";
    public static final String VAL_DOMAINS = "policyMappingsDomains";

    private static final String ISSUER_POLICY_ID = "Issuer Policy Id";
    private static final String SUBJECT_POLICY_ID = "Subject Policy Id";
    private static final String POLICY_ID_ENABLE = "Enable";

    private static final int DEF_NUM_MAPPINGS = 1;
    private static final int MAX_NUM_MAPPINGS = 100;

    public PolicyMappingsExtDefault() {
        super();
    }

    protected int getNumMappings() {
        int num = DEF_NUM_MAPPINGS;
        String numMappings = getConfig(CONFIG_NUM_POLICY_MAPPINGS);

        if (numMappings != null) {
            try {
                num = Integer.parseInt(numMappings);
            } 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_NUM_POLICY_MAPPINGS)) {
          try {
            num = Integer.parseInt(value);

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

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

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

    protected void refreshConfigAndValueNames() {
        super.refreshConfigAndValueNames();

        addValueName(VAL_CRITICAL);
        addValueName(VAL_DOMAINS);

        addConfigName(CONFIG_CRITICAL);
        int num = getNumMappings();

        addConfigName(CONFIG_NUM_POLICY_MAPPINGS);
        for (int i = 0; i < num; i++) {
            addConfigName(CONFIG_ISSUER_DOMAIN_POLICY + i);
            addConfigName(CONFIG_SUBJECT_DOMAIN_POLICY + i);
            addConfigName(CONFIG_ENABLE + i);
        }
    }

    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.startsWith(CONFIG_ISSUER_DOMAIN_POLICY)) {
            return new Descriptor(IDescriptor.STRING, null,
                    null,
                    CMS.getUserMessage(locale, "CMS_PROFILE_ISSUER_DOMAIN_POLICY"));
        } else if (name.startsWith(CONFIG_SUBJECT_DOMAIN_POLICY)) {
            return new Descriptor(IDescriptor.STRING, null,
                    null,
                    CMS.getUserMessage(locale, "CMS_PROFILE_SUBJECT_DOMAIN_POLICY"));
        } else if (name.startsWith(CONFIG_ENABLE)) {
            return new Descriptor(IDescriptor.BOOLEAN, null,
                    "false",
                    CMS.getUserMessage(locale, "CMS_PROFILE_ENABLE"));
        } else if (name.startsWith(CONFIG_NUM_POLICY_MAPPINGS)) {
            return new Descriptor(IDescriptor.INTEGER, null,
                   "1",
                   CMS.getUserMessage(locale, "CMS_PROFILE_NUM_POLICY_MAPPINGS"));
        }

        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_DOMAINS)) {
            return new Descriptor(IDescriptor.STRING_LIST, null,
                    null,
                    CMS.getUserMessage(locale, "CMS_PROFILE_DOMAINS"));
        }
        return null;
    }

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

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

            ext = (PolicyMappingsExtension)
                        getExtension(PKIXExtensions.PolicyMappings_Id.toString(),
                            info);

            if(ext == null)  {
                populate(null,info);

            }

            if (name.equals(VAL_CRITICAL)) {
                ext = (PolicyMappingsExtension)
                        getExtension(PKIXExtensions.PolicyMappings_Id.toString(), 
                            info);
                boolean val = Boolean.valueOf(value).booleanValue();

                if(ext == null)  {
                    return;
                }
                ext.setCritical(val); 
            } else if (name.equals(VAL_DOMAINS)) { 
                ext = (PolicyMappingsExtension)
                        getExtension(PKIXExtensions.PolicyMappings_Id.toString(), 
                            info);
               
                if(ext == null)  {
                    return;
                } 
                Vector v = parseRecords(value);
                int size = v.size();

                String issuerPolicyId = null;
                String subjectPolicyId = null;
                String enable = null;
                Vector policyMaps = new Vector();

                for (int i = 0; i < size; i++) {
                    NameValuePairs nvps = (NameValuePairs) v.elementAt(i);
                    Enumeration names = nvps.getNames();

                    while (names.hasMoreElements()) {
                        String name1 = (String) names.nextElement();

                        if (name1.equals(ISSUER_POLICY_ID)) {
                            issuerPolicyId = nvps.getValue(name1);
                        } else if (name1.equals(SUBJECT_POLICY_ID)) {
                            subjectPolicyId = nvps.getValue(name1);
                        } else if (name1.equals(POLICY_ID_ENABLE)) {
                            enable = nvps.getValue(name1);
                        }
                    }
                   
                    if (enable != null && enable.equals("true")) {
                        if (issuerPolicyId == null || 
                            issuerPolicyId.length() == 0 || subjectPolicyId == null ||
                            subjectPolicyId.length() == 0) 
                            throw new EPropertyException(CMS.getUserMessage( 
                                        locale, "CMS_PROFILE_POLICY_ID_NOT_FOUND"));
                        CertificatePolicyMap map = new CertificatePolicyMap(
                                new CertificatePolicyId(new ObjectIdentifier(issuerPolicyId)),
                                new CertificatePolicyId(new ObjectIdentifier(subjectPolicyId)));

                        policyMaps.addElement(map);
                    }
                }
                ext.set(PolicyMappingsExtension.MAP, policyMaps);
            } else {
                throw new EPropertyException(CMS.getUserMessage( 
                            locale, "CMS_INVALID_PROPERTY", name));
            }

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

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

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

        ext = (PolicyMappingsExtension)
                    getExtension(PKIXExtensions.PolicyMappings_Id.toString(),
                        info);
        if(ext == null)
        {
            try {
                populate(null,info);

            } catch (EProfileException e) {
                 throw new EPropertyException(CMS.getUserMessage(
                      locale, "CMS_INVALID_PROPERTY", name));
            }

        }

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

            if (ext == null) {
                return null;
            }
            if (ext.isCritical()) {
                return "true";
            } else {
                return "false";
            }
        } else if (name.equals(VAL_DOMAINS)) { 
            ext = (PolicyMappingsExtension)
                    getExtension(PKIXExtensions.PolicyMappings_Id.toString(), 
                        info);

            if (ext == null)
                return "";

            int num_mappings = getNumMappings();

            Enumeration maps = ext.getMappings();
         
            int num = 0;
            StringBuffer sb = new StringBuffer();

            Vector recs = new Vector();

            for (int i = 0; i < num_mappings; i++) {
                NameValuePairs pairs = new NameValuePairs();

                if (maps.hasMoreElements()) { 
                    CertificatePolicyMap map = 
                        (CertificatePolicyMap) maps.nextElement();
                
                    CertificatePolicyId i1 = map.getIssuerIdentifier();
                    CertificatePolicyId s1 = map.getSubjectIdentifier();               

                    pairs.add(ISSUER_POLICY_ID, i1.getIdentifier().toString());
                    pairs.add(SUBJECT_POLICY_ID, s1.getIdentifier().toString());
                    pairs.add(POLICY_ID_ENABLE, "true");
                } else {
                    pairs.add(ISSUER_POLICY_ID, "");
                    pairs.add(SUBJECT_POLICY_ID, "");
                    pairs.add(POLICY_ID_ENABLE, "false");
			
                }
                recs.addElement(pairs);
            }    
 
            return buildRecords(recs);
        } else {
            throw new EPropertyException(CMS.getUserMessage( 
                        locale, "CMS_INVALID_PROPERTY", name));
        }
    }

    public String getText(Locale locale) {
        StringBuffer sb = new StringBuffer();
        String numMappings = getConfig(CONFIG_NUM_POLICY_MAPPINGS);
        int num = getNumMappings();

        for (int i = 0; i < num; i++) {
            sb.append("Record #");
            sb.append(i);
            sb.append("{");
            sb.append(ISSUER_POLICY_ID + ":");
            sb.append(getConfig(CONFIG_ISSUER_DOMAIN_POLICY + i));
            sb.append(",");
            sb.append(SUBJECT_POLICY_ID + ":");
            sb.append(getConfig(CONFIG_SUBJECT_DOMAIN_POLICY + i));
            sb.append(",");
            sb.append(POLICY_ID_ENABLE + ":");
            sb.append(getConfig(CONFIG_ENABLE + i));
            sb.append("}");
        }
        return CMS.getUserMessage(locale, 
                "CMS_PROFILE_DEF_POLICY_MAPPINGS_EXT", 
                getConfig(CONFIG_CRITICAL), sb.toString());
    }

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

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

    public PolicyMappingsExtension createExtension() {
        PolicyMappingsExtension ext = null; 

        try {
            boolean critical = getConfigBoolean(CONFIG_CRITICAL);
            Vector policyMaps = new Vector();
            int num = getNumMappings();

            for (int i = 0; i < num; i++) { 
                String enable = getConfig(CONFIG_ENABLE + i);

                if (enable != null && enable.equals("true")) {
                    String issuerID = getConfig(CONFIG_ISSUER_DOMAIN_POLICY + i);

                    if (issuerID == null || issuerID.length() == 0) {
                        return null;
                    }

                    String subjectID = getConfig(CONFIG_SUBJECT_DOMAIN_POLICY + i);

                    if (subjectID == null || subjectID.length() == 0) {
                        return null;
                    }

                    CertificatePolicyMap map = new CertificatePolicyMap(
                            new CertificatePolicyId(new ObjectIdentifier(issuerID)),
                            new CertificatePolicyId(new ObjectIdentifier(subjectID)));

                    policyMaps.addElement(map);
                }
            }

            ext = new PolicyMappingsExtension(critical, policyMaps);
        } catch (Exception e) {
            CMS.debug("PolicyMappingsExtDefault: createExtension " + 
                e.toString());
        }

        return ext;
    }
}