VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Main/UserPreferences.cpp
AgeCommit message (Expand)AuthorFilesLines
2020-06-26Linux/MacOSX: fix warning reported by static code analysis tool about possibl...Mounir IDRASSI1-2/+2
2017-06-23Update IDRIX copyright yearMounir IDRASSI1-1/+1
2016-05-10Remove trailing whitespaceDavid Foerster1-13/+13
2016-01-20Copyright: update dates to include 2016.Mounir IDRASSI1-1/+1
2015-08-06Update license information to reflect the use of a dual license Apache 2.0 an...Mounir IDRASSI1-4/+8
2015-06-24Linux/MacOSX: restore normal file mode to some source filesMounir IDRASSI1-0/+0
2015-02-28Linux/MacOSX: A configuration option for default hash and default TrueCrypt m...Mounir IDRASSI1-0/+25
2014-11-08Change namespace from TrueCrypt to VeraCrypt. Rename method from Resources Re...Mounir IDRASSI1-1/+1
2014-11-08wxWidgets 3.0 compatibility modificationsMounir IDRASSI1-1/+1
2014-11-08Add TrueCrypt 7.1a MacOSX/Linux specific source files.Mounir IDRASSI1-0/+235
'>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
/*
 Derived from source code of TrueCrypt 7.1a, which is
 Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
 by the TrueCrypt License 3.0.

 Modifications and additions to the original source code (contained in this file)
 and all other portions of this file are Copyright (c) 2013-2017 IDRIX
 and are governed by the Apache License 2.0 the full text of which is
 contained in the file License.txt included in VeraCrypt binary and source
 code distribution packages.
*/

#include "MountOptions.h"
#include "Platform/MemoryStream.h"
#include "Platform/SerializerFactory.h"

namespace VeraCrypt
{
	void MountOptions::CopyFrom (const MountOptions &other)
	{
#define TC_CLONE(NAME) NAME = other.NAME
#define TC_CLONE_SHARED(TYPE,NAME) NAME = other.NAME ? make_shared <TYPE> (*other.NAME) : shared_ptr <TYPE> ()

		TC_CLONE (CachePassword);
		TC_CLONE (FilesystemOptions);
		TC_CLONE (FilesystemType);
		TC_CLONE_SHARED (KeyfileList, Keyfiles);
		TC_CLONE_SHARED (DirectoryPath, MountPoint);
		TC_CLONE (NoFilesystem);
		TC_CLONE (NoHardwareCrypto);
		TC_CLONE (NoKernelCrypto);
		TC_CLONE_SHARED (VolumePassword, Password);
		TC_CLONE (Pim);
		if (other.Kdf)
		{
			Kdf.reset(other.Kdf->Clone());
		}
		else
			Kdf.reset();
		TC_CLONE_SHARED (VolumePath, Path);
		TC_CLONE (PartitionInSystemEncryptionScope);
		TC_CLONE (PreserveTimestamps);
		TC_CLONE (Protection);
		TC_CLONE_SHARED (VolumePassword, ProtectionPassword);
		TC_CLONE (ProtectionPim);
		if (other.ProtectionKdf)
			ProtectionKdf.reset(other.ProtectionKdf->Clone());
		else
			ProtectionKdf.reset();
		TC_CLONE_SHARED (KeyfileList, ProtectionKeyfiles);
		TC_CLONE (Removable);
		TC_CLONE (SharedAccessAllowed);
		TC_CLONE (SlotNumber);
		TC_CLONE (UseBackupHeaders);
		TC_CLONE (TrueCryptMode);
	}

