MYCertificate-iPhone.m
author snej@snej.local
Thu Apr 09 22:46:48 2009 -0700 (2009-04-09)
changeset 6 2d7692f9b6b4
parent 1 60e4cbbb5128
child 8 4c0eafa7b233
permissions -rw-r--r--
Updated the README for the 0.1 release.
     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 /** Creates a MYCertificate object for an existing Keychain certificate reference. */
    19 - (id) initWithCertificateRef: (SecCertificateRef)certificateRef {
    20     self = [super initWithKeychainItemRef: (SecKeychainItemRef)certificateRef];
    21     if (self) {
    22         _certificateRef = certificateRef;     // superclass has already CFRetained it
    23     }
    24     return self;
    25 }
    26 
    27 /** Creates a MYCertificate object from exported key data, but does not add it to any keychain. */
    28 - (id) initWithCertificateData: (NSData*)data
    29 {
    30     SecCertificateRef certificateRef = SecCertificateCreateWithData(NULL, (CFDataRef)data);
    31     self = [self initWithCertificateRef: certificateRef];
    32     CFRelease(certificateRef);
    33     return self;
    34 }
    35 
    36 
    37 @synthesize certificateRef=_certificateRef;
    38 
    39 - (NSData*) certificateData {
    40     CFDataRef data = SecCertificateCopyData(_certificateRef);
    41     return data ?[(id)CFMakeCollectable(data) autorelease] :nil;
    42 }
    43 
    44 - (MYPublicKey*) publicKey {
    45     SecTrustRef trust = NULL;
    46     SecPolicyRef policy = SecPolicyCreateBasicX509();
    47     OSStatus err = SecTrustCreateWithCertificates((CFArrayRef)$array((id)_certificateRef),
    48                                                   policy,
    49                                                   &trust);
    50     CFRelease(policy);
    51     if (!check(err,@"SecTrustCreateWithCertificates"))
    52         return nil;
    53     
    54     MYPublicKey *key = nil;
    55     SecKeyRef keyRef = SecTrustCopyPublicKey(trust);
    56     if (keyRef) {
    57         key = [[[MYPublicKey alloc] initWithKeyRef: keyRef] autorelease];
    58         CFRelease(keyRef);
    59     }
    60     CFRelease(trust);
    61     return key;
    62 }
    63 
    64 
    65 - (NSString*) commonName {
    66     CFStringRef name = SecCertificateCopySubjectSummary(_certificateRef);
    67     return name ?[(id)CFMakeCollectable(name) autorelease] :nil;
    68 }
    69 
    70 
    71 @end
    72 
    73 #endif MYCRYPTO_USE_IPHONE_API