ObjXMPP  Check-in [46f67c971b]

Overview
Comment:Adjust to ObjFW changes
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 46f67c971bc57abd330349d54d237931079a832baaf20909e77039f6ce4b896d
User & Date: js on 2021-04-28 23:20:55
Other Links: manifest | tags
Context
2021-04-29
00:06
Adjust to ObjFW style check-in: 9919057cb8 user: js tags: trunk
2021-04-28
23:20
Adjust to ObjFW changes check-in: 46f67c971b user: js tags: trunk
2020-01-04
00:59
Fix fallback to A/AAAA in case of missing SRV check-in: 985f2f71af user: js tags: trunk
Changes

Modified src/XMPPConnection.m from [7485f3a9a3] to [dc8d242d03].

1
2
3


4
5
6
7
8
9
10
1


2
3
4
5
6
7
8
9
10

-
-
+
+







/*
 * Copyright (c) 2010, 2011, 2012, 2013, 2015, 2016, 2017, 2018, 2019
 *   Jonathan Schleifer <js@heap.zone>
 * Copyright (c) 2010, 2011, 2012, 2013, 2015, 2016, 2017, 2018, 2019, 2021
 *   Jonathan Schleifer <js@nil.im>
 * Copyright (c) 2011, 2012, Florian Zeitz <florob@babelmonkeys.de>
 *
 * https://heap.zone/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
340
341
342
343
344
345
346
347
348


349
350
351
352
353
354
355
340
341
342
343
344
345
346


347
348
349
350
351
352
353
354
355







-
-
+
+







		[_socket asyncConnectToHost: _server
				       port: _port];
	else {
		OFString *SRVDomain = [_domainToASCII
		    stringByPrependingString: @"_xmpp-client._tcp."];
		OFDNSQuery *query = [OFDNSQuery
		    queryWithDomainName: SRVDomain
			       DNSClass: OF_DNS_CLASS_IN
			     recordType: OF_DNS_RECORD_TYPE_SRV];
			       DNSClass: OFDNSClassIN
			     recordType: OFDNSRecordTypeSRV];
		[[OFThread DNSResolver] asyncPerformQuery: query
						 delegate: self];
	}

	objc_autoreleasePoolPop(pool);
}

749
750
751
752
753
754
755
756

757
758
759
760
761
762
763
749
750
751
752
753
754
755

756
757
758
759
760
761
762
763







-
+







		SSLSocket *newSock;

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

		newSock = [[SSLSocket alloc] initWithSocket: _socket];
		newSock.certificateVerificationEnabled = false;
		newSock.verifiesCertificates = false;
#if 0
		/* FIXME: Not yet implemented by ObjOpenSSL */
		[newSock setCertificateFile: _certificateFile];
		[newSock setPrivateKeyFile: _privateKeyFile];
		[newSock setPrivateKeyPassphrase: _privateKeyPassphrase];
#endif
		[newSock startTLSWithExpectedHost: nil];
1080
1081
1082
1083
1084
1085
1086
1087

1088
1089
1090
1091
1092
1093
1094
1080
1081
1082
1083
1084
1085
1086

1087
1088
1089
1090
1091
1092
1093
1094







-
+







		  selector: @selector(xmpp_handleSessionForConnection:IQ:)];
}

- (void)xmpp_handleSessionForConnection: (XMPPConnection *)connection
				     IQ: (XMPPIQ *)IQ
{
	if (![IQ.type isEqual: @"result"])
		OF_ENSURE(0);
		OFEnsure(0);

	[_delegates broadcastSelector: @selector(connection:wasBoundToJID:)
			   withObject: self
			   withObject: _JID];
}

- (OFString *)xmpp_IDNAToASCII: (OFString *)domain

Modified src/XMPPContactManager.m from [f3f3c997c4] to [59c5b114c5].

1
2
3

4
5
6
7
8
9
10
1
2

3
4
5
6
7
8
9
10


