| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package tpm |
| 18 | |
| 19 | import ( |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 20 | "bytes" |
| 21 | "crypto" |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 22 | "crypto/rand" |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 23 | "crypto/rsa" |
| 24 | "crypto/x509" |
| Tim Windelschmidt | 3215ee8 | 2024-04-23 15:12:39 +0200 | [diff] [blame] | 25 | "errors" |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 26 | "fmt" |
| 27 | "io" |
| 28 | "os" |
| 29 | "path/filepath" |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 30 | "strconv" |
| Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 31 | "strings" |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 32 | "sync" |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 33 | "time" |
| 34 | |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 35 | tpm2tools "github.com/google/go-tpm-tools/client" |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 36 | "github.com/google/go-tpm/tpm2" |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 37 | "github.com/google/go-tpm/tpmutil" |
| Lorenz Brun | 662182f | 2022-03-10 14:06:48 +0100 | [diff] [blame] | 38 | "golang.org/x/crypto/nacl/secretbox" |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 39 | "golang.org/x/sys/unix" |
| Lorenz Brun | 6570219 | 2023-08-31 16:27:38 +0200 | [diff] [blame] | 40 | "google.golang.org/protobuf/proto" |
| 41 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame^] | 42 | tpmpb "source.monogon.dev/osbase/tpm/proto" |
| Serge Bazanski | 77cb6c5 | 2020-12-19 00:09:22 +0100 | [diff] [blame] | 43 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame^] | 44 | "source.monogon.dev/osbase/logtree" |
| 45 | "source.monogon.dev/osbase/sysfs" |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 46 | ) |
| 47 | |
| 48 | var ( |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 49 | // SecureBootPCRs are all PCRs that measure the current Secure Boot |
| 50 | // configuration. This is what we want if we rely on secure boot to verify |
| 51 | // boot integrity. The firmware hashes the secure boot policy and custom |
| 52 | // keys into the PCR. |
| Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 53 | // |
| 54 | // This requires an extra step that provisions the custom keys. |
| 55 | // |
| 56 | // Some background: https://mjg59.dreamwidth.org/48897.html?thread=1847297 |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 57 | // (the initramfs issue mentioned in the article has been solved by |
| 58 | // integrating it into the kernel binary, and we don't have a shim |
| 59 | // bootloader) |
| Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 60 | // |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 61 | // PCR7 alone is not sufficient - it needs to be combined with firmware |
| 62 | // measurements. |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 63 | SecureBootPCRs = []int{7} |
| 64 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 65 | // FirmwarePCRs are alle PCRs that contain the firmware measurements. See: |
| 66 | // https://trustedcomputinggroup.org/wp-content/uploads/TCG_EFI_Platform_1_22_Final_-v15.pdf |
| Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 67 | FirmwarePCRs = []int{ |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 68 | 0, // platform firmware |
| 69 | 2, // option ROM code |
| 70 | 3, // option ROM configuration and data |
| Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 71 | } |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 72 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 73 | // FullSystemPCRs are all PCRs that contain any measurements up to the |
| 74 | // currently running EFI payload. |
| Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 75 | FullSystemPCRs = []int{ |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 76 | 0, // platform firmware |
| 77 | 1, // host platform configuration |
| 78 | 2, // option ROM code |
| 79 | 3, // option ROM configuration and data |
| 80 | 4, // EFI payload |
| Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 83 | // Using FullSystemPCRs is the most secure, but also the most brittle |
| 84 | // option since updating the EFI binary, updating the platform firmware, |
| 85 | // changing platform settings or updating the binary would invalidate the |
| 86 | // sealed data. It's annoying (but possible) to predict values for PCR4, |
| 87 | // and even more annoying for the firmware PCR (comparison to known values |
| 88 | // on similar hardware is the only thing that comes to mind). |
| Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 89 | // |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 90 | // See also: https://github.com/mxre/sealkey (generates PCR4 from EFI |
| 91 | // image, BSD license) |
| Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 92 | // |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 93 | // Using only SecureBootPCRs is the easiest and still reasonably secure, if |
| 94 | // we assume that the platform knows how to take care of itself (i.e. Intel |
| 95 | // Boot Guard), and that secure boot is implemented properly. It is, |
| 96 | // however, a much larger amount of code we need to trust. |
| Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 97 | // |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 98 | // We do not care about PCR 5 (GPT partition table) since modifying it is |
| 99 | // harmless. All of the boot options and cmdline are hardcoded in the |
| 100 | // kernel image, and we use no bootloader, so there's no PCR for bootloader |
| 101 | // configuration or kernel cmdline. |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 102 | ) |
| 103 | |
| 104 | var ( |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 105 | numSRTMPCRs = 16 |
| 106 | srtmPCRs = tpm2.PCRSelection{Hash: tpm2.AlgSHA256, PCRs: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}} |
| 107 | // TCG Trusted Platform Module Library Level 00 Revision 0.99 Table 6 |
| 108 | tpmGeneratedValue = uint32(0xff544347) |
| 109 | ) |
| 110 | |
| 111 | var ( |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 112 | // ErrNotExists is returned when no TPMs are available in the system |
| 113 | ErrNotExists = errors.New("no TPMs found") |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 114 | // ErrNotInitialized is returned when this package was not initialized |
| 115 | // successfully |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 116 | ErrNotInitialized = errors.New("no TPM was initialized") |
| 117 | ) |
| 118 | |
| 119 | // Singleton since the TPM is too |
| 120 | var tpm *TPM |
| 121 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 122 | // We're serializing all TPM operations since it has a limited number of |
| 123 | // handles and recovering if it runs out is difficult to implement correctly. |
| 124 | // Might also be marginally more secure. |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 125 | var lock sync.Mutex |
| 126 | |
| 127 | // TPM represents a high-level interface to a connected TPM 2.0 |
| 128 | type TPM struct { |
| Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 129 | logger logtree.LeveledLogger |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 130 | device io.ReadWriteCloser |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 131 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 132 | // We keep the AK loaded since it's used fairly often and deriving it is |
| 133 | // expensive |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 134 | akHandleCache tpmutil.Handle |
| 135 | akPublicKey crypto.PublicKey |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 138 | // Initialize finds and opens the TPM (if any). If there is no TPM available it |
| 139 | // returns ErrNotExists |
| Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 140 | func Initialize(logger logtree.LeveledLogger) error { |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 141 | lock.Lock() |
| 142 | defer lock.Unlock() |
| 143 | tpmDir, err := os.Open("/sys/class/tpm") |
| 144 | if err != nil { |
| Tim Windelschmidt | c20e372 | 2024-04-23 15:04:45 +0200 | [diff] [blame] | 145 | return fmt.Errorf("failed to open sysfs TPM class: %w", err) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 146 | } |
| 147 | defer tpmDir.Close() |
| 148 | |
| 149 | tpms, err := tpmDir.Readdirnames(2) |
| 150 | if err != nil { |
| Tim Windelschmidt | c20e372 | 2024-04-23 15:04:45 +0200 | [diff] [blame] | 151 | return fmt.Errorf("failed to read TPM device class: %w", err) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | if len(tpms) == 0 { |
| 155 | return ErrNotExists |
| 156 | } |
| 157 | if len(tpms) > 1 { |
| Lorenz Brun | a50e845 | 2020-09-09 17:09:27 +0200 | [diff] [blame] | 158 | // If this is changed GetMeasurementLog() needs to be updated too |
| Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 159 | logger.Warningf("Found more than one TPM, using the first one") |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 160 | } |
| 161 | tpmName := tpms[0] |
| 162 | ueventData, err := sysfs.ReadUevents(filepath.Join("/sys/class/tpm", tpmName, "uevent")) |
| Tim Windelschmidt | 096654a | 2024-04-18 23:10:19 +0200 | [diff] [blame] | 163 | if err != nil { |
| 164 | return fmt.Errorf("failed to read uevents: %w", err) |
| 165 | } |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 166 | majorDev, err := strconv.Atoi(ueventData["MAJOR"]) |
| 167 | if err != nil { |
| 168 | return fmt.Errorf("failed to convert uevent: %w", err) |
| 169 | } |
| 170 | minorDev, err := strconv.Atoi(ueventData["MINOR"]) |
| 171 | if err != nil { |
| 172 | return fmt.Errorf("failed to convert uevent: %w", err) |
| 173 | } |
| 174 | if err := unix.Mknod("/dev/tpm", 0600|unix.S_IFCHR, int(unix.Mkdev(uint32(majorDev), uint32(minorDev)))); err != nil { |
| Tim Windelschmidt | c20e372 | 2024-04-23 15:04:45 +0200 | [diff] [blame] | 175 | return fmt.Errorf("failed to create TPM device node: %w", err) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 176 | } |
| 177 | device, err := tpm2.OpenTPM("/dev/tpm") |
| 178 | if err != nil { |
| Tim Windelschmidt | c20e372 | 2024-04-23 15:04:45 +0200 | [diff] [blame] | 179 | return fmt.Errorf("failed to open TPM: %w", err) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 180 | } |
| 181 | tpm = &TPM{ |
| 182 | device: device, |
| 183 | logger: logger, |
| 184 | } |
| 185 | return nil |
| 186 | } |
| 187 | |
| Lorenz Brun | 8b78689 | 2022-01-13 14:21:16 +0100 | [diff] [blame] | 188 | // IsInitialized returns true if Initialize was called an at least one |
| 189 | // TPM 2.0 was found and initialized. Otherwise it returns false. |
| 190 | func IsInitialized() bool { |
| 191 | lock.Lock() |
| 192 | defer lock.Unlock() |
| Tim Windelschmidt | 683b62b | 2024-04-18 23:40:33 +0200 | [diff] [blame] | 193 | return tpm != nil |
| Lorenz Brun | 8b78689 | 2022-01-13 14:21:16 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 196 | // GenerateSafeKey uses two sources of randomness (Kernel & TPM) to generate |
| 197 | // the key |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 198 | func GenerateSafeKey(size uint16) ([]byte, error) { |
| 199 | lock.Lock() |
| 200 | defer lock.Unlock() |
| 201 | if tpm == nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 202 | return nil, ErrNotInitialized |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 203 | } |
| 204 | encryptionKeyHost := make([]byte, size) |
| 205 | if _, err := io.ReadFull(rand.Reader, encryptionKeyHost); err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 206 | return nil, fmt.Errorf("failed to generate host portion of new key: %w", err) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 207 | } |
| 208 | var encryptionKeyTPM []byte |
| 209 | for i := 48; i > 0; i-- { |
| 210 | tpmKeyPart, err := tpm2.GetRandom(tpm.device, size-uint16(len(encryptionKeyTPM))) |
| 211 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 212 | return nil, fmt.Errorf("failed to generate TPM portion of new key: %w", err) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 213 | } |
| 214 | encryptionKeyTPM = append(encryptionKeyTPM, tpmKeyPart...) |
| 215 | if len(encryptionKeyTPM) >= int(size) { |
| 216 | break |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if len(encryptionKeyTPM) != int(size) { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 221 | return nil, fmt.Errorf("got incorrect amount of TPM randomess: %v, requested %v", len(encryptionKeyTPM), size) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | encryptionKey := make([]byte, size) |
| 225 | for i := uint16(0); i < size; i++ { |
| 226 | encryptionKey[i] = encryptionKeyHost[i] ^ encryptionKeyTPM[i] |
| 227 | } |
| 228 | return encryptionKey, nil |
| 229 | } |
| 230 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 231 | // Seal seals sensitive data and only allows access if the current platform |
| 232 | // configuration in matches the one the data was sealed on. |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 233 | func Seal(data []byte, pcrs []int) ([]byte, error) { |
| Lorenz Brun | 662182f | 2022-03-10 14:06:48 +0100 | [diff] [blame] | 234 | // Generate a key and use secretbox to encrypt and authenticate the actual |
| 235 | // payload as go-tpm2 uses a raw seal operation limiting payload size to |
| 236 | // 128 bytes which is insufficient. |
| 237 | boxKey, err := GenerateSafeKey(32) |
| 238 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 239 | return nil, fmt.Errorf("failed to generate boxKey: %w", err) |
| Lorenz Brun | 662182f | 2022-03-10 14:06:48 +0100 | [diff] [blame] | 240 | } |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 241 | lock.Lock() |
| 242 | defer lock.Unlock() |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 243 | srk, err := tpm2tools.StorageRootKeyRSA(tpm.device) |
| 244 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 245 | return nil, fmt.Errorf("failed to load TPM SRK: %w", err) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 246 | } |
| 247 | defer srk.Close() |
| Lorenz Brun | 662182f | 2022-03-10 14:06:48 +0100 | [diff] [blame] | 248 | var boxKeyArr [32]byte |
| 249 | copy(boxKeyArr[:], boxKey) |
| 250 | // Nonce is not used as we're generating a new boxKey for every operation, |
| 251 | // therefore we can just leave it all-zero. |
| 252 | var unusedNonce [24]byte |
| 253 | encryptedData := secretbox.Seal(nil, data, &unusedNonce, &boxKeyArr) |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 254 | sealedKey, err := srk.Seal(boxKey, tpm2tools.SealOpts{Current: tpm2.PCRSelection{Hash: tpm2.AlgSHA256, PCRs: pcrs}}) |
| Lorenz Brun | 662182f | 2022-03-10 14:06:48 +0100 | [diff] [blame] | 255 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 256 | return nil, fmt.Errorf("failed to seal boxKey: %w", err) |
| Lorenz Brun | 662182f | 2022-03-10 14:06:48 +0100 | [diff] [blame] | 257 | } |
| 258 | sealedBytes := tpmpb.ExtendedSealedBytes{ |
| 259 | SealedKey: sealedKey, |
| 260 | EncryptedPayload: encryptedData, |
| 261 | } |
| 262 | rawSealedBytes, err := proto.Marshal(&sealedBytes) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 263 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 264 | return nil, fmt.Errorf("failed to marshal sealed data: %w", err) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 265 | } |
| Lorenz Brun | 662182f | 2022-03-10 14:06:48 +0100 | [diff] [blame] | 266 | return rawSealedBytes, nil |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 269 | // Unseal unseals sensitive data if the current platform configuration allows |
| 270 | // and sealing constraints allow it. |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 271 | func Unseal(data []byte) ([]byte, error) { |
| 272 | lock.Lock() |
| 273 | defer lock.Unlock() |
| 274 | if tpm == nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 275 | return nil, ErrNotInitialized |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 276 | } |
| 277 | srk, err := tpm2tools.StorageRootKeyRSA(tpm.device) |
| 278 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 279 | return nil, fmt.Errorf("failed to load TPM SRK: %w", err) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 280 | } |
| 281 | defer srk.Close() |
| 282 | |
| Lorenz Brun | 662182f | 2022-03-10 14:06:48 +0100 | [diff] [blame] | 283 | var sealedBytes tpmpb.ExtendedSealedBytes |
| 284 | if err := proto.Unmarshal(data, &sealedBytes); err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 285 | return nil, fmt.Errorf("failed to unmarshal sealed data: %w", err) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 286 | } |
| Lorenz Brun | ed6bcac | 2022-05-04 17:39:41 +0200 | [diff] [blame] | 287 | if sealedBytes.SealedKey == nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 288 | return nil, fmt.Errorf("sealed data structure is invalid: no sealed key") |
| Lorenz Brun | ed6bcac | 2022-05-04 17:39:41 +0200 | [diff] [blame] | 289 | } |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 290 | // Logging this for auditing purposes |
| Tim Windelschmidt | bda384c | 2024-04-11 01:41:57 +0200 | [diff] [blame] | 291 | var pcrList []string |
| Lorenz Brun | 662182f | 2022-03-10 14:06:48 +0100 | [diff] [blame] | 292 | for _, pcr := range sealedBytes.SealedKey.Pcrs { |
| Lorenz Brun | 800e7c9 | 2023-07-12 22:37:39 +0200 | [diff] [blame] | 293 | pcrList = append(pcrList, strconv.FormatUint(uint64(pcr), 10)) |
| Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 294 | } |
| Lorenz Brun | 662182f | 2022-03-10 14:06:48 +0100 | [diff] [blame] | 295 | tpm.logger.Infof("Attempting to unseal key protected with PCRs %s", strings.Join(pcrList, ",")) |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 296 | unsealedKey, err := srk.Unseal(sealedBytes.SealedKey, tpm2tools.UnsealOpts{}) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 297 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 298 | return nil, fmt.Errorf("failed to unseal key: %w", err) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 299 | } |
| Lorenz Brun | 662182f | 2022-03-10 14:06:48 +0100 | [diff] [blame] | 300 | var key [32]byte |
| 301 | if len(unsealedKey) != len(key) { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 302 | return nil, fmt.Errorf("unsealed key has wrong length: expected %v bytes, got %v", len(key), len(unsealedKey)) |
| Lorenz Brun | 662182f | 2022-03-10 14:06:48 +0100 | [diff] [blame] | 303 | } |
| 304 | copy(key[:], unsealedKey) |
| 305 | var unusedNonce [24]byte |
| 306 | payload, ok := secretbox.Open(nil, sealedBytes.EncryptedPayload, &unusedNonce, &key) |
| 307 | if !ok { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 308 | return nil, errors.New("payload box cannot be opened") |
| Lorenz Brun | 662182f | 2022-03-10 14:06:48 +0100 | [diff] [blame] | 309 | } |
| 310 | return payload, nil |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 311 | } |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 312 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 313 | // Standard AK template for RSA2048 non-duplicatable restricted signing for |
| 314 | // attestation |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 315 | var akTemplate = tpm2.Public{ |
| 316 | Type: tpm2.AlgRSA, |
| 317 | NameAlg: tpm2.AlgSHA256, |
| 318 | Attributes: tpm2.FlagSignerDefault, |
| 319 | RSAParameters: &tpm2.RSAParams{ |
| 320 | Sign: &tpm2.SigScheme{ |
| 321 | Alg: tpm2.AlgRSASSA, |
| 322 | Hash: tpm2.AlgSHA256, |
| 323 | }, |
| 324 | KeyBits: 2048, |
| 325 | }, |
| 326 | } |
| 327 | |
| 328 | func loadAK() error { |
| 329 | var err error |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 330 | // Rationale: The AK is an EK-equivalent key and used only for attestation. |
| 331 | // Using a non-primary key here would require us to store the wrapped |
| 332 | // version somewhere, which is inconvenient. This being a primary key in |
| 333 | // the Endorsement hierarchy means that it can always be recreated and can |
| 334 | // never be "destroyed". Under our security model this is of no concern |
| 335 | // since we identify a node by its IK (Identity Key) which we can destroy. |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 336 | tpm.akHandleCache, tpm.akPublicKey, err = tpm2.CreatePrimary(tpm.device, tpm2.HandleEndorsement, |
| 337 | tpm2.PCRSelection{}, "", "", akTemplate) |
| 338 | return err |
| 339 | } |
| 340 | |
| 341 | // Process documented in TCG EK Credential Profile 2.2.1 |
| 342 | func loadEK() (tpmutil.Handle, crypto.PublicKey, error) { |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 343 | // The EK is a primary key which is supposed to be certified by the |
| 344 | // manufacturer of the TPM. Its public attributes are standardized in TCG |
| 345 | // EK Credential Profile 2.0 Table 1. These need to match exactly or we |
| 346 | // aren't getting the key the manufacturere signed. tpm2tools contains such |
| 347 | // a template already, so we're using that instead of redoing it ourselves. |
| 348 | // This ignores the more complicated ways EKs can be specified, the |
| 349 | // additional stuff you can do is just absolutely crazy (see 2.2.1.2 |
| 350 | // onward) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 351 | return tpm2.CreatePrimary(tpm.device, tpm2.HandleEndorsement, |
| 352 | tpm2.PCRSelection{}, "", "", tpm2tools.DefaultEKTemplateRSA()) |
| 353 | } |
| 354 | |
| 355 | // GetAKPublic gets the TPM2T_PUBLIC of the AK key |
| 356 | func GetAKPublic() ([]byte, error) { |
| 357 | lock.Lock() |
| 358 | defer lock.Unlock() |
| 359 | if tpm == nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 360 | return nil, ErrNotInitialized |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 361 | } |
| 362 | if tpm.akHandleCache == tpmutil.Handle(0) { |
| 363 | if err := loadAK(); err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 364 | return nil, fmt.Errorf("failed to load AK primary key: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | public, _, _, err := tpm2.ReadPublic(tpm.device, tpm.akHandleCache) |
| 368 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 369 | return nil, err |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 370 | } |
| 371 | return public.Encode() |
| 372 | } |
| 373 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 374 | // TCG TPM v2.0 Provisioning Guidance v1.0 7.8 Table 2 and TCG EK Credential |
| 375 | // Profile v2.1 2.2.1.4 de-facto Standard for Windows These are both |
| 376 | // non-normative and reference Windows 10 documentation that's no longer |
| 377 | // available :( But in practice this is what people are using, so if it's |
| 378 | // normative or not doesn't really matter |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 379 | const ekCertHandle = 0x01c00002 |
| 380 | |
| 381 | // GetEKPublic gets the public key and (if available) Certificate of the EK |
| 382 | func GetEKPublic() ([]byte, []byte, error) { |
| 383 | lock.Lock() |
| 384 | defer lock.Unlock() |
| 385 | if tpm == nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 386 | return nil, []byte{}, ErrNotInitialized |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 387 | } |
| 388 | ekHandle, publicRaw, err := loadEK() |
| 389 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 390 | return nil, []byte{}, fmt.Errorf("failed to load EK primary key: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 391 | } |
| 392 | defer tpm2.FlushContext(tpm.device, ekHandle) |
| 393 | // Don't question the use of HandleOwner, that's the Standardâ„¢ |
| 394 | ekCertRaw, err := tpm2.NVReadEx(tpm.device, ekCertHandle, tpm2.HandleOwner, "", 0) |
| 395 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 396 | return nil, []byte{}, err |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | publicKey, err := x509.MarshalPKIXPublicKey(publicRaw) |
| 400 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 401 | return nil, []byte{}, err |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | return publicKey, ekCertRaw, nil |
| 405 | } |
| 406 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 407 | // MakeAKChallenge generates a challenge for TPM residency and attributes of |
| 408 | // the AK |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 409 | func MakeAKChallenge(ekPubKey, akPub []byte, nonce []byte) ([]byte, []byte, error) { |
| 410 | ekPubKeyData, err := x509.ParsePKIXPublicKey(ekPubKey) |
| 411 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 412 | return nil, []byte{}, fmt.Errorf("failed to decode EK pubkey: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 413 | } |
| 414 | akPubData, err := tpm2.DecodePublic(akPub) |
| 415 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 416 | return nil, []byte{}, fmt.Errorf("failed to decode AK public part: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 417 | } |
| 418 | // Make sure we're attesting the right attributes (in particular Restricted) |
| 419 | if !akPubData.MatchesTemplate(akTemplate) { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 420 | return nil, []byte{}, errors.New("the key being challenged is not a valid AK") |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 421 | } |
| 422 | akName, err := akPubData.Name() |
| 423 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 424 | return nil, []byte{}, fmt.Errorf("failed to derive AK name: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 425 | } |
| 426 | return generateRSA(akName.Digest, ekPubKeyData.(*rsa.PublicKey), 16, nonce, rand.Reader) |
| 427 | } |
| 428 | |
| 429 | // SolveAKChallenge solves a challenge for TPM residency of the AK |
| 430 | func SolveAKChallenge(credBlob, secretChallenge []byte) ([]byte, error) { |
| 431 | lock.Lock() |
| 432 | defer lock.Unlock() |
| 433 | if tpm == nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 434 | return nil, ErrNotInitialized |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 435 | } |
| 436 | if tpm.akHandleCache == tpmutil.Handle(0) { |
| 437 | if err := loadAK(); err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 438 | return nil, fmt.Errorf("failed to load AK primary key: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | ekHandle, _, err := loadEK() |
| 443 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 444 | return nil, fmt.Errorf("failed to load EK: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 445 | } |
| 446 | defer tpm2.FlushContext(tpm.device, ekHandle) |
| 447 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 448 | // This is necessary since the EK requires an endorsement handle policy in |
| 449 | // its session. For us this is stupid because we keep all hierarchies open |
| 450 | // anyways since a) we cannot safely store secrets on the OS side |
| 451 | // pre-global unlock and b) it makes no sense in this security model since |
| 452 | // an uncompromised host OS will not let an untrusted entity attest as |
| 453 | // itself and a compromised OS can either not pass PCR policy checks or the |
| 454 | // game's already over (you successfully runtime-exploited a production |
| 455 | // Metropolis node). |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 456 | endorsementSession, _, err := tpm2.StartAuthSession( |
| 457 | tpm.device, |
| 458 | tpm2.HandleNull, |
| 459 | tpm2.HandleNull, |
| 460 | make([]byte, 16), |
| 461 | nil, |
| 462 | tpm2.SessionPolicy, |
| 463 | tpm2.AlgNull, |
| 464 | tpm2.AlgSHA256) |
| 465 | if err != nil { |
| 466 | panic(err) |
| 467 | } |
| 468 | defer tpm2.FlushContext(tpm.device, endorsementSession) |
| 469 | |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 470 | _, _, err = tpm2.PolicySecret(tpm.device, tpm2.HandleEndorsement, tpm2.AuthCommand{Session: tpm2.HandlePasswordSession, Attributes: tpm2.AttrContinueSession}, endorsementSession, nil, nil, nil, 0) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 471 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 472 | return nil, fmt.Errorf("failed to make a policy secret session: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | for { |
| 476 | solution, err := tpm2.ActivateCredentialUsingAuth(tpm.device, []tpm2.AuthCommand{ |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 477 | // Use standard no-password authenatication |
| 478 | {Session: tpm2.HandlePasswordSession, Attributes: tpm2.AttrContinueSession}, |
| 479 | // Use a full policy session for the EK |
| 480 | {Session: endorsementSession, Attributes: tpm2.AttrContinueSession}, |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 481 | }, tpm.akHandleCache, ekHandle, credBlob, secretChallenge) |
| Tim Windelschmidt | af821c8 | 2024-04-23 15:03:52 +0200 | [diff] [blame] | 482 | var warn tpm2.Warning |
| 483 | if errors.As(err, &warn) && warn.Code == tpm2.RCRetry { |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 484 | time.Sleep(100 * time.Millisecond) |
| 485 | continue |
| 486 | } |
| 487 | return solution, err |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // FlushTransientHandles flushes all sessions and non-persistent handles |
| 492 | func FlushTransientHandles() error { |
| 493 | lock.Lock() |
| 494 | defer lock.Unlock() |
| 495 | if tpm == nil { |
| 496 | return ErrNotInitialized |
| 497 | } |
| 498 | flushHandleTypes := []tpm2.HandleType{tpm2.HandleTypeTransient, tpm2.HandleTypeLoadedSession, tpm2.HandleTypeSavedSession} |
| 499 | for _, handleType := range flushHandleTypes { |
| 500 | handles, err := tpm2tools.Handles(tpm.device, handleType) |
| 501 | if err != nil { |
| 502 | return err |
| 503 | } |
| 504 | for _, handle := range handles { |
| 505 | if err := tpm2.FlushContext(tpm.device, handle); err != nil { |
| 506 | return err |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | return nil |
| 511 | } |
| 512 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 513 | // AttestPlatform performs a PCR quote using the AK and returns the quote and |
| 514 | // its signature |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 515 | func AttestPlatform(nonce []byte) ([]byte, []byte, error) { |
| 516 | lock.Lock() |
| 517 | defer lock.Unlock() |
| 518 | if tpm == nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 519 | return nil, []byte{}, ErrNotInitialized |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 520 | } |
| 521 | if tpm.akHandleCache == tpmutil.Handle(0) { |
| 522 | if err := loadAK(); err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 523 | return nil, []byte{}, fmt.Errorf("failed to load AK primary key: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 524 | } |
| 525 | } |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 526 | // We only care about SHA256 since SHA1 is weak. This is supported on at |
| 527 | // least GCE and Intel / AMD fTPM, which is good enough for now. Alg is |
| 528 | // null because that would just hash the nonce, which is dumb. |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 529 | quote, signature, err := tpm2.Quote(tpm.device, tpm.akHandleCache, "", "", nonce, srtmPCRs, |
| 530 | tpm2.AlgNull) |
| 531 | if err != nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 532 | return nil, []byte{}, fmt.Errorf("failed to quote PCRs: %w", err) |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 533 | } |
| 534 | return quote, signature.RSA.Signature, err |
| 535 | } |
| 536 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 537 | // VerifyAttestPlatform verifies a given attestation. You can rely on all data |
| 538 | // coming back as being from the TPM on which the AK is bound to. |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 539 | func VerifyAttestPlatform(nonce, akPub, quote, signature []byte) (*tpm2.AttestationData, error) { |
| 540 | hash := crypto.SHA256.New() |
| 541 | hash.Write(quote) |
| 542 | |
| 543 | akPubData, err := tpm2.DecodePublic(akPub) |
| 544 | if err != nil { |
| 545 | return nil, fmt.Errorf("invalid AK: %w", err) |
| 546 | } |
| 547 | akPublicKey, err := akPubData.Key() |
| 548 | if err != nil { |
| 549 | return nil, fmt.Errorf("invalid AK: %w", err) |
| 550 | } |
| 551 | akRSAKey, ok := akPublicKey.(*rsa.PublicKey) |
| 552 | if !ok { |
| 553 | return nil, errors.New("invalid AK: invalid key type") |
| 554 | } |
| 555 | |
| 556 | if err := rsa.VerifyPKCS1v15(akRSAKey, crypto.SHA256, hash.Sum(nil), signature); err != nil { |
| 557 | return nil, err |
| 558 | } |
| 559 | |
| 560 | quoteData, err := tpm2.DecodeAttestationData(quote) |
| 561 | if err != nil { |
| 562 | return nil, err |
| 563 | } |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 564 | // quoteData.Magic works together with the TPM's Restricted key attribute. |
| 565 | // If this attribute is set (which it needs to be for the AK to be |
| 566 | // considered valid) the TPM will not sign external data having this prefix |
| 567 | // with such a key. Only data that originates inside the TPM like quotes |
| 568 | // and key certifications can have this prefix and sill be signed by a |
| 569 | // restricted key. This check is thus vital, otherwise somebody can just |
| 570 | // feed the TPM an arbitrary attestation to sign with its AK and this |
| 571 | // function will happily accept the forged attestation. |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 572 | if quoteData.Magic != tpmGeneratedValue { |
| 573 | return nil, errors.New("invalid TPM quote: data marker for internal data not set - forged attestation") |
| 574 | } |
| 575 | if quoteData.Type != tpm2.TagAttestQuote { |
| 576 | return nil, errors.New("invalid TPM qoute: not a TPM quote") |
| 577 | } |
| 578 | if !bytes.Equal(quoteData.ExtraData, nonce) { |
| 579 | return nil, errors.New("invalid TPM quote: wrong nonce") |
| 580 | } |
| 581 | |
| 582 | return quoteData, nil |
| 583 | } |
| 584 | |
| 585 | // GetPCRs returns all SRTM PCRs in-order |
| 586 | func GetPCRs() ([][]byte, error) { |
| 587 | lock.Lock() |
| 588 | defer lock.Unlock() |
| 589 | if tpm == nil { |
| Tim Windelschmidt | 3074ec6 | 2024-04-23 15:08:05 +0200 | [diff] [blame] | 590 | return nil, ErrNotInitialized |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 591 | } |
| 592 | pcrs := make([][]byte, numSRTMPCRs) |
| 593 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 594 | // The TPM can (and most do) return partial results. Let's just retry as |
| 595 | // many times as we have PCRs since each read should return at least one |
| 596 | // PCR. |
| Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 597 | readLoop: |
| 598 | for i := 0; i < numSRTMPCRs; i++ { |
| 599 | sel := tpm2.PCRSelection{Hash: tpm2.AlgSHA256} |
| 600 | for pcrN := 0; pcrN < numSRTMPCRs; pcrN++ { |
| 601 | if len(pcrs[pcrN]) == 0 { |
| 602 | sel.PCRs = append(sel.PCRs, pcrN) |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | readPCRs, err := tpm2.ReadPCRs(tpm.device, sel) |
| 607 | if err != nil { |
| 608 | return nil, fmt.Errorf("failed to read PCRs: %w", err) |
| 609 | } |
| 610 | |
| 611 | for pcrN, pcr := range readPCRs { |
| 612 | pcrs[pcrN] = pcr |
| 613 | } |
| 614 | for _, pcr := range pcrs { |
| 615 | // If at least one PCR is still not read, continue |
| 616 | if len(pcr) == 0 { |
| 617 | continue readLoop |
| 618 | } |
| 619 | } |
| 620 | break |
| 621 | } |
| 622 | |
| 623 | return pcrs, nil |
| 624 | } |
| Lorenz Brun | a50e845 | 2020-09-09 17:09:27 +0200 | [diff] [blame] | 625 | |
| Tim Windelschmidt | 8732d43 | 2024-04-18 23:20:05 +0200 | [diff] [blame] | 626 | // GetMeasurementLog returns the binary log of all data hashed into PCRs. The |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 627 | // result can be parsed by eventlog. As this library currently doesn't support |
| 628 | // extending PCRs it just returns the log as supplied by the EFI interface. |
| Lorenz Brun | a50e845 | 2020-09-09 17:09:27 +0200 | [diff] [blame] | 629 | func GetMeasurementLog() ([]byte, error) { |
| Lorenz Brun | 764a2de | 2021-11-22 16:26:36 +0100 | [diff] [blame] | 630 | return os.ReadFile("/sys/kernel/security/tpm0/binary_bios_measurements") |
| Lorenz Brun | a50e845 | 2020-09-09 17:09:27 +0200 | [diff] [blame] | 631 | } |