Skip to content

Commit 3ed3c4f

Browse files
committed
Add support for libmemcached encryption
1 parent e622992 commit 3ed3c4f

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

Diff for: config.m4

+19
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,29 @@ if test "$PHP_MEMCACHED" != "no"; then
317317
CFLAGS="$ORIG_CFLAGS"
318318
LIBS="$ORIG_LIBS"
319319

320+
CFLAGS="$CFLAGS $PHP_LIBMEMCACHED_INCLUDES"
321+
LIBS="$LIBS $PHP_LIBMEMCACHED_LIBS"
322+
320323
if test "$ac_cv_have_memcached_exist" = "yes"; then
321324
AC_DEFINE(HAVE_MEMCACHED_EXIST, [1], [Whether memcached_exist is defined])
322325
fi
323326

327+
AC_CACHE_CHECK([whether memcached_set_encoding_key is defined], ac_cv_have_memcached_set_encoding_key, [
328+
AC_TRY_LINK(
329+
[ #include <libmemcached/memcached.h> ],
330+
[ memcached_set_encoding_key (NULL, NULL, 0); ],
331+
[ ac_cv_have_memcached_set_encoding_key="yes" ],
332+
[ ac_cv_have_memcached_set_encoding_key="no" ]
333+
)
334+
])
335+
336+
CFLAGS="$ORIG_CFLAGS"
337+
LIBS="$ORIG_LIBS"
338+
339+
if test "$ac_cv_have_memcached_set_encoding_key" = "yes"; then
340+
AC_DEFINE(HAVE_MEMCACHED_SET_ENCODING_KEY, [1], [Whether memcached_set_encoding_key is defined])
341+
fi
342+
324343
PHP_MEMCACHED_FILES="php_memcached.c php_libmemcached_compat.c g_fmt.c"
325344

326345
if test "$PHP_SYSTEM_FASTLZ" != "no"; then

Diff for: memcached-api.php

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ class Memcached {
9696

9797
const HAVE_MSGPACK;
9898

99+
const HAVE_ENCODING;
100+
99101
/**
100102
* Feature support
101103
*/
@@ -363,6 +365,8 @@ public function isPristine( ) {}
363365

364366
public function setSaslAuthData( $username, $password ) {}
365367

368+
public function setEncodingKey( $key ) {}
369+
366370
}
367371

368372
class MemcachedException extends Exception {

Diff for: package.xml

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Fixes
159159
<file role='test' name='testdata.res'/>
160160
<file role='test' name='config.inc'/>
161161
<file role='test' name='sasl_basic.phpt'/>
162+
<file role='test' name='set_encoding_key.phpt'/>
162163
<file role='test' name='getserverbykey.phpt'/>
163164
<file role='test' name='gh_155.phpt'/>
164165
<file role='test' name='get_flags.phpt'/>

Diff for: php_memcached.c

+44
Original file line numberDiff line numberDiff line change
@@ -3265,6 +3265,32 @@ static PHP_METHOD(Memcached, setSaslAuthData)
32653265
/* }}} */
32663266
#endif /* HAVE_MEMCACHED_SASL */
32673267

3268+
#ifdef HAVE_MEMCACHED_SET_ENCODING_KEY
3269+
/* {{{ Memcached::setEncodingKey(string key)
3270+
Sets AES encryption key (libmemcached 1.0.6 and higher) */
3271+
static PHP_METHOD(Memcached, setEncodingKey)
3272+
{
3273+
MEMC_METHOD_INIT_VARS;
3274+
memcached_return status;
3275+
zend_string *key;
3276+
3277+
/* "S" */
3278+
ZEND_PARSE_PARAMETERS_START(1, 1)
3279+
Z_PARAM_STR(key)
3280+
ZEND_PARSE_PARAMETERS_END();
3281+
3282+
MEMC_METHOD_FETCH_OBJECT;
3283+
3284+
status = memcached_set_encoding_key(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key));
3285+
3286+
if (s_memc_status_handle_result_code(intern, status) == FAILURE) {
3287+
RETURN_FALSE;
3288+
}
3289+
RETURN_TRUE;
3290+
}
3291+
/* }}} */
3292+
#endif /* HAVE_MEMCACHED_SET_ENCODING_KEY */
3293+
32683294
/* {{{ Memcached::getResultCode()
32693295
Returns the result code from the last operation */
32703296
static PHP_METHOD(Memcached, getResultCode)
@@ -4034,6 +4060,12 @@ ZEND_BEGIN_ARG_INFO(arginfo_setSaslAuthData, 0)
40344060
ZEND_END_ARG_INFO()
40354061
#endif
40364062

4063+
#ifdef HAVE_MEMCACHED_SET_ENCODING_KEY
4064+
ZEND_BEGIN_ARG_INFO(arginfo_setEncodingKey, 0)
4065+
ZEND_ARG_INFO(0, key)
4066+
ZEND_END_ARG_INFO()
4067+
#endif
4068+
40374069
ZEND_BEGIN_ARG_INFO(arginfo_setOption, 0)
40384070
ZEND_ARG_INFO(0, option)
40394071
ZEND_ARG_INFO(0, value)
@@ -4133,6 +4165,9 @@ static zend_function_entry memcached_class_methods[] = {
41334165
MEMC_ME(setBucket, arginfo_setBucket)
41344166
#ifdef HAVE_MEMCACHED_SASL
41354167
MEMC_ME(setSaslAuthData, arginfo_setSaslAuthData)
4168+
#endif
4169+
#ifdef HAVE_MEMCACHED_SET_ENCODING_KEY
4170+
MEMC_ME(setEncodingKey, arginfo_setEncodingKey)
41364171
#endif
41374172
MEMC_ME(isPersistent, arginfo_isPersistent)
41384173
MEMC_ME(isPristine, arginfo_isPristine)
@@ -4282,6 +4317,15 @@ static void php_memc_register_constants(INIT_FUNC_ARGS)
42824317
REGISTER_MEMC_CLASS_CONST_BOOL(HAVE_MSGPACK, 0);
42834318
#endif
42844319

4320+
/*
4321+
* Indicate whether set_encoding_key is available
4322+
*/
4323+
#ifdef HAVE_MEMCACHED_SET_ENCODING_KEY
4324+
REGISTER_MEMC_CLASS_CONST_BOOL(HAVE_ENCODING, 1);
4325+
#else
4326+
REGISTER_MEMC_CLASS_CONST_BOOL(HAVE_ENCODING, 0);
4327+
#endif
4328+
42854329
#ifdef HAVE_MEMCACHED_SESSION
42864330
REGISTER_MEMC_CLASS_CONST_BOOL(HAVE_SESSION, 1);
42874331
#else

Diff for: tests/set_encoding_key.phpt

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
Test libmemcached encryption
3+
--SKIPIF--
4+
<?php
5+
include dirname (__FILE__) . '/config.inc';
6+
if (!extension_loaded("memcached")) die ("skip");
7+
if (!Memcached::HAVE_ENCODING) die ("skip no set_encoding_key support enabled");
8+
?>
9+
--FILE--
10+
<?php
11+
include dirname (__FILE__) . '/config.inc';
12+
$m = memc_get_instance ();
13+
14+
var_dump ($m->setEncodingKey("Hello"));
15+
16+
$key = uniqid ('encoding_test_');
17+
var_dump ($m->set ($key, 'set using encoding'));
18+
var_dump ($m->get ($key));
19+
20+
echo "OK" . PHP_EOL;
21+
22+
# Change the encryption key. The old value will be inaccessible.
23+
var_dump ($m->setEncodingKey("World"));
24+
var_dump ($m->get ($key));
25+
26+
echo "OK" . PHP_EOL;
27+
28+
var_dump ($m->set ($key, 'set using encoding'));
29+
var_dump ($m->get ($key));
30+
31+
echo "OK" . PHP_EOL;
32+
33+
?>
34+
--EXPECT--
35+
bool(true)
36+
bool(true)
37+
string(18) "set using encoding"
38+
OK
39+
bool(true)
40+
bool(false)
41+
OK
42+
bool(true)
43+
string(18) "set using encoding"
44+
OK

0 commit comments

Comments
 (0)