summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/policy/constraints/SigningAlgorithmConstraints.java
blob: be9870935ec336889c1c500533d8e68060ccd4ac (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
// --- 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.policy.constraints;


import java.util.*;
import java.io.*;
import com.netscape.certsrv.request.IRequest;
import com.netscape.certsrv.request.PolicyResult;
import com.netscape.certsrv.policy.*;
import com.netscape.certsrv.base.*;
import com.netscape.certsrv.authority.*;
import com.netscape.certsrv.common.*;
import com.netscape.certsrv.logging.ILogger;
import com.netscape.certsrv.apps.CMS;
import netscape.security.x509.*;
import com.netscape.certsrv.ca.*;
import netscape.security.extensions.*;
import com.netscape.cms.policy.APolicyRule;


/**
 * SigningAlgorithmConstraints enforces that only a supported
 * signing algorithm be requested.
 * <P>
 * <PRE>
 * NOTE:  The Policy Framework has been replaced by the Profile Framework.
 * </PRE>
 * <P>
 *
 * @deprecated
 * @version $Revision$, $Date$
 */
public class SigningAlgorithmConstraints extends APolicyRule
    implements IEnrollmentPolicy, IExtendedPluginInfo {
    private String[] mAllowedAlgs = null; // algs allowed by this policy
    static String[] mDefaultAllowedAlgs = null; // default algs allowed by this policy based on CA's key
    private String[] mConfigAlgs = null; // algs listed in config file
    private boolean winnowedByKey = false;
    IAuthority mAuthority = null;
    private final static String PROP_ALGORITHMS = "algorithms";

    private final static Vector defConfParams = new Vector();

    static {
        StringBuffer sb = new StringBuffer();
        sb.append(PROP_ALGORITHMS);
        sb.append("=");
        int i = 0;
        boolean first = true;

        mDefaultAllowedAlgs = new String[AlgorithmId.ALL_SIGNING_ALGORITHMS.length];
        for (i = 0; i < AlgorithmId.ALL_SIGNING_ALGORITHMS.length; i++) {
            mDefaultAllowedAlgs[i] = AlgorithmId.ALL_SIGNING_ALGORITHMS[i];
            if (first == true) {
                sb.append(AlgorithmId.ALL_SIGNING_ALGORITHMS[i]);
                first = false;
            } else {
                sb.append(",");
                sb.append(AlgorithmId.ALL_SIGNING_ALGORITHMS[i]);
            }
        }
        defConfParams.addElement(sb.toString());
    }

    public SigningAlgorithmConstraints() {
        NAME = "SigningAlgorithmConstraints";
        DESC = "Enforces Signing Algorithm Constraints.";
    }

    /**
     * Initializes this policy rule.
     * <P>
     *
     * The entries probably are of the form
     *      ra.Policy.rule.<ruleName>.implName=SigningAlgorithmConstraints
     *      ra.Policy.rule.<ruleName>.algorithms=SHA-1WithRSA, SHA-1WithDSA
     *      ra.Policy.rule.<ruleName>.enable=true
     *      ra.Policy.rule.<ruleName>.predicate=ou==Sales
     *
     * @param config	The config store reference
     */
    public void init(ISubsystem owner, IConfigStore config)
        throws EBaseException {
        mAuthority = (IAuthority) ((IPolicyProcessor) owner).getAuthority();

        // Get allowed algorithms from config file
        if (config != null) {
            String algNames = null;

            try {
                algNames = config.getString(PROP_ALGORITHMS, null);
            } catch (Exception e) {
                String[] params = {getInstanceName(), e.toString(), PROP_ALGORITHMS};

                throw new EPolicyException(
                        CMS.getUserMessage("CMS_POLICY_PARAM_CONFIG_ERROR", params));
            }

            if (algNames != null) {
                // parse alg names into Vector
                StringTokenizer tok = new StringTokenizer(algNames, ",");
                Vector algs = new Vector();

                while (tok.hasMoreTokens()) {
                    algs.addElement(tok.nextToken().trim());
                }

                // convert to array for speedy traversals during apply()
                int itemCount = algs.size();

                mAllowedAlgs = new String[itemCount];
                for (int i = 0; i < itemCount; i++) {
                    mAllowedAlgs[i] = (String) algs.elementAt(i);
                }
			
            }

        }

        // these are the algorithms from the config file
        mConfigAlgs = mAllowedAlgs;
        if (mConfigAlgs == null) {
            mConfigAlgs = new String[0];
        }

        if (mAllowedAlgs != null) {
            // winnow out unknown algorithms
            winnowAlgs(AlgorithmId.ALL_SIGNING_ALGORITHMS, 
                "CMS_POLICY_UNKNOWN_SIGNING_ALG", true);
        } else {
            // if nothing was in the config file, allow all known algs
            mAllowedAlgs = AlgorithmId.ALL_SIGNING_ALGORITHMS;
        }

        // winnow out algorithms that don't make sense for the key
        winnowByKey();

        if (mAllowedAlgs.length == 0) {
            throw new EPolicyException(
                    CMS.getUserMessage("CMS_POLICY_SIGNALG_NOT_MATCH_CAKEY", NAME));
        }

    }

    /**
     * winnow out algorithms that don't make sense for the CA's key
     */
    private synchronized void winnowByKey() throws EBaseException {
        // only do this successfully once
        if (winnowedByKey) {
            return;
        }

        // don't do this ever for DRM
        if (!(mAuthority instanceof ICertAuthority)) {
            winnowedByKey = true;
            return;
        }

        // get list of algorithms allowed for the key
        String[] allowedByKey =
            ((ICertAuthority) mAuthority).getCASigningAlgorithms();

        if (allowedByKey != null) {
            // don't show algorithms that don't match CA's key in UI.	
            mDefaultAllowedAlgs = new String[allowedByKey.length];
            for (int i = 0; i < allowedByKey.length; i++)
                mDefaultAllowedAlgs[i] = allowedByKey[i];
                // winnow out algorithms that don't match CA's signing key
            winnowAlgs(allowedByKey,
                "CMS_POLICY_SIGNALG_NOT_MATCH_CAKEY_1", false);
            winnowedByKey = true;
        } else {
            // We don't know the CA's signing algorithms.  Maybe we're
            // an RA that hasn't talked to the CA yet? Try again later.
        }
    }

    /**
     * Winnows out of mAllowedAlgorithms those algorithms that aren't allowed
     * for some reason.
     *
     * @param allowed An array of allowed algorithms.  Only algorithms in this
     *    list will survive the winnowing process.
     * @param reason A string describing the problem with an algorithm
     *		that is not allowed by this list. Must be a predefined string in PolicyResources.
     */
    private void winnowAlgs(String[] allowed, String reason, boolean isError) 
        throws EBaseException {
        int i, j, goodSize;

        // validate the currently-allowed algorithms
        Vector goodAlgs = new Vector();

        for (i = 0; i < mAllowedAlgs.length; i++) {
            for (j = 0; j < allowed.length; j++) {
                if (mAllowedAlgs[i].equals(allowed[j])) {
                    goodAlgs.addElement(mAllowedAlgs[i]);
                    break;
                }
            }
            // if algorithm is not allowed, log a warning
            if (j == allowed.length) {
                EPolicyException e = new EPolicyException(CMS.getUserMessage(reason, NAME, mAllowedAlgs[i]));

                if (isError) {
                    log(ILogger.LL_FAILURE, e.toString());
                    throw new EPolicyException(CMS.getUserMessage(reason,
                                NAME, mAllowedAlgs[i]));
                } else {
                    log(ILogger.LL_WARN, e.toString());
                }
            }
        }

        // convert back into an array
        goodSize = goodAlgs.size();
        if (mAllowedAlgs.length != goodSize) {
            mAllowedAlgs = new String[ goodSize ];
            for (i = 0; i < goodSize; i++) {
                mAllowedAlgs[i] = (String) goodAlgs.elementAt(i);
            }
        }
    }

    /**
     * Applies the policy on the given Request.
     * <P>
     *
     * @param req	The request on which to apply policy.
     * @return The policy result object.
     */
    public PolicyResult apply(IRequest req) {
        int i, j;

        PolicyResult result = PolicyResult.ACCEPTED;

        try {

            // Get the certificate info from the request
            //X509CertInfo certInfo[] = (X509CertInfo[])
            //    req.get(IRequest.CERT_INFO);
            X509CertInfo certInfo[] = req.getExtDataInCertInfoArray(IRequest.CERT_INFO);

            // We need to have a certificate info set
            if (certInfo == null) {
                setError(req, CMS.getUserMessage("CMS_POLICY_NO_CERT_INFO",
                        getInstanceName()), "");
                return PolicyResult.REJECTED;
            }

            // Else check if the key algorithm is supported.
            for (i = 0; i < certInfo.length; i++) {
                // make sure our list of allowed algorithms makes
                // sense for our key. Do this each time.
                if (!winnowedByKey) {
                    winnowByKey();
                }

                CertificateAlgorithmId certAlgId = (CertificateAlgorithmId)
                    certInfo[i].get(X509CertInfo.ALGORITHM_ID);

                AlgorithmId algId = (AlgorithmId)
                    certAlgId.get(CertificateAlgorithmId.ALGORITHM);
                String alg = algId.getName();

                // test against the list of allowed algorithms
                for (j = 0; j < mAllowedAlgs.length; j++) {
                    if (mAllowedAlgs[j].equals(alg)) {
                        break;
                    }
                }
                if (j == mAllowedAlgs.length) {
                    // if the algor doesn't match the CA's key replace
                    // it with one that does.
                    if (mAllowedAlgs[0].equals("SHA1withDSA") ||
                        alg.equals("SHA1withDSA")) {
                        certInfo[i].set(X509CertInfo.ALGORITHM_ID,
                            new CertificateAlgorithmId(
                                AlgorithmId.get(mAllowedAlgs[0])));
                        return PolicyResult.ACCEPTED;
                    }

                    // didn't find a match, alg not allowed
                    setError(req, CMS.getUserMessage("CMS_POLICY_SIGNING_ALG_VIOLATION",
                            getInstanceName(), alg), "");
                    result = PolicyResult.REJECTED;
                }
            }
        } catch (Exception e) {
            // e.printStackTrace();
            String params[] = {getInstanceName(), e.toString()};

            setError(req, CMS.getUserMessage("CMS_POLICY_UNEXPECTED_POLICY_ERROR", 
                    params), "");
            result = PolicyResult.REJECTED;
        }
        return result;
    }

    /**
     * Return configured parameters for a policy rule instance.
     *
     * @return nvPairs A Vector of name/value pairs.
     */
    public Vector getInstanceParams() { 
        Vector confParams = new Vector();
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < mConfigAlgs.length; i++) {
            sb.append(mConfigAlgs[i]);
            sb.append(",");
        }
        if (sb.length() > 0)
            sb.setLength(sb.length() - 1);
        confParams.addElement(PROP_ALGORITHMS + "=" + sb.toString());
        return confParams;
    }

    /**
     * Return default parameters for a policy implementation.
     *
     * @return nvPairs A Vector of name/value pairs.
     */
    public Vector getDefaultParams() { 
        StringBuffer sb = new StringBuffer();
        sb.append(PROP_ALGORITHMS);
        sb.append("=");
        boolean first = true;

        defConfParams.removeAllElements();

        for (int i = 0; i < mDefaultAllowedAlgs.length; i++) {
            if (first == true) {
                sb.append(mDefaultAllowedAlgs[i]);
                first = false;
            } else {
                sb.append(",");
                sb.append(mDefaultAllowedAlgs[i]);
            }
        }
        defConfParams.addElement(sb.toString());

        return defConfParams; 
    }

    public String[] getExtendedPluginInfo(Locale locale) {
        if (!winnowedByKey) {
            try { 
                winnowByKey(); 
            } catch (Exception e) { 
            }
        }

        String[] params = null;

        String[] params_BOTH = {
                PROP_ALGORITHMS + ";" + "choice(MD2withRSA\\,MD5withRSA\\,SHA1withRSA\\,SHA256withRSA\\,SHA512withRSA\\,SHA1withDSA," +
                "MD2withRSA\\,MD5withRSA\\,SHA1withRSA\\,SHA1withDSA,"+
                "MD2withRSA\\,MD5withRSA\\,SHA1withRSA," +
                "MD2withRSA\\,SHA1withRSA\\,SHA1withDSA," +
                "MD5withRSA\\,SHA1withRSA\\,SHA1withDSA," +
                "MD2withRSA\\,MD5withRSA\\,SHA1withDSA," +
                "MD2withRSA\\,MD5withRSA," +
                "MD2withRSA\\,SHA1withRSA," +
                "MD2withRSA\\,SHA1withDSA," +
                "MD5withRSA\\,SHA1withRSA," +
                "MD5withRSA\\,SHA1withDSA," +
                "SHA1withRSA\\,SHA1withDSA," +
                "MD2withRSA," +
                "MD5withRSA," +
                "SHA1withRSA," +
                "SHA1withDSA);List of algorithms to restrict the requested signing algorithm " +
                "to be one of the algorithms supported by Certificate System",
                IExtendedPluginInfo.HELP_TOKEN + ";configuration-policyrules-signingalgconstraints",
                IExtendedPluginInfo.HELP_TEXT +
                ";Restricts the requested signing algorithm to be one of" +
                " the algorithms supported by Certificate System"
            };

        String[] params_RSA = {
                PROP_ALGORITHMS + ";" + "choice(MD2withRSA\\,MD5withRSA\\,SHA1withRSA," +
                "MD2withRSA\\,MD5withRSA," +
                "MD2withRSA\\,SHA1withRSA," +
                "MD5withRSA\\,SHA1withRSA," +
                "MD2withRSA," +
                "MD5withRSA," +
                "SHA1withRSA);Restrict the requested signing algorithm to be " +
                "one of the algorithms supported by Certificate System",
                IExtendedPluginInfo.HELP_TOKEN + ";configuration-policyrules-signingalgconstraints",
                IExtendedPluginInfo.HELP_TEXT +
                ";Restricts the requested signing algorithm to be one of" +
                " the algorithms supported by Certificate System"
            };

        String[] params_DSA = {
                PROP_ALGORITHMS + ";" + "choice(SHA1withDSA);Restrict the requested signing " +
                "algorithm to be one of the algorithms supported by Certificate " +
                "System",
                IExtendedPluginInfo.HELP_TOKEN + ";configuration-policyrules-signingalgconstraints",
                IExtendedPluginInfo.HELP_TEXT +
                ";Restricts the requested signing algorithm to be one of" +
                " the algorithms supported by Certificate System"
            };

        switch (mDefaultAllowedAlgs.length) {
        case 1:
            params = params_DSA;
            break;

        case 3:
            params = params_RSA;
            break;

        case 4:
        default:
            params = params_BOTH;
            break;

        }

        return params;
    }

}