ObjXMPP  Check-in [efaee4fc4c]

Overview
Comment:Adjust to ObjFW changes & small fixes
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: efaee4fc4c90d83d91382a8a57d7f354ad95638a6004253f66e423460321f814
User & Date: js on 2017-07-23 11:19:48
Other Links: manifest | tags
Context
2017-07-23
11:35
Stop using OFAutoreleasePool check-in: 7f939be668 user: js tags: trunk
11:19
Adjust to ObjFW changes & small fixes check-in: efaee4fc4c user: js tags: trunk
2017-05-13
15:03
Adjust to ObjFW changes check-in: 2faf18cba7 user: js tags: trunk
Changes

Modified src/XMPPAuthenticator.h from [d2168a1fd5] to [d85386eb0a].

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 * \return A initialized XMPPAuthenticator
 */
- initWithAuthzid: (nullable OFString *)authzid
	  authcid: (nullable OFString *)authcid
	 password: (nullable OFString *)password OF_DESIGNATED_INITIALIZER;

/**
 * \brief Returns an OFDataArray containing the initial authentication message.
 *
 * \return An OFDataAray containing the initial authentication message
 */
- (OFDataArray *)initialMessage;

/**
 * \brief Continue authentication with the specified data.
 *
 * \param data The continuation data send by the server
 * \return The appropriate response if the data was a challenge, nil otherwise
 */
- (nullable OFDataArray *)continueWithData: (OFDataArray *)data;
@end

OF_ASSUME_NONNULL_END







|



|







|



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 * \return A initialized XMPPAuthenticator
 */
- initWithAuthzid: (nullable OFString *)authzid
	  authcid: (nullable OFString *)authcid
	 password: (nullable OFString *)password OF_DESIGNATED_INITIALIZER;

/**
 * \brief Returns OFData containing the initial authentication message.
 *
 * \return An OFDataAray containing the initial authentication message
 */
- (nullable OFData *)initialMessage;

/**
 * \brief Continue authentication with the specified data.
 *
 * \param data The continuation data send by the server
 * \return The appropriate response if the data was a challenge, nil otherwise
 */
- (nullable OFData *)continueWithData: (OFData *)data;
@end

OF_ASSUME_NONNULL_END

Modified src/XMPPAuthenticator.m from [8f69cece0e] to [545c182be0].

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
	[_authzid release];
	[_authcid release];
	[_password release];

	[super dealloc];
}

- (OFDataArray *)initialMessage
{
	return nil;
}

- (OFDataArray *)continueWithData: (OFDataArray *)challenge
{
	return nil;
}
@end







|




|




61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
	[_authzid release];
	[_authcid release];
	[_password release];

	[super dealloc];
}

- (OFData *)initialMessage
{
	return nil;
}

- (OFData *)continueWithData: (OFData *)challenge
{
	return nil;
}
@end

Modified src/XMPPConnection.m from [e5df95b15f] to [728196e00a].

891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
	assert(0);
}

