Debian Codegraph @

metadata/checks.go

raw
1 /*
2 * checks.go - Some sanity check methods for our metadata structures
3 *
4 * Copyright 2017 Google Inc.
5 * Author: Joe Richey ([email protected])
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations under
17 * the License.
18 */
19
20 package metadata
21
22 import (
23 "log"
24 "math"
25
26 "github.com/pkg/errors"
27 "google.golang.org/protobuf/proto"
28
29 "github.com/google/fscrypt/util"
30 )
31
32 var errNotInitialized = errors.New("not initialized")
33
34 // Metadata is the interface to all of the protobuf structures that can be
35 // checked for validity.
36 type Metadata interface {
37 CheckValidity() error
38 proto.Message
39 }
40
41 // CheckValidity ensures the mode has a name and isn't empty.
42 func (m EncryptionOptions_Mode) CheckValidity() error {
43 if m == EncryptionOptions_default {
44 return errNotInitialized
45 }
46 if m.String() == "" {
47 return errors.Errorf("unknown %d", m)
48 }
49 return nil
50 }
51
52 // CheckValidity ensures the source has a name and isn't empty.
53 func (s SourceType) CheckValidity() error {
54 if s == SourceType_default {
55 return errNotInitialized
56 }
57 if s.String() == "" {
58 return errors.Errorf("unknown %d", s)
59 }
60 return nil
61 }
62
63 // MaxParallelism is the maximum allowed value for HashingCosts.Parallelism.
64 const MaxParallelism = math.MaxUint8
65
66 // CheckValidity ensures the hash costs will be accepted by Argon2.
67 func (h *HashingCosts) CheckValidity() error {
68 if h == nil {
69 return errNotInitialized
70 }
71
72 minP := int64(1)
73 p := uint8(h.Parallelism)
74 if h.Parallelism < minP || h.Parallelism > MaxParallelism {
75 if h.TruncationFixed || p == 0 {
76 return errors.Errorf("parallelism cost %d is not in range [%d, %d]",
77 h.Parallelism, minP, MaxParallelism)
78 }
79 // Previously we unconditionally casted costs.Parallelism to a uint8,
80 // so we replicate this behavior for backwards compatibility.
81 log.Printf("WARNING: Truncating parallelism cost of %d to %d", h.Parallelism, p)
82 }
83
84 minT := int64(1)
85 maxT := int64(math.MaxUint32)
86 if h.Time < minT || h.Time > maxT {
87 return errors.Errorf("time cost %d is not in range [%d, %d]", h.Time, minT, maxT)
88 }
89
90 minM := 8 * int64(p)
91 maxM := int64(math.MaxUint32)
92 if h.Memory < minM || h.Memory > maxM {
93 return errors.Errorf("memory cost %d KiB is not in range [%d, %d]", h.Memory, minM, maxM)
94 }
95 return nil
96 }
97
98 // CheckValidity ensures our buffers are the correct length.
99 func (w *WrappedKeyData) CheckValidity() error {
100 if w == nil {
101 return errNotInitialized
102 }
103 if len(w.EncryptedKey) == 0 {
104 return errors.Wrap(errNotInitialized, "encrypted key")
105 }
106 if err := util.CheckValidLength(IVLen, len(w.IV)); err != nil {
107 return errors.Wrap(err, "IV")
108 }
109 return errors.Wrap(util.CheckValidLength(HMACLen, len(w.Hmac)), "HMAC")
110 }
111
112 // CheckValidity ensures our ProtectorData has the correct fields for its source.
113 func (p *ProtectorData) CheckValidity() error {
114 if p == nil {
115 return errNotInitialized
116 }
117
118 if err := p.Source.CheckValidity(); err != nil {
119 return errors.Wrap(err, "protector source")
120 }
121
122 // Source specific checks
123 switch p.Source {
124 case SourceType_pam_passphrase:
125 if p.Uid < 0 {
126 return errors.Errorf("UID=%d is negative", p.Uid)
127 }
128 fallthrough
129 case SourceType_custom_passphrase:
130 if err := p.Costs.CheckValidity(); err != nil {
131 return errors.Wrap(err, "passphrase hashing costs")
132 }
133 if err := util.CheckValidLength(SaltLen, len(p.Salt)); err != nil {
134 return errors.Wrap(err, "passphrase hashing salt")
135 }
136 }
137
138 // Generic checks
139 if err := p.WrappedKey.CheckValidity(); err != nil {
140 return errors.Wrap(err, "wrapped protector key")
141 }
142 if err := util.CheckValidLength(ProtectorDescriptorLen, len(p.ProtectorDescriptor)); err != nil {
143 return errors.Wrap(err, "protector descriptor")
144
145 }
146 err := util.CheckValidLength(InternalKeyLen, len(p.WrappedKey.EncryptedKey))
147 return errors.Wrap(err, "encrypted protector key")
148 }
149
150 // CheckValidity ensures each of the options is valid.
151 func (e *EncryptionOptions) CheckValidity() error {
152 if e == nil {
153 return errNotInitialized
154 }
155 if _, ok := util.Index(e.Padding, paddingArray); !ok {
156 return errors.Errorf("padding of %d is invalid", e.Padding)
157 }
158 if err := e.Contents.CheckValidity(); err != nil {
159 return errors.Wrap(err, "contents encryption mode")
160 }
161 if err := e.Filenames.CheckValidity(); err != nil {
162 return errors.Wrap(err, "filenames encryption mode")
163 }
164 // If PolicyVersion is unset, treat it as 1.
165 if e.PolicyVersion == 0 {
166 e.PolicyVersion = 1
167 }
168 if e.PolicyVersion != 1 && e.PolicyVersion != 2 {
169 return errors.Errorf("policy version of %d is invalid", e.PolicyVersion)
170 }
171 return nil
172 }
173
174 // CheckValidity ensures the fields are valid and have the correct lengths.
175 func (w *WrappedPolicyKey) CheckValidity() error {
176 if w == nil {
177 return errNotInitialized
178 }
179 if err := w.WrappedKey.CheckValidity(); err != nil {
180 return errors.Wrap(err, "wrapped key")
181 }
182 if err := util.CheckValidLength(PolicyKeyLen, len(w.WrappedKey.EncryptedKey)); err != nil {
183 return errors.Wrap(err, "encrypted key")
184 }
185 err := util.CheckValidLength(ProtectorDescriptorLen, len(w.ProtectorDescriptor))
186 return errors.Wrap(err, "wrapping protector descriptor")
187 }
188
189 // CheckValidity ensures the fields and each wrapped key are valid.
190 func (p *PolicyData) CheckValidity() error {
191 if p == nil {
192 return errNotInitialized
193 }
194 // Check each wrapped key
195 for i, w := range p.WrappedPolicyKeys {
196 if err := w.CheckValidity(); err != nil {
197 return errors.Wrapf(err, "policy key slot %d", i)
198 }
199 }
200
201 if err := p.Options.CheckValidity(); err != nil {
202 return errors.Wrap(err, "policy options")
203 }
204
205 var expectedLen int
206 switch p.Options.PolicyVersion {
207 case 1:
208 expectedLen = PolicyDescriptorLenV1
209 case 2:
210 expectedLen = PolicyDescriptorLenV2
211 default:
212 return errors.Errorf("policy version of %d is invalid", p.Options.PolicyVersion)
213 }
214
215 if err := util.CheckValidLength(expectedLen, len(p.KeyDescriptor)); err != nil {
216 return errors.Wrap(err, "policy key descriptor")
217 }
218
219 return nil
220 }
221
222 // CheckValidity ensures the Config has all the necessary info for its Source.
223 func (c *Config) CheckValidity() error {
224 // General checks
225 if c == nil {
226 return errNotInitialized
227 }
228 if err := c.Source.CheckValidity(); err != nil {
229 return errors.Wrap(err, "default config source")
230 }
231
232 // Source specific checks
233 switch c.Source {
234 case SourceType_pam_passphrase, SourceType_custom_passphrase:
235 if err := c.HashCosts.CheckValidity(); err != nil {
236 return errors.Wrap(err, "config hashing costs")
237 }
238 }
239
240 return errors.Wrap(c.Options.CheckValidity(), "config options")
241 }