Skip to content

Commit 75ceeaf

Browse files
bjoriderickr
authored andcommitted
PHPC-391 & PHPC-389: Stream and SSL API changes
1 parent f1372c8 commit 75ceeaf

File tree

3 files changed

+72
-26
lines changed

3 files changed

+72
-26
lines changed

config.m4

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,6 @@ if test "$MONGODB" != "no"; then
134134
EXTRA_LDFLAGS="$COVERAGE_CFLAGS"
135135
fi
136136

137-
if test "$PHP_FOUND_VERNUM" -lt "70000"; then
138-
AC_DEFINE(PHONGO_TODO_SSL, 1, [Worksfine in PHP5])
139-
AC_DEFINE(PHONGO_TODO_MAKE_STD_ZVAL, 1, [Worksfine in PHP5])
140-
AC_DEFINE(PHONGO_TODO_INI, 1, [Worksfine in PHP5])
141-
AC_DEFINE(PHONGO_TODO_STREAM, 1, [Worksfine in PHP5])
142-
fi
143137
MONGODB_BSON="\
144138
src/bson.c \
145139
";

phongo_compat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
# define PHONGO_RETVAL_STRINGL(s, slen) RETVAL_STRINGL(s, slen, 1)
200200
# define PHONGO_RETURN_STRINGL(s, slen) RETURN_STRINGL(s, slen, 1)
201201
# define PHONGO_RETURN_STRING(s) RETURN_STRING(s, 1)
202+
# define PHP_STREAM_CONTEXT(stream) ((php_stream_context*) (stream)->context)
202203
#endif
203204

204205

