TLS Fundamentals
A certificate is used to ensure trust between two parties during a transaction.
When a user tries to access a web server, TLS certificates ensure that the communication between the user and the server is encrypted and that the server and the user are who they say they are.
If a user sent their credentials without encryption, a hacker with a simple sniffer would be able to retrieve the credentials by sniffing network traffic and break into the user's account.
Once encrypted, the data will be scrambled and only those who have the key will be able to decipher it; if someone sniffs the network, this data will make no sense at all.
If the key to decrypt needs to be sent to the server over the same network, then it would make no sense, because this key could also be intercepted on the way and would be used to decrypt the data.
The system in which both sides know the encryption key is called symmetric encryption. The same key used to encrypt is used to decrypt.
Asymmetric encryption is when a pair of keys (two keys), one PUBLIC key that encrypts the data (known by everyone) and another PRIVATE key (known only by the recipient) that decodes the data. When we want to send something, we encrypt the content with their PUBLIC key, and only the recipient will be able to decrypt it using their PRIVATE key.
We already do all this process on GitHub, GitLab, SSH, etc.
If you don't have a key pair for SSH, it's time to have one.
ssh-keygen
The command above will generate a key pair for SSH purposes.
- id_rsa: is the private key that only you should know
- id_rsa.pub: is your public key that you can put on GitHub, GitLab, on servers you access via SSH, etc.
Knowing this scenario, when we access a server via HTTPS in the browser for example, the server automatically sends its public key to the user's browser. The browser creates a symmetric encryption key (it already knows which one it is), encrypts it with the server's public key, and sends it back to it. Only the server is capable of decrypting this with its private key. Therefore, both will know a symmetric key without anyone else knowing, and from that moment on, it is possible to encrypt everything using only the symmetric key.
One important thing about asymmetric key pairs: if you encrypt something with the public key, only the private key will decrypt it. It is not possible to decrypt using the same public key something you encrypted with it. If you encrypt something with your private key, the public key will be able to decrypt it and everyone knows the public key. That's why it's necessary that everything that goes to you is encrypted with your public key and what will go to others, encrypt with their public key and not your own.
To generate a key pair for SSL purposes, the command is a little different.
openssl genrsa -out mysite.key 1024
cat mysite.key
-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAM7KqoAHnpP91ix8
O33/HIKxAmm1vIludtZ0YdWoEBlLZ2DMmq7e8MbP6QEkPuElu6g9eZLqb0zVNDsP
UETlYe2nJ+oLJWH0M6WIwIgoITzyIJRjwK9QVTW7AqAOePtlugeEE/JyrlZGzuog
M2xMHUr4JIx19DsUNoXhpt++2mTlAgMBAAECgYAYvOXoqwCtc5BLghEb4YjnmYVQ
YA1N44kT3phVluVWIU8cpV1bzg+/uN+G428iQAFS/wesK59k8Zxt9EcF9urf7C5c
gnS4RTD7FpYyTn8sGPe5CvAort89kFSeYEoAzfPaIjpxGWg5mBMz2OBU+gPLC+3e
8RZT80WIQvR9MuGrgQJBAOvIcMf/bCH/0mdhGtXlP/lFXF+WEMK7jsb+DWbTYAXu
sYwan3/TfsZPvMEqQHLeWmDXvA+rpATT8NaIOuaj9uMCQQDghdnjf/5hDSeFHlpc
HNFX0ToE6THqVl9kRevVWUsuAnOMuPRCfC43+yF7olGmSC26A1D0vXRD18GyaNOf
0zeXAkEAilikBFXU5uYqWMpbaqIHNYYDO3rb/sb6cbxjtK/WKeTyfJiqeeBpBIeI
43adNklNkoFDMZauuL57FAX/OZF/rQJAV81geSxLsW5wfwlwssb+QwZqVxNO1uJy
KYeCg4uI7/Q7ddO6iemANZo67Nt8bLebUjwLw7n3JrTQZdHCuKTj/wJBAMW+rymx
DrgwtfuIZwRCWIH7xSQmLNa/DWkFc+zMtbBiMIFFXRVKm8/zJyQOVY1PCqcd50vc
FrcqB+7CxSbRlYQ=
-----END PRIVATE KEY-----
openssl rsa -in mysite.key -pubout > mysite.pem
cat mysite.pem
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOyqqAB56T/dYsfDt9/xyCsQJp
tbyJbnbWdGHVqBAZS2dgzJqu3vDGz+kBJD7hJbuoPXmS6m9M1TQ7D1BE5WHtpyfq
CyVh9DOliMCIKCE88iCUY8CvUFU1uwKgDnj7ZboHhBPycq5WRs7qIDNsTB1K+CSM
dfQ7FDaF4abfvtpk5QIDAQAB
-----END PUBLIC KEY-----
The openssl command can generate keys with different algorithms and sizes. For example, we could have generated one with 2048. See how the size of the private key has changed a lot.
openssl genrsa -out mysite.key 2048
cat mysite.key
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDb7sWrKNtNMfkl
mFwiK9OikyKbaHzzSFiQldgr4FFwmLonbFyqWBDGfIlZFr7yKPEVs7YWtghkDNDx
qgbAuMm3+Q5C4zuiu0t2biP6zVz6c4PsYPZ+HRrkeSfDbMo2WMMa5KGf2rh/Bzsm
90+qItbcJMCg1l+/CMWK4TQFsSdt8YAKf4ZU6XV3xtFTtWnt59lfy/syYjWf2eAH
ATATXAqEi3qlUkpHf0qODQY8MoVPrD+HSRlQCCaBl2pMq+J18x3UPwMWPCKsF2FU
ihGgTbUSHdagYs8Mqy/7i5iKRoKsxu4znD15TF/4GuS/uJ7W3wwRodHOFGh/lYZq
FDA6i/yJAgMBAAECggEAAjAh7038wIvaurEFWaGt/VQRaBJmC9WQa//Ror4ckU4z
q+i8E0XFBPSAOU6sn6QgoeKj0R4Gf1hRS9YxMwGjoBzuM2QnV7sdSRRXb0tTvXog
ud1NFnwbpGJALwWKDXhcmIvlv71hUII16HFVbY/jK1WCRRdc5H2l2XfcGKJmjgVw
UXjVOL7643WKoJl62OfIg/aqcvMs4fA/VqQ9UWBzpsRibE6yfSzVvjKMFrJo3oN7
wN3RyYNtzS98cjLB0BTN0qEPVaKLQuA8VYEOq4/qwOC5LXLamJJCfQVxbvGqX/ew
vZKqOH7/slV4G5O0NE36OM7p3IWAWybkOfkCsm8KMQKBgQD9KpC1XBgOAzJHmqGz
eKMOinx/jZyWiCCPNH/Wy3Rg+Nu+XDtoE/KbL+NZQqKbxQZOg+bBYIE0gZjw6Txb
uwpo8dLrMSIaMokn6aFfks6BxbrQFoziwJP9q1L+omE7WXPmpkiqGGskWuok/XIa
YM3qEVjAdIXSrbFEqZK/00V2EwKBgQDeZPpStTR+DlNTe8iLHdsB5uhzKA+A0Jr5
Tr6dwCSClqqOd8Qd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PDKsv+4uYikaCrMbuM5w9eUxf+Brkv7ie1t8MEaHRzhRof5WGahQwOov8
iQIDAQAB
-----END PUBLIC KEY-----
But none of this guarantees that the server we are trying to access is actually the correct server. A hacker could have a server and host an identical website posing as the real server and send their public key to you and you would encrypt the data for them with their public key.
Let's imagine that the hacker is someone who administers a network and they simply point the network's DNS server to point https://server.com to the IP with the website they created.
This is where the moment of knowing whether the public key you received is legitimate or not comes in.
When we receive the public key, we don't just receive the public key, we receive a certificate and inside it contains the public key. In this case, the certificate has some information that will be necessary to validate some information.
Previously, we generated the private key and then the public key using the private key. With this command, we can use the private key to generate a certificate and this will contain the public key.
openssl req -x509 -new -key mysite.key -out certificado.pem
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) [AU]:BR
State or Province Name (full name) [Some-State]:ES
Locality Name (eg, city) []:Vila Velha
Organization Name (eg, company) [Internet Widgits Pty Ltd]:devsecops
Organizational Unit Name (eg, section) []:personal
Common Name (e.g. server FQDN or YOUR name) []:devsecops
Email Address []:[email protected]
cat certificado.pem
-----BEGIN CERTIFICATE-----
MIID9zCCAt+gAwIBAgIUfYH2ViX4fsY2IrO7fLfkNTp7uv0wDQYJKoZIhvcNAQEL
BQAwgYoxCzAJBgNVBAYTAkJSMQswCQYDVQQIDAJFUzETMBEGA1UEBwwKVmlsYSBW
ZWxoYTESMBAGA1UECgwJZGV2c2Vjb3BzMREwDwYDVQQLDAhwZXJzb25hbDESMBAG
A1UEAwwJZGV2c2Vjb3BzMR4wHAYJKoZIhvcNAQkBFg9kYXZpZEBnbWFpbC5jb20w
HhcNMjQwMjA0MTgxNzAzWhcNMjQwMzA1MTgxNzAzWjCBijELMAkGA1UEBhMCQlIx
CzAJBgNVBAgMAkVTMRMwEQYDVQQHDApWaWxhIFZlbGhhMRIwEAYDVQQKDAlkZXZz
ZWNvcHMxETAPBgNVBAsMCHBlcnNvbmFsMRIwEAYDVQQDDAlkZXZzZWNvcHMxHjAc
BgkqhkiG9w0BCQEWD2RhdmlkQGdtYWlsLmNvbTCCASIwDQYJKoZIhvcNAQEBBQAD
ggEPADCCAQoCggEBAKyVjzmLJsbi0zmon1NFYqzCn5gMSL6q5OfhtlBUxE8iDu4B
H+9eP6c8FK6rLuA5nNCTbUQj37d7rwekIjF1KN6+B+W/av/xkRi3mbBzIR73Iwhv
OatNz+XXJPDH9hxSa7VKKmJ/TG4GvkbForBrVCsMbcvlh1EN87HIT3nYUzB8QRX+
NDeiv4o3sEB8NwrWcO8cRwSLxUMr1iQSEQcpdBv4OH6paQkqyJM9KxoR7u0IYXSt
O61NBPfrHRXoGwJvkkVEay6acWj4CO+J+Ft9piy5wG/BlvKV9X3aGF72qS2Hipoe
qC+L/ICpviMZANW0lstNbgV6FbUS2/pGUNjzpqkCAwEAAaNTMFEwHQYDVR0OBBYE
FCdy2zrMEzzK3W6X9Bbbi1saMQowMB8GA1UdIwQYMBaAFCdy2zrMEzzK3W6X9Bbb
i1saMQowMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAImSI9i1
Rr8BeDuPuPa4SxNROyR9mXO+UqAPnb1Omrtp8+4tfrs2lXLFYwcTMub4dHLeV30f
k9BJtpcbGvDyDy2yC/S4Nqn+wB1uKmjQFW8XBe4RCTRWUvWqC9CARUNGF5ABveN7
ZU+w7hg1hZI1AQiP/cz2Z/yPDZ+sgyrctmMrN2YJB47cErXNw2bcDd9TUBgdoI6A
ZtkvAmbRdKkc0yU7Q6aUDby5nunpV1B72hkX6dpg4WT8APem1KGG4h2ZXZlSKgC1
qzJaMqWLom47xcRSMM0CTCQDh37OPQlUNsOPyVcrVrSBhuXuqcDQ1NtoUfNGa/VU
oZY5Zwj/v1JYpak=
-----END CERTIFICATE-----
Opening this file in the system we can see this and other things that didn't fit in the screenshot.

