Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(419)

Unified Diff: components/os_crypt/key_storage_linux.cc

Issue 1973483002: OSCrypt for POSIX uses libsecret to store a randomised encryption key. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactored CL Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/os_crypt/key_storage_linux.cc
diff --git a/components/os_crypt/key_storage_linux.cc b/components/os_crypt/key_storage_linux.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b95085969fce38984c7a6898639ed28db0f2b151
--- /dev/null
+++ b/components/os_crypt/key_storage_linux.cc
@@ -0,0 +1,89 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/os_crypt/key_storage_linux.h"
+
+#include "base/base64.h"
+#include "base/rand_util.h"
+#include "base/strings/string_number_conversions.h"
+#include "components/os_crypt/libsecret_util_posix.h"
+
+#ifdef OFFICIAL_BUILD
vabr (Chromium) 2016/05/13 15:10:17 I'm not sure we need to differentiate the name her
cfroussios 2016/05/13 17:09:11 This name is the name Seahorse uses to display the
+const char kKeyStorageEntryName[] = "Chrome Safe Storage";
+#else
+const char kKeyStorageEntryName[] = "Chromium Safe Storage";
+#endif
+
+// static
+std::unique_ptr<KeyStorage> KeyStorage::FindService() {
vabr (Chromium) 2016/05/13 15:10:17 nit: Here "Find" is a bit confusing. It sounds lik
cfroussios 2016/05/13 17:09:11 Done.
+ std::unique_ptr<KeyStorage> key_storage;
+
+ key_storage.reset(new KeyStorageLibsecret());
vabr (Chromium) 2016/05/13 15:10:17 nit: You can merge lines 20 and 22.
cfroussios 2016/05/13 17:09:11 I added this redundant expression to be symmetrica
+ if (key_storage->Init())
+ return key_storage;
+
+ return nullptr;
+}
+
+const SecretSchema kKeystoreSchema = {
+ "chrome_libsecret_os_crypt_password",
+ SECRET_SCHEMA_NONE,
+ {
+ {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING},
+ }};
+
+std::string AddRandomPasswordInLibsecret() {
vabr (Chromium) 2016/05/13 15:10:17 nit: Please put local (=not exported beyond this .
cfroussios 2016/05/13 17:09:11 Done.
+ std::string password;
+ base::Base64Encode(base::RandBytesAsString(128 / 8), &password);
+ GError* error = nullptr;
+ LibsecretLoader::secret_password_store_sync(
+ &kKeystoreSchema, nullptr, kKeyStorageEntryName, password.c_str(),
+ nullptr, &error, nullptr);
+
+ if (error) {
+ VLOG(1) << "Libsecret lookup failed: " << error->message;
+ return std::string();
+ }
+ return password;
+}
+
+std::string KeyStorageLibsecret::GetKey() {
+ GError* error = nullptr;
+ LibsecretAttributesBuilder attrs;
+ SecretValue* password_libsecret = LibsecretLoader::secret_service_lookup_sync(
+ nullptr, &kKeystoreSchema, attrs.Get(), nullptr, &error);
+
+ if (error) {
+ VLOG(1) << "Libsecret lookup failed: " << error->message;
+ g_error_free(error);
+ return "";
vabr (Chromium) 2016/05/13 15:10:17 "" -> std::string()
cfroussios 2016/05/13 17:09:11 Done.
+ } else if (password_libsecret == nullptr) {
+ return AddRandomPasswordInLibsecret();
+ } else {
+ std::string password(
+ LibsecretLoader::secret_value_get_text(password_libsecret));
+ LibsecretLoader::secret_value_unref(password_libsecret);
+ return password;
+ }
+}
+
+bool KeyStorageLibsecret::Init() {
+ return LibsecretLoader::EnsureLibsecretLoaded();
+}
+
+KeyStorageMock::KeyStorageMock(std::string in_key) : key(in_key) {}
vabr (Chromium) 2016/05/13 15:10:17 Either use std::move(in_key) or change the type of
cfroussios 2016/05/13 17:09:11 Done.
+
+std::string KeyStorageMock::GetKey() {
+ if (key == "")
vabr (Chromium) 2016/05/13 15:10:17 if (key_.empty()) (empty() is more efficient than
cfroussios 2016/05/13 17:09:11 Done.
+ base::Base64Encode(base::RandBytesAsString(128 / 8), &key);
+ return key;
+}
+
+bool KeyStorageMock::Init() {
+ return true;
+}
+
+void KeyStorageMock::ResetTo(std::string in_key) {
+ key = std::move(in_key);
+}

Powered by Google App Engine
This is Rietveld 408576698