Chapter 2. Creating and managing TLS keys and certificates
You can encrypt communication transmitted between two systems by using the TLS (Transport Layer Security) protocol. This standard uses asymmetric cryptography with private and public keys, digital signatures, and certificates.
2.1. TLS certificates Copy linkLink copied to clipboard!
TLS (Transport Layer Security) is a protocol that enables client-server applications to pass information securely. TLS uses a system of public and private key pairs to encrypt communication transmitted between clients and servers. TLS is the successor protocol to SSL (Secure Sockets Layer).
TLS uses X.509 certificates to bind identities, such as hostnames or organizations, to public keys using digital signatures. X.509 is a standard that defines the format of public key certificates.
Authentication of a secure application depends on the integrity of the public key value in the application’s certificate. If an attacker replaces the public key with its own public key, it can impersonate the true application and gain access to secure data. To prevent this type of attack, all certificates must be signed by a certification authority (CA). A CA is a trusted node that confirms the integrity of the public key value in a certificate.
A CA signs a public key by adding its digital signature and issues a certificate. A digital signature is a message encoded with the CA’s private key. The CA’s public key is made available to applications by distributing the certificate of the CA. Applications verify that certificates are validly signed by decoding the CA’s digital signature with the CA’s public key.
To have a certificate signed by a CA, you must generate a public key, and send it to a CA for signing. This is referred to as a certificate signing request (CSR). A CSR contains also a distinguished name (DN) for the certificate. The DN information that you can provide for either type of certificate can include a two-letter country code for your country, a full name of your state or province, your city or town, a name of your organization, your email address, and it can also be empty. Many current commercial CAs prefer the Subject Alternative Name extension and ignore DNs in CSRs.
RHEL provides two main toolkits for working with TLS certificates: GnuTLS and OpenSSL. You can create, read, sign, and verify certificates using the openssl
utility from the openssl
package. The certtool
utility provided by the gnutls-utils
package can do the same operations using a different syntax and above all a different set of libraries in the back end.
2.2. Creating a private CA using OpenSSL Copy linkLink copied to clipboard!
Private certificate authorities (CA) are useful when your scenario requires verifying entities within your internal network. For example, use a private CA when you create a VPN gateway with authentication based on certificates signed by a CA under your control or when you do not want to pay a commercial CA. To sign certificates in such use cases, the private CA uses a self-signed certificate.
Prerequisites
-
You have
root
privileges or permissions to enter administrative commands withsudo
. Commands that require such privileges are marked with#
.
Procedure
Generate a private key for your CA. For example, the following command creates a 256-bit Elliptic Curve Digital Signature Algorithm (ECDSA) key:
openssl genpkey -algorithm ec -pkeyopt ec_paramgen_curve:P-256 -out <ca.key>
$ openssl genpkey -algorithm ec -pkeyopt ec_paramgen_curve:P-256 -out <ca.key>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The time for the key-generation process depends on the hardware and entropy of the host, the selected algorithm, and the length of the key.
Create a certificate signed using the private key generated in the previous command:
openssl req -key <ca.key> -new -x509 -days 3650 -addext keyUsage=critical,keyCertSign,cRLSign -subj "/CN=<Example_CA>" -out <ca.crt>
$ openssl req -key <ca.key> -new -x509 -days 3650 -addext keyUsage=critical,keyCertSign,cRLSign -subj "/CN=<Example_CA>" -out <ca.crt>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The generated
ca.crt
file is a self-signed CA certificate that you can use to sign other certificates for ten years. In the case of a private CA, you can replace <Example_CA> with any string as the common name (CN).Set secure permissions on the private key of your CA, for example:
chown <root>:<root> <ca.key> chmod 600 <ca.key>
# chown <root>:<root> <ca.key> # chmod 600 <ca.key>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Next steps
To use a self-signed CA certificate as a trust anchor on client systems, copy the CA certificate to the client and add it to the clients' system-wide truststore as
root
:trust anchor <ca.crt>
# trust anchor <ca.crt>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow See Chapter 3, Using shared system certificates for more information.
Verification
Create a certificate signing request (CSR), and use your CA to sign the request. The CA must successfully create a certificate based on the CSR, for example:
openssl x509 -req -in <client-cert.csr> -CA <ca.crt> -CAkey <ca.key> -CAcreateserial -days 365 -extfile <openssl.cnf> -extensions <client-cert> -out <client-cert.crt>
$ openssl x509 -req -in <client-cert.csr> -CA <ca.crt> -CAkey <ca.key> -CAcreateserial -days 365 -extfile <openssl.cnf> -extensions <client-cert> -out <client-cert.crt> Signature ok subject=C = US, O = Example Organization, CN = server.example.com Getting CA Private Key
Copy to Clipboard Copied! Toggle word wrap Toggle overflow See Section 2.5, “Using a private CA to issue certificates for CSRs with OpenSSL” for more information.
Display the basic information about your self-signed CA:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Verify the consistency of the private key:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.3. Creating a private key and a CSR for a TLS server certificate using OpenSSL Copy linkLink copied to clipboard!
You can use TLS-encrypted communication channels only if you have a valid TLS certificate from a certificate authority (CA). To obtain the certificate, you must create a private key and a certificate signing request (CSR) for your server first.
Procedure
Generate a private key on your server system, for example:
openssl genpkey -algorithm ec -pkeyopt ec_paramgen_curve:P-256 -out <server-private.key>
$ openssl genpkey -algorithm ec -pkeyopt ec_paramgen_curve:P-256 -out <server-private.key>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Use a text editor of your choice to prepare a configuration file that simplifies creating your CSR, for example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
extendedKeyUsage = serverAuth
option limits the use of a certificate.Create a CSR using the private key you created previously:
openssl req -key <server-private.key> -config <example_server.cnf> -new -out <server-cert.csr>
$ openssl req -key <server-private.key> -config <example_server.cnf> -new -out <server-cert.csr>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you omit the
-config
option, thereq
utility prompts you for additional information, for example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Next steps
- Submit the CSR to a CA of your choice for signing. Alternatively, for an internal use scenario within a trusted network, use your private CA for signing. See Section 2.5, “Using a private CA to issue certificates for CSRs with OpenSSL” for more information.
Verification
After you obtain the requested certificate from the CA, check that the human-readable parts of the certificate match your requirements, for example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.4. Creating a private key and a CSR for a TLS client certificate using OpenSSL Copy linkLink copied to clipboard!
You can use TLS-encrypted communication channels only if you have a valid TLS certificate from a certificate authority (CA). To obtain the certificate, you must create a private key and a certificate signing request (CSR) for your client first.
Procedure
Generate a private key on your client system, for example:
openssl genpkey -algorithm ec -pkeyopt ec_paramgen_curve:P-256 -out <client-private.key>
$ openssl genpkey -algorithm ec -pkeyopt ec_paramgen_curve:P-256 -out <client-private.key>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Use a text editor of your choice to prepare a configuration file that simplifies creating your CSR, for example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
extendedKeyUsage = clientAuth
option limits the use of a certificate.Create a CSR using the private key you created previously:
openssl req -key <client-private.key> -config <example_client.cnf> -new -out <client-cert.csr>
$ openssl req -key <client-private.key> -config <example_client.cnf> -new -out <client-cert.csr>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you omit the
-config
option, thereq
utility prompts you for additional information, for example:You are about to be asked to enter information that will be incorporated into your certificate request. … Common Name (eg, your name or your server's hostname) []: <client.example.com> Email Address []: <client@example.com>
You are about to be asked to enter information that will be incorporated into your certificate request. … Common Name (eg, your name or your server's hostname) []: <client.example.com> Email Address []: <client@example.com>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Next steps
- Submit the CSR to a CA of your choice for signing. Alternatively, for an internal use scenario within a trusted network, use your private CA for signing. See Section 2.5, “Using a private CA to issue certificates for CSRs with OpenSSL” for more information.
Verification
Check that the human-readable parts of the certificate match your requirements, for example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.5. Using a private CA to issue certificates for CSRs with OpenSSL Copy linkLink copied to clipboard!
To enable systems to establish a TLS-encrypted communication channel, a certificate authority (CA) must provide valid certificates to them. If you have a private CA, you can create the requested certificates by signing certificate signing requests (CSRs) from the systems.
Prerequisites
- You have already configured a private CA. See the Creating a private CA by using OpenSSL section for more information.
- You have a file containing a CSR. You can find an example of creating the CSR in the Section 2.3, “Creating a private key and a CSR for a TLS server certificate using OpenSSL” section.
Procedure
Optional: Use a text editor of your choice to prepare an OpenSSL configuration file for adding extensions to certificates, for example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note that the previous example illustrates only the principle and
openssl
does not add all extensions to the certificate automatically. You must add the extensions you require either to the CNF file or append them to parameters of theopenssl
command.Use the
x509
utility to create a certificate based on a CSR, for example:openssl x509 -req -in <server-cert.csr> -CA <ca.crt> -CAkey <ca.key> -CAcreateserial -days 365 -extfile <openssl.cnf> -extensions <server-cert> -out <server-cert.crt>
$ openssl x509 -req -in <server-cert.csr> -CA <ca.crt> -CAkey <ca.key> -CAcreateserial -days 365 -extfile <openssl.cnf> -extensions <server-cert> -out <server-cert.crt> Signature ok subject=C = US, O = Example Organization, CN = server.example.com Getting CA Private Key
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To increase security, delete the serial-number file before you create another certificate from a CSR. This way, you ensure that the serial number is always random. If you omit the
CAserial
option for specifying a custom file name, the serial-number file name is the same as the file name of the certificate, but its extension is replaced with the.srl
extension (server-cert.srl
in the previous example).
2.6. Creating a private CA using GnuTLS Copy linkLink copied to clipboard!
Private certificate authorities (CA) are useful when your scenario requires verifying entities within your internal network. For example, use a private CA when you create a VPN gateway with authentication based on certificates signed by a CA under your control or when you do not want to pay a commercial CA. To sign certificates in such use cases, the private CA uses a self-signed certificate.
Prerequisites
-
You have
root
privileges or permissions to enter administrative commands withsudo
. Commands that require such privileges are marked with#
. You have already installed GnuTLS on your system. If you did not, you can use this command:
yum install gnutls-utils
$ yum install gnutls-utils
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Procedure
Generate a private key for your CA. For example, the following command creates a 256-bit ECDSA (Elliptic Curve Digital Signature Algorithm) key:
certtool --generate-privkey --sec-param High --key-type=ecdsa --outfile <ca.key>
$ certtool --generate-privkey --sec-param High --key-type=ecdsa --outfile <ca.key>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The time for the key-generation process depends on the hardware and entropy of the host, the selected algorithm, and the length of the key.
Create a template file for a certificate.
Create a file with a text editor of your choice, for example:
vi <ca.cfg>
$ vi <ca.cfg>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the file to include the necessary certification details:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Create a certificate signed using the private key generated in step 1:
The generated <ca.crt> file is a self-signed CA certificate that you can use to sign other certificates for one year. <ca.crt> file is the public key (certificate). The loaded file <ca.key> is the private key. You should keep this file in safe location.
certtool --generate-self-signed --load-privkey <ca.key> --template <ca.cfg> --outfile <ca.crt>
$ certtool --generate-self-signed --load-privkey <ca.key> --template <ca.cfg> --outfile <ca.crt>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set secure permissions on the private key of your CA, for example:
chown <root>:<root> <ca.key> chmod 600 <ca.key>
# chown <root>:<root> <ca.key> # chmod 600 <ca.key>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Next steps
To use a self-signed CA certificate as a trust anchor on client systems, copy the CA certificate to the client and add it to the clients' system-wide truststore as
root
:trust anchor <ca.crt>
# trust anchor <ca.crt>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow See Chapter 3, Using shared system certificates for more information.
Verification
Display the basic information about your self-signed CA:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a certificate signing request (CSR), and use your CA to sign the request. The CA must successfully create a certificate based on the CSR, for example:
Generate a private key for your CA:
certtool --generate-privkey --outfile <example-server.key>
$ certtool --generate-privkey --outfile <example-server.key>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Open a new configuration file in a text editor of your choice, for example:
vi <example-server.cfg>
$ vi <example-server.cfg>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the file to include the necessary certification details:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Generate a request with the previously created private key:
certtool --generate-request --load-privkey <example-server.key> --template <example-server.cfg> --outfile <example-server.crq>
$ certtool --generate-request --load-privkey <example-server.key> --template <example-server.cfg> --outfile <example-server.crq>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Generate the certificate and sign it with the private key of the CA:
certtool --generate-certificate --load-request <example-server.crq> --load-ca-certificate <ca.crt> --load-ca-privkey <ca.key> --outfile <example-server.crt>
$ certtool --generate-certificate --load-request <example-server.crq> --load-ca-certificate <ca.crt> --load-ca-privkey <ca.key> --outfile <example-server.crt>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.7. Creating a private key and a CSR for a TLS server certificate using GnuTLS Copy linkLink copied to clipboard!
To obtain the certificate, you must create a private key and a certificate signing request (CSR) for your server first.
Procedure
Generate a private key on your server system, for example:
certtool --generate-privkey --sec-param High --outfile <example-server.key>
$ certtool --generate-privkey --sec-param High --outfile <example-server.key>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Use a text editor of your choice to prepare a configuration file that simplifies creating your CSR, for example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a CSR using the private key you created previously:
certtool --generate-request --template <example-server.cfg> --load-privkey <example-server.key> --outfile <example-server.crq>
$ certtool --generate-request --template <example-server.cfg> --load-privkey <example-server.key> --outfile <example-server.crq>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you omit the
--template
option, thecertool
utility prompts you for additional information, for example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Next steps
- Submit the CSR to a CA of your choice for signing. Alternatively, for an internal use scenario within a trusted network, use your private CA for signing. See the Using a private CA to issue certificates for CSRs with GnuTLS section for more information.
Verification
After you obtain the requested certificate from the CA, check that the human-readable parts of the certificate match your requirements, for example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.8. Creating a private key and a CSR for a TLS client certificate using GnuTLS Copy linkLink copied to clipboard!
To obtain the certificate, you must create a private key and a certificate signing request (CSR) for your client first.
Procedure
Generate a private key on your client system, for example:
certtool --generate-privkey --sec-param High --outfile <example-client.key>
$ certtool --generate-privkey --sec-param High --outfile <example-client.key>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Use a text editor of your choice to prepare a configuration file that simplifies creating your CSR, for example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a CSR using the private key you created previously:
certtool --generate-request --template <example-client.cfg> --load-privkey <example-client.key> --outfile <example-client.crq>
$ certtool --generate-request --template <example-client.cfg> --load-privkey <example-client.key> --outfile <example-client.crq>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you omit the
--template
option, thecerttool
utility prompts you for additional information, for example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Next steps
- Submit the CSR to a CA of your choice for signing. Alternatively, for an internal use scenario within a trusted network, use your private CA for signing. See Section 2.9, “Using a private CA to issue certificates for CSRs with GnuTLS” for more information.
Verification
Check that the human-readable parts of the certificate match your requirements, for example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.9. Using a private CA to issue certificates for CSRs with GnuTLS Copy linkLink copied to clipboard!
To enable systems to establish a TLS-encrypted communication channel, a certificate authority (CA) must provide valid certificates to them. If you have a private CA, you can create the requested certificates by signing certificate signing requests (CSRs) from the systems.
Prerequisites
- You have already configured a private CA. See Section 2.6, “Creating a private CA using GnuTLS” for more information.
- You have a file containing a CSR. You can find an example of creating the CSR in Section 2.7, “Creating a private key and a CSR for a TLS server certificate using GnuTLS” .
Procedure
Optional: Use a text editor of your choice to prepare an GnuTLS configuration file for adding extensions to certificates, for example:
vi <server-extensions.cfg>
$ vi <server-extensions.cfg> honor_crq_extensions ocsp_uri = "http://ocsp.example.com"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
certtool
utility to create a certificate based on a CSR, for example:certtool --generate-certificate --load-request <example-server.crq> --load-ca-privkey <ca.key> --load-ca-certificate <ca.crt> --template <server-extensions.cfg> --outfile <example-server.crt>
$ certtool --generate-certificate --load-request <example-server.crq> --load-ca-privkey <ca.key> --load-ca-certificate <ca.crt> --template <server-extensions.cfg> --outfile <example-server.crt>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow