sshsig

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 Imports: 11 Imported by: 18 modules, 12 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sshsig

Go Reference

This Go library implements the SSHSIG wire protocol, and can be used to sign and verify messages using SSH keys.

Compared to other implementations, this library does all the following:

  • Accepts an io.Reader as input for signing and verifying messages.
  • Performs simple public key fingerprint and namespace mismatch checks in Verify. Malicious input will still fail signature verification, but this provides more useful error messages.
  • Properly uses rsa-sha2-256 or rsa-sha2-512 (matching the hash algorithm, like OpenSSH) as signature algorithm when signing with an RSA private key, as described in the protocol.
  • Does not accept a Sign operation without a namespace as specified in the protocol.
  • Allows Verify operations to be performed with an empty namespace, for compatibility with loose implementations. An empty namespace only matches signatures created with an empty namespace; it does not skip the namespace check.
  • Rejects DSA keys, which are no longer supported by OpenSSH.
  • Provides Armor and Unarmor functions to encode/decode the signature to/from an (armored) PEM format.

For more information about the use of this library, see the Go Reference.

Acknowledgements

There are several other implementations of the SSHSIG protocol in Go, from which this library has borrowed ideas:

Documentation

Overview

Package sshsig provides an API to sign and verify messages using SSH keys. It is an implementation of the SSH Signature format as described in https://github.com/openssh/openssh-portable/blob/V_9_2_P1/PROTOCOL.sshsig.

Index

Examples

Constants

View Source
const PEMType = "SSH SIGNATURE"

PEMType is the PEM type of an armored SSH signature.

Variables

View Source
var (
	// ErrUnsupportedHashAlgorithm is returned when the hash algorithm is not
	// supported by the protocol.
	ErrUnsupportedHashAlgorithm = errors.New("unsupported hash algorithm")
	// ErrUnavailableHashAlgorithm is returned when the hash algorithm is
	// supported, but its implementation is not linked into the binary.
	ErrUnavailableHashAlgorithm = errors.New("unavailable hash algorithm")
)
View Source
var (
	// ErrUnsupportedKeyType is returned for DSA keys and certificates. DSA is
	// limited to SHA-1, and OpenSSH no longer supports it.
	ErrUnsupportedKeyType = errors.New("unsupported key type")

	// ErrUnsupportedSignatureFormat is returned when the signature algorithm
	// is not allowed for the public key, e.g. "ssh-rsa" (SHA-1) for an RSA
	// key, or does not match the key type.
	ErrUnsupportedSignatureFormat = errors.New("unsupported signature format")
)
View Source
var (
	// ErrPublicKeyMismatch is returned by Verify if the public key used to
	// verify the signature is nil, or does not match the public key in the
	// signature.
	ErrPublicKeyMismatch = errors.New("public key does not match")

	// ErrNamespaceMismatch is returned by Verify if the namespace in the signature
	// does not match the namespace used to verify the signature.
	ErrNamespaceMismatch = errors.New("namespace does not match")

	// ErrHashAlgorithmMismatch is returned by Verify if the hash algorithm in
	// the signature does not match the hash algorithm used to verify the
	// signature.
	ErrHashAlgorithmMismatch = errors.New("hash algorithm does not match")

	// ErrSignatureMismatch is returned by Verify if the signature is not a
	// valid signature of the message.
	ErrSignatureMismatch = errors.New("signature does not match")
)
View Source
var (
	// ErrUnsupportedSignatureVersion is returned when the signature version is
	// not supported.
	ErrUnsupportedSignatureVersion = errors.New("unsupported signature version")
	// ErrInvalidMagicPreamble is returned when the magic preamble is not
	// "SSHSIG".
	ErrInvalidMagicPreamble = errors.New("invalid magic preamble")
	// ErrMalformedSignature is returned when a Signature is incomplete, or its
	// encoding is invalid, e.g. trailing data after the signature. It is also
	// returned, together with ErrInvalidMagicPreamble, when the magic preamble
	// is not "SSHSIG".
	ErrMalformedSignature = errors.New("malformed signature")
)
View Source
var ErrInvalidPEM = errors.New("invalid PEM")

ErrInvalidPEM is returned when the input holds no PEM block, or its first block is not of type PEMType.

View Source
var ErrMissingNamespace = errors.New("missing namespace")

ErrMissingNamespace is returned by Sign if the namespace value is missing.

Functions

func Armor

func Armor(s *Signature) []byte

Armor returns a PEM-encoded Signature. It does not perform any validation.

func Verify

