HTTP is a plaintext transmission protocol with poor security. Therefore, HTTPS is used for encrypted transmission. The key point is TLS/SSL
First, TLS/SSL protocol development
SSL (Secure Socket Layer) was originally created in 1994 as an extension of HTTP, and gradually developed into an independent protocol with three updates (V1.0, V2.0, and V3.0). Later, the protocol was standardized on V3.0 and named TLS (Transport Layer Security Protocol V1.0). Therefore, TLS can be interpreted as an upgraded version of SSL.
HTTPS = HTTP + TLS/SSL
TCP ensures the reliability (integrity) of data transmission. Therefore, any data can be processed by TLS/SSL before arriving at TCP.
- The default HTTP server port is 80
- The default HTTPS server port is 443
HTTP communication risks:
- Impersonation risk: Impersonating another person to participate in communications
- Risk of wiretapping: access to communications
- Tampering risk: Communication content is modified
TLS/SSL protocol core:
- certification
- Key agreement
- Data encryption
The TLS/SSL protocol consists of two layers:
- Handshake layer
- Encryption layer
TLS/SSL handshake
Before encrypted communication can begin, the client and server must first establish a connection and exchange parameters, a process called a handshake.
Related concepts:
1. Authentication: The client must pass the CA organization and adopt the technical solution of signed digital certificate to authenticate the server to avoid the man-in-the-middle attack.
2. Password suite negotiation: The client and the server need to negotiate a password suite agreed by both sides. The password suite determines the encryption algorithm and key negotiation algorithm used for the connection.
Key negotiation: Different key negotiation algorithms have different handshake processes. Because both RSA and static DH algorithms have forward security problems, the DHE algorithm and ECDHE algorithm are most commonly used at present (they are not related to the server key pair).
4. Handshake message integrity check: The handshake message is protected by TLS/SSL protocol encryption layer to ensure the confidentiality and integrity of the handshake message. If the handshake message is tampered, the whole handshake process will fail.
Handshake based on RSA algorithm:
- Client Provides the encryption protocol version number, random number generated by the client, and encryption suite supported by the client.
- The server confirms the version of the encryption protocol used, the encryption suite used by both parties, and provides digital certificates (including public keys) and random numbers.
- The client validates the digital certificate and returns a new random number (pre-master key) encrypted with the public key in the digital certificate
- The server uses its own private key to obtain the pre-master key sent by the client.
The client and server use the first two random numbers and the pre-master key to generate the master key according to the agreed encryption suite, and use the master key to encrypt and decrypt the subsequent communication.
The whole handshake is in plaintext, and therefore has security risks (the third random number may be decrypted). You can change the default RSA algorithm to the DH algorithm to improve security.
Handshake based on DH algorithm:
- Client Provides the encryption protocol version number, random number generated by the client, and encryption suite supported by the client.
- The server confirms the version of the encryption protocol used, the encryption suite used by both parties, and provides digital certificates (including public keys) and random numbers.
- The server uses the private key to sign client random numbers, server random numbers, and server DH parameters to generate server signatures.
- The server sends DH parameters and server signatures to the client.
- The client sends DH parameters of the client to the server
Then, the client verifies the server signature using the public key. The client and server generate the pre-master key using the DH parameters of the server and the DH parameters of the client respectively, and then generate the master key (session key) using the pre-master key, client random number, and server random number. The handshake is completed, and subsequent communication is encrypted and decrypted using the master key.
In addition, during authentication, if the client finds that the server certificate is invalid, a warning is issued to the user, who chooses whether to continue the communication.
TLS/SSL encryption
The handshake layer negotiates the algorithm and key block required by the encryption layer, and the encryption layer performs encryption operation and integrity protection.
There are three main encryption modes:
- Stream password encryption mode
- Block encryption mode
- AEAD mode
AEAD encryption is recommended for encryption and integrity operations.
OpenSSL and TLS/SSL
TLS/SSL protocol is the design specification, OpenSSL is the most common TLS/SSL protocol implementation.
OpenSSL is an underlying cryptographic library that encapsulates all cryptography algorithms, certificate management, and TLS/SSL protocol implementations.
For developers, a proper understanding and use of the underlying OpenSSL libraries is enough.
Reference:
- HTTPS in Plain English
- www.ruanyifeng.com/blog/2014/0…
- www.ruanyifeng.com/blog/2014/0…
- Razeen. Me/post/SSL – ha…
- Segmentfault.com/a/119000000…