summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/policy/extensions/PrivateKeyUsagePeriodExt.java
blob: 52c8f1f6965aff1da79ce585aace1b2b5c9d97a1 (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
// --- 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.io.IOException;
import java.security.cert.CertificateException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Vector;

import netscape.security.x509.CertificateExtensions;
import netscape.security.x509.CertificateVersion;
import netscape.security.x509.PrivateKeyUsageExtension;
import netscape.security.x509.X509CertInfo;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.base.IExtendedPluginInfo;
import com.netscape.certsrv.base.ISubsystem;
import com.netscape.certsrv.logging.ILogger;
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;

/**
 * PrivateKeyUsagePeriod Identifier Extension policy.
 * <P>
 * 
 * <PRE>
 * NOTE:  The Policy Framework has been replaced by the Profile Framework.
 * </PRE>
 * <P>
 * 
 * @deprecated
 * @version $Revision$, $Date$
 */
public class PrivateKeyUsagePeriodExt extends APolicyRule
        implements IEnrollmentPolicy, IExtendedPluginInfo {

    private final static String PROP_NOT_BEFORE = "notBefore";
    private final static String PROP_NOT_AFTER = "notAfter";
    protected static final String PROP_IS_CRITICAL = "critical";

    // 6 months roughly
    private final static long defDuration = 60L * 60 * 24 * 180 * 1000;

    private static final String DATE_PATTERN = "MM/dd/yyyy";
    static SimpleDateFormat formatter = new SimpleDateFormat(DATE_PATTERN);
    private static Date now = CMS.getCurrentDate();
    private static Date six_months = new Date(now.getTime() + defDuration);

    public static final String DEFAULT_NOT_BEFORE = formatter.format(now);
    public static final String DEFAULT_NOT_AFTER = formatter.format(six_months);

    // PKIX specifies the that the extension SHOULD NOT be critical
    public static final boolean DEFAULT_CRITICALITY = false;

    protected String mNotBefore;
    protected String mNotAfter;
    protected boolean mCritical;

    private static Vector<String> defaultParams;

    static {

        formatter.setLenient(false);

        defaultParams = new Vector<String>();
        defaultParams.addElement(PROP_IS_CRITICAL + "=" + DEFAULT_CRITICALITY);
        defaultParams.addElement(PROP_NOT_BEFORE + "=" + DEFAULT_NOT_BEFORE);
        defaultParams.addElement(PROP_NOT_AFTER + "=" + DEFAULT_NOT_AFTER);
    }

    public String[] getExtendedPluginInfo(Locale locale) {
        String[] params = {
                PROP_IS_CRITICAL + ";boolean;RFC 2459 recommendation: The profile " +
                        "recommends against the use of this extension. CAs " +
                        "conforming to the profile MUST NOT generate certs with " +
                        "critical private key usage period extensions.",
                PROP_NOT_BEFORE + ";string; Date before which the Private Key is invalid.",
                PROP_NOT_AFTER + ";string; Date after which the Private Key is invalid.",
                IExtendedPluginInfo.HELP_TOKEN +
                        ";configuration-policyrules-privatekeyusageperiod",
                IExtendedPluginInfo.HELP_TEXT +
                        ";Adds (deprecated) Private Key Usage Period Extension. " +
                        "Defined in RFC 2459 (4.2.1.4)"
            };

        return params;
    }

    /**
     * Adds the private key usage extension to all certs.
     */
    public PrivateKeyUsagePeriodExt() {
        NAME = "PrivateKeyUsagePeriodExt";
        DESC = "Sets Private Key Usage Extension for a certificate";
    }

    /**
     * Initializes this policy rule.
     * ra.Policy.rule.<ruleName>.implName=PrivateKeyUsageExtension
     * ra.Policy.rule.<ruleName>.enable=true
     * ra.Policy.rule.<ruleName>.notBefore=30
     * ra.Policy.rule.<ruleName>.notAfter=180
     * ra.Policy.rule.<ruleName>.critical=false
     * ra.Policy.rule.<ruleName>.predicate=ou==Sales
     * 
     * @param config The config store reference
     */
    public void init(ISubsystem owner, IConfigStore config)
            throws EBaseException {

        try {
            // Get params.
            mNotBefore = config.getString(PROP_NOT_BEFORE, null);
            mNotAfter = config.getString(PROP_NOT_AFTER, null);
            mCritical = config.getBoolean(PROP_IS_CRITICAL, false);

            // Check the parameter formats
            String notBefore;
            String notAfter;

            notBefore = formatter.format(formatter.parse(mNotBefore.trim()));
            notAfter = formatter.format(formatter.parse(mNotAfter.trim()));
        } catch (Exception e) {
            // e.printStackTrace();
            Object[] params = { getInstanceName(), e };

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

    }

    /**
     * Adds a private key usage extension if none exists.
     * 
     * @param req The request on which to apply policy.
     * @return The policy result object.
     */
    public PolicyResult apply(IRequest req) {
        PolicyResult res = PolicyResult.ACCEPTED;

        // get cert info.
        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; // unrecoverable error.
        }

        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) {
        // get private key usage extension from cert info if any.
        CertificateExtensions extensions = null;
        PrivateKeyUsageExtension ext = null;

        try {
            // get subject key id extension if any.
            extensions = (CertificateExtensions)
                    certInfo.get(X509CertInfo.EXTENSIONS);
        } catch (IOException e) {
            // no extensions or subject key identifier extension.
        } catch (CertificateException e) {
            // no extensions or subject key identifier extension.
        }

        if (extensions == null) {
            extensions = new CertificateExtensions();
        } else {
            // remove any previously computed version of the extension
            try {
                extensions.delete(PrivateKeyUsageExtension.NAME);

            } catch (IOException e) {
            }

        }

        try {
            ext = new PrivateKeyUsageExtension(
                        formatter.parse(mNotBefore),
                        formatter.parse(mNotAfter));
            certInfo.set(X509CertInfo.VERSION,
                    new CertificateVersion(CertificateVersion.V3));
            extensions.set(PrivateKeyUsageExtension.NAME, ext);
        } catch (Exception e) {
            if (e instanceof RuntimeException)
                throw (RuntimeException) e;
            log(ILogger.LL_FAILURE,
                    CMS.getLogMessage("POLICY_ERROR_CREATE_PRIVATE_KEY_EXT", e.toString()));
            setError(req, CMS.getUserMessage("CMS_POLICY_SUBJECT_KEY_ID_ERROR"), NAME);
            return PolicyResult.REJECTED;
        }
        return PolicyResult.ACCEPTED;
    }

    /**
     * Return configured parameters for a policy rule instance.
     * 
     * @return Empty Vector since this policy has no configuration parameters.
     *         for this policy instance.
     */
    public Vector<String> getInstanceParams() {
        Vector<String> params = new Vector<String>();

        params.addElement(PROP_IS_CRITICAL + "=" + mCritical);
        params.addElement(PROP_NOT_BEFORE + "=" + mNotBefore);
        params.addElement(PROP_NOT_AFTER + "=" + mNotAfter);
        return params;
    }

    /**
     * Return default parameters for a policy implementation.
     * 
     * @return Empty Vector since this policy implementation has no
     *         configuration parameters.
     */
    public Vector<String> getDefaultParams() {
        Vector<String> defParams = new Vector<String>();

        defParams.addElement(PROP_IS_CRITICAL + "=" + DEFAULT_CRITICALITY);
        defParams.addElement(PROP_NOT_BEFORE + "=" + DEFAULT_NOT_BEFORE);
        defParams.addElement(PROP_NOT_AFTER + "=" + DEFAULT_NOT_AFTER);
        return defParams;
    }
}