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 里增加一條數據畏腕,第一個參數表示數據箕肃,第二個參數表示執(zhí)行該操作后,指向剛添加的這個數據的引用咽安,如果不需要用到這條數據捍歪,可以傳入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) {
            // 繼續(xù)查找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 就是沒有
    }
}

注意
區(qū)別(標識)一個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做簽名檢查的地方都打了不定焙贷,使得不是蘋果頒發(fā)的證書簽名的文件撵割,也能正常使用,或者偽造證書簽名的app也能夠正常使用辙芍。

  • 獲取access group的名稱

使用keychain dumper獲取access group

  • 使用keychain dumper啡彬,就可以得到所有的相關信息羹与。 但是,要在設備上執(zhí)行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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末腾誉,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子峻呕,更是在濱河造成了極大的恐慌利职,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瘦癌,死亡現(xiàn)場離奇詭異猪贪,居然都是意外死亡,警方通過查閱死者的電腦和手機佩憾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門哮伟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人妄帘,你說我怎么就攤上這事楞黄。” “怎么了抡驼?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵鬼廓,是天一觀的道長。 經常有香客問我致盟,道長碎税,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任馏锡,我火速辦了婚禮雷蹂,結果婚禮上,老公的妹妹穿的比我還像新娘杯道。我一直安慰自己匪煌,他們只是感情好,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布党巾。 她就那樣靜靜地躺著萎庭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪齿拂。 梳的紋絲不亂的頭發(fā)上驳规,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天,我揣著相機與錄音署海,去河邊找鬼吗购。 笑死医男,一個胖子當著我的面吹牛,可吹牛的內容都是我干的巩搏。 我是一名探鬼主播昨登,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼贯底!你這毒婦竟也來了丰辣?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤禽捆,失蹤者是張志新(化名)和其女友劉穎笙什,沒想到半個月后,有當地人在樹林里發(fā)現(xiàn)了一具尸體胚想,經...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡琐凭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了浊服。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片统屈。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖牙躺,靈堂內的尸體忽然破棺而出愁憔,到底是詐尸還是另有隱情,我是刑警寧澤孽拷,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布吨掌,位于F島的核電站,受9級特大地震影響脓恕,放射性物質發(fā)生泄漏膜宋。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一炼幔、第九天 我趴在偏房一處隱蔽的房頂上張望秋茫。 院中可真熱鬧,春花似錦乃秀、人聲如沸肛著。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至衙傀,卻和暖如春抬吟,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背统抬。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工火本, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留危队,地道東北人。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓钙畔,卻偏偏與公主長得像茫陆,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子擎析,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內容

  • 引言 關于開發(fā)證書配置(Certificates & Identifiers & Provisioning Pro...
    奮斗的蝸牛閱讀 7,388評論 2 20
  • app里的登錄模塊就是保存登錄過用戶名和密碼簿盅,如果用戶選擇記住密碼則保存最長保存7天,這樣用戶下次登錄app的時候...
    TerryD閱讀 11,454評論 1 4
  • 進入辟谷狀態(tài)后應該這樣做: 1揍魂、接到信息不要吃飯桨醋。只喝白開水(不渴不喝,喝水不限量)现斋,初學者每天最多吃9個棗喜最、一個...
    文靜文閱讀 730評論 0 1
  • set cindent 首行縮進 ctrl + n 自動補全 set vb 不提示錯誤聲音 $到行尾 vimrc ...
    夜周三更閱讀 481評論 0 0
  • 村上春樹說自己是通過實實在在運動的群體,通過選擇的磨難庄蹋,得到極其私人地感悟到的東西瞬内,也許并不值得推而廣之,但卻是真...
    龍荒閱讀 198評論 0 1