forked from canonical/secboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyring.go
102 lines (86 loc) · 3.06 KB
/
keyring.go
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
// -*- 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 (
"errors"
"fmt"
"os"
"syscall"
"github.com/snapcore/secboot/internal/keyring"
"golang.org/x/xerrors"
)
const (
keyringPurposeAuxiliary = "aux"
keyringPurposeDiskUnlock = "unlock"
)
var ErrKernelKeyNotFound = errors.New("cannot find key in kernel keyring")
func keyringPrefixOrDefault(prefix string) string {
if prefix == "" {
return "ubuntu-fde"
}
return prefix
}
// GetDiskUnlockKeyFromKernel retrieves the key that was used to unlock the
// encrypted container at the specified path. The value of prefix must match
// the prefix that was supplied via ActivateVolumeOptions during unlocking.
//
// If remove is true, the key will be removed from the kernel keyring prior
// to returning.
//
// If no key is found, a ErrKernelKeyNotFound error will be returned.
func GetDiskUnlockKeyFromKernel(prefix, devicePath string, remove bool) (DiskUnlockKey, error) {
key, err := keyring.GetKeyFromUserKeyring(devicePath, keyringPurposeDiskUnlock, keyringPrefixOrDefault(prefix))
if err != nil {
var e syscall.Errno
if xerrors.As(err, &e) && e == syscall.ENOKEY {
return nil, ErrKernelKeyNotFound
}
return nil, err
}
if remove {
if err := keyring.RemoveKeyFromUserKeyring(devicePath, keyringPurposeDiskUnlock, keyringPrefixOrDefault(prefix)); err != nil {
fmt.Fprintf(os.Stderr, "secboot: cannot remove key from keyring: %v\n", err)
}
}
return key, nil
}
// GetPrimaryKeyFromKernel retrieves the auxiliary key associated with the
// KeyData that was used to unlock the encrypted container at the specified path.
// The value of prefix must match the prefix that was supplied via
// ActivateVolumeOptions during unlocking.
//
// If remove is true, the key will be removed from the kernel keyring prior
// to returning.
//
// If no key is found, a ErrKernelKeyNotFound error will be returned.
func GetPrimaryKeyFromKernel(prefix, devicePath string, remove bool) (PrimaryKey, error) {
key, err := keyring.GetKeyFromUserKeyring(devicePath, keyringPurposeAuxiliary, keyringPrefixOrDefault(prefix))
if err != nil {
var e syscall.Errno
if xerrors.As(err, &e) && e == syscall.ENOKEY {
return nil, ErrKernelKeyNotFound
}
return nil, err
}
if remove {
if err := keyring.RemoveKeyFromUserKeyring(devicePath, keyringPurposeAuxiliary, keyringPrefixOrDefault(prefix)); err != nil {
fmt.Fprintf(os.Stderr, "secboot: cannot remove key from keyring: %v\n", err)
}
}
return key, nil
}