crypto/ssh: Parse ECDSA key using struct Change parseECDSA() to unmarshal the key's contents into a struct representing the wire format, consistent with the parseRSA() and parseDSA(), to make the code more readable and its intent clearer. Change-Id: Iea85630107ac0b3e681807d2278390c8c50ce141 Reviewed-on: https://go-review.googlesource.com/13663 Reviewed-by: Han-Wen Nienhuys <hanwenn@gmail.com> Reviewed-by: Adam Langley <agl@golang.org>
diff --git a/ssh/keys.go b/ssh/keys.go index e8af511..3272d7c 100644 --- a/ssh/keys.go +++ b/ssh/keys.go
@@ -422,14 +422,19 @@ // parseECDSA parses an ECDSA key according to RFC 5656, section 3.1. func parseECDSA(in []byte) (out PublicKey, rest []byte, err error) { - identifier, in, ok := parseString(in) - if !ok { - return nil, nil, errShortRead + var w struct { + Curve string + KeyBytes []byte + Rest []byte `ssh:"rest"` + } + + if err := Unmarshal(in, &w); err != nil { + return nil, nil, err } key := new(ecdsa.PublicKey) - switch string(identifier) { + switch w.Curve { case "nistp256": key.Curve = elliptic.P256() case "nistp384": @@ -440,16 +445,11 @@ return nil, nil, errors.New("ssh: unsupported curve") } - var keyBytes []byte - if keyBytes, in, ok = parseString(in); !ok { - return nil, nil, errShortRead - } - - key.X, key.Y = elliptic.Unmarshal(key.Curve, keyBytes) + key.X, key.Y = elliptic.Unmarshal(key.Curve, w.KeyBytes) if key.X == nil || key.Y == nil { return nil, nil, errors.New("ssh: invalid curve point") } - return (*ecdsaPublicKey)(key), in, nil + return (*ecdsaPublicKey)(key), w.Rest, nil } func (key *ecdsaPublicKey) Marshal() []byte {