-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypto.cc
874 lines (774 loc) · 31.5 KB
/
crypto.cc
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Contains the implementation of class Crypto
#include "cryptohome/crypto.h"
#include <sys/types.h>
#include <crypto/sha2.h>
#include <limits>
#include <map>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/rsa.h>
#include <openssl/sha.h>
#include <unistd.h>
#include <utility>
#include <base/files/file_path.h>
#include <base/logging.h>
#include <base/strings/string_number_conversions.h>
#include <brillo/secure_blob.h>
#include "cryptohome/attestation.pb.h"
#include "cryptohome/challenge_credential_auth_block.h"
#include "cryptohome/cryptohome_common.h"
#include "cryptohome/cryptohome_metrics.h"
#include "cryptohome/cryptolib.h"
#include "cryptohome/double_wrapped_compat_auth_block.h"
#include "cryptohome/key_objects.h"
#include "cryptohome/le_credential_manager_impl.h"
#include "cryptohome/libscrypt_compat.h"
#include "cryptohome/libscrypt_compat_auth_block.h"
#include "cryptohome/pin_weaver_auth_block.h"
#include "cryptohome/platform.h"
#include "cryptohome/tpm_bound_to_pcr_auth_block.h"
#include "cryptohome/tpm_init.h"
#include "cryptohome/tpm_not_bound_to_pcr_auth_block.h"
#include "cryptohome/vault_keyset.h"
using base::FilePath;
using brillo::SecureBlob;
namespace cryptohome {
namespace {
// Location where we store the Low Entropy (LE) credential manager related
// state.
const char kSignInHashTreeDir[] = "/home/.shadow/low_entropy_creds";
// Maximum size of the salt file.
const int64_t kSystemSaltMaxSize = (1 << 20); // 1 MB
// File permissions of salt file (modulo umask).
const mode_t kSaltFilePermissions = 0644;
// This generates the reset secret for PinWeaver credentials. Doing it per
// secret is confusing and difficult to maintain. It's necessary so that
// different credentials can all maintain the same reset secret (i.e. the
// password resets the PIN), without storing said secret in the clear. In the
// USS key hierarchy, only one reset secret will exist.
bool GenerateResetSecret(const VaultKeyset& vault_keyset,
brillo::SecureBlob* reset_secret,
brillo::SecureBlob* reset_salt) {
DCHECK(reset_secret);
DCHECK(reset_salt);
// For new users, a reset seed is stored in the VaultKeyset, which is derived
// into the reset secret.
if (!vault_keyset.reset_seed().empty()) {
SecureBlob local_reset_seed(vault_keyset.reset_seed().begin(),
vault_keyset.reset_seed().end());
*reset_salt = CryptoLib::CreateSecureRandomBlob(kAesBlockSize);
*reset_secret = CryptoLib::HmacSha256(*reset_salt, local_reset_seed);
return true;
}
// When a user credential is being migrated (such as the password), the reset
// secret needs to remain the same to unlock the PIN. In this case, the reset
// secret is passed through the vault keyset.
if (!vault_keyset.reset_secret().empty()) {
reset_secret->assign(vault_keyset.reset_secret().begin(),
vault_keyset.reset_secret().end());
return true;
}
LOG(ERROR) << "The VaultKeyset doesn't have a reset seed, so we can't"
" set up an LE credential.";
return false;
}
bool UnwrapVKKVaultKeyset(const SerializedVaultKeyset& serialized,
const KeyBlobs& vkk_data,
VaultKeyset* keyset,
CryptoError* error) {
const SecureBlob& vkk_key = vkk_data.vkk_key.value();
const SecureBlob& vkk_iv = vkk_data.vkk_iv.value();
const SecureBlob& chaps_iv = vkk_data.chaps_iv.value();
// Decrypt the keyset protobuf.
SecureBlob local_encrypted_keyset(serialized.wrapped_keyset().begin(),
serialized.wrapped_keyset().end());
SecureBlob plain_text;
if (!CryptoLib::AesDecryptDeprecated(local_encrypted_keyset, vkk_key, vkk_iv,
&plain_text)) {
LOG(ERROR) << "AES decryption failed for vault keyset.";
PopulateError(error, CryptoError::CE_OTHER_CRYPTO);
return false;
}
if (!keyset->FromKeysBlob(plain_text)) {
LOG(ERROR) << "Failed to decode the keys blob.";
PopulateError(error, CryptoError::CE_OTHER_CRYPTO);
return false;
}
// Decrypt the chaps key.
if (serialized.has_wrapped_chaps_key()) {
SecureBlob local_wrapped_chaps_key(serialized.wrapped_chaps_key());
SecureBlob unwrapped_chaps_key;
if (!CryptoLib::AesDecryptDeprecated(local_wrapped_chaps_key, vkk_key,
chaps_iv, &unwrapped_chaps_key)) {
LOG(ERROR) << "AES decryption failed for chaps key.";
PopulateError(error, CryptoError::CE_OTHER_CRYPTO);
return false;
}
keyset->set_chaps_key(unwrapped_chaps_key);
}
// Decrypt the reset seed.
if (vkk_data.wrapped_reset_seed != base::nullopt &&
!vkk_data.wrapped_reset_seed.value().empty()) {
SecureBlob unwrapped_reset_seed;
SecureBlob local_wrapped_reset_seed =
SecureBlob(serialized.wrapped_reset_seed());
SecureBlob local_reset_iv = SecureBlob(serialized.reset_iv());
if (!CryptoLib::AesDecryptDeprecated(local_wrapped_reset_seed, vkk_key,
local_reset_iv,
&unwrapped_reset_seed)) {
LOG(ERROR) << "AES decryption failed for reset seed.";
PopulateError(error, CryptoError::CE_OTHER_CRYPTO);
return false;
}
keyset->set_reset_seed(unwrapped_reset_seed);
}
return true;
}
bool UnwrapScryptVaultKeyset(const SerializedVaultKeyset& serialized,
const KeyBlobs& vkk_data,
VaultKeyset* keyset,
CryptoError* error) {
SecureBlob blob = SecureBlob(serialized.wrapped_keyset());
SecureBlob decrypted(blob.size());
if (!LibScryptCompat::Decrypt(blob, vkk_data.scrypt_key->derived_key(),
&decrypted)) {
return false;
}
if (serialized.has_wrapped_chaps_key()) {
SecureBlob chaps_key;
SecureBlob wrapped_chaps_key = SecureBlob(serialized.wrapped_chaps_key());
chaps_key.resize(wrapped_chaps_key.size());
if (!LibScryptCompat::Decrypt(wrapped_chaps_key,
vkk_data.chaps_scrypt_key->derived_key(),
&chaps_key)) {
return false;
}
keyset->set_chaps_key(chaps_key);
}
if (serialized.has_wrapped_reset_seed()) {
SecureBlob reset_seed;
SecureBlob wrapped_reset_seed = SecureBlob(serialized.wrapped_reset_seed());
reset_seed.resize(wrapped_reset_seed.size());
if (!LibScryptCompat::Decrypt(
wrapped_reset_seed,
vkk_data.scrypt_wrapped_reset_seed_key->derived_key(),
&reset_seed)) {
return false;
}
keyset->set_reset_seed(reset_seed);
}
// There is a SHA hash included at the end of the decrypted blob. However,
// scrypt already appends a MAC, so if the payload is corrupted we will fail
// on the first call to DecryptScryptBlob.
// TODO(crbug.com/984782): get rid of this entirely.
if (decrypted.size() < SHA_DIGEST_LENGTH) {
LOG(ERROR) << "Message length underflow: " << decrypted.size() << " bytes?";
return false;
}
decrypted.resize(decrypted.size() - SHA_DIGEST_LENGTH);
keyset->FromKeysBlob(decrypted);
return true;
}
bool WrapVaultKeysetWithAesDeprecated(const VaultKeyset& vault_keyset,
const KeyBlobs& blobs,
bool store_reset_seed,
SerializedVaultKeyset* serialized) {
if (blobs.vkk_key == base::nullopt || blobs.vkk_iv == base::nullopt ||
blobs.chaps_iv == base::nullopt) {
DLOG(FATAL) << "Fields missing from KeyBlobs.";
return false;
}
SecureBlob vault_blob;
if (!vault_keyset.ToKeysBlob(&vault_blob)) {
LOG(ERROR) << "Failure serializing keyset to buffer";
return false;
}
SecureBlob vault_cipher_text;
if (!CryptoLib::AesEncryptDeprecated(vault_blob, blobs.vkk_key.value(),
blobs.vkk_iv.value(),
&vault_cipher_text)) {
return false;
}
serialized->set_wrapped_keyset(vault_cipher_text.data(),
vault_cipher_text.size());
if (vault_keyset.chaps_key().size() == CRYPTOHOME_CHAPS_KEY_LENGTH) {
SecureBlob wrapped_chaps_key;
if (!CryptoLib::AesEncryptDeprecated(
vault_keyset.chaps_key(), blobs.vkk_key.value(),
blobs.chaps_iv.value(), &wrapped_chaps_key)) {
return false;
}
serialized->set_wrapped_chaps_key(wrapped_chaps_key.data(),
wrapped_chaps_key.size());
} else {
serialized->clear_wrapped_chaps_key();
}
// If a reset seed is present, encrypt and store it, else clear the field.
if (store_reset_seed && vault_keyset.reset_seed().size() != 0) {
const auto reset_iv = CryptoLib::CreateSecureRandomBlob(kAesBlockSize);
SecureBlob wrapped_reset_seed;
if (!CryptoLib::AesEncryptDeprecated(vault_keyset.reset_seed(),
blobs.vkk_key.value(), reset_iv,
&wrapped_reset_seed)) {
LOG(ERROR) << "AES encryption of Reset seed failed.";
return false;
}
serialized->set_wrapped_reset_seed(wrapped_reset_seed.data(),
wrapped_reset_seed.size());
serialized->set_reset_iv(reset_iv.data(), reset_iv.size());
} else {
serialized->clear_wrapped_reset_seed();
serialized->clear_reset_iv();
}
return true;
}
bool WrapScryptVaultKeyset(const VaultKeyset& vault_keyset,
const KeyBlobs& key_blobs,
SerializedVaultKeyset* serialized) {
if (vault_keyset.IsLECredential()) {
LOG(ERROR) << "Low entropy credentials cannot be scrypt-wrapped.";
return false;
}
brillo::SecureBlob blob;
if (!vault_keyset.ToKeysBlob(&blob)) {
LOG(ERROR) << "Failure serializing keyset to buffer";
return false;
}
// Append the SHA1 hash of the keyset blob. This is done solely for
// backwards-compatibility purposes, since scrypt already creates a
// MAC for the encrypted blob. It is ignored in DecryptScrypt since
// it is redundant.
brillo::SecureBlob hash = CryptoLib::Sha1(blob);
brillo::SecureBlob local_blob = SecureBlob::Combine(blob, hash);
brillo::SecureBlob cipher_text;
if (!LibScryptCompat::Encrypt(key_blobs.scrypt_key->derived_key(),
key_blobs.scrypt_key->ConsumeSalt(), local_blob,
kDefaultScryptParams, &cipher_text)) {
LOG(ERROR) << "Scrypt encrypt of keyset blob failed.";
return false;
}
serialized->set_wrapped_keyset(cipher_text.data(), cipher_text.size());
if (vault_keyset.chaps_key().size() == CRYPTOHOME_CHAPS_KEY_LENGTH) {
SecureBlob wrapped_chaps_key;
if (!LibScryptCompat::Encrypt(key_blobs.chaps_scrypt_key->derived_key(),
key_blobs.chaps_scrypt_key->ConsumeSalt(),
vault_keyset.chaps_key(),
kDefaultScryptParams, &wrapped_chaps_key)) {
LOG(ERROR) << "Scrypt encrypt of chaps key blob failed.";
return false;
}
serialized->set_wrapped_chaps_key(wrapped_chaps_key.data(),
wrapped_chaps_key.size());
} else {
serialized->clear_wrapped_chaps_key();
}
// If there is a reset seed, encrypt and store it.
if (vault_keyset.reset_seed().size() != 0) {
brillo::SecureBlob wrapped_reset_seed;
if (!LibScryptCompat::Encrypt(
key_blobs.scrypt_wrapped_reset_seed_key->derived_key(),
key_blobs.scrypt_wrapped_reset_seed_key->ConsumeSalt(),
vault_keyset.reset_seed(), kDefaultScryptParams,
&wrapped_reset_seed)) {
LOG(ERROR) << "Scrypt encrypt of reset seed failed.";
return false;
}
serialized->set_wrapped_reset_seed(wrapped_reset_seed.data(),
wrapped_reset_seed.size());
} else {
serialized->clear_wrapped_reset_seed();
}
return true;
}
} // namespace
Crypto::Crypto(Platform* platform)
: tpm_(NULL),
platform_(platform),
tpm_init_(NULL),
disable_logging_for_tests_(false) {}
Crypto::~Crypto() {}
bool Crypto::Init(TpmInit* tpm_init) {
CHECK(tpm_init) << "Crypto wanted to use TPM but was not provided a TPM";
if (tpm_ == NULL) {
tpm_ = tpm_init->get_tpm();
}
tpm_init_ = tpm_init;
tpm_init_->SetupTpm(true);
if (tpm_->GetLECredentialBackend() &&
tpm_->GetLECredentialBackend()->IsSupported()) {
le_manager_ = std::make_unique<LECredentialManagerImpl>(
tpm_->GetLECredentialBackend(), base::FilePath(kSignInHashTreeDir));
}
return true;
}
CryptoError Crypto::EnsureTpm(bool reload_key) const {
CryptoError result = CryptoError::CE_NONE;
if (tpm_ && tpm_init_) {
if (reload_key || !tpm_init_->HasCryptohomeKey()) {
tpm_init_->SetupTpm(true);
}
}
return result;
}
bool Crypto::GetOrCreateSalt(const FilePath& path,
size_t length,
bool force,
SecureBlob* salt) const {
int64_t file_len = 0;
if (platform_->FileExists(path)) {
if (!platform_->GetFileSize(path, &file_len)) {
LOG(ERROR) << "Can't get file len for " << path.value();
return false;
}
}
SecureBlob local_salt;
if (force || file_len == 0 || file_len > kSystemSaltMaxSize) {
LOG(ERROR) << "Creating new salt at " << path.value() << " (" << force
<< ", " << file_len << ")";
// If this salt doesn't exist, automatically create it.
local_salt = CryptoLib::CreateSecureRandomBlob(length);
if (!platform_->WriteSecureBlobToFileAtomicDurable(path, local_salt,
kSaltFilePermissions)) {
LOG(ERROR) << "Could not write user salt";
return false;
}
} else {
local_salt.resize(file_len);
if (!platform_->ReadFileToSecureBlob(path, &local_salt)) {
LOG(ERROR) << "Could not read salt file of length " << file_len;
return false;
}
}
if (salt) {
salt->swap(local_salt);
}
return true;
}
void Crypto::PasswordToPasskey(const char* password,
const brillo::SecureBlob& salt,
SecureBlob* passkey) {
CHECK(password);
std::string ascii_salt = CryptoLib::SecureBlobToHex(salt);
// Convert a raw password to a password hash
SHA256_CTX sha_context;
SecureBlob md_value(SHA256_DIGEST_LENGTH);
SHA256_Init(&sha_context);
SHA256_Update(&sha_context, ascii_salt.data(), ascii_salt.length());
SHA256_Update(&sha_context, password, strlen(password));
SHA256_Final(md_value.data(), &sha_context);
md_value.resize(SHA256_DIGEST_LENGTH / 2);
SecureBlob local_passkey(SHA256_DIGEST_LENGTH);
CryptoLib::SecureBlobToHexToBuffer(md_value, local_passkey.data(),
local_passkey.size());
passkey->swap(local_passkey);
}
bool Crypto::UnwrapVaultKeyset(const SerializedVaultKeyset& serialized,
const KeyBlobs& vkk_data,
VaultKeyset* keyset,
CryptoError* error) {
bool has_vkk_key = vkk_data.vkk_key != base::nullopt &&
vkk_data.vkk_iv != base::nullopt &&
vkk_data.chaps_iv != base::nullopt;
bool has_scrypt_key = vkk_data.scrypt_key != nullptr;
bool successfully_unwrapped = false;
if (has_vkk_key && !has_scrypt_key) {
successfully_unwrapped =
UnwrapVKKVaultKeyset(serialized, vkk_data, keyset, error);
} else if (has_scrypt_key && !has_vkk_key) {
successfully_unwrapped =
UnwrapScryptVaultKeyset(serialized, vkk_data, keyset, error);
} else {
DLOG(FATAL) << "An invalid key combination exists";
return false;
}
if (successfully_unwrapped) {
// By this point we know that the TPM is successfully owned, everything
// is initialized, and we were able to successfully decrypt a
// TPM-wrapped keyset. So, for TPMs with updateable firmware, we assume
// that it is stable (and the TPM can invalidate the old version).
// TODO(dlunev): We shall try to get this out of cryptohome eventually.
const bool tpm_backed =
(serialized.flags() & SerializedVaultKeyset::TPM_WRAPPED) ||
(serialized.flags() & SerializedVaultKeyset::LE_CREDENTIAL);
if (tpm_backed && tpm_ != nullptr) {
tpm_->DeclareTpmFirmwareStable();
}
}
return successfully_unwrapped;
}
bool Crypto::DecryptScrypt(const SerializedVaultKeyset& serialized,
const SecureBlob& key,
CryptoError* error,
VaultKeyset* keyset) const {
SecureBlob blob = SecureBlob(serialized.wrapped_keyset());
SecureBlob decrypted(blob.size());
if (!CryptoLib::DeprecatedDecryptScryptBlob(blob, key, &decrypted, error)) {
LOG(ERROR) << "Wrapped keyset Scrypt decrypt failed.";
return false;
}
bool chaps_key_present = serialized.has_wrapped_chaps_key();
if (chaps_key_present) {
SecureBlob chaps_key;
SecureBlob wrapped_chaps_key = SecureBlob(serialized.wrapped_chaps_key());
chaps_key.resize(wrapped_chaps_key.size());
// Perform a Scrypt operation on wrapped chaps key.
if (!CryptoLib::DeprecatedDecryptScryptBlob(wrapped_chaps_key, key,
&chaps_key, error)) {
LOG(ERROR) << "Chaps key scrypt decrypt failed.";
return false;
}
keyset->set_chaps_key(chaps_key);
}
if (serialized.has_wrapped_reset_seed()) {
SecureBlob reset_seed;
SecureBlob wrapped_reset_seed = SecureBlob(serialized.wrapped_reset_seed());
reset_seed.resize(wrapped_reset_seed.size());
// Perform a Scrypt operation on wrapped reset seed.
if (!CryptoLib::DeprecatedDecryptScryptBlob(wrapped_reset_seed, key,
&reset_seed, error)) {
LOG(ERROR) << "Reset seed scrypt decrypt failed.";
return false;
}
keyset->set_reset_seed(reset_seed);
}
// There is a SHA hash included at the end of the decrypted blob. However,
// scrypt already appends a MAC, so if the payload is corrupted we will fail
// on the first call to DecryptScryptBlob.
// TODO(crbug.com/984782): get rid of this entirely.
if (decrypted.size() < SHA_DIGEST_LENGTH) {
LOG(ERROR) << "Message length underflow: " << decrypted.size() << " bytes?";
return false;
}
decrypted.resize(decrypted.size() - SHA_DIGEST_LENGTH);
keyset->FromKeysBlob(decrypted);
return true;
}
bool Crypto::NeedsPcrBinding(const uint64_t& label) const {
DCHECK(le_manager_)
<< "le_manage_ doesn't exist when calling NeedsPcrBinding()";
return le_manager_->NeedsPcrBinding(label);
}
bool Crypto::DecryptVaultKeyset(const SerializedVaultKeyset& serialized,
const SecureBlob& vault_key,
bool locked_to_single_user,
unsigned int* crypt_flags,
CryptoError* error,
VaultKeyset* vault_keyset) {
if (crypt_flags)
*crypt_flags = serialized.flags();
PopulateError(error, CryptoError::CE_NONE);
unsigned int flags = serialized.flags();
std::unique_ptr<AuthBlock> auth_block = DeriveAuthBlock(flags);
if (!auth_block) {
LOG(ERROR) << "Keyset wrapped with unknown method.";
return false;
}
AuthInput auth_input = {vault_key, locked_to_single_user};
AuthBlockState auth_state = {serialized};
KeyBlobs vkk_data;
if (!auth_block->Derive(auth_input, auth_state, &vkk_data, error)) {
return false;
}
if (flags & SerializedVaultKeyset::LE_CREDENTIAL) {
// This is possible to be empty if an old version of CR50 is running.
if (vkk_data.reset_secret.has_value() &&
!vkk_data.reset_secret.value().empty()) {
vault_keyset->set_reset_secret(vkk_data.reset_secret.value());
}
}
return UnwrapVaultKeyset(serialized, vkk_data, vault_keyset, error);
}
bool Crypto::GenerateAndWrapKeys(const VaultKeyset& vault_keyset,
const KeyBlobs& blobs,
bool store_reset_seed,
SerializedVaultKeyset* serialized) const {
if (serialized->flags() & SerializedVaultKeyset::SCRYPT_WRAPPED) {
return WrapScryptVaultKeyset(vault_keyset, blobs, serialized);
}
return WrapVaultKeysetWithAesDeprecated(vault_keyset, blobs, store_reset_seed,
serialized);
}
bool Crypto::EncryptVaultKeyset(const VaultKeyset& vault_keyset,
const SecureBlob& vault_key,
const SecureBlob& vault_key_salt,
const std::string& obfuscated_username,
SerializedVaultKeyset* serialized) const {
std::unique_ptr<AuthBlock> auth_block = CreateAuthBlock(vault_keyset);
if (!auth_block) {
LOG(ERROR) << "Failed to retrieve auth block.";
return false;
}
bool store_reset_seed = true;
base::Optional<SecureBlob> reset_secret;
if (vault_keyset.IsLECredential()) {
SecureBlob inner_reset_secret;
SecureBlob reset_salt;
if (!GenerateResetSecret(vault_keyset, &inner_reset_secret, &reset_salt)) {
return false;
}
reset_secret = inner_reset_secret;
serialized->set_reset_salt(reset_salt.data(), reset_salt.size());
store_reset_seed = false;
// This field only applies to PinWeaver credentials.
serialized->mutable_key_data()->mutable_policy()->set_auth_locked(false);
}
AuthInput user_input = {vault_key, /*locked_to_single_user*=*/base::nullopt,
vault_key_salt, obfuscated_username, reset_secret};
KeyBlobs vkk_data;
CryptoError error;
auto auth_state = auth_block->Create(user_input, &vkk_data, &error);
if (auth_state == base::nullopt) {
LOG_IF(ERROR, !disable_logging_for_tests_)
<< "Failed to create the credential: " << error;
return false;
}
const SerializedVaultKeyset& out_serialized =
auth_state.value().vault_keyset.value();
serialized->set_flags(out_serialized.flags());
if (out_serialized.has_le_fek_iv()) {
serialized->set_le_fek_iv(out_serialized.le_fek_iv());
}
if (out_serialized.has_le_chaps_iv()) {
serialized->set_le_chaps_iv(out_serialized.le_chaps_iv());
}
if (out_serialized.has_le_label()) {
serialized->set_le_label(out_serialized.le_label());
}
if (out_serialized.has_tpm_key()) {
serialized->set_tpm_key(out_serialized.tpm_key());
}
if (out_serialized.has_tpm_public_key_hash()) {
serialized->set_tpm_public_key_hash(out_serialized.tpm_public_key_hash());
}
if (out_serialized.has_extended_tpm_key()) {
serialized->set_extended_tpm_key(out_serialized.extended_tpm_key());
}
if (!GenerateAndWrapKeys(vault_keyset, vkk_data, store_reset_seed,
serialized)) {
LOG(ERROR) << "GenerateAndWrapKeys failed.";
return false;
}
serialized->set_salt(vault_key_salt.data(), vault_key_salt.size());
return true;
}
bool Crypto::EncryptWithTpm(const SecureBlob& data,
std::string* encrypted_data) const {
SecureBlob aes_key;
SecureBlob sealed_key;
if (!CreateSealedKey(&aes_key, &sealed_key))
return false;
return EncryptData(data, aes_key, sealed_key, encrypted_data);
}
bool Crypto::DecryptWithTpm(const std::string& encrypted_data,
SecureBlob* data) const {
SecureBlob aes_key;
SecureBlob sealed_key;
if (!UnsealKey(encrypted_data, &aes_key, &sealed_key)) {
return false;
}
return DecryptData(encrypted_data, aes_key, data);
}
bool Crypto::CreateSealedKey(SecureBlob* aes_key,
SecureBlob* sealed_key) const {
if (!tpm_->GetRandomDataSecureBlob(kDefaultAesKeySize, aes_key)) {
LOG(ERROR) << "GetRandomDataSecureBlob failed.";
return false;
}
if (!tpm_->SealToPCR0(*aes_key, sealed_key)) {
LOG(ERROR) << "Failed to seal cipher key.";
return false;
}
return true;
}
bool Crypto::EncryptData(const SecureBlob& data,
const SecureBlob& aes_key,
const SecureBlob& sealed_key,
std::string* encrypted_data) const {
SecureBlob iv;
if (!tpm_->GetRandomDataSecureBlob(kAesBlockSize, &iv)) {
LOG(ERROR) << "GetRandomDataSecureBlob failed.";
return false;
}
SecureBlob encrypted_data_blob;
if (!CryptoLib::AesEncryptSpecifyBlockMode(
data, 0, data.size(), aes_key, iv, CryptoLib::kPaddingStandard,
CryptoLib::kCbc, &encrypted_data_blob)) {
LOG(ERROR) << "Failed to encrypt serial data.";
return false;
}
EncryptedData encrypted_pb;
encrypted_pb.set_wrapped_key(sealed_key.data(), sealed_key.size());
encrypted_pb.set_iv(iv.data(), iv.size());
encrypted_pb.set_encrypted_data(encrypted_data_blob.data(),
encrypted_data_blob.size());
encrypted_pb.set_mac(
CryptoLib::ComputeEncryptedDataHMAC(encrypted_pb, aes_key));
if (!encrypted_pb.SerializeToString(encrypted_data)) {
LOG(ERROR) << "Could not serialize data to string.";
return false;
}
return true;
}
bool Crypto::UnsealKey(const std::string& encrypted_data,
SecureBlob* aes_key,
SecureBlob* sealed_key) const {
EncryptedData encrypted_pb;
if (!encrypted_pb.ParseFromString(encrypted_data)) {
LOG(ERROR) << "Could not decrypt data as it was not an EncryptedData "
<< "protobuf";
return false;
}
SecureBlob tmp(encrypted_pb.wrapped_key().begin(),
encrypted_pb.wrapped_key().end());
sealed_key->swap(tmp);
if (!tpm_->Unseal(*sealed_key, aes_key)) {
LOG(ERROR) << "Cannot unseal aes key.";
return false;
}
return true;
}
bool Crypto::DecryptData(const std::string& encrypted_data,
const brillo::SecureBlob& aes_key,
brillo::SecureBlob* data) const {
EncryptedData encrypted_pb;
if (!encrypted_pb.ParseFromString(encrypted_data)) {
LOG(ERROR) << "Could not decrypt data as it was not an EncryptedData "
<< "protobuf";
return false;
}
std::string mac = CryptoLib::ComputeEncryptedDataHMAC(encrypted_pb, aes_key);
if (mac.length() != encrypted_pb.mac().length()) {
LOG(ERROR) << "Corrupted data in encrypted pb.";
return false;
}
if (0 != brillo::SecureMemcmp(mac.data(), encrypted_pb.mac().data(),
mac.length())) {
LOG(ERROR) << "Corrupted data in encrypted pb.";
return false;
}
SecureBlob iv(encrypted_pb.iv().begin(), encrypted_pb.iv().end());
SecureBlob encrypted_data_blob(encrypted_pb.encrypted_data().begin(),
encrypted_pb.encrypted_data().end());
if (!CryptoLib::AesDecryptSpecifyBlockMode(
encrypted_data_blob, 0, encrypted_data_blob.size(), aes_key, iv,
CryptoLib::kPaddingStandard, CryptoLib::kCbc, data)) {
LOG(ERROR) << "Failed to decrypt encrypted data.";
return false;
}
return true;
}
bool Crypto::ResetLECredential(const SerializedVaultKeyset& serialized_reset,
CryptoError* error,
const VaultKeyset& vk) const {
if (!tpm_)
return false;
// Bail immediately if we don't have a valid LECredentialManager.
if (!le_manager_) {
LOG(ERROR) << "Attempting to Reset LECredential on a platform that doesn't "
"support LECredential";
PopulateError(error, CryptoError::CE_LE_NOT_SUPPORTED);
return false;
}
CHECK(serialized_reset.flags() & SerializedVaultKeyset::LE_CREDENTIAL);
SecureBlob local_reset_seed(vk.reset_seed().begin(), vk.reset_seed().end());
SecureBlob reset_salt(serialized_reset.reset_salt().begin(),
serialized_reset.reset_salt().end());
if (local_reset_seed.empty() || reset_salt.empty()) {
LOG(ERROR) << "Reset seed/salt is empty, can't reset LE credential.";
PopulateError(error, CryptoError::CE_OTHER_FATAL);
return false;
}
SecureBlob reset_secret = CryptoLib::HmacSha256(reset_salt, local_reset_seed);
int ret =
le_manager_->ResetCredential(serialized_reset.le_label(), reset_secret);
if (ret != LE_CRED_SUCCESS) {
PopulateError(error, ret == LE_CRED_ERROR_INVALID_RESET_SECRET
? CryptoError::CE_LE_INVALID_SECRET
: CryptoError::CE_OTHER_FATAL);
return false;
}
return true;
}
int Crypto::GetWrongAuthAttempts(
const SerializedVaultKeyset& le_serialized) const {
DCHECK(le_manager_)
<< "le_manage_ doesn't exist when calling GetWrongAuthAttempts()";
return le_manager_->GetWrongAuthAttempts(le_serialized.le_label());
}
bool Crypto::RemoveLECredential(uint64_t label) const {
if (!tpm_) {
LOG(WARNING) << "No TPM instance for RemoveLECredential.";
return false;
}
// Bail immediately if we don't have a valid LECredentialManager.
if (!le_manager_) {
LOG(ERROR) << "No LECredentialManager instance for RemoveLECredential.";
return false;
}
return le_manager_->RemoveCredential(label) == LE_CRED_SUCCESS;
}
bool Crypto::is_cryptohome_key_loaded() const {
if (tpm_ == NULL || tpm_init_ == NULL) {
return false;
}
return tpm_init_->HasCryptohomeKey();
}
bool Crypto::CanUnsealWithUserAuth() const {
if (!tpm_)
return false;
if (tpm_->GetVersion() != Tpm::TPM_1_2)
return true;
if (!tpm_->DelegateCanResetDACounter())
return false;
base::Optional<bool> is_pcr_bound = tpm_->IsDelegateBoundToPcr();
if (is_pcr_bound.has_value() && !is_pcr_bound.value())
return true;
#if USE_DOUBLE_EXTEND_PCR_ISSUE
return false;
#else
return true;
#endif
}
std::unique_ptr<AuthBlock> Crypto::CreateAuthBlock(
const VaultKeyset& vk) const {
if (vk.IsLECredential()) {
return std::make_unique<PinWeaverAuthBlock>(le_manager_.get(), tpm_init_);
}
if (vk.IsSignatureChallengeProtected()) {
return std::make_unique<ChallengeCredentialAuthBlock>();
}
bool use_tpm = tpm_ && tpm_->IsOwned();
bool with_user_auth = CanUnsealWithUserAuth();
if (use_tpm && with_user_auth) {
return std::make_unique<TpmBoundToPcrAuthBlock>(tpm_, tpm_init_);
}
if (use_tpm && !with_user_auth) {
return std::make_unique<TpmNotBoundToPcrAuthBlock>(tpm_, tpm_init_);
}
return std::make_unique<LibScryptCompatAuthBlock>();
}
std::unique_ptr<AuthBlock> Crypto::DeriveAuthBlock(int serialized_key_flags) {
if (serialized_key_flags & SerializedVaultKeyset::LE_CREDENTIAL) {
return std::make_unique<PinWeaverAuthBlock>(le_manager_.get(), tpm_init_);
} else if (serialized_key_flags &
SerializedVaultKeyset::SIGNATURE_CHALLENGE_PROTECTED) {
return std::make_unique<ChallengeCredentialAuthBlock>();
} else if (serialized_key_flags & SerializedVaultKeyset::SCRYPT_WRAPPED &&
serialized_key_flags & SerializedVaultKeyset::TPM_WRAPPED) {
return std::make_unique<DoubleWrappedCompatAuthBlock>(tpm_, tpm_init_);
} else if (serialized_key_flags & SerializedVaultKeyset::TPM_WRAPPED) {
if (serialized_key_flags & SerializedVaultKeyset::PCR_BOUND) {
return std::make_unique<TpmBoundToPcrAuthBlock>(tpm_, tpm_init_);
} else {
return std::make_unique<TpmNotBoundToPcrAuthBlock>(tpm_, tpm_init_);
}
} else if (serialized_key_flags & SerializedVaultKeyset::SCRYPT_WRAPPED) {
return std::make_unique<LibScryptCompatAuthBlock>();
}
return nullptr;
}
} // namespace cryptohome