Chromium Code Reviews| 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 |