summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/policy/extensions/SubjectAltNameExt.java
blob: eb7dba173fbc0a21d856019fda6301db8ee2e136 (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
// --- 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.apps.*;
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.authentication.*;
import com.netscape.certsrv.common.*;
import com.netscape.certsrv.logging.ILogger;
import netscape.security.x509.*;
import netscape.ldap.*;
import com.netscape.cms.policy.APolicyRule;


/**
 * Subject Alternative Name extension policy.
 *
 * Adds the subject alternative name extension as configured.
 *
 * Two forms are supported.  1) For S/MIME certificates, email
 * addresses are copied from data stored in the request by the
 * authentication component.  Both 'e' and 'altEmail' are supported
 * so that both the primary address and alternative forms may be
 * certified.  Only the primary goes in the subjectName position (which
 * should be phased out).
 *
 * e
 * mailAlternateAddress
 * <P>
 * <PRE>
 * NOTE:  The Policy Framework has been replaced by the Profile Framework.
 * </PRE>
 * <P>
 *
 * @deprecated
 * @version $Revision$, $Date$
 */
public class SubjectAltNameExt extends APolicyRule
    implements IEnrollmentPolicy, IExtendedPluginInfo {
    // (standard says SHOULD be marked critical if included.) 
    protected static final String PROP_CRITICAL = "critical";
    protected static final boolean DEF_CRITICAL = false;

    protected IConfigStore mConfig = null;
    protected boolean mEnabled = false;
    protected boolean mCritical = DEF_CRITICAL;
    protected int mNumGNs = 0;
    protected ISubjAltNameConfig[] mGNs = null;

    Vector mInstanceParams = new Vector();

    // init default params and extended plugin info.
    private static Vector mDefParams = new Vector();
    static {
        // default params.
        mDefParams.addElement(PROP_CRITICAL + "=" + DEF_CRITICAL);
        mDefParams.addElement(
            IGeneralNameUtil.PROP_NUM_GENERALNAMES + "=" + 
            IGeneralNameUtil.DEF_NUM_GENERALNAMES);
        for (int i = 0; i < IGeneralNameUtil.DEF_NUM_GENERALNAMES; i++) {
            CMS.getSubjAltNameConfigDefaultParams(
                IGeneralNameUtil.PROP_GENERALNAME + i, mDefParams);
        }
    }

    private String[] mExtendedPluginInfo = null;

    public SubjectAltNameExt() {
        NAME = "SubjectAltNameExt";
        DESC = "Sets alternative subject names for certificates";
    }

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

        // get criticality
        mCritical = mConfig.getBoolean(PROP_CRITICAL, DEF_CRITICAL);

        // get enabled
        mEnabled = mConfig.getBoolean(
                    IPolicyProcessor.PROP_ENABLE, false);

        // get general names configuration.
        mNumGNs = mConfig.getInteger(IGeneralNameUtil.PROP_NUM_GENERALNAMES); 
        if (mNumGNs <= 0) {
            throw new EBaseException(
                    CMS.getUserMessage("CMS_BASE_MUST_BE_POSITIVE_NUMBER", 
                        IGeneralNameUtil.PROP_NUM_GENERALNAMES));
        }
        mGNs = new ISubjAltNameConfig[mNumGNs];
        for (int i = 0; i < mNumGNs; i++) {
            String name = IGeneralNameUtil.PROP_GENERALNAME + i;
            IConfigStore substore = mConfig.getSubStore(name);

            mGNs[i] = CMS.createSubjAltNameConfig(name, substore, mEnabled);
        }

        // init instance params.
        mInstanceParams.addElement(PROP_CRITICAL + "=" + mCritical);
        mInstanceParams.addElement(
            IGeneralNameUtil.PROP_NUM_GENERALNAMES + "=" + mNumGNs);
        for (int j = 0; j < mGNs.length; j++) {
            mGNs[j].getInstanceParams(mInstanceParams);
        }
    }

    /**
     * Adds the subject alternative names extension if not set already.
     *
     * <P>
     *
     * @param req	The request on which to apply policy.
     * @return The policy result object.
     */
    public PolicyResult apply(IRequest req) {
        PolicyResult res = PolicyResult.ACCEPTED;

        // Find the X509CertInfo object in the 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; // unrecoverable error.
        }

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

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

    public PolicyResult applyCert(IRequest req, X509CertInfo certInfo) {
        PolicyResult res = PolicyResult.ACCEPTED;

        try {
            // Find the extensions in the certInfo
            CertificateExtensions extensions = (CertificateExtensions)
                certInfo.get(X509CertInfo.EXTENSIONS);

            // Remove any previously computed version of the extension
            // unless it is from RA. If from RA, accept what RA put in 
            // request and don't add our own.
            if (extensions != null) {
                String sourceId = req.getSourceId();

                if (sourceId != null && sourceId.length() > 0) 
                    return res; // accepted
                try {
                    extensions.delete(SubjectAlternativeNameExtension.NAME);
                } catch (IOException e) {
                    // extension isn't there
                }
            }

            // form list of general names for the extension.
            GeneralNames gns = new GeneralNames();

            for (int i = 0; i < mNumGNs; i++) {
                Object value = null;

                value = req.getExtDataInString(mGNs[i].getPfx(), mGNs[i].getAttr());
                if (value == null) {
                    continue;
                }
                Vector gn = mGNs[i].formGeneralNames(value);

                if (gn.size() == 0)
                    continue;
                for (Enumeration n = gn.elements(); n.hasMoreElements();) {
                    gns.addElement((GeneralName) n.nextElement());
                }
            }

            // nothing was found in request to put into extension
            if (gns.size() == 0) 
                return res;	// accepted

            String subject = certInfo.get(X509CertInfo.SUBJECT).toString();

            boolean curCritical = mCritical;

            if (subject.equals("")) {
                curCritical = true;
            }
            
            // make the extension 
            SubjectAlternativeNameExtension 
                sa = new SubjectAlternativeNameExtension(curCritical, gns);

            // add it to certInfo.
            if (extensions == null)
                extensions = createCertificateExtensions(certInfo);

            extensions.set(SubjectAlternativeNameExtension.NAME, sa);

            return res; // accepted.

        } catch (IOException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("BASE_IO_ERROR", e.getMessage()));
            setError(req, CMS.getUserMessage("CMS_POLICY_UNEXPECTED_POLICY_ERROR"), 
                NAME, e.getMessage());
            return PolicyResult.REJECTED; // unrecoverable error.
        } catch (CertificateException e) {
            log(ILogger.LL_FAILURE, CMS.getLogMessage("CA_CERT_INFO_ERROR", e.getMessage()));
            setError(req, CMS.getUserMessage("CMS_POLICY_UNEXPECTED_POLICY_ERROR"), 
                NAME, "Certificate Info Error");
            return PolicyResult.REJECTED; // unrecoverable error.
        } catch (Exception e) {
            log(ILogger.LL_FAILURE, 
                CMS.getLogMessage("BASE_INTERNAL_ERROR_1", e.getMessage()));
            setError(req, CMS.getUserMessage("CMS_POLICY_UNEXPECTED_POLICY_ERROR"), 
                NAME, "Internal Error");
            return PolicyResult.REJECTED; // unrecoverable error.
        }
    }

    /**
     * Create a new SET of extensions in the certificate info
     * object.
     *
     * This should be a method in the X509CertInfo object
     */
    protected CertificateExtensions 
    createCertificateExtensions(X509CertInfo certInfo)
        throws IOException, CertificateException {
        CertificateExtensions extensions;

        // Force version to V3
        certInfo.set(X509CertInfo.VERSION, 
            new CertificateVersion(CertificateVersion.V3));

        extensions = new CertificateExtensions();
        certInfo.set(X509CertInfo.EXTENSIONS, extensions);

        return extensions;
    }

    /**
     * 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 mDefParams;
    }

    public String[] getExtendedPluginInfo(Locale locale) {

        // extended plugin info.
        Vector info = new Vector();

        info.addElement(PROP_CRITICAL + ";boolean;RFC2459 recommendation: If the certificate subject field contains an empty sequence, the extension MUST be marked critical.");
        info.addElement(IGeneralNameUtil.PROP_NUM_GENERALNAMES_INFO);
        for (int i = 0; i < IGeneralNameUtil.DEF_NUM_GENERALNAMES; i++) {
            CMS.getSubjAltNameConfigExtendedPluginInfo(
                IGeneralNameUtil.PROP_GENERALNAME + i, info);
        }
        info.addElement(IExtendedPluginInfo.HELP_TOKEN +
            ";configuration-policyrules-subjaltname");
        info.addElement(IExtendedPluginInfo.HELP_TEXT +
            ";This policy inserts the Subject Alternative Name " +
            "Extension into the certificate. See RFC 2459 (4.2.1.7). " +
            "* Note: you probably want to use this policy in " +
            "conjunction with an authentication manager which sets " +
            "the 'mail' or 'mailalternateaddress' values in the authToken. " +
            "See the 'ldapStringAttrs' parameter in the Directory-based " +
            "authentication plugin");
        mExtendedPluginInfo = new String[info.size()];
        info.copyInto(mExtendedPluginInfo);
        return mExtendedPluginInfo;
    }

}