Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build OpenSSL on Windows with Visual Studio 2017?

Tags:

I am trying to use OpenSSL but I am stuck on the step of compiling. The OpenSSL project has very unfriendly (bad) documentation.

Is there any actual help how to build the latest OpenSSL version on Windows with Visual Studio 2017?

I didn't find any helpful information on the official OpenSSL site. Yes, there are a lot of posts on the Internet about OpenSSL compilation, but all of them are obsolete.

like image 288
AeroSun Avatar asked Aug 03 '17 21:08

AeroSun


People also ask

Is there an OpenSSL for Windows?

In some situations, it can be useful to generate a CSR using OpenSSL. This manual describes the installation of OpenSSL under Windows. Download the OpenSSL for Windows installation package.


2 Answers

I've not used VS2017 but previous versions. I imagine it is much the same. Note the instructions below are for OpenSSL 1.1.0 or above. They do not work for OpenSSL 1.0.2. In brief the steps are:

  1. Install Perl (either ActiveState or Strawberry)

[EDIT, see my (kritzel_sw) comment below: I would strongly recommend to use Strawberry)]

  1. Install NASM

  2. Make sure both Perl and NASM are on your %PATH%

  3. Fire up a Visual Studio Developer Command Prompt with administrative privileges (make sure you use the 32-bit one if you are building 32-bit OpenSSL, or the 64-bit one if you are building 64-bit OpenSSL)

  4. From the root of the OpenSSL source directory enter perl Configure VC-WIN32, if you want 32-bit OpenSSL or perl Configure VC-WIN64A if you want 64-bit OpenSSL

  5. Enter nmake

  6. Enter nmake test

  7. Enter nmake install

[EDIT, unless you change the target directory in the configuration, nmake install needs administrator privileges. So the VC command prompt must be started as administrator for this final step]

If anything goes wrong at any stage, check the INSTALL file and the NOTES.WIN file.

like image 93
Matt Caswell Avatar answered Sep 28 '22 15:09

Matt Caswell


Modified version of The Quantum Physicist python script

It can compile OpenSSL 1.0.x or OpenSSL 1.1.x

It can compile with multiple version of Visual Studio 2017/2019 included.

1) Create the file: CompileOpenSSL.py

import os import os.path from subprocess import call import shutil import sys import re import argparse  # args parser = argparse.ArgumentParser() parser.add_argument("-f", "--filename", help="First argument must be the tar.gz file of OpenSSL source", required=True) parser.add_argument("-a", "--arch", help="Second argument must be x86 or amd64", required=True) parser.add_argument("-v", "--vs_version", help="Visual Studio version (eg:90, 140, 150)", required=True) parser.set_defaults(writeVersionInfos=False) args = parser.parse_args()  compile_flags = "-no-asm" #compile_flags = "-no-asm -no-shared"  openssl_32_flag = "VC-WIN32" openssl_64_flag = "VC-WIN64A"  working_dir = os.getcwd()  dirname  = args.filename.replace(".tar.gz","")  src_32_suffix = "_" + "vs" + args.vs_version + "_32" src_64_suffix = "_" + "vs" + args.vs_version + "_64"  vs_tools_env_var = "VS" + args.vs_version + "COMNTOOLS"   if args.arch != "x86" and args.arch != "amd64":     print("Second argument must be x86 or amd64")     exit(1)   if not bool(re.match("(openssl-){1}(\d)+(.)(\d)+(.)(\d)+(\w)+(.tar.gz)",args.filename)):     print("The file given doesn't seem to be an openssl source file. It must be in the form: openssl-x.y.zw.tar.gz")     exit(1)   call("7z x -y " + args.filename) #extract the .gz file  dirname_src_32 = dirname + src_32_suffix dirname_src_64 = dirname + src_64_suffix dirname_bin_32 = dirname + src_32_suffix + "_build" dirname_bin_64 = dirname + src_64_suffix + "_build"  openssl_tar_file = args.filename[0:-3]  if args.arch == "x86":  #delete previous directories     shutil.rmtree(os.getcwd()+'/'+dirname, ignore_errors=True)     shutil.rmtree(os.getcwd()+'/'+dirname_src_32, ignore_errors=True)  #extract tar file for 32      call("7z x -y " + openssl_tar_file)     os.rename(dirname, dirname_src_32)  #Compile 32     os.chdir(dirname_src_32)      print("perl Configure " + openssl_32_flag + " --prefix=" + os.path.join(working_dir,dirname_bin_32) + " " + compile_flags)     call("perl Configure " + openssl_32_flag + " --prefix=" + os.path.join(working_dir,dirname_bin_32) + " " + compile_flags,shell=True)      if( os.path.exists("ms/do_ms.bat") ):         call("ms\do_ms.bat",shell=True)         print(os.getcwd())         call("nmake -f ms/ntdll.mak",shell=True)         call("nmake -f ms/ntdll.mak install",shell=True)     else:         call("nmake",shell=True)         call("nmake test",shell=True)         call("nmake install",shell=True)      print("32-bit compilation complete.")  #Go back to base dir os.chdir(working_dir) ################  if args.arch == "amd64":  #delete previous directories     shutil.rmtree(os.getcwd()+'/'+dirname, ignore_errors=True)     shutil.rmtree(os.getcwd()+'/'+dirname_src_64, ignore_errors=True)   #extract for 64     call("7z x -y " + openssl_tar_file)     os.rename(dirname, dirname_src_64)  #Compile 64     os.chdir(dirname_src_64)      call("perl Configure " + openssl_64_flag + " --prefix=" + os.path.join(working_dir,dirname_bin_64) + " " + compile_flags,shell=True)     if( os.path.exists("ms\do_ms.bat") ):         call("ms\do_win64a.bat",shell=True)         call("nmake -f ms/ntdll.mak",shell=True)         call("nmake -f ms/ntdll.mak install",shell=True)     else:         call("nmake",shell=True)         call("nmake test",shell=True)         call("nmake install",shell=True)      print("64-bit compilation complete.")  #Go back to base dir os.chdir(working_dir) ################  os.remove(openssl_tar_file) 

