Skip to content

Commit

Permalink
feat: enable cert pinning for redirection capabilities (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsdmike authored Aug 22, 2024
1 parent 556b79c commit ed14c05
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
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

0 comments on commit ed14c05

Please sign in to comment.