1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2024 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using KeePassLib.Cryptography;
using KeePassLib.Native;
using KeePassLib.Utility;
#if KeePassLibSD
using KeePassLibSD;
#endif
namespace KeePassLib.Serialization
{
public sealed class HashedBlockStream : Stream
{
private const int NbDefaultBufferSize = 1024 * 1024; // 1 MB
private Stream m_sBase;
private readonly bool m_bWriting;
private readonly bool m_bVerify;
private BinaryReader m_brInput = null;
private BinaryWriter m_bwOutput = null;
private bool m_bEos = false;
private byte[] m_pbBuffer;
private int m_iBufferPos = 0;
private uint m_uBlockIndex = 0;
public override bool CanRead
{
get { return !m_bWriting; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return m_bWriting; }
}
public override long Length
{
get { Debug.Assert(false); throw new NotSupportedException(); }
}
public override long Position
{
get { Debug.Assert(false); throw new NotSupportedException(); }
set { Debug.Assert(false); throw new NotSupportedException(); }
}
public HashedBlockStream(Stream sBase, bool bWriting) :
this(sBase, bWriting, 0, true)
{
}
public HashedBlockStream(Stream sBase, bool bWriting, int nBufferSize) :
this(sBase, bWriting, nBufferSize, true)
{
}
public HashedBlockStream(Stream sBase, bool bWriting, int nBufferSize,
bool bVerify)
{
if(sBase == null) throw new ArgumentNullException("sBase");
if(nBufferSize < 0) throw new ArgumentOutOfRangeException("nBufferSize");
if(nBufferSize == 0) nBufferSize = NbDefaultBufferSize;
m_sBase = sBase;
m_bWriting = bWriting;
m_bVerify = bVerify;
UTF8Encoding utf8 = StrUtil.Utf8;
if(!m_bWriting) // Reading mode
{
if(!m_sBase.CanRead) throw new InvalidOperationException();
m_brInput = new BinaryReader(m_sBase, utf8);
m_pbBuffer = MemUtil.EmptyByteArray;
}
else // Writing mode
{
if(!m_sBase.CanWrite) throw new InvalidOperationException();
m_bwOutput = new BinaryWriter(m_sBase, utf8);
m_pbBuffer = new byte[nBufferSize];
}
}
protected override void Dispose(bool disposing)
{
if(disposing && (m_sBase != null))
{
if(!m_bWriting) // Reading mode
{
m_brInput.Close();
m_brInput = null;
}
else // Writing mode
{
if(m_iBufferPos == 0) // No data left in buffer
WriteSafeBlock(); // Write terminating block
else
{
WriteSafeBlock(); // Write remaining buffered data
WriteSafeBlock(); // Write terminating block
}
Flush();
m_bwOutput.Close();
m_bwOutput = null;
}
m_sBase.Close();
m_sBase = null;
}
SetBuffer(MemUtil.EmptyByteArray);
base.Dispose(disposing);
}
private void SetBuffer(byte[] pb)
{
MemUtil.ZeroByteArray(m_pbBuffer); // Erase previous buffer
m_pbBuffer = pb;
}
public override void Flush()
{
Debug.Assert(m_sBase != null); // Object should not be disposed
if(m_bWriting && (m_bwOutput != null)) m_bwOutput.Flush();
}
public override long Seek(long lOffset, SeekOrigin soOrigin)
{
Debug.Assert(false);
throw new NotSupportedException();
}
public override void SetLength(long lValue)
{
Debug.Assert(false);
throw new NotSupportedException();
}
public override int Read(byte[] pbBuffer, int iOffset, int nCount)
{
if(m_bWriting) throw new InvalidOperationException();
int nRemaining = nCount;
while(nRemaining > 0)
{
if(m_iBufferPos == m_pbBuffer.Length)
{
if(!ReadSafeBlock())
return (nCount - nRemaining); // Bytes actually read
}
int nCopy = Math.Min(m_pbBuffer.Length - m_iBufferPos, nRemaining);
Debug.Assert(nCopy > 0);
Array.Copy(m_pbBuffer, m_iBufferPos, pbBuffer, iOffset, nCopy);
iOffset += nCopy;
m_iBufferPos += nCopy;
nRemaining -= nCopy;
}
return nCount;
}
private bool ReadSafeBlock()
{
if(m_bEos) return false; // End of stream reached already
m_iBufferPos = 0;
if(m_brInput.ReadUInt32() != m_uBlockIndex)
throw new InvalidDataException();
++m_uBlockIndex;
byte[] pbStoredHash = m_brInput.ReadBytes(32);
if((pbStoredHash == null) || (pbStoredHash.Length != 32))
throw new InvalidDataException();
int nBufferSize = 0;
try { nBufferSize = m_brInput.ReadInt32(); }
catch(NullReferenceException) // Mono bug workaround (LaunchPad 783268)
{
if(!NativeLib.IsUnix()) throw;
}
if(nBufferSize < 0)
throw new InvalidDataException();
if(nBufferSize == 0)
{
for(int iHash = 0; iHash < 32; ++iHash)
{
if(pbStoredHash[iHash] != 0)
throw new InvalidDataException();
}
m_bEos = true;
SetBuffer(MemUtil.EmptyByteArray);
return false;
}
SetBuffer(m_brInput.ReadBytes(nBufferSize));
if((m_pbBuffer == null) || ((m_pbBuffer.Length != nBufferSize) && m_bVerify))
throw new InvalidDataException();
if(m_bVerify)
{
byte[] pbComputedHash = CryptoUtil.HashSha256(m_pbBuffer);
if((pbComputedHash == null) || (pbComputedHash.Length != 32))
throw new InvalidOperationException();
if(!MemUtil.ArraysEqual(pbStoredHash, pbComputedHash))
throw new InvalidDataException();
}
return true;
}
public override void Write(byte[] pbBuffer, int iOffset, int nCount)
{
if(!m_bWriting) throw new InvalidOperationException();
while(nCount > 0)
{
if(m_iBufferPos == m_pbBuffer.Length)
WriteSafeBlock();
int nCopy = Math.Min(m_pbBuffer.Length - m_iBufferPos, nCount);
Debug.Assert(nCopy > 0);
Array.Copy(pbBuffer, iOffset, m_pbBuffer, m_iBufferPos, nCopy);
iOffset += nCopy;
m_iBufferPos += nCopy;
nCount -= nCopy;
}
}
private void WriteSafeBlock()
{
m_bwOutput.Write(m_uBlockIndex);
++m_uBlockIndex;
if(m_iBufferPos != 0)
{
byte[] pbHash = CryptoUtil.HashSha256(m_pbBuffer, 0, m_iBufferPos);
// For KeePassLibSD:
// SHA256Managed sha256 = new SHA256Managed();
// byte[] pbHash;
// if(m_iBufferPos == m_pbBuffer.Length)
// pbHash = sha256.ComputeHash(m_pbBuffer);
// else
// {
// byte[] pbData = new byte[m_iBufferPos];
// Array.Copy(m_pbBuffer, 0, pbData, 0, m_iBufferPos);
// pbHash = sha256.ComputeHash(pbData);
// }
m_bwOutput.Write(pbHash);
}
else
{
m_bwOutput.Write((ulong)0); // Zero hash
m_bwOutput.Write((ulong)0);
m_bwOutput.Write((ulong)0);
m_bwOutput.Write((ulong)0);
}
m_bwOutput.Write(m_iBufferPos);
if(m_iBufferPos != 0)
m_bwOutput.Write(m_pbBuffer, 0, m_iBufferPos);
m_iBufferPos = 0;
}
}
}
|