1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/MYCertificate.m Sat Apr 04 22:56:13 2009 -0700
1.3 @@ -0,0 +1,104 @@
1.4 +//
1.5 +// MYCertificate.m
1.6 +// MYCrypto
1.7 +//
1.8 +// Created by Jens Alfke on 3/26/09.
1.9 +// Copyright 2009 Jens Alfke. All rights reserved.
1.10 +//
1.11 +
1.12 +#import "MYCertificate.h"
1.13 +#import "MYCrypto_Private.h"
1.14 +
1.15 +#if !USE_IPHONE_API
1.16 +
1.17 +
1.18 +@implementation MYCertificate
1.19 +
1.20 +
1.21 +/** Creates a MYCertificate object for an existing Keychain certificate reference. */
1.22 +- (id) initWithCertificateRef: (SecCertificateRef)certificateRef {
1.23 + self = [super initWithKeychainItemRef: (SecKeychainItemRef)certificateRef];
1.24 + if (self) {
1.25 + _certificateRef = certificateRef; // superclass has already CFRetained it
1.26 + }
1.27 + return self;
1.28 +}
1.29 +
1.30 +/** Creates a MYCertificate object from exported key data, but does not add it to any keychain. */
1.31 +- (id) initWithCertificateData: (NSData*)data
1.32 + type: (CSSM_CERT_TYPE) type
1.33 + encoding: (CSSM_CERT_ENCODING) encoding
1.34 +{
1.35 + Assert(data);
1.36 + CSSM_DATA cssmData = {.Data=(void*)data.bytes, .Length=data.length};
1.37 + SecCertificateRef certificateRef = NULL;
1.38 + if (!check(SecCertificateCreateFromData(&cssmData, type, encoding, &certificateRef),
1.39 + @"SecCertificateCreateFromData")) {
1.40 + [self release];
1.41 + return nil;
1.42 + }
1.43 + self = [self initWithCertificateRef: certificateRef];
1.44 + CFRelease(certificateRef);
1.45 + return self;
1.46 +}
1.47 +
1.48 +- (id) initWithCertificateData: (NSData*)data {
1.49 + return [self initWithCertificateData: data
1.50 + type: CSSM_CERT_X_509v3
1.51 + encoding: CSSM_CERT_ENCODING_BER];
1.52 +}
1.53 +
1.54 ++ (MYCertificate*) preferredCertificateForName: (NSString*)name {
1.55 + SecCertificateRef certRef = NULL;
1.56 + if (!check(SecCertificateCopyPreference((CFStringRef)name, 0, &certRef),
1.57 + @"SecCertificateCopyPreference"))
1.58 + return nil;
1.59 + return [[[MYCertificate alloc] initWithCertificateRef: certRef] autorelease];
1.60 +}
1.61 +
1.62 +- (BOOL) setPreferredCertificateForName: (NSString*)name {
1.63 + return check(SecCertificateSetPreference(_certificateRef, (CFStringRef)name, 0, NULL),
1.64 + @"SecCertificateSetPreference");
1.65 +}
1.66 +
1.67 +@synthesize certificateRef=_certificateRef;
1.68 +
1.69 +- (NSData*) certificateData {
1.70 + CSSM_DATA cssmData;
1.71 + if (!check(SecCertificateGetData(_certificateRef, &cssmData),
1.72 + @"SecCertificateGetData"))
1.73 + return nil;
1.74 + return [NSData dataWithBytes: cssmData.Data length: cssmData.Length];
1.75 +}
1.76 +
1.77 +- (MYPublicKey*) publicKey {
1.78 + SecKeyRef keyRef = NULL;
1.79 + if (!check(SecCertificateCopyPublicKey(_certificateRef, &keyRef),
1.80 + @"SecCertificateCopyPublicKey") || !keyRef)
1.81 + return nil;
1.82 + MYPublicKey *key = [[[MYPublicKey alloc] initWithKeyRef: keyRef] autorelease];
1.83 + CFRelease(keyRef);
1.84 + return key;
1.85 +}
1.86 +
1.87 +- (NSString*) commonName {
1.88 + CFStringRef name = NULL;
1.89 + if (!check(SecCertificateCopyCommonName(_certificateRef, &name),
1.90 + @"SecCertificateCopyCommonName") || !name)
1.91 + return nil;
1.92 + return [(id)CFMakeCollectable(name) autorelease];
1.93 +}
1.94 +
1.95 +- (NSArray*) emailAddresses {
1.96 + CFArrayRef addrs = NULL;
1.97 + if (!check(SecCertificateCopyEmailAddresses(_certificateRef, &addrs),
1.98 + @"SecCertificateCopyEmailAddresses") || !addrs)
1.99 + return nil;
1.100 + return [(id)CFMakeCollectable(addrs) autorelease];
1.101 +}
1.102 +
1.103 +
1.104 +@end
1.105 +
1.106 +
1.107 +#endif !USE_IPHONE_API