-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathtcp-ssl-server-client.php
More file actions
55 lines (45 loc) · 1.29 KB
/
tcp-ssl-server-client.php
File metadata and controls
55 lines (45 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
require_once __DIR__.'/../Semaphore.php';
$context = stream_context_create([
'ssl' => [
'local_cert' => __DIR__.'/ssl/server-and-key.pem',
'cafile' => __DIR__.'/ssl/ca.pem',
'capture_peer_cert' => true,
],
]);
$socketServer = stream_socket_server('tcp://127.0.0.1:19999', $errNo, $errStr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context);
stream_socket_enable_crypto($socketServer, false);
$client = stream_socket_accept($socketServer);
stream_set_blocking($client, true);
stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_TLSv1_2_SERVER);
// Verify client certificate
$name = null;
if (isset(stream_context_get_options($client)['ssl']['peer_certificate'])) {
$client_cert = stream_context_get_options($client)['ssl']['peer_certificate'];
$name = openssl_x509_parse($client_cert)['subject']['CN'];
}
if ('socket-adapter-client' == $name) {
fwrite($client, str_replace(
"\n",
"\r\n",
<<<EOR
HTTP/1.1 200 OK
Content-Type: text/plain
Test
EOR
));
} else {
fwrite($client, str_replace(
"\n",
"\r\n",
<<<EOR
HTTP/1.1 403 Invalid ssl certificate
Content-Type: text/plain
Test
EOR
));
}
while (!@feof($client)) {
@fread($client, 1000);
}
Http\Client\Socket\Tests\Semaphore::release();