MYCertificate now checks validity of self-signed certs loaded from the keychain (because the Security framework doesn't validate self-signed certs.)
5 // Created by Jens Alfke on 4/9/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
10 #import "MYCrypto_Private.h"
14 @implementation MYIdentity
17 /** Creates a MYIdentity object for an existing Keychain identity reference. */
18 + (MYIdentity*) identityWithIdentityRef: (SecIdentityRef)identityRef {
19 return [[[self alloc] initWithIdentityRef: identityRef] autorelease];
22 - (id) initWithIdentityRef: (SecIdentityRef)identityRef {
24 SecCertificateRef certificateRef;
25 if (!check(SecIdentityCopyCertificate(identityRef, &certificateRef), @"SecIdentityCopyCertificate")) {
29 self = [super initWithCertificateRef: certificateRef];
31 _identityRef = identityRef;
32 CFRetain(identityRef);
34 CFRelease(certificateRef);
39 - (id) initWithCertificateRef: (SecCertificateRef)certificateRef {
40 self = [super initWithCertificateRef: certificateRef];
42 #if !MYCRYPTO_USE_IPHONE_API
43 if (!check(SecIdentityCreateWithCertificate(NULL, certificateRef, &_identityRef),
44 @"SecIdentityCreateWithCertificate")) {
49 MYSHA1Digest *keyDigest = self.publicKey.publicKeyDigest;
51 Warn(@"MYIdentity: Couldn't get key digest of cert %@",certificateRef);
55 _identityRef = [self.keychain identityWithDigest: keyDigest].identityRef;
57 Warn(@"MYIdentity: Couldn't look up identity for cert %@ with %@",certificateRef, keyDigest);
62 // Debugging: Make sure the cert is correct
63 SecCertificateRef identitysCert = NULL;
64 SecIdentityCopyCertificate(_identityRef, &identitysCert);
65 CFDataRef identitysData = SecCertificateCopyData(identitysCert);
66 AssertEqual(self.certificateData, (NSData*)identitysData);
67 CFRelease(identitysData);
68 CFRelease(identitysCert);
70 CFRetain(_identityRef);
78 if (_identityRef) CFRelease(_identityRef);
84 if (_identityRef) CFRelease(_identityRef);
89 @synthesize identityRef=_identityRef;
91 - (MYPrivateKey*) privateKey {
92 SecKeyRef keyRef = NULL;
93 if (!check(SecIdentityCopyPrivateKey(_identityRef, &keyRef), @"SecIdentityCopyPrivateKey"))
95 MYPrivateKey *privateKey = [[MYPrivateKey alloc] _initWithKeyRef: keyRef
96 publicKey: self.publicKey];
98 return [privateKey autorelease];
102 - (BOOL) removeFromKeychain {
103 return [self.privateKey removeFromKeychain] && [super removeFromKeychain];
107 #if !TARGET_OS_IPHONE
109 + (MYIdentity*) preferredIdentityForName: (NSString*)name
112 SecIdentityRef identityRef;
113 OSStatus err = SecIdentityCopyPreference((CFStringRef)name, 0, NULL, &identityRef);
114 if (err==errKCItemNotFound || !check(err,@"SecIdentityCopyPreference") || !identityRef)
116 return [self identityWithIdentityRef: identityRef];
119 - (BOOL) makePreferredIdentityForName: (NSString*)name {
121 return check(SecIdentitySetPreference(_identityRef, (CFStringRef)name, 0),
122 @"SecIdentitySetPreference");
125 #endif !TARGET_OS_IPHONE
132 Copyright (c) 2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
134 Redistribution and use in source and binary forms, with or without modification, are permitted
135 provided that the following conditions are met:
137 * Redistributions of source code must retain the above copyright notice, this list of conditions
138 and the following disclaimer.
139 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
140 and the following disclaimer in the documentation and/or other materials provided with the
143 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
144 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
145 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
146 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
147 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
148 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
149 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
150 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.