SlideShare a Scribd company logo
IOS KEYCHAIN
HappyMan
2014/12/30
What is KeyChain
• Keychain is an encrypted container where you
can store secured information like passwords,
certificates, identities, …etc.
• In iOS, each application has its own keychain.
• To share the data between apps, they should
have the same Access Group in code signing
entitlements.
Accessing password-protected services
using a keychain in OS X
Accessing
an Internet
server using
iPhone
Keychain
Services
• KeyChain 是 iOS 提供的一種安全保存私密資
料的方式,整個系統的 keychain 被保存在
隱秘的位置
(/private/var/Keychains/keychain-2.db),
其中保存的資料是經過加密的。
優點
• 每個組( keychain-access-groups )之間資料存
取隔離,沒有權限的 app無法讀取他人資料,
保證資料的安全
• 全域性統一儲存,即使刪除 app , keychain
中的資料依然存在,下次重新安裝app還能
存取
• 存儲後的資料會加密
• 同一個組的 app 可以共享 keychain 中的資
料
缺點
• 刪除 app 後不會清除 keychain 裡的資料,
如果儲存密碼等敏感性資料有一定的風險。
(越獄後 keychain 能被導出來)
iOS Keychain 介紹
實作API
• 新增:SecItemAdd
• 尋找:SecItemCopyMatching
• 更新:SecItemUpdate
• 移除:SecItemDelete
準備資料
• -(NSMutableDictionary *) prepareDict:(NSString *)key
• {
• NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
• [dict setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
•
• NSData *encodedKey = [key dataUsingEncoding:NSUTF8StringEncoding];
• [dict setObject:encodedKey forKey:(__bridge id)kSecAttrGeneric];
• [dict setObject:encodedKey forKey:(__bridge id)kSecAttrAccount];
• [dict setObject:service forKey:(__bridge id)kSecAttrService];
• [dict setObject:(__bridge id)kSecAttrAccessibleAlwaysThisDeviceOnly forKey:(__bridge
id)kSecAttrAccessible];
•
• return dict;
• }
新增
• -(BOOL) insert:(NSString *)key :(NSData *)data
• {
• NSMutableDictionary *dict =[self prepareDict:key];
• [dict setObject:data forKey:(__bridge id)kSecValueData];
•
• OSStatus status = SecItemAdd((__bridge CFDictionaryRef)dict,
NULL);
• if(errSecSuccess != status) {
• NSLog(@"Unable add item with key = %@ error:
%d",key,(int)status);
• }
• return (status == errSecSuccess);
• }
尋找
• -(NSData*) find:(NSString *)key
• {
• NSMutableDictionary *dict = [self prepareDict:key];
• [dict setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
• [dict setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
• CFTypeRef result = NULL;
• OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)dict,&result);
•
• if(status != errSecSuccess) {
• NSLog(@"Unable to fetch item for key %@ with error: %d",key,(int)status);
• return nil;
• }
•
• return (__bridge NSData *)result;
• }
更新
• -(BOOL) update:(NSString*)key :(NSData *)data
• {
• NSMutableDictionary *dictKey =[self prepareDict:key];
•
• NSMutableDictionary *dictUpdate =[[NSMutableDictionary alloc] init];
• [dictUpdate setObject:data forKey:(__bridge id)kSecValueData];
•
• OSStatus status = SecItemUpdate((__bridge CFDictionaryRef)dictKey, (__bridge
CFDictionaryRef)dictUpdate);
• if(status != errSecSuccess) {
• NSLog(@"Unable add update with key = %@ error: %d",key,(int)status);
• }
• return (status == errSecSuccess);
• }
移除
• -(BOOL) remove:(NSString *)key
• {
• NSMutableDictionary *dict = [self prepareDict:key];
• OSStatus status = SecItemDelete((__bridge
CFDictionaryRef)dict);
• if(status != errSecSuccess) {
• NSLog(@"Unable to remove item for key %@ with error:
%d",key,(int)status);
• }
• return (status == errSecSuccess);
• }
開源
• SSKeychain
https://github.com/soffes/sskeychain
Star: 1730 (2014/12/30)
• SFHFKeychainUtils
https://github.com/kamiro/SFHFKeychainUtils
Star: 60 (2014/12/30)
• Me: 2 projects
Demo
• https://github.com/happymanx/KeyChainTest
– 1). Initialization of the class
– 2). How to Add an item to keychain
– 3). Find an item in the keychain
– 4). Update an item in the keychain
– 5). Remove an item from keychain
參考
• iOS KeyChain Tutorial
http://hayageek.com/ios-keychain-tutorial/
• Securing and Encrypting Data on iOS
http://code.tutsplus.com/tutorials/securing-and-
encrypting-data-on-ios--mobile-21263
• Basic Security in iOS 5 – Part 1
http://www.raywenderlich.com/6475/basic-
security-in-ios-5-tutorial-part-1
• Basic Security in iOS 5 – Part 2
http://www.raywenderlich.com/6603/basic-
security-in-ios-5-tutorial-part-2
參考
• iOS Keychain: Sharing data between apps
http://shaune.com.au/ios-keychain-sharing-data-
between-apps/
• Keychain Group Access
http://useyourloaf.com/blog/2010/04/03/keycha
in-group-access.html
• 將密碼儲存於 KeyChain
http://wp.me/p1my2P-3S0
• KeyChain 使用與共享數據
http://blog.csdn.net/ibcker/article/details/24839
143
Apple連結
• Keychain Services Programming Guide
https://developer.apple.com/library/mac/docum
entation/Security/Conceptual/keychainServConc
epts/
• Keychain Services Reference
https://developer.apple.com/library/mac/docum
entation/Security/Reference/keychainservices/
• #WWDC14 session 711 - Keychain and
Authentication with Touch ID

