File: key_storage_libsecret.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (160 lines) | stat: -rw-r--r-- 5,215 bytes parent folder | download
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
// 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_libsecret.h"

#include "base/base64.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "components/os_crypt/libsecret_util_linux.h"

namespace {

#if defined(GOOGLE_CHROME_BUILD)
const char kApplicationName[] = "chrome";
#else
const char kApplicationName[] = "chromium";
#endif

// Deprecated in M55 (crbug.com/639298)
const SecretSchema kKeystoreSchemaV1 = {
    "chrome_libsecret_os_crypt_password",
    SECRET_SCHEMA_NONE,
    {
        {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING},
    }};

const SecretSchema kKeystoreSchemaV2 = {
    "chrome_libsecret_os_crypt_password_v2",
    SECRET_SCHEMA_DONT_MATCH_NAME,
    {
        {"application", SECRET_SCHEMA_ATTRIBUTE_STRING},
        {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING},
    }};

// From a search result, extracts a SecretValue, asserting that there was at
// most one result. Returns nullptr if there are no results.
SecretValue* ToSingleSecret(GList* secret_items) {
  GList* first = g_list_first(secret_items);
  if (first == nullptr)
    return nullptr;
  if (g_list_next(first) != nullptr) {
    VLOG(1) << "OSCrypt found more than one encryption keys.";
  }
  SecretItem* secret_item = static_cast<SecretItem*>(first->data);
  SecretValue* secret_value =
      LibsecretLoader::secret_item_get_secret(secret_item);
  g_list_free(secret_items);
  return secret_value;
}

}  // namespace

std::string KeyStorageLibsecret::AddRandomPasswordInLibsecret() {
  std::string password;
  base::Base64Encode(base::RandBytesAsString(16), &password);
  GError* error = nullptr;
  bool success = LibsecretLoader::secret_password_store_sync(
      &kKeystoreSchemaV2, nullptr, KeyStorageLinux::kKey, password.c_str(),
      nullptr, &error, "application", kApplicationName, nullptr);
  if (error) {
    VLOG(1) << "Libsecret lookup failed: " << error->message;
    g_error_free(error);
    return std::string();
  }
  if (!success) {
    VLOG(1) << "Libsecret lookup failed.";
    return std::string();
  }

  VLOG(1) << "OSCrypt generated a new password.";
  return password;
}

std::string KeyStorageLibsecret::GetKey() {
  GError* error = nullptr;
  LibsecretAttributesBuilder attrs;
  attrs.Append("application", kApplicationName);
  GList* search_results = LibsecretLoader::secret_service_search_sync(
      nullptr /* default secret service */, &kKeystoreSchemaV2, attrs.Get(),
      static_cast<SecretSearchFlags>(SECRET_SEARCH_UNLOCK |
                                     SECRET_SEARCH_LOAD_SECRETS),
      nullptr, &error);
  if (error) {
    VLOG(1) << "Libsecret lookup failed: " << error->message;
    g_error_free(error);
    return std::string();
  }
  SecretValue* password_libsecret = ToSingleSecret(search_results);
  if (!password_libsecret) {
    std::string password = Migrate();
    if (!password.empty())
      return password;
    return AddRandomPasswordInLibsecret();
  }
  std::string password(
      LibsecretLoader::secret_value_get_text(password_libsecret));
  LibsecretLoader::secret_value_unref(password_libsecret);
  return password;
}

bool KeyStorageLibsecret::Init() {
  bool loaded = LibsecretLoader::EnsureLibsecretLoaded();
  if (loaded)
    LibsecretLoader::EnsureKeyringUnlocked();
  return loaded;
}

std::string KeyStorageLibsecret::Migrate() {
  GError* error = nullptr;
  LibsecretAttributesBuilder attrs;

  // Detect old entry.
  GList* search_results = LibsecretLoader::secret_service_search_sync(
      nullptr /* default secret service */, &kKeystoreSchemaV1, attrs.Get(),
      static_cast<SecretSearchFlags>(SECRET_SEARCH_UNLOCK |
                                     SECRET_SEARCH_LOAD_SECRETS),
      nullptr, &error);
  if (error) {
    g_error_free(error);
    g_list_free(search_results);
    return std::string();
  }
  SecretValue* password_libsecret = ToSingleSecret(search_results);
  if (!password_libsecret)
    return std::string();

  VLOG(1) << "OSCrypt detected a deprecated password in Libsecret.";
  std::string password(
      LibsecretLoader::secret_value_get_text(password_libsecret));
  LibsecretLoader::secret_value_unref(password_libsecret);

  // Create new entry.
  bool success = LibsecretLoader::secret_password_store_sync(
      &kKeystoreSchemaV2, nullptr, KeyStorageLinux::kKey, password.c_str(),
      nullptr, &error, "application", kApplicationName, nullptr);
  if (error) {
    VLOG(1) << "Failed to store migrated password. " << error->message;
    g_error_free(error);
    return std::string();
  }
  if (!success) {
    VLOG(1) << "Failed to store migrated password.";
    return std::string();
  }

  // Delete old entry.
  // Even if deletion failed, we have to use the password that we created.
  success = LibsecretLoader::secret_password_clear_sync(
      &kKeystoreSchemaV1, nullptr, &error, nullptr);
  if (error) {
    VLOG(1) << "OSCrypt failed to delete deprecated password. "
            << error->message;
    g_error_free(error);
  }

  VLOG(1) << "OSCrypt migrated from deprecated password.";

  return password;
}