I am trying to make the most basic x509 public key cert file possible on Android in C via the NDK. I built libcrypto and libssl (although I am currently only using libcrypto stuff) with the stuff from https://github.com/guardianproject/openssl-android.
Since I was having some problems getting this to work I ended up getting it working in linux where I have a more pleasant debugging setup. The code I have which is working on my linux box is as follows:
void x509(RSA *rsa_keys) {
EVP_PKEY *evp = EVP_PKEY_new();
X509 *x509 = X509_new();
EVP_PKEY_assign_RSA(evp, rsa_keys);
X509_gmtime_adj(X509_get_notBefore(x509),0);
X509_gmtime_adj(X509_get_notAfter(x509), (long) 60*60*24*365);
X509_set_pubkey(x509, evp);
unsigned char *out = NULL;
int length = i2d_X509(x509, &out);
FILE *x509_file = fopen("cert.cer", "w");
fwrite(out, 1, length, x509_file);
}
This code runs fine and writes out a file which is parsed fine by openssl with
openssl x509 -inform DER -text -in cert.cer
When I run the same code in the NDK on Android I get as far as the X509_set_pubkey(x509, evp) call (determined by printout debugging) without any openssl errors reported by ERR_get_error() and then get a signal 11 (SIGSEGV), code 1 (SEGV_MAPPER) with a big stack trace
the only thing that seems very useful from the stack trace is that the crash happened deep in libcrypto.so
08-17 01:40:41.261: INFO/DEBUG(10354): #00 pc 00041d1a /system/lib/libcrypto.so
08-17 01:40:41.261: INFO/DEBUG(10354): #01 pc 000357c6 /system/lib/libcrypto.so
08-17 01:40:41.261: INFO/DEBUG(10354): #02 pc 000337ba /system/lib/libcrypto.so
08-17 01:40:41.261: INFO/DEBUG(10354): #03 pc 000338c8 /system/lib/libcrypto.so
08-17 01:40:41.268: INFO/DEBUG(10354): #04 pc 000339e6 /system/lib/libcrypto.so
08-17 01:40:41.268: INFO/DEBUG(10354): #05 pc 00033f00 /system/lib/libcrypto.so
08-17 01:40:41.268: INFO/DEBUG(10354): #06 pc 00033b20 开发者_如何学编程/system/lib/libcrypto.so
08-17 01:40:41.268: INFO/DEBUG(10354): #07 pc 00033f44 /system/lib/libcrypto.so
08-17 01:40:41.268: INFO/DEBUG(10354): #08 pc 00033f9c /system/lib/libcrypto.so
08-17 01:40:41.268: INFO/DEBUG(10354): #09 pc 000770e6 /system/lib/libcrypto.so
08-17 01:40:41.268: INFO/DEBUG(10354): #10 pc 00076fec /system/lib/libcrypto.so
08-17 01:40:41.268: INFO/DEBUG(10354): #11 pc 00036b68 /system/lib/libcrypto.so
08-17 01:40:41.268: INFO/DEBUG(10354): #12 pc 0007ed1a /system/lib/libcrypto.so
The only thing I can think of is that there is a problem with the Openssl I built but that seems somewhat unlikely, any thoughts?
Thanks
It could be you need to call OpenSSL_add_all_algorithms() before using the RSA functions.
精彩评论