Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ Application Options:
--connect-timeout=<seconds> Maximum time in seconds allowed for the connection phase.
-I, --head Fetch the headers only.
-k, --insecure Disables TLS verification of the connection.
--cacert=<file> Path to custom CA

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also have to be updated (just run gocurl --help and copy/paste)

certificate file.
--tlsv1.3 Forces gocurl to use TLS v1.3 or newer.
--tlsv1.2 Forces gocurl to use TLS v1.2 or newer.
--tls-max=<VERSION> (TLS) VERSION defines maximum supported TLS version. Can be
Expand Down
19 changes: 19 additions & 0 deletions internal/client/clientdialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net"
"os"
"time"

"github.com/ameshkov/gocurl/internal/client/cfcrypto"
Expand Down Expand Up @@ -223,6 +225,23 @@ func createTLSConfig(hostname string, cfg *config.Config, out *output.Output) (t
tlsConfig.InsecureSkipVerify = true
}

// Load CA certificate if specified
if cfg.CACert != "" {
caCert, err := os.ReadFile(cfg.CACert)
if err != nil {
out.Error("Failed to read CA certificate: %v", err)
return nil
}

caCertPool := x509.NewCertPool()
if !caCertPool.AppendCertsFromPEM(caCert) {
out.Error("Failed to parse CA certificate")
return nil
}

tlsConfig.RootCAs = caCertPool
}

if len(cfg.TLSRandom) == 32 {
out.Debug("Overriding TLS ClientHello random value")
tlsConfig.Rand = &tlsRandomReader{data: cfg.TLSRandom}
Expand Down
13 changes: 13 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -44,6 +45,9 @@ type Config struct {
// Insecure disables TLS verification of the connection.
Insecure bool

// Provide your own CA certificates
CACert string

// TLSMinVersion is a minimum supported TLS version.
TLSMinVersion uint16

Expand Down Expand Up @@ -181,6 +185,7 @@ func ParseConfig(args []string) (cfg *Config, err error) {
Method: opts.Method,
Head: opts.Head,
Insecure: opts.Insecure,
CACert: opts.CACert,
Data: opts.Data,
OutputJSON: opts.OutputJSON,
OutputPath: opts.OutputPath,
Expand Down Expand Up @@ -324,6 +329,14 @@ func ParseConfig(args []string) (cfg *Config, err error) {
}
}

// Handle CA certificate file if specified
if opts.CACert != "" {
if _, err := os.Stat(opts.CACert); err != nil {
return nil, fmt.Errorf("CA certificate file not found %s: %w", opts.CACert, err)
}
cfg.CACert = opts.CACert
}

return cfg, nil
}

Expand Down
3 changes: 3 additions & 0 deletions internal/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type Options struct {
// Insecure disables TLS verification of the connection.
Insecure bool `short:"k" long:"insecure" description:"Disables TLS verification of the connection." optional:"yes" optional-value:"true"`

// Provide your own CA certificates
CACert string `long:"cacert" description:"Path to custom CA certificate file." value-name:"<file>"`

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please elaborate a little in the description

Suggested change
CACert string `long:"cacert" description:"Path to custom CA certificate file." value-name:"<file>"`
CACert string `long:"cacert" description:"Path to a certificate file to verify the peer. The file may contain multiple CA certificates. The certificate(s) must be in PEM format." value-name:"<file>"`


// TLSv13 forces to use TLS v1.3.
TLSv13 bool `long:"tlsv1.3" description:"Forces gocurl to use TLS v1.3 or newer." optional:"yes" optional-value:"true"`

Expand Down