︙ | | | ︙ | |
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#import "XMPPExceptions.h"
#define HMAC_IPAD 0x36
#define HMAC_OPAD 0x5c
@interface XMPPSCRAMAuth ()
- (OFString *)xmpp_genNonce;
- (const uint8_t *)xmpp_HMACWithKey: (OFData *)key
data: (OFData *)data;
- (OFData *)xmpp_hiWithData: (OFData *)str
salt: (OFData *)salt
iterationCount: (intmax_t)i;
- (OFData *)xmpp_parseServerFirstMessage: (OFData *)data;
- (OFData *)xmpp_parseServerFinalMessage: (OFData *)data;
@end
|
|
<
|
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#import "XMPPExceptions.h"
#define HMAC_IPAD 0x36
#define HMAC_OPAD 0x5c
@interface XMPPSCRAMAuth ()
- (OFString *)xmpp_genNonce;
- (const uint8_t *)xmpp_HMACWithKey: (OFData *)key data: (OFData *)data;
- (OFData *)xmpp_hiWithData: (OFData *)str
salt: (OFData *)salt
iterationCount: (intmax_t)i;
- (OFData *)xmpp_parseServerFirstMessage: (OFData *)data;
- (OFData *)xmpp_parseServerFinalMessage: (OFData *)data;
@end
|
︙ | | | ︙ | |
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
|
- (void)setAuthzid: (OFString *)authzid
{
OFString *old = _authzid;
if (authzid) {
OFMutableString *new = [[authzid mutableCopy] autorelease];
[new replaceOccurrencesOfString: @"="
withString: @"=3D"];
[new replaceOccurrencesOfString: @","
withString: @"=2C"];
[new makeImmutable];
_authzid = [new copy];
} else
_authzid = nil;
[old release];
}
- (void)setAuthcid: (OFString *)authcid
{
OFString *old = _authcid;
if (authcid) {
OFMutableString *new = [[authcid mutableCopy] autorelease];
[new replaceOccurrencesOfString: @"="
withString: @"=3D"];
[new replaceOccurrencesOfString: @","
withString: @"=2C"];
[new makeImmutable];
_authcid = [new copy];
} else
_authcid = nil;
[old release];
}
|
|
<
|
<
|
<
|
<
|
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
|
- (void)setAuthzid: (OFString *)authzid
{
OFString *old = _authzid;
if (authzid) {
OFMutableString *new = [[authzid mutableCopy] autorelease];
[new replaceOccurrencesOfString: @"=" withString: @"=3D"];
[new replaceOccurrencesOfString: @"," withString: @"=2C"];
[new makeImmutable];
_authzid = [new copy];
} else
_authzid = nil;
[old release];
}
- (void)setAuthcid: (OFString *)authcid
{
OFString *old = _authcid;
if (authcid) {
OFMutableString *new = [[authcid mutableCopy] autorelease];
[new replaceOccurrencesOfString: @"=" withString: @"=3D"];
[new replaceOccurrencesOfString: @"," withString: @"=2C"];
[new makeImmutable];
_authcid = [new copy];
} else
_authcid = nil;
[old release];
}
|
︙ | | | ︙ | |
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
|
if (_plusAvailable && _connection.encrypted) {
OFData *channelBinding = [((SSLSocket *)[_connection socket])
channelBindingDataWithType: @"tls-unique"];
[tmpArray addItems: channelBinding.items
count: channelBinding.count];
}
tmpString = tmpArray.stringByBase64Encoding;
[ret addItems: "c="
count: 2];
[ret addItems: tmpString.UTF8String
count: tmpString.UTF8StringLength];
// Add r=<nonce>
[ret addItem: ","];
[ret addItems: "r="
count: 2];
[ret addItems: sNonce.UTF8String
count: sNonce.UTF8StringLength];
/*
* IETF RFC 5802:
* SaltedPassword := Hi(Normalize(password), salt, i)
*/
tmpArray = [OFMutableData dataWithItems: _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 addItems: _clientFirstMessageBare.UTF8String
count: _clientFirstMessageBare.UTF8StringLength];
[authMessage addItem: ","];
[authMessage addItems: data.items
count: data.count * data.itemSize];
[authMessage addItem: ","];
[authMessage addItems: ret.items
count: ret.count];
/*
* IETF RFC 5802:
* ClientKey := HMAC(SaltedPassword, "Client Key")
*/
clientKey = [self xmpp_HMACWithKey: saltedPassword
data: [OFData dataWithItems: "Client Key"
count: 10]];
/*
* IETF RFC 5802:
* StoredKey := H(ClientKey)
*/
[hash updateWithBuffer: (void *)clientKey
length: [_hashType digestSize]];
|
|
<
|
<
|
<
|
<
|
<
|
<
>
|
|
<
|
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
|
if (_plusAvailable && _connection.encrypted) {
OFData *channelBinding = [((SSLSocket *)[_connection socket])
channelBindingDataWithType: @"tls-unique"];
[tmpArray addItems: channelBinding.items
count: channelBinding.count];
}
tmpString = tmpArray.stringByBase64Encoding;
[ret addItems: "c=" count: 2];
[ret addItems: tmpString.UTF8String count: tmpString.UTF8StringLength];
// Add r=<nonce>
[ret addItem: ","];
[ret addItems: "r=" count: 2];
[ret addItems: sNonce.UTF8String count: sNonce.UTF8StringLength];
/*
* IETF RFC 5802:
* SaltedPassword := Hi(Normalize(password), salt, i)
*/
tmpArray = [OFMutableData dataWithItems: _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 addItems: _clientFirstMessageBare.UTF8String
count: _clientFirstMessageBare.UTF8StringLength];
[authMessage addItem: ","];
[authMessage addItems: data.items count: data.count * data.itemSize];
[authMessage addItem: ","];
[authMessage addItems: ret.items count: ret.count];
/*
* IETF RFC 5802:
* ClientKey := HMAC(SaltedPassword, "Client Key")
*/
clientKey = [self
xmpp_HMACWithKey: saltedPassword
data: [OFData dataWithItems: "Client Key" count: 10]];
/*
* IETF RFC 5802:
* StoredKey := H(ClientKey)
*/
[hash updateWithBuffer: (void *)clientKey
length: [_hashType digestSize]];
|
︙ | | | ︙ | |
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
|
count: hash.digestSize]
data: authMessage];
/*
* IETF RFC 5802:
* ServerKey := HMAC(SaltedPassword, "Server Key")
*/
serverKey = [self xmpp_HMACWithKey: saltedPassword
data: [OFData dataWithItems: "Server Key"
count: 10]];
/*
* IETF RFC 5802:
* ServerSignature := HMAC(ServerKey, AuthMessage)
*/
tmpArray = [OFMutableData dataWithItems: serverKey
count: [_hashType digestSize]];
[_serverSignature release];
_serverSignature = [[OFData alloc]
initWithItems: [self xmpp_HMACWithKey: tmpArray
data: authMessage]
count: [_hashType digestSize]];
/*
* IETF RFC 5802:
* ClientProof := ClientKey XOR ClientSignature
*/
tmpArray = [OFMutableData dataWithCapacity: [_hashType digestSize]];
for (i = 0; i < [_hashType digestSize]; i++) {
uint8_t c = clientKey[i] ^ clientSignature[i];
[tmpArray addItem: &c];
}
// Add p=<base64(ClientProof)>
[ret addItem: ","];
[ret addItems: "p="
count: 2];
tmpString = tmpArray.stringByBase64Encoding;
[ret addItems: tmpString.UTF8String
count: tmpString.UTF8StringLength];
return ret;
}
- (OFData *)xmpp_parseServerFinalMessage: (OFData *)data
{
OFString *mess, *value;
|
>
|
|
<
|
<
|
<
|
<
|
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
|
count: hash.digestSize]
data: authMessage];
/*
* IETF RFC 5802:
* ServerKey := HMAC(SaltedPassword, "Server Key")
*/
serverKey = [self
xmpp_HMACWithKey: saltedPassword
data: [OFData dataWithItems: "Server Key" count: 10]];
/*
* IETF RFC 5802:
* ServerSignature := HMAC(ServerKey, AuthMessage)
*/
tmpArray = [OFMutableData dataWithItems: serverKey
count: [_hashType digestSize]];
[_serverSignature release];
_serverSignature = [[OFData alloc]
initWithItems: [self xmpp_HMACWithKey: tmpArray data: authMessage]
count: [_hashType digestSize]];
/*
* IETF RFC 5802:
* ClientProof := ClientKey XOR ClientSignature
*/
tmpArray = [OFMutableData dataWithCapacity: [_hashType digestSize]];
for (i = 0; i < [_hashType digestSize]; i++) {
uint8_t c = clientKey[i] ^ clientSignature[i];
[tmpArray addItem: &c];
}
// Add p=<base64(ClientProof)>
[ret addItem: ","];
[ret addItems: "p=" count: 2];
tmpString = tmpArray.stringByBase64Encoding;
[ret addItems: tmpString.UTF8String count: tmpString.UTF8StringLength];
return ret;
}
- (OFData *)xmpp_parseServerFinalMessage: (OFData *)data
{
OFString *mess, *value;
|
︙ | | | ︙ | |
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
uint8_t *kI = NULL, *kO = NULL;
id <OFCryptographicHash> hashI, hashO;
if (key.itemSize * key.count > blockSize) {
hashI = [[[_hashType alloc] init] autorelease];
[hashI updateWithBuffer: key.items
length: key.itemSize * key.count];
[k addItems: hashI.digest
count: hashI.digestSize];
} else
[k addItems: key.items
count: key.itemSize * key.count];
@try {
kI = OFAllocMemory(1, blockSize);
kO = OFAllocMemory(1, blockSize);
kSize = k.count;
memcpy(kI, k.items, kSize);
|
|
<
|
<
|
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
uint8_t *kI = NULL, *kO = NULL;
id <OFCryptographicHash> hashI, hashO;
if (key.itemSize * key.count > blockSize) {
hashI = [[[_hashType alloc] init] autorelease];
[hashI updateWithBuffer: key.items
length: key.itemSize * key.count];
[k addItems: hashI.digest count: hashI.digestSize];
} else
[k addItems: key.items count: key.itemSize * key.count];
@try {
kI = OFAllocMemory(1, blockSize);
kO = OFAllocMemory(1, blockSize);
kSize = k.count;
memcpy(kI, k.items, kSize);
|
︙ | | | ︙ | |
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
539
540
541
542
543
544
545
546
547
548
|
@try {
memset(result, 0, digestSize);
salty = [[salt mutableCopy] autorelease];
[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 = [[OFMutableData alloc] init];
[tmp addItems: uOld
count: digestSize];
/* releases uOld and previous tmp */
objc_autoreleasePoolPop(pool);
pool = objc_autoreleasePoolPush();
[tmp autorelease];
u = [self xmpp_HMACWithKey: str
data: tmp];
for (k = 0; k < digestSize; k++)
result[k] ^= u[k];
uOld = u;
}
ret = [OFData dataWithItems: result
count: digestSize];
} @finally {
OFFreeMemory(result);
}
[ret retain];
objc_autoreleasePoolPop(pool);
return [ret autorelease];
}
@end
|
|
<
|
<
|
<
|
<
|
488
489
490
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
|
@try {
memset(result, 0, digestSize);
salty = [[salt mutableCopy] autorelease];
[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 = [[OFMutableData alloc] init];
[tmp addItems: uOld count: digestSize];
/* releases uOld and previous tmp */
objc_autoreleasePoolPop(pool);
pool = objc_autoreleasePoolPush();
[tmp autorelease];
u = [self xmpp_HMACWithKey: str data: tmp];
for (k = 0; k < digestSize; k++)
result[k] ^= u[k];
uOld = u;
}
ret = [OFData dataWithItems: result count: digestSize];
} @finally {
OFFreeMemory(result);
}
[ret retain];
objc_autoreleasePoolPop(pool);
return [ret autorelease];
}
@end
|