Slide 1

Slide 1 text

KEYS FROM THE CASTLE ANCIENT ART OF MANAGING KEYS AND TRUST @vixentael #appbuilders17

Slide 2

Slide 2 text

WE ALL FAIL IN BUILDING SECURE MOBILE APPS

Slide 3

Slide 3 text

@vixentael Lead Developer at stanfy.com Core Contributor at themis/ cossacklabs.com Feel free to reach me with any mobile security questions. I do check my inbox :)

Slide 4

Slide 4 text

THE PLAN what is trust? key management 101 goals and processes key management on iOS #appbuilders17 @vixentael mr. Box

Slide 5

Slide 5 text

LET’S TALK ABOUT ESTABLISHING TRUST

Slide 6

Slide 6 text

ESTABLISHING TRUST is ensuring you and remote party share some identifiable secret #appbuilders17 @vixentael

Slide 7

Slide 7 text

ESTABLISHING TRUST #appbuilders17 @vixentael USING MATH! is ensuring you and remote party share some identifiable secret

Slide 8

Slide 8 text

servers mobile data in transit via public channels WHERE IT HAPPENS? #appbuilders17 @vixentael trust

Slide 9

Slide 9 text

#appbuilders17 @vixentael OUR INFRASTRUCTURE IS FULL OF KEYS AND DATA, ALL THAT IN CABLES. (transatlantic cable is transferring data, aka boxes)

Slide 10

Slide 10 text

WHAT DO WE NEED TRUST FOR? TO PROTECT THE DATA! confidentiality authenticity integrity #appbuilders17 @vixentael

Slide 11

Slide 11 text

HOW DOES IT WORK? #appbuilders17 @vixentael

Slide 12

Slide 12 text

HOW DOES IT WORK? confidentiality authenticity integrity #appbuilders17 @vixentael

Slide 13

Slide 13 text

#appbuilders17 @vixentael KEYS ARE WHAT WE TRUST

Slide 14

Slide 14 text

KEY MANAGEMENT FOR MOBILE DEVS

Slide 15

Slide 15 text

secret key (symmetric ciphers) public/private keys (asymmetric ciphers, PGP & SSL) password
 KDF(pass) = good one-time pin WHAT IS A KEY? – ARRAY OF BYTES #appbuilders17 @vixentael

Slide 16

Slide 16 text

KEYS APP TOKENS USER PASSWORDS PUBLIC CERTS #appbuilders17 @vixentael let appId = "VK1TTYC4TV" let poolId = "us-east-1:r0s3s4r3-r3d-13375p34k" var userPass = "F4C38D"

Slide 17

Slide 17 text

WE USE KEYS TO PROTECT THE DATA #appbuilders17 @vixentael

Slide 18

Slide 18 text

THE DATA? User’s data Access to external resources Identifiable data of other people #appbuilders17 @vixentael

Slide 19

Slide 19 text

THREATS TO THE DATA #appbuilders17 @vixentael

Slide 20

Slide 20 text

KEYS ARE SMALL CHUNKS OF DATA #appbuilders17 @vixentael

Slide 21

Slide 21 text

#appbuilders17 @vixentael THREATS TO THE KEYS

Slide 22

Slide 22 text

‣ stolen ‣ replayed ‣ replaced PROTECT KEYS TOO! — KEYS CAN BE: #appbuilders17 @vixentael

Slide 23

Slide 23 text

“TRUST AND SECURITY ARE PRESERVED, YET SYSTEM IS USABLE”

Slide 24

Slide 24 text

MAKING USABLE SYSTEM generation exchange storage access rotation revocation service #appbuilders17 @vixentael

Slide 25

Slide 25 text

KEY GENERATION #appbuilders17 @vixentael RND Secret Generation Key or Keypair good math where user inputs a secret or where it’s safe to store WHEN/WHERE?

Slide 26

Slide 26 text

KEY PAIR #appbuilders17 @vixentael let keyGeneratorEC: TSKeyGen = TSKeyGen(algorithm: .EC) let privateKeyEC: Data = keyGeneratorEC.privateKey let publicKeyEC: Data = keyGeneratorEC.publicKey https://github.com/cossacklabs/themis

Slide 27

Slide 27 text

KDF #appbuilders17 @vixentael let password: Array = Array("s33krit".utf8) let salt: Array = Array("nacllcan".utf8) try PKCS5.PBKDF2(password: password, salt: salt, iterations: 4096, variant: .sha256).calculate() https://github.com/krzyzanowskim/CryptoSwift

Slide 28

Slide 28 text

KEY EXCHANGE #appbuilders17 @vixentael — exchanging unique secret between parties to ensure authenticity and, sometimes, confidentiality.

Slide 29

Slide 29 text

KEY EXCHANGE #appbuilders17 @vixentael {“passw”:“123456”} passw: “123456” Alice-the-App Bob-the-Server insecure channel

Slide 30

Slide 30 text

KEY EXCHANGE 5720b3c2 fe674f54 73e10ad4 ... HTTPS SSL pinning ephemeral keys

Slide 31

Slide 31 text

KEY STORAGE #appbuilders17 @vixentael Never store the keys with the data they protect. Protect keys in a key vault.

Slide 32

Slide 32 text

KEY ACCESS #appbuilders17 @vixentael Make sure they are easy to access legitimately. Ensure that any secret key is protected from unauthorized access.

