Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(713)

Side by Side Diff: net/ntlm/ntlm_buffer_writer.h

Issue 2879353002: Add a buffer reader/writer for NTLM. (Closed)
Patch Set: Remove ReleaseBufferPtr Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/ntlm/ntlm_buffer_reader_unittest.cc ('k') | net/ntlm/ntlm_buffer_writer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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_BASE_NTLM_BUFFER_WRITER_H_
6 #define NET_BASE_NTLM_BUFFER_WRITER_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <memory>
12 #include <string>
13
14 #include "base/strings/string16.h"
15 #include "base/strings/string_piece.h"
16 #include "net/base/net_export.h"
17 #include "net/ntlm/ntlm_constants.h"
18
19 namespace net {
20 namespace ntlm {
21
22 // Supports various bounds checked low level buffer operations required by an
23 // NTLM implementation.
24 //
25 // The class supports sequential write to an internally managed buffer. All
26 // writes perform bounds checking to ensure enough space is remaining in the
27 // buffer.
28 //
29 // The internal buffer is allocated in the constructor with size |buffer_len|
30 // and owned by the class.
31 //
32 // Write* methods write the buffer at the current cursor position and perform
33 // any necessary type conversion and provide the data in out params. After a
34 // successful write the cursor position is advanced past the written field.
35 //
36 // Failed writes leave the internal cursor at the same position as before the
37 // call.
38 //
39 // Based on [MS-NLMP]: NT LAN Manager (NTLM) Authentication Protocol
40 // Specification version 28.0 [1]. Additional NTLM reference [2].
41 //
42 // [1] https://msdn.microsoft.com/en-us/library/cc236621.aspx
43 // [2] http://davenport.sourceforge.net/ntlm.html
44 class NET_EXPORT_PRIVATE NtlmBufferWriter {
45 public:
46 explicit NtlmBufferWriter(size_t buffer_len);
47 ~NtlmBufferWriter();
48
49 size_t GetLength() const { return buffer_len_; }
50 size_t GetCursor() const { return cursor_; }
51 bool IsEndOfBuffer() const { return cursor_ >= GetLength(); }
52
53 // Gets a base::StringPiece view over the entire buffer.
54 base::StringPiece GetBuffer() const {
55 return base::StringPiece(reinterpret_cast<const char*>(buffer_.get()),
56 buffer_len_);
57 }
58
59 // Returns true if there are |len| more bytes between the current cursor
60 // position and the end of the buffer.
61 bool CanWrite(size_t len) const;
62
63 // Writes a 16 bit unsigned value (little endian). If there are not 16
64 // more bits available in the buffer, it returns false.
65 bool WriteUInt16(uint16_t value) WARN_UNUSED_RESULT;
66
67 // Writes a 32 bit unsigned value (little endian). If there are not 32
68 // more bits available in the buffer, it returns false.
69 bool WriteUInt32(uint32_t value) WARN_UNUSED_RESULT;
70
71 // Writes a 64 bit unsigned value (little endian). If there are not 64
72 // more bits available in the buffer, it returns false.
73 bool WriteUInt64(uint64_t value) WARN_UNUSED_RESULT;
74
75 // Writes flags as a 32 bit unsigned value (little endian).
76 bool WriteFlags(NegotiateFlags flags) WARN_UNUSED_RESULT;
77
78 // Writes |len| bytes from |buffer|. If there are not |len| more bytes in
79 // the buffer, it returns false.
80 bool WriteBytes(const uint8_t* buffer, size_t len) WARN_UNUSED_RESULT;
81
82 // Writes the bytes from the |base::StringPiece|. If there are not enough
83 // bytes in the buffer, it returns false.
84 bool WriteBytes(base::StringPiece bytes) WARN_UNUSED_RESULT;
85
86 // Writes |count| bytes of zeros to the buffer. If there are not |count|
87 // more bytes in available in the buffer, it returns false.
88 bool WriteZeros(size_t count) WARN_UNUSED_RESULT;
89
90 // A security buffer is an 8 byte structure that defines the offset and
91 // length of a payload (string, struct, or byte array) that appears after
92 // the fixed part of the message.
93 //
94 // The structure in the NTLM message is (little endian fields):
95 // uint16 - |length| Length of payload
96 // uint16 - Allocation (ignored and always set to |length|)
97 // uint32 - |offset| Offset from start of message
98 bool WriteSecurityBuffer(SecurityBuffer sec_buf) WARN_UNUSED_RESULT;
99
100 // Writes a string of 8 bit characters to the buffer.
101 //
102 // When Unicode was not negotiated only the hostname string will go through
103 // this code path. The 8 bit bytes of the hostname are written to the buffer.
104 // The encoding is not relevant.
105 bool WriteUtf8String(const std::string& str) WARN_UNUSED_RESULT;
106
107 // Converts the 16 bit characters to UTF8 and writes the resulting 8 bit
108 // characters.
109 //
110 // If Unicode was not negotiated, the username and domain get converted to
111 // UTF8 in the message. Since they are just treated as additional bytes
112 // input to hash the encoding doesn't matter. In practice, only a very old or
113 // non-Windows server might trigger this code path since we always attempt
114 // to negotiate Unicode and servers are supposed to honor that request.
115 bool WriteUtf16AsUtf8String(const base::string16& str) WARN_UNUSED_RESULT;
116
117 // Treats |str| as UTF8, converts to UTF-16 and writes it with little-endian
118 // byte order to the buffer.
119 //
120 // Two specific strings go through this code path.
121 //
122 // One case is the hostname. When the the Unicode flag has been set during
123 // negotiation, the hostname needs to appear in the message with 16-bit
124 // characters.
125 //
126 // The other case is the Service Principal Name (SPN). With Extended
127 // Protection for Authentication (EPA) enabled, it appears in the target info
128 // inside an AV Pair, where strings always have 16-bit characters.
129 bool WriteUtf8AsUtf16String(const std::string& str) WARN_UNUSED_RESULT;
130
131 // Writes UTF-16 LE characters to the buffer. For these strings, such as
132 // username and the domain the actual encoding isn't important; they are just
133 // treated as additional bytes of input to the hash.
134 bool WriteUtf16String(const base::string16& str) WARN_UNUSED_RESULT;
135
136 // Writes the 8 byte NTLM signature "NTLMSSP\0" into the buffer.
137 bool WriteSignature() WARN_UNUSED_RESULT;
138
139 // There are 3 message types Negotiate (sent by client), Challenge (sent by
140 // server), and Authenticate (sent by client).
141 //
142 // This writes |message_type| as a uint32_t into the buffer.
143 bool WriteMessageType(MessageType message_type) WARN_UNUSED_RESULT;
144
145 // Performs |WriteSignature| then |WriteMessageType|.
146 bool WriteMessageHeader(MessageType message_type) WARN_UNUSED_RESULT;
147
148 private:
149 // Writes |sizeof(T)| bytes little-endian of an integer type to the buffer.
150 template <typename T>
151 bool WriteUInt(T value);
152
153 // Sets the cursor position. The caller should use |GetLength| or
154 // |CanWrite| to verify the bounds before calling this method.
155 void SetCursor(size_t cursor);
156
157 // Advances the cursor by |count|. The caller should use |GetLength| or
158 // |CanWrite| to verify the bounds before calling this method.
159 void AdvanceCursor(size_t count) { SetCursor(GetCursor() + count); }
160
161 // Returns a pointer to the start of the buffer.
162 uint8_t* GetBufferPtr() const { return buffer_.get(); }
163
164 // Returns pointer into the buffer at the current cursor location.
165 uint8_t* GetBufferPtrAtCursor() const { return buffer_.get() + GetCursor(); }
166
167 std::unique_ptr<uint8_t[]> buffer_;
168 size_t buffer_len_;
169 size_t cursor_;
170
171 DISALLOW_COPY_AND_ASSIGN(NtlmBufferWriter);
172 };
173
174 } // namespace ntlm
175 } // namespace net
176
177 #endif // NET_BASE_NTLM_BUFFER_WRITER_H_
OLDNEW
« no previous file with comments | « net/ntlm/ntlm_buffer_reader_unittest.cc ('k') | net/ntlm/ntlm_buffer_writer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698