windows - Secure Socket Connections using c++ -
i trying ssl/tls connections work in windows. right using schannel, not sure correct way go it. here code. exception thrown @ initializesecuritycontexta() function
#include "windows.h" #pragma comment(lib, "ws2_32.lib") #define security_win32 #include <schannel.h> #include <security.h> int callback winmain(hinstance currentinstance, hinstance previousinstance, lpstr bs1, int bs2) { // initialize winsock 2.0 wsadata versioninfo; wsastartup (0x0202, &versioninfo); // load security dll hmodule securitydllmodule = loadlibrary("secur32.dll"); // initialize schannel init_security_interface initsecurtyinterfacefunction = (init_security_interface)getprocaddress(securitydllmodule, "initsecurityinterfacea"); psecurityfunctiontable schannel = initsecurtyinterfacefunction(); if (!schannel) messagebox(0, "failed initialize schannel", "message", mb_taskmodal | mb_ok); else messagebox(0, "initialized schannel", "message", mb_taskmodal | mb_ok); // setup schannel credentials dword protocol = sp_prot_tls1; schannel_cred schannelcredentials; zeromemory(&schannelcredentials, sizeof(schannelcredentials)); schannelcredentials.dwversion = schannel_cred_version; schannelcredentials.grbitenabledprotocols = protocol; schannelcredentials.dwflags |= sch_cred_no_default_creds; schannelcredentials.dwflags |= sch_cred_manual_cred_validation; // client credentials handle credhandle clientcredentials; security_status securitystatus = schannel->acquirecredentialshandlea( 0, unisp_name_a, secpkg_cred_outbound, 0, &schannelcredentials, 0, 0, &clientcredentials, 0 ); if (securitystatus != sec_e_ok) messagebox(0, "failed credenetials", "message", mb_taskmodal | mb_ok); else messagebox(0, "got client credenetials", "message", mb_taskmodal | mb_ok); // connect google socket mysocket = socket(pf_inet, sock_stream, 0); sockaddr_in sin; sin.sin_family = af_inet; sin.sin_port = htons(443); hostent *hp = gethostbyname("www.google.com"); memcpy(&sin.sin_addr, hp->h_addr, 4); if (connect(mysocket, (struct sockaddr *)&sin, sizeof(sin)) == socket_error) messagebox(0, "error connecting", "message", mb_taskmodal | mb_ok); else messagebox(0, "connected", "message", mb_taskmodal | mb_ok); // perform handshake: dword sspiflags = ( isc_req_sequence_detect | isc_req_replay_detect | isc_req_confidentiality | isc_ret_extended_error | isc_req_allocate_memory | isc_req_stream ); secbuffer outbuffers[1]; outbuffers[0].pvbuffer = 0; outbuffers[0].buffertype = secbuffer_token; outbuffers[0].cbbuffer = 0; secbufferdesc outbuffer; outbuffer.cbuffers = 1; outbuffer.pbuffers = outbuffers; outbuffer.ulversion = secbuffer_version; dword sspioutflags; ctxthandle* contexthandle; security_status scret = schannel->initializesecuritycontexta( &clientcredentials, 0, "www.google.com", sspiflags, 0, security_native_drep, 0, 0, contexthandle, &outbuffer, &sspioutflags, 0 ); if (scret != sec_i_continue_needed) messagebox(0, "error initializing security context", "message", mb_taskmodal | mb_ok); else messagebox(0, "security context initialized", "message", mb_taskmodal | mb_ok); // done messagebox(0, "done", "message", mb_taskmodal | mb_ok); return 0; }
change ctxthandle* contexthandle;
ctxthandle contexthandle;
, instead of contexthandle
pass &contexthandle
initializesecuritycontexta()
call.
Comments
Post a Comment