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>
This commit is contained in:
committed by
Adam Langley
parent
2b8f0970a8
commit
d5c5f1769f
+11
-11
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user