Course: Network and System Administration

In Class Lab: 17

Name: Certificates and HTTPS

Objective: Use OpenSSL to create a private key and a self-signed certificate, read what is inside them, and protect the key. Then turn on HTTPS in the Nginx web server from Lab 13, open the firewall for it, and check that your laptop's browser receives your certificate.

In Lab 13 you built a web server that speaks plain HTTP, and your browser marked it Not secure. Anyone on the network between your browser and the server can read, or change, HTTP traffic. HTTPS is HTTP sent through an encrypted TLS tunnel. You will often see TLS called by its older name, SSL, as in the Nginx setting ssl_certificate.

TLS uses the same ideas as SSH in Lab 7:

SSH (Lab 7) HTTPS (this lab)
The server has a private host key it never shares. The web server has a private key it never shares.
The server sends its public host key. The web server sends its certificate: its public key, its name, and who vouches for it.
The first time, you are asked if you trust the key, and it is saved in known_hosts. Your browser trusts the certificate only if a certificate authority (CA) it already trusts signed it.

A certificate you sign yourself is self-signed. No CA vouches for it, so browsers warn about it, but the encryption is just as strong. Self-signed certificates are fine for learning, and for test or development servers where you control the server and client.

Note: This lab is done on your development server, NOT your production server. Connect with SSH from Terminal or PuTTY and log in as your own user. A few steps also use a browser or a command on your laptop.

This lab uses the web server from Lab 13 and the firewall commands from Lab 14. Nginx should be installed and running, and your laptop should be able to open your page with http://.

How to use the hints

Some steps give you every command. Others give you a Goal and ask you to work out the commands yourself. If you get stuck, open the hint. Once you have an answer, or if you are really stuck, open the solution and compare it with your own.

Lab overview

  1. Part 1: How a browser checks a certificate - Task 1
  2. Part 2: Create a key and a certificate, one step at a time - Task 2
  3. Part 3: Protect the private key - Task 3
  4. Part 4: Set your own defaults and use one command - Tasks 4 and 5
  5. Part 5: Turn on HTTPS in Nginx - Task 6
  6. Part 6: Open the firewall and use a browser - Tasks 7 and 8
  7. Part 7: Save your notes in GitLab - Task 9

If you get stuck, check the Troubleshooting section at the end of this lab.

The examples in this lab use the username angus and the UTM address 192.168.64.7. Always use your username and address. Your dates, serial numbers and versions will be different.


Part 1: How a browser checks a certificate

Before you make a certificate, look at a real one. curl -v (verbose) prints the details of the connection, including the certificate the server sent. Those details go to standard error, so send them into the pipe with 2>&1, as in Lab 5:

curl -vI https://www.washington.edu 2>&1 | grep "subject:\|issuer:\|expire date:\|verify ok"
* subject: C=US; ST=Washington; O=University of Washington; CN=www.washington.edu * expire date: Jan 13 23:59:59 2027 GMT * issuer: C=US; O=InCommon, LLC; CN=InCommon RSA OV SSL CA 3 * SSL certificate verify ok.
Line It means
subject: Who the certificate is for. CN is the Common Name, the site's name.
expire date: Certificates don't last forever. After this date, browsers refuse it.
issuer: The certificate authority that signed it. Here it is InCommon.
SSL certificate verify ok curl trusts the issuer, and the name you typed matches the certificate.

Two checks must both pass for a browser or curl to trust a site:

  1. Trust: the certificate was signed by a CA on the system's trusted list.
  2. Name: the name you typed in the address is listed in the certificate.

Your server's trusted list is one file of CA certificates. Count them:

grep -c "BEGIN CERTIFICATE" /etc/pki/tls/certs/ca-bundle.crt

You should see well over 100. Your browser has a similar list.

Task 1: Read another site's certificate

Goal: Find who issued the certificate for pce.uw.edu, and when it expires. Is it the same CA as www.washington.edu?

Hint

Change the address in the curl -vI command above. You only need the issuer: and expire date: lines.

