blob: 2250775de6dd44b320501dddd349dd8a09d4b6ef [file] [log] [blame]
cfroussios3b5a4e42016-05-31 11:02:181// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "components/os_crypt/key_storage_libsecret.h"
6
7#include "base/base64.h"
8#include "base/rand_util.h"
9#include "base/strings/string_number_conversions.h"
10#include "components/os_crypt/libsecret_util_linux.h"
11
12namespace {
13
cfroussios3b5a4e42016-05-31 11:02:1814const SecretSchema kKeystoreSchema = {
15 "chrome_libsecret_os_crypt_password",
16 SECRET_SCHEMA_NONE,
17 {
18 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING},
19 }};
20
cfroussios2e6729a42016-07-26 09:18:1221} // namespace
22
23std::string KeyStorageLibsecret::AddRandomPasswordInLibsecret() {
cfroussios3b5a4e42016-05-31 11:02:1824 std::string password;
25 base::Base64Encode(base::RandBytesAsString(16), &password);
26 GError* error = nullptr;
27 LibsecretLoader::secret_password_store_sync(
cfroussios2e6729a42016-07-26 09:18:1228 &kKeystoreSchema, nullptr, KeyStorageLinux::kKey, password.c_str(),
cfroussios3b5a4e42016-05-31 11:02:1829 nullptr, &error, nullptr);
30
31 if (error) {
32 VLOG(1) << "Libsecret lookup failed: " << error->message;
33 return std::string();
34 }
35 return password;
36}
37
cfroussios3b5a4e42016-05-31 11:02:1838std::string KeyStorageLibsecret::GetKey() {
39 GError* error = nullptr;
40 LibsecretAttributesBuilder attrs;
41 SecretValue* password_libsecret = LibsecretLoader::secret_service_lookup_sync(
42 nullptr, &kKeystoreSchema, attrs.Get(), nullptr, &error);
43
44 if (error) {
45 VLOG(1) << "Libsecret lookup failed: " << error->message;
46 g_error_free(error);
47 return std::string();
48 }
49 if (!password_libsecret) {
50 return AddRandomPasswordInLibsecret();
51 }
52 std::string password(
53 LibsecretLoader::secret_value_get_text(password_libsecret));
54 LibsecretLoader::secret_value_unref(password_libsecret);
55 return password;
56}
57
58bool KeyStorageLibsecret::Init() {
59 return LibsecretLoader::EnsureLibsecretLoaded();
60}