OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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 NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_MESSAGE_H_ | |
6 #define NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_MESSAGE_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <cstdint> | |
12 #include <memory> | |
13 #include <string> | |
14 #include <vector> | |
15 | |
16 #include "base/strings/string_piece.h" | |
17 #include "net/base/net_export.h" | |
18 #include "net/quic/quic_protocol.h" | |
19 | |
20 namespace net { | |
21 | |
22 // An intermediate format of a handshake message that's convenient for a | |
23 // CryptoFramer to serialize from or parse into. | |
24 class NET_EXPORT_PRIVATE CryptoHandshakeMessage { | |
25 public: | |
26 CryptoHandshakeMessage(); | |
27 CryptoHandshakeMessage(const CryptoHandshakeMessage& other); | |
28 CryptoHandshakeMessage(CryptoHandshakeMessage&& other); | |
29 ~CryptoHandshakeMessage(); | |
30 | |
31 CryptoHandshakeMessage& operator=(const CryptoHandshakeMessage& other); | |
32 CryptoHandshakeMessage& operator=(CryptoHandshakeMessage&& other); | |
33 | |
34 // Clears state. | |
35 void Clear(); | |
36 | |
37 // GetSerialized returns the serialized form of this message and caches the | |
38 // result. Subsequently altering the message does not invalidate the cache. | |
39 const QuicData& GetSerialized() const; | |
40 | |
41 // MarkDirty invalidates the cache created by |GetSerialized|. | |
42 void MarkDirty(); | |
43 | |
44 // SetValue sets an element with the given tag to the raw, memory contents of | |
45 // |v|. | |
46 template <class T> | |
47 void SetValue(QuicTag tag, const T& v) { | |
48 tag_value_map_[tag] = | |
49 std::string(reinterpret_cast<const char*>(&v), sizeof(v)); | |
50 } | |
51 | |
52 // SetVector sets an element with the given tag to the raw contents of an | |
53 // array of elements in |v|. | |
54 template <class T> | |
55 void SetVector(QuicTag tag, const std::vector<T>& v) { | |
56 if (v.empty()) { | |
57 tag_value_map_[tag] = std::string(); | |
58 } else { | |
59 tag_value_map_[tag] = std::string(reinterpret_cast<const char*>(&v[0]), | |
60 v.size() * sizeof(T)); | |
61 } | |
62 } | |
63 | |
64 // Returns the message tag. | |
65 QuicTag tag() const { return tag_; } | |
66 // Sets the message tag. | |
67 void set_tag(QuicTag tag) { tag_ = tag; } | |
68 | |
69 const QuicTagValueMap& tag_value_map() const { return tag_value_map_; } | |
70 | |
71 void SetStringPiece(QuicTag tag, base::StringPiece value); | |
72 | |
73 // Erase removes a tag/value, if present, from the message. | |
74 void Erase(QuicTag tag); | |
75 | |
76 // GetTaglist finds an element with the given tag containing zero or more | |
77 // tags. If such a tag doesn't exist, it returns false. Otherwise it sets | |
78 // |out_tags| and |out_len| to point to the array of tags and returns true. | |
79 // The array points into the CryptoHandshakeMessage and is valid only for as | |
80 // long as the CryptoHandshakeMessage exists and is not modified. | |
81 QuicErrorCode GetTaglist(QuicTag tag, | |
82 const QuicTag** out_tags, | |
83 size_t* out_len) const; | |
84 | |
85 bool GetStringPiece(QuicTag tag, base::StringPiece* out) const; | |
86 | |
87 // GetNthValue24 interprets the value with the given tag to be a series of | |
88 // 24-bit, length prefixed values and it returns the subvalue with the given | |
89 // index. | |
90 QuicErrorCode GetNthValue24(QuicTag tag, | |
91 unsigned index, | |
92 base::StringPiece* out) const; | |
93 QuicErrorCode GetUint32(QuicTag tag, uint32_t* out) const; | |
94 QuicErrorCode GetUint64(QuicTag tag, uint64_t* out) const; | |
95 | |
96 // size returns 4 (message tag) + 2 (uint16_t, number of entries) + | |
97 // (4 (tag) + 4 (end offset))*tag_value_map_.size() + ∑ value sizes. | |
98 size_t size() const; | |
99 | |
100 // set_minimum_size sets the minimum number of bytes that the message should | |
101 // consume. The CryptoFramer will add a PAD tag as needed when serializing in | |
102 // order to ensure this. Setting a value of 0 disables padding. | |
103 // | |
104 // Padding is useful in order to ensure that messages are a minimum size. A | |
105 // QUIC server can require a minimum size in order to reduce the | |
106 // amplification factor of any mirror DoS attack. | |
107 void set_minimum_size(size_t min_bytes); | |
108 | |
109 size_t minimum_size() const; | |
110 | |
111 // DebugString returns a multi-line, string representation of the message | |
112 // suitable for including in debug output. | |
113 std::string DebugString() const; | |
114 | |
115 private: | |
116 // GetPOD is a utility function for extracting a plain-old-data value. If | |
117 // |tag| exists in the message, and has a value of exactly |len| bytes then | |
118 // it copies |len| bytes of data into |out|. Otherwise |len| bytes at |out| | |
119 // are zeroed out. | |
120 // | |
121 // If used to copy integers then this assumes that the machine is | |
122 // little-endian. | |
123 QuicErrorCode GetPOD(QuicTag tag, void* out, size_t len) const; | |
124 | |
125 std::string DebugStringInternal(size_t indent) const; | |
126 | |
127 QuicTag tag_; | |
128 QuicTagValueMap tag_value_map_; | |
129 | |
130 size_t minimum_size_; | |
131 | |
132 // The serialized form of the handshake message. This member is constructed | |
133 // lasily. | |
134 mutable std::unique_ptr<QuicData> serialized_; | |
135 }; | |
136 | |
137 } // namespace net | |
138 | |
139 #endif // NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_MESSAGE_H_ | |
OLD | NEW |