QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 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 QUICHE_QUIC_CORE_CRYPTO_CRYPTO_FRAMER_H_ |
| 6 | #define QUICHE_QUIC_CORE_CRYPTO_CRYPTO_FRAMER_H_ |
| 7 | |
| 8 | #include <cstddef> |
| 9 | #include <cstdint> |
| 10 | #include <memory> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 11 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #include <utility> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake_message.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_message_parser.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | #include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h" |
| 19 | |
| 20 | namespace quic { |
| 21 | |
| 22 | class CryptoFramer; |
| 23 | class QuicData; |
| 24 | class QuicDataWriter; |
| 25 | |
| 26 | class QUIC_EXPORT_PRIVATE CryptoFramerVisitorInterface { |
| 27 | public: |
| 28 | virtual ~CryptoFramerVisitorInterface() {} |
| 29 | |
| 30 | // Called if an error is detected. |
| 31 | virtual void OnError(CryptoFramer* framer) = 0; |
| 32 | |
| 33 | // Called when a complete handshake message has been parsed. |
| 34 | virtual void OnHandshakeMessage(const CryptoHandshakeMessage& message) = 0; |
| 35 | }; |
| 36 | |
| 37 | // A class for framing the crypto messages that are exchanged in a QUIC |
| 38 | // session. |
| 39 | class QUIC_EXPORT_PRIVATE CryptoFramer : public CryptoMessageParser { |
| 40 | public: |
| 41 | CryptoFramer(); |
| 42 | |
| 43 | ~CryptoFramer() override; |
| 44 | |
| 45 | // ParseMessage parses exactly one message from the given QuicStringPiece. If |
| 46 | // there is an error, the message is truncated, or the message has trailing |
| 47 | // garbage then nullptr will be returned. |
| 48 | static std::unique_ptr<CryptoHandshakeMessage> ParseMessage( |
| 49 | QuicStringPiece in); |
| 50 | |
| 51 | // Set callbacks to be called from the framer. A visitor must be set, or |
| 52 | // else the framer will crash. It is acceptable for the visitor to do |
| 53 | // nothing. If this is called multiple times, only the last visitor |
| 54 | // will be used. |visitor| will be owned by the framer. |
| 55 | void set_visitor(CryptoFramerVisitorInterface* visitor) { |
| 56 | visitor_ = visitor; |
| 57 | } |
| 58 | |
| 59 | QuicErrorCode error() const override; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 60 | const std::string& error_detail() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 61 | |
| 62 | // Processes input data, which must be delivered in order. Returns |
| 63 | // false if there was an error, and true otherwise. ProcessInput optionally |
| 64 | // takes an EncryptionLevel, but it is ignored. The variant with the |
| 65 | // EncryptionLevel is provided to match the CryptoMessageParser interface. |
| 66 | bool ProcessInput(QuicStringPiece input, EncryptionLevel level) override; |
| 67 | bool ProcessInput(QuicStringPiece input); |
| 68 | |
| 69 | // Returns the number of bytes of buffered input data remaining to be |
| 70 | // parsed. |
| 71 | size_t InputBytesRemaining() const override; |
| 72 | |
| 73 | // Checks if the specified tag has been seen. Returns |true| if it |
| 74 | // has, and |false| if it has not or a CHLO has not been seen. |
| 75 | bool HasTag(QuicTag tag) const; |
| 76 | |
| 77 | // Even if the CHLO has not been fully received, force processing of |
| 78 | // the handshake message. This is dangerous and should not be used |
| 79 | // except as a mechanism of last resort. |
| 80 | void ForceHandshake(); |
| 81 | |
| 82 | // Returns a new QuicData owned by the caller that contains a serialized |
| 83 | // |message|, or nullptr if there was an error. |
QUICHE team | 3fe6a8b | 2019-03-14 09:10:38 -0700 | [diff] [blame] | 84 | static std::unique_ptr<QuicData> ConstructHandshakeMessage( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 85 | const CryptoHandshakeMessage& message); |
| 86 | |
| 87 | // Debug only method which permits processing truncated messages. |
| 88 | void set_process_truncated_messages(bool process_truncated_messages) { |
| 89 | process_truncated_messages_ = process_truncated_messages; |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | // Clears per-message state. Does not clear the visitor. |
| 94 | void Clear(); |
| 95 | |
| 96 | // Process does does the work of |ProcessInput|, but returns an error code, |
| 97 | // doesn't set error_ and doesn't call |visitor_->OnError()|. |
| 98 | QuicErrorCode Process(QuicStringPiece input); |
| 99 | |
| 100 | static bool WritePadTag(QuicDataWriter* writer, |
| 101 | size_t pad_length, |
| 102 | uint32_t* end_offset); |
| 103 | |
| 104 | // Represents the current state of the parsing state machine. |
| 105 | enum CryptoFramerState { |
| 106 | STATE_READING_TAG, |
| 107 | STATE_READING_NUM_ENTRIES, |
| 108 | STATE_READING_TAGS_AND_LENGTHS, |
| 109 | STATE_READING_VALUES |
| 110 | }; |
| 111 | |
| 112 | // Visitor to invoke when messages are parsed. |
| 113 | CryptoFramerVisitorInterface* visitor_; |
| 114 | // Last error. |
| 115 | QuicErrorCode error_; |
| 116 | // Remaining unparsed data. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 117 | std::string buffer_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 118 | // Current state of the parsing. |
| 119 | CryptoFramerState state_; |
| 120 | // The message currently being parsed. |
| 121 | CryptoHandshakeMessage message_; |
| 122 | // The issue which caused |error_| |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 123 | std::string error_detail_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 124 | // Number of entires in the message currently being parsed. |
| 125 | uint16_t num_entries_; |
| 126 | // tags_and_lengths_ contains the tags that are currently being parsed and |
| 127 | // their lengths. |
| 128 | std::vector<std::pair<QuicTag, size_t>> tags_and_lengths_; |
| 129 | // Cumulative length of all values in the message currently being parsed. |
| 130 | size_t values_len_; |
| 131 | // Set to true to allow of processing of truncated messages for debugging. |
| 132 | bool process_truncated_messages_; |
| 133 | }; |
| 134 | |
| 135 | } // namespace quic |
| 136 | |
| 137 | #endif // QUICHE_QUIC_CORE_CRYPTO_CRYPTO_FRAMER_H_ |