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

draft: fix(util): match non-pointer public key types #48

Merged
merged 4 commits into from
Dec 18, 2023
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
33 changes: 22 additions & 11 deletions integration_tests/ssh3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ var ssh3ServerPath string
const DEFAULT_URL_PATH = "/ssh3-tests"
var serverCommand *exec.Cmd
var serverSession *Session
var privKeyPath string
var rsaPrivKeyPath string
var ed25519PrivKeyPath string
var attackerPrivKeyPath string
var username string
// must exist on the machine to successfully run the tests
Expand Down Expand Up @@ -65,10 +66,11 @@ var _ = BeforeSuite(func() {
serverSession, err = Start(serverCommand, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())

privKeyPath = os.Getenv("TESTUSER_PRIVKEY")
rsaPrivKeyPath = os.Getenv("TESTUSER_PRIVKEY")
ed25519PrivKeyPath = os.Getenv("TESTUSER_ED25519_PRIVKEY")
attackerPrivKeyPath = os.Getenv("ATTACKER_PRIVKEY")
username = os.Getenv("TESTUSER_USERNAME")
Expect(fileExists(privKeyPath)).To(BeTrue())
Expect(fileExists(rsaPrivKeyPath)).To(BeTrue())
Expect(fileExists(attackerPrivKeyPath)).To(BeTrue())
}
})
Expand Down Expand Up @@ -113,8 +115,17 @@ var _ = Describe("Testing the ssh3 cli", func() {
}

Context("Client behaviour", func() {
It("Should connect using privkey", func() {
clientArgs = append(getClientArgs(privKeyPath), "echo", "Hello, World!")
It("Should connect using an RSA privkey", func() {
clientArgs = append(getClientArgs(rsaPrivKeyPath), "echo", "Hello, World!")
command := exec.Command(ssh3Path, clientArgs...)
session, err := Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(session).Should(Exit(0))
Eventually(session).Should(Say("Hello, World!\n"))
})

It("Should connect using an ed25519 privkey", func() {
clientArgs = append(getClientArgs(ed25519PrivKeyPath), "echo", "Hello, World!")
command := exec.Command(ssh3Path, clientArgs...)
session, err := Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -123,10 +134,10 @@ var _ = Describe("Testing the ssh3 cli", func() {
})

It("Should return the correct exit status", func() {
clientArgs0 := append(getClientArgs(privKeyPath), "exit", "0")
clientArgs1 := append(getClientArgs(privKeyPath), "exit", "1")
clientArgs255 := append(getClientArgs(privKeyPath), "exit", "255")
clientArgsMinus1 := append(getClientArgs(privKeyPath), "exit", "-1")
clientArgs0 := append(getClientArgs(rsaPrivKeyPath), "exit", "0")
clientArgs1 := append(getClientArgs(rsaPrivKeyPath), "exit", "1")
clientArgs255 := append(getClientArgs(rsaPrivKeyPath), "exit", "255")
clientArgsMinus1 := append(getClientArgs(rsaPrivKeyPath), "exit", "-1")

command0 := exec.Command(ssh3Path, clientArgs0...)
session, err := Start(command0, GinkgoWriter, GinkgoWriter)
Expand Down Expand Up @@ -192,7 +203,7 @@ var _ = Describe("Testing the ssh3 cli", func() {

Eventually(serverStarted).Should(Receive())
// Execute the client with TCP port forwarding
clientArgs := getClientArgs(privKeyPath, "-forward-tcp", fmt.Sprintf("%d/%s@%d", localPort, remoteAddr.IP, remoteAddr.Port))
clientArgs := getClientArgs(rsaPrivKeyPath, "-forward-tcp", fmt.Sprintf("%d/%s@%d", localPort, remoteAddr.IP, remoteAddr.Port))
command := exec.Command(ssh3Path, clientArgs...)
session, err := Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -301,7 +312,7 @@ var _ = Describe("Testing the ssh3 cli", func() {

Eventually(serverStarted).Should(Receive())
// Execute the client with UDP port forwarding
clientArgs := getClientArgs(privKeyPath, "-forward-udp", fmt.Sprintf("%d/%s@%d", localPort, remoteAddr.IP, remoteAddr.Port))
clientArgs := getClientArgs(rsaPrivKeyPath, "-forward-udp", fmt.Sprintf("%d/%s@%d", localPort, remoteAddr.IP, remoteAddr.Port))
command := exec.Command(ssh3Path, clientArgs...)
session, err := Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Expand Down
23 changes: 21 additions & 2 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/golang-jwt/jwt/v5"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/crypto/cryptobyte"
cryptobyte_asn1 "golang.org/x/crypto/cryptobyte/asn1"
)
Expand Down Expand Up @@ -164,13 +165,31 @@ func (q *DatagramsQueue) WaitNext(ctx context.Context) ([]byte, error) {
}

func JWTSigningMethodFromCryptoPubkey(pubkey crypto.PublicKey) (jwt.SigningMethod, error) {
log.Debug().Type("SigningMethodType", pubkey).Msg("fetching singing method from crypto.PublicKey")

switch pubkey.(type) {
case *rsa.PublicKey:
log.
Trace().
Type("SigningMethodType", pubkey).
Str("FoundSigningMethod", "RSA").
Msg("found public key type")
return jwt.SigningMethodRS256, nil
case *ed25519.PublicKey:
case ed25519.PublicKey:
log.
Trace().
Type("SigningMethodType", pubkey).
Str("FoundSigningMethod", "ED25519").
Msg("found public key type")
return jwt.SigningMethodEdDSA, nil
default:
log.
Error().
Type("SigningMethodType", pubkey).
Str("FoundSigningMethod", "unknown").
Msg("did not find public key type")
return nil, UnknownSSHPubkeyType{pubkey: pubkey}
}
return nil, UnknownSSHPubkeyType{pubkey: pubkey}
}

func Sha256Fingerprint(in []byte) string {
Expand Down