Solution
curl -vI https://pce.uw.edu 2>&1 | grep "issuer:\|expire date:"
* expire date: May 3 23:59:59 2027 GMT * issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=GeoTrust TLS RSA CA G1

A different CA, GeoTrust (owned by DigiCert), signed it. Your server trusts both, because both are in ca-bundle.crt. The date you see may be later, because sites replace their certificates before they expire.


Part 2: Create a key and a certificate, one step at a time

Making a certificate takes three steps:

Step File What it is
1 private_certificate.key The private key. It stays secret on the server, like your SSH private key.
2 certificate_request.csr A certificate signing request (CSR): your public key and your server's name, ready for someone to sign. It is safe to share.
3 public_certificate.crt The certificate: the CSR, signed. Normally a CA signs it. Today you sign it with your own key.

Make a folder for this lab and move into it:

mkdir -p ~/inclass/lab-17/certs cd ~/inclass/lab-17/certs

Question: Do you need sudo for the openssl commands in this Part? If you used it, who would own the files?

Answer

No. You are writing files into your own folder. With sudo, root would own them, and you would need sudo again just to read your own key.

Step 1: The private key

openssl genrsa -out private_certificate.key 2048
Part What it does
openssl One program with many subcommands, like git in Lab 10.
genrsa Generates an RSA private key.
-out private_certificate.key The file to write.
2048 The key size in bits. 2048 is the smallest size you should use today.

Step 2: The certificate signing request

openssl req -new -key private_certificate.key -out certificate_request.csr
Part What it does
req Works with certificate requests.
-new Makes a new request.
-key private_certificate.key The private key the request is for. The request holds the matching public key.
-out certificate_request.csr The file to write.

OpenSSL asks for your server's details, called a distinguished name. A value in [ ] is the default, which you get by pressing Enter. For the Common Name, use the name from your Nginx configuration in Lab 13, itfdn.uw.edu. Press Enter to leave the email address, challenge password and optional company name blank:

