| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame^] | 1 | package launch |
| Serge Bazanski | 2b6dc31 | 2024-06-04 17:44:55 +0200 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "sort" |
| 11 | "strings" |
| 12 | |
| 13 | "github.com/bazelbuild/rules_go/go/runfiles" |
| 14 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame^] | 15 | "source.monogon.dev/osbase/test/launch" |
| Serge Bazanski | 2b6dc31 | 2024-06-04 17:44:55 +0200 | [diff] [blame] | 16 | ) |
| 17 | |
| 18 | // A TPMFactory manufactures virtual TPMs using swtpm. |
| 19 | // |
| 20 | // A factory has an assigned state directory into which it will write per-factory |
| 21 | // data (like CA certificates and keys). Each manufactured TPM also has a state |
| 22 | // directory, which is first generated on manufacturing, and then passed to an |
| 23 | // swtpm instance. |
| 24 | type TPMFactory struct { |
| 25 | stateDir string |
| 26 | } |
| 27 | |
| 28 | // NewTPMFactory creates a new TPM factory at a given state path. The state path |
| 29 | // is a directory used to persist TPM factory data. It will be created if needed, |
| 30 | // and can be reused across TPM factories (but not used in parallel). |
| 31 | func NewTPMFactory(stateDir string) (*TPMFactory, error) { |
| 32 | if err := os.MkdirAll(stateDir, 0744); err != nil { |
| 33 | return nil, fmt.Errorf("could not create state directory: %w", err) |
| 34 | } |
| 35 | |
| 36 | f := &TPMFactory{ |
| 37 | stateDir: stateDir, |
| 38 | } |
| 39 | |
| 40 | if err := os.MkdirAll(f.caDir(), 0700); err != nil { |
| 41 | return nil, fmt.Errorf("could not create CA state directory: %w", err) |
| 42 | } |
| 43 | err := writeSWTPMConfig(f.localCAConfPath(), map[string]string{ |
| 44 | "statedir": f.caDir(), |
| 45 | "signingkey": filepath.Join(f.caDir(), "signkey.pem"), |
| 46 | "issuercert": filepath.Join(f.caDir(), "issuercert.pem"), |
| 47 | "certserial": filepath.Join(f.caDir(), "certserial"), |
| 48 | }) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | return f, nil |
| 53 | } |
| 54 | |
| 55 | func (f *TPMFactory) caDir() string { |
| 56 | return filepath.Join(f.stateDir, "ca") |
| 57 | } |
| 58 | |
| 59 | func (f *TPMFactory) localCAConfPath() string { |
| 60 | return filepath.Join(f.caDir(), "swtpm-localca.conf") |
| 61 | } |
| 62 | |
| 63 | func (f *TPMFactory) localCAOptionsPath() string { |
| 64 | return filepath.Join(f.caDir(), "swtpm-localca.options") |
| 65 | } |
| 66 | |
| 67 | func (f *TPMFactory) swtpmConfPath() string { |
| 68 | return filepath.Join(f.stateDir, "swtpm.conf") |
| 69 | } |
| 70 | |
| 71 | // writeSWTPMConfig serializes a key/value config file for swtpm tools into a |
| 72 | // path. |
| 73 | func writeSWTPMConfig(path string, data map[string]string) error { |
| 74 | var keys []string |
| 75 | for k := range data { |
| 76 | keys = append(keys, k) |
| 77 | } |
| 78 | sort.Strings(keys) |
| 79 | |
| 80 | f, err := os.Create(path) |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | defer f.Close() |
| 85 | for _, k := range keys { |
| 86 | if _, err := fmt.Fprintf(f, "%s = %s\n", k, data[k]); err != nil { |
| 87 | return err |
| 88 | } |
| 89 | } |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | // A TPMPlatform defines a platform that a TPM is part of. This will usually be |
| 94 | // some kind of device, in this case a virtual device. |
| 95 | type TPMPlatform struct { |
| 96 | Manufacturer string |
| 97 | Version string |
| 98 | Model string |
| 99 | } |
| 100 | |
| 101 | // Manufacture builds a new TPM for a given platform at a path. The path points |
| 102 | // to a directory that will be created if it doens't exist yet, and can be passed |
| 103 | // to swtpm to actually emulate the created TPM. |
| 104 | func (f *TPMFactory) Manufacture(ctx context.Context, path string, platform *TPMPlatform) error { |
| 105 | launch.Log("Starting to manufacture TPM for %s... (%+v)", path, platform) |
| 106 | |
| 107 | // Path to state file. Used to make sure Manufacture runs only once. |
| 108 | permall := filepath.Join(path, "tpm2-00.permall") |
| 109 | |
| 110 | if _, err := os.Stat(permall); err == nil { |
| 111 | launch.Log("Skipping manufacturing TPM for %s, already exists", path) |
| 112 | return nil |
| 113 | } |
| 114 | |
| 115 | // Find all tools. |
| 116 | swtpm, err := runfiles.Rlocation("swtpm/swtpm") |
| 117 | if err != nil { |
| 118 | return fmt.Errorf("could not find swtpm: %w", err) |
| 119 | } |
| 120 | swtpmSetup, err := runfiles.Rlocation("swtpm/swtpm_setup") |
| 121 | if err != nil { |
| 122 | return fmt.Errorf("could not find swtpm_setup: %w", err) |
| 123 | } |
| 124 | swtpmLocalca, err := runfiles.Rlocation("swtpm/swtpm_localca") |
| 125 | if err != nil { |
| 126 | return fmt.Errorf("could not find swtpm_localca: %w", err) |
| 127 | } |
| 128 | swtpmCert, err := runfiles.Rlocation("_main/metropolis/test/swtpm/swtpm_cert/swtpm_cert_/swtpm_cert") |
| 129 | if err != nil { |
| 130 | return fmt.Errorf("could not find swtpm_cert: %w", err) |
| 131 | } |
| 132 | certtool, err := runfiles.Rlocation("_main/metropolis/test/swtpm/certtool/certtool_/certtool") |
| 133 | if err != nil { |
| 134 | return fmt.Errorf("could not find certtool: %w", err) |
| 135 | } |
| 136 | |
| 137 | // Prepare swtpm-localca.options. |
| 138 | options := []string{ |
| 139 | "--platform-manufacturer " + platform.Manufacturer, |
| 140 | "--platform-version " + platform.Version, |
| 141 | "--platform-model " + platform.Model, |
| 142 | "", |
| 143 | } |
| 144 | err = os.WriteFile(f.localCAOptionsPath(), []byte(strings.Join(options, "\n")), 0600) |
| 145 | if err != nil { |
| 146 | return fmt.Errorf("could not write local options: %w", err) |
| 147 | } |
| 148 | |
| 149 | // Prepare swptm.conf. |
| 150 | err = writeSWTPMConfig(f.swtpmConfPath(), map[string]string{ |
| 151 | "create_certs_tool": swtpmLocalca, |
| 152 | "create_certs_tool_config": f.localCAConfPath(), |
| 153 | "create_certs_tool_options": f.localCAOptionsPath(), |
| 154 | }) |
| 155 | if err != nil { |
| 156 | return fmt.Errorf("could not write swtpm.conf: %w", err) |
| 157 | } |
| 158 | |
| 159 | if err := os.MkdirAll(path, 0700); err != nil { |
| 160 | return fmt.Errorf("could not make output path: %w", err) |
| 161 | } |
| 162 | cmd := exec.CommandContext(ctx, swtpmSetup, |
| 163 | "--tpm", fmt.Sprintf("%s socket", swtpm), |
| 164 | "--tpmstate", path, |
| 165 | "--create-ek-cert", |
| 166 | "--create-platform-cert", |
| 167 | "--allow-signing", |
| 168 | "--tpm2", |
| 169 | "--display", |
| 170 | "--pcr-banks", "sha1,sha256,sha384,sha512", |
| 171 | "--config", f.swtpmConfPath()) |
| 172 | cmd.Env = append(cmd.Env, fmt.Sprintf("PATH=%s:%s", filepath.Dir(swtpmCert), filepath.Dir(certtool))) |
| 173 | cmd.Env = append(cmd.Env, "MONOGON_LIBTPMS_ACKNOWLEDGE_UNSAFE=yes") |
| 174 | if out, err := cmd.CombinedOutput(); err != nil { |
| 175 | log.Printf("Manufacturing TPM for %s failed: swtm_setup: %s", path, out) |
| 176 | return fmt.Errorf("swtpm_setup failed: %w", err) |
| 177 | } |
| 178 | |
| 179 | if _, err := os.Stat(permall); os.IsNotExist(err) { |
| 180 | log.Printf("Manufacturing TPM for %s failed: state file did not get created", path) |
| 181 | return fmt.Errorf("%s did not get created during TPM manufacture", permall) |
| 182 | } |
| 183 | |
| 184 | launch.Log("Successfully manufactured TPM for %s", path) |
| 185 | return nil |
| 186 | } |