- boot/ (Java/Maven, JNPF 7.0) — 移除旧的 boot/.git,源码直接纳入仓库
- web/ (Vue 3 + Vite + Ant Design Vue, pnpm)
- docs/, scard/, nax/
- refs/INDEX.md + refs/国标/(其余 refs/* 开源子仓库不纳入)
- 已忽略: connector_data/, jweb/, log/, web/{dist,node_modules,.pnpm}, boot/*/target/, .DS_Store, .idea, .vscode
389 lines
12 KiB
Go
389 lines
12 KiB
Go
// Protocol message definitions for security connector
|
|
// This file defines all API messages, error codes, and protocol constants
|
|
|
|
package proto
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
// ErrorCode represents protocol-level error codes
|
|
type ErrorCode string
|
|
|
|
func (e ErrorCode) Error() string { return string(e) }
|
|
|
|
var (
|
|
ErrSuccess = ErrorCode("success")
|
|
ErrInvalidRequest ErrorCode = "invalid_request"
|
|
ErrDeviceNotFound ErrorCode = "device_not_found"
|
|
ErrAuthFailed ErrorCode = "auth_failed"
|
|
ErrChallengeExpired ErrorCode = "challenge_expired"
|
|
ErrInvalidSignature ErrorCode = "invalid_signature"
|
|
ErrAttestationFailed ErrorCode = "attestation_failed"
|
|
ErrKeyNotFound ErrorCode = "key_not_found"
|
|
ErrKeyVersionMismatch ErrorCode = "key_version_mismatch"
|
|
ErrPolicyDenied ErrorCode = "policy_denied"
|
|
ErrInternalError ErrorCode = "internal_error"
|
|
ErrTPMError ErrorCode = "tpm_error"
|
|
ErrNetworkError ErrorCode = "network_error"
|
|
ErrEasytierError ErrorCode = "easytier_error"
|
|
ErrK3sError ErrorCode = "k3s_error"
|
|
)
|
|
|
|
// MessageType represents the type of protocol message
|
|
type MessageType string
|
|
|
|
const (
|
|
// Device registration and auth
|
|
TypeEnrollNonceRequest MessageType = "enroll_nonce_request"
|
|
TypeEnrollNonceResponse MessageType = "enroll_nonce_response"
|
|
TypeRegisterRequest MessageType = "register_request"
|
|
TypeRegisterResponse MessageType = "register_response"
|
|
TypeChallengeRequest MessageType = "challenge_request"
|
|
TypeChallengeResponse MessageType = "challenge_response"
|
|
TypeAuthRequest MessageType = "auth_request"
|
|
TypeAuthResponse MessageType = "auth_response"
|
|
|
|
// Key management
|
|
TypeKeyInit MessageType = "key_init"
|
|
TypeKeyUpdate MessageType = "key_update"
|
|
TypeKeyIncremental MessageType = "key_incremental_update"
|
|
|
|
// EasyTier
|
|
TypeEasytierJoin MessageType = "easytier_join"
|
|
TypeEasytierLeave MessageType = "easytier_leave"
|
|
|
|
// K3s
|
|
TypeK3sJoin MessageType = "k3s_join"
|
|
TypeK3sLeave MessageType = "k3s_leave"
|
|
|
|
// Device status
|
|
TypeDeviceStatus MessageType = "device_status"
|
|
TypeTPMInit MessageType = "tpm_init"
|
|
|
|
// Management plane (SSWFlow TDS)
|
|
TypeRevokeRequest MessageType = "revoke_request"
|
|
|
|
// Inter-connector communication
|
|
TypeCrossSessionRequest MessageType = "cross_session_request"
|
|
TypeCrossSessionResponse MessageType = "cross_session_response"
|
|
)
|
|
|
|
// APIRequest is the base structure for all API requests
|
|
type APIRequest struct {
|
|
Type MessageType `json:"type"`
|
|
DeviceID string `json:"device_id,omitempty"`
|
|
Timestamp int64 `json:"timestamp,omitempty"`
|
|
Sequence uint64 `json:"sequence,omitempty"`
|
|
}
|
|
|
|
// APIResponse is the base structure for all API responses
|
|
type APIResponse struct {
|
|
Type MessageType `json:"type"`
|
|
Code ErrorCode `json:"code"`
|
|
Message string `json:"message,omitempty"`
|
|
Timestamp int64 `json:"timestamp"`
|
|
Sequence uint64 `json:"sequence,omitempty"`
|
|
}
|
|
|
|
// EnrollNonceRequest asks the server for a fresh attestation nonce.
|
|
//
|
|
// This step exists because a device-chosen nonce proves nothing: the quote it
|
|
// binds to can be precomputed offline or replayed indefinitely. The nonce must
|
|
// come from the verifier.
|
|
type EnrollNonceRequest struct {
|
|
APIRequest
|
|
DeviceID string `json:"device_id"`
|
|
}
|
|
|
|
// EnrollNonceResponse carries the server-issued attestation challenge.
|
|
type EnrollNonceResponse struct {
|
|
APIResponse
|
|
Nonce string `json:"nonce"` // base64, must be quoted verbatim
|
|
PCRMask uint32 `json:"pcr_mask"` // PCR set the server wants quoted
|
|
ExpireAt int64 `json:"expire_at"` // unix seconds
|
|
}
|
|
|
|
// RegisterRequest is sent when a device first registers with the server
|
|
type RegisterRequest struct {
|
|
APIRequest
|
|
// TPM-related fields (all base64 encoded)
|
|
TPMPublicKey string `json:"tpm_public_key"` // TPM-generated public key (P-256)
|
|
TPMQuote string `json:"tpm_quote"` // TPM Quote data
|
|
QuoteSignature string `json:"quote_signature"` // TPM Quote signature
|
|
QuotePCRMask uint32 `json:"quote_pcr_mask"` // PCRs included in quote
|
|
Nonce string `json:"nonce"` // Client-generated nonce
|
|
// Device identification
|
|
DeviceID string `json:"device_id"` // Unique device identifier
|
|
FirmwareHash string `json:"firmware_hash"` // Hash of current firmware
|
|
// Endpoint the device is reachable at on the SSWFlow platform. Connectors
|
|
// may report this during registration; the server derives a stable
|
|
// per-device default when omitted so the management plane can still bind
|
|
// the security-plane device record back to its connector record by endpoint.
|
|
Endpoint string `json:"endpoint,omitempty"`
|
|
// CSR fields
|
|
CSR string `json:"csr"` // Certificate signing request
|
|
}
|
|
|
|
// RegisterResponse is returned after successful device registration
|
|
type RegisterResponse struct {
|
|
APIResponse
|
|
// KEK (Key Encryption Key) encrypted with session key
|
|
EncryptedKEK string `json:"encrypted_kek"`
|
|
KEKVersion int `json:"kek_version"`
|
|
// Session certificate
|
|
Certificate string `json:"certificate"`
|
|
// Server public key for ECDH
|
|
ServerPublicKey string `json:"server_public_key"`
|
|
// Validity period
|
|
ValidUntil int64 `json:"valid_until"`
|
|
// Attestation outcome, so the device knows whether it was admitted on the
|
|
// strength of its boot measurement or merely tolerated.
|
|
AttestationStatus string `json:"attestation_status,omitempty"` // verified|tofu-recorded|unverified|disabled
|
|
PCRDigest string `json:"pcr_digest,omitempty"`
|
|
}
|
|
|
|
// ChallengeRequest is sent by server to initiate challenge-response auth
|
|
type ChallengeRequest struct {
|
|
APIRequest
|
|
Nonce string `json:"nonce"` // Server-generated nonce
|
|
Challenge string `json:"challenge"` // Challenge data
|
|
ExpireAt int64 `json:"expire_at"` // Expiration timestamp
|
|
}
|
|
|
|
// ChallengeResponse is sent by device with TPM signature
|
|
type ChallengeResponse struct {
|
|
APIRequest
|
|
Nonce string `json:"nonce"` // Must match server's nonce
|
|
Signature string `json:"signature"` // TPM ECDSA signature
|
|
PCRValues string `json:"pcr_values"`// Current PCR values (optional)
|
|
}
|
|
|
|
// AuthResponse is returned after successful challenge-response verification
|
|
type AuthResponse struct {
|
|
APIResponse
|
|
SessionKey string `json:"session_key"` // Derived session key
|
|
KeyBundle string `json:"key_bundle"` // Encrypted key bundle
|
|
ValidUntil int64 `json:"valid_until"`
|
|
}
|
|
|
|
// KeyInitRequest requests initialization of device keys
|
|
type KeyInitRequest struct {
|
|
APIRequest
|
|
KeyType string `json:"key_type"` // "kek" or "dek"
|
|
}
|
|
|
|
// KeyUpdateRequest requests a key update
|
|
type KeyUpdateRequest struct {
|
|
APIRequest
|
|
KeyID string `json:"key_id"`
|
|
KeyVersion int `json:"key_version"`
|
|
}
|
|
|
|
// KeyIncrementalUpdate represents the incremental key update message
|
|
type KeyIncrementalUpdate struct {
|
|
Type MessageType `json:"type"`
|
|
DeviceID string `json:"device_id"`
|
|
KeyVersion int `json:"key_version"`
|
|
KeyID string `json:"key_id"`
|
|
PartialUpdate PartialKey `json:"partial_update"`
|
|
Signature string `json:"signature"` // Server FPGA signature
|
|
Timestamp int64 `json:"timestamp"`
|
|
}
|
|
|
|
// PartialKey describes a partial key update
|
|
type PartialKey struct {
|
|
Offset int `json:"offset"` // Byte offset in key
|
|
Length int `json:"length"` // Number of bytes
|
|
Data string `json:"data"` // Encrypted partial key data
|
|
}
|
|
|
|
// DeviceStatus represents the current status of a device
|
|
type DeviceStatus struct {
|
|
DeviceID string `json:"device_id"`
|
|
Status string `json:"status"` // "pending", "registered", "active", "revoked"
|
|
KeyVersion int `json:"key_version"`
|
|
LastAuthTime time.Time `json:"last_auth_time"`
|
|
KEKID string `json:"kek_id"`
|
|
PCRValues []string `json:"pcr_values,omitempty"`
|
|
EasytierNode bool `json:"easytier_node"`
|
|
K3sNode bool `json:"k3s_node"`
|
|
}
|
|
|
|
// EasyTierJoinRequest requests joining an EasyTier network
|
|
type EasyTierJoinRequest struct {
|
|
APIRequest
|
|
NetworkID string `json:"network_id"`
|
|
NodeID string `json:"node_id"`
|
|
}
|
|
|
|
// EasyTierJoinResponse contains EasyTier join credentials
|
|
type EasyTierJoinResponse struct {
|
|
APIResponse
|
|
Credential string `json:"credential"` // device_id:temp_key for --enable-creds
|
|
ServerAddr string `json:"server_addr"`
|
|
NetworkID string `json:"network_id"`
|
|
}
|
|
|
|
// K3sJoinRequest requests joining a k3s cluster
|
|
type K3sJoinRequest struct {
|
|
APIRequest
|
|
ClusterID string `json:"cluster_id"`
|
|
NodeName string `json:"node_name"`
|
|
NodeType string `json:"node_type"` // "agent" or "server"
|
|
}
|
|
|
|
// K3sJoinResponse contains k3s join information
|
|
type K3sJoinResponse struct {
|
|
APIResponse
|
|
NodeToken string `json:"node_token"`
|
|
NodePassword string `json:"node_password"`
|
|
ServerURL string `json:"server_url"`
|
|
CACert string `json:"ca_cert"`
|
|
}
|
|
|
|
// TPMInitRequest requests TPM initialization
|
|
type TPMInitRequest struct {
|
|
APIRequest
|
|
DeviceID string `json:"device_id"`
|
|
}
|
|
|
|
// TPMInitResponse contains TPM initialization result
|
|
type TPMInitResponse struct {
|
|
APIResponse
|
|
RootCert string `json:"root_cert"` // Root certificate from server FPGA
|
|
}
|
|
|
|
// CrossSessionRequest requests a cross-connector encrypted session
|
|
// Design doc section 5.5: Connector A requests communication session with Connector B
|
|
type CrossSessionRequest struct {
|
|
APIRequest
|
|
SourceDeviceID string `json:"source_device_id"` // Requesting connector
|
|
TargetDeviceID string `json:"target_device_id"` // Target connector
|
|
Purpose string `json:"purpose"` // Communication purpose description
|
|
}
|
|
|
|
// CrossSessionResponse contains the encrypted cross-session key for each connector
|
|
// Server encrypts Cross_SK with each device's public key
|
|
type CrossSessionResponse struct {
|
|
APIResponse
|
|
EncryptedCrossSK string `json:"encrypted_cross_sk"` // Cross_SK encrypted with requestor's public key
|
|
TargetEncryptedSK string `json:"target_encrypted_sk"` // Cross_SK encrypted with target's public key (for target connector)
|
|
}
|
|
|
|
// ParseRequest parses a JSON request body into the appropriate struct
|
|
func ParseRequest(data []byte) (interface{}, error) {
|
|
var base APIRequest
|
|
if err := json.Unmarshal(data, &base); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch base.Type {
|
|
case TypeEnrollNonceRequest:
|
|
var req EnrollNonceRequest
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
return &req, nil
|
|
|
|
case TypeRegisterRequest:
|
|
var req RegisterRequest
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
return &req, nil
|
|
|
|
case TypeChallengeRequest:
|
|
var req ChallengeRequest
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
return &req, nil
|
|
|
|
case TypeChallengeResponse:
|
|
var req ChallengeResponse
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
return &req, nil
|
|
|
|
case TypeKeyInit:
|
|
var req KeyInitRequest
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
return &req, nil
|
|
|
|
case TypeKeyUpdate:
|
|
var req KeyUpdateRequest
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
return &req, nil
|
|
|
|
case TypeKeyIncremental:
|
|
var req KeyIncrementalUpdate
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
return &req, nil
|
|
|
|
case TypeEasytierJoin:
|
|
var req EasyTierJoinRequest
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
return &req, nil
|
|
|
|
case TypeK3sJoin:
|
|
var req K3sJoinRequest
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
return &req, nil
|
|
|
|
case TypeTPMInit:
|
|
var req TPMInitRequest
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
return &req, nil
|
|
|
|
case TypeCrossSessionRequest:
|
|
var req CrossSessionRequest
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
return &req, nil
|
|
|
|
case TypeCrossSessionResponse:
|
|
var resp CrossSessionResponse
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
return nil, err
|
|
}
|
|
return &resp, nil
|
|
|
|
default:
|
|
return nil, ErrInvalidRequest
|
|
}
|
|
}
|
|
|
|
// NewSuccessResponse creates a successful API response
|
|
func NewSuccessResponse(msgType MessageType) *APIResponse {
|
|
return &APIResponse{
|
|
Type: msgType,
|
|
Code: ErrSuccess,
|
|
Timestamp: time.Now().Unix(),
|
|
}
|
|
}
|
|
|
|
// NewErrorResponse creates an error API response
|
|
func NewErrorResponse(msgType MessageType, code ErrorCode, message string) *APIResponse {
|
|
return &APIResponse{
|
|
Type: msgType,
|
|
Code: code,
|
|
Message: message,
|
|
Timestamp: time.Now().Unix(),
|
|
}
|
|
} |