Skip to content

Commit

Permalink
Merge pull request #394 from Simperium/develop
Browse files Browse the repository at this point in the history
Simperium Mark 0.7.3
  • Loading branch information
jleandroperez committed Nov 17, 2014
2 parents 326ed9b + ae74f1b commit 1792f14
Show file tree
Hide file tree
Showing 17 changed files with 832 additions and 561 deletions.
161 changes: 161 additions & 0 deletions External/SSKeychain/SSKeychain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
//
// SSKeychain.h
// SSKeychain
//
// Created by Sam Soffes on 5/19/10.
// Copyright (c) 2010-2014 Sam Soffes. All rights reserved.
//

#import "SSKeychainQuery.h"

/**
Error code specific to SSKeychain that can be returned in NSError objects.
For codes returned by the operating system, refer to SecBase.h for your
platform.
*/
typedef NS_ENUM(OSStatus, SSKeychainErrorCode) {
/** Some of the arguments were invalid. */
SSKeychainErrorBadArguments = -1001,
};

/** SSKeychain error domain */
extern NSString *const kSSKeychainErrorDomain;

/** Account name. */
extern NSString *const kSSKeychainAccountKey;

/**
Time the item was created.
The value will be a string.
*/
extern NSString *const kSSKeychainCreatedAtKey;

/** Item class. */
extern NSString *const kSSKeychainClassKey;

/** Item description. */
extern NSString *const kSSKeychainDescriptionKey;

/** Item label. */
extern NSString *const kSSKeychainLabelKey;

/** Time the item was last modified.
The value will be a string.
*/
extern NSString *const kSSKeychainLastModifiedKey;

/** Where the item was created. */
extern NSString *const kSSKeychainWhereKey;

/**
Simple wrapper for accessing accounts, getting passwords, setting passwords, and deleting passwords using the system
Keychain on Mac OS X and iOS.
This was originally inspired by EMKeychain and SDKeychain (both of which are now gone). Thanks to the authors.
SSKeychain has since switched to a simpler implementation that was abstracted from [SSToolkit](http://sstoolk.it).
*/
@interface SSKeychain : NSObject

#pragma mark - Classic methods

