35
Android Security Mechanisms Lecture 9 Android and Low-level Optimizations Summer School 1 August 2015 This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/. Android Security Mechanisms, Lecture 9 1/35

Android Security Mechanisms

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Android Security Mechanisms

Android Security MechanismsLecture 9

Android and Low-level Optimizations Summer School

1 August 2015

This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of thislicense, visit http://creativecommons.org/licenses/by/4.0/.

Android Security Mechanisms, Lecture 9 1/35

Page 2: Android Security Mechanisms

Android Permissions

Cryptographic Providers

Network Security

Bibliography

Keywords

Android Security Mechanisms, Lecture 9 2/35

Page 3: Android Security Mechanisms

Outline

Android Permissions

Cryptographic Providers

Network Security

Bibliography

Keywords

Android Security Mechanisms, Lecture 9 3/35

Page 4: Android Security Mechanisms

Android Permissions

I A string

I The ability to perform a particular operationI Built-in permissions documented in the platform API

referenceI Defined in the android package

I Custom permissions - defined by system or user apps

I pm list permissions

I Defining package + .permission + nameI android.permission.REBOOTI com.android.laucher3.permission.RECEIVE_LAUNCH_-

BROADCASTS

Android Security Mechanisms, Lecture 9 4/35

Page 5: Android Security Mechanisms

Android Permissions

I Apps request permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

I Assigned to apps at install time by the package managerservice

I Central database of installed packagesI /data/system/packages.xml

I Programatically access package information fromandroid.content.pm.PackageManager

I getPackageInfo() returns PackageInfo instance

I Cannot be changed or revoked without uninstalling app

Android Security Mechanisms, Lecture 9 5/35

Page 6: Android Security Mechanisms

Permission Protection Levels

I Potential risk and procedure to grant permissionI Normal

I Low riskI Automatically granted without user confirmationI ACCESS_NETWORK_STATE, GET_ACCOUNTS

I DangerousI Access to user data or control over the deviceI Requires user confirmation - accept or cancel installationI CAMERA, READ_SMS

Android Security Mechanisms, Lecture 9 6/35

Page 7: Android Security Mechanisms

Permission Protection Levels

I SignatureI Highest level of protectionI Apps signed with the same key as the app that declared the

permissionI Built-in permissions are used by system apps (signed with

platform key)I NET_ADMIN, ACCESS_ALL_EXTERNAL_STORAGE

I SignatureOrSystemI Apps part of system image or signed with the same key as the

app that declared the permissionI Vendors may have preinstalled apps without using the platform

key

Android Security Mechanisms, Lecture 9 7/35

Page 8: Android Security Mechanisms

Kernel-Level Enforcement

I Access to regular files, device nodes and local socketsmanaged by the Linux kernel, based on UID, GID

I Permissions are mapped to supplementary GIDs

I Built-in permission mapping in/etc/permission/platform.xml

I Example:I INTERNET permission associated with GID inetI Only apps with INTERNET permission can create network

socketsI The kernel verifies if the app belongs to GID inet

Android Security Mechanisms, Lecture 9 8/35

Page 9: Android Security Mechanisms

Framework-Level Enforcement

I Static permission enforcementI System keeps track of permissions associated to each app

componentI Checks whether callers have the required permission before

allowing accessI Enforcement by runtime environmentI Isolating security decisions from business logicI Less flexible

I Dynamic permission enforcementI Components check to see if the caller has the necessary

permissionsI Decisions made by each component, not by runtime

environmentI More fine-grained access controlI More operations in components

Android Security Mechanisms, Lecture 9 9/35

Page 10: Android Security Mechanisms

Dynamic Enforcement

I Helper methods in android.content.Context class toperform permission check

I checkPermission(String permission, int pid, intuid)

I Returns PERMISSION_GRANTED or PERMISSION_DENIEDI For root and system, permission is automatically grantedI If permission is declared by calling app, it is grantedI Deny for private componentsI Queries the Package Manager

I enforcePermission(String permission, int pid, intuid, String message)

I Throws SecurityException with message if permission is notgranted

Android Security Mechanisms, Lecture 9 10/35

Page 11: Android Security Mechanisms

Static Enforcement

I An app tries to call a component of another app - intent

I Target component - android:permission

I Caller - <uses-permission>I Activity Manager

I Resolves intentI Checks if target component has an associated permissionI Delegates permission check to Package Manager

I If caller has necessary permission, the target component isstarted

I Otherwise, a SecurityException is generated

Android Security Mechanisms, Lecture 9 11/35

Page 12: Android Security Mechanisms

Activity and Service Permission Enforcement

I Permission checks for activitiesI Intent is passed to Context.startActivity() or

