// Copyright 2026 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package ssh

import (
	
	
	
	

	
)

const (
	muxProtocolVersion = 4

	muxMsgHello = 0x00000001
	muxCProxy   = 0x1000000f
	muxSProxy   = 0x8000000f
)

const controlProxyRequestID = 0

// handshakeControlProxy attempts to establish a transport connection with an
// OpenSSH ControlMaster socket in proxy mode. For details see:
// https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.mux
func handshakeControlProxy( io.ReadWriteCloser) (connTransport, error) {
	if  := controlProxyWritePacket(, func( *cryptobyte.Builder) {
		.AddUint32(muxMsgHello)
		.AddUint32(muxProtocolVersion)
	});  != nil {
		return nil, fmt.Errorf("mux hello write failed: %w", )
	}
	if  := controlProxyWritePacket(, func( *cryptobyte.Builder) {
		.AddUint32(muxCProxy)
		.AddUint32(controlProxyRequestID)
	});  != nil {
		return nil, fmt.Errorf("mux client proxy write failed: %w", )
	}

	, ,  := controlProxyReadMessage()
	if  != nil {
		return nil, fmt.Errorf("mux hello read failed: %w", )
	}
	if  != muxMsgHello {
		return nil, fmt.Errorf("expected hello response, got %v", )
	}
	var  uint32
	if !.ReadUint32(&) {
		return nil, errors.New("EOF reading mux protocol version")
	}
	if  != muxProtocolVersion {
		return nil, fmt.Errorf("mux server has unsupported version %v", )
	}
	, ,  = controlProxyReadMessage()
	if  != nil {
		return nil, fmt.Errorf("mux server proxy read failed: %w", )
	}
	if  != muxSProxy {
		return nil, fmt.Errorf("expected server proxy response, got %v", )
	}
	var  uint32
	if !.ReadUint32(&) {
		return nil, errors.New("EOF reading request id")
	}
	if  != controlProxyRequestID {
		return nil, fmt.Errorf("expected request id %v, got %v", controlProxyRequestID, )
	}
	return &controlProxyTransport{}, nil
}

// controlProxyTransport implements the connTransport interface for
// ControlMaster connections. Each controlMessage has zero length padding and
// no MAC.
type controlProxyTransport struct {
	rw io.ReadWriteCloser
}

func ( *controlProxyTransport) () error {
	return .rw.Close()
}

func ( *controlProxyTransport) ( []byte) error {
	return controlProxyWritePacket(.rw, func( *cryptobyte.Builder) {
		.AddUint8(0) // Padding length.
		.AddBytes()
	})
}

func ( *controlProxyTransport) () ([]byte, error) {
	,  := controlProxyReadPacket(.rw)
	if  != nil {
		return nil, fmt.Errorf("ssh: error reading control message: %w", )
	}
	// Discard the padding length.
	if len() < 1 {
		return nil, errors.New("ssh: EOF reading padding length")
	}
	if [0] != 0 {
		return nil, errors.New("ssh: unexpected non-zero padding in control message")
	}
	return [1:], nil
}

func ( *controlProxyTransport) () NegotiatedAlgorithms {
	return NegotiatedAlgorithms{}
}

func ( *controlProxyTransport) () []byte {
	return nil
}

func ( *controlProxyTransport) () error {
	return nil
}

func controlProxyWritePacket( io.Writer,  cryptobyte.BuilderContinuation) error {
	var  []byte
	 := cryptobyte.NewBuilder()
	.AddUint32LengthPrefixed()
	,  := .Bytes()
	if  != nil {
		return 
	}
	_,  = .Write()
	return 
}

func controlProxyReadPacket( io.Reader) (cryptobyte.String, error) {
	var  uint32
	if  := binary.Read(, binary.BigEndian, &);  != nil {
		return nil, 
	}
	if  > maxPacket {
		return nil, fmt.Errorf("message length %v exceeds maximum %v", , maxPacket)
	}
	 := make([]byte, )
	if ,  := io.ReadFull(, );  != nil {
		return nil, 
	}
	return , nil
}

func controlProxyReadMessage( io.Reader) ( uint32,  cryptobyte.String,  error) {
	,  = controlProxyReadPacket()
	if  != nil {
		return 0, nil, fmt.Errorf("error reading message body: %w", )
	}
	if !.ReadUint32(&) {
		return 0, nil, errors.New("EOF reading message type")
	}
	return , , nil
}