	void MountOptions::Deserialize (shared_ptr <Stream> stream)
	{
		Serializer sr (stream);
		wstring nameValue;

		sr.Deserialize ("CachePassword", CachePassword);
		sr.Deserialize ("FilesystemOptions", FilesystemOptions);
		sr.Deserialize ("FilesystemType", FilesystemType);

		Keyfiles = Keyfile::DeserializeList (stream, "Keyfiles");

		if (!sr.DeserializeBool ("MountPointNull"))
			MountPoint.reset (new DirectoryPath (sr.DeserializeWString ("MountPoint")));
		else
			MountPoint.reset();

		sr.Deserialize ("NoFilesystem", NoFilesystem);
		sr.Deserialize ("NoHardwareCrypto", NoHardwareCrypto);
		sr.Deserialize ("NoKernelCrypto", NoKernelCrypto);

		if (!sr.DeserializeBool ("PasswordNull"))
			Password = Serializable::DeserializeNew <VolumePassword> (stream);
		else
			Password.reset();

		if (!sr.DeserializeBool ("PathNull"))
			Path.reset (new VolumePath (sr.DeserializeWString ("Path")));
		else
			Path.reset();

		sr.Deserialize ("PartitionInSystemEncryptionScope", PartitionInSystemEncryptionScope);
		sr.Deserialize ("PreserveTimestamps", PreserveTimestamps);

		Protection = static_cast <VolumeProtection::Enum> (sr.DeserializeInt32 ("Protection"));

		if (!sr.DeserializeBool ("ProtectionPasswordNull"))
			ProtectionPassword = Serializable::DeserializeNew <VolumePassword> (stream);
		else
			ProtectionPassword.reset();

		ProtectionKeyfiles = Keyfile::DeserializeList (stream, "ProtectionKeyfiles");
		sr.Deserialize ("Removable", Removable);
		sr.Deserialize ("SharedAccessAllowed", SharedAccessAllowed);
		sr.Deserialize ("SlotNumber", SlotNumber);
		sr.Deserialize ("UseBackupHeaders", UseBackupHeaders);

		sr.Deserialize ("TrueCryptMode", TrueCryptMode);

		try
		{
			if (!sr.DeserializeBool ("KdfNull"))
			{
				sr.Deserialize ("Kdf", nameValue);
				Kdf = Pkcs5Kdf::GetAlgorithm (nameValue, TrueCryptMode);
			}
		}
		catch(...) {}

		try
		{
			if (!sr.DeserializeBool ("ProtectionKdfNull"))
			{
				sr.Deserialize ("ProtectionKdf", nameValue);
				ProtectionKdf = Pkcs5Kdf::GetAlgorithm (nameValue, TrueCryptMode);
			}
		}
		catch(...) {}

		sr.Deserialize ("Pim", Pim);
		sr.Deserialize ("ProtectionPim", ProtectionPim);
	}

	void MountOptions::Serialize (shared_ptr <Stream> stream) const
	{
		Serializable::Serialize (stream);
		Serializer sr (stream);

		sr.Serialize ("CachePassword", CachePassword);
		sr.Serialize ("FilesystemOptions", FilesystemOptions);
		sr.Serialize ("FilesystemType", FilesystemType);
		Keyfile::SerializeList (stream, "Keyfiles", Keyfiles);

		sr.Serialize ("MountPointNull", MountPoint == nullptr);
		if (MountPoint)
			sr.Serialize ("MountPoint", wstring (*MountPoint));

		sr.Serialize ("NoFilesystem", NoFilesystem);
		sr.Serialize ("NoHardwareCrypto", NoHardwareCrypto);
		sr.Serialize ("NoKernelCrypto", NoKernelCrypto);

		sr.Serialize ("PasswordNull", Password == nullptr);
		if (Password)
			Password->Serialize (stream);

		sr.Serialize ("PathNull", Path == nullptr);
		if (Path)
			sr.Serialize ("Path", wstring (*Path));

		sr.Serialize ("PartitionInSystemEncryptionScope", PartitionInSystemEncryptionScope);
		sr.Serialize ("PreserveTimestamps", PreserveTimestamps);
		sr.Serialize ("Protection", static_cast <uint32> (Protection));

		sr.Serialize ("ProtectionPasswordNull", ProtectionPassword == nullptr);
		if (ProtectionPassword)
			ProtectionPassword->Serialize (stream);

		Keyfile::SerializeList (stream, "ProtectionKeyfiles", ProtectionKeyfiles);
		sr.Serialize ("Removable", Removable);
		sr.Serialize ("SharedAccessAllowed", SharedAccessAllowed);
		sr.Serialize ("SlotNumber", SlotNumber);
		sr.Serialize ("UseBackupHeaders", UseBackupHeaders);

		sr.Serialize ("TrueCryptMode", TrueCryptMode);

		sr.Serialize ("KdfNull", Kdf == nullptr);
		if (Kdf)
			sr.Serialize ("Kdf", Kdf->GetName());

		sr.Serialize ("ProtectionKdfNull", ProtectionKdf == nullptr);
		if (ProtectionKdf)
			sr.Serialize ("ProtectionKdf", ProtectionKdf->GetName());

		sr.Serialize ("Pim", Pim);
		sr.Serialize ("ProtectionPim", ProtectionPim);
	}

	TC_SERIALIZER_FACTORY_ADD_CLASS (MountOptions);
}