1 // Copyright 2015 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BSSL_DER_INPUT_H_ 6 #define BSSL_DER_INPUT_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <string> 12 #include <string_view> 13 14 #include <openssl/base.h> 15 #include <openssl/span.h> 16 17 #if defined(__has_include) 18 #if __has_include(<version>) 19 #include <version> 20 #endif 21 #endif 22 23 #if defined(__cpp_lib_ranges) && __cpp_lib_ranges >= 201911L 24 #include <ranges> 25 BSSL_NAMESPACE_BEGIN 26 namespace der { 27 class OPENSSL_EXPORT Input; 28 } 29 BSSL_NAMESPACE_END 30 31 // Mark `Input` as satisfying the `view` and `borrowed_range` concepts. This 32 // should be done before the definition of `Input`, so that any inlined calls to 33 // range functionality use the correct specializations. 34 template <> 35 inline constexpr bool std::ranges::enable_view<bssl::der::Input> = true; 36 template <> 37 inline constexpr bool std::ranges::enable_borrowed_range<bssl::der::Input> = 38 true; 39 #endif 40 41 BSSL_NAMESPACE_BEGIN 42 namespace der { 43 44 // An opaque class that represents a fixed buffer of data of a fixed length, 45 // to be used as an input to other operations. An Input object does not own 46 // the data it references, so callers are responsible for making sure that 47 // the data outlives the Input object and any other associated objects. 48 // 49 // All data access for an Input should be done through the ByteReader class. 50 // This class and associated classes are designed with safety in mind to make it 51 // difficult to read memory outside of an Input. ByteReader provides a simple 52 // API for reading through the Input sequentially. For more complicated uses, 53 // multiple instances of a ByteReader for a particular Input can be created. 54 // 55 // TODO(crbug.com/boringssl/661): This class will gradually be replaced with 56 // bssl::Span<const uint8_t>. Avoid relying on APIs that are not part of 57 // bssl::Span. 58 class OPENSSL_EXPORT Input { 59 public: 60 // Creates an empty Input, one from which no data can be read. 61 constexpr Input() = default; 62 63 // Creates an Input from a span. The constructed Input is only valid as long 64 // as |data| points to live memory. If constructed from, say, a 65 // |std::vector<uint8_t>|, mutating the vector will invalidate the Input. Input(bssl::Span<const uint8_t> data)66 constexpr Input(bssl::Span<const uint8_t> data) : data_(data) {} 67 68 // Creates an Input from the given |data| and |len|. Input(const uint8_t * data,size_t len)69 constexpr explicit Input(const uint8_t *data, size_t len) 70 : data_(Span(data, len)) {} 71 72 // Deprecated: Use StringAsBytes. 73 // 74 // Creates an Input from a std::string_view. The constructed Input is only 75 // valid as long as |data| points to live memory. If constructed from, say, a 76 // |std::string|, mutating the vector will invalidate the Input. Input(std::string_view str)77 explicit Input(std::string_view str) : data_(StringAsBytes(str)) {} 78 79 // The following APIs have the same semantics as in |bssl::Span|. begin()80 constexpr Span<const uint8_t>::iterator begin() const { 81 return data_.begin(); 82 } end()83 constexpr Span<const uint8_t>::iterator end() const { return data_.end(); } data()84 constexpr const uint8_t *data() const { return data_.data(); } size()85 constexpr size_t size() const { return data_.size(); } empty()86 constexpr bool empty() const { return data_.empty(); } 87 constexpr uint8_t operator[](size_t idx) const { return data_[idx]; } front()88 constexpr uint8_t front() const { return data_.front(); } back()89 constexpr uint8_t back() const { return data_.back(); } 90 constexpr Input subspan(size_t pos = 0, 91 size_t len = Span<const uint8_t>::npos) const { 92 return Input(data_.subspan(pos, len)); 93 } first(size_t len)94 constexpr Input first(size_t len) const { return Input(data_.first(len)); } last(size_t len)95 constexpr Input last(size_t len) const { return Input(data_.last(len)); } 96 97 // Deprecated: use BytesAsStringView and convert to std::string. 98 // 99 // Returns a copy of the data represented by this object as a std::string. 100 std::string AsString() const; 101 102 // Deprecated: Use ByteAsString. 103 // 104 // Returns a std::string_view pointing to the same data as the Input. The 105 // resulting string_view must not outlive the data that was used to construct 106 // this Input. AsStringView()107 std::string_view AsStringView() const { return BytesAsStringView(data_); } 108 109 // Deprecated: This class implicitly converts to bssl::Span<const uint8_t>. 110 // 111 // Returns a span pointing to the same data as the Input. The resulting span 112 // must not outlive the data that was used to construct this Input. AsSpan()113 Span<const uint8_t> AsSpan() const { return *this; } 114 115 // Deprecated: Use size() instead. Length()116 constexpr size_t Length() const { return size(); } 117 118 // Deprecated: Use data() instead. UnsafeData()119 constexpr const uint8_t *UnsafeData() const { return data(); } 120 121 private: 122 // TODO(crbug.com/770501): Replace this type with span altogether. 123 Span<const uint8_t> data_; 124 }; 125 126 // Return true if |lhs|'s data and |rhs|'s data are byte-wise equal. 127 OPENSSL_EXPORT bool operator==(Input lhs, Input rhs); 128 129 // Return true if |lhs|'s data and |rhs|'s data are not byte-wise equal. 130 OPENSSL_EXPORT bool operator!=(Input lhs, Input rhs); 131 132 // Returns true if |lhs|'s data is lexicographically less than |rhs|'s data. 133 OPENSSL_EXPORT constexpr bool operator<(Input lhs, Input rhs) { 134 // This is `std::lexicographical_compare`, but that's not `constexpr` until 135 // C++-20. 136 auto *it1 = lhs.data(); 137 auto *it2 = rhs.data(); 138 const auto *end1 = lhs.data() + lhs.size(); 139 const auto *end2 = rhs.data() + rhs.size(); 140 for (; it1 != end1 && it2 != end2; ++it1, ++it2) { 141 if (*it1 < *it2) { 142 return true; 143 } else if (*it2 < *it1) { 144 return false; 145 } 146 } 147 148 return it2 != end2; 149 } 150 151 // This class provides ways to read data from an Input in a bounds-checked way. 152 // The ByteReader is designed to read through the input sequentially. Once a 153 // byte has been read with a ByteReader, the caller can't go back and re-read 154 // that byte with the same reader. Of course, the caller can create multiple 155 // ByteReaders for the same input (or copy an existing ByteReader). 156 // 157 // For something simple like a single byte lookahead, the easiest way to do 158 // that is to copy the ByteReader and call ReadByte() on the copy - the original 159 // ByteReader will be unaffected and the peeked byte will be read through 160 // ReadByte(). For other read patterns, it can be useful to mark where one is 161 // in a ByteReader to be able to return to that spot. 162 // 163 // Some operations using Mark can also be done by creating a copy of the 164 // ByteReader. By using a Mark instead, you use less memory, but more 165 // importantly, you end up with an immutable object that matches the semantics 166 // of what is intended. 167 class OPENSSL_EXPORT ByteReader { 168 public: 169 // Creates a ByteReader to read the data represented by an Input. 170 explicit ByteReader(Input in); 171 172 // Reads a single byte from the input source, putting the byte read in 173 // |*byte_p|. If a byte cannot be read from the input (because there is 174 // no input left), then this method returns false. 175 [[nodiscard]] bool ReadByte(uint8_t *out); 176 177 // Reads |len| bytes from the input source, and initializes an Input to 178 // point to that data. If there aren't enough bytes left in the input source, 179 // then this method returns false. 180 [[nodiscard]] bool ReadBytes(size_t len, Input *out); 181 182 // Returns how many bytes are left to read. BytesLeft()183 size_t BytesLeft() const { return data_.size(); } 184 185 // Returns whether there is any more data to be read. 186 bool HasMore(); 187 188 private: 189 void Advance(size_t len); 190 191 bssl::Span<const uint8_t> data_; 192 }; 193 194 } // namespace der 195 BSSL_NAMESPACE_END 196 197 #endif // BSSL_DER_INPUT_H_ 198