SSL signature verrification cross language issue - c++

I have the following code in an C websocket server application I have. The code performs ssl signature verification on a message with a given public key. The code works fine in the C application, but recently I started writing it on c++.The issue I encountered is that the same code, that is below, is in both applications, without change, both times receiving the same input data, but the one compiled with c++ yields SSL error bad signature.
Here is the code:
int verifyMessageSignature(const char* decoded_message, int pos,
unsigned char* signature, char* publicKey)
{
SSL_library_init();
SSL_load_error_strings();
ERR_load_BIO_strings();
// OpenSSL_add_all_algorithms()
if (!publicKey)
{
printf("publicKey is null\n");
}
BIO* keyBio = BIO_new_mem_buf(publicKey, -1);
if(!keyBio)
{
printf("failed to created BIO\n");
printError(ERR_get_error());
}
BIO_set_mem_eof_return(keyBio, 0);
RSA* rsa = PEM_read_bio_RSA_PUBKEY(keyBio, NULL, NULL, NULL);
if (!rsa)
{
printf("Error in PEM_read_bio_RSA_PUBKEY\n");
printError(ERR_get_error());
}
EVP_MD_CTX *mdctx = NULL;
if (!(mdctx = EVP_MD_CTX_create()))
{
printf("Error in ctx\n");
printError(ERR_get_error());
}
EVP_PKEY* pk = EVP_PKEY_new();
if (EVP_PKEY_set1_RSA(pk, rsa) != 1)
{
printf("err in EVP_PKEY_set1_RSA\n");
printError(ERR_get_error());
}
if (EVP_DigestVerifyInit(mdctx, NULL, EVP_sha1(), NULL, pk) != 1)
{
printf("error in EVP_DigestVerifyInit\n");
printError(ERR_get_error());
}
if (EVP_DigestVerifyUpdate(mdctx, decoded_message, pos) != 1)
{
printf("error in EVP_DigestVerifyUpdate\n");
printError(ERR_get_error());
}
if (EVP_DigestVerifyFinal(mdctx, signature, 512) == 1)
{
/* Success */
printf("Successful verification!\n");
}
else
{
/* Failure */
printf("Unsuccessful verification!\n");
printError(ERR_get_error());
BIO_free_all(keyBio);
RSA_free(rsa);
EVP_PKEY_free(pk);
EVP_MD_CTX_destroy(mdctx);
ERR_free_strings();
return 1;
}
BIO_free_all(keyBio);
RSA_free(rsa);
EVP_PKEY_free(pk);
EVP_MD_CTX_destroy(mdctx);
ERR_free_strings();
return 0;
}
This code works fine in C. it successfully verifies the signature in my tests, whilst the same code, with the same input data (keys, messages, etc..) in c++ yields bad signature.
I am compiling under Ubuntu, using gcc and g++ (latest)
What could be causing this issue?

Solved. The issue was that i was using std::string to pass the decoded message around, which somehow was fucking it up. I switched the c++ code to use also char* strings for this section and is fine now.
Thanks for the replies, I hope someone else finds this useful.
Edit:
PS: I am using another function for generating the decoded message itself. Usage of std::string there was causing this issue.

Related

C++ and OpenSSL Library: How can I set subjectAltName (SAN) from code?

