Home > AI > IOS > StoreKit >

integrate OpenSSL to Swift

Download reliable source

Step 1: download the source from official website

Be sure to only download the library from a trusted source (i.e. https://www.openssl.org/source/) and check the hash of your download.

Step 2: create the folder structure

OpenSSL
--.gitkeep (keep this directory)
--x86_64
--arm

Below the script will check if the download is intact.

#!/bin/bash
# Jonathan Cardasis - 2018
# Builds OpenSSL libssl and libcrypto for macOS
# binary distribution (i386 and x86).

VERSION="1.1.0g"
VERSION_SHA256_CHECKSUM="de4d501267da39310905cb6dc8c6121f7a2cad45a7707f76df828fe1b85073af"

####################################
curl -O https://www.openssl.org/source/openssl-$VERSION.tar.gz

# Run a checksum to ensure this file wasn't tampered with
FILE_CHECKSUM=$(shasum -a 256 openssl-$VERSION.tar.gz | awk '{print $1; exit}')
if [ "$FILE_CHECKSUM" != "$VERSION_SHA256_CHECKSUM" ]; then
  echo "OpenSSL v$VERSION failed checksum. Please ensure that you are on a trusted network."
  exit 1
fi

Compile to static libray by yourself

Step 3-1: Compile (x86)

i386iPhone simulator, 32 and 64bit compatible.
x86_64iPad simulator, 32 and 64bit compatible.
arm7The oldest iOS 7-supporting devices
arm7siPhone 5 and 5C
arm64For the 64-bit ARM processor in iPhone 5S
$cd openssl-{version}
$./Configure iphoneos-cross --prefix=/Users/dph/Documents... -no-asm 

--prefix is where files are put in the make
-no-asm exclude i386

Then change the CFLAGS in the Makefile

CFLAGS=-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.4.sdk -Wall -O3

You need to check your own iPhoneSimulator version

Then, you can successfully make and make install.

Then, you can check the generated files.

folderusage
includeheader files needed when calling
libIt contains dynamic and static libraries (different openssl will produce different libraries, some may only have static libraries)
You can use this command lipo -info libssl.a to check its architecture
share
bin
ssl

Step 3-2: Compile (arm)

At first, you need to delete the original openssl-{version} and re-extract the `tar.gz`

$./Configure iphoneos-cross --prefix=/Users/dph/Documents/ios/0-PTE-V1/OpenSSL/arm

This changes prefix and doesn't has -no-asm

Then, modify the Makefile

CC= /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch arm64

(different, add -arch arm64)


CFLAGS=-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk -Wall -O3

(use iPhoneOS sdk)

Then, you can use make and make install.

Then, you can use the lipo -info libcrypto.a to check its architecture.

Step 4: Merge two static libraris into one

At first, you can put the two static files in the same directory for convenient operation.

$ lipo -create libssl_arm.a libssl_x86.a -output libssl.a
$ lipo -info libssl.a                                    

$lipo -create libcrypto_x86.a libcrypto_arm.a -output libcrypto.a
$ lipo -info libcrypto.a

Step 5: Reference the files

The final generated library is two, libcrypto.a, libssl.a. and include .

Integrate the result files to Xcode

At this stage, you need these files from OpensSSL

OpenSSL
--lib
----libssl.a
----libcrypto.a
--include

Step 2: expose the header, you have two choise

Step2-1 Module map

You need create shim.c to expose OpenSSL headers and module.modulemap to convert to a Swift module.

shim.c

#ifndef shim_h
#define shim_h

#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/md4.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/rand.h>
#include <openssl/ripemd.h>
#include <openssl/pkcs12.h>
#include <openssl/x509v3.h>

#endif /* shim_h */

module.modulemap

/// Expose OpenSSL for extraneous Swift usage
module OpenSSL {
    header "shim.h"
}

Finally, your file structure should look like this.

Step 3: configure Xcode

Building Settings -> Library Search Path -> $(SRCROOT)/OpenSSL/include (recursive)

Building Settings -> Header Search Path -> $(SRCROOT)/OpenSSL/lib (recursive)

Building Setttings -> Import Path -> $(SRCROOT)/$(TARGET_NAME)

swift package init –type system-module

Step 4: test

ContentView.swift

import SwiftUI
import OpenSSL
import Security


struct ContentView: View {
    init() {
        print(OPENSSL_INIT_ADD_ALL_DIGESTS)
    }
    
    var body: some View {
        Text("Hello, world!")
            .padding()
    }
}

Step 2-2: Objective-C Bridging Head

create the Bridging-Header.h (you can create a Objective-C, then Xcode will ask if to create the Bridging-Header.h

#import <openssl/pkcs7.h>
#import <openssl/objects.h>
#import <openssl/evp.h>
#import <openssl/ssl.h>
//#import <openssl/asn1_locl.h> (in this case, this cannot be found)

Building Settings -> Objective-C Bridging Header ->

Trouble Shooting

  1. Building for iOS but the linked library was built for iOS Simulator

You accidentally use iPhoneSimulator.sdk as the CFLAGS in arm, you should use iPhoneOS.sdk

2. doesn’t contain a bitcode

Build Settings > Enable Bitcode > No

3. Errors were encounterd while preparing your device for development.

reboot your iPhone.

Leave a Reply