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