• Tutorial: iPhone App with compiled OpenSSL Library

    by  • February 1, 2010 • Encryption, Featured, iPhone, Mac, Tutorials • 27 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 0.9.8o and Xcode 3.2 with iPhoneOS 3.1.3.
    An updated version of this tutorial for OpenSSL 1.0 and Xcode 3.2.3 with iOS 4 is here:
    http://www.x2on.de/2010/07/13/tutorial-iphone-app-with-compiled-openssl-1-0-0a-library/

    UPDATE 2010-07-13: New tutorial for OpenSSL 1.0.0a and iOS4
    UPDATE 2010-06-30: I have updated OpenSSL to 1.0.0a with iOS 4.0 as base SDK
    UPDATE 2010-06-10: I have updated OpenSSL to 0.9.8o + iPad Version
    UPDATE 2010-03-31: I have updated OpenSSL to 0.9.8n
    UPDATE 2010-02-26: I have updated OpenSSL to 0.9.8m

    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
    • Build OpenSSL for i368 iPhoneSimulator:
      • cd openssl-0.9.8o
        mkdir openssl_arm
        mkdir openssl_i386
        ./config --openssldir=/Users//openssl-0.9.8o/openssl_i386
      • Edit Makefile:
        • Change CC = cc to:
          CC= /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.0
        • Add as first item to CFLAG:
          -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk
      • 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 it:
        make
        make install
      • Your libcrypto.a and libssl.a are in the folder openssl_i368/lib
      • Rename the two files to libcrypto_i386.a and libssl_i386.a
    • Build OpenSSL for arm iPhoneOS:
      • mv openssl_i386 openssl_i386_lib
        make clean
      • Edit Makefile:
        • Search and replace openssl_i386 with openssl_arm
        • Change -arch i386 in CFLAG to:
          -arch armv6
        • Change
          CC=  /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.0

          to

          CC=  /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.0
        • Change
          -isysroot  /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk

          to

          -isysroot  /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.2.sdk
      • Build it:
        make
        make install
      • Your libcrypto.a and libssl.a are in the folder openssl_arm/lib
      • Rename the two files to libcrypto_arm.a and libssl_arm.a

    Edit your iPhone-XCode project:

    • Copy the “include” folder from OpenSSL into your project-folder
    • Copy the libcrypto_*.a and libss_*.a files into your project-folder
    • Drag the libcrypto_*.a and libss_*.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 iPhone OS 3.1.2 XCode Project with OpenSSL 0.9.8o 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

    27 Responses to Tutorial: iPhone App with compiled OpenSSL Library

    1. ken Grant
      March 16, 2010 at 14:26
      • March 18, 2010 at 09:19

        Thank you for your hint. The links are now correct.

        • ken Grant
          March 18, 2010 at 10:07

          You’re welcome. Also ,apologies for not thanking you for taking the time to post this helpful tutorial. Keep up the good work.

          Ken

        • Sai Jithendra
          March 25, 2011 at 07:35

          Hello Flex Schulze,

          I used above correct links posted by KEN GRANT. I am using that openssl in my iphone app. But it is giving me the following error when I tried to genereate keypair using RSA_generate_key_ex(rsa, 1024, e, NULL).

          “Detected an attempt to call a symbol in system libraries that is not present on the iPhone:
          open$UNIX2003 called from function RAND_poll in image MyIphoneApp.
          If you are encountering this problem running a simulator binary within gdb, make sure you ‘set start-with-shell off’ first.
          Program received signal: “SIGABRT”. ”

          Can U help me please!

          Thanks,
          Sai Jithendra.

    2. Steve
      April 27, 2010 at 19:51

      Don’t forget to call make clean after building the i386 version. otherwise compiling the arm version might fail.

    3. Don Williams
      June 10, 2010 at 16:59

      I’m having trouble building this for the iPhoneOS 3.2 SDK. Building for the Simulator works.

      After making the changes for iPhoneOS, when I type “make” I get the following error:

      —– stuff snipped —–
      making all in crypto…
      ( echo “#ifndef MK1MF_BUILD”;
      echo ‘ /* auto-generated by crypto/Makefile for crypto/cversion.c */’;
      echo ‘ #define CFLAGS “/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.0 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.2.sdk -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -arch armv6 -O3 -fomit-frame-pointer -DL_ENDIAN”‘;
      echo ‘ #define PLATFORM “darwin-i386-cc”‘;
      echo ” #define DATE “`LC_ALL=C LC_TIME=C date`”";
      echo ‘#endif’ ) >buildinf.h
      /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.0 -I. -I.. -I../include -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.2.sdk -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -arch armv6 -O3 -fomit-frame-pointer -DL_ENDIAN -c -o cryptlib.o cryptlib.c
      In file included from ../include/openssl/bio.h:67,
      from cryptlib.h:74,
      from cryptlib.c:117:
      /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.2.sdk/usr/include/stdarg.h:4:25: error: stdarg.h: No such file or directory
      make[1]: *** [cryptlib.o] Error 1
      make: *** [build_crypto] Error 1

      Anybody know how to fix this? I searched Google for the cause and all I can find is that the #include_next file directive on line 4 cannot be found for the given architecture. Here is the contents of /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.2.sdk/usr/include/stdarg.h:

      —–
      /* This file is public domain. */
      /* GCC uses its own copy of this header */
      #if defined(__GNUC__)
      #include_next
      #elif defined(__MWERKS__)
      #include “mw_stdarg.h”
      #else
      #error “This header only supports __MWERKS__.”
      #endif
      —–

      I am using Mac OS X Snow Leopard 10.6.3 on an iMac 27″ with Intel Core i7, Xcode 3.2 SDK.

      • June 10, 2010 at 18:05

        I have tried to compile and everything works fine. I also updated the lib on github.

        • June 10, 2010 at 21:20

          Did you use OpenSSL 1.0? This tutorial works only with the 0.9.8… versions.

          • Don Williams
            June 10, 2010 at 22:10

            I was using OpenSSL v0.9.8o. Very frustrating that I cannot get it to build for the device.

            • June 11, 2010 at 07:44

              Did you “make clean” bevore building for arm? (I have added this to the tutorial above.)

            • Don Williams
              June 11, 2010 at 14:09

              Yes. “make clean” was done before building for arm.

            • June 12, 2010 at 10:03

              Please read the tutorial twice and do every step. I tested it and it works well with SnowLeopard 10.6.3 and Xcode 3.2 SK. Also change the Path for your openssldir: –openssldir=/ /openssl-0.9.8o/openssl_i386

        • Don Williams
          June 10, 2010 at 22:25

          Thanks. I downloaded your source from github, but when I build the OpenSSL-1 project using Xcode I get warnings:

          A) When building for the Simulator 3.1.2, I get:
          ld: warning: in /Users/dwilliams/dev/iPhoneApplicationDevelopment/OpenSource/x2on-OpenSSL-for-iPhone-fe3a22b/libcrypto_arm.a, file was built for unsupported file format which is not the architecture being linked (i386)
          ld: warning: in /Users/dwilliams/dev/iPhoneApplicationDevelopment/OpenSource/x2on-OpenSSL-for-iPhone-fe3a22b/libssl_arm.a, file was built for unsupported file format which is not the architecture being linked (i386)

          B) When building for the Device 3.1.2, I get:
          ld: warning: in /Users/dwilliams/dev/iPhoneApplicationDevelopment/OpenSource/x2on-OpenSSL-for-iPhone-fe3a22b/libcrypto_i386.a, file is not of required architecture
          ld: warning: in /Users/dwilliams/dev/iPhoneApplicationDevelopment/OpenSource/x2on-OpenSSL-for-iPhone-fe3a22b/libssl_i386.a, file is not of required architecture

          C) When I change the Base SDK to 3.2, I get an additional warning:
          [BWARN]warning: building with ‘Targeted Device Family’ set to iPhone only (’1′) not supported with SDK ‘Device – iPhone OS 3.2′.

          • June 11, 2010 at 07:46

            These warnings are normal. Every lib has only one architecture. You can ignore these warnings.
            For iPad you musst rename the “*_arm_sdk3.2.a” to “*_arm.a” and the same with the i386 files.

            • Don Williams
              June 11, 2010 at 14:15

              Okay. I renamed the files for iPad as suggested. But I still get the same warnings.

              Unfortunately for me, our build policy has “treat warnings as errors” so I cannot ignore the warnings — even if they are “normal”.

            • June 12, 2010 at 10:05

              Then you must change the build settings for your project to remove the warnings. Or build OpenSSL as an Universal-Libary. But i can’t help you with this.

    4. Don Williams
      June 10, 2010 at 17:07

      NOTE: Comment submission clipped line 4 of /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.2.sdk/usr/include/stdarg.h in my comment above. It should be:
      #include_next <stdarg.h>

    5. June 29, 2010 at 18:28

      I ran into the same issue as Don William even though I followed the instruction. After a bit of online research I found the following post : http://lists.apple.com/archives/Xcode-users/2009/Jan/msg00409.html

      “Which, for those who haven’t looked in /usr/include/stdarg.h, explains the “No such file” error. That file does a “#include_next ” when you’re using GCC, which pulls in the next instance of stdarg.h in the header search path. That “next” instance is in an architecture- specific directory somewhere under /usr/lib/gcc, and it’s *that* one that’s not being found.”

      Not quite sure how to fix this. I am using Xcode 3.2.3 and my error is the same as Dons.

    6. kudor gyozo
      July 12, 2010 at 11:49

      Hello, I get the following errors when building the arm version. Do I have to change all i386 to armv6 or only in CFLAG in the makefile? I’m building openssl 1.0.0a.

      x86cpuid.s:7:bad instruction `pushl %ebp’
      x86cpuid.s:8:bad instruction `pushl %ebx’
      x86cpuid.s:9:bad instruction `pushl %esi’
      x86cpuid.s:10:bad instruction `pushl %edi’
      [...]

    7. Pingback: Tutorial: iPhone App with compiled OpenSSL 1.0.0a Library | x2on.de

    8. AndreasMaier
      September 28, 2010 at 21:40

      Thank you! I thought I would get it never to work.

    9. Shampa
      September 30, 2010 at 11:57

      After Build OpenSSL for “i368 iPhoneSimulator” when i start for “Build OpenSSL for arm iPhoneOS” i must have to run the make clean command but each time this error occurs
      ————————————————————————————-
      rm -f *.map *.so *.so.* *.dll engines/*.so engines/*.dll *.a engines/*.a */lib */*/lib
      rm: openssl_i386_lib/lib: is a directory
      make: *** [libclean] Error 1
      ————————————————————————————–
      my xcode version is 3.1.4 and iPhoneOS 3.1.3 SDK.

      • October 1, 2010 at 09:55

        You must remove the directory lib and create it after the clean command.

        • Shampa
          October 1, 2010 at 11:38

          Thanx a lot .
          Currently i am working in an iphone project where i have to use openssl and libosip2 libraries . kindly if you suggest any tutorial for compiling libosip2 library for both iphone simulator and device .
          by ./configure , make and make install process the archive file (.a files) i get can be used in xcode project and works fine with iphone simulator but it does not work with iphone device . so i am in a big trouble now :(
          i use the libosip2 version “3.3.0″
          here is the link — http://ftp.debian.org/pool/main/libo/libosip2/
          so kindly can you help me out in this problem ?

          • October 1, 2010 at 11:58

            You must compile it for arm too (Simulator is i386, Device is arm).

    Leave a Reply

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