snej@0: // snej@0: // MYCertificate-iPhone.m snej@0: // MYCrypto-iPhone snej@0: // snej@0: // Created by Jens Alfke on 3/30/09. snej@0: // Copyright 2009 Jens Alfke. All rights reserved. snej@0: // snej@0: snej@0: #import "MYCertificate.h" snej@0: #import "MYCrypto_Private.h" snej@0: snej@2: #if MYCRYPTO_USE_IPHONE_API snej@0: snej@0: snej@0: @implementation MYCertificate snej@0: snej@0: snej@8: + (MYCertificate*) certificateWithCertificateRef: (SecCertificateRef)certificateRef { snej@8: return [[[self alloc] initWithCertificateRef: certificateRef] autorelease]; snej@8: } snej@8: snej@0: /** Creates a MYCertificate object for an existing Keychain certificate reference. */ snej@0: - (id) initWithCertificateRef: (SecCertificateRef)certificateRef { snej@0: self = [super initWithKeychainItemRef: (SecKeychainItemRef)certificateRef]; snej@0: if (self) { snej@0: _certificateRef = certificateRef; // superclass has already CFRetained it snej@0: } snej@0: return self; snej@0: } snej@0: snej@0: /** Creates a MYCertificate object from exported key data, but does not add it to any keychain. */ snej@0: - (id) initWithCertificateData: (NSData*)data snej@0: { snej@0: SecCertificateRef certificateRef = SecCertificateCreateWithData(NULL, (CFDataRef)data); snej@0: self = [self initWithCertificateRef: certificateRef]; snej@0: CFRelease(certificateRef); snej@0: return self; snej@0: } snej@0: snej@0: snej@8: - (BOOL)isEqualToCertificate:(MYCertificate*)cert { snej@8: return [self isEqual: cert] || [self.certificateData isEqual: cert.certificateData]; snej@8: } snej@8: snej@0: @synthesize certificateRef=_certificateRef; snej@0: snej@0: - (NSData*) certificateData { snej@0: CFDataRef data = SecCertificateCopyData(_certificateRef); snej@0: return data ?[(id)CFMakeCollectable(data) autorelease] :nil; snej@0: } snej@0: snej@0: - (MYPublicKey*) publicKey { snej@0: SecTrustRef trust = NULL; snej@0: SecPolicyRef policy = SecPolicyCreateBasicX509(); snej@0: OSStatus err = SecTrustCreateWithCertificates((CFArrayRef)$array((id)_certificateRef), snej@0: policy, snej@0: &trust); snej@0: CFRelease(policy); snej@0: if (!check(err,@"SecTrustCreateWithCertificates")) snej@0: return nil; snej@0: snej@0: MYPublicKey *key = nil; snej@0: SecKeyRef keyRef = SecTrustCopyPublicKey(trust); snej@0: if (keyRef) { snej@0: key = [[[MYPublicKey alloc] initWithKeyRef: keyRef] autorelease]; snej@0: CFRelease(keyRef); snej@0: } snej@0: CFRelease(trust); snej@0: return key; snej@0: } snej@0: snej@0: snej@0: - (NSString*) commonName { snej@0: CFStringRef name = SecCertificateCopySubjectSummary(_certificateRef); snej@0: return name ?[(id)CFMakeCollectable(name) autorelease] :nil; snej@0: } snej@0: snej@0: snej@0: @end snej@0: snej@2: #endif MYCRYPTO_USE_IPHONE_API