All Projects → jcbsmpsn → Golang Https Example

jcbsmpsn / Golang Https Example

Licence: apache-2.0
Extremely simple HTTPS client in Go, along with all the openssl commands to make certs work. Errors and solutions in the documentation.

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Golang Https Example

Ssl Proxy
🔒 Simple zero-config SSL reverse proxy with real autogenerated certificates (LetsEncrypt, self-signed, provided)
Stars: ✭ 427 (+186.58%)
Mutual labels:  certificate, certificates
Certificate Authority Situational Awareness
Identifies unexpected and prohibited certificate authority certificates on Windows systems. #nsacyber
Stars: ✭ 99 (-33.56%)
Mutual labels:  certificate, certificates
Dca
Docker Certified Associate Exam Preparation Guide
Stars: ✭ 883 (+492.62%)
Mutual labels:  certificate, certificates
Sharkey
Sharkey is a service for managing certificates for use by OpenSSH
Stars: ✭ 360 (+141.61%)
Mutual labels:  certificate, certificates
Certificaat
General-purpose ACME client
Stars: ✭ 88 (-40.94%)
Mutual labels:  certificate, certificates
Teleport
Certificate authority and access plane for SSH, Kubernetes, web apps, databases and desktops
Stars: ✭ 10,602 (+7015.44%)
Mutual labels:  certificate
Pokemon Go Xposed
📱 Xposed module for Pokemon Go - Circumvents the certificate pinning by injecting the expected SSL trust chain, allows you to MITM and to configure a custom API endpoint.
Stars: ✭ 120 (-19.46%)
Mutual labels:  certificate
Pki
The Dogtag Certificate System is an enterprise-class Certificate Authority (CA) which supports all aspects of certificate lifecycle management, including key archival, OCSP and smartcard management.
Stars: ✭ 97 (-34.9%)
Mutual labels:  certificate
Lpic 1 Anki Flashcards
Deck of Anki flashcards for the LPIC-1 (Linux System Administrator) exams 101 and 102 of the Linux Professional Institute (LPI).
Stars: ✭ 90 (-39.6%)
Mutual labels:  certificate
Cli
🧰 A zero trust swiss army knife for working with X509, OAuth, JWT, OATH OTP, etc.
Stars: ✭ 2,151 (+1343.62%)
Mutual labels:  certificate
Certstrap
Tools to bootstrap CAs, certificate requests, and signed certificates.
Stars: ✭ 1,689 (+1033.56%)
Mutual labels:  certificate
Getssl
obtain free SSL certificates from letsencrypt ACME server Suitable for automating the process on remote servers.
Stars: ✭ 1,687 (+1032.21%)
Mutual labels:  certificate
Easycert
EasyCert quickly generates web server TLS certificates that have been self-signed by a private certificate authority that it also creates.
Stars: ✭ 121 (-18.79%)
Mutual labels:  certificate
Chef Acme
Chef cookbook to request SSL certificates at Let's Encrypt
Stars: ✭ 98 (-34.23%)
Mutual labels:  certificate
Aspnetcorecertificates
Certificate Manager in .NET Core for creating and using X509 certificates
Stars: ✭ 135 (-9.4%)
Mutual labels:  certificate
Ssl Checker
Python script that collects SSL/TLS information from hosts
Stars: ✭ 94 (-36.91%)
Mutual labels:  certificate
Docker Nginx Gunicorn Flask Letsencrypt
Boilerplate code for setting up Nginx + Gunicorn + Flask + automated LetsEncrypt certificates (https) using docker-compose.
Stars: ✭ 117 (-21.48%)
Mutual labels:  certificate
Pem
Easy PEM file parsing in Python.
Stars: ✭ 122 (-18.12%)
Mutual labels:  certificate
Vault Openvpn
Small wrapper utility to manage OpenVPN configuration combined with a Vault PKI
Stars: ✭ 112 (-24.83%)
Mutual labels:  certificate
Cfrpki
Cloudflare's RPKI Toolbox
Stars: ✭ 104 (-30.2%)
Mutual labels:  certificate

TLS Connection Options in Golang

This is not an official Google product

Golang sample code for a minimal HTTPS client and server that demos:

  • a server certificate that satisfies SAN requirements.
  • a client that trusts a specific certificate.
  • a server that authenticates the client based on the client certificate used in connection negotiation.

Generating Key and Self Signed Cert

openssl req \
    -x509 \
    -nodes \
    -newkey rsa:2048 \
    -keyout server.key \
    -out server.crt \
    -days 3650 \
    -subj "/C=GB/ST=London/L=London/O=Global Security/OU=IT Department/CN=*"

Running the Server

go run https_server.go

Running the Client

go run https_client.go

Error/Solution

Error: x509: certificate signed by unknown authority

Solution: The certificate served by https_server is self signed. This message means that the Go lang https library can't find a way to trust the certificate the server is responding with.

