summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/policy/constraints/ValidityConstraints.java
blob: 62c49450b2cf71f3523fda775fad046f6ce418e6 (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
// --- 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.Date;
import java.util.Locale;
import java.util.Vector;

import netscape.security.x509.CertificateValidity;
import netscape.security.x509.X509CertInfo;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.base.IExtendedPluginInfo;
import com.netscape.certsrv.base.ISubsystem;
import com.netscape.certsrv.policy.EPolicyException;
import com.netscape.certsrv.policy.IEnrollmentPolicy;
import com.netscape.certsrv.request.IRequest;
import com.netscape.certsrv.request.PolicyResult;
import com.netscape.cms.policy.APolicyRule;


/**
 * ValidityConstraints is a default rule for Enrollment and
 * Renewal that enforces minimum and maximum validity periods
 * and changes them if not met.
 *
 * Optionally the lead and lag times - i.e how far back into the
 * front or back the notBefore date could go in minutes can also
 * be specified.
 * <P>
 * <PRE>
 * NOTE:  The Policy Framework has been replaced by the Profile Framework.
 * </PRE>
 * <P>
 *
 * @deprecated
 * @version $Revision$, $Date$
 */
public class ValidityConstraints extends APolicyRule
    implements IEnrollmentPolicy, IExtendedPluginInfo {
    protected long mMinValidity;
    protected long mMaxValidity;
    protected long mLeadTime;
    protected long mLagTime;
    protected long mNotBeforeSkew;

    private final static String PROP_MIN_VALIDITY = "minValidity";
    private final static String PROP_MAX_VALIDITY = "maxValidity";
    private final static String PROP_LEAD_TIME = "leadTime";
    private final static String PROP_LAG_TIME = "lagTime";
    private final static String PROP_NOT_BEFORE_SKEW = "notBeforeSkew";
    public final static int DEF_MIN_VALIDITY = 180;
    public final static int DEF_MAX_VALIDITY = 730;
    public final static int DEF_LEAD_TIME = 10;
    public final static int DEF_LAG_TIME = 10;
    public final static int DEF_NOT_BEFORE_SKEW = 5;
    public final static long DAYS_TO_MS_FACTOR = 24L * 3600 * 1000;
    public final static long MINS_TO_MS_FACTOR = 60L * 1000;

    private final static Vector defConfParams = new Vector();

    static {
        defConfParams.addElement(PROP_MIN_VALIDITY + "=" +
            DEF_MIN_VALIDITY);
        defConfParams.addElement(PROP_MAX_VALIDITY + "=" +
            DEF_MAX_VALIDITY);
        defConfParams.addElement(PROP_LEAD_TIME + "=" +
            DEF_LEAD_TIME);
        defConfParams.addElement(PROP_LAG_TIME + "=" +
            DEF_LAG_TIME);
        defConfParams.addElement(PROP_NOT_BEFORE_SKEW + "=" +
            DEF_NOT_BEFORE_SKEW);
    }

    public String[] getExtendedPluginInfo(Locale locale) {
        String[] params = {
                PROP_MIN_VALIDITY + ";number;Minimum Validity time, in days",
                PROP_MAX_VALIDITY + ";number;Maximum Validity time, in days",
                PROP_LEAD_TIME + ";number;Number of minutes in the future a request's notBefore can be",
                PROP_LAG_TIME + ";number;NOT CURRENTLY IN USE",
                PROP_NOT_BEFORE_SKEW + ";number;Number of minutes a cert's notBefore should be in the past",
                IExtendedPluginInfo.HELP_TOKEN +
                ";configuration-policyrules-validityconstraints",
                IExtendedPluginInfo.HELP_TEXT +
                ";Ensures that the user's requested validity period is " +
                "acceptable. If not specified, as is usually the case, " +
                "this policy will set the validity. See RFC 2459."
            };

        return params;

    }

    public ValidityConstraints() {
        NAME = "ValidityConstraints";
        DESC = "Enforces minimum and maximum validity constraints.";
    }

    /**
     * Initializes this policy rule.
     * <P>
     *
     * The entries probably are of the form:
     *
     *      ra.Policy.rule.<ruleName>.implName=ValidityConstraints
     *      ra.Policy.rule.<ruleName>.enable=true
     *      ra.Policy.rule.<ruleName>.minValidity=30
     *      ra.Policy.rule.<ruleName>.maxValidity=180
     *      ra.Policy.rule.<ruleName>.predicate=ou==Sales
     *
     * @param config	The config store reference
     */
    public void init(ISubsystem owner, IConfigStore config)
        throws EPolicyException {

        // Get min and max validity in days and configure them.
        try {
            String val = config.getString(PROP_MIN_VALIDITY, null);

            if (val == null)
                mMinValidity = DEF_MIN_VALIDITY * DAYS_TO_MS_FACTOR;
            else
                mMinValidity = Long.parseLong(val) * DAYS_TO_MS_FACTOR;

            val = config.getString(PROP_MAX_VALIDITY, null);
            if (val == null)
                mMaxValidity = DEF_MAX_VALIDITY * DAYS_TO_MS_FACTOR;
            else
                mMaxValidity = Long.parseLong(val) * DAYS_TO_MS_FACTOR;

            val = config.getString(PROP_LEAD_TIME, null);
            if (val != null)
                mLeadTime = Long.parseLong(val) * MINS_TO_MS_FACTOR;
            else
                mLeadTime = DEF_LEAD_TIME * MINS_TO_MS_FACTOR;

            val = config.getString(PROP_LAG_TIME, null);
            if (val != null)
                mLagTime = Long.parseLong(val) * MINS_TO_MS_FACTOR;
            else
                mLagTime = DEF_LAG_TIME * MINS_TO_MS_FACTOR;

            val = config.getString(PROP_NOT_BEFORE_SKEW, null);
            if (val != null)
                mNotBeforeSkew = Long.parseLong(val) * MINS_TO_MS_FACTOR;
            else
                mNotBeforeSkew = DEF_NOT_BEFORE_SKEW * MINS_TO_MS_FACTOR;
        } catch (Exception e) {
            // e.printStackTrace();
            String[] params = {getInstanceName(), e.toString()};

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

    /**
     * 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) {

        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);

            // There should be a certificate info set.
            if (certInfo == null) {
                setError(req, CMS.getUserMessage("CMS_POLICY_NO_CERT_INFO",
                        getInstanceName()), "");
                return PolicyResult.REJECTED;
            }

            // Else check if validity is within the limit
            for (int i = 0; i < certInfo.length; i++) {
                CertificateValidity validity = (CertificateValidity)
                    certInfo[i].get(X509CertInfo.VALIDITY);

                Date notBefore = null, notAfter = null;

                if (validity != null) {
                    notBefore = (Date)
                            validity.get(CertificateValidity.NOT_BEFORE);
                    notAfter = (Date)
                            validity.get(CertificateValidity.NOT_AFTER);
                }

                // If no validity is supplied yet, make one.  The default
                // validity is supposed to pass the following checks, so
                // bypass further checking.
                // (date = 0 is hack for serialization)

                if (validity == null ||
                    (notBefore.getTime() == 0 && notAfter.getTime() == 0)) {
                    certInfo[i].set(X509CertInfo.VALIDITY,
                        makeDefaultValidity(req));
                    continue;
                }

                Date now = CMS.getCurrentDate();

                if (notBefore.getTime() > (now.getTime() + mLeadTime)) {
                    setError(req, CMS.getUserMessage("CMS_POLICY_INVALID_BEGIN_TIME",
                            getInstanceName()), "");
                    result = PolicyResult.REJECTED;
                }
                if ((notAfter.getTime() - notBefore.getTime()) >
                    mMaxValidity) {
                    String params[] = {getInstanceName(), 
                            String.valueOf(
                                ((notAfter.getTime() - notBefore.getTime()) / DAYS_TO_MS_FACTOR)),
                            String.valueOf(mMaxValidity / DAYS_TO_MS_FACTOR)};

                    setError(req, CMS.getUserMessage("CMS_POLICY_MORE_THAN_MAX_VALIDITY", params), "");
                    result = PolicyResult.REJECTED;
                }
                if ((notAfter.getTime() - notBefore.getTime()) <
                    mMinValidity) {
                    String params[] = {getInstanceName(), 
                            String.valueOf(
                                ((notAfter.getTime() - notBefore.getTime()) / DAYS_TO_MS_FACTOR)),
                            String.valueOf(mMinValidity / DAYS_TO_MS_FACTOR)};

                    setError(req, CMS.getUserMessage("CMS_POLICY_LESS_THAN_MIN_VALIDITY", params), "");
                    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();

        confParams.addElement(PROP_MIN_VALIDITY + "=" +
            mMinValidity / DAYS_TO_MS_FACTOR);
        confParams.addElement(PROP_MAX_VALIDITY + "=" +
            mMaxValidity / DAYS_TO_MS_FACTOR);
        confParams.addElement(PROP_LEAD_TIME + "=" 
            + mLeadTime / MINS_TO_MS_FACTOR);
        confParams.addElement(PROP_LAG_TIME + "=" + 
            mLagTime / MINS_TO_MS_FACTOR);
        confParams.addElement(PROP_NOT_BEFORE_SKEW + "=" + 
            mNotBeforeSkew / MINS_TO_MS_FACTOR);
        return confParams;
    }

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

    /**
     * Create a default validity value for a request
     *
     * This code can be easily overridden in a derived class, if the
     * calculations here aren't accepatble.
     *
     * TODO: it might be good to base this calculation on the creation
     * time of the request.
     */
    protected CertificateValidity makeDefaultValidity(IRequest req) {
        long now = roundTimeToSecond((CMS.getCurrentDate()).getTime());

        // We will set the max duration as the default validity.
        long notBeforeTime = now - mNotBeforeSkew;
        Date notBefore = new Date(notBeforeTime);
        Date notAfter = new Date(notBeforeTime + mMaxValidity);

        return new CertificateValidity(notBefore, notAfter);
    }

    /**
     * convert a millisecond resolution time into one with 1 second
     * resolution.  Most times in certificates are storage at 1
     * second resolution, so its better if we deal with things at
     * that level.
     */
    protected long roundTimeToSecond(long input) {
        return (input / 1000) * 1000;
    }
}