Sep 11, 2018 You can use Java key tool or some other tool, but we will be working with OpenSSL. To generate a public and private key with a certificate signing request (CSR), run the following OpenSSL command: openssl req –out certificatesigningrequest.csr -new -newkey rsa:2048 -nodes -keyout privatekey.key. Generate an RSA private key using default parameters: openssl genpkey -algorithm RSA -out key.pem. Encrypt output private key using 128 bit AES and the passphrase 'hello': openssl genpkey -algorithm RSA -out key.pem -aes-128-cbc -pass pass:hello. Generate a 2048 bit RSA key using 3 as the public exponent.
# Define where to store the generated certs and metadata. |
DIR='$(pwd)/tls' |
# Optional: Ensure the target directory exists and is empty. |
rm -rf '${DIR}' |
mkdir -p '${DIR}' |
# Create the openssl configuration file. This is used for both generating |
# the certificate as well as for specifying the extensions. It aims in favor |
# of automation, so the DN is encoding and not prompted. |
cat >'${DIR}/openssl.cnf'<<EOF |
[req] |
default_bits = 2048 |
encrypt_key = no # Change to encrypt the private key using des3 or similar |
default_md = sha256 |
prompt = no |
utf8 = yes |
# Speify the DN here so we aren't prompted (along with prompt = no above). |
distinguished_name = req_distinguished_name |
# Extensions for SAN IP and SAN DNS |
req_extensions = v3_req |
# Be sure to update the subject to match your organization. |
[req_distinguished_name] |
C = US |
ST = California |
L = The Cloud |
O = Demo |
CN = My Certificate |
# Allow client and server auth. You may want to only allow server auth. |
# Link to SAN names. |
[v3_req] |
basicConstraints = CA:FALSE |
subjectKeyIdentifier = hash |
keyUsage = digitalSignature, keyEncipherment |
extendedKeyUsage = clientAuth, serverAuth |
subjectAltName = @alt_names |
# Alternative names are specified as IP.# and DNS.# for IP addresses and |
# DNS accordingly. |
[alt_names] |
IP.1 = 1.2.3.4 |
DNS.1 = my.dns.name |
EOF |
# Create the certificate authority (CA). This will be a self-signed CA, and this |
# command generates both the private key and the certificate. You may want to |
# adjust the number of bits (4096 is a bit more secure, but not supported in all |
# places at the time of this publication). |
# |
# To put a password on the key, remove the -nodes option. |
# |
# Be sure to update the subject to match your organization. |
openssl req |
-new |
-newkey rsa:2048 |
-days 120 |
-nodes |
-x509 |
-subj '/C=US/ST=California/L=The Cloud/O=My Company CA' |
-keyout '${DIR}/ca.key' |
-out '${DIR}/ca.crt' |
# |
# For each server/service you want to secure with your CA, repeat the |
# following steps: |
# |
# Generate the private key for the service. Again, you may want to increase |
# the bits to 4096. |
openssl genrsa -out '${DIR}/my-service.key' 2048 |
# Generate a CSR using the configuration and the key just generated. We will |
# give this CSR to our CA to sign. |
openssl req |
-new -key '${DIR}/my-service.key' |
-out '${DIR}/my-service.csr' |
-config '${DIR}/openssl.cnf' |
# Sign the CSR with our CA. This will generate a new certificate that is signed |
# by our CA. |
openssl x509 |
-req |
-days 120 |
-in '${DIR}/my-service.csr' |
-CA '${DIR}/ca.crt' |
-CAkey '${DIR}/ca.key' |
-CAcreateserial |
-extensions v3_req |
-extfile '${DIR}/openssl.cnf' |
-out '${DIR}/my-service.crt' |
# (Optional) Verify the certificate. |
openssl x509 -in '${DIR}/my-service.crt' -noout -text |
# Here is a sample response (truncate): |
# |
# Certificate: |
# Signature Algorithm: sha256WithRSAEncryption |
# Issuer: C = US, ST = California, L = The Cloud, O = My Organization CA |
# Subject: C = US, ST = California, L = The Cloud, O = Demo, CN = My Certificate |
# # .. |
# X509v3 extensions: |
# X509v3 Basic Constraints: |
# CA:FALSE |
# X509v3 Subject Key Identifier: |
# 36:7E:F0:3D:93:C6:ED:02:22:A9:3D:FF:18:B6:63:5F:20:52:6E:2E |
# X509v3 Key Usage: |
# Digital Signature, Key Encipherment |
# X509v3 Extended Key Usage: |
# TLS Web Client Authentication, TLS Web Server Authentication |
# X509v3 Subject Alternative Name: |
# IP Address:1.2.3.4, DNS:my.dns.name |
# |
While Encrypting a File with a Password from the Command Line using OpenSSLis very useful in its own right, the real power of the OpenSSL library is itsability to support the use of public key cryptograph for encrypting orvalidating data in an unattended manner (where the password is not required toencrypt) is done with public keys.
You can generate a public and private RSA key pair like this:
openssl genrsa -des3 -out private.pem 2048
That generates a 2048-bit RSA key pair, encrypts them with a password you provideand writes them to a file. You need to next extract the public key file. You willuse this, for instance, on your web server to encrypt content so that it canonly be read with the private key.
This is a command that is
openssl rsa -in private.pem -outform PEM -pubout -out public.pem
The -pubout
flag is really important. Be sure to include it.
Next open the public.pem
and ensure that it starts with-----BEGIN PUBLIC KEY-----
. This is how you know that this file is thepublic key of the pair and not a private key.
To check the file from the command line you can use the less
command, like this:
less public.pem
A previous version of the post gave this example in error.
openssl rsa -in private.pem -out private_unencrypted.pem -outform PEM
The error is that the -pubout
was dropped from the end of the command.That changes the meaning of the command from that of exporting the public keyto exporting the private key outside of its encrypted wrapper. Inspecting theoutput file, in this case private_unencrypted.pem
clearly shows that the keyis a RSA private key as it starts with -----BEGIN RSA PRIVATE KEY-----
.
But if you have lost the public key part but still have the private key, there is a way to regenerate the key.With the public key missing, the following command will show you that there is no public key for this SSH key. .A lost SSH public-key or a web service generates an SSH key but does not provide the public-key part to you. $ ssh-keygen -l -f /.ssh/idrsatest is not a public key file.The -l option instructs to show the fingerprint in the public key while the -f option specifies the file of the key to list the fingerprint for.To generate the missing public key again from the private key, the following command will generate the public key of the private key provided with the -f option. There is a solution for this situation.When you have an SSH key you need the public key to setup. What to do now? Generate public key from private rsa.
It is important to visually inspect you private and public key files to makesure that they are what you expect. OpenSSL will clearly explain the nature ofthe key block with a -----BEGIN RSA PRIVATE KEY-----
or -----BEGIN PUBLIC KEY-----
.
You can use less to inspect each of your two files in turn:
less private.pem
to verify that it starts with a -----BEGIN RSA PRIVATE KEY-----
less public.pem
to verify that it starts with a -----BEGIN PUBLIC KEY-----
The next section shows a full example of what each key file should look like.
The generated files are base64-encoded encryption keys in plain text format.If you select a password for your private key, its file will be encrypted withyour password. Be sure to remember this password or the key pair becomes useless.
Depending on the nature of the information you will protect, it’s important tokeep the private key backed up and secret. The public key can be distributedanywhere or embedded in your web application scripts, such as in your PHP,Ruby, or other scripts. Again, backup your keys!
Remember, if the key goes away the data encrypted to it is gone. Keeping aprinted copy of the key material in a sealed envelope in a bank safety depositbox is a good way to protect important keys against loss due to fire or harddrive failure.
If you, dear reader, were planning any funny business with the private key that I have just published here. Know that they were made especially for this series of blog posts. I do not use them for anything else.
Learn more about our services or drop us your email and we'll e-mail you back.