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