This is a self-signed certificate. Note the Verified by: devsecops field.
If you generated the certificate, you will have to sign it yourself and no one trusts you, so... this is not considered a secure certificate.
We could generate a certificate saying we are Amazon, passing all the necessary data on its behalf.
What proves that a site is who it says it is?
Looking more closely at an amazon.com certificate, for example, we have:

Now a certificate like Amazon's has Issued By DigiCert Global CA G2. That is, someone other than Amazon verified that this certificate is legitimate.
That's where the most important part comes in: Who signed and issued the certificate?
The browser already does this work for you. It verifies who signed and issued the certificate and if it is not a Certificate Authority (CA), it will say that this certificate is not valid, therefore the page will not be secure.
Several CAs exist and can sign a certificate.
and many others...
To generate a legitimate certificate and pay for it, you need to create a CSR (Certificate Signing Request), which is an encoded data block that is generated by an entity requesting a digital certificate. A CSR contains the public information associated with a public key for which the certificate will be issued.
When you want to obtain a digital certificate, you need to generate a CSR and send it to a trusted Certificate Authority (CA) for signing. The CA uses the information in the CSR to create a digital certificate that binds the public key provided in the CSR to a certain holder (such as a domain or an organization).
To generate a CSR, the command is as follows.
openssl req -new -key mysite.key -out mysite.csr
# We answer some questions...
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) [AU]:BR
State or Province Name (full name) [Some-State]:ES
Locality Name (eg, city) []:Vila Velha
Organization Name (eg, company) [Internet Widgits Pty Ltd]:devsecops.puziol.com.br
Organizational Unit Name (eg, section) []:devsecops
Common Name (e.g. server FQDN or YOUR name) []:devsecops.puziol.com.br
Email Address []:[email protected]
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:mypass
An optional company name []:devsecops
cat mysite.csr
-----BEGIN CERTIFICATE REQUEST-----
MIIDHjCCAgYCAQAwgacxCzAJBgNVBAYTAkJSMQswCQYDVQQIDAJFUzETMBEGA1UE
BwwKVmlsYSBWZWxoYTEgMB4GA1UECgwXZGV2c2Vjb3BzLnB1emlvbC5jb20uYnIx
EjAQBgNVBAsMCWRldnNlY29wczEgMB4GA1UEAwwXZGV2c2Vjb3BzLnB1emlvbC5j
b20uYnIxHjAcBgkqhkiG9w0BCQEWD2RhdmlkQGdtYWlsLmNvbTCCASIwDQYJKoZI
hvcNAQEBBQADggEPADCCAQoCggEBAKyVjzmLJsbi0zmon1NFYqzCn5gMSL6q5Ofh
tlBUxE8iDu4BH+9eP6c8FK6rLuA5nNCTbUQj37d7rwekIjF1KN6+B+W/av/xkRi3
mbBzIR73IwhvOatNz+XXJPDH9hxSa7VKKmJ/TG4GvkbForBrVCsMbcvlh1EN87HI
T3nYUzB8QRX+NDeiv4o3sEB8NwrWcO8cRwSLxUMr1iQSEQcpdBv4OH6paQkqyJM9
KxoR7u0IYXStO61NBPfrHRXoGwJvkkVEay6acWj4CO+J+Ft9piy5wG/BlvKV9X3a
GF72qS2HipoeqC+L/ICpviMZANW0lstNbgV6FbUS2/pGUNjzpqkCAwEAAaAxMBUG
CSqGSIb3DQEJBzEIDAZteXBhc3MwGAYJKoZIhvcNAQkCMQsMCWRldnNlY29wczAN
BgkqhkiG9w0BAQsFAAOCAQEAqdM/RmCkIuMAIFcqK6nb4+dJfNXtmggv+0yLZu0K
ccN1STg4XgttfvR1ZJAS38IUEWSEUIJ7K4nkxdUDZnxoXFEFpzN5PKpdp6/i3Sv2
o6xp7uWQhIs08zeg5MA4wuKjcNMev58zv7w4YTQNlhZzJj6mtWIWTkb2y5XYwLeV
1vHz6vqbb2TE+UFNmBteX0q+QOK63ymoODVNBTss36c05Hs6pr8WYAN6nNIK3HcK
/b015LP6J6AdZGx3CXDxc+UDRMn1W0vG86/yduRqMZmeUTE/BD7l5PoFxZwnUJqX
j+PtE66Cj5z6duvO4qa/hdQmT/R+DR7QOTEbsDMrdSKKUg==
-----END CERTIFICATE REQUEST-----
# We can also pass all the parameters in the command
# C = Country
# ST = State
# O = Organization
# CN = Common Name
openssl req -new -key mysite.key -out mysite.csr -subj "/C=BR/ST=ES/O=devsecops/CN=devsecops.puziol.com.br"
This generated .csr file must be sent to the certificate authority for signing. A CA has its public and private key. A curiosity is that this type of signing server needs to be kept with maximum security because if its private key is exposed, all certificates will have to be automatically invalidated.
The CA will check the data and send back the signed issued certificate with the private key. Now we will have a certificate that the browser will trust.
But how does the browser know it was signed by a legitimate CA and not a fake CA?
Inside browsers, we have several built-in public keys from legitimate CAs.

It's worth remembering that these are public CAs that help us validate public sites. These authorities cannot validate a site that is on an organization's private network.
Most CAs have services that can be deployed internally as an internal CAA Server within the company. However, we can create our own CA to sign internal certificates and install it in browsers.
And last but not least is how does the server ensure that the user is who they say they are? It could be a hacker posing as someone intercepting traffic somehow.
Several government tax services require companies to have their certificates to prove their identity when communicating with their servers. The server requests the client's certificate to know that it is indeed who it says it is. That's why it's also necessary to install these certificates on the client's machine.
This entire infrastructure created is known as PKI (Public Key Infrastructure).
Certificates with public keys generally have the following names.
Certificate with public key.
- server.crt
- server.pem
- client.crt
- client.pem
Private keys generally have the word key as an extension or part of the name.
- server.key
- server-key.pem
- client.key
- client-key.pem
But if you check what we have inside, you can see the difference already at the beginning of the file.
-
-----BEGIN PRIVATE KEY-----
-
-----BEGIN PUBLIC KEY-----
-
-----BEGIN CERTIFICATE-----