Coding OpenSSL on a Mac
This post is more about a note to myself about using OpenSSL in Visual Studio Code (VS Code). If the post benefits you, then I'm glad that it did. I performed all the steps described here on an Apple MacBook running macOS Big Sur 11.5.1.
Before proceeding, we need to have these two programs installed:
- Homebrew: to install OpenSSL
- Visual Studio Code: the integrated development environment (IDE) I use to code OpenSSL. Of course, you can use any other IDEs that you prefer.
OpenSSL
Installing OpenSSL
Use Homebrew to install OpenSSL:
brew install openssl@1.1
On my Mac, Homebrew installs OpenSSL at
/opt/homebrew/Cellar/openssl@1.1/1.1.1k.
At the time of writing, the latest stable version of OpenSSL is 1.1.1k.
If we open a terminal and type:
openssl version
the output will display LibreSSL 2.8.3 which is not the version of OpenSSL that we have just installed. This is the version of OpenSSL that MacOS uses. We can also check using the which command to see the binary's path:
which openssl
which points to /usr/bin/local.
To display the OpenSSL version that we have just installed, enter the following:
/opt/homebrew/Cellar/openssl@1.1/1.1.1k/bin/openssl version
The above command will output OpenSSL 1.1.1k 25 Mar 2021.
Note: I read somewhere that it is not recommended to replace MacOS' OpenSSL with the one that we want. Let it live in peace.
Setting the Symbolic Link for the Header Files
Before we can start coding with OpenSSL, we need to tell the system where to find the various OpenSSL header files. We can do this by creating a symbolic link at /usr/local/include which points to /opt/homebrew/Cellar/openssl\@1.1/1.1.1k/include/openssl/. We do this using the ln command:
ln -s /opt/homebrew/Cellar/openssl\@1.1/1.1.1k/include/openssl/ /usr/local/include/
To know whether or not we are successful in adding the symbolic link, use ls -l:
ls -l /usr/local/include/
We should obtain the following output for openssl:
lrwxr-xr-x ... openssl -> /opt/homebrew/Cellar/openssl@1.1/1.1.1k/include/openssl
where the first l here means that openssl is a symbolic link and the actual link is printed after the -> arrow. The ... means that there are other texts there but I have hid them.Setting the Symbolic Link for the OpenSSL Library
We also need to tell the system where to find the C library for performing the cryptographic operations in OpenSSL. We can do this by creating a symbolic link at /usr/local/lib which points to /opt/homebrew/Cellar/openssl\@1.1/1.1.1k/lib/libcrypto.a. Note that here, we are using OpenSSL's static library. Similarly as we did for the OpenSSL headers, use the ln command:
ln -s /opt/homebrew/Cellar/openssl\@1.1/1.1.1k/lib/libcrypto.a /usr/local/lib/
Check whether or not we are successful in adding the symbolic link:
ls -l /usr/local/lib/
We should obtain the following output for openssl:
lrwxr-xr-x ... libcrypto.a -> /opt/homebrew/Cellar/openssl@1.1/1.1.1k/lib/libcrypto.a
VS Code
Once we have setup the necessary symbolic links, we are now ready to write some codes that use the OpenSSL cryptographic library. Since we have created a symbolic link to the OpenSSL headers at /usr/local/include/, we should be able to add the necessary OpenSSL header files. In fact, VS Code should be able to detect the presence of those header files. Some examples of OpenSSL header files are:
#include <openssl/rand.h>
#include <openssl/aes.h>
Since we have added a symbolic link that points to OpenSSL's static library, we can compile the C file that contains OpenSSL functions as follows:
gcc -o openssl-test openssl-test.c -lcrypto -Wall
where openssl-test.c is the name of the C file and -lcrypto refers to the libcrypto.a static library.
Simple OpenSSL Code
#include <string.h>
Comments
Post a Comment