TCP/TCPEndpoint.m
author jim@Baldrick.local
Tue May 05 23:24:50 2009 -0700 (2009-05-05)
changeset 43 aab592ac36fc
parent 0 9d67172bb323
child 49 20cccc7c26ee
permissions -rw-r--r--
bug fixes and improvements to new bonjour classes and tcplistener, also added a static library target
jens@0
     1
//
jens@0
     2
//  BLIPEndpoint.m
jens@0
     3
//  MYNetwork
jens@0
     4
//
jens@0
     5
//  Created by Jens Alfke on 5/14/08.
jens@0
     6
//  Copyright 2008 Jens Alfke. All rights reserved.
jens@0
     7
//
jens@0
     8
jens@0
     9
#import "TCPEndpoint.h"
jens@26
    10
#import "Test.h"
jens@26
    11
#import "CollectionUtils.h"
jens@0
    12
#import "ExceptionUtils.h"
jens@26
    13
#import <Security/Security.h>
jens@0
    14
jens@0
    15
jens@0
    16
NSString* const kTCPPropertySSLClientSideAuthentication = @"kTCPPropertySSLClientSideAuthentication";
jens@0
    17
jens@0
    18
jens@0
    19
@implementation TCPEndpoint
jens@0
    20
jens@0
    21
jens@0
    22
- (void) dealloc
jens@0
    23
{
jens@0
    24
    [_sslProperties release];
jens@0
    25
    [super dealloc];
jens@0
    26
}
jens@0
    27
jens@0
    28
jens@0
    29
- (NSMutableDictionary*) SSLProperties {return _sslProperties;}
jens@0
    30
jens@0
    31
- (void) setSSLProperties: (NSMutableDictionary*)props
jens@0
    32
{
jens@0
    33
    if( props != _sslProperties ) {
jens@0
    34
        [_sslProperties release];
jens@0
    35
        _sslProperties = [props mutableCopy];
jens@0
    36
    }
jens@0
    37
}
jens@0
    38
jens@0
    39
- (void) setSSLProperty: (id)value forKey: (NSString*)key
jens@0
    40
{
jens@0
    41
    if( value ) {
jens@0
    42
        if( ! _sslProperties )
jens@0
    43
            _sslProperties = [[NSMutableDictionary alloc] init];
jens@0
    44
        [_sslProperties setObject: value forKey: key];
jens@0
    45
    } else
jens@0
    46
        [_sslProperties removeObjectForKey: key];
jens@0
    47
}
jens@0
    48
jens@0
    49
- (NSString*) securityLevel                 {return [_sslProperties objectForKey: (id)kCFStreamSSLLevel];}
jens@0
    50
- (void) setSecurityLevel: (NSString*)level {[self setSSLProperty: level forKey: (id)kCFStreamSSLLevel];}
jens@0
    51
jens@26
    52
- (void) setPeerToPeerIdentity: (SecIdentityRef)identity {
jens@26
    53
    Assert(identity);
jens@26
    54
    self.SSLProperties = $mdict(
jens@26
    55
             {(id)kCFStreamSSLLevel, NSStreamSocketSecurityLevelTLSv1},
jens@26
    56
             {kTCPPropertySSLCertificates, $array((id)identity)},
jens@26
    57
             {kTCPPropertySSLAllowsAnyRoot, $true},
jens@26
    58
             {kTCPPropertySSLPeerName, [NSNull null]},
jens@26
    59
             {kTCPPropertySSLClientSideAuthentication, $object(kTCPAlwaysAuthenticate)});
jens@26
    60
}
jens@0
    61
jens@0
    62
- (void) tellDelegate: (SEL)selector withObject: (id)param
jens@0
    63
{
jens@0
    64
    if( [_delegate respondsToSelector: selector] ) {
jens@0
    65
        @try{
jens@0
    66
            [_delegate performSelector: selector withObject: self withObject: param];
jens@0
    67
        }catchAndReport(@"%@ delegate",self.class);
jens@0
    68
    }
jens@0
    69
}
jens@0
    70
jens@0
    71
jens@26
    72
+ (NSString*) describeCert: (SecCertificateRef)cert {
jens@26
    73
    if (!cert)
jens@26
    74
        return @"(null)";
jens@26
    75
    NSString *desc;
jens@26
    76
#if TARGET_OS_IPHONE && !defined(__SEC_TYPES__)
jens@26
    77
    CFStringRef summary = NULL;
jens@26
    78
    SecCertificateCopySubjectSummary(cert);
jens@26
    79
    desc = $sprintf(@"Certificate[%@]", summary);
jens@26
    80
    if(summary) CFRelease(summary);
jens@26
    81
#else
jens@26
    82
    CFStringRef name=NULL;
jens@26
    83
    CFArrayRef emails=NULL;
jens@26
    84
    SecCertificateCopyCommonName(cert, &name);
jens@26
    85
    SecCertificateCopyEmailAddresses(cert, &emails);
jens@26
    86
    desc = $sprintf(@"Certificate[\"%@\", <%@>]",
jens@26
    87
                              name, [(NSArray*)emails componentsJoinedByString: @">, <"]);
jens@26
    88
    if(name) CFRelease(name);
jens@26
    89
    if(emails) CFRelease(emails);
jens@26
    90
#endif
jens@26
    91
    return desc;
jens@26
    92
}
jens@26
    93
jens@26
    94
+ (NSString*) describeIdentity: (SecIdentityRef)identity {
jens@26
    95
    if (!identity)
jens@26
    96
        return @"(null)";
jens@26
    97
    SecCertificateRef cert;
jens@26
    98
    SecIdentityCopyCertificate(identity, &cert);
jens@26
    99
    return $sprintf(@"Identity[%@]", [self describeCert: cert]);
jens@26
   100
}
jens@26
   101
jens@26
   102
jens@0
   103
@end
jens@0
   104
jens@0
   105
jens@0
   106
/*
jens@0
   107
 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
jens@0
   108
 
jens@0
   109
 Redistribution and use in source and binary forms, with or without modification, are permitted
jens@0
   110
 provided that the following conditions are met:
jens@0
   111
 
jens@0
   112
 * Redistributions of source code must retain the above copyright notice, this list of conditions
jens@0
   113
 and the following disclaimer.
jens@0
   114
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
jens@0
   115
 and the following disclaimer in the documentation and/or other materials provided with the
jens@0
   116
 distribution.
jens@0
   117
 
jens@0
   118
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
jens@0
   119
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
jens@0
   120
 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
jens@0
   121
 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jens@0
   122
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
jens@0
   123
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
jens@0
   124
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
jens@0
   125
 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jens@0
   126
 */