118 lines
3.5 KiB
Go
118 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func panelHMACDigest(secret, base string) string {
|
|
mac := hmac.New(sha256.New, []byte(secret))
|
|
_, _ = mac.Write([]byte(base))
|
|
return hex.EncodeToString(mac.Sum(nil))
|
|
}
|
|
|
|
func panelHMACMatch(secret, base, sigHex string) bool {
|
|
return secret != "" && timingSafeEq(sigHex, panelHMACDigest(secret, base))
|
|
}
|
|
|
|
func (a *Agent) verifyPanelSignedEnvelope(r *http.Request, basePrefix string) (bool, string) {
|
|
secret, why := a.verifyPanelSignedEnvelopeKey(r, basePrefix)
|
|
return secret != "", why
|
|
}
|
|
|
|
// Only the per-node signing key (or its live predecessor) can authenticate
|
|
// an admin envelope. Record the nonce only after validating its signature.
|
|
func (a *Agent) verifyPanelSignedEnvelopeKey(r *http.Request, basePrefix string) (string, string) {
|
|
secret, prevSecret, prevLive := a.panelHMACSecrets()
|
|
if secret == "" {
|
|
return "", "hmac-not-configured"
|
|
}
|
|
ts := r.Header.Get("x-panel-ts")
|
|
sig := strings.TrimSpace(r.Header.Get("x-panel-sign"))
|
|
if ts == "" || sig == "" {
|
|
return "", "missing-signature"
|
|
}
|
|
tsNum, err := strconv.ParseInt(strings.TrimSpace(ts), 10, 64)
|
|
if err != nil {
|
|
return "", "expired"
|
|
}
|
|
now := time.Now().UnixMilli()
|
|
drift := now - tsNum
|
|
if drift < 0 {
|
|
drift = -drift
|
|
}
|
|
if drift > panelSignDriftMillis {
|
|
return "", "expired"
|
|
}
|
|
nonce := strings.TrimSpace(r.Header.Get("x-panel-nonce"))
|
|
if nonce == "" {
|
|
if a.panelRequireNonce {
|
|
return "", "nonce-required"
|
|
}
|
|
} else if !validPanelNonce(nonce) {
|
|
return "", "bad-nonce"
|
|
}
|
|
base := basePrefix + "|" + ts
|
|
if nonce != "" {
|
|
base += "|" + nonce
|
|
}
|
|
if !panelHMACMatch(secret, base, sig) {
|
|
if !prevLive || !panelHMACMatch(prevSecret, base, sig) {
|
|
return "", "bad-signature"
|
|
}
|
|
secret = prevSecret
|
|
}
|
|
if nonce != "" && !a.recordPanelNonce(nonce, now) {
|
|
return "", "replayed"
|
|
}
|
|
return secret, ""
|
|
}
|
|
|
|
func (a *Agent) verifyNodeAdminRequest(r *http.Request, action, subject string) (bool, string) {
|
|
secret, why := a.verifyNodeAdminRequestKey(r, action, subject)
|
|
return secret != "", why
|
|
}
|
|
|
|
func (a *Agent) verifyNodeAdminRequestKey(r *http.Request, action, subject string) (string, string) {
|
|
role := strings.ToLower(strings.TrimSpace(r.Header.Get("x-panel-role")))
|
|
if role != "admin" {
|
|
return "", "admin-required"
|
|
}
|
|
base := strings.TrimSpace(a.uuid) + "|" + strings.TrimSpace(action) + "|" + strings.TrimSpace(subject) + "|" + role
|
|
return a.verifyPanelSignedEnvelopeKey(r, base)
|
|
}
|
|
|
|
type panelProbeProof struct {
|
|
nonce string
|
|
signature string
|
|
}
|
|
|
|
func (proof panelProbeProof) addTo(response map[string]any) {
|
|
if proof.signature != "" {
|
|
response["panel_nonce"] = proof.nonce
|
|
response["panel_nonce_proof"] = proof.signature
|
|
}
|
|
}
|
|
|
|
func (a *Agent) verifyPanelProbe(r *http.Request, purpose string) (bool, panelProbeProof, string) {
|
|
var proof panelProbeProof
|
|
if r.Header.Get("x-panel-sign") == "" && r.Header.Get("x-panel-nonce") == "" && r.Header.Get("x-panel-ts") == "" {
|
|
return true, proof, ""
|
|
}
|
|
secret, why := a.verifyNodeAdminRequestKey(r, "probe", purpose)
|
|
if secret == "" {
|
|
return false, proof, why
|
|
}
|
|
if nonce := strings.TrimSpace(r.Header.Get("x-panel-nonce")); nonce != "" {
|
|
// adpanel:node-probe-proof - the response cannot reuse any value sent in the request as proof of key possession.
|
|
base := strings.TrimSpace(a.uuid) + "|probe-response|" + strings.TrimSpace(purpose) + "|" + nonce
|
|
proof = panelProbeProof{nonce: nonce, signature: panelHMACDigest(secret, base)}
|
|
}
|
|
return true, proof, ""
|
|
}
|