/**
Returns a string containing the password for a given account and service, or `nil` if the Keychain doesn't have a
password for the given parameters.
@param serviceName The service for which to return the corresponding password.
@param account The account for which to return the corresponding password.
@return Returns a string containing the password for a given account and service, or `nil` if the Keychain doesn't
have a password for the given parameters.
*/
+ (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account;
+ (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;


/**
Deletes a password from the Keychain.
@param serviceName The service for which to delete the corresponding password.
@param account The account for which to delete the corresponding password.
@return Returns `YES` on success, or `NO` on failure.
*/
+ (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account;
+ (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;


/**
Sets a password in the Keychain.
@param password The password to store in the Keychain.
@param serviceName The service for which to set the corresponding password.
@param account The account for which to set the corresponding password.
@return Returns `YES` on success, or `NO` on failure.
*/
+ (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account;
+ (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;


/**
Returns an array containing the Keychain's accounts, or `nil` if the Keychain has no accounts.
See the `NSString` constants declared in SSKeychain.h for a list of keys that can be used when accessing the
dictionaries returned by this method.
@return An array of dictionaries containing the Keychain's accounts, or `nil` if the Keychain doesn't have any
accounts. The order of the objects in the array isn't defined.
*/
+ (NSArray *)allAccounts;


/**
Returns an array containing the Keychain's accounts for a given service, or `nil` if the Keychain doesn't have any
accounts for the given service.
See the `NSString` constants declared in SSKeychain.h for a list of keys that can be used when accessing the
dictionaries returned by this method.
@param serviceName The service for which to return the corresponding accounts.
@return An array of dictionaries containing the Keychain's accounts for a given `serviceName`, or `nil` if the Keychain
doesn't have any accounts for the given `serviceName`. The order of the objects in the array isn't defined.
*/
+ (NSArray *)accountsForService:(NSString *)serviceName;


#pragma mark - Configuration

#if __IPHONE_4_0 && TARGET_OS_IPHONE
/**
Returns the accessibility type for all future passwords saved to the Keychain.
@return Returns the accessibility type.
The return value will be `NULL` or one of the "Keychain Item Accessibility
Constants" used for determining when a keychain item should be readable.
@see setAccessibilityType
*/
+ (CFTypeRef)accessibilityType;

/**
Sets the accessibility type for all future passwords saved to the Keychain.
@param accessibilityType One of the "Keychain Item Accessibility Constants"
used for determining when a keychain item should be readable.
If the value is `NULL` (the default), the Keychain default will be used.
@see accessibilityType
*/
+ (void)setAccessibilityType:(CFTypeRef)accessibilityType;
#endif

@end
94 changes: 94 additions & 0 deletions External/SSKeychain/SSKeychain.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// SSKeychain.m
// SSKeychain
//
// Created by Sam Soffes on 5/19/10.
// Copyright (c) 2010-2014 Sam Soffes. All rights reserved.
//

#import "SSKeychain.h"

NSString *const kSSKeychainErrorDomain = @"com.samsoffes.sskeychain";
NSString *const kSSKeychainAccountKey = @"acct";
NSString *const kSSKeychainCreatedAtKey = @"cdat";
NSString *const kSSKeychainClassKey = @"labl";
NSString *const kSSKeychainDescriptionKey = @"desc";
NSString *const kSSKeychainLabelKey = @"labl";
NSString *const kSSKeychainLastModifiedKey = @"mdat";
NSString *const kSSKeychainWhereKey = @"svce";

#if __IPHONE_4_0 && TARGET_OS_IPHONE
static CFTypeRef SSKeychainAccessibilityType = NULL;
#endif

@implementation SSKeychain

+ (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account {
return [self passwordForService:serviceName account:account error:nil];
}


+ (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account error:(NSError *__autoreleasing *)error {
SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
query.service = serviceName;
query.account = account;
[query fetch:error];
return query.password;
}


+ (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account {
return [self deletePasswordForService:serviceName account:account error:nil];
}


+ (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account error:(NSError *__autoreleasing *)error {
SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
query.service = serviceName;
query.account = account;
return [query deleteItem:error];
}


+ (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account {
return [self setPassword:password forService:serviceName account:account error:nil];
}


+ (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError *__autoreleasing *)error {
SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
query.service = serviceName;
query.account = account;
query.password = password;
return [query save:error];
}


+ (NSArray *)allAccounts {
return [self accountsForService:nil];
}


+ (NSArray *)accountsForService:(NSString *)serviceName {
SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
query.service = serviceName;
return [query fetchAll:nil];
}


#if __IPHONE_4_0 && TARGET_OS_IPHONE
+ (CFTypeRef)accessibilityType {
return SSKeychainAccessibilityType;
}


+ (void)setAccessibilityType:(CFTypeRef)accessibilityType {
CFRetain(accessibilityType);
if (SSKeychainAccessibilityType) {
CFRelease(SSKeychainAccessibilityType);
}
SSKeychainAccessibilityType = accessibilityType;
}
#endif

@end
133 changes: 133 additions & 0 deletions External/SSKeychain/SSKeychainQuery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
//
// SSKeychainQuery.h
// SSKeychain
//
// Created by Caleb Davenport on 3/19/13.
// Copyright (c) 2013-2014 Sam Soffes. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <Security/Security.h>

#if __IPHONE_7_0 || __MAC_10_9
// Keychain synchronization available at compile time
#define SSKEYCHAIN_SYNCHRONIZATION_AVAILABLE 1
#endif

#ifdef SSKEYCHAIN_SYNCHRONIZATION_AVAILABLE
typedef NS_ENUM(NSUInteger, SSKeychainQuerySynchronizationMode) {
SSKeychainQuerySynchronizationModeAny,
SSKeychainQuerySynchronizationModeNo,
SSKeychainQuerySynchronizationModeYes
};
#endif

/**
Simple interface for querying or modifying keychain items.
*/
@interface SSKeychainQuery : NSObject

/** kSecAttrAccount */
@property (nonatomic, copy) NSString *account;

/** kSecAttrService */
@property (nonatomic, copy) NSString *service;

/** kSecAttrLabel */
@property (nonatomic, copy) NSString *label;

#if __IPHONE_3_0 && TARGET_OS_IPHONE
/** kSecAttrAccessGroup (only used on iOS) */
@property (nonatomic, copy) NSString *accessGroup;
#endif

#ifdef SSKEYCHAIN_SYNCHRONIZATION_AVAILABLE
/** kSecAttrSynchronizable */
@property (nonatomic) SSKeychainQuerySynchronizationMode synchronizationMode;
#endif

/** Root storage for password information */
@property (nonatomic, copy) NSData *passwordData;

/**
This property automatically transitions between an object and the value of
`passwordData` using NSKeyedArchiver and NSKeyedUnarchiver.
*/
@property (nonatomic, copy) id<NSCoding> passwordObject;

/**
Convenience accessor for setting and getting a password string. Passes through
to `passwordData` using UTF-8 string encoding.
*/
@property (nonatomic, copy) NSString *password;


///------------------------
/// @name Saving & Deleting
///------------------------

/**
Save the receiver's attributes as a keychain item. Existing items with the
given account, service, and access group will first be deleted.
@param error Populated should an error occur.
@return `YES` if saving was successful, `NO` otherwise.
*/
- (BOOL)save:(NSError **)error;

/**
Delete keychain items that match the given account, service, and access group.
@param error Populated should an error occur.
@return `YES` if saving was successful, `NO` otherwise.
*/
- (BOOL)deleteItem:(NSError **)error;


///---------------
/// @name Fetching
///---------------

/**
Fetch all keychain items that match the given account, service, and access
group. The values of `password` and `passwordData` are ignored when fetching.
@param error Populated should an error occur.
@return An array of dictionaries that represent all matching keychain items or
`nil` should an error occur.
The order of the items is not determined.
*/
- (NSArray *)fetchAll:(NSError **)error;

/**
Fetch the keychain item that matches the given account, service, and access
group. The `password` and `passwordData` properties will be populated unless
an error occurs. The values of `password` and `passwordData` are ignored when
fetching.
@param error Populated should an error occur.
@return `YES` if fetching was successful, `NO` otherwise.
*/
- (BOOL)fetch:(NSError **)error;


///-----------------------------
/// @name Synchronization Status
///-----------------------------

#ifdef SSKEYCHAIN_SYNCHRONIZATION_AVAILABLE
/**
Returns a boolean indicating if keychain synchronization is available on the device at runtime. The #define
SSKEYCHAIN_SYNCHRONIZATION_AVAILABLE is only for compile time. If you are checking for the presence of synchronization,
you should use this method.
@return A value indicating if keychain synchronization is available
*/
+ (BOOL)isSynchronizationAvailable;
#endif

@end

0 comments on commit 1792f14

Please sign in to comment.