50 |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
51 |
*/ |
*/ |
52 |
public class SaslCramMD5 |
public class SaslCramMD5 |
53 |
implements SaslClient |
implements SaslClient |
54 |
{ |
{ |
55 |
|
|
56 |
private String username; |
private String username; |
57 |
private String password; |
private String password; |
58 |
private boolean complete; |
private boolean complete; |
59 |
|
|
60 |
public SaslCramMD5 (String username, String password) |
public SaslCramMD5(String username, String password) |
61 |
{ |
{ |
62 |
this.username = username; |
this.username = username; |
63 |
this.password = password; |
this.password = password; |
64 |
} |
} |
65 |
|
|
66 |
public String getMechanismName () |
public String getMechanismName() |
67 |
{ |
{ |
68 |
return "CRAM-MD5"; |
return "CRAM-MD5"; |
69 |
} |
} |
70 |
|
|
71 |
public boolean hasInitialResponse () |
public boolean hasInitialResponse() |
72 |
{ |
{ |
73 |
return false; |
return false; |
74 |
} |
} |
75 |
|
|
76 |
public byte[] evaluateChallenge (byte[] challenge) |
public byte[] evaluateChallenge(byte[] challenge) |
77 |
throws SaslException |
throws SaslException |
78 |
{ |
{ |
79 |
try |
try |
80 |
{ |
{ |
81 |
byte[] s = password.getBytes ("US-ASCII"); |
byte[] s = password.getBytes("US-ASCII"); |
82 |
byte[] digest = hmac_md5 (s, challenge); |
byte[] digest = hmac_md5(s, challenge); |
83 |
byte[] r0 = username.getBytes ("US-ASCII"); |
byte[] r0 = username.getBytes("US-ASCII"); |
84 |
byte[] r1 = new byte[r0.length + digest.length + 1]; |
byte[] r1 = new byte[r0.length + digest.length + 1]; |
85 |
System.arraycopy(r0, 0, r1, 0, r0.length); // add username |
System.arraycopy(r0, 0, r1, 0, r0.length); // add username |
86 |
r1[r0.length] = 0x20; // SPACE |
r1[r0.length] = 0x20; // SPACE |
91 |
catch (UnsupportedEncodingException e) |
catch (UnsupportedEncodingException e) |
92 |
{ |
{ |
93 |
String msg = "Username or password contains non-ASCII characters"; |
String msg = "Username or password contains non-ASCII characters"; |
94 |
throw new SaslException (msg, e); |
throw new SaslException(msg, e); |
95 |
} |
} |
96 |
catch (NoSuchAlgorithmException e) |
catch (NoSuchAlgorithmException e) |
97 |
{ |
{ |
98 |
String msg = "MD5 algorithm not available"; |
String msg = "MD5 algorithm not available"; |
99 |
throw new SaslException (msg, e); |
throw new SaslException(msg, e); |
100 |
} |
} |
101 |
} |
} |
102 |
|
|
103 |
public boolean isComplete () |
public boolean isComplete() |
104 |
{ |
{ |
105 |
return complete; |
return complete; |
106 |
} |
} |
107 |
|
|
108 |
public byte[] unwrap (byte[] incoming, int off, int len) |
public byte[] unwrap(byte[] incoming, int off, int len) |
109 |
throws SaslException |
throws SaslException |
110 |
{ |
{ |
111 |
byte[] ret = new byte[len - off]; |
byte[] ret = new byte[len - off]; |
112 |
System.arraycopy (incoming, off, ret, 0, len); |
System.arraycopy(incoming, off, ret, 0, len); |
113 |
return ret; |
return ret; |
114 |
} |
} |
115 |
|
|
116 |
public byte[] wrap (byte[] outgoing, int off, int len) |
public byte[] wrap(byte[] outgoing, int off, int len) |
117 |
throws SaslException |
throws SaslException |
118 |
{ |
{ |
119 |
byte[] ret = new byte[len - off]; |
byte[] ret = new byte[len - off]; |
120 |
System.arraycopy (outgoing, off, ret, 0, len); |
System.arraycopy(outgoing, off, ret, 0, len); |
121 |
return ret; |
return ret; |
122 |
} |
} |
123 |
|
|
124 |
public Object getNegotiatedProperty (String name) |
public Object getNegotiatedProperty(String name) |
125 |
{ |
{ |
126 |
return null; |
return null; |
127 |
} |
} |
128 |
|
|
129 |
public void dispose () |
public void dispose() |
130 |
{ |
{ |
131 |
} |
} |
132 |
|
|
149 |
MessageDigest md5 = MessageDigest.getInstance("MD5"); |
MessageDigest md5 = MessageDigest.getInstance("MD5"); |
150 |
// if key is longer than 64 bytes reset it to key=MD5(key) |
// if key is longer than 64 bytes reset it to key=MD5(key) |
151 |
if (key.length>64) |
if (key.length>64) |
152 |
{ |
{ |
153 |
md5.update(key); |
md5.update(key); |
154 |
key = md5.digest(); |
key = md5.digest(); |
155 |
} |
} |
156 |
// start out by storing key in pads |
// start out by storing key in pads |
157 |
System.arraycopy(key, 0, k_ipad, 0, key.length); |
System.arraycopy(key, 0, k_ipad, 0, key.length); |
158 |
System.arraycopy(key, 0, k_opad, 0, key.length); |
System.arraycopy(key, 0, k_opad, 0, key.length); |
159 |
// XOR key with ipad and opad values |
// XOR key with ipad and opad values |
160 |
for (int i=0; i<64; i++) |
for (int i=0; i<64; i++) |
161 |
{ |
{ |
162 |
k_ipad[i] ^= 0x36; |
k_ipad[i] ^= 0x36; |
163 |
k_opad[i] ^= 0x5c; |
k_opad[i] ^= 0x5c; |
164 |
} |
} |
165 |
// perform inner MD5 |
// perform inner MD5 |
166 |
md5.reset(); |
md5.reset(); |
167 |
md5.update(k_ipad); |
md5.update(k_ipad); |
176 |
} |
} |
177 |
|
|
178 |
} |
} |
179 |
|
|