Extracting Client Certificates
You can display all of the server’s certificates using the following command:
$ openssl s_client -showcerts server.name:port | sed -n '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/p'
This output can be piped or copy/pasted to a text file (only keep the
parts between the BEGIN and END CERTIFICATE sections)
and give it a .crt
file extension. You can then use it as
input to whatever client app needs it.
In the below example we pipe the openssl
output to
grep
to remove the identifiers of the certificates. You may
not get your prompt back until the command times out, so you
should wait a bit and after a bit you should get something similar to
the following output:
$ openssl s_client -showcerts example.com:443 2>&1 |grep -v '^depth'| sed -n '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/p'
-----BEGIN CERTIFICATE-----
MIIHSjCCBjKgAwIBAgIQDB/LGEUYx+OGZ0EjbWtz8TANBgkqhkiG9w0BAQsFADBP
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMSkwJwYDVQQDEyBE
aWdpQ2VydCBUTFMgUlNBIFNIQTI1NiAyMDIwIENBMTAeFw0yMzAxMTMwMDAwMDBa
Fw0yNDAyMTMyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv
cm5pYTEUMBIGA1UEBxMLTG9zIEFuZ2VsZXMxQjBABgNVBAoMOUludGVybmV0wqBD
b3Jwb3JhdGlvbsKgZm9ywqBBc3NpZ25lZMKgTmFtZXPCoGFuZMKgTnVtYmVyczEY
MBYGA1UEAxMPd3d3LmV4YW1wbGUub3JnMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
MIIBCgKCAQEAwoB3iVm4RW+6StkR+nutx1fQevu2+t0Fu6KBcbvhfyHSXy7w0nJO
dTT4jWLjStpRkNQBPZwMwHH35i+21gdnJtDe/xfO8IX9McFmyodlBUcqX8CruIzD
v9AXf2OjXPBG+4aq+03XKl5/muATl32++301Vw1dXoGYNeoWQqLTsHT3WS3tOOf+
ehuzNuZ+rj+ephaD3lMBToEArrtC9R91KTTN6YSAOK48NxTA8CfOMFK5itxfIq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cO3aXbMM9yt1QP6FCvrzMXi3cEsaiVi6gL3z
ax3pfs8LulicWdSQ0/1s/dCYbbdxglvPbQtaCdB73sRD2Cqk3p5BJl+7j5nL3a7h
qG+fh/50tx8bIKuxT8b1Z11dmzzp/2n3YWzW2fP9NsarA4h20ksudYbj/NhVfSbC
EXffPgK2fPOre3qGNm+499iTcc+G33Mw+nur7SpZyEKEOxEXGlLzyQ4UfaJbcme6
ce1XR2bFuAJKZTRei9AqPCCcUZlM51Ke92sRKw2Sfh3oius2FkOH6ipjv3U/697E
A7sKPPcw7+uvTPyLNhBzPvOk
-----END CERTIFICATE-----
Tags: cli, certificates, openssl, motd
Encrypting Files using OpenSSL
Let’s say we have a file that contains sensitive information and we
want to encyrpt it. You can encrypt a file very easily using the
openssl
command:
$ cat secret.txt
This file contains some very secret stuff
$ openssl enc -e -aes-256-cbc -pbkdf2 -a -salt -in secret.txt -out secret.enc
enter aes-256-cbc encryption password: <enter-a-password>
Verifying - enter aes-256-cbc encryption password: <enter-a-password>
$ cat secret.enc
U2FsdGVkX19Rnz48WjLeljd19wvNOhQy+zzYwxCANezCTkqpGMl9zs4HdwdUzZjl
VQkUsCJ7b0rUpRi83UlcwA==
Now we decrypt it (output to a different file). Make sure you enter the same password that you input above:
$ openssl enc -d -aes-256-cbc -pbkdf2 -a -salt -in secret.enc -out unencrypted-secret.txt
enter aes-256-cbc decryption password: <enter-a-password>
$ cat unencrypted-secret.txt
This file contains some very secret stuff
$
Tags: cli, openssl, encryption, motd
Finding the SSL Directory on a Server
I’ve had situations where I was configuring a secure connection to an
application and needed to know where the SSL certificates are stored on
the server. You can easily find out this information using the
openssl
and grep
commands:
$ openssl version -a | grep OPENSSLDIR
OPENSSLDIR: "/etc/pki/tls"