Junior — Middle
How is certificate transfer carried out during HTTPS connection establishment?
sobes.tech AI
Answer from AI
During the establishment of an HTTPS connection, a TLS handshake occurs, during which the server sends its certificate to the client. This certificate contains the server's public key and confirms its authenticity, signed by a Certificate Authority (CA). The client verifies the certificate to ensure it is valid and matches the expected domain.
In Go, the crypto/tls package is used for working with HTTPS. When establishing a connection, the server automatically sends its certificate if it is configured in tls.Config.
Example of configuring an HTTPS server in Go:
package main
import (
"crypto/tls"
"log"
"net/http"
)
func main() {
srv := &http.Server{
Addr: ":443",
TLSConfig: &tls.Config{
// Certificate and key are loaded automatically when the server starts
},
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello HTTPS"))
})
log.Fatal(srv.ListenAndServeTLS("server.crt", "server.key"))
}
Here, ListenAndServeTLS automatically loads the certificate and key and sends the certificate to the client during the TLS handshake.