I have some security key in an application. I want to store it securly. I like to store it in a native shared library (maybe generated from some code). After that I want it to be returned by a method that will check the signature of the original APK. So no one can use this file except trusted applications. I know, that ndk library could be also decompiled, but this is even harder to make reverse engineering of native code then java .class files.
Question:
- Is there a way to calk the signature of the origin apk from the native code (c/c++)?
- How can I make sure that the library is called from the trusted application?
TL;DR An example can be found here.
I get a signature on native layer(C-code) another way:
'META-INF/CERT.RSA'
from the APK;'META-INF/CERT.RSA'
The code for getting the APK path:
We can use zlib since 3 API in Android and I use minizip for convinience.
The code to extract of the
META-INF/CERT.RSA
below:For parsing
META-INF/CERT.RSA
I use parts of the code from one public repository. It's too big to be posted on StackOverflow, so the full source code of the working example can be found here.upd:
Here is an example how we can get MD5 from a signature(using mbed TLS):
I will try to answer your first question here:
Signature of your application is stored in the DEX(Dalvik executable) file of your APK. DEX files have following structure:
So, this is the beginning of the header of DEX file:
So, to calk a signature of your apk, you should compute SHA-1 signature of your DEX file starting from the offset 32.
To get access to DEX file of your apk from native code, you can read process memory, which is stored in /proc/self/maps:
Each row in proc/$ID/maps file has following structure:
Here you can find a better description of proc/$ID/maps file's structure: Understanding Linux /proc/id/maps
To detect location of DEX file in process memory you should check out 'pathname' column in every row of your proc/self/maps file. When the row corresponding to DEX file will be found, you should get starting and ending addresses of the DEX file region:
So, when you will have starting and ending addresses of your apk's bytecode, you will be able to compute signature of your apk.