You've already forked step-ca-cli
mirror of
https://github.com/smallstep/cli.git
synced 2026-10-29 00:17:23 +03:00
* lowercase URL before checking prefixes * don't overwrite error stack for step errors * add trimURLPrefix test
66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
package certificate
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/smallstep/cli/crypto/x509util"
|
|
)
|
|
|
|
var urlPrefixes = []string{"https://", "tcp://", "tls://"}
|
|
|
|
// getPeerCertificates creates a connection to a remote server and returns the
|
|
// list of server certificates.
|
|
//
|
|
// If the address does not contain a port then default to port 443.
|
|
//
|
|
// Params
|
|
// *addr*: e.g. smallstep.com
|
|
// *roots*: a file, a directory, or a comma-separated list of files.
|
|
// *insecure*: do not verify that the server's certificate has been signed by
|
|
// a trusted root
|
|
func getPeerCertificates(addr, roots string, insecure bool) ([]*x509.Certificate, error) {
|
|
var (
|
|
err error
|
|
rootCAs *x509.CertPool
|
|
)
|
|
if roots != "" {
|
|
rootCAs, err = x509util.ReadCertPool(roots)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "failure to load root certificate pool from input path '%s'", roots)
|
|
}
|
|
}
|
|
if !strings.Contains(addr, ":") {
|
|
addr += ":443"
|
|
}
|
|
tlsConfig := &tls.Config{RootCAs: rootCAs}
|
|
if insecure {
|
|
tlsConfig.InsecureSkipVerify = true
|
|
}
|
|
conn, err := tls.Dial("tcp", addr, tlsConfig)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "failed to connect")
|
|
}
|
|
conn.Close()
|
|
return conn.ConnectionState().PeerCertificates, nil
|
|
}
|
|
|
|
// trimURLPrefix returns the url split into prefix and suffix and a bool which
|
|
// tells if the input string had a recognizable URL prefix.
|
|
//
|
|
// Examples:
|
|
// trimURLPrefix("https://smallstep.com") -> "https://", "smallstep.com", true
|
|
// trimURLPrefix("./certs/root_ca.crt") -> "", "", false
|
|
// trimURLPrefix("hTtPs://sMaLlStEp.cOm") -> "hTtPs://", "sMaLlStEp.cOm", true
|
|
func trimURLPrefix(url string) (string, string, bool) {
|
|
tmp := strings.ToLower(url)
|
|
for _, prefix := range urlPrefixes {
|
|
if strings.HasPrefix(tmp, prefix) {
|
|
return url[:len(prefix)], url[len(prefix):], true
|
|
}
|
|
}
|
|
return "", "", false
|
|
}
|