TCP/TCPEndpoint+Certs.m
changeset 58 6577813acf12
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/TCP/TCPEndpoint+Certs.m	Fri Jul 03 17:50:28 2009 -0700
     1.3 @@ -0,0 +1,53 @@
     1.4 +//
     1.5 +//  TCPEndpoint+Certs.m
     1.6 +//  MYNetwork-iPhone
     1.7 +//
     1.8 +//  Created by Jens Alfke on 5/21/09.
     1.9 +//  Copyright 2009 Jens Alfke. All rights reserved.
    1.10 +//
    1.11 +
    1.12 +#import "TCPEndpoint.h"
    1.13 +#import "CollectionUtils.h"
    1.14 +#import <Security/Security.h>
    1.15 +
    1.16 +
    1.17 +/** These are some optional category methods for TCPEndpoint for dumping info about certificates.
    1.18 +    They're useful if you're working with SSL connections, but they do link against the Security
    1.19 +    framework, so they're moved into this extra file that you can choose to compile into your
    1.20 +    project or not.
    1.21 +*/
    1.22 +@implementation TCPEndpoint (Certificates)
    1.23 +
    1.24 +
    1.25 ++ (NSString*) describeCert: (SecCertificateRef)cert {
    1.26 +    if (!cert)
    1.27 +        return @"(null)";
    1.28 +    NSString *desc;
    1.29 +#if TARGET_OS_IPHONE && !defined(__SEC_TYPES__)
    1.30 +    CFStringRef summary = NULL;
    1.31 +    SecCertificateCopySubjectSummary(cert);
    1.32 +    desc = $sprintf(@"Certificate[%@]", summary);
    1.33 +    if(summary) CFRelease(summary);
    1.34 +#else
    1.35 +    CFStringRef name=NULL;
    1.36 +    CFArrayRef emails=NULL;
    1.37 +    SecCertificateCopyCommonName(cert, &name);
    1.38 +    SecCertificateCopyEmailAddresses(cert, &emails);
    1.39 +    desc = $sprintf(@"Certificate[\"%@\", <%@>]",
    1.40 +                    name, [(NSArray*)emails componentsJoinedByString: @">, <"]);
    1.41 +    if(name) CFRelease(name);
    1.42 +    if(emails) CFRelease(emails);
    1.43 +#endif
    1.44 +    return desc;
    1.45 +}
    1.46 +
    1.47 ++ (NSString*) describeIdentity: (SecIdentityRef)identity {
    1.48 +    if (!identity)
    1.49 +        return @"(null)";
    1.50 +    SecCertificateRef cert;
    1.51 +    SecIdentityCopyCertificate(identity, &cert);
    1.52 +    return $sprintf(@"Identity[%@]", [self describeCert: cert]);
    1.53 +}
    1.54 +
    1.55 +
    1.56 +@end