Lesson 6 of 10
Digital Signatures & PKI
Digital signatures provide authentication, integrity, and non-repudiation for digital documents and code. Public Key Infrastructure (PKI) is the system that manages the trust relationships between public keys and identities - the foundation of HTTPS.
How Digital Signatures Work
# Digital Signature Process
SIGNING (done by the sender with their PRIVATE key):
1. Hash the message: digest = SHA256(message)
2. Encrypt the hash with private key: signature = RSA_encrypt(digest, private_key)
3. Send: message + signature
VERIFICATION (done by anyone with the sender's PUBLIC key):
1. Decrypt signature with public key: received_digest = RSA_decrypt(signature, public_key)
2. Hash the received message: computed_digest = SHA256(message)
3. Compare: if received_digest == computed_digest → signature is VALID
What this proves:
AUTHENTICATION: Only the private key holder could have created this signature
INTEGRITY: Any modification to the message invalidates the signature
NON-REPUDIATION: Signer cannot deny signing - only their private key could produce itDigital Signatures in Python
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from cryptography.hazmat.primitives import serialization
from cryptography.exceptions import InvalidSignature
# Ed25519 - modern, fast, secure alternative to RSA for signatures
private_key = Ed25519PrivateKey.generate()
public_key = private_key.public_key()
# Sign a document
message = b"Software update v2.1.0 - SHA256: abc123def456..."
signature = private_key.sign(message)
print(f"Signature ({len(signature)} bytes): {signature.hex()[:40]}...")
# Verify signature (anyone with public key can verify)
try:
public_key.verify(signature, message)
print("Signature VALID - document is authentic and unmodified")
except InvalidSignature:
print("Signature INVALID - document may have been tampered with!")
# Test with modified message
tampered_message = b"Malicious update v2.1.0 - SHA256: malware..."
try:
public_key.verify(signature, tampered_message)
except InvalidSignature:
print("Tampering detected - signature does not match modified message")Public Key Infrastructure (PKI) & X.509 Certificates
- X.509 Certificate: A digital document that binds a public key to an identity (domain name, organization). Contains: subject (who), public key, issuer (Certificate Authority), validity period, digital signature by the CA
- Certificate Authority (CA): A trusted organization that verifies identity and signs certificates. Browser root stores contain ~150 trusted root CAs (e.g., DigiCert, Let's Encrypt, Sectigo)
- Certificate Chain: Leaf certificate → Intermediate CA → Root CA. The browser trusts the root CA and verifies each link in the chain
- Let's Encrypt: Free, automated CA that provides DV (Domain Validation) certificates via ACME protocol - no excuse for HTTP on any website
- Certificate Revocation: CRL (Certificate Revocation List) and OCSP (Online Certificate Status Protocol) - mechanisms to invalidate compromised certificates before expiry
Certificate Inspection
# Inspect TLS certificates from command line
# View certificate details
openssl s_client -connect github.com:443 -showcerts < /dev/null 2>/dev/null \
| openssl x509 -noout -text
# Shows: subject, issuer, valid dates, SANs (Subject Alternative Names), key size
# Quick certificate check
openssl s_client -connect google.com:443 < /dev/null 2>/dev/null \
| openssl x509 -noout -dates
# notBefore=Jan 1 00:00:00 2025 GMT
# notAfter=Mar 31 23:59:59 2025 GMT
# Check certificate from a file
openssl x509 -in certificate.pem -noout -text
# Verify certificate chain
openssl verify -CAfile chain.pem certificate.pem
# Common attack: Certificate expired
# Set up monitoring: certbot renew --dry-run, cert-manager in Kubernetes
# PagerDuty/alerting for certificates expiring within 30 daysCommon Mistakes
- Self-signed certificates in production - trains users to click through certificate warnings, making them vulnerable to MitM attacks with other invalid certificates
- Not monitoring certificate expiry - automated monitoring is essential; expired certificates cause production outages and cause browsers to block access
- Certificate pinning misconfiguration - hard-coding a certificate instead of a public key means you must update all mobile app versions when the certificate renews
Tip
Tip
Practice Digital Signatures PKI in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
The foundational security model: Confidentiality (privacy), Integrity (trust), and Availability (uptime).
Practice Task
Note
Practice Task - (1) Write a working example of Digital Signatures PKI from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Digital Signatures PKI is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cybersecurity code.
Key Takeaways
- Digital signatures provide authentication, integrity, and non-repudiation for digital documents and code.
- X.509 Certificate: A digital document that binds a public key to an identity (domain name, organization). Contains: subject (who), public key, issuer (Certificate Authority), validity period, digital signature by the CA
- Certificate Authority (CA): A trusted organization that verifies identity and signs certificates. Browser root stores contain ~150 trusted root CAs (e.g., DigiCert, Let's Encrypt, Sectigo)
- Certificate Chain: Leaf certificate → Intermediate CA → Root CA. The browser trusts the root CA and verifies each link in the chain
Finished this lesson?