2) Create the file: CompileOpenSSL_vs.cmd

ECHO  -------------------------------------- ECHO Require Python, 7Zip, PERL and NASM in PATH ECHO  --------------------------------------  Rem ------------------------------------------------------ Rem TO CONFIGURE ----------------------------------------- Rem ------------------------------------------------------  Rem SET YOUR LOCAL PATHS----------------------------------------- SET PATH=C:\Program Files (x86)\7-Zip;C:\Perl64\bin;M:\Backup\Coders\_tools\7-Zip\;%PATH%   Rem SET YOUR OPENSSL ARCHIVE----------------------------------------- REM SET FILENAME=openssl-1.0.2r.tar.gz  SET FILENAME=openssl-1.1.1b.tar.gz  Rem SET THE VERSION OF YOUR VISUAL STUDIO----------------------------------------- SET VSVERSION=%1   Rem ------------------------------------------------------ Rem COMPILATION LAUNCH ----------------------------------- Rem ------------------------------------------------------  Rem UTILS PATH----- SET VSCOMNTOOLSNAME=VS%VSVERSION%COMNTOOLS  Rem Pick the good path for Visual Studio----------------------------------------- IF %VSVERSION% GEQ 150 (     Echo DO NOT FORGET TO ADD A SYSTEM VARIABLE %VSCOMNTOOLSNAME% - like: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\"     SET VCVARPATH="%%%VSCOMNTOOLSNAME%%%..\..\VC\Auxiliary\Build\vcvarsall.bat" ) ELSE (     SET VCVARPATH="%%%VSCOMNTOOLSNAME%%%..\..\VC\vcvarsall.bat" )  Rem Set env ----------------------------------------- @pushd "%~dp0" call %VCVARPATH% %2 @popd  Rem ------------------------------------------------------ Rem TEST APP EXIST ----------------------------------- Rem ------------------------------------------------------  where /q 7z.exe IF ERRORLEVEL 1 (     ECHO The application "7z.exe" is missing. Ensure to add/install it to the PATH in beginning of this script, check SET PATH     PAUSE     EXIT /B )  where /q perl.exe IF ERRORLEVEL 1 (     ECHO The application "perl.exe" is missing. Ensure to add/install it to the PATH in beginning of this script, check SET PATH     PAUSE     EXIT /B )  where /q nmake.exe IF ERRORLEVEL 1 (     ECHO The application "nmake.exe" is missing. Ensure to add/install it to the PATH in beginning of this script, check SET PATH     PAUSE     EXIT /B )  where /q py.exe IF ERRORLEVEL 1 (     ECHO The application "py.exe" [shortcut of python] is missing. Ensure to add/install it to the PATH in beginning of this script, check SET PATH     PAUSE     EXIT /B )  Rem Launch compilation -----------------------------------------  py CompileOpenSSL.py -f %FILENAME% -a %2 -v %VSVERSION%   PAUSE 

3) Launch compilation from command line (Outside Visual Studio) eg:

CompileOpenSSL_vs.cmd 150 x86 CompileOpenSSL_vs.cmd 150 amd64  CompileOpenSSL_vs.cmd 90 x86 
like image 26
Ti-R Avatar answered Sep 28 '22 17:09

Ti-R