1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/MYCertificate-iPhone.m Sat Apr 04 20:42:03 2009 -0700
1.3 @@ -0,0 +1,77 @@
1.4 +//
1.5 +// MYCertificate-iPhone.m
1.6 +// MYCrypto-iPhone
1.7 +//
1.8 +// Created by Jens Alfke on 3/30/09.
1.9 +// Copyright 2009 Jens Alfke. All rights reserved.
1.10 +//
1.11 +
1.12 +#import "MYCertificate.h"
1.13 +#import "MYCrypto_Private.h"
1.14 +
1.15 +#if USE_IPHONE_API
1.16 +
1.17 +
1.18 +@implementation MYCertificate
1.19 +
1.20 +
1.21 +/** Creates a MYCertificate object for an existing Keychain certificate reference. */
1.22 +- (id) initWithCertificateRef: (SecCertificateRef)certificateRef {
1.23 + self = [super initWithKeychainItemRef: (SecKeychainItemRef)certificateRef];
1.24 + if (self) {
1.25 + _certificateRef = certificateRef; // superclass has already CFRetained it
1.26 + }
1.27 + return self;
1.28 +}
1.29 +
1.30 +/** Creates a MYCertificate object from exported key data, but does not add it to any keychain. */
1.31 +- (id) initWithCertificateData: (NSData*)data
1.32 +{
1.33 + SecCertificateRef certificateRef = SecCertificateCreateWithData(NULL, (CFDataRef)data);
1.34 + self = [self initWithCertificateRef: certificateRef];
1.35 + CFRelease(certificateRef);
1.36 + return self;
1.37 +}
1.38 +
1.39 +
1.40 +@synthesize certificateRef=_certificateRef;
1.41 +
1.42 +- (NSData*) certificateData {
1.43 + CFDataRef data = SecCertificateCopyData(_certificateRef);
1.44 + return data ?[(id)CFMakeCollectable(data) autorelease] :nil;
1.45 +}
1.46 +
1.47 +- (MYPublicKey*) publicKey {
1.48 + SecTrustRef trust = NULL;
1.49 + SecPolicyRef policy = SecPolicyCreateBasicX509();
1.50 + OSStatus err = SecTrustCreateWithCertificates((CFArrayRef)$array((id)_certificateRef),
1.51 + policy,
1.52 + &trust);
1.53 + CFRelease(policy);
1.54 + if (!check(err,@"SecTrustCreateWithCertificates"))
1.55 + return nil;
1.56 +
1.57 + MYPublicKey *key = nil;
1.58 + SecKeyRef keyRef = SecTrustCopyPublicKey(trust);
1.59 + if (keyRef) {
1.60 + key = [[[MYPublicKey alloc] initWithKeyRef: keyRef] autorelease];
1.61 + CFRelease(keyRef);
1.62 + }
1.63 + CFRelease(trust);
1.64 + return key;
1.65 +}
1.66 +
1.67 +
1.68 +- (NSString*) commonName {
1.69 + CFStringRef name = SecCertificateCopySubjectSummary(_certificateRef);
1.70 + return name ?[(id)CFMakeCollectable(name) autorelease] :nil;
1.71 +}
1.72 +
1.73 +- (NSArray*) emailAddresses {
1.74 + return nil; //FIX UNIMPLEMENTED
1.75 +}
1.76 +
1.77 +
1.78 +@end
1.79 +
1.80 +#endif USE_IPHONE_API