There are two possible solutions.

  1. Disable the client side certificate verification. This solution has the advantage of expediency, but the disadvantage of making your client code susceptible to man in the middle attacks.

    @@ -11,7 +11,9 @@ import (
     func main() {
            client := &http.Client{
                    Transport: &http.Transport{
    -                       TLSClientConfig: &tls.Config{},
    +                       TLSClientConfig: &tls.Config{
    +                               InsecureSkipVerify: true,
    +                       },
                    },
            }
    
  2. Add the server certificate to the list of certificate authorities trusted by the client.

    @@ -9,9 +10,18 @@ import (
     )
    
     func main() {
    +       caCert, err := ioutil.ReadFile("server.crt")
    +       if err != nil {
    +               log.Fatal(err)
    +       }
    +       caCertPool := x509.NewCertPool()
    +       caCertPool.AppendCertsFromPEM(caCert)
    +
            client := &http.Client{
                    Transport: &http.Transport{
    -                       TLSClientConfig: &tls.Config{},
    +                       TLSClientConfig: &tls.Config{
    +                               RootCAs: caCertPool,
    +                       },
                    },
            }
    

Error: x509: invalid signature: parent certificate cannot sign this kind of certificate

Solution: The wrong kind of server certificate was generated. The property in the CA that signed the server certificate indicates that the signing certificate is not a CA. Since this is a self signed server certificate, it needs the signing permission to sign itself.

Using openssl x509 -in server.crt -text -noout to look at the details of the server certificate reveals that it is missing the CA flag, which should look like this:

            X509v3 Basic Constraints:
                CA:TRUE

Error: x509: cannot validate certificate for 127.0.0.1 because it doesn't contain any IP SANs

Solution: A SAN is a Subject Alternative Name, an x509 extension that allows additional names to be specified as valid domains for the certficate.

Starting with Go 1.3, when connecting to a server via the IP address rather than the hostname, the CN field in the server certificate is ignored by the client golang libraries and names specified as SANs will be used instead.

Using openssl x509 -in server.crt -text -noout to look at the details of the server certificate reveals that it is missing a SAN section, which should look like this:

        X509v3 extensions:
            X509v3 Subject Alternative Name:
                IP Address:127.0.0.1

There are two possible solutions.

  1. Use a name to connect to the server instead of an IP address. If the client connects with a name matching the certificate CN, a SAN is not required.

    -       resp, err := client.Get("https://127.0.0.1:8443")
    +       resp, err := client.Get("https://localhost:8443")
    

    Using openssl x509 -in server.crt -text -noout to look at the Subject line should show CN= matching the name of the server. localhost or * will work.

            Subject: CN=*
    
  2. Add a SAN to the certificate with the IP address of the server.

    To add a SAN to a certificate, there is multiple steps required, that will generate a separate CA and use that to sign the server certificate signing request.

    openssl req \
        -newkey rsa:2048 \
        -nodes \
        -days 3650 \
        -x509 \
        -keyout ca.key \
        -out ca.crt \
        -subj "/CN=*"
    openssl req \
        -newkey rsa:2048 \
        -nodes \
        -keyout server.key \
        -out server.csr \
        -subj "/C=GB/ST=London/L=London/O=Global Security/OU=IT Department/CN=*"
    openssl x509 \
        -req \
        -days 365 \
        -sha256 \
        -in server.csr \
        -CA ca.crt \
        -CAkey ca.key \
        -CAcreateserial \
        -out server.crt \
        -extfile <(echo subjectAltName = IP:127.0.0.1)
    

Error: tls: client didn't provide a certificate

Solution: When the server code has the option set to authenticate client connections using the client certificate, like this:

-       cfg := &tls.Config{}
+       cfg := &tls.Config{
+               ClientAuth: tls.RequireAndVerifyClientCert,
+       }

the server will drop connections from clients using certs that are untrusted, where trust is established by a relationship to one of the CAs that the TLS server knows about.

In order to establish a connection, the client will have to present a trusted certificate. To start, generate a client certificate to use:

openssl req \
    -x509 \
    -nodes \
    -newkey rsa:2048 \
    -keyout client.key \
    -out client.crt \
    -days 3650 \
    -subj "/C=GB/ST=London/L=London/O=Global Security/OU=IT Department/CN=*"

Next, configure the client to send a certificate with connection attempts:

+       cert, err := tls.LoadX509KeyPair("client.crt", "client.key")
+       if err != nil {
+               log.Fatal(err)
+       }
+
        client := &http.Client{
                Transport: &http.Transport{
                        TLSClientConfig: &tls.Config{
                                RootCAs:      caCertPool,
+                               Certificates: []tls.Certificate{cert},
                        },
                },
        }

Then, configure the server to trust the client certificate:

+       caCert, err := ioutil.ReadFile("client.crt")
+       if err != nil {
+               log.Fatal(err)
+       }
+       caCertPool := x509.NewCertPool()
+       caCertPool.AppendCertsFromPEM(caCert)
        cfg := &tls.Config{
                ClientAuth: tls.RequireAndVerifyClientCert,
+               ClientCAs:  caCertPool,
        }
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].