I'm trying to create self-signed request with subjectAltName from c++ code (trying to implement dynamic self-signed certificates like this to actual version of OpenResty, but there is not sollution for subjectAltName).
Please, provide some examples of setting SANs from C++/OpenSSL code. I trying some like this:
X509_EXTENSION *ext;
STACK_OF (X509_EXTENSION) * extlist;
char *ext_name = "subjectAltName";
char *ext_value = "DNS:lohsport.com";
extlist = sk_X509_EXTENSION_new_null ();
ext = X509V3_EXT_conf (NULL, NULL, ext_name, ext_value);
if(ext == NULL)
{
*err = "Error creating subjectAltName extension";
goto failed;
}
sk_X509_EXTENSION_push (extlist, ext);
if (!X509_REQ_add_extensions (x509_req, extlist)){
*err = "Error adding subjectAltName to the request";
goto failed;
}
sk_X509_EXTENSION_pop_free (extlist, X509_EXTENSION_free);
It's compiling successfully but not works.
I would be grateful for any help.
UPDATE
Now i trying to work as in selfsing.c demo of OpenSSL Library:
1) I defined a function for adding extensions to CSR:
int add_ext(STACK_OF(X509_EXTENSION) *sk, int nid, char *value)
{
X509_EXTENSION *ex;
ex = X509V3_EXT_conf_nid(NULL, NULL, nid, value);
if (!ex)
return 0;
sk_X509_EXTENSION_push(sk, ex);
return 1;
}
2) Add this block to my function which generates CSR:
char Buffer[512];
// Format the value
sprintf (Buffer, "DNS:%s", info->common_name);
xts = sk_X509_EXTENSION_new_null();
add_ext(exts, NID_subject_alt_name, Buffer);
if(X509_REQ_add_extensions(x509_req, exts) != 1) {
*err = "X509_REQ_add_extensions() failed";
goto failed;
}
sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
The code again compiles correctly, certificates are generated on the fly, but alternative names still don't work, and I get an error in the browser:
NET :: ERR_CERT_COMMON_NAME_INVALID
and I don’t see the alternative name information in the certificate details.
What other solutions can there be for a SAN problem? I can provide all the code for example on githab if it can help.
Hello I have done something like that (but I am working with X509 object not X509_REQ object like you :
static int cs_cert_set_subject_alt_name(X509 *x509_cert)
{
char *subject_alt_name = "IP: 192.168.1.1";
X509_EXTENSION *extension_san = NULL;
ASN1_OCTET_STRING *subject_alt_name_ASN1 = NULL;
int ret = -1;
subject_alt_name_ASN1 = ASN1_OCTET_STRING_new();
if (!subject_alt_name_ASN1) {
goto err;
}
ASN1_OCTET_STRING_set(subject_alt_name_ASN1, (unsigned char*) subject_alt_name, strlen(subject_alt_name));
if (!X509_EXTENSION_create_by_NID(&extension_san, NID_subject_alt_name, 0, subject_alt_name_ASN1)) {
goto err;
}
ASN1_OCTET_STRING_free(subject_alt_name_ASN1);
ret = X509_add_ext(x509_cert, extension_san, -1);
if (!ret) {
goto err;
}
X509_EXTENSION_free(extension_san);
err:
if (subject_alt_name_ASN1) ASN1_OCTET_STRING_free(subject_alt_name_ASN1);
if (extension_san) X509_EXTENSION_free(extension_san);
return -1;
}
It worked for me for the moment, I still have some trouble when I want to update an already existing certificate with a new subject alt name (because of a new ip address).
To see the result and check if the subject alt name is made :
$ openssl x509 -text -in cert.pem
Instead of X509V3_EXT_conf function try with X509V3_EXT_conf_nid function in which you pass the NID instead of name.
ext = X509V3_EXT_conf (NULL, NULL, ext_name, ext_value);
can be
ext = X509V3_EXT_conf_nid(NULL, NULL, NID_subject_alt_name, ext_value);
Your code might not be working because you might not be exactly matching the extension name with the one in OpenSSL code.

error:0906D06C:PEM routines:PEM_read_bio:no start

Getting this very annoying error. error:0906D06C:PEM routines:PEM_read_bio:no start
Code:
RSA* publickey = cWrapper.getPublicKey("C:/rsa-stuff/public.pem");
QByteArray plain = "The man in the black fled into the desert and the gunslinger followed...";
QByteArray encrypted = cWrapper.encryptRSA(publickey, plain);
In encryptRSA():
const char* publicKeyStr = data.constData();
qDebug() << publicKeyStr;
BIO* bio = BIO_new_mem_buf((void*)publicKeyStr, -1);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
RSA* rsaPubKey = PEM_read_bio_RSAPublicKey(bio, NULL, NULL, NULL);
if(!rsaPubKey) {
qDebug() << "Could not load public key, " << ERR_error_string(ERR_get_error(), NULL); // error is here
}
BIO_free(bio);
This is how I read file:
QByteArray data;
QFile file(filename);
if(!file.open(QFile::ReadOnly))
{
printf("Error reading file: %s\n", file.errorString());
return data;
}
data = file.readAll();
file.close();
return data;
When I print out publicKeyStr, looks fine. This is notepad++ view with all characters enabled:
Anyone know what I am doing wrong? Super annoying issue :(
First of all, it's not this problem because I don't get the trusted part. Anyhow, I did try all the "solutions" and none of them worked, same error.
Your RSA public key is in SubjectPublicKeyInfo PEM format, but you are trying to read it using PEM_read_bio_RSAPublicKey which tries to read a PEM RSA key in PKCS#1 format. Try using PEM_read_bio_RSA_PUBKEY instead.
https://www.openssl.org/docs/man1.1.0/crypto/PEM_read_bio_RSAPublicKey.html
I got that same error on an openSSL1.1.0f I ported. The error showed up in my logger when reading out the root certificate from an mqtt client connection, until I figured out that I had forwarded the ERR_put_error() directly to my logger, whereas in openssl - the "real" error handling is kept in an ERR_STATE error buffer, and so sometimes (like in this case), errors are "expected", and the ERR_STATE error buffer is cleared (before anyone should check it).
in crypto/pem/pem_info.c, line 65:
i = PEM_read_bio(bp, &name, &header, &data, &len);
if (i == 0) {
error = ERR_GET_REASON(ERR_peek_last_error());
if (error == PEM_R_NO_START_LINE) {
ERR_clear_error();
break;
}
goto err;
meaning it runs througth the BIO_gets inside the PEM_read_bio until it returns zero, and if you get this PEM_R_NO_START_LINE, then thats just a way of saying its done.
By that time though, the error had already landed in my logger. So for anyone being confused by errors he or she is forwarding directly from ERR_put_error, use the ERR_print_errors_fp(stderr); in your errorhandling routine instead. In my case, as I dont have a stderr, I made a patched version of it, like:
void errorhandling()
{
unsigned long l;
char buf[256];
const char *file, *data;
int line, flags;
while ((l = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0)
{
ERR_error_string_n(l, buf, sizeof buf);
printf("%s:%s:%d:%s\n", buf, file, line, (flags & ERR_TXT_STRING) ? data : "");
}
}

How to avoid SIGABRT when generating RSA Signature at EVP_SignFinal

I'm trying to generate a RSA Signature with libopenssl for c++:
But when I run my code, I get a SIGABRT. I did some deep debugging into libopenssl internal stuff to see where the Segfault comes from. I'll come to this later on.
First I want to make clear, that the RSA PrivateKey was successfully loaded from a .pem file. So Im pretty sure that's not the problem's origin.
So my question is: How to avoid the SIGABRT and what is the cause of it ?
I'm doing this for my B.Sc. Thesis so I really appreciate your help :)
Signature Generation Function:
DocumentSignature* RSASignatureGenerator::generateSignature(ContentHash* ch, CryptographicKey* pK) throw(PDVSException) {
OpenSSL_add_all_algorithms();
OpenSSL_add_all_ciphers();
OpenSSL_add_all_digests();
if(pK == nullptr)
throw MissingPrivateKeyException();
if(pK->getKeyType() != CryptographicKey::KeyType::RSA_PRIVATE || !dynamic_cast<RSAPrivateKey*>(pK))
throw KeyTypeMissmatchException(pK->getPem()->getPath().string(), "Generate RSA Signature");
//get msg to encrypt
const char* msg = ch->getStringHash().c_str();
//get openssl rsa key
RSA* rsaPK = dynamic_cast<RSAPrivateKey*>(pK)->createOpenSSLRSAKeyObject();
//create openssl signing context
EVP_MD_CTX* rsaSignCtx = EVP_MD_CTX_create();
EVP_PKEY* priKey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(priKey, rsaPK);
//init ctxt
if (EVP_SignInit(rsaSignCtx, EVP_sha256()) <=0)
throw RSASignatureGenerationException();
//add data to sign
if (EVP_SignUpdate(rsaSignCtx, msg, std::strlen(msg)) <= 0) {
throw RSASignatureGenerationException();
}
//create result byte signature struct
DocumentSignature::ByteSignature* byteSig = new DocumentSignature::ByteSignature();
//set size to max possible
byteSig->size = EVP_MAX_MD_SIZE;
//alloc buffer memory
byteSig->data = (unsigned char*)malloc(byteSig->size);
//do signing
if (EVP_SignFinal(rsaSignCtx, byteSig->data, (unsigned int*) &byteSig->size, priKey) <= 0)
throw RSASignatureGenerationException();
DocumentSignature* res = new DocumentSignature(ch);
res->setByteSignature(byteSig);
EVP_MD_CTX_destroy(rsaSignCtx);
//TODO open SSL Memory leaks -> where to free open ssl stuff?!
return res;
}
RSA* rsaPK = dynamic_cast(pK)->createOpenSSLRSAKeyObject();
virtual RSA* createOpenSSLRSAKeyObject() throw (PDVSException) override {
RSA* rsa = NULL;
const char* c_string = _pem->getContent().c_str();
BIO * keybio = BIO_new_mem_buf((void*)c_string, -1);
if (keybio==NULL)
throw OpenSSLRSAPrivateKeyObjectCreationException(_pem->getPath());
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
if(rsa == nullptr)
throw OpenSSLRSAPrivateKeyObjectCreationException(_pem->getPath());
//BIO_free(keybio);
return rsa;
}
SigAbrt origin in file openssl/crypto/mem.c
void CRYPTO_free(void *str, const char *file, int line)
{
if (free_impl != NULL && free_impl != &CRYPTO_free) {
free_impl(str, file, line);
return;
}
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
if (call_malloc_debug) {
CRYPTO_mem_debug_free(str, 0, file, line);
free(str);
CRYPTO_mem_debug_free(str, 1, file, line);
} else {
free(str);
}
#else
free(str); // <<<<<<< HERE
#endif
}
the stacktrace
stacktrace screenshot from debugger (clion - gdb based)
I just found the Bug (and Im really not sure if this is a libopenssl bug..)
//set size to max possible
byteSig->size = EVP_MAX_MD_SIZE;
//alloc buffer memory
byteSig->data = (unsigned char*)malloc(byteSig->size);
The problem was when I set the buffer size to EVP_MAX_MD_SIZE!
The (in my opinion) very very strange thing is, that you have to keep the size uninitialized! (not even set to 0 - just "size_t size;" ).
Strange thing here is that then you also HAVE TO allocate memory just like I did. I dont understand this because then an undefined size of memory gets allocated..
What the really weird is that libopenssl internally sets the size back to 0 and allocates the memory itself.. (I detected this by browsing the libopenssl source code)

COM port handle for reading data in c++ vs2012

I have a code to open and read serial COM port in vs2012 c++ which is working fine when I run the code separately in an individual solution.The code is as follow:
Serial* SP = new Serial("\\\\.\\COM3"); // adjust as needed
if (SP->IsConnected()) // check com port availability
printf("We're connected"); // send the result
char incomingData[512] = ""; // don't forget to pre-allocate memory
int dataLength = 256;
int readResult = 0; //if there is no reading it is -1
while(SP->IsConnected())
{
readResult = SP->ReadData(incomingData,dataLength);
//std::string test(incomingData);
res1=strtol(incomingData,&pos1,10); //receive data in right patern
res2=atof(pos1); //convert the character to integer
res3=(double)res2; // convert integer to double (as my desired output is a double)
printf("%f\n",res2); // print the result
Sleep(50); // pause so that I can see the coming data
}
in which Serial,ReadData and other functions and headers are defined in a separate header and .cpp file.
My problem occurs when I want to plug the code in my other solution (SOFA Simulation) which I want to use to make a graphical interface. but I get the INVALID_HANDLE_VALUE error and the get last error gives me ERROR_FILE_NOT_FOUND. this is my code in the solution I want to use:
namespace sofa
{
namespace component
{
namespace behaviormodel
{
MyBehaviorModel::MyBehaviorModel():
customUnsignedData(initData(&customUnsignedData, (unsigned)1,"Custom Unsigned Data","Example of unsigned data with custom widget")),
regularUnsignedData(initData(&regularUnsignedData, (unsigned)1,"Unsigned Data","Example of unsigned data with standard widget"))
{
customUnsignedData.setWidget("widget_myData");
}
MyBehaviorModel::~MyBehaviorModel()
{
}
void MyBehaviorModel::init()
{
}
void MyBehaviorModel::reinit()
{
}
void MyBehaviorModel::updatePosition(SReal dt)
{
Serial* SP = new Serial("\\\\.\\COM3"); // adjust as needed
if (SP->IsConnected())
printf("We're connected");
char incomingData[512] = ""; // don't forget to pre-allocate memory
int dataLength = 256;
int readResult = 0;
while(SP->IsConnected())
{
readResult = SP->ReadData(incomingData,dataLength);
//std::string test(incomingData);
res1=strtol(incomingData,&pos1,10);
res2=atof(pos1);
res3=(double)res2;
printf("%f\n",res2);
Sleep(50);
}
dx=0.01;
dy=0.01;
dz+=0.01;
using core::behavior::MechanicalState;
mState1 = dynamic_cast<MechanicalState<sofa::defaulttype::Rigid3dTypes> *> (this->getContext()->getMechanicalState());
helper::WriteAccessor<sofa::core::objectmodel:: Data<sofa::defaulttype::Rigid3dTypes::VecCoord> > xp = *mState1- >write(core::VecCoordId::position());
xp[0].getCenter()=sofa::defaulttype::Vec<3,Real>((Real)dx,(Real)dy,(Real)(res2);
}
SOFA_DECL_CLASS(MyBehaviorModel)
int MyBehaviorModelClass = core::RegisterObject("Dummy component with a custom widget.").add< MyBehaviorModel >();
} // namespace behaviormodel
} // namespace component
} // namespace sofa
I really can not figure out what the problem is because as I said the problem is not from my serial reader code as I tested it and I know it works fine separately.can you find out where the problem lies?
thanks in advance!
This is my Serial constructor:
Serial::Serial(char *portName)
{//We're not yet connected
this->connected = false;
//Try to connect to the given port throuh CreateFile
this->hSerial = CreateFile((LPCWSTR)portName,
GENERIC_READ ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
//Check if the connection was successfull
if(this->hSerial==INVALID_HANDLE_VALUE)
{
//If not success full display an Error
if(GetLastError()==ERROR_FILE_NOT_FOUND){
//Print Error if neccessary
printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
}
else
{
printf("ERROR!!!");
}
}
else
{
//If connected we try to set the comm parameters
DCB dcbSerialParams = {0};
//Try to get the current
if (!GetCommState(this->hSerial, &dcbSerialParams))
{
//If impossible, show an error
printf("failed to get current serial parameters!");
}
else
{
//Define serial connection parameters for the arduino board
dcbSerialParams.BaudRate=CBR_9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
//Setting the DTR to Control_Enable ensures that the Arduino is properly
//reset upon establishing a connection
dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;
//Set the parameters and check for their proper application
if(!SetCommState(hSerial, &dcbSerialParams))
{
printf("ALERT: Could not set Serial Port parameters");
}
else
{
//If everything went fine we're connected
this->connected = true;
//Flush any remaining characters in the buffers
PurgeComm(this->hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR);
//We wait 2s as the arduino board will be reseting
Sleep(ARDUINO_WAIT_TIME);
}
}
}
}
The function CreateFile is actually a Macro that is mapped to either CreateFileA or CreateFileW depending on your project (unicode) configuration. As others have mentioned, you should not use a type cast to LPCWSTR to hide the fact that your code is not correct, you just need to use the right type of string.
If a function expects a widestring (LPCWSTR) and you pass it a chunk of memory that contains an ANSI string, it will never work. In this particular case, you can use the function CreateFileA directly so that you can pass your ANSI string to it.

How do I access services of UPnP device?

The device: Belkin Wemo Switch
Dev environment: MS VC++ 2010 on Windows7
I'm trying to enumerate the services of a UPnP device using C++ from Windows.
I've got the IUPnPDevice pointer and can access several properties.
I've got the IUPnPServices pointer and can count the correct number of services (7).
I use QueryInterface() to get the IEnumVARIANT pointer (which appears to succeed).
However, the Next() method always fails with HRESULT of 0x80040500 - which translates as Windows error 1280 (0x500) - ERROR_ALREADY_FIBER.
This error does not make any sense to me.
(I've tried using both IEnumVARIANT and IEnumUnknown - as the docs indicate it could be either, but both produce the same result.)
I've included below the complete source file, plus the output it produces.
[Note: It's hardcoded to use the udn of my own device]
I'd be very grateful if anyone can help as I'm currently stuck.
Best regards,
Dave
Code:
// UpnpTest1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <upnp.h>
static void DumpComError(const TCHAR *api, HRESULT hr);
int _tmain(int argc, _TCHAR* argv[])
{
int retcode=-1; // assume failure
HRESULT hr = CoInitialize(0);
if (hr==S_OK)
{
IUPnPDeviceFinder *deviceFinder=0;
hr = CoCreateInstance(CLSID_UPnPDeviceFinder, 0, CLSCTX_INPROC_SERVER, IID_IUPnPDeviceFinder, (void**)&deviceFinder);
if (hr==S_OK)
{
IUPnPDevice *device=0;
hr = deviceFinder->FindByUDN(L"uuid:Socket-1_0-221239K11002F6", &device);
if (hr==S_OK)
{
if (device)
{
TCHAR *manufacturer=0, *manufacturerUrl=0;
TCHAR *description=0, *name=0, *modelUrl=0;
TCHAR *serialNumber=0, *udn=0, *upc=0, *deviceType=0;
TCHAR *presentationUrl=0;
device->get_ManufacturerName(&manufacturer);
device->get_ManufacturerURL(&manufacturerUrl);
device->get_Description(&description);
device->get_FriendlyName(&name);
device->get_ModelURL(&modelUrl);
device->get_SerialNumber(&serialNumber);
device->get_UniqueDeviceName(&udn);
device->get_UPC(&upc);
device->get_Type(&deviceType);
device->get_PresentationURL(&presentationUrl);
_tprintf(_T("MANUFACTURER: %s [%s]\n"), manufacturer, manufacturerUrl);
_tprintf(_T("MODEL: %s [%s]\n [%s]\n"), description, name, modelUrl);
_tprintf(_T("DEVICE: serial=%s\n udn=%s\n upc=%s\n type=%s\n"), serialNumber, udn, upc, deviceType);
_tprintf(_T("URL: %s\n"), presentationUrl);
IUPnPServices *services=0;
hr = device->get_Services(&services);
if (hr==S_OK)
{
if (services)
{
long numberOfServices=0;
services->get_Count(&numberOfServices);
if (numberOfServices>0)
{
IUnknown *unknown=0;
hr = services->get__NewEnum(&unknown);
if (hr==S_OK)
{
if (unknown)
{
IEnumVARIANT *enumInterface=0;
hr = unknown->QueryInterface(IID_IEnumVARIANT,(void**)&enumInterface);
if (enumInterface)
{
VARIANT var;
unsigned long fetched=0;
hr = enumInterface->Next(1, &var, &fetched);
if (hr==S_OK)
{
}
else
DumpComError(_T("IEnumVARIANT::Next"), hr);
}
else
DumpComError(_T("IUnknown::QueryInterface"), hr);
}
else
fprintf(stderr, "Failed to get enumeration interface.\n");
}
else
DumpComError(_T("IUPnPServices::get__NewEnum"), hr);
}
else
fprintf(stderr, "No services available.\n");
}
else
fprintf(stderr, "Failed to get services collection.\n");
}
else
DumpComError(_T("IUPnPDevice::get_Services"), hr);
}
else
fprintf(stderr, "Device not found.\n");
}
else
DumpComError(_T("IUPnPDeviceFinder::FindByUDN"), hr);
}
else
DumpComError(_T("CoCreateIndex"), hr);
}
else
DumpComError(_T("CoInitialize"), hr);
return retcode;
}
static void AddBoolToString(const TCHAR *name, bool value, TCHAR *buf, int &i, int max)
{
if (name && *name && value && buf && i>=0)
i += _snwprintf_s(&buf[i], max-i, (max-i-1)*sizeof(TCHAR), _T("%s%s=YES"), (i>0? _T("; "): _T("")), name);
}
static void AddIntToString(const TCHAR *name, int value, TCHAR *buf, int &i, int max)
{
if (name && *name && value && buf && i>=0)
i += _snwprintf_s(&buf[i], max-i, (max-i-1)*sizeof(TCHAR), _T("%s%s=%d"), (i>0? _T("; "): _T("")), name, value);
}
static void DumpComError(const TCHAR *api, HRESULT hr)
{
bool failure = (hr&0x80000000? true: false);
bool severe = (hr&0x40000000? true: false);
bool microsoft = (hr&0x20000000? false: true);
bool ntStatus = (hr&0x10000000? true: false);
bool xBit = (hr&0x08000000? true: false);
int facility = (hr&0x07FF0000)>>16;
int code = (hr&0x0000FFFF);
TCHAR buf[1024]={0};
int bufsize = sizeof(buf)/sizeof(TCHAR);
int i=0;
AddBoolToString(_T("failure"), failure, buf, i, bufsize);
AddBoolToString(_T("severe"), severe, buf, i, bufsize);
AddBoolToString(_T("microsoft"), microsoft, buf, i, bufsize);
AddBoolToString(_T("ntStatus"), ntStatus, buf, i, bufsize);
AddBoolToString(_T("xBit"), xBit, buf, i, bufsize);
AddIntToString(_T("facility"), facility, buf, i, bufsize);
AddIntToString(_T("code"), code, buf, i, bufsize);
_ftprintf(stderr, _T("\n%s() failed, hr=0x%08x\n[%s]\n"), api, hr, buf);
}
Output:
It produces following output:
MANUFACTURER: Belkin International Inc. [http://www.belkin.com/]
MODEL: Belkin Plugin Socket 1.0 [WeMo Switch]
[http://www.belkin.com/plugin/]
DEVICE: serial=221239K11002F6
udn=uuid:Socket-1_0-221239K11002F6
upc=123456789
type=urn:Belkin:device:controllee:1
URL: http://192.168.1.16:49153/pluginpres.html
IEnumVARIANT::Next() failed, hr=0x80040500
[failure=YES; microsoft=YES; facility=4; code=1280]
EDIT:
After a lot of dead-ends, I have managed to get this working by manually building the SOAP requests, and sending the requests via TCP using Windows sockets. Tricky bit was getting the syntax just right as I had no experience of SOAP before. [UPnP was useful to identify the IP address & port number - as these can change]. Once up and running - it's actually a lot simpler than the UPnP interface. Let me know if you're interested and I can post the code... It doesn't directly answer the question I posed here, so it wouldn't make sense to answer my question with this detail.
However, if you're interested, let me know and I can post the code.
Cheers,
Dave
HRESULT of 0x80040500 is not what you think, but UPNP_E_INVALID_DOCUMENT. For explanation of how such ambiguity is possible, see my answer in another SO question.
My guess is that your Belkin device is giving non-conformant device description or service description XML. Non-conformant doesn't necessarily mean broken format, UPnP specification has tons of secondary requirements. Try Device Spy from Intel Developer Tools (link at bottom of the other answer), if the device pops up, then run Device Validator from the same suite on it.
My experience is similar, in that UPnPDeviceFinder simply doesn't work. It never sends out the UPnP Search packet, so the devices don't respond. The only way to get it to work is if you also use windows media player or the "Cast To Device" menu (which is WMP) to initiate the search. As it is UPnPDeviceFinder will return some devices, only if they happen to be broadcasting at that moment, but even finding an XBox (another Microsoft product) doesn't work in the examples without other activity going on.