-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathexport_test.go
More file actions
431 lines (373 loc) · 12.5 KB
/
export_test.go
File metadata and controls
431 lines (373 loc) · 12.5 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// go:build test
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2021 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 (
"context"
"crypto"
"encoding/json"
"io"
"math/big"
"time"
"golang.org/x/sys/unix"
"github.com/snapcore/secboot/internal/keyring"
"github.com/snapcore/secboot/internal/luks2"
"github.com/snapcore/secboot/internal/luksview"
"github.com/snapcore/secboot/internal/paths"
)
const (
ActivateStateCustomDataKey = activateStateCustomDataKey
AuthRequestorKey = authRequestorKey
AuthRequestorUserVisibleNameKey = authRequestorUserVisibleNameKey
ExternalKeyDataKey = externalKeyDataKey
ExternalUnlockKeyKey = externalUnlockKeyKey
KeyringDescPrefixKey = keyringDescPrefixKey
KeyringKeyPurposeAuxiliary = keyringKeyPurposeAuxiliary
LegacyKeyringKeyDescPathsKey = legacyKeyringKeyDescPathsKey
NilHash = nilHash
PassphraseTriesKey = passphraseTriesKey
PinTriesKey = pinTriesKey
RecoveryKeyTriesKey = recoveryKeyTriesKey
StderrLoggerKey = stderrLoggerKey
)
var (
AcquireArgon2OutOfProcessHandlerSystemLock = acquireArgon2OutOfProcessHandlerSystemLock
AddKeyToUserKeyring = addKeyToUserKeyring
AddKeyToUserKeyringLegacy = addKeyToUserKeyringLegacy
ErrArgon2OutOfProcessHandlerSystemLockTimeout = errArgon2OutOfProcessHandlerSystemLockTimeout
ErrInvalidPrimaryKey = errInvalidPrimaryKey
ErrInvalidRecoveryKey = errInvalidRecoveryKey
ErrorToKeyslotError = errorToKeyslotError
FormatKeyringKeyDesc = formatKeyringKeyDesc
FormatUserAuthTypeString = formatUserAuthTypeString
NewActivateOneContainerStateMachine = newActivateOneContainerStateMachine
ParseKeyringKeyDesc = parseKeyringKeyDesc
StorageContainerHandlers = storageContainerHandlers
UnmarshalV1KeyPayload = unmarshalV1KeyPayload
UnmarshalProtectedKeys = unmarshalProtectedKeys
)
type (
ActivateConfigImpl = activateConfig
ActivateConfigKey = activateConfigKey
ActivateOneContainerStateMachine = activateOneContainerStateMachine
ActivateOneContainerStateMachineFlags = activateOneContainerStateMachineFlags
AutoAuthRequestor = autoAuthRequestor
ExternalKeyData = externalKeyData
ExternalUnlockKey = externalUnlockKey
KdfParams = kdfParams
PlymouthAuthRequestor = plymouthAuthRequestor
PlymouthRequestUserCredentialContext = plymouthRequestUserCredentialContext
ProtectedKeys = protectedKeys
SystemdAuthRequestor = systemdAuthRequestor
)
func KDFOptionsKdfParams(opts KDFOptions, defaultTargetDuration time.Duration, keyLen uint32) (*KdfParams, error) {
return opts.kdfParams(defaultTargetDuration, keyLen)
}
func (c activateConfig) Len() int {
return len(c)
}
func (c *ActivateContext) Config() ActivateConfigGetter {
return c.cfg
}
func (c *ActivateContext) PrimaryKey() PrimaryKey {
return c.primaryKey
}
func (m *activateOneContainerStateMachine) ActivationState() (*ContainerActivateState, error) {
return m.activationState()
}
func (m *activateOneContainerStateMachine) PrimaryKeyInfo() (PrimaryKey, keyring.KeyID, error) {
return m.primaryKeyInfo()
}
func (s *ActivateState) Copy() *ActivateState {
out := &ActivateState{
PrimaryKeyID: s.PrimaryKeyID,
}
if s.Activations != nil {
out.Activations = make(map[string]*ContainerActivateState)
for k, v := range s.Activations {
out.Activations[k] = v.Copy()
}
}
return out
}
func (s *ContainerActivateState) Copy() *ContainerActivateState {
out := &ContainerActivateState{
Status: s.Status,
Keyslot: s.Keyslot,
DeactivateReason: s.DeactivateReason,
}
if s.KeyslotErrors != nil {
out.KeyslotErrors = make(map[string]KeyslotErrorType)
for k, v := range s.KeyslotErrors {
out.KeyslotErrors[k] = v
}
}
if s.KeyslotErrorsOrder != nil {
out.KeyslotErrorsOrder = make([]string, len(s.KeyslotErrorsOrder))
copy(out.KeyslotErrorsOrder, s.KeyslotErrorsOrder)
}
if s.CustomData != nil {
out.CustomData = make(json.RawMessage, len(s.CustomData))
copy(out.CustomData, s.CustomData)
}
return out
}
func (o *Argon2Options) KdfParams(defaultTargetDuration time.Duration, keyLen uint32) (*KdfParams, error) {
return o.kdfParams(defaultTargetDuration, keyLen)
}
func (o *PBKDF2Options) KdfParams(defaultTargetDuration time.Duration, keyLen uint32) (*KdfParams, error) {
return o.kdfParams(defaultTargetDuration, keyLen)
}
func MockAddKeyToUserKeyring(fn func([]byte, StorageContainer, KeyringKeyPurpose, string) (keyring.KeyID, error)) (restore func()) {
orig := addKeyToUserKeyring
addKeyToUserKeyring = fn
return func() {
addKeyToUserKeyring = orig
}
}
func MockArgon2OutOfProcessHandlerSystemLockPath(path string) (restore func()) {
orig := paths.Argon2OutOfProcessHandlerSystemLockPath
paths.Argon2OutOfProcessHandlerSystemLockPath = path
return func() {
paths.Argon2OutOfProcessHandlerSystemLockPath = orig
}
}
func MockAcquireArgon2OutOfProcessHandlerSystemLockAcquiredCheckpoint(fn func()) (restore func()) {
orig := acquireArgon2OutOfProcessHandlerSystemLockAcquiredCheckpoint
acquireArgon2OutOfProcessHandlerSystemLockAcquiredCheckpoint = fn
return func() {
acquireArgon2OutOfProcessHandlerSystemLockAcquiredCheckpoint = orig
}
}
func MockArgon2SysLockStderr(w io.Writer) (restore func()) {
orig := argon2SysLockStderr
argon2SysLockStderr = w
return func() {
argon2SysLockStderr = orig
}
}
func MockKeyringAddKey(fn func([]byte, keyring.KeyType, string, keyring.KeyID) (keyring.KeyID, error)) (restore func()) {
orig := keyringAddKey
keyringAddKey = fn
return func() {
keyringAddKey = orig
}
}
func MockLUKS2Activate(fn func(string, string, []byte, int) error) (restore func()) {
origActivate := luks2Activate
luks2Activate = fn
return func() {
luks2Activate = origActivate
}
}
func MockLUKS2AddKey(fn func(string, []byte, []byte, *luks2.AddKeyOptions) error) (restore func()) {
origAddKey := luks2AddKey
luks2AddKey = fn
return func() {
luks2AddKey = origAddKey
}
}
func MockLUKS2Deactivate(fn func(string) error) (restore func()) {
origDeactivate := luks2Deactivate
luks2Deactivate = fn
return func() {
luks2Deactivate = origDeactivate
}
}
func MockLUKS2Format(fn func(string, string, []byte, *luks2.FormatOptions) error) (restore func()) {
origFormat := luks2Format
luks2Format = fn
return func() {
luks2Format = origFormat
}
}
func MockLUKS2ImportToken(fn func(string, luks2.Token, *luks2.ImportTokenOptions) error) (restore func()) {
origImportToken := luks2ImportToken
luks2ImportToken = fn
return func() {
luks2ImportToken = origImportToken
}
}
func MockLUKS2KillSlot(fn func(string, int) error) (restore func()) {
origKillSlot := luks2KillSlot
luks2KillSlot = fn
return func() {
luks2KillSlot = origKillSlot
}
}
func MockLUKS2RemoveToken(fn func(string, int) error) (restore func()) {
origRemoveToken := luks2RemoveToken
luks2RemoveToken = fn
return func() {
luks2RemoveToken = origRemoveToken
}
}
func MockLUKS2SetSlotPriority(fn func(string, int, luks2.SlotPriority) error) (restore func()) {
origSetSlotPriority := luks2SetSlotPriority
luks2SetSlotPriority = fn
return func() {
luks2SetSlotPriority = origSetSlotPriority
}
}
func MockNewLUKSView(fn func(context.Context, string) (*luksview.View, error)) (restore func()) {
origNewLUKSView := newLUKSView
newLUKSView = fn
return func() {
newLUKSView = origNewLUKSView
}
}
func MockNewPlymouthAuthRequestor(fn func(AuthRequestorStringer) (AuthRequestor, error)) (restore func()) {
orig := newPlymouthAuthRequestor
newPlymouthAuthRequestor = fn
return func() {
newPlymouthAuthRequestor = orig
}
}
func MockNewSystemdAuthRequestor(fn func(io.Writer, SystemdAuthRequestorStringFn) (AuthRequestor, error)) (restore func()) {
orig := newSystemdAuthRequestor
newSystemdAuthRequestor = fn
return func() {
newSystemdAuthRequestor = orig
}
}
func MockPBKDF2Benchmark(fn func(time.Duration, crypto.Hash) (uint, error)) (restore func()) {
orig := pbkdf2Benchmark
pbkdf2Benchmark = fn
return func() {
pbkdf2Benchmark = orig
}
}
func MockRuntimeNumCPU(n int) (restore func()) {
orig := runtimeNumCPU
runtimeNumCPU = func() int {
return n
}
return func() {
runtimeNumCPU = orig
}
}
func MockStderr(w io.Writer) (restore func()) {
orig := osStderr
osStderr = w
return func() {
osStderr = orig
}
}
func MockKeyDataGeneration(n int) (restore func()) {
orig := KeyDataGeneration
KeyDataGeneration = n
return func() {
KeyDataGeneration = orig
}
}
func MockHashAlgAvailable() (restore func()) {
orig := hashAlgAvailable
hashAlgAvailable = func(*HashAlg) bool {
return false
}
return func() {
hashAlgAvailable = orig
}
}
func (d *KeyData) DerivePassphraseKeys(passphrase string) (key, iv, auth []byte, err error) {
return d.derivePassphraseKeys(passphrase)
}
func (r *autoAuthRequestor) Requestors() []AuthRequestor {
return r.requestors
}
func (r *autoAuthRequestor) LastUsed() AuthRequestor {
return r.lastUsed
}
func NewAutoAuthRequestorForTesting(requestors []AuthRequestor, lastUsed AuthRequestor) *autoAuthRequestor {
return &autoAuthRequestor{
requestors: requestors,
lastUsed: lastUsed,
}
}
func (r *plymouthAuthRequestor) LastRequestUserCredentialCtx() plymouthRequestUserCredentialContext {
return r.lastRequestUserCredentialCtx
}
func NewPlymouthAuthRequestorForTesting(stringer AuthRequestorStringer, lastRequestUserCredentialCtx *plymouthRequestUserCredentialContext) *plymouthAuthRequestor {
if lastRequestUserCredentialCtx == nil {
var zeroCtx plymouthRequestUserCredentialContext
lastRequestUserCredentialCtx = &zeroCtx
}
return &plymouthAuthRequestor{
stringer: stringer,
lastRequestUserCredentialCtx: *lastRequestUserCredentialCtx,
}
}
func (r *systemdAuthRequestor) LastRequestUserCredentialPath() string {
return r.lastRequestUserCredentialPath
}
func NewSystemdAuthRequestorForTesting(console io.Writer, stringFn SystemdAuthRequestorStringFn, lastRequestUserCredentialPath string) *systemdAuthRequestor {
return &systemdAuthRequestor{
console: console,
stringFn: stringFn,
lastRequestUserCredentialPath: lastRequestUserCredentialPath,
}
}
func MockUnixStat(f func(devicePath string, st *unix.Stat_t) error) (restore func()) {
old := unixStat
unixStat = f
return func() {
unixStat = old
}
}
func MakePIN(length int, data []byte) PIN {
val := new(big.Int).SetBytes(data)
return PIN{
length: uint8(length - 1),
value: *val,
}
}
func NewExternalKeyData(name string, r KeyDataReader, data *KeyData) *externalKeyData {
return &externalKeyData{
name: name,
r: r,
data: data,
}
}
func NewExternalUnlockKey(name string, key DiskUnlockKey, src ExternalUnlockKeySource) *externalUnlockKey {
return &externalUnlockKey{
name: name,
key: key,
src: src,
}
}
func NewInvalidKeyDataError(err error) *InvalidKeyDataError {
return &InvalidKeyDataError{err: err}
}
func NewIncompatibleKeyDataRoleParamsError(err error) *IncompatibleKeyDataRoleParamsError {
return &IncompatibleKeyDataRoleParamsError{err: err}
}
func NewInvalidKeyDataRoleParamsError(err error) *InvalidKeyDataRoleParamsError {
return &InvalidKeyDataRoleParamsError{err: err}
}
func NewUserAuthUnavailableError(err error) *UserAuthUnavailableError {
return &UserAuthUnavailableError{err: err}
}
func NewPlatformUninitializedError(err error) *PlatformUninitializedError {
return &PlatformUninitializedError{err: err}
}
func NewPlatformDeviceUnavailableError(err error) *PlatformDeviceUnavailableError {
return &PlatformDeviceUnavailableError{err: err}
}
func WillCheckStorageContainerBindingOption() *flagOption {
return &willCheckStorageContainerBindingOption
}