Bug 1882593 [wpt PR 44836] - Add test for unknown, invalid ancillary chunk which...
[gecko.git] / dom / security / SRICheck.h
blob3efacf41a1122199225c380b2cf0bf6b11885f25
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_SRICheck_h
8 #define mozilla_dom_SRICheck_h
10 #include "nsTString.h"
11 #include "nsStringFwd.h"
12 #include "nsCOMPtr.h"
13 #include "nsICryptoHash.h"
15 class nsIChannel;
16 class nsIConsoleReportCollector;
18 namespace mozilla::dom {
20 class SRIMetadata;
22 class SRICheck final {
23 public:
24 /**
25 * Parse the multiple hashes specified in the integrity attribute and
26 * return the strongest supported hash.
28 static nsresult IntegrityMetadata(const nsAString& aMetadataList,
29 const nsACString& aSourceFileURI,
30 nsIConsoleReportCollector* aReporter,
31 SRIMetadata* outMetadata);
34 // The SRICheckDataVerifier can be used in 2 different mode:
36 // 1. The streaming mode involves reading bytes from an input, and to use
37 // the |Update| function to stream new bytes, and to use the |Verify|
38 // function to check the hash of the content with the hash provided by
39 // the metadata.
41 // Optionally, one can serialize the verified hash with |ExportDataSummary|,
42 // in a buffer in order to rely on the second mode the next time.
44 // 2. The pre-computed mode, involves reading a hash with |ImportDataSummary|,
45 // which got exported by the SRICheckDataVerifier and potentially cached, and
46 // then use the |Verify| function to check against the hash provided by the
47 // metadata.
48 class SRICheckDataVerifier final {
49 public:
50 SRICheckDataVerifier(const SRIMetadata& aMetadata,
51 const nsACString& aSourceFileURI,
52 nsIConsoleReportCollector* aReporter);
54 // Append the following bytes to the content used to compute the hash. Once
55 // all bytes are streamed, use the Verify function to check the integrity.
56 nsresult Update(uint32_t aStringLen, const uint8_t* aString);
58 // Verify that the computed hash corresponds to the metadata.
59 nsresult Verify(const SRIMetadata& aMetadata, nsIChannel* aChannel,
60 const nsACString& aSourceFileURI,
61 nsIConsoleReportCollector* aReporter);
63 bool IsComplete() const { return mComplete; }
65 // Report the length of the computed hash and its type, such that we can
66 // reserve the space for encoding it in a vector.
67 uint32_t DataSummaryLength();
68 static uint32_t EmptyDataSummaryLength();
70 // Write the computed hash and its type in a pre-allocated buffer.
71 nsresult ExportDataSummary(uint32_t aDataLen, uint8_t* aData);
72 static nsresult ExportEmptyDataSummary(uint32_t aDataLen, uint8_t* aData);
74 // Report the length of the computed hash and its type, such that we can
75 // skip these data while reading a buffer.
76 static nsresult DataSummaryLength(uint32_t aDataLen, const uint8_t* aData,
77 uint32_t* length);
79 // Extract the computed hash and its type, such that we can |Verify| if it
80 // matches the metadata. The buffer should be at least the same size or
81 // larger than the value returned by |DataSummaryLength|.
82 nsresult ImportDataSummary(uint32_t aDataLen, const uint8_t* aData);
84 private:
85 nsCOMPtr<nsICryptoHash> mCryptoHash;
86 nsAutoCString mComputedHash;
87 size_t mBytesHashed;
88 uint32_t mHashLength;
89 int8_t mHashType;
90 bool mInvalidMetadata;
91 bool mComplete;
93 nsresult EnsureCryptoHash();
94 nsresult Finish();
95 nsresult VerifyHash(const SRIMetadata& aMetadata, uint32_t aHashIndex,
96 const nsACString& aSourceFileURI,
97 nsIConsoleReportCollector* aReporter);
100 } // namespace mozilla::dom
102 #endif // mozilla_dom_SRICheck_h