tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SRIMetadata.h (2783B)


      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/. */
      6 
      7 #ifndef mozilla_dom_SRIMetadata_h
      8 #define mozilla_dom_SRIMetadata_h
      9 
     10 #include "SRICheck.h"
     11 #include "nsString.h"
     12 #include "nsTArray.h"
     13 
     14 namespace mozilla::dom {
     15 
     16 class SRIMetadata final {
     17  friend class SRICheck;
     18 
     19 public:
     20  static const uint32_t MAX_ALTERNATE_HASHES = 256;
     21  static const int8_t UNKNOWN_ALGORITHM = -1;
     22 
     23  /**
     24   * Create an empty metadata object.
     25   */
     26  SRIMetadata() : mAlgorithmType(UNKNOWN_ALGORITHM), mEmpty(true) {}
     27 
     28  /**
     29   * Split a string token into the components of an SRI metadata
     30   * attribute.
     31   */
     32  explicit SRIMetadata(const nsACString& aToken);
     33 
     34  /**
     35   * Returns true when this object's hash algorithm is weaker than the
     36   * other object's hash algorithm.
     37   */
     38  bool operator<(const SRIMetadata& aOther) const;
     39 
     40  /**
     41   * Not implemented. Should not be used.
     42   */
     43  bool operator>(const SRIMetadata& aOther) const;
     44 
     45  /**
     46   * Add another metadata's hash to this one.
     47   */
     48  SRIMetadata& operator+=(const SRIMetadata& aOther);
     49 
     50  /**
     51   * Returns true when the two metadata use the same hash algorithm.
     52   */
     53  bool operator==(const SRIMetadata& aOther) const;
     54 
     55  bool IsEmpty() const { return mEmpty; }
     56  bool IsMalformed() const { return mHashes.IsEmpty() || mAlgorithm.IsEmpty(); }
     57  bool IsAlgorithmSupported() const {
     58    return mAlgorithmType != UNKNOWN_ALGORITHM;
     59  }
     60  bool IsValid() const { return !IsMalformed() && IsAlgorithmSupported(); }
     61 
     62  uint32_t HashCount() const { return mHashes.Length(); }
     63  void GetHash(uint32_t aIndex, nsCString* outHash) const;
     64  void GetAlgorithm(nsCString* outAlg) const { *outAlg = mAlgorithm; }
     65  void GetHashType(int8_t* outType, uint32_t* outLength) const;
     66 
     67  const nsString& GetIntegrityString() const { return mIntegrityString; }
     68 
     69  // Return true if:
     70  // - this integrity metadata is empty, or
     71  // - the other integrity metadata has the same hash algorithm and also the
     72  // same set of values otherwise, return false.
     73  //
     74  // This method simply checks if the other integrity metadata is identical to
     75  // this one (if it exists), so that a load that has been checked against that
     76  // other integrity metadata implies that the current integrity metadata is
     77  // also satisfied.
     78  bool CanTrustBeDelegatedTo(const SRIMetadata& aOther) const;
     79 
     80 private:
     81  CopyableTArray<nsCString> mHashes;
     82  nsString mIntegrityString;
     83  nsCString mAlgorithm;
     84  int8_t mAlgorithmType;
     85  bool mEmpty;
     86 };
     87 
     88 }  // namespace mozilla::dom
     89 
     90 #endif  // mozilla_dom_SRIMetadata_h