Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to specify additional CA certificates for SSL peer validation #147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
Expand Down Expand Up @@ -62,6 +63,7 @@ var (
clientCertFile string
fourOnly bool
sixOnly bool
cacert string

// number of redirects followed
redirectsFollowed int
Expand All @@ -84,6 +86,7 @@ func init() {
flag.StringVar(&clientCertFile, "E", "", "client cert file for tls config")
flag.BoolVar(&fourOnly, "4", false, "resolve IPv4 addresses only")
flag.BoolVar(&sixOnly, "6", false, "resolve IPv6 addresses only")
flag.StringVar(&cacert, "cacert", "", "CA certificate to verify peer against (SSL)")

flag.Usage = usage
}
Expand Down Expand Up @@ -140,6 +143,23 @@ func main() {
visit(url)
}

// readCACerts - helper function to load additional CA certificates
func readCACerts(filename string) (*x509.CertPool, error) {
if filename == "" {
return nil, nil
}
certFileBytes, err := ioutil.ReadFile(filename)

if err != nil {
return nil, fmt.Errorf("failed to read CA certificate file: %v", err)
}

certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(certFileBytes)

return certPool, nil
}

// readClientCert - helper function to read client certificate
// from pem formatted file
func readClientCert(filename string) []tls.Certificate {
Expand Down Expand Up @@ -269,10 +289,16 @@ func visit(url *url.URL) {
host = req.Host
}

rootCAs, err := readCACerts(cacert)
if err != nil {
log.Printf("warning: failed to read CA certificates: %s\n", err)
}

tr.TLSClientConfig = &tls.Config{
ServerName: host,
InsecureSkipVerify: insecure,
Certificates: readClientCert(clientCertFile),
RootCAs: rootCAs,
}

// Because we create a custom TLSClientConfig, we have to opt-in to HTTP/2.
Expand Down