SlideShare a Scribd company logo
Android Security
Key Management
Roberto Gassirà (r.gassira@mseclab.com)
Roberto Piccirillo (r.piccirillo@mseclab.com)
Android Security
Key Management
Roberto Piccirillo
●  Senior Security Analyst - Mobile Security Lab
○  Vulnerability Assessment (IT, Mobile Application)
○  Hijacking Mobile Data Connection
■  BlackHat Europe 2009
■  DeepSec Vienna 2009
■  HITB Amsterdam 2010
○  Android Secure Development
@robpicone
Android Security
Key Management
Roberto Gassirà
●  Senior Security Analyst - Mobile Security Lab
○  Vulnerability Assessment (IT, Mobile Application)
○  Hijacking Mobile Data Connection
■  BlackHat Europe 2009
■  DeepSec Vienna 2009
■  HITB Amsterdam 2010
○  Android Secure Development
●  IpTrack Developer
@robgas
Android Security
Key Management
Agenda
●  Key Management e CryptoSystem
●  Mobile Application: protezione dei dati
●  Key Management in Android e sue evoluzioni
●  Keychain e AndroidKeyStore
●  Tipologie di AndroidKeyStore
●  Codelab
○  Generazione chiave pubblica/privata
○  Accesso AndroidKeyStore
○  Digital signature e Encryption
Android Security
Key Management
Key Management
"Key management is the management of
cryptographic keys in a cryptosystem."
Android Security
Key Management
CryptoSystem
●  "refers to a suite of algorithms needed to implement
a particular form of encryption and decryption"
●  Tipologie di encryption:
○  Symmetric Key Algorithms
■  Identical encryption key for
encryption/decryption
○  Asymmetric Key Algorithms
■  Different key for encryption/decryption
Android Security
Key Management
In app?
●  Protezione dati riservati
○  Dati dell'applicazione
○  Dati su /sdcard
○  Chiavi di cifratura
●  Scambio sicuro di dati
○  Documento
○  Mail
○  SMS
○  Chiave di sessione
●  Firma digitale
○  Documento
○  Mail
Android Security
Key Management
Key Management in Android
●  Application Level
○  File nell'applicazione ( Shared Prefs, File, ... )
○  Code Obfuscation
○  Generazione a run-time
Android Security
Key Management
Key Management in Android
●  "User" level
○  PBKDF2 (Password Based Key Derivation Function)
■  Algoritmo di generazione ( PBEWithSHA256And256BitAES-CBC-BC )
■  Salt
■  Numero di Iterazioni
●  System Level
○  KeyChain (da >= 4.0 )
○  AndroidKeyStore (ufficiale da >= 4.3)
Android Security
Key Management
KeyChain e AndroidKeyStore
●  KeyChain
○  Accessibile da qualunque applicazione
●  AndroidKeyStore
○  Accessibile alla singola applicazione ed al singolo utente
○  Memorizza solo coppia chiave pubblica/privata RSA 2048
Android Security
Key Management
Key Management Evolution
API LEVEL 14 API LEVEL 18
Global Level:
KeyChain
( Public API )
App Level:
KeyStore
( Closed API )
Global Level Only:
Default TrustStore
cacerts.bks
(ROOTED device)
Global Level:
KeyChain
( Public API )
App Level and
per User Level:
AndroidKeyStore
( Public API )
Android Security
Key Management
AndroidKeyStore Storage
●  Due tipologie di Storage
○  Hardware-backed (Nexus 7, Nexus
4, Nexus 5 :-) con OS >= 4.3)
○  Secure Element
○  TPM
○  TrustZone
○  Software only (Rimanenti dispositivi
con OS >= 4.3)
Android Security
Key Management
Storage?
import android.security.KeyChain;
if (KeyChain.isBoundKeyAlgorithm("RSA"))
// Hardware-Backed
else
// Software Only
Android Security
Key Management
AndroidKeyStore
in pratica
Android Security
Key Management
Cosa faremo
CodeLab diviso in 4 step:
1. Generazioni Chiavi
2. Accesso AndroidKeyStore
3. Firma e Verifica
4. Cifratura/Decifratura
Android Security
Key Management
Definizione Specifiche Certificato
Context cx = getActivity();
String pkg = cx.getPackageName();
Calendar notBefore = Calendar.getInstance();
Calendar notAfter = Calendar.getInstance();
notAfter.add(1, Calendar.YEAR);
import android.security.KeyPairGeneratorSpec.Builder;
Builder builder = new KeyPairGeneratorSpec.Builder(cx);
builder.setAlias(“DEVKEY1”);
String infocert = String.format("CN=%s, OU=%s", “DEVKEY1”, pkg);
builder.setSubject(new X500Principal(infocert));
builder.setSerialNumber(BigInteger.ONE);
builder.setStartDate(notBefore.getTime());
builder.setEndDate(notAfter.getTime());
KeyPairGeneratorSpec spec = builder.build();
Definizione Paramentri
Temporali
Self-Signed X.509
●  Common Name (CN)
●  Subject (OU)
●  Serial Number
Generazione delle specifiche del
Certificato
ALIAS per indicizzare
il certificato
Android Security
Key Management
Generazione chiave pubblica/privata
KeyPairGenerator kpGenerator;
kpGenerator = KeyPairGenerator
.getInstance("RSA", "AndroidKeyStore");
kpGenerator.initialize(spec);
KeyPair kp;
kp = kpGenerator.generateKeyPair();
Engine per generazione
chiave Privata/Pubblica
Istanza Engine con:
●  Algoritmo RSA
●  Provider: AndroidKeyStore
Init Engine con le specifiche del certificato
Dopo la generazione, le chiavi saranno memorizzate in
Android Key Store e potranno essere recuperate mediante l’ALIAS
●  Generazione chiave Privata/Pubblica
Android Security
Key Management
Abbiamo il riferimento keyStore che utilizzeremo per
accedere alla coppia chiave Pubblica/Privata mediante
l’identificativo ALIAS
Dovrebbe essere utilizzato nel caso si ha un
InputStream da caricare (per esempio il nome di un
KeyStore importato). Se non invocato l’applicazione
andrà in CRASH
Inizializzazione Android Key Store
keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
Richiesta di Android Key Store
Android Security
Key Management
RSA Digital Signature
●  Digital Signature
○  Authentication, Non-Repudiation and Integrity
○  RSA Private key to Sign
○  RSA Public Key to Verify
KeyStore.Entry entry = ks.getEntry(“DEVKEY1”, null);
byte[] data = “DevFest Rome 2013!”.getBytes();
Signature s = Signature.getInstance(“SHA256withRSA”);
s.initSign(((KeyStore.PrivateKeyEntry) entry).getPrivateKey());
s.update(data);
byte[] signature = s.sign();
String result = null;
result = Base64.encodeToString(signature, Base64.DEFAULT);
Accesso chiave Pubblica e Privata
identificata dall’ALIAS==DEVKEY1
Scelta dell’algoritmo
Chiave Privata per firmare
Firma e codifica in
Base64
Android Security
Key Management
Verify RSA Digital Signature
byte[] data = input.getBytes();
byte[] signature;
signature = Base64.decode(signatureStr, Base64.DEFAULT);
KeyStore.Entry entry = ks.getEntry(“DEVKEY1”, null);
Signature s = Signature.getInstance("SHA256withRSA");
s.initVerify(((KeyStore.PrivateKeyEntry) entry).getCertificate());
s.update(data);
boolean valid = s.verify(signature);
Decodifica Base64
Accesso chiave Pubblica e Privata
identificata dall’ALIAS==DEVKEY1
Scelta dell’algoritmo
Chiave Pubblica nel
certificato utilizzata per
verificare
TRUE == Verified
FALSE== Not Verified
Android Security
Key Management
RSA Encryption
●  Encryption
○  Confidentiality
○  RSA Public key to Encrypt
○  RSA Private key to Decrypt
PublicKey publicKeyEnc = ((KeyStore.PrivateKeyEntry) entry)
.getCertificate().getPublicKey();
String textToEncrypt = new String("DevFest Rome 2013");
byte[] textToEncryptToByte = textToEncrypt.getBytes();
Cipher encCipher = null;
byte[] encryptedText = null;
encCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
encCipher.init(Cipher.ENCRYPT_MODE, publicKeyEnc);
encryptedText = encCipher.doFinal(textToEncryptToByte);
Accesso alla chiave
pubblica per cifrare
●  Scelta algoritmo
●  Encryption con chiave
pubblica
Testo Cifrato
Android Security
Key Management
RSA Decryption
Cipher decCipher = null;
byte[] plainTextByte = null;
decCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
decCipher.init(Cipher.DECRYPT_MODE,
((KeyStore.PrivateKeyEntry) entry).getPrivateKey());
plainTextByte = decCipher.doFinal(ecryptedText);
String plainText = new String(plainTextByte);
Scelta algoritmo
Decryption con la
chiave privata
Decryption con chiave privata
Testo decifrato
Android Security
Key Management
Si osserva che...
●  Vari tipi di screen lock
●  La scelta di screen lock
impatta sulla persistenza
delle chiavi generate
●  Cambiando il tipo di screen
lock le chiavi vengono
cancellate
Android Security
Key Management
Comportamento atteso?
●  La documentazione riporta
●  Le chiavi non dovrebbero essere cancellate quando il
tipo di screen lock viene cambiato dall’utente
Android Security
Key Management
Cryptographic stuff on devices
●  Device con Storage “Hardware-backed”
●  Device con Storage “Software-only”
Android Security
Key Management
References
●  http://developer.android.com/about/versions/android-4.3.html#Security
●  http://developer.android.com/reference/java/security/KeyStore.html
●  http://en.wikipedia.org/wiki/Encryption
●  http://en.wikipedia.org/wiki/Digital_signature
●  http://nelenkov.blogspot.it/2013/08/credential-storage-enhancements-
android-43.html
●  http://nelenkov.blogspot.it/2012/05/storing-application-secrets-in-androids.html
●  http://nelenkov.blogspot.it/2012/04/using-password-based-encryption-on.html
●  http://nelenkov.blogspot.it/2011/11/ics-credential-storage-implementation.html
●  http://developer.android.com/reference/android/security/
KeyPairGeneratorSpec.html
Android Security
Key Management
Grazie
Q&A www.mseclab.com
www.consulthink.it
research@mseclab.com
Android Security
Key Management
Requisiti
●  Portatile
●  Eclipse con ADT Plugin 22.3.0
●  SDK Android 4.4 ( API 19 )
●  Android SDK Build-tools 19

