Let iOS support OpenSSL

Source: Internet
Author: User
Tags openssl library

Today, the boss sent me a project, found the lack of LIBSSL.A and libcrypto.a files, so the Internet search, presumably this is understood.

This is like a code base, to let him support iOS need to hit a package, hit the. a file.

This stupid method does not work at all--I use the method of foreign friends ~ ~ Thanks to the author of the original

Http://iosco.de/use-openssl-on-ios.html

IOS generates a generic OpenSSL library/ Post a reply

The server PHP side defines the interface and needs to pass parameters that use RSA encryption, for which the iOS side needs to introduce the OpenSSL library for RSA encryption and decryption. This article describes how to build a virtual machine, a generic OpenSSL library for a real machine, during iOS development.

Stupid method to generate generic OpenSSL library

What is generic: Enables both the real machine and the simulator to use the built-in OpenSSL library

First, download OpenSSL

Download the latest OpenSSL source code via http://www.openssl.org/source/

Second, compile OpenSSL

Enter the OpenSSL directory and create the appropriate subfolders to hold the compiled libraries.

Prepare for Operation
      1. Create a Directory

12 # used to store a static resource file package with OpenSSL compiled under different schemas mkdir openssl_arm64 openssl_armv7 openssl_armv7s openssl_i386

      1. Text substitution

Enter the OpenSSL directory, open the Crypto/ui/ui_openssl.c file, search for static volatile sig_atomic_t intr_signal; Replace with static volatile int intr_signal

To compile the OpenSSL library used by the emulator
      1. Configuring OpenSSL information

12 The # openssldir option is the path to which it was compiled and is set to the openssl_i386 you just created . /Configure BSD-generic32 --openssldir=/Users/xxx/ OpenSSL-1.0.2/openssl_i386

      1. Update makefile

12345 # Open the makefile file, search for CC = gcc, replace it with the followingCC= /applications/Xcode. App/Contents/Developer/usr/bin/gcc -Arch i386 # on thenext line of CC =, the content is cflag= Config item, after the space after its equal sign "add" the following (the original Cflag after the content is not deleted, remain unchanged) Cflag= -isysroot /applications/xcode.< Span class= "crayon-i" >app/contents/developer/platforms/< Span class= "crayon-i" >iphonesimulator. Platform/developer/ sdks/iphonesimulator sdk&NBSP;&NBSP;

      1. Compile

Exit the compilation of the makefile file, save the makefile file.

Execute the following command at the terminal:

123 Make make install

      1. Backup files

When the make install finishes executing, check to see if the Openssl_i386 folder has generated relevant content. If it has been generated, the openssl_i386 directory can be copied as a whole for backup.

The OpenSSL library used by the real machine

The OpenSSL library used by the real machine, as in the previous steps of the build simulator using the library, is fine-tuned as follows:

      1. Configuring OpenSSL information

Each time you generate a OpenSSL library in a different environment, you need to reconfigure the Configure. The following commands are implemented in the terminal respectively:

12345 # first empty the last compilation configuration make  # replace storage directory . /configure bsd- Generic32 --openssldir =/users/xxx/openssl- 1.0.2/openssl_xxxx

      1. Update makefile File

123456 # Open the makefile file, search CC= gcc, replace it with the following Arch is supported by the Real machine architecture:arm64 arm7v armv7s CC= /applications/Xcode. App/Contents/Developer/usr/bin/gcc -Arch armv7s # cc= next line, content cflag= config item, after the space after its equals sign "join" The following content (original cflag after the content is not deleted, remains unchanged) # configuration item, point to True machine sdk, not emulator sdk /Applications< Span class= "Crayon-o" >/xcode. App/contents/ Developer/platforms/ iphoneos. Platform/developer/ sdks/iphoneos.< Span class= "Crayon-i" >sdk Cflag= -isysroot /applications/Xcodeapp/contents/developer/platforms/< Span class= "crayon-i" >iphoneos. Platform/developer/ sdks/iphoneos.< Span class= "crayon-i" >sdk  
Iii. merging different architecture OpenSSL libraries

Now that you have built the OpenSSL library required for the emulator and the real-world environment, you need to merge it. Go to the directory where the files are backed up, and then merge them by executing the following commands:

12345678 # Merging FilesLipo -CreateOpenssl_arm64/Lib/Libcrypto. A openssl_armv7/lib< Span class= "Crayon-o" >/libcrypto.a openssl_armv7s/lib/libcrypto.a openssl_i386< Span class= "Crayon-o" >/lib/libcrypto.a -output . /libcrypto.a Lipo -CreateOpenssl_arm64/Lib/Libssl. A openssl_armv7/lib/Libssl. A openssl_armv7s/lib/Libssl . A openssl_i386/lib/Libssl. A -output . /Libssl. A # View schema information supported by the merged libraryLipo -info libcrypto. A Lipo -info libssl. A

Quick Build Generic OpenSSL library

From the above operation process, we can see that the real machine environment, each compilation of the same process, nothing but replace the-arch behind. Considering the emulator environment, you can quickly generate the environment that needs to be compiled by scripting.

On GitHub, there are already foreign friends to help us do the corresponding script, we can be happy and quiet waiting for the program to automatically generate the OpenSSL library for the real machine and simulator. For details, see: Https://github.com/x2on/OpenSSL-for-iPhone

12 # It's so quiet ~ . /Build-libssl. Sh

Error description

After the compilation process (make execution), there may be the following error message, can be ignored!

Clang:warning:argument unused during compilation: '-pthread '
Making all in Tools ...
Make[1]: Nothing to is done for ' all '.

Let iOS support OpenSSL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.