From d5c5f1769f2fcd2377be6f29863081f59a4fc80f Mon Sep 17 00:00:00 2001 From: Matt Bostock Date: Sun, 16 Aug 2015 22:25:54 +0100 Subject: [PATCH] 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 Reviewed-by: Adam Langley --- ssh/keys.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) 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 @@ func ecHash(curve elliptic.Curve) crypto.Hash { // 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 @@ func parseECDSA(in []byte) (out PublicKey, rest []byte, err error) { 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 {