Changed the X.509 version number in generated certs from 1 to 3, so that SecCertificateCreateFromData on iPhone will accept them. :-/
5 // Created by Jens Alfke on 4/9/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
10 #import "MYCrypto_Private.h"
13 @implementation MYIdentity
16 /** Creates a MYIdentity object for an existing Keychain identity reference. */
17 + (MYIdentity*) identityWithIdentityRef: (SecIdentityRef)identityRef {
18 return [[[self alloc] initWithIdentityRef: identityRef] autorelease];
21 - (id) initWithIdentityRef: (SecIdentityRef)identityRef {
23 SecCertificateRef certificateRef;
24 if (!check(SecIdentityCopyCertificate(identityRef, &certificateRef), @"SecIdentityCopyCertificate")) {
28 self = [super initWithCertificateRef: certificateRef];
30 _identityRef = identityRef;
31 CFRetain(identityRef);
33 CFRelease(certificateRef);
38 - (id) initWithCertificateRef: (SecCertificateRef)certificateRef {
39 self = [super initWithCertificateRef: certificateRef];
41 #if !MYCRYPTO_USE_IPHONE_API
42 if (!check(SecIdentityCreateWithCertificate(NULL, certificateRef, &_identityRef),
43 @"SecIdentityCreateWithCertificate")) {
48 Assert(NO,@"-[MYIdentity initWithCertificateRef] isn't implemented for iPhone yet!");//FIX
56 if (_identityRef) CFRelease(_identityRef);
62 if (_identityRef) CFRelease(_identityRef);
67 @synthesize identityRef=_identityRef;
69 - (MYPrivateKey*) privateKey {
70 SecKeyRef keyRef = NULL;
71 if (!check(SecIdentityCopyPrivateKey(_identityRef, &keyRef), @"SecIdentityCopyPrivateKey"))
73 MYPrivateKey *privateKey = [[MYPrivateKey alloc] _initWithKeyRef: keyRef
74 publicKey: self.publicKey];
76 return [privateKey autorelease];
82 + (MYIdentity*) preferredIdentityForName: (NSString*)name
85 SecIdentityRef identityRef;
86 OSStatus err = SecIdentityCopyPreference((CFStringRef)name, 0, NULL, &identityRef);
87 if (err==errKCItemNotFound || !check(err,@"SecIdentityCopyPreference") || !identityRef)
89 return [self identityWithIdentityRef: identityRef];
92 - (BOOL) makePreferredIdentityForName: (NSString*)name {
94 return check(SecIdentitySetPreference(_identityRef, (CFStringRef)name, 0),
95 @"SecIdentitySetPreference");
98 #endif !TARGET_OS_IPHONE
105 Copyright (c) 2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
107 Redistribution and use in source and binary forms, with or without modification, are permitted
108 provided that the following conditions are met:
110 * Redistributions of source code must retain the above copyright notice, this list of conditions
111 and the following disclaimer.
112 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
113 and the following disclaimer in the documentation and/or other materials provided with the
116 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
117 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
118 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
119 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
120 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
121 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
122 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
123 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.