-
Notifications
You must be signed in to change notification settings - Fork 68
/
rest.go
228 lines (203 loc) · 7.19 KB
/
rest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Package rest contains the code to use the REST-based Google API
package rest
import (
"context"
"encoding/base64"
"fmt"
"log"
"strings"
"github.com/google/go-tpm-tools/verifier"
"github.com/google/go-tpm-tools/verifier/oci"
v1 "cloud.google.com/go/confidentialcomputing/apiv1"
confidentialcomputingpb "cloud.google.com/go/confidentialcomputing/apiv1/confidentialcomputingpb"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
locationpb "google.golang.org/genproto/googleapis/cloud/location"
)
// BadRegionError indicates that:
// - the requested Region cannot be used with this API
// - other Regions _can_ be used with this API
type BadRegionError struct {
RequestedRegion string
AvailableRegions []string
err error
}
func (e *BadRegionError) Error() string {
return fmt.Sprintf(
"invalid region %q, available regions are [%s]: %v",
e.RequestedRegion, strings.Join(e.AvailableRegions, ", "), e.err,
)
}
func (e *BadRegionError) Unwrap() error {
return e.err
}
// NewClient creates a new REST client which is configured to perform
// attestations in a particular project and region. Returns a *BadRegionError
// if the requested project is valid, but the region is invalid.
func NewClient(ctx context.Context, projectID string, region string, opts ...option.ClientOption) (verifier.Client, error) {
client, err := v1.NewRESTClient(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("can't create ConfidentialComputing v1 API client: %w", err)
}
projectName := fmt.Sprintf("projects/%s", projectID)
locationName := fmt.Sprintf("%s/locations/%v", projectName, region)
getReq := &locationpb.GetLocationRequest{
Name: locationName,
}
location, getErr := client.GetLocation(ctx, getReq)
if getErr == nil {
return &restClient{client, location}, nil
}
// If we can't get the location, try to list the locations. This handles
// situations where the projectID is invalid.
listReq := &locationpb.ListLocationsRequest{
Name: projectName,
}
listIter := client.ListLocations(ctx, listReq)
// The project is valid, but can't get the desired region.
var regions []string
for {
resp, err := listIter.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, fmt.Errorf("listing regions in project %q: %w", projectID, err)
}
regions = append(regions, resp.LocationId)
}
return nil, &BadRegionError{
RequestedRegion: region,
AvailableRegions: regions,
err: getErr,
}
}
type restClient struct {
v1Client *v1.Client
location *locationpb.Location
}
// CreateChallenge implements verifier.Client
func (c *restClient) CreateChallenge(ctx context.Context) (*verifier.Challenge, error) {
// Pass an empty Challenge for the input (all params are output-only)
req := &confidentialcomputingpb.CreateChallengeRequest{
Parent: c.location.Name,
Challenge: &confidentialcomputingpb.Challenge{},
}
chal, err := c.v1Client.CreateChallenge(ctx, req)
if err != nil {
return nil, fmt.Errorf("calling v1.CreateChallenge: %w", err)
}
return convertChallengeFromREST(chal)
}
// VerifyAttestation implements verifier.Client
func (c *restClient) VerifyAttestation(ctx context.Context, request verifier.VerifyAttestationRequest) (*verifier.VerifyAttestationResponse, error) {
if request.Challenge == nil || request.Attestation == nil {
return nil, fmt.Errorf("nil value provided in challenge")
}
req := convertRequestToREST(request)
req.Challenge = request.Challenge.Name
response, err := c.v1Client.VerifyAttestation(ctx, req)
if err != nil {
return nil, fmt.Errorf("calling v1.VerifyAttestation: %w", err)
}
return convertResponseFromREST(response)
}
var encoding = base64.StdEncoding
func convertChallengeFromREST(chal *confidentialcomputingpb.Challenge) (*verifier.Challenge, error) {
nonce, err := encoding.DecodeString(chal.TpmNonce)
if err != nil {
return nil, fmt.Errorf("failed to decode Challenge.Nonce: %w", err)
}
return &verifier.Challenge{
Name: chal.Name,
Nonce: nonce,
}, nil
}
func convertRequestToREST(request verifier.VerifyAttestationRequest) *confidentialcomputingpb.VerifyAttestationRequest {
idTokens := make([]string, len(request.GcpCredentials))
for i, token := range request.GcpCredentials {
idTokens[i] = string(token)
}
quotes := make([]*confidentialcomputingpb.TpmAttestation_Quote, len(request.Attestation.GetQuotes()))
for i, quote := range request.Attestation.GetQuotes() {
pcrVals := map[int32][]byte{}
for idx, val := range quote.GetPcrs().GetPcrs() {
pcrVals[int32(idx)] = val
}
quotes[i] = &confidentialcomputingpb.TpmAttestation_Quote{
RawQuote: quote.GetQuote(),
RawSignature: quote.GetRawSig(),
HashAlgo: int32(quote.GetPcrs().GetHash()),
PcrValues: pcrVals,
}
}
certs := make([][]byte, len(request.Attestation.GetIntermediateCerts()))
for i, cert := range request.Attestation.GetIntermediateCerts() {
certs[i] = cert
}
signatures := make([]*confidentialcomputingpb.ContainerImageSignature, len(request.ContainerImageSignatures))
for i, sig := range request.ContainerImageSignatures {
signature, err := convertOCISignatureToREST(sig)
if err != nil {
log.Printf("failed to convert OCI signature [%v] to ContainerImageSignature proto: %v", sig, err)
continue
}
signatures[i] = signature
}
var tokenType confidentialcomputingpb.TokenType
switch request.TokenOptions.TokenType {
case "OIDC":
tokenType = confidentialcomputingpb.TokenType_TOKEN_TYPE_OIDC
case "PKI":
tokenType = confidentialcomputingpb.TokenType_TOKEN_TYPE_PKI
case "LIMITED_AWS":
tokenType = confidentialcomputingpb.TokenType_TOKEN_TYPE_LIMITED_AWS
default:
tokenType = confidentialcomputingpb.TokenType_TOKEN_TYPE_UNSPECIFIED
}
return &confidentialcomputingpb.VerifyAttestationRequest{
GcpCredentials: &confidentialcomputingpb.GcpCredentials{
ServiceAccountIdTokens: idTokens,
},
TpmAttestation: &confidentialcomputingpb.TpmAttestation{
Quotes: quotes,
TcgEventLog: request.Attestation.GetEventLog(),
CanonicalEventLog: request.Attestation.GetCanonicalEventLog(),
AkCert: request.Attestation.GetAkCert(),
CertChain: certs,
},
ConfidentialSpaceInfo: &confidentialcomputingpb.ConfidentialSpaceInfo{
SignedEntities: []*confidentialcomputingpb.SignedEntity{{ContainerImageSignatures: signatures}},
},
TokenOptions: &confidentialcomputingpb.TokenOptions{
Audience: request.TokenOptions.CustomAudience,
Nonce: request.TokenOptions.CustomNonce,
TokenType: tokenType,
},
}
}
func convertResponseFromREST(resp *confidentialcomputingpb.VerifyAttestationResponse) (*verifier.VerifyAttestationResponse, error) {
token := []byte(resp.GetOidcClaimsToken())
return &verifier.VerifyAttestationResponse{
ClaimsToken: token,
PartialErrs: resp.PartialErrors,
}, nil
}
func convertOCISignatureToREST(signature oci.Signature) (*confidentialcomputingpb.ContainerImageSignature, error) {
payload, err := signature.Payload()
if err != nil {
return nil, err
}
b64Sig, err := signature.Base64Encoded()
if err != nil {
return nil, err
}
sigBytes, err := encoding.DecodeString(b64Sig)
if err != nil {
return nil, err
}
return &confidentialcomputingpb.ContainerImageSignature{
Payload: payload,
Signature: sigBytes,
}, nil
}