Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
“Welcome to the discussion in the comments section. The nuggets will be giving away 100 nuggets in the comments section after the diggnation project. See the event article for details.”
Introduction to the
Since HTTP was upgraded from 1.1 to 2, everything has changed. Although HTTP2 does not mandate the use of an encrypted protocol for transport, industry standards include popular browsers that only support HTTP2 over HTTPS.
So how to add HTTP2 protocol support in HTTPS? Today this article will talk about the SSL/TLS protocol extensions NPN and ALPN.
SSL/TLS
Secure Socket Layer (SSL) is a set of protocols designed by Netscape in 1994 and released in version 3.0 in 1995.
Transport Layer Security (TLS) is a protocol designed by IETF based on SSL3.0, which is actually the later version of SSL.
SSL/TLS is a cryptographic communication framework, which is the most widely used cryptographic communication method in the world.
TLS is divided into two layers. The bottom layer is the TLS recording protocol, which encrypts messages using symmetric ciphers.
The upper layer is TLS handshake protocol, which is mainly divided into handshake protocol, password specification change protocol and application data protocol.
One of the most important is the handshake protocol, through the client and server side interaction, and share some necessary information, so as to generate shared keys and interactive certificates.
Let’s take a step-by-step look at what each step means:
-
client hello
The client sends a Client Hello message to the server containing the following contents:
- Available version number
- The current time
- Random number of client
- The session ID
- List of available cipher suites
- List of available compression methods
We mentioned earlier that TLS is actually a cryptographic framework, and some of its components are actually interchangeable. Here’s the version number available, the list of cryptographic suites available, and the list of compression methods available: ask the server what services they support.
Client random number is a random number generated by the client to generate a symmetric key.
-
server hello
After receiving the Client Hello message, the server returns a Server Hello message to the client containing the following information:
- The version number used
- The current time
- Server random number
- The session ID
- Password suite used
- Compression method used
The version number used, the cipher suite used, and the compression method used are answers to Step 1.
Server random number is a random number generated by the server to generate a symmetric key.
-
Optional step: Certificate
The server sends its own list of certificates, and because certificates can be hierarchical, it needs to send the certificates signed for the server in addition to processing the server’s own certificates. The client will validate the certificate on the server side. No certificate is required if you are communicating anonymously.
-
Optional :ServerKeyExchange
If the certificate information in step 3 is insufficient, ServerKeyExchange can be sent to build an encrypted channel.
The content of ServerKeyExchange may take two forms:
- If RSA is selected, the parameters (E, N) used by RSA to construct the public key password are passed. Ciphertext = plaintext E mod N Ciphertext = plaintext ^E\ mod\ N Ciphertext = plaintext E mod N. If you know E and N, then you know the public key of RSA. For details, see the RSA algorithm
- If the diff-Hellman key exchange protocol is selected, the key exchange parameters are transmitted. For details, see diffie-Hellman, a more secure key generation method
-
Optional step :Certificate Test
If you are in a restricted access environment, such as fabric, the server also needs to ask the client for a certificate. If client authentication is not required, skip this step.
-
Server Hello Done The server sends a server Hello Done message to inform the client that the message is finished.
-
Optional step :Certificate
In response to Step 5, the client sends the client certificate to the server
-
ClientKeyExchange
Again, there are two cases:
- In public key or RSA mode, the client generates a primary password based on the random number generated by the client and the server, encrypts the password using the public key, and sends the primary password back to the server.
- If the diff-Hellman key exchange protocol is used, the client sends the values that its side needs to expose to generate the Diff-Hellman key. For details, refer to diffie-Hellman, a more secure key generation method, so that the server can calculate a preliminary primary password from this public value.
-
Optional :CertificateVerify
The client proves to the server that it is the holder of the client certificate.
-
ChangeCipherSpec(ready to switch passwords)
ChangeCipherSpec is a password specification change protocol message, which indicates that subsequent messages will be encrypted with the previously negotiated key.
-
Finished (Handshake agreement ends)
The client tells the server that the handshake protocol is over.
-
ChangeCipherSpec(ready to switch passwords)
The server tells the client that it needs to change the password.
-
Finished (Handshake agreement ends)
The server tells the client that the handshake protocol is over.
-
Switch to application data protocol
The server and client then communicate in an encrypted way.
NPN and ALPN
When we introduce SSL/TLS protocol, the last step is to switch to the application data protocol, so how do the client and the server negotiate which application data protocol to use? Using HTTP1.1? Or HTTP2? Or SPDY?
This is where the TLS extension protocol is used. Next Protocol Negotiation (NPN) and Application Layer Protocol Negotiation (ALPN) are two extension protocols of TLS.
They are mainly used in TLS to negotiate which application data protocol the client and server should use to communicate.
Where NPN is the extension used by SPDY and ALPN is the extension used by HTTP2.
What’s the difference between them?
Compared with NPN, ALPN has listed the application layer protocols supported by the client in the Client Hello message. The server only needs to select the supported protocols. There is one less interactive step than NPN, so ALPN is the recommended protocol.
The following is a specific interaction flow chart:
Examples of interaction
The following uses ALPN as an example to explain the specific interaction process. First, the Client sends the “Client Hello” message:
Handshake Type: Client Hello (1) Length: 141 Version: TLS 1.2 (0x0303) Random: dd67b5943e5efd0740519f38071008b59efbd68ab3114587... Session ID Length: 0 Cipher Suites Length: 10 Cipher Suites (5 suites) Compression Methods Length: 1 Compression Methods (1 method) Extensions Length: 90 [other extensions omitted] Extension: application_layer_protocol_negotiation (len=14) Type: application_layer_protocol_negotiation (16) Length: 14 ALPN Extension Length: 12 ALPN Protocol ALPN string length: 2 ALPN Next Protocol: h2 ALPN string length: 8 ALPN Next Protocol: HTTP /1.1Copy the code
You can see that in the Extension field of the Client Hello message, ALPN is used, and the two ALPN protocols available are listed: H2 and HTTP /1.1.
The corresponding Server Hello message selects the ALPN protocol as follows:
Handshake Type: Server Hello (2)
Length: 94
Version: TLS 1.2 (0x0303)
Random: 44e447964d7e8a7d3b404c4748423f02345241dcc9c7e332...
Session ID Length: 32
Session ID: 7667476d1d698d0a90caa1d9a449be814b89a0b52f470e2d...
Cipher Suite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (0xc02f)
Compression Method: null (0)
Extensions Length: 22
[other extensions omitted]
Extension: application_layer_protocol_negotiation (len=5)
Type: application_layer_protocol_negotiation (16)
Length: 5
ALPN Extension Length: 3
ALPN Protocol
ALPN string length: 2
ALPN Next Protocol: h2
Copy the code
As shown above, the server chooses H2, and eventually HTTP2 will be used as the subsequent application layer data protocol after the client and server TLS handshake is completed.
conclusion
Both NPN and ALPN are extensions of TLS, and ALPN is more useful.
This article is available at www.flydean.com/08-ssl-tls-…
The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!
Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!