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