My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2007-2009 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @author vlads
* @version $Id$
*/
package com.intel.bluetooth.obex;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Vector;

import javax.obex.Authenticator;
import javax.obex.PasswordAuthentication;
import javax.obex.ServerRequestHandler;

import com.intel.bluetooth.DebugLog;

class OBEXAuthentication {

private static byte[] privateKey;

private static long uniqueTimestamp = 0;

private static final byte COLUMN[] = { ':' };

static class Challenge {

private String realm;

private boolean isUserIdRequired;

private boolean isFullAccess;

byte nonce[];

Challenge(byte data[]) throws IOException {
this.read(data);
}

Challenge(String realm, boolean isUserIdRequired, boolean isFullAccess, byte[] nonce) {
this.realm = realm;
this.isUserIdRequired = isUserIdRequired;
this.isFullAccess = isFullAccess;
this.nonce = nonce;
}

byte[] write() {
ByteArrayOutputStream buf = new ByteArrayOutputStream();

buf.write(0x00); // Tag
buf.write(0x10); // Len
buf.write(nonce, 0, 0x10);

byte options = (byte) ((isUserIdRequired ? 1 : 0) | ((!isFullAccess) ? 2 : 0));
buf.write(0x01); // Tag
buf.write(0x01); // Len
buf.write(options);

if (realm != null) {
byte realmArray[];
byte charSetCode;
try {
realmArray = OBEXUtils.getUTF16Bytes(realm);
charSetCode = -1; // 0xFF; Unicode
} catch (UnsupportedEncodingException e) {
try {
realmArray = realm.getBytes("iso-8859-1");
} catch (UnsupportedEncodingException e1) {
realmArray = new byte[0];
}
charSetCode = 1; // iso-8859-1
}
buf.write(0x02); // Tag
buf.write(realmArray.length + 1); // Len
buf.write(charSetCode);
buf.write(realmArray, 0, realmArray.length);
}

return buf.toByteArray();
}

void read(byte data[]) throws IOException {
DebugLog.debug("authChallenge", data);
for (int i = 0; i < data.length;) {
int tag = data[i] & 0xFF;
int len = data[i + 1] & 0xFF;
i += 2;
switch (tag) {
case 0:
if (len != 0x10) {
throw new IOException("OBEX Digest Challenge error in tag Nonce");
}
nonce = new byte[0x10];
System.arraycopy(data, i, nonce, 0, 0x10);
break;
case 1:
byte options = data[i];
DebugLog.debug("authChallenge options", options);
isUserIdRequired = ((options & 1) != 0);
isFullAccess = ((options & 2) == 0);
break;
case 2:
int charSetCode = data[i] & 0xFF;
byte chars[] = new byte[len - 1];
System.arraycopy(data, i + 1, chars, 0, chars.length);
if (charSetCode == 0xFF) {
realm = OBEXUtils.newStringUTF16(chars);
} else if (charSetCode == 0) {
realm = new String(chars, "ASCII");
} else if (charSetCode <= 9) {
realm = new String(chars, "ISO-8859-" + charSetCode);
} else {
DebugLog.error("Unsupported charset code " + charSetCode + " in Challenge");
// throw new UnsupportedEncodingException("charset code
// " + charSetCode);
// BUG on SE K790a
realm = new String(chars, 0, len - 1, "ASCII");
}
break;
default:
DebugLog.error("invalid authChallenge tag " + tag);
}
i += len;
}
}

public boolean isUserIdRequired() {
return isUserIdRequired;
}

public boolean isFullAccess() {
return isFullAccess;
}

public String getRealm() {
return realm;
}

}

static class DigestResponse {

byte requestDigest[];

byte userName[];

byte nonce[];

byte[] write() {
ByteArrayOutputStream buf = new ByteArrayOutputStream();

buf.write(0x00); // Tag
buf.write(0x10); // Len
buf.write(requestDigest, 0, 0x10);

if (userName != null) {
buf.write(0x01); // Tag
buf.write(userName.length); // Len
buf.write(userName, 0, userName.length);
}

buf.write(0x02); // Tag
buf.write(0x10); // Len
buf.write(nonce, 0, 0x10);

return buf.toByteArray();
}

void read(byte data[]) throws IOException {
for (int i = 0; i < data.length;) {
int tag = data[i] & 0xFF;
int len = data[i + 1] & 0xFF;
i += 2;
switch (tag) {
case 0:
if (len != 0x10) {
throw new IOException("OBEX Digest Response error in tag request-digest");
}
requestDigest = new byte[0x10];
System.arraycopy(data, i, requestDigest, 0, 0x10);
break;
case 1:
userName = new byte[len];
System.arraycopy(data, i, userName, 0, userName.length);
break;
case 2:
if (len != 0x10) {
throw new IOException("OBEX Digest Response error in tag Nonce");
}
nonce = new byte[0x10];
System.arraycopy(data, i, nonce, 0, 0x10);
break;
}
i += len;
}
}
}

static byte[] createChallenge(String realm, boolean isUserIdRequired, boolean isFullAccess) {
Challenge challenge = new Challenge(realm, isUserIdRequired, isFullAccess, createNonce());
return challenge.write();
}

static boolean handleAuthenticationResponse(OBEXHeaderSetImpl incomingHeaders, Authenticator authenticator,
ServerRequestHandler serverHandler, Vector authChallengesSent) throws IOException {
if (!incomingHeaders.hasAuthenticationResponses()) {
return false;
}
for (Enumeration iter = incomingHeaders.getAuthenticationResponses(); iter.hasMoreElements();) {
byte[] authResponse = (byte[]) iter.nextElement();
DigestResponse dr = new DigestResponse();
dr.read(authResponse);
DebugLog.debug("got nonce", dr.nonce);

// Verify that we did sent the Challenge that triggered this Responses
Challenge challengeSent = null;
for (Enumeration challengeIter = authChallengesSent.elements(); challengeIter.hasMoreElements();) {
Challenge c = (Challenge) challengeIter.nextElement();
if (equals(c.nonce, dr.nonce)) {
challengeSent = c;
break;
}
}
if (challengeSent == null) {
throw new IOException("Authentication response for unknown challenge");
}

byte[] password = authenticator.onAuthenticationResponse(dr.userName);
if (password == null) {
throw new IOException("Authentication request failed, password is not supplied");
}
// DebugLog.debug("authenticate using password", new String(password));
// DebugLog.debug("password used", password);
MD5DigestWrapper md5 = new MD5DigestWrapper();
md5.update(dr.nonce);
md5.update(COLUMN);
md5.update(password);
byte[] claulated = md5.digest();
if (!equals(dr.requestDigest, claulated)) {
DebugLog.debug("got digest", dr.requestDigest);
DebugLog.debug(" expected", claulated);
if (serverHandler != null) {
serverHandler.onAuthenticationFailure(dr.userName);
} else {
throw new IOException("Authentication failure");
}
} else {
return true;
}
}
return false;
}

static void handleAuthenticationChallenge(OBEXHeaderSetImpl incomingHeaders, OBEXHeaderSetImpl replyHeaders,
Authenticator authenticator) throws IOException {
if (!incomingHeaders.hasAuthenticationChallenge()) {
return;
}
for (Enumeration iter = incomingHeaders.getAuthenticationChallenges(); iter.hasMoreElements();) {
byte[] authChallenge = (byte[]) iter.nextElement();
Challenge challenge = new Challenge(authChallenge);
PasswordAuthentication pwd = authenticator.onAuthenticationChallenge(challenge.getRealm(), challenge
.isUserIdRequired(), challenge.isFullAccess());
DigestResponse dr = new DigestResponse();
dr.nonce = challenge.nonce;
DebugLog.debug("got nonce", dr.nonce);
if (challenge.isUserIdRequired()) {
dr.userName = pwd.getUserName();
}
MD5DigestWrapper md5 = new MD5DigestWrapper();
md5.update(dr.nonce);
md5.update(COLUMN);
md5.update(pwd.getPassword());
dr.requestDigest = md5.digest();
// DebugLog.debug("password", new String(pwd.getPassword()));
// DebugLog.debug("password used", pwd.getPassword());
DebugLog.debug("send digest", dr.requestDigest);
replyHeaders.addAuthenticationResponse(dr.write());
}
}

private static synchronized byte[] createNonce() {
MD5DigestWrapper md5 = new MD5DigestWrapper();
md5.update(createTimestamp());
md5.update(COLUMN);
md5.update(getPrivateKey());
return md5.digest();
}

static boolean equals(byte[] digest1, byte[] digest2) {
for (int i = 0; i < 0x10; i++) {
if (digest1[i] != digest2[i]) {
return false;
}
}
return true;
}

private static synchronized byte[] getPrivateKey() {
if (privateKey != null) {
return privateKey;
}
MD5DigestWrapper md5 = new MD5DigestWrapper();
md5.update(createTimestamp());
privateKey = md5.digest();
return privateKey;
}

private static synchronized byte[] createTimestamp() {
long t = System.currentTimeMillis();
if (t <= uniqueTimestamp) {
t = uniqueTimestamp + 1;
}
uniqueTimestamp = t;
byte[] buf = new byte[8];
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (t >> (buf.length - 1 << 3));
t <<= 8;
}
return buf;
}

}

Change log

r2915 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
Go to: 
Project members, sign in to write a code review

Older revisions

r2641 by skarzhevskyy on Dec 22, 2008   Diff
Fixed client side handling of
Authentication Challenge, will retry
sending headers
r2604 by skarzhevskyy on Dec 17, 2008   Diff
Work on OBEX TCK
r2476 by skarzhevskyy on Dec 1, 2008   Diff
Correction to headers
All revisions of this file

File info

Size: 9950 bytes, 344 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting