• Tutorial: iPhone App with compiled OpenSSL 1.0.0a Library

    by  • July 13, 2010 • Encryption, iPad, iPhone, Mac, Programming, Projects, Software, Tutorials • 18 Comments

    This is a tutorial for using self-compiled builds of the OpenSSL-library on the iPhone. You can build apps with XCode and the official SDK from Apple with this. I also made a small example-app for using the libraries with XCode and the iPhone/iPhone-Simulator. You can also download the precompiled OpenSSL-library (1.0.0a).

    This tutorial works with OpenSSL 1.0.0a and Xcode 3.2.3 with iOS 4.

    Update: Script for automatic building OpenSSL (currently 1.0.0d) for iOS: http://www.x2on.de/2010/12/16/tutorial-script-for-building-openssl-for-ios-iphoneipad/

    You can find the tutorial for OpenSSL 0.9.8 at http://www.x2on.de/2010/02/01/tutorial-iphone-app-with-compiled-openssl-library/

    You can get the Source of the example app with compiled OpenSSL at http://github.com/x2on/OpenSSL-for-iPhone

    Create OpenSSL Libary:

    Download OpenSSL Sourcecode
    Change in crypto/ui/ui_openssl.c

    static volatile sig_atomic_t intr_signal;

    to

    static volatile int intr_signal;

    for preventing building error

    Build OpenSSL for i368 iPhoneSimulator:

    mkdir ssllibs
    cd openssl-1.0.0a
    mkdir openssl_armv6 openssl_armv7 openssl_i386
    ./configure BSD-generic32 --openssldir=/Users/<username>/openssl-1.0.0a/openssl_i386
    Edit Makefile:

    Change CC = cc to:

    CC= /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc -arch i386

    Add as first item to CFLAG:

    -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk
    Build it:
    make
    make install

    Your libcrypto.a and libssl.a are in the folder openssl_i368/lib

    Build OpenSSL for armv6 iOS:

    mv openssl_i386 ../ssllibs
    make clean
    ./configure BSD-generic32 --openssldir=/Users/<username>/openssl-1.0.0a/openssl_armv6
    Edit Makefile:

    Change CC = cc to:

    CC= /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc -arch armv6

    Add as first item to CFLAG:

    -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.0.sdk
    Build it:
    make
    make install

    Your libcrypto.a and libssl.a are in the folder openssl_armv6/lib

    Build OpenSSL for armv7 iOS:

    mv openssl_armv6 ../ssllibs
    make clean
    ./configure BSD-generic32 --openssldir=/Users/<username>/openssl-1.0.0a/openssl_armv7
    Edit Makefile:

    Change CC = cc to:

    CC= /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc -arch armv7

    Add as first item to CFLAG:

    -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.0.sdk
    Build it:
    make
    make install

    Your libcrypto.a and libssl.a are in the folder openssl_armv7/lib

    Create universal libs:

    cp -r openssl_armv7/include ../ssllibs/
    lipo -create ../ssllibs/openssl_i386/lib/libcrypto.a ../ssllibs/openssl_armv6/lib/libcrypto.a openssl_armv7/lib/libcrypto.a -output ../ssllibs/libcrypto.a
    lipo -create ../ssllibs/openssl_i386/lib/libssl.a ../ssllibs/openssl_armv6/lib/libssl.a openssl_armv7/lib/libssl.a -output ../ssllibs/libssl.a

    Edit your iPhone-XCode project:

    Copy the “include” folder from OpenSSL into your project-folder
    Copy the libcrypto.a and libssl.a files into your project-folder
    Drag the libcrypto.a and libssl.a files into your XCode Framework Folder-Tree
    Open the Build-Info from your “Target” (Righclick on Target – Get Info)
    Change Library Search Paths to

    $(inherited) "$(SRCROOT)"

    Change User Header Search Paths to include
    Activate Always Search User Paths

    Enjoy OpenSSL on the iPhone!

    I have made an iOS 3.2/4.0 XCode Project with OpenSSL 1.0.0a libaries. The examples uses the MD5-algorithm to calculate an md5 hash from an UITextfield.

    Download: http://github.com/x2on/OpenSSL-for-iPhone

    Disclaimer: Use this at your own risk.

    Links:

    Sponsor

    18 Responses to Tutorial: iPhone App with compiled OpenSSL 1.0.0a Library

    1. Pingback: Tutorial: iPhone App with compiled OpenSSL Library | x2on.de

    2. kudor gyozo
      July 14, 2010 at 08:46

      I can’t believe this:
      library not found for -lcrt1.10.6.o
      I don’t know what this is but it is in usr/lib!
      Anyway, thanks for the effort!

      • kudor gyozo
        July 14, 2010 at 09:42

        included usr/lib in path; it builds for i386, now lets see the others..

    3. kudor gyozo
      July 14, 2010 at 10:34

      Thank you for this tutorial, it works!

    4. Kenneth Lewis
      August 8, 2010 at 22:10

      Hi Felix,

      I have included the libcrypto.a and libssl.a as a framework in my project but how are you able to use #include .

      I get Openssl/md5.h: No such file or directory.

      I have Always Search User Paths checked, Library Search Paths set to these framework files, User Header Search Paths set to where the include folder is located.

      Thanks,

      Kenneth

    5. Kenneth Lewis
      August 8, 2010 at 22:11

      That’s, #include openssl/md5.h – arrows left out because they got cut out lol.

    6. Kenneth Lewis
      August 8, 2010 at 22:18

      Another note, I got your sample app and it compiles just fine and has no problem with the #import openssl/md5.h. I have looked at the settings of that project and can’t figure why it works for yours but not mine.

      Thanks,

      Kenneth

    7. Kenneth Lewis
      August 8, 2010 at 22:41

      Also, my Library Search Paths is /Users/username/ssllibs and User Header Search Paths is /Users/username/ssllibs/include and under include is openssl folder with the header files in it.

      Thanks,

      Kenneth

    8. Kenneth Lewis
      August 9, 2010 at 12:07

      I figured what my issue was with the paths thanks Felix for your fine article.

    9. Morphelian
      August 12, 2011 at 13:38

      lipo: ../ssllibs/openssl_armv6/lib/libcrypto.a and openssl_armv7/lib/libcrypto.a have the same architectures (x86_64) and can’t be in the same fat output file

      What does that means? How can it be fixed?

      Thanks

      • vonRichthofen
        January 20, 2012 at 17:12

        Hello Morphelian,

        do you already have a solution for that fat output file problem?

        I have the same.

        Regards
        Michael

    10. September 8, 2011 at 11:40

      Thanks so much!

      I’m running Xcode 4 with IOS5 and after taking all these steps I finally got it to work.

    11. September 11, 2011 at 23:52

      This tutorial worked me me. Thanks!

      Morphellian — it sounds the armv6 and armv7 librcrypto.a libraries you generated were most likely generated for the same architecture (check your makefile).

    12. Shicai Hu
      November 7, 2011 at 03:44

      In iOS, private key (or public key) is in the form of SecKeyRef. I can get private key as follows:

      SecIdentityCopyPrivateKey(identityRef, &privateKeyRef);

      where identityRef, privateKeyRef are as from

      SecIdentityref identity;
      SecKeyRef privateKeyRef;

      What can I do to get EVP_PKey format private key for OpenSSL from iOS’s SevKeyRef key?

      Thanks

    13. Chris
      January 8, 2012 at 18:20

      In step “BUILD OPENSSL FOR I368 IPHONESIMULATOR” I receive the following error when I do “make install”:
      “…
      created directory `App/openssl-1.0.0f/openssl_i386/man/man7′
      /bin/sh: -c: line 0: syntax error near unexpected token `Test’
      /bin/sh: -c: line 0: `pod2man=”`cd ./util; ./pod2mantest /usr/bin/perl`”; here=”`pwd`”; filecase=; if [ “BSD-generic32″ = “DJGPP” -o “BSD-generic32″ = “Cygwin”
      …”

      I’ve changed
      “-isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk”
      to
      “-isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk”
      due I need it for iOS5.

      Any suggestions?

    14. vonRichthofen
      January 20, 2012 at 16:11

      Hello and thanx for that tutorial.

      What means:

      Add as first item to CFLAG:

      -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.0.sdk

      in terms of real doing?

      Thank you
      Red Baron

    15. sanjay255310@gmail.com
      February 7, 2012 at 13:32

      Hi
      I’m getting this error:

      Directory not found for option ‘-Llib’
      ld: library not found for -lssl

      I’m using xcode 4.2 and sdk 5.0. Please help me to solve this problem.

      Thanks
      sanjay

      • February 8, 2012 at 18:28

        Just have a look at the github project and use that script.

    Leave a Reply

    Your email address will not be published. Required fields are marked *