Project

General

Profile

Download (3.3 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2
<?php
3
/*
4
 * openvpn.tls-verify.php
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7
 * Copyright (c) 2011-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
10
 * All rights reserved.
11
 *
12
 * 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
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * 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
 */
24

    
25
/*
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
if (isset($_GET['certdepth'])) {
41
	$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

    
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
	if ($n == "CN") {
55
		$common_name = $v;
56
	}
57
}
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
	syslog(LOG_WARNING, "Certificate depth {$cert_depth} exceeded max allowed depth of {$allowed_depth}.");
65
	if (isset($_GET['certdepth'])) {
66
		echo "FAILED";
67
		closelog();
68
		return;
69
	} else {
70
		closelog();
71
		exit(1);
72
	}
73
}
74

    
75
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
		$cert_details = openssl_x509_parse($cert_contents);
82
		$issuer = $capath . $cert_details['hash'] . ".0";
83
		$serial = $_GET['serial'];
84
		$status = exec("/usr/bin/openssl ocsp -issuer " . escapeshellarg($issuer)
85
			. " -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
		} else if (preg_match('/Cert Status: good/', $status)) {
94
			if (preg_match('/^Response verify OK/', $status)) {
95
				break;
96
			}
97
		} else {
98
			echo "FAILED";
99
			closelog();
100
			return;
101
		}
102
	}
103
}
104

    
105
// Debug
106
//syslog(LOG_WARNING, "Found certificate {$argv[2]} with depth {$cert_depth}");
107

    
108
closelog();
109
if (isset($_GET['certdepth'])) {
110
	echo "OK";
111
} else {
112
	exit(0);
113
}
114

    
115
?>
(34-34/61)