From efa436974d8203485eec6faa1bf0116bb32f6fcd Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Mon, 30 Nov 2015 11:35:41 +0100 Subject: Linux/MacOSX: Implement Unicode passwords suppport. Make validation of parameters in GUI more robust. --- src/Main/CommandLineInterface.cpp | 42 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'src/Main/CommandLineInterface.cpp') diff --git a/src/Main/CommandLineInterface.cpp b/src/Main/CommandLineInterface.cpp index 3563f0b0..1b2ec46a 100644 --- a/src/Main/CommandLineInterface.cpp +++ b/src/Main/CommandLineInterface.cpp @@ -376,7 +376,7 @@ namespace VeraCrypt ArgNewKeyfiles = ToKeyfileList (str); if (parser.Found (L"new-password", &str)) - ArgNewPassword.reset (new VolumePassword (wstring (str))); + ArgNewPassword = ToUTF8Password (str); if (parser.Found (L"new-pim", &str)) { @@ -415,7 +415,7 @@ namespace VeraCrypt { if (Preferences.UseStandardInput) throw_err (L"--password cannot be used with --stdin"); - ArgPassword.reset (new VolumePassword (wstring (str))); + ArgPassword = ToUTF8Password (str); } if (parser.Found (L"pim", &str)) @@ -456,7 +456,7 @@ namespace VeraCrypt if (parser.Found (L"protection-password", &str)) { - ArgMountOptions.ProtectionPassword.reset (new VolumePassword (wstring (str))); + ArgMountOptions.ProtectionPassword = ToUTF8Password (str); ArgMountOptions.Protection = VolumeProtection::HiddenVolumeReadOnly; } @@ -713,5 +713,41 @@ namespace VeraCrypt return filteredVolumes; } + shared_ptr ToUTF8Password (const wchar_t* str, size_t charCount) + { + if (charCount > 0) + { + shared_ptr utf8Buffer = ToUTF8Buffer (str, charCount); + return shared_ptr(new VolumePassword (*utf8Buffer)); + } + else + return shared_ptr(new VolumePassword ()); + } + + shared_ptr ToUTF8Buffer (const wchar_t* str, size_t charCount) + { + if (charCount == (size_t) -1) + charCount = wcslen (str); + + if (charCount > 0) + { + wxMBConvUTF8 utf8; + size_t ulen = utf8.FromWChar (NULL, 0, str, charCount); + if (wxCONV_FAILED == ulen) + throw PasswordUTF8Invalid (SRC_POS); + SecureBuffer passwordBuf(ulen); + ulen = utf8.FromWChar ((char*) (byte*) passwordBuf, ulen, str, charCount); + if (wxCONV_FAILED == ulen) + throw PasswordUTF8Invalid (SRC_POS); + if (ulen > VolumePassword::MaxSize) + throw PasswordUTF8TooLong (SRC_POS); + + ConstBufferPtr utf8Buffer ((byte*) passwordBuf, ulen); + return shared_ptr(new SecureBuffer (utf8Buffer)); + } + else + return shared_ptr(new SecureBuffer ()); + } + auto_ptr CmdLine; } -- cgit v1.2.3