- (void)XMPP_handleSASL: (OFXMLElement *)element
{
	if ([[element name] isEqual: @"challenge"]) {
		OFXMLElement *responseTag;
		OFDataArray *challenge = [OFDataArray
		    dataArrayWithBase64EncodedString: [element stringValue]];
		OFDataArray *response = [_authModule
		    continueWithData: challenge];

		responseTag = [OFXMLElement elementWithName: @"response"
						  namespace: XMPP_NS_SASL];
		if (response) {
			if ([response count] == 0)
				[responseTag setStringValue: @"="];
			else
				[responseTag setStringValue:
				    [response stringByBase64Encoding]];
		}

		[self sendStanza: responseTag];
		return;
	}

	if ([[element name] isEqual: @"success"]) {
		[_authModule continueWithData: [OFDataArray
		    dataArrayWithBase64EncodedString: [element stringValue]]];

		[_delegates broadcastSelector: @selector(
						   connectionWasAuthenticated:)
				   withObject: self];

		/* Stream restart */
		[self XMPP_startStream];







|
|
<
|
















|
|







891
892
893
894
895
896
897
898
899

900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
	assert(0);
}

- (void)XMPP_handleSASL: (OFXMLElement *)element
{
	if ([[element name] isEqual: @"challenge"]) {
		OFXMLElement *responseTag;
		OFData *challenge =
		    [OFData dataWithBase64EncodedString: [element stringValue]];

		OFData *response = [_authModule continueWithData: challenge];

		responseTag = [OFXMLElement elementWithName: @"response"
						  namespace: XMPP_NS_SASL];
		if (response) {
			if ([response count] == 0)
				[responseTag setStringValue: @"="];
			else
				[responseTag setStringValue:
				    [response stringByBase64Encoding]];
		}

		[self sendStanza: responseTag];
		return;
	}

	if ([[element name] isEqual: @"success"]) {
		[_authModule continueWithData: [OFData
		    dataWithBase64EncodedString: [element stringValue]]];

		[_delegates broadcastSelector: @selector(
						   connectionWasAuthenticated:)
				   withObject: self];

		/* Stream restart */
		[self XMPP_startStream];
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088

	assert(0);
}

- (void)XMPP_sendAuth: (OFString *)authName
{
	OFXMLElement *authTag;
	OFDataArray *initialMessage = [_authModule initialMessage];

	authTag = [OFXMLElement elementWithName: @"auth"
				      namespace: XMPP_NS_SASL];
	[authTag addAttributeWithName: @"mechanism"
			  stringValue: authName];
	if (initialMessage) {
		if ([initialMessage count] == 0)







|







1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087

	assert(0);
}

- (void)XMPP_sendAuth: (OFString *)authName
{
	OFXMLElement *authTag;
	OFData *initialMessage = [_authModule initialMessage];

	authTag = [OFXMLElement elementWithName: @"auth"
				      namespace: XMPP_NS_SASL];
	[authTag addAttributeWithName: @"mechanism"
			  stringValue: authName];
	if (initialMessage) {
		if ([initialMessage count] == 0)

Modified src/XMPPDiscoEntity.m from [f708ffe294] to [40cdae8631].

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
- (OFString *)capsHash
{
	OFEnumerator *enumerator;
	XMPPDiscoIdentity *identity;
	OFString *feature;
	OFMutableString *caps = [OFMutableString string];
	OFSHA1Hash *hash = [OFSHA1Hash cryptoHash];
	OFDataArray *digest = [OFDataArray dataArray];

	enumerator = [_identities objectEnumerator];
	while ((identity = [enumerator nextObject]) != nil)
		[caps appendFormat: @"%@/%@//%@<", [identity category],
		    [identity type], [identity name]];

	enumerator = [_features objectEnumerator];
	while ((feature = [enumerator nextObject]) != nil)
		[caps appendFormat: @"%@<", feature];

	[hash updateWithBuffer: [caps UTF8String]
			length: [caps UTF8StringLength]];

	[digest addItems: [hash digest]
		   count: [OFSHA1Hash digestSize]];

	return [digest stringByBase64Encoding];
}

- (void)connection: (XMPPConnection *)connection
     wasBoundToJID: (XMPPJID *)JID
{







|













|
|







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
- (OFString *)capsHash
{
	OFEnumerator *enumerator;
	XMPPDiscoIdentity *identity;
	OFString *feature;
	OFMutableString *caps = [OFMutableString string];
	OFSHA1Hash *hash = [OFSHA1Hash cryptoHash];
	OFData *digest;

	enumerator = [_identities objectEnumerator];
	while ((identity = [enumerator nextObject]) != nil)
		[caps appendFormat: @"%@/%@//%@<", [identity category],
		    [identity type], [identity name]];

	enumerator = [_features objectEnumerator];
	while ((feature = [enumerator nextObject]) != nil)
		[caps appendFormat: @"%@<", feature];

	[hash updateWithBuffer: [caps UTF8String]
			length: [caps UTF8StringLength]];

	digest = [OFData dataWithItems: [hash digest]
				 count: [[hash class] digestSize]];

	return [digest stringByBase64Encoding];
}

- (void)connection: (XMPPConnection *)connection
     wasBoundToJID: (XMPPJID *)JID
{

Modified src/XMPPEXTERNALAuth.m from [c6103d3079] to [9d3e3b5205].

36
37
38
39
40
41
42
43
44
45
46
47
48
49



50
51
52
53
+ (instancetype)EXTERNALAuthWithAuthzid: (OFString *)authzid
{
	return [[[self alloc] initWithAuthzid: authzid
				      authcid: nil
				     password: nil] autorelease];
}

- (OFDataArray *)initialMessage
{
	OFDataArray *message = [OFDataArray dataArray];

	/* authzid */
	if (_authzid)
		[message addItem: _authzid];




	return message;
}
@end







|

|


|
|
>
>
>




36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
+ (instancetype)EXTERNALAuthWithAuthzid: (OFString *)authzid
{
	return [[[self alloc] initWithAuthzid: authzid
				      authcid: nil
				     password: nil] autorelease];
}

- (OFData *)initialMessage
{
	OFMutableData *message = [OFMutableData data];

	/* authzid */
	if (_authzid != nil)
		[message addItems: [_authzid UTF8String]
			    count: [_authzid UTF8StringLength]];

	[message makeImmutable];

	return message;
}
@end

Modified src/XMPPFileStorage.m from [d3dafb0daf] to [8d2b51a333].

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <stdlib.h>

#import <ObjFW/OFString.h>
#import <ObjFW/OFArray.h>
#import <ObjFW/OFDictionary.h>
#import <ObjFW/OFNumber.h>
#import <ObjFW/OFDataArray.h>
#import <ObjFW/OFAutoreleasePool.h>

#import <ObjFW/OFNotImplementedException.h>

#import "XMPPFileStorage.h"

@implementation XMPPFileStorage







|







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <stdlib.h>

#import <ObjFW/OFString.h>
#import <ObjFW/OFArray.h>
#import <ObjFW/OFDictionary.h>
#import <ObjFW/OFNumber.h>
#import <ObjFW/OFData.h>
#import <ObjFW/OFAutoreleasePool.h>

#import <ObjFW/OFNotImplementedException.h>

#import "XMPPFileStorage.h"

@implementation XMPPFileStorage
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
	self = [super init];

	@try {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

		_file = [file copy];
		@try {
			_data = [[[OFDataArray dataArrayWithContentsOfFile:
			    file] messagePackValue] retain];
		} @catch (id e) {
			_data = [[OFMutableDictionary alloc] init];
		}

		[pool release];
	} @catch (id e) {
		[self release];







|
|







48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
	self = [super init];

	@try {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

		_file = [file copy];
		@try {
			_data = [[[OFData dataWithContentsOfFile: file]
			    messagePackValue] retain];
		} @catch (id e) {
			_data = [[OFMutableDictionary alloc] init];
		}

		[pool release];
	} @catch (id e) {
		[self release];

Modified src/XMPPMulticastDelegate.h from [51673325c7] to [35f3f776c8].

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 * POSSIBILITY OF SUCH DAMAGE.
 */

#import <ObjFW/OFObject.h>

OF_ASSUME_NONNULL_BEGIN

@class OFDataArray;

/**
 * \brief A class to provide multiple delegates in a single class
 */
@interface XMPPMulticastDelegate: OFObject
{
	OFDataArray *_delegates;
}

/**
 * \brief Adds a delegate which should receive the broadcasts.
 *
 * \param delegate The delegate to add
 */







|






|







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 * POSSIBILITY OF SUCH DAMAGE.
 */

#import <ObjFW/OFObject.h>

OF_ASSUME_NONNULL_BEGIN

@class OFMutableData;

/**
 * \brief A class to provide multiple delegates in a single class
 */
@interface XMPPMulticastDelegate: OFObject
{
	OFMutableData *_delegates;
}

/**
 * \brief Adds a delegate which should receive the broadcasts.
 *
 * \param delegate The delegate to add
 */

Modified src/XMPPMulticastDelegate.m from [ad24b3b3f5] to [067d646987].

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
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#import <ObjFW/ObjFW.h>
#import <ObjFW/OFDataArray.h>

#import "XMPPMulticastDelegate.h"

@implementation XMPPMulticastDelegate
- init
{
	self = [super init];

	@try {

		_delegates = [[OFDataArray alloc] initWithItemSize: sizeof(id)];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}







|









>
|







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
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#import <ObjFW/ObjFW.h>
#import <ObjFW/OFData.h>

#import "XMPPMulticastDelegate.h"

@implementation XMPPMulticastDelegate
- init
{
	self = [super init];

	@try {
		_delegates = [[OFMutableData alloc]
		    initWithItemSize: sizeof(id)];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
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
		return;
	}
}

- (bool)broadcastSelector: (SEL)selector
	       withObject: (id)object
{

	OFDataArray *currentDelegates = [_delegates copy];
	id *items = [currentDelegates items];
	size_t i, count = [currentDelegates count];
	bool handled = false;

	for (i = 0; i < count; i++) {
		id responder = items[i];

		if (![responder respondsToSelector: selector])
			continue;

		bool (*imp)(id, SEL, id) = (bool(*)(id, SEL, id))
		    [responder methodForSelector: selector];

		handled |= imp(responder, selector, object);
	}



	return handled;
}

- (bool)broadcastSelector: (SEL)selector
	       withObject: (id)object1
	       withObject: (id)object2
{

	OFDataArray *currentDelegates = [_delegates copy];
	id *items = [currentDelegates items];
	size_t i, count = [currentDelegates count];
	bool handled = false;

	for (i = 0; i < count; i++) {
		id responder = items[i];

		if (![responder respondsToSelector: selector])
			continue;

		bool (*imp)(id, SEL, id, id) = (bool(*)(id, SEL, id, id))
		    [responder methodForSelector: selector];

		handled |= imp(responder, selector, object1, object2);
	}



	return handled;
}
@end







>
|
















>
>







>
|















>
>




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
		return;
	}
}

- (bool)broadcastSelector: (SEL)selector
	       withObject: (id)object
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMutableData *currentDelegates = [[_delegates copy] autorelease];
	id *items = [currentDelegates items];
	size_t i, count = [currentDelegates count];
	bool handled = false;

	for (i = 0; i < count; i++) {
		id responder = items[i];

		if (![responder respondsToSelector: selector])
			continue;

		bool (*imp)(id, SEL, id) = (bool(*)(id, SEL, id))
		    [responder methodForSelector: selector];

		handled |= imp(responder, selector, object);
	}

	[pool release];

	return handled;
}

- (bool)broadcastSelector: (SEL)selector
	       withObject: (id)object1
	       withObject: (id)object2
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMutableData *currentDelegates = [[_delegates copy] autorelease];
	id *items = [currentDelegates items];
	size_t i, count = [currentDelegates count];
	bool handled = false;

	for (i = 0; i < count; i++) {
		id responder = items[i];

		if (![responder respondsToSelector: selector])
			continue;

		bool (*imp)(id, SEL, id, id) = (bool(*)(id, SEL, id, id))
		    [responder methodForSelector: selector];

		handled |= imp(responder, selector, object1, object2);
	}

	[pool release];

	return handled;
}
@end

Modified src/XMPPPLAINAuth.m from [9f0783c62c] to [8868878d5b].

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
			    password: (OFString *)password
{
	return [[[self alloc] initWithAuthzid: authzid
				      authcid: authcid
				     password: password] autorelease];
}

- (OFDataArray *)initialMessage
{
	OFDataArray *message = [OFDataArray dataArray];

	/* authzid */
	if (_authzid)
		[message addItem: _authzid];


	/* separator */
	[message addItem: ""];

	/* authcid */
	[message addItems: [_authcid UTF8String]
		    count: [_authcid UTF8StringLength]];

	/* separator */
	[message addItem: ""];

	/* passwd */
	[message addItems: [_password UTF8String]
		    count: [_password UTF8StringLength]];



	return message;
}
@end







|

|


|
|
>














>
>




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
			    password: (OFString *)password
{
	return [[[self alloc] initWithAuthzid: authzid
				      authcid: authcid
				     password: password] autorelease];
}

- (OFData *)initialMessage
{
	OFMutableData *message = [OFMutableData data];

	/* authzid */
	if (_authzid != nil)
		[message addItems: [_authzid UTF8String]
			    count: [_authzid UTF8StringLength]];

	/* separator */
	[message addItem: ""];

	/* authcid */
	[message addItems: [_authcid UTF8String]
		    count: [_authcid UTF8StringLength]];

	/* separator */
	[message addItem: ""];

	/* passwd */
	[message addItems: [_password UTF8String]
		    count: [_password UTF8StringLength]];

	[message makeImmutable];

	return message;
}
@end

Modified src/XMPPSCRAMAuth.h from [fc2ab6d5ef] to [f16a3ea40b].

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 */
@interface XMPPSCRAMAuth: XMPPAuthenticator
{
	Class _hashType;
	OFString *_cNonce;
	OFString *_GS2Header;
	OFString *_clientFirstMessageBare;
	OFDataArray *_serverSignature;
	XMPPConnection *_connection;
	bool _plusAvailable;
	bool _authenticated;
}

/**
 * \brief Creates a new autoreleased XMPPSCRAMAuth with an authcid and password.







|







31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 */
@interface XMPPSCRAMAuth: XMPPAuthenticator
{
	Class _hashType;
	OFString *_cNonce;
	OFString *_GS2Header;
	OFString *_clientFirstMessageBare;
	OFData *_serverSignature;
	XMPPConnection *_connection;
	bool _plusAvailable;
	bool _authenticated;
}

/**
 * \brief Creates a new autoreleased XMPPSCRAMAuth with an authcid and password.

Modified src/XMPPSCRAMAuth.m from [e2f43c5d67] to [ee42191b66].

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#define HMAC_IPAD 0x36
#define HMAC_OPAD 0x5c

OF_ASSUME_NONNULL_BEGIN

@interface XMPPSCRAMAuth ()
- (OFString *)XMPP_genNonce;
- (const uint8_t *)XMPP_HMACWithKey: (OFDataArray *)key
			data: (OFDataArray *)data;
- (OFDataArray *)XMPP_hiWithData: (OFDataArray *)str
			   salt: (OFDataArray *)salt
		 iterationCount: (intmax_t)i;
- (OFDataArray *)XMPP_parseServerFirstMessage: (OFDataArray *)data;
- (OFDataArray *)XMPP_parseServerFinalMessage: (OFDataArray *)data;
@end

OF_ASSUME_NONNULL_END

@implementation XMPPSCRAMAuth
+ (instancetype)SCRAMAuthWithAuthcid: (OFString *)authcid
			    password: (OFString *)password







|
|
|
|
|
|
|







37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#define HMAC_IPAD 0x36
#define HMAC_OPAD 0x5c

OF_ASSUME_NONNULL_BEGIN

@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

OF_ASSUME_NONNULL_END

@implementation XMPPSCRAMAuth
+ (instancetype)SCRAMAuthWithAuthcid: (OFString *)authcid
			    password: (OFString *)password
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
		_authcid = [new retain];
	} else
		_authcid = nil;

	[old release];
}

- (OFDataArray *)initialMessage
{
	OFDataArray *ret = [OFDataArray dataArray];

	/* New authentication attempt, reset status */
	[_cNonce release];
	_cNonce = nil;
	[_GS2Header release];
	_GS2Header = nil;
	[_serverSignature release];
	_serverSignature = nil;
	_authenticated = false;

	if (_authzid)
		_GS2Header = [[OFString alloc]
		    initWithFormat: @"%@,a=%@,",
				    (_plusAvailable ? @"p=tls-unique" : @"y"),
				    _authzid];
	else
		_GS2Header = (_plusAvailable ? @"p=tls-unique,," : @"y,,");

	_cNonce = [[self XMPP_genNonce] retain];

	[_clientFirstMessageBare release];
	_clientFirstMessageBare = nil;
	_clientFirstMessageBare = [[OFString alloc]
	    initWithFormat: @"n=%@,r=%@", _authcid, _cNonce];

	[ret addItems: [_GS2Header UTF8String]
		count: [_GS2Header UTF8StringLength]];

	[ret addItems: [_clientFirstMessageBare UTF8String]
		count: [_clientFirstMessageBare UTF8StringLength]];



	return ret;
}

- (OFDataArray *)continueWithData: (OFDataArray *)data
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFDataArray *ret;

	if (!_serverSignature)
		ret = [self XMPP_parseServerFirstMessage: data];
	else
		ret = [self XMPP_parseServerFinalMessage: data];

	[ret retain];
	[pool release];

	return [ret autorelease];
}

- (OFDataArray *)XMPP_parseServerFirstMessage: (OFDataArray *)data
{
	size_t i;
	const uint8_t *clientKey, *serverKey, *clientSignature;
	intmax_t iterCount = 0;
	id <OFCryptoHash> hash;

	OFDataArray *ret, *authMessage, *tmpArray, *salt = nil, *saltedPassword;
	OFString *tmpString, *sNonce = nil;
	OFEnumerator *enumerator;
	OFString *comp;
	enum {
		GOT_SNONCE    = 0x01,
		GOT_SALT      = 0x02,
		GOT_ITERCOUNT = 0x04
	} got = 0;

	hash = [[[_hashType alloc] init] autorelease];
	ret = [OFDataArray dataArray];
	authMessage = [OFDataArray dataArray];

	OFString *chal = [OFString stringWithUTF8String: [data items]
						 length: [data count] *
							 [data itemSize]];

	enumerator =
	    [[chal componentsSeparatedByString: @","] objectEnumerator];







|

|










|




















>
>



|


|












|





>
|










|
|







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
		_authcid = [new retain];
	} else
		_authcid = nil;

	[old release];
}

- (OFData *)initialMessage
{
	OFMutableData *ret = [OFMutableData data];

	/* New authentication attempt, reset status */
	[_cNonce release];
	_cNonce = nil;
	[_GS2Header release];
	_GS2Header = nil;
	[_serverSignature release];
	_serverSignature = nil;
	_authenticated = false;

	if (_authzid != nil)
		_GS2Header = [[OFString alloc]
		    initWithFormat: @"%@,a=%@,",
				    (_plusAvailable ? @"p=tls-unique" : @"y"),
				    _authzid];
	else
		_GS2Header = (_plusAvailable ? @"p=tls-unique,," : @"y,,");

	_cNonce = [[self XMPP_genNonce] retain];

	[_clientFirstMessageBare release];
	_clientFirstMessageBare = nil;
	_clientFirstMessageBare = [[OFString alloc]
	    initWithFormat: @"n=%@,r=%@", _authcid, _cNonce];

	[ret addItems: [_GS2Header UTF8String]
		count: [_GS2Header UTF8StringLength]];

	[ret addItems: [_clientFirstMessageBare UTF8String]
		count: [_clientFirstMessageBare UTF8StringLength]];

	[ret makeImmutable];

	return ret;
}

- (OFData *)continueWithData: (OFData *)data
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFData *ret;

	if (!_serverSignature)
		ret = [self XMPP_parseServerFirstMessage: data];
	else
		ret = [self XMPP_parseServerFinalMessage: data];

	[ret retain];
	[pool release];

	return [ret autorelease];
}

- (OFData *)XMPP_parseServerFirstMessage: (OFData *)data
{
	size_t i;
	const uint8_t *clientKey, *serverKey, *clientSignature;
	intmax_t iterCount = 0;
	id <OFCryptoHash> hash;
	OFMutableData *ret, *authMessage, *tmpArray;
	OFData *salt = nil, *saltedPassword;
	OFString *tmpString, *sNonce = nil;
	OFEnumerator *enumerator;
	OFString *comp;
	enum {
		GOT_SNONCE    = 0x01,
		GOT_SALT      = 0x02,
		GOT_ITERCOUNT = 0x04
	} got = 0;

	hash = [[[_hashType alloc] init] autorelease];
	ret = [OFMutableData data];
	authMessage = [OFMutableData data];

	OFString *chal = [OFString stringWithUTF8String: [data items]
						 length: [data count] *
							 [data itemSize]];

	enumerator =
	    [[chal componentsSeparatedByString: @","] objectEnumerator];
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
				    exceptionWithConnection: nil
						     reason: @"Received wrong "
							     @"nonce"];

			sNonce = entry;
			got |= GOT_SNONCE;
		} else if ([comp hasPrefix: @"s="]) {
			salt = [OFDataArray
			    dataArrayWithBase64EncodedString: entry];
			got |= GOT_SALT;
		} else if ([comp hasPrefix: @"i="]) {
			iterCount = [entry decimalValue];
			got |= GOT_ITERCOUNT;
		}
	}

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

	// Add c=<base64(GS2Header+channelBindingData)>
	tmpArray = [OFDataArray dataArray];
	[tmpArray addItems: [_GS2Header UTF8String]
		     count: [_GS2Header UTF8StringLength]];
	if (_plusAvailable && [_connection encrypted]) {
		OFDataArray *channelBinding =
		    [((SSLSocket *)[_connection socket])
		    channelBindingDataWithType: @"tls-unique"];
		[tmpArray addItems: [channelBinding items]
			     count: [channelBinding count]];
	}
	tmpString = [tmpArray stringByBase64Encoding];
	[ret addItems: "c="
		count: 2];







