@@ -65,7 +65,7 @@ static int socket_flush(php_stream *stream);
65
65
static int socket_close (php_stream *stream, int close_handle);
66
66
static int socket_stat (php_stream *stream, php_stream_statbuf *ssb);
67
67
static int socket_cast (php_stream *stream, int castas, void **ret);
68
-
68
+ static bool socket_ssl_set_options (Socket *sock, php_stream_context *context);
69
69
// clang-format off
70
70
71
71
ZEND_BEGIN_ARG_INFO_EX (arginfo_swoole_void, 0 , 0 , 0 )
@@ -747,11 +747,15 @@ static bool php_openssl_capture_peer_certs(php_stream *stream, Socket *sslsock)
747
747
}
748
748
749
749
static int socket_enable_crypto (php_stream *stream, Socket *sock, php_stream_xport_crypto_param *cparam STREAMS_DC) {
750
+ php_stream_context *context = PHP_STREAM_CONTEXT (stream);
750
751
if (cparam->inputs .activate && !sock->ssl_is_available ()) {
751
752
sock->enable_ssl_encrypt ();
752
753
if (!sock->ssl_check_context ()) {
753
754
return -1 ;
754
755
}
756
+ if (!socket_ssl_set_options (sock, context)) {
757
+ return -1 ;
758
+ }
755
759
if (!sock->ssl_handshake ()) {
756
760
return -1 ;
757
761
}
@@ -760,11 +764,10 @@ static int socket_enable_crypto(php_stream *stream, Socket *sock, php_stream_xpo
760
764
return sock->ssl_shutdown () ? 0 : -1 ;
761
765
}
762
766
763
- php_stream_context *context = PHP_STREAM_CONTEXT (stream);
764
767
if (context) {
765
768
zval *val = php_stream_context_get_option (context, " ssl" , " capture_peer_cert" );
766
- if (val && zend_is_true (val)) {
767
- return php_openssl_capture_peer_certs (stream, sock) ? 0 : -1 ;
769
+ if (val && zend_is_true (val) && ! php_openssl_capture_peer_certs (stream, sock) ) {
770
+ return -1 ;
768
771
}
769
772
}
770
773
@@ -993,6 +996,49 @@ static int socket_set_option(php_stream *stream, int option, int value, void *pt
993
996
return PHP_STREAM_OPTION_RETURN_OK;
994
997
}
995
998
999
+ static bool socket_ssl_set_options (Socket *sock, php_stream_context *context) {
1000
+ if (context && ZVAL_IS_ARRAY (&context->options )) {
1001
+ #ifdef SW_USE_OPENSSL
1002
+ zval *ztmp;
1003
+
1004
+ if (sock->ssl_is_enable () && php_swoole_array_get_value (Z_ARRVAL_P (&context->options ), " ssl" , ztmp) &&
1005
+ ZVAL_IS_ARRAY (ztmp)) {
1006
+
1007
+ zval zalias;
1008
+ array_init (&zalias);
1009
+ zend_array *options = Z_ARRVAL_P (ztmp);
1010
+
1011
+ auto add_alias = [&zalias, options](const char *name, const char *alias) {
1012
+ zval *ztmp;
1013
+ if (php_swoole_array_get_value_ex (options, name, ztmp)) {
1014
+ add_assoc_zval_ex (&zalias, alias, strlen (alias), ztmp);
1015
+ zval_add_ref (ztmp);
1016
+ }
1017
+ };
1018
+
1019
+ add_alias (" peer_name" , " ssl_host_name" );
1020
+ add_alias (" verify_peer" , " ssl_verify_peer" );
1021
+ add_alias (" allow_self_signed" , " ssl_allow_self_signed" );
1022
+ add_alias (" cafile" , " ssl_cafile" );
1023
+ add_alias (" capath" , " ssl_capath" );
1024
+ add_alias (" local_cert" , " ssl_cert_file" );
1025
+ add_alias (" local_pk" , " ssl_key_file" );
1026
+ add_alias (" passphrase" , " ssl_passphrase" );
1027
+ add_alias (" verify_depth" , " ssl_verify_depth" );
1028
+ add_alias (" disable_compression" , " ssl_disable_compression" );
1029
+
1030
+ php_swoole_socket_set_ssl (sock, &zalias);
1031
+ if (!sock->ssl_check_context ()) {
1032
+ return false ;
1033
+ }
1034
+ zval_dtor (&zalias);
1035
+ }
1036
+ #endif
1037
+ }
1038
+
1039
+ return true ;
1040
+ }
1041
+
996
1042
static php_stream *socket_create (const char *proto,
997
1043
size_t protolen,
998
1044
const char *resourcename,
@@ -1055,43 +1101,8 @@ static php_stream *socket_create(const char *proto,
1055
1101
goto _failed;
1056
1102
}
1057
1103
1058
- if (context && ZVAL_IS_ARRAY (&context->options )) {
1059
- #ifdef SW_USE_OPENSSL
1060
- zval *ztmp;
1061
-
1062
- if (sock->ssl_is_enable () && php_swoole_array_get_value (Z_ARRVAL_P (&context->options ), " ssl" , ztmp) &&
1063
- ZVAL_IS_ARRAY (ztmp)) {
1064
-
1065
- zval zalias;
1066
- array_init (&zalias);
1067
- zend_array *options = Z_ARRVAL_P (ztmp);
1068
-
1069
- auto add_alias = [&zalias, options](const char *name, const char *alias) {
1070
- zval *ztmp;
1071
- if (php_swoole_array_get_value_ex (options, name, ztmp)) {
1072
- add_assoc_zval_ex (&zalias, alias, strlen (alias), ztmp);
1073
- zval_add_ref (ztmp);
1074
- }
1075
- };
1076
-
1077
- add_alias (" peer_name" , " ssl_host_name" );
1078
- add_alias (" verify_peer" , " ssl_verify_peer" );
1079
- add_alias (" allow_self_signed" , " ssl_allow_self_signed" );
1080
- add_alias (" cafile" , " ssl_cafile" );
1081
- add_alias (" capath" , " ssl_capath" );
1082
- add_alias (" local_cert" , " ssl_cert_file" );
1083
- add_alias (" local_pk" , " ssl_key_file" );
1084
- add_alias (" passphrase" , " ssl_passphrase" );
1085
- add_alias (" verify_depth" , " ssl_verify_depth" );
1086
- add_alias (" disable_compression" , " ssl_disable_compression" );
1087
-
1088
- php_swoole_socket_set_ssl (sock, &zalias);
1089
- if (!sock->ssl_check_context ()) {
1090
- goto _failed;
1091
- }
1092
- zval_dtor (&zalias);
1093
- }
1094
- #endif
1104
+ if (!socket_ssl_set_options (sock, context)) {
1105
+ goto _failed;
1095
1106
}
1096
1107
1097
1108
return stream;
0 commit comments