* Created class MYCertificateRequest, factored out of MYCertificateInfo.
* Added method to create a MYIdentity directly from a MYCertificateRequest.
* Added raw modulus+exponent accessor and initializer for MYPublicKey.
* Removed obsolete MYCertGen code, and the MYPrivateKey identity-creation method that used it.
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);
39 - (id) initWithCertificateRef: (SecCertificateRef)certificateRef {
40 self = [super initWithCertificateRef: certificateRef];
42 if (!check(SecIdentityCreateWithCertificate(NULL, certificateRef, &_identityRef),
43 @"SecIdentityCreateWithCertificate")) {
54 if (_identityRef) CFRelease(_identityRef);
60 if (_identityRef) CFRelease(_identityRef);
65 @synthesize identityRef=_identityRef;
67 - (MYPrivateKey*) privateKey {
68 SecKeyRef keyRef = NULL;
69 if (!check(SecIdentityCopyPrivateKey(_identityRef, &keyRef), @"SecIdentityCopyPrivateKey"))
71 MYPrivateKey *privateKey = [[MYPrivateKey alloc] _initWithKeyRef: keyRef
72 publicKey: self.publicKey];
74 return [privateKey autorelease];
80 + (MYIdentity*) preferredIdentityForName: (NSString*)name
83 SecIdentityRef identityRef;
84 OSStatus err = SecIdentityCopyPreference((CFStringRef)name, 0, NULL, &identityRef);
85 if (err==errKCItemNotFound || !check(err,@"SecIdentityCopyPreference") || !identityRef)
87 return [self identityWithIdentityRef: identityRef];
90 - (BOOL) makePreferredIdentityForName: (NSString*)name {
92 return check(SecIdentitySetPreference(_identityRef, (CFStringRef)name, 0),
93 @"SecIdentitySetPreference");
96 #endif !TARGET_OS_IPHONE
103 Copyright (c) 2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
105 Redistribution and use in source and binary forms, with or without modification, are permitted
106 provided that the following conditions are met:
108 * Redistributions of source code must retain the above copyright notice, this list of conditions
109 and the following disclaimer.
110 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
111 and the following disclaimer in the documentation and/or other materials provided with the
114 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
115 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
116 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
117 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
118 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
119 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
120 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
121 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.