keyChain and TouchID

Keychain and Touch ID

keychain结构

keychain结构.png

注意
1 kSecClass Key 定义属于哪一种字典结构
2 不同类型包含不同的Attribute,这些attributes定义了这个item的具体信息
3 每个item可以包含一个密码项来存储对应的密码

keychain 四个方法

增删改查
OSStatus SecItemAdd(CFDictionaryRef attributes, CFTypeRef *result)

SecItemAdd表示往Keychain 里增加一条数据,第一个参数表示数据,第二个参数表示执行该操作后,指向刚添加的这个数据的引用,如果不需要用到这条数据,可以传入NULL(翻译自apple文档)

OSStatus SecItemCopyMatching(CFDictionaryRef query, CFTypeRef *result)

SecItemCopyMatching表示查询Keychain里是否有符号条件的记录。第一个参数查询条件,第二个查询到结果的引用。

OSStatus SecItemUpdate(CFDictionaryRef query, CFDictionaryRef attributesToUpdate)

SecItemUpdate更新Keychain里的记录。第一个参数表示查询条件,第二个表示当根据第一个查询条件后,用于更新的值。

OSStatus SecItemDelete(CFDictionaryRef query)

SecItemDelete删除符号查询条件的记录。参数表示查询条件

添加 Add

- (IBAction)add:(id)sender {
    if (nameField.text.length > 0 && passwordField.text.length > 0) {
        // 一个mutable字典结构存储item信息
        NSMutableDictionary* dic = [NSMutableDictionary dictionary];
        // 确定所属的类class
        [dic setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
        // 设置其他属性attributes
        [dic setObject:nameField.text forKey:(id)kSecAttrAccount];
        // 添加密码 secValue  注意是object 是 NSData
        [dic setObject:[passwordField.text dataUsingEncoding:NSUTF8StringEncoding] forKey:(id)kSecValueData];
        // SecItemAdd
        OSStatus s = SecItemAdd((CFDictionaryRef)dic, NULL);
        NSLog(@"add : %ld",s);
    }
}

查找

// 查找全部
- (IBAction)sel:(id)sender {
    NSDictionary* query = [NSDictionary dictionaryWithObjectsAndKeys:kSecClassGenericPassword,kSecClass, 
                           kSecMatchLimitAll,kSecMatchLimit,
                           kCFBooleanTrue,kSecReturnAttributes,nil];
    CFTypeRef result = nil;
    OSStatus s = SecItemCopyMatching((CFDictionaryRef)query, &result);
    NSLog(@"select all : %ld",s);
    NSLog(@"%@",result);
}

// 按名称查找
- (IBAction)sname:(id)sender {
    if (nameField.text.length >0) {
        // 查找条件:1.class 2.attributes 3.search option
        NSDictionary* query = [NSDictionary dictionaryWithObjectsAndKeys:kSecClassGenericPassword,kSecClass, 
                               nameField.text,kSecAttrAccount,
                               kCFBooleanTrue,kSecReturnAttributes,nil];
        CFTypeRef result = nil;
        // 先找到一个item
        OSStatus s = SecItemCopyMatching((CFDictionaryRef)query, &result);
        NSLog(@"select name : %ld",s);  //  errSecItemNotFound 就是找不到
        NSLog(@"%@",result);        
        if (s == noErr) {
            // 继续查找item的secValue
            NSMutableDictionary* dic = [NSMutableDictionary dictionaryWithDictionary:result];
            // 存储格式
            [dic setObject:(id)kCFBooleanTrue forKey:kSecReturnData];
            // 确定class
            [dic setObject:[query objectForKey:kSecClass] forKey:kSecClass];
            NSData* data = nil;
            // 查找secValue
            if (SecItemCopyMatching((CFDictionaryRef)dic, (CFTypeRef*)&data) == noErr) {
                if (data.length)
                    NSLog(@"%@",[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
            }
        }
    }
}

# 修改

- (IBAction)update:(id)sender {
    if (nameField.text.length >0 && passwordField.text.length > 0) {
        // 先查找看看有没有
        NSDictionary* query = [NSDictionary dictionaryWithObjectsAndKeys:kSecClassGenericPassword,kSecClass, 
                               nameField.text,kSecAttrAccount,
                               kCFBooleanTrue,kSecReturnAttributes,nil];
        
        CFTypeRef result = nil;
        if (SecItemCopyMatching((CFDictionaryRef)query, &result) == noErr)
        {    
            // 更新后的数据,基础是搜到的result
            NSMutableDictionary* update = [NSMutableDictionary dictionaryWithDictionary:(NSDictionary*)result];
            // 修改要跟新的项 注意先加后删的class项
            [update setObject:[query objectForKey:kSecClass] forKey:kSecClass];
            [update setObject:[passwordField.text dataUsingEncoding:NSUTF8StringEncoding] forKey:kSecValueData];
            [update removeObjectForKey:kSecClass];
#if TARGET_IPHONE_SIMULATOR
            // 模拟器的都有个默认的组“test”,删了,不然会出错
            [update removeObjectForKey:(id)kSecAttrAccessGroup];
#endif
            // 得到要修改的item,根据result,但要添加class
            NSMutableDictionary* updateItem = [NSMutableDictionary dictionaryWithDictionary:result];
            [updateItem setObject:[query objectForKey:(id)kSecClass] forKey:(id)kSecClass];
            // SecItemUpdate
            OSStatus status = SecItemUpdate((CFDictionaryRef)updateItem, (CFDictionaryRef)update);
            NSLog(@"update:%ld",status);

删除

- (IBAction)del:(id)sender {
    if (nameField.text.length >0) {
        // 删除的条件
        NSDictionary* query = [NSDictionary dictionaryWithObjectsAndKeys:kSecClassGenericPassword,kSecClass, 
                               nameField.text,kSecAttrAccount,nil];
        // SecItemDelete
        OSStatus status = SecItemDelete((CFDictionaryRef)query);
        NSLog(@"delete:%ld",status);    //  errSecItemNotFound 就是没有
    }
}

注意
区别(标识)一个item要用kSecAttrAccount和kSecAttrService

Apple 官方Sample code

- (void)addItemAsync {
    CFErrorRef error = NULL;
    
    // Should be the secret invalidated when passcode is removed? If not then use kSecAttrAccessibleWhenUnlocked
    SecAccessControlRef sacObject = SecAccessControlCreateWithFlags(kCFAllocatorDefault,
                                                                    kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly,
                                                                    kSecAccessControlUserPresence, &error);
    
    if (sacObject == NULL || error != NULL) {
        NSString *errorString = [NSString stringWithFormat:@"SecItemAdd can't create sacObject: %@", error];
        
        self.textView.text = [self.textView.text stringByAppendingString:errorString];
        
        return;
    }
    
    // we want the operation to fail if there is an item which needs authentication so we will use
    // kSecUseNoAuthenticationUI
    NSDictionary *attributes = @{
                                 (__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
                                 (__bridge id)kSecAttrService: @"SampleService",
                                 (__bridge id)kSecValueData: [@"SECRET_PASSWORD_TEXT" dataUsingEncoding:NSUTF8StringEncoding],
                                 (__bridge id)kSecUseNoAuthenticationUI: @YES,
                                 (__bridge id)kSecAttrAccessControl: (__bridge_transfer id)sacObject
                                 };
    
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        OSStatus status =  SecItemAdd((__bridge CFDictionaryRef)attributes, nil);
        
        NSString *errorString = [self keychainErrorToString:status];
        NSString *message = [NSString stringWithFormat:@"SecItemAdd status: %@", errorString];
        
        [self printMessage:message inTextView:self.textView];
    });
}

官方wrapper类
https://developer.apple.com/library/ios/documentation/Security/Conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html

Touch ID

LAContext: Represents an authentication context.

LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myLocalizedReasonString = <#String explaining why app needs authentication#>;
 
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
    [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                  localizedReason:myLocalizedReasonString
                            reply:^(BOOL success, NSError *error) {
            if (success) {
                // User authenticated successfully, take appropriate action
            } else {
                // User did not authenticate successfully, look at error and take appropriate action
            }
        }];
} else {
    // Could not evaluate policy; look at authError and present an appropriate message to user
}

判断是否支持touchID

- (void)canEvaluatePolicy {
    LAContext *context = [[LAContext alloc] init];
    __block  NSString *message;
    NSError *error;
    BOOL success;
    
    // test if we can evaluate the policy, this test will tell us if Touch ID is available and enrolled
    success = [context canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
    if (success) {
        message = [NSString stringWithFormat:@"Touch ID is available"];
    }
    else {
        message = [NSString stringWithFormat:@"Touch ID is not available"];
    }
    
    [super printMessage:message inTextView:self.textView];
}

使用默认的方式进行指纹识别

- (void)evaluatePolicy {
    LAContext *context = [[LAContext alloc] init];
    __block  NSString *message;
    
    // Show the authentication UI with our reason string.
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Unlock access to locked feature" reply:^(BOOL success, NSError *authenticationError) {
         if (success) {
             message = @"evaluatePolicy: succes";
         }
         else {
             message = [NSString stringWithFormat:@"evaluatePolicy: %@", authenticationError.localizedDescription];
         }

         [self printMessage:message inTextView:self.textView];
     }];
}

定制UI进行指纹识别

- (void)evaluatePolicy2 {
    LAContext *context = [[LAContext alloc] init];
    __block NSString *message;
    
    // Set text for the localized fallback button.
    context.localizedFallbackTitle = @"Enter PIN";
    
    // Show the authentication UI with our reason string.
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Unlock access to locked feature" reply:^(BOOL success, NSError *authenticationError) {
         if (success) {
             message = @"evaluatePolicy: succes";
         }
         else {
             message = [NSString stringWithFormat:@"evaluatePolicy: %@", authenticationError.localizedDescription];
         }
         
         [self printMessage:message inTextView:self.textView];
     }];
}

定制Demo

LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myLocalizedReasonString = <#String explaining why app needs authentication#>;

if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {

    [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
              localizedReason:myLocalizedReasonString
                        reply:^(BOOL success, NSError *error) { 

                            if (success) {
                                   // User authenticated successfully, take appropriate action
                                   dispatch_async(dispatch_get_main_queue(), ^{
                                          // write all your code here
                               });
                            } else {
                                   // User did not authenticate successfully, look at error and take appropriate action
                               switch (error.code) {
                                   case LAErrorAuthenticationFailed:
                                       NSLog(@"Authentication Failed");
                                       break;
                                   case LAErrorUserCancel:
                                       NSLog(@"User pressed Cancel button");
                                       break;
                                   case LAErrorUserFallback:
                                       NSLog(@"User pressed \"Enter Password\"");
                                       break;
                                   default:
                                       NSLog(@"Touch ID is not configured");
                                       break;
                               }
                               NSLog(@"Authentication Fails");
                            }
                        }];
} else {
    // Could not evaluate policy; look at authError and present an appropriate message to user
}


keyChain 不是绝对的安全

keychain的几个特点

  • app保持的信息是用一个app unique签名的,只能够自己访问
  • 不同app之间可以勇敢access_group共享
  • 在不同app之间的共享,只能在一个公司内的app共享

越狱机器上keychain不安全

通过上面的几个特点,要访问keychain中的数据,需要一下几点:

  • 和app同样的证书

jail break 相当于apple做签名检查的地方都打了不定,使得不是苹果颁发的证书签名的文件,也能正常使用,或者伪造证书签名的app也能够正常使用。

  • 获取access group的名称

使用keychain dumper获取access group

  • 使用keychain dumper,就可以得到所有的相关信息。 但是,要在设备上执行keychain dumper,就需要用chmod 777设置其权限,需要root权限,而jail break之后的默认密码是:alpine。

参考文献
http://blog.csdn.net/yiyaaixuexi/article/details/18404343
http://wufawei.com/2013/11/ios-application-security-12/
http://wufawei.com/2013/06/Keychain-is-not-safe/
https://github.com/ptoomey3/Keychain-Dumper

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容

  • 引言 关于开发证书配置(Certificates & Identifiers & Provisioning Pro...
    奋斗的蜗牛阅读 7,162评论 2 20
  • app里的登录模块就是保存登录过用户名和密码,如果用户选择记住密码则保存最长保存7天,这样用户下次登录app的时候...
    TerryD阅读 11,193评论 1 4
  • 进入辟谷状态后应该这样做: 1、接到信息不要吃饭。只喝白开水(不渴不喝,喝水不限量),初学者每天最多吃9个枣、一个...
    文静文阅读 616评论 0 1
  • set cindent 首行缩进 ctrl + n 自动补全 set vb 不提示错误声音 $到行尾 vimrc ...
    夜周三更阅读 459评论 0 0
  • 村上春树说自己是通过实实在在运动的群体,通过选择的磨难,得到极其私人地感悟到的东西,也许并不值得推而广之,但却是真...
    龙荒阅读 177评论 0 1