|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +// Package main provides a standalone mock_wsd binary to test workload attestation. |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "log" |
| 12 | + "net" |
| 13 | + "net/http" |
| 14 | + "os" |
| 15 | + "os/signal" |
| 16 | + "syscall" |
| 17 | + |
| 18 | + clogging "cloud.google.com/go/logging" |
| 19 | + "github.com/google/go-tpm-tools/client" |
| 20 | + "github.com/google/go-tpm-tools/launcher/agent" |
| 21 | + "github.com/google/go-tpm-tools/launcher/spec" |
| 22 | + "github.com/google/go-tpm-tools/launcher/teeserver/models" |
| 23 | + "github.com/google/go-tpm-tools/verifier/util" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + mockWsdSocketPath = "/run/workload_attestation.sock" |
| 28 | +) |
| 29 | + |
| 30 | +type cmdLogger struct { |
| 31 | + *log.Logger |
| 32 | +} |
| 33 | + |
| 34 | +func (l *cmdLogger) Log(severity clogging.Severity, msg string, args ...any) { |
| 35 | + l.Printf("%v: %s %v\n", severity, msg, args) |
| 36 | +} |
| 37 | + |
| 38 | +func (l *cmdLogger) Info(msg string, args ...any) { |
| 39 | + l.Printf("INFO: %s %v\n", msg, args) |
| 40 | +} |
| 41 | + |
| 42 | +func (l *cmdLogger) Warn(msg string, args ...any) { |
| 43 | + l.Printf("WARN: %s %v\n", msg, args) |
| 44 | +} |
| 45 | + |
| 46 | +func (l *cmdLogger) Error(msg string, args ...any) { |
| 47 | + l.Printf("ERROR: %s %v\n", msg, args) |
| 48 | +} |
| 49 | + |
| 50 | +func (l *cmdLogger) SerialConsoleFile() *os.File { |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +func (l *cmdLogger) Close() { |
| 55 | +} |
| 56 | + |
| 57 | +type keyHandle struct { |
| 58 | + Handle string `json:"handle"` |
| 59 | +} |
| 60 | + |
| 61 | +type getKeyEndorsementRequest struct { |
| 62 | + Challenge []byte `json:"challenge"` |
| 63 | + KeyHandle keyHandle `json:"key_handle"` |
| 64 | +} |
| 65 | + |
| 66 | +type getKeyEndorsementResponse struct { |
| 67 | + Endorsement keyEndorsement `json:"endorsement"` |
| 68 | +} |
| 69 | + |
| 70 | +type keyEndorsement struct { |
| 71 | + VMProtectedKeyEndorsement vmProtectedKeyEndorsement `json:"vm_protected_key_endorsement"` |
| 72 | +} |
| 73 | + |
| 74 | +type vmProtectedKeyEndorsement struct { |
| 75 | + BindingKeyAttestation *keyAttestation `json:"binding_key_attestation,omitempty"` |
| 76 | + ProtectedKeyAttestation *keyAttestation `json:"protected_key_attestation,omitempty"` |
| 77 | +} |
| 78 | + |
| 79 | +type keyAttestation struct { |
| 80 | + Attestation *models.VMAttestation `json:"attestation"` |
| 81 | +} |
| 82 | + |
| 83 | +func main() { |
| 84 | + if os.Getuid() != 0 { |
| 85 | + log.Println("Warning: mock_wsd usually requires root privileges to create sockets in /run") |
| 86 | + } |
| 87 | + |
| 88 | + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) |
| 89 | + defer stop() |
| 90 | + |
| 91 | + logger := &cmdLogger{log.New(os.Stdout, "mock_wsd ", log.LstdFlags)} |
| 92 | + |
| 93 | + // 1. Initialize Attestation Agent for teeserver |
| 94 | + launchSpec := spec.LaunchSpec{} |
| 95 | + launchSpec.Experiments.EnableAttestationEvidence = true |
| 96 | + |
| 97 | + vTPM, err := os.OpenFile("/dev/tpmrm0", os.O_RDWR, 0) |
| 98 | + if err != nil { |
| 99 | + logger.Error(fmt.Sprintf("Failed to open vTPM: %v", err)) |
| 100 | + } else { |
| 101 | + defer vTPM.Close() |
| 102 | + } |
| 103 | + var tpmCloser io.ReadWriteCloser |
| 104 | + if vTPM != nil { |
| 105 | + tpmCloser = vTPM |
| 106 | + } |
| 107 | + |
| 108 | + var akFetcher util.TpmKeyFetcher |
| 109 | + if tpmCloser != nil { |
| 110 | + akFetcher = client.GceAttestationKeyECC |
| 111 | + } else { |
| 112 | + akFetcher = func(_ io.ReadWriter) (*client.Key, error) { |
| 113 | + return nil, fmt.Errorf("no vTPM available") |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + attestAgent, err := agent.CreateAttestationAgent( |
| 118 | + tpmCloser, akFetcher, nil, nil, nil, launchSpec, logger, |
| 119 | + ) |
| 120 | + if err != nil { |
| 121 | + logger.Error(fmt.Sprintf("failed to create attestation agent: %v", err)) |
| 122 | + os.Exit(1) |
| 123 | + } |
| 124 | + defer attestAgent.Close() |
| 125 | + |
| 126 | + errChan := make(chan error, 1) |
| 127 | + |
| 128 | + // 2. Start mock_wsd server |
| 129 | + mux := http.NewServeMux() |
| 130 | + // Pass the attestAgent directly to avoid the loopback HTTP call |
| 131 | + mux.HandleFunc("/v1/workload/attestation/key_endorsement", func(w http.ResponseWriter, r *http.Request) { |
| 132 | + handleGetKeyEndorsement(w, r, attestAgent, logger) |
| 133 | + }) |
| 134 | + |
| 135 | + if err := os.RemoveAll(mockWsdSocketPath); err != nil { |
| 136 | + logger.Error(fmt.Sprintf("Failed to remove existing socket %s: %v", mockWsdSocketPath, err)) |
| 137 | + } |
| 138 | + |
| 139 | + listener, err := net.Listen("unix", mockWsdSocketPath) |
| 140 | + if err != nil { |
| 141 | + logger.Error(fmt.Sprintf("Failed to listen on %s: %v", mockWsdSocketPath, err)) |
| 142 | + os.Exit(1) |
| 143 | + } |
| 144 | + defer listener.Close() |
| 145 | + |
| 146 | + if err := os.Chmod(mockWsdSocketPath, 0777); err != nil { |
| 147 | + logger.Warn(fmt.Sprintf("failed to chmod socket %s: %v", mockWsdSocketPath, err)) |
| 148 | + } |
| 149 | + |
| 150 | + logger.Info("Starting mock_wsd server attached to TEE Server", "socket", mockWsdSocketPath) |
| 151 | + go func() { |
| 152 | + errChan <- http.Serve(listener, mux) |
| 153 | + }() |
| 154 | + |
| 155 | + // 3. Wait for termination |
| 156 | + select { |
| 157 | + case err := <-errChan: |
| 158 | + if err != nil { |
| 159 | + logger.Error(fmt.Sprintf("server error: %v", err)) |
| 160 | + os.Exit(1) |
| 161 | + } |
| 162 | + case <-ctx.Done(): |
| 163 | + logger.Info("Shutting down mock_wsd server") |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func handleGetKeyEndorsement(w http.ResponseWriter, r *http.Request, attestAgent agent.AttestationAgent, logger *cmdLogger) { |
| 168 | + if r.Method != http.MethodPost { |
| 169 | + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 170 | + return |
| 171 | + } |
| 172 | + |
| 173 | + var req getKeyEndorsementRequest |
| 174 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 175 | + http.Error(w, fmt.Sprintf("Failed to decode request: %v", err), http.StatusBadRequest) |
| 176 | + return |
| 177 | + } |
| 178 | + |
| 179 | + if len(req.Challenge) == 0 { |
| 180 | + http.Error(w, "challenge is required", http.StatusBadRequest) |
| 181 | + return |
| 182 | + } |
| 183 | + |
| 184 | + // Call the generic AttestationEvidence function directly from the agent |
| 185 | + attestation, err := attestAgent.AttestationEvidence(r.Context(), req.Challenge, nil) |
| 186 | + if err != nil { |
| 187 | + logger.Error(fmt.Sprintf("Error getting evidence from agent: %v", err)) |
| 188 | + http.Error(w, fmt.Sprintf("Internal error: %v", err), http.StatusInternalServerError) |
| 189 | + return |
| 190 | + } |
| 191 | + |
| 192 | + // Construct the response |
| 193 | + // For the mock, we only include the attestation in BindingKeyAttestation |
| 194 | + resp := getKeyEndorsementResponse{ |
| 195 | + Endorsement: keyEndorsement{ |
| 196 | + VMProtectedKeyEndorsement: vmProtectedKeyEndorsement{ |
| 197 | + BindingKeyAttestation: &keyAttestation{ |
| 198 | + Attestation: attestation, |
| 199 | + }, |
| 200 | + }, |
| 201 | + }, |
| 202 | + } |
| 203 | + |
| 204 | + w.Header().Set("Content-Type", "application/json") |
| 205 | + if err := json.NewEncoder(w).Encode(resp); err != nil { |
| 206 | + logger.Error(fmt.Sprintf("Error encoding response: %v", err)) |
| 207 | + } |
| 208 | +} |
0 commit comments