<
|











|



<
|







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
				    exceptionWithConnection: nil
						     reason: @"Received wrong "
							     @"nonce"];

			sNonce = entry;
			got |= GOT_SNONCE;
		} else if ([comp hasPrefix: @"s="]) {

			salt = [OFData dataWithBase64EncodedString: entry];
			got |= GOT_SALT;
		} else if ([comp hasPrefix: @"i="]) {
			iterCount = [entry decimalValue];
			got |= GOT_ITERCOUNT;
		}
	}

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

	// Add c=<base64(GS2Header+channelBindingData)>
	tmpArray = [OFMutableData data];
	[tmpArray addItems: [_GS2Header UTF8String]
		     count: [_GS2Header UTF8StringLength]];
	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];
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
	[ret addItems: [sNonce UTF8String]
		count: [sNonce UTF8StringLength]];

	/*
	 * IETF RFC 5802:
	 * SaltedPassword := Hi(Normalize(password), salt, i)
	 */
	tmpArray = [OFDataArray dataArray];
	[tmpArray addItems: [_password UTF8String]
		     count: [_password UTF8StringLength]];

	saltedPassword = [self XMPP_hiWithData: tmpArray
					  salt: salt
				iterationCount: iterCount];

	/*
	 * IETF RFC 5802:
	 * AuthMessage := client-first-message-bare + "," +







<
|
|
<







288
289
290
291
292
293
294

295
296

297
298
299
300
301
302
303
	[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 + "," +
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
396
397
398
399
400
401
402
403
404
405
406
407
	[authMessage addItems: [ret items]
			count: [ret count]];

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

	/*
	 * IETF RFC 5802:
	 * ServerSignature := HMAC(ServerKey, AuthMessage)
	 */
	tmpArray = [OFDataArray dataArray];
	[tmpArray addItems: serverKey
		     count: [_hashType digestSize]];


	_serverSignature = [[OFDataArray alloc] init];
	[_serverSignature addItems: [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 addItems: "p="
		count: 2];
	tmpString = [tmpArray stringByBase64Encoding];
	[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 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
			    exceptionWithConnection: nil
					     reason: @"Received wrong "







<
<
<

|
>







<
|
|












<
|
|







<
|
|
>
>
|
|
|
|





|
















|











|
<







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

396
397
398
399
400
401
402
	[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]];

	tmpArray = [OFMutableData dataWithItems: [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 = [OFMutableData dataWithItems: "Server Key"
					  count: 10];
	serverKey = [self XMPP_HMACWithKey: saltedPassword
				      data: tmpArray];

	/*
	 * IETF RFC 5802:
	 * ServerSignature := HMAC(ServerKey, AuthMessage)
	 */

	tmpArray = [OFMutableData dataWithItems: serverKey
					  count: [_hashType digestSize]];

	[_serverSignature release];
	_serverSignature = [[OFMutableData alloc]
	    initWithItems: [self XMPP_HMACWithKey: tmpArray
					     data: authMessage]
		    count: [_hashType digestSize]];

	/*
	 * IETF RFC 5802:
	 * ClientProof := ClientKey XOR ClientSignature
	 */
	tmpArray = [OFMutableData data];
	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;

	/*
	 * server-final-message already received,
	 * we were just waiting for the last word from the server
	 */
	if (_authenticated)
		return nil;

	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
			    exceptionWithConnection: nil
					     reason: @"Received wrong "
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
	}

	return [OFString stringWithCString: (char *)buf
				  encoding: OF_STRING_ENCODING_ASCII
				    length: 64];
}

- (const uint8_t *)XMPP_HMACWithKey: (OFDataArray *)key
			       data: (OFDataArray *)data
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFDataArray *k = [OFDataArray dataArray];
	size_t i, kSize, blockSize = [_hashType blockSize];
	uint8_t *kI = NULL, *kO = NULL;
	id <OFCryptoHash> hashI, hashO;

	if ([key itemSize] * [key count] > blockSize) {
		hashI = [[[_hashType alloc] init] autorelease];
		[hashI updateWithBuffer: [key items]







|
|


|







426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
	}

	return [OFString stringWithCString: (char *)buf
				  encoding: OF_STRING_ENCODING_ASCII
				    length: 64];
}

- (const uint8_t *)XMPP_HMACWithKey: (OFData *)key
			       data: (OFData *)data
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMutableData *k = [OFMutableData data];
	size_t i, kSize, blockSize = [_hashType blockSize];
	uint8_t *kI = NULL, *kO = NULL;
	id <OFCryptoHash> hashI, hashO;

	if ([key itemSize] * [key count] > blockSize) {
		hashI = [[[_hashType alloc] init] autorelease];
		[hashI updateWithBuffer: [key items]
486
487
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548

	[hashO retain];
	[pool release];

	return [[hashO autorelease] digest];
}

- (OFDataArray *)XMPP_hiWithData: (OFDataArray *)str
			    salt: (OFDataArray *)salt
		  iterationCount: (intmax_t)i
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	size_t digestSize = [_hashType digestSize];
	uint8_t *result = NULL;
	const uint8_t *u, *uOld;
	intmax_t j, k;
	OFDataArray *salty, *tmp, *ret;

	result = [self allocMemoryWithSize: digestSize];

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

		salty = [[salt copy] 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 = [[OFDataArray alloc] init];
			[tmp addItems: uOld
				count: digestSize];

			[pool releaseObjects]; // releases uOld and previous tmp
			[tmp autorelease];

			u = [self XMPP_HMACWithKey: str
					      data: tmp];

			for (k = 0; k < digestSize; k++)
				result[k] ^= u[k];

			uOld = u;
		}

		ret = [OFDataArray dataArray];
		[ret addItems: result
			count: digestSize];
	} @finally {
		[self freeMemory: result];
	}

	[ret retain];
	[pool release];

	return [ret autorelease];
}
@end







