fleet/server/mdm/scep/challenge/challenge.go
Victor Lyuboslavsky e0faa14025
Updating scep package with latest fixes (#22372)
Updating scep package with changes up to
github.com/micromdm/scep@781f8042a79cabcf61a5e6c01affdbadcb785932
Fixes needed for NDES client for #21955 

Manually pulled in the recent changes. You can view the changes in the
remote like:
https://github.com/getvictor/scep/compare/fleet...micromdm%3Ascep%3Amain
2024-09-27 07:04:11 -05:00

40 lines
1.1 KiB
Go

// Package challenge defines an interface for a dynamic challenge password cache.
package challenge
import (
"context"
"crypto/x509"
"errors"
scepserver "github.com/fleetdm/fleet/v4/server/mdm/scep/server"
"github.com/smallstep/scep"
)
// Validator validates challenge passwords.
type Validator interface {
// HasChallenge validates pw as valid.
HasChallenge(pw string) (bool, error)
}
// Store is a dynamic challenge password cache.
type Store interface {
// SCEPChallenge generates a new challenge password.
SCEPChallenge() (string, error)
Validator
}
// Middleware wraps next in a CSRSigner that verifies and invalidates the challenge.
func Middleware(store Validator, next scepserver.CSRSignerContext) scepserver.CSRSignerContextFunc {
return func(ctx context.Context, m *scep.CSRReqMessage) (*x509.Certificate, error) {
// TODO: compare challenge only for PKCSReq?
valid, err := store.HasChallenge(m.ChallengePassword)
if err != nil {
return nil, err
}
if !valid {
return nil, errors.New("invalid challenge")
}
return next.SignCSRContext(ctx, m)
}
}