* chore(deps): consolidate dependency and security bumps Dependabot PRs applied: - #12878 qs 6.15.2 -> 6.16.0 (web/web-runtime) - #13013 alpine 3.24.1 -> 3.24.2 - #13061 actions/checkout -> v7.0.1 (missed instance in k8s.yml) - #13062 actions/setup-node -> v7.0.0 (missed instance in k8s.yml) - #13075 simple-git 3.36.0 -> 4.0.1 (security) - #13076 katex 0.16.45 -> 0.18.2+ (security) - #13077 dompurify 3.4.13 -> 3.4.16 (security) - #13079 go deps minor-and-patch group, 18 updates (opa 1.19.0 -> 1.21.1, otel 1.46 -> 1.47, bleve, govips, grpc-gateway, lico, tablewriter, logrus, gjson, x/exp, x/image, genproto, etc.) Skipped (already satisfied on master): #12513, #12624, #12625, #12633 Manual security bumps via pnpm.overrides (no dependabot PR existed): - axios -> 1.20.0 - smol-toml -> 1.9.0 - postcss -> 8.5.23 - postcss-selector-parser -> 7.1.6 - colord -> 2.9.4 - unhead -> 2.1.13 - markdown-it -> 14.3.1 - esbuild -> 0.28.1 - brace-expansion 5.0.11 -> 5.0.12 Not fixable: sprintf-js and elliptic have no patched version released upstream yet; left as-is. go build and pnpm install verified clean. * chore(deps): fix residual transitive vulns found by trivy trivy flagged duplicate/stale resolutions that survived the earlier override pass: dompurify (3.4.12 -> 3.4.16), qs (6.14.2 -> 6.16.0), and fast-uri (3.1.7 -> 3.1.8, CVE-2026-86472). Added/bumped pnpm.overrides accordingly. trivy fs scan of web/pnpm-lock.yaml now clean. * chore(deps): bump reva to bac0e1ee144ab853a81c6b02142032f102dd245a Pins github.com/owncloud/reva/v2 to bac0e1ee144ab853a81c6b02142032f102dd245a, pulling in its go-playground/locales and huandu/xstrings version bumps. Appends this PR to changelog/unreleased/bump-reva.md. go build ./... verified clean after go mod vendor resync. * fix(ci): revert unhead override, breaks web build * revert: keep k8s.yml actions/checkout and setup-node pinned
312 lines
8.8 KiB
Go
312 lines
8.8 KiB
Go
// Package dsig provides digital signature operations for Go.
|
|
// It contains low-level signature generation and verification tools that
|
|
// can be used by other signing libraries
|
|
//
|
|
// The package follows these design principles:
|
|
// 1. Does minimal checking of input parameters (for performance); callers need to ensure that the parameters are valid.
|
|
// 2. All exported functions are strongly typed (i.e. they do not take `any` types unless they absolutely have to).
|
|
// 3. Does not rely on other high-level packages (standalone, except for internal packages).
|
|
package dsig
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/sha256"
|
|
"crypto/sha512"
|
|
"fmt"
|
|
"hash"
|
|
"io"
|
|
"sync"
|
|
)
|
|
|
|
// Family represents the cryptographic algorithm family
|
|
type Family int
|
|
|
|
const (
|
|
InvalidFamily Family = iota
|
|
HMAC
|
|
RSA
|
|
ECDSA
|
|
EdDSAFamily
|
|
Custom
|
|
// MLDSAFamily covers the ML-DSA parameter sets. It is deliberately not
|
|
// Custom: Custom means this library knows nothing about the algorithm,
|
|
// which would be false here and misleads callers that switch on Family.
|
|
//
|
|
// It sits after Custom so the values earlier releases assigned stay put.
|
|
MLDSAFamily
|
|
maxFamily
|
|
)
|
|
|
|
// String returns the string representation of the Family
|
|
func (f Family) String() string {
|
|
switch f {
|
|
case HMAC:
|
|
return "HMAC"
|
|
case RSA:
|
|
return "RSA"
|
|
case ECDSA:
|
|
return "ECDSA"
|
|
case EdDSAFamily:
|
|
return "EdDSA"
|
|
case Custom:
|
|
return "Custom"
|
|
case MLDSAFamily:
|
|
return "ML-DSA"
|
|
default:
|
|
return "InvalidFamily"
|
|
}
|
|
}
|
|
|
|
// AlgorithmInfo contains metadata about a digital signature algorithm
|
|
type AlgorithmInfo struct {
|
|
Family Family // The cryptographic family (HMAC, RSA, ECDSA, EdDSA)
|
|
Meta any // Family-specific metadata
|
|
}
|
|
|
|
// HMACFamilyMeta contains metadata specific to HMAC algorithms
|
|
type HMACFamilyMeta struct {
|
|
HashFunc func() hash.Hash // Hash function constructor
|
|
}
|
|
|
|
// RSAFamilyMeta contains metadata specific to RSA algorithms
|
|
type RSAFamilyMeta struct {
|
|
Hash crypto.Hash // Hash algorithm
|
|
PSS bool // Whether to use PSS padding (false = PKCS#1 v1.5)
|
|
}
|
|
|
|
// ECDSAFamilyMeta contains metadata specific to ECDSA algorithms
|
|
type ECDSAFamilyMeta struct {
|
|
Hash crypto.Hash // Hash algorithm
|
|
}
|
|
|
|
// EdDSAFamilyMeta contains metadata specific to EdDSA algorithms
|
|
// Currently EdDSA doesn't need specific metadata, but this provides extensibility
|
|
type EdDSAFamilyMeta struct {
|
|
// Reserved for future use
|
|
}
|
|
|
|
// Signer is an interface for custom signing implementations.
|
|
// For the Custom algorithm family, info.Meta must implement this interface
|
|
// to support signing. The implementation struct can carry any additional
|
|
// metadata it needs (hash functions, curves, etc.).
|
|
type Signer interface {
|
|
Sign(key any, payload []byte, rand io.Reader) ([]byte, error)
|
|
}
|
|
|
|
// SignerWithOpts is an optional interface that Custom-family signers
|
|
// can implement to receive a per-call [crypto.SignerOpts]. The
|
|
// canonical use case is ML-DSA, whose Sign method accepts an
|
|
// *mldsa.Options carrying a domain-separation context that the plain
|
|
// [Signer] interface cannot convey. Custom Meta values that do not
|
|
// implement this interface still work with [SignWithOpts]: the
|
|
// dispatcher falls back to the plain [Signer.Sign] method and the opts
|
|
// argument is dropped.
|
|
//
|
|
// Implementing both [Signer] and SignerWithOpts is supported, but
|
|
// implementing only SignerWithOpts is sufficient because the dispatcher
|
|
// checks for it first.
|
|
type SignerWithOpts interface {
|
|
SignWithOpts(key any, payload []byte, opts crypto.SignerOpts, rand io.Reader) ([]byte, error)
|
|
}
|
|
|
|
// Verifier is an interface for custom verification implementations.
|
|
// For the Custom algorithm family, info.Meta must implement this interface
|
|
// to support verification. The implementation struct can carry any additional
|
|
// metadata it needs (hash functions, curves, etc.).
|
|
type Verifier interface {
|
|
Verify(key any, payload, signature []byte) error
|
|
}
|
|
|
|
// VerifierWithOpts is the verification counterpart of [SignerWithOpts].
|
|
// See [SignerWithOpts] for usage notes.
|
|
type VerifierWithOpts interface {
|
|
VerifyWithOpts(key any, payload, signature []byte, opts crypto.SignerOpts) error
|
|
}
|
|
|
|
var algorithms = make(map[string]AlgorithmInfo)
|
|
var builtinAlgorithms = make(map[string]struct{})
|
|
var muAlgorithms sync.RWMutex
|
|
|
|
// RegisterAlgorithm registers a new digital signature algorithm with the specified family and metadata.
|
|
//
|
|
// info.Meta should contain extra metadata for some algorithms. HMAC, RSA, and ECDSA
|
|
// families need their respective metadata (HMACFamilyMeta, RSAFamilyMeta, and
|
|
// ECDSAFamilyMeta). Metadata for EdDSA is optional. For the Custom family, Meta
|
|
// must implement at least one of the Signer, SignerWithOpts, Verifier, or
|
|
// VerifierWithOpts interfaces.
|
|
//
|
|
// Re-registration of an already-registered algorithm name is rejected. Use
|
|
// UnregisterAlgorithm to remove it first if you need to replace it.
|
|
func RegisterAlgorithm(name string, info AlgorithmInfo) error {
|
|
muAlgorithms.Lock()
|
|
defer muAlgorithms.Unlock()
|
|
|
|
if _, exists := algorithms[name]; exists {
|
|
return fmt.Errorf("algorithm %s is already registered", name)
|
|
}
|
|
|
|
// Validate the metadata matches the family
|
|
switch info.Family {
|
|
case HMAC:
|
|
if _, ok := info.Meta.(HMACFamilyMeta); !ok {
|
|
return fmt.Errorf("invalid HMAC metadata for algorithm %s", name)
|
|
}
|
|
case RSA:
|
|
if _, ok := info.Meta.(RSAFamilyMeta); !ok {
|
|
return fmt.Errorf("invalid RSA metadata for algorithm %s", name)
|
|
}
|
|
case ECDSA:
|
|
if _, ok := info.Meta.(ECDSAFamilyMeta); !ok {
|
|
return fmt.Errorf("invalid ECDSA metadata for algorithm %s", name)
|
|
}
|
|
case EdDSAFamily:
|
|
// EdDSA metadata is optional for now
|
|
case Custom, MLDSAFamily:
|
|
// Both families carry their implementation in Meta. The other families
|
|
// put passive metadata there. For ML-DSA this is forced: crypto/mldsa
|
|
// exists only from Go 1.27, so the algorithm cannot be described by a
|
|
// value type this file could name.
|
|
_, isSigner := info.Meta.(Signer)
|
|
_, isSignerWithOpts := info.Meta.(SignerWithOpts)
|
|
_, isVerifier := info.Meta.(Verifier)
|
|
_, isVerifierWithOpts := info.Meta.(VerifierWithOpts)
|
|
if !isSigner && !isSignerWithOpts && !isVerifier && !isVerifierWithOpts {
|
|
return fmt.Errorf("%s algorithm %s: Meta must implement Signer, SignerWithOpts, Verifier, or VerifierWithOpts", info.Family, name)
|
|
}
|
|
default:
|
|
return fmt.Errorf("unsupported algorithm family %s for algorithm %s", info.Family, name)
|
|
}
|
|
|
|
algorithms[name] = info
|
|
return nil
|
|
}
|
|
|
|
// UnregisterAlgorithm removes a previously registered algorithm by name.
|
|
// Built-in algorithms cannot be unregistered.
|
|
// It is a no-op if the algorithm is not registered.
|
|
func UnregisterAlgorithm(name string) error {
|
|
muAlgorithms.Lock()
|
|
defer muAlgorithms.Unlock()
|
|
|
|
if _, ok := builtinAlgorithms[name]; ok {
|
|
return fmt.Errorf("algorithm %s is a built-in algorithm and cannot be unregistered", name)
|
|
}
|
|
|
|
delete(algorithms, name)
|
|
return nil
|
|
}
|
|
|
|
// GetAlgorithmInfo retrieves the algorithm information for a given algorithm name.
|
|
// Returns the info and true if found, zero value and false if not found.
|
|
func GetAlgorithmInfo(name string) (AlgorithmInfo, bool) {
|
|
muAlgorithms.RLock()
|
|
defer muAlgorithms.RUnlock()
|
|
|
|
info, ok := algorithms[name]
|
|
return info, ok
|
|
}
|
|
|
|
func init() {
|
|
// Register all standard algorithms with their metadata
|
|
toRegister := map[string]AlgorithmInfo{
|
|
// HMAC algorithms
|
|
HMACWithSHA256: {
|
|
Family: HMAC,
|
|
Meta: HMACFamilyMeta{
|
|
HashFunc: sha256.New,
|
|
},
|
|
},
|
|
HMACWithSHA384: {
|
|
Family: HMAC,
|
|
Meta: HMACFamilyMeta{
|
|
HashFunc: sha512.New384,
|
|
},
|
|
},
|
|
HMACWithSHA512: {
|
|
Family: HMAC,
|
|
Meta: HMACFamilyMeta{
|
|
HashFunc: sha512.New,
|
|
},
|
|
},
|
|
|
|
// RSA PKCS#1 v1.5 algorithms
|
|
RSAPKCS1v15WithSHA256: {
|
|
Family: RSA,
|
|
Meta: RSAFamilyMeta{
|
|
Hash: crypto.SHA256,
|
|
PSS: false,
|
|
},
|
|
},
|
|
RSAPKCS1v15WithSHA384: {
|
|
Family: RSA,
|
|
Meta: RSAFamilyMeta{
|
|
Hash: crypto.SHA384,
|
|
PSS: false,
|
|
},
|
|
},
|
|
RSAPKCS1v15WithSHA512: {
|
|
Family: RSA,
|
|
Meta: RSAFamilyMeta{
|
|
Hash: crypto.SHA512,
|
|
PSS: false,
|
|
},
|
|
},
|
|
|
|
// RSA PSS algorithms
|
|
RSAPSSWithSHA256: {
|
|
Family: RSA,
|
|
Meta: RSAFamilyMeta{
|
|
Hash: crypto.SHA256,
|
|
PSS: true,
|
|
},
|
|
},
|
|
RSAPSSWithSHA384: {
|
|
Family: RSA,
|
|
Meta: RSAFamilyMeta{
|
|
Hash: crypto.SHA384,
|
|
PSS: true,
|
|
},
|
|
},
|
|
RSAPSSWithSHA512: {
|
|
Family: RSA,
|
|
Meta: RSAFamilyMeta{
|
|
Hash: crypto.SHA512,
|
|
PSS: true,
|
|
},
|
|
},
|
|
|
|
// ECDSA algorithms
|
|
ECDSAWithP256AndSHA256: {
|
|
Family: ECDSA,
|
|
Meta: ECDSAFamilyMeta{
|
|
Hash: crypto.SHA256,
|
|
},
|
|
},
|
|
ECDSAWithP384AndSHA384: {
|
|
Family: ECDSA,
|
|
Meta: ECDSAFamilyMeta{
|
|
Hash: crypto.SHA384,
|
|
},
|
|
},
|
|
ECDSAWithP521AndSHA512: {
|
|
Family: ECDSA,
|
|
Meta: ECDSAFamilyMeta{
|
|
Hash: crypto.SHA512,
|
|
},
|
|
},
|
|
|
|
// EdDSA algorithm
|
|
EdDSA: {
|
|
Family: EdDSAFamily,
|
|
Meta: EdDSAFamilyMeta{},
|
|
},
|
|
}
|
|
|
|
for name, info := range toRegister {
|
|
if err := RegisterAlgorithm(name, info); err != nil {
|
|
panic(fmt.Sprintf("failed to register algorithm %s: %v", name, err))
|
|
}
|
|
builtinAlgorithms[name] = struct{}{}
|
|
}
|
|
}
|