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

x/crypto/ssh: expose negotiated algorithms #58523

Open
hanwen opened this issue Feb 14, 2023 · 40 comments
Open

x/crypto/ssh: expose negotiated algorithms #58523

hanwen opened this issue Feb 14, 2023 · 40 comments
Labels
Proposal Proposal-Accepted Proposal-Crypto Proposal related to crypto packages or other security issues
Milestone

Comments

@hanwen
Copy link
Contributor

hanwen commented Feb 14, 2023

Folks running production servers are under opposing pressures, to both

  1. stop supporting obsolete or vulnerable algorithms and/or key sizes.
  2. keep serving customers and migrate them away from obsolete algorithms

they can only make informed decisions on what to do (reach out to customers, switch off algorithms etc.) if they have data which algorithms are actually in use.

For the TLS library, I think this can be achieved by inspecting https://pkg.go.dev/crypto/tls#ConnectionState.

The SSH library doesn't expose this information, more in particular:

  • Cipher, MAC, Compression in read/write directions
  • host key algorithm
  • key exchange algorithm.

this information is captured in type algorithms
https://cs.opensource.google/go/x/crypto/+/master:ssh/common.go;l=185;drc=6fad3dfc18918c2ac9c112e46b32473bd2e5e2f9

the proposal is to export type algorithms, and add

func Algorithms() Algorithms

to the ssh.Conn type.

This is technically an incompatible API change. However, the ssh.Conn (as well as ssh.Channel) are misdesigned, and should have been structs instead. They are not meant to be implemented by other package, so I think it's OK to add this method to the interface.

@gopherbot gopherbot added this to the Proposal milestone Feb 14, 2023
@seankhliao seankhliao changed the title proposal: x/crypto/ssh - expose negotiated algorithms. proposal: x/crypto/ssh: expose negotiated algorithms. Feb 14, 2023
@ianlancetaylor ianlancetaylor added the Proposal-Crypto Proposal related to crypto packages or other security issues label Feb 14, 2023
@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Feb 14, 2023
@ianlancetaylor ianlancetaylor changed the title proposal: x/crypto/ssh: expose negotiated algorithms. proposal: x/crypto/ssh: expose negotiated algorithms Feb 14, 2023
@ianlancetaylor
Copy link
Member

CC @golang/security

@rsc
Copy link
Contributor

rsc commented Mar 29, 2023

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc rsc moved this from Incoming to Active in Proposals Mar 29, 2023
@rsc
Copy link
Contributor

rsc commented Apr 5, 2023

/cc @FiloSottile and @rolandshoemaker for thoughts

@rsc
Copy link
Contributor

rsc commented May 30, 2023

The compatible way to do this would be to define

type AlgorithmsConn interface {
    Conn
    Algorithms() Algorithms
}

@rsc
Copy link
Contributor

rsc commented May 30, 2023

Talked to @FiloSottile. In general we are supportive of expanding things here. The algorithms struct right now has lots of other unexported fields. It is unclear to us what exactly the public API for Algorithms would be.

@hanwen
Copy link
Contributor Author

hanwen commented Jun 5, 2023

my proposal is

type DirectionAlgorithms struct {
	Cipher      string
	MAC         string
	Compression string
}
type Algorithms struct {
	Kex     string
	HostKey string
	W       DirectionAlgorithms
	R       DirectionAlgorithms
}

Regarding

type AlgorithmsConn interface {
    Conn
    Algorithms() Algorithms
}

This is backward compatible, but runs the risk that we'd create a new interface for every bit of info we'd want to add down the line.

Maybe we could do func AlgorithmsOf(c Conn) *Algorithms instead?

@rsc
Copy link
Contributor

rsc commented Jun 7, 2023

What do W and R stand for and are those the right names?

@hanwen
Copy link
Contributor Author

hanwen commented Jun 12, 2023

Oh, sure. How about:

type Algorithms struct {
	Kex     string
	HostKey string
	Write       DirectionAlgorithms
	Read       DirectionAlgorithms
}

