Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(280)

Unified Diff: net/base/x509_cert_types.cc

Issue 11274032: Separate http_security_headers from transport_security_state (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/base/x509_cert_types.cc
===================================================================
--- net/base/x509_cert_types.cc (revision 163343)
+++ net/base/x509_cert_types.cc (working copy)
@@ -7,10 +7,13 @@
#include <cstdlib>
#include <cstring>
+#include "base/base64.h"
#include "base/logging.h"
#include "base/sha1.h"
#include "base/string_number_conversions.h"
#include "base/string_piece.h"
+#include "base/string_split.h"
+#include "base/string_util.h"
#include "base/time.h"
#include "net/base/x509_certificate.h"
@@ -37,7 +40,6 @@
} // namespace
-// static
bool IsSHA1HashInSortedArray(const SHA1HashValue& hash,
const uint8* array,
size_t array_byte_len) {
@@ -47,6 +49,45 @@
CompareSHA1Hashes);
}
+bool HashesIntersect(const HashValueVector& a,
+ const HashValueVector& b) {
+ for (HashValueVector::const_iterator i = a.begin(); i != a.end(); ++i) {
+ HashValueVector::const_iterator j =
+ std::find_if(b.begin(), b.end(), HashValuesEqualPredicate(*i));
+ if (j != b.end())
+ return true;
+ }
+ return false;
+}
+
+std::string HashesToBase64String(const HashValueVector& hashes) {
+ std::string str;
+ for (size_t i = 0; i != hashes.size(); ++i) {
+ if (i != 0)
+ str += ",";
+ str += hashes[i].WriteBase64String();
+ }
+ return str;
+}
+
+bool Base64StringToHashes(const std::string& hashes_str,
+ HashValueVector* hashes) {
+ if (!hashes_str.empty()) {
Ryan Sleevi 2012/10/25 01:59:09 Rather than structuring code along the if (success
Ryan Sleevi 2012/10/25 01:59:09 design nit: You have not documented what the pre-a
unsafe 2012/10/25 06:59:54 Fixed - documented, clears |hashes|, returns true
+ std::vector<std::string> type_and_b64s;
+ base::SplitString(hashes_str, ',', &type_and_b64s);
+
+ for (size_t i = 0; i != type_and_b64s.size(); i++) {
Ryan Sleevi 2012/10/25 01:59:09 style nit: pre-increment (++i) http://google-styl
unsafe 2012/10/25 06:59:54 Done.
+ std::string type_and_b64;
+ RemoveChars(type_and_b64s[i], " \t\r\n", &type_and_b64);
Ryan Sleevi 2012/10/25 01:59:09 RemoveChars(type_and_b64s[i], " \t\r\n", &type_and
unsafe 2012/10/25 06:59:54 This function isn't processing the HTTP header, it
+ net::HashValue hash;
+ if (!hash.ParseBase64String(type_and_b64))
+ return false;
+ hashes->push_back(hash);
+ }
+ }
+ return true;
+}
+
CertPrincipal::CertPrincipal() {
}
@@ -157,6 +198,36 @@
}
}
+bool HashValue::ParseBase64String(const std::string& value) {
+ std::string b64;
Ryan Sleevi 2012/10/25 01:59:09 naming nit: b64 is too abbreviated. Applies throug
unsafe 2012/10/25 06:59:54 Changed the names.
+ if (value.substr(0, 5) == "sha1/") {
+ tag = HASH_VALUE_SHA1;
+ b64 = value.substr(5, 28); // length of base64 string
+ } else if (value.substr(0, 7) == "sha256/") {
+ tag = HASH_VALUE_SHA256;
+ b64 = value.substr(7, 44); // length of base64 string
+ } else {
+ return false;
+ }
+
+ std::string decoded;
+ if (!base::Base64Decode(b64, &decoded) || decoded.size() != size()) {
+ return false;
+ }
+ memcpy(data(), decoded.data(), size());
+ return true;
+}
+
+std::string HashValue::WriteBase64String() const {
+ std::string b64;
+ base::Base64Encode(std::string((const char*)data(), size()), &b64);
Ryan Sleevi 2012/10/25 01:59:09 design nit: Encode expects a base::StringPiece rat
unsafe 2012/10/25 06:59:54 Done.
+ if (tag == HASH_VALUE_SHA1)
+ return std::string("sha1/" + b64);
+ else if (tag == HASH_VALUE_SHA256)
+ return std::string("sha256/" + b64);
+ return std::string("unknown/" + b64);
Ryan Sleevi 2012/10/25 01:59:09 nit: Write this as a switch statement, with no "de
unsafe 2012/10/25 06:59:54 Changed to a switch. The existing code in this fi
+}
+
size_t HashValue::size() const {
switch (tag) {
case HASH_VALUE_SHA1:

Powered by Google App Engine
This is Rietveld 408576698