OLD | NEW |
(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 #include "net/ntlm/ntlm_buffer_reader.h" |
| 6 |
| 7 #include <string.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 |
| 11 namespace net { |
| 12 namespace ntlm { |
| 13 |
| 14 NtlmBufferReader::NtlmBufferReader(base::StringPiece buffer) |
| 15 : buffer_(buffer), cursor_(0) { |
| 16 DCHECK(buffer.data()); |
| 17 } |
| 18 |
| 19 NtlmBufferReader::NtlmBufferReader(const uint8_t* ptr, size_t len) |
| 20 : NtlmBufferReader( |
| 21 base::StringPiece(reinterpret_cast<const char*>(ptr), len)) {} |
| 22 |
| 23 NtlmBufferReader::~NtlmBufferReader() {} |
| 24 |
| 25 bool NtlmBufferReader::CanRead(size_t len) const { |
| 26 return CanReadFrom(GetCursor(), len); |
| 27 } |
| 28 |
| 29 bool NtlmBufferReader::CanReadFrom(size_t offset, size_t len) const { |
| 30 if (len == 0) |
| 31 return true; |
| 32 |
| 33 return (len <= GetLength() && offset <= GetLength() - len); |
| 34 } |
| 35 |
| 36 bool NtlmBufferReader::ReadUInt16(uint16_t* value) { |
| 37 return ReadUInt<uint16_t>(value); |
| 38 } |
| 39 |
| 40 bool NtlmBufferReader::ReadUInt32(uint32_t* value) { |
| 41 return ReadUInt<uint32_t>(value); |
| 42 } |
| 43 |
| 44 bool NtlmBufferReader::ReadUInt64(uint64_t* value) { |
| 45 return ReadUInt<uint64_t>(value); |
| 46 } |
| 47 |
| 48 bool NtlmBufferReader::ReadFlags(NegotiateFlags* flags) { |
| 49 uint32_t raw; |
| 50 if (!ReadUInt32(&raw)) |
| 51 return false; |
| 52 |
| 53 *flags = static_cast<NegotiateFlags>(raw); |
| 54 return true; |
| 55 } |
| 56 |
| 57 bool NtlmBufferReader::ReadBytes(uint8_t* buffer, size_t len) { |
| 58 if (!CanRead(len)) |
| 59 return false; |
| 60 |
| 61 memcpy(reinterpret_cast<void*>(buffer), |
| 62 reinterpret_cast<const void*>(GetBufferAtCursor()), len); |
| 63 |
| 64 AdvanceCursor(len); |
| 65 return true; |
| 66 } |
| 67 |
| 68 bool NtlmBufferReader::ReadBytesFrom(const SecurityBuffer& sec_buf, |
| 69 uint8_t* buffer) { |
| 70 if (!CanReadFrom(sec_buf)) |
| 71 return false; |
| 72 |
| 73 memcpy(reinterpret_cast<void*>(buffer), |
| 74 reinterpret_cast<const void*>(GetBufferPtr() + sec_buf.offset), |
| 75 sec_buf.length); |
| 76 |
| 77 return true; |
| 78 } |
| 79 |
| 80 bool NtlmBufferReader::ReadSecurityBuffer(SecurityBuffer* sec_buf) { |
| 81 return ReadUInt16(&sec_buf->length) && SkipBytes(sizeof(uint16_t)) && |
| 82 ReadUInt32(&sec_buf->offset); |
| 83 } |
| 84 |
| 85 bool NtlmBufferReader::ReadMessageType(MessageType* message_type) { |
| 86 uint32_t raw_message_type; |
| 87 if (!ReadUInt32(&raw_message_type)) |
| 88 return false; |
| 89 |
| 90 *message_type = static_cast<MessageType>(raw_message_type); |
| 91 |
| 92 if (*message_type != MessageType::kNegotiate && |
| 93 *message_type != MessageType::kChallenge && |
| 94 *message_type != MessageType::kAuthenticate) |
| 95 return false; |
| 96 |
| 97 return true; |
| 98 } |
| 99 |
| 100 bool NtlmBufferReader::SkipSecurityBuffer() { |
| 101 return SkipBytes(kSecurityBufferLen); |
| 102 } |
| 103 |
| 104 bool NtlmBufferReader::SkipSecurityBufferWithValidation() { |
| 105 SecurityBuffer sec_buf; |
| 106 return ReadSecurityBuffer(&sec_buf) && CanReadFrom(sec_buf); |
| 107 } |
| 108 |
| 109 bool NtlmBufferReader::SkipBytes(size_t count) { |
| 110 if (!CanRead(count)) |
| 111 return false; |
| 112 |
| 113 AdvanceCursor(count); |
| 114 return true; |
| 115 } |
| 116 |
| 117 bool NtlmBufferReader::MatchSignature() { |
| 118 if (!CanRead(kSignatureLen)) |
| 119 return false; |
| 120 |
| 121 if (memcmp(kSignature, GetBufferAtCursor(), kSignatureLen) != 0) |
| 122 return false; |
| 123 |
| 124 AdvanceCursor(kSignatureLen); |
| 125 return true; |
| 126 } |
| 127 |
| 128 bool NtlmBufferReader::MatchMessageType(MessageType message_type) { |
| 129 MessageType actual_message_type; |
| 130 return ReadMessageType(&actual_message_type) && |
| 131 (actual_message_type == message_type); |
| 132 } |
| 133 |
| 134 bool NtlmBufferReader::MatchMessageHeader(MessageType message_type) { |
| 135 return MatchSignature() && MatchMessageType(message_type); |
| 136 } |
| 137 |
| 138 bool NtlmBufferReader::MatchZeros(size_t count) { |
| 139 if (!CanRead(count)) |
| 140 return false; |
| 141 |
| 142 for (size_t i = 0; i < count; i++) { |
| 143 if (GetBufferAtCursor()[i] != 0) |
| 144 return false; |
| 145 } |
| 146 |
| 147 AdvanceCursor(count); |
| 148 return true; |
| 149 } |
| 150 |
| 151 bool NtlmBufferReader::MatchEmptySecurityBuffer() { |
| 152 SecurityBuffer sec_buf; |
| 153 return ReadSecurityBuffer(&sec_buf) && (sec_buf.offset <= GetLength()) && |
| 154 (sec_buf.length == 0); |
| 155 } |
| 156 |
| 157 template <typename T> |
| 158 bool NtlmBufferReader::ReadUInt(T* value) { |
| 159 size_t int_size = sizeof(T); |
| 160 if (!CanRead(int_size)) |
| 161 return false; |
| 162 |
| 163 *value = 0; |
| 164 for (size_t i = 0; i < int_size; i++) { |
| 165 *value += static_cast<T>(GetByteAtCursor()) << (i * 8); |
| 166 AdvanceCursor(1); |
| 167 } |
| 168 |
| 169 return true; |
| 170 } |
| 171 |
| 172 void NtlmBufferReader::SetCursor(size_t cursor) { |
| 173 DCHECK_LE(cursor, GetLength()); |
| 174 |
| 175 cursor_ = cursor; |
| 176 } |
| 177 |
| 178 } // namespace ntlm |
| 179 } // namespace net |
OLD | NEW |