summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/profile/def/SubjectDirAttributesExtDefault.java
blob: afd3fcd0a1c84693eddbc335746ec2bb147db6e6 (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
// --- 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 subject directory attributes extension
 * into the certificate template.
 *
 * @version $Revision$, $Date$
 */
public class SubjectDirAttributesExtDefault extends EnrollExtDefault {

    public static final String CONFIG_CRITICAL = "subjDirAttrsCritical";
    public static final String CONFIG_NUM_ATTRS = "subjDirAttrsNum";
    public static final String CONFIG_ATTR_NAME = "subjDirAttrName_";
    public static final String CONFIG_PATTERN = "subjDirAttrPattern_";
    public static final String CONFIG_ENABLE = "subjDirAttrEnable_";

    public static final String VAL_CRITICAL = "subjDirAttrCritical";
    public static final String VAL_ATTR = "subjDirAttrValue";

    private static final int DEF_NUM_ATTRS = 1;
    private static final int MAX_NUM_ATTRS = 100;
    private static final String ENABLE = "Enable";
    private static final String ATTR_NAME = "Attribute Name";
    private static final String ATTR_VALUE = "Attribute Value";

    public SubjectDirAttributesExtDefault() {
        super();
    }

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

    protected int getNumAttrs() {
        int num = DEF_NUM_ATTRS;
        String val = getConfig(CONFIG_NUM_ATTRS);

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

        if (num >= MAX_NUM_ATTRS)
            num = DEF_NUM_ATTRS;

        return num;
    }

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

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

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


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

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

        addValueName(VAL_CRITICAL);
        addValueName(VAL_ATTR);

        addConfigName(CONFIG_CRITICAL);
        int num = getNumAttrs();
        addConfigName(CONFIG_NUM_ATTRS);
        for (int i = 0; i < num; i++) {
            addConfigName(CONFIG_ATTR_NAME + i);
            addConfigName(CONFIG_PATTERN + 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_ATTR_NAME)) {
            return new Descriptor(IDescriptor.STRING, null, 
                    null,
                    CMS.getUserMessage(locale, "CMS_PROFILE_NUM_ATTRS"));
        } else if (name.startsWith(CONFIG_ATTR_NAME)) {
            return new Descriptor(IDescriptor.STRING, null, 
                    null,
                    CMS.getUserMessage(locale, "CMS_PROFILE_ATTR_NAME"));
        } else if (name.startsWith(CONFIG_PATTERN)) {
            return new Descriptor(IDescriptor.STRING, null, 
                    null,
                    CMS.getUserMessage(locale, "CMS_PROFILE_ATTR_VALUE"));
        } else if (name.startsWith(CONFIG_ENABLE)) {
            return new Descriptor(IDescriptor.BOOLEAN, null, 
                    null,
                    CMS.getUserMessage(locale, "CMS_PROFILE_ENABLE"));
        } else if (name.startsWith(CONFIG_NUM_ATTRS)) {
            return new Descriptor(IDescriptor.INTEGER, null,
                    "1",
                    CMS.getUserMessage(locale, "CMS_PROFILE_NUM_ATTRS")); 
        }  

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

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

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

            ext = (SubjectDirAttributesExtension)
              getExtension(PKIXExtensions.SubjectDirectoryAttributes_Id.toString(),
              info);

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

                if(ext == null)
                {
                    return;
                }
                ext.setCritical(val); 
            } else if (name.equals(VAL_ATTR)) { 
                ext = (SubjectDirAttributesExtension)
                  getExtension(PKIXExtensions.SubjectDirectoryAttributes_Id.toString(),
                  info);

                if(ext == null)
                {
                    return;
                }
                Vector v = parseRecords(value);
                int size = v.size();
              
                boolean critical = ext.isCritical();

                X500NameAttrMap map = X500NameAttrMap.getDefault();
                Vector attrV = new Vector();
                for (int i=0; i < size; i++) {
                    NameValuePairs nvps = (NameValuePairs) v.elementAt(i);
                    Enumeration names = nvps.getNames();
                    String attrName = null;
                    String attrValue = null;
                    String enable = "false";
                    while (names.hasMoreElements()) {
                        String name1 = (String) names.nextElement();

                        if (name1.equals(ATTR_NAME)) {
                            attrName = nvps.getValue(name1);
                        } else if (name1.equals(ATTR_VALUE)) {
                            attrValue = nvps.getValue(name1);
                        } else if (name1.equals(ENABLE)) {
                            enable = nvps.getValue(name1);
                        }
                    }

                    if (enable.equals("true")) {
                        AttributeConfig attributeConfig =  
                          new AttributeConfig(attrName, attrValue); 
                        Attribute attr = attributeConfig.mAttribute;
                        if (attr != null)
                            attrV.addElement(attr);
                    }
                }

                if (attrV.size() > 0) {
                    Attribute[] attrList = new Attribute[attrV.size()];
                    attrV.copyInto(attrList);
                    ext = new SubjectDirAttributesExtension(attrList, critical);
                } else
                    return;
            } else {
                throw new EPropertyException(CMS.getUserMessage( 
                            locale, "CMS_INVALID_PROPERTY", name));
            }

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

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

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

        ext = (SubjectDirAttributesExtension)
          getExtension(PKIXExtensions.SubjectDirectoryAttributes_Id.toString(),
          info);

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

            if (ext == null) {
                return null;
            }
            if (ext.isCritical()) {
                return "true";
            } else {
                return "false";
            }
        } else if (name.equals(VAL_ATTR)) { 
            ext = (SubjectDirAttributesExtension)
              getExtension(PKIXExtensions.SubjectDirectoryAttributes_Id.toString(), 
              info);

            if (ext == null)
                return "";

            X500NameAttrMap map = X500NameAttrMap.getDefault();

            Vector recs = new Vector();
            int num = getNumAttrs();
            Enumeration e = ext.getAttributesList();
            CMS.debug("SubjectDirAttributesExtDefault: getValue: attributesList="+e);
            int i=0;

            while (e.hasMoreElements()) {
                NameValuePairs pairs = new NameValuePairs();
                pairs.add(ENABLE, "true");
                Attribute attr = (Attribute)(e.nextElement());
                CMS.debug("SubjectDirAttributesExtDefault: getValue: attribute="+attr);
                ObjectIdentifier oid = attr.getOid();
                CMS.debug("SubjectDirAttributesExtDefault: getValue: oid="+oid);
   
                String vv = map.getName(oid);

                if (vv != null) 
                    pairs.add(ATTR_NAME, vv);
                else
                    pairs.add(ATTR_NAME, oid.toString());
                Enumeration v = attr.getValues();
                
                // just support single value for now
                StringBuffer ss = new StringBuffer();
                while (v.hasMoreElements()) {
                    if (ss.length() == 0)
                        ss.append((String)(v.nextElement()));
                    else {
                        ss.append(",");
                        ss.append((String)(v.nextElement()));
                    }
                }

                pairs .add(ATTR_VALUE, ss.toString());
                recs.addElement(pairs);
                i++;
            }
                
            for (;i < num; i++) {
                NameValuePairs pairs = new NameValuePairs();
                pairs.add(ENABLE, "false");
                pairs.add(ATTR_NAME, "GENERATIONQUALIFIER");
                pairs.add(ATTR_VALUE, "");
                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();
        int num = getNumAttrs();

        for (int i = 0; i < num; i++) {
            sb.append("Record #");
            sb.append(i);
            sb.append("{");
            sb.append(ATTR_NAME + ":");
            sb.append(getConfig(CONFIG_ATTR_NAME + i));
            sb.append(",");
            sb.append(ATTR_VALUE + ":");
            sb.append(getConfig(CONFIG_PATTERN + i));
            sb.append(",");
            sb.append(ENABLE + ":");
            sb.append(getConfig(CONFIG_ENABLE + i));
            sb.append("}");
        }
        return CMS.getUserMessage(locale, 
                "CMS_PROFILE_DEF_SUBJECT_DIR_ATTR_EXT", 
                getConfig(CONFIG_CRITICAL),
                sb.toString());
    }

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

        if (ext == null)
            return;

        addExtension(PKIXExtensions.SubjectDirectoryAttributes_Id.toString(), 
            ext, info);
    }

    public SubjectDirAttributesExtension createExtension(IRequest request)
      throws EProfileException {
        SubjectDirAttributesExtension ext = null; 
        int num = 0;

        boolean critical = getConfigBoolean(CONFIG_CRITICAL);

        num = getNumAttrs();
         
        AttributeConfig attributeConfig = null;
        Vector attrs = new Vector();
        for (int i = 0; i < num; i++) {
            String enable = getConfig(CONFIG_ENABLE + i); 
            if (enable != null && enable.equals("true")) {
                String attrName = getConfig(CONFIG_ATTR_NAME + i);
                String pattern = getConfig(CONFIG_PATTERN + i); 
                if (pattern == null || pattern.equals(""))
                    pattern = " ";

                //check pattern syntax
                int startpos = pattern.indexOf("$");
                int lastpos = pattern.lastIndexOf("$");
                String attrValue = pattern;
                if (!pattern.equals("") && startpos != -1 &&
                  startpos == 0 && lastpos != -1 &&
                  lastpos == (pattern.length()-1)) {
                    if (request != null) {
                        try {
                            attrValue = mapPattern(request, pattern);
                        } catch (IOException e) {
                            throw new EProfileException(e.toString());
                        }
                    }
                } 
                try {
                    attributeConfig = new AttributeConfig(attrName, attrValue);
                } catch (EPropertyException e) {
                    throw new EProfileException(e.toString());
                }
                Attribute attr = attributeConfig.mAttribute;
                if (attr != null) {
                    attrs.addElement(attr);
                }
            }
        }

        if (attrs.size() > 0) {
            Attribute[] attrList = new Attribute[attrs.size()];
            attrs.copyInto(attrList);
            try {
                ext =
                  new SubjectDirAttributesExtension(attrList, critical);
            } catch (IOException e) {
                throw new EProfileException(e.toString());
            }
        }

        return ext;
    }
}

