summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/policy/extensions/NSCCommentExt.java
blob: f3ae4efa4d5472c3559f0045ec00a2284b7652b6 (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
// --- 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.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.util.Locale;
import java.util.Vector;

import netscape.security.x509.CertificateExtensions;
import netscape.security.x509.CertificateVersion;
import netscape.security.x509.DisplayText;
import netscape.security.x509.NSCCommentExtension;
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;

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

    protected static final String PROP_USER_NOTICE_DISPLAY_TEXT = "displayText";
    protected static final String PROP_COMMENT_FILE = "commentFile";
    protected static final String PROP_CRITICAL = "critical";
    protected static final String PROP_INPUT_TYPE = "inputType";
    protected static final String TEXT = "Text";
    protected static final String FILE = "File";

    protected String mUserNoticeDisplayText;
    protected String mCommentFile;
    protected String mInputType;
    protected boolean mCritical;
    private Vector<String> mParams = new Vector<String>();

    protected String tempCommentFile;
    protected boolean certApplied = false;

    /**
     * Adds the Netscape comment in the end-entity certificates or
     * CA certificates. The policy is set to be non-critical with the
     * provided OID.
     */
    public NSCCommentExt() {
        NAME = "NSCCommentExt";
        DESC = "Sets non-critical Netscape Comment extension in certs";
    }

    /**
     * Initializes this policy rule.
     * <p>
     * The entries may be of the form:
     * 
     * ca.Policy.rule.<ruleName>.implName=NSCCommentExtImpl ca.Policy.rule.<ruleName>.displayText=<n>
     * ca.Policy.rule.<ruleName>.commentFile=<n> ca.Policy.rule.<ruleName>.enable=false
     * 
     * @param config The config store reference
     */
    public void init(ISubsystem owner, IConfigStore config)
            throws EBaseException {

        FileInputStream fileStream = null;

        try {
            mCritical = config.getBoolean(PROP_CRITICAL, false);
            mParams.addElement(PROP_CRITICAL + "=" + mCritical);

            mInputType = config.getString(PROP_INPUT_TYPE, null);
            mParams.addElement(PROP_INPUT_TYPE + "=" + mInputType);

            mUserNoticeDisplayText = config.getString(PROP_USER_NOTICE_DISPLAY_TEXT, "");
            mParams.addElement(PROP_USER_NOTICE_DISPLAY_TEXT + "=" + mUserNoticeDisplayText);

            tempCommentFile = config.getString(PROP_COMMENT_FILE, "");

            boolean enable = config.getBoolean(PROP_ENABLE, false);

            if ((enable == true)) {

                if (mInputType.equals("File")) {
                    if (tempCommentFile.equals(""))
                        throw new Exception("No file name provided");

                    fileStream = new FileInputStream(tempCommentFile);
                    fileStream.close();
                }
            }

            if (tempCommentFile.equals(""))
                mCommentFile = "";
            else
                mCommentFile = tempCommentFile.replace('\\', '/');

            config.putString(PROP_COMMENT_FILE, mCommentFile);

            mParams.addElement(PROP_COMMENT_FILE + "=" + mCommentFile);
        } catch (FileNotFoundException e) {
            Object[] params = { getInstanceName(), "File not found : " + tempCommentFile };

            throw new EPolicyException(CMS.getUserMessage("CMS_POLICY_INVALID_POLICY_CONFIG"), params);
        } catch (Exception e) {
            Object[] params = { getInstanceName(), e.getMessage() };

            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 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 r = applyCert(req, ci[i]);

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

    public PolicyResult applyCert(IRequest req, X509CertInfo certInfo) {

        certApplied = false;
        CertificateExtensions extensions = null;

        try {
            extensions = (CertificateExtensions)
                    certInfo.get(X509CertInfo.EXTENSIONS);
        } catch (IOException e) {
        } catch (CertificateException e) {
        }

        if (extensions == null) {
            extensions = new CertificateExtensions();
            try {
                certInfo.set(X509CertInfo.VERSION,
                        new CertificateVersion(CertificateVersion.V3));
                certInfo.set(X509CertInfo.EXTENSIONS, extensions);
            } catch (Exception e) {
            }
        } else {
            // remove any previously computed version of the extension
            try {
                extensions.delete(NSCCommentExtension.NAME);

            } catch (IOException e) {
                // this is the hack: for some reason, the key which is the name
                // of the policy has been converted into the OID 
                try {
                    extensions.delete("2.16.840.1.113730.1.13");
                } catch (IOException ee) {
                }
            }
        }
        if (mInputType.equals("File")) {
            //		if ((mUserNoticeDisplayText.equals("")) && !(mCommentFile.equals(""))) {
            try {
                // Read the comments file
                BufferedReader fis = new BufferedReader(new FileReader(mCommentFile));

                String line = null;
                StringBuffer buffer = new StringBuffer();

                while ((line = fis.readLine()) != null)
                    buffer.append(line);
                mUserNoticeDisplayText = new String(buffer);
                fis.close();
            } catch (IOException e) {
                setError(req, CMS.getUserMessage("CMS_POLICY_UNEXPECTED_POLICY_ERROR"),
                        NAME, " Comment Text file not found : " + mCommentFile);
                log(ILogger.LL_FAILURE,
                        CMS.getLogMessage("POLICY_COMMENT_FILE_NOT_FOUND", e.toString()));
                return PolicyResult.REJECTED;

            }

        }

        certApplied = true;

        DisplayText displayText =
                new DisplayText(DisplayText.tag_IA5String, mUserNoticeDisplayText);

        try {
            NSCCommentExtension cpExt =
                    new NSCCommentExtension(mCritical, mUserNoticeDisplayText);

            extensions.set(NSCCommentExtension.NAME, cpExt);
        } catch (Exception e) {
            log(ILogger.LL_FAILURE,
                    CMS.getLogMessage("POLICY_ERROR_CERTIFICATE_POLICIES_1", NAME));
            setError(req,
                    CMS.getUserMessage("CMS_POLICY_CERTIFICATE_POLICIES_ERROR"), NAME);
            return PolicyResult.REJECTED;
        }
        return PolicyResult.ACCEPTED;
    }

    public String[] getExtendedPluginInfo(Locale locale) {
        String[] params = {
                PROP_CRITICAL + ";boolean;Netscape recommendation: non-critical.",
                PROP_INPUT_TYPE + ";choice(Text,File);Whether the comments " +
                        "would be entered in the displayText field or come from " +
                        "a file.",
                PROP_USER_NOTICE_DISPLAY_TEXT + ";string;The comment that may be " +
                        "displayed to the user when the certificate is viewed.",
                PROP_COMMENT_FILE + ";string; If data source is 'File', specify " +
                        "the file name with full path.",
                IExtendedPluginInfo.HELP_TOKEN +
                        ";configuration-policyrules-nsccomment",
                IExtendedPluginInfo.HELP_TEXT +
                        ";Adds 'netscape comment' extension. See manual"
            };

        return params;

    }

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

    /**
     * Return default parameters for a policy implementation.
     * 
     * @return nvPairs A Vector of name/value pairs.
     */
    public Vector<String> getDefaultParams() {
        Vector<String> defParams = new Vector<String>();

        defParams.addElement(PROP_CRITICAL + "=false");
        defParams.addElement(PROP_INPUT_TYPE + "=" + TEXT);
        defParams.addElement(PROP_USER_NOTICE_DISPLAY_TEXT + "=");
        defParams.addElement(PROP_COMMENT_FILE + "=");
        return defParams;
    }
}