1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.http.security;
15
16 import java.io.Serializable;
17 import java.security.MessageDigest;
18
19 import org.eclipse.jetty.util.StringUtil;
20 import org.eclipse.jetty.util.TypeUtil;
21 import org.eclipse.jetty.util.log.Log;
22 import org.eclipse.jetty.util.log.Logger;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public abstract class Credential implements Serializable
40 {
41 private static final Logger LOG = Log.getLogger(Credential.class);
42
43 private static final long serialVersionUID = -7760551052768181572L;
44
45
46
47
48
49
50
51
52
53
54
55 public abstract boolean check(Object credentials);
56
57
58
59
60
61
62
63
64
65
66 public static Credential getCredential(String credential)
67 {
68 if (credential.startsWith(Crypt.__TYPE)) return new Crypt(credential);
69 if (credential.startsWith(MD5.__TYPE)) return new MD5(credential);
70
71 return new Password(credential);
72 }
73
74
75
76
77
78 public static class Crypt extends Credential
79 {
80 private static final long serialVersionUID = -2027792997664744210L;
81
82 public static final String __TYPE = "CRYPT:";
83
84 private final String _cooked;
85
86 Crypt(String cooked)
87 {
88 _cooked = cooked.startsWith(Crypt.__TYPE) ? cooked.substring(__TYPE.length()) : cooked;
89 }
90
91 @Override
92 public boolean check(Object credentials)
93 {
94 if (credentials instanceof char[])
95 credentials=new String((char[])credentials);
96 if (!(credentials instanceof String) && !(credentials instanceof Password))
97 LOG.warn("Can't check " + credentials.getClass() + " against CRYPT");
98
99 String passwd = credentials.toString();
100 return _cooked.equals(UnixCrypt.crypt(passwd, _cooked));
101 }
102
103 public static String crypt(String user, String pw)
104 {
105 return "CRYPT:" + UnixCrypt.crypt(pw, user);
106 }
107 }
108
109
110
111
112
113 public static class MD5 extends Credential
114 {
115 private static final long serialVersionUID = 5533846540822684240L;
116
117 public static final String __TYPE = "MD5:";
118
119 public static final Object __md5Lock = new Object();
120
121 private static MessageDigest __md;
122
123 private final byte[] _digest;
124
125
126 MD5(String digest)
127 {
128 digest = digest.startsWith(__TYPE) ? digest.substring(__TYPE.length()) : digest;
129 _digest = TypeUtil.parseBytes(digest, 16);
130 }
131
132
133 public byte[] getDigest()
134 {
135 return _digest;
136 }
137
138
139 @Override
140 public boolean check(Object credentials)
141 {
142 try
143 {
144 byte[] digest = null;
145
146 if (credentials instanceof char[])
147 credentials=new String((char[])credentials);
148 if (credentials instanceof Password || credentials instanceof String)
149 {
150 synchronized (__md5Lock)
151 {
152 if (__md == null) __md = MessageDigest.getInstance("MD5");
153 __md.reset();
154 __md.update(credentials.toString().getBytes(StringUtil.__ISO_8859_1));
155 digest = __md.digest();
156 }
157 if (digest == null || digest.length != _digest.length) return false;
158 for (int i = 0; i < digest.length; i++)
159 if (digest[i] != _digest[i]) return false;
160 return true;
161 }
162 else if (credentials instanceof MD5)
163 {
164 MD5 md5 = (MD5) credentials;
165 if (_digest.length != md5._digest.length) return false;
166 for (int i = 0; i < _digest.length; i++)
167 if (_digest[i] != md5._digest[i]) return false;
168 return true;
169 }
170 else if (credentials instanceof Credential)
171 {
172
173
174 return ((Credential) credentials).check(this);
175 }
176 else
177 {
178 LOG.warn("Can't check " + credentials.getClass() + " against MD5");
179 return false;
180 }
181 }
182 catch (Exception e)
183 {
184 LOG.warn(e);
185 return false;
186 }
187 }
188
189
190 public static String digest(String password)
191 {
192 try
193 {
194 byte[] digest;
195 synchronized (__md5Lock)
196 {
197 if (__md == null)
198 {
199 try
200 {
201 __md = MessageDigest.getInstance("MD5");
202 }
203 catch (Exception e)
204 {
205 LOG.warn(e);
206 return null;
207 }
208 }
209
210 __md.reset();
211 __md.update(password.getBytes(StringUtil.__ISO_8859_1));
212 digest = __md.digest();
213 }
214
215 return __TYPE + TypeUtil.toString(digest, 16);
216 }
217 catch (Exception e)
218 {
219 LOG.warn(e);
220 return null;
221 }
222 }
223 }
224 }