Building OpenSSL on Windows is not THAT hard

Building OpenSSL on Windows is not THAT hard

Alright alright, before you all start bashing me left and right for the title, let me clarify something: building OpenSSL from source code on Windows may not be straightforward for first-timers, but once you get the hang of it, it's actually quite simple, in my humble opinion.

Now, you might be thinking, why, Saurabh, why do you want to build openssl from source code and that too on Windows machine when you can build it easily in any Unix machine? And you are right, if I could do that, I might not be writing this article. Actually, I proposed doing that but due to few things which I can’t tell you because it is work related and I am not sure if I should write those things or not in this article.

Let just say that I was required to build openSSL on Windows for a non-windows target and the script needed to be run via command prompt.

So here’s what I did and why I did.

Steps:

  1. Install Chocolatey: Windows Package Manager.
    Why?
    Having a package manager is nice and makes thing easy to automate.

  2. Install NASM using chocolatey:
    Why?
    NASM or Netwide Assembler is an assembler and disassembler for x86 chips.

     choco install nasm -y
    
  3. Install Cygwin and using that install Perl.
    Why?

    To install Perl because of cross-compilation which Strawberry Perl didn’t offered. But I eventually needed it to build Pod::Usage as well.

    I installed Cygwin using chocolatey but you can do that using setup-x86_64.exe and then I installed Perl using setup-x86_64.exe as Perl doesn’t get come with Cygwin by default.

  4. Manually download Pod::Usage module and build it using Cygwin terminal.**
    Why?**
    When you run the Configure script in OpenSSL, it may invoke Perl scripts that use Pod::Usage to display helpful information or error messages if the configuration is incorrect.

    [RANT] This was one of the most time consuming and blocking part for me as Pod::Usage didn’t come with the Perl Installation.

    I was not able install it via cpan (Comprehensive Perl Archive Network) as well and I tried couple of more things but they didn’t work either and since, it was crucial for generating the Makefile, thus, I took this route but general suggestion is to use cpan.

    I am not proud of it but yeah I will take another look, maybe I missed something, will update if anything new comes up.

    The process will looks something like the following

     wget https://cpan.metacpan.org/authors/id/M/MA/MAREKR/Pod-Usage-x.xx.tar.gz
     tar -xzf Pod-Usage-x.xx.tar.gz
     cd Pod-Usage-x.xx
     perl Makefile.PL
     make
     make test
     make install
    
  5. Install Visual Studio 2022 Build Tools.
    Why?

    Provide essential tools to build OpenSSL.

    Install VCTools workload (C++ Desktop Development) and install all the recommended workload as well, refer this.

  6. Find vcvarsall.bat file and execute it with x86 as a param.
    Why?
    It sets up the build environment and provide nmake tool which we will be using to build openSSL.

    You will find it inside Visual Studio Build tool folder. I am not going to spoon feed everything.

     "<path to vcvarsall.bat\vcvarsall.bat" x86
    
  7. Download OpenSSL zip file from the internet source and extract the content.

  8. Use Perl to generate the Makefile.

     # go inside openssl folder to find the Configure file and execute the following command there
     perl Configure [Configuration flags]
    
  9. Use nmake to build the static libraries.

That’s it.


An Interesting Problem that I encountered

So far, so good, eh, maybe not, I mean for me, it wasn’t because I didn’t know that,

“Command Prompt have characters limits for a single command i.e., 8191 characters.”

Apparently, for me, the generated Makefile has one line of instruction related to the archival step of buildinglibcryto.a which was failing due to characters limit.

I was suggested by a co-worker to increase the character limitation of command prompt but by then I read this article and I thought that is not possible so I didn’t explore that route and did something else.