We could also do

type Algorithms struct {
	Kex     string
	HostKey string
	ClientToServer       DirectionAlgorithms
	ServerToClient       DirectionAlgorithms
}

which is probably a bit more self-explanatory for callers, but it means slightly more complexity on the implementation side.

Also, I think callers will typically only be implementing either the client or the server, so Read and Write are sufficiently unambiguous.

@rolandshoemaker
Copy link
Member

There does seem to be a bunch of stuff we'd like to expose down the road, and expanding the Conn interface each time seems unideal. I'd probably vote for something like func AlgorithmsFor(Conn) Algorithms instead.

In terms of the shape of Algorithms I think just exporting all of the currently private fields seems fine. I don't think the ClientToServer/ServerToClient names are particularly clearer. I think it'd probably be simpler to just expand w and r to Write and Read, which paired with the Direction part of DirectionAlgorithms I think makes it clear what they represent.

@rsc
Copy link
Contributor

rsc commented Jun 21, 2023

Re AlgorithmsConn vs AlgorithmsFor(Conn), we've established the AlgorithmsConn "extension interface" pattern in other contexts already, specifically the fs.FS interface and its extension interfaces, so I think that's probably okay. So it sounds like

type Algorithms struct {
	Kex     string
	HostKey string
	Read       DirectionAlgorithms
	Write       DirectionAlgorithms
}

type AlgorithmsConn interface {
	Conn
	Algorithms() Algorithms
}

type DirectionAlgorithms struct {
	Cipher      string
	MAC         string
	Compression string
}

Do I have that right?

@rolandshoemaker
Copy link
Member

Yup, sounds right.

@hanwen
Copy link
Contributor Author

hanwen commented Jun 21, 2023

Let me look around the bug tracker to see if there are other kind of things we could potentially expose (one thing that comes to mind is wiring Context through the SSH package) to see if we could do a combined update.

@neild
Copy link
Contributor

neild commented Jun 21, 2023

This seems like a case where ssh.Conn should have been a concrete type to permit future expansion.

Perhaps we should add a concrete type, and say that the Conns returned by ssh package constructors can always be type asserted to *ssh.SSHConn? That would avoid the need to add a new interface every time we think of something that should be exposed.

@hanwen
Copy link
Contributor Author

hanwen commented Jun 21, 2023

This seems like a case where ssh.Conn should have been a concrete type to permit future expansion.

yes, I was a young and innocent Go developer :-)

Perhaps we should add a concrete type, and say that the Conns returned by ssh package constructors can always be type asserted to *ssh.SSHConn? That would avoid the need to add a new interface every time we think of something that should be exposed.

I like that idea better than the extended interfaces, but it will be a bit of work to add the type. Maybe ssh.Connection (to avoid the stutter.)

@rsc
Copy link
Contributor

rsc commented Jul 5, 2023

If we use #58523 (comment), have all concerns been addressed?

@hanwen
Copy link
Contributor Author

hanwen commented Jul 5, 2023

I wanted to do something like https://go-review.googlesource.com/c/crypto/+/507779

then we can add the algorithms as a normal method on Connection

@rsc
Copy link
Contributor

rsc commented Jul 12, 2023

Having both Conn and Connection doesn't sound right, even as a way to deal with legacy questions.

@hanwen
Copy link
Contributor Author

hanwen commented Jul 13, 2023

Having both Conn and Connection doesn't sound right, even as a way to deal with legacy questions.

aside from naming details, are you saying it is better that we should add a new interface for every method we add?

@rsc
Copy link
Contributor

rsc commented Jul 19, 2023

Are there a bunch more methods that will need to be added?

@rsc
Copy link
Contributor

rsc commented Jul 26, 2023

/cc @drakkan and @FiloSottile for thoughts

@rsc
Copy link
Contributor

rsc commented Nov 14, 2023

Probably 61537 is wrong and we should drop Algorithms from the remaining struct fields in 61537. It is strange to have it in some fields but not others, and the overall struct is named Algorithms so having that word in the fields is redundant.