func Verify(m io.Reader, sig *Signature, pub ssh.PublicKey, h HashAlgorithm, namespace string) error

Verify verifies that sig is a valid signature of the message read from m, made with the key pub, the hash algorithm h and the namespace.

Verify returns an error wrapping:

  • ErrMalformedSignature if sig, sig.PublicKey or sig.Signature is nil, or the encoding of sig.PublicKey or sig.Signature is invalid.
  • ErrUnsupportedSignatureVersion if sig.Version is not supported.
  • ErrPublicKeyMismatch if pub is nil or does not equal sig.PublicKey.
  • ErrNamespaceMismatch if namespace does not equal sig.Namespace.
  • ErrUnsupportedHashAlgorithm or ErrUnavailableHashAlgorithm if h cannot be used.
  • ErrHashAlgorithmMismatch if h does not equal sig.HashAlgorithm.
  • ErrUnsupportedSignatureFormat if the signature algorithm is not allowed for the key, e.g. "ssh-rsa" (SHA-1) for an RSA key, or does not match the key type.
  • ErrUnsupportedKeyType if the key is a DSA key or certificate.
  • ErrSignatureMismatch if the signature does not match the message.

Any other error is returned while reading m.

The namespace specifies an unambiguous interpretation domain for the signature, e.g. file signing. This prevents cross-protocol attacks caused by signatures intended for one domain being accepted in another. Unlike Sign, Verify accepts an empty namespace to allow verification of signatures created by looser implementations. An empty namespace is not a wildcard: it only matches signatures with an empty namespace.

Example
// Load a public key to verify with.
pub, _, _, _, err := ssh.ParseAuthorizedKey([]byte(ed25519PublicKey))
if err != nil {
	panic(err)
}

// Load the armored (PEM) signature to verify.
armored := []byte(`-----BEGIN SSH SIGNATURE-----
U1NIU0lHAAAAAQAAAGgAAAATZWNkc2Etc2hhMi1uaXN0cDI1NgAAAAhuaXN0cDI1
NgAAAEEEOIYuWF0v/w8XVrOLUa30nMhLwiXdsf4aow88kfpnfA/Zn+Xhr9nRh97e
tNV1/Kqv1VE/On/YH+094IhlatyELQAAAARmaWxlAAAAAAAAAAZzaGE1MTIAAABk
AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAABJAAAAIBXp90537Om8Xbv0iTxVwvSy
iZmhAca7kPt0uSg0IVtTAAAAIQCbN+co4miAJ7t9XLIQuOaOQCM5P0AxRCdsMG4e
BnAL0w==
-----END SSH SIGNATURE-----`)
sig, err := sshsig.Unarmor(armored)
if err != nil {
	panic(err)
}

// Verify the signature, using the same hash algorithm and namespace as
// used to sign the message. If the signature is valid, no error is
// returned.
message := []byte("Hello world!")
if err := sshsig.Verify(bytes.NewReader(message), sig, pub, sig.HashAlgorithm, sig.Namespace); err != nil {
	panic(err)
}

// When more strict verification is required, the hash algorithm and/or
// namespace can be checked against the expected values.
if err := sshsig.Verify(bytes.NewReader(message), sig, pub, sshsig.HashSHA512, "file"); err != nil {
	panic(err)
}

Types

type HashAlgorithm

type HashAlgorithm string

HashAlgorithm represents an algorithm used to compute a hash of a message.

const (
	// HashSHA256 is the SHA-256 hash algorithm.
	HashSHA256 HashAlgorithm = "sha256"
	// HashSHA512 is the SHA-512 hash algorithm.
	HashSHA512 HashAlgorithm = "sha512"
)

func SupportedHashAlgorithms

func SupportedHashAlgorithms() []HashAlgorithm

SupportedHashAlgorithms returns a sorted list of supported hash algorithms.

func (HashAlgorithm) Available

func (h HashAlgorithm) Available() error

Available returns ErrUnsupportedHashAlgorithm if the hash algorithm is not supported, ErrUnavailableHashAlgorithm if the hash algorithm is not available, nil otherwise.

func (HashAlgorithm) Hash deprecated

func (h HashAlgorithm) Hash() hash.Hash

Hash returns a new hash.Hash computing the hash algorithm. It panics if the hash algorithm cannot be used.

Deprecated: Use New, which returns an error instead of panicking.

func (HashAlgorithm) New added in v0.3.0

func (h HashAlgorithm) New() (hash.Hash, error)

New returns a new hash.Hash computing the hash algorithm. It returns the error from Available if the hash algorithm cannot be used.

func (HashAlgorithm) String

