@@ -202,7 +202,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
202
202
cert = CertCreateCertificateContext (X509_ASN_ENCODING , encoded_cert , encoded_cert_len );
203
203
204
204
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 );
206
208
goto fail ;
207
209
}
208
210
@@ -224,16 +226,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
224
226
NULL , /* pvStructInfo */
225
227
& blob_private_len ); /* pcbStructInfo */
226
228
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 );
237
232
goto fail ;
238
233
}
239
234
@@ -247,7 +242,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
247
242
blob_private ,
248
243
& blob_private_len );
249
244
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 );
251
248
goto fail ;
252
249
}
253
250
@@ -259,7 +256,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
259
256
PROV_RSA_FULL , /* dwProvType */
260
257
CRYPT_VERIFYCONTEXT ); /* dwFlags */
261
258
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 );
263
262
goto fail ;
264
263
}
265
264
@@ -273,7 +272,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
273
272
0 , /* dwFlags */
274
273
& hKey ); /* phKey, OUT */
275
274
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 );
277
278
CryptReleaseContext (provider , 0 );
278
279
goto fail ;
279
280
}
@@ -287,7 +288,9 @@ mongoc_secure_channel_setup_certificate_from_file (const char *filename)
287
288
0 , /* dwFlags */
288
289
(const void * ) provider ); /* pvData */
289
290
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 );
291
294
goto fail ;
292
295
}
293
296
@@ -356,7 +359,9 @@ mongoc_secure_channel_setup_ca (mongoc_ssl_opt_t *opt)
356
359
357
360
cert = CertCreateCertificateContext (X509_ASN_ENCODING , encoded_cert , encoded_cert_len );
358
361
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 );
360
365
goto fail ;
361
366
}
362
367
@@ -368,12 +373,16 @@ mongoc_secure_channel_setup_ca (mongoc_ssl_opt_t *opt)
368
373
L"Root" ); /* system store name. "My" or "Root" */
369
374
370
375
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 );
372
379
goto fail ;
373
380
}
374
381
375
382
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 );
377
386
goto fail ;
378
387
}
379
388
@@ -447,12 +456,16 @@ mongoc_secure_channel_setup_crl (mongoc_ssl_opt_t *opt)
447
456
L"Root" ); /* system store name. "My" or "Root" */
448
457
449
458
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 );
451
462
goto fail ;
452
463
}
453
464
454
465
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 );
456
469
goto fail ;
457
470
}
458
471
@@ -614,13 +627,12 @@ mongoc_secure_channel_handshake_step_1 (mongoc_stream_tls_t *tls, char *hostname
614
627
& secure_channel -> ret_flags , /* pfContextAttr OUT param */
615
628
& secure_channel -> ctxt -> time_stamp /* ptsExpiry OUT param */
616
629
);
617
-
618
630
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 );
624
636
return false;
625
637
}
626
638
@@ -849,24 +861,14 @@ mongoc_secure_channel_handshake_step_2 (mongoc_stream_tls_t *tls, char *hostname
849
861
850
862
851
863
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 );
861
866
MONGOC_LOG_AND_SET_ERROR (error ,
862
867
MONGOC_ERROR_STREAM ,
863
868
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" ,
868
870
msg );
869
- LocalFree (msg );
871
+ bson_free (msg );
870
872
}
871
873
}
872
874
return false;
0 commit comments