2929#include <mongoc/mongoc-stream-tls-secure-channel-private.h>
3030#include <mongoc/mongoc-errno-private.h>
3131#include <mongoc/mongoc-error.h>
32+ #include <mongoc/mongoc-error-private.h>
3233#include <common-string-private.h>
3334#include <common-cmp-private.h>
3435
@@ -202,7 +203,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
202203 cert = CertCreateCertificateContext (X509_ASN_ENCODING , encoded_cert , encoded_cert_len );
203204
204205 if (!cert ) {
205- MONGOC_ERROR ("Failed to extract public key from '%s'. Error 0x%.8X" , filename , (unsigned int ) GetLastError ());
206+ char * msg = mongoc_winerr_to_string (GetLastError ());
207+ MONGOC_ERROR ("Failed to extract public key from '%s': %s" , filename , msg );
208+ bson_free (msg );
206209 goto fail ;
207210 }
208211
@@ -224,16 +227,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
224227 NULL , /* pvStructInfo */
225228 & blob_private_len ); /* pcbStructInfo */
226229 if (!success ) {
227- LPTSTR msg = NULL ;
228- FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY ,
229- NULL ,
230- GetLastError (),
231- LANG_NEUTRAL ,
232- (LPTSTR ) & msg ,
233- 0 ,
234- NULL );
235- MONGOC_ERROR ("Failed to parse private key. %s (0x%.8X)" , msg , (unsigned int ) GetLastError ());
236- LocalFree (msg );
230+ char * msg = mongoc_winerr_to_string (GetLastError ());
231+ MONGOC_ERROR ("Failed to parse private key. %s" , msg );
232+ bson_free (msg );
237233 goto fail ;
238234 }
239235
@@ -247,7 +243,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
247243 blob_private ,
248244 & blob_private_len );
249245 if (!success ) {
250- MONGOC_ERROR ("Failed to parse private key. Error 0x%.8X" , (unsigned int ) GetLastError ());
246+ char * msg = mongoc_winerr_to_string (GetLastError ());
247+ MONGOC_ERROR ("Failed to parse private key: %s" , msg );
248+ bson_free (msg );
251249 goto fail ;
252250 }
253251
@@ -259,7 +257,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
259257 PROV_RSA_FULL , /* dwProvType */
260258 CRYPT_VERIFYCONTEXT ); /* dwFlags */
261259 if (!success ) {
262- MONGOC_ERROR ("CryptAcquireContext failed with error 0x%.8X" , (unsigned int ) GetLastError ());
260+ char * msg = mongoc_winerr_to_string (GetLastError ());
261+ MONGOC_ERROR ("CryptAcquireContext failed: %s" , msg );
262+ bson_free (msg );
263263 goto fail ;
264264 }
265265
@@ -273,7 +273,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
273273 0 , /* dwFlags */
274274 & hKey ); /* phKey, OUT */
275275 if (!success ) {
276- MONGOC_ERROR ("CryptImportKey for private key failed with error 0x%.8X" , (unsigned int ) GetLastError ());
276+ char * msg = mongoc_winerr_to_string (GetLastError ());
277+ MONGOC_ERROR ("CryptImportKey for private key failed: %s" , msg );
278+ bson_free (msg );
277279 CryptReleaseContext (provider , 0 );
278280 goto fail ;
279281 }
@@ -287,7 +289,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
287289 0 , /* dwFlags */
288290 (const void * ) provider ); /* pvData */
289291 if (!success ) {
290- MONGOC_ERROR ("Can't associate private key with public key: 0x%.8X" , (unsigned int ) GetLastError ());
292+ char * msg = mongoc_winerr_to_string (GetLastError ());
293+ MONGOC_ERROR ("Can't associate private key with public key: %s" , msg );
294+ bson_free (msg );
291295 goto fail ;
292296 }
293297
@@ -356,7 +360,9 @@ mongoc_secure_channel_setup_ca (mongoc_ssl_opt_t *opt)
356360
357361 cert = CertCreateCertificateContext (X509_ASN_ENCODING , encoded_cert , encoded_cert_len );
358362 if (!cert ) {
359- MONGOC_WARNING ("Could not convert certificate" );
363+ char * msg = mongoc_winerr_to_string (GetLastError ());
364+ MONGOC_WARNING ("Could not convert certificate: %s" , msg );
365+ bson_free (msg );
360366 goto fail ;
361367 }
362368
@@ -368,12 +374,16 @@ mongoc_secure_channel_setup_ca (mongoc_ssl_opt_t *opt)
368374 L"Root" ); /* system store name. "My" or "Root" */
369375
370376 if (cert_store == NULL ) {
371- MONGOC_ERROR ("Error opening certificate store" );
377+ char * msg = mongoc_winerr_to_string (GetLastError ());
378+ MONGOC_ERROR ("Error opening certificate store: %s" , msg );
379+ bson_free (msg );
372380 goto fail ;
373381 }
374382
375383 if (!CertAddCertificateContextToStore (cert_store , cert , CERT_STORE_ADD_USE_EXISTING , NULL )) {
376- MONGOC_WARNING ("Failed adding the cert" );
384+ char * msg = mongoc_winerr_to_string (GetLastError ());
385+ MONGOC_WARNING ("Failed adding the cert: %s" , msg );
386+ bson_free (msg );
377387 goto fail ;
378388 }
379389
@@ -447,12 +457,16 @@ mongoc_secure_channel_setup_crl (mongoc_ssl_opt_t *opt)
447457 L"Root" ); /* system store name. "My" or "Root" */
448458
449459 if (cert_store == NULL ) {
450- MONGOC_ERROR ("Error opening certificate store" );
460+ char * msg = mongoc_winerr_to_string (GetLastError ());
461+ MONGOC_ERROR ("Error opening certificate store: %s" , msg );
462+ bson_free (msg );
451463 goto fail ;
452464 }
453465
454466 if (!CertAddCRLContextToStore (cert_store , crl , CERT_STORE_ADD_USE_EXISTING , NULL )) {
455- MONGOC_WARNING ("Failed adding the CRL" );
467+ char * msg = mongoc_winerr_to_string (GetLastError ());
468+ MONGOC_WARNING ("Failed adding the CRL: %s" , msg );
469+ bson_free (msg );
456470 goto fail ;
457471 }
458472
@@ -614,13 +628,12 @@ mongoc_secure_channel_handshake_step_1 (mongoc_stream_tls_t *tls, char *hostname
614628 & secure_channel -> ret_flags , /* pfContextAttr OUT param */
615629 & secure_channel -> ctxt -> time_stamp /* ptsExpiry OUT param */
616630 );
617-
618631 if (sspi_status != SEC_I_CONTINUE_NEEDED ) {
619- MONGOC_LOG_AND_SET_ERROR ( error ,
620- MONGOC_ERROR_STREAM ,
621- MONGOC_ERROR_STREAM_SOCKET ,
622- "initial InitializeSecurityContext failed: %ld" ,
623- sspi_status );
632+ // Cast signed SECURITY_STATUS to unsigned DWORD. FormatMessage expects DWORD.
633+ char * msg = mongoc_winerr_to_string (( DWORD ) sspi_status );
634+ MONGOC_LOG_AND_SET_ERROR (
635+ error , MONGOC_ERROR_STREAM , MONGOC_ERROR_STREAM_SOCKET , "initial InitializeSecurityContext failed: %s" , msg );
636+ bson_free ( msg );
624637 return false;
625638 }
626639
@@ -849,24 +862,14 @@ mongoc_secure_channel_handshake_step_2 (mongoc_stream_tls_t *tls, char *hostname
849862
850863
851864 default : {
852- LPTSTR msg = NULL ;
853-
854- FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY ,
855- NULL ,
856- GetLastError (),
857- LANG_NEUTRAL ,
858- (LPTSTR ) & msg ,
859- 0 ,
860- NULL );
865+ // Cast signed SECURITY_STATUS to unsigned DWORD. FormatMessage expects DWORD.
866+ char * msg = mongoc_winerr_to_string ((DWORD ) sspi_status );
861867 MONGOC_LOG_AND_SET_ERROR (error ,
862868 MONGOC_ERROR_STREAM ,
863869 MONGOC_ERROR_STREAM_SOCKET ,
864- "Failed to initialize security context, error code: "
865- "0x%04X%04X: %s" ,
866- (unsigned int ) (sspi_status >> 16 ) & 0xffff ,
867- (unsigned int ) sspi_status & 0xffff ,
870+ "Failed to initialize security context: %s" ,
868871 msg );
869- LocalFree (msg );
872+ bson_free (msg );
870873 }
871874 }
872875 return false;
0 commit comments