summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/policy/extensions/PolicyConstraintsExt.java
blob: 4396a09dd6cd76acb393caadd5b32618c46824cd (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
// --- 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.extensions;


import java.util.*;
import java.io.*;
import java.security.cert.*;
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.ca.*;
import com.netscape.certsrv.apps.*;
import com.netscape.certsrv.logging.ILogger;
import netscape.security.x509.*;
import netscape.security.util.*;
import com.netscape.cms.policy.APolicyRule;


/**
 * Policy Constraints Extension Policy
 * Adds the policy constraints extension to (CA) certificates. 
 * Filtering of CA certificates is done through predicates.
 * <P>
 * <PRE>
 * NOTE:  The Policy Framework has been replaced by the Profile Framework.
 * </PRE>
 * <P>
 *
 * @deprecated
 * @version $Revision$, $Date$
 */
public class PolicyConstraintsExt extends APolicyRule
    implements IEnrollmentPolicy, IExtendedPluginInfo {
    protected static final String PROP_CRITICAL = "critical";
    protected static final String 
        PROP_REQ_EXPLICIT_POLICY = "reqExplicitPolicy";
    protected static final String 
        PROP_INHIBIT_POLICY_MAPPING = "inhibitPolicyMapping";

    protected static final boolean DEF_CRITICAL = false;
    protected static final int DEF_REQ_EXPLICIT_POLICY = -1; 	// not set
    protected static final int DEF_INHIBIT_POLICY_MAPPING = -1;	// not set

    protected boolean mEnabled = false;
    protected IConfigStore mConfig = null;

    protected boolean mCritical = DEF_CRITICAL;
    protected int mReqExplicitPolicy = DEF_REQ_EXPLICIT_POLICY;
    protected int mInhibitPolicyMapping = DEF_INHIBIT_POLICY_MAPPING;
    protected PolicyConstraintsExtension mPolicyConstraintsExtension = null;

    protected Vector mInstanceParams = new Vector();

    protected static Vector mDefaultParams = new Vector();
    static {
        mDefaultParams.addElement(PROP_CRITICAL + "=" + DEF_CRITICAL);
        mDefaultParams.addElement(
            PROP_REQ_EXPLICIT_POLICY + "=" + DEF_REQ_EXPLICIT_POLICY);
        mDefaultParams.addElement(
            PROP_INHIBIT_POLICY_MAPPING + "=" + DEF_INHIBIT_POLICY_MAPPING);
    }

    public PolicyConstraintsExt() {
        NAME = "PolicyConstriantsExt";
        DESC = "Sets Policy Constraints Extension on subordinate CA certs";
    }

    /**
     * Initializes this policy rule.
     * <P>
     *
     * The entries may be of the form:
     *
     *      ca.Policy.rule.<ruleName>.predicate=certType==ca
     *      ca.Policy.rule.<ruleName>.implName=
     *      ca.Policy.rule.<ruleName>.enable=true
     *
     * @param config	The config store reference
     */
    public void init(ISubsystem owner, IConfigStore config)
        throws EBaseException {
        mConfig = config;

        // XXX should do do this ? 
        // if CA does not allow subordinate CAs by way of basic constraints, 
        // this policy always rejects 
        /*****
         ICertAuthority certAuthority = (ICertAuthority)
         ((GenericPolicyProcessor)owner).mAuthority;
         if (certAuthority instanceof ICertificateAuthority) {
         CertificateChain caChain = certAuthority.getCACertChain();
         X509Certificate caCert = null;
         // Note that in RA the chain could be null if CA was not up when
         // RA was started. In that case just set the length to -1 and let
         // CA reject if it does not allow any subordinate CA certs.
         if (caChain != null) {
         caCert = caChain.getFirstCertificate();
         if (caCert != null) 
         mCAPathLen = caCert.getBasicConstraints();
         }
         }
         ****/

        mEnabled = mConfig.getBoolean(
                    IPolicyProcessor.PROP_ENABLE, false);
        mCritical = mConfig.getBoolean(PROP_CRITICAL, DEF_CRITICAL);

        mReqExplicitPolicy = mConfig.getInteger(
                    PROP_REQ_EXPLICIT_POLICY, DEF_REQ_EXPLICIT_POLICY);
        mInhibitPolicyMapping = mConfig.getInteger(
                    PROP_INHIBIT_POLICY_MAPPING, DEF_INHIBIT_POLICY_MAPPING);

        if (mReqExplicitPolicy < -1) 
            mReqExplicitPolicy = -1;
        if (mInhibitPolicyMapping < -1) 
            mInhibitPolicyMapping = -1;
		
            // create instance of policy constraings extension 
        try {
            mPolicyConstraintsExtension = 
                    new PolicyConstraintsExtension(mCritical, 
                        mReqExplicitPolicy, mInhibitPolicyMapping);
            CMS.debug(
                "PolicyConstraintsExt: Created Policy Constraints Extension: " +
                mPolicyConstraintsExtension);
        } catch (IOException e) {
            log(ILogger.LL_FAILURE,
                CMS.getLogMessage("POLICY_ERROR_CANT_INIT_POLICY_CONST_EXT", e.toString()));
            throw new EBaseException(
                    CMS.getUserMessage("CMS_BASE_INTERNAL_ERROR",
                        "Could not init Policy Constraints Extension. Error: " + e));
        }

        // form instance params 
        mInstanceParams.addElement(PROP_CRITICAL + "=" + mCritical);
        mInstanceParams.addElement(
            PROP_REQ_EXPLICIT_POLICY + "=" + mReqExplicitPolicy);
        mInstanceParams.addElement(
            PROP_INHIBIT_POLICY_MAPPING + "=" + mInhibitPolicyMapping);
    }

    /**
     * Adds Policy Constraints Extension to a (CA) certificate.
     * 
     * If a Policy constraints Extension is already there, accept it if 
     * it's been approved by agent, else replace it.
     *
     * @param req	The request on which to apply policy.
     * @return The policy result object.
     */
    public PolicyResult apply(IRequest req) {
        // if extension hasn't been properly configured reject requests until 
        // it has been resolved (or disabled).
        if (mPolicyConstraintsExtension == null) {
            return PolicyResult.ACCEPTED;
        }

        // get certInfo from request.
        X509CertInfo[] ci = 
            req.getExtDataInCertInfoArray(IRequest.CERT_INFO);

        if (ci == null || ci[0] == null) {
            setError(req, CMS.getUserMessage("CMS_POLICY_NO_CERT_INFO"), NAME);
            return PolicyResult.REJECTED; 
        }

        for (int i = 0; i < ci.length; i++) {
            PolicyResult certRes = applyCert(req, ci[i]);

            if (certRes == PolicyResult.REJECTED)
                return certRes;
        }
        return PolicyResult.ACCEPTED;
    }

    public PolicyResult applyCert(IRequest req, X509CertInfo certInfo) {

        // check if name constraints extension already exists.
        // if not agent approved, replace name constraints extension with ours.
        // else ignore.
        try {
            PolicyConstraintsExtension policyConstraintsExt = null;
            CertificateExtensions extensions = (CertificateExtensions)
                certInfo.get(X509CertInfo.EXTENSIONS);

            try {
                if (extensions != null) {
                    policyConstraintsExt = (PolicyConstraintsExtension)
                            extensions.get(PolicyConstraintsExtension.NAME);
                }
            } catch (IOException e) {
                // extension isn't there. 
            }

            if (policyConstraintsExt != null) {
                if (agentApproved(req)) {
                    return PolicyResult.ACCEPTED;
                } else {
                    extensions.delete(PolicyConstraintsExtension.NAME);
                }
            }

            if (extensions == null) {
                certInfo.set(X509CertInfo.VERSION,
                    new CertificateVersion(CertificateVersion.V3));
                extensions = new CertificateExtensions();
                certInfo.set(X509CertInfo.EXTENSIONS, extensions);
            }
            extensions.set(
                "PolicyConstriantsExt", mPolicyConstraintsExtension);
            CMS.debug("PolicyConstraintsExt: added our policy constraints extension");
            return PolicyResult.ACCEPTED;
        } catch (IOException e) {
            log(ILogger.LL_FAILURE, 
                CMS.getLogMessage("POLICY_ERROR_CANT_PROCESS_POLICY_CONST_EXT", e.toString()));
            setError(req, CMS.getUserMessage("CMS_POLICY_UNEXPECTED_POLICY_ERROR"), 
                NAME, e.getMessage());
            return PolicyResult.REJECTED;
        } catch (CertificateException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("CA_CERT_INFO_ERROR", e.toString()));
            setError(req, CMS.getUserMessage("CMS_POLICY_UNEXPECTED_POLICY_ERROR"), 
                NAME, "Certificate Info Error");
            return PolicyResult.REJECTED;
        }
    }

    /**
     * Return configured parameters for a policy rule instance.
     *
     * @return nvPairs A Vector of name/value pairs.
     */
    public Vector getInstanceParams() { 
        return mInstanceParams;
    }

    /**
     * Return default parameters for a policy implementation.
     *
     * @return nvPairs A Vector of name/value pairs.
     */
    public Vector getDefaultParams() { 
        return mDefaultParams;
    }

    /**
     * gets plugin info for pretty console edit displays. 
     */
    public String[] getExtendedPluginInfo(Locale locale) {
        mInstanceParams.addElement(PROP_CRITICAL + "=" + mCritical);
        mInstanceParams.addElement(
            PROP_REQ_EXPLICIT_POLICY + "=" + mReqExplicitPolicy);
        mInstanceParams.addElement(
            PROP_INHIBIT_POLICY_MAPPING + "=" + mInhibitPolicyMapping);

        String[] params = {
                PROP_CRITICAL + ";boolean;RFC 2459 recommendation: may be critical or non-critical.",
                PROP_REQ_EXPLICIT_POLICY + ";integer;Number of addional certificates that may appear in the path before an explicit policy is required. If less than 0 this field is unset in the extension.",
                PROP_INHIBIT_POLICY_MAPPING + ";integer;Number of addional certificates that may appear in the path before policy mapping is no longer permitted. If less than 0 this field is unset in the extension.",
                IExtendedPluginInfo.HELP_TOKEN + ";configuration-policyrules-policyconstraints"
            };

        return params;
    }
}