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

feat: enable cert pinning for redirection capabilities #396

Merged
merged 2 commits into from
Aug 22, 2024
Merged
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: 1 addition & 1 deletion pkg/wsman/client/wsman.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type Target struct {
bufferPool sync.Pool
UseTLS bool
InsecureSkipVerify bool
PinnedCert string
}

const timeout = 10 * time.Second
Expand Down Expand Up @@ -174,7 +175,6 @@ func (t *Target) GetServerCertificate() (*tls.Certificate, error) {
nohttps := strings.Replace(t.endpoint, "https://", "", 1)
nohttps = strings.Replace(nohttps, "/wsman", "", 1)

// Perform a connection to trigger the TLS handshake
conn, err := tls.Dial("tcp", nohttps, tlsConfig)
if err != nil {
return nil, err
Expand Down
35 changes: 32 additions & 3 deletions pkg/wsman/client/wsman_tcp.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package client

import (
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/hex"
"fmt"
"net"
"sync"
Expand All @@ -22,6 +25,7 @@ func NewWsmanTCP(cp Parameters) *Target {
challenge: &AuthChallenge{},
UseTLS: cp.UseTLS,
InsecureSkipVerify: cp.SelfSignedAllowed,
PinnedCert: cp.PinnedCert,
bufferPool: sync.Pool{
New: func() interface{} {
return make([]byte, 4096) // Adjust size according to your needs.
Expand All @@ -33,10 +37,35 @@ func NewWsmanTCP(cp Parameters) *Target {
// Connect establishes a TCP connection to the endpoint specified in the Target struct.
func (t *Target) Connect() error {
var err error

if t.UseTLS {
t.conn, err = tls.Dial("tcp", t.endpoint, &tls.Config{
InsecureSkipVerify: t.InsecureSkipVerify,
})
// check if pinnedCert is not null and not empty
var config *tls.Config
if len(t.PinnedCert) > 0 {
config = &tls.Config{
InsecureSkipVerify: t.InsecureSkipVerify,
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
for _, rawCert := range rawCerts {
cert, err := x509.ParseCertificate(rawCert)
if err != nil {
return err
}

// Compare the current certificate with the pinned certificate
sha256Fingerprint := sha256.Sum256(cert.Raw)
if hex.EncodeToString(sha256Fingerprint[:]) == t.PinnedCert {
return nil // Success: The certificate matches the pinned certificate
}
}

return fmt.Errorf("certificate pinning failed")
},
}
} else {
config = &tls.Config{InsecureSkipVerify: t.InsecureSkipVerify}
}

t.conn, err = tls.Dial("tcp", t.endpoint, config)
} else {
t.conn, err = net.Dial("tcp", t.endpoint)
}
Expand Down
Loading