Slide 33

Slide 33 text

KEY ROTATION #appbuilders17 @vixentael Define a key lifecycle.

Slide 34

Slide 34 text

KEY ROTATION #appbuilders17 @vixentael Limit quantity of data encrypted with one key. Define a key lifecycle.

Slide 35

Slide 35 text

KEY ROTATION #appbuilders17 @vixentael Limit quantity of data encrypted with one key. Define a key lifecycle. Build support for changing algorithms and keys when needed.

Slide 36

Slide 36 text

KEY REVOCATION #appbuilders17 @vixentael Make sure that compromised or outdated keys don’t work.

Slide 37

Slide 37 text

SERVICE #appbuilders17 @vixentael BACKUPS ADMIN ACCESS KEY LINKING

Slide 38

Slide 38 text

KEY MANAGEMENT IN IOS

Slide 39

Slide 39 text

ESTABLISHING TRUST #appbuilders17 @vixentael 1. On-channel exchange: SSL pinning / SSL pre-keying https://developer.apple.com/reference/foundation/ urlsessiondelegate/1409308-urlsession func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { }

Slide 40

Slide 40 text

ESTABLISHING TRUST #appbuilders17 @vixentael 2. Mediated exchange / Public key infrastructure keybase.io

Slide 41

Slide 41 text

ESTABLISHING TRUST #appbuilders17 @vixentael 3. Trusted channel exchange

Slide 42

Slide 42 text

VERIFYING TRUST let pathToCert = Bundle.main.path(forResource: "pathtomycert", ofType: "cer") let localCertificate:NSData = NSData(contentsOfFile: pathToCert!)! let serverTrustPolicy = ServerTrustPolicy.pinCertificates( certificates: [SecCertificateCreateWithData(nil, localCertificate)!], validateCertificateChain: true, validateHost: true ) let serverTrustPolicies = [ "myserver.com": serverTrustPolicy ] let alamofireManager = Alamofire.SessionManager( configuration: URLSessionConfiguration.default, serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies) ) https://github.com/Alamofire/Alamofire#server-trust-policy-manager https://www.owasp.org/index.php/Pinning_Cheat_Sheet

Slide 43

Slide 43 text

STORING TRUST #appbuilders17 @vixentael

Slide 44

Slide 44 text

TRY NOT TO STORE KEYS

Slide 45

Slide 45 text

TRY NOT TO STORE KEYS BUT IF YOU DO, BE BOLD!

Slide 46

Slide 46 text

STORING TRUST (KEYS) #appbuilders17 @vixentael USER DEFINED APP DEFINED Keychain Encrypted KDF Obfuscated Encrypted Calculated

Slide 47

Slide 47 text

OBFUSCATE #appbuilders17 @vixentael ‣ Store keys as HEX ‣ Replace chars ‣ Rename .cert to .mp3 ‣ Combine from separate pieces

Slide 48

Slide 48 text

OBFUSCATE #appbuilders17 @vixentael ‣ Store keys as HEX ‣ Replace chars ‣ Rename .cert to .mp3 ‣ Combine from separate pieces ORING BORING BORING BORING BORING BORING BO

Slide 49

Slide 49 text

STORE ENCRYPTED #appbuilders17 @vixentael 1. Encrypt keys during development 2. Store encrypted keys 3. Decrypt before using

Slide 50

Slide 50 text

USE NICE TOOLS #appbuilders17 @vixentael SCIENTIFIC BACKGROUND TRUST BIG GUYS GOOD TRACK RECORD https://github.com/RNCryptor/RNCryptor https://github.com/cossacklabs/themis https://github.com/krzyzanowskim/CryptoSwift CommonCrypto wrappers Ports of popular libs Keychain wrappers https://www.cossacklabs.com/choose-your-ios-crypto.html

Slide 51

Slide 51 text

FAKE KEYS #appbuilders17 @vixentael Poison keys, marker keys let key = “0XD34DB33F" Analyze logs to find marker keys Block those users/apps

Slide 52

Slide 52 text

HONEYPOT #appbuilders17 @vixentael Put fake keys in obvious places: ‣ plist ‣ static strings ‣ fake certs

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

KEYPOINTS #appbuilders17 @vixentael 1. Keys to data are data too; you should protect them. 2. Separate keys from the data; don’t keep everything in one basket. 3. Protecting keys is a system of typical actions and goals.

Slide 55

Slide 55 text

ACCESS TRUST TO EVERY COMPONENT CAREFULLY; BUILD TOOLS TO MANAGE IT.

Slide 56

Slide 56 text

LINKS1 Cryptographic Storage Cheat Sheet https://www.owasp.org/index.php/Cryptographic_Storage_Cheat_Sheet Key Management Cheat Sheet https://www.owasp.org/index.php/Key_Management_Cheat_Sheet Managing Keys, Certificates, and Passwords https://developer.apple.com/library/content/documentation/Security/ Conceptual/cryptoservices/KeyManagementAPIs/KeyManagementAPIs.html

Slide 57

Slide 57 text

LINKS2 https://speakerdeck.com/vixentael/

Slide 58

Slide 58 text

Lead Developer at stanfy.com Core Contributor at themis/ cossacklabs.com Feel free to reach me with any mobile security questions. I do check my inbox :) @vixentael