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

Unified Diff: Source/core/frame/SubresourceIntegrity.cpp

Issue 566083003: Implementation of subresource integrity attribute for secure origins. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Block resources at insecure origins Created 6 years, 3 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: Source/core/frame/SubresourceIntegrity.cpp
diff --git a/Source/core/frame/SubresourceIntegrity.cpp b/Source/core/frame/SubresourceIntegrity.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..fd4f3d97c40c2b6d828de51ee737d3adf1c9b61e
--- /dev/null
+++ b/Source/core/frame/SubresourceIntegrity.cpp
@@ -0,0 +1,51 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "core/frame/SubresourceIntegrity.h"
+
+#include "platform/Crypto.h"
+#include "public/platform/WebCrypto.h"
+#include "public/platform/WebCryptoAlgorithm.h"
+#include "wtf/text/Base64.h"
+#include "wtf/text/StringUTF8Adaptor.h"
+#include "wtf/text/WTFString.h"
+
+namespace blink {
+
+static bool DigestsEqual(const DigestValue& digest1, const DigestValue& digest2)
+{
+ if (digest1.size() != digest2.size())
+ return false;
+
+ for (size_t i = 0; i < digest1.size(); i++) {
+ if (digest1[i] != digest2[i])
+ return false;
+ }
+
+ return true;
+}
+
+// TODO(jww) If CheckSubresourceIntegrity fails, Blink should create a console
+// message to alert the developer of the failure.
+bool SubresourceIntegrity::CheckSubresourceIntegrity(const String& source, const String& integrity)
+{
+ Vector<char> hashVector;
+ base64Decode(integrity, hashVector);
Mike West 2014/09/13 03:44:31 Please add some TODOs about parsing the `ni:///` s
+
+ StringUTF8Adaptor normalizedSource(source, StringUTF8Adaptor::Normalize, WTF::EntitiesForUnencodables);
+
+ DigestValue digest;
+ bool digestSuccess = computeDigest(HashAlgorithmSha256, normalizedSource.data(), normalizedSource.length(), digest);
+
+ if (digestSuccess) {
+ DigestValue convertedHashVector;
+ convertedHashVector.append(reinterpret_cast<uint8_t*>(hashVector.data()), hashVector.size());
+ return DigestsEqual(digest, convertedHashVector);
+ }
+
+ return false;
+}
+
+} // namespace blink
« Source/core/dom/ScriptLoader.cpp ('K') | « Source/core/frame/SubresourceIntegrity.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698