func (h HashAlgorithm) String() string

String returns the string representation of the hash algorithm.

func (HashAlgorithm) Supported

func (h HashAlgorithm) Supported() error

Supported returns ErrUnsupportedHashAlgorithm if the hash algorithm is not supported, nil otherwise. Use Available if the intention is to make use of the hash algorithm, as it also checks if the hash algorithm is available.

type Signature

type Signature struct {
	// Version is the version of the signature format.
	// It currently supports version 1, any other value will be rejected with
	// ErrUnsupportedSignatureVersion.
	Version uint32
	// PublicKey is the public key used to create the Signature.
	PublicKey ssh.PublicKey
	// Namespace is the domain of the signature, and is used to prevent signature
	// reuse across different applications.
	Namespace string
	// HashAlgorithm is the hash algorithm used to hash the Signature message.
	HashAlgorithm HashAlgorithm
	// Signature is the SSH signature of the hash of the message.
	Signature *ssh.Signature
}

Signature represents the SSH signature of a message. It can be marshaled into an SSH wire format using Marshal, or into an armored (PEM) format using Armor.

Manual construction of this type is not recommended. Use ParseSignature or Unarmor instead to retrieve a Signature from a wire or armored (PEM) format.

func ParseSignature

func ParseSignature(b []byte) (*Signature, error)

ParseSignature parses a signature in SSH wire format into a Signature. It checks the signature against the requirements of the protocol, but does not verify it: use Verify for that.

ParseSignature returns an error wrapping:

  • ErrMalformedSignature if b cannot be decoded, or has trailing data.
  • ErrInvalidMagicPreamble, together with ErrMalformedSignature, if b does not start with the magic preamble "SSHSIG".
  • ErrUnsupportedSignatureVersion if the version is not supported.
  • ErrUnsupportedHashAlgorithm if the hash algorithm is not supported.
  • ErrUnsupportedKeyType if the key is a DSA key or certificate.
  • ErrUnsupportedSignatureFormat if the signature algorithm is not allowed for the key, e.g. "ssh-rsa" (SHA-1) for an RSA key, or does not match the key type.

func Sign

func Sign(m io.Reader, signer ssh.Signer, h HashAlgorithm, namespace string) (*Signature, error)

Sign generates a signature of the message from the io.Reader using the given ssh.Signer private key. The signature hash is computed using the provided HashAlgorithm.

The purpose of the namespace value is to specify an unambiguous interpretation domain for the signature, e.g. file signing. This prevents cross-protocol attacks caused by signatures intended for one domain being accepted in another. The namespace must not be empty, or ErrMissingNamespace will be returned.

When the signer is an RSA key or an RSA certificate, the signature algorithm is "rsa-sha2-256" for HashSHA256 and "rsa-sha2-512" for HashSHA512, as OpenSSH does. The signer must then implement ssh.AlgorithmSigner, or an error is returned.

Sign returns an error wrapping ErrUnsupportedSignatureFormat if the signer returns a signature with an algorithm the protocol does not allow for the key, e.g. an SSH agent signing with "ssh-rsa", and an error wrapping ErrMalformedSignature if the signature has trailing data.

Sign returns an error wrapping ErrUnsupportedKeyType for DSA keys and certificates.

The returned Signature holds the signature over the hash of the message and the metadata needed to verify it, but not the message itself.

Example
// Load the private key to sign with.
signer, err := ssh.ParsePrivateKey([]byte(ecdsaPrivateKey))
if err != nil {
	panic(err)
}

// Sign a message with the private key, using an SHA-512 hash and the
// namespace "file".
message := []byte("Hello world!")
sig, err := sshsig.Sign(bytes.NewReader(message), signer, sshsig.HashSHA512, "file")
if err != nil {
	panic(err)
}

// Print the signature in armored (PEM) format.
armored := sshsig.Armor(sig)
fmt.Printf("%s", armored)

func SignWithRand

func SignWithRand(m, rand io.Reader, signer ssh.Signer, h HashAlgorithm, namespace string) (*Signature, error)

SignWithRand is like Sign, but uses the provided rand io.Reader to create any necessary random values. Most callers likely want to use Sign instead.

func Unarmor

func Unarmor(b []byte) (*Signature, error)

Unarmor decodes a PEM-encoded signature into a Signature. It returns an error wrapping ErrInvalidPEM if the PEM block is missing or of the wrong type, or the error from ParseSignature if parsing the signature fails.

func (Signature) Marshal

func (s Signature) Marshal() []byte

Marshal returns the Signature in SSH wire format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址