php_phongo.c

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -997,11 +997,11 @@ int php_phongo_peer_verify(php_stream *stream, X509 *cert, const char *hostname,
997997
zval **verify_peer_name;
998998

999999
/* This option is available since PHP 5.6.0 */
1000-
if (php_stream_context_get_option(stream->context, "ssl", "verify_peer_name", &verify_peer_name) == SUCCESS && zend_is_true(*verify_peer_name)) {
1000+
if (php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "ssl", "verify_peer_name", &verify_peer_name) == SUCCESS && zend_is_true(*verify_peer_name)) {
10011001
zval **zhost = NULL;
10021002
const char *peer;
10031003

1004-
if (php_stream_context_get_option(stream->context, "ssl", "peer_name", &zhost) == SUCCESS) {
1004+
if (php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "ssl", "peer_name", &zhost) == SUCCESS) {
10051005
convert_to_string_ex(zhost);
10061006
peer = Z_STRVAL_PP(zhost);
10071007
} else {
@@ -1018,23 +1018,35 @@ int php_phongo_peer_verify(php_stream *stream, X509 *cert, const char *hostname,
10181018
}
10191019
#endif
10201020

1021-
#ifdef PHONGO_TODO_SSL
10221021
bool php_phongo_ssl_verify(php_stream *stream, const char *hostname, bson_error_t *error TSRMLS_DC)
10231022
{
1023+
#if PHP_VERSION_ID >= 70000
1024+
zval *zcert;
1025+
zval *verify_expiry;
1026+
#else
10241027
zval **zcert;
10251028
zval **verify_expiry;
1029+
#endif
10261030
X509 *cert;
10271031

1028-
if (!stream->context) {
1032+
if (!PHP_STREAM_CONTEXT(stream)) {
10291033
return true;
10301034
}
10311035

1032-
if (!(php_stream_context_get_option(stream->context, "ssl", "peer_certificate", &zcert) == SUCCESS && Z_TYPE_PP(zcert) == IS_RESOURCE)) {
1036+
#if PHP_VERSION_ID >= 70000
1037+
if (!((zcert = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "ssl", "peer_certificate")) != NULL && Z_TYPE_P(zcert) == IS_RESOURCE)) {
1038+
#else
1039+
if (!(php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "ssl", "peer_certificate", &zcert) == SUCCESS && Z_TYPE_PP(zcert) == IS_RESOURCE)) {
1040+
#endif
10331041
bson_set_error(error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_CONNECT, "Could not capture certificate of %s", hostname);
10341042
return false;
10351043
}
10361044

1045+
#if PHP_VERSION_ID >= 70000
1046+
cert = (X509 *)x509_from_zval(zcert TSRMLS_CC);
1047+
#else
10371048
cert = (X509 *)x509_from_zval(*zcert TSRMLS_CC);
1049+
#endif
10381050
if (!cert) {
10391051
bson_set_error(error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_CONNECT, "Could not get certificate of %s", hostname);
10401052
return false;
@@ -1046,7 +1058,11 @@ bool php_phongo_ssl_verify(php_stream *stream, const char *hostname, bson_error_
10461058
}
10471059
#endif
10481060

1049-
if (php_stream_context_get_option(stream->context, "ssl", "verify_expiry", &verify_expiry) == SUCCESS && zend_is_true(*verify_expiry)) {
1061+
#if PHP_VERSION_ID >= 70000
1062+
if ((verify_expiry = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "ssl", "verify_expiry")) != NULL && zend_is_true(verify_expiry)) {
1063+
#else
1064+
if (php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "ssl", "verify_expiry", &verify_expiry) == SUCCESS && zend_is_true(*verify_expiry)) {
1065+
#endif
10501066
time_t current = time(NULL);
10511067
time_t valid_from = php_mongo_asn1_time_to_time_t(X509_get_notBefore(cert) TSRMLS_CC);
10521068
time_t valid_until = php_mongo_asn1_time_to_time_t(X509_get_notAfter(cert) TSRMLS_CC);
@@ -1063,7 +1079,6 @@ bool php_phongo_ssl_verify(php_stream *stream, const char *hostname, bson_error_
10631079

10641080
return true;
10651081
}
1066-
#endif
10671082

10681083
mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_host_list_t *host, void *user_data, bson_error_t *error) /* {{{ */
10691084
{
@@ -1136,18 +1151,17 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
11361151
efree(uniqid);
11371152

11381153
if (mongoc_uri_get_ssl(uri)) {
1139-
#ifdef PHONGO_TODO_SSL
11401154
zend_error_handling error_handling;
11411155

11421156
zend_replace_error_handling(EH_THROW, php_phongo_sslconnectionexception_ce, &error_handling TSRMLS_CC);
11431157

11441158
MONGOC_DEBUG("Enabling SSL");
11451159

11461160
/* Capture the server certificate so we can do further verification */
1147-
if (stream->context) {
1161+
if (PHP_STREAM_CONTEXT(stream)) {
11481162
zval capture;
11491163
ZVAL_BOOL(&capture, 1);
1150-
php_stream_context_set_option(stream->context, "ssl", "capture_peer_cert", &capture);
1164+
php_stream_context_set_option(PHP_STREAM_CONTEXT(stream), "ssl", "capture_peer_cert", &capture);
11511165
}
11521166

11531167
if (php_stream_xport_crypto_setup(stream, PHONGO_CRYPTO_METHOD, NULL TSRMLS_CC) < 0) {
@@ -1174,7 +1188,6 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
11741188
}
11751189

11761190
zend_restore_error_handling(&error_handling TSRMLS_CC);
1177-
#endif
11781191
}
11791192
efree(dsn);
11801193

@@ -1552,9 +1565,37 @@ static mongoc_uri_t *php_phongo_make_uri(const char *uri_string, bson_t *options
15521565
return uri;
15531566
} /* }}} */
15541567

1555-
#ifdef PHONGO_TODO_SSL
15561568
void php_phongo_populate_default_ssl_ctx(php_stream_context *ctx, zval *driverOptions) /* {{{ */
15571569
{
1570+
#if PHP_VERSION_ID >= 70000
1571+
zval *tmp;
1572+
1573+
#define SET_STRING_CTX(name) \
1574+
if (driverOptions && php_array_exists(driverOptions, name)) { \
1575+
zval ztmp; \
1576+
zend_bool ctmp_free; \
1577+
int ctmp_len; \
1578+
char *ctmp; \
1579+
ctmp = php_array_fetchl_string(driverOptions, name, sizeof(name)-1, &ctmp_len, &ctmp_free); \
1580+
ZVAL_STRING(&ztmp, ctmp); \
1581+
if (ctmp_free) { \
1582+
str_efree(ctmp); \
1583+
} \
1584+
php_stream_context_set_option(ctx, "ssl", name, &ztmp); \
1585+
}
1586+
#define SET_BOOL_CTX(name, defaultvalue) \
1587+
{ \
1588+
zval ztmp; \
1589+
if (driverOptions && php_array_exists(driverOptions, name)) { \
1590+
ZVAL_BOOL(&ztmp, php_array_fetchl_bool(driverOptions, ZEND_STRL(name))); \
1591+
php_stream_context_set_option(ctx, "ssl", name, &ztmp); \
1592+
} \
1593+
else if ((tmp = php_stream_context_get_option(ctx, "ssl", name)) == NULL) { \
1594+
ZVAL_BOOL(&ztmp, defaultvalue); \
1595+
php_stream_context_set_option(ctx, "ssl", name, &ztmp); \
1596+
} \
1597+
}
1598+
#else
15581599
zval **tmp;
15591600

15601601
#define SET_STRING_CTX(name) \
@@ -1567,7 +1608,6 @@ void php_phongo_populate_default_ssl_ctx(php_stream_context *ctx, zval *driverOp
15671608
ZVAL_STRING(&ztmp, ctmp, ctmp_free); \
15681609
php_stream_context_set_option(ctx, "ssl", name, &ztmp); \
15691610
}
1570-
15711611
#define SET_BOOL_CTX(name, defaultvalue) \
15721612
{ \
15731613
zval ztmp; \
@@ -1580,6 +1620,7 @@ void php_phongo_populate_default_ssl_ctx(php_stream_context *ctx, zval *driverOp
15801620
php_stream_context_set_option(ctx, "ssl", name, &ztmp); \
15811621
} \
15821622
}
1623+
#endif
15831624

15841625
SET_BOOL_CTX("verify_peer", 1);
15851626
SET_BOOL_CTX("verify_peer_name", 1);
@@ -1597,7 +1638,6 @@ void php_phongo_populate_default_ssl_ctx(php_stream_context *ctx, zval *driverOp
15971638
#undef SET_BOOL_CTX
15981639
#undef SET_STRING_CTX
15991640
} /* }}} */
1600-
#endif
16011641

16021642
static bool php_phongo_apply_rp_options_to_client(mongoc_client_t *client, bson_t *options TSRMLS_DC) /* {{{ */
16031643
{
@@ -1821,19 +1861,20 @@ static mongoc_client_t *php_phongo_make_mongo_client(const mongoc_uri_t *uri, zv
18211861
}
18221862
#endif
18231863

1824-
#ifdef PHONGO_TODO_STREAM
1864+
#if PHP_VERSION_ID >= 70000
1865+
if (driverOptions && (tmp = zend_hash_str_find(Z_ARRVAL_P(driverOptions), "context", sizeof("context")-1)) != NULL) {
1866+
ctx = php_stream_context_from_zval(tmp, 0);
1867+
#else
18251868
if (driverOptions && zend_hash_find(Z_ARRVAL_P(driverOptions), "context", strlen("context") + 1, (void**)&tmp) == SUCCESS) {
18261869
ctx = php_stream_context_from_zval(*tmp, 0);
1870+
#endif
18271871
} else {
18281872
GET_DEFAULT_CONTEXT();
18291873
}
1830-
#endif
18311874

1832-
#ifdef PHONGO_TODO_SSL
18331875
if (mongoc_uri_get_ssl(uri)) {
18341876
php_phongo_populate_default_ssl_ctx(ctx, driverOptions);
18351877
}
1836-
#endif
18371878

18381879
#ifdef HAVE_SYSTEM_LIBMONGOC
18391880
mongoc_version = mongoc_get_version();
@@ -1866,23 +1907,33 @@ static mongoc_client_t *php_phongo_make_mongo_client(const mongoc_uri_t *uri, zv
18661907
mech = mongoc_uri_get_auth_mechanism(uri);
18671908

18681909
/* Check if we are doing X509 auth, in which case extract the username (subject) from the cert if no username is provided */
1869-
#ifdef PHONGO_TODO_SSL
18701910
if (mech && !strcasecmp(mech, "MONGODB-X509") && !mongoc_uri_get_username(uri)) {
1911+
#if PHP_VERSION_ID >= 70000
1912+
zval *pem;
1913+
#else
18711914
zval **pem;
1915+
#endif
18721916

1917+
#if PHP_VERSION_ID >= 70000
1918+
if ((pem = php_stream_context_get_option(ctx, "ssl", "local_cert")) != NULL) {
1919+
#else
18731920
if (SUCCESS == php_stream_context_get_option(ctx, "ssl", "local_cert", &pem)) {
1921+
#endif
18741922
char filename[MAXPATHLEN];
18751923

1924+
#if PHP_VERSION_ID >= 70000
1925+
if (VCWD_REALPATH(zval_get_string(pem)->val, filename)) {
1926+
#else
18761927
convert_to_string_ex(pem);
18771928
if (VCWD_REALPATH(Z_STRVAL_PP(pem), filename)) {
1929+
#endif
18781930
mongoc_ssl_opt_t ssl_options;
18791931

18801932
ssl_options.pem_file = filename;
18811933
mongoc_client_set_ssl_opts(client, &ssl_options);
18821934
}
18831935
}
18841936
}
1885-
#endif
18861937

18871938
mongoc_client_set_stream_initiator(client, phongo_stream_initiator, ctx);
18881939

0 commit comments

Comments
 (0)