@drakkan
Copy link
Member

drakkan commented Nov 15, 2023

Probably 61537 is wrong and we should drop Algorithms from the remaining struct fields in 61537. It is strange to have it in some fields but not others, and the overall struct is named Algorithms so having that word in the fields is redundant.

We already have HostKeyAlgorithms in ClientConfig and PublicKeyAuthAlgorithms in ServerConfig, keeping the same name may avoid confusion even if Algorithm is redundant, anyway we can also improve docs to avoid any confusion

@rsc
Copy link
Contributor

rsc commented Nov 15, 2023

I still think we should drop Algorithms inside the Algorithms struct. It is defensible to have them in ClientConfig and ServerConfig since that is not ClientConfigAlgorithms and ServerConfigAlgorithms.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2023

No change in consensus, so accepted. 🎉
This issue now tracks the work of implementing the proposal.
— rsc for the proposal review group

The proposal is to add

type NegotiatedAlgorithms struct {
	KeyExchange string 
	HostKey string
	Read    DirectionAlgorithms
	Write   DirectionAlgorithms
}

type AlgorithmsConnMetadata interface {
	ConnMetadata
	Algorithms() NegotiatedAlgorithms
}

type DirectionAlgorithms struct {
	Cipher      string
	MAC         string
}

@rsc rsc moved this from Likely Accept to Accepted in Proposals Dec 4, 2023
@rsc rsc changed the title proposal: x/crypto/ssh: expose negotiated algorithms x/crypto/ssh: expose negotiated algorithms Dec 4, 2023
@rsc rsc modified the milestones: Proposal, Backlog Dec 4, 2023
@drakkan
Copy link
Member

drakkan commented Dec 9, 2023

Thanks for accepting this proposal!
I rebased and adapted the CL that implements it. It depends on #61537 and approval of #54743 should also be discussed to avoid to have KEX algorithms supported only on the client side

@onemorezero
Copy link

Hi, this feature will be super useful, any updates on when it will be merged?

drakkan added a commit to drakkan/crypto that referenced this issue Feb 24, 2024
Fixes golang/go#58523

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue Mar 7, 2024
Fixes golang/go#58523

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue Apr 9, 2024
Fixes golang/go#58523

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue May 7, 2024
Fixes golang/go#58523

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue May 9, 2024
Fixes golang/go#58523

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue May 23, 2024
Fixes golang/go#58523
Fixes golang/go#46638

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue Jun 4, 2024
Fixes golang/go#58523
Fixes golang/go#46638

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue Jul 13, 2024
Fixes golang/go#58523
Fixes golang/go#46638

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue Jul 26, 2024
Fixes golang/go#58523
Fixes golang/go#46638

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue Aug 11, 2024
Fixes golang/go#58523
Fixes golang/go#46638

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue Aug 12, 2024
Fixes golang/go#58523
Fixes golang/go#46638

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue Oct 5, 2024
Fixes golang/go#58523
Fixes golang/go#46638

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue Nov 9, 2024
Fixes golang/go#58523
Fixes golang/go#46638

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue Nov 9, 2024
Fixes golang/go#58523
Fixes golang/go#46638

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue Dec 7, 2024
Fixes golang/go#58523
Fixes golang/go#46638

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue Dec 15, 2024
Fixes golang/go#58523
Fixes golang/go#46638

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue Dec 15, 2024
Fixes golang/go#58523
Fixes golang/go#46638

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
drakkan added a commit to drakkan/crypto that referenced this issue Jan 11, 2025
Fixes golang/go#58523
Fixes golang/go#46638

Change-Id: Ic64bd2fdd6e9ec96acac3ed4be842e2fbb15231d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Proposal Proposal-Accepted Proposal-Crypto Proposal related to crypto packages or other security issues
Projects
Status: Accepted
Development

No branches or pull requests

8 participants