summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/profile/constraint/ValidityConstraint.java
blob: 95c322214814bd590d84cc16ffbcfdf9f00367cf (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
// --- 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.constraint;


import java.io.IOException;
import java.util.Date;
import java.util.Locale;

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.profile.EProfileException;
import com.netscape.certsrv.profile.ERejectException;
import com.netscape.certsrv.profile.IPolicyDefault;
import com.netscape.certsrv.profile.IProfile;
import com.netscape.certsrv.property.Descriptor;
import com.netscape.certsrv.property.EPropertyException;
import com.netscape.certsrv.property.IDescriptor;
import com.netscape.certsrv.request.IRequest;
import com.netscape.cms.profile.def.CAValidityDefault;
import com.netscape.cms.profile.def.NoDefault;
import com.netscape.cms.profile.def.UserValidityDefault;
import com.netscape.cms.profile.def.ValidityDefault;


/**
 * This class implements the validity constraint.
 * It checks if the validity in the certificate
 * template satisfies the criteria.
 *
 * @version $Revision$, $Date$
 */
public class ValidityConstraint extends EnrollConstraint {

    public static final String CONFIG_RANGE = "range";
    public static final String CONFIG_NOT_BEFORE_GRACE_PERIOD = "notBeforeGracePeriod";
    public static final String CONFIG_CHECK_NOT_BEFORE = "notBeforeCheck";
    public static final String CONFIG_CHECK_NOT_AFTER = "notAfterCheck";
    public final static long SECS_IN_MS = 1000L;

    private Date mDefNotBefore = null;
    private Date mDefNotAfter = null;

    public ValidityConstraint() {
        super();
        addConfigName(CONFIG_RANGE);
        addConfigName(CONFIG_NOT_BEFORE_GRACE_PERIOD);
        addConfigName(CONFIG_CHECK_NOT_BEFORE);
        addConfigName(CONFIG_CHECK_NOT_AFTER);
    }

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

    public void setConfig(String name, String value)
        throws EPropertyException {
        if (name.equals(CONFIG_RANGE) ||
            name.equals(CONFIG_NOT_BEFORE_GRACE_PERIOD)) {
          try {
            Integer.parseInt(value);
          } catch (Exception e) {
                throw new EPropertyException(CMS.getUserMessage(
                            "CMS_INVALID_PROPERTY", name));
          }
        }
        super.setConfig(name, value);
    }

    public IDescriptor getConfigDescriptor(Locale locale, String name) {
        if (name.equals(CONFIG_RANGE)) {
            return new Descriptor(IDescriptor.INTEGER, null, "365",
                    CMS.getUserMessage(locale, "CMS_PROFILE_VALIDITY_RANGE"));
        } else if (name.equals(CONFIG_NOT_BEFORE_GRACE_PERIOD)) {
            return new Descriptor(IDescriptor.INTEGER, null, "0",
                    CMS.getUserMessage(locale, "CMS_PROFILE_VALIDITY_NOT_BEFORE_GRACE_PERIOD"));
        } else if (name.equals(CONFIG_CHECK_NOT_BEFORE)) {
            return new Descriptor(IDescriptor.BOOLEAN, null, "false",
                    CMS.getUserMessage(locale, "CMS_PROFILE_VALIDITY_CHECK_NOT_BEFORE"));
        } else if (name.equals(CONFIG_CHECK_NOT_AFTER)) {
            return new Descriptor(IDescriptor.BOOLEAN, null, "false",
                    CMS.getUserMessage(locale, "CMS_PROFILE_VALIDITY_CHECK_NOT_AFTER"));
        }
        return null;
    }

    /**
     * Validates the request. The request is not modified
     * during the validation.
     */
    public void validate(IRequest request, X509CertInfo info)
        throws ERejectException {
        CertificateValidity v = null;

        try {
            v = (CertificateValidity) info.get(X509CertInfo.VALIDITY);
        } catch (Exception e) {
            throw new ERejectException(CMS.getUserMessage(getLocale(request),
                        "CMS_PROFILE_VALIDITY_NOT_FOUND"));
        }
        Date notBefore = null;

        try {
            notBefore = (Date) v.get(CertificateValidity.NOT_BEFORE);
        } catch (IOException e) {
            CMS.debug("ValidityConstraint: not before not found");
            throw new ERejectException(CMS.getUserMessage(getLocale(request),
                        "CMS_PROFILE_VALIDITY_NOT_FOUND"));
        }
        Date notAfter = null;

        try {
            notAfter = (Date) v.get(CertificateValidity.NOT_AFTER);
        } catch (IOException e) {
            CMS.debug("ValidityConstraint: not after not found");
            throw new ERejectException(CMS.getUserMessage(getLocale(request),
                        "CMS_PROFILE_VALIDITY_NOT_FOUND"));
        }

        if (notAfter.getTime() < notBefore.getTime()) {
            CMS.debug("ValidityConstraint: notAfter (" + notAfter + ") < notBefore (" + notBefore + ")");
            throw new ERejectException(CMS.getUserMessage(getLocale(request),
                        "CMS_PROFILE_NOT_AFTER_BEFORE_NOT_BEFORE"));
        }

        long millisDiff = notAfter.getTime() - notBefore.getTime();
        CMS.debug("ValidityConstraint: millisDiff=" + millisDiff + " notAfter=" + notAfter.getTime() + " notBefore=" + notBefore.getTime());
        long long_days = (millisDiff / 1000 ) / 86400;
        CMS.debug("ValidityConstraint: long_days: "+long_days);
        int days = (int)long_days;
        CMS.debug("ValidityConstraint: days: "+days);

        if (days > Integer.parseInt(getConfig(CONFIG_RANGE))) {
            throw new ERejectException(CMS.getUserMessage(getLocale(request),
                        "CMS_PROFILE_VALIDITY_OUT_OF_RANGE", 
                        Integer.toString(days)));
        }

        // 613828 
        // The validity field shall specify a notBefore value 
        // that does not precede the current time and a notAfter 
        // value that does not precede the value specified in 
        // notBefore (test can be automated; try entering violating 
        // time values and check result).
        String notBeforeCheckStr = getConfig(CONFIG_CHECK_NOT_BEFORE);
        boolean notBeforeCheck;

        if (notBeforeCheckStr == null || notBeforeCheckStr.equals("")) {
            notBeforeCheckStr = "false";
        }
        notBeforeCheck = Boolean.valueOf(notBeforeCheckStr).booleanValue(); 

        String notAfterCheckStr = getConfig(CONFIG_CHECK_NOT_AFTER);
        boolean notAfterCheck;

        if (notAfterCheckStr == null || notAfterCheckStr.equals("")) {
            notAfterCheckStr = "false";
        }
        notAfterCheck = Boolean.valueOf(notAfterCheckStr).booleanValue(); 

        String notBeforeGracePeriodStr = getConfig(CONFIG_NOT_BEFORE_GRACE_PERIOD);
        if (notBeforeGracePeriodStr == null || notBeforeGracePeriodStr.equals("")) {
            notBeforeGracePeriodStr = "0";
        }
        long notBeforeGracePeriod = Long.parseLong(notBeforeGracePeriodStr) * SECS_IN_MS;

        Date current = CMS.getCurrentDate();
        if (notBeforeCheck) {
            if (notBefore.getTime() > (current.getTime() + notBeforeGracePeriod)) {
                CMS.debug("ValidityConstraint: notBefore (" + notBefore + ") > current + "+
                          "gracePeriod (" + new Date(current.getTime() + notBeforeGracePeriod) + ")");
                throw new ERejectException(CMS.getUserMessage(getLocale(request),
                            "CMS_PROFILE_NOT_BEFORE_AFTER_CURRENT"));
            }
        }
        if (notAfterCheck) {
            if (notAfter.getTime() < current.getTime()) {
                CMS.debug("ValidityConstraint: notAfter (" + notAfter + ") <  current + (" + current + ")");
                throw new ERejectException(CMS.getUserMessage(getLocale(request),
                            "CMS_PROFILE_NOT_AFTER_BEFORE_CURRENT"));
            }
        }
    }

    public String getText(Locale locale) {
        return CMS.getUserMessage(locale, "CMS_PROFILE_CONSTRAINT_VALIDITY_TEXT", getConfig(CONFIG_RANGE));
    }

    public boolean isApplicable(IPolicyDefault def) {
        if (def instanceof NoDefault)
            return true;
        if (def instanceof UserValidityDefault)
            return true;
        if (def instanceof ValidityDefault)
            return true;
        if (def instanceof CAValidityDefault)
            return true;
        return false;
    }
}