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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
// +build !nosecboot
/*
* Copyright (C) 2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package secboot
import (
"io"
"time"
sb "github.com/snapcore/secboot"
)
var (
EFIImageFromBootFile = efiImageFromBootFile
LockTPMSealedKeys = lockTPMSealedKeys
)
func MockSbConnectToDefaultTPM(f func() (*sb.TPMConnection, error)) (restore func()) {
old := sbConnectToDefaultTPM
sbConnectToDefaultTPM = f
return func() {
sbConnectToDefaultTPM = old
}
}
func MockProvisionTPM(f func(tpm *sb.TPMConnection, mode sb.ProvisionMode, newLockoutAuth []byte) error) (restore func()) {
old := provisionTPM
provisionTPM = f
return func() {
provisionTPM = old
}
}
func MockSbAddEFISecureBootPolicyProfile(f func(profile *sb.PCRProtectionProfile, params *sb.EFISecureBootPolicyProfileParams) error) (restore func()) {
old := sbAddEFISecureBootPolicyProfile
sbAddEFISecureBootPolicyProfile = f
return func() {
sbAddEFISecureBootPolicyProfile = old
}
}
func MockSbAddEFIBootManagerProfile(f func(profile *sb.PCRProtectionProfile, params *sb.EFIBootManagerProfileParams) error) (restore func()) {
old := sbAddEFIBootManagerProfile
sbAddEFIBootManagerProfile = f
return func() {
sbAddEFIBootManagerProfile = old
}
}
func MockSbAddSystemdEFIStubProfile(f func(profile *sb.PCRProtectionProfile, params *sb.SystemdEFIStubProfileParams) error) (restore func()) {
old := sbAddSystemdEFIStubProfile
sbAddSystemdEFIStubProfile = f
return func() {
sbAddSystemdEFIStubProfile = old
}
}
func MockSbAddSnapModelProfile(f func(profile *sb.PCRProtectionProfile, params *sb.SnapModelProfileParams) error) (restore func()) {
old := sbAddSnapModelProfile
sbAddSnapModelProfile = f
return func() {
sbAddSnapModelProfile = old
}
}
func MockSbSealKeyToTPMMultiple(f func(tpm *sb.TPMConnection, keys []*sb.SealKeyRequest, params *sb.KeyCreationParams) (sb.TPMPolicyAuthKey, error)) (restore func()) {
old := sbSealKeyToTPMMultiple
sbSealKeyToTPMMultiple = f
return func() {
sbSealKeyToTPMMultiple = old
}
}
func MockSbUpdateKeyPCRProtectionPolicyMultiple(f func(tpm *sb.TPMConnection, keyPaths []string, authKey sb.TPMPolicyAuthKey, pcrProfile *sb.PCRProtectionProfile) error) (restore func()) {
old := sbUpdateKeyPCRProtectionPolicyMultiple
sbUpdateKeyPCRProtectionPolicyMultiple = f
return func() {
sbUpdateKeyPCRProtectionPolicyMultiple = old
}
}
func MockSbBlockPCRProtectionPolicies(f func(tpm *sb.TPMConnection, pcrs []int) error) (restore func()) {
old := sbBlockPCRProtectionPolicies
sbBlockPCRProtectionPolicies = f
return func() {
sbBlockPCRProtectionPolicies = old
}
}
func MockSbActivateVolumeWithRecoveryKey(f func(volumeName, sourceDevicePath string,
keyReader io.Reader, options *sb.ActivateVolumeOptions) error) (restore func()) {
old := sbActivateVolumeWithRecoveryKey
sbActivateVolumeWithRecoveryKey = f
return func() {
sbActivateVolumeWithRecoveryKey = old
}
}
func MockSbActivateVolumeWithTPMSealedKey(f func(tpm *sb.TPMConnection, volumeName, sourceDevicePath, keyPath string,
pinReader io.Reader, options *sb.ActivateVolumeOptions) (bool, error)) (restore func()) {
old := sbActivateVolumeWithTPMSealedKey
sbActivateVolumeWithTPMSealedKey = f
return func() {
sbActivateVolumeWithTPMSealedKey = old
}
}
func MockSbActivateVolumeWithKey(f func(volumeName, sourceDevicePath string, key []byte,
options *sb.ActivateVolumeOptions) error) (restore func()) {
old := sbActivateVolumeWithKey
sbActivateVolumeWithKey = f
return func() {
sbActivateVolumeWithKey = old
}
}
func MockSbMeasureSnapSystemEpochToTPM(f func(tpm *sb.TPMConnection, pcrIndex int) error) (restore func()) {
old := sbMeasureSnapSystemEpochToTPM
sbMeasureSnapSystemEpochToTPM = f
return func() {
sbMeasureSnapSystemEpochToTPM = old
}
}
func MockSbMeasureSnapModelToTPM(f func(tpm *sb.TPMConnection, pcrIndex int, model sb.SnapModel) error) (restore func()) {
old := sbMeasureSnapModelToTPM
sbMeasureSnapModelToTPM = f
return func() {
sbMeasureSnapModelToTPM = old
}
}
func MockRandomKernelUUID(f func() string) (restore func()) {
old := randutilRandomKernelUUID
randutilRandomKernelUUID = f
return func() {
randutilRandomKernelUUID = old
}
}
func MockSbInitializeLUKS2Container(f func(devicePath, label string, key []byte,
opts *sb.InitializeLUKS2ContainerOptions) error) (restore func()) {
old := sbInitializeLUKS2Container
sbInitializeLUKS2Container = f
return func() {
sbInitializeLUKS2Container = old
}
}
func MockSbAddRecoveryKeyToLUKS2Container(f func(devicePath string, key []byte, recoveryKey sb.RecoveryKey) error) (restore func()) {
old := sbAddRecoveryKeyToLUKS2Container
sbAddRecoveryKeyToLUKS2Container = f
return func() {
sbAddRecoveryKeyToLUKS2Container = old
}
}
func MockIsTPMEnabled(f func(tpm *sb.TPMConnection) bool) (restore func()) {
old := isTPMEnabled
isTPMEnabled = f
return func() {
isTPMEnabled = old
}
}
func MockFDEHasRevealKey(f func() bool) (restore func()) {
old := FDEHasRevealKey
FDEHasRevealKey = f
return func() {
FDEHasRevealKey = old
}
}
func MockFdeRevealKeyCommandExtra(args []string) (restore func()) {
oldFdeRevealKeyCommandExtra := fdeRevealKeyCommandExtra
fdeRevealKeyCommandExtra = args
return func() {
fdeRevealKeyCommandExtra = oldFdeRevealKeyCommandExtra
}
}
func MockFdeRevealKeyRuntimeMax(d time.Duration) (restore func()) {
oldFdeRevealKeyRuntimeMax := fdeRevealKeyRuntimeMax
fdeRevealKeyRuntimeMax = d
return func() {
fdeRevealKeyRuntimeMax = oldFdeRevealKeyRuntimeMax
}
}
func MockFdeRevealKeyPollWaitParanoiaFactor(n int) (restore func()) {
oldFdeRevealKeyPollWaitParanoiaFactor := fdeRevealKeyPollWaitParanoiaFactor
fdeRevealKeyPollWaitParanoiaFactor = n
return func() {
fdeRevealKeyPollWaitParanoiaFactor = oldFdeRevealKeyPollWaitParanoiaFactor
}
}
|