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
56
57
58
59
60
61
|
<?php
// Copyright (C) 2015 Remy van Elst
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
function check_json($host,$port) {
$data = [];
$stream = stream_context_create (array("ssl" =>
array("capture_peer_cert" => true,
"capture_peer_cert_chain" => true,
"verify_peer" => false,
"verify_peer_name" => false,
"allow_self_signed" => true,
"capture_session_meta" => true,
"sni_enabled" => true)));
$read_stream = stream_socket_client("ssl://$host:$port", $errno, $errstr, 2, STREAM_CLIENT_CONNECT, $stream);
if ( $read_stream === false ) {
$data["error"] = ["Failed to connect: " . htmlspecialchars($errstr)];
return $data;
} else {
$context = stream_context_get_params($read_stream);
$context_meta = stream_context_get_options($read_stream)['ssl']['session_meta'];
$cert_data = openssl_x509_parse($context["options"]["ssl"]["peer_certificate"]);
$chain_data = $context["options"]["ssl"]["peer_certificate_chain"];
$chain_length = count($chain_data);
if (isset($chain_data) && $chain_length < 10) {
$chain_length = count($chain_data);
$chain_arr_keys = ($chain_data);
foreach(array_keys($chain_arr_keys) as $key) {
$curr = $chain_data[$key];
$next = $chain_data[$key+1];
$prev = $chain_data[$key-1];
$chain_key = (string)$key+1;
if ($key == 0) {
$data["connection"] = ssl_conn_metadata_json($host, $port, $read_stream, $chain_data);
//$data["chain"][$chain_key] = cert_parse_json($curr, $next, $host, true);
} else {
//$data["chain"][$chain_key] = cert_parse_json($curr, $next, null, false);
}
}
} else {
$data["error"] = ["Chain too long."];
return $data;
}
}
return $data;
}
?>
|