Skip to content

Commit

Permalink
Use ecdh package for more (un)marshal operations
Browse files Browse the repository at this point in the history
  • Loading branch information
hslatman committed Jul 9, 2024
1 parent f9dd5e7 commit 3e863b2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
10 changes: 6 additions & 4 deletions pemutil/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"crypto/cipher"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"encoding/binary"
Expand Down Expand Up @@ -188,15 +187,18 @@ func SerializeOpenSSHPrivateKey(key crypto.PrivateKey, opts ...Options) (*pem.Bl
return nil, errors.Errorf("error serializing key: unsupported curve %s", k.Curve.Params().Name)
}

pub := elliptic.Marshal(k.Curve, k.PublicKey.X, k.PublicKey.Y)
p, err := k.PublicKey.ECDH()
if err != nil {
return nil, errors.Wrapf(err, "failed converting *ecdsa.PublicKey to *ecdh.PublicKey")

Check warning on line 192 in pemutil/ssh.go

View check run for this annotation

Codecov / codecov/patch

pemutil/ssh.go#L192

Added line #L192 was not covered by tests
}

// Marshal public key.
pubKey := struct {
KeyType string
Curve string
Pub []byte
}{
keyType, curve, pub,
keyType, curve, p.Bytes(),
}
w.PubKey = ssh.Marshal(pubKey)

Expand All @@ -207,7 +209,7 @@ func SerializeOpenSSHPrivateKey(key crypto.PrivateKey, opts ...Options) (*pem.Bl
D *big.Int
Comment string
}{
curve, pub, k.D,
curve, p.Bytes(), k.D,
ctx.comment,
}
pk1.Keytype = keyType
Expand Down
18 changes: 12 additions & 6 deletions sshutil/sshutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package sshutil
import (
"crypto"
"crypto/dsa" //nolint:staticcheck // support for DSA fingerprints
"crypto/ecdh"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rsa"
"fmt"
"math/big"

"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
Expand Down Expand Up @@ -48,13 +50,17 @@ func cryptoSKPublicKey(pub ssh.PublicKey) (crypto.PublicKey, error) {
if err := ssh.Unmarshal(pub.Marshal(), &w); err != nil {
return nil, err
}
key := new(ecdsa.PublicKey)
key.Curve = elliptic.P256()
key.X, key.Y = elliptic.Unmarshal(key.Curve, w.Key)
if key.X == nil || key.Y == nil {
return nil, fmt.Errorf("invalid curve point")

p, err := ecdh.P256().NewPublicKey(w.Key)
if err != nil {
return nil, fmt.Errorf("failed decoding ECDSA key: %w", err)

Check warning on line 56 in sshutil/sshutil.go

View check run for this annotation

Codecov / codecov/patch

sshutil/sshutil.go#L56

Added line #L56 was not covered by tests
}
return key, nil

return &ecdsa.PublicKey{
Curve: elliptic.P256(),
X: big.NewInt(0).SetBytes(p.Bytes()[1:33]),
Y: big.NewInt(0).SetBytes(p.Bytes()[33:]),
}, nil
case "sk-ssh-ed25519@openssh.com":
var w struct {
Name string
Expand Down
7 changes: 5 additions & 2 deletions sshutil/sshutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"reflect"
"testing"

"github.com/stretchr/testify/require"
"go.step.sm/crypto/keyutil"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
Expand Down Expand Up @@ -36,14 +36,17 @@ func generateKey(t *testing.T, kty, crv string, size int) (crypto.PublicKey, ssh
}

func generateFakeSKKey(t *testing.T, pub crypto.PublicKey) ssh.PublicKey {
t.Helper()
switch k := pub.(type) {
case *ecdsa.PublicKey:
p, err := k.ECDH()
require.NoError(t, err)
w := struct {
Name string
ID string
Key []byte
Application string
}{"sk-ecdsa-sha2-nistp256@openssh.com", "nistp256", elliptic.Marshal(k.Curve, k.X, k.Y), "ssh"}
}{"sk-ecdsa-sha2-nistp256@openssh.com", "nistp256", p.Bytes(), "ssh"}
return &skKey{
typ: "sk-ecdsa-sha2-nistp256@openssh.com",
bytes: ssh.Marshal(w),
Expand Down

0 comments on commit 3e863b2

Please sign in to comment.