Front end basic finishing HTTPS related

Time:2020-3-13

outline

  1. TCP triple handshake establishing connection
  2. Client sends a list of symmetric and asymmetric encryption Suites supported by client random +
  3. The server saves the client random, selects an encryption suite from it, and returns the service random and digital certificate
  4. The client receives the service random and the digital certificate, and begins to verify the validity of the digital certificate and the validity of the server; saves the public key, and generates the pre master random number, which is encrypted by the public key and sent to the server
  5. The server obtains the client pre master and decrypts it with the private key, and uses the client random, service random and pre master to synthesize the final symmetric encryption secret key master secret
  6. Start communication

Verification of digital certificate:

  1. The client uses the hash algorithm provided by CA to back calculate the digital certificate to get the information summary a
  2. Client decrypts digital certificate with CA’s public key to get information digest B
  3. Certificate is valid if a / B is consistent

Note: the CA certificate chain will always look for the direct root certificate (built in the operating system)

1、 Why encryption is needed

HTTP uses plaintext to transmit data, which may be stolen or modified at every step of the transmission process. This attack method is calledMan in the middle attack
In order to solve this problem, the HTTP protocol stack is introducedSecurity layer (SSL / TLS)

2、 Use symmetric encryption

Symmetric encryption: both encryption and decryption use the same secret key.
The encryption process is as follows:

  1. Browser sends its supportedEncryption suite (encryption method)List and a random number client random
  2. The server will select an encryption suite from the encryption suite list, and then return to the random number service random
  3. It is better for the server and browser to return the confirmation message respectively

Problem: encryption suite and random number are both transmitted in plaintext, so it is easy to hijack and forge secret key

3、 Use asymmetric encryption

Asymmetric encryption: there are two secret keys a / B, and data can only be decrypted by B if a is required to encrypt, otherwise, data encrypted by B must be decrypted by a secret key.
In HTTPS, the public key is the one transmitted through plaintext, and the private key is the one left by the server itself.
The encryption process is as follows:

  1. Browser sends encryption suite list to server
  2. Then the server chooses to add a secret package and sends the public key to the browser in clear text
  3. Both parties confirm the returned message

Existing problems: asymmetric encryption efficiency is too low to guarantee the data security sent by the server to the browser (because the public key is easy to obtain, the data of the server can be decrypted through the public key, and then returned to the client)

4、 Use combination encryption

In the transmission process, symmetric encryption is used; but in the process of obtaining symmetric encryption, asymmetric encryption is used.
The encryption process is as follows:

  1. The browser sends a list of symmetric encryption suites, asymmetric encryption suites, and a random number client random to the server
  2. The service saves the random number client random, selects symmetric encryption and asymmetric encryption suite, generates the random number service random, and sends the selected encryption suite, service random and public key to the browser
  3. The browser saves the public key, generates the random number pre master, encrypts the pre master with the public key, and sends the encrypted data to the server
  4. It is better for the server to decrypt the pre master data with the private key and return the confirmation message.

At this time, the server and browser have a common client random, service random and pre master, and then generate these three groups of random numbersSymmetric secret key
Note: if the third party hijacks the pre master, it cannot decrypt it, because only the server has the private key.

5、 Add digital certificate

Through the combination of symmetric and asymmetric encryption, the encrypted transmission of data can be perfectly realized.
However, it is impossible to avoid hijacking DNS to modify IP address, and then establish intermediate site to steal public key and private key through forged IP.
So the server has to prove “I am me” to the browser.
Certificate issued by authority (such as ID card issued by police station of Public Security Bureau)
This authority becameCA(Certificate Authority), the certificate issued is calledDigital certificate
For the browser, digital certificate has two functions: to prove the identity of the server and to carry the public key of the server.
At this point, the request process changes as follows:

  1. The server returns a digital certificate, and the public key is contained in the digital certificate
  2. The browser has verified the certificates. After verifying the certificates, it can continue the following process.

Digital certificate: Application

  1. The server prepares the public key and private key, and the private key is used by itself
  2. The server submits public key / company / site and other information to Ca and waits for confirmation
  3. CA confirms authenticity and legitimacy through online / offline
  4. Through audit, issue digital certificate (server public key, organization information, CA information, effective time, certificate serial number, etc., including a CA generated signature)

Digital certificate process

  1. CA useHASH functionCalculate the information submitted in clear text and obtainInformation digest
  2. CA uses its private key pairInformation digestEncrypt(The encrypted ciphertext is the digital signature issued by Ca

Digital certificates: verifying

  1. After the browser obtains the digital certificate, it uses the hash algorithm of CA to get the information summary a
  2. After decrypting the digital certificate through CA’s public key, the information digest B is obtained
  3. Compare a / B and confirm that the certificate is valid if it is consistent

Digital certificate chain

There are two types: intermediate Ca and root CA
Root CA is embedded in the operating system and is authenticated by WebTrust
The intermediate CA usually handles the application business of the server

The browser will look up the root certificate along the certificate chain until it is in the operating system. If it is, it is legal. Otherwise, it is illegal.

Apply for a free certificate

  • https://freessl.cn/

summary

  • Symmetric asymmetric hybrid encryption solves the security problem of HTTP data transmission
  • Digital certificate solves the problem of server reliability

Reference material

  • Deep understanding of how HTTPS works