@@ -5694,6 +5694,143 @@ test_create_encrypted_collection_bad_keyId (void *unused)
5694
5694
mongoc_client_destroy (client );
5695
5695
}
5696
5696
5697
+ typedef struct listen_socket {
5698
+ mongoc_socket_t * socket ;
5699
+ mongoc_cond_t cond ;
5700
+ bson_mutex_t mutex ;
5701
+ bool failed ;
5702
+ char ip [16 ];
5703
+ unsigned short port ;
5704
+ } listen_socket_args_t ;
5705
+
5706
+ static BSON_THREAD_FUN (listen_socket , arg )
5707
+ {
5708
+ listen_socket_args_t * args = arg ;
5709
+ mongoc_socket_t * socket ;
5710
+ struct sockaddr_in server_addr = {0 };
5711
+ args -> failed = false;
5712
+
5713
+ // create a TcpListener on 127.0.0.1 endpoint
5714
+ socket = mongoc_socket_new (AF_INET , SOCK_STREAM , 0 );
5715
+ BSON_ASSERT (socket );
5716
+
5717
+ server_addr .sin_family = AF_INET ;
5718
+ server_addr .sin_addr .s_addr = htonl (INADDR_LOOPBACK );
5719
+ server_addr .sin_port = htons (0 );
5720
+ mongoc_socklen_t addr_len = (mongoc_socklen_t ) sizeof server_addr ;
5721
+
5722
+ int r =
5723
+ mongoc_socket_bind (socket , (struct sockaddr * ) & server_addr , addr_len );
5724
+ BSON_ASSERT (r == 0 );
5725
+
5726
+ // forward the port and ip for mongocryptdURI
5727
+ r = mongoc_socket_getsockname (
5728
+ socket , (struct sockaddr * ) & server_addr , & addr_len );
5729
+ BSON_ASSERT (r == 0 );
5730
+
5731
+ bson_mutex_lock (& args -> mutex );
5732
+ args -> port = ntohs (server_addr .sin_port );
5733
+ inet_ntop (AF_INET , & server_addr .sin_addr , args -> ip , sizeof (args -> ip ));
5734
+ mongoc_cond_signal (& args -> cond );
5735
+ bson_mutex_unlock (& args -> mutex );
5736
+
5737
+ // listen on socket
5738
+ r = mongoc_socket_listen (socket , 100 );
5739
+ BSON_ASSERT (r == 0 );
5740
+ _mongoc_usleep (1000 ); // wait to see if received connection
5741
+ mongoc_socket_t * ret = mongoc_socket_accept (socket , 100 );
5742
+ if (ret ) {
5743
+ // not null received a connection and test should fail
5744
+ args -> failed = true;
5745
+ }
5746
+ // signal that test is complete.
5747
+ bson_mutex_lock (& args -> mutex );
5748
+ mongoc_cond_signal (& args -> cond );
5749
+ bson_mutex_unlock (& args -> mutex );
5750
+
5751
+ mongoc_socket_destroy (socket );
5752
+ BSON_THREAD_RETURN ;
5753
+ }
5754
+
5755
+ /* Prose Test 20: Bypass creating mongocryptd client when shared library is
5756
+ * loaded */
5757
+ static void
5758
+ test_bypass_mongocryptd_shared_library (void * unused )
5759
+ {
5760
+ BSON_UNUSED (unused );
5761
+ mongoc_client_t * client_encrypted ;
5762
+ mongoc_auto_encryption_opts_t * auto_encryption_opts ;
5763
+ bson_t * kms_providers ;
5764
+ mongoc_database_t * db ;
5765
+ mongoc_collection_t * coll ;
5766
+ bson_error_t error ;
5767
+ bson_thread_t socket_thread ;
5768
+
5769
+ // start the socket on a thread
5770
+ listen_socket_args_t * args = bson_malloc0 (sizeof (listen_socket_args_t ));
5771
+ bson_mutex_init (& args -> mutex );
5772
+ mongoc_cond_init (& args -> cond );
5773
+ mcommon_thread_create (& socket_thread , listen_socket , args );
5774
+
5775
+ // configure mongoclient with auto encryption
5776
+ char * env_cryptSharedLibPath =
5777
+ test_framework_getenv ("MONGOC_TEST_CRYPT_SHARED_LIB_PATH" );
5778
+ BSON_ASSERT (env_cryptSharedLibPath );
5779
+ auto_encryption_opts = mongoc_auto_encryption_opts_new ();
5780
+ kms_providers = BCON_NEW (
5781
+ "local" , "{" , "key" , BCON_BIN (0 , (uint8_t * ) LOCAL_MASTERKEY , 96 ), "}" );
5782
+ mongoc_auto_encryption_opts_set_kms_providers (auto_encryption_opts ,
5783
+ kms_providers );
5784
+ mongoc_auto_encryption_opts_set_keyvault_namespace (
5785
+ auto_encryption_opts , "keyvault" , "datakeys" );
5786
+
5787
+ // wait for port and ip to be set on the other thread
5788
+ while (!args -> port ) {
5789
+ int ret = mongoc_cond_timedwait (& args -> cond , & args -> mutex , 5000 );
5790
+ /* ret non-zero indicates an error (a timeout) */
5791
+ BSON_ASSERT (!ret );
5792
+ }
5793
+
5794
+ // configure extra options
5795
+ bson_t * extra = tmp_bson ("{'mongocryptdURI': 'mongodb://%s:%d', "
5796
+ "'cryptSharedLibPath': '%s'}" ,
5797
+ args -> ip ,
5798
+ args -> port ,
5799
+ env_cryptSharedLibPath );
5800
+ mongoc_auto_encryption_opts_set_extra (auto_encryption_opts , extra );
5801
+ bson_free (env_cryptSharedLibPath );
5802
+ // get the client
5803
+ client_encrypted = test_framework_new_default_client ();
5804
+ bool ret = mongoc_client_enable_auto_encryption (
5805
+ client_encrypted , auto_encryption_opts , & error );
5806
+ ASSERT_OR_PRINT (ret , error );
5807
+
5808
+ // insert a document
5809
+ db = mongoc_client_get_database (client_encrypted , "db" );
5810
+ coll = mongoc_database_get_collection (db , "coll" );
5811
+ ret = mongoc_collection_insert_one (coll ,
5812
+ tmp_bson ("{'unencrypted': 'test'}" ),
5813
+ NULL /* opts */ ,
5814
+ NULL /* reply */ ,
5815
+ & error );
5816
+ ASSERT_OR_PRINT (ret , error );
5817
+ ret = mongoc_cond_timedwait (& args -> cond , & args -> mutex , 5000 );
5818
+ /* ret non-zero indicates an error (a timeout) */
5819
+ BSON_ASSERT (!ret );
5820
+ // failed should be false if the signal did not receive a connection
5821
+ BSON_ASSERT (!args -> failed );
5822
+ mcommon_thread_join (socket_thread );
5823
+
5824
+ bson_mutex_destroy (& args -> mutex );
5825
+ mongoc_cond_destroy (& args -> cond );
5826
+ bson_destroy (kms_providers );
5827
+ mongoc_auto_encryption_opts_destroy (auto_encryption_opts );
5828
+ mongoc_collection_destroy (coll );
5829
+ mongoc_database_destroy (db );
5830
+ mongoc_client_destroy (client_encrypted );
5831
+ bson_free (args );
5832
+ }
5833
+
5697
5834
void
5698
5835
test_client_side_encryption_install (TestSuite * suite )
5699
5836
{
@@ -6030,4 +6167,13 @@ test_client_side_encryption_install (TestSuite *suite)
6030
6167
test_framework_skip_if_no_client_side_encryption ,
6031
6168
test_framework_skip_if_max_wire_version_less_than_17 ,
6032
6169
test_framework_skip_if_single );
6170
+ TestSuite_AddFull (
6171
+ suite ,
6172
+ "/client_side_encryption/bypass_mongocryptd_shared_library" ,
6173
+ test_bypass_mongocryptd_shared_library ,
6174
+ NULL ,
6175
+ NULL ,
6176
+ test_framework_skip_if_no_client_side_encryption ,
6177
+ test_framework_skip_if_max_wire_version_less_than_17 ,
6178
+ _skip_if_no_crypt_shared );
6033
6179
}
0 commit comments