More Related Content

PPT
Intrusion detection system ppt
PDF
iCloud keychain
PPT
The rsa algorithm JooSeok Song
PDF
HMAC authentication
PPTX
Metasploit For Beginners
PDF
Inline Hooking in Windows
PDF
Fault Injection Attacks
PPSX
Généralités sur la notion d’Algorithme
Intrusion detection system ppt
iCloud keychain
The rsa algorithm JooSeok Song
HMAC authentication
Metasploit For Beginners
Inline Hooking in Windows
Fault Injection Attacks
Généralités sur la notion d’Algorithme

What's hot (8)

PDF
PPT
PUBLIC KEY ENCRYPTION
PDF
18CS2005 Cryptography and Network Security
PPTX
Authentication(pswrd,token,certificate,biometric)
PDF
SYMMETRIC CRYPTOGRAPHY
PDF
IP Security
PDF
Cloud-Barista 제7차 컨퍼런스 : 멀티클라우드, 컴퓨팅 인프라에 제약없는 서비스 생태계
PPTX
Cryptography Presentation
PUBLIC KEY ENCRYPTION
18CS2005 Cryptography and Network Security
Authentication(pswrd,token,certificate,biometric)
SYMMETRIC CRYPTOGRAPHY
IP Security
Cloud-Barista 제7차 컨퍼런스 : 멀티클라우드, 컴퓨팅 인프라에 제약없는 서비스 생태계
Cryptography Presentation
Ad

Viewers also liked (20)

KEY
Security and Encryption on iOS
PPTX
iOS Security and Encryption
PPTX
iOS Application Exploitation
PDF
A (not-so-quick) Primer on iOS Encryption David Schuetz - NCC Group
PPTX
Introduction to Core Data - Jason Shapiro
PPTX
Power of linked list
PDF
iOS Application Penetation Test
PDF
iOS Application Security
PPT
iOS Application Penetration Testing for Beginners
PDF
IOS Encryption Systems
PDF
DeathNote of Microsoft Windows Kernel
PDF
Reverse Engineering iOS apps
PPTX
iOS-Application-Security-iAmPr3m
PPTX
Layer architecture of ios (1)
PPTX
Android vs ios System Architecture in OS perspective
PDF
Architecting iOS Project
PDF
Apple iOS Report
PPTX
Presentation on iOS
PPTX
Apple iOS
PPTX
iOS platform
Security and Encryption on iOS
iOS Security and Encryption
iOS Application Exploitation
A (not-so-quick) Primer on iOS Encryption David Schuetz - NCC Group
Introduction to Core Data - Jason Shapiro
Power of linked list
iOS Application Penetation Test
iOS Application Security
iOS Application Penetration Testing for Beginners
IOS Encryption Systems
DeathNote of Microsoft Windows Kernel
Reverse Engineering iOS apps
iOS-Application-Security-iAmPr3m
Layer architecture of ios (1)
Android vs ios System Architecture in OS perspective
Architecting iOS Project
Apple iOS Report
Presentation on iOS
Apple iOS
iOS platform
Ad

