OpenSSL
Recently I had to add In-App Purchases to an app I am developing as a personal hobby. Dealing with IAP alone can be a pain if you don’t know much about cryptography, but luckily there are many libraries that make this process simpler.
But here’s a small catch: Almost all of the IAP libraries (and many other libraries you will want to use) require OpenSSL. Particularly libraries that deal with cryptography may need it. With the release of the iPhone 5S and it’s 64-Bit processor, it is a little bit tricky to compile it and get it to work.
At first I found a pre-compiled version of OpenSSL and added it to my project. The problem was that I was only testing with an iPod Touch 5, and as such I didn’t see many potential problems with the different processor architecture of the 5S. I used that pre-compiled OpenSSL library for a while until I tested it on my iPhone 5S and oh boy. Whenever I tried to compile for ARM64, it complained that all the cryptography-related symbols were not found.
Luckily, compiling OpenSSL to work with both ARM and ARM64 is really easy and fast.
Step by step:
- Download this shell script.
- Create a new directory and cd to it. It’s easier to see all the generated files this way (for the sake of simplicity, I created a “pls” directory in my Documents directory and cd to it).
- Run it on terminal:
/Users/andyibanez/Downloads/gist5154962-d5874b176ff9793036d53aae0e12dbc5b708963a/openssl-build.sh
This will do most of the hard work for you: It will download OpenSSL, compile it, and create OpenSSL libraries for both ARM, ARM64, and even for Mac!
Please note that executing this script may take a few minutes. Not only does it have to download OpenSSL, but it also has to compile it and it is a middle-sized library. It shouldn’t take longer than 5 minutes in an i5 processor, but still patience is to be expected.The output should be something like this:
Cleaning up Downloading openssl-1.0.1e.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 4355k 100 4355k 0 0 8618 0 0:08:37 0:08:37 --:--:-- 5299 Unpacking openssl Building openssl-1.0.1e for i386 Building openssl-1.0.1e for x86_64 Copying headers Building Mac libraries Building openssl-1.0.1e for iPhoneOS 7.0 armv7 Building openssl-1.0.1e for iPhoneOS 7.0 armv7s Building openssl-1.0.1e for iPhoneOS 7.0 arm64 Building openssl-1.0.1e for iPhoneSimulator 7.0 x86_64 Building openssl-1.0.1e for iPhoneSimulator 7.0 i386 Building iOS libraries Adding 64-bit libraries Cleaning up Done
- In the directory you created on step 2, you should now have two directories and one file: “include”, “lib”, and “openssl-VERSION_NUMBER.tar.gz”. Y0u can ignore the latter.
- Open “lib”. It should have a total of 4 files. This is intuitive but I will state it for the sake of completeness: For iOS, you’re only interested in “libcrypto_iOS.a” and “libssl_iOS.a”.
- In your XCode project, open its settings and go to the “Build Phases” tab. Extend “Link Binary with Libraries” and drag and drop both .a files from the step above there.
- Now go to the “Build Settings” tab, look for “Search Paths”, and then “Header Search Paths”. Double click it until you see a menu like this:
- Like you can see in the screenshot, I just dragged the entire “include” directory there. This directory has an openssl directory, but you don’t need to concern yourself with it.
And that’s it! You should now be able to use OpenSSL with both ARM and ARM64 applications.
Only Mac libraries are coming when I execute /Downloads/gist10657400-135f496ea3502103b525e55134f4d957f3bbf737/openssl-build.sh in ”Lib” folder
I actually had the same problem when running this script for the second time three days ago.
I solved it by changing the Xcode path in the Terminal.
sudo xcode-select -switch
you changed the Xcode path from where / to where? i have the same issue, i dont get the IOS libs
When I do “xcode-select –print-path”, it gives me the correct path which is “/Applications/Xcode.app/Contents/Developer”. But still I am not able to build. The script just stops after building the mac libraries. Is there any updates to the script?
Andy – Thanks for the great post! I had to do two things to get this to work for Xcode6 / iOS8.1:
#1. Updated the script that you reference (openssl-build.sh) to use the 8.1 SDK instead of 7.1. (To do this I replaced all occurrences of ‘7.1’ with ‘8.1’ in the script itself).
#2. Run the script from a path that did not have any spaces in it using: sh openssl-build.sh
Hi Derek. I am also trying to compile it for iOS 8/Xcode-6. As suggested by you, I replaced all occurrences of 7.1 with 8.1. But I am not able to build it. Tried a lot. Could you please help. Is it possible for you to share the libraries which you built for iOS 64 bit? I know that’s a weird question though.
It stops at:
fbartolom$ ./build3-openssl.sh
Cleaning up
Downloading openssl-1.0.1h.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 4370k 100 4370k 0 0 745k 0 0:00:05 0:00:05 –:–:– 830k
Unpacking openssl
Building openssl-1.0.1h for i386
and creates nothing in the lib directory. I have OS X Maverick.
The logs report:
make: *** No rule to make target `install’. Stop.
The error in the logs is at:
installing man7/des_modes.7
installing man3/dh.3
installing man3/dsa.3
installing man3/ecdsa.3
installing man3/engine.3
installing man3/err.3
installing man3/evp.3
installing man3/hmac.3
/bin/sh: /tmp/openssl-1.0.1h-i386/man/man3/hmac.3: Too many levels of symbolic links
make: *** [install_docs] Error 1
It went better after I cleared the temp files; now I get:
openssl-ios fbartolom$ build3-openssl.sh
Cleaning up
Downloading openssl-1.0.1h.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 4370k 100 4370k 0 0 802k 0 0:00:05 0:00:05 –:–:– 852k
Unpacking openssl
Building openssl-1.0.1h for i386
Building openssl-1.0.1h for x86_64
Copying headers
Building Mac libraries
Building openssl-1.0.1h for iPhoneOS 7.1 armv7
but strangely the include directory only includes:
-rw-r–r– 1 fbartolom staff 6028448 27 Dic 13:33 libcrypto_Mac.a
-rw-r–r– 1 fbartolom staff 1072504 27 Dic 13:33 libssl_Mac.a
The problem is that the script is looking for:
clang: warning: no such sysroot directory: ‘/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk’
While my directory contains iPhoneOS8.1.sdk’. Passing 8.1 as a -z parameter does not work. What to do?
My present problem is importing: openssl/bio.h; Xcode does not seem to find it, even after having included the libraries, and I need it to process the new inApp receipts. Any hint?