I have a program which connects to the wireless networks. It combines the Win API and Qt API. The problem is that it fails to connect for example to the network with SSID: Escritório. For other networks with only English characters it connects successfully. So, it seems the issue is with QString conversion containing this letter: ó.
Code:
QString securedAPProfile(QString profileName, QString apName, _DOT11_AUTH_ALGORITHM authAlgorithm, _DOT11_CIPHER_ALGORITHM encryption, QString password, bool hiddenAP, bool isAutoConnection)
{
QString xmlProfile = QString("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>%1</name><SSIDConfig><SSID><hex>%2</hex><name>%3</name></SSID><nonBroadcast>%4</nonBroadcast></SSIDConfig><connectionType>ESS</connectionType><connectionMode>%5</connectionMode><MSM><security><authEncryption><authentication>%6</authentication><encryption>%7</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>%8</keyType><protected>false</protected><keyMaterial>%9</keyMaterial></sharedKey></security></MSM></WLANProfile>").arg(profileName, getSSIDHex(apName), apName, checkHiddenAP(hiddenAP), checkAutoConnection(isAutoConnection), checkAuthentication(authAlgorithm), checkEncryption(encryption), checkKeyType(encryption), password);
return xmlProfile;
}
LPCWSTR wlanProfile = reinterpret_cast<LPCWSTR>(profileName.utf16());
DWORD dwResult = WlanSetProfile(hClient, &adapterGUID, NULL, reinterpret_cast<LPCWSTR>(securedAPProfile(profileName, apName, authAlgorithm, encryption, password, hiddenAP, isAutoConnection).utf16()), nullptr, TRUE, nullptr, &wlanReasonCode);
WLAN_CONNECTION_PARAMETERS connectionParameters;
memset(&connectionParameters, NULL, sizeof(WLAN_CONNECTION_PARAMETERS));
connectionParameters.wlanConnectionMode = wlan_connection_mode_profile;
connectionParameters.strProfile = wlanProfile;
It displays the following error: "The specific network is not available. (163851)" and does not connect to this network. Using Windows interface, it successfully connects. I have tried to use different QString methods including the QTextCodec::codecForName method with UTF-8/Windows-1251 encoding.
QString xmlProfileName = QTextCodec::codecForName("Windows-1251")->toUnicode(profileName.toLocal8Bit());
QString xmlAPName = QTextCodec::codecForName("Windows-1251")->toUnicode(apName.toLocal8Bit());
Then set these variables to the profile as arguments but no result:
It leads to WlanConnect: 87 code, which means that wireless profile is not valid or could be corrupted.
Any ideas how to convert QString with ó character (or similar Spanish accent characters) to LPCWSTR? What encoding should I use to fix this issue? Thank you.
Finally! I have fixed this issue. The problem was with wrong SSID hex encoding in the wireless profile.
Code:
QString getHex(QString ssid)
{
const char hexDigits[] = "0123456789ABCDEF";
std::string hexSSID;
hexSSID.reserve(ssid.toStdString().length() * 2);
for (unsigned char ssidChar : ssid.toStdString()) {
hexSSID.push_back(hexDigits[ssidChar >> 4]);
hexSSID.push_back(hexDigits[ssidChar & 15]);
}
return QString::fromStdString(hexSSID).toUpper();
}
Now it connects to all networks names - Spanish/Cyrillic/English. The issue is resolved.
Related
There are other similar questions on this site, but they either do not related to winsock2 or they are suitable only for use with ipv4 address spaces. The default compiler for Visual Studio 2019 produces an error when the ntoa function is used, hence an ipv4 and ipv6 solution is required.
I did once produce the code to do this for a Linux system however I am currently at work and do not have access to that. It may or may not be "copy and paste"-able into a windows environment with winsock2. (Edit: I will of course add that code later this evening, but of course it might not be useful.)
The following contains an example, however this is an example for client side code, not server side code.
https://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedInternet3c.html
Here, the getaddrinfo() function is used to obtain a structure containing matching ipv4 and ipv6 addresses. To obtain this information there is some interaction with DNS, which is not required in this case.
I have some server code which calls accept() (after bind and listen) to accept a client connection. I want to be able to print the client ip address and port to stdout.
The most closely related question on this site is here. However the answer uses ntoa and is only ipv4 compatible.
What I have so far:
So far I have something sketched out like this:
SOCKET acceptSocket = INVALID_SOCKET;
SOCKADDR_IN addr; // both of these are NOT like standard unix sockets
// I don't know how they differ and if they can be used with standard
// unix like function calls (eg: inet_ntop)
int addrlen = sizeof addr;
acceptSocket = accept(listenSocket, (SOCKADDR*)&addr, &addrlen);
if(acceptSocket == INVALID_SOCKET)
{
// some stuff
}
else
{
const std::size_t addrbuflen = INET6_ADDRSRTLEN;
char addrbuf[addrbuflen] = '\0'
inet_ntop(AF_INET, (void*)addr.sin_addr, (PSTR)addrbuf, addrbuflen);
// above line does not compile and mixes unix style function calls
// with winsock2 structures
std::cout << addrbuf << ':' << addr.sin_port << std::endl;
}
getpeername()
int ret = getpeername(acceptSocket, addrbuf, &addrbuflen);
// addrbuf cannot convert from char[65] to sockaddr*
if(ret == ???)
{
// TODO
}
You need to access the SOCKADDR. This is effectively a discriminated union. The first field tells you whether its an IPv4 (==AF_INET) or IPv6 (==AF_INET6) address. Depending on that you cast the addr pointer to be either struct sockaddr_in* or struct sockaddr_in6*, and then read off the IP address from the relevant field.
C++ code snippet in vs2019:
char* CPortListener::get_ip_str(struct sockaddr* sa, char* s, size_t maxlen)
{
switch (sa->sa_family) {
case AF_INET:
inet_ntop(AF_INET, &(((struct sockaddr_in*)sa)->sin_addr),
s, maxlen);
break;
case AF_INET6:
inet_ntop(AF_INET6, &(((struct sockaddr_in6*)sa)->sin6_addr),
s, maxlen);
break;
default:
strncpy(s, "Unknown AF", maxlen);
return NULL;
}
return s;
}
Example:
{
...
char s[INET6_ADDRSTRLEN];
sockaddr_storage ca;
socklen_t al = sizeof(ca);
SOCKET recv = accept(sd, (sockaddr*)&ca, &al);
pObj->m_ip = get_ip_str(((sockaddr*)&ca),s,sizeof(s));
}
I'm trying to change the name of the computer programmatically. Occasionally we have to wipe a system and restore it's database in an upgrade. I'm trying to have all the system settings be read out of the database and be set up automatically. Most of it is pretty simple stuff, but changing the name of the system is really throwing me for a loop.
EDIT: code edited to reflect changes from comments
if(dbHostName.length() > MAX_COMPUTERNAME_LENGTH)
{
dbHostName.truncate(MAX_COMPUTERNAME_LENGTH);
}
LPCTSTR cname = dbHostName.toStdWString().c_str();
bool nameset = SetComputerNameEx(ComputerNamePhysicalDnsHostname, cname);
if(nameset) qDebug() << "Computer name changed to" << dbHostName;
else qDebug() << "Computer name NOT changed!";
I'm taking a QString in, making sure it's not too long, converting it to a standard wide string, converting that to a LPCTSTR and then attempting to use that to change the computer name.
This returns false: Computer name not changed!
Giving credit to #user4581301 and #IInspectable for contributing the suggestions that led to the the below solutions. Both of these worked, I chose the second one because there does not appear to be an agreement on how best to convert a string to an LPCTSTR object.
if(dbHostName.length() > MAX_COMPUTERNAME_LENGTH)
{
dbHostName.truncate(MAX_COMPUTERNAME_LENGTH);
}
std::wstring wstring = dbHostName.toStdWString();
LPCTSTR cname = wstring.c_str();
SetComputerNameEx(ComputerNamePhysicalDnsHostname, cname);
And this is the actual solution I selected, but again, they both worked on Windows 8.1.
if(dbHostName.length() > MAX_COMPUTERNAME_LENGTH)
{
dbHostName.truncate(MAX_COMPUTERNAME_LENGTH);
}
std::string sname = dbHostName.toStdString();
LPCSTR cname = sname.c_str();
SetComputerNameExA(ComputerNamePhysicalDnsHostname, cname);
Edit 5/24/18: Incidentally this also works, and is much more concise
bool nameSet = SetComputerNameEx(ComputerNamePhysicalDnsHostname, dbHostName.toStdWString().c_str());
I'm facing a rather interesting issue in regards to Authenticode signing an UWP appxbundle file.
Some background:
The client provided us with a SafeNet USB token containing the signing certificate. The private key is not exportable, of course. I want to be able to use this certificate for our automated release builds to sign the package. Unfortunately, the token requires a PIN to be entered once per session, so for example if the build agent reboots, the build will fail. We enabled single login on the token so it's enough to unlock it once a session.
Current state:
We can use signtool on the appxbundle without any problems, given the token has been unlocked. This works well enough but breaks as soon as the machine is rebooted or the workstation is locked.
After some searching I managed to find this piece of code. This takes the signing parameters (including the token PIN) and invokes Windows API to sign the target file. I managed to compile this and it worked flawlessly for signing the installation wrapper (EXE file) - the token did not ask for PIN and was unlocked automatically by the API call.
However, when I invoked the same code on the appxbundle file, the call to CryptUIWizDigitalSign failed with error code 0x80080209 APPX_E_INVALID_SIP_CLIENT_DATA. This is a mystery to me because invoking signtool on the same bundle, with the same parameters/certificate works without problem so the certificate should be fully compatible with the package.
Does anyone have experience with something like this? Is there a way to figure out what is the root cause of the error (what is incompatible between my cert and the bundle)?
EDIT 1
In response to a comment:
The code I'm using to call the APIs (taken directly from the aforementioned SO question)
#include <windows.h>
#include <cryptuiapi.h>
#include <iostream>
#include <string>
#pragma comment (lib, "cryptui.lib")
const std::wstring ETOKEN_BASE_CRYPT_PROV_NAME = L"eToken Base Cryptographic Provider";
std::string utf16_to_utf8(const std::wstring& str)
{
if (str.empty())
{
return "";
}
auto utf8len = ::WideCharToMultiByte(CP_UTF8, 0, str.data(), str.size(), NULL, 0, NULL, NULL);
if (utf8len == 0)
{
return "";
}
std::string utf8Str;
utf8Str.resize(utf8len);
::WideCharToMultiByte(CP_UTF8, 0, str.data(), str.size(), &utf8Str[0], utf8Str.size(), NULL, NULL);
return utf8Str;
}
struct CryptProvHandle
{
HCRYPTPROV Handle = NULL;
CryptProvHandle(HCRYPTPROV handle = NULL) : Handle(handle) {}
~CryptProvHandle() { if (Handle) ::CryptReleaseContext(Handle, 0); }
};
HCRYPTPROV token_logon(const std::wstring& containerName, const std::string& tokenPin)
{
CryptProvHandle cryptProv;
if (!::CryptAcquireContext(&cryptProv.Handle, containerName.c_str(), ETOKEN_BASE_CRYPT_PROV_NAME.c_str(), PROV_RSA_FULL, CRYPT_SILENT))
{
std::wcerr << L"CryptAcquireContext failed, error " << std::hex << std::showbase << ::GetLastError() << L"\n";
return NULL;
}
if (!::CryptSetProvParam(cryptProv.Handle, PP_SIGNATURE_PIN, reinterpret_cast<const BYTE*>(tokenPin.c_str()), 0))
{
std::wcerr << L"CryptSetProvParam failed, error " << std::hex << std::showbase << ::GetLastError() << L"\n";
return NULL;
}
auto result = cryptProv.Handle;
cryptProv.Handle = NULL;
return result;
}
int wmain(int argc, wchar_t** argv)
{
if (argc < 6)
{
std::wcerr << L"usage: etokensign.exe <certificate file path> <private key container name> <token PIN> <timestamp URL> <path to file to sign>\n";
return 1;
}
const std::wstring certFile = argv[1];
const std::wstring containerName = argv[2];
const std::wstring tokenPin = argv[3];
const std::wstring timestampUrl = argv[4];
const std::wstring fileToSign = argv[5];
CryptProvHandle cryptProv = token_logon(containerName, utf16_to_utf8(tokenPin));
if (!cryptProv.Handle)
{
return 1;
}
CRYPTUI_WIZ_DIGITAL_SIGN_EXTENDED_INFO extInfo = {};
extInfo.dwSize = sizeof(extInfo);
extInfo.pszHashAlg = szOID_NIST_sha256; // Use SHA256 instead of default SHA1
CRYPT_KEY_PROV_INFO keyProvInfo = {};
keyProvInfo.pwszContainerName = const_cast<wchar_t*>(containerName.c_str());
keyProvInfo.pwszProvName = const_cast<wchar_t*>(ETOKEN_BASE_CRYPT_PROV_NAME.c_str());
keyProvInfo.dwProvType = PROV_RSA_FULL;
CRYPTUI_WIZ_DIGITAL_SIGN_CERT_PVK_INFO pvkInfo = {};
pvkInfo.dwSize = sizeof(pvkInfo);
pvkInfo.pwszSigningCertFileName = const_cast<wchar_t*>(certFile.c_str());
pvkInfo.dwPvkChoice = CRYPTUI_WIZ_DIGITAL_SIGN_PVK_PROV;
pvkInfo.pPvkProvInfo = &keyProvInfo;
CRYPTUI_WIZ_DIGITAL_SIGN_INFO signInfo = {};
signInfo.dwSize = sizeof(signInfo);
signInfo.dwSubjectChoice = CRYPTUI_WIZ_DIGITAL_SIGN_SUBJECT_FILE;
signInfo.pwszFileName = fileToSign.c_str();
signInfo.dwSigningCertChoice = CRYPTUI_WIZ_DIGITAL_SIGN_PVK;
signInfo.pSigningCertPvkInfo = &pvkInfo;
signInfo.pwszTimestampURL = timestampUrl.c_str();
signInfo.pSignExtInfo = &extInfo;
if (!::CryptUIWizDigitalSign(CRYPTUI_WIZ_NO_UI, NULL, NULL, &signInfo, NULL))
{
std::wcerr << L"CryptUIWizDigitalSign failed, error " << std::hex << std::showbase << ::GetLastError() << L"\n";
return 1;
}
std::wcout << L"Successfully signed " << fileToSign << L"\n";
return 0;
}
The certificate is a CER file (public portion only) exported from the token and the container name is taken from the token's info. As I mentioned, this works correctly for EXE files.
The signtool command
signtool sign /sha1 "cert thumbprint" /fd SHA256 /n "subject name" /t "http://timestamp.verisign.com/scripts/timestamp.dll" /debug "$path"
This also works, when I call it either manually or from the CI build when the token is unlocked. But the code above fails with the mentioned error.
EDIT 2
Thanks to all of you, I now have a working implementation! I ended up using the SignerSignEx2 API, as suggested by RbMm. This seems to work fine for both appx bundles and PE files (different parameters for each). Verified on Windows 10 with a TFS 2017 build agent - unlocks the token, finds a specified certificate in the cert store, and signs+timestamps the specified file.
I published the result on GitHub, if anyone is interested: https://github.com/mareklinka/SafeNetTokenSigner
first of all i look where CryptUIWizDigitalSign failed:
the CryptUIWizDigitalSign called SignerSignEx function, with pSipData == 0. for sign PE file (exe, dll, sys) - this is ok and will be work. but for appxbundle (zip archive file type) this parameter mandatory and must point to APPX_SIP_CLIENT_DATA: for appxbundle call stack is
CryptUIWizDigitalSign
SignerSignEx
HRESULT Appx::Packaging::AppxSipClientData::Initialize(SIP_SUBJECTINFO* subjectInfo)
at very begin of Appx::Packaging::AppxSipClientData::Initialize we can view next code:
if (!subjectInfo->pClientData) return APPX_E_INVALID_SIP_CLIENT_DATA;
this is exactly where your code fail.
instead of CryptUIWizDigitalSign need direct call SignerSignEx2 and pSipData is mandatory parameter in this case.
in msdn exist full worked example - How to programmatically sign an app package (C++)
the key point here:
APPX_SIP_CLIENT_DATA sipClientData = {};
sipClientData.pSignerParams = &signerParams;
signerParams.pSipData = &sipClientData;
the modern SignTool call SignerSignEx2 direct:
here again clear visible:
if (!subjectInfo->pClientData) return APPX_E_INVALID_SIP_CLIENT_DATA;
after this called
HRESULT Appx::Packaging::Packaging::SignFile(
PCWSTR FileName, APPX_SIP_CLIENT_DATA* sipClientData)
here at begin next code:
if (!sipClientData->pSignerParams) return APPX_E_INVALID_SIP_CLIENT_DATA;
this clear stated in msdn:
You must provide a pointer to an APPX_SIP_CLIENT_DATA structure as
the pSipData parameter when you sign an app package. You must
populate the pSignerParams member of APPX_SIP_CLIENT_DATA with
the same parameters that you use to sign the app package. To do this,
define your desired parameters on the SIGNER_SIGN_EX2_PARAMS
structure, assign the address of this structure to pSignerParams,
and then directly reference the structure's members as well when you
call SignerSignEx2.
question - why need again provide the same parameters, which used in call SignerSignEx2 ? because appxbundle is really archive, which containing multiple files. and every file need be sign. for this Appx::Packaging::Packaging::SignFile recursive call SignerSignEx2 again :
for this recursive calls pSignerParams and used - for call SignerSignEx2 with exactly the same parameters as top call
I am attempting to detect current system language with QLocale:
QLocale::Language sysLangId = QLocale::system().language();
However, it's not working correctly. I'm on Russian Windows 7 with English language pack applied, but language() returns Russian instead of English. Is there any workaround?
When I was working on Localization in Qt, I used
QString locale = QLocale::system().name();
When I tested getting the locale, I found it was dependent on the Format in the Region and Language settings:
Control Panel > Region and Language > Format
Hope that helps.
I've found 2 ways to solve my problem. The Qt way is to use QLocale::system().uiLanguages().
On my system it returns a list with a single item "en-US". The problem with that is I need a language name, like "english", so I'd have to add a map for converting language code to language name. It's no big deal, but I've decided to use WinAPI:
QString sysLangName;
const LANGID langId = GetUserDefaultUILanguage();
WCHAR langName[1000] = {0};
if (GetLocaleInfoW(MAKELCID(langId, SORT_DEFAULT), LOCALE_SENGLANGUAGE, langName, sizeof langName / sizeof langName[0] - 1) != 0)
sysLangName = QString::fromWcharArray(langName);
I had the same problem and I solved with this code.
QString local = QLocale::languageToString(QLocale::system().language());
To get the language name you can simply use QLocale::languageToString(QLocale::system().language()); or maybe QLocale::system().nativeLanguageName(); but the real problem is as you mentioned that the QLocale::system() does not always match the actual system locale on windows. This can be observed if you change the locale during program execution. In this case the QLocale::system() does not get up-to-date and returns the old value. Here is a workaround I used in Qt5:
class WinEventFilter : public QAbstractNativeEventFilter
{
public:
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result)
{
if (((MSG*)message)->message == WM_WININICHANGE )
{
// Workaround - in Qt5 the system locale is not up to date and we have to manually update it.
#ifdef _DEBUG
QLibrary lib("Qt5Cored.dll");
#else
QLibrary lib("Qt5Core.dll");
#endif
void (* func)() = lib.resolve("?updateSystemPrivate#QLocalePrivate##SAXXZ");
if (func)
func();
else
qDebug()<<"! Unable to resolve updateSystemPrivate()";
// Workaround end
qDebug()<<"WM_WININICHANGE"<<QLocale::languageToString(QLocale::system().language());
}
return false;
}
};
and my application class constructor looks like this:
MyApplication::MyApplication( int & argc, char ** argv )
: QApplication(argc, argv)
{
WinEventFilter *pFilter = new WinEventFilter(this);
installNativeEventFilter(m_pEventFilter);
}
Hope this helps.
I'm trying to wrap libssh2 in Qt, and have the following code:
const char* username = inUsername.toLocal8Bit().data();
const char* password = inPass.toLocal8Bit().data();
Problem is, that username and password doesn't connect to the system. Why?
Because, according to the debugger,
username "5.1p1 Debian-6ubuntu2"
password "5.1p1 Debian-6ubuntu2"
Those are not the values I've given for the username or password. I've tried toAscii, toLatin1, and appending (or not) the .data(). Still, I get these values, instead of the expected values. I'm on Windows, which is why it's even more troubling, since, as far as I can tell, nothing I have was compiled on Debian or Ubuntu.
What's going on here?
This code:
const char* username = inUsername.toLocal8Bit().data();
is equivalent to this:
const char * username;
{
const QByteArray l8b = inUsername.toLocal8Bit();
username = l8b.data();
}
Do you see what's going on? By the time the statement has executed, the temporary QByteArray has been deleted by the compiler again. Since data() only returns a pointer to the internal QByteArray buffer, username now points to deleted/freed memory.
To solve the problem, make username and password QByteArrays instead of const char*s, and use username.data(), password.data() instead where you used username, password before.