MYCertificate-iPhone.m
author snej@snej.local
Sun Apr 12 22:16:14 2009 -0700 (2009-04-12)
changeset 9 aa5eb3fd6ebf
parent 2 8982b8fada63
child 21 2c300b15b381
permissions -rw-r--r--
Doc touch-up
     1 //
     2 //  MYCertificate-iPhone.m
     3 //  MYCrypto-iPhone
     4 //
     5 //  Created by Jens Alfke on 3/30/09.
     6 //  Copyright 2009 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "MYCertificate.h"
    10 #import "MYCrypto_Private.h"
    11 
    12 #if MYCRYPTO_USE_IPHONE_API
    13 
    14 
    15 @implementation MYCertificate
    16 
    17 
    18 + (MYCertificate*) certificateWithCertificateRef: (SecCertificateRef)certificateRef {
    19     return [[[self alloc] initWithCertificateRef: certificateRef] autorelease];
    20 }
    21 
    22 /** Creates a MYCertificate object for an existing Keychain certificate reference. */
    23 - (id) initWithCertificateRef: (SecCertificateRef)certificateRef {
    24     self = [super initWithKeychainItemRef: (SecKeychainItemRef)certificateRef];
    25     if (self) {
    26         _certificateRef = certificateRef;     // superclass has already CFRetained it
    27     }
    28     return self;
    29 }
    30 
    31 /** Creates a MYCertificate object from exported key data, but does not add it to any keychain. */
    32 - (id) initWithCertificateData: (NSData*)data
    33 {
    34     SecCertificateRef certificateRef = SecCertificateCreateWithData(NULL, (CFDataRef)data);
    35     self = [self initWithCertificateRef: certificateRef];
    36     CFRelease(certificateRef);
    37     return self;
    38 }
    39 
    40 
    41 - (BOOL)isEqualToCertificate:(MYCertificate*)cert {
    42     return [self isEqual: cert] || [self.certificateData isEqual: cert.certificateData];
    43 }
    44 
    45 @synthesize certificateRef=_certificateRef;
    46 
    47 - (NSData*) certificateData {
    48     CFDataRef data = SecCertificateCopyData(_certificateRef);
    49     return data ?[(id)CFMakeCollectable(data) autorelease] :nil;
    50 }
    51 
    52 - (MYPublicKey*) publicKey {
    53     SecTrustRef trust = NULL;
    54     SecPolicyRef policy = SecPolicyCreateBasicX509();
    55     OSStatus err = SecTrustCreateWithCertificates((CFArrayRef)$array((id)_certificateRef),
    56                                                   policy,
    57                                                   &trust);
    58     CFRelease(policy);
    59     if (!check(err,@"SecTrustCreateWithCertificates"))
    60         return nil;
    61     
    62     MYPublicKey *key = nil;
    63     SecKeyRef keyRef = SecTrustCopyPublicKey(trust);
    64     if (keyRef) {
    65         key = [[[MYPublicKey alloc] initWithKeyRef: keyRef] autorelease];
    66         CFRelease(keyRef);
    67     }
    68     CFRelease(trust);
    69     return key;
    70 }
    71 
    72 
    73 - (NSString*) commonName {
    74     CFStringRef name = SecCertificateCopySubjectSummary(_certificateRef);
    75     return name ?[(id)CFMakeCollectable(name) autorelease] :nil;
    76 }
    77 
    78 
    79 @end
    80 
    81 #endif MYCRYPTO_USE_IPHONE_API