|
|
|






|

















|















<
|
|










481
482
483
484
485
486
487
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
529
530

531
532
533
534
535
536
537
538
539
540
541
542

	[hashO retain];
	[pool release];

	return [[hashO autorelease] digest];
}

- (OFData *)XMPP_hiWithData: (OFData *)str
		       salt: (OFData *)salt
	     iterationCount: (intmax_t)i
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	size_t digestSize = [_hashType digestSize];
	uint8_t *result = NULL;
	const uint8_t *u, *uOld;
	intmax_t j, k;
	OFMutableData *salty, *tmp, *ret;

	result = [self allocMemoryWithSize: digestSize];

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

		salty = [[salt copy] 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];

			[pool releaseObjects]; // releases uOld and previous tmp
			[tmp autorelease];

			u = [self XMPP_HMACWithKey: str
					      data: tmp];

			for (k = 0; k < digestSize; k++)
				result[k] ^= u[k];

			uOld = u;
		}


		ret = [OFMutableData dataWithItems: result
					     count: digestSize];
	} @finally {
		[self freeMemory: result];
	}

	[ret retain];
	[pool release];

	return [ret autorelease];
}
@end

Modified src/XMPPStanza.m from [26c7c49d18] to [813b227b64].

107
108
109
110
111
112
113
114

115
116
117
118
119
120
121
}

- initWithName: (OFString *)name
	  type: (OFString *)type
	    ID: (OFString *)ID
{
	self = [super initWithName: name
			 namespace: XMPP_NS_CLIENT];


	@try {
		if (![name isEqual: @"iq"] && ![name isEqual: @"message"] &&
		    ![name isEqual: @"presence"])
			@throw [OFInvalidArgumentException exception];

		[self setDefaultNamespace: XMPP_NS_CLIENT];







|
>







107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
}

- initWithName: (OFString *)name
	  type: (OFString *)type
	    ID: (OFString *)ID
{
	self = [super initWithName: name
			 namespace: XMPP_NS_CLIENT
		       stringValue: nil];

	@try {
		if (![name isEqual: @"iq"] && ![name isEqual: @"message"] &&
		    ![name isEqual: @"presence"])
			@throw [OFInvalidArgumentException exception];

		[self setDefaultNamespace: XMPP_NS_CLIENT];