startActivityForResult()I Resolves to an activity that declares a permission

I Permission checks for servicesI Intent passed to Context.startService() or

stopService() or bindService()I Resolves to a service that declares a permission

I If caller does not have the necessary permission, generatesSecurityExceptions

Android Security Mechanisms, Lecture 9 12/35

Page 13: Android Security Mechanisms

Content Provider Permission Enforcement

I Protect the whole component or a particular exported URI

I Different permissions for reading and writing

I Read permission - ContentResolver.query() on provider orURI

I Write permission - ContentResolver.insert(),update(), delete() on provider or URI

I Synchronous checks

Android Security Mechanisms, Lecture 9 13/35

Page 14: Android Security Mechanisms

Broadcast Permission Enforcement

I Receivers may be required to have a permissionI Context.sendBroadcast(Intent intent, String

receiverPermission)I Check when delivering intent to receiversI No permission - broadcast not received, no exception

I Broadcasters may need to have a permission to send abroadcast

I Specified in manifest or in registerReceiverI Checked when delivering broadcastI No permission - no delivery, no exception

I 2 checks for each delivery: for sender and receiver

Android Security Mechanisms, Lecture 9 14/35

Page 15: Android Security Mechanisms

Custom Permissions

I Declared by apps

I Checked statically by the system or dynamically by thecomponents

I Declared in AndroidManifest.xml

<pe rm i s s i on−t r e eand ro i d : name=”com . example . app . p e rm i s s i o n ”and ro i d : l a b e l=”@ s t r i n g / e x amp l e p e rm i s s i o n t r e e l a b e l ” />

<pe rm i s s i on−groupand ro i d : name=”com . example . app . p e rm i s s i on−group .TEST GROUP”and ro i d : l a b e l=”@ s t r i n g / t e s t p e rm i s s i o n g r o u p l a b e l ”and ro i d : d e s c r i p t i o n=”@ s t r i n g / t e s t p e rm i s s i o n g r o u p d e s c ” />

<p e rm i s s i o nand ro i d : name=”com . example . app . p e rm i s s i o n . PERMISSION1”and ro i d : l a b e l=”@ s t r i n g / p e rm i s s i o n 1 l a b e l ”and ro i d : d e s c r i p t i o n=”@ s t r i n g / p e rm i s s i o n 1 d e s c ”and ro i d : pe rm i s s i onGroup=”com . example . app . p e rm i s s i on−group .TEST GROUP”and ro i d : p r o t e c t i o n L e v e l=”s i g n a t u r e ” />

Android Security Mechanisms, Lecture 9 15/35

Page 16: Android Security Mechanisms

Outline

Android Permissions

Cryptographic Providers

Network Security

Bibliography

Keywords

Android Security Mechanisms, Lecture 9 16/35

Page 17: Android Security Mechanisms

JCA Provider Architecture

I Java Cryptography Architecture (JCA)I Extensible cryptographic provider frameworkI Set of APIs - major cryptographic primitivesI Applications specify an algorithm, do not depend on particular

provider implementation

I Cryptographic Service Provider (CSP)I Package with implementation of cryptographic servicesI Advertises the implemented services and algorithmsI JCA maintains a registry of providers and their algorithmsI Providers in a order of preference

I Service Provider Interface (SPI)I Common interface for implementations of a specific algorithmI Abstract class implemented by provider

Android Security Mechanisms, Lecture 9 17/35

Page 18: Android Security Mechanisms

JCA Engine Classes

I JCA engines provide:I Cryptographic operations (encrypt/decrypt, sign/verify, hash)I Generation or conversion of cryptographic material (keys,

parameters)I Management and storage of cryptographic objects (keys,

certificates)

I Decouple client code from algorithm implementation

I Static factory method getInstance()

I Request implementation indirectly

s t a t i c EngineClassName g e t I n s t a n c e ( S t r i n g a l g o r i t hm )throws NoSuchAlgor i thmExcept ion

s t a t i c EngineClassName g e t I n s t a n c e ( S t r i n g a l go r i t hm , S t r i n g p r o v i d e r )throws NoSuchAlgor i thmExcept ion , NoSuchProv ide rExcept i on

s t a t i c EngineClassName g e t I n s t a n c e ( S t r i n g a l go r i t hm , P r o v i d e r p r o v i d e r )throws NoSuchAlgor i thmExcept ion

Android Security Mechanisms, Lecture 9 18/35

Page 19: Android Security Mechanisms

Message Digest

I Hash function

MessageDigest md = MessageDiges t . g e t I n s t a n c e (”SHA−256”);by te [ ] data = getMessage ( ) ;by te [ ] hash = md. d i g e s t ( data ) ;

I Data provided in chuncks using update() then call digest()