You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:US State or Province Name (full name) []:Washington Locality Name (eg, city) [Default City]:Seattle Organization Name (eg, company) [Default Company Ltd]:UW PCE Organizational Unit Name (eg, section) []:IT FDN 140 Common Name (eg, your name or your server's hostname) []:itfdn.uw.edu Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:

Tip: The defaults XX, Default City and Default Company Ltd come from a configuration file. In Part 4 you change them to your own values.

Step 3: Sign the request

Sign the request with your own private key. That is what makes the certificate self-signed:

openssl x509 -req -days 365 -in certificate_request.csr -signkey private_certificate.key -out public_certificate.crt
Certificate request self-signature ok subject=C=US, ST=Washington, L=Seattle, O=UW PCE, OU=IT FDN 140, CN=itfdn.uw.edu
Part What it does
x509 Works with certificates. X.509 is the name of the certificate format.
-req The input is a request, not a certificate.
-days 365 The certificate is valid for one year from now.
-in certificate_request.csr The request to sign.
-signkey private_certificate.key Sign it with this key. A CA would use its key here instead.
-out public_certificate.crt The certificate to write.

Check your files

ls -la

ls -la output showing certificate_request.csr, private_certificate.key with rw------- permissions, and public_certificate.crt

You should have three files. Look at the permissions. OpenSSL made the private key -rw------- (600), readable only by you, as chmod 600 did for your SSH files in Lab 7. The request and the certificate are public, so anyone may read them.

All three are plain text files, in a format called PEM. Look at the key and the certificate:

cat private_certificate.key cat public_certificate.crt

cat of the private key, which starts with BEGIN PRIVATE KEY, and the certificate, which starts with BEGIN CERTIFICATE

The key starts with -----BEGIN PRIVATE KEY----- and the certificate with -----BEGIN CERTIFICATE-----. The lines in between are the data, encoded as text. openssl can decode them for you. Check that the key is valid:

openssl rsa -in private_certificate.key -check -noout
RSA key ok

-noout stops openssl from printing the key again after it checks it. Now decode the certificate. It is long, so read it with less and press q to quit:

openssl x509 -in public_certificate.crt -text -noout | less
Field It means
Serial Number A number that identifies this certificate. You look for it in your browser in Part 6.
Issuer Who signed it. Here it is the same as the Subject, which is what self-signed means.
Validity Not Before and Not After: the 365 days you asked for.
Subject Who the certificate is for: the values you typed in Step 2.
Subject Public Key Info The public key that matches private_certificate.key.

Note: If you lose the private key, the certificate is useless, because nobody can prove they own it. It is the same with your SSH key in Lab 7: the public key alone can't log you in. If someone else copies the private key, they can pretend to be your server. Guard it.

Task 2: Print only the fields you need

Goal: Print just the subject, the issuer, the two dates and the serial number of public_certificate.crt, on a few short lines, without less.

Hint
Solution
openssl x509 -in public_certificate.crt -noout -subject -issuer -dates -serial
subject=C=US, ST=Washington, L=Seattle, O=UW PCE, OU=IT FDN 140, CN=itfdn.uw.edu issuer=C=US, ST=Washington, L=Seattle, O=UW PCE, OU=IT FDN 140, CN=itfdn.uw.edu notBefore=Nov 12 18:02:11 2026 GMT notAfter=Nov 12 18:02:11 2027 GMT serial=5E0C3A9B1F7D24C68E0A1B2C3D4E5F60718293A4

The subject and issuer match, so the certificate is self-signed. The dates are one year apart.

Another way: -enddate prints only the expiry date. Many monitoring scripts use it to warn before a certificate expires.


Part 3: Protect the private key

Permissions protect the key from other users, but not from you, or from root. One wrong rm or > (Lab 5) and it is gone. Linux has file attributes that go beyond permissions. The immutable attribute, i, means nobody can change, rename or delete the file, not even root, until the attribute is removed.

Command What it does
chattr Changes a file's attributes. +i adds immutable, -i removes it. Only root may set or remove i.
lsattr Lists a file's attributes. An i shows the file is immutable.

Try it on an empty test file first:

cd ~/inclass/lab-17 touch testfile.txt chattr +i testfile.txt
chattr: Operation not permitted while setting flags on testfile.txt

You own the file, but only root can make it immutable. Use sudo, then check:

sudo chattr +i testfile.txt lsattr testfile.txt
----i----------------- testfile.txt

The number of dashes can differ. The i is what matters. ls -l doesn't show attributes, so the permissions look the same as before. Now try to change the file and delete it, first as yourself, then as root:

echo "hello" >> testfile.txt rm testfile.txt sudo rm testfile.txt
-bash: testfile.txt: Operation not permitted rm: remove write-protected regular empty file 'testfile.txt'? y rm: cannot remove 'testfile.txt': Operation not permitted rm: cannot remove 'testfile.txt': Operation not permitted

Even root can't delete it. Remove the attribute, and you can delete it yourself without sudo, because you own it and the folder it is in:

sudo chattr -i testfile.txt rm testfile.txt ls -l

Question: Root can always run sudo chattr -i first. So what does the immutable attribute really protect against?

Answer

Mistakes and careless scripts. A command such as rm -rf, or a program that writes over a file, fails instead of destroying it. To change the file, someone has to remove the attribute on purpose first.

Task 3: Make your private key immutable

Goal: Make private_certificate.key immutable and prove it with lsattr. Then show that root can't delete it, and that openssl genrsa can't write over it. Leave it immutable.

Hint
Solution
cd ~/inclass/lab-17/certs sudo chattr +i private_certificate.key lsattr private_certificate.key sudo rm private_certificate.key openssl genrsa -out private_certificate.key 2048

lsattr shows the i, sudo rm says Operation not permitted, and openssl can't open the file for writing. The key is safe. Here is the same idea from start to finish, including removing the attribute again:

ls, chattr +i, sudo rm fails, rm fails, chattr -i, then rm works on private_certificate.key

Note: Don't run the last two commands in the screenshot. Your key stays immutable. If you ever need to delete this folder, run sudo chattr -i on the key first.


Part 4: Set your own defaults and use one command

You typed the same details in Part 2 that you would type for every certificate. OpenSSL reads its defaults, such as Default City, from a configuration file called openssl.cnf. You will copy it and put your own values in the copy.

Find which package has a command

First, you need to find the file. The locate command finds files by name, but it isn't installed. If you don't know a command's package, dnf provides tells you. Try it with dig, which you installed in Lab 13:

sudo dnf provides "*/bin/dig"
bind-utils-32:9.16.23-24.el9_5.aarch64 : Utilities for querying DNS name servers Repo : @System Matched from: Filename : /usr/bin/dig ...
Part What it does
dnf provides Searches every package for a file.
"*/bin/dig" The file to find. The * matches any folder in front, so it finds /usr/bin/dig and /usr/local/bin/dig too. The quotes stop the shell from expanding the * itself.
Repo : @System The package is already installed. Other lines name the repository it comes from.

This is the package you installed in Lab 13: bind-utils.

Task 4: Find openssl.cnf

Goal: Find the package that provides the locate command and install it. Then use locate to find where openssl.cnf is.

Hint
Solution
sudo dnf provides "*/locate" sudo dnf install -y mlocate sudo updatedb locate openssl.cnf
Command What it does
dnf provides Names the package mlocate. If yours names a different package, such as plocate, install that one.
updatedb Builds the database of file names. It runs again by itself once a day, so locate doesn't find files made since.
locate openssl.cnf Lists every path with openssl.cnf in it.

Among the results is the file you need:

/etc/pki/tls/openssl.cnf

Another way: openssl version -d prints the folder OpenSSL keeps its configuration in: OPENSSLDIR: "/etc/pki/tls".

Change the defaults in a copy

Don't edit the system file. Copy it into your folder:

cd ~/inclass/lab-17/certs cp /etc/pki/tls/openssl.cnf openssl.cnf vim openssl.cnf

In Vim, type /req_distinguished_name and press Enter to jump to the section, then n to reach the second match, the line [ req_distinguished_name ]. Lines that end in _default are the defaults. A # turns a line off, as in Lab 13. Change or add the _default lines so the start of the section looks like this. Lines not shown here, such as the 1.organizationName comments and the emailAddress lines, stay as they are:

[ req_distinguished_name ] countryName = Country Name (2 letter code) countryName_default = US countryName_min = 2 countryName_max = 2 stateOrProvinceName = State or Province Name (full name) stateOrProvinceName_default = Washington localityName = Locality Name (eg, city) localityName_default = Seattle 0.organizationName = Organization Name (eg, company) 0.organizationName_default = UW PCE organizationalUnitName = Organizational Unit Name (eg, section) organizationalUnitName_default = IT FDN 140 commonName = Common Name (eg, your name or your server\'s hostname) commonName_default = itfdn.uw.edu commonName_max = 64
Change Why
XX, Default City, Default Company Ltd Replaced with your values.
#stateOrProvinceName_default, #organizationalUnitName_default Removed the # and added a value, so the line is used.
commonName_default A new line, so you don't have to type the name each time.

Save and quit with :wq. Check your changes against the original with diff, as in Lab 8:

diff /etc/pki/tls/openssl.cnf openssl.cnf

Every line that starts with < is the original, and every line that starts with > is yours. Here is the whole process:

Copying openssl.cnf and editing the req_distinguished_name defaults in Vim

One command for everything

Now make a second key and certificate with one command. It does all three steps from Part 2, and it reads your defaults. Type the whole command, including the \ at the end of the first line, which continues the command on the next line, as in Lab 9:

openssl req -x509 -config openssl.cnf -noenc -days 365 -newkey rsa:2048 \ -keyout private_key_custom.key -out public_cert_custom.crt -addext "subjectAltName=DNS:itfdn.uw.edu,DNS:localhost"

Press Enter at each question. Your defaults are already in the [ ].

Part What it does
req -x509 Makes a self-signed certificate instead of a request. No .csr file is left behind.
-config openssl.cnf Uses your copy, in this folder, instead of /etc/pki/tls/openssl.cnf.
-noenc No encryption on the private key. A web server must read its key when it starts, with nobody there to type a passphrase. Older guides use -nodes for this.
-days 365 Valid for one year.
-newkey rsa:2048 Makes a new 2048-bit RSA key at the same time, like genrsa.
-keyout private_key_custom.key Where to write the new private key.
-out public_cert_custom.crt Where to write the certificate.
-addext "subjectAltName=..." Adds a Subject Alternative Name (SAN) list: every name this certificate is good for.

Note: Browsers and curl no longer check the Common Name. They only check the SAN list. A certificate without one fails the name check from Part 1, even when the CN is right. Your first certificate has no SAN. This one lists itfdn.uw.edu and localhost.

Task 5: Check your new certificate

Goal: Show that the new certificate has your defaults in its subject, and both names in its SAN list. Then show that the new key is valid, and check its permissions.

Hint
Solution
openssl x509 -in public_cert_custom.crt -noout -subject -ext subjectAltName openssl rsa -in private_key_custom.key -check -noout ls -l
subject=C=US, ST=Washington, L=Seattle, O=UW PCE, OU=IT FDN 140, CN=itfdn.uw.edu X509v3 Subject Alternative Name: DNS:itfdn.uw.edu, DNS:localhost RSA key ok

ls -l shows private_key_custom.key as -rw-------, like the first key.

Going further: run the same -ext subjectAltName command on public_certificate.crt. It prints no Subject Alternative Name list, because the first certificate has none.


Part 5: Turn on HTTPS in Nginx

Nginx needs the certificate and its private key in a place it can read. The usual place on Red Hat is under /etc/pki. Your original configuration from Lab 13, /etc/nginx/nginx.conf.orig, has a commented-out example that uses /etc/pki/nginx, so use that.

sudo mkdir -p /etc/pki/nginx/private sudo chmod 700 /etc/pki/nginx/private sudo cp ~/inclass/lab-17/certs/public_cert_custom.crt /etc/pki/nginx/ sudo cp ~/inclass/lab-17/certs/private_key_custom.key /etc/pki/nginx/private/ sudo ls -lZ /etc/pki/nginx /etc/pki/nginx/private
/etc/pki/nginx: total 4 drwx------. 2 root root unconfined_u:object_r:cert_t:s0 36 Nov 12 18:20 private -rw-r--r--. 1 root root unconfined_u:object_r:cert_t:s0 1428 Nov 12 18:20 public_cert_custom.crt /etc/pki/nginx/private: total 4 -rw-------. 1 root root unconfined_u:object_r:cert_t:s0 1704 Nov 12 18:20 private_key_custom.key
You see It means
root root sudo cp made new files owned by root.
drwx------ on private Only root can even list the keys.
-rw------- on the key cp kept the 600 permissions of your copy. Only root can read it.
cert_t The SELinux type from Lab 16 for certificates. The web server's type, httpd_t, may read cert_t files.

Question: The Nginx workers run as the user nginx (Lab 13), and the key is readable only by root. How can Nginx read it?

Answer

The main Nginx process runs as root. It reads the certificate and key when it starts or reloads, and only then starts the workers as nginx. The workers never need to open the key file.

Common Pitfall: Use cp, not mv. In Lab 16 you saw that mv keeps a file's SELinux label. A key moved from your home folder keeps user_home_t, and Nginx is denied when it reloads. If that happens, fix the label with sudo restorecon -v on the file.

Add an HTTPS server block

Back up your configuration from Lab 13, then edit it:

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.lab13 sudo vim /etc/nginx/nginx.conf

Add a second server block, for port 443, inside the http { } block, after the first server block. Your whole file should look like this:

user nginx; worker_processes 1; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { worker_connections 1024; } http { access_log /var/log/nginx/access.log; server { listen 80; server_name itfdn.uw.edu; root /usr/share/nginx/html; location / { } } server { listen 443 ssl http2; server_name itfdn.uw.edu; root /usr/share/nginx/html; ssl_certificate /etc/pki/nginx/public_cert_custom.crt; ssl_certificate_key /etc/pki/nginx/private/private_key_custom.key; location / { } } }
New line What it does
listen 443 ssl http2; Answer on port 443, the HTTPS port, using TLS. http2 allows the newer, faster HTTP/2.
server_name itfdn.uw.edu; The same name as the HTTP site, and the name in your certificate.
root The same web root, so both sites show the same pages.
ssl_certificate The certificate Nginx sends to browsers.
ssl_certificate_key The private key that matches it. The paths must match the files you copied exactly.

Tip: Every { needs a matching }. In Vim, put the cursor on a { and press % to jump to its partner. Press % again to jump back.

Test, reload and check, as in Lab 13:

sudo nginx -t sudo systemctl reload nginx sudo ss -tlnp | grep nginx
LISTEN 0 511 0.0.0.0:443 0.0.0.0:* users:(("nginx",pid=1290,fd=7),("nginx",pid=1066,fd=7)) LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=1290,fd=6),("nginx",pid=1066,fd=6))

Nginx now listens on both 80 and 443. Ask for your page over HTTPS:

curl -I https://localhost
curl: (60) SSL certificate problem: self-signed certificate More details here: https://curl.se/docs/sslcerts.html ...

That is the trust check from Part 1 failing. No CA on your server's list signed your certificate. Tell curl to skip the checks:

curl -k -I https://localhost
HTTP/2 200 server: nginx/1.20.1 content-type: text/html ...
Command What it does
curl -I https://localhost Refuses to connect, because it can't trust the certificate.
curl -k -I https://localhost -k (--insecure) skips both checks. The traffic is still encrypted, but you don't know who you are talking to.
HTTP/2 200 Your page, sent with HTTP/2 inside TLS.

Warning: Only use -k on your own test servers. On any other site, a certificate error can mean someone is pretending to be that site.

Task 6: Make curl trust your certificate

Goal: Make curl -I https://localhost succeed without -k, by telling curl to trust your certificate. Then try the same thing with https://127.0.0.1, and explain why it fails.

Hint
Solution
curl --cacert ~/inclass/lab-17/certs/public_cert_custom.crt -I https://localhost curl --cacert ~/inclass/lab-17/certs/public_cert_custom.crt -I https://127.0.0.1
HTTP/2 200 ... curl: (60) SSL: no alternative certificate subject name matches target host name '127.0.0.1'

--cacert adds your certificate to the list curl trusts, so the trust check passes. With localhost, the name check passes too. 127.0.0.1 isn't in the SAN list, so the name check fails, even though it is the same server and the same certificate.

Going further: --resolve tells curl which address to use for a name, like the /etc/hosts line in Lab 13, Task 6. This uses the name itfdn.uw.edu, which is in the SAN list, and connects to your own server:

curl --cacert ~/inclass/lab-17/certs/public_cert_custom.crt --resolve itfdn.uw.edu:443:127.0.0.1 -I https://itfdn.uw.edu

Part 6: Open the firewall and use a browser

So far every request has come from the server itself, and the firewall doesn't apply to those (Lab 13). Now try from your laptop.

Hypervisor Do this first HTTPS address from your laptop
UTM (Mac) Nothing. https://192.168.64.7, using your IP address
VirtualBox (Windows) Add a rule, as you did for HTTP in Lab 13: VM Settings, Network, Port Forwarding. Name https, protocol TCP, host port 8443, guest port 443. Leave the IP fields blank. https://127.0.0.1:8443

VirtualBox port forwarding rule from host port 8443 to guest port 443

On your laptop, open Terminal or Command Prompt, and try your HTTPS address with curl, as in Lab 14. Use -k, because your laptop doesn't trust your certificate either:

Hypervisor Command on your laptop
UTM (Mac) curl -k -I --max-time 5 https://192.168.64.7
VirtualBox (Windows) curl -k -I --max-time 5 https://127.0.0.1:8443

It should fail. Your server is listening on 443, but the firewall only allows http and ssh.

Task 7: Allow HTTPS through the firewall

Goal: Permanently allow the https service in your server's zone, make it active, and prove it with curl from your laptop.

Hint

Lab 14, Task 5 added https and then removed it. This time, keep it.

Solution

On the server:

sudo firewall-cmd --get-active-zones sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload sudo firewall-cmd --zone=public --list-services
cockpit dhcpv6-client http https ssh

Then run the same curl -k -I on your laptop. It now shows HTTP/2 200, or HTTP/1.1 200 on a laptop whose curl doesn't use HTTP/2.

Look at it in a browser

Type your HTTPS address into a browser on your laptop. Remember the https://. The browser warns you, because your certificate isn't signed by a CA it trusts, and because you typed an address that isn't in the SAN list:

Chrome warning: Your connection is not private, NET::ERR_CERT_INVALID, with the Advanced button

Each browser has its own way past the warning:

Browser To continue
Chrome or Edge Advanced, then Proceed to ... (unsafe).
Firefox Advanced, then Accept the Risk and Continue.
Safari Show Details, then visit this website, then Visit Website. It may ask for your Mac password.

You should see your page from Lab 13. The address bar still says Not secure, or shows a crossed-out lock: the traffic is encrypted, but the browser can't prove who sent it.

Task 8: Match the certificate in your browser

Goal: Prove that the certificate your browser received is the one you made. Find the serial number and the SHA-256 fingerprint in your browser, and compare them with the same values from openssl on your server.

Hint
Solution

On the server:

openssl x509 -in /etc/pki/nginx/public_cert_custom.crt -noout -serial -fingerprint -sha256
serial=3C9F0E1A6B2D47F58A01C2E3D4B5A6978F10E2D3 sha256 Fingerprint=8A:41:0C:...:F7:22

In the browser, open the certificate viewer and compare. The browser may show the serial number with colons, in pairs, such as 3C:9F:0E:.... The digits are the same. Matching fingerprints prove it is your certificate, just as you matched SSH key fingerprints in Lab 7, Task 2.


Part 7: Save your notes in GitLab

Add what you have learned to your GitLab repository from Lab 10:

cd ~/git/gitlab/itfdn-140-inclass-$USER git pull echo "# Certificates and HTTPS The following are notes and tips related to **openssl**, **certificates**, **chattr** and **HTTPS in Nginx**. " >> tls-tips.md cat tls-tips.md

Goal: Add at least three tips or commands to tls-tips.md that you would want next time. Add a link to the file at the end of README.md. Commit both files in one commit, and push. In GitLab, click the link in your README and check that it opens your notes.

Hint
Solution
vim tls-tips.md

Add your own tips, then save with :wq. For example:

## Make a self-signed certificate `openssl req -x509 -noenc -days 365 -newkey rsa:2048 -keyout site.key -out site.crt -addext "subjectAltName=DNS:name"` ## Read a certificate `openssl x509 -in site.crt -noout -subject -issuer -dates -serial`. A remote one: `curl -vI https://site 2>&1 | grep "subject:\|issuer:"` ## Protect a file, even from root `sudo chattr +i file`, check with `lsattr file`, undo with `sudo chattr -i file`.

Add the link, then check, commit and push:

echo "" >> README.md echo "[Certificates and HTTPS](tls-tips.md)" >> README.md git status git diff git add README.md tls-tips.md git commit -m "docs: add certificate and HTTPS tips and link from README" git push git log --oneline

Lab summary

You have now:

Your web server now answers on both http and https.

Questions to consider

  1. Which file must never leave the server: the .key, the .csr or the .crt? Why is it safe to share the other two?
  2. Your browser warned you about your certificate. What would you need for the warning to go away for everyone who visits your site?
  3. curl --cacert worked with https://localhost but not with https://127.0.0.1. Which of the two checks failed, and why?
  4. Is traffic sent with curl -k encrypted? What does -k give up?
  5. Root can remove the immutable attribute. So why use it at all?
  6. Why does the private key for a web server usually have no passphrase, when your SSH key from Lab 7 does?
  7. Your site still answers on plain http. What could you change so visitors always end up on https?

Troubleshooting

Q. openssl says "Can't open ... for writing, Permission denied".

A. You are in a folder you can't write to, or you made the files with sudo earlier. Run cd ~/inclass/lab-17/certs and check the owners with ls -l.

Q. openssl says "Can't open ... for writing, Operation not permitted".

A. The file is immutable. Check with lsattr, and remove the attribute with sudo chattr -i if you really mean to replace it.

Q. I can't delete ~/inclass/lab-17, even with sudo rm -r.

A. private_certificate.key is immutable from Task 3. Run sudo chattr -i ~/inclass/lab-17/certs/private_certificate.key first.

Q. openssl req -config openssl.cnf doesn't show my defaults.

A. Check that you are in the folder that has your copy, with ls openssl.cnf, and that your _default lines don't start with #. Use diff /etc/pki/tls/openssl.cnf openssl.cnf to see what you changed.

Q. dnf provides "*/locate" doesn't name mlocate.

A. Install the package it does name. If locate then finds nothing, run sudo updatedb again.

Q. sudo nginx -t says "cannot load certificate" or "No such file or directory".

A. A path in ssl_certificate or ssl_certificate_key doesn't match a real file. Compare the two paths with sudo ls -l /etc/pki/nginx /etc/pki/nginx/private.

Q. sudo nginx -t says "key values mismatch".

A. The key isn't the one that goes with the certificate, for example private_certificate.key with public_cert_custom.crt. Copy the matching pair from Part 4.

Q. sudo nginx -t says the configuration is fine, but systemctl reload nginx fails, or HTTPS doesn't work.

A. sudo nginx -t runs in your own SELinux domain, unconfined_t (id -Z in Lab 16), but the service runs as httpd_t, so SELinux can block the service and not the test. Check sudo ausearch -m avc -ts recent, as in Lab 16. If the key or certificate has the type user_home_t, you used mv. Fix it with sudo restorecon -v /etc/pki/nginx/public_cert_custom.crt /etc/pki/nginx/private/private_key_custom.key, then reload.

Q. nginx -t warns that the "http2" parameter is deprecated.

A. Newer versions of Nginx want listen 443 ssl; and a separate line, http2 on;. Your setup still works with the warning.

Q. curl https://localhost says "Failed to connect to localhost port 443".

A. Nothing listens on 443. Check sudo ss -tlnp | grep nginx. If 443 is missing, check that the new server block is inside http { }, that you ran sudo systemctl reload nginx, and look at systemctl status nginx.

Q. curl --cacert says "no alternative certificate subject name matches".

A. The name in the address isn't in the SAN list. Check the list with openssl x509 -in /etc/pki/nginx/public_cert_custom.crt -noout -ext subjectAltName. If there is no list, you forgot -addext in Part 4. Make the certificate again, copy it and reload Nginx.

Q. From my laptop, HTTPS says "No route to host" or times out.

A. The firewall is blocking it. Check sudo firewall-cmd --zone=public --list-services for https, and that you ran --reload. On VirtualBox, check the https rule and use host port 8443.

Q. My browser shows "This site can't provide a secure connection" or "SSL_ERROR_RX_RECORD_TOO_LONG".

A. The browser is speaking HTTPS to a port that speaks HTTP. On VirtualBox, check that the https rule points to guest port 443, not 80. Also check the address starts with https://.

Q. git push is rejected.

A. Your GitLab repository has a change you don't have yet. Run git pull, then git push again, as in Lab 10.

If you can't solve a problem, post it in this week's Teams channel, or reach out by email for help.


Lab complete