Skip to content

Instantly share code, notes, and snippets.

@junxie6
Forked from zapstar/openssl_2way_auth.sh
Created September 8, 2021 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junxie6/ad26d1926787ca9b4d0c032df96feeab to your computer and use it in GitHub Desktop.
Save junxie6/ad26d1926787ca9b4d0c032df96feeab to your computer and use it in GitHub Desktop.
Steps to create CA, server and client keys + certificates for SSL 2-way authentication
# Move to root directory...
cd /
mkdir keys
cd keys
# Generate a self signed certificate for the CA along with a key.
mkdir -p ca/private
chmod 700 ca/private
# NOTE: I'm using -nodes, this means that once anybody gets
# their hands on this particular key, they can become this CA.
openssl req \
-x509 \
-nodes \
-days 3650 \
-newkey rsa:4096 \
-keyout ca/private/ca_key.pem \
-out ca/ca_cert.pem \
-subj "/C=US/ST=Acme State/L=Acme City/O=Acme Inc./CN=example.com"
# Create server private key and certificate request
mkdir -p server/private
chmod 700 ca/private
openssl genrsa -out server/private/server_key.pem 4096
openssl req -new \
-key server/private/server_key.pem \
-out server/server.csr \
-subj "/C=US/ST=Acme State/L=Acme City/O=Acme Inc./CN=server.example.com"
# Create client private key and certificate request
mkdir -p client/private
chmod 700 client/private
openssl genrsa -out client/private/client_key.pem 4096
openssl req -new \
-key client/private/client_key.pem \
-out client/client.csr \
-subj "/C=US/ST=Acme State/L=Acme City/O=Acme Inc./CN=client.example.com"
# Generate certificates
openssl x509 -req -days 1460 -in server/server.csr \
-CA ca/ca_cert.pem -CAkey ca/private/ca_key.pem \
-CAcreateserial -out server/server_cert.pem
openssl x509 -req -days 1460 -in client/client.csr \
-CA ca/ca_cert.pem -CAkey ca/private/ca_key.pem \
-CAcreateserial -out client/client_cert.pem
# Now test both the server and the client
# On one shell, run the following
openssl s_server -CAfile ca/ca_cert.pem -cert server/server_cert.pem -key server/private/server_key.pem -Verify 1
# On another shell, run the following
openssl s_client -CAfile ca/ca_cert.pem -cert client/client_cert.pem -key client/private/client_key.pem
# Once the negotiation is complete, any line you type is sent over to the other side.
# By line, I mean some text followed by a keyboard return press.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment