Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basics of TLS support #6

Merged
merged 15 commits into from
Dec 5, 2013
Prev Previous commit
Next Next commit
Add callback to handle encrypted client keys
  • Loading branch information
mgdm committed Dec 4, 2013
commit 35d477fdbca3bbeb0452c64e7615d9a3cf8d8975
20 changes: 18 additions & 2 deletions mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ zend_object_handlers mosquitto_std_object_handlers;
ZEND_DECLARE_MODULE_GLOBALS(mosquitto)

static inline mosquitto_client_object *mosquitto_client_object_get(zval *zobj TSRMLS_DC);
static int php_mosquitto_pw_callback(char *buf, int size, int rwflag, void *userdata);

/* {{{ Arginfo */

Expand Down Expand Up @@ -121,6 +122,7 @@ PHP_METHOD(Mosquitto_Client, setTlsCertificates)
int ca_path_len = 0, cert_path_len = 0, key_path_len = 0, key_pw_len, retval = 0;
zval *stat;
zend_bool is_dir = 0;
int (*pw_callback)(char *, int, int, void *) = NULL;

PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!|s!s!s!",
Expand Down Expand Up @@ -148,10 +150,16 @@ PHP_METHOD(Mosquitto_Client, setTlsCertificates)
is_dir = Z_BVAL_P(stat);
zval_dtor(stat);

if (key_pw != NULL) {
pw_callback = php_mosquitto_pw_callback;
MQTTG(client_key) = estrdup(key_pw);
MQTTG(client_key_len) = key_pw_len;
}

if (is_dir) {
retval = mosquitto_tls_set(object->client, NULL, ca_path, cert_path, key_path, NULL);
retval = mosquitto_tls_set(object->client, NULL, ca_path, cert_path, key_path, pw_callback);
} else {
retval = mosquitto_tls_set(object->client, ca_path, NULL, cert_path, key_path, NULL);
retval = mosquitto_tls_set(object->client, ca_path, NULL, cert_path, key_path, pw_callback);
}

php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
Expand Down Expand Up @@ -1070,6 +1078,14 @@ PHP_MOSQUITTO_API void php_mosquitto_unsubscribe_callback(struct mosquitto *mosq
}
}

static int php_mosquitto_pw_callback(char *buf, int size, int rwflag, void *userdata) {
TSRMLS_FETCH();

strncpy(buf, MQTTG(client_key), size);
efree(MQTTG(client_key));
return MQTTG(client_key_len);
}

/* {{{ mosquitto_client_methods */
const zend_function_entry mosquitto_client_methods[] = {
PHP_ME(Mosquitto_Client, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
Expand Down
1 change: 1 addition & 0 deletions php_mosquitto.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ static int php_mosquitto_message_write_##name(mosquitto_message_object *mosquitt

ZEND_BEGIN_MODULE_GLOBALS(mosquitto)
char *client_key;
int client_key_len;
zend_object_handlers mosquitto_std_object_handlers;
zend_error_handling mosquitto_original_error_handling;
ZEND_END_MODULE_GLOBALS(mosquitto)
Expand Down