Sobes.tech
Junior

What is TLS?

sobes.tech AI

Answer from AI

TLS (Transport Layer Security) is a cryptographic protocol that provides secure communication over a network, for example, between a web browser and a server.

Key functions:

  • Authentication: Verification of the parties' identities (usually using X.509 certificates).
  • Confidentiality: Encryption of transmitted data to prevent unauthorized access.
  • Integrity: Ensuring that data has not been altered during transmission.

The TLS protocol operates at the transport layer of the OSI model (or higher, depending on the specific implementation and context). It replaced SSL (Secure Sockets Layer). Current versions: TLS 1.2 and TLS 1.3.

In the context of Go, TLS is used in the standard library net/http to support HTTPS.

package main

import (
	"crypto/tls"
	"net/http"
	"log"
)

func main() {
	// Example of setting up an HTTP server with TLS support
	// Certificate and private key files are required
	certFile := "server.crt"
	keyFile := "server.key"

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello, secure world!"))
	})

	// Starting HTTPS server
	log.Fatal(http.ListenAndServeTLS(":8443", certFile, keyFile, nil))
}

The interaction occurs in several stages (Handshake):

  1. ClientHello: The client sends a list of supported TLS versions, cipher suites, and other parameters.
  2. ServerHello: The server selects the optimal parameters from those offered by the client.
  3. Certificate: The server sends its certificate.
  4. ServerKeyExchange (optional): Key exchange.
  5. CertificateRequest (optional): The server may request a client certificate for mutual authentication.
  6. ServerHelloDone: The server indicates the end of its part of the Handshake.
  7. Certificate (optional): The client sends its certificate.
  8. ClientKeyExchange: The client sends a pre-master secret encrypted with the server's public key, used to generate session keys.
  9. CertificateVerify (optional): The client verifies the server's signature.
  10. ChangeCipherSpec: The client and server notify each other that they will use the negotiated session keys.
  11. Finished: The handshake is finalized with encrypted messages to verify the success of the process.

TLS provides a reliable security level for network communications, serving as the standard for HTTPS and other protocols requiring data protection.