Commits

Ruslan Osmanov  committed 5764862

Add: EventSslContext properties: local_pk and local_cert
Fix: classes/ssl_context.c: passwd_callback callback fetched passphrase by string key

  • Participants
  • Parent commits 885c29e

Comments (0)

Files changed (5)

File classes/ssl_context.c

 #include "src/util.h"
 #include "src/priv.h"
 
+#include "classes/ssl_context.h"
+
 #ifndef HAVE_EVENT_OPENSSL_LIB
 # error "HAVE_EVENT_OPENSSL_LIB undefined"
 #endif
     HashTable  *ht  = (HashTable *) data;
     zval      **val = NULL;
 
-	if (zend_hash_find(ht, "passphrase", sizeof("passphrase"),
+	if (zend_hash_index_find(ht, PHP_EVENT_OPT_PASSPHRASE,
 				(void **) &val) == SUCCESS) {
         if (Z_STRLEN_PP(val) < num - 1) {
             memcpy(buf, Z_STRVAL_PP(val), Z_STRLEN_PP(val) + 1);
 }
 /* }}} */
 
-/* {{{ set_local_cert */
-static int set_local_cert(SSL_CTX *ctx, const char *certfile, const char *private_key TSRMLS_DC)
+/* {{{ _php_event_ssl_ctx_set_private_key */
+int _php_event_ssl_ctx_set_private_key(SSL_CTX *ctx, const char *private_key TSRMLS_DC)
+{
+    if (private_key) {
+        char resolved_path_buff_pk[MAXPATHLEN];
+
+        if (VCWD_REALPATH(private_key, resolved_path_buff_pk)) {
+            if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff_pk, SSL_FILETYPE_PEM) != 1) {
+                php_error_docref(NULL TSRMLS_CC, E_WARNING,
+                    	"Unable to set private key file `%s'",
+                    	resolved_path_buff_pk);
+                return -1;
+            }
+
+    		return 0;
+        }
+    }
+
+    return -1;
+}
+/* }}} */
+
+/* {{{ _php_event_ssl_ctx_set_local_cert */
+int _php_event_ssl_ctx_set_local_cert(SSL_CTX *ctx, const char *certfile, const char *private_key TSRMLS_DC)
 {
 	char resolved_path_buff[MAXPATHLEN];
 
         }
 
         if (private_key) {
-            char resolved_path_buff_pk[MAXPATHLEN];
-
-            if (VCWD_REALPATH(private_key, resolved_path_buff_pk)) {
-                if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff_pk, SSL_FILETYPE_PEM) != 1) {
-                    php_error_docref(NULL TSRMLS_CC, E_WARNING,
-                    		"Unable to set private key file `%s'",
-                    		resolved_path_buff_pk);
-                    return -1;
-                }
-            }
+        	if (_php_event_ssl_ctx_set_private_key(ctx, private_key TSRMLS_CC)) {
+        		return -1;
+        	}
         } else {
             if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff, SSL_FILETYPE_PEM) != 1) {
                 php_error_docref(NULL TSRMLS_CC, E_WARNING,
 
 				if (zend_hash_index_find(ht, PHP_EVENT_OPT_LOCAL_PK,
 						(void **) &ppz_private_key) == SUCCESS) {
-					set_local_cert(ctx, Z_STRVAL_PP(ppzval), Z_STRVAL_PP(ppz_private_key));
+					_php_event_ssl_ctx_set_local_cert(ctx, Z_STRVAL_PP(ppzval), Z_STRVAL_PP(ppz_private_key));
 				} else {
-					set_local_cert(ctx, Z_STRVAL_PP(ppzval), NULL);
+					_php_event_ssl_ctx_set_local_cert(ctx, Z_STRVAL_PP(ppzval), NULL);
 				}
 				break;
 			case PHP_EVENT_OPT_LOCAL_PK:

File classes/ssl_context.h

+/*
+   +----------------------------------------------------------------------+
+   | PHP Version 5                                                        |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1997-2013 The PHP Group                                |
+   +----------------------------------------------------------------------+
+   | This source file is subject to version 3.01 of the PHP license,      |
+   | that is bundled with this package in the file LICENSE, and is        |
+   | available through the world-wide-web at the following url:           |
+   | http://www.php.net/license/3_01.txt                                  |
+   | If you did not receive a copy of the PHP license and are unable to   |
+   | obtain it through the world-wide-web, please send a note to          |
+   | license@php.net so we can mail you a copy immediately.               |
+   +----------------------------------------------------------------------+
+   | Author: Ruslan Osmanov <osmanov@php.net>                             |
+   +----------------------------------------------------------------------+
+*/
+
+#ifndef PHP_EVENT_SSL_CONTEXT_H
+#define PHP_EVENT_SSL_CONTEXT_H
+
+int _php_event_ssl_ctx_set_private_key(SSL_CTX *ctx, const char *private_key TSRMLS_DC);
+int _php_event_ssl_ctx_set_local_cert(SSL_CTX *ctx, const char *certfile, const char *private_key TSRMLS_DC);
+
+#endif /* PHP_EVENT_SSL_CONTEXT_H */
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: noet sw=4 ts=4 sts=4 fdm=marker
+ * vim<600: noet sw=4 ts=4 sts=4
+ */
 static HashTable event_bevent_properties;
 static HashTable event_buffer_properties;
 static HashTable event_buffer_pos_properties;
+#ifdef HAVE_EVENT_OPENSSL_LIB
+static HashTable event_ssl_context_properties;
+#endif
 
 static zend_object_handlers object_handlers;
 
 			php_event_ssl_context_ce_functions);
 	ce = php_event_ssl_context_ce;
 	ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
+	zend_hash_init(&event_ssl_context_properties, 0, NULL, NULL, 1);
+	PHP_EVENT_ADD_CLASS_PROPERTIES(&event_ssl_context_properties, event_ssl_context_property_entries);
+	PHP_EVENT_DECL_CLASS_PROPERTIES(ce, event_ssl_context_property_entry_info);
+	zend_hash_add(&classes, ce->name, ce->name_length + 1, &event_ssl_context_properties,
+			sizeof(event_ssl_context_properties), NULL);
 #endif /* HAVE_EVENT_OPENSSL_LIB */
 
 }
 #include "src/priv.h"
 #include "src/util.h"
 
+/* {{{ get_ssl_option */
+static zval **get_ssl_option(const HashTable *ht, ulong idx)
+{
+    zval **val;
+
+	if (zend_hash_index_find(ht, idx, (void **) &val) == SUCCESS) {
+		return val;
+    }
+
+    return NULL;
+}
+/* }}} */
+
+
 /* {{{ event_timer_pending_prop_read */
 static int event_timer_pending_prop_read(php_event_abstract_object_t *obj, zval **retval TSRMLS_DC)
 {
 /* }}} */
 #endif
 
+#ifdef HAVE_EVENT_OPENSSL_LIB
+#include "classes/ssl_context.h"
+
+/* {{{ event_ssl_context_local_cert_prop_write*/
+static int event_ssl_context_local_cert_prop_write(php_event_abstract_object_t *obj, zval *value TSRMLS_DC)
+{
+	php_event_ssl_context_t *ectx = (php_event_ssl_context_t *) obj;
+	zval **val                    = get_ssl_option(ectx->ht, PHP_EVENT_OPT_LOCAL_PK);
+	char *private_key             = val ? Z_STRVAL_PP(val) : NULL;
+
+	if (_php_event_ssl_ctx_set_local_cert(ectx->ctx, Z_STRVAL_P(value), private_key TSRMLS_CC)) {
+		return FAILURE;
+	}
+
+	return SUCCESS;
+}
+/* }}} */
+
+/* {{{ event_ssl_context_local_cert_prop_read */
+static int event_ssl_context_local_cert_prop_read(php_event_abstract_object_t *obj, zval **retval TSRMLS_DC)
+{
+	php_event_ssl_context_t *ectx = (php_event_ssl_context_t *) obj;
+	zval **val                    = get_ssl_option(ectx->ht, PHP_EVENT_OPT_LOCAL_CERT);
+
+	if (val) {
+		MAKE_STD_ZVAL(*retval);
+		ZVAL_STRINGL(*retval, Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1);
+	} else {
+		ALLOC_INIT_ZVAL(*retval);
+	}
+
+	return SUCCESS;
+}
+/* }}} */
+
+/* {{{ event_ssl_context_local_pk_prop_write */
+static int event_ssl_context_local_pk_prop_write(php_event_abstract_object_t *obj, zval *value TSRMLS_DC)
+{
+	php_event_ssl_context_t *ectx = (php_event_ssl_context_t *) obj;
+
+	if (_php_event_ssl_ctx_set_private_key(ectx->ctx, Z_STRVAL_P(value) TSRMLS_CC)) {
+		return FAILURE;
+	}
+
+	return SUCCESS;
+}
+/* }}} */
+
+/* {{{ event_ssl_context_local_pk_prop_read */
+static int event_ssl_context_local_pk_prop_read(php_event_abstract_object_t *obj, zval **retval TSRMLS_DC)
+{
+	php_event_ssl_context_t *ectx = (php_event_ssl_context_t *) obj;
+	zval **val                    = get_ssl_option(ectx->ht, PHP_EVENT_OPT_LOCAL_PK);
+
+	if (val) {
+		MAKE_STD_ZVAL(*retval);
+		ZVAL_STRINGL(*retval, Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1);
+	} else {
+		ALLOC_INIT_ZVAL(*retval);
+	}
+
+	return SUCCESS;
+}
+/* }}} */
+#endif
+
 
 const php_event_property_entry_t event_property_entries[] = {
 	{"timer_pending",           sizeof("timer_pending") - 1, event_timer_pending_prop_read, NULL, NULL},
 	{"position", sizeof("position") - 1, event_buffer_pos_position_prop_read, NULL, NULL},
     {NULL, 0, NULL, NULL, NULL}
 };
+#ifdef HAVE_EVENT_OPENSSL_LIB
+const php_event_property_entry_t event_ssl_context_property_entries[] = {
+	{"local_cert", sizeof("local_cert") - 1, event_ssl_context_local_cert_prop_read, event_ssl_context_local_cert_prop_write, NULL},
+	{"local_pk", sizeof("local_pk") - 1, event_ssl_context_local_pk_prop_read, event_ssl_context_local_pk_prop_write, NULL},
+    {NULL, 0, NULL, NULL, NULL}
+};
+#endif
 
 const zend_property_info event_property_entry_info[] = {
 	{ZEND_ACC_PUBLIC, "timer_pending", sizeof("timer_pending") - 1, -1, 0, NULL, 0, NULL},
 	{ZEND_ACC_PUBLIC, "position", sizeof("position") - 1, -1, 0, NULL, 0, NULL},
 	{0, NULL, 0, -1, 0, NULL, 0, NULL}
 };
-
-
-#if HAVE_EVENT_EXTRA_LIB
-
-const php_event_property_entry_t event_dns_base_property_entries[] = {
-    {NULL, 0, NULL, NULL, NULL}
-};
-const php_event_property_entry_t event_listener_property_entries[] = {
-    {NULL, 0, NULL, NULL, NULL}
-};
-const php_event_property_entry_t event_http_conn_property_entries[] = {
-    {NULL, 0, NULL, NULL, NULL}
-};
-const php_event_property_entry_t event_http_property_entries[] = {
-    {NULL, 0, NULL, NULL, NULL}
-};
-
-const zend_property_info event_dns_base_property_entry_info[] = {
-	{0, NULL, 0, -1, 0, NULL, 0, NULL}
-};
-const zend_property_info event_listener_property_entry_info[] = {
-	{0, NULL, 0, -1, 0, NULL, 0, NULL}
-};
-const zend_property_info event_http_conn_property_entry_info[] = {
+#ifdef HAVE_EVENT_OPENSSL_LIB
+const zend_property_info event_ssl_context_property_entry_info[] = {
+	{ZEND_ACC_PUBLIC, "local_cert", sizeof("local_cert") - 1, -1, 0, NULL, 0, NULL},
+	{ZEND_ACC_PUBLIC, "local_pk", sizeof("local_pk") - 1, -1, 0, NULL, 0, NULL},
 	{0, NULL, 0, -1, 0, NULL, 0, NULL}
 };
-const zend_property_info event_http_property_entry_info[] = {
-	{0, NULL, 0, -1, 0, NULL, 0, NULL}
-};
-
 #endif
+
 /*
  * Local variables:
  * tab-width: 4
 extern const php_event_property_entry_t event_bevent_property_entries[];
 extern const php_event_property_entry_t event_buffer_property_entries[];
 extern const php_event_property_entry_t event_buffer_pos_property_entries[];
+#ifdef HAVE_EVENT_OPENSSL_LIB
+extern const php_event_property_entry_t event_ssl_context_property_entries[];
+#endif
 
 extern const zend_property_info event_property_entry_info[];
 extern const zend_property_info event_base_property_entry_info[];
 extern const zend_property_info event_bevent_property_entry_info[];
 extern const zend_property_info event_buffer_property_entry_info[];
 extern const zend_property_info event_buffer_pos_property_entry_info[];
+#ifdef HAVE_EVENT_OPENSSL_LIB
+extern const zend_property_info event_ssl_context_property_entry_info[];
+#endif
 
 #if HAVE_EVENT_EXTRA_LIB
 extern const zend_function_entry php_event_dns_base_ce_functions[];
 extern zend_class_entry *php_event_listener_ce;
 extern zend_class_entry *php_event_http_conn_ce;
 extern zend_class_entry *php_event_http_ce;
-
-extern const php_event_property_entry_t event_dns_base_property_entries[];
-extern const php_event_property_entry_t event_listener_property_entries[];
-extern const php_event_property_entry_t event_http_conn_property_entries[];
-extern const php_event_property_entry_t event_http_property_entries[];
-
-extern const zend_property_info event_dns_base_property_entry_info[];
-extern const zend_property_info event_listener_property_entry_info[];
-extern const zend_property_info event_http_conn_property_entry_info[];
-extern const zend_property_info event_http_property_entry_info[];
 #endif /* HAVE_EVENT_EXTRA_LIB */
 
 
+
 #endif /* PHP_EVENT_PRIV_H */
 
 /*