Project

General

Profile

Bug #11830 ยป openvpn.tls-verify.php

Konstantin Panchenko, 01/20/2022 03:47 AM

 
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-2022 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
syslog(LOG_WARNING, "Certificate Depth {$cert_depth}");
51
syslog(LOG_WARNING, "Certificate Subject {$cert_subject}");
52
syslog(LOG_WARNING, "Certificate Allowed Depth {$allowed_depth}");
53
syslog(LOG_WARNING, "Certificate Server CN {$server_cn}");
54
syslog(LOG_WARNING, "Certificate Serial {$_GET['serial']}");
55
syslog(LOG_WARNING, "Current Config {$_GET['config']}");
56

    
57
/* Reserved for future use in case we decide to verify CNs and such as well
58
$subj = explode("/", $cert_subject);
59
foreach ($subj at $s) {
60
	list($n, $v) = explode("=", $s);
61
	if ($n == "CN") {
62
		$common_name = $v;
63
	}
64
}
65
*/
66

    
67
/* Replaced by sed with proper variables used below ( $server_cn and $allowed_depth ). */
68
//<template>
69

    
70
if (isset($allowed_depth) && ($cert_depth > $allowed_depth)) {
71
	syslog(LOG_WARNING, "Certificate depth {$cert_depth} exceeded max allowed depth of {$allowed_depth}.");
72
	if (isset($_GET['certdepth'])) {
73
		echo "FAILED";
74
		closelog();
75
		return;
76
	} else {
77
		closelog();
78
		exit(1);
79
	}
80
}
81

    
82
syslog(LOG_WARNING, "Current config {$_GET['config']}");
83

    
84
preg_match('/\/var\/etc\/openvpn\/server(\d+)\/config\.ovpn/', $_GET['config'], $current_vpnid);
85
$current_vpnid_str = implode(" ", $current_vpnid);
86
syslog(LOG_WARNING, "Current VPN ID {$current_vpnid_str}");
87

    
88
$iteration = 1;
89
foreach ($config['openvpn']['openvpn-server'] as $ovpns) {
90
	syslog(LOG_WARNING, "Iteration {$iteration}");
91
	$ovpns_str = implode(",",$ovpns);
92
	syslog(LOG_WARNING, "OpenVPN Configuration {$ovpns_str}");
93
	if (($ovpns['vpnid'] == $current_vpnid['1']) && ($ovpns['ocspcheck'] == 'yes')) {
94
		$capath = "/var/etc/openvpn/server{$ovpns['vpnid']}/ca/";
95
		$ca = lookup_ca($ovpns['caref']);
96
		$cert_contents = base64_decode($ca['crt']);
97
		$cert_details = openssl_x509_parse($cert_contents);
98
		$issuer = $capath . $cert_details['hash'] . ".0";
99
		$serial = $_GET['serial'];
100
		$status_out = null;
101
		$status = exec("/usr/bin/openssl ocsp -resp_text -issuer " . escapeshellarg($issuer)
102
			. " -no_nonce"
103
			. " -CApath " . escapeshellarg($capath)
104
			. " -url " . escapeshellarg($ovpns['ocspurl'])
105
			. " -serial " . escapeshellarg($serial), $status_out);
106

    
107
		$status = implode(",", $status_out);
108
		syslog(LOG_WARNING, "Returned status {$status}");
109
		if (preg_match('/(error|fail)/', $status)) {
110
			syslog(LOG_WARNING, "Certificate - (error|fail)");
111
			echo "FAILED";
112
			closelog();
113
			return;
114
		} else if (preg_match('/Cert Status: good/', $status)) {
115
			syslog(LOG_WARNING, "Cert Status good!");
116
			if (preg_match('/OCSP Response Status: successful \(0x0\)/', $status)) {
117
				syslog(LOG_WARNING, "Response verify OK");
118
				break;
119
			}
120
		} else {
121
			syslog(LOG_WARNING, "Certificate - General fail");
122
			echo "FAILED";
123
			closelog();
124
			return;
125
		}
126
	}
127
	$iteration = ++$iteration;
128
}
129

    
130
// Debug
131
//syslog(LOG_WARNING, "Found certificate {$argv[2]} with depth {$cert_depth}");
132

    
133
syslog(LOG_WARNING, "Validation done!!");
134

    
135
closelog();
136
if (isset($_GET['certdepth'])) {
137
	echo "OK";
138
} else {
139
	exit(0);
140
}
141

    
142
?>
    (1-1/1)