@@ -36,6 +36,20 @@ static void phongo_clientencryption_create_datakey(php_phongo_clientencryption_t
36
36
static void phongo_clientencryption_encrypt (php_phongo_clientencryption_t * clientencryption , zval * zvalue , zval * zciphertext , zval * options );
37
37
static void phongo_clientencryption_decrypt (php_phongo_clientencryption_t * clientencryption , zval * zciphertext , zval * zvalue );
38
38
39
+ /* {{{ proto void MongoDB\Driver\ClientEncryption::__construct(array $options)
40
+ Constructs a new ClientEncryption */
41
+ static PHP_METHOD (ClientEncryption , __construct )
42
+ {
43
+ zval * options ;
44
+
45
+ PHONGO_PARSE_PARAMETERS_START (1 , 1 )
46
+ Z_PARAM_ARRAY (options )
47
+ PHONGO_PARSE_PARAMETERS_END ();
48
+
49
+ /* An exception will be thrown on error. */
50
+ phongo_clientencryption_init (Z_CLIENTENCRYPTION_OBJ_P (getThis ()), options , NULL );
51
+ } /* }}} */
52
+
39
53
/* {{{ proto MongoDB\BSON\Binary MongoDB\Driver\ClientEncryption::createDataKey(string $kmsProvider[, array $options])
40
54
Creates a new key document and inserts into the key vault collection. */
41
55
static PHP_METHOD (ClientEncryption , createDataKey )
@@ -99,6 +113,10 @@ static PHP_METHOD(ClientEncryption, decrypt)
99
113
phongo_clientencryption_decrypt (intern , ciphertext , return_value );
100
114
} /* }}} */
101
115
116
+ ZEND_BEGIN_ARG_INFO_EX (ai_ClientEncryption___construct , 0 , 0 , 0 )
117
+ ZEND_ARG_ARRAY_INFO (0 , options , 1 )
118
+ ZEND_END_ARG_INFO ()
119
+
102
120
ZEND_BEGIN_ARG_INFO_EX (ai_ClientEncryption_createDataKey , 0 , 0 , 1 )
103
121
ZEND_ARG_INFO (0 , kmsProvider )
104
122
ZEND_ARG_ARRAY_INFO (0 , options , 1 )
@@ -118,10 +136,10 @@ ZEND_END_ARG_INFO()
118
136
119
137
static zend_function_entry php_phongo_clientencryption_me [] = {
120
138
/* clang-format off */
139
+ PHP_ME (ClientEncryption , __construct , ai_ClientEncryption___construct , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
121
140
PHP_ME (ClientEncryption , createDataKey , ai_ClientEncryption_createDataKey , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
122
141
PHP_ME (ClientEncryption , encrypt , ai_ClientEncryption_encrypt , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
123
142
PHP_ME (ClientEncryption , decrypt , ai_ClientEncryption_decrypt , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
124
- ZEND_NAMED_ME (__construct , PHP_FN (MongoDB_disabled___construct ), ai_ClientEncryption_void , ZEND_ACC_PRIVATE | ZEND_ACC_FINAL )
125
143
ZEND_NAMED_ME (__wakeup , PHP_FN (MongoDB_disabled___wakeup ), ai_ClientEncryption_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
126
144
PHP_FE_END
127
145
/* clang-format on */
@@ -197,14 +215,13 @@ void php_phongo_clientencryption_init_ce(INIT_FUNC_ARGS) /* {{{ */
197
215
} /* }}} */
198
216
199
217
#ifdef MONGOC_ENABLE_CLIENT_SIDE_ENCRYPTION
200
- /* keyVaultClientManager is an output parameter and will be assigned the
201
- * keyVaultNamespace Manager (if any) . */
202
- static mongoc_client_encryption_opts_t * phongo_clientencryption_opts_from_zval (zval * defaultKeyVaultClient , zval * options , zval * * keyVaultClientManager ) /* {{{ */
218
+ /* key_vault_client_manager is an output parameter and will be assigned to the
219
+ * effective keyVaultClient . */
220
+ static mongoc_client_encryption_opts_t * phongo_clientencryption_opts_from_zval (zval * options , zval * default_key_vault_client_manager , zval * * key_vault_client_manager ) /* {{{ */
203
221
{
204
- mongoc_client_encryption_opts_t * opts ;
222
+ mongoc_client_encryption_opts_t * opts = mongoc_client_encryption_opts_new () ;
205
223
206
- opts = mongoc_client_encryption_opts_new ();
207
- * keyVaultClientManager = NULL ;
224
+ * key_vault_client_manager = NULL ;
208
225
209
226
if (!options || Z_TYPE_P (options ) != IS_ARRAY ) {
210
227
/* Returning opts as-is will defer to mongoc_client_encryption_new to
@@ -221,10 +238,15 @@ static mongoc_client_encryption_opts_t* phongo_clientencryption_opts_from_zval(z
221
238
}
222
239
223
240
mongoc_client_encryption_opts_set_keyvault_client (opts , Z_MANAGER_OBJ_P (key_vault_client )-> client );
224
- * keyVaultClientManager = key_vault_client ;
241
+ * key_vault_client_manager = key_vault_client ;
242
+ } else if (default_key_vault_client_manager ) {
243
+ mongoc_client_encryption_opts_set_keyvault_client (opts , Z_MANAGER_OBJ_P (default_key_vault_client_manager )-> client );
244
+ * key_vault_client_manager = default_key_vault_client_manager ;
225
245
} else {
226
- mongoc_client_encryption_opts_set_keyvault_client (opts , Z_MANAGER_OBJ_P (defaultKeyVaultClient )-> client );
227
- * keyVaultClientManager = defaultKeyVaultClient ;
246
+ /* If the ClientEncryption object is being constructed directly, the
247
+ * "keyVaultClient" option must be explicitly provided. */
248
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT , "The \"keyVaultClient\" option is required when constructing a ClientEncryption object directly" );
249
+ goto cleanup ;
228
250
}
229
251
230
252
if (php_array_existsc (options , "keyVaultNamespace" )) {
@@ -301,32 +323,35 @@ static mongoc_client_encryption_opts_t* phongo_clientencryption_opts_from_zval(z
301
323
return NULL ;
302
324
} /* }}} */
303
325
304
- void phongo_clientencryption_init (zval * return_value , zval * manager , zval * options ) /* {{{ */
326
+ void phongo_clientencryption_init (php_phongo_clientencryption_t * intern , zval * options , zval * default_key_vault_client_manager ) /* {{{ */
305
327
{
306
- php_phongo_clientencryption_t * intern ;
307
328
mongoc_client_encryption_t * client_encryption ;
308
329
mongoc_client_encryption_opts_t * opts ;
309
- zval * key_vault_client_manager = manager ;
330
+ zval * key_vault_client_manager = NULL ;
310
331
bson_error_t error = { 0 };
311
332
312
- opts = phongo_clientencryption_opts_from_zval (manager , options , & key_vault_client_manager );
333
+ opts = phongo_clientencryption_opts_from_zval (options , default_key_vault_client_manager , & key_vault_client_manager );
334
+
313
335
if (!opts ) {
314
336
/* Exception already thrown */
315
337
goto cleanup ;
316
338
}
317
339
318
340
client_encryption = mongoc_client_encryption_new (opts , & error );
341
+
319
342
if (!client_encryption ) {
320
343
phongo_throw_exception_from_bson_error_t (& error );
321
-
322
344
goto cleanup ;
323
345
}
324
346
325
- object_init_ex (return_value , php_phongo_clientencryption_ce );
326
-
327
- intern = Z_CLIENTENCRYPTION_OBJ_P (return_value );
328
347
intern -> client_encryption = client_encryption ;
329
- ZVAL_ZVAL (& intern -> key_vault_client_manager , key_vault_client_manager , 1 , 0 );
348
+
349
+ /* Note: key_vault_client_manager should always be assigned if options were
350
+ * successfully parsed by phongo_clientencryption_opts_from_zval, but let's
351
+ * be defensive. */
352
+ if (key_vault_client_manager ) {
353
+ ZVAL_ZVAL (& intern -> key_vault_client_manager , key_vault_client_manager , 1 , 0 );
354
+ }
330
355
331
356
cleanup :
332
357
if (opts ) {
@@ -584,7 +609,7 @@ static void phongo_clientencryption_decrypt(php_phongo_clientencryption_t* clien
584
609
bson_value_destroy (& value );
585
610
} /* }}} */
586
611
#else /* MONGOC_ENABLE_CLIENT_SIDE_ENCRYPTION */
587
- void phongo_clientencryption_init (zval * return_value , zval * manager , zval * options ) /* {{{ */
612
+ void phongo_clientencryption_init (php_phongo_clientencryption_t * intern , zval * options , zval * default_key_vault_client_manager ) /* {{{ */
588
613
{
589
614
phongo_throw_exception_no_cse (PHONGO_ERROR_RUNTIME , "Cannot configure clientEncryption object." );
590
615
}
0 commit comments