user1264176
user1264176

Reputation: 1128

libwebsockets lib with openssl for iOS crashes in SSL_CTX_new()

I am trying to use libwebsockets (http://libwebsockets.org/trac/libwebsockets) library for websockets support in my app. I need it to be secure so I am trying to build it with openssl lib which I've built using https://github.com/x2on/OpenSSL-for-iPhone/blob/master/build-libssl.sh script. CMake creates libwebsockets project which I slightly modify to use static openssl library. Then I add this project to my workspace and build my app. App is built without problems. When I launch it and try to establish websocket connection I get crash in SHA1_Final function while I try to create new context using SSL_CTX_new().

Crash backtrace

* thread #1: tid = 0x2503, 0x000fa47e MyApp`SHA1_Final + 22, queue = 'com.apple.main-thread, stop reason = EXC_BAD_ACCESS (code=1, address=0x22986341)
frame #0: 0x000fa47e MyApp`SHA1_Final + 22
frame #1: 0x00130a90 MyApp`EVP_DigestFinal_ex + 56
frame #2: 0x0012e45c MyApp`ssleay_rand_add + 492
frame #3: 0x0012f056 MyApp`RAND_poll + 614
frame #4: 0x0012e726 MyApp`ssleay_rand_bytes + 166
frame #5: 0x000ef49c MyApp`SSL_CTX_new + 464
frame #6: 0x00153218 MyApp`libwebsocket_create_context(info=0x2fda90bc) + 1364 at libwebsockets.c:1975

I also have nss library (https://developer.mozilla.org/en-US/docs/NSS) and a bunch of other security related files which is needed for other libraries in my project.

I have tried to create simple test project with one view and only libwebsockets and openssl library in it and in that project openssl doesn't crash.

I guess that the problem I have is related either to existence of nss and openssl libraries in the same project or to possible different compile settings for openssl and other projects. (And yes, I've checked, libwebsockets lib calls SSL_library_init()).

I would greatly appreciate any thoughts why this happens and how to fix it.

Upvotes: 1

Views: 2012

Answers (2)

Siddhesh Mahadeshwar
Siddhesh Mahadeshwar

Reputation: 1611

I got a reason behind that, -objC from other linker flag allow to import all the objective c categories from static library and here it conflicts with openssl. if you remove that flag then it will take openssl for linking.

Upvotes: 0

user1264176
user1264176

Reputation: 1128

OK. I have nailed down the problem.

NSS library has SHA1 functions which has the same names that openssl has. I don't completely understand why linker didn't complain about duplicate symbols but the problem was that openssl was using one of SHA1 functions from NSS and all the others from openssl. By doing that ssl context was being corrupted by wrong function from NSS and then crashing in openssl function.

I would appreciate some thoughts why linker let it all compile but moving all the app to use only openssl solved my problem.

Upvotes: 2

Related Questions