Skip to content

Commit ffa8006

Browse files
committed
PHON-99: Add & Implement SSLConnectionException
1 parent c4b2651 commit ffa8006

File tree

6 files changed

+154
-10
lines changed

6 files changed

+154
-10
lines changed

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ if test "$PHONGO" != "no"; then
173173
src/MongoDB/Exception.c \
174174
src/MongoDB/RuntimeException.c \
175175
src/MongoDB/ConnectionException.c \
176+
src/MongoDB/SSLConnectionException.c \
176177
src/MongoDB/WriteException.c \
177178
";
178179

php_phongo.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -857,35 +857,39 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
857857

858858
if (!stream) {
859859
bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_CONNECT, "Failed connecting to '%s:%d': %s", host->host, host->port, errmsg);
860+
efree(dsn);
860861
return NULL;
861862
}
862863

864+
/* Avoid invalid leak warning in debug mode when freeing the stream */
865+
#if ZEND_DEBUG
866+
stream->__exposed = 1;
867+
#endif
868+
863869
if (mongoc_uri_get_ssl(uri)) {
864870
zend_error_handling error_handling;
865-
zend_replace_error_handling(EH_THROW, phongo_exception_from_mongoc_domain(MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_SOCKET), &error_handling TSRMLS_CC);
871+
zend_replace_error_handling(EH_THROW, php_phongo_sslconnectionexception_ce, &error_handling TSRMLS_CC);
866872

867873
mongoc_log(MONGOC_LOG_LEVEL_DEBUG, MONGOC_LOG_DOMAIN, "Enabling SSL");
868874
if (php_stream_xport_crypto_setup(stream, STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL TSRMLS_CC) < 0) {
869875
zend_restore_error_handling(&error_handling TSRMLS_CC);
870-
bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_INVALID_TYPE, "Failed to setup crypto, is the OpenSSL extension loaded?");
871876
php_stream_free(stream, PHP_STREAM_FREE_CLOSE_PERSISTENT | PHP_STREAM_FREE_RSRC_DTOR);
877+
bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_INVALID_TYPE, "Failed to setup crypto, is the OpenSSL extension loaded?");
878+
efree(dsn);
872879
return NULL;
873880
}
874-
zend_restore_error_handling(&error_handling TSRMLS_CC);
875881

876882
if (php_stream_xport_crypto_enable(stream, 1 TSRMLS_CC) < 0) {
883+
zend_restore_error_handling(&error_handling TSRMLS_CC);
877884
php_stream_free(stream, PHP_STREAM_FREE_CLOSE_PERSISTENT | PHP_STREAM_FREE_RSRC_DTOR);
878885
bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_INVALID_TYPE, "Failed to setup crypto, is the server running with SSL?");
886+
efree(dsn);
879887
return NULL;
880888
}
889+
zend_restore_error_handling(&error_handling TSRMLS_CC);
881890
}
882891
efree(dsn);
883892

884-
/* Avoid invalid leak warning in debug mode when freeing the stream */
885-
#if ZEND_DEBUG
886-
stream->__exposed = 1;
887-
#endif
888-
889893

890894
base_stream = ecalloc(1, sizeof(php_phongo_stream_socket));
891895
base_stream->stream = stream;
@@ -1420,6 +1424,7 @@ PHP_MINIT_FUNCTION(phongo)
14201424
PHP_MINIT(Exception)(INIT_FUNC_ARGS_PASSTHRU);
14211425
PHP_MINIT(RuntimeException)(INIT_FUNC_ARGS_PASSTHRU);
14221426
PHP_MINIT(ConnectionException)(INIT_FUNC_ARGS_PASSTHRU);
1427+
PHP_MINIT(SSLConnectionException)(INIT_FUNC_ARGS_PASSTHRU);
14231428
PHP_MINIT(WriteException)(INIT_FUNC_ARGS_PASSTHRU);
14241429

14251430
PHP_MINIT(Type)(INIT_FUNC_ARGS_PASSTHRU);

php_phongo_classes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ extern PHONGO_API zend_class_entry *php_phongo_writeresult_ce;
191191
extern PHONGO_API zend_class_entry *php_phongo_exception_ce;
192192
extern PHONGO_API zend_class_entry *php_phongo_runtimeexception_ce;
193193
extern PHONGO_API zend_class_entry *php_phongo_connectionexception_ce;
194+
extern PHONGO_API zend_class_entry *php_phongo_sslconnectionexception_ce;
194195
extern PHONGO_API zend_class_entry *php_phongo_writeexception_ce;
195196

196197
extern PHONGO_API zend_class_entry *php_phongo_type_ce;
@@ -225,6 +226,7 @@ PHP_MINIT_FUNCTION(WriteResult);
225226
PHP_MINIT_FUNCTION(Exception);
226227
PHP_MINIT_FUNCTION(RuntimeException);
227228
PHP_MINIT_FUNCTION(ConnectionException);
229+
PHP_MINIT_FUNCTION(SSLConnectionException);
228230
PHP_MINIT_FUNCTION(WriteException);
229231

230232
PHP_MINIT_FUNCTION(Type);

