Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh: sign and verify #316

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions ssh/sig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package ssh

import (
"crypto/sha512"
"encoding/pem"
"fmt"
"io"
)

// blob according to the SSHSIG protocol.
type blob struct {
Namespace string
Reserved string
HashAlgorithm string
Hash []byte
}

// signedData according to the SSHSIG protocol.
type signedData struct {
MagicPreamble [6]byte
Version uint32
PublicKey []byte
Namespace string
Reserved string
HashAlgorithm string
Signature []byte
}

const (
sigMagicPreamble = "SSHSIG"
sigVersion = 1
signHashAlgorithm = "sha512"
)

func createSignBlob(message []byte, namespace string) ([]byte, error) {
hash := sha512.New()
if _, err := hash.Write(message); err != nil {
return nil, err
}
return append([]byte(sigMagicPreamble), Marshal(blob{
Namespace: namespace,
HashAlgorithm: signHashAlgorithm,
Hash: hash.Sum(nil),
})...), nil
}

// Sign returns a detached SSH Signature for the provided message.
//
// The namespace is a domain-specific identifier for the context in which the
// signature will be used. It must match between the Sign and [Verify] calls. A
// fully-qualified suffix is recommended, e.g. "[email protected]".
//
// These signatures are compatible with those generated by "ssh-keygen -Y sign",
// and can be verified with [Verify] or "ssh-keygen -Y verify". The returned
// bytes are usually PEM encoded with [encoding/pem] and type "SSH SIGNATURE".
//
// If the Signer has an RSA PublicKey, it must also implement [AlgorithmSigner].
// If it also implements [MultiAlgorithmSigner], the first algorithm returned by
// Algorithms will be used, otherwise "rsa-sha2-512" is used.
func Sign(s Signer, rand io.Reader, message []byte, namespace string) ([]byte, error) {
signer, ok := s.(AlgorithmSigner)
if !ok {
return nil, fmt.Errorf("invalid signer")
}

algorithm := KeyAlgoRSASHA512
multiSigner, ok := s.(MultiAlgorithmSigner)
if ok {
algorithm = multiSigner.Algorithms()[0]
}

data, err := createSignBlob(message, namespace)
if err != nil {
return nil, err
}

sig, err := signer.SignWithAlgorithm(rand, data, algorithm)
if err != nil {
return nil, err
}

signedData := signedData{
Version: sigVersion,
PublicKey: s.PublicKey().Marshal(),
Namespace: namespace,
HashAlgorithm: signHashAlgorithm,
Signature: Marshal(sig),
}
copy(signedData.MagicPreamble[:], []byte(sigMagicPreamble))

return pem.EncodeToMemory(&pem.Block{
Type: "SSH SIGNATURE",
Bytes: Marshal(signedData),
}), nil
}

// Verify verifies a detached SSH Signature for the provided message.
//
// The namespace is a domain-specific identifier for the context in which the
// signature will be used. It must match between the [Sign] and Verify calls. A
// fully-qualified suffix is recommended, e.g. "[email protected]".
//
// The provided signature is usually decoded from a PEM block of type "SSH
// SIGNATURE" using [encoding/pem].
func Verify(pub PublicKey, message, signature []byte, namespace string) error {
var sig signedData
if err := Unmarshal(signature, &sig); err != nil {
return err
}
if sig.Version != sigVersion {
return fmt.Errorf("invalid version: %d", sig.Version)
}
if s := string(sig.MagicPreamble[:]); s != sigMagicPreamble {
return fmt.Errorf("invalid header: %s", s)
}
if sig.Namespace != namespace {
return fmt.Errorf("invalid namespace: %s", sig.Namespace)
}
if sig.HashAlgorithm != signHashAlgorithm {
return fmt.Errorf("invalid hash algorithm: %s", sig.HashAlgorithm)
}

var sshSig Signature
if err := Unmarshal(sig.Signature, &sshSig); err != nil {
return err
}

data, err := createSignBlob(message, namespace)
if err != nil {
return err
}

return pub.Verify(data, &sshSig)
}
28 changes: 28 additions & 0 deletions ssh/sig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ssh

import (
"crypto/rand"
"encoding/pem"
"testing"
)

func TestEd25519SignVerify(t *testing.T) {
signer, ok := testSigners["ed25519"]
if !ok {
t.Fatalf("cannot find signer: ed25519")
}

const message = "gopher test message"
const namespace = "gopher@test"

signature, err := Sign(signer, rand.Reader, []byte(message), namespace)
if err != nil {
t.Fatalf("could not sign: %v", err)
}

block, _ := pem.Decode(signature)
err = Verify(signer.PublicKey(), []byte(message), block.Bytes, namespace)
if err != nil {
t.Fatalf("could not verify signature: %v", err)
}
}