84

I am trying to use openssl to get a certificate, and it seems to keep hanging. I have done a lot of research but not all of the available options seem to work on Windows.

openssl s_client -showcerts -connect google.com:443 > cert.txt

I have tried this:

openssl s_client -connect xyz:443 < quit.txt > cert.txt

Where quit.txt contains "quit\n" from http://bytes.com/topic/php/answers/8802-automate-openssl-s_client-command-batch-php-script

That did not work. I also looked at Openssl s_clinet -connect scripting. Force quit help

I have also tried -prexit

I have also looked into this as well and can't get it working: https://serverfault.com/questions/139728/how-to-download-ssl-certificate-from-a-website

I was doing so well! I managed to do something that I thought would be impossible and a simple thing like this bug managed to stop me for the time being :(

Community
  • 1
  • 1
Adiboy
  • 843
  • 1
  • 6
  • 5
  • 1
    Edited basic spelling and grammar, attempting to make it look like you put some effort into asking the question. Relocate the new version to ServerFault, where you're more likely to get an answer. – Adam Liss Feb 26 '12 at 03:02
  • Check out http://stackoverflow.com/questions/16823068/gnuwin32-openssl-s-client-conn-to-websphere-mq-server-not-closing-at-eof-hangs -- SendKeys("\n") in WScript can make OpenSSL quit where an EOF cannot. – clacke Oct 13 '13 at 07:37

5 Answers5

177

On windows, simply typing winpty before your openssl command will do the trick. So, for example, you could create a certificate like so:

winpty openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days XXX

Glenn Werner
  • 2,048
  • 2
  • 12
  • 16
31

It looks like some OpenSSL distributions for Windows are expecting an additional keypress, independant of standard input. Quit.txt gets correctly piped into openssl's STDIN (the server receives QUIT command), but nothing happens until you press any key.

This problem does not exist in Cygwin's version of OpenSSL. Unfortunatly base installation of Cygwin takes about 100 MB of disk space, but you can try to extract only openssl.exe and required libraries.

This method works:

echo QUIT | c:\cygwin\bin\openssl.exe s_client -showcerts -connect google.com:443 > cert.txt
MBu
  • 2,880
  • 2
  • 19
  • 25
  • 1
    thank you for that MBu. it worked this is the list of files i needed to copy cygcrypto-0.9.8.dll cyggcc_s-1.dll cygssl-0.9.8.dll cygwin1.dll cygz.dll find-serial.bat libeay32.dll openssl.exe ssleay32.dll – Adiboy Feb 28 '12 at 01:29
  • 1
    Non only on Windows but also on Linux the echo QUIT piped into the command solves the stalling output issue. – David Ramirez Nov 04 '14 at 17:17
  • This solved my time out issue when trying to scan for expired certificates on on our network. I had to add a sleep to make it more stable (sleep 2; echo QUIT;)|openssl ... – LinuxGuru Oct 09 '18 at 20:08
  • has anyone found out how to suppress the console output depth = ... etc on that command when still sending ouput to a file? – GWD Jun 21 '22 at 16:16
10

If running under mingw64 on windows you can use the winpty program to correctly wrap the terminal

Eg creating alias under bash alias openssl='winpty openssl.exe'

Then openssl s_client -connect blah

Should work as expected

crafty
  • 10,346
  • 1
  • 18
  • 15
4

For reasons i do not completeley understand, echoing QUIT or quit\n into the input did not work in my case. I'm using MINGW64 with OpenSSL 1.0.2d on Windows 8.1, and i'm using openssl to get certificates from servers inside a bash script. However, just running the openssl command in background and waiting a bit worked for me:

#!/bin/bash

openssl s_client -connect my.server.com:443 -showcerts > output.txt 2>/dev/null &
sleep 2
k6ps
  • 359
  • 4
  • 11
0

The data to send to the server is expected when using the s_client option of openssl. On *nix, prepending echo |sends a CR so that openssl does not hang, for example:

echo | openssl s_client -servername www.microsoft.com -connect www.microsoft.com:443 2>/dev/null  | openssl x509 -noout -subject -issuer -dates
Tony BenBrahim
  • 7,040
  • 2
  • 36
  • 49