Debian Codegraph @

actions/callback.go

raw
1 /*
2 * callback.go - defines how the caller of an action function passes along a key
3 * to be used in this package.
4 *
5 * Copyright 2017 Google Inc.
6 * Author: Joe Richey ([email protected])
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 * use this file except in compliance with the License. You may obtain a copy of
10 * the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 * License for the specific language governing permissions and limitations under
18 * the License.
19 */
20
21 package actions
22
23 import (
24 "log"
25
26 "github.com/pkg/errors"
27
28 "github.com/google/fscrypt/crypto"
29 "github.com/google/fscrypt/filesystem"
30 "github.com/google/fscrypt/metadata"
31 )
32
33 // ProtectorInfo is the information a caller will receive about a Protector
34 // before they have to return the corresponding key. This is currently a
35 // read-only view of metadata.ProtectorData.
36 type ProtectorInfo struct {
37 data *metadata.ProtectorData
38 }
39
40 // Descriptor is the Protector's descriptor used to uniquely identify it.
41 func (pi *ProtectorInfo) Descriptor() string { return pi.data.GetProtectorDescriptor() }
42
43 // Source indicates the type of the descriptor (how it should be unlocked).
44 func (pi *ProtectorInfo) Source() metadata.SourceType { return pi.data.GetSource() }
45
46 // Name is used to describe custom passphrase and raw key descriptors.
47 func (pi *ProtectorInfo) Name() string { return pi.data.GetName() }
48
49 // UID is used to identify the user for login passphrases.
50 func (pi *ProtectorInfo) UID() int64 { return pi.data.GetUid() }
51
52 // KeyFunc is passed to a function that will require some type of key.
53 // The info parameter is provided so the callback knows which key to provide.
54 // The retry parameter indicates that a previous key provided by this callback
55 // was incorrect (this allows for user feedback like "incorrect passphrase").
56 //
57 // For passphrase sources, the returned key should be a passphrase. For raw
58 // sources, the returned key should be a 256-bit cryptographic key. Consumers
59 // of the callback will wipe the returned key. An error returned by the callback
60 // will be propagated back to the caller.
61 type KeyFunc func(info ProtectorInfo, retry bool) (*crypto.Key, error)
62
63 // getWrappingKey uses the provided callback to get the wrapping key
64 // corresponding to the ProtectorInfo. This runs the passphrase hash for
65 // passphrase sources or just relays the callback for raw sources.
66 func getWrappingKey(info ProtectorInfo, keyFn KeyFunc, retry bool) (*crypto.Key, error) {
67 // For raw key sources, we can just use the key directly.
68 if info.Source() == metadata.SourceType_raw_key {
69 return keyFn(info, retry)
70 }
71
72 // Run the passphrase hash for other sources.
73 passphrase, err := keyFn(info, retry)
74 if err != nil {
75 return nil, err
76 }
77 defer passphrase.Wipe()
78
79 log.Printf("running passphrase hash for protector %s", info.Descriptor())
80 return crypto.PassphraseHash(passphrase, info.data.Salt, info.data.Costs)
81 }
82
83 // unwrapProtectorKey uses the provided callback and ProtectorInfo to return
84 // the unwrapped protector key. This will repeatedly call keyFn to get the
85 // wrapping key until the correct key is returned by the callback or the
86 // callback returns an error.
87 func unwrapProtectorKey(info ProtectorInfo, keyFn KeyFunc) (*crypto.Key, error) {
88 retry := false
89 for {
90 wrappingKey, err := getWrappingKey(info, keyFn, retry)
91 if err != nil {
92 return nil, err
93 }
94
95 protectorKey, err := crypto.Unwrap(wrappingKey, info.data.WrappedKey)
96 wrappingKey.Wipe()
97
98 switch errors.Cause(err) {
99 case nil:
100 log.Printf("valid wrapping key for protector %s", info.Descriptor())
101 return protectorKey, nil
102 case crypto.ErrBadAuth:
103 // After the first failure, we let the callback know we are retrying.
104 log.Printf("invalid wrapping key for protector %s", info.Descriptor())
105 retry = true
106 continue
107 default:
108 return nil, err
109 }
110 }
111 }
112
113 // ProtectorOption is information about a protector relative to a Policy.
114 type ProtectorOption struct {
115 ProtectorInfo
116 // LinkedMount is the mountpoint for a linked protector. It is nil if
117 // the protector is not a linked protector (or there is a LoadError).
118 LinkedMount *filesystem.Mount
119 // LoadError is non-nil if there was an error in getting the data for
120 // the protector.
121 LoadError error
122 }
123
124 // OptionFunc is passed to a function that needs to unlock a Policy.
125 // The callback is used to specify which protector should be used to unlock a
126 // Policy. The descriptor indicates which Policy we are using, while the options
127 // correspond to the valid Protectors protecting the Policy.
128 //
129 // The OptionFunc should either return a valid index into options, which
130 // corresponds to the desired protector, or an error (which will be propagated
131 // back to the caller).
132 type OptionFunc func(policyDescriptor string, options []*ProtectorOption) (int, error)