More from ShengWen Chiou (20)

PPTX
iOS Extension
PPTX
FMDB 研究
PPTX
Realm 研究
PPTX
Crashlytics 使用教學
PPTX
DBAccess 研究
PPTX
Xamarin.iOS中引用第三方Objective-C的Class Library
PPTX
Xamarin.iOS中引用自製Objective-C的Class Library
PPTX
iBeacon 相關應用
PPTX
Xamarin 研究
PPTX
What’s New In watch OS
PPTX
Apple Watch Feature
PPTX
Symbolicate Crash 使用教學
PPTX
Apple Watch Specifications
PPTX
Apple Watch UI Elements
PPTX
Apple Watch Human Interface Guidelines
PPTX
AppleDoc 使用教學
PPTX
Quickblox Study
PPTX
Auto layout 介紹
PPTX
iOS Touch ID 介紹
PPTX
CocoaPods 使用教學
iOS Extension
FMDB 研究
Realm 研究
Crashlytics 使用教學
DBAccess 研究
Xamarin.iOS中引用第三方Objective-C的Class Library
Xamarin.iOS中引用自製Objective-C的Class Library
iBeacon 相關應用
Xamarin 研究
What’s New In watch OS
Apple Watch Feature
Symbolicate Crash 使用教學
Apple Watch Specifications
Apple Watch UI Elements
Apple Watch Human Interface Guidelines
AppleDoc 使用教學
Quickblox Study
Auto layout 介紹
iOS Touch ID 介紹
CocoaPods 使用教學

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
AI in Product Development-omnex systems
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Digital Strategies for Manufacturing Companies
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Introduction to Artificial Intelligence
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Essential Infomation Tech presentation.pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
AI in Product Development-omnex systems
How Creative Agencies Leverage Project Management Software.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Operating system designcfffgfgggggggvggggggggg
Understanding Forklifts - TECH EHS Solution
Digital Strategies for Manufacturing Companies
wealthsignaloriginal-com-DS-text-... (1).pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Softaken Excel to vCard Converter Software.pdf
Reimagine Home Health with the Power of Agentic AI​
PTS Company Brochure 2025 (1).pdf.......
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Introduction to Artificial Intelligence
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Essential Infomation Tech presentation.pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...

