Debian Codegraph @

launcher/teeserver/tee_server.go

raw
1 // Package teeserver implements a server to be run in the launcher.
2 // Used for communicate between the host/launcher and the container.
3 package teeserver
4
5 import (
6 "context"
7 "encoding/json"
8 "fmt"
9 "net"
10 "net/http"
11
12 attestationpb "github.com/GoogleCloudPlatform/confidential-space/server/proto/gen/attestation"
13 "github.com/containerd/containerd/protobuf/proto"
14 keymanager "github.com/google/go-tpm-tools/keymanager/km_common/proto"
15 wsd "github.com/google/go-tpm-tools/keymanager/workload_service"
16 "github.com/google/go-tpm-tools/launcher/agent"
17 "github.com/google/go-tpm-tools/launcher/internal/logging"
18 "github.com/google/go-tpm-tools/launcher/spec"
19 "github.com/google/go-tpm-tools/verifier"
20 "github.com/google/go-tpm-tools/verifier/models"
21 "google.golang.org/grpc/codes"
22 "google.golang.org/grpc/status"
23 "google.golang.org/protobuf/encoding/protojson"
24 )
25
26 const (
27 gcaEndpoint = "/v1/token"
28 itaEndpoint = "/v1/intel/token"
29 evidenceEndpoint = "/v1/evidence"
30 endorsementEndpoint = "/v1/keys:getEndorsement"
31 )
32
33 var clientErrorCodes = map[codes.Code]struct{}{
34 codes.InvalidArgument: {},
35 codes.FailedPrecondition: {},
36 codes.PermissionDenied: {},
37 codes.Unauthenticated: {},
38 codes.NotFound: {},
39 codes.Aborted: {},
40 codes.OutOfRange: {},
41 codes.Canceled: {},
42 }
43
44 // AttestClients contains clients for supported verifier services that can be used to
45 // get attestation tokens.
46 type AttestClients struct {
47 GCA verifier.Client
48 ITA verifier.Client
49 }
50
51 type attestHandler struct {
52 ctx context.Context
53 attestAgent agent.AttestationAgent
54 // defaultTokenFile string
55 logger logging.Logger
56 launchSpec spec.LaunchSpec
57 clients AttestClients
58 keyClaimsProvider wsd.KeyClaimsProvider
59 }
60
61 // TeeServer is a server that can be called from a container through a unix
62 // socket file.
63 type TeeServer struct {
64 server *http.Server
65 netListener net.Listener
66 }
67
68 // New takes in a socket and start to listen to it, and create a server
69 func New(ctx context.Context, unixSock string, a agent.AttestationAgent, logger logging.Logger, launchSpec spec.LaunchSpec, clients AttestClients, keyClaimsProvider wsd.KeyClaimsProvider) (*TeeServer, error) {
70 var err error
71 nl, err := net.Listen("unix", unixSock)
72 if err != nil {
73 return nil, fmt.Errorf("cannot listen to the socket [%s]: %v", unixSock, err)
74 }
75
76 if launchSpec.Experiments.EnableKeyManager && keyClaimsProvider == nil {
77 return nil, fmt.Errorf("key claims provider cannot be nil when key manager is enabled")
78 }
79
80 teeServer := TeeServer{
81 netListener: nl,
82 server: &http.Server{
83 Handler: (&attestHandler{
84 ctx: ctx,
85 attestAgent: a,
86 logger: logger,
87 launchSpec: launchSpec,
88 clients: clients,
89 keyClaimsProvider: keyClaimsProvider,
90 }).Handler(),
91 },
92 }
93 return &teeServer, nil
94 }
95
96 // Handler creates a multiplexer for the server.
97 func (a *attestHandler) Handler() http.Handler {
98 mux := http.NewServeMux()
99 // to test default token: curl --unix-socket <socket> http://localhost/v1/token
100 // to test custom token:
101 // curl -d '{"audience":"<aud>", "nonces":["<nonce1>"]}' -H "Content-Type: application/json" -X POST
102 // --unix-socket /tmp/container_launcher/teeserver.sock http://localhost/v1/token
103 // to test attestation evidence:
104 // curl -d '{"challenge":"<challenge>"}' -H "Content-Type: application/json" -X POST
105 // --unix-socket /tmp/container_launcher/teeserver.sock http://localhost/v1/evidence
106
107 mux.HandleFunc(gcaEndpoint, a.getToken)
108 mux.HandleFunc(itaEndpoint, a.getITAToken)
109 mux.HandleFunc(evidenceEndpoint, a.getAttestationEvidence)
110 mux.HandleFunc(endorsementEndpoint, a.getKeyEndorsement)
111 return mux
112 }
113
114 func (a *attestHandler) logAndWriteError(errStr string, status int, w http.ResponseWriter) {
115 a.logger.Error(errStr)
116 w.WriteHeader(status)
117 w.Write([]byte(errStr))
118 }
119
120 // getDefaultToken handles the request to get the default OIDC token.
121 // For now this function will just read the content of the file and return.
122 // Later, this function can use attestation agent to get a token directly.
123 func (a *attestHandler) getToken(w http.ResponseWriter, r *http.Request) {
124 w.Header().Set("Content-Type", "text/html")
125
126 a.logger.Info(fmt.Sprintf("%s called", gcaEndpoint))
127 if a.clients.GCA == nil {
128 errStr := "no GCA verifier client present, please try rebooting your VM"
129 a.logAndWriteError(errStr, http.StatusInternalServerError, w)
130 return
131 }
132
133 a.attest(w, r, a.clients.GCA)
134 }
135
136 // getITAToken retrieves a attestation token signed by ITA.
137 func (a *attestHandler) getITAToken(w http.ResponseWriter, r *http.Request) {
138 w.Header().Set("Content-Type", "text/html")
139
140 a.logger.Info(fmt.Sprintf("%s called", itaEndpoint))
141
142 // If the handler does not have an ITA client, return error.
143 if a.clients.ITA == nil {
144 errStr := "no ITA verifier client present - ensure ITA Region and Key are defined in metadata"
145 a.logAndWriteError(errStr, http.StatusInternalServerError, w)
146 return
147 }
148
149 a.attest(w, r, a.clients.ITA)
150 }
151
152 // getAttestationEvidence retrieves the attestation evidence.
153 func (a *attestHandler) getAttestationEvidence(w http.ResponseWriter, r *http.Request) {
154 if r.Method != http.MethodPost {
155 a.logAndWriteHTTPError(w, http.StatusMethodNotAllowed, fmt.Errorf("method not allowed"))
156 return
157 }
158
159 var req struct {
160 Challenge []byte `json:"challenge"`
161 }
162
163 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
164 a.logAndWriteHTTPError(w, http.StatusBadRequest, fmt.Errorf("failed to decode request: %v", err))
165 return
166 }
167 if len(req.Challenge) == 0 {
168 a.logAndWriteHTTPError(w, http.StatusBadRequest, fmt.Errorf("challenge is required"))
169 return
170 }
171
172 evidence, err := a.attestAgent.AttestationEvidence(a.ctx, req.Challenge, nil)
173 if err != nil {
174 a.logAndWriteHTTPError(w, http.StatusInternalServerError, err)
175 return
176 }
177
178 evidenceBytes, err := protojson.Marshal(evidence)
179 if err != nil {
180 a.logAndWriteHTTPError(w, http.StatusInternalServerError, fmt.Errorf("failed to marshal evidence: %v", err))
181 return
182 }
183
184 w.Header().Set("Content-Type", "application/json")
185 w.Write(evidenceBytes)
186 }
187
188 // getKeyEndorsement retrieves the attestation evidence with KEM and binding key claims.
189 func (a *attestHandler) getKeyEndorsement(w http.ResponseWriter, r *http.Request) {
190 if !a.launchSpec.Experiments.EnableKeyManager {
191 a.logAndWriteHTTPError(w, http.StatusForbidden, fmt.Errorf("keymanager not enabled"))
192 return
193 }
194
195 if r.Method != http.MethodPost {
196 a.logAndWriteHTTPError(w, http.StatusMethodNotAllowed, fmt.Errorf("method not allowed"))
197 return
198 }
199
200 var req struct {
201 Challenge []byte `json:"challenge"`
202 KeyHandle struct {
203 Handle string `json:"handle"`
204 } `json:"key_handle"`
205 }
206
207 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
208 a.logAndWriteHTTPError(w, http.StatusBadRequest, fmt.Errorf("failed to decode request: %v", err))
209 return
210 }
211
212 if len(req.Challenge) == 0 {
213 a.logAndWriteHTTPError(w, http.StatusBadRequest, fmt.Errorf("challenge is required"))
214 return
215 }
216
217 if len(req.KeyHandle.Handle) == 0 {
218 a.logAndWriteHTTPError(w, http.StatusBadRequest, fmt.Errorf("key_handle is required"))
219 return
220 }
221
222 kemKeyClaims, err := a.keyClaimsProvider.GetKeyClaims(a.ctx, req.KeyHandle.Handle, keymanager.KeyType_KEY_TYPE_VM_PROTECTION_KEY)
223 if err != nil {
224 a.logAndWriteHTTPError(w, http.StatusInternalServerError, fmt.Errorf("failed to get KEM key claims"))
225 return
226 }
227
228 bindingKeyClaims, err := a.keyClaimsProvider.GetKeyClaims(a.ctx, req.KeyHandle.Handle, keymanager.KeyType_KEY_TYPE_VM_PROTECTION_BINDING)
229 if err != nil {
230 a.logAndWriteHTTPError(w, http.StatusInternalServerError, fmt.Errorf("failed to get binding key claims"))
231 return
232 }
233
234 bindingBytes, err := proto.Marshal(bindingKeyClaims)
235 if err != nil {
236 a.logAndWriteHTTPError(w, http.StatusInternalServerError, fmt.Errorf("failed to marshal binding key claims: %v", err))
237 return
238 }
239
240 kemBytes, err := proto.Marshal(kemKeyClaims)
241 if err != nil {
242 a.logAndWriteHTTPError(w, http.StatusInternalServerError, fmt.Errorf("failed to marshal KEM key claims: %v", err))
243 return
244 }
245
246 kemEvidence, err := a.attestAgent.AttestationEvidence(a.ctx, req.Challenge, kemBytes)
247 if err != nil {
248 a.logAndWriteHTTPError(w, http.StatusInternalServerError, fmt.Errorf("failed to collect attestation evidence with kem key claims"))
249 return
250 }
251
252 bindingEvidence, err := a.attestAgent.AttestationEvidence(a.ctx, req.Challenge, bindingBytes)
253 if err != nil {
254 a.logAndWriteHTTPError(w, http.StatusInternalServerError, fmt.Errorf("failed to collect attestation evidence with binding key claims"))
255 return
256 }
257
258 keyEndorsement := &attestationpb.KeyEndorsement{
259 Endorsement: &attestationpb.KeyEndorsement_VmProtectedKeyEndorsement{
260 VmProtectedKeyEndorsement: &attestationpb.VmProtectedKeyEndorsement{
261 BindingKeyAttestation: &attestationpb.KeyAttestation{
262 Attestation: bindingEvidence,
263 },
264 ProtectedKeyAttestation: &attestationpb.KeyAttestation{
265 Attestation: kemEvidence,
266 },
267 },
268 },
269 }
270
271 keyEndorsementBytes, err := protojson.Marshal(keyEndorsement)
272 if err != nil {
273 a.logAndWriteHTTPError(w, http.StatusInternalServerError, fmt.Errorf("failed to marshal evidence: %v", err))
274 return
275 }
276
277 w.Header().Set("Content-Type", "application/json")
278 w.Write(keyEndorsementBytes)
279 }
280
281 func (a *attestHandler) attest(w http.ResponseWriter, r *http.Request, client verifier.Client) {
282 switch r.Method {
283 case http.MethodGet:
284 if err := a.attestAgent.Refresh(a.ctx); err != nil {
285 a.logAndWriteHTTPError(w, http.StatusInternalServerError, fmt.Errorf("failed to refresh attestation agent: %w", err))
286 return
287 }
288
289 token, err := a.attestAgent.AttestWithClient(a.ctx, agent.AttestAgentOpts{}, client)
290 if err != nil {
291 a.handleAttestError(w, err, "failed to retrieve attestation service token")
292 return
293 }
294
295 w.WriteHeader(http.StatusOK)
296 w.Write(token)
297 return
298
299 case http.MethodPost:
300 var tokenOptions models.TokenOptions
301 decoder := json.NewDecoder(r.Body)
302 decoder.DisallowUnknownFields()
303
304 err := decoder.Decode(&tokenOptions)
305 if err != nil {
306 err = fmt.Errorf("failed to parse POST body as TokenOptions: %v", err)
307 a.logAndWriteHTTPError(w, http.StatusBadRequest, err)
308 return
309 }
310
311 if tokenOptions.Audience == "" {
312 err := fmt.Errorf("use GET request for the default identity token")
313 a.logAndWriteHTTPError(w, http.StatusBadRequest, err)
314 return
315 }
316
317 if tokenOptions.TokenType == "" {
318 err := fmt.Errorf("token_type is a required parameter")
319 a.logAndWriteHTTPError(w, http.StatusBadRequest, err)
320 return
321 }
322
323 // Do not check that TokenTypeOptions matches TokenType in the launcher.
324 opts := agent.AttestAgentOpts{
325 TokenOptions: &tokenOptions,
326 }
327 tok, err := a.attestAgent.AttestWithClient(a.ctx, opts, client)
328 if err != nil {
329 a.handleAttestError(w, err, "failed to retrieve custom attestation service token")
330 return
331 }
332
333 w.WriteHeader(http.StatusOK)
334 w.Write(tok)
335 return
336 default:
337 // TODO: add an url pointing to the REST API document
338 err := fmt.Errorf("TEE server received an invalid HTTP method: %s", r.Method)
339 a.logAndWriteHTTPError(w, http.StatusBadRequest, err)
340 }
341 }
342
343 func (a *attestHandler) logAndWriteHTTPError(w http.ResponseWriter, statusCode int, err error) {
344 a.logger.Error(err.Error())
345 w.WriteHeader(statusCode)
346 w.Write([]byte(err.Error()))
347 }
348
349 // Serve starts the server, will block until the server shutdown.
350 func (s *TeeServer) Serve() error {
351 return s.server.Serve(s.netListener)
352 }
353
354 // Shutdown will terminate the server and the underlying listener.
355 func (s *TeeServer) Shutdown(ctx context.Context) error {
356 err := s.server.Shutdown(ctx)
357 err2 := s.netListener.Close()
358
359 if err != nil {
360 return err
361 }
362 if err2 != nil {
363 return err2
364 }
365 return nil
366 }
367
368 func (a *attestHandler) handleAttestError(w http.ResponseWriter, err error, message string) {
369 st, ok := status.FromError(err)
370 if ok {
371 if _, exists := clientErrorCodes[st.Code()]; exists {
372 // User errors, like invalid arguments. Map user errors to 400 Bad Request.
373 a.logAndWriteHTTPError(w, http.StatusBadRequest, fmt.Errorf("%s: %w", message, err))
374 return
375 }
376 // Server-side or transient errors. Map user errors 500 Internal Server Error.
377 a.logAndWriteHTTPError(w, http.StatusInternalServerError, fmt.Errorf("%s: %w", message, err))
378 return
379 }
380 // If it's not a gRPC error, it's likely an internal error within the launcher.
381 // Map user errors 500 Internal Server Error
382 a.logAndWriteHTTPError(w, http.StatusInternalServerError, fmt.Errorf("%s: %w", message, err))
383 }