Project

General

Profile

Download (3.3 KB) Statistics
| Branch: | Tag: | Revision:
1 cb7d18d5 Renato Botelho
#!/usr/local/bin/php-cgi -f
2 98963f27 jim-p
<?php
3
/*
4 ac24dc24 Renato Botelho
 * openvpn.tls-verify.php
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7 38809d47 Renato Botelho do Couto
 * Copyright (c) 2011-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9 0284d79e jim-p
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
10 ac24dc24 Renato Botelho
 * All rights reserved.
11
 *
12 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15 ac24dc24 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
17 ac24dc24 Renato Botelho
 *
18 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23 ac24dc24 Renato Botelho
 */
24 09221bc3 Renato Botelho
25 98963f27 jim-p
/*
26
 * OpenVPN calls this script to validate a certificate
27
 *  This script is called ONCE per DEPTH of the certificate chain
28
 *  Normal operation would have two runs - one for the server certificate
29
 *  and one for the client certificate. Beyond that, you're dealing with
30
 *  intermediates.
31
 */
32
33
require_once("globals.inc");
34
require_once("config.inc");
35
require_once("interfaces.inc");
36
37
openlog("openvpn", LOG_ODELAY, LOG_AUTH);
38
39
/* read data from command line */
40 ed56ce5a Ermal LUÇI
if (isset($_GET['certdepth'])) {
41 b95b40a1 Ermal
	$cert_depth = $_GET['certdepth'];
42
	$cert_subject = urldecode($_GET['certsubject']);
43
	$allowed_depth = $_GET['depth'];
44
	$server_cn = $_GET['servercn'];
45
} else {
46
	$cert_depth = intval($argv[1]);
47
	$cert_subject = $argv[2];
48
}
49 98963f27 jim-p
50
/* Reserved for future use in case we decide to verify CNs and such as well
51
$subj = explode("/", $cert_subject);
52
foreach ($subj at $s) {
53
	list($n, $v) = explode("=", $s);
54 b37a2e8c Phil Davis
	if ($n == "CN") {
55 98963f27 jim-p
		$common_name = $v;
56 b37a2e8c Phil Davis
	}
57 98963f27 jim-p
}
58
*/
59
60
/* Replaced by sed with proper variables used below ( $server_cn and $allowed_depth ). */
61
//<template>
62
63
if (isset($allowed_depth) && ($cert_depth > $allowed_depth)) {
64 882af7b4 jim-p
	syslog(LOG_WARNING, "Certificate depth {$cert_depth} exceeded max allowed depth of {$allowed_depth}.");
65 ed56ce5a Ermal LUÇI
	if (isset($_GET['certdepth'])) {
66 b95b40a1 Ermal
		echo "FAILED";
67
		closelog();
68
		return;
69
	} else {
70
		closelog();
71
		exit(1);
72
	}
73 98963f27 jim-p
}
74
75 79bcd2d2 Viktor G
preg_match('/\/var\/etc\/openvpn\/server(\d+)\/config\.ovpn/', $_GET['config'], $current_vpnid);
76
foreach ($config['openvpn']['openvpn-server'] as $ovpns) {
77
	if (($ovpns['vpnid'] == $current_vpnid['1']) && ($ovpns['ocspcheck'] == 'yes')) {
78
		$capath = "/var/etc/openvpn/server{$ovpns['vpnid']}/ca/";
79
		$ca = lookup_ca($ovpns['caref']);
80
		$cert_contents = base64_decode($ca['crt']);
81 3db11061 jim-p
		$cert_details = openssl_x509_parse($cert_contents);
82
		$issuer = $capath . $cert_details['hash'] . ".0";
83 79bcd2d2 Viktor G
		$serial = $_GET['serial'];
84 3db11061 jim-p
		$status = exec("/usr/bin/openssl ocsp -issuer " . escapeshellarg($issuer)
85 79bcd2d2 Viktor G
			. " -no_nonce"
86
			. " -CApath " . escapeshellarg($capath)
87
			. " -url " . escapeshellarg($ovpns['ocspurl'])
88
			. " -serial " . escapeshellarg($serial));
89
		if (preg_match('/(error|fail)/', $status)) {
90
			echo "FAILED";
91
			closelog();
92
			return;
93 ffc44c36 jim-p
		} else if (preg_match('/Cert Status: good/', $status)) {
94
			if (preg_match('/^Response verify OK/', $status)) {
95 79bcd2d2 Viktor G
				break;
96
			}
97
		} else {
98
			echo "FAILED";
99
			closelog();
100
			return;
101
		}
102
	}
103
}
104
105 98963f27 jim-p
// Debug
106 882af7b4 jim-p
//syslog(LOG_WARNING, "Found certificate {$argv[2]} with depth {$cert_depth}");
107 98963f27 jim-p
108 b95b40a1 Ermal
closelog();
109 b37a2e8c Phil Davis
if (isset($_GET['certdepth'])) {
110 b95b40a1 Ermal
	echo "OK";
111 b37a2e8c Phil Davis
} else {
112 b95b40a1 Ermal
	exit(0);
113 b37a2e8c Phil Davis
}
114 98963f27 jim-p
115
?>