summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cms/profile/constraint/KeyUsageExtConstraint.java
blob: ff87f9045ffedd96403d3c3019f4881823578d5a (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
// --- 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.util.*;
import java.io.*;
import com.netscape.certsrv.base.*;
import com.netscape.certsrv.profile.*;
import com.netscape.certsrv.request.*;
import com.netscape.certsrv.property.*;
import com.netscape.certsrv.apps.*;
import com.netscape.cms.profile.common.*;
import com.netscape.cms.profile.def.*;

import netscape.security.x509.*;


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

    public static final String CONFIG_CRITICAL = "keyUsageCritical";
    public static final String CONFIG_DIGITAL_SIGNATURE =
        "keyUsageDigitalSignature";
    public static final String CONFIG_NON_REPUDIATION =
        "keyUsageNonRepudiation";
    public static final String CONFIG_KEY_ENCIPHERMENT =
        "keyUsageKeyEncipherment";
    public static final String CONFIG_DATA_ENCIPHERMENT =
        "keyUsageDataEncipherment";
    public static final String CONFIG_KEY_AGREEMENT = "keyUsageKeyAgreement";
    public static final String CONFIG_KEY_CERTSIGN = "keyUsageKeyCertSign";
    public static final String CONFIG_CRL_SIGN = "keyUsageCrlSign";
    public static final String CONFIG_ENCIPHER_ONLY = "keyUsageEncipherOnly";
    public static final String CONFIG_DECIPHER_ONLY = "keyUsageDecipherOnly";

    public KeyUsageExtConstraint() {
        super();
        addConfigName(CONFIG_CRITICAL);
        addConfigName(CONFIG_DIGITAL_SIGNATURE);
        addConfigName(CONFIG_NON_REPUDIATION);
        addConfigName(CONFIG_KEY_ENCIPHERMENT);
        addConfigName(CONFIG_DATA_ENCIPHERMENT);
        addConfigName(CONFIG_KEY_AGREEMENT);
        addConfigName(CONFIG_KEY_CERTSIGN);
        addConfigName(CONFIG_CRL_SIGN);
        addConfigName(CONFIG_ENCIPHER_ONLY);
        addConfigName(CONFIG_DECIPHER_ONLY);
    }

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

    }

    public IDescriptor getConfigDescriptor(Locale locale, String name) { 
        if (name.equals(CONFIG_CRITICAL)) {
            return new Descriptor(IDescriptor.CHOICE, "true,false,-",
                    "-",
                    CMS.getUserMessage(locale, "CMS_PROFILE_CRITICAL"));
        } else if (name.equals(CONFIG_DIGITAL_SIGNATURE)) {
            return new Descriptor(IDescriptor.CHOICE, "true,false,-",
                    "-",
                    CMS.getUserMessage(locale, "CMS_PROFILE_DIGITAL_SIGNATURE"));
        } else if (name.equals(CONFIG_NON_REPUDIATION)) {
            return new Descriptor(IDescriptor.CHOICE, "true,false,-",
                    "-",
                    CMS.getUserMessage(locale, "CMS_PROFILE_NON_REPUDIATION"));
        } else if (name.equals(CONFIG_KEY_ENCIPHERMENT)) {
            return new Descriptor(IDescriptor.CHOICE, "true,false,-",
                    "-",
                    CMS.getUserMessage(locale, "CMS_PROFILE_KEY_ENCIPHERMENT"));
        } else if (name.equals(CONFIG_DATA_ENCIPHERMENT)) {
            return new Descriptor(IDescriptor.CHOICE, "true,false,-",
                    "-",
                    CMS.getUserMessage(locale, "CMS_PROFILE_DATA_ENCIPHERMENT"));
        } else if (name.equals(CONFIG_KEY_AGREEMENT)) {
            return new Descriptor(IDescriptor.CHOICE, "true,false,-",
                    "-",
                    CMS.getUserMessage(locale, "CMS_PROFILE_KEY_AGREEMENT"));
        } else if (name.equals(CONFIG_KEY_CERTSIGN)) {
            return new Descriptor(IDescriptor.CHOICE, "true,false,-",
                    "-",
                    CMS.getUserMessage(locale, "CMS_PROFILE_KEY_CERTSIGN"));
        } else if (name.equals(CONFIG_CRL_SIGN)) {
            return new Descriptor(IDescriptor.CHOICE, "true,false,-",
                    "-",
                    CMS.getUserMessage(locale, "CMS_PROFILE_CRL_SIGN"));
        } else if (name.equals(CONFIG_ENCIPHER_ONLY)) {
            return new Descriptor(IDescriptor.CHOICE, "true,false,-",
                    "-",
                    CMS.getUserMessage(locale, "CMS_PROFILE_ENCIPHER_ONLY"));
        } else if (name.equals(CONFIG_DECIPHER_ONLY)) {
            return new Descriptor(IDescriptor.CHOICE, "true,false,-",
                    "-",
                    CMS.getUserMessage(locale, "CMS_PROFILE_DECIPHER_ONLY"));
        }
        return null;
    }

    public boolean isSet(boolean bits[], int position) {
        if (bits.length <= position)
            return false;
        return bits[position];
    }

    /**
     * Validates the request. The request is not modified
     * during the validation.
     */
    public void validate(IRequest request, X509CertInfo info)
        throws ERejectException {
        KeyUsageExtension ext = (KeyUsageExtension) 
            getExtension(PKIXExtensions.KeyUsage_Id.toString(), info);

        if (ext == null) {
            throw new ERejectException(
                    CMS.getUserMessage(
                        getLocale(request),
                        "CMS_PROFILE_EXTENSION_NOT_FOUND",
                        PKIXExtensions.KeyUsage_Id.toString()));
        }

        boolean[] bits = ext.getBits();
        String value = getConfig(CONFIG_CRITICAL);

        if (!isOptional(value)) {
            boolean critical = getBoolean(value);

            if (critical != ext.isCritical()) { 
                throw new ERejectException( 
                        CMS.getUserMessage(getLocale(request), 
                            "CMS_PROFILE_CRITICAL_NOT_MATCHED"));
            }
        }
        value = getConfig(CONFIG_DIGITAL_SIGNATURE);
        if (!isOptional(value)) {
            boolean bit = getBoolean(value);

            if (bit != isSet(bits, 0)) {
                throw new ERejectException( 
                        CMS.getUserMessage(getLocale(request), 
                            "CMS_PROFILE_DIGITAL_SIGNATURE_NOT_MATCHED",
                            value));
            } 
        }
        value = getConfig(CONFIG_NON_REPUDIATION);
        if (!isOptional(value)) {
            boolean bit = getBoolean(value);

            if (bit != isSet(bits, 1)) {
                throw new ERejectException( 
                        CMS.getUserMessage(getLocale(request), 
                            "CMS_PROFILE_NON_REPUDIATION_NOT_MATCHED",
                            value));
            } 
        }
        value = getConfig(CONFIG_KEY_ENCIPHERMENT);
        if (!isOptional(value)) {
            boolean bit = getBoolean(value);

            if (bit != isSet(bits, 2)) {
                throw new ERejectException( 
                        CMS.getUserMessage(getLocale(request), 
                            "CMS_PROFILE_KEY_ENCIPHERMENT_NOT_MATCHED",
                            value));
            } 
        }
        value = getConfig(CONFIG_DATA_ENCIPHERMENT);
        if (!isOptional(value)) {
            boolean bit = getBoolean(value);

            if (bit != isSet(bits, 3)) {
                throw new ERejectException( 
                        CMS.getUserMessage(getLocale(request), 
                            "CMS_PROFILE_DATA_ENCIPHERMENT_NOT_MATCHED",
                            value));
            } 
        }
        value = getConfig(CONFIG_KEY_AGREEMENT);
        if (!isOptional(value)) {
            boolean bit = getBoolean(value);

            if (bit != isSet(bits, 4)) {
                throw new ERejectException( 
                        CMS.getUserMessage(getLocale(request), 
                            "CMS_PROFILE_KEY_AGREEMENT_NOT_MATCHED",
                            value));
            } 
        }
        value = getConfig(CONFIG_KEY_CERTSIGN);
        if (!isOptional(value)) {
            boolean bit = getBoolean(value);

            if (bit != isSet(bits, 5)) {
                throw new ERejectException( 
                        CMS.getUserMessage(getLocale(request), 
                            "CMS_PROFILE_KEY_CERTSIGN_NOT_MATCHED",
                            value));
            } 
        }
        value = getConfig(CONFIG_CRL_SIGN);
        if (!isOptional(value)) {
            boolean bit = getBoolean(value);

            if (bit != isSet(bits, 6)) {
                throw new ERejectException( 
                        CMS.getUserMessage(getLocale(request), 
                            "CMS_PROFILE_CRL_SIGN_NOT_MATCHED",
                            value));
            } 
        }
        value = getConfig(CONFIG_ENCIPHER_ONLY);
        if (!isOptional(value)) {
            boolean bit = getBoolean(value);

            if (bit != isSet(bits, 7)) {
                throw new ERejectException( 
                        CMS.getUserMessage(getLocale(request), 
                            "CMS_PROFILE_ENCIPHER_ONLY_NOT_MATCHED",
                            value));
            } 
        }
        value = getConfig(CONFIG_DECIPHER_ONLY);
        if (!isOptional(value)) {
            boolean bit = getBoolean(value);

            if (bit != isSet(bits, 8)) {
                throw new ERejectException( 
                        CMS.getUserMessage(getLocale(request), 
                            "CMS_PROFILE_DECIPHER_ONLY_NOT_MATCHED",
                            value));
            } 
        }
    }

    public String getText(Locale locale) {
        String params[] = {
                getConfig(CONFIG_CRITICAL),
                getConfig(CONFIG_DIGITAL_SIGNATURE),
                getConfig(CONFIG_NON_REPUDIATION),
                getConfig(CONFIG_KEY_ENCIPHERMENT),
                getConfig(CONFIG_DATA_ENCIPHERMENT),
                getConfig(CONFIG_KEY_AGREEMENT),
                getConfig(CONFIG_KEY_CERTSIGN),
                getConfig(CONFIG_CRL_SIGN),
                getConfig(CONFIG_ENCIPHER_ONLY),
                getConfig(CONFIG_DECIPHER_ONLY)
            };

        return CMS.getUserMessage(locale, 
                "CMS_PROFILE_CONSTRAINT_KEY_USAGE_EXT_TEXT", params);
    }

    public boolean isApplicable(IPolicyDefault def) {
        if (def instanceof NoDefault)
            return true;
        if (def instanceof KeyUsageExtDefault)
            return true;
        if (def instanceof UserExtensionDefault)
            return true;
        return false;
    }
}