ObjXMPP  Diff

Differences From Artifact [e92e687eeb]:

To Artifact [b45bfcda94]:


168
169
170
171
172
173
174
175
176


177
178
179


180
181
182
183
184
185
186
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 addNItems: [GS2Header UTF8StringLength]
	    fromCArray: [GS2Header UTF8String]];
	[ret addItemsFromCArray: [GS2Header UTF8String]
			  count: [GS2Header UTF8StringLength]];

	[ret addNItems: [clientFirstMessageBare UTF8StringLength]
	    fromCArray: [clientFirstMessageBare UTF8String]];
	[ret addItemsFromCArray: [clientFirstMessageBare UTF8String]
			  count: [clientFirstMessageBare UTF8StringLength]];


	return ret;
}

- (OFDataArray*)continueWithData: (OFDataArray*)data
{
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
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







-
-
+
+



-
-
+
+


-
-
-
-
+
+
+
+



-
-
-
-
+
+
+
+






-
-
+
+











-
-
+
+
+

+
-
+
-

-
-
+
+






-
-
+
+










-
-
+
+













-
-
+
+








-
-
+
+

-
-
-
+
+
+
+













-
-
+
+

-
-
+
+







	}

	if (got != (GOT_SNONCE | GOT_SALT | GOT_ITERCOUNT))
		@throw [OFInvalidServerReplyException exceptionWithClass: isa];

	// Add c=<base64(GS2Header+channelBindingData)>
	tmpArray = [OFDataArray dataArray];
	[tmpArray addNItems: [GS2Header UTF8StringLength]
		 fromCArray: [GS2Header UTF8String]];
	[tmpArray addItemsFromCArray: [GS2Header UTF8String]
			       count: [GS2Header UTF8StringLength]];
	if (plusAvailable && [connection encrypted]) {
		OFDataArray *channelBinding = [((SSLSocket*)[connection socket])
		    channelBindingDataWithType: @"tls-unique"];
		[tmpArray addNItems: [channelBinding count]
			 fromCArray: [channelBinding cArray]];
		[tmpArray addItemsFromCArray: [channelBinding cArray]
				       count: [channelBinding count]];
	}
	tmpString = [tmpArray stringByBase64Encoding];
	[ret addNItems: 2
	    fromCArray: "c="];
	[ret addNItems: [tmpString UTF8StringLength]
	    fromCArray: [tmpString UTF8String]];
	[ret addItemsFromCArray: "c="
			  count: 2];
	[ret addItemsFromCArray: [tmpString UTF8String]
			  count: [tmpString UTF8StringLength]];

	// Add r=<nonce>
	[ret addItem: ","];
	[ret addNItems: 2
	    fromCArray: "r="];
	[ret addNItems: [sNonce UTF8StringLength]
	    fromCArray: [sNonce UTF8String]];
	[ret addItemsFromCArray: "r="
			  count: 2];
	[ret addItemsFromCArray: [sNonce UTF8String]
			  count: [sNonce UTF8StringLength]];

	/*
	 * IETF RFC 5802:
	 * SaltedPassword := Hi(Normalize(password), salt, i)
	 */
	tmpArray = [OFDataArray dataArray];
	[tmpArray addNItems: [password UTF8StringLength]
		 fromCArray: [password UTF8String]];
	[tmpArray addItemsFromCArray: [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 addNItems: [clientFirstMessageBare UTF8StringLength]
		    fromCArray: [clientFirstMessageBare UTF8String]];
	[authMessage addItemsFromCArray: [clientFirstMessageBare UTF8String]
				  count: [clientFirstMessageBare
					     UTF8StringLength]];
	[authMessage addItem: ","];
	[authMessage addItemsFromCArray: [data cArray]
	[authMessage addNItems: [data count] * [data itemSize]
				  count: [data count] * [data itemSize]];
		    fromCArray: [data cArray]];
	[authMessage addItem: ","];
	[authMessage addNItems: [ret count]
		    fromCArray: [ret cArray]];
	[authMessage addItemsFromCArray: [ret cArray]
				  count: [ret count]];

	/*
	 * IETF RFC 5802:
	 * ClientKey := HMAC(SaltedPassword, "Client Key")
	 */
	tmpArray = [OFDataArray dataArray];
	[tmpArray addNItems: 10
		 fromCArray: "Client Key"];
	[tmpArray addItemsFromCArray: "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 addNItems: [hashType digestSize]
		 fromCArray: [hash digest]];
	[tmpArray addItemsFromCArray: [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 addNItems: 10
		 fromCArray: "Server Key"];
	[tmpArray addItemsFromCArray: "Server Key"
			       count: 10];
	serverKey = [self XMPP_HMACWithKey: saltedPassword
				      data: tmpArray];

	/*
	 * IETF RFC 5802:
	 * ServerSignature := HMAC(ServerKey, AuthMessage)
	 */
	tmpArray = [OFDataArray dataArray];
	[tmpArray addNItems: [hashType digestSize]
		 fromCArray: serverKey];
	[tmpArray addItemsFromCArray: serverKey
			       count: [hashType digestSize]];
	serverSignature = [[OFDataArray alloc] init];
	[serverSignature addNItems: [hashType digestSize]
			fromCArray: [self XMPP_HMACWithKey: tmpArray
						      data: authMessage]];
	[serverSignature addItemsFromCArray: [self
						 XMPP_HMACWithKey: tmpArray
							     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 addNItems: 2
	    fromCArray: "p="];
	[ret addItemsFromCArray: "p="
			  count: 2];
	tmpString = [tmpArray stringByBase64Encoding];
	[ret addNItems: [tmpString UTF8StringLength]
	    fromCArray: [tmpString UTF8String]];
	[ret addItemsFromCArray: [tmpString UTF8String]
			  count: [tmpString UTF8StringLength]];

	return ret;
}

- (OFDataArray*)XMPP_parseServerFinalMessage: (OFDataArray*)data
{
	OFString *mess, *value;
433
434
435
436
437
438
439
440
441


442

443

444
445
446
447
448
449
450
451
435
436
437
438
439
440
441


442
443
444
445

446

447
448
449
450
451
452
453







-
-
+
+

+
-
+
-







	uint8_t *kI = NULL, *kO = NULL;
	OFHash *hashI, *hashO;

	if ([key itemSize] * [key count] > blockSize) {
		hashI = [[[hashType alloc] init] autorelease];
		[hashI updateWithBuffer: [key cArray]
				length: [key itemSize] * [key count]];
		[k addNItems: [hashType digestSize]
		  fromCArray: [hashI digest]];
		[k addItemsFromCArray: [hashI digest]
				count: [hashType digestSize]];
	} else
		[k addItemsFromCArray: [key cArray]
		[k addNItems: [key itemSize] * [key count]
				count: [key itemSize] * [key count]];
		  fromCArray: [key cArray]];

	@try {
		kI = [self allocMemoryWithSize: blockSize];
		kO = [self allocMemoryWithSize: blockSize];

		kSize = [k count];
		memcpy(kI, [k cArray], kSize);
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
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
536
537







-
-
+
+









-
-
+
+













-
-
+
+











	result = [self allocMemoryWithSize: digestSize];

	@try {
		memset(result, 0, digestSize);

		salty = [[salt_ copy] autorelease];
		[salty addNItems: 4
		      fromCArray: "\0\0\0\1"];
		[salty addItemsFromCArray: "\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 addNItems: digestSize
			    fromCArray: uOld];
			[tmp addItemsFromCArray: 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 addNItems: digestSize
		    fromCArray: result];
		[ret addItemsFromCArray: result
				  count: digestSize];
	} @finally {
		[self freeMemory: result];
	}

	[ret retain];
	[pool release];

	return [ret autorelease];
}
@end