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
|
// --- 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.constraints;
import java.util.*;
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.apps.CMS;
import com.netscape.certsrv.common.*;
import netscape.security.x509.*;
import com.netscape.cms.policy.APolicyRule;
/**
* KeyAlgorithmConstraints enforces a constraint that the RA or a CA
* honor only the keys generated using one of the permitted algorithms
* such as RSA, DSA or DH.
*
* @version $Revision: 14561 $, $Date: 2007-05-01 10:28:56 -0700 (Tue, 01 May 2007) $
*/
public class KeyAlgorithmConstraints extends APolicyRule
implements IEnrollmentPolicy, IExtendedPluginInfo {
private Vector mAlgorithms;
private final static String DEF_KEY_ALGORITHM = "RSA,DSA";
private final static String PROP_ALGORITHMS = "algorithms";
private final static String[] supportedAlgorithms =
{"RSA", "DSA", "DH" };
private final static Vector defConfParams = new Vector();
static {
defConfParams.addElement(PROP_ALGORITHMS + "=" +
DEF_KEY_ALGORITHM);
}
public String[] getExtendedPluginInfo(Locale locale) {
String params[] = {
"algorithms;choice(RSA\\,DSA,RSA,DSA);Certificate's key can be one of these algorithms",
IExtendedPluginInfo.HELP_TOKEN +
";configuration-policyrules-keyalgorithmconstraints",
IExtendedPluginInfo.HELP_TEXT +
";Rejects the request if the key in the certificate is " +
"not of the type specified"
};
return params;
}
public KeyAlgorithmConstraints() {
NAME = "KeyAlgorithmConstraints";
DESC = "Enforces Key Algorithm Constraints.";
}
/**
* Initializes this policy rule.
* <P>
*
* The entries probably are of the form
* ra.Policy.rule.<ruleName>.implName=KeyAlgorithmConstraints
* ra.Policy.rule.<ruleName>.algorithms=RSA,DSA
* ra.Policy.rule.<ruleName>.enable=true
* ra.Policy.rule.<ruleName>.predicate=ou==Sales
*
* @param config The config store reference
*/
public void init(ISubsystem owner, IConfigStore config)
throws EPolicyException {
mAlgorithms = new Vector();
if (config == null || config.size() == 0) {
mAlgorithms.addElement(DEF_KEY_ALGORITHM);
return;
}
// Get Algorithm names
String algNames = null;
try {
algNames = config.getString(PROP_ALGORITHMS, null);
} catch (Exception e) {
String[] params = {getInstanceName(), e.toString()};
throw new EPolicyException(
CMS.getUserMessage("CMS_POLICY_INVALID_POLICY_CONFIG", params));
}
if (algNames == null) {
mAlgorithms.addElement(DEF_KEY_ALGORITHM);
return;
}
StringTokenizer tok = new StringTokenizer(algNames, ",");
while (tok.hasMoreTokens()) {
String alg = tok.nextToken().trim().toUpperCase();
if (alg.length() == 0)
continue;
mAlgorithms.addElement(alg);
}
// Check if configured algorithms are supported.
for (Enumeration e = mAlgorithms.elements();
e.hasMoreElements();) {
int i;
String configuredAlg = (String) e.nextElement();
// See if it is a supported algorithm.
for (i = 0; i < supportedAlgorithms.length; i++) {
if (configuredAlg.equals(supportedAlgorithms[i]))
break;
}
// Did we not find it?
if (i == supportedAlgorithms.length)
throw new EPolicyException(
CMS.getUserMessage("CMS_POLICY_UNSUPPORTED_KEY_ALG",
getInstanceName(), configuredAlg));
}
}
/**
* 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 result = PolicyResult.ACCEPTED;
try {
// Get the certificate info from the request
// X509CertInfo certInfo[] = (X509CertInfo[])
// req.get(IRequest.CERT_INFO);
X509CertInfo certInfo[] = req.getExtDataInCertInfoArray(IRequest.CERT_INFO);
// We need to have a certificate info set
if (certInfo == null) {
setError(req, CMS.getUserMessage("CMS_POLICY_NO_CERT_INFO",
getInstanceName()), "");
return PolicyResult.REJECTED;
}
// Else check if the key algorithm is supported.
for (int i = 0; i < certInfo.length; i++) {
CertificateX509Key certKey = (CertificateX509Key)
certInfo[i].get(X509CertInfo.KEY);
X509Key key = (X509Key) certKey.get(CertificateX509Key.KEY);
String alg = key.getAlgorithmId().getName().toUpperCase();
if (!mAlgorithms.contains(alg)) {
setError(req, CMS.getUserMessage("CMS_POLICY_KEY_ALG_VIOLATION",
getInstanceName(), alg), "");
result = PolicyResult.REJECTED;
}
}
} catch (Exception e) {
String params[] = {getInstanceName(), e.toString()};
setError(req, CMS.getUserMessage("CMS_POLICY_UNEXPECTED_POLICY_ERROR",
params), "");
result = PolicyResult.REJECTED;
}
return result;
}
/**
* Return configured parameters for a policy rule instance.
*
* @return nvPairs A Vector of name/value pairs.
*/
public Vector getInstanceParams() {
Vector v = new Vector();
StringBuffer sb = new StringBuffer();
for (Enumeration e = mAlgorithms.elements(); e.hasMoreElements();) {
sb.append((String) e.nextElement());
sb.append(",");
}
if (sb.length() > 0)
sb.setLength(sb.length() - 1);
v.addElement(PROP_ALGORITHMS + "=" + sb.toString());
return v;
}
/**
* Return default parameters for a policy implementation.
*
* @return nvPairs A Vector of name/value pairs.
*/
public Vector getDefaultParams() {
return defConfParams;
}
}
|