src/MongoDB/SSLConnectionException.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
+---------------------------------------------------------------------------+
3+
| PHP Driver for MongoDB |
4+
+---------------------------------------------------------------------------+
5+
| Copyright 2013-2014 MongoDB, Inc. |
6+
| |
7+
| Licensed under the Apache License, Version 2.0 (the "License"); |
8+
| you may not use this file except in compliance with the License. |
9+
| You may obtain a copy of the License at |
10+
| |
11+
| http://www.apache.org/licenses/LICENSE-2.0 |
12+
| |
13+
| Unless required by applicable law or agreed to in writing, software |
14+
| distributed under the License is distributed on an "AS IS" BASIS, |
15+
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16+
| See the License for the specific language governing permissions and |
17+
| limitations under the License. |
18+
+---------------------------------------------------------------------------+
19+
| Copyright (c) 2014, MongoDB, Inc. |
20+
+---------------------------------------------------------------------------+
21+
*/
22+
23+
#ifdef HAVE_CONFIG_H
24+
# include "config.h"
25+
#endif
26+
27+
/* External libs */
28+
#include <bson.h>
29+
#include <mongoc.h>
30+
31+
/* PHP Core stuff */
32+
#include <php.h>
33+
#include <php_ini.h>
34+
#include <ext/standard/info.h>
35+
#include <Zend/zend_interfaces.h>
36+
#include <ext/spl/spl_iterators.h>
37+
/* Our Compatability header */
38+
#include "php_compat_53.h"
39+
40+
/* Our stuffz */
41+
#include "php_phongo.h"
42+
#include "php_bson.h"
43+
#include <ext/spl/spl_exceptions.h>
44+
45+
46+
PHONGO_API zend_class_entry *php_phongo_sslconnectionexception_ce;
47+
48+
/* {{{ MongoDB\SSLConnectionException */
49+
50+
static zend_function_entry php_phongo_sslconnectionexception_me[] = {
51+
PHP_FE_END
52+
};
53+
54+
/* }}} */
55+
56+
57+
/* {{{ PHP_MINIT_FUNCTION */
58+
PHP_MINIT_FUNCTION(SSLConnectionException)
59+
{
60+
(void)type;
61+
(void)module_number;
62+
zend_class_entry ce;
63+
64+
INIT_NS_CLASS_ENTRY(ce, "MongoDB", "SSLConnectionException", php_phongo_sslconnectionexception_me);
65+
php_phongo_sslconnectionexception_ce = zend_register_internal_class_ex(&ce, php_phongo_connectionexception_ce, NULL TSRMLS_CC);
66+
php_phongo_sslconnectionexception_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
67+
68+
return SUCCESS;
69+
}
70+
/* }}} */
71+
72+
73+
74+
/*
75+
* Local variables:
76+
* tab-width: 4
77+
* c-basic-offset: 4
78+
* End:
79+
* vim600: noet sw=4 ts=4 fdm=marker
80+
* vim<600: noet sw=4 ts=4
81+
*/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
MongoDB\Write\Batch: #001 Variety Batch
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
$dsn = sprintf("%s/?ssl=true", MONGODB_STANDALONE_SSL_URI);
10+
11+
$SSL_DIR = realpath(__DIR__ . "/" . "./../../scripts/ssl/");
12+
$opts = array(
13+
"ssl" => array(
14+
"peer_name" => "MongoDB",
15+
"verify_peer" => true,
16+
"verify_peer_name" => true,
17+
"allow_self_signed" => false,
18+
"cafile" => $SSL_DIR . "/ca.pem", /* Defaults to openssl.cafile */
19+
"capath" => $SSL_DIR, /* Defaults to openssl.capath */
20+
"local_cert" => $SSL_DIR . "/client.pem",
21+
"passphrase" => "Very secretive client.pem passphrase",
22+
"CN_match" => "Common Name (CN) match",
23+
"verify_depth" => 5,
24+
"ciphers" => "HIGH:!EXPORT:!aNULL@STRENGTH",
25+
"capture_peer_cert" => true,
26+
"capture_peer_cert_chain" => true,
27+
"SNI_enabled" => true,
28+
"disable_compression" => false,
29+
"peer_fingerprint" => "",
30+
),
31+
);
32+
$context = stream_context_create($opts);
33+
34+
$mc = new MongoDB\Manager($dsn, array(), array("context" => $context, "debug" => STDERR));
35+
36+
37+
echo throws(function() use($mc) {
38+
$batch = new MongoDB\WriteBatch;
39+
$batch->insert(array("my" => "value"));
40+
$retval = $mc->executeWriteBatch(NS, $batch);
41+
}, "MongoDB\\SSLConnectionException", "executeWriteBatch"), "\n";
42+
/*
43+
44+
$opts = stream_context_get_params($context);
45+
var_dump($opts);
46+
$cert = openssl_x509_parse($opts["options"]["ssl"]["peer_certificate"]);
47+
var_dump($cert["name"]);
48+
*/
49+
?>
50+
===DONE===
51+
<?php exit(0); ?>
52+
--EXPECTF--
53+
OK: Got MongoDB\SSLConnectionException thrown from executeWriteBatch
54+
%s
55+
===DONE===

tests/utils/tools.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ function throws(callable $function, $exceptionname, $infunction = null) {
9494
} else {
9595
printf("ALMOST: Got %s - but was thrown in %s, not %s\n", $exceptionname, $function, $infunction);
9696
}
97-
return;
97+
return $e->getMessage();
9898
}
9999
printf("OK: Got %s\n", $exceptionname);
100100
} else {
101101
printf("ALMOST: Got %s - expected %s\n", get_class($e), $exceptionname);
102102
}
103-
return;
103+
return $e->getMessage();
104104
}
105105
echo "FAILED: Expected $exceptionname thrown!\n";
106106
}

0 commit comments

Comments
 (0)