iOS Keychain 介紹

  • 2. What is KeyChain • Keychain is an encrypted container where you can store secured information like passwords, certificates, identities, …etc. • In iOS, each application has its own keychain. • To share the data between apps, they should have the same Access Group in code signing entitlements.
  • 5. • KeyChain 是 iOS 提供的一種安全保存私密資 料的方式,整個系統的 keychain 被保存在 隱秘的位置 (/private/var/Keychains/keychain-2.db), 其中保存的資料是經過加密的。
  • 6. 優點 • 每個組( keychain-access-groups )之間資料存 取隔離,沒有權限的 app無法讀取他人資料, 保證資料的安全 • 全域性統一儲存,即使刪除 app , keychain 中的資料依然存在,下次重新安裝app還能 存取 • 存儲後的資料會加密 • 同一個組的 app 可以共享 keychain 中的資 料
  • 7. 缺點 • 刪除 app 後不會清除 keychain 裡的資料, 如果儲存密碼等敏感性資料有一定的風險。 (越獄後 keychain 能被導出來)
  • 9. 實作API • 新增:SecItemAdd • 尋找:SecItemCopyMatching • 更新:SecItemUpdate • 移除:SecItemDelete
  • 10. 準備資料 • -(NSMutableDictionary *) prepareDict:(NSString *)key • { • NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; • [dict setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass]; • • NSData *encodedKey = [key dataUsingEncoding:NSUTF8StringEncoding]; • [dict setObject:encodedKey forKey:(__bridge id)kSecAttrGeneric]; • [dict setObject:encodedKey forKey:(__bridge id)kSecAttrAccount]; • [dict setObject:service forKey:(__bridge id)kSecAttrService]; • [dict setObject:(__bridge id)kSecAttrAccessibleAlwaysThisDeviceOnly forKey:(__bridge id)kSecAttrAccessible]; • • return dict; • }
  • 11. 新增 • -(BOOL) insert:(NSString *)key :(NSData *)data • { • NSMutableDictionary *dict =[self prepareDict:key]; • [dict setObject:data forKey:(__bridge id)kSecValueData]; • • OSStatus status = SecItemAdd((__bridge CFDictionaryRef)dict, NULL); • if(errSecSuccess != status) { • NSLog(@"Unable add item with key = %@ error: %d",key,(int)status); • } • return (status == errSecSuccess); • }
  • 12. 尋找 • -(NSData*) find:(NSString *)key • { • NSMutableDictionary *dict = [self prepareDict:key]; • [dict setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit]; • [dict setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData]; • CFTypeRef result = NULL; • OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)dict,&result); • • if(status != errSecSuccess) { • NSLog(@"Unable to fetch item for key %@ with error: %d",key,(int)status); • return nil; • } • • return (__bridge NSData *)result; • }
  • 13. 更新 • -(BOOL) update:(NSString*)key :(NSData *)data • { • NSMutableDictionary *dictKey =[self prepareDict:key]; • • NSMutableDictionary *dictUpdate =[[NSMutableDictionary alloc] init]; • [dictUpdate setObject:data forKey:(__bridge id)kSecValueData]; • • OSStatus status = SecItemUpdate((__bridge CFDictionaryRef)dictKey, (__bridge CFDictionaryRef)dictUpdate); • if(status != errSecSuccess) { • NSLog(@"Unable add update with key = %@ error: %d",key,(int)status); • } • return (status == errSecSuccess); • }
  • 14. 移除 • -(BOOL) remove:(NSString *)key • { • NSMutableDictionary *dict = [self prepareDict:key]; • OSStatus status = SecItemDelete((__bridge CFDictionaryRef)dict); • if(status != errSecSuccess) { • NSLog(@"Unable to remove item for key %@ with error: %d",key,(int)status); • } • return (status == errSecSuccess); • }
  • 15. 開源 • SSKeychain https://github.com/soffes/sskeychain Star: 1730 (2014/12/30) • SFHFKeychainUtils https://github.com/kamiro/SFHFKeychainUtils Star: 60 (2014/12/30) • Me: 2 projects
  • 16. Demo • https://github.com/happymanx/KeyChainTest – 1). Initialization of the class – 2). How to Add an item to keychain – 3). Find an item in the keychain – 4). Update an item in the keychain – 5). Remove an item from keychain
  • 17. 參考 • iOS KeyChain Tutorial http://hayageek.com/ios-keychain-tutorial/ • Securing and Encrypting Data on iOS http://code.tutsplus.com/tutorials/securing-and- encrypting-data-on-ios--mobile-21263 • Basic Security in iOS 5 – Part 1 http://www.raywenderlich.com/6475/basic- security-in-ios-5-tutorial-part-1 • Basic Security in iOS 5 – Part 2 http://www.raywenderlich.com/6603/basic- security-in-ios-5-tutorial-part-2
  • 18. 參考 • iOS Keychain: Sharing data between apps http://shaune.com.au/ios-keychain-sharing-data- between-apps/ • Keychain Group Access http://useyourloaf.com/blog/2010/04/03/keycha in-group-access.html • 將密碼儲存於 KeyChain http://wp.me/p1my2P-3S0 • KeyChain 使用與共享數據 http://blog.csdn.net/ibcker/article/details/24839 143
  • 19. Apple連結 • Keychain Services Programming Guide https://developer.apple.com/library/mac/docum entation/Security/Conceptual/keychainServConc epts/ • Keychain Services Reference https://developer.apple.com/library/mac/docum entation/Security/Reference/keychainservices/ • #WWDC14 session 711 - Keychain and Authentication with Touch ID