︙ | | |
17
18
19
20
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
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
75
76
77
78
79
80
81
|
17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#define XMPP_CONNECTION_M
#include <assert.h>
#include <stringprep.h>
#include <idna.h>
#import <ObjOpenSSL/SSLSocket.h>
#import "XMPPConnection.h"
#import "XMPPSCRAMAuth.h"
#import "XMPPPLAINAuth.h"
#import "XMPPStanza.h"
#import "XMPPJID.h"
#import "XMPPIQ.h"
#import "XMPPMessage.h"
#import "XMPPPresence.h"
#import "XMPPRoster.h"
#import "XMPPRoster_private.h"
#import "XMPPRosterItem.h"
#import "XMPPExceptions.h"
@interface XMPPConnection ()
- (void)XMPP_startStream;
- (void)XMPP_handleStanza: (OFXMLElement*)elem;
- (void)XMPP_sendAuth: (OFString*)name;
- (void)XMPP_handleIQ: (XMPPIQ*)iq;
- (void)XMPP_handleMessage: (XMPPMessage*)msg;
- (void)XMPP_handlePresence: (XMPPPresence*)pres;
- (void)XMPP_handleFeatures: (OFXMLElement*)elem;
- (void)XMPP_sendResourceBind;
- (void)XMPP_handleResourceBind: (XMPPIQ*)iq;
- (void)XMPP_sendSession;
- (void)XMPP_handleSession: (XMPPIQ*)iq;
- (void)XMPP_handleRoster: (XMPPIQ*)iq;
@end
@implementation XMPPConnection
@synthesize JID, port, delegate, roster;
- init
{
self = [super init];
@try {
sock = [[OFTCPSocket alloc] init];
parser = [[OFXMLParser alloc] init];
elementBuilder = [[OFXMLElementBuilder alloc] init];
port = 5222;
parser.delegate = self;
elementBuilder.delegate = self;
[parser setDelegate: self];
[elementBuilder setDelegate: self];
roster = [[XMPPRoster alloc] initWithConnection: self];
} @catch (id e) {
[self release];
@throw e;
}
|
︙ | | |
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
-
-
-
-
+
+
+
+
|
OFString *old = server;
char *srv;
Idna_rc rc;
if ((rc = idna_to_ascii_8z([server_ cString],
&srv, IDNA_USE_STD3_ASCII_RULES)) != IDNA_SUCCESS)
@throw [XMPPIDNATranslationFailedException
newWithClass: isa
connection: self
operation: @"ToASCII"
string: server_];
newWithClass: isa
connection: self
operation: @"ToASCII"
string: server_];
@try {
server = [[OFString alloc] initWithCString: srv];
} @finally {
free(srv);
}
|
︙ | | |
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
|
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
278
279
280
|
+
+
+
-
-
-
+
+
+
-
+
-
+
|
- (void)parser: (OFXMLParser*)p
didStartElement: (OFString*)name
withPrefix: (OFString*)prefix
namespace: (OFString*)ns
attributes: (OFArray*)attrs
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFEnumerator *enumerator;
OFXMLAttribute *attr;
if (![name isEqual: @"stream"] || ![prefix isEqual: @"stream"] ||
![ns isEqual: XMPP_NS_STREAM]) {
of_log(@"Did not get expected stream start!");
assert(0);
}
enumerator = [attrs objectEnumerator];
for (OFXMLAttribute *attr in attrs) {
if ([attr.name isEqual: @"from"] &&
![attr.stringValue isEqual: server]) {
while ((attr = [enumerator nextObject]) != nil) {
if ([[attr name] isEqual: @"from"] &&
![[attr stringValue] isEqual: server]) {
of_log(@"Got invalid from in stream start!");
assert(0);
}
}
parser.delegate = elementBuilder;
[parser setDelegate: elementBuilder];
[pool release];
}
- (void)elementBuilder: (OFXMLElementBuilder*)b
didBuildElement: (OFXMLElement*)elem
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
elem.defaultNamespace = XMPP_NS_CLIENT;
[elem setDefaultNamespace: XMPP_NS_CLIENT];
[elem setPrefix: @"stream"
forNamespace: XMPP_NS_STREAM];
of_log(@"In: %@", elem);
[self XMPP_handleStanza: elem];
|
︙ | | |
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
-
-
+
+
-
+
-
+
-
-
+
+
-
-
+
+
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
-
-
+
+
-
|
@"xmlns='" XMPP_NS_CLIENT @"' "
@"xmlns:stream='" XMPP_NS_STREAM @"' "
@"version='1.0'>", server];
}
- (void)XMPP_handleStanza: (OFXMLElement*)elem
{
if ([elem.namespace isEqual: XMPP_NS_CLIENT]) {
if ([elem.name isEqual: @"iq"]) {
if ([[elem namespace] isEqual: XMPP_NS_CLIENT]) {
if ([[elem name] isEqual: @"iq"]) {
[self XMPP_handleIQ: [XMPPIQ stanzaWithElement: elem]];
return;
}
if ([elem.name isEqual: @"message"]) {
if ([[elem name] isEqual: @"message"]) {
[self XMPP_handleMessage:
[XMPPMessage stanzaWithElement: elem]];
return;
}
if ([elem.name isEqual: @"presence"]) {
if ([[elem name] isEqual: @"presence"]) {
[self XMPP_handlePresence:
[XMPPPresence stanzaWithElement: elem]];
return;
}
assert(0);
}
if ([elem.namespace isEqual: XMPP_NS_STREAM]) {
if ([elem.name isEqual: @"features"]) {
if ([[elem namespace] isEqual: XMPP_NS_STREAM]) {
if ([[elem name] isEqual: @"features"]) {
[self XMPP_handleFeatures: elem];
return;
}
assert(0);
}
if ([elem.namespace isEqual: XMPP_NS_STARTTLS]) {
if ([elem.name isEqual: @"proceed"]) {
if ([[elem namespace] isEqual: XMPP_NS_STARTTLS]) {
if ([[elem name] isEqual: @"proceed"]) {
/* FIXME: Catch errors here */
sock = [[SSLSocket alloc] initWithSocket: sock];
/* Stream restart */
parser.delegate = self;
[parser setDelegate: self];
[self XMPP_startStream];
return;
}
if ([elem.name isEqual: @"failure"])
if ([[elem name] isEqual: @"failure"])
/* TODO: Find/create an exception to throw here */
@throw [OFException newWithClass: isa];
assert(0);
}
if ([elem.namespace isEqual: XMPP_NS_SASL]) {
if ([elem.name isEqual: @"challenge"]) {
if ([[elem namespace] isEqual: XMPP_NS_SASL]) {
if ([[elem name] isEqual: @"challenge"]) {
OFXMLElement *responseTag;
OFDataArray *challenge =
[OFDataArray dataArrayWithBase64EncodedString:
[elem.children.firstObject stringValue]];
[[[elem children] firstObject] stringValue]];
OFDataArray *response = [authModule
calculateResponseWithChallenge: challenge];
responseTag = [OFXMLElement
elementWithName: @"response"
namespace: XMPP_NS_SASL];
[responseTag addChild:
[OFXMLElement elementWithCharacters:
[response stringByBase64Encoding]]];
[self sendStanza: responseTag];
return;
}
if ([elem.name isEqual: @"success"]) {
if ([[elem name] isEqual: @"success"]) {
[authModule parseServerFinalMessage:
[OFDataArray dataArrayWithBase64EncodedString:
[elem.children.firstObject stringValue]]];
[[[elem children] firstObject] stringValue]]];
if ([delegate respondsToSelector:
@selector(connectionWasAuthenticated:)])
[delegate connectionWasAuthenticated: self];
/* Stream restart */
parser.delegate = self;
[parser setDelegate: self];
[self XMPP_startStream];
return;
}
if ([elem.name isEqual: @"failure"]) {
if ([[elem name] isEqual: @"failure"]) {
of_log(@"Auth failed!");
// FIXME: Do more parsing/handling
@throw [XMPPAuthFailedException
newWithClass: isa
connection: self
reason: elem.stringValue];
reason: [elem stringValue]];
}
assert(0);
}
assert(0);
}
- (void)XMPP_handleIQ: (XMPPIQ*)iq
{
BOOL handled = NO;
if ([iq.ID isEqual: bindID]) {
if ([[iq ID] isEqual: bindID]) {
[self XMPP_handleResourceBind: iq];
return;
}
if ([iq.ID isEqual: sessionID]) {
if ([[iq ID] isEqual: sessionID]) {
[self XMPP_handleSession: iq];
return;
}
if ([iq.ID isEqual: rosterID]) {
if ([[iq ID] isEqual: rosterID]) {
[self XMPP_handleRoster: iq];
return;
}
if ([delegate respondsToSelector: @selector(connection:didReceiveIQ:)])
handled = [delegate connection: self
didReceiveIQ: iq];
if (!handled) {
XMPPJID *from = iq.from;
XMPPJID *to = iq.to;
XMPPJID *from = [iq from];
XMPPJID *to = [iq to];
OFXMLElement *error;
[iq setType: @"error"];
iq.to = from;
[iq setTo: from];
[iq setFrom: to];
iq.from = to;
error = [OFXMLElement elementWithName: @"error"];
[error addAttributeWithName: @"type"
stringValue: @"cancel"];
[error addChild:
[OFXMLElement elementWithName: @"service-unavailable"
namespace: XMPP_NS_STANZAS]];
|
︙ | | |
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
491
492
493
494
495
496
497
498
|
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
|
-
-
-
-
+
+
+
+
-
-
+
+
-
-
+
+
+
+
+
+
-
+
|
[delegate connection: self
didReceivePresence: pres];
}
- (void)XMPP_handleFeatures: (OFXMLElement*)elem
{
OFXMLElement *starttls =
[elem elementsForName: @"starttls"
namespace: XMPP_NS_STARTTLS].firstObject;
OFXMLElement *bind = [elem elementsForName: @"bind"
namespace: XMPP_NS_BIND].firstObject;
[[elem elementsForName: @"starttls"
namespace: XMPP_NS_STARTTLS] firstObject];
OFXMLElement *bind = [[elem elementsForName: @"bind"
namespace: XMPP_NS_BIND] firstObject];
OFXMLElement *session =
[elem elementsForName: @"session"
namespace: XMPP_NS_SESSION].firstObject;
[[elem elementsForName: @"session"
namespace: XMPP_NS_SESSION] firstObject];
OFArray *mechs = [elem elementsForName: @"mechanisms"
namespace: XMPP_NS_SASL];
OFMutableArray *mechanisms = [OFMutableArray array];
if (starttls != nil) {
[self sendStanza:
[OFXMLElement elementWithName: @"starttls"
namespace: XMPP_NS_STARTTLS]];
return;
}
if (mechs.count > 0) {
for (OFXMLElement *mech in [mechs.firstObject children])
if ([mechs count] > 0) {
OFEnumerator *enumerator;
OFXMLElement *mech;
enumerator = [[[mechs firstObject] children] objectEnumerator];
while ((mech = [enumerator nextObject]) != nil)
[mechanisms addObject:
[mech.children.firstObject stringValue]];
[[[mech children] firstObject] stringValue]];
if ([mechanisms containsObject: @"SCRAM-SHA-1"]) {
authModule = [[XMPPSCRAMAuth alloc]
initWithAuthcid: username
password: password
hash: [OFSHA1Hash class]];
[self XMPP_sendAuth: @"SCRAM-SHA-1"];
|
︙ | | |
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
|
-
+
-
+
-
-
+
+
-
+
-
+
|
}
- (void)XMPP_handleResourceBind: (XMPPIQ*)iq
{
OFXMLElement *bindElem;
OFXMLElement *jidElem;
if (![iq.type isEqual: @"result"])
if (![[iq type] isEqual: @"result"])
assert(0);
bindElem = iq.children.firstObject;
bindElem = [[iq children] firstObject];
if (![bindElem.name isEqual: @"bind"] ||
![bindElem.namespace isEqual: XMPP_NS_BIND])
if (![[bindElem name] isEqual: @"bind"] ||
![[bindElem namespace] isEqual: XMPP_NS_BIND])
assert(0);
jidElem = bindElem.children.firstObject;
jidElem = [[bindElem children] firstObject];
JID = [[XMPPJID alloc] initWithString:
[jidElem.children.firstObject stringValue]];
[[[jidElem children] firstObject] stringValue]];
[bindID release];
bindID = nil;
if (needsSession) {
[self XMPP_sendSession];
return;
|
︙ | | |
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
|
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
|
-
+
|
[iq addChild: [OFXMLElement elementWithName: @"session"
namespace: XMPP_NS_SESSION]];
[self sendStanza: iq];
}
- (void)XMPP_handleSession: (XMPPIQ*)iq
{
if (![iq.type isEqual: @"result"])
if (![[iq type] isEqual: @"result"])
assert(0);
if ([delegate respondsToSelector: @selector(connection:wasBoundToJID:)])
[delegate connection: self
wasBoundToJID: JID];
[sessionID release];
|
︙ | | |
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
|
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
|
+
+
-
+
-
+
-
-
+
+
-
+
+
+
+
-
-
+
+
-
-
-
-
-
+
+
+
+
+
+
-
-
-
+
+
+
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
namespace: XMPP_NS_ROSTER]];
[self sendStanza: iq];
}
- (void)XMPP_handleRoster: (XMPPIQ*)iq
{
OFXMLElement *rosterElem;
OFEnumerator *enumerator;
OFXMLElement *elem;
if (![iq.type isEqual: @"result"])
if (![[iq type] isEqual: @"result"])
assert(0);
rosterElem = iq.children.firstObject;
rosterElem = [[iq children] firstObject];
if (![rosterElem.name isEqual: @"query"] ||
![rosterElem.namespace isEqual: XMPP_NS_ROSTER])
if (![[rosterElem name] isEqual: @"query"] ||
![[rosterElem namespace] isEqual: XMPP_NS_ROSTER])
assert(0);
for (OFXMLElement *elem in rosterElem.children) {
enumerator = [[rosterElem children] objectEnumerator];
while ((elem = [enumerator nextObject]) != nil) {
XMPPRosterItem *rosterItem;
OFMutableArray *groups = [OFMutableArray array];
OFEnumerator *groupEnumerator;
OFXMLElement *groupElem;
if (![elem.name isEqual: @"item"] ||
![elem.ns isEqual: XMPP_NS_ROSTER])
if (![[elem name] isEqual: @"item"] ||
![[elem namespace] isEqual: XMPP_NS_ROSTER])
continue;
rosterItem = [XMPPRosterItem rosterItem];
rosterItem.JID = [XMPPJID JIDWithString:
[elem attributeForName: @"jid"].stringValue];
rosterItem.name = [elem attributeForName: @"name"].stringValue;
rosterItem.subscription =
[elem attributeForName: @"subscription"].stringValue;
[rosterItem setJID: [XMPPJID JIDWithString:
[[elem attributeForName: @"jid"] stringValue]]];
[rosterItem setName:
[[elem attributeForName: @"name"] stringValue]];
[rosterItem setSubscription:
[[elem attributeForName: @"subscription"] stringValue]];
for (OFXMLElement *groupElem in
[elem elementsForName: @"group"
namespace: XMPP_NS_ROSTER])
groupEnumerator =
[[elem elementsForName: @"group"
namespace: XMPP_NS_ROSTER] objectEnumerator];
while ((groupElem = [groupEnumerator nextObject]) != nil)
[groups addObject:
[groupElem.children.firstObject stringValue]];
[[[groupElem children] firstObject] stringValue]];
if (groups.count > 0)
rosterItem.groups = groups;
if ([groups count] > 0)
[rosterItem setGroups: groups];
[roster XMPP_addRosterItem: rosterItem];
}
if ([delegate respondsToSelector:
@selector(connectionDidReceiveRoster:)])
[delegate connectionDidReceiveRoster: self];
[rosterID release];
rosterID = nil;
}
- (XMPPJID*)JID
{
return [[JID copy] autorelease];
}
- (void)setPort: (uint16_t)port_
{
port = port_;
}
- (uint16_t)port
{
return port;
}
- (void)setDelegate: (id <XMPPConnectionDelegate>)delegate_
{
id old = delegate;
delegate = [(id)delegate_ retain];
[old release];
}
- (id <XMPPConnectionDelegate>)delegate
{
return [[delegate retain] autorelease];
}
- (XMPPRoster*)roster
{
return [[roster retain] autorelease];
}
@end
@implementation OFObject (XMPPConnectionDelegate)
- (void)connectionWasAuthenticated: (XMPPConnection*)conn
{
}
- (void)connection: (XMPPConnection*)conn
wasBoundToJID: (XMPPJID*)jid
{
}
- (void)connectionDidReceiveRoster: (XMPPConnection*)conn
{
}
- (BOOL)connection: (XMPPConnection*)conn
didReceiveIQ: (XMPPIQ*)iq
{
return NO;
}
- (void)connection: (XMPPConnection*)conn
didReceivePresence: (XMPPPresence*)pres
{
}
- (void)connection: (XMPPConnection*)conn
didReceiveMessage: (XMPPMessage*)msg
{
}
- (void)connectionWasClosed: (XMPPConnection*)conn
{
}
@end
|