I If data is short and fixed - hashed in one step using digest()

Android Security Mechanisms, Lecture 9 19/35

Page 20: Android Security Mechanisms

Signature

I Digital signature algorithms based on asymmetric encryption

I Algorithm name: <digest>with<encryption>

I Sign:

byte [ ] data = ”message to be s i g n ed ” . ge tBy te s (” ASCII ” ) ;

S i g n a t u r e s = S i gna t u r e . g e t I n s t a n c e (” SHA256withRSA ” ) ;s . i n i t S i g n ( p r i vKey ) ;s i g . update ( data ) ;by te [ ] s i g n a t u r e = s i g . s i g n ( ) ;

I Verify:

S i g na t u r e s = S i gna t u r e . g e t I n s t a n c e (” SHA256withRSA ” ) ;s . i n i t V e r i f y ( pubKey ) ;s . update ( data ) ;boo l ean v a l i d = s . v e r i f y ( s i g n a t u r e ) ;

Android Security Mechanisms, Lecture 9 20/35

Page 21: Android Security Mechanisms

Cipher

I Encryption and decryption operations

I Encryption:

Se c r e t key = ge tSec r e tKey ( ) ;

C iphe r c = C iphe r . g e t I n s t a n c e (”AES/CBC/PKCS5Padding ” ) ;

by te [ ] i v = new byte [ c . g e tB l o c kS i z e ( ) ] ;SecureRandom s r = new SecureRandom ( ) ;s r . n ex tBy te s ( i v ) ;I vParamete rSpec i v p = new IvParamete rSpec ( i v ) ;c . i n i t ( C iphe r .ENCRYPT MODE, key , i v p ) ;

by te [ ] data = ”Message to en c r yp t ” . ge tBy t e s (”UTF−8”);by te [ ] c i p h e r t e x t = c . doF i n a l ( data ) ;

Android Security Mechanisms, Lecture 9 21/35

Page 22: Android Security Mechanisms

Cipher

I Decryption:

Ciphe r c = C iphe r . g e t I n s t a n c e (”AES/CBC/PKCS5Padding ” ) ;c . i n i t ( C iphe r .DECRYPT MODE, key , i v p ) ;

by te [ ] data = c . doF i n a l ( c i p h e r t e x t ) ;

Android Security Mechanisms, Lecture 9 22/35

Page 23: Android Security Mechanisms

MAC

I Message Authentication Code algorithms

Secre tKey key = ge tSec r e tKey ( ) ;Mac m = Mac . g e t I n s t a n c e (”HmacSha256 ” ) ;m. i n i t ( key ) ;by te [ ] data = ”Message ” . ge tBy te s (”UTF−8”);by te [ ] hmac = m. doF i n a l ( data ) ;

Android Security Mechanisms, Lecture 9 23/35

Page 24: Android Security Mechanisms

KeyGenerator

I Generates symmetric keys

I Additional checks for weak keys

I Set key parity when necessary

I Takes advantage of the cryptographic hardware

KeyGenerator kg = KeyGenerator . g e t I n s t a n c e (”HmacSha256 ” ) ;Sec re tKey key = kg . gene ra teKey ( ) ;

KeyGenerator kg = KeyGenerator . g e t I n s t a n c e (”AES” ) ;kg . i n i t ( 2 5 6 ) ;Sec re tKey key = kg . gene ra teKey ( ) ;

Android Security Mechanisms, Lecture 9 24/35

Page 25: Android Security Mechanisms

KeyPairGenerator

I Generates public and private keys

KeyPa i rGene ra to r kpg = KeyPa i rGene ra to r . g e t I n s t a n c e (”RSA” ) ;kpg . i n i t i a l i z e ( 1 024 ) ;KeyPai r p a i r = kpg . g ene r a t eKeyPa i r ( ) ;P r i va t eKey p r i v = p a i r . g e t P r i v a t e ( ) ;Pub l i cKey pub = p a i r . g e tPub l i c ( ) ;

Android Security Mechanisms, Lecture 9 25/35

Page 26: Android Security Mechanisms

Android JCA Providers

I Harmony’s Crypto ProviderI Limited JCA provider part of the Java runtime libraryI SecureRandom (SHA1PRNG), KeyFactory (DSA)I MessageDigest (SHA-1), Signature (SHA1withDSA)

I Android’s Bouncy Castle ProviderI Full-featured JCA providerI Part of the Bouncy Castle Crypto APII Cipher, KeyGenerator, Mac, MessageDigest, SecretKeyFactory,

Signature, CertificateFactoryI Large number of algorithms

I AndroidOpenSSL ProviderI Native code, performance reasonsI Covers most functionality of Bouncy CastleI Preferred providerI Implementation uses JNI to access OpenSSL’s native code

