diff -r 1dfe820d7ebe -r 3af1d1c0ceb5 MYKeychain.m --- a/MYKeychain.m Wed Apr 08 16:30:52 2009 -0700 +++ b/MYKeychain.m Sun Apr 19 21:19:35 2009 -0700 @@ -9,12 +9,15 @@ #import "MYKeychain.h" #import "MYCrypto_Private.h" #import "MYDigest.h" +#import "MYIdentity.h" + #if !MYCRYPTO_USE_IPHONE_API @interface MYKeyEnumerator : NSEnumerator { + @private MYKeychain *_keychain; SecKeychainSearchRef _search; SecItemClass _itemClass; @@ -27,6 +30,17 @@ @end +@interface MYIdentityEnumerator : NSEnumerator +{ + @private + SecIdentitySearchRef _searchRef; +} + +- (id) initWithKeychain: (MYKeychain*)keychain; +@end + + + @implementation MYKeychain @@ -249,6 +263,10 @@ attributes: NULL count: 0] autorelease]; } +- (NSEnumerator*) enumerateIdentities { + return [[[MYIdentityEnumerator alloc] initWithKeychain: self] autorelease]; +} + - (NSEnumerator*) enumerateSymmetricKeys { return [[[MYKeyEnumerator alloc] initWithKeychain: self itemClass: kSecSymmetricKeyItemClass @@ -318,6 +336,12 @@ encoding: CSSM_CERT_ENCODING_BER]; } +- (BOOL) addCertificate: (MYCertificate*)certificate { + Assert(certificate); + return check(SecCertificateAddToKeychain(certificate.certificateRef, self.keychainRefOrDefault), + @"SecCertificateAddToKeychain"); +} + - (MYSymmetricKey*) generateSymmetricKeyOfSize: (unsigned)keySizeInBits algorithm: (CCAlgorithm)algorithm @@ -413,6 +437,31 @@ return key; } +@end + + + +@implementation MYIdentityEnumerator + +- (id) initWithKeychain: (MYKeychain*)keychain { + self = [super init]; + if (self) { + if (!check(SecIdentitySearchCreate(keychain.keychainRef, 0, &_searchRef), + @"SecIdentitySearchCreate")) { + [self release]; + return nil; + } + } + return self; +} + +- (id) nextObject { + SecIdentityRef identityRef = NULL; + OSStatus err = SecIdentitySearchCopyNext(_searchRef, &identityRef); + if (err==errKCItemNotFound || !check(err, @"SecIdentitySearchCopyNext")) + return nil; + return [[[MYIdentity alloc] initWithIdentityRef: identityRef] autorelease]; +} @end