Documentation ¶
Overview ¶
Package sigs implements the IETF draft specification "Signing HTTP Messages" https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures
Index ¶
- Constants
- func ApplyDigest(request *http.Request, digestName string, digestFunc DigestFunc) error
- func DecodePrivatePEM(pemString string) (crypto.PrivateKey, error)
- func DecodePublicPEM(pemString string) (crypto.PublicKey, error)
- func DigestSHA256(body []byte) string
- func DigestSHA512(body []byte) string
- func EncodePrivatePEM(privateKey *rsa.PrivateKey) string
- func EncodePublicPEM(privateKey *rsa.PrivateKey) string
- func GetSignature(request *http.Request) string
- func HasSignature(request *http.Request) bool
- func Sign(request *http.Request, publicKeyID string, privateKey crypto.PrivateKey, ...) error
- func Verify(request *http.Request, keyFinder PublicKeyFinder, options ...VerifierOption) error
- func VerifyDigest(request *http.Request, allowedHashes ...crypto.Hash) error
- func WithSigner(signer Signer) remote.Option
- type DigestFunc
- type PublicKeyFinder
- type Signature
- func (signature Signature) AlgorithmPrefix() string
- func (signature Signature) Base64() string
- func (signature Signature) Bytes() []byte
- func (signature Signature) CreatedString() string
- func (signature Signature) ExpiresString() string
- func (signature Signature) IsExpired(duration int) bool
- func (signature Signature) String() string
- type Signer
- type SignerOption
- type Verifier
- type VerifierOption
- func VerifierBodyDigests(digests ...crypto.Hash) VerifierOption
- func VerifierFields(fields ...string) VerifierOption
- func VerifierIgnoreBodyDigest() VerifierOption
- func VerifierIgnoreTimeout() VerifierOption
- func VerifierSignatureHashes(hashes ...crypto.Hash) VerifierOption
- func VerifierTimeout(seconds int) VerifierOption
Constants ¶
const Algorithm_ECDSA_SHA256 = "ecdsa-sha256"
Deprecated. The “ecdsa-sha256” signature algorithm. Deprecated by the standard because it reveals which hash and digest algorithm is used.
const Algorithm_ECDSA_SHA512 = "ecdsa-sha512"
const Algorithm_HMAC_SHA256 = "hmac-sha256"
Deprecated. The “hmac-sha256” signature algorithm. Deprecated by the standard because it reveals which hash and digest algorithm is used.
const Algorithm_HMAC_SHA512 = "hmac-sha512"
TODO: Are these supported by the actual specs?
const Algorithm_HS2019 = "hs2019"
The “hs2019” signature algorithm. This is the only non-deprecated algorithm. Unlike the other algorithms, the hash and digest functions are not implied by the choice of this signature algorithm. Instead, the hash and digest functions are chosen based on the key used. RSA, HMAC, and ECDSA keys are all supported. TODO: How to implement hs2019?
const Algorithm_RSA_SHA256 = "rsa-sha256"
Deprecated. The “rsa-sha256” signature algorithm. Deprecated by the standard because it reveals which hash and digest algorithm is used.
const Algorithm_RSA_SHA512 = "rsa-sha512"
const FieldCreated = "(created)"
FieldCreated is not supported at this time, and will generate an error. https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures#section-2.3
const FieldDate = "date"
const FieldDigest = "digest"
FieldDigest represents the Digest header field that validates the request body. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Digest https://datatracker.ietf.org/doc/draft-ietf-httpbis-digest-headers/
const FieldExpires = "(expires)"
FieldExpires is not supported at this time, and will generate an error. https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures#section-2.3
const FieldHost = "host"
const FieldRequestTarget = "(request-target)"
Variables ¶
This section is empty.
Functions ¶
func ApplyDigest ¶
func ApplyDigest(request *http.Request, digestName string, digestFunc DigestFunc) error
ApplyDigest calculates the digest of the body from a given http.Request, then adds the digest to the Request's header.
func DecodePrivatePEM ¶
func DecodePrivatePEM(pemString string) (crypto.PrivateKey, error)
DecodePrivatePEM converts a PEM string into a private key
func DecodePublicPEM ¶
DecodePublicPEM converts a PEM string into a public key
func DigestSHA256 ¶
DigestSHA256 calculates the SHA-256 digest of a slice of bytes
func DigestSHA512 ¶
DigestSHA512 calculates the SHA-512 digest of a given slice of bytes
func EncodePrivatePEM ¶
func EncodePrivatePEM(privateKey *rsa.PrivateKey) string
EncodePrivatePEM converts a private key into a PEM string
func EncodePublicPEM ¶
func EncodePublicPEM(privateKey *rsa.PrivateKey) string
EncodePublicPEM converts a public key into a PEM string
func GetSignature ¶
GetSignature returns the HTTP Signature from the request
func HasSignature ¶
HasSignature returns TRUE if the request has a Signature header
func Sign ¶
func Sign(request *http.Request, publicKeyID string, privateKey crypto.PrivateKey, options ...SignerOption) error
Sign signs a given http.Request. It is syntactic sugar for NewSigner(options...).Sign(request)
func Verify ¶
func Verify(request *http.Request, keyFinder PublicKeyFinder, options ...VerifierOption) error
Verify verifies the given http.Request. This is syntactic sugar for NewVerifier(options...).Verify(request)
func VerifyDigest ¶
VerifyDigest verifies that the digest in the http.Request header matches the contents of the http.Request body.
func WithSigner ¶
WithSigner is a remote.Option that signs an outbound HTTP request
Types ¶
type DigestFunc ¶
DigestFunc defines a function that calculates the digest of a given byte array
type PublicKeyFinder ¶
SignatureFinder is a function that can look up a public key. This is injected into the Verify function by the inbox.
type Signature ¶
type Signature struct { KeyID string // ID (URL) of the key used to create this signature Algorithm string // Algorithm used to create this signature (should be ignored per IEFT spec) Headers []string // List of headers that were signed Signature []byte // Base64 encoded signature Created int64 // Unix epoch (in seconds) when this signature was created Expires int64 // Unix epoch (in seconds) when this signature expires }
https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures#section-2.1
func NewSignature ¶
func NewSignature() Signature
NewSignature returns a fully initialized Signature object
func ParseSignature ¶
ParseSignature parses a string into an HTTP Signature
func (Signature) AlgorithmPrefix ¶
AlgorithmPrefix returns the first part of the algorithm name, such as "rsa", "hmac", or "ecdsa"
func (Signature) CreatedString ¶
func (Signature) ExpiresString ¶
type Signer ¶
type Signer struct { PublicKeyID string PrivateKey crypto.PrivateKey Fields []string SignatureHash crypto.Hash BodyDigest crypto.Hash HS2019 bool Created int64 Expires int64 }
Signer contains all of the settings necessary to sign a request
func NewSigner ¶
func NewSigner(publicKeyID string, privateKey crypto.PrivateKey, options ...SignerOption) Signer
NewSigner returns a fully initialized Signer
func (*Signer) MakeSignature ¶
MakeSignature generates a Signature string for the given http.Request
func (*Signer) With ¶
func (signer *Signer) With(options ...SignerOption)
Use applies the given options to the Signer
type SignerOption ¶
type SignerOption func(*Signer)
SignerOption is a function that modifies a Signer
func SignerBodyDigest ¶
func SignerBodyDigest(digest crypto.Hash) SignerOption
SignerBodyDigests sets the digest algorithm to be used when creating the "Digest" header.
func SignerCreated ¶
func SignerCreated(created int64) SignerOption
func SignerExpires ¶
func SignerExpires(expires int64) SignerOption
func SignerFields ¶
func SignerFields(fields ...string) SignerOption
SignerFields sets the http.Request fields to be signed
func SignerSignatureHash ¶
func SignerSignatureHash(hash crypto.Hash) SignerOption
SignerSignatureDigest sets the hashing algorithm to be used when we sign a request.
type Verifier ¶
type Verifier struct { Fields []string BodyDigests []crypto.Hash // List of algorithms to accept from remote servers when they create a Digest header. Default is SHA256 and SHA512 SignatureHashes []crypto.Hash // Digest algorithm used to create the signature. Default is SHA256, SHA512 Timeout int // Number of seconds before signatures are expired. Default is 43200 seconds (12 hours). CheckDigest bool // If true, then the verifier will check the Digest header. Default is true. }
Verifier contains all of the settings necessary to verify a request
func NewVerifier ¶
func NewVerifier(options ...VerifierOption) Verifier
NewVerifier returns a fully initialized Verifier
func (*Verifier) Use ¶
func (verifier *Verifier) Use(options ...VerifierOption)
Use applies the given options to the Verifier
type VerifierOption ¶
type VerifierOption func(*Verifier)
VerifierOption is a function that modifies a Verifier
func VerifierBodyDigests ¶
func VerifierBodyDigests(digests ...crypto.Hash) VerifierOption
VerifierDigests sets the list of algorithms that we will accept from remote servers when they create a "Digest" http header. ALL recognized digests must be valid to pass, and AT LEAST ONE of the algorithms must be from this list.
func VerifierFields ¶
func VerifierFields(fields ...string) VerifierOption
VerifierFields sets the list of http.Request fields that MUST ALL be present in the "Signature" header from a remote server for a signature to be accepted. Extra fields are allowed in the Signature, and will still be verified.
func VerifierIgnoreBodyDigest ¶
func VerifierIgnoreBodyDigest() VerifierOption
VerifierIgnoreBodyDigest sets the verifier to ignore the "Digest" header. This is useful for testing but should not be used in production.
func VerifierIgnoreTimeout ¶
func VerifierIgnoreTimeout() VerifierOption
VerifierIgnoreTimeout sets the verifier to ignore message and signature time stamps. This is useful for testing signatures, but should not be used in production.
func VerifierSignatureHashes ¶
func VerifierSignatureHashes(hashes ...crypto.Hash) VerifierOption
VerifierSignatureHashes sets the hashing algorithms to use when validating the "Signature" header. Hashes are tried in order, and the FIRST successful match returns success. If ALL hash attempts fail, then validation fails.
func VerifierTimeout ¶
func VerifierTimeout(seconds int) VerifierOption
VerifierTimeout sets the maximum age of a request and signature (in seconds). Messages received after this time duration will be rejected. Default is 43200 seconds (12 hours).