Android Security Mechanisms, Lecture 9 26/35

Page 27: Android Security Mechanisms

Outline

Android Permissions

Cryptographic Providers

Network Security

Bibliography

Keywords

Android Security Mechanisms, Lecture 9 27/35

Page 28: Android Security Mechanisms

SSL/TLS

I Secure Sockets Layer (SSL) and Transport Layer Security(TLS)

I SSL is the predecesor of TLS

I Secure point-to-point communication protocols

I Authentication, Message confidentiality and integrity forcommunication over TCP/IP

I Combination of symmetric and asymmetric encryption forconfidentiality and integrity

I Public key certificates for authentication

I Java Secure Socket Extension (JSSE)

Android Security Mechanisms, Lecture 9 28/35

Page 29: Android Security Mechanisms

Authentication

I Based on public key cryptography and certificates

I Both ends presents its certificate

I If trusted, they negotiate a shared key for securing thecommunication using pairs of public/private keys

I JSSE delegates trust decisions to TrustManager andauthentication key selection to KeyManager

I Each SSLSocket has access to them through SSLContext

I TrustManager has a set of trusted CA certificates (trustanchors)

Android Security Mechanisms, Lecture 9 29/35

Page 30: Android Security Mechanisms

Obtain Trust Anchors

I Default JSSE TrustManager initialized using the system truststore

I /system/etc/security/cacerts.bks

TrustManagerFactory tmf = TrustManagerFactory. g e t I n s t a n c e ( TrustManagerFactory . g e tDe f a u l tA l g o r i t hm ( ) ) ;

tmf . i n i t ( ( KeyStore ) n u l l ) ;

X509TrustManager xtm = ( X509TrustManager ) tmf. getTrustManagers ( ) [ 0 ] ;

f o r ( X 5 0 9C e r t i f i c a t e c e r t : xtm . g e tA c c e p t e d I s s u e r s ( ) ) {S t r i n g c e r t S t r = ”S : ” + c e r t . getSubjectDN ( ) . getName ( )

+ ”\ n I : ” + c e r t . ge t I s sue rDN ( ) . getName ( ) ;Log . d (TAG, c e r t S t r ) ;

}

Android Security Mechanisms, Lecture 9 30/35

Page 31: Android Security Mechanisms

Use your own Trust Store

I Generate your trust store using Bouncy Castle and openSSL incomand line

I Preferred HTTPS API

KeyStore l o c a l T r u s t S t o r e = KeyStore . g e t I n s t a n c e (”BKS” ) ;InputSt ream i n = ge tRe sou r c e s ( ) . openRawResource (

R . raw . my t r u s t s t o r e ) ;l o c a l T r u s t S t o r e . l o ad ( in , TRUSTSTORE PASSWORD. toCharAr ray ( ) ) ;

TrustManagerFactory tmf = TrustManagerFactory. g e t I n s t a n c e ( TrustManagerFactory . g e tDe f a u l tA l g o r i t hm ( ) ) ;

tmf . i n i t ( t r u s t S t o r e ) ;

SSLContext s s l C t x = SSLContext . g e t I n s t a n c e (”TLS ” ) ;s s l C t x . i n i t ( n u l l , tmf . getTrustManagers ( ) , n u l l ) ;

URL u r l = new URL(” h t t p s : // myse rve r . com ” ) ;HttpsURLConnect ion u r l Conn e c t i o n = ( HttpsURLConnect ion ) u r lu r l Conn e c t i o n . s e tSSLSocke tFac to r y ( s s l C t x . g e tSock e tFac t o r y ( ) ) ;

Android Security Mechanisms, Lecture 9 31/35

Page 32: Android Security Mechanisms

Outline

Android Permissions

Cryptographic Providers

Network Security

Bibliography

Keywords

Android Security Mechanisms, Lecture 9 32/35

Page 33: Android Security Mechanisms

Bibliography

I Android Security Internals, Nikolay Elenkov

I http://nelenkov.blogspot.ro/2011/12/

using-custom-certificate-trust-store-on.html

I https://github.com/nelenkov/custom-cert-https

Android Security Mechanisms, Lecture 9 33/35

Page 34: Android Security Mechanisms

Outline

Android Permissions

Cryptographic Providers

Network Security

Bibliography

Keywords

Android Security Mechanisms, Lecture 9 34/35

Page 35: Android Security Mechanisms

Keywords

I Permissions

I Protection levels

I Static enforcement

I Dynamic enforcement

I Custom permissions

I Java Cryptography Architecture

I Cryptographic Service Provider

I Engine classes

I Java Secure Socket Extension

I Trust Store

Android Security Mechanisms, Lecture 9 35/35