Skip to content

Commit 91704ac

Browse files
committed
Allow ssl client to only set certificate
1 parent d78ca8c commit 91704ac

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

src/protocol/ssl.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ bool SSLContext::create() {
437437
ERR_reason_error_string(error), error);
438438
return false;
439439
}
440+
}
441+
if (!key_file.empty()) {
440442
/*
441443
* set the private key from KeyFile (may be the same as CertFile)
442444
*/
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
--TEST--
2+
swoole_runtime/ssl: client with local_cert/local_pk
3+
--SKIPIF--
4+
<?php
5+
require __DIR__ . '/../../include/skipif.inc';
6+
skip_if_no_ssl();
7+
?>
8+
--FILE--
9+
<?php
10+
require __DIR__ . '/../../include/bootstrap.php';
11+
12+
swoole\runtime::enableCoroutine();
13+
14+
$ready = new Chan;
15+
16+
go(function () use ($ready) {
17+
$context = stream_context_create();
18+
stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
19+
stream_context_set_option($context, 'ssl', 'verify_peer', true);
20+
stream_context_set_option($context, 'ssl', 'local_cert', SSL_FILE_DIR.'/server.crt');
21+
stream_context_set_option($context, 'ssl', 'local_pk', SSL_FILE_DIR.'/server.key');
22+
stream_context_set_option($context, 'ssl', 'cafile', SSL_FILE_DIR.'/ca.crt');
23+
24+
$socket = stream_socket_server("ssl://0.0.0.0:8000", $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context);
25+
if (!$socket) {
26+
echo "$errstr ($errno)<br />\n";
27+
} else {
28+
$ready->push(true);
29+
$conn = stream_socket_accept($socket);
30+
fwrite($conn, 'The local time is ' . date('n/j/Y g:i a'));
31+
fclose($conn);
32+
fclose($socket);
33+
echo "OK\n";
34+
}
35+
});
36+
37+
go(function () use ($ready) {
38+
$ready->pop();
39+
40+
$context = stream_context_create();
41+
stream_context_set_option($context, 'ssl', 'local_cert', SSL_FILE_DIR . '/client.crt');
42+
stream_context_set_option($context, 'ssl', 'local_pk', SSL_FILE_DIR . '/client.key');
43+
44+
$fp = stream_socket_client("ssl://127.0.0.1:8000", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
45+
if (!$fp) {
46+
echo "$errstr ($errno)<br />\n";
47+
} else {
48+
$data = fread($fp, 8192);
49+
fclose($fp);
50+
Assert::assert(strpos($data, 'local time') !== false);
51+
echo "OK\n";
52+
}
53+
});
54+
55+
swoole_event_wait();
56+
?>
57+
--EXPECT--
58+
OK
59+
OK
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
--TEST--
2+
swoole_runtime/ssl: client without local_pk
3+
--SKIPIF--
4+
<?php
5+
require __DIR__ . '/../../include/skipif.inc';
6+
skip_if_no_ssl();
7+
?>
8+
--FILE--
9+
<?php
10+
require __DIR__ . '/../../include/bootstrap.php';
11+
12+
swoole\runtime::enableCoroutine();
13+
14+
$ready = new Chan;
15+
16+
go(function () use ($ready) {
17+
$context = stream_context_create();
18+
stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
19+
stream_context_set_option($context, 'ssl', 'verify_peer', true);
20+
stream_context_set_option($context, 'ssl', 'local_cert', SSL_FILE_DIR.'/server.crt');
21+
stream_context_set_option($context, 'ssl', 'local_pk', SSL_FILE_DIR.'/server.key');
22+
stream_context_set_option($context, 'ssl', 'cafile', SSL_FILE_DIR.'/ca.crt');
23+
24+
$socket = stream_socket_server("ssl://0.0.0.0:8000", $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context);
25+
if (!$socket) {
26+
echo "$errstr ($errno)<br />\n";
27+
} else {
28+
$ready->push(true);
29+
$conn = stream_socket_accept($socket);
30+
fwrite($conn, 'The local time is ' . date('n/j/Y g:i a'));
31+
fclose($conn);
32+
fclose($socket);
33+
echo "OK\n";
34+
}
35+
});
36+
37+
go(function () use ($ready) {
38+
$ready->pop();
39+
40+
$context = stream_context_create();
41+
stream_context_set_option($context, 'ssl', 'local_cert', SSL_FILE_DIR . '/client.crt');
42+
43+
$fp = stream_socket_client("ssl://127.0.0.1:8000", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
44+
if (!$fp) {
45+
echo "$errstr ($errno)<br />\n";
46+
} else {
47+
$data = fread($fp, 8192);
48+
fclose($fp);
49+
Assert::assert(strpos($data, 'local time') !== false);
50+
echo "OK\n";
51+
}
52+
});
53+
54+
swoole_event_wait();
55+
?>
56+
--EXPECTF--
57+
Warning: stream_socket_client(): ssl require key file in %s on line %d
58+
OK
59+
OK

0 commit comments

Comments
 (0)