OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/nacl/nacl_validation_query.h" | 5 #include "chrome/nacl/nacl_validation_query.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "crypto/nss_util.h" | 8 #include "crypto/nss_util.h" |
9 #include "chrome/nacl/nacl_validation_db.h" | 9 #include "chrome/nacl/nacl_validation_db.h" |
10 #include "native_client/src/trusted/validator/validation_cache.h" | 10 #include "native_client/src/trusted/validator/validation_cache.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 // TODO(ncbray) remove when nacl_helper becomes the only code path. | 43 // TODO(ncbray) remove when nacl_helper becomes the only code path. |
44 // http://code.google.com/p/chromium/issues/detail?id=118263 | 44 // http://code.google.com/p/chromium/issues/detail?id=118263 |
45 #if defined(USE_NSS) | 45 #if defined(USE_NSS) |
46 crypto::ForceNSSNoDBInit(); | 46 crypto::ForceNSSNoDBInit(); |
47 #endif | 47 #endif |
48 CHECK(hasher_.Init(profile_key)); | 48 CHECK(hasher_.Init(profile_key)); |
49 } | 49 } |
50 | 50 |
51 void NaClValidationQuery::AddData(const char* data, size_t length) { | 51 void NaClValidationQuery::AddData(const char* data, size_t length) { |
52 CHECK(state_ == READY); | 52 CHECK(state_ == READY); |
53 CHECK(buffer_length_ >= 0); | 53 CHECK(buffer_length_ <= sizeof(buffer_)); |
54 CHECK(buffer_length_ <= (int) sizeof(buffer_)); | |
55 // Chrome's HMAC class doesn't support incremental signing. Work around | 54 // Chrome's HMAC class doesn't support incremental signing. Work around |
56 // this by using a (small) temporary buffer to accumulate data. | 55 // this by using a (small) temporary buffer to accumulate data. |
57 // Check if there is space in the buffer. | 56 // Check if there is space in the buffer. |
58 if (buffer_length_ + kDigestLength > (int) sizeof(buffer_)) { | 57 if (buffer_length_ + kDigestLength > sizeof(buffer_)) { |
59 // Hash the buffer to make space. | 58 // Hash the buffer to make space. |
60 CompressBuffer(); | 59 CompressBuffer(); |
61 } | 60 } |
62 // Hash the input data into the buffer. Assumes that sizeof(buffer_) >= | 61 // Hash the input data into the buffer. Assumes that sizeof(buffer_) >= |
63 // kDigestLength * 2 (the buffer can store at least two digests.) | 62 // kDigestLength * 2 (the buffer can store at least two digests.) |
64 CHECK(hasher_.Sign(base::StringPiece(data, length), | 63 CHECK(hasher_.Sign(base::StringPiece(data, length), |
65 reinterpret_cast<unsigned char*>(buffer_ + buffer_length_), | 64 reinterpret_cast<unsigned char*>(buffer_ + buffer_length_), |
66 kDigestLength)); | 65 kDigestLength)); |
67 buffer_length_ += kDigestLength; | 66 buffer_length_ += kDigestLength; |
68 } | 67 } |
69 | 68 |
70 void NaClValidationQuery::AddData(const unsigned char* data, size_t length) { | 69 void NaClValidationQuery::AddData(const unsigned char* data, size_t length) { |
71 AddData(reinterpret_cast<const char*>(data), length); | 70 AddData(reinterpret_cast<const char*>(data), length); |
72 } | 71 } |
73 | 72 |
74 void NaClValidationQuery::AddData(const base::StringPiece& data) { | 73 void NaClValidationQuery::AddData(const base::StringPiece& data) { |
75 AddData(data.data(), data.length()); | 74 AddData(data.data(), data.length()); |
76 } | 75 } |
77 | 76 |
78 int NaClValidationQuery::QueryKnownToValidate() { | 77 int NaClValidationQuery::QueryKnownToValidate() { |
79 CHECK(state_ == READY); | 78 CHECK(state_ == READY); |
80 // It is suspicious if we have less than a digest's worth of data. | 79 // It is suspicious if we have less than a digest's worth of data. |
81 CHECK(buffer_length_ >= kDigestLength); | 80 CHECK(buffer_length_ >= kDigestLength); |
82 CHECK(buffer_length_ <= (int) sizeof(buffer_)); | 81 CHECK(buffer_length_ <= sizeof(buffer_)); |
83 state_ = GET_CALLED; | 82 state_ = GET_CALLED; |
84 // Ensure the buffer contains only one digest worth of data. | 83 // Ensure the buffer contains only one digest worth of data. |
85 CompressBuffer(); | 84 CompressBuffer(); |
86 return db_->QueryKnownToValidate(std::string(buffer_, buffer_length_)); | 85 return db_->QueryKnownToValidate(std::string(buffer_, buffer_length_)); |
87 } | 86 } |
88 | 87 |
89 void NaClValidationQuery::SetKnownToValidate() { | 88 void NaClValidationQuery::SetKnownToValidate() { |
90 CHECK(state_ == GET_CALLED); | 89 CHECK(state_ == GET_CALLED); |
91 CHECK(buffer_length_ == kDigestLength); | 90 CHECK(buffer_length_ == kDigestLength); |
92 state_ = SET_CALLED; | 91 state_ = SET_CALLED; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 // Make sure any fields introduced in a cross-repo change are zeroed. | 135 // Make sure any fields introduced in a cross-repo change are zeroed. |
137 memset(cache, 0, sizeof(*cache)); | 136 memset(cache, 0, sizeof(*cache)); |
138 cache->handle = new NaClValidationQueryContext(db, profile_key, nacl_version); | 137 cache->handle = new NaClValidationQueryContext(db, profile_key, nacl_version); |
139 cache->CreateQuery = CreateQuery; | 138 cache->CreateQuery = CreateQuery; |
140 cache->AddData = AddData; | 139 cache->AddData = AddData; |
141 cache->QueryKnownToValidate = QueryKnownToValidate; | 140 cache->QueryKnownToValidate = QueryKnownToValidate; |
142 cache->SetKnownToValidate = SetKnownToValidate; | 141 cache->SetKnownToValidate = SetKnownToValidate; |
143 cache->DestroyQuery = DestroyQuery; | 142 cache->DestroyQuery = DestroyQuery; |
144 return cache; | 143 return cache; |
145 } | 144 } |
OLD | NEW |