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