|
|
1.1 ! root 1: /* ! 2: Copyright (c) 2008 TrueCrypt Foundation. All rights reserved. ! 3: ! 4: Governed by the TrueCrypt License 2.4 the full text of which is contained ! 5: in the file License.txt included in TrueCrypt binary and source code ! 6: distribution packages. ! 7: */ ! 8: ! 9: #include "VolumePassword.h" ! 10: #include "Platform/SerializerFactory.h" ! 11: #include "Platform/StringConverter.h" ! 12: ! 13: namespace TrueCrypt ! 14: { ! 15: VolumePassword::VolumePassword () : PasswordSize (0), Unportable (false) ! 16: { ! 17: AllocateBuffer (); ! 18: } ! 19: ! 20: VolumePassword::VolumePassword (const char *password, size_t size) ! 21: { ! 22: Set ((const byte *) password, size); ! 23: } ! 24: ! 25: VolumePassword::VolumePassword (const byte *password, size_t size) ! 26: { ! 27: Set (password, size); ! 28: } ! 29: ! 30: VolumePassword::VolumePassword (const wchar_t *password, size_t charCount) ! 31: { ! 32: Set (password, charCount); ! 33: } ! 34: ! 35: VolumePassword::VolumePassword (const wstring &password) ! 36: { ! 37: Set (password.c_str(), password.size()); ! 38: } ! 39: ! 40: VolumePassword::~VolumePassword () ! 41: { ! 42: } ! 43: ! 44: void VolumePassword::AllocateBuffer () ! 45: { ! 46: if (!PasswordBuffer.IsAllocated ()) ! 47: PasswordBuffer.Allocate (MaxSize); ! 48: } ! 49: ! 50: void VolumePassword::CheckPortability () const ! 51: { ! 52: if (Unportable || !IsPortable()) ! 53: throw UnportablePassword (SRC_POS); ! 54: } ! 55: ! 56: void VolumePassword::Deserialize (shared_ptr <Stream> stream) ! 57: { ! 58: Serializer sr (stream); ! 59: uint64 passwordSize; ! 60: sr.Deserialize ("PasswordSize", passwordSize); ! 61: PasswordSize = static_cast <size_t> (passwordSize); ! 62: sr.Deserialize ("PasswordBuffer", BufferPtr (PasswordBuffer)); ! 63: ! 64: Buffer wipeBuffer (128 * 1024); ! 65: sr.Deserialize ("WipeData", wipeBuffer); ! 66: } ! 67: ! 68: bool VolumePassword::IsPortable () const ! 69: { ! 70: for (size_t i = 0; i < PasswordSize; i++) ! 71: { ! 72: if (PasswordBuffer[i] >= 0x7f || PasswordBuffer[i] < 0x20) ! 73: return false; ! 74: } ! 75: return true; ! 76: } ! 77: ! 78: void VolumePassword::Serialize (shared_ptr <Stream> stream) const ! 79: { ! 80: Serializable::Serialize (stream); ! 81: Serializer sr (stream); ! 82: sr.Serialize ("PasswordSize", static_cast <uint64> (PasswordSize)); ! 83: sr.Serialize ("PasswordBuffer", ConstBufferPtr (PasswordBuffer)); ! 84: ! 85: // Wipe password from an eventual pipe buffer ! 86: Buffer wipeBuffer (128 * 1024); ! 87: wipeBuffer.Zero(); ! 88: sr.Serialize ("WipeData", ConstBufferPtr (wipeBuffer)); ! 89: } ! 90: ! 91: void VolumePassword::Set (const byte *password, size_t size) ! 92: { ! 93: AllocateBuffer (); ! 94: ! 95: if (size > MaxSize) ! 96: throw PasswordTooLong (SRC_POS); ! 97: ! 98: PasswordBuffer.CopyFrom (ConstBufferPtr (password, size)); ! 99: PasswordSize = size; ! 100: ! 101: Unportable = !IsPortable(); ! 102: } ! 103: ! 104: void VolumePassword::Set (const wchar_t *password, size_t charCount) ! 105: { ! 106: if (charCount > MaxSize) ! 107: throw PasswordTooLong (SRC_POS); ! 108: ! 109: union Conv ! 110: { ! 111: byte b[sizeof (wchar_t)]; ! 112: wchar_t c; ! 113: }; ! 114: ! 115: Conv conv; ! 116: conv.c = L'A'; ! 117: ! 118: int lsbPos = -1; ! 119: for (size_t i = 0; i < sizeof (conv.b); ++i) ! 120: { ! 121: if (conv.b[i] == L'A') ! 122: { ! 123: lsbPos = i; ! 124: break; ! 125: } ! 126: } ! 127: ! 128: if (lsbPos == -1) ! 129: throw ParameterIncorrect (SRC_POS); ! 130: ! 131: bool unportable = false; ! 132: byte passwordBuf[MaxSize]; ! 133: for (size_t i = 0; i < charCount; ++i) ! 134: { ! 135: conv.c = password[i]; ! 136: passwordBuf[i] = conv.b[lsbPos]; ! 137: for (int j = 0; j < sizeof (wchar_t); ++j) ! 138: { ! 139: if (j != lsbPos && conv.b[j] != 0) ! 140: unportable = true; ! 141: } ! 142: } ! 143: ! 144: Set (passwordBuf, charCount); ! 145: ! 146: if (unportable) ! 147: Unportable = true; ! 148: } ! 149: ! 150: void VolumePassword::Set (const ConstBufferPtr &password) ! 151: { ! 152: Set (password, password.Size()); ! 153: } ! 154: ! 155: void VolumePassword::Set (const VolumePassword &password) ! 156: { ! 157: Set (password.DataPtr(), password.Size()); ! 158: } ! 159: ! 160: TC_SERIALIZER_FACTORY_ADD_CLASS (VolumePassword); ! 161: ! 162: #define TC_EXCEPTION(TYPE) TC_SERIALIZER_FACTORY_ADD(TYPE) ! 163: #undef TC_EXCEPTION_NODECL ! 164: #define TC_EXCEPTION_NODECL(TYPE) TC_SERIALIZER_FACTORY_ADD(TYPE) ! 165: ! 166: TC_SERIALIZER_FACTORY_ADD_EXCEPTION_SET (PasswordException); ! 167: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.