-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathPBKDF2Realm.java
More file actions
229 lines (211 loc) · 8.21 KB
/
PBKDF2Realm.java
File metadata and controls
229 lines (211 loc) · 8.21 KB
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
/**
* ***********************************************************************
*
* Version Date: January 8, 2018
*
* Contributor(s):
* C. Heazel (WiSC): Applied modifications to address Fortify issues
*
* ***********************************************************************
*/
package com.occamlab.te.realm;
import java.io.File;
import java.lang.reflect.Constructor;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.catalina.Realm;
import org.apache.catalina.realm.GenericPrincipal;
import org.apache.catalina.realm.RealmBase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.occamlab.te.realm.PasswordStorage.CannotPerformOperationException;
import com.occamlab.te.realm.PasswordStorage.InvalidHashException;
/**
* A custom Tomcat Realm implementation that reads user information from an XML
* file located in a sub-directory of the TEAMengine users directory. A sample
* representation is shown below.
*
* <pre>
* <user>
* <name>p.fogg</name>
* <roles>
* <name>user</name>
* </roles>
* <password>password-digest</password>
* <email>p.fogg@example.org</email>
* </user>
* </pre>
* <p>
* The password digest must be generated using the {@link PasswordStorage
* PBKDF2} function; it consists of five fields separated by the colon (':')
* character. For example:
* <code>sha1:64000:18:a6BHX18eMTR1WnCvyR6NzG6VMJcdJE2D:8qPU0jpdPIapbyC+H5dqiaNE</code>
* </p>
*
* @see <a href="https://github.com/defuse/password-hashing">Secure Password
* Storage v2.0</a>
*/
public class PBKDF2Realm extends RealmBase {
private static final Logger LOGR = Logger.getLogger(PBKDF2Realm.class.getName());
private String rootPath = null;
private DocumentBuilder DB = null;
private HashMap<String, Principal> principals = UserGenericPrincipal.getInstance().getPrincipals();
public String getRoot() {
return rootPath;
}
/**
* Return the Principal associated with the specified username and
* credentials, if one exists in the user data store; otherwise return null.
*/
@Override
public Principal authenticate(String username, String credentials) {
GenericPrincipal principal = (GenericPrincipal) getPrincipal(username);
if (null != principal) {
try {
if (!PasswordStorage.verifyPassword(credentials, principal.getPassword())) {
principal = null;
}
} catch (CannotPerformOperationException | InvalidHashException e) {
LOGR.log(Level.WARNING, e.getMessage());
principal = null;
}
}
return principal;
}
@Override
protected String getPassword(String username) {
GenericPrincipal principal = (GenericPrincipal) getPrincipal(username);
if (principal == null) {
return null;
} else {
return principal.getPassword();
}
}
/**
* Return the Principal associated with the given user name. If the user
* name starts with an asterisk (*), the credentials are refreshed without
* having to restart Tomcat.
*/
@Override
protected Principal getPrincipal(String username) {
Principal principal;
// force lookup of user info
if (username.startsWith("*")) {
principal = readPrincipal(username.substring(1));
if (principal != null) {
synchronized (principals) {
principals.put(username.substring(1), principal);
}
}
}
synchronized (principals) {
principal = (Principal) principals.get(username);
}
if (principal == null) {
principal = readPrincipal(username);
if (principal != null) {
synchronized (principals) {
principals.put(username, principal);
}
}
}
return principal;
}
@Override
protected String getName() {
return "UserFilesRealm";
}
/**
* Sets the location of the root users directory. This is specified by the
* "root" attribute of the Realm element in the context definition.
*
* @param root
* A String specifying a directory location (TE_BASE/users).
*/
public void setRoot(String root) {
rootPath = root;
}
private GenericPrincipal readPrincipal(String username) {
List<String> roles = new ArrayList<String>();
File usersdir = new File(rootPath);
if (!usersdir.isDirectory()) {
usersdir = new File(System.getProperty("TE_BASE"), "users");
}
File userfile = new File(new File(usersdir, username), "user.xml");
if (!userfile.canRead()) {
return null;
}
Document userInfo = null;
try {
if (DB == null) {
DB = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}
userInfo = DB.parse(userfile);
} catch (Exception e) {
LOGR.log(Level.WARNING, "Failed to read user info at " + userfile.getAbsolutePath(), e);
// fortify Mod: If the user info was not read, then there is no point in continuing
return null;
}
Element userElement = (Element) (userInfo.getElementsByTagName("user").item(0));
Element passwordElement = (Element) (userElement.getElementsByTagName("password").item(0));
String password = passwordElement.getTextContent();
Element rolesElement = (Element) (userElement.getElementsByTagName("roles").item(0));
NodeList roleElements = rolesElement.getElementsByTagName("name");
for (int i = 0; i < roleElements.getLength(); i++) {
String name = ((Element) roleElements.item(i)).getTextContent();
roles.add(name);
}
GenericPrincipal principal = createGenericPrincipal(username, password, roles);
return principal;
}
/**
* Creates a new GenericPrincipal representing the specified user.
*
* @param username
* The username for this user.
* @param password
* The authentication credentials for this user.
* @param roles
* The set of roles (specified using String values) associated
* with this user.
* @return A GenericPrincipal for use by this Realm implementation.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
GenericPrincipal createGenericPrincipal(String username, String password, List<String> roles) {
Class klass = null;
try {
klass = Class.forName("org.apache.catalina.realm.GenericPrincipal");
} catch (ClassNotFoundException ex) {
LOGR.log(Level.SEVERE, ex.getMessage());
// Fortify Mod: If klass is not populated, then there is no point in continuing
return null;
}
Constructor[] ctors = klass.getConstructors();
Class firstParamType = ctors[0].getParameterTypes()[0];
Class[] paramTypes = new Class[] { Realm.class, String.class, String.class, List.class };
Object[] ctorArgs = new Object[] { this, username, password, roles };
GenericPrincipal principal = null;
try {
if (Realm.class.isAssignableFrom(firstParamType)) {
// Tomcat 6
Constructor ctor = klass.getConstructor(paramTypes);
principal = (GenericPrincipal) ctor.newInstance(ctorArgs);
} else {
// Realm parameter removed in Tomcat 7
Constructor ctor = klass.getConstructor(Arrays.copyOfRange(paramTypes, 1, paramTypes.length));
principal = (GenericPrincipal) ctor.newInstance(Arrays.copyOfRange(ctorArgs, 1, ctorArgs.length));
}
} catch (Exception ex) {
LOGR.log(Level.WARNING, ex.getMessage());
}
return principal;
}
}