Verify AU payload manifest signature if present.

In order to support downloads over http for a number of reasons, we need
to secure http downloads. The first step in this process is to
verify the signature of the manifest itself before parsing. This can be
done even for https-based downloads in order to provide defense-in-depth
against a SSL attack. This CL adds the required verification logic in
update_engine, if such a manifest signature is present in the Omaha
response.

Until the delta generator is modified in a subsequent check-in to update
the manifest and payload with the required signature, none of this new
code will have any effect.

The delta generator change to populate non-zero values for these new
fields will follow in subsequent CLs.

BUG=chromium-os:33602
TEST=Tested on ZGB to make sure existing functionality works fine.
     Added new unit tests.
Change-Id: I2d8b09c23faf87049893b1dee97a34e1f300aded
Reviewed-on: https://gerrit.chromium.org/gerrit/32844
Commit-Ready: Jay Srinivasan <jaysri@chromium.org>
Reviewed-by: Jay Srinivasan <jaysri@chromium.org>
Tested-by: Jay Srinivasan <jaysri@chromium.org>
diff --git a/omaha_hash_calculator.cc b/omaha_hash_calculator.cc
index de70d10..9fe82a2 100644
--- a/omaha_hash_calculator.cc
+++ b/omaha_hash_calculator.cc
@@ -19,6 +19,38 @@
 
 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:
+  DISALLOW_COPY_AND_ASSIGN(ScopedBioHandle);
+  BIO* bio_;
+
+  void FreeCurrentBio() {
+    if (bio_) {
+      BIO_free_all(bio_);
+      bio_ = NULL;
+    }
+  }
+};
+
 OmahaHashCalculator::OmahaHashCalculator() : valid_(false) {
   valid_ = (SHA256_Init(&ctx_) == 1);
   LOG_IF(ERROR, !valid_) << "SHA256_Init failed";
@@ -69,10 +101,10 @@
   bool success = true;
   BIO *b64 = BIO_new(BIO_f_base64());
   if (!b64)
-    LOG(INFO) << "BIO_new(BIO_f_base64()) failed";
+    LOG(ERROR) << "BIO_new(BIO_f_base64()) failed";
   BIO *bmem = BIO_new(BIO_s_mem());
   if (!bmem)
-    LOG(INFO) << "BIO_new(BIO_s_mem()) failed";
+    LOG(ERROR) << "BIO_new(BIO_s_mem()) failed";
   if (b64 && bmem) {
     b64 = BIO_push(b64, bmem);
     success =
@@ -91,6 +123,33 @@
   return success;
 }
 
+bool OmahaHashCalculator::Base64Decode(const string& in,
+                                       vector<char>* out) {
+  ScopedBioHandle b64(BIO_new(BIO_f_base64()));
+  if (!b64.bio()) {
+    LOG(ERROR) << "Unable to create BIO object to decode base64 hash.";
+    return false;
+  }
+
+  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]);
+  }
+
+  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() {