More Related Content

PDF
Prevenzione degli attacchi informatici che coinvolgono dati sensibili aziendali
PPTX
Test driven development demo
PPTX
Art of-unittesting
PDF
Introduction to test_driven_development
PPS
Bathroom Installers Dublin
PDF
Consulthink Overview
PPTX
Mobilitat sostenible
DOCX
Ekonomi teknik 2
Prevenzione degli attacchi informatici che coinvolgono dati sensibili aziendali
Test driven development demo
Art of-unittesting
Introduction to test_driven_development
Bathroom Installers Dublin
Consulthink Overview
Mobilitat sostenible
Ekonomi teknik 2

Viewers also liked (15)

DOCX
Analisis rate of return
PDF
Industri Minyak di Masa Depan
PPTX
IPv6 - Breve panoramica tra mito e realtà
PPTX
Iptek dan pengolah sampah didesa cepor
DOCX
Peran warga dalam mendukung upaya pemenuhan kebutuhan listrik
PPTX
Iptek dan pengolah sampah didesa cepor
PPTX
Quality Software Development LifeCycle
DOCX
Analisis rate of return
PDF
Artikel IPTEK Tentang Pertambangan
DOCX
Konsep nilai waktu dari uang
PDF
Droidcon it 2015: Android Lollipop for Enterprise
PPTX
Behavior Driven Development
PPT
Schizophrenia
PDF
Test driven development_continuous_integration
PDF
SEO: Getting Personal
Analisis rate of return
Industri Minyak di Masa Depan
IPv6 - Breve panoramica tra mito e realtà
Iptek dan pengolah sampah didesa cepor
Peran warga dalam mendukung upaya pemenuhan kebutuhan listrik
Iptek dan pengolah sampah didesa cepor
Quality Software Development LifeCycle
Analisis rate of return
Artikel IPTEK Tentang Pertambangan
Konsep nilai waktu dari uang
Droidcon it 2015: Android Lollipop for Enterprise
Behavior Driven Development
Schizophrenia
Test driven development_continuous_integration
SEO: Getting Personal
Ad

