Skip to content

Commit d2e5038

Browse files
author
Benjamin Wireman
authored
Allow for programaitc access of ssh result types (#260)
#260
1 parent 145470a commit d2e5038

File tree

9 files changed

+56
-56
lines changed

9 files changed

+56
-56
lines changed

lib/ssh/cipher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ type cbcCipher struct {
372372
oracleCamouflage uint32
373373
}
374374

375-
func newCBCCipher(c cipher.Block, iv, key, macKey []byte, algs directionAlgorithms) (packetCipher, error) {
375+
func newCBCCipher(c cipher.Block, iv, key, macKey []byte, algs DirectionAlgorithms) (packetCipher, error) {
376376
cbc := &cbcCipher{
377377
mac: macModes[algs.MAC].new(macKey),
378378
decrypter: cipher.NewCBCDecrypter(c, iv),
@@ -386,7 +386,7 @@ func newCBCCipher(c cipher.Block, iv, key, macKey []byte, algs directionAlgorith
386386
return cbc, nil
387387
}
388388

389-
func newAESCBCCipher(iv, key, macKey []byte, algs directionAlgorithms) (packetCipher, error) {
389+
func newAESCBCCipher(iv, key, macKey []byte, algs DirectionAlgorithms) (packetCipher, error) {
390390
c, err := aes.NewCipher(key)
391391
if err != nil {
392392
return nil, err
@@ -400,7 +400,7 @@ func newAESCBCCipher(iv, key, macKey []byte, algs directionAlgorithms) (packetCi
400400
return cbc, nil
401401
}
402402

403-
func newTripleDESCBCCipher(iv, key, macKey []byte, algs directionAlgorithms) (packetCipher, error) {
403+
func newTripleDESCBCCipher(iv, key, macKey []byte, algs DirectionAlgorithms) (packetCipher, error) {
404404
c, err := des.NewTripleDESCipher(key)
405405
if err != nil {
406406
return nil, err

lib/ssh/cipher_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestPacketCiphers(t *testing.T) {
2727

2828
for cipher := range cipherModes {
2929
kr := &kexResult{Hash: crypto.SHA1}
30-
algs := directionAlgorithms{
30+
algs := DirectionAlgorithms{
3131
Cipher: cipher,
3232
MAC: "hmac-sha1",
3333
Compression: "none",
@@ -68,7 +68,7 @@ func TestCBCOracleCounterMeasure(t *testing.T) {
6868
defer delete(cipherModes, aes128cbcID)
6969

7070
kr := &kexResult{Hash: crypto.SHA1}
71-
algs := directionAlgorithms{
71+
algs := DirectionAlgorithms{
7272
Cipher: aes128cbcID,
7373
MAC: "hmac-sha1",
7474
Compression: "none",

lib/ssh/common.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -119,74 +119,74 @@ func findCommon(what string, client []string, server []string) (common string, e
119119
return "", fmt.Errorf("ssh: no common algorithm for %s; client offered: %v, server offered: %v", what, client, server)
120120
}
121121

122-
type directionAlgorithms struct {
122+
type DirectionAlgorithms struct {
123123
Cipher string `json:"cipher"`
124124
MAC string `json:"mac"`
125125
Compression string `json:"compression"`
126126
}
127127

128-
type algorithms struct {
129-
kex string
130-
hostKey string
131-
w directionAlgorithms
132-
r directionAlgorithms
128+
type Algorithms struct {
129+
Kex string
130+
HostKey string
131+
W DirectionAlgorithms
132+
R DirectionAlgorithms
133133
}
134134

135-
func (alg *algorithms) MarshalJSON() ([]byte, error) {
135+
func (alg *Algorithms) MarshalJSON() ([]byte, error) {
136136
aux := struct {
137137
Kex string `json:"dh_kex_algorithm"`
138138
HostKey string `json:"host_key_algorithm"`
139-
W directionAlgorithms `json:"client_to_server_alg_group"`
140-
R directionAlgorithms `json:"server_to_client_alg_group"`
139+
W DirectionAlgorithms `json:"client_to_server_alg_group"`
140+
R DirectionAlgorithms `json:"server_to_client_alg_group"`
141141
}{
142-
Kex: alg.kex,
143-
HostKey: alg.hostKey,
144-
W: alg.w,
145-
R: alg.r,
142+
Kex: alg.Kex,
143+
HostKey: alg.HostKey,
144+
W: alg.W,
145+
R: alg.R,
146146
}
147147

148148
return json.Marshal(aux)
149149
}
150150

151-
func findAgreedAlgorithms(clientKexInit, serverKexInit *kexInitMsg) (algs *algorithms, err error) {
152-
result := &algorithms{}
151+
func findAgreedAlgorithms(clientKexInit, serverKexInit *KexInitMsg) (algs *Algorithms, err error) {
152+
result := &Algorithms{}
153153

154-
result.kex, err = findCommon("key exchange", clientKexInit.KexAlgos, serverKexInit.KexAlgos)
154+
result.Kex, err = findCommon("key exchange", clientKexInit.KexAlgos, serverKexInit.KexAlgos)
155155
if err != nil {
156156
return
157157
}
158158

159-
result.hostKey, err = findCommon("host key", clientKexInit.ServerHostKeyAlgos, serverKexInit.ServerHostKeyAlgos)
159+
result.HostKey, err = findCommon("host key", clientKexInit.ServerHostKeyAlgos, serverKexInit.ServerHostKeyAlgos)
160160
if err != nil {
161161
return
162162
}
163163

164-
result.w.Cipher, err = findCommon("client to server cipher", clientKexInit.CiphersClientServer, serverKexInit.CiphersClientServer)
164+
result.W.Cipher, err = findCommon("client to server cipher", clientKexInit.CiphersClientServer, serverKexInit.CiphersClientServer)
165165
if err != nil {
166166
return
167167
}
168168

169-
result.r.Cipher, err = findCommon("server to client cipher", clientKexInit.CiphersServerClient, serverKexInit.CiphersServerClient)
169+
result.R.Cipher, err = findCommon("server to client cipher", clientKexInit.CiphersServerClient, serverKexInit.CiphersServerClient)
170170
if err != nil {
171171
return
172172
}
173173

174-
result.w.MAC, err = findCommon("client to server MAC", clientKexInit.MACsClientServer, serverKexInit.MACsClientServer)
174+
result.W.MAC, err = findCommon("client to server MAC", clientKexInit.MACsClientServer, serverKexInit.MACsClientServer)
175175
if err != nil {
176176
return
177177
}
178178

179-
result.r.MAC, err = findCommon("server to client MAC", clientKexInit.MACsServerClient, serverKexInit.MACsServerClient)
179+
result.R.MAC, err = findCommon("server to client MAC", clientKexInit.MACsServerClient, serverKexInit.MACsServerClient)
180180
if err != nil {
181181
return
182182
}
183183

184-
result.w.Compression, err = findCommon("client to server compression", clientKexInit.CompressionClientServer, serverKexInit.CompressionClientServer)
184+
result.W.Compression, err = findCommon("client to server compression", clientKexInit.CompressionClientServer, serverKexInit.CompressionClientServer)
185185
if err != nil {
186186
return
187187
}
188188

189-
result.r.Compression, err = findCommon("server to client compression", clientKexInit.CompressionServerClient, serverKexInit.CompressionServerClient)
189+
result.R.Compression, err = findCommon("server to client compression", clientKexInit.CompressionServerClient, serverKexInit.CompressionServerClient)
190190
if err != nil {
191191
return
192192
}

lib/ssh/handshake.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type keyingTransport interface {
2828
// prepareKeyChange sets up a key change. The key change for a
2929
// direction will be effected if a msgNewKeys message is sent
3030
// or received.
31-
prepareKeyChange(*algorithms, *kexResult) error
31+
prepareKeyChange(*Algorithms, *kexResult) error
3232
}
3333

3434
// handshakeTransport implements rekeying on top of a keyingTransport
@@ -68,7 +68,7 @@ type handshakeTransport struct {
6868
mu sync.Mutex
6969
cond *sync.Cond
7070
sentInitPacket []byte
71-
sentInitMsg *kexInitMsg
71+
sentInitMsg *KexInitMsg
7272
writtenSinceKex uint64
7373
writeError error
7474

@@ -264,7 +264,7 @@ func (t *handshakeTransport) requestKeyChange() error {
264264

265265
// sendKexInitLocked sends a key change message. t.mu must be locked
266266
// while this happens.
267-
func (t *handshakeTransport) sendKexInitLocked(isFirst keyChangeCategory) (*kexInitMsg, []byte, error) {
267+
func (t *handshakeTransport) sendKexInitLocked(isFirst keyChangeCategory) (*KexInitMsg, []byte, error) {
268268
// kexInits may be sent either in response to the other side,
269269
// or because our side wants to initiate a key change, so we
270270
// may have already sent a kexInit. In that case, don't send a
@@ -273,7 +273,7 @@ func (t *handshakeTransport) sendKexInitLocked(isFirst keyChangeCategory) (*kexI
273273
return t.sentInitMsg, t.sentInitPacket, nil
274274
}
275275

276-
msg := &kexInitMsg{
276+
msg := &KexInitMsg{
277277
KexAlgos: t.config.KeyExchanges,
278278
CiphersClientServer: t.config.Ciphers,
279279
CiphersServerClient: t.config.Ciphers,
@@ -352,7 +352,7 @@ func (t *handshakeTransport) enterKeyExchangeLocked(otherInitPacket []byte) erro
352352
}
353353
}
354354

355-
otherInit := &kexInitMsg{}
355+
otherInit := &KexInitMsg{}
356356
if err := Unmarshal(otherInitPacket, otherInit); err != nil {
357357
return err
358358
}
@@ -403,12 +403,12 @@ func (t *handshakeTransport) enterKeyExchangeLocked(otherInitPacket []byte) erro
403403
}
404404
}
405405

406-
kex, ok := kexAlgoMap[algs.kex]
406+
kex, ok := kexAlgoMap[algs.Kex]
407407
if !ok {
408-
return fmt.Errorf("ssh: unexpected key exchange algorithm %v", algs.kex)
408+
return fmt.Errorf("ssh: unexpected key exchange algorithm %v", algs.Kex)
409409
}
410410

411-
kex = kex.GetNew(algs.kex)
411+
kex = kex.GetNew(algs.Kex)
412412

413413
if t.config.ConnLog != nil {
414414
t.config.ConnLog.DHKeyExchange = kex
@@ -447,10 +447,10 @@ func (t *handshakeTransport) enterKeyExchangeLocked(otherInitPacket []byte) erro
447447
return nil
448448
}
449449

450-
func (t *handshakeTransport) server(kex kexAlgorithm, algs *algorithms, magics *handshakeMagics) (*kexResult, error) {
450+
func (t *handshakeTransport) server(kex kexAlgorithm, algs *Algorithms, magics *handshakeMagics) (*kexResult, error) {
451451
var hostKey Signer
452452
for _, k := range t.hostKeys {
453-
if algs.hostKey == k.PublicKey().Type() {
453+
if algs.HostKey == k.PublicKey().Type() {
454454
hostKey = k
455455
}
456456
}
@@ -459,7 +459,7 @@ func (t *handshakeTransport) server(kex kexAlgorithm, algs *algorithms, magics *
459459
return r, err
460460
}
461461

462-
func (t *handshakeTransport) client(kex kexAlgorithm, algs *algorithms, magics *handshakeMagics) (*kexResult, error) {
462+
func (t *handshakeTransport) client(kex kexAlgorithm, algs *Algorithms, magics *handshakeMagics) (*kexResult, error) {
463463
result, err := kex.Client(t.conn, t.config.Rand, magics, t.config)
464464
if err != nil {
465465
return nil, err

lib/ssh/handshake_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ type errorKeyingTransport struct {
354354
readLeft, writeLeft int
355355
}
356356

357-
func (n *errorKeyingTransport) prepareKeyChange(*algorithms, *kexResult) error {
357+
func (n *errorKeyingTransport) prepareKeyChange(*Algorithms, *kexResult) error {
358358
return nil
359359
}
360360
func (n *errorKeyingTransport) getSessionID() []byte {

lib/ssh/log.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ type HandshakeLog struct {
2020
Banner string `json:"banner,omitempty"`
2121
ServerID *EndpointId `json:"server_id,omitempty"`
2222
ClientID *EndpointId `json:"client_id,omitempty"`
23-
ServerKex *kexInitMsg `json:"server_key_exchange,omitempty"`
24-
ClientKex *kexInitMsg `json:"client_key_exchange,omitempty"`
25-
AlgorithmSelection *algorithms `json:"algorithm_selection,omitempty"`
23+
ServerKex *KexInitMsg `json:"server_key_exchange,omitempty"`
24+
ClientKex *KexInitMsg `json:"client_key_exchange,omitempty"`
25+
AlgorithmSelection *Algorithms `json:"algorithm_selection,omitempty"`
2626
DHKeyExchange kexAlgorithm `json:"key_exchange,omitempty"`
2727
UserAuth []string `json:"userauth,omitempty"`
2828
Crypto *kexResult `json:"crypto,omitempty"`

lib/ssh/messages.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (d *disconnectMsg) Error() string {
5454
// See RFC 4253, section 7.1.
5555
const msgKexInit = 20
5656

57-
type kexInitMsg struct {
57+
type KexInitMsg struct {
5858
Cookie [16]byte `sshtype:"20"`
5959
KexAlgos []string
6060
ServerHostKeyAlgos []string
@@ -86,7 +86,7 @@ type JsonKexInitMsg struct {
8686
Reserved uint32 `json:"reserved"`
8787
}
8888

89-
func (kex *kexInitMsg) MarshalJSON() ([]byte, error) {
89+
func (kex *KexInitMsg) MarshalJSON() ([]byte, error) {
9090
temp := JsonKexInitMsg{
9191
Cookie: kex.Cookie[:],
9292
KexAlgos: kex.KexAlgos,
@@ -753,7 +753,7 @@ func decode(packet []byte) (interface{}, error) {
753753
case msgServiceAccept:
754754
msg = new(serviceAcceptMsg)
755755
case msgKexInit:
756-
msg = new(kexInitMsg)
756+
msg = new(KexInitMsg)
757757
case msgKexDHInit:
758758
msg = new(kexDHInitMsg)
759759
case msgKexDHReply:

lib/ssh/messages_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestUnmarshalShortKexInitPacket(t *testing.T) {
166166
// This used to panic.
167167
// Issue 11348
168168
packet := []byte{0x14, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xff, 0xff, 0xff, 0xff}
169-
kim := &kexInitMsg{}
169+
kim := &KexInitMsg{}
170170
if err := Unmarshal(packet, kim); err == nil {
171171
t.Error("truncated packet unmarshaled without error")
172172
}
@@ -228,8 +228,8 @@ func randomInt(rand *rand.Rand) *big.Int {
228228
return new(big.Int).SetInt64(int64(int32(rand.Uint32())))
229229
}
230230

231-
func (*kexInitMsg) Generate(rand *rand.Rand, size int) reflect.Value {
232-
ki := &kexInitMsg{}
231+
func (*KexInitMsg) Generate(rand *rand.Rand, size int) reflect.Value {
232+
ki := &KexInitMsg{}
233233
randomBytes(ki.Cookie[:], rand)
234234
ki.KexAlgos = randomNameList(rand)
235235
ki.ServerHostKeyAlgos = randomNameList(rand)
@@ -254,7 +254,7 @@ func (*kexDHInitMsg) Generate(rand *rand.Rand, size int) reflect.Value {
254254
}
255255

256256
var (
257-
_kexInitMsg = new(kexInitMsg).Generate(rand.New(rand.NewSource(0)), 10).Elem().Interface()
257+
_kexInitMsg = new(KexInitMsg).Generate(rand.New(rand.NewSource(0)), 10).Elem().Interface()
258258
_kexDHInitMsg = new(kexDHInitMsg).Generate(rand.New(rand.NewSource(0)), 10).Elem().Interface()
259259

260260
_kexInit = Marshal(_kexInitMsg)
@@ -268,7 +268,7 @@ func BenchmarkMarshalKexInitMsg(b *testing.B) {
268268
}
269269

270270
func BenchmarkUnmarshalKexInitMsg(b *testing.B) {
271-
m := new(kexInitMsg)
271+
m := new(KexInitMsg)
272272
for i := 0; i < b.N; i++ {
273273
Unmarshal(_kexInit, m)
274274
}

lib/ssh/transport.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ type connectionState struct {
6868
// prepareKeyChange sets up key material for a keychange. The key changes in
6969
// both directions are triggered by reading and writing a msgNewKey packet
7070
// respectively.
71-
func (t *transport) prepareKeyChange(algs *algorithms, kexResult *kexResult) error {
72-
if ciph, err := newPacketCipher(t.reader.dir, algs.r, kexResult); err != nil {
71+
func (t *transport) prepareKeyChange(algs *Algorithms, kexResult *kexResult) error {
72+
if ciph, err := newPacketCipher(t.reader.dir, algs.R, kexResult); err != nil {
7373
return err
7474
} else {
7575
t.reader.pendingKeyChange <- ciph
7676
}
7777

78-
if ciph, err := newPacketCipher(t.writer.dir, algs.w, kexResult); err != nil {
78+
if ciph, err := newPacketCipher(t.writer.dir, algs.W, kexResult); err != nil {
7979
return err
8080
} else {
8181
t.writer.pendingKeyChange <- ciph
@@ -192,7 +192,7 @@ var (
192192
)
193193

194194
// generateKeys generates key material for IV, MAC and encryption.
195-
func generateKeys(d direction, algs directionAlgorithms, kex *kexResult) (iv, key, macKey []byte) {
195+
func generateKeys(d direction, algs DirectionAlgorithms, kex *kexResult) (iv, key, macKey []byte) {
196196
cipherMode := cipherModes[algs.Cipher]
197197
macMode := macModes[algs.MAC]
198198

@@ -209,7 +209,7 @@ func generateKeys(d direction, algs directionAlgorithms, kex *kexResult) (iv, ke
209209
// setupKeys sets the cipher and MAC keys from kex.K, kex.H and sessionId, as
210210
// described in RFC 4253, section 6.4. direction should either be serverKeys
211211
// (to setup server->client keys) or clientKeys (for client->server keys).
212-
func newPacketCipher(d direction, algs directionAlgorithms, kex *kexResult) (packetCipher, error) {
212+
func newPacketCipher(d direction, algs DirectionAlgorithms, kex *kexResult) (packetCipher, error) {
213213
iv, key, macKey := generateKeys(d, algs, kex)
214214

215215
if algs.Cipher == gcmCipherID {

0 commit comments

Comments
 (0)