Running Openssl from a bash script on windows - Subject does not start with '/'

前端 未结 2 1995
深忆病人
深忆病人 2021-01-30 02:16

In my script I have:

openssl req \\
  -x509 \\
  -new \\
  -nodes \\
  -key certs/ca/my-root-ca.key.pem \\
  -days 3652 \\
  -out certs/ca/my-root-ca.crt.pem \\
         


        
2条回答
  •  走了就别回头了
    2021-01-30 02:41

    I personally found this to be specific to the OpenSSL binary in use. On my system using msys2/mingw64 I've noticed that two different OpenSSL binaries are present, for example:

    $ whereis openssl; echo; which openssl
    openssl: /usr/bin/openssl.exe /usr/lib/openssl /mingw64/bin/openssl.exe /usr/share/man/man1/openssl.1ssl.gz
    
    /mingw64/bin/openssl
    

    I believe it to be the use of /mingw64/bin/openssl that requires using a subject that begins with //, however I'm not sure if this is specific to the package/build or the version of OpenSSL so to be sure, the version of each binary is below:

    $ while read -r _openSslBin; do printf "${_openSslBin}: "; ${_openSslBin} version; done < <(whereis openssl | egrep -o '[^ ]+?\.exe ')
    /usr/bin/openssl.exe: OpenSSL 1.0.2p  14 Aug 2018
    /mingw64/bin/openssl.exe: OpenSSL 1.1.1  11 Sep 2018
    

    I've found the following example of bash code to select the correct binary based on the OpenSSL version when using msys/mingw to work on my machine:

    # determine openssl binary to use based on OS
    # -------------------------------------------
    _os="$(uname -s | awk 'BEGIN{FS="_"} {print $1}' | egrep -o '[A-Za-z]+')"
    if [ "${_os,,}" = "mingw" ] || [ "${_os,,}" == "msys" ]; then
      while read -r _currentOpenSslBin; do
        if [[ "$(${_currentOpenSslBin}  version | awk '{print $2}')" =~ ^(1\.0\.[0-9].*|0\.\9\.8.*)$ ]]; then
          _openSslBin="${_currentOpenSslBin}"
        fi
      done < <(whereis openssl | egrep -o '\/[^ ]+?\.exe ' | egrep -v 'mingw')
      if [ -n "${_openSslBin}" ]; then
        printf "OpenSSL Binary: ${_openSslBin} (v. $(${_openSslBin}  version | awk '{print $2}'))\n"
      else
        printf "Unable to find compatible version of OpenSSL for use with '${_os}' OS, now exiting...\n"
        exit 1
      fi
    else
      _openSslBin="openssl"
    fi
    
    # display selected openssl binary and it's version
    # ------------------------------------------------
    printf "${_openSslBin}: "; ${_openSslBin} version
    

    In addition to fixing issues with passing the subject string I also found this to resolve issues with the size of the DN (I passed a custom openssl.cnf with a policy that did not set a max_size for any of the fields and that still had problems when using /mingw64/bin/openssl.exe).

提交回复
热议问题