168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
-
-
+
+
-
-
+
+
-
|
[clientFirstMessageBare release];
clientFirstMessageBare = nil;
clientFirstMessageBare = [[OFString alloc] initWithFormat: @"n=%@,r=%@",
authcid,
cNonce];
[ret addItemsFromCArray: [GS2Header UTF8String]
count: [GS2Header UTF8StringLength]];
[ret addItems: [GS2Header UTF8String]
count: [GS2Header UTF8StringLength]];
[ret addItemsFromCArray: [clientFirstMessageBare UTF8String]
count: [clientFirstMessageBare UTF8StringLength]];
[ret addItems: [clientFirstMessageBare UTF8String]
count: [clientFirstMessageBare UTF8StringLength]];
return ret;
}
- (OFDataArray*)continueWithData: (OFDataArray*)data
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
|
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
-
+
|
GOT_ITERCOUNT = 0x04
} got = 0;
hash = [[[hashType alloc] init] autorelease];
ret = [OFDataArray dataArray];
authMessage = [OFDataArray dataArray];
OFString *chal = [OFString stringWithUTF8String: [data cArray]
OFString *chal = [OFString stringWithUTF8String: [data items]
length: [data count] *
[data itemSize]];
enumerator =
[[chal componentsSeparatedByString: @","] objectEnumerator];
while ((comp = [enumerator nextObject]) != nil) {
OFString *entry = [comp substringWithRange:
|
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
-
-
+
+
-
-
+
+
-
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
-
-
+
+
-
-
+
+
-
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
-
-
-
+
+
-
-
+
+
-
-
+
+
-
+
|
if (got != (GOT_SNONCE | GOT_SALT | GOT_ITERCOUNT))
@throw [OFInvalidServerReplyException
exceptionWithClass: [self class]];
// Add c=<base64(GS2Header+channelBindingData)>
tmpArray = [OFDataArray dataArray];
[tmpArray addItemsFromCArray: [GS2Header UTF8String]
count: [GS2Header UTF8StringLength]];
[tmpArray addItems: [GS2Header UTF8String]
count: [GS2Header UTF8StringLength]];
if (plusAvailable && [connection encrypted]) {
OFDataArray *channelBinding = [((SSLSocket*)[connection socket])
channelBindingDataWithType: @"tls-unique"];
[tmpArray addItemsFromCArray: [channelBinding cArray]
count: [channelBinding count]];
[tmpArray addItems: [channelBinding items]
count: [channelBinding count]];
}
tmpString = [tmpArray stringByBase64Encoding];
[ret addItemsFromCArray: "c="
count: 2];
[ret addItemsFromCArray: [tmpString UTF8String]
count: [tmpString UTF8StringLength]];
[ret addItems: "c="
count: 2];
[ret addItems: [tmpString UTF8String]
count: [tmpString UTF8StringLength]];
// Add r=<nonce>
[ret addItem: ","];
[ret addItemsFromCArray: "r="
count: 2];
[ret addItemsFromCArray: [sNonce UTF8String]
count: [sNonce UTF8StringLength]];
[ret addItems: "r="
count: 2];
[ret addItems: [sNonce UTF8String]
count: [sNonce UTF8StringLength]];
/*
* IETF RFC 5802:
* SaltedPassword := Hi(Normalize(password), salt, i)
*/
tmpArray = [OFDataArray dataArray];
[tmpArray addItemsFromCArray: [password UTF8String]
count: [password UTF8StringLength]];
[tmpArray addItems: [password UTF8String]
count: [password UTF8StringLength]];
saltedPassword = [self XMPP_hiWithData: tmpArray
salt: salt
iterationCount: iterCount];
/*
* IETF RFC 5802:
* AuthMessage := client-first-message-bare + "," +
* server-first-message + "," +
* client-final-message-without-proof
*/
[authMessage addItemsFromCArray: [clientFirstMessageBare UTF8String]
count: [clientFirstMessageBare
[authMessage addItems: [clientFirstMessageBare UTF8String]
count: [clientFirstMessageBare UTF8StringLength]];
UTF8StringLength]];
[authMessage addItem: ","];
[authMessage addItemsFromCArray: [data cArray]
count: [data count] * [data itemSize]];
[authMessage addItems: [data items]
count: [data count] * [data itemSize]];
[authMessage addItem: ","];
[authMessage addItemsFromCArray: [ret cArray]
count: [ret count]];
[authMessage addItems: [ret items]
count: [ret count]];
/*
* IETF RFC 5802:
* ClientKey := HMAC(SaltedPassword, "Client Key")
*/
tmpArray = [OFDataArray dataArray];
[tmpArray addItemsFromCArray: "Client Key"
count: 10];
[tmpArray addItems: "Client Key"
count: 10];
clientKey = [self XMPP_HMACWithKey: saltedPassword
data: tmpArray];
/*
* IETF RFC 5802:
* StoredKey := H(ClientKey)
*/
[hash updateWithBuffer: (void*) clientKey
length: [hashType digestSize]];
tmpArray = [OFDataArray dataArray];
[tmpArray addItemsFromCArray: [hash digest]
count: [hashType digestSize]];
[tmpArray addItems: [hash digest]
count: [hashType digestSize]];
/*
* IETF RFC 5802:
* ClientSignature := HMAC(StoredKey, AuthMessage)
*/
clientSignature = [self XMPP_HMACWithKey: tmpArray
data: authMessage];
/*
* IETF RFC 5802:
* ServerKey := HMAC(SaltedPassword, "Server Key")
*/
tmpArray = [OFDataArray dataArray];
[tmpArray addItemsFromCArray: "Server Key"
count: 10];
[tmpArray addItems: "Server Key"
count: 10];
serverKey = [self XMPP_HMACWithKey: saltedPassword
data: tmpArray];
/*
* IETF RFC 5802:
* ServerSignature := HMAC(ServerKey, AuthMessage)
*/
tmpArray = [OFDataArray dataArray];
[tmpArray addItemsFromCArray: serverKey
count: [hashType digestSize]];
[tmpArray addItems: serverKey
count: [hashType digestSize]];
serverSignature = [[OFDataArray alloc] init];
[serverSignature addItemsFromCArray: [self
[serverSignature addItems: [self XMPP_HMACWithKey: tmpArray
XMPP_HMACWithKey: tmpArray
data: authMessage]
count: [hashType digestSize]];
data: authMessage]
count: [hashType digestSize]];
/*
* IETF RFC 5802:
* ClientProof := ClientKey XOR ClientSignature
*/
tmpArray = [OFDataArray dataArray];
for (i = 0; i < [hashType digestSize]; i++) {
uint8_t c = clientKey[i] ^ clientSignature[i];
[tmpArray addItem: &c];
}
// Add p=<base64(ClientProof)>
[ret addItem: ","];
[ret addItemsFromCArray: "p="
count: 2];
[ret addItems: "p="
count: 2];
tmpString = [tmpArray stringByBase64Encoding];
[ret addItemsFromCArray: [tmpString UTF8String]
count: [tmpString UTF8StringLength]];
[ret addItems: [tmpString UTF8String]
count: [tmpString UTF8StringLength]];
return ret;
}
- (OFDataArray*)XMPP_parseServerFinalMessage: (OFDataArray*)data
{
OFString *mess, *value;
/*
* server-final-message already received,
* we were just waiting for the last word from the server
*/
if (authenticated)
return nil;
mess = [OFString stringWithUTF8String: [data cArray]
mess = [OFString stringWithUTF8String: [data items]
length: [data count] *
[data itemSize]];
value = [mess substringWithRange: of_range(2, [mess length] - 2)];
if ([mess hasPrefix: @"v="]) {
if (![value isEqual: [serverSignature stringByBase64Encoding]])
@throw [XMPPAuthFailedException
|
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
-
+
-
-
+
+
-
-
+
+
-
+
-
+
|
OFDataArray *k = [OFDataArray dataArray];
size_t i, kSize, blockSize = [hashType blockSize];
uint8_t *kI = NULL, *kO = NULL;
OFHash *hashI, *hashO;
if ([key itemSize] * [key count] > blockSize) {
hashI = [[[hashType alloc] init] autorelease];
[hashI updateWithBuffer: [key cArray]
[hashI updateWithBuffer: [key items]
length: [key itemSize] * [key count]];
[k addItemsFromCArray: [hashI digest]
count: [hashType digestSize]];
[k addItems: [hashI digest]
count: [hashType digestSize]];
} else
[k addItemsFromCArray: [key cArray]
count: [key itemSize] * [key count]];
[k addItems: [key items]
count: [key itemSize] * [key count]];
@try {
kI = [self allocMemoryWithSize: blockSize];
kO = [self allocMemoryWithSize: blockSize];
kSize = [k count];
memcpy(kI, [k cArray], kSize);
memcpy(kI, [k items], kSize);
memset(kI + kSize, 0, blockSize - kSize);
memcpy(kO, kI, blockSize);
for (i = 0; i < blockSize; i++) {
kI[i] ^= HMAC_IPAD;
kO[i] ^= HMAC_OPAD;
}
hashI = [[[hashType alloc] init] autorelease];
[hashI updateWithBuffer: (char*)kI
length: blockSize];
[hashI updateWithBuffer: [data cArray]
[hashI updateWithBuffer: [data items]
length: [data itemSize] * [data count]];
hashO = [[[hashType alloc] init] autorelease];
[hashO updateWithBuffer: (char*)kO
length: blockSize];
[hashO updateWithBuffer: (char*)[hashI digest]
length: [hashType digestSize]];
|
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
-
-
+
+
-
-
+
+
-
-
+
+
|
result = [self allocMemoryWithSize: digestSize];
@try {
memset(result, 0, digestSize);
salty = [[salt_ copy] autorelease];
[salty addItemsFromCArray: "\0\0\0\1"
count: 4];
[salty addItems: "\0\0\0\1"
count: 4];
uOld = [self XMPP_HMACWithKey: str
data: salty];
for (j = 0; j < digestSize; j++)
result[j] ^= uOld[j];
for (j = 0; j < i - 1; j++) {
tmp = [OFDataArray dataArray];
[tmp addItemsFromCArray: uOld
count: digestSize];
[tmp addItems: uOld
count: digestSize];
u = [self XMPP_HMACWithKey: str
data: tmp];
for (k = 0; k < digestSize; k++)
result[k] ^= u[k];
uOld = u;
[pool releaseObjects];
}
ret = [OFDataArray dataArray];
[ret addItemsFromCArray: result
count: digestSize];
[ret addItems: result
count: digestSize];
} @finally {
[self freeMemory: result];
}
[ret retain];
[pool release];
return [ret autorelease];
}
@end
|