More from Consulthinkspa (12)

PPTX
GDPR - Il Nuovo Regolamento Generale sulla Protezione dei Dati
PPTX
Big Data Vs. Open Data
PPTX
Data Science
PPTX
Hot trend 2017
PPTX
Pensiero Analogico e Microservizi
PDF
DevOps - Come diventare un buon DevOpper
PDF
Increasing Android app security for free - Roberto Gassirà, Roberto Piccirill...
PDF
Scenari introduzione Application Service Governance in Azienda
PDF
Test Driven Development
PPTX
BitCoin Protocol
PDF
Big data - stack tecnologico
PDF
Consulthink @ GDG Meets U - L'Aquila2014 - Codelab: Android Security -Il ke...
GDPR - Il Nuovo Regolamento Generale sulla Protezione dei Dati
Big Data Vs. Open Data
Data Science
Hot trend 2017
Pensiero Analogico e Microservizi
DevOps - Come diventare un buon DevOpper
Increasing Android app security for free - Roberto Gassirà, Roberto Piccirill...
Scenari introduzione Application Service Governance in Azienda
Test Driven Development
BitCoin Protocol
Big data - stack tecnologico
Consulthink @ GDG Meets U - L'Aquila2014 - Codelab: Android Security -Il ke...
Ad

Android Security - Key Management at GDG DevFest Rome 2013

  • 1. Android Security Key Management Roberto Gassirà (r.gassira@mseclab.com) Roberto Piccirillo (r.piccirillo@mseclab.com)
  • 2. Android Security Key Management Roberto Piccirillo ●  Senior Security Analyst - Mobile Security Lab ○  Vulnerability Assessment (IT, Mobile Application) ○  Hijacking Mobile Data Connection ■  BlackHat Europe 2009 ■  DeepSec Vienna 2009 ■  HITB Amsterdam 2010 ○  Android Secure Development @robpicone
  • 3. Android Security Key Management Roberto Gassirà ●  Senior Security Analyst - Mobile Security Lab ○  Vulnerability Assessment (IT, Mobile Application) ○  Hijacking Mobile Data Connection ■  BlackHat Europe 2009 ■  DeepSec Vienna 2009 ■  HITB Amsterdam 2010 ○  Android Secure Development ●  IpTrack Developer @robgas
  • 4. Android Security Key Management Agenda ●  Key Management e CryptoSystem ●  Mobile Application: protezione dei dati ●  Key Management in Android e sue evoluzioni ●  Keychain e AndroidKeyStore ●  Tipologie di AndroidKeyStore ●  Codelab ○  Generazione chiave pubblica/privata ○  Accesso AndroidKeyStore ○  Digital signature e Encryption
  • 5. Android Security Key Management Key Management "Key management is the management of cryptographic keys in a cryptosystem."
  • 6. Android Security Key Management CryptoSystem ●  "refers to a suite of algorithms needed to implement a particular form of encryption and decryption" ●  Tipologie di encryption: ○  Symmetric Key Algorithms ■  Identical encryption key for encryption/decryption ○  Asymmetric Key Algorithms ■  Different key for encryption/decryption
  • 7. Android Security Key Management In app? ●  Protezione dati riservati ○  Dati dell'applicazione ○  Dati su /sdcard ○  Chiavi di cifratura ●  Scambio sicuro di dati ○  Documento ○  Mail ○  SMS ○  Chiave di sessione ●  Firma digitale ○  Documento ○  Mail
  • 8. Android Security Key Management Key Management in Android ●  Application Level ○  File nell'applicazione ( Shared Prefs, File, ... ) ○  Code Obfuscation ○  Generazione a run-time
  • 9. Android Security Key Management Key Management in Android ●  "User" level ○  PBKDF2 (Password Based Key Derivation Function) ■  Algoritmo di generazione ( PBEWithSHA256And256BitAES-CBC-BC ) ■  Salt ■  Numero di Iterazioni ●  System Level ○  KeyChain (da >= 4.0 ) ○  AndroidKeyStore (ufficiale da >= 4.3)
  • 10. Android Security Key Management KeyChain e AndroidKeyStore ●  KeyChain ○  Accessibile da qualunque applicazione ●  AndroidKeyStore ○  Accessibile alla singola applicazione ed al singolo utente ○  Memorizza solo coppia chiave pubblica/privata RSA 2048
  • 11. Android Security Key Management Key Management Evolution API LEVEL 14 API LEVEL 18 Global Level: KeyChain ( Public API ) App Level: KeyStore ( Closed API ) Global Level Only: Default TrustStore cacerts.bks (ROOTED device) Global Level: KeyChain ( Public API ) App Level and per User Level: AndroidKeyStore ( Public API )
  • 12. Android Security Key Management AndroidKeyStore Storage ●  Due tipologie di Storage ○  Hardware-backed (Nexus 7, Nexus 4, Nexus 5 :-) con OS >= 4.3) ○  Secure Element ○  TPM ○  TrustZone ○  Software only (Rimanenti dispositivi con OS >= 4.3)
  • 13. Android Security Key Management Storage? import android.security.KeyChain; if (KeyChain.isBoundKeyAlgorithm("RSA")) // Hardware-Backed else // Software Only
  • 15. Android Security Key Management Cosa faremo CodeLab diviso in 4 step: 1. Generazioni Chiavi 2. Accesso AndroidKeyStore 3. Firma e Verifica 4. Cifratura/Decifratura
  • 16. Android Security Key Management Definizione Specifiche Certificato Context cx = getActivity(); String pkg = cx.getPackageName(); Calendar notBefore = Calendar.getInstance(); Calendar notAfter = Calendar.getInstance(); notAfter.add(1, Calendar.YEAR); import android.security.KeyPairGeneratorSpec.Builder; Builder builder = new KeyPairGeneratorSpec.Builder(cx); builder.setAlias(“DEVKEY1”); String infocert = String.format("CN=%s, OU=%s", “DEVKEY1”, pkg); builder.setSubject(new X500Principal(infocert)); builder.setSerialNumber(BigInteger.ONE); builder.setStartDate(notBefore.getTime()); builder.setEndDate(notAfter.getTime()); KeyPairGeneratorSpec spec = builder.build(); Definizione Paramentri Temporali Self-Signed X.509 ●  Common Name (CN) ●  Subject (OU) ●  Serial Number Generazione delle specifiche del Certificato ALIAS per indicizzare il certificato
  • 17. Android Security Key Management Generazione chiave pubblica/privata KeyPairGenerator kpGenerator; kpGenerator = KeyPairGenerator .getInstance("RSA", "AndroidKeyStore"); kpGenerator.initialize(spec); KeyPair kp; kp = kpGenerator.generateKeyPair(); Engine per generazione chiave Privata/Pubblica Istanza Engine con: ●  Algoritmo RSA ●  Provider: AndroidKeyStore Init Engine con le specifiche del certificato Dopo la generazione, le chiavi saranno memorizzate in Android Key Store e potranno essere recuperate mediante l’ALIAS ●  Generazione chiave Privata/Pubblica
  • 18. Android Security Key Management Abbiamo il riferimento keyStore che utilizzeremo per accedere alla coppia chiave Pubblica/Privata mediante l’identificativo ALIAS Dovrebbe essere utilizzato nel caso si ha un InputStream da caricare (per esempio il nome di un KeyStore importato). Se non invocato l’applicazione andrà in CRASH Inizializzazione Android Key Store keyStore = KeyStore.getInstance("AndroidKeyStore"); keyStore.load(null); Richiesta di Android Key Store
  • 19. Android Security Key Management RSA Digital Signature ●  Digital Signature ○  Authentication, Non-Repudiation and Integrity ○  RSA Private key to Sign ○  RSA Public Key to Verify KeyStore.Entry entry = ks.getEntry(“DEVKEY1”, null); byte[] data = “DevFest Rome 2013!”.getBytes(); Signature s = Signature.getInstance(“SHA256withRSA”); s.initSign(((KeyStore.PrivateKeyEntry) entry).getPrivateKey()); s.update(data); byte[] signature = s.sign(); String result = null; result = Base64.encodeToString(signature, Base64.DEFAULT); Accesso chiave Pubblica e Privata identificata dall’ALIAS==DEVKEY1 Scelta dell’algoritmo Chiave Privata per firmare Firma e codifica in Base64
  • 20. Android Security Key Management Verify RSA Digital Signature byte[] data = input.getBytes(); byte[] signature; signature = Base64.decode(signatureStr, Base64.DEFAULT); KeyStore.Entry entry = ks.getEntry(“DEVKEY1”, null); Signature s = Signature.getInstance("SHA256withRSA"); s.initVerify(((KeyStore.PrivateKeyEntry) entry).getCertificate()); s.update(data); boolean valid = s.verify(signature); Decodifica Base64 Accesso chiave Pubblica e Privata identificata dall’ALIAS==DEVKEY1 Scelta dell’algoritmo Chiave Pubblica nel certificato utilizzata per verificare TRUE == Verified FALSE== Not Verified
  • 21. Android Security Key Management RSA Encryption ●  Encryption ○  Confidentiality ○  RSA Public key to Encrypt ○  RSA Private key to Decrypt PublicKey publicKeyEnc = ((KeyStore.PrivateKeyEntry) entry) .getCertificate().getPublicKey(); String textToEncrypt = new String("DevFest Rome 2013"); byte[] textToEncryptToByte = textToEncrypt.getBytes(); Cipher encCipher = null; byte[] encryptedText = null; encCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); encCipher.init(Cipher.ENCRYPT_MODE, publicKeyEnc); encryptedText = encCipher.doFinal(textToEncryptToByte); Accesso alla chiave pubblica per cifrare ●  Scelta algoritmo ●  Encryption con chiave pubblica Testo Cifrato
  • 22. Android Security Key Management RSA Decryption Cipher decCipher = null; byte[] plainTextByte = null; decCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); decCipher.init(Cipher.DECRYPT_MODE, ((KeyStore.PrivateKeyEntry) entry).getPrivateKey()); plainTextByte = decCipher.doFinal(ecryptedText); String plainText = new String(plainTextByte); Scelta algoritmo Decryption con la chiave privata Decryption con chiave privata Testo decifrato
  • 23. Android Security Key Management Si osserva che... ●  Vari tipi di screen lock ●  La scelta di screen lock impatta sulla persistenza delle chiavi generate ●  Cambiando il tipo di screen lock le chiavi vengono cancellate
  • 24. Android Security Key Management Comportamento atteso? ●  La documentazione riporta ●  Le chiavi non dovrebbero essere cancellate quando il tipo di screen lock viene cambiato dall’utente
  • 25. Android Security Key Management Cryptographic stuff on devices ●  Device con Storage “Hardware-backed” ●  Device con Storage “Software-only”
  • 26. Android Security Key Management References ●  http://developer.android.com/about/versions/android-4.3.html#Security ●  http://developer.android.com/reference/java/security/KeyStore.html ●  http://en.wikipedia.org/wiki/Encryption ●  http://en.wikipedia.org/wiki/Digital_signature ●  http://nelenkov.blogspot.it/2013/08/credential-storage-enhancements- android-43.html ●  http://nelenkov.blogspot.it/2012/05/storing-application-secrets-in-androids.html ●  http://nelenkov.blogspot.it/2012/04/using-password-based-encryption-on.html ●  http://nelenkov.blogspot.it/2011/11/ics-credential-storage-implementation.html ●  http://developer.android.com/reference/android/security/ KeyPairGeneratorSpec.html
  • 27. Android Security Key Management Grazie Q&A www.mseclab.com www.consulthink.it research@mseclab.com
  • 28. Android Security Key Management Requisiti ●  Portatile ●  Eclipse con ADT Plugin 22.3.0 ●  SDK Android 4.4 ( API 19 ) ●  Android SDK Build-tools 19