[Day 10] 1. PKI Lab using OpenSSL in CLI
Lab Objective:
- Set up your own Certificate Authority (CA) using OpenSSL command-line tools.
- Configure an HTTPS website using certificates generated by the CA.
Prerequisite:
Ensure your DNS is properly configured and functional (set it up yourself).
Machine Structure:
plaintext- shuhari.local/ - rootca.shuhari.local (192.168.100.51) -> Root CA - subca.shuhari.local (192.168.100.52) -> Subordinate CA - deb3.shuhari.local (www.shuhari.local) (192.168.100.53) -> Web server
- Install on all three machines.
openssl - Configure DNS for name resolution.
Note: OpenSSL is an open-source library, similar to XCA.
Steps to Follow:
1. Install Required Packages on All Machines:
Run the following command on all three machines to install OpenSSL and Tree:
bashsudo apt-get install -y openssl tree vim
2. Verify DNS Configuration:
Use
nslookupplaintextrootca.shuhari.local subca.shuhari.local deb3.shuhari.local www.shuhari.local rootca subca deb3 www
- Ensure all entries return correct results.
Exam Tip: You may be asked to configure reverse DNS lookup, so set it up here as well.
3. Verify Hostname Commands:
Run these commands and ensure they work as expected:
bashhostname hostname -f hostname -s hostname -d
4. Create the File Structure:
Perform these steps in
Root CA-
Create a folder for the CA and required subdirectories:
bashmkdir -p /home/shuhari/ca cd /home/shuhari/ca mkdir -p certs crl newcerts private subca/csr subca/certs tree ca -
Secure the
folder, as it will store private keys:privatebashchmod 700 private/- Folder Purpose:
- : Stores certificates.
certs - : Contains the Certificate Revocation List.
crl - : Stores new certificates.
newcerts
- Folder Purpose:
5. Create the CA Database:
While in the
cabashtouch index.txt
touch index.txt.attr
echo 1000 > serial
echo 1000 > crlnumber
- Files Created:
- : Tracks issued certificates.
index.txt - : Additional attributes for the database.
index.txt.attr - : Stores the next certificate serial number (starting with 1000).
serial - : Tracks the CRL serial number.
crlnumber
6. Configure rootca.cnf
:
rootca.cnfDownload the
rootca.cnfcabashvim rootca.cnf
- Update the path from
dirto/root/ca./home/shuhari/ca
Here’s the next part of your notes, simplified and organized while keeping all details intact:
7. Generate the Private Key
On the Root CA machine, the private key should be stored inside the
privatecabashopenssl genrsa -aes256 -out private/ca.key.pem 4096
chmod 400 private/ca.key.pem
- Explanation:
- The private key is generated using the RSA algorithm with AES-256 encryption.
- Key size: 4096 bits.
- A password will be required for key generation.
- The command ensures the private key is secured with read-only permissions.
chmod
8. Create the Self-Signed Certificate
On the Root CA machine, generate the self-signed certificate:
bashcd ca
openssl req -config rootca.cnf -key private/ca.key.pem -new -x509 -days 7300 -sha256 -extensions v3_ca -out certs/ca.cert.pem
-
Options:
- : Requests OpenSSL to create a certificate.
req - : Specifies the configuration file (
-config).rootca.cnf - : Path to the private key.
-key - : Creates a new certificate.
-new - : Indicates the certificate standard being used.
-x509 - : Certificate validity period (7300 days = 20 years).
-days - : Specifies the hashing algorithm.
-sha256 - : Specifies the template being used (
-extensions).v3_ca - : File where the certificate will be saved.
-out
-
Input Required: Fill in the requested details:
plaintextPassword: Enter the private key's password. Country Name: IN State: MH Locality Name: Pune Organization Name: ShuHaRi Labs Organizational Unit Name: IS Common Name: ShuHaRi Root CA Email Address: info@shuhari.local -
Result: The Root CA is now configured.
9. Set Up the Sub CA Database
On the Sub CA machine, create the required file structure and database files:
bashmkdir subca
cd subca
mkdir certs crl csr newcerts private
chmod 700 private/
touch index.txt
touch index.txt.attr
echo 1000 > serial
echo 1000 > crlnumber
- Folder Purpose:
- : Stores issued certificates.
certs - : Contains the Certificate Revocation List.
crl - : Stores Certificate Signing Requests.
csr - : Contains newly issued certificates.
newcerts - : Stores private keys (secured with
private).chmod 700
10. Download the Sub CA Configuration File (subca.cnf
)
subca.cnf-
Obtain the
file from the server and place it inside thesubca.cnffolder.subca -
Modify the file to use the correct directory path:
bashvim subca.cnfChange the
path fromdirto/root/subca./home/shuhari/subca
11. Generate the Sub CA Private Key
On the Sub CA machine, generate the private key:
bashopenssl genrsa -aes256 -out private/subca.key.pem 4096
chmod 400 private/subca.key.pem
- Explanation:
- The private key is generated using the RSA algorithm with AES-256 encryption.
- Key size: 4096 bits.
- A password will be required for key generation.
- The command ensures the private key is secured with read-only permissions.
chmod
Here’s the next part of your notes, simplified and organized:
12. Create the Certificate Signing Request (CSR)
On the Sub CA machine, generate a CSR:
bashopenssl req -config subca.cnf -new -sha256 -key private/subca.key.pem -out csr/subca.csr.pem
-
Explanation:
- : Requests OpenSSL to create a certificate signing request.
req - : Specifies the configuration file (
-config).subca.cnf - : Generates a new CSR.
-new - : Uses the SHA-256 hashing algorithm.
-sha256 - : Specifies the private key for signing.
-key - : Specifies the output file for the CSR.
-out
-
Input: Enter the same details used when setting up the Root CA.
13. Export the CSR to Root CA
Copy the CSR file from the Sub CA machine to the Root CA machine:
bashscp /home/shuhari/subca/csr/subca.csr.pem shuhari@<rootca_ip>:/home/shuhari/ca/subca/csr/
14. Sign the CSR from Root CA
On the Root CA machine, sign the Sub CA CSR to create the certificate:
bashopenssl ca -config rootca.cnf -extensions v3_intermediate_ca -days 3650 -notext -md sha256 -in subca/csr/subca.csr.pem -out subca/certs/subca.cert.pem
- Explanation:
- : Requests OpenSSL to act as the Certificate Authority.
ca - : Specifies the configuration file (
-config).rootca.cnf - : Uses the template
-extensions.v3_intermediate_ca - : Specifies the certificate validity period (3650 days = 10 years).
-days - : Disables additional text output.
-notext - : Uses the SHA-256 hashing algorithm.
-md sha256 - : Input file (Sub CA CSR).
-in - : Output file (Sub CA certificate).
-out
15. Verify the Changes
On the Root CA machine, check the database files to confirm the CSR was signed:
bashcat index.txt
cat serial
cat serial.old
cat index.txt.attr
16. Verify the Sub CA Certificate
Check if the generated Sub CA certificate matches the one in
newcertsbashmd5sum subca/certs/subca.cert.pem newcerts/1000.pem
- Expected Result: Both should display the same MD5 checksum.
17. Export the Sub CA Certificate Back
Copy the Sub CA certificate from the Root CA machine to the Sub CA machine:
bashscp /home/shuhari/ca/subca/certs/subca.cert.pem shuhari@<subca_ip>:/home/shuhari/subca/certs/
18. Create the Private Key for Deb3
On the Deb3 machine, generate a private key:
bashmkdir certs
cd certs
openssl genrsa -out www.shuhari.local.key.pem 2048
- Explanation:
- Generates a private key using the RSA algorithm.
- Key size: 2048 bits.
19. Generate a CSR for Deb3
-
Download the
file on the Deb3 machine.subca.cnf -
Create a CSR inside the
directory:certsbashopenssl req -config subca.cnf -key www.shuhari.local.key.pem -new -sha256 -out www.shuhari.local.csr.pem
- Input: Provide the required details, altering these:
- :
Common Namewww.shuhari.local - :
Email Addressinfo@shuhari.local
Important: The Common Name (CN) must match the web server name.
- Result: The CSR for the web server is now ready.
Here’s the continuation of your organized and simplified notes:
20. Copy the CSR to Sub CA
On the Deb3 machine, copy the CSR file to the Sub CA machine:
bashscp /home/shuhari/certs/www.shuhari.local.csr.pem shuhari@<subca_ip>:/home/shuhari/subca/csr
21. Generate the Signed Certificate for the Web Server
On the Sub CA machine, sign the web server’s CSR using the Sub CA’s private key:
bashcd subca
openssl ca -config subca.cnf -extensions server_cert -days 375 -notext -md sha256 -in csr/www.shuhari.local.csr.pem -out certs/www.shuhari.local.cert.pem
-
Explanation:
- : OpenSSL acts as a Certificate Authority.
ca - : Specifies the configuration file (
-config).subca.cnf - : Uses the
-extensionstemplate.server_cert - : Sets the certificate validity period to 375 days.
-days - : Disables extra text output.
-notext - : Uses SHA-256 for hashing.
-md sha256 - : Input CSR file.
-in - : Output signed certificate.
-out
-
Result: A signed certificate for the web server is generated.
22. Copy the Signed Certificate Back to Deb3
On the Sub CA machine, copy the signed certificate to the Deb3 machine:
bashscp /home/shuhari/subca/certs/www.shuhari.local.cert.pem shuhari@<deb3_ip>:/home/shuhari/certs/www.shuhari.local.cert.pem
23. Copy Root CA and Sub CA Certificates to Deb3
-
Copy the Root CA certificate:
bashscp /home/shuhari/ca/certs/ca.cert.pem shuhari@<deb3_ip>:/home/shuhari/certs/ -
Copy the Sub CA certificate:
bashscp /home/shuhari/subca/certs/subca.cert.pem shuhari@<deb3_ip>:/home/shuhari/certs/
24. Create the Bundle Certificate
On the Deb3 machine, combine all certificates into a bundle to establish the chain of trust:
bashcd certs
cat www.shuhari.local.cert.pem subca.cert.pem ca.cert.pem > bundle.cert.pem
cat bundle.cert.pem
-
Explanation:
- The bundle certificate links the web server certificate with the Sub CA and Root CA certificates, forming a complete chain of trust.
- Use to view the content of the
cat.bundle.cert.pem
-
Result: PKI configuration is complete.
25. Configure the HTTP Server on Deb3
-
Verify if Apache2 is installed:
bashsudo which apache2 -
Install Apache2 if not already installed:
bashsudo apt-get install apache2 -
Create a directory for SSL files:
bashsudo mkdir -p /etc/apache2/ssl -
Copy the bundle certificate and private key to the SSL directory:
bashsudo cp bundle.cert.pem /etc/apache2/ssl/ sudo cp www.shuhari.local.key.pem /etc/apache2/ssl/ -
Secure the SSL files by limiting permissions:
bashsudo chmod 600 /etc/apache2/ssl/*
26. Implement Encryption for the Web Server
-
Enable the SSL module in Apache2:
bashsudo a2enmod ssl -
Check the
andsites-availabledirectories:sites-enabledbashsudo ls -l /etc/apache2/sites-available/ sudo ls -l /etc/apache2/sites-enabled/-
You’ll notice that the
file exists indefault-ssl.confbut not insites-available.sites-enabled -
Use the following command to enable the site, creating a symbolic link from
tosites-available:sites-enabledbashsudo a2ensite default-ssl
-
-
Verify that the site is now enabled:
bashsudo ls -l /etc/apache2/sites-enabled/ -
Edit the
file:default-ssl.confbashsudo vim /etc/apache2/sites-available/default-ssl.conf-
Add the following line on the 3rd line:
plaintextServerName www.shuhari.local:443 -
Update the paths for the certificate and key files:
plaintextSSLCertificateFile /etc/apache2/ssl/bundle.cert.pem SSLCertificateKeyFile /etc/apache2/ssl/www.shuhari.local.key.pem
-
-
Restart the Apache2 service to apply the changes:
bashsudo systemctl restart apache2 sudo systemctl status apache2
27. Test the Website
-
Open a browser and try accessing the website:
plaintexthttps://www.shuhari.local -
You will encounter a warning for an insecure website because the browser doesn’t yet trust your certificate. However, you can inspect the certificate trust chain in the browser.
28. Import the Root CA Certificate in Windows
-
Copy the
certificate to your Windows machine.Root CA -
Import the certificate:
-
Open Manage User Certificates.
-
Navigate to:
plaintextTrusted Root Certification Authorities → Certificates -
Right-click and select:
plaintextAll tasks → Import -
Follow the wizard:
- Click Next.
- Select the certificate file you copied to your Windows machine.
- Click Next and Finish.
-
-
Update the DNS entries on your Windows machine:
-
Open Network Connections:
plaintextncpa.cpl -
Modify the DNS settings for both your Wi-Fi adapter and VMWare adapter to include your DNS server's IP address.
-
29. Access the Website Again
-
Open a browser and go to:
plaintexthttps://www.shuhari.local -
This time, the browser should trust the certificate, and you can securely access the website without warnings.
Read Next
[Day 10] 2. IDS (Intrusion Detection System)
## **Intrusion** An intrusion is an unauthorized attempt to access, manipulate, or disable a computer system or network. IDS helps to monitor and d...
[Day 10] 3. NFQueue and the IPS mode of Suricata
### **NFQueue**: NFQueue is a feature of Linux's Netfilter framework that allows packets to move from the kernel to user space. This lets applicati...