Skip to content

Commit aa931f3

Browse files
committed
Merge pull request #324
2 parents 05baae9 + c6d43a5 commit aa931f3

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

php_phongo.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -983,13 +983,13 @@ ssize_t phongo_stream_poll (mongoc_stream_poll_t *streams, size_t nstreams, int3
983983
} /* }}} */
984984

985985
#if PHP_VERSION_ID < 50600
986-
int php_mongo_verify_hostname(const char *hostname, X509 *cert TSRMLS_DC)
986+
static int php_phongo_verify_hostname(const char *hostname, X509 *cert TSRMLS_DC)
987987
{
988-
if (php_mongo_matches_san_list(cert, hostname) == SUCCESS) {
988+
if (php_mongodb_matches_san_list(cert, hostname) == SUCCESS) {
989989
return SUCCESS;
990990
}
991991

992-
if (php_mongo_matches_common_name(cert, hostname TSRMLS_CC) == SUCCESS) {
992+
if (php_mongodb_matches_common_name(cert, hostname TSRMLS_CC) == SUCCESS) {
993993
return SUCCESS;
994994
}
995995

@@ -1012,10 +1012,15 @@ int php_phongo_peer_verify(php_stream *stream, X509 *cert, const char *hostname,
10121012
peer = hostname;
10131013
}
10141014

1015-
if (php_mongo_verify_hostname(peer, cert TSRMLS_CC) == FAILURE) {
1015+
#ifdef HAVE_OPENSSL_EXT
1016+
if (php_phongo_verify_hostname(peer, cert TSRMLS_CC) == FAILURE) {
10161017
bson_set_error(error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_CONNECT, "Remote certificate SubjectAltName or CN does not match '%s'", hostname);
10171018
return false;
10181019
}
1020+
#else
1021+
bson_set_error(error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_CONNECT, "Cannot verify remote certificate SubjectAltName or CN. Please ensure that extension is compiled against PHP with OpenSSL or disable the \"verify_peer_name\" SSL context option.");
1022+
return false;
1023+
#endif
10191024
}
10201025

10211026
return true;
@@ -1067,9 +1072,10 @@ bool php_phongo_ssl_verify(php_stream *stream, const char *hostname, bson_error_
10671072
#else
10681073
if (php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "ssl", "verify_expiry", &verify_expiry) == SUCCESS && zend_is_true(*verify_expiry)) {
10691074
#endif
1075+
#ifdef HAVE_OPENSSL_EXT
10701076
time_t current = time(NULL);
1071-
time_t valid_from = php_mongo_asn1_time_to_time_t(X509_get_notBefore(cert) TSRMLS_CC);
1072-
time_t valid_until = php_mongo_asn1_time_to_time_t(X509_get_notAfter(cert) TSRMLS_CC);
1077+
time_t valid_from = php_mongodb_asn1_time_to_time_t(X509_get_notBefore(cert) TSRMLS_CC);
1078+
time_t valid_until = php_mongodb_asn1_time_to_time_t(X509_get_notAfter(cert) TSRMLS_CC);
10731079

10741080
if (valid_from > current) {
10751081
bson_set_error(error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_CONNECT, "Certificate is not valid yet on %s", hostname);
@@ -1079,6 +1085,10 @@ bool php_phongo_ssl_verify(php_stream *stream, const char *hostname, bson_error_
10791085
bson_set_error(error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_CONNECT, "Certificate has expired on %s", hostname);
10801086
return false;
10811087
}
1088+
#else
1089+
bson_set_error(error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_CONNECT, "Cannot verify certificate expiration. Please ensure that extension is compiled against PHP with OpenSSL or disable the \"verify_expiry\" SSL context option.");
1090+
return false;
1091+
#endif
10821092
}
10831093

10841094
return true;

src/contrib/php-ssl.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#define timezone _timezone /* timezone is called _timezone in LibC */
3535
#endif
3636

37-
int php_mongo_matches_wildcard_name(const char *subjectname, const char *certname) /* {{{ */
37+
int php_mongodb_matches_wildcard_name(const char *subjectname, const char *certname) /* {{{ */
3838
{
3939
char *wildcard = NULL;
4040
int prefix_len, suffix_len, subject_len;
@@ -70,7 +70,7 @@ int php_mongo_matches_wildcard_name(const char *subjectname, const char *certnam
7070
}
7171
/* }}} */
7272

73-
int php_mongo_matches_san_list(X509 *peer, const char *subject_name) /* {{{ */
73+
int php_mongodb_matches_san_list(X509 *peer, const char *subject_name) /* {{{ */
7474
{
7575
int i, len;
7676
unsigned char *cert_name = NULL;
@@ -96,7 +96,7 @@ int php_mongo_matches_san_list(X509 *peer, const char *subject_name) /* {{{ */
9696
cert_name[len-1] = '\0';
9797
}
9898

99-
if (php_mongo_matches_wildcard_name(subject_name, (const char *)cert_name) == SUCCESS) {
99+
if (php_mongodb_matches_wildcard_name(subject_name, (const char *)cert_name) == SUCCESS) {
100100
OPENSSL_free(cert_name);
101101
return SUCCESS;
102102
}
@@ -124,7 +124,7 @@ int php_mongo_matches_san_list(X509 *peer, const char *subject_name) /* {{{ */
124124
}
125125
/* }}} */
126126

127-
int php_mongo_matches_common_name(X509 *peer, const char *subject_name TSRMLS_DC) /* {{{ */
127+
int php_mongodb_matches_common_name(X509 *peer, const char *subject_name TSRMLS_DC) /* {{{ */
128128
{
129129
char buf[1024];
130130
X509_NAME *cert_name;
@@ -137,7 +137,7 @@ int php_mongo_matches_common_name(X509 *peer, const char *subject_name TSRMLS_DC
137137
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate peer certificate CN");
138138
} else if ((size_t) cert_name_len != strlen(buf)) {
139139
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Peer certificate CN=`%.*s' is malformed", cert_name_len, buf);
140-
} else if (php_mongo_matches_wildcard_name(subject_name, buf) == SUCCESS) {
140+
} else if (php_mongodb_matches_wildcard_name(subject_name, buf) == SUCCESS) {
141141
return SUCCESS;
142142
} else {
143143
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Peer certificate CN=`%.*s' did not match expected CN=`%s'", cert_name_len, buf, subject_name);
@@ -147,7 +147,7 @@ int php_mongo_matches_common_name(X509 *peer, const char *subject_name TSRMLS_DC
147147
}
148148
/* }}} */
149149

150-
time_t php_mongo_asn1_time_to_time_t(ASN1_UTCTIME * timestr TSRMLS_DC) /* {{{ */
150+
time_t php_mongodb_asn1_time_to_time_t(ASN1_UTCTIME * timestr TSRMLS_DC) /* {{{ */
151151
{
152152
/*
153153
This is how the time string is formatted:

src/contrib/php-ssl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
#include <openssl/x509.h>
3535
#include <openssl/x509v3.h>
3636

37-
int php_mongo_matches_wildcard_name(const char *subjectname, const char *certname);
38-
int php_mongo_matches_san_list(X509 *peer, const char *subject_name);
39-
int php_mongo_matches_common_name(X509 *peer, const char *subject_name TSRMLS_DC);
40-
time_t php_mongo_asn1_time_to_time_t(ASN1_UTCTIME * timestr TSRMLS_DC);
37+
int php_mongodb_matches_wildcard_name(const char *subjectname, const char *certname);
38+
int php_mongodb_matches_san_list(X509 *peer, const char *subject_name);
39+
int php_mongodb_matches_common_name(X509 *peer, const char *subject_name TSRMLS_DC);
40+
time_t php_mongodb_asn1_time_to_time_t(ASN1_UTCTIME * timestr TSRMLS_DC);
4141

4242
#endif /* HAVE_OPENSSL_EXT */
4343
#endif

0 commit comments

Comments
 (0)