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

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

Issue 2879353002: Add a buffer reader/writer for NTLM. (Closed)
Patch Set: Add a buffer reader/writer for NTLM. Created 3 years, 6 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
OLDNEW
(Empty)
1 // Copyright (c) 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/http/ntlm.h"
18
19 namespace net {
20
21 // Supports various bounds checked low level buffer operations required by an
22 // NTLM implementation.
23 //
24 // The class supports sequential write to an internally managed buffer. All
25 // writes perform bounds checking to ensure enough space is remaining in the
26 // buffer.
27 //
28 // The internal buffer is allocated in the constructor with size |buffer_len|
29 // and owned by the class. Use |ReleaseBufferPtr| to take ownership of the
30 // internal buffer. Thereafter all operations on the class will fail.
31 //
32 //
33 // Write* methods write the buffer at the current cursor position and perform
34 // any necessary type conversion and provide the data in out params. After a
35 // successful write the cursor position is advanced past the written field.
36 //
37 // Failed writes leave the internal cursor at the same position as before the
38 // call.
39 //
40 //
41 // Based on [MS-NLMP]: NT LAN Manager (NTLM) Authentication Protocol
42 // Specification version 28.0 [1]
43 //
44 // [1] https://msdn.microsoft.com/en-us/library/cc236621.aspx
45 class NET_EXPORT_PRIVATE NtlmBufferWriter {
46 public:
47 explicit NtlmBufferWriter(size_t buffer_len);
48 ~NtlmBufferWriter();
49
50 size_t GetLength() const { return buffer_len_; }
51 size_t GetCursor() const { return cursor_; }
52 bool IsEndOfBuffer() const { return cursor_ >= GetLength(); }
53
54 // Releases ownership of the internal buffer. Subsequent writes
55 // will all fail.
56 uint8_t* ReleaseBufferPtr() {
asanka 2017/06/08 19:40:10 Why not something like a std::vector<uint8_t> ? Th
zentaro 2017/06/12 23:12:08 If the output format was more general/variable lik
asanka 2017/06/16 03:21:28 But the caller now needs to know the encoding over
zentaro 2017/06/21 00:38:46 The caller needs to know the lengths anyway. The f
57 buffer_len_ = 0;
58 cursor_ = 0;
59 return buffer_.release();
60 }
61
62 // Gets a base::StringPiece view over the entire buffer.
63 base::StringPiece GetBuffer() const {
64 return base::StringPiece(reinterpret_cast<const char*>(buffer_.get()),
65 buffer_len_);
66 }
67
68 // Returns true if there are |len| more bytes between the current cursor
69 // position and the end of the buffer.
70 bool CanWrite(size_t len) const;
71
72 // Writes a 16 bit unsigned value (little endian). If there are not 16
73 // more bits available in the buffer it returns false.
74 bool WriteUInt16(uint16_t value);
75
76 // Writes a 32 bit unsigned value (little endian). If there are not 32
77 // more bits available in the buffer it returns false.
78 bool WriteUInt32(uint32_t value);
79
80 // Writes a 64 bit unsigned value (little endian). If there are not 64
81 // more bits available in the buffer it returns false.
82 bool WriteUInt64(uint64_t value);
83
84 // Writes |len| bytes from |buffer|. If there are not |len| more bytes in
85 // the buffer it returns false.
86 bool WriteBytes(const uint8_t* buffer, size_t len);
87
88 // Writes the bytes from the |base::StringPiece|. If there are not enough
89 // bytes in the buffer it returns false.
90 bool WriteBytes(base::StringPiece bytes);
91
92 // Writes |count| bytes of zeros to the buffer. If there are not |count|
93 // more bytes in available in the buffer it returns false.
94 bool WriteZeros(size_t count);
95
96 // A security buffer is an 8 byte structure that defines the offset and
97 // length of a payload (string, struct, or byte array) that appears after
98 // the fixed part of the message.
99 //
100 // The structure in the NTLM message is (little endian fields):
101 // uint16 - |length| Length of payload
102 // uint16 - Allocation (ignored and always set to |length|)
103 // uint32 - |offset| Offset from start of message
104 bool WriteSecurityBuffer(ntlm::SecurityBuffer sec_buf);
105
106 // Writes a security buffer with all fields zero. This implies there is
107 // no payload for this field. See |WriteSecurityBuffer|.
108 bool WriteEmptySecurityBuffer();
109
110 // Writes a string of 8 bit characters to the buffer.
111 //
112 // When unicode was not negotiated only the hostname string will go through
113 // this code path. The 8 bit bytes of the hostname are written to the buffer.
114 // The encoding is not relevant.
115 bool WriteNarrowString(const std::string& str);
116
117 // Converts the 16 bit characters to UTF8 and writes the resulting 8 bit
118 // characters.
119 //
120 // If unicode was not negotiated, username and domain get converted to UTF8
asanka 2017/06/08 19:40:10 UTF-8? Shouldn't it need to be CP-1252?
zentaro 2017/06/12 23:12:08 Per other discussion it's actually OEM (basically
121 // in the message. Since they are just treated as additional bytes input to
122 // hash the encoding doesn't matter. In practice only a very old or non-
123 // windows server might trigger this code path since we always attempt to
124 // negotiate unicode and servers are supposed to honor that request.
125 bool WriteString16AsUTF8(const base::string16& str);
asanka 2017/06/08 19:40:09 Nit: prefer to avoid stretches of upper case chara
zentaro 2017/06/12 23:12:08 Done. Normally I would but I just followed the con
126
127 // Treats |str| as UTF8, converts to UTF-16 and writes it with little-endian
128 // byte order to the buffer.
129 //
130 // Two specific strings go through this code path.
131 //
132 // One case is the hostname. When the the unicode flag has been set during
133 // negotiation the hostname needs to appear in the message with 16 bit
134 // chars. The high byte of each char is just set to 0.
135 //
136 // The other case is the spn. With EPA enabled it appears in the target info
137 // inside an AV Pair where strings always have 16 bit chars.
138 bool WriteNarrowStringAsString16(const std::string& str);
asanka 2017/06/08 19:40:10 Nit: Call it WriteUtf8AsUnicodeString since we are
zentaro 2017/06/12 23:12:07 Done.
139
140 // Writes UTF-16 LE characters to the buffer. For these strings such as
141 // username and domain the actual encoding isn't really important. They are
142 // just treated as additional bytes of input to the hash.
143 bool WriteUnicodeString(const base::string16& str);
144
145 // Writes the 8 byte NTLM signature "NTLMSSP\0" into the buffer.
146 bool WriteSignature();
147
148 // There are 3 message types Negotiate (sent by client), Challenge (sent by
149 // server), and Authenticate (sent by client).
150 //
151 // This writes |message_type| as a uint32_t into the buffer.
152 bool WriteMessageType(ntlm::MessageType message_type);
153
154 // Performs |WriteSignature| then |WriteMessageType|.
155 bool WriteMessageHeader(ntlm::MessageType message_type);
156
157 // Returns the size in bytes of a string16 depending whether unicode
158 // was negotiated.
159 static size_t GetStringPayloadLength(const base::string16& str,
160 bool is_unicode);
161
162 // Returns the size in bytes of a std::string depending whether unicode
163 // was negotiated.
164 static size_t GetStringPayloadLength(const std::string& str, bool is_unicode);
Ryan Sleevi 2017/06/08 18:36:34 Do these need to be public-static? Could they jus
zentaro 2017/06/21 00:38:46 Done.
165
166 private:
167 // Writes |sizeof(T)| bytes little-endian of an integer type to the buffer.
168 template <typename T>
169 bool WriteUInt(T value);
170
171 // Sets the cursor position. Will return false if outside the buffer.
172 bool SetCursor(size_t cursor);
173
174 // Returns a pointer to the start of the buffer.
175 uint8_t* GetBufferPtr() const { return buffer_.get(); }
176
177 std::unique_ptr<uint8_t[]> buffer_;
178 size_t buffer_len_;
179 size_t cursor_;
180
181 DISALLOW_COPY_AND_ASSIGN(NtlmBufferWriter);
182 };
183
184 } // namespace net
185
186 #endif // NET_BASE_NTLM_BUFFER_WRITER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698