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