Debian Codegraph @

launcher/util.go

raw
1 package launcher
2
3 import (
4 "context"
5 "fmt"
6 "io"
7 "os"
8 "path/filepath"
9 "strings"
10
11 "github.com/google/go-tpm/legacy/tpm2"
12 "google.golang.org/api/impersonate"
13 "google.golang.org/api/option"
14 )
15
16 // TPMDAParams holds TPM Dictionary Attack parameters.
17 type TPMDAParams struct {
18 LockoutCounter uint32
19 MaxTries uint32 // aka "MaxAuthFail" in TPM Properties
20 RecoveryTime uint32 // aka "LockoutInterval" in TPM Properties
21 LockoutRecovery uint32 // aka "LockoutRecovery" in TPM Properties
22 StartupClearOrderly bool
23 }
24
25 // FetchImpersonatedToken return an access token for the impersonated service account.
26 func FetchImpersonatedToken(ctx context.Context, serviceAccount string, audience string, opts ...option.ClientOption) ([]byte, error) {
27 config := impersonate.IDTokenConfig{
28 Audience: audience,
29 TargetPrincipal: serviceAccount,
30 IncludeEmail: true,
31 }
32
33 tokenSource, err := impersonate.IDTokenSource(ctx, config, opts...)
34 if err != nil {
35 return nil, fmt.Errorf("error creating token source: %v", err)
36 }
37
38 token, err := tokenSource.Token()
39 if err != nil {
40 return nil, fmt.Errorf("error retrieving token: %v", err)
41 }
42
43 return []byte(token.AccessToken), nil
44 }
45
46 // SetTPMDAParams takes in a TPM and updates its Dictionary Attack parameters
47 // Only MaxAuthFail, LockoutInterval and LockoutRecovery of TPMDAParams are
48 // used in this function.
49 func SetTPMDAParams(tpm io.ReadWriter, daParams TPMDAParams) error {
50 // empty auth
51 auth := tpm2.AuthCommand{
52 Session: tpm2.HandlePasswordSession,
53 Attributes: tpm2.AttrContinueSession,
54 Auth: []byte(""),
55 }
56 return tpm2.DictionaryAttackParameters(tpm, auth, daParams.MaxTries, daParams.RecoveryTime, daParams.LockoutRecovery)
57 }
58
59 // GetTPMDAInfo takes in a TPM and read its Dictionary Attack parameters
60 func GetTPMDAInfo(tpm io.ReadWriter) (*TPMDAParams, error) {
61 var tpmDAParams TPMDAParams
62
63 lockoutCounter, err := getCapabilityProperty(tpm, tpm2.LockoutCounter) // 526
64 if err != nil {
65 return nil, err
66 }
67 tpmDAParams.LockoutCounter = lockoutCounter.Value
68
69 maxAuthFail, err := getCapabilityProperty(tpm, tpm2.MaxAuthFail) // 527
70 if err != nil {
71 return nil, err
72 }
73 tpmDAParams.MaxTries = maxAuthFail.Value
74
75 lockoutInterval, err := getCapabilityProperty(tpm, tpm2.LockoutInterval) // 528
76 if err != nil {
77 return nil, err
78 }
79 tpmDAParams.RecoveryTime = lockoutInterval.Value
80
81 lockoutRecovery, err := getCapabilityProperty(tpm, tpm2.LockoutRecovery) // 529
82 if err != nil {
83 return nil, err
84 }
85 tpmDAParams.LockoutRecovery = lockoutRecovery.Value
86
87 startUpClear, err := getCapabilityProperty(tpm, tpm2.TPMAStartupClear)
88 if err != nil {
89 return nil, err
90 }
91 // get the 31st bit (TPM-Rev-2.0-Part-2-Structures-01.38.pdf, Page 73)
92 tpmDAParams.StartupClearOrderly = (startUpClear.Value&(1<<31)>>31 == 1)
93
94 return &tpmDAParams, nil
95 }
96
97 func getCapabilityProperty(tpm io.ReadWriter, property tpm2.TPMProp) (*tpm2.TaggedProperty, error) {
98 vals, _, err := tpm2.GetCapability(tpm, tpm2.CapabilityTPMProperties, 1, uint32(property))
99 if err != nil {
100 return nil, err
101 }
102 val, ok := vals[0].(tpm2.TaggedProperty)
103 if !ok {
104 return nil, fmt.Errorf("failed to cast returned value to tpm2.TaggedProperty: %v", val)
105 }
106 if val.Tag != property {
107 return nil, fmt.Errorf("failed to get expected property from the TPM, want: %v, got: %v", property, val)
108 }
109 return &val, nil
110 }
111
112 func listFilesWithPrefix(targetDir string, prefix string) ([]string, error) {
113 var targetFiles []string
114
115 entries, err := os.ReadDir(targetDir)
116 if err != nil {
117 return nil, fmt.Errorf("error reading directory: %v", err)
118 }
119 for _, entry := range entries {
120 if entry.IsDir() {
121 continue
122 }
123 if strings.HasPrefix(entry.Name(), prefix) {
124 targetFiles = append(targetFiles, filepath.Join(targetDir, entry.Name()))
125 }
126 }
127 return targetFiles, nil
128 }