class AttributeConfig {

    protected ObjectIdentifier mAttributeOID = null;
    protected Attribute mAttribute = null;

    public AttributeConfig(String attrName, String attrValue)
      throws EPropertyException {
        X500NameAttrMap map = X500NameAttrMap.getDefault();
     
        if (attrName == null || attrName.length() == 0) {
            throw new EPropertyException(
              CMS.getUserMessage("CMS_PROFILE_SUBJDIR_EMPTY_ATTRNAME", attrName));
        }
 
        if (attrValue == null || attrValue.length() == 0) {
            throw new EPropertyException(
              CMS.getUserMessage("CMS_PROFILE_SUBJDIR_EMPTY_ATTRVAL", attrValue));
        }

        try {
            mAttributeOID = new ObjectIdentifier(attrName);
        } catch (Exception e) {
            CMS.debug("SubjectDirAttributesExtDefault: invalid OID syntax: "+ attrName);
        }

        if (mAttributeOID == null) {
            mAttributeOID = map.getOid(attrName);
            if (mAttributeOID == null)
                throw new EPropertyException(
                  CMS.getUserMessage("CMS_BASE_INVALID_ATTRIBUTE", attrName));
            try {
                checkValue(mAttributeOID, attrValue);
            } catch (IOException e) {
                throw new EPropertyException(CMS.getUserMessage(
                  "CMS_BASE_INVALID_ATTR_VALUE", e.getMessage()));
            }
        }


        try {
            mAttribute = new Attribute(mAttributeOID, 
              str2MultiValues(attrValue));
        } catch (IOException e) {
            throw new EPropertyException(CMS.getUserMessage(
              "CMS_BASE_INVALID_ATTR_VALUE", e.getMessage()));
        }
    }

    private static void checkValue(ObjectIdentifier oid, String val) 
      throws IOException {
        AVAValueConverter c = X500NameAttrMap.getDefault().getValueConverter(oid);
        DerValue derval;

        derval = c.getValue(val); // errs encountered will get thrown.
        return;
    }

    private Vector str2MultiValues(String attrValue) {
        StringTokenizer tokenizer = new StringTokenizer(attrValue, ",");
        Vector v = new Vector();
        while (tokenizer.hasMoreTokens()) {
            v.addElement(tokenizer.nextToken());
        }
 
        return v;
    }
}