@@ -202,7 +202,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
202202 cert = CertCreateCertificateContext (X509_ASN_ENCODING , encoded_cert , encoded_cert_len );
203203
204204 if (!cert ) {
205- MONGOC_ERROR ("Failed to extract public key from '%s'. Error 0x%.8X" , filename , (unsigned int ) GetLastError ());
205+ char * msg = mongoc_winerr_to_string (GetLastError ());
206+ MONGOC_ERROR ("Failed to extract public key from '%s': %s" , filename , msg );
207+ bson_free (msg );
206208 goto fail ;
207209 }
208210
@@ -224,16 +226,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
224226 NULL , /* pvStructInfo */
225227 & blob_private_len ); /* pcbStructInfo */
226228 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 );
229+ char * msg = mongoc_winerr_to_string (GetLastError ());
230+ MONGOC_ERROR ("Failed to parse private key. %s" , msg );
231+ bson_free (msg );
237232 goto fail ;
238233 }
239234
@@ -247,7 +242,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
247242 blob_private ,
248243 & blob_private_len );
249244 if (!success ) {
250- MONGOC_ERROR ("Failed to parse private key. Error 0x%.8X" , (unsigned int ) GetLastError ());
245+ char * msg = mongoc_winerr_to_string (GetLastError ());
246+ MONGOC_ERROR ("Failed to parse private key: %s" , msg );
247+ bson_free (msg );
251248 goto fail ;
252249 }
253250
@@ -259,7 +256,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
259256 PROV_RSA_FULL , /* dwProvType */
260257 CRYPT_VERIFYCONTEXT ); /* dwFlags */
261258 if (!success ) {
262- MONGOC_ERROR ("CryptAcquireContext failed with error 0x%.8X" , (unsigned int ) GetLastError ());
259+ char * msg = mongoc_winerr_to_string (GetLastError ());
260+ MONGOC_ERROR ("CryptAcquireContext failed: %s" , msg );
261+ bson_free (msg );
263262 goto fail ;
264263 }
265264
@@ -273,7 +272,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
273272 0 , /* dwFlags */
274273 & hKey ); /* phKey, OUT */
275274 if (!success ) {
276- MONGOC_ERROR ("CryptImportKey for private key failed with error 0x%.8X" , (unsigned int ) GetLastError ());
275+ char * msg = mongoc_winerr_to_string (GetLastError ());
276+ MONGOC_ERROR ("CryptImportKey for private key failed: %s" , msg );
277+ bson_free (msg );
277278 CryptReleaseContext (provider , 0 );
278279 goto fail ;
279280 }
@@ -287,7 +288,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
287288 0 , /* dwFlags */
288289 (const void * ) provider ); /* pvData */
289290 if (!success ) {
290- MONGOC_ERROR ("Can't associate private key with public key: 0x%.8X" , (unsigned int ) GetLastError ());
291+ char * msg = mongoc_winerr_to_string (GetLastError ());
292+ MONGOC_ERROR ("Can't associate private key with public key: %s" , msg );
293+ bson_free (msg );
291294 goto fail ;
292295 }
293296
@@ -356,7 +359,9 @@ mongoc_secure_channel_setup_ca (mongoc_ssl_opt_t *opt)
356359
357360 cert = CertCreateCertificateContext (X509_ASN_ENCODING , encoded_cert , encoded_cert_len );
358361 if (!cert ) {
359- MONGOC_WARNING ("Could not convert certificate" );
362+ char * msg = mongoc_winerr_to_string (GetLastError ());
363+ MONGOC_WARNING ("Could not convert certificate: %s" , msg );
364+ bson_free (msg );
360365 goto fail ;
361366 }
362367
@@ -368,12 +373,16 @@ mongoc_secure_channel_setup_ca (mongoc_ssl_opt_t *opt)
368373 L"Root" ); /* system store name. "My" or "Root" */
369374
370375 if (cert_store == NULL ) {
371- MONGOC_ERROR ("Error opening certificate store" );
376+ char * msg = mongoc_winerr_to_string (GetLastError ());
377+ MONGOC_ERROR ("Error opening certificate store: %s" , msg );
378+ bson_free (msg );
372379 goto fail ;
373380 }
374381
375382 if (!CertAddCertificateContextToStore (cert_store , cert , CERT_STORE_ADD_USE_EXISTING , NULL )) {
376- MONGOC_WARNING ("Failed adding the cert" );
383+ char * msg = mongoc_winerr_to_string (GetLastError ());
384+ MONGOC_WARNING ("Failed adding the cert: %s" , msg );
385+ bson_free (msg );
377386 goto fail ;
378387 }
379388
@@ -447,12 +456,16 @@ mongoc_secure_channel_setup_crl (mongoc_ssl_opt_t *opt)
447456 L"Root" ); /* system store name. "My" or "Root" */
448457
449458 if (cert_store == NULL ) {
450- MONGOC_ERROR ("Error opening certificate store" );
459+ char * msg = mongoc_winerr_to_string (GetLastError ());
460+ MONGOC_ERROR ("Error opening certificate store: %s" , msg );
461+ bson_free (msg );
451462 goto fail ;
452463 }
453464
454465 if (!CertAddCRLContextToStore (cert_store , crl , CERT_STORE_ADD_USE_EXISTING , NULL )) {
455- MONGOC_WARNING ("Failed adding the CRL" );
466+ char * msg = mongoc_winerr_to_string (GetLastError ());
467+ MONGOC_WARNING ("Failed adding the CRL: %s" , msg );
468+ bson_free (msg );
456469 goto fail ;
457470 }
458471
@@ -614,13 +627,12 @@ mongoc_secure_channel_handshake_step_1 (mongoc_stream_tls_t *tls, char *hostname
614627 & secure_channel -> ret_flags , /* pfContextAttr OUT param */
615628 & secure_channel -> ctxt -> time_stamp /* ptsExpiry OUT param */
616629 );
617-
618630 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 );
631+ // Cast signed SECURITY_STATUS to unsigned DWORD. FormatMessage expects DWORD.
632+ char * msg = mongoc_winerr_to_string (( DWORD ) sspi_status );
633+ MONGOC_LOG_AND_SET_ERROR (
634+ error , MONGOC_ERROR_STREAM , MONGOC_ERROR_STREAM_SOCKET , "initial InitializeSecurityContext failed: %s" , msg );
635+ bson_free ( msg );
624636 return false;
625637 }
626638
@@ -849,24 +861,14 @@ mongoc_secure_channel_handshake_step_2 (mongoc_stream_tls_t *tls, char *hostname
849861
850862
851863 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 );
864+ // Cast signed SECURITY_STATUS to unsigned DWORD. FormatMessage expects DWORD.
865+ char * msg = mongoc_winerr_to_string ((DWORD ) sspi_status );
861866 MONGOC_LOG_AND_SET_ERROR (error ,
862867 MONGOC_ERROR_STREAM ,
863868 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 ,
869+ "Failed to initialize security context: %s" ,
868870 msg );
869- LocalFree (msg );
871+ bson_free (msg );
870872 }
871873 }
872874 return false;
0 commit comments