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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
//go:build !nosecboot
/*
* 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 (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"io"
sb "github.com/snapcore/secboot"
sb_scope "github.com/snapcore/secboot/bootscope"
sb_hooks "github.com/snapcore/secboot/hooks"
"github.com/snapcore/snapd/kernel/fde"
"github.com/snapcore/snapd/kernel/fde/optee"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
)
var fdeHasRevealKey = fde.HasRevealKey
var sbSetModel = sb_scope.SetModel
var sbSetBootMode = sb_scope.SetBootMode
var sbSetKeyRevealer = sb_hooks.SetKeyRevealer
// taggedHandle wraps a raw handle from a secboot hook and adds a method field.
// This field is used to route the handle to the correct [sb_hooks.KeyRevealer].
// Note that this is currently only used for OPTEE at the moment to preserve
// backwards compatibility.
type taggedHandle struct {
Method string `json:"method"`
Handle json.RawMessage `json:"handle"`
}
func init() {
v2Handler := &fdeHookV2DataHandler{}
flags := sb.PlatformKeyDataHandlerFlags(0)
sb.RegisterPlatformKeyDataHandler(platformFdeHookV2, v2Handler, flags)
}
type hookKeyProtector struct {
runHook fde.RunSetupHookFunc
keyName string
}
func (h *hookKeyProtector) ProtectKey(rand io.Reader, cleartext, aad []byte) (ciphertext []byte, handle []byte, err error) {
keyParams := &fde.InitialSetupParams{
Key: cleartext,
KeyName: h.keyName,
}
res, err := fde.InitialSetup(h.runHook, keyParams)
if err != nil {
return nil, nil, err
}
if res.Handle == nil {
return res.EncryptedKey, nil, nil
} else {
return res.EncryptedKey, *res.Handle, nil
}
}
// KeyProtector is an abstraction for an externally supplied key setup hook.
type KeyProtector sb_hooks.KeyProtector
// KeyProtectorFactory enables creating a [KeyProtector] implementation.
type KeyProtectorFactory interface {
// ForKeyName returns a new [KeyProtector].
ForKeyName(name string) KeyProtector
}
var ErrNoKeyProtector = errors.New("cannot find supported FDE key protector")
type fdeKeyProtectorFactory struct {
runHook fde.RunSetupHookFunc
}
func (f *fdeKeyProtectorFactory) ForKeyName(name string) KeyProtector {
return &hookKeyProtector{
runHook: f.runHook,
keyName: name,
}
}
// FDESetupHookKeyProtectorFactory returns a [KeyProtectorFactory] that will use
// the kernel's fde-setup hook to protect the key, invoked via the given
// runHook.
func FDESetupHookKeyProtectorFactory(runHook fde.RunSetupHookFunc) KeyProtectorFactory {
return &fdeKeyProtectorFactory{runHook: runHook}
}
// OPTEEKeyProtectorFactory returns a [KeyProtectorFactory] that will use
// the system's OPTEE trusted application to protect the key.
func OPTEEKeyProtectorFactory() KeyProtectorFactory {
return &opteeKeyProtectorFactory{}
}
type opteeKeyProtectorFactory struct{}
func (o *opteeKeyProtectorFactory) ForKeyName(name string) KeyProtector {
return &opteeKeyProtector{}
}
type opteeKeyProtector struct{}
func (o *opteeKeyProtector) ProtectKey(rand io.Reader, cleartext, aad []byte) (ciphertext []byte, handle []byte, err error) {
client := optee.NewFDETAClient()
rawHandle, sealed, err := client.EncryptKey(cleartext)
if err != nil {
return nil, nil, err
}
parsed, err := json.Marshal(rawHandle)
if err != nil {
return nil, nil, err
}
tagged := taggedHandle{
Method: "optee",
Handle: parsed,
}
handleJSON, err := json.Marshal(tagged)
if err != nil {
return nil, nil, err
}
return sealed, handleJSON, nil
}
func SealKeysWithProtector(kpf KeyProtectorFactory, keys []SealKeyRequest, params *SealKeysWithFDESetupHookParams) error {
var primaryKey sb.PrimaryKey
if params.PrimaryKey != nil {
// TODO:FDEM:FIX: add unit test taking that primary key
primaryKey = params.PrimaryKey
}
// if we have any keys, then we'll be replacing the singleton key protector
// in sb_hooks. make sure we reset it before leaving this function.
if len(keys) > 0 {
defer sb_hooks.SetKeyProtector(nil, 0)
}
for _, skr := range keys {
protector := kpf.ForKeyName(skr.KeyName)
// TODO:FDEM: add support for AEAD (consider OP-TEE work)
flags := sb_hooks.KeyProtectorNoAEAD
sb_hooks.SetKeyProtector(protector, flags)
protectedKey, primaryKeyOut, unlockKey, err := sb_hooks.NewProtectedKey(rand.Reader, &sb_hooks.KeyParams{
PrimaryKey: primaryKey,
Role: skr.KeyName,
AuthorizedSnapModels: []sb.SnapModel{
params.Model,
},
AuthorizedBootModes: skr.BootModes,
})
if err != nil {
return err
}
if primaryKey == nil {
primaryKey = primaryKeyOut
}
if err := skr.BootstrappedContainer.AddKey(skr.SlotName, unlockKey); err != nil {
return err
}
keyWriter, err := skr.getWriter()
if err != nil {
return err
}
if err := protectedKey.WriteAtomic(keyWriter); err != nil {
return err
}
if skr.SlotName == "default" {
// "default" key will only be using hook on data disk. "save" disk will be handled
// with the protector key.
skr.BootstrappedContainer.RegisterKeyAsUsed(primaryKeyOut, unlockKey)
}
}
if primaryKey != nil && params.AuxKeyFile != "" {
if err := osutil.AtomicWriteFile(params.AuxKeyFile, primaryKey, 0600, 0); err != nil {
return fmt.Errorf("cannot write the policy auth key file: %v", err)
}
}
return nil
}
func setAuthorizedSnapModelsOnHooksKeydataImpl(kd *sb_hooks.KeyData, rand io.Reader, key sb.PrimaryKey, models ...sb.SnapModel) error {
return kd.SetAuthorizedSnapModels(rand, key, models...)
}
var setAuthorizedSnapModelsOnHooksKeydata = setAuthorizedSnapModelsOnHooksKeydataImpl
func setAuthorizedBootModesOnHooksKeydataImpl(kd *sb_hooks.KeyData, rand io.Reader, key sb.PrimaryKey, bootmodes ...string) error {
return kd.SetAuthorizedBootModes(rand, key, bootmodes...)
}
var setAuthorizedBootModesOnHooksKeydata = setAuthorizedBootModesOnHooksKeydataImpl
// resealKeysWithFDESetupHookImpl updates hook based keydatas for given
// files with a specific list of models
func resealKeysWithFDESetupHookImpl(keys []KeyDataLocation, primaryKeyDevices []string, fallbackPrimaryKeyFiles []string, models []ModelForSealing, bootModes []string) error {
var sbModels []sb.SnapModel
for _, model := range models {
sbModels = append(sbModels, model)
}
var primaryKey []byte
for _, key := range keys {
var keyDataWriter sb.KeyDataWriter
keyData, tokenWriter, tokenErr := key.readTokenAndGetWriter()
if tokenErr == nil {
keyDataWriter = tokenWriter
} else {
loadedKey := &defaultKeyLoader{}
const hintExpectFDEHook = true
if err := readKeyFile(key.KeyFile, loadedKey, hintExpectFDEHook); err != nil {
return tokenErr
}
// Non-nil FDEHookKeyV1 indicates that V1 hook key is used
if loadedKey.FDEHookKeyV1 != nil {
// V1 keys do not need resealing
continue
}
if loadedKey.KeyData == nil {
return fmt.Errorf("internal error: keydata was expected, but none found")
}
keyData = loadedKey.KeyData
keyDataWriter = sb.NewFileKeyDataWriter(key.KeyFile)
}
if primaryKey == nil {
pk, err := GetPrimaryKey(primaryKeyDevices, fallbackPrimaryKeyFiles)
if err != nil {
return err
}
primaryKey = pk
}
if keyData.Generation() == 1 {
if err := keyData.SetAuthorizedSnapModels(primaryKey, sbModels...); err != nil {
return err
}
} else {
hooksKeyData, err := sb_hooks.NewKeyData(keyData)
if err != nil {
return err
}
if err := setAuthorizedSnapModelsOnHooksKeydata(hooksKeyData, rand.Reader, primaryKey, sbModels...); err != nil {
return err
}
if err := setAuthorizedBootModesOnHooksKeydata(hooksKeyData, rand.Reader, primaryKey, bootModes...); err != nil {
return err
}
}
if err := keyData.WriteAtomic(keyDataWriter); err != nil {
return err
}
}
return nil
}
var resealKeysWithFDESetupHook = resealKeysWithFDESetupHookImpl
func unlockDiskWithHookV1Key(mapperName, sourceDevice string, sealed []byte) error {
p := fde.RevealParams{
SealedKey: sealed,
}
options := sb.ActivateVolumeOptions{}
unlockKey, err := fde.Reveal(&p)
if err != nil {
return err
}
return sbActivateVolumeWithKey(mapperName, sourceDevice, unlockKey, &options)
}
type fdeHookV2DataHandler struct{}
func (fh *fdeHookV2DataHandler) RecoverKeys(data *sb.PlatformKeyData, encryptedPayload []byte) ([]byte, error) {
var handle *json.RawMessage
if len(data.EncodedHandle) != 0 {
rawHandle := json.RawMessage(data.EncodedHandle)
handle = &rawHandle
}
p := fde.RevealParams{
SealedKey: encryptedPayload,
Handle: handle,
V2Payload: true,
}
return fde.Reveal(&p)
}
func (fh *fdeHookV2DataHandler) ChangeAuthKey(data *sb.PlatformKeyData, old, new []byte, context any) ([]byte, error) {
return nil, fmt.Errorf("cannot change auth key yet")
}
func (fh *fdeHookV2DataHandler) RecoverKeysWithAuthKey(data *sb.PlatformKeyData, encryptedPayload, key []byte) ([]byte, error) {
return nil, fmt.Errorf("cannot recover keys with auth keys yet")
}
type keyRevealerV3 struct{}
func (kr *keyRevealerV3) RevealKey(data, ciphertext, aad []byte) (plaintext []byte, err error) {
logger.Noticef("Called reveal key")
// try to parse as new tagged format first. if that fails, assume this is
// the older handle format that isn't inside of a JSON object.
//
// NOTE: if the handle happens to be a JSON object, it must have the
// "method" field set for us to consider the method. otherwise, the handle
// is not unwrapped and we pass along the full JSON blob.
var tagged taggedHandle
if len(data) == 0 || json.Unmarshal(data, &tagged) != nil || tagged.Method == "" {
logger.Debug("cannot parse handle as JSON object, using fde-setup hook revealer")
return revealWithHooks(data, ciphertext)
}
switch tagged.Method {
case "optee":
return revealWithOPTEE(tagged.Handle, ciphertext)
default:
return nil, fmt.Errorf("unknown key revealer method: %s", tagged.Method)
}
}
func revealWithOPTEE(handleJSON []byte, ciphertext []byte) ([]byte, error) {
var handle []byte
if err := json.Unmarshal(handleJSON, &handle); err != nil {
return nil, err
}
client := optee.NewFDETAClient()
return client.DecryptKey(ciphertext, handle)
}
func revealWithHooks(handleJSON []byte, ciphertext []byte) ([]byte, error) {
var handle *json.RawMessage
if len(handleJSON) != 0 {
tmp := json.RawMessage(handleJSON)
handle = &tmp
}
p := fde.RevealParams{
SealedKey: ciphertext,
Handle: handle,
V2Payload: true,
}
return fde.Reveal(&p)
}
// FDEOpteeTAPresent returns true if we detect that the expected OPTEE TA that
// enables FDE is present.
func FDEOpteeTAPresent() bool {
return optee.NewFDETAClient().Present()
}
|