| 1 |
/* |
| 2 |
* keyring_test.go - tests for the keyring package |
| 3 |
* |
| 4 |
* Copyright 2017 Google Inc. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 7 |
* use this file except in compliance with the License. You may obtain a copy of |
| 8 |
* the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 |
* License for the specific language governing permissions and limitations under |
| 16 |
* the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package keyring |
| 20 |
|
| 21 |
import ( |
| 22 |
"os/user" |
| 23 |
"strconv" |
| 24 |
"testing" |
| 25 |
|
| 26 |
"github.com/google/fscrypt/crypto" |
| 27 |
"github.com/google/fscrypt/filesystem" |
| 28 |
"github.com/google/fscrypt/metadata" |
| 29 |
"github.com/google/fscrypt/util" |
| 30 |
) |
| 31 |
|
| 32 |
// Reader that always returns the same byte |
| 33 |
type ConstReader byte |
| 34 |
|
| 35 |
func (r ConstReader) Read(b []byte) (n int, err error) { |
| 36 |
for i := range b { |
| 37 |
b[i] = byte(r) |
| 38 |
} |
| 39 |
return len(b), nil |
| 40 |
} |
| 41 |
|
| 42 |
// Makes a key of the same repeating byte |
| 43 |
func makeKey(b byte, n int) (*crypto.Key, error) { |
| 44 |
return crypto.NewFixedLengthKeyFromReader(ConstReader(b), n) |
| 45 |
} |
| 46 |
|
| 47 |
var ( |
| 48 |
testUser, _ = util.EffectiveUser() |
| 49 |
fakeValidPolicyKey, _ = makeKey(42, metadata.PolicyKeyLen) |
| 50 |
fakeInvalidPolicyKey, _ = makeKey(42, metadata.PolicyKeyLen-1) |
| 51 |
fakeV1Descriptor = "0123456789abcdef" |
| 52 |
fakeV2Descriptor, _ = crypto.ComputeKeyDescriptor(fakeValidPolicyKey, 2) |
| 53 |
) |
| 54 |
|
| 55 |
func assertKeyStatus(t *testing.T, descriptor string, options *Options, |
| 56 |
expectedStatus KeyStatus) { |
| 57 |
status, err := GetEncryptionKeyStatus(descriptor, options) |
| 58 |
if err != nil { |
| 59 |
t.Error(err) |
| 60 |
} |
| 61 |
if status != expectedStatus { |
| 62 |
t.Errorf("Expected key status %v but got key status %v", expectedStatus, status) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
// getTestMount retrieves the Mount for a test filesystem, or skips the test if |
| 67 |
// no test filesystem is available. |
| 68 |
func getTestMount(t *testing.T) *filesystem.Mount { |
| 69 |
root, err := util.TestRoot() |
| 70 |
if err != nil { |
| 71 |
t.Skip(err) |
| 72 |
} |
| 73 |
mount, err := filesystem.GetMount(root) |
| 74 |
if err != nil { |
| 75 |
t.Skip(err) |
| 76 |
} |
| 77 |
return mount |
| 78 |
} |
| 79 |
|
| 80 |
// getTestMountV2 is like getTestMount, but it also checks that the |
| 81 |
// filesystem keyring and v2 encryption policies are supported. |
| 82 |
func getTestMountV2(t *testing.T) *filesystem.Mount { |
| 83 |
mount := getTestMount(t) |
| 84 |
if !IsFsKeyringSupported(mount) { |
| 85 |
t.Skip("No support for fs keyring, skipping test.") |
| 86 |
} |
| 87 |
return mount |
| 88 |
} |
| 89 |
|
| 90 |
func requireRoot(t *testing.T) { |
| 91 |
if !util.IsUserRoot() { |
| 92 |
t.Skip("Not root, skipping test.") |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// getNonRootUsers checks for root permission, then returns the users for uids |
| 97 |
// 1000...1000+count-1. If this fails, the test is skipped. |
| 98 |
func getNonRootUsers(t *testing.T, count int) []*user.User { |
| 99 |
requireRoot(t) |
| 100 |
users := make([]*user.User, count) |
| 101 |
for i := 0; i < count; i++ { |
| 102 |
uid := 1000 + i |
| 103 |
user, err := user.LookupId(strconv.Itoa(uid)) |
| 104 |
if err != nil { |
| 105 |
t.Skip(err) |
| 106 |
} |
| 107 |
users[i] = user |
| 108 |
} |
| 109 |
return users |
| 110 |
} |
| 111 |
|
| 112 |
func getOptionsForFsKeyringUsers(t *testing.T, numNonRootUsers int) (rootOptions *Options, userOptions []*Options) { |
| 113 |
mount := getTestMountV2(t) |
| 114 |
nonRootUsers := getNonRootUsers(t, numNonRootUsers) |
| 115 |
rootOptions = &Options{ |
| 116 |
Mount: mount, |
| 117 |
User: testUser, |
| 118 |
} |
| 119 |
userOptions = make([]*Options, numNonRootUsers) |
| 120 |
for i := 0; i < numNonRootUsers; i++ { |
| 121 |
userOptions[i] = &Options{ |
| 122 |
Mount: mount, |
| 123 |
User: nonRootUsers[i], |
| 124 |
} |
| 125 |
} |
| 126 |
return |
| 127 |
} |
| 128 |
|
| 129 |
// testAddAndRemoveKey does the common tests for adding+removing keys that are |
| 130 |
// run in multiple configurations (v1 policies with user keyring, v1 policies |
| 131 |
// with fs keyring, and v2 policies with fs keyring). |
| 132 |
func testAddAndRemoveKey(t *testing.T, descriptor string, options *Options) { |
| 133 |
|
| 134 |
// Basic add, get status, and remove |
| 135 |
if err := AddEncryptionKey(fakeValidPolicyKey, descriptor, options); err != nil { |
| 136 |
t.Error(err) |
| 137 |
} |
| 138 |
assertKeyStatus(t, descriptor, options, KeyPresent) |
| 139 |
if err := RemoveEncryptionKey(descriptor, options, false); err != nil { |
| 140 |
t.Error(err) |
| 141 |
} |
| 142 |
assertKeyStatus(t, descriptor, options, KeyAbsent) |
| 143 |
err := RemoveEncryptionKey(descriptor, options, false) |
| 144 |
if err != ErrKeyNotPresent { |
| 145 |
t.Error(err) |
| 146 |
} |
| 147 |
|
| 148 |
// Adding a key twice should succeed |
| 149 |
if err := AddEncryptionKey(fakeValidPolicyKey, descriptor, options); err != nil { |
| 150 |
t.Error(err) |
| 151 |
} |
| 152 |
if err := AddEncryptionKey(fakeValidPolicyKey, descriptor, options); err != nil { |
| 153 |
t.Error("AddEncryptionKey should not fail if key already exists") |
| 154 |
} |
| 155 |
RemoveEncryptionKey(descriptor, options, false) |
| 156 |
assertKeyStatus(t, descriptor, options, KeyAbsent) |
| 157 |
|
| 158 |
// Adding a key with wrong length should fail |
| 159 |
if err := AddEncryptionKey(fakeInvalidPolicyKey, descriptor, options); err == nil { |
| 160 |
RemoveEncryptionKey(descriptor, options, false) |
| 161 |
t.Error("AddEncryptionKey should fail with wrong-length key") |
| 162 |
} |
| 163 |
assertKeyStatus(t, descriptor, options, KeyAbsent) |
| 164 |
} |
| 165 |
|
| 166 |
func TestUserKeyring(t *testing.T) { |
| 167 |
mount := getTestMount(t) |
| 168 |
options := &Options{ |
| 169 |
Mount: mount, |
| 170 |
User: testUser, |
| 171 |
UseFsKeyringForV1Policies: false, |
| 172 |
} |
| 173 |
testAddAndRemoveKey(t, fakeV1Descriptor, options) |
| 174 |
} |
| 175 |
|
| 176 |
func TestFsKeyringV1PolicyKey(t *testing.T) { |
| 177 |
requireRoot(t) |
| 178 |
mount := getTestMountV2(t) |
| 179 |
options := &Options{ |
| 180 |
Mount: mount, |
| 181 |
User: testUser, |
| 182 |
UseFsKeyringForV1Policies: true, |
| 183 |
} |
| 184 |
testAddAndRemoveKey(t, fakeV1Descriptor, options) |
| 185 |
} |
| 186 |
|
| 187 |
func TestV2PolicyKey(t *testing.T) { |
| 188 |
mount := getTestMountV2(t) |
| 189 |
options := &Options{ |
| 190 |
Mount: mount, |
| 191 |
User: testUser, |
| 192 |
} |
| 193 |
testAddAndRemoveKey(t, fakeV2Descriptor, options) |
| 194 |
} |
| 195 |
|
| 196 |
func TestV2PolicyKeyCannotBeRemovedByAnotherUser(t *testing.T) { |
| 197 |
rootOptions, userOptions := getOptionsForFsKeyringUsers(t, 2) |
| 198 |
user1Options := userOptions[0] |
| 199 |
user2Options := userOptions[1] |
| 200 |
|
| 201 |
// Add key as non-root user. |
| 202 |
if err := AddEncryptionKey(fakeValidPolicyKey, fakeV2Descriptor, user1Options); err != nil { |
| 203 |
t.Error(err) |
| 204 |
} |
| 205 |
assertKeyStatus(t, fakeV2Descriptor, user1Options, KeyPresent) |
| 206 |
assertKeyStatus(t, fakeV2Descriptor, user2Options, KeyPresentButOnlyOtherUsers) |
| 207 |
assertKeyStatus(t, fakeV2Descriptor, rootOptions, KeyPresentButOnlyOtherUsers) |
| 208 |
|
| 209 |
// Key shouldn't be removable by another user, even root. |
| 210 |
err := RemoveEncryptionKey(fakeV2Descriptor, user2Options, false) |
| 211 |
if err != ErrKeyAddedByOtherUsers { |
| 212 |
t.Error(err) |
| 213 |
} |
| 214 |
assertKeyStatus(t, fakeV2Descriptor, user1Options, KeyPresent) |
| 215 |
assertKeyStatus(t, fakeV2Descriptor, user2Options, KeyPresentButOnlyOtherUsers) |
| 216 |
assertKeyStatus(t, fakeV2Descriptor, rootOptions, KeyPresentButOnlyOtherUsers) |
| 217 |
err = RemoveEncryptionKey(fakeV2Descriptor, rootOptions, false) |
| 218 |
if err != ErrKeyAddedByOtherUsers { |
| 219 |
t.Error(err) |
| 220 |
} |
| 221 |
assertKeyStatus(t, fakeV2Descriptor, user1Options, KeyPresent) |
| 222 |
assertKeyStatus(t, fakeV2Descriptor, user2Options, KeyPresentButOnlyOtherUsers) |
| 223 |
assertKeyStatus(t, fakeV2Descriptor, rootOptions, KeyPresentButOnlyOtherUsers) |
| 224 |
|
| 225 |
if err := RemoveEncryptionKey(fakeV2Descriptor, user1Options, false); err != nil { |
| 226 |
t.Error(err) |
| 227 |
} |
| 228 |
assertKeyStatus(t, fakeV2Descriptor, user1Options, KeyAbsent) |
| 229 |
assertKeyStatus(t, fakeV2Descriptor, user2Options, KeyAbsent) |
| 230 |
assertKeyStatus(t, fakeV2Descriptor, rootOptions, KeyAbsent) |
| 231 |
} |
| 232 |
|
| 233 |
func TestV2PolicyKeyMultipleUsers(t *testing.T) { |
| 234 |
rootOptions, userOptions := getOptionsForFsKeyringUsers(t, 2) |
| 235 |
user1Options := userOptions[0] |
| 236 |
user2Options := userOptions[1] |
| 237 |
|
| 238 |
// Add key as two non-root users. |
| 239 |
if err := AddEncryptionKey(fakeValidPolicyKey, fakeV2Descriptor, user1Options); err != nil { |
| 240 |
t.Error(err) |
| 241 |
} |
| 242 |
if err := AddEncryptionKey(fakeValidPolicyKey, fakeV2Descriptor, user2Options); err != nil { |
| 243 |
t.Error(err) |
| 244 |
} |
| 245 |
assertKeyStatus(t, fakeV2Descriptor, user1Options, KeyPresent) |
| 246 |
assertKeyStatus(t, fakeV2Descriptor, user2Options, KeyPresent) |
| 247 |
assertKeyStatus(t, fakeV2Descriptor, rootOptions, KeyPresentButOnlyOtherUsers) |
| 248 |
|
| 249 |
// Remove key as one user. |
| 250 |
err := RemoveEncryptionKey(fakeV2Descriptor, user1Options, false) |
| 251 |
if err != ErrKeyAddedByOtherUsers { |
| 252 |
t.Error(err) |
| 253 |
} |
| 254 |
assertKeyStatus(t, fakeV2Descriptor, user1Options, KeyPresentButOnlyOtherUsers) |
| 255 |
assertKeyStatus(t, fakeV2Descriptor, user2Options, KeyPresent) |
| 256 |
assertKeyStatus(t, fakeV2Descriptor, rootOptions, KeyPresentButOnlyOtherUsers) |
| 257 |
|
| 258 |
// Remove key as the other user. |
| 259 |
err = RemoveEncryptionKey(fakeV2Descriptor, user2Options, false) |
| 260 |
if err != nil { |
| 261 |
t.Error(err) |
| 262 |
} |
| 263 |
assertKeyStatus(t, fakeV2Descriptor, user1Options, KeyAbsent) |
| 264 |
assertKeyStatus(t, fakeV2Descriptor, user2Options, KeyAbsent) |
| 265 |
assertKeyStatus(t, fakeV2Descriptor, rootOptions, KeyAbsent) |
| 266 |
} |
| 267 |
|
| 268 |
func TestV2PolicyKeyWrongDescriptor(t *testing.T) { |
| 269 |
mount := getTestMountV2(t) |
| 270 |
options := &Options{ |
| 271 |
Mount: mount, |
| 272 |
User: testUser, |
| 273 |
} |
| 274 |
// one wrong but valid hex, and one not valid hex |
| 275 |
wrongV2Descriptors := []string{"abcdabcdabcdabcdabcdabcdabcdabcd", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"} |
| 276 |
|
| 277 |
for _, desc := range wrongV2Descriptors { |
| 278 |
if err := AddEncryptionKey(fakeValidPolicyKey, desc, options); err == nil { |
| 279 |
RemoveEncryptionKey(desc, options, false) |
| 280 |
t.Error("For v2 policy keys, AddEncryptionKey should fail if the descriptor is wrong") |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
func TestV2PolicyKeyBadMount(t *testing.T) { |
| 286 |
options := &Options{ |
| 287 |
Mount: &filesystem.Mount{Path: "/NONEXISTENT_MOUNT"}, |
| 288 |
User: testUser, |
| 289 |
} |
| 290 |
if err := AddEncryptionKey(fakeValidPolicyKey, fakeV2Descriptor, options); err == nil { |
| 291 |
RemoveEncryptionKey(fakeV2Descriptor, options, false) |
| 292 |
t.Error("AddEncryptionKey should have failed with bad mount!") |
| 293 |
} |
| 294 |
if err := RemoveEncryptionKey(fakeV2Descriptor, options, false); err == nil { |
| 295 |
t.Error("RemoveEncryptionKey should have failed with bad mount!") |
| 296 |
} |
| 297 |
status, err := GetEncryptionKeyStatus(fakeV2Descriptor, options) |
| 298 |
if err == nil { |
| 299 |
t.Error("GetEncryptionKeyStatus should have failed with bad mount!") |
| 300 |
} |
| 301 |
if status != KeyStatusUnknown { |
| 302 |
t.Error("GetEncryptionKeyStatus should have returned unknown status!") |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
func TestV2PolicyKeyRemoveForAllUsers(t *testing.T) { |
| 307 |
rootOptions, userOptions := getOptionsForFsKeyringUsers(t, 2) |
| 308 |
user1Options := userOptions[0] |
| 309 |
user2Options := userOptions[1] |
| 310 |
|
| 311 |
// Add key as two non-root users. |
| 312 |
if err := AddEncryptionKey(fakeValidPolicyKey, fakeV2Descriptor, user1Options); err != nil { |
| 313 |
t.Error(err) |
| 314 |
} |
| 315 |
if err := AddEncryptionKey(fakeValidPolicyKey, fakeV2Descriptor, user2Options); err != nil { |
| 316 |
t.Error(err) |
| 317 |
} |
| 318 |
assertKeyStatus(t, fakeV2Descriptor, user1Options, KeyPresent) |
| 319 |
assertKeyStatus(t, fakeV2Descriptor, user2Options, KeyPresent) |
| 320 |
assertKeyStatus(t, fakeV2Descriptor, rootOptions, KeyPresentButOnlyOtherUsers) |
| 321 |
|
| 322 |
// Remove key for all users as root. |
| 323 |
err := RemoveEncryptionKey(fakeV2Descriptor, rootOptions, true) |
| 324 |
if err != nil { |
| 325 |
t.Error(err) |
| 326 |
} |
| 327 |
assertKeyStatus(t, fakeV2Descriptor, user1Options, KeyAbsent) |
| 328 |
assertKeyStatus(t, fakeV2Descriptor, user2Options, KeyAbsent) |
| 329 |
assertKeyStatus(t, fakeV2Descriptor, rootOptions, KeyAbsent) |
| 330 |
} |