summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/base/ArgBlock.java
blob: 2e0d19201759ae2090a337f29068f194887bf709 (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
// --- 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.cmscore.base;


import java.util.*;
import java.io.*;
import netscape.security.pkcs.*;
import java.security.*;
import java.math.BigInteger;
import com.netscape.certsrv.base.*;
import com.netscape.certsrv.apps.CMS;


/**
 * This class represents a set of indexed arguments. 
 * Each argument is indexed by a key, which can be 
 * used during the argument retrieval.
 *
 * @version $Revision$, $Date$
 */
public class ArgBlock implements IArgBlock {

    /*==========================================================
     * variables
     *==========================================================*/
    public static final String 
        CERT_NEW_REQUEST_HEADER = "-----BEGIN NEW CERTIFICATE REQUEST-----";
    public static final String 
        CERT_NEW_REQUEST_TRAILER = "-----END NEW CERTIFICATE REQUEST-----";
    public static final String 
        CERT_REQUEST_HEADER = "-----BEGIN CERTIFICATE REQUEST-----";
    public static final String 
        CERT_REQUEST_TRAILER = "-----END CERTIFICATE REQUEST-----";
    public static final String 
        CERT_RENEWAL_HEADER = "-----BEGIN RENEWAL CERTIFICATE REQUEST-----";
    public static final String 
        CERT_RENEWAL_TRAILER = "-----END RENEWAL CERTIFICATE REQUEST-----";

    private Hashtable mArgs = new Hashtable();

	private String mType = "unspecified-argblock";

    /*==========================================================
     * constructors
     *==========================================================*/
    /**
     * Constructs an argument block with the given hashtable values.
     * @param realm the type of argblock - used for debugging the values
     */
    public ArgBlock(String realm, Hashtable httpReq) {
		mType = realm;
		populate(httpReq);
	}
    
    /**
     * Constructs an argument block with the given hashtable values.
     *
     * @param httpReq hashtable keys and values
     */
    public ArgBlock(Hashtable httpReq) {
		populate(httpReq);
	}

	private void populate(Hashtable httpReq) {
        // Add all parameters from the request
        Enumeration e = httpReq.keys();

        if (e != null) {
            while (e.hasMoreElements()) {
                String name = (String) e.nextElement();
                String value = (String) httpReq.get(name);

                addStringValue(name, value);
            }
        }
    }

    /**
     * Constructs an empty argument block.
     */
    public ArgBlock() {
    }

    /*==========================================================
     * public methods
     *==========================================================*/

    /**
     * Checks if this argument block contains the given key.
     *
     * @param n key
     * @return true if key is present
     */
    public boolean isValuePresent(String n) {
		CMS.traceHashKey(mType, n);
        if (mArgs.get(n) != null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Adds string-based value into this argument block.
     *
     * @param n key
     * @param v value
     * @return value
     */
    public Object addStringValue(String n, String v) {
        if (v == null) {
            return mArgs.put(n, Character.valueOf((char) 0));
        } else {
            return mArgs.put(n, v);
        }
    }

    /**
     * Retrieves argument value as string.
     *
     * @param n key
     * @return argument value as string
     * @exception EBaseException failed to retrieve value
     */
    public String getValueAsString(String n) throws EBaseException {
		String t= (String)mArgs.get(n);
		CMS.traceHashKey(mType, n, t);

        if (t != null) {
            return t;
        } else {
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_ATTRIBUTE_NOT_FOUND", n));
        }
    }

    /**
     * Retrieves argument value as string.
     *
     * @param n key
     * @param def default value to be returned if key is not present
     * @return argument value as string
     */
    public String getValueAsString(String n, String def) {
        String val = (String) mArgs.get(n);
		CMS.traceHashKey(mType, n, val, def);

        if (val != null) {
            return val;
        } else {
            return def;
        }
    }

    /**
     * Retrieves argument value as integer.
     *
     * @param n key
     * @return argument value as int
     * @exception EBaseException failed to retrieve value
     */
    public int getValueAsInt(String n) throws EBaseException {
        if (mArgs.get(n) != null) {
			CMS.traceHashKey(mType, n, (String)mArgs.get(n));
            try {
                return new Integer((String) mArgs.get(n)).intValue();
            } catch (NumberFormatException e) {
                throw new EBaseException(
                        CMS.getUserMessage("CMS_BASE_INVALID_ATTR_TYPE", n, e.toString()));
            }
        } else {
			CMS.traceHashKey(mType, n, "<notpresent>");
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_ATTRIBUTE_NOT_FOUND", n));
        }
    }

    /**
     * Retrieves argument value as integer.
     *
     * @param n key
     * @param def default value to be returned if key is not present
     * @return argument value as int
     */
    public int getValueAsInt(String n, int def) {
		CMS.traceHashKey(mType, n, (String)mArgs.get(n), ""+def);
        if (mArgs.get(n) != null) {
            try {
                return new Integer((String) mArgs.get(n)).intValue();
            } catch (NumberFormatException e) {
                return def;
            }
        } else {
            return def;
        }
    }

    /**
     * Retrieves argument value as big integer.
     *
     * @param n key
     * @return argument value as big integer
     * @exception EBaseException failed to retrieve value
     */
    public BigInteger getValueAsBigInteger(String n)
        throws EBaseException {
        String v = (String) mArgs.get(n);

        if (v != null) {
            try {
                return new BigInteger(v, 10);
            } catch (NumberFormatException e) {
                try {
                    return new BigInteger(v, 16);
                } catch (NumberFormatException ex) {
                    throw new EBaseException(
                            CMS.getUserMessage("CMS_BASE_INVALID_ATTR_TYPE", n, ex.toString()));
                }
            }
        } else {
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_ATTRIBUTE_NOT_FOUND", n));
        }
    }

    /**
     * Retrieves argument value as big integer.
     *
     * @param n key
     * @param def default value to be returned if key is not present
     * @return argument value as big integer
     */
    public BigInteger getValueAsBigInteger(String n, BigInteger def) {
        try {
            return getValueAsBigInteger(n);
        } catch (EBaseException e) {
            return def;
        }
    }

    /**
     * Retrieves argument value as object
     *
     * @param n key
     * @return argument value as object
     * @exception EBaseException failed to retrieve value
     */
    public Object getValue(Object n) throws EBaseException {
        if (mArgs.get(n) != null) {
            return mArgs.get(n);
        } else {
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_ATTRIBUTE_NOT_FOUND", (String) n));
        }
    }

    /**
     * Retrieves argument value as object
     *
     * @param n key
     * @param def default value to be returned if key is not present
     * @return argument value as object
     */
    public Object getValue(Object n, Object def) {
        if (mArgs.get(n) != null) {
            return mArgs.get(n);
        } else {
            return def;
        }
    }

    /**
     * Gets boolean value. They should be "true" or "false".
     *
     * @param name name of the input type
     * @return boolean type: <code>true</code> or <code>false</code>
     * @exception EBaseException failed to retrieve value
     */
    public boolean getValueAsBoolean(String name)  throws EBaseException {
        String val = (String) mArgs.get(name);
		CMS.traceHashKey(mType, name, val);

        if (val != null) {
            if (val.equalsIgnoreCase("true") || 
                val.equalsIgnoreCase("on")) 
                return true;
            else
                return false;
        } else {
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_ATTRIBUTE_NOT_FOUND", name));
        }
    }

    /**
     * Gets boolean value. They should be "true" or "false".
     *
     * @param name name of the input type
     * @return boolean type: <code>true</code> or <code>false</code>
     */
    public boolean getValueAsBoolean(String name, boolean def) {
        boolean val;

        try { 
            val = getValueAsBoolean(name); 
            return val;
        } catch (EBaseException e) { 
            return def; 
        }
    }

    /**
     * Gets KeyGenInfo
     *
     * @param name name of the input type
     * @param verify true if signature validation is required
     * @exception EBaseException
     * @return KeyGenInfo object
     */
    public KeyGenInfo getValueAsKeyGenInfo(String name, KeyGenInfo def)
        throws EBaseException {
        KeyGenInfo keyGenInfo;

		CMS.traceHashKey(mType, name);
        if (mArgs.get(name) != null) {
            try {
                keyGenInfo = new KeyGenInfo((String) mArgs.get(name));
            } catch (IOException e) {
                return def;
            }

        } else {
            return def;
        }
        return keyGenInfo;
    }

    /**
     * Gets PKCS10 request. This pkcs10 attribute does not 
     * contain header information.
     *
     * @param name name of the input type
     * @return pkcs10 request
     * @exception EBaseException failed to retrieve value
     */
    public PKCS10 getValueAsRawPKCS10(String name) throws EBaseException {
        PKCS10 request;

        if (mArgs.get(name) != null) {
			CMS.traceHashKey(mType, name, (String)mArgs.get(name));

            String tempStr = unwrap((String) mArgs.get(name), false);

            if (tempStr == null) {
                throw new EBaseException(
                        CMS.getUserMessage("CMS_BASE_INVALID_ATTR_VALUE", name, "Empty Content")); 
            }
            try {
                request = decodePKCS10(tempStr);
            } catch (Exception e) {
                throw new EBaseException(
                        CMS.getUserMessage("CMS_BASE_INVALID_ATTR_VALUE", name, e.toString())); 
            }
        } else {
			CMS.traceHashKey(mType, name, "<notpresent>");
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_ATTRIBUTE_NOT_FOUND", name));
        }

        return request;
    }

    /**
     * Gets PKCS10 request. This pkcs10 attribute does not 
     * contain header information.
     *
     * @param name name of the input type
     * @param def default PKCS10
     * @return pkcs10 request
     * @exception EBaseException failed to retrieve value
     */
    public PKCS10 getValueAsRawPKCS10(String name, PKCS10 def)
        throws EBaseException {
        PKCS10 request;

		CMS.traceHashKey(mType, name);
        if (mArgs.get(name) != null) {

            String tempStr = unwrap((String) mArgs.get(name), false);

            if (tempStr == null) {
                return def;
            }
            try {
                request = decodePKCS10(tempStr);
            } catch (Exception e) {
                return def;
            }
        } else {
            return def;
        }
        return request;
    }

    /**
     * Retrieves PKCS10
     *
     * @param name name of the  input type
     * @param checkheader true if header must be present
     * @return PKCS10 object
     * @exception EBaseException failed to retrieve value
     */
    public PKCS10 getValueAsPKCS10(String name, boolean checkheader) 
        throws EBaseException {
        PKCS10 request;

		CMS.traceHashKey(mType, name);
        if (mArgs.get(name) != null) {

            String tempStr = unwrap((String) mArgs.get(name), checkheader);

            if (tempStr == null) {
                throw new EBaseException(
                        CMS.getUserMessage("CMS_BASE_INVALID_ATTR_VALUE", name, "Empty Content")); 
            }
            try {
                request = decodePKCS10(tempStr);
            } catch (Exception e) {
                throw new EBaseException(
                        CMS.getUserMessage("CMS_BASE_INVALID_ATTR_VALUE", name, e.toString())); 
            }
        } else {
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_ATTRIBUTE_NOT_FOUND", name));
        }

        return request;
    }

    /**
     * Retrieves PKCS10
     *
     * @param name name of the  input type
     * @param checkheader true if header must be present
     * @param def default PKCS10
     * @return PKCS10 object 
     * @exception EBaseException
     */
    public PKCS10 getValueAsPKCS10(
        String name, boolean checkheader, PKCS10 def) 
        throws EBaseException {
        PKCS10 request;

		CMS.traceHashKey(mType, name);

        if (mArgs.get(name) != null) {

            String tempStr = unwrap((String) mArgs.get(name), checkheader);

            if (tempStr == null) {
                return def;
            }
            try {
                request = decodePKCS10(tempStr);
            } catch (Exception e) {
                return def;
            }
        } else {
            return def;
        }

        return request;
    }

    /**
     * Retrieves PKCS10
     *
     * @param name name of the  input type
     * @param def default PKCS10
     * @return PKCS10 object 
     * @exception EBaseException
     */
    public PKCS10 getValuePKCS10(String name, PKCS10 def) 
        throws EBaseException {
        PKCS10 request;
        String p10b64 = (String) mArgs.get(name);
		CMS.traceHashKey(mType, name);

        if (p10b64 != null) {

            try {
                request = decodePKCS10(p10b64);
                return request;
            } catch (Exception e) {
                return def;
            }
        } else {
            return def;
        }
    }

    /**
     * Sets argument into this block.
     *
     * @param name key
     * @param ob value
     */
    public void set(String name, Object ob) {
        mArgs.put(name, ob);
    }

    /**
     * Retrieves argument.
     *
     * @param name key
     * @return object value
     */
    public Object get(String name) {
		CMS.traceHashKey(mType, name);
        return mArgs.get(name);
    }

    /**
     * Deletes argument by the given key.
     *
     * @param name key
     */
    public void delete(String name) {
        mArgs.remove(name);
    }

    /**
     * Retrieves a list of argument keys.
     *
     * @return a list of string-based keys
     */
    public Enumeration getElements() {
        return mArgs.keys();
    }

    /**
     * Retrieves a list of argument keys.
     *
     * @return a list of string-based keys
     */
    public Enumeration elements() {
        return mArgs.keys();
    }

    /**
     * Adds long-type arguments to this block.
     *
     * @param n key
     * @param v value
     * @return value
     */
    public Object addLongValue(String n, long v) {
        return mArgs.put(n, Long.valueOf(v));
    }

    /**
     * Adds integer-type arguments to this block.
     *
     * @param n key
     * @param v value
     * @return value
     */
    public Object addIntegerValue(String n, int v) {
        return mArgs.put(n, Integer.valueOf(v));
    }

    /**
     * Adds boolean-type arguments to this block.
     *
     * @param n key
     * @param v value
     * @return value
     */
    public Object addBooleanValue(String n, boolean v) {
        if (v) {
            return mArgs.put(n, new Boolean("true"));
        } else {
            return mArgs.put(n, new Boolean("false"));
        }
    }

    /**
     * Adds integer-type arguments to this block.
     *
     * @param n key
     * @param v value
     * @param radix radix
     * @return value
     */
    public Object addBigIntegerValue(String n, BigInteger v, int radix) {
        return mArgs.put(n, v.toString(radix));
    }

    /*==========================================================
     * private methods
     *==========================================================*/


    /**
     * Unwrap PKCS10 Package
     *
     * @param request string formated PKCS10 request
     * @exception EBaseException
     * @return Base64Encoded PKCS10 request
     */
    private String unwrap(String request, boolean checkHeader)
        throws EBaseException {
        String unwrapped;
        String header = null;
        int head = -1;
        int trail = -1;

        // check for "-----BEGIN NEW CERTIFICATE REQUEST-----";
        if (header == null) {
            head = request.indexOf(CERT_NEW_REQUEST_HEADER);
            trail = request.indexOf(CERT_NEW_REQUEST_TRAILER);

            if (!(head == -1 && trail == -1)) {
                header = CERT_NEW_REQUEST_HEADER;
            }
        }

        // check for "-----BEGIN CERTIFICATE REQUEST-----";
        if (header == null) {
            head = request.indexOf(CERT_REQUEST_HEADER);
            trail = request.indexOf(CERT_REQUEST_TRAILER);

            // If this is not a request header, check if this is a renewal
            // header.
            if (!(head == -1 && trail == -1)) {
                header = CERT_REQUEST_HEADER;
		
            }
        }

        // check for "-----BEGIN RENEWAL CERTIFICATE REQUEST-----";
        if (header == null) {
            head = request.indexOf(CERT_RENEWAL_HEADER);
            trail = request.indexOf(CERT_RENEWAL_TRAILER);
            if (!(head == -1 && trail == -1)) {
                header = CERT_RENEWAL_HEADER;
            }
        }

        // Now validate if any headers or trailers are in place
        if (head == -1 && checkHeader) {
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_MISSING_PKCS10_HEADER"));
        }
        if (trail == -1 && checkHeader) {
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_MISSING_PKCS10_TRAILER"));
        }

        if (header != null) {
            unwrapped = request.substring(head + header.length(), trail);
        } else {
            unwrapped = request;
        }

        // strip all the crtl-characters (i.e. \r\n)
        StringTokenizer st = new StringTokenizer(unwrapped, "\t\r\n ");
        StringBuffer stripped = new StringBuffer();

        while (st.hasMoreTokens()) {
            stripped.append(st.nextToken());
        }

        return stripped.toString();
    }

    /**
     * Decode Der encoded PKCS10 certifictae Request
     *
     * @param base64Request Base64 Encoded Certificate Request
     * @exception Exception
     * @return PKCS10
     */
    private PKCS10 decodePKCS10(String base64Request)
        throws EBaseException {
        PKCS10 pkcs10 = null;

        try {
            byte[] decodedBytes = com.netscape.osutil.OSUtil.AtoB(base64Request);

            pkcs10 = new PKCS10(decodedBytes);
        } catch (NoSuchProviderException e) { 
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_INTERNAL_ERROR", e.toString()));
        } catch (IOException e) { 
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_INTERNAL_ERROR", e.toString()));
        } catch (SignatureException e) {
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_INTERNAL_ERROR", e.toString()));
        } catch (NoSuchAlgorithmException e) {
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_INTERNAL_ERROR", e.toString()));
        }

        return pkcs10;
    }

}