author | snej@snej.local |
Sun Apr 12 22:16:14 2009 -0700 (2009-04-12) | |
changeset 9 | aa5eb3fd6ebf |
parent 2 | 8982b8fada63 |
child 21 | 2c300b15b381 |
permissions | -rw-r--r-- |
snej@0 | 1 |
// |
snej@0 | 2 |
// MYCertificate-iPhone.m |
snej@0 | 3 |
// MYCrypto-iPhone |
snej@0 | 4 |
// |
snej@0 | 5 |
// Created by Jens Alfke on 3/30/09. |
snej@0 | 6 |
// Copyright 2009 Jens Alfke. All rights reserved. |
snej@0 | 7 |
// |
snej@0 | 8 |
|
snej@0 | 9 |
#import "MYCertificate.h" |
snej@0 | 10 |
#import "MYCrypto_Private.h" |
snej@0 | 11 |
|
snej@2 | 12 |
#if MYCRYPTO_USE_IPHONE_API |
snej@0 | 13 |
|
snej@0 | 14 |
|
snej@0 | 15 |
@implementation MYCertificate |
snej@0 | 16 |
|
snej@0 | 17 |
|
snej@8 | 18 |
+ (MYCertificate*) certificateWithCertificateRef: (SecCertificateRef)certificateRef { |
snej@8 | 19 |
return [[[self alloc] initWithCertificateRef: certificateRef] autorelease]; |
snej@8 | 20 |
} |
snej@8 | 21 |
|
snej@0 | 22 |
/** Creates a MYCertificate object for an existing Keychain certificate reference. */ |
snej@0 | 23 |
- (id) initWithCertificateRef: (SecCertificateRef)certificateRef { |
snej@0 | 24 |
self = [super initWithKeychainItemRef: (SecKeychainItemRef)certificateRef]; |
snej@0 | 25 |
if (self) { |
snej@0 | 26 |
_certificateRef = certificateRef; // superclass has already CFRetained it |
snej@0 | 27 |
} |
snej@0 | 28 |
return self; |
snej@0 | 29 |
} |
snej@0 | 30 |
|
snej@0 | 31 |
/** Creates a MYCertificate object from exported key data, but does not add it to any keychain. */ |
snej@0 | 32 |
- (id) initWithCertificateData: (NSData*)data |
snej@0 | 33 |
{ |
snej@0 | 34 |
SecCertificateRef certificateRef = SecCertificateCreateWithData(NULL, (CFDataRef)data); |
snej@0 | 35 |
self = [self initWithCertificateRef: certificateRef]; |
snej@0 | 36 |
CFRelease(certificateRef); |
snej@0 | 37 |
return self; |
snej@0 | 38 |
} |
snej@0 | 39 |
|
snej@0 | 40 |
|
snej@8 | 41 |
- (BOOL)isEqualToCertificate:(MYCertificate*)cert { |
snej@8 | 42 |
return [self isEqual: cert] || [self.certificateData isEqual: cert.certificateData]; |
snej@8 | 43 |
} |
snej@8 | 44 |
|
snej@0 | 45 |
@synthesize certificateRef=_certificateRef; |
snej@0 | 46 |
|
snej@0 | 47 |
- (NSData*) certificateData { |
snej@0 | 48 |
CFDataRef data = SecCertificateCopyData(_certificateRef); |
snej@0 | 49 |
return data ?[(id)CFMakeCollectable(data) autorelease] :nil; |
snej@0 | 50 |
} |
snej@0 | 51 |
|
snej@0 | 52 |
- (MYPublicKey*) publicKey { |
snej@0 | 53 |
SecTrustRef trust = NULL; |
snej@0 | 54 |
SecPolicyRef policy = SecPolicyCreateBasicX509(); |
snej@0 | 55 |
OSStatus err = SecTrustCreateWithCertificates((CFArrayRef)$array((id)_certificateRef), |
snej@0 | 56 |
policy, |
snej@0 | 57 |
&trust); |
snej@0 | 58 |
CFRelease(policy); |
snej@0 | 59 |
if (!check(err,@"SecTrustCreateWithCertificates")) |
snej@0 | 60 |
return nil; |
snej@0 | 61 |
|
snej@0 | 62 |
MYPublicKey *key = nil; |
snej@0 | 63 |
SecKeyRef keyRef = SecTrustCopyPublicKey(trust); |
snej@0 | 64 |
if (keyRef) { |
snej@0 | 65 |
key = [[[MYPublicKey alloc] initWithKeyRef: keyRef] autorelease]; |
snej@0 | 66 |
CFRelease(keyRef); |
snej@0 | 67 |
} |
snej@0 | 68 |
CFRelease(trust); |
snej@0 | 69 |
return key; |
snej@0 | 70 |
} |
snej@0 | 71 |
|
snej@0 | 72 |
|
snej@0 | 73 |
- (NSString*) commonName { |
snej@0 | 74 |
CFStringRef name = SecCertificateCopySubjectSummary(_certificateRef); |
snej@0 | 75 |
return name ?[(id)CFMakeCollectable(name) autorelease] :nil; |
snej@0 | 76 |
} |
snej@0 | 77 |
|
snej@0 | 78 |
|
snej@0 | 79 |
@end |
snej@0 | 80 |
|
snej@2 | 81 |
#endif MYCRYPTO_USE_IPHONE_API |