| 1 |
package util |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"net/http" |
| 7 |
"net/http/httptest" |
| 8 |
|
| 9 |
"cloud.google.com/go/confidentialcomputing/apiv1/confidentialcomputingpb" |
| 10 |
"github.com/golang-jwt/jwt/v4" |
| 11 |
"golang.org/x/net/http2" |
| 12 |
"google.golang.org/protobuf/encoding/protojson" |
| 13 |
) |
| 14 |
|
| 15 |
// FakeChallengeUUID is the challenge for fake attestation server |
| 16 |
const FakeChallengeUUID = "947b4f7b-e6d4-4cfe-971c-39ffe00268ba" |
| 17 |
|
| 18 |
// FakeTpmNonce is the tpm nonce for fake attestation server |
| 19 |
const FakeTpmNonce = "R29vZ0F0dGVzdFYxeGtJUGlRejFPOFRfTzg4QTRjdjRpQQ==" |
| 20 |
|
| 21 |
// FakeCustomNonce is the custom nonce for fake attestation server |
| 22 |
var FakeCustomNonce = []string{"1234567890", "1234567890"} |
| 23 |
|
| 24 |
// FakeCustomNonce is the custom audience for fake attestation server |
| 25 |
const FakeCustomAudience = "https://api.test.com" |
| 26 |
|
| 27 |
// MockAttestationServer provides fake implementation for the GCE attestation server. |
| 28 |
type MockAttestationServer struct { |
| 29 |
Server *httptest.Server |
| 30 |
} |
| 31 |
|
| 32 |
type fakeOidcTokenPayload struct { |
| 33 |
Audience string `json:"aud"` |
| 34 |
IssuedAt int64 `json:"iat"` |
| 35 |
ExpiredAt int64 `json:"exp"` |
| 36 |
} |
| 37 |
|
| 38 |
func (payload *fakeOidcTokenPayload) Valid() error { |
| 39 |
return nil |
| 40 |
} |
| 41 |
|
| 42 |
// NewMockAttestationServer creates a mock verifier |
| 43 |
func NewMockAttestationServer() (*MockAttestationServer, error) { |
| 44 |
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 45 |
locationPath := "/v1/projects/test-project/locations/us-central" |
| 46 |
if r.URL.Path == locationPath { |
| 47 |
location := "{\n \"name\": \"projects/test-project/locations/us-central-1\",\n \"locationId\": \"us-central-1\"\n}\n" |
| 48 |
w.Write([]byte(location)) |
| 49 |
} |
| 50 |
challengePath := locationPath + "-1/challenges" |
| 51 |
if r.URL.Path == challengePath { |
| 52 |
challenge := "{\n \"name\": \"projects/test-project/locations/us-central-1/challenges/" + FakeChallengeUUID + "\",\n \"createTime\": \"2023-09-21T01:04:48.230111757Z\",\n \"expireTime\": \"2023-09-21T02:04:48.230111757Z\",\n \"tpmNonce\": \"" + FakeTpmNonce + "\"\n}\n" |
| 53 |
w.Write([]byte(challenge)) |
| 54 |
} |
| 55 |
verifyAttestationPath := challengePath + "/" + FakeChallengeUUID + ":verifyAttestation" |
| 56 |
if r.URL.Path == verifyAttestationPath { |
| 57 |
err := validateCustomNonceAndAudienceFromRequest(r) |
| 58 |
if err != nil { |
| 59 |
fmt.Print("error validating Custom Nonce and Custom Audience") |
| 60 |
http.Error(w, "Invalid Nonce or Audience", http.StatusBadRequest) // Return 400 Bad Request |
| 61 |
return |
| 62 |
} |
| 63 |
payload := &fakeOidcTokenPayload{ |
| 64 |
Audience: "test", |
| 65 |
IssuedAt: 1709752525, |
| 66 |
ExpiredAt: 1919752525, |
| 67 |
} |
| 68 |
jwtTokenUnsigned := jwt.NewWithClaims(jwt.SigningMethodHS256, payload) |
| 69 |
fakeJwtToken, err := jwtTokenUnsigned.SignedString([]byte("kcxjxnalpraetgccnnwhpnfwocxscaih")) |
| 70 |
if err != nil { |
| 71 |
fmt.Print("error creating test OIDC token") |
| 72 |
http.Error(w, "Invalid OIDC token creation", http.StatusBadRequest) // Return 400 Bad Request |
| 73 |
return |
| 74 |
} |
| 75 |
w.Write([]byte("{\n \"oidcClaimsToken\": \"" + fakeJwtToken + "\"\n}\n")) |
| 76 |
} |
| 77 |
}) |
| 78 |
httpServer := httptest.NewUnstartedServer(handler) |
| 79 |
if err := http2.ConfigureServer(httpServer.Config, new(http2.Server)); err != nil { |
| 80 |
return nil, fmt.Errorf("failed to configure HTTP/2 server: %v", err) |
| 81 |
} |
| 82 |
httpServer.Start() |
| 83 |
|
| 84 |
return &MockAttestationServer{Server: httpServer}, nil |
| 85 |
} |
| 86 |
|
| 87 |
// Stop shuts down the server. |
| 88 |
func (s *MockAttestationServer) Stop() { |
| 89 |
s.Server.Close() |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
// validateCustomNonceAndAudienceFromRequest validates the custom nonce and custom audience from a VerifyAttestationRequest. |
| 94 |
func validateCustomNonceAndAudienceFromRequest(r *http.Request) error { |
| 95 |
req := &confidentialcomputingpb.VerifyAttestationRequest{} |
| 96 |
body, err := io.ReadAll(r.Body) |
| 97 |
if err != nil { |
| 98 |
return fmt.Errorf("error reading request body: %v", err) |
| 99 |
} |
| 100 |
defer r.Body.Close() |
| 101 |
|
| 102 |
if err := protojson.Unmarshal(body, req); err != nil { |
| 103 |
return fmt.Errorf("error decoding attestation request: %v", err) |
| 104 |
} |
| 105 |
|
| 106 |
if req.TokenOptions != nil { |
| 107 |
if req.TokenOptions.Nonce != nil { |
| 108 |
if req.TokenOptions.Nonce[0] != FakeCustomNonce[0] || req.TokenOptions.Nonce[1] != FakeCustomNonce[1] { |
| 109 |
return fmt.Errorf("error comparing custom nonce: %v", err) |
| 110 |
} |
| 111 |
} |
| 112 |
if req.TokenOptions.Audience != "" { |
| 113 |
if req.TokenOptions.Audience != FakeCustomAudience { |
| 114 |
return fmt.Errorf("error comparing custom audience: %v", err) |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return nil |
| 120 |
} |