-
+







/*
 * Copyright (c) 2013, Florian Zeitz <florob@babelmonkeys.de>
 * Copyright (c) 2013, 2016, 2019, Jonathan Schleifer <js@heap.zone>
 * Copyright (c) 2013, 2016, 2019, 2021, Jonathan Schleifer <js@nil.im>
 *
 * https://heap.zone/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
157
158
159
160
161
162
163

164
165
166
167
168
169
170







-







{
	XMPPContact *contact;
	XMPPJID *JID = presence.from;
	OFString *type = presence.type;

	/* Subscription request */
	if ([type isEqual: @"subscribe"]) {
		of_log(@"ObjXMPP: received subscription request");
		[_delegates broadcastSelector: @selector(contactManager:
						 didReceiveSubscriptionRequest:)
				   withObject: self
				   withObject: presence];
		return;
	}

Modified src/XMPPDiscoEntity.m from [24c15d2174] to [99bc6fd072].

1
2
3

4
5
6
7
8
9
10
1
2

3
4
5
6
7
8
9
10


-
+







/*
 * Copyright (c) 2013, Florian Zeitz <florob@babelmonkeys.de>
 * Copyright (c) 2013, 2016, 2019, Jonathan Schleifer <js@heap.zone>
 * Copyright (c) 2013, 2016, 2019, 2021, Jonathan Schleifer <js@nil.im>
 *
 * https://heap.zone/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
110
111
112
113
114
115
116
117

118
119
120
121
122
123
124
125
110
111
112
113
114
115
116

117

118
119
120
121
122
123
124







-
+
-







	[_discoNodes setObject: node
			forKey: node.node];
}

- (OFString *)capsHash
{
	OFMutableString *caps = [OFMutableString string];
	OFSHA1Hash *hash = [OFSHA1Hash
	OFSHA1Hash *hash = [OFSHA1Hash hashWithAllowsSwappableMemory: true];
	    cryptoHashWithAllowsSwappableMemory: true];
	OFData *digest;

	for (XMPPDiscoIdentity *identity in _identities)
		[caps appendFormat: @"%@/%@//%@<",
		    identity.category, identity.type, identity.name];

	for (OFString *feature in _features)

Modified src/XMPPDiscoIdentity.m from [45b58d387c] to [6198736245].

1
2
3

4
5
6
7
8
9
10
1
2

3
4
5
6
7
8
9
10


-
+







/*
 * Copyright (c) 2013, Florian Zeitz <florob@babelmonkeys.de>
 * Copyright (c) 2013, 2016, 2019, Jonathan Schleifer <js@heap.zone>
 * Copyright (c) 2013, 2016, 2019, 2021, Jonathan Schleifer <js@nil.im>
 *
 * https://heap.zone/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
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
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







-
+

-
+

-
+

-
-
-
+
+
+

-
+




-
+


-
+
-


-
+







-
+



-
+





	    (_name == identity->_name || [_name isEqual: identity->_name]) &&
	    [_type isEqual: identity->_type])
		return true;

	return false;
}

- (uint32_t)hash
- (unsigned long)hash
{
	uint32_t hash;
	unsigned long hash;

	OF_HASH_INIT(hash);
	OFHashInit(&hash);

	OF_HASH_ADD_HASH(hash, _category.hash);
	OF_HASH_ADD_HASH(hash, _type.hash);
	OF_HASH_ADD_HASH(hash, _name.hash);
	OFHashAddHash(&hash, _category.hash);
	OFHashAddHash(&hash, _type.hash);
	OFHashAddHash(&hash, _name.hash);

	OF_HASH_FINALIZE(hash);
	OFHashFinalize(&hash);

	return hash;
}

- (of_comparison_result_t)compare: (id <OFComparing>)object
- (OFComparisonResult)compare: (id <OFComparing>)object
{
	XMPPDiscoIdentity *identity;
	of_comparison_result_t categoryResult;
	OFComparisonResult categoryResult, typeResult;
	of_comparison_result_t typeResult;

	if (object == self)
		return OF_ORDERED_SAME;
		return OFOrderedSame;

	if (![(id)object isKindOfClass: [XMPPDiscoIdentity class]])
		@throw [OFInvalidArgumentException exception];

	identity = (XMPPDiscoIdentity *)object;

	categoryResult = [_category compare: identity->_category];
	if (categoryResult != OF_ORDERED_SAME)
	if (categoryResult != OFOrderedSame)
		return categoryResult;

	typeResult = [_type compare: identity->_type];
	if (typeResult != OF_ORDERED_SAME)
	if (typeResult != OFOrderedSame)
		return typeResult;

	return [_name compare: identity->_name];
}
@end

Modified src/XMPPFileStorage.m from [1e77051632] to [18e0ab5705].

1
2

3
4
5
6
7
8
9
1

2
3
4
5
6
7
8
9

-
+







/*
 * Copyright (c) 2012, 2019, Jonathan Schleifer <js@webkeks.org>
 * Copyright (c) 2012, 2019, 2021, Jonathan Schleifer <js@nil.im>
 *
 * https://heap.zone/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
46
47
48
49
50
51
52
53

54
55
56
57
58
59
60
46
47
48
49
50
51
52

53
54
55
56
57
58
59
60







-
+








	@try {
		void *pool = objc_autoreleasePoolPush();

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

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
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
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







-
+




-
+





-
+


-
+

-
+







	boolean = [[self xmpp_objectForPath: path] boolValue];

	objc_autoreleasePoolPop(pool);

	return boolean;
}

- (void)setIntegerValue: (intmax_t)integer
- (void)setIntegerValue: (long long)integer
		forPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();

	[self xmpp_setObject: [OFNumber numberWithIntMax: integer]
	[self xmpp_setObject: [OFNumber numberWithLongLong: integer]
		     forPath: path];

	objc_autoreleasePoolPop(pool);
}

- (intmax_t)integerValueForPath: (OFString *)path
- (long long)integerValueForPath: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	intmax_t integer;
	long long integer;

	integer = [[self xmpp_objectForPath: path] intMaxValue];
	integer = [[self xmpp_objectForPath: path] longLongValue];

	objc_autoreleasePoolPop(pool);

	return integer;
}

- (void)setArray: (OFArray *)array

Modified src/XMPPJID.m from [25d212cac9] to [3cb3be1d40].

1
2

3
4
5
6
7
8
9
1

2
3
4
5
6
7
8
9

-
+







/*
 * Copyright (c) 2011, 2012, 2013, 2019, Jonathan Schleifer <js@heap.zone>
 * Copyright (c) 2011, 2012, 2013, 2019, 2021, Jonathan Schleifer <js@nil.im>
 * Copyright (c) 2011, 2012, 2013, Florian Zeitz <florob@babelmonkeys.de>
 *
 * https://heap.zone/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
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
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







-
-
+
+





-
+





-
+








		nodesep = [string rangeOfString: @"@"].location;
		resourcesep = [string rangeOfString: @"/"].location;

		if (nodesep == SIZE_MAX)
			self.node = nil;
		else
			self.node =
			    [string substringWithRange: of_range(0, nodesep)];
			self.node = [string substringWithRange:
			    OFRangeMake(0, nodesep)];

		if (resourcesep == SIZE_MAX) {
			self.resource = nil;
			resourcesep = string.length;
		} else {
			of_range_t range = of_range(resourcesep + 1,
			OFRange range = OFRangeMake(resourcesep + 1,
			    string.length - resourcesep - 1);
			self.resource = [string substringWithRange: range];
		}

		self.domain = [string substringWithRange:
		    of_range(nodesep + 1, resourcesep - nodesep - 1)];
		    OFRangeMake(nodesep + 1, resourcesep - nodesep - 1)];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
231
232
233
234
235
236
237
238

239
240

241
242

243
244
245
246



247
248

249
250
251
252
231
232
233
234
235
236
237

238
239

240
241

242
243



244
245
246
247

248
249
250
251
252







-
+

-
+

-
+

-
-
-
+
+
+

-
+




	    [_domain isEqual: JID->_domain] && (_resource == JID->_resource ||
	    [_resource isEqual: JID->_resource]))
		return true;

	return false;
}

- (uint32_t)hash
- (unsigned long)hash
{
	uint32_t hash;
	unsigned long hash;

	OF_HASH_INIT(hash);
	OFHashInit(&hash);

	OF_HASH_ADD_HASH(hash, _node.hash);
	OF_HASH_ADD_HASH(hash, _domain.hash);
	OF_HASH_ADD_HASH(hash, _resource.hash);
	OFHashAddHash(&hash, _node.hash);
	OFHashAddHash(&hash, _domain.hash);
	OFHashAddHash(&hash, _resource.hash);

	OF_HASH_FINALIZE(hash);
	OFHashFinalize(&hash);

	return hash;
}
@end

Modified src/XMPPPresence.m from [47e770589e] to [a2a1eacbe7].

1
2


3
4
5
6
7
8
9
1

2
3
4
5
6
7
8
9
10

-
+
+







/*
 * Copyright (c) 2011, 2012, 2013, 2016, 2019, Jonathan Schleifer <js@heap.zone>
 * Copyright (c) 2011, 2012, 2013, 2016, 2019, 2021,
 *   Jonathan Schleifer <js@nil.im>
 * Copyright (c) 2011, 2012, 2013, Florian Zeitz <florob@babelmonkeys.de>
 *
 * https://heap.zone/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
39
40
41
42
43
44
45
46

47
48
49
50
51
52
53
40
41
42
43
44
45
46

47
48
49
50
51
52
53
54







-
+







	if ([show isEqual: @"away"])
		return 2;
	if ([show isEqual: @"dnd"])
		return 3;
	if ([show isEqual: @"xa"])
		return 4;

	OF_ENSURE(0);
	OFEnsure(0);
}

@implementation XMPPPresence
@synthesize status = _status, show = _show, priority = _priority;

+ (instancetype)presence
{
110
111
112
113
114
115
116
117
118


119
120
121
122
123
124
125
111
112
113
114
115
116
117


118
119
120
121
122
123
124
125
126







-
-
+
+








		if ((subElement = [element elementForName: @"status"
						namespace: XMPP_NS_CLIENT]))
			self.status = subElement.stringValue;

		if ((subElement = [element elementForName: @"priority"
						namespace: XMPP_NS_CLIENT]))
			self.priority = [OFNumber
			    numberWithIntMax: subElement.decimalValue];
			self.priority = [OFNumber numberWithLongLong:
			    [subElement longLongValueWithBase: 10]];
	} @catch (id e) {
		[self release];
		@throw e;
	}

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







-
+












-
+









-
+




-
+


-
+







-
+





-
+

-
+




-
+


-
+

-
+


	old = _status;
	_status = [status copy];
	[old release];
}

- (void)setPriority: (OFNumber *)priority
{
	intmax_t prio = priority.intMaxValue;
	long long prio = priority.longLongValue;
	OFNumber *old;

	if ((prio < -128) || (prio > 127))
		@throw [OFInvalidArgumentException exception];

	OFXMLElement *oldPriority = [self elementForName: @"priority"
					       namespace: XMPP_NS_CLIENT];

	if (oldPriority != nil)
		[self removeChild: oldPriority];

	OFString *priority_s =
	    [OFString stringWithFormat: @"%" @PRId8, priority.int8Value];
	    [OFString stringWithFormat: @"%hhd", priority.charValue];
	[self addChild: [OFXMLElement elementWithName: @"priority"
					    namespace: XMPP_NS_CLIENT
					  stringValue: priority_s]];

	old = _priority;
	_priority = [priority copy];
	[old release];
}

- (of_comparison_result_t)compare: (id <OFComparing>)object
- (OFComparisonResult)compare: (id <OFComparing>)object
{
	XMPPPresence *otherPresence;
	OFNumber *otherPriority;
	OFString *otherShow;
	of_comparison_result_t priorityOrder;
	OFComparisonResult priorityOrder;

	if (object == self)
		return OF_ORDERED_SAME;
		return OFOrderedSame;

	if (![(id)object isKindOfClass: [XMPPPresence class]])
		@throw [OFInvalidArgumentException exception];

	otherPresence = (XMPPPresence *)object;
	otherPriority = otherPresence.priority;
	if (otherPriority == nil)
		otherPriority = [OFNumber numberWithInt8: 0];
		otherPriority = [OFNumber numberWithChar: 0];

	if (_priority != nil)
		priorityOrder = [_priority compare: otherPriority];
	else
		priorityOrder =
		    [[OFNumber numberWithInt8: 0] compare: otherPriority];
		    [[OFNumber numberWithChar: 0] compare: otherPriority];

	if (priorityOrder != OF_ORDERED_SAME)
	if (priorityOrder != OFOrderedSame)
		return priorityOrder;

	otherShow = otherPresence.show;
	if ([_show isEqual: otherShow])
		return OF_ORDERED_SAME;
		return OFOrderedSame;

	if (showToInt(_show) < showToInt(otherShow))
		return OF_ORDERED_ASCENDING;
		return OFOrderedAscending;

	return OF_ORDERED_DESCENDING;
	return OFOrderedDescending;
}
@end

Modified src/XMPPSCRAMAuth.m from [9093752bba] to [b14abcbfc5].

1
2
3

4
5
6
7
8
9
10
1
2

3
4
5
6
7
8
9
10


-
+







/*
 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
 * Copyright (c) 2011, 2019, Jonathan Schleifer <js@webkeks.org>
 * Copyright (c) 2011, 2019, 2021, Jonathan Schleifer <js@nil.im>
 *
 * https://heap.zone/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
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
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







-
-
+
+




















-
+














-
+







	return [ret autorelease];
}

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

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

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

	for (OFString *component in
	    [challenge componentsSeparatedByString: @","]) {
		OFString *entry = [component substringWithRange:
		    of_range(2, component.length - 2)];
		    OFRangeMake(2, component.length - 2)];

		if ([component hasPrefix: @"r="]) {
			if (![entry hasPrefix: _cNonce])
				@throw [XMPPAuthFailedException
				    exceptionWithConnection: nil
						     reason: @"Received wrong "
							     @"nonce"];

			sNonce = entry;
			got |= GOT_SNONCE;
		} else if ([component hasPrefix: @"s="]) {
			salt = [OFData dataWithBase64EncodedString: entry];
			got |= GOT_SALT;
		} else if ([component hasPrefix: @"i="]) {
			iterCount = entry.decimalValue;
			iterCount = [entry longLongValueWithBase: 10];
			got |= GOT_ITERCOUNT;
		}
	}

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

393
394
395
396
397
398
399
400

401
402
403
404
405
406
407
393
394
395
396
397
398
399

400
401
402
403
404
405
406
407







-
+







	 * 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)];
	value = [mess substringWithRange: OFRangeMake(2, mess.length - 2)];

	if ([mess hasPrefix: @"v="]) {
		if (![value isEqual: _serverSignature.stringByBase64Encoding])
			@throw [XMPPAuthFailedException
			    exceptionWithConnection: nil
					     reason: @"Received wrong "
						     @"ServerSignature"];
426
427
428
429
430
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
471

472
473
474
475
476
477

478
479

480
481
482
483


484
485
486
487
488
489
490
426
427
428
429
430
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

471

472
473
474
475

476


477

478


479
480
481
482
483
484
485
486
487







-
+










-
+












-
-
+
+












-
+
-




-
+
-
-
+
-

-
-
+
+








		// ...so we can use it to replace ','
		if (buf[i] == ',')
			buf[i] = '~';
	}

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

- (const uint8_t *)xmpp_HMACWithKey: (OFData *)key
			       data: (OFData *)data
{
	void *pool = objc_autoreleasePoolPush();
	OFMutableData *k = [OFMutableData data];
	size_t i, kSize, blockSize = [_hashType blockSize];
	uint8_t *kI = NULL, *kO = NULL;
	id <OFCryptoHash> hashI, hashO;
	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 = [self allocMemoryWithSize: blockSize];
		kO = [self allocMemoryWithSize: blockSize];
		kI = OFAllocMemory(1, blockSize);
		kO = OFAllocMemory(1, blockSize);

		kSize = k.count;
		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: kI
		[hashI updateWithBuffer: kI length: blockSize];
				 length: blockSize];
		[hashI updateWithBuffer: data.items
				 length: data.itemSize * data.count];

		hashO = [[[_hashType alloc] init] autorelease];
		[hashO updateWithBuffer: kO
		[hashO updateWithBuffer: kO length: blockSize];
				 length: blockSize];
		[hashO updateWithBuffer: hashI.digest
		[hashO updateWithBuffer: hashI.digest length: hashI.digestSize];
				 length: hashI.digestSize];
	} @finally {
		[self freeMemory: kI];
		[self freeMemory: kO];
		OFFreeMemory(kI);
		OFFreeMemory(kO);
	}

	[hashO retain];

	objc_autoreleasePoolPop(pool);

	return [[hashO autorelease] digest];
498
499
500
501
502
503
504
505

506
507
508
509
510
511
512
495
496
497
498
499
500
501

502
503
504
505
506
507
508
509







-
+







	size_t digestSize = [_hashType digestSize];
	uint8_t *result = NULL;
	const uint8_t *u, *uOld;
	intmax_t j, k;
	OFMutableData *salty, *tmp;
	OFData *ret;

	result = [self allocMemoryWithSize: digestSize];
	result = OFAllocMemory(1, digestSize);

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

		salty = [[salt mutableCopy] autorelease];
		[salty addItems: "\0\0\0\1"
			  count: 4];
535
536
537
538
539
540
541
542

543
544
545
546
547
548
549
550
551
532
533
534
535
536
537
538

539
540
541
542
543
544
545
546
547
548







-
+










			uOld = u;
		}

		ret = [OFData dataWithItems: result
				      count: digestSize];
	} @finally {
		[self freeMemory: result];
		OFFreeMemory(result);
	}

	[ret retain];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}
@end

Modified src/XMPPStorage.h from [1c089b9216] to [d938a6605c].

1
2

3
4
5
6
7
8
9
1

2
3
4
5
6
7
8
9

-
+







/*
 * Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org>
 * Copyright (c) 2012, 2021, Jonathan Schleifer <js@nil.im>
 *
 * https://heap.zone/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
32
33
34
35
36
37
38
39

40
41

42
43
44
45
46
47
48
49
50
32
33
34
35
36
37
38

39
40

41
42
43
44
45
46
47
48
49
50







-
+

-
+









- (void)save;
- (void)setStringValue: (nullable OFString *)string
	       forPath: (OFString *)path;
- (nullable OFString *)stringValueForPath: (OFString *)path;
- (void)setBooleanValue: (bool)boolean
		forPath: (OFString *)path;
- (bool)booleanValueForPath: (OFString *)path;
- (void)setIntegerValue: (intmax_t)integer
- (void)setIntegerValue: (long long)integer
		forPath: (OFString *)path;
- (intmax_t)integerValueForPath: (OFString *)path;
- (long long)integerValueForPath: (OFString *)path;
- (void)setArray: (nullable OFArray *)array
	 forPath: (OFString *)path;
- (nullable OFArray *)arrayForPath: (OFString *)path;
- (void)setDictionary: (nullable OFDictionary *)dictionary
	      forPath: (OFString *)path;
- (nullable OFDictionary *)dictionaryForPath: (OFString *)path;
@end

OF_ASSUME_NONNULL_END

Modified tests/test.m from [6b93900f41] to [c9e6f90b6f].

1
2

3
4
5
6
7
8
9
1

2
3
4
5
6
7
8
9

-
+







/*
 * Copyright (c) 2010, 2011, 2019, Jonathan Schleifer <js@webkeks.org>
 * Copyright (c) 2010, 2011, 2019, 2021, Jonathan Schleifer <js@nil.im>
 * Copyright (c) 2011, 2012, Florian Zeitz <florob@babelmonkeys.de>
 *
 * https://heap.zone/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
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
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







-
+










-
+



-
+







- (void)applicationDidFinishLaunching
{
	OFArray *arguments = [OFApplication arguments];

	XMPPPresence *pres = [XMPPPresence presence];
	pres.show = @"xa";
	pres.status = @"Bored";
	pres.priority = [OFNumber numberWithInt8: 20];
	pres.priority = [OFNumber numberWithChar: 20];
	pres.to = [XMPPJID JIDWithString: @"alice@example.com"];
	pres.from = [XMPPJID JIDWithString: @"bob@example.org"];
	assert([pres.XMLString isEqual: @"<presence to='alice@example.com' "
	    @"from='bob@example.org'><show>xa</show>"
	    @"<status>Bored</status><priority>20</priority>"
	    @"</presence>"]);

	XMPPPresence *pres2 = [XMPPPresence presence];
	pres2.show = @"away";
	pres2.status = @"Bored";
	pres2.priority = [OFNumber numberWithInt8: 23];
	pres2.priority = [OFNumber numberWithChar: 23];
	pres2.to = [XMPPJID JIDWithString: @"alice@example.com"];
	pres2.from = [XMPPJID JIDWithString: @"bob@example.org"];

	assert([pres compare: pres2] == OF_ORDERED_ASCENDING);
	assert([pres compare: pres2] == OFOrderedAscending);

	XMPPMessage *msg = [XMPPMessage messageWithType: @"chat"];
	msg.body = @"Hello everyone";
	msg.to = [XMPPJID JIDWithString: @"jdev@conference.jabber.org"];
	msg.from = [XMPPJID JIDWithString: @"alice@example.com"];
	assert([msg.XMLString isEqual: @"<message type='chat' "
	    @"to='jdev@conference.jabber.org' "
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
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







-
+














-
+





-
+




-
+





-
-
+
+








	roster = [[XMPPRoster alloc] initWithConnection: conn];
	[roster addDelegate: self];

	[[XMPPStreamManagement alloc] initWithConnection: conn];

	if (arguments.count != 3) {
		of_log(@"Invalid count of command line arguments!");
		OFLog(@"Invalid count of command line arguments!");
		[OFApplication terminateWithStatus: 1];
	}

	conn.domain = [arguments objectAtIndex: 0];
	conn.username = [arguments objectAtIndex: 1];
	conn.password = [arguments objectAtIndex: 2];
	conn.resource = @"ObjXMPP";

	[conn asyncConnect];
}

-  (void)connection: (XMPPConnection *)conn
  didReceiveElement: (OFXMLElement *)element
{
	of_log(@"In:  %@", element);
	OFLog(@"In:  %@", element);
}

- (void)connection: (XMPPConnection *)conn
    didSendElement: (OFXMLElement *)element
{
	of_log(@"Out: %@", element);
	OFLog(@"Out: %@", element);
}

- (void)connectionWasAuthenticated: (XMPPConnection *)conn
{
	of_log(@"Auth successful");
	OFLog(@"Auth successful");
}

- (void)connection: (XMPPConnection *)conn_
     wasBoundToJID: (XMPPJID *)JID
{
	of_log(@"Bound to JID: %@", JID.fullJID);
	of_log(@"Supports SM: %@",
	OFLog(@"Bound to JID: %@", JID.fullJID);
	OFLog(@"Supports SM: %@",
	    conn_.supportsStreamManagement ? @"true" : @"false");

	XMPPDiscoEntity *discoEntity =
	    [[XMPPDiscoEntity alloc] initWithConnection: conn];

	[discoEntity addIdentity:
	    [XMPPDiscoIdentity identityWithCategory: @"client"
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
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







-
+


-
+











-
+









-
-
-
+
+
+

-
+







-
+





-
+







-
+





-
+











-
+


	[roster requestRoster];
}

- (void)rosterWasReceived: (XMPPRoster *)roster_
{
	XMPPPresence *pres;

	of_log(@"Got roster: %@", roster_.rosterItems);
	OFLog(@"Got roster: %@", roster_.rosterItems);

	pres = [XMPPPresence presence];
	pres.priority = [OFNumber numberWithInt8: 10];
	pres.priority = [OFNumber numberWithChar: 10];
	pres.status = @"ObjXMPP test is working!";

	[conn sendStanza: pres];

#ifdef OF_HAVE_BLOCKS
	XMPPIQ *IQ = [XMPPIQ IQWithType: @"get"
				     ID: [conn generateStanzaID]];
	[IQ addChild: [OFXMLElement elementWithName: @"ping"
					  namespace: @"urn:xmpp:ping"]];
	[conn	   sendIQ: IQ
	    callbackBlock: ^ (XMPPConnection *c, XMPPIQ *resp) {
		of_log(@"Ping response: %@", resp);
		OFLog(@"Ping response: %@", resp);
	}];
#endif
}

- (void)connectionDidUpgradeToTLS: (XMPPConnection *)conn_
{
	OFString *reason;

	if (![conn_ checkCertificateAndGetReason: &reason]) {
		[of_stdout writeString: @"Couldn't verify certificate: "];
		[of_stdout writeFormat: @"%@\n", reason];
		[of_stdout writeString: @"Do you want to continue [y/N]? "];
		[OFStdOut writeString: @"Couldn't verify certificate: "];
		[OFStdOut writeFormat: @"%@\n", reason];
		[OFStdOut writeString: @"Do you want to continue [y/N]? "];

		if (![[of_stdin readLine] hasPrefix: @"y"])
		if (![[OFStdIn readLine] hasPrefix: @"y"])
			[OFApplication terminateWithStatus: 1];
	}
}

-         (void)roster: (XMPPRoster *)roster_
  didReceiveRosterItem: (XMPPRosterItem *)rosterItem
{
	of_log(@"Got roster push: %@", rosterItem);
	OFLog(@"Got roster push: %@", rosterItem);
}

- (bool)connection: (XMPPConnection *)conn
      didReceiveIQ: (XMPPIQ *)iq
{
	of_log(@"IQ: %@", iq);
	OFLog(@"IQ: %@", iq);

	return NO;
}

-  (void)connection: (XMPPConnection *)conn
  didReceiveMessage: (XMPPMessage *)msg
{
	of_log(@"Message: %@", msg);
	OFLog(@"Message: %@", msg);
}

-   (void)connection: (XMPPConnection *)conn
  didReceivePresence: (XMPPPresence *)pres
{
	of_log(@"Presence: %@", pres);
	OFLog(@"Presence: %@", pres);
}

-  (void)connection: (XMPPConnection *)conn
  didThrowException: (id)e
{
	@throw e;
}

- (void)connectionWasClosed: (XMPPConnection *)conn
		      error: (OFXMLElement *)error
{
	of_log(@"Connection was closed: %@", error);
	OFLog(@"Connection was closed: %@", error);
}
@end