Chromium Code Reviews| OLD | NEW |
|---|---|
| (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_message.h" | |
| 18 | |
| 19 namespace net { | |
| 20 | |
| 21 // Supports various bounds checked low level buffer | |
| 22 // operations required by an NTLM implementation. | |
| 23 // | |
| 24 // The class supports sequential write to an internally | |
| 25 // managed buffer. All writes perform bounds checking to ensure | |
| 26 // enough space is remaining in the buffer. | |
| 27 // | |
| 28 // The internal buffer is allocated in the constructor with | |
| 29 // size |buffer_len| and owned by the class. Use | |
| 30 // |ReleaseBufferPtr| to take ownership of the internal | |
| 31 // buffer. Thereafter all operations on the class will fail. | |
| 32 // | |
| 33 // | |
| 34 // Write* methods write the buffer at the current cursor | |
| 35 // position and perform any necessary type conversion and | |
| 36 // provide the data in out params. After a successful write | |
| 37 // the cursor position is advanced past the written field. | |
| 38 // | |
| 39 // Failed writes leave the internal cursor at the same | |
| 40 // position as before the call. | |
| 41 // | |
| 42 // | |
| 43 // Based on [MS-NLMP]: NT LAN Manager (NTLM) Authentication | |
| 44 // Protocol specification version 28.0 [1] | |
| 45 // | |
| 46 // [1] https://msdn.microsoft.com/en-us/library/cc236621.aspx | |
| 47 class NET_EXPORT NtlmBufferWriter { | |
| 48 public: | |
| 49 NtlmBufferWriter(size_t buffer_len); | |
| 50 ~NtlmBufferWriter(); | |
| 51 | |
| 52 size_t GetLength() const { return buffer_len_; } | |
| 53 size_t GetCursor() const { return cursor_; } | |
| 54 bool IsEndOfBuffer() const { return cursor_ >= GetLength(); } | |
| 55 uint8_t* GetBufferPtr() const { return buffer_.get(); } | |
| 56 | |
| 57 // Releases ownership of the internal buffer. Subsequent writes | |
| 58 // will all fail. | |
| 59 uint8_t* ReleaseBufferPtr() { | |
| 60 buffer_len_ = 0; | |
| 61 cursor_ = 0; | |
| 62 return buffer_.release(); | |
| 63 } | |
| 64 | |
| 65 base::StringPiece GetBuffer() const { | |
| 66 return base::StringPiece(reinterpret_cast<const char*>(buffer_.get()), | |
| 67 buffer_len_); | |
| 68 } | |
| 69 | |
| 70 // Returns true if there are |len| more bytes between the | |
| 71 // current cursor position and the end of the buffer. | |
| 72 bool CanWrite(size_t len) const; | |
| 73 | |
| 74 bool WriteUInt16(uint16_t value); | |
| 75 bool WriteUInt32(uint32_t value); | |
| 76 bool WriteUInt64(uint64_t value); | |
| 77 bool WriteBytes(const uint8_t* buffer, size_t len); | |
| 78 | |
| 79 bool WriteZeros(size_t count); | |
| 80 | |
| 81 // A security buffer is an 8 byte structure that defines the | |
| 82 // offset and length of a payload (string, struct or byte array) | |
| 83 // that appears after the fixed part of the message. | |
| 84 // | |
| 85 // The structure is (little endian fields): | |
| 86 // uint16 - |length| Length of payload | |
| 87 // uint16 - Allocation (always set to |length|) | |
| 88 // uint32 - |offset| Offset from start of message | |
| 89 bool WriteSecurityBuffer(uint16_t length, uint32_t offset); | |
| 90 bool WriteEmptySecurityBuffer(); | |
| 91 | |
| 92 // Writes a string of 8 bit characters to the buffer. | |
| 93 bool WriteNarrowString(const std::string& str); | |
| 94 | |
| 95 // Converts the 16 bit characters to UTF8 and writes | |
| 96 // the resulting 8 bit characters. | |
| 97 bool WriteString16AsUTF8(const base::string16& str); | |
| 98 | |
| 99 // Zero pads an 8 bit string to a 16 bit string and writes | |
| 100 // the resulting 16 bit characters. | |
|
Ryan Sleevi
2017/05/30 19:02:23
This comment I found a bit confusing. Could you de
zentaro
2017/06/05 17:28:44
I tried to clarify. In practice I think nothing ev
| |
| 101 bool WriteNarrowStringAsString16(const std::string& str); | |
| 102 | |
| 103 // Writes the 16 bit characters to the buffer. | |
| 104 bool WriteUnicodeString(const base::string16& str); | |
| 105 | |
| 106 // Writes the 8 byte NTLM signature "NTLMSSP\0" into the buffer. | |
| 107 bool WriteSignature(); | |
| 108 | |
| 109 // There are 3 message types Negotiate (sent by client), | |
| 110 // Challenge (sent by server), and Authenticate (sent by client). | |
| 111 // | |
| 112 // This writes |message_type| as a uint32_t into the buffer. | |
| 113 bool WriteMessageType(NtlmMessage::MessageType message_type); | |
| 114 | |
| 115 // Performs |WriteSignature| then |WriteMessageType|. | |
| 116 bool WriteMessageHeader(NtlmMessage::MessageType message_type); | |
| 117 | |
| 118 static size_t GetStringPayloadLength(const base::string16& str, | |
| 119 bool is_unicode); | |
| 120 static size_t GetStringPayloadLength(const std::string& str, bool is_unicode); | |
| 121 | |
| 122 private: | |
| 123 // Writes |sizeof(T)| bytes little-endian of an integer type to | |
| 124 // the buffer. | |
| 125 template <typename T> | |
| 126 bool WriteUInt(T value); | |
| 127 bool SetCursor(size_t cursor); | |
| 128 bool WriteUnicodeStringIntoBuffer(const base::string16& value, | |
| 129 uint8_t* buffer, | |
| 130 size_t buffer_len); | |
| 131 | |
| 132 std::unique_ptr<uint8_t[]> buffer_; | |
| 133 size_t buffer_len_; | |
| 134 size_t cursor_; | |
| 135 | |
| 136 DISALLOW_COPY_AND_ASSIGN(NtlmBufferWriter); | |
| 137 }; | |
| 138 | |
| 139 } // namespace net | |
| 140 | |
| 141 #endif // NET_BASE_NTLM_BUFFER_WRITER_H_ | |
| OLD | NEW |