Jay Taylor's notes
back to listing indexUsing openssl to get the certificate from a server
[web search]- Home
-
- Public
- Stack Overflow
- Tags
- Users
- Jobs
-
- Teams
- Create Team
I am trying to get the certificate of a remote server, which I can then use to add to my keystore and use within my java application.
A senior dev (who is on holidays :( ) informed me I can run this:
openssl s_client -connect host.host:9999
To get a raw certificate dumped out, which I can then copy and export. I receive the following output:
depth=1 /C=NZ/ST=Test State or Province/O=Organization Name/OU=Organizational Unit Name/CN=Test CA
verify error:num=19:self signed certificate in certificate chain
verify return:0
23177:error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure:s3_pkt.c:1086:SSL alert number 40
23177:error:140790E5:SSL routines:SSL23_WRITE:ssl handshake failure:s23_lib.c:188:
I have also tried with this option
-showcerts
and this one (running on debian mind you)
-CApath /etc/ssl/certs/
But get the same error.
This source says I can use that CApath flag but it doesn't seem to help. I tried multiple paths to no avail.
Please let me know where I'm going wrong.
With SNI
If the remote server is using SNI (that is, sharing multiple SSL hosts on a single IP address) you will need to send the correct hostname in order to get the right certificate.
openssl s_client -showcerts -servername www.example.com -connect www.example.com:443 </dev/null
Without SNI
If the remote server is not using SNI, then you can skip -servername
parameter:
openssl s_client -showcerts -connect www.example.com:443 </dev/null
To view the full details of a site's cert you can use this chain of commands as well:
$ echo | \
openssl s_client -servername www.example.com -connect www.example.com:443 2>/dev/null | \
openssl x509 -text
-
3Hmm. I still get the same error when trying that command. I noticed my Openssl version is 'OpenSSL 0.9.8g 19 Oct 2007'. Do you have any ideas? – nasty pasty Oct 26 '11 at 2:07
-
31Useful:
echo "" | openssl s_client -connect server:port -prexit 2>/dev/null | sed -n -e '/BEGIN\ CERTIFICATE/,/END\ CERTIFICATE/ p'
stackoverflow.com/a/12918442/843000 – mbrownnyc Jan 9 '13 at 20:31 -
12Alternative useful script, from madboa.com:
echo | openssl s_client -connect server:port 2>&1 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > cert.pem
– rmeakins Aug 5 '13 at 5:44 -
9To make this a bit more concise, you can replace the
sed
withopenssl x509
, and read it in using a sub-shell:openssl x509 -in <(openssl s_client -connect server:port -prexit 2>/dev/null)
– Gabe Martin-Dempesy Aug 14 '13 at 17:28 -
17Also
echo | openssl s_client -connect google.com:443 2>/dev/null | openssl x509
– MattSizzle Jan 15 '14 at 3:35
While I agree with Ari's answer (and upvoted it :), I needed to do an extra step to get it to work with Java on Windows (where it needed to be deployed):
openssl s_client -showcerts -connect www.example.com:443 < /dev/null | openssl x509 -outform DER > derp.der
Before adding the openssl x509 -outform DER
conversion, I was getting an error from keytool on Windows complaining about the certificate's format. Importing the .der file worked fine.
-
Odd. I've been using PEM certificates with keytool on Windows since Java 6 and never faced an issue. – imgx64 Nov 1 '17 at 5:19
It turns out there is more complexity here: I needed to provide many more details to get this rolling. I think its something to do with the fact that its a connection that needs client authentication, and the hankshake needed more info to continue to the stage where the certificates were dumped.
Here is my working command:
openssl s_client -connect host:port -key our_private_key.pem -showcerts \
-cert our_server-signed_cert.pem
Hopefully this is a nudge in the right direction for anyone who could do with some more info.
-
5I am sorry, but your answer doesn't make much sense. You needed to pass the certificate to the server in order to get the certificate? – Ari Maniatis Nov 14 '11 at 22:12
-
1
-
11It turns out '-prexit' will return that data as well. E.g.; openssl s_client -connect host:port -prexit – Robert Sep 20 '12 at 12:54
The easiest command line for this, which includes the PEM output to add it to the keystore, as well as a human readable output and also supports SNI, which is important if you are working with an HTTP server is:
openssl s_client -servername example.com -connect example.com:443 \
</dev/null 2>/dev/null | openssl x509 -text
The -servername option is to enable SNI support and the openssl x509 -text prints the certificate in human readable format.
-
You may add to your -servername your subdomain, for instance ws.example.com instead of example.com (apply this to the -connect parameter too). – russellhoff Jan 26 '17 at 11:26
To get the certificate of remote server you can use openssl
tool and you can find it between BEGIN CERTIFICATE
and END CERTIFICATE
which you need to copy and paste into your certificate file (CRT).
Here is the command demonstrating it:
ex +'/BEGIN CERTIFICATE/,/END CERTIFICATE/p' <(echo | openssl s_client -showcerts -connect example.com:443) -scq > file.crt
To return all certificates from the chain, just add g
(global) like:
ex +'g/BEGIN CERTIFICATE/,/END CERTIFICATE/p' <(echo | openssl s_client -showcerts -connect example.com:443) -scq
Then you can simply import your certificate file (file.crt
) into your keychain and make it trusted, so Java shouldn't complain.
On OS X you can double-click on the file or drag and drop in your Keychain Access, so it'll appear in login/Certificates. Then double-click on the imported certificated and make it Always Trust for SSL.
On CentOS 5 you can append them into /etc/pki/tls/certs/ca-bundle.crt
file (and run: sudo update-ca-trust force-enable
), or in CentOS 6 copy them into /etc/pki/ca-trust/source/anchors/
and run sudo update-ca-trust extract
.
In Ubuntu, copy them into /usr/local/share/ca-certificates
and run sudo update-ca-certificates
.
You can get and store the server root certificate using next bash script:
CERTS=$(echo -n | openssl s_client -connect $HOST_NAME:$PORT -showcerts | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p')
echo "$CERTS" | awk -v RS="-----BEGIN CERTIFICATE-----" 'NR > 1 { printf RS $0 > "'$SERVER_ROOT_CERTIFICATE'"; close("'$SERVER_ROOT_CERTIFICATE'") }'
Just overwrite required variables.
to print only the certificate chain and not the server's certificate:
# MYHOST=myhost.com
# MYPORT=443
# openssl s_client -connect ${MYHOST}:${MYPORT} -showcerts 2>/dev/null </dev/null | awk '/^.*'"${MYHOST}"'/,/-----END CERTIFICATE-----/{next;}/-----BEGIN/,/-----END CERTIFICATE-----/{print}'
to update CA trust on CentOS/RHEL 6/7 :
# update-ca-trust enable
# openssl s_client -connect ${MYHOST}:${MYPORT} -showcerts 2>/dev/null </dev/null | awk '/^.*'"${MYHOST}"'/,/-----END CERTIFICATE-----/{next;}/-----BEGIN/,/-----END CERTIFICATE-----/{print}' >/etc/pki/ca-trust/source/anchors/myca.cert
# update-ca-trust extract
on CentOS/RHEL 5:
# openssl s_client -connect ${MYHOST}:${MYPORT} -showcerts 2>/dev/null </dev/null | awk '/^.*'"${MYHOST}"'/,/-----END CERTIFICATE-----/{next;}/-----BEGIN/,/-----END CERTIFICATE-----/{print}' >>/etc/pki/tls/certs/ca-bundle.crt
For the benefit of others like me who tried to follow the good advice here when accessing AWS CloudFront but failed, the trick is to add -servername domain.name..
.
Your Answer
Not the answer you're looking for? Browse other questions tagged linux security certificate openssl ssl-certificate or ask your own question.
asked |
6 years, 8 months ago |
viewed |
419,021 times |
active |
Linked
Related
Hot Network Questions
-
Does the Flame Arrows' magic end on the piece of ammunition when the spell ends?
-
How difficult is it to intercept the POST response body when SSL is used?
-
Importance of professional website to search committees
-
How can one spellcaster avoid complete lockdown via Counterspell against multiple other casters?
-
PhD without Enrolling in Formal Program
-
An invisible traveller
-
Please explain Dedekind cuts because I'm about to give up
-
Are rejections usually quick?
-
Did Obama defy precedent by not visiting the D-Day monument?
-
Could we breathe an atmosphere that is not nitrogen based?
-
Why mv is so much faster than cp? What's the best rescue for a wrong mv action?
-
Randomizing until 0
-
When is a lack of long, sophisticated words to describe an otherwise simple concept bad?
-
What spell lets you break an item to return home?
-
Should we limit who can submit stories in Scrum?
-
Humans are gone - what can I harvest from their cities 30M years later?
-
How to provide feedback about an unprofessional manager during the exit interview?
-
Gap (2") between PVC conduit and exposed foundation (exterior)
-
As a beginner , what version of Sitecore should I setup and practice 8 or 9?
-
Difference between watching a film THROUGH tv and ON tv
-
How would intercourse be verified as a form of marriage?
-
Which Go rank is higher?
-
What is the part of a bottle called where the liquid comes out?
-
How can I ask my husband about his work progress without him becoming defensive?
site design / logo © 2018 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2018.7.9.30969