MYCertificate-iPhone.m
author Jens Alfke <jens@mooseyard.com>
Sat Jun 06 15:01:28 2009 -0700 (2009-06-06)
changeset 21 2c300b15b381
parent 8 4c0eafa7b233
child 23 39fec79de6e8
permissions -rw-r--r--
* Created class MYCertificateRequest, factored out of MYCertificateInfo.
* Added method to create a MYIdentity directly from a MYCertificateRequest.
* Added raw modulus+exponent accessor and initializer for MYPublicKey.
* Removed obsolete MYCertGen code, and the MYPrivateKey identity-creation method that used it.
     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 "MYCertificateInfo.h"
    11 #import "MYCrypto_Private.h"
    12 
    13 #if MYCRYPTO_USE_IPHONE_API
    14 
    15 
    16 @implementation MYCertificate
    17 
    18 
    19 + (MYCertificate*) certificateWithCertificateRef: (SecCertificateRef)certificateRef {
    20     return [[[self alloc] initWithCertificateRef: certificateRef] autorelease];
    21 }
    22 
    23 /** Creates a MYCertificate object for an existing Keychain certificate reference. */
    24 - (id) initWithCertificateRef: (SecCertificateRef)certificateRef {
    25     self = [super initWithKeychainItemRef: (SecKeychainItemRef)certificateRef];
    26     if (self) {
    27         _certificateRef = certificateRef;     // superclass has already CFRetained it
    28     }
    29     return self;
    30 }
    31 
    32 /** Creates a MYCertificate object from exported key data, but does not add it to any keychain. */
    33 - (id) initWithCertificateData: (NSData*)data
    34 {
    35     SecCertificateRef certificateRef = SecCertificateCreateWithData(NULL, (CFDataRef)data);
    36     self = [self initWithCertificateRef: certificateRef];
    37     CFRelease(certificateRef);
    38     return self;
    39 }
    40 
    41 - (void) dealloc
    42 {
    43     [_info release];
    44     [super dealloc];
    45 }
    46 
    47 
    48 - (BOOL)isEqualToCertificate:(MYCertificate*)cert {
    49     return [self isEqual: cert] || [self.certificateData isEqual: cert.certificateData];
    50 }
    51 
    52 @synthesize certificateRef=_certificateRef;
    53 
    54 - (NSData*) certificateData {
    55     CFDataRef data = SecCertificateCopyData(_certificateRef);
    56     return data ?[(id)CFMakeCollectable(data) autorelease] :nil;
    57 }
    58 
    59 - (MYPublicKey*) publicKey {
    60     SecTrustRef trust = NULL;
    61     SecPolicyRef policy = SecPolicyCreateBasicX509();
    62     OSStatus err = SecTrustCreateWithCertificates((CFArrayRef)$array((id)_certificateRef),
    63                                                   policy,
    64                                                   &trust);
    65     CFRelease(policy);
    66     if (!check(err,@"SecTrustCreateWithCertificates"))
    67         return nil;
    68     
    69     MYPublicKey *key = nil;
    70     SecKeyRef keyRef = SecTrustCopyPublicKey(trust);
    71     if (keyRef) {
    72         key = [[[MYPublicKey alloc] initWithKeyRef: keyRef] autorelease];
    73         CFRelease(keyRef);
    74     }
    75     CFRelease(trust);
    76     return key;
    77 }
    78 
    79 - (MYIdentity*) identity {
    80     return [self.keychain identityWithDigest: self.publicKey.publicKeyDigest];
    81 }
    82 
    83 
    84 - (MYCertificateInfo*) info {
    85     if (!_info) {
    86         NSError *error;
    87         _info = [[MYCertificateInfo alloc] initWithCertificateData: self.certificateData
    88                                                              error: &error];
    89         if (!_info)
    90             Warn(@"Couldn't parse certificate %@: %@", self, error);
    91     }
    92     return _info;
    93 }
    94 
    95 - (NSString*) commonName {
    96     CFStringRef name = SecCertificateCopySubjectSummary(_certificateRef);
    97     return name ?[(id)CFMakeCollectable(name) autorelease] :nil;
    98 }
    99 
   100 
   101 @end
   102 
   103 #endif MYCRYPTO_USE_IPHONE_API