TCP/TCPEndpoint.m
changeset 32 b3254a2f6d6c
parent 0 9d67172bb323
child 49 20cccc7c26ee
     1.1 --- a/TCP/TCPEndpoint.m	Fri May 23 17:37:36 2008 -0700
     1.2 +++ b/TCP/TCPEndpoint.m	Wed Apr 29 13:57:10 2009 -0700
     1.3 @@ -7,8 +7,10 @@
     1.4  //
     1.5  
     1.6  #import "TCPEndpoint.h"
     1.7 -
     1.8 +#import "Test.h"
     1.9 +#import "CollectionUtils.h"
    1.10  #import "ExceptionUtils.h"
    1.11 +#import <Security/Security.h>
    1.12  
    1.13  
    1.14  NSString* const kTCPPropertySSLClientSideAuthentication = @"kTCPPropertySSLClientSideAuthentication";
    1.15 @@ -47,6 +49,15 @@
    1.16  - (NSString*) securityLevel                 {return [_sslProperties objectForKey: (id)kCFStreamSSLLevel];}
    1.17  - (void) setSecurityLevel: (NSString*)level {[self setSSLProperty: level forKey: (id)kCFStreamSSLLevel];}
    1.18  
    1.19 +- (void) setPeerToPeerIdentity: (SecIdentityRef)identity {
    1.20 +    Assert(identity);
    1.21 +    self.SSLProperties = $mdict(
    1.22 +             {(id)kCFStreamSSLLevel, NSStreamSocketSecurityLevelTLSv1},
    1.23 +             {kTCPPropertySSLCertificates, $array((id)identity)},
    1.24 +             {kTCPPropertySSLAllowsAnyRoot, $true},
    1.25 +             {kTCPPropertySSLPeerName, [NSNull null]},
    1.26 +             {kTCPPropertySSLClientSideAuthentication, $object(kTCPAlwaysAuthenticate)});
    1.27 +}
    1.28  
    1.29  - (void) tellDelegate: (SEL)selector withObject: (id)param
    1.30  {
    1.31 @@ -58,6 +69,37 @@
    1.32  }
    1.33  
    1.34  
    1.35 ++ (NSString*) describeCert: (SecCertificateRef)cert {
    1.36 +    if (!cert)
    1.37 +        return @"(null)";
    1.38 +    NSString *desc;
    1.39 +#if TARGET_OS_IPHONE && !defined(__SEC_TYPES__)
    1.40 +    CFStringRef summary = NULL;
    1.41 +    SecCertificateCopySubjectSummary(cert);
    1.42 +    desc = $sprintf(@"Certificate[%@]", summary);
    1.43 +    if(summary) CFRelease(summary);
    1.44 +#else
    1.45 +    CFStringRef name=NULL;
    1.46 +    CFArrayRef emails=NULL;
    1.47 +    SecCertificateCopyCommonName(cert, &name);
    1.48 +    SecCertificateCopyEmailAddresses(cert, &emails);
    1.49 +    desc = $sprintf(@"Certificate[\"%@\", <%@>]",
    1.50 +                              name, [(NSArray*)emails componentsJoinedByString: @">, <"]);
    1.51 +    if(name) CFRelease(name);
    1.52 +    if(emails) CFRelease(emails);
    1.53 +#endif
    1.54 +    return desc;
    1.55 +}
    1.56 +
    1.57 ++ (NSString*) describeIdentity: (SecIdentityRef)identity {
    1.58 +    if (!identity)
    1.59 +        return @"(null)";
    1.60 +    SecCertificateRef cert;
    1.61 +    SecIdentityCopyCertificate(identity, &cert);
    1.62 +    return $sprintf(@"Identity[%@]", [self describeCert: cert]);
    1.63 +}
    1.64 +
    1.65 +
    1.66  @end
    1.67  
    1.68