Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / components / nacl / loader / nacl_validation_query.cc
blob0a3582e9dd9147e6c57bef23582b39c32d9dd683
1 // Copyright 2013 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.
5 #include "components/nacl/loader/nacl_validation_query.h"
7 #include "base/logging.h"
8 #include "components/nacl/loader/nacl_validation_db.h"
9 #include "crypto/nss_util.h"
10 #include "native_client/src/include/portability.h"
11 #include "native_client/src/public/nacl_file_info.h"
12 #include "native_client/src/trusted/validator/validation_cache.h"
14 NaClValidationQueryContext::NaClValidationQueryContext(
15 NaClValidationDB* db,
16 const std::string& profile_key,
17 const std::string& nacl_version)
18 : db_(db),
19 profile_key_(profile_key),
20 nacl_version_(nacl_version) {
22 // Sanity checks.
23 CHECK(profile_key.length() >= 8);
24 CHECK(nacl_version.length() >= 4);
27 NaClValidationQuery* NaClValidationQueryContext::CreateQuery() {
28 NaClValidationQuery* query = new NaClValidationQuery(db_, profile_key_);
29 // Changing the version effectively invalidates existing hashes.
30 query->AddData(nacl_version_);
31 return query;
34 bool NaClValidationQueryContext::ResolveFileToken(
35 struct NaClFileToken* file_token,
36 int32* fd,
37 std::string* path) {
38 // This should no longer be used.
39 CHECK(false);
40 return false;
43 NaClValidationQuery::NaClValidationQuery(NaClValidationDB* db,
44 const std::string& profile_key)
45 : state_(READY),
46 hasher_(crypto::HMAC::SHA256),
47 db_(db),
48 buffer_length_(0) {
49 // Without this line on Linux, HMAC::Init will instantiate a singleton that
50 // in turn attempts to open a file. Disabling this behavior avoids a ~70 ms
51 // stall the first time HMAC is used.
52 // This function is also called in nacl_helper_linux.cc, but nacl_helper may
53 // not be used in all cases.
54 // TODO(ncbray) remove when nacl_helper becomes the only code path.
55 // http://code.google.com/p/chromium/issues/detail?id=118263
56 #if defined(USE_NSS)
57 crypto::ForceNSSNoDBInit();
58 #endif
59 CHECK(hasher_.Init(profile_key));
62 void NaClValidationQuery::AddData(const char* data, size_t length) {
63 CHECK(state_ == READY);
64 CHECK(buffer_length_ <= sizeof(buffer_));
65 // Chrome's HMAC class doesn't support incremental signing. Work around
66 // this by using a (small) temporary buffer to accumulate data.
67 // Check if there is space in the buffer.
68 if (buffer_length_ + kDigestLength > sizeof(buffer_)) {
69 // Hash the buffer to make space.
70 CompressBuffer();
72 // Hash the input data into the buffer. Assumes that sizeof(buffer_) >=
73 // kDigestLength * 2 (the buffer can store at least two digests.)
74 CHECK(hasher_.Sign(base::StringPiece(data, length),
75 reinterpret_cast<unsigned char*>(buffer_ + buffer_length_),
76 kDigestLength));
77 buffer_length_ += kDigestLength;
80 void NaClValidationQuery::AddData(const unsigned char* data, size_t length) {
81 AddData(reinterpret_cast<const char*>(data), length);
84 void NaClValidationQuery::AddData(const base::StringPiece& data) {
85 AddData(data.data(), data.length());
88 int NaClValidationQuery::QueryKnownToValidate() {
89 CHECK(state_ == READY);
90 // It is suspicious if we have less than a digest's worth of data.
91 CHECK(buffer_length_ >= kDigestLength);
92 CHECK(buffer_length_ <= sizeof(buffer_));
93 state_ = GET_CALLED;
94 // Ensure the buffer contains only one digest worth of data.
95 CompressBuffer();
96 return db_->QueryKnownToValidate(std::string(buffer_, buffer_length_));
99 void NaClValidationQuery::SetKnownToValidate() {
100 CHECK(state_ == GET_CALLED);
101 CHECK(buffer_length_ == kDigestLength);
102 state_ = SET_CALLED;
103 db_->SetKnownToValidate(std::string(buffer_, buffer_length_));
106 // Reduce the size of the data in the buffer by hashing it and writing it back
107 // to the buffer.
108 void NaClValidationQuery::CompressBuffer() {
109 // Calculate the digest into a temp buffer. It is likely safe to calculate it
110 // directly back into the buffer, but this is an "accidental" semantic we're
111 // avoiding depending on.
112 unsigned char temp[kDigestLength];
113 CHECK(hasher_.Sign(base::StringPiece(buffer_, buffer_length_), temp,
114 kDigestLength));
115 memcpy(buffer_, temp, kDigestLength);
116 buffer_length_ = kDigestLength;
119 // OO wrappers
121 static void* CreateQuery(void* handle) {
122 return static_cast<NaClValidationQueryContext*>(handle)->CreateQuery();
125 static void AddData(void* query, const uint8* data, size_t length) {
126 static_cast<NaClValidationQuery*>(query)->AddData(data, length);
129 static int QueryKnownToValidate(void* query) {
130 return static_cast<NaClValidationQuery*>(query)->QueryKnownToValidate();
133 static void SetKnownToValidate(void* query) {
134 static_cast<NaClValidationQuery*>(query)->SetKnownToValidate();
137 static void DestroyQuery(void* query) {
138 delete static_cast<NaClValidationQuery*>(query);
141 static int ResolveFileToken(void* handle, struct NaClFileToken* file_token,
142 int32* fd, char** file_path,
143 uint32* file_path_length) {
144 std::string path;
145 *file_path = NULL;
146 *file_path_length = 0;
147 bool ok = static_cast<NaClValidationQueryContext*>(handle)->
148 ResolveFileToken(file_token, fd, &path);
149 if (ok) {
150 *file_path = static_cast<char*>(malloc(path.length() + 1));
151 CHECK(*file_path);
152 memcpy(*file_path, path.data(), path.length());
153 (*file_path)[path.length()] = 0;
154 *file_path_length = static_cast<uint32>(path.length());
156 return ok;
159 struct NaClValidationCache* CreateValidationCache(
160 NaClValidationDB* db, const std::string& profile_key,
161 const std::string& nacl_version) {
162 NaClValidationCache* cache =
163 static_cast<NaClValidationCache*>(malloc(sizeof(NaClValidationCache)));
164 // Make sure any fields introduced in a cross-repo change are zeroed.
165 memset(cache, 0, sizeof(*cache));
166 cache->handle = new NaClValidationQueryContext(db, profile_key, nacl_version);
167 cache->CreateQuery = CreateQuery;
168 cache->AddData = AddData;
169 cache->QueryKnownToValidate = QueryKnownToValidate;
170 cache->SetKnownToValidate = SetKnownToValidate;
171 cache->DestroyQuery = DestroyQuery;
172 cache->ResolveFileToken = ResolveFileToken;
173 return cache;