platform2: Switch over to using base64 functions from libchromeos
Replaced existing implementations of Base64Encode/Base64Decode
with the functions from libchromeos, which were added as part
of an earlier change (see CL:247690).
BUG=None
TEST=`FEATURES=test emerge-link cryptohome debugd metrics privetd update_engine`
Change-Id: I8cec677ce2c2fd3b97ca2228d35c2cf5cd133f4c
Reviewed-on: https://chromium-review.googlesource.com/247792
Reviewed-by: Vitaly Buka <[email protected]>
Tested-by: Alex Vakulenko <[email protected]>
Commit-Queue: Alex Vakulenko <[email protected]>
diff --git a/delta_performer.cc b/delta_performer.cc
index 280913c..bcbfb46 100644
--- a/delta_performer.cc
+++ b/delta_performer.cc
@@ -17,6 +17,7 @@
#include <base/format_macros.h>
#include <base/strings/string_util.h>
#include <base/strings/stringprintf.h>
+#include <chromeos/data_encoding.h>
#include <google/protobuf/repeated_field.h>
#include "update_engine/bzip_extent_writer.h"
@@ -272,15 +273,10 @@
namespace {
void LogPartitionInfoHash(const PartitionInfo& info, const string& tag) {
- string sha256;
- if (OmahaHashCalculator::Base64Encode(info.hash().data(),
- info.hash().size(),
- &sha256)) {
- LOG(INFO) << "PartitionInfo " << tag << " sha256: " << sha256
- << " size: " << info.size();
- } else {
- LOG(ERROR) << "Base64Encode failed for tag: " << tag;
- }
+ string sha256 = chromeos::data_encoding::Base64Encode(info.hash().data(),
+ info.hash().size());
+ LOG(INFO) << "PartitionInfo " << tag << " sha256: " << sha256
+ << " size: " << info.size();
}
void LogPartitionInfo(const DeltaArchiveManifest& manifest) {
@@ -884,13 +880,14 @@
}
// Convert base64-encoded signature to raw bytes.
- vector<char> metadata_signature;
- if (!OmahaHashCalculator::Base64Decode(install_plan_->metadata_signature,
- &metadata_signature)) {
+ chromeos::Blob signature;
+ if (!chromeos::data_encoding::Base64Decode(install_plan_->metadata_signature,
+ &signature)) {
LOG(ERROR) << "Unable to decode base64 metadata signature: "
<< install_plan_->metadata_signature;
return ErrorCode::kDownloadMetadataSignatureError;
}
+ vector<char> metadata_signature{signature.begin(), signature.end()};
// See if we should use the public RSA key in the Omaha response.
base::FilePath path_to_public_key(public_key_path_);
@@ -1180,11 +1177,7 @@
}
string StringForHashBytes(const void* bytes, size_t size) {
- string ret;
- if (!OmahaHashCalculator::Base64Encode(bytes, size, &ret)) {
- ret = "<unknown>";
- }
- return ret;
+ return chromeos::data_encoding::Base64Encode(bytes, size);
}
} // namespace
diff --git a/omaha_hash_calculator.cc b/omaha_hash_calculator.cc
index 9899d59..49f2dae 100644
--- a/omaha_hash_calculator.cc
+++ b/omaha_hash_calculator.cc
@@ -8,9 +8,7 @@
#include <base/logging.h>
#include <base/posix/eintr_wrapper.h>
-#include <openssl/bio.h>
-#include <openssl/buffer.h>
-#include <openssl/evp.h>
+#include <chromeos/data_encoding.h>
#include "update_engine/utils.h"
@@ -19,40 +17,6 @@
namespace chromeos_update_engine {
-// Helper class to free a BIO structure when a method goes out of scope.
-class ScopedBioHandle {
- public:
- explicit ScopedBioHandle(BIO* bio) : bio_(bio) {}
- ~ScopedBioHandle() {
- FreeCurrentBio();
- }
-
- void set_bio(BIO* bio) {
- if (bio_ != bio) {
- // Free the current bio, but only if the caller is not trying to set
- // the same bio object again, so that the operation can be idempotent.
- FreeCurrentBio();
- }
- bio_ = bio;
- }
-
- BIO* bio() {
- return bio_;
- }
-
- private:
- BIO* bio_;
-
- void FreeCurrentBio() {
- if (bio_) {
- BIO_free_all(bio_);
- bio_ = nullptr;
- }
- }
-
- DISALLOW_COPY_AND_ASSIGN(ScopedBioHandle);
-};
-
OmahaHashCalculator::OmahaHashCalculator() : valid_(false) {
valid_ = (SHA256_Init(&ctx_) == 1);
LOG_IF(ERROR, !valid_) << "SHA256_Init failed";
@@ -97,76 +61,6 @@
return bytes_processed;
}
-bool OmahaHashCalculator::Base64Encode(const void* data,
- size_t size,
- string* out) {
- bool success = true;
- BIO *b64 = BIO_new(BIO_f_base64());
- if (!b64)
- LOG(ERROR) << "BIO_new(BIO_f_base64()) failed";
- BIO *bmem = BIO_new(BIO_s_mem());
- if (!bmem)
- LOG(ERROR) << "BIO_new(BIO_s_mem()) failed";
- if (b64 && bmem) {
- b64 = BIO_push(b64, bmem);
- success =
- (BIO_write(b64, data, size) == static_cast<int>(size));
- if (success)
- success = (BIO_flush(b64) == 1);
-
- BUF_MEM *bptr = nullptr;
- BIO_get_mem_ptr(b64, &bptr);
- out->assign(bptr->data, bptr->length - 1);
- }
- if (b64) {
- BIO_free_all(b64);
- b64 = nullptr;
- }
- return success;
-}
-
-bool OmahaHashCalculator::Base64Decode(const string& raw_in,
- vector<char>* out) {
- out->clear();
-
- ScopedBioHandle b64(BIO_new(BIO_f_base64()));
- if (!b64.bio()) {
- LOG(ERROR) << "Unable to create BIO object to decode base64 hash";
- return false;
- }
-
- // Canonicalize the raw input to get rid of all newlines in the string
- // and set the NO_NL flag so that BIO_read decodes properly. Otherwise
- // BIO_read would just return 0 without decode anything.
- string in;
- for (size_t i = 0; i < raw_in.size(); i++)
- if (raw_in[i] != '\n')
- in.push_back(raw_in[i]);
-
- BIO_set_flags(b64.bio(), BIO_FLAGS_BASE64_NO_NL);
-
- BIO *bmem = BIO_new_mem_buf(const_cast<char*>(in.c_str()), in.size());
- if (!bmem) {
- LOG(ERROR) << "Unable to get BIO buffer to decode base64 hash";
- return false;
- }
-
- b64.set_bio(BIO_push(b64.bio(), bmem));
-
- const int kOutBufferSize = 1024;
- char out_buffer[kOutBufferSize];
- int num_bytes_read = 1; // any non-zero value is fine to enter the loop.
- while (num_bytes_read > 0) {
- num_bytes_read = BIO_read(b64.bio(), &out_buffer, kOutBufferSize);
- for (int i = 0; i < num_bytes_read; i++)
- out->push_back(out_buffer[i]);
- }
-
- LOG(INFO) << "Decoded " << out->size()
- << " bytes from " << in.size() << " base64-encoded bytes";
- return true;
-}
-
// Call Finalize() when all data has been passed in. This mostly just
// calls OpenSSL's SHA256_Final() and then base64 encodes the hash.
bool OmahaHashCalculator::Finalize() {
@@ -178,7 +72,9 @@
&ctx_) == 1);
// Convert raw_hash_ to base64 encoding and store it in hash_.
- return Base64Encode(&raw_hash_[0], raw_hash_.size(), &hash_);
+ hash_ = chromeos::data_encoding::Base64Encode(raw_hash_.data(),
+ raw_hash_.size());
+ return true;
}
bool OmahaHashCalculator::RawHashOfBytes(const char* data,
diff --git a/omaha_hash_calculator.h b/omaha_hash_calculator.h
index b9bfa4f..4689bf7 100644
--- a/omaha_hash_calculator.h
+++ b/omaha_hash_calculator.h
@@ -75,15 +75,6 @@
static std::string OmahaHashOfString(const std::string& str);
static std::string OmahaHashOfData(const std::vector<char>& data);
- // Encodes data of given size as a base64 out string
- static bool Base64Encode(const void* data, size_t size, std::string* out);
-
- // Decodes given base64-encoded in string into the out vector. Since the
- // output can have null characters, we're returning a byte vector instead of
- // a string. This method works fine even if |raw_in| has any newlines.
- // Any existing contents of |out| will be erased.
- static bool Base64Decode(const std::string& raw_in, std::vector<char>* out);
-
private:
// If non-empty, the final base64 encoded hash and the raw hash. Will only be
// set to non-empty when Finalize is called.
diff --git a/payload_generator/payload_signer.cc b/payload_generator/payload_signer.cc
index a33c24f..f3cc4de 100644
--- a/payload_generator/payload_signer.cc
+++ b/payload_generator/payload_signer.cc
@@ -7,6 +7,7 @@
#include <base/logging.h>
#include <base/strings/string_split.h>
#include <base/strings/string_util.h>
+#include <chromeos/data_encoding.h>
#include <openssl/pem.h>
#include "update_engine/omaha_hash_calculator.h"
@@ -317,9 +318,8 @@
private_key_path,
&signature));
- TEST_AND_RETURN_FALSE(OmahaHashCalculator::Base64Encode(&signature[0],
- signature.size(),
- out_signature));
+ *out_signature = chromeos::data_encoding::Base64Encode(signature.data(),
+ signature.size());
return true;
}
diff --git a/utils.cc b/utils.cc
index 9a596a7..2007e9b 100644
--- a/utils.cc
+++ b/utils.cc
@@ -36,6 +36,7 @@
#include <base/strings/string_split.h>
#include <base/strings/string_util.h>
#include <base/strings/stringprintf.h>
+#include <chromeos/data_encoding.h>
#include <glib.h>
#include "update_engine/clock_interface.h"
@@ -1463,18 +1464,15 @@
}
string CalculateP2PFileId(const string& payload_hash, size_t payload_size) {
- string encoded_hash;
- OmahaHashCalculator::Base64Encode(payload_hash.c_str(),
- payload_hash.size(),
- &encoded_hash);
+ string encoded_hash = chromeos::data_encoding::Base64Encode(payload_hash);
return base::StringPrintf("cros_update_size_%zu_hash_%s",
- payload_size,
- encoded_hash.c_str());
+ payload_size,
+ encoded_hash.c_str());
}
bool DecodeAndStoreBase64String(const string& base64_encoded,
base::FilePath *out_path) {
- vector<char> contents;
+ chromeos::Blob contents;
out_path->clear();
@@ -1483,7 +1481,7 @@
return false;
}
- if (!OmahaHashCalculator::Base64Decode(base64_encoded, &contents) ||
+ if (!chromeos::data_encoding::Base64Decode(base64_encoded, &contents) ||
contents.size() == 0) {
LOG(ERROR) << "Error decoding base64.";
return false;
@@ -1495,7 +1493,7 @@
return false;
}
- if (fwrite(&contents[0], 1, contents.size(), file) != contents.size()) {
+ if (fwrite(contents.data(), 1, contents.size(), file) != contents.size()) {
PLOG(ERROR) << "Error writing to temporary file.";
if (fclose(file) != 0)
PLOG(ERROR) << "Error closing temporary file.";