summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/policy/extensions/SubjectKeyIdentifierExt.java
blob: a8069be29cbf628e72b5763e6f6324c7555e6491 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// --- 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.request.IRequest;
import com.netscape.certsrv.request.PolicyResult;
import com.netscape.certsrv.policy.*;
import com.netscape.certsrv.base.*;
import com.netscape.certsrv.ca.*;
import com.netscape.certsrv.logging.ILogger;
import com.netscape.certsrv.apps.*;
import netscape.security.x509.*;
import netscape.security.util.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import com.netscape.cms.policy.APolicyRule;


/**
 * Subject Public Key Extension Policy
 * Adds the subject public key id extension to certificates. 
 * <P>
 * <PRE>
 * NOTE:  The Policy Framework has been replaced by the Profile Framework.
 * </PRE>
 * <P>
 *
 * @deprecated
 * @version $Revision$, $Date$
 */
public class SubjectKeyIdentifierExt extends APolicyRule
    implements IEnrollmentPolicy, IExtendedPluginInfo {
    protected static final String PROP_CRITICAL = "critical";
    protected static final String PROP_KEYID_TYPE = "keyIdentifierType";
    protected static final String PROP_REQATTR_NAME = "requestAttrName";

    protected static final String KEYID_TYPE_SHA1 = "SHA1";
    protected static final String KEYID_TYPE_TYPEFIELD = "TypeField";
    protected static final String KEYID_TYPE_SPKISHA1 = "SpkiSHA1";
    protected static final String KEYID_TYPE_REQATTR = "RequestAttribute";

    protected static final boolean DEF_CRITICAL = false;
    protected static final String DEF_KEYID_TYPE = KEYID_TYPE_SHA1;
    protected static final String DEF_REQATTR_NAME = "KeyIdentifier";

    protected boolean mEnabled = false;
    protected IConfigStore mConfig = null;

    protected boolean mCritical = DEF_CRITICAL;
    protected String mKeyIdType = DEF_KEYID_TYPE;;
    protected String mReqAttrName = DEF_REQATTR_NAME;

    protected Vector mInstanceParams = new Vector();

    protected static Vector mDefaultParams = new Vector();
    static {
        // form static default params.
        mDefaultParams.addElement(PROP_CRITICAL + "=" + DEF_CRITICAL);
        mDefaultParams.addElement(PROP_KEYID_TYPE + "=" + DEF_KEYID_TYPE);

        /*
         mDefaultParams.addElement(PROP_REQATTR_NAME+"="+DEF_REQATTR_NAME);
         */
    }

    public SubjectKeyIdentifierExt() {
        NAME = "SubjectKeyIdentifierExt";
        DESC = "Adds Subject Key Idenifier Extension to certs";
    }

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

        mEnabled = mConfig.getBoolean(
                    IPolicyProcessor.PROP_ENABLE, false);
        mCritical = mConfig.getBoolean(PROP_CRITICAL, DEF_CRITICAL);

        mKeyIdType = mConfig.getString(PROP_KEYID_TYPE, DEF_KEYID_TYPE);

        /*
         mReqAttrName = mConfig.getString(PROP_REQATTR_NAME, DEF_REQATTR_NAME);
         */

        // parse key id type
        if (mKeyIdType.equalsIgnoreCase(KEYID_TYPE_SHA1)) 
            mKeyIdType = KEYID_TYPE_SHA1;
        else if (mKeyIdType.equalsIgnoreCase(KEYID_TYPE_TYPEFIELD)) 
            mKeyIdType = KEYID_TYPE_TYPEFIELD;

            /*
             else if (mKeyIdType.equalsIgnoreCase(KEYID_TYPE_REQATTR)
             mKeyIdType = KEYID_TYPE_REQATTR;
             */
        else if (mKeyIdType.equalsIgnoreCase(KEYID_TYPE_SPKISHA1)) 
            mKeyIdType = KEYID_TYPE_SPKISHA1;
        else {
            log(ILogger.LL_FAILURE, 
                CMS.getLogMessage("KRA_UNKNOWN_KEY_ID_TYPE", mKeyIdType));
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTR_VALUE", 
                        PROP_KEYID_TYPE, 
                        "value must be one of " +
                        KEYID_TYPE_SHA1 + ", " +
                        KEYID_TYPE_TYPEFIELD + ", " +
                        KEYID_TYPE_SPKISHA1));
        }

        // form instance params 
        mInstanceParams.addElement(PROP_CRITICAL + "=" + mCritical);
        mInstanceParams.addElement(PROP_KEYID_TYPE + "=" + mKeyIdType);

        /*
         mInstanceParams.addElement(PROP_REQATTR_NAME+"="+mReqAttrName);
         */
    }

    /**
     * Adds Subject Key identifier Extension to a certificate.
     * If the extension is already there, accept it.
     *
     * @param req	The request on which to apply policy.
     * @return The policy result object.
     */
    public PolicyResult apply(IRequest req) {
        // get certInfo from 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; 
        }

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

        try {
            // if subject key id extension already exists, leave it if approved.
            SubjectKeyIdentifierExtension subjectKeyIdExt = null;
            CertificateExtensions extensions = (CertificateExtensions)
                certInfo.get(X509CertInfo.EXTENSIONS);

            try {
                if (extensions != null) {
                    subjectKeyIdExt = (SubjectKeyIdentifierExtension)
                            extensions.get(SubjectKeyIdentifierExtension.NAME);
                }
            } catch (IOException e) {
                // extension isn't there. 
            }
            if (subjectKeyIdExt != null) {
                if (agentApproved(req)) {
                    CMS.debug(
                        "SubjectKeyIdentifierExt: agent approved request id " + req.getRequestId() +
                        " already has subject key id extension with value " +
                        subjectKeyIdExt);
                    return PolicyResult.ACCEPTED;
                } else {
                    CMS.debug(
                        "SubjectKeyIdentifierExt: request id from user " + req.getRequestId() +
                        " had subject key identifier - deleted to be replaced");
                    extensions.delete(SubjectKeyIdentifierExtension.NAME);
                }
            }

            // create subject key id extension.
            KeyIdentifier keyId = null;

            try { 
                keyId = formKeyIdentifier(certInfo, req); 
            } catch (EBaseException e) {
                setPolicyException(req, e);
                return PolicyResult.REJECTED;
            }
            subjectKeyIdExt = 
                    new SubjectKeyIdentifierExtension(
                        mCritical, keyId.getIdentifier());

            // add subject key id extension.
            if (extensions == null) {
                certInfo.set(X509CertInfo.VERSION,
                    new CertificateVersion(CertificateVersion.V3));
                extensions = new CertificateExtensions();
                certInfo.set(X509CertInfo.EXTENSIONS, extensions);
            }
            extensions.set(
                SubjectKeyIdentifierExtension.NAME, subjectKeyIdExt);
            CMS.debug(
                "SubjectKeyIdentifierExt: added subject key id ext to request " + req.getRequestId());
            return PolicyResult.ACCEPTED;
        } catch (IOException e) {
            log(ILogger.LL_FAILURE, 
                CMS.getLogMessage("POLICY_UNEXPECTED_POLICY_ERROR,NAME", e.getMessage()));
            setError(req, CMS.getUserMessage("CMS_POLICY_UNEXPECTED_POLICY_ERROR"), 
                NAME, e.getMessage());
            return PolicyResult.REJECTED;
        } 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;
        }
    }

    /**
     * Form the Key Identifier in the Subject Key Identifier extension.
     * <p>
     * @param certInfo Certificate Info
     * @param req request
     * @return A Key Identifier.
     */
    protected KeyIdentifier formKeyIdentifier(
        X509CertInfo certInfo, IRequest req) throws EBaseException {
        KeyIdentifier keyId = null;

        if (mKeyIdType == KEYID_TYPE_SHA1) {
            keyId = formSHA1KeyId(certInfo);
        } else if (mKeyIdType == KEYID_TYPE_TYPEFIELD) {
            keyId = formTypeFieldKeyId(certInfo);
        } /*
         else if (mKeyIdType == KEYID_TYPE_REQATTR) {
         keyId = formReqAttrKeyId(certInfo, req);
         }
         */ else if (mKeyIdType == KEYID_TYPE_SPKISHA1) {
            keyId = formSpkiSHA1KeyId(certInfo);
        } else {
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTR_VALUE",
                        mKeyIdType, "Unknown Key Identifier type."));
        }
        return keyId;
    }

    /**
     * Form key identifier from a type field value of 0100 followed by 
     * the least significate 60 bits of the sha-1 hash of the subject 
     * public key BIT STRING in accordance with RFC 2459. 
     * <p>
     * @param certInfo - certificate info
     * @return A Key Identifier with value formulatd as described.
     */

    protected KeyIdentifier formTypeFieldKeyId(X509CertInfo certInfo)
        throws EBaseException {
        KeyIdentifier keyId = null;
        X509Key key = null;

        try {
            CertificateX509Key certKey =
                (CertificateX509Key) certInfo.get(X509CertInfo.KEY);

            if (certKey == null) {
                log(ILogger.LL_FAILURE, CMS.getLogMessage("POLICY_MISSING_KEY_1", NAME));
                throw new EPolicyException(CMS.getUserMessage("CMS_POLICY_MISSING_KEY", NAME));
            }
            key = (X509Key) certKey.get(CertificateX509Key.KEY);
            if (key == null) {
                log(ILogger.LL_FAILURE, CMS.getLogMessage("POLICY_MISSING_KEY_1", NAME));
                throw new EPolicyException(CMS.getUserMessage("CMS_POLICY_MISSING_KEY", NAME));
            }
        } catch (IOException e) {
            log(ILogger.LL_FAILURE, 
                CMS.getLogMessage("POLICY_ERROR_GET_KEY_FROM_CERT", e.toString()));
            throw new EPolicyException(
                    CMS.getUserMessage("CMS_POLICY_SUBJECT_KEY_ID_ERROR", NAME));
        } catch (CertificateException e) {
            log(ILogger.LL_FAILURE, 
                CMS.getLogMessage("POLICY_ERROR_GET_KEY_FROM_CERT", e.toString()));
            throw new EPolicyException(
                    CMS.getUserMessage("CMS_POLICY_SUBJECT_KEY_ID_ERROR", NAME));
        }
        try {
            byte[] octetString = new byte[8];
            MessageDigest md = MessageDigest.getInstance("SHA-1");

            md.update(key.getKey());
            byte[] hash = md.digest();

            System.arraycopy(hash, hash.length - 8, octetString, 0, 8);
            octetString[0] &= (0x08f & octetString[0]);
            keyId = new KeyIdentifier(octetString);
        } catch (NoSuchAlgorithmException e) {
            log(ILogger.LL_FAILURE, 
                CMS.getLogMessage("POLICY_ERROR_SUBJECT_KEY_ID_1", NAME));
            throw new EPolicyException(
                    CMS.getUserMessage("CMS_POLICY_SUBJECT_KEY_ID_ERROR", NAME));
        }
        return keyId;
    }

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

    /**
     * Gets extended plugin info for pretty Console displays. 
     */
    public String[] getExtendedPluginInfo(Locale locale) {
        String[] params = {
                PROP_CRITICAL + ";boolean;RFC 2459 recommendation: MUST NOT be marked critical.",
                PROP_KEYID_TYPE + ";" +
                "choice(" + KEYID_TYPE_SHA1 + "," +
                KEYID_TYPE_TYPEFIELD + "," +
                KEYID_TYPE_SPKISHA1 + ");" +
                "Method to derive the Key Identifier.",
                IExtendedPluginInfo.HELP_TOKEN +
                ";configuration-policyrules-subjectkeyidentifier",
                IExtendedPluginInfo.HELP_TEXT +
                ";Adds the Subject Key Identifier extension. See RFC 2459 (4.2.1.2)"
            };

        return params;
    }
}