-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathactivate.go
More file actions
1309 lines (1146 loc) · 47.3 KB
/
activate.go
File metadata and controls
1309 lines (1146 loc) · 47.3 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2025 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/subtle"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"sort"
internal_bootscope "github.com/snapcore/secboot/internal/bootscope"
"github.com/snapcore/secboot/internal/keyring"
"golang.org/x/sys/unix"
)
var (
// ErrCannotActivate is returned from ActivateContext.ActivateContainer
// if a storage container cannot be activated because there are no valid
// keyslots and / or there are no more passphrase, PIN or recovery key
// attempts remaining.
ErrCannotActivate = errors.New("cannot activate: no valid keyslots and / or no more passphrase, PIN or recovery key tries remaining")
errInvalidPrimaryKey = errors.New("invalid primary key")
errNoPrimaryKey = errors.New("no primary key was obtained during activation")
errInvalidRecoveryKey = errors.New("the supplied recovery key is incorrect")
)
// errorKeyslot is used to represent a Keyslot in a keyslotAttemptRecord
// in the case where StorageContainerReader.ReadKeyslot returns an error.
type errorKeyslot struct {
slotType KeyslotType
name string
}
func (i *errorKeyslot) Type() KeyslotType {
return i.slotType
}
func (i *errorKeyslot) Name() string {
return i.name
}
func (i *errorKeyslot) Priority() int {
return 0
}
func (i *errorKeyslot) Data() KeyDataReader {
return nil
}
// keyslotAttemptRecord binds together information about a keyslot.
type keyslotAttemptRecord struct {
slot Keyslot // The backend supplied Keyslot.
data *KeyData // A cache of the decoded KeyData for platform keys.
flags PlatformKeyDataHandlerFlags // The flags that the handling platform is registered with.
externalUnlockKey *externalUnlockKey // An externally recovered unlock key.
userAuthUnavailable bool // The platform indicated that user auth is unavailable.
err error // The first error that occurred with this keyslot.
errNumber int // The number of the error, used for ordering.
}
func (r *keyslotAttemptRecord) usable(flags activateOneContainerStateMachineFlags) bool {
if r.err != nil {
// In general, a keyslot that has encountered an error becomes unusable,
// with one exception being that if the error is a result of the user
// supplying an incorrect credential (passphrase, PIN, or recovery key),
// it should remain usable.
var expectedUserAuthErr error
switch {
// XXX: Keep this commented out for now because we don't check recovery
// keyslot usability once we have built the initial list of them.
//case r.slot.Type() == KeyslotTypeRecovery:
// // Recovery keyslot
// expectedUserAuthErr = errInvalidRecoveryKey
case r.slot.Type() == KeyslotTypePlatform && r.data != nil && r.data.AuthMode() == AuthModePassphrase:
// Passphrase keyslot
expectedUserAuthErr = ErrInvalidPassphrase
case r.slot.Type() == KeyslotTypePlatform && r.data != nil && r.data.AuthMode() == AuthModePIN:
expectedUserAuthErr = ErrInvalidPIN
default:
// Any other type of keyslot is unusable with any error.
return false
}
if !errors.Is(r.err, expectedUserAuthErr) || r.userAuthUnavailable {
// Anything other than a user auth error makes a keyslot unusable. It
// is also unusable if the platform indicated on a previous attempt
// that user auth is not available.
return false
}
}
// Check if the keyslot is usable with the current flags.
switch {
case r.slot.Type() == KeyslotTypeRecovery && flags&activatePermitRecoveryKey == 0:
// Recovery keys are not permitted.
return false
case r.slot.Type() == KeyslotTypePlatform && flags&activateRequirePlatformKeyProtectedByStorageContainer > 0:
// Platform keys are permitted if they are protected by a platform registered with
// the PlatformProtectedByStorageContainer flag.
if r.flags&PlatformProtectedByStorageContainer == 0 {
// The platform is not registered with the PlatformProtectedByStorageContainer flag.
return false
}
case r.slot.Type() == KeyslotTypePlatform && r.data != nil && r.data.Generation() < 2 && flags&activateCrossCheckPrimaryKey > 0:
// v1 platform keys are not permitted if we are in a context where the primary
// key needs to be crosschecked with those recovered from other storage containers.
return false
case r.slot.Type() == KeyslotTypePlatform && r.externalUnlockKey != nil && flags&activateCrossCheckPrimaryKey > 0:
// External unlock keys are permitted if they have a source of
// ExternalUnlockKeyFromStorageContainer, as they don't have a primary key
// that can be checked against those recovered from other storage containers.
if r.flags&PlatformProtectedByStorageContainer == 0 {
// The unlock key doesn't have a source of ExternalUnlockKeyFromStorageContainer.
return false
}
}
return true
}
type keyslotAttemptRecordPrioritySlice []*keyslotAttemptRecord
func (s keyslotAttemptRecordPrioritySlice) Len() int {
return len(s)
}
func (s keyslotAttemptRecordPrioritySlice) Less(i, j int) bool {
switch {
case s[i].slot.Priority() != s[j].slot.Priority():
// Order higher priority keyslots first.
return s[i].slot.Priority() > s[j].slot.Priority()
default:
// Order keyslots with the same priority in
// name order.
return s[i].slot.Name() < s[j].slot.Name()
}
}
func (s keyslotAttemptRecordPrioritySlice) Swap(i, j int) {
tmp := s[j]
s[j] = s[i]
s[i] = tmp
}
type keyslotAttemptRecordErrorSlice []*keyslotAttemptRecord
func (s keyslotAttemptRecordErrorSlice) Len() int {
return len(s)
}
func (s keyslotAttemptRecordErrorSlice) Less(i, j int) bool {
return s[i].errNumber < s[j].errNumber
}
func (s keyslotAttemptRecordErrorSlice) Swap(i, j int) {
tmp := s[j]
s[j] = s[i]
s[i] = tmp
}
func (s keyslotAttemptRecordPrioritySlice) hasUsable(flags activateOneContainerStateMachineFlags) bool {
for _, slot := range s {
if slot.usable(flags) {
return true
}
}
return false
}
type activateOneContainerStateMachineFlags int
const (
// activatePermitRecoveryKey allows recovery keyslots to be used.
// Note that platform keyslots are always permitted to be used.
activatePermitRecoveryKey activateOneContainerStateMachineFlags = 1 << iota
// activateRequrePlatformProtectedByStorageContainer is used to
// require that platform keyslots are protected by platforms registered
// with the PlatformProtectedByStorageContainer flag in order to
// be used.
activateRequirePlatformKeyProtectedByStorageContainer
// activateCrossCheckPrimaryKey is used to require that the
// primary key recovered from a platform keyslot is cross-checked
// against a previously used primary key before it can be used
// for unlocking.
activateCrossCheckPrimaryKey
)
// activateOneContainerStateMachineTask describes a state of
// the state machine, including a readable name and a callback function.
type activateOneContainerStateMachineTask struct {
name string
fn func(context.Context) error
}
// activateOneContainerStateMachine is a state machine for activating
// a single StorageContainer.
type activateOneContainerStateMachine struct {
container StorageContainer // The associated storage contaier.
// cfg is the configuration for this activation, including those
// supplied to NewActivateContext (which apply to all activations),
// and those supplied to ActivateContext.ActivateContainer (which
// inherits the global configuration).
cfg ActivateConfigGetter
primaryKey PrimaryKey // The primary key obtained from a previous activation
flags activateOneContainerStateMachineFlags
stderr io.Writer // For writing error messages to.
next activateOneContainerStateMachineTask // The next task to run.
err error // The first fatal error for this statemachine
status ActivationStatus // Whether and how this container is activated.
activationKeyslotName string // On successful activation, the name of the keyslot used.
primaryKeyID keyring.KeyID // If added to the keyring, the ID of the primary key.
keyslotRecords map[string]*keyslotAttemptRecord // Keyslot specific status, keyed by keyslot name.
keyslotErrCount int // The number of keyslot errors.
}
func newActivateOneContainerStateMachine(container StorageContainer, cfg ActivateConfigGetter, primaryKey PrimaryKey, flags activateOneContainerStateMachineFlags) *activateOneContainerStateMachine {
// Check whether we have a custom stderr using WithStderrLogger
stderr, exists := ActivateConfigGet[io.Writer](cfg, stderrLoggerKey)
if !exists {
stderr = osStderr
}
m := &activateOneContainerStateMachine{
container: container,
cfg: cfg,
primaryKey: primaryKey,
flags: flags,
stderr: stderr,
keyslotRecords: make(map[string]*keyslotAttemptRecord),
}
m.next = activateOneContainerStateMachineTask{
name: "init-external-unlock-key-attempts",
fn: m.initExternalUnlockKeyAttempts,
}
return m
}
func (m *activateOneContainerStateMachine) setKeyslotError(rec *keyslotAttemptRecord, err error) {
userAuthUnavailable := isUserAuthUnavailableError(err)
if userAuthUnavailable {
// Record that user auth is unavailable separately, because
// UserAuthUnavailableError doesn't override an existing
// ErrInvalid{PIN,Passphrase} error.
rec.userAuthUnavailable = true
}
// UserAuthUnavailableError shouldn't overwrite an existing
// ErrInvalid{PIN,Passphrase} error, as we only want to surface
// this error if no authentication was attempted. We know that if
// rec.err is not nil then it is either ErrInvalidPassphrase or
// ErrInvalidPIN because those are the only errors that don't
// invalidate the keyslot.
if !userAuthUnavailable || rec.err == nil {
rec.err = err
rec.errNumber = m.keyslotErrCount
m.keyslotErrCount += 1
}
if errors.Is(err, errInvalidRecoveryKey) || errors.Is(err, ErrInvalidPassphrase) || errors.Is(err, ErrInvalidPIN) {
return
}
fmt.Fprintf(m.stderr, "Error with keyslot %q: %v\n", rec.slot.Name(), err)
}
func (m *activateOneContainerStateMachine) checkPrimaryKeyValid(platformFlags PlatformKeyDataHandlerFlags, primaryKey PrimaryKey) bool {
if m.flags&activateCrossCheckPrimaryKey == 0 {
// Checking the primary key was not requested for the current context.
return true
}
if platformFlags&PlatformProtectedByStorageContainer > 0 {
// Checking the primary key is not required because the key was recovered
// from a previously unlocked storage container.
return true
}
return subtle.ConstantTimeCompare(primaryKey, m.primaryKey) == 1
}
func (m *activateOneContainerStateMachine) addKeyslotRecord(name string, rec *keyslotAttemptRecord) error {
if _, exists := m.keyslotRecords[name]; exists {
return fmt.Errorf("duplicate keyslots with the name %q", name)
}
m.keyslotRecords[name] = rec
if rec.err == nil && rec.slot.Type() == KeyslotTypePlatform {
switch {
case rec.data == nil && rec.externalUnlockKey == nil:
return errors.New("no key metadata or external unlock key")
case rec.data != nil && rec.externalUnlockKey != nil:
return errors.New("key metadata and external unlock key supplied")
case rec.data != nil:
_, flags, err := RegisteredPlatformKeyDataHandler(rec.data.PlatformName())
if err != nil {
// No handler registered for this platform.
rec.err = ErrNoPlatformHandlerRegistered
}
rec.flags = flags
default:
// This is an external unlock key.
if rec.externalUnlockKey.src == ExternalUnlockKeyFromStorageContainer {
rec.flags |= PlatformProtectedByStorageContainer
}
}
}
if rec.err != nil {
rec.errNumber = m.keyslotErrCount
m.keyslotErrCount += 1
fmt.Fprintf(m.stderr, "Error with keyslot %q: %v\n", rec.slot.Name(), rec.err)
}
return nil
}
// collect externally provided unlock keys supplied via WithExternalUnlockKey.
func (m *activateOneContainerStateMachine) initExternalUnlockKeyAttempts(ctx context.Context) error {
m.next = activateOneContainerStateMachineTask{
name: "init-external-key-attempts",
fn: m.initExternalKeyAttempts,
}
// Find external unlock keys supplied via WithExternalUnlockKey.
external, exists := ActivateConfigGet[[]*externalUnlockKey](m.cfg, externalUnlockKeyKey)
if !exists {
return nil
}
for _, data := range external {
slot := newExternalKeyslot(data.name, nil)
rec := &keyslotAttemptRecord{
slot: slot,
externalUnlockKey: data,
}
if err := m.addKeyslotRecord(slot.Name(), rec); err != nil {
return fmt.Errorf("cannot add external unlock key data %q: %w", slot.Name(), err)
}
}
return nil
}
// collect externally provided KeyData supplied via WithExternalKeyData.
func (m *activateOneContainerStateMachine) initExternalKeyAttempts(ctx context.Context) error {
m.next = activateOneContainerStateMachineTask{
name: "init-keyslots-attemps",
fn: m.initKeyslotAttempts,
}
// Find external keys supplied via WithExternalKeyData and WithExternalKeyDataFromReader.
external, exists := ActivateConfigGet[[]*externalKeyData](m.cfg, externalKeyDataKey)
if !exists {
// Option not supplied.
return nil
}
for _, data := range external {
slot := newExternalKeyslot(data.name, data.r)
kd := data.data
if kd == nil {
var err error
kd, err = ReadKeyData(slot.Data())
if err != nil {
rec := &keyslotAttemptRecord{
slot: slot,
err: &InvalidKeyDataError{err: err},
}
if err := m.addKeyslotRecord(slot.Name(), rec); err != nil {
return fmt.Errorf("cannot add external key metadata %q: %w", slot.Name(), err)
}
continue
}
}
rec := &keyslotAttemptRecord{
slot: slot,
data: kd,
}
if err := m.addKeyslotRecord(slot.Name(), rec); err != nil {
return fmt.Errorf("cannot add external key metadata %q: %w", slot.Name(), err)
}
}
return nil
}
func (m *activateOneContainerStateMachine) initKeyslotAttempts(ctx context.Context) error {
m.next = activateOneContainerStateMachineTask{
name: "try-no-user-auth-keyslots",
fn: m.tryNoUserAuthKeyslots,
}
r, err := m.container.OpenRead(ctx)
if err != nil {
return fmt.Errorf("cannot open storage container for reading: %w", err)
}
defer r.Close()
names, err := r.ListKeyslotNames(ctx)
if err != nil {
return fmt.Errorf("cannot list keyslot names from StorageContainer: %w", err)
}
for _, name := range names {
slot, err := r.ReadKeyslot(ctx, name)
if err != nil {
rec := &keyslotAttemptRecord{
slot: &errorKeyslot{
slotType: KeyslotTypeUnknown,
name: name,
},
err: &InvalidKeyDataError{fmt.Errorf("cannot read keyslot: %w", err)},
}
if err := m.addKeyslotRecord(name, rec); err != nil {
return fmt.Errorf("cannot add keyslot metadata %q: %w", name, err)
}
continue
}
rec := &keyslotAttemptRecord{
slot: slot,
}
switch slot.Type() {
case KeyslotTypePlatform:
kd, err := ReadKeyData(slot.Data())
if err != nil {
rec.err = &InvalidKeyDataError{fmt.Errorf("cannot decode keyslot metadata: %w", err)}
}
rec.data = kd
case KeyslotTypeRecovery:
// Nothing to do here
default:
rec.err = &InvalidKeyDataError{fmt.Errorf("invalid type %q for keyslot metadata", slot.Type())}
}
if err := m.addKeyslotRecord(name, rec); err != nil {
return fmt.Errorf("cannot add keyslot metadata %q: %w", name, err)
}
}
return nil
}
func (m *activateOneContainerStateMachine) tryNoUserAuthKeyslots(ctx context.Context) error {
var records keyslotAttemptRecordPrioritySlice
for _, record := range m.keyslotRecords {
if !record.usable(m.flags) {
// Skipping this unusable keyslot.
continue
}
if record.slot.Type() != KeyslotTypePlatform {
continue
}
if record.data != nil && record.data.AuthMode() != AuthModeNone {
// This one requires user auth, so skip this one.
continue
}
records = append(records, record)
}
// This sorts by keyslot priority.
sort.Sort(records)
for _, record := range records {
var (
primaryKey PrimaryKey
unlockKey DiskUnlockKey
)
if record.data != nil {
var err error
unlockKey, primaryKey, err = record.data.RecoverKeys()
if err != nil {
m.setKeyslotError(record, fmt.Errorf("cannot recover keys from keyslot: %w", err))
continue
}
} else {
unlockKey = record.externalUnlockKey.key
}
if record.data != nil && record.data.Generation() < 2 {
model := internal_bootscope.GetModel()
if model == nil {
m.setKeyslotError(record, errors.New("encountered generation 1 key but bootscope.SetModel has not been called"))
continue
}
authorized, err := record.data.IsSnapModelAuthorized(primaryKey, model)
if err != nil {
m.setKeyslotError(record, &InvalidKeyDataError{fmt.Errorf("cannot check if snap model is authorized: %w", err)})
continue
}
if !authorized {
m.setKeyslotError(record, &IncompatibleKeyDataRoleParamsError{errors.New("snap model is not authorized")})
continue
}
}
if err := m.tryUnlockWithPlatformKeyHelper(ctx, record, primaryKey, unlockKey); err != nil {
m.setKeyslotError(record, err)
continue
}
m.next = activateOneContainerStateMachineTask{
name: "add-keyring-keys",
fn: func(ctx context.Context) error {
return m.addKeyringKeys(ctx, unlockKey, primaryKey)
},
}
return nil
}
// We didn't unlock with any keyslots that require no user authentication,
// so try those that require PINs or passphrases, and recovery keys next.
m.next = activateOneContainerStateMachineTask{
name: "try-with-user-auth-keyslots",
fn: m.tryWithUserAuthKeyslots,
}
return nil
}
func (m *activateOneContainerStateMachine) tryWithUserAuthKeyslots(ctx context.Context) error {
// The caller must use WithAuthRequestor for this to work.
authRequestor, exists := ActivateConfigGet[AuthRequestor](m.cfg, authRequestorKey)
if !exists {
// The caller didn't use WithAuthRequestor, so we're done now.
fmt.Fprintln(m.stderr, "Cannot try keyslots that require a user credential because WithAuthRequestor wasn't supplied")
return ErrCannotActivate
}
var (
// Keep separate slices for different authentication types.
passphraseSlotRecords keyslotAttemptRecordPrioritySlice
pinSlotRecords keyslotAttemptRecordPrioritySlice
recoverySlotRecords keyslotAttemptRecordPrioritySlice
)
// Gather keyslots
for _, record := range m.keyslotRecords {
if !record.usable(m.flags) {
// Skipping this unusable keyslot.
continue
}
switch record.slot.Type() {
case KeyslotTypeRecovery:
// We've found a recovery keyslot.
recoverySlotRecords = append(recoverySlotRecords, record)
case KeyslotTypePlatform:
// We've found a platform keyslot. Don't support platform
// keyslots older than v2.
if record.data.Generation() >= 2 {
switch record.data.AuthMode() {
case AuthModeNone:
// Skip as we've already tried these.
case AuthModePassphrase:
passphraseSlotRecords = append(passphraseSlotRecords, record)
case AuthModePIN:
pinSlotRecords = append(pinSlotRecords, record)
default:
m.setKeyslotError(record, &InvalidKeyDataError{fmt.Errorf("unknown user auth mode for keyslot: %s", record.data.AuthMode())})
}
}
}
}
// Sort everything by priority.
sort.Sort(passphraseSlotRecords)
sort.Sort(pinSlotRecords)
sort.Sort(recoverySlotRecords)
// Get the value of WithAuthRequestorUserVisibleName, if used.
name, _ := ActivateConfigGet[string](m.cfg, authRequestorUserVisibleNameKey)
// Get the permitted number of tries for each authentication type.
passphraseTries, _ := ActivateConfigGet[uint](m.cfg, passphraseTriesKey)
pinTries, _ := ActivateConfigGet[uint](m.cfg, pinTriesKey)
recoveryKeyTries, _ := ActivateConfigGet[uint](m.cfg, recoveryKeyTriesKey)
// TODO: Obtain values for PIN, passphrase ratelimiting from options when this
// is implemented. Rate limiting is tricky because it relies on us temporarily
// removing flags from the authType passed to AuthRequestor.RequestUserCredential,
// and then adding them back when the rate limiting expires. Right now, any change
// to authType requires us to restart systemd-ask-password (assuming the systemd
// implementation of AuthRequestor is used), which won't be a great experience if,
// eg, the user is in the middle of entering a recovery key having just entered an
// incorred PIN, when PIN becomes available again, requiring us to add back
// UserAuthTypePIN to authType and restarting systemd-ask-password in order to
// update the prompt - the result will be that the update to the prompt will cause
// the user to lose what they've entered so far.
//
// Maybe rate limiting will require us to replace systemd-ask-password with
// something that fits this user experience better in the future.
// Determine the available authentication types.
var authType UserAuthType
if len(passphraseSlotRecords) > 0 {
authType |= UserAuthTypePassphrase
}
if len(pinSlotRecords) > 0 {
authType |= UserAuthTypePIN
}
if len(recoverySlotRecords) > 0 {
authType |= UserAuthTypeRecoveryKey
}
updateAvailableAuthType := func() {
if passphraseTries == 0 {
// No more passphrase key tries are left.
authType &^= UserAuthTypePassphrase
}
if pinTries == 0 {
// No more PIN key tries are left.
authType &^= UserAuthTypePIN
}
if recoveryKeyTries == 0 {
// No more recovery key tries are left.
authType &^= UserAuthTypeRecoveryKey
}
}
updateAvailableAuthType()
var (
triedAuthType UserAuthType // The types of credentials that were tried.
invalidAuthType UserAuthType // The types of credentials that weren't tried because the format is invalid.
successfulAuthType UserAuthType // The type of credential that was used successfully.
)
for authType != UserAuthType(0) {
cred, credAuthType, err := authRequestor.RequestUserCredential(ctx, name, m.container.Path(), authType)
if err != nil {
return fmt.Errorf("cannot request user credential: %w", err)
}
credAuthType &= authType
// We have a user credential.
// 1) If it's a passphrase, try it against every keyslot with a passphrase.
// 2) If it's a PIN, try it against every keyslot with a passphrase.
// 3) If it's a recovery key, try it against every recovery keyslot.
var (
unlockKey DiskUnlockKey
primaryKey PrimaryKey
)
if credAuthType&UserAuthTypePassphrase > 0 {
triedAuthType |= UserAuthTypePassphrase
passphraseTries -= 1
if uk, pk, success := m.tryPassphraseKeyslotsHelper(ctx, passphraseSlotRecords, cred); success {
successfulAuthType = UserAuthTypePassphrase
unlockKey = uk
primaryKey = pk
}
}
if m.status == activationIncomplete && credAuthType&UserAuthTypePIN > 0 {
pin, err := ParsePIN(cred)
switch {
case err != nil:
// The user supplied credential isn't a valid PIN.
invalidAuthType |= UserAuthTypePIN
default:
// This is a valid PIN
triedAuthType |= UserAuthTypePIN
pinTries -= 1
if uk, pk, success := m.tryPINKeyslotsHelper(ctx, pinSlotRecords, pin); success {
successfulAuthType = UserAuthTypePIN
unlockKey = uk
primaryKey = pk
}
}
}
if m.status == activationIncomplete && credAuthType&UserAuthTypeRecoveryKey > 0 {
recoveryKey, err := ParseRecoveryKey(cred)
switch {
case err != nil:
// The user supplied credential isn't a valid recovery key.
invalidAuthType |= UserAuthTypeRecoveryKey
default:
// This is a valid recovery key
triedAuthType |= UserAuthTypeRecoveryKey
recoveryKeyTries -= 1
if m.tryRecoveryKeyslotsHelper(ctx, recoverySlotRecords, recoveryKey) {
successfulAuthType = UserAuthTypeRecoveryKey
unlockKey = DiskUnlockKey(recoveryKey[:])
}
}
}
if m.status == activationIncomplete {
// We haven't unlocked yet, so try again.
// Firstly, don't retry a method where there are no more usable keyslots.
if !passphraseSlotRecords.hasUsable(m.flags) {
passphraseTries = 0
}
if !pinSlotRecords.hasUsable(m.flags) {
pinTries = 0
}
// Update authType flags
prevAuthType := authType
updateAvailableAuthType()
exhaustedAuthType := prevAuthType &^ authType
// Notify the UI
result := UserAuthResultFailed
failedAuthType := triedAuthType
if triedAuthType == UserAuthType(0) && invalidAuthType != UserAuthType(0) {
result = UserAuthResultInvalidFormat
failedAuthType = invalidAuthType
}
if err := authRequestor.NotifyUserAuthResult(ctx, result, failedAuthType, exhaustedAuthType); err != nil {
fmt.Fprintf(m.stderr, "Cannot notify user of auth failure: %v\n", err)
}
continue
}
// We have unlocked successfully.
if err := authRequestor.NotifyUserAuthResult(ctx, UserAuthResultSuccess, successfulAuthType, 0); err != nil {
fmt.Fprintf(m.stderr, "Cannot notify user of auth success: %v\n", err)
}
m.next = activateOneContainerStateMachineTask{
name: "add-keyring-keys",
fn: func(ctx context.Context) error {
return m.addKeyringKeys(ctx, unlockKey, primaryKey)
},
}
return nil
}
// We have failed to unlock
m.next = activateOneContainerStateMachineTask{}
return ErrCannotActivate
}
func (m *activateOneContainerStateMachine) tryPassphraseKeyslotsHelper(ctx context.Context, slotRecords keyslotAttemptRecordPrioritySlice, passphrase string) (unlockKey DiskUnlockKey, primaryKey PrimaryKey, success bool) {
for _, record := range slotRecords {
if !record.usable(m.flags) {
// A previous error might have marked this as unusable.
continue
}
unlockKey, primaryKey, err := record.data.RecoverKeysWithPassphrase(passphrase)
if err != nil {
m.setKeyslotError(record, fmt.Errorf("cannot recover keys from keyslot: %w", err))
continue
}
// Clear any previous ErrInvalidPassphrase error.
record.err = nil
if err := m.tryUnlockWithPlatformKeyHelper(ctx, record, primaryKey, unlockKey); err != nil {
m.setKeyslotError(record, err)
continue
}
// Unlocking succeeded with this keyslot
return unlockKey, primaryKey, true
}
// We were unable to unlock with any passphrase keyslot
return nil, nil, false
}
func (m *activateOneContainerStateMachine) tryPINKeyslotsHelper(ctx context.Context, slotRecords keyslotAttemptRecordPrioritySlice, pin PIN) (unlockKey DiskUnlockKey, primaryKey PrimaryKey, success bool) {
for _, record := range slotRecords {
if !record.usable(m.flags) {
// A previous error might have marked this as unusable.
continue
}
unlockKey, primaryKey, err := record.data.RecoverKeysWithPIN(pin)
if err != nil {
m.setKeyslotError(record, fmt.Errorf("cannot recover keys from keyslot: %w", err))
continue
}
// Clear any previous ErrInvalidPIN error.
record.err = nil
if err := m.tryUnlockWithPlatformKeyHelper(ctx, record, primaryKey, unlockKey); err != nil {
m.setKeyslotError(record, err)
continue
}
// Unlocking succeeded with this keyslot
return unlockKey, primaryKey, true
}
// We were unable to unlock with any PIN keyslot
return nil, nil, false
}
func (m *activateOneContainerStateMachine) tryUnlockWithPlatformKeyHelper(ctx context.Context, rec *keyslotAttemptRecord, primaryKey PrimaryKey, unlockKey DiskUnlockKey) error {
// We can only check the primary key for keys that were recovered from
// KeyData, so it's skipped for external unlock keys. We've already checked
// that the external unlock key is safe to use in this context.
if rec.data != nil && !m.checkPrimaryKeyValid(rec.flags, primaryKey) {
return errInvalidPrimaryKey
}
if err := m.container.Activate(ctx, rec.slot, unlockKey, m.cfg); err != nil {
// XXX: This could fail for any number of reasons, such as invalid supplied parameters,
// but the current API doesn't have a way of communicating this and in the luks2
// backend, systemd-cryptsetup only gives us an exit code of 1 regardless of whether
// the key is wrong or an already active volume name is supplied, so we just assume
// invalid data for now. I'd really like to do better than this though and distinguish
// between the key being wrong or the caller providing incorrect options. Given how
// little of systemd-cryptsetup's functionality we use, perhaps in the future we could
// replace it by a simple C application that makes use of libcryptsetup and returns
// useful information back to us via a combination of JSON output on stdout and / or
// exit codes.
return &InvalidKeyDataError{fmt.Errorf("cannot activate container with key recovered from keyslot metadata: %w", err)}
}
// We have unlocked successfully.
m.status = ActivationSucceededWithPlatformKey
m.activationKeyslotName = rec.slot.Name()
return nil
}
func (m *activateOneContainerStateMachine) tryRecoveryKeyslotsHelper(ctx context.Context, slotRecords keyslotAttemptRecordPrioritySlice, recoveryKey RecoveryKey) (success bool) {
for _, record := range slotRecords {
// XXX: Not sure what to do with errors from Activate yet. The most common error
// will be because the recovery key is wrong, but we have no way to know. The API
// doesn't provide a way of communicating this type of information, and
// systemd-cryptsetup which is used by the luks2 backend returns exit code 1 for all
// errors. Because of this, it's not appropriate to mark the keyslot with an error
// that makes it unusable, and it's probably not appropriate to log to stderr either.
//
// As mentioned in another comment, perhaps rather than writing error messages to stderr
// and having an option to customize where stderr messages go, it might be better for us
// to provide an option that allows us to integrate with the callers logging framework,
// which can allow us to write messages at different log levels.
//
// Note that we don't check if the keyslot is usable here (calling record.usable()), as
// we did that when building the list of recovery keys and nothing we do to the list
// of recovery keys will make them unusable.
if err := m.container.Activate(ctx, record.slot, recoveryKey[:], m.cfg); err == nil {
// Unlocking succeeded with this keyslot.
record.err = nil
m.status = ActivationSucceededWithRecoveryKey
m.activationKeyslotName = record.slot.Name()
return true
}
// The most likely failure here is an invalid key, so set the error for this keyslot
// as such so that it will be communicated via ActivateState in the future.
m.setKeyslotError(record, errInvalidRecoveryKey)
}
// We were unable to unlock with any recovery keyslot.
return false
}
func (m *activateOneContainerStateMachine) addKeyringKeys(ctx context.Context, unlockKey DiskUnlockKey, primaryKey PrimaryKey) error {
m.next = activateOneContainerStateMachineTask{}
// Get the value supplied by WithKeyringDescriptionPrefix.
prefix, _ := ActivateConfigGet[string](m.cfg, keyringDescPrefixKey)
prefix = keyringPrefixOrDefault(prefix)
// We don't return an error if either of these fail because we don't
// want failure to add keys to the keyring to mark activation of the
// storage container as failed.
if _, err := addKeyToUserKeyring(unlockKey, m.container, KeyringKeyPurposeUnlock, prefix); err != nil {
fmt.Fprintln(m.stderr, "Cannot add unlock key to user keyring:", err)
}
if len(primaryKey) > 0 {
id, err := addKeyToUserKeyring(primaryKey, m.container, KeyringKeyPurposePrimary, prefix)
switch {
case err != nil:
fmt.Fprintln(m.stderr, fmt.Sprintf("Cannot add primary key to user keyring: %v", err))
case len(m.primaryKey) == 0:
// This is the first primary key from a keyslot that
// was used to successfully unlock a storage container,
// so retain it in order for it to be used for cross-checking
// with other containers.
m.primaryKeyID = id
m.primaryKey = primaryKey
}
}
legacyDescPaths, exists := ActivateConfigGet[[]string](m.cfg, legacyKeyringKeyDescPathsKey)
if !exists {
return nil
}
containerPath := m.container.Path()
var containerSt unix.Stat_t
err := unixStat(containerPath, &containerSt)
switch {
case errors.Is(err, os.ErrNotExist):
fmt.Fprintln(m.stderr, "Ignoring WithLegacyKeyringDescriptionPaths because the container path does not refer to a filesystem object")
return nil
case err != nil:
fmt.Fprintln(m.stderr, "Cannot use WithLegacyKeyringDescriptionPaths:", &os.PathError{Op: "stat", Path: containerPath, Err: err})
return nil
case containerSt.Mode&unix.S_IFMT != unix.S_IFBLK:
fmt.Fprintln(m.stderr, "Ignoring WithLegacyKeyringDescriptionPaths because the container is not a block device")
return nil
}
addLegacyKey := func(path string) {
var st unix.Stat_t
err := unixStat(path, &st)
switch {
case errors.Is(err, os.ErrNotExist):
fmt.Fprintf(m.stderr, "Ignoring WithLegacyKeyringDescriptionPaths path %q which does not exist\n", path)
return
case err != nil:
fmt.Fprintf(m.stderr, "Cannot use WithLegacyKeyringDescriptionPaths path %q: %v\n", path, &os.PathError{Op: "stat", Path: path, Err: err})
return
case st.Mode&unix.S_IFMT != unix.S_IFBLK:
fmt.Fprintf(m.stderr, "Ignoring WithLegacyKeyringDescriptionPaths path %q because it is not a block device\n", path)
return
case st.Rdev != containerSt.Rdev:
fmt.Fprintf(m.stderr, "Ignoring WithLegacyKeyringDescriptionPaths path %q because it does not refer to the container block device\n", path)
return
}
if err := addKeyToUserKeyringLegacy(unlockKey, path, KeyringKeyPurposeUnlock, prefix); err != nil {
fmt.Fprintf(m.stderr, "Cannot add unlock key to user keyring with legacy path description %q: %v\n", path, err)
}
if len(primaryKey) > 0 {
if err := addKeyToUserKeyringLegacy(primaryKey, path, keyringKeyPurposeAuxiliary, prefix); err != nil {
fmt.Fprintf(m.stderr, "Cannot add primary key to user keyring with legacy path description %q: %v\n", path, err)
}
}
}
for _, path := range legacyDescPaths {
addLegacyKey(path)
}
return nil
}
func (m *activateOneContainerStateMachine) primaryKeyInfo() (PrimaryKey, keyring.KeyID, error) {
if m.hasMoreWork() {
return nil, 0, errors.New("state machine has not finished")
}
if m.status != ActivationSucceededWithPlatformKey {
return nil, 0, errNoPrimaryKey
}
data := m.keyslotRecords[m.activationKeyslotName].data
switch {
case data == nil:
// The activation keyslot is an external unlock key, which doesn't
// have a primary key.
return nil, 0, errNoPrimaryKey
case data.Generation() < 2:
// The activation keyslot does not have a primary key that can be
// used to demonstrate binding of other storage containers.
return nil, 0, errNoPrimaryKey
}
return m.primaryKey, m.primaryKeyID, nil
}
func (m *activateOneContainerStateMachine) activationState() (*ContainerActivateState, error) {
if m.hasMoreWork() {
return nil, errors.New("state machine has not finished")
}