Project

General

Profile

Download (18.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	Copyright (C) 2008 Shrew Soft Inc
5
	Copyright (C) 2010 Jim Pingle <jimp@pfsense.org>
6
	All rights reserved.
7

    
8
        Redistribution and use in source and binary forms, with or without
9
        modification, are permitted provided that the following conditions are met:
10

    
11
        1. Redistributions of source code must retain the above copyright notice,
12
           this list of conditions and the following disclaimer.
13

    
14
        2. Redistributions in binary form must reproduce the above copyright
15
           notice, this list of conditions and the following disclaimer in the
16
           documentation and/or other materials provided with the distribution.
17

    
18
        THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
        INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
        AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
        AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
        OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
        SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
        INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
        CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
        ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
        POSSIBILITY OF SUCH DAMAGE.
28

    
29
		DISABLE_PHP_LINT_CHECKING
30
		pfSense_MODULE:	certificate_managaer
31
*/
32

    
33
define("OPEN_SSL_CONF_PATH", "/etc/ssl/openssl.cnf");
34

    
35
require_once("functions.inc");
36

    
37
$openssl_digest_algs = array("sha1", "sha224", "sha256", "sha384", "sha512");
38

    
39
function & lookup_ca($refid) {
40
	global $config;
41

    
42
	if (is_array($config['ca']))
43
		foreach ($config['ca'] as & $ca)
44
			if ($ca['refid'] == $refid)
45
				return $ca;
46

    
47
	return false;
48
}
49

    
50
function & lookup_ca_by_subject($subject) {
51
	global $config;
52

    
53
	if (is_array($config['ca']))
54
		foreach ($config['ca'] as & $ca)
55
		{
56
			$ca_subject = cert_get_subject($ca['crt']);
57
			if ($ca_subject == $subject)
58
				return $ca;
59
		}
60

    
61
	return false;
62
}
63

    
64
function & lookup_cert($refid) {
65
	global $config;
66

    
67
	if (is_array($config['cert']))
68
		foreach ($config['cert'] as & $cert)
69
			if ($cert['refid'] == $refid)
70
				return $cert;
71

    
72
	return false;
73
}
74

    
75
function & lookup_cert_by_name($name) {
76
	global $config;
77
	if (is_array($config['cert']))
78
		foreach ($config['cert'] as & $cert)
79
			if ($cert['descr'] == $name)
80
				return $cert;
81
}
82

    
83
function & lookup_crl($refid) {
84
	global $config;
85

    
86
	if (is_array($config['crl']))
87
		foreach ($config['crl'] as & $crl)
88
			if ($crl['refid'] == $refid)
89
				return $crl;
90

    
91
	return false;
92
}
93

    
94
function ca_chain_array(& $cert) {
95
	if($cert['caref']) {
96
		$chain = array();
97
		$crt = lookup_ca($cert['caref']);
98
		$chain[] = $crt;
99
		while ($crt) {
100
			$caref = $crt['caref'];
101
			if($caref)
102
				$crt = lookup_ca($caref);
103
			else
104
				$crt = false;
105
			if($crt)
106
				$chain[] = $crt;
107
		}
108
		return $chain;
109
	}
110
	return false;
111
}
112

    
113
function ca_chain(& $cert) {
114
	if($cert['caref']) {
115
		$ca = "";
116
		$cas = ca_chain_array($cert);
117
		if (is_array($cas))
118
			foreach ($cas as & $ca_cert)
119
			{
120
				$ca .= base64_decode($ca_cert['crt']);
121
				$ca .= "\n";
122
			}
123
		return $ca;
124
	}
125
	return "";
126
}
127

    
128
function ca_import(& $ca, $str, $key="", $serial=0) {
129
	global $config;
130

    
131
	$ca['crt'] = base64_encode($str);
132
	if (!empty($key))
133
		$ca['prv'] = base64_encode($key);
134
	if (!empty($serial))
135
		$ca['serial'] = $serial;
136
	$subject = cert_get_subject($str, false);
137
	$issuer = cert_get_issuer($str, false);
138
	
139
	// Find my issuer unless self-signed
140
	if($issuer <> $subject) {
141
		$issuer_crt =& lookup_ca_by_subject($issuer);
142
		if($issuer_crt)
143
			$ca['caref'] = $issuer_crt['refid'];
144
	}
145

    
146
	/* Correct if child certificate was loaded first */
147
	if (is_array($config['ca']))
148
		foreach ($config['ca'] as & $oca)
149
		{
150
			$issuer = cert_get_issuer($oca['crt']);
151
			if($ca['refid']<>$oca['refid'] && $issuer==$subject)
152
				$oca['caref'] = $ca['refid'];
153
		}
154
	if (is_array($config['cert']))
155
		foreach ($config['cert'] as & $cert)
156
		{
157
			$issuer = cert_get_issuer($cert['crt']);
158
			if($issuer==$subject)
159
				$cert['caref'] = $ca['refid'];
160
		}
161
	return true;
162
}
163

    
164
function ca_create(& $ca, $keylen, $lifetime, $dn, $digest_alg = "sha256") {
165

    
166
	$args = array(
167
		"x509_extensions" => "v3_ca",
168
		"digest_alg" => $digest_alg,
169
		"private_key_bits" => (int)$keylen,
170
		"private_key_type" => OPENSSL_KEYTYPE_RSA,
171
		"encrypt_key" => false);
172

    
173
	// generate a new key pair
174
	$res_key = openssl_pkey_new($args);
175
	if (!$res_key) return false;
176

    
177
	// generate a certificate signing request
178
	$res_csr = openssl_csr_new($dn, $res_key, $args);
179
	if (!$res_csr) return false;
180

    
181
	// self sign the certificate
182
	$res_crt = openssl_csr_sign($res_csr, null, $res_key, $lifetime, $args);
183
	if (!$res_crt) return false;
184

    
185
	// export our certificate data
186
	if (!openssl_pkey_export($res_key, $str_key) ||
187
	    !openssl_x509_export($res_crt, $str_crt))
188
		return false;
189

    
190
	// return our ca information
191
	$ca['crt'] = base64_encode($str_crt);
192
	$ca['prv'] = base64_encode($str_key);
193
	$ca['serial'] = 0;
194

    
195
	return true;
196
}
197

    
198
function ca_inter_create(& $ca, $keylen, $lifetime, $dn, $caref, $digest_alg = "sha256") {
199
	// Create Intermediate Certificate Authority
200
	$signing_ca =& lookup_ca($caref);
201
	if (!$signing_ca)
202
		return false;
203

    
204
	$signing_ca_res_crt = openssl_x509_read(base64_decode($signing_ca['crt']));
205
	$signing_ca_res_key = openssl_pkey_get_private(array(0 => base64_decode($signing_ca['prv']) , 1 => ""));
206
	if (!$signing_ca_res_crt || !$signing_ca_res_key) return false;
207
	$signing_ca_serial = ++$signing_ca['serial'];
208

    
209
	$args = array(
210
		"x509_extensions" => "v3_ca",
211
		"digest_alg" => $digest_alg,
212
		"private_key_bits" => (int)$keylen,
213
		"private_key_type" => OPENSSL_KEYTYPE_RSA,
214
		"encrypt_key" => false);
215

    
216
	// generate a new key pair
217
	$res_key = openssl_pkey_new($args);
218
	if (!$res_key) return false;
219

    
220
	// generate a certificate signing request
221
	$res_csr = openssl_csr_new($dn, $res_key, $args);
222
	if (!$res_csr) return false;
223

    
224
	// Sign the certificate
225
	$res_crt = openssl_csr_sign($res_csr, $signing_ca_res_crt, $signing_ca_res_key, $lifetime, $args, $signing_ca_serial);
226
	if (!$res_crt) return false;
227

    
228
	// export our certificate data
229
	if (!openssl_pkey_export($res_key, $str_key) ||
230
	    !openssl_x509_export($res_crt, $str_crt))
231
		return false;
232

    
233
	// return our ca information
234
	$ca['crt'] = base64_encode($str_crt);
235
	$ca['prv'] = base64_encode($str_key);
236
	$ca['serial'] = 0;
237

    
238
	return true;
239
}
240

    
241
function cert_import(& $cert, $crt_str, $key_str) {
242

    
243
	$cert['crt'] = base64_encode($crt_str);
244
	$cert['prv'] = base64_encode($key_str);
245

    
246
	$subject = cert_get_subject($crt_str, false);
247
	$issuer = cert_get_issuer($crt_str, false);
248
	
249
	// Find my issuer unless self-signed
250
	if($issuer <> $subject) {
251
		$issuer_crt =& lookup_ca_by_subject($issuer);
252
		if($issuer_crt)
253
			$cert['caref'] = $issuer_crt['refid'];
254
	}
255
	return true;
256
}
257

    
258
function cert_create(& $cert, $caref, $keylen, $lifetime, $dn, $type="user", $digest_alg = "sha256") {
259

    
260
	$ca =& lookup_ca($caref);
261
	if (!$ca)
262
		return false;
263

    
264
	$ca_str_crt = base64_decode($ca['crt']);
265
	$ca_str_key = base64_decode($ca['prv']);
266
	$ca_res_crt = openssl_x509_read($ca_str_crt);
267
	$ca_res_key = openssl_pkey_get_private(array(0 => $ca_str_key, 1 => ""));
268
	if(!$ca_res_key) return false;
269
	$ca_serial = ++$ca['serial'];
270

    
271
	switch ($type) {
272
		case "ca":
273
			$cert_type = "v3_ca";
274
			break;
275
		case "server":
276
			$cert_type = "server";
277
			break;
278
		default:
279
			$cert_type = "usr_cert";
280
			break;
281
	}
282

    
283
	$args = array(
284
		"x509_extensions" => $cert_type,
285
		"digest_alg" => $digest_alg,
286
		"private_key_bits" => (int)$keylen,
287
		"private_key_type" => OPENSSL_KEYTYPE_RSA,
288
		"encrypt_key" => false);
289

    
290
	// generate a new key pair
291
	$res_key = openssl_pkey_new($args);
292
	if(!$res_key) return false;
293

    
294
	// generate a certificate signing request
295
	$res_csr = openssl_csr_new($dn, $res_key, $args);
296
	if(!$res_csr) return false;
297

    
298
	// self sign the certificate
299
	$res_crt = openssl_csr_sign($res_csr, $ca_res_crt, $ca_res_key, $lifetime,
300
				 $args, $ca_serial);
301
	if(!$res_crt) return false;
302

    
303
	// export our certificate data
304
	if (!openssl_pkey_export($res_key, $str_key) ||
305
	    !openssl_x509_export($res_crt, $str_crt))
306
		return false;
307

    
308
	// return our certificate information
309
	$cert['caref'] = $caref;
310
	$cert['crt'] = base64_encode($str_crt);
311
	$cert['prv'] = base64_encode($str_key);
312
	$cert['type'] = $type;
313

    
314
	return true;
315
}
316

    
317
function csr_generate(& $cert, $keylen, $dn, $digest_alg = "sha256") {
318

    
319
	$args = array(
320
		"x509_extensions" => "v3_req",
321
		"digest_alg" => $digest_alg,
322
		"private_key_bits" => (int)$keylen,
323
		"private_key_type" => OPENSSL_KEYTYPE_RSA,
324
		"encrypt_key" => false);
325

    
326
	// generate a new key pair
327
	$res_key = openssl_pkey_new($args);
328
	if(!$res_key) return false;
329

    
330
	// generate a certificate signing request
331
	$res_csr = openssl_csr_new($dn, $res_key, $args);
332
	if(!$res_csr) return false;
333

    
334
	// export our request data
335
	if (!openssl_pkey_export($res_key, $str_key) ||
336
	    !openssl_csr_export($res_csr, $str_csr))
337
		return false;
338

    
339
	// return our request information
340
	$cert['csr'] = base64_encode($str_csr);
341
	$cert['prv'] = base64_encode($str_key);
342

    
343
	return true;
344
}
345

    
346
function csr_complete(& $cert, $str_crt) {
347

    
348
	// return our request information
349
	$cert['crt'] = base64_encode($str_crt);
350
	unset($cert['csr']);
351

    
352
	return true;
353
}
354

    
355
function csr_get_subject($str_crt, $decode = true) {
356

    
357
	if ($decode)
358
		$str_crt = base64_decode($str_crt);
359

    
360
	$components = openssl_csr_get_subject($str_crt);
361

    
362
	if (empty($components) || !is_array($components))
363
		return "unknown";
364

    
365
	ksort($components);
366
	foreach ($components as $a => $v) {
367
		if (!strlen($subject))
368
			$subject = "{$a}={$v}";
369
		else
370
			$subject = "{$a}={$v}, {$subject}";
371
	}
372

    
373
	return $subject;
374
}
375

    
376
function cert_get_subject($str_crt, $decode = true) {
377

    
378
	if ($decode)
379
		$str_crt = base64_decode($str_crt);
380

    
381
	$inf_crt = openssl_x509_parse($str_crt);
382
	$components = $inf_crt['subject'];
383

    
384
	if (empty($components) || !is_array($components))
385
		return "unknown";
386

    
387
	ksort($components);
388
	foreach ($components as $a => $v) {
389
		if (is_array($v)) {
390
			ksort($v);
391
			foreach ($v as $w) {
392
				$asubject = "{$a}={$w}";
393
				$subject = (strlen($subject)) ? "{$asubject}, {$subject}" : $asubject;
394
			}
395
		} else {
396
			$asubject = "{$a}={$v}";
397
			$subject = (strlen($subject)) ? "{$asubject}, {$subject}" : $asubject;
398
		}
399
	}
400

    
401
	return $subject;
402
}
403

    
404
function cert_get_subject_array($crt) {
405
	$str_crt = base64_decode($crt);
406
	$inf_crt = openssl_x509_parse($str_crt);
407
	$components = $inf_crt['subject'];
408

    
409
	if (!is_array($components))
410
		return;
411

    
412
	$subject_array = array();
413

    
414
	foreach($components as $a => $v)
415
		$subject_array[] = array('a' => $a, 'v' => $v);
416

    
417
	return $subject_array;
418
}
419

    
420
function cert_get_subject_hash($crt) {
421
	$str_crt = base64_decode($crt);
422
	$inf_crt = openssl_x509_parse($str_crt);
423
	return $inf_crt['subject'];
424
}
425

    
426
function cert_get_issuer($str_crt, $decode = true) {
427

    
428
	if ($decode)
429
		$str_crt = base64_decode($str_crt);
430

    
431
	$inf_crt = openssl_x509_parse($str_crt);
432
	$components = $inf_crt['issuer'];
433
	
434
	if (empty($components) || !is_array($components))
435
		return "unknown";
436

    
437
	ksort($components);
438
	foreach ($components as $a => $v) {
439
		if (!strlen($issuer))
440
			$issuer = "{$a}={$v}";
441
		else
442
			$issuer = "{$a}={$v}, {$issuer}";
443
	}
444

    
445
	return $issuer;
446
}
447

    
448
/* this function works on x509 (crt), rsa key (prv), and req(csr) */
449
function cert_get_modulus($str_crt, $decode = true, $type = "crt"){
450
	if ($decode)
451
		$str_crt = base64_decode($str_crt);
452

    
453
	$modulus = "";
454
	if ( in_array($type, array("crt", "prv", "csr")) ) {
455
			$type = str_replace( array("crt","prv","csr"), array("x509","rsa","req"), $type);
456
			$modulus = exec("echo \"{$str_crt}\" | openssl {$type} -noout -modulus");
457
	}
458
	return $modulus;
459
}
460
function csr_get_modulus($str_crt, $decode = true){
461
	return cert_get_modulus($str_crt, $decode, "csr");
462
}
463

    
464
function cert_get_purpose($str_crt, $decode = true) {
465
	if ($decode)
466
		$str_crt = base64_decode($str_crt);
467
	$crt_details = openssl_x509_parse($str_crt);
468
	$purpose = array();
469
	$purpose['ca'] = (stristr($crt_details['extensions']['basicConstraints'], 'CA:TRUE') === false) ? 'No': 'Yes';
470
	$purpose['server'] = ($crt_details['extensions']['nsCertType'] == "SSL Server") ? 'Yes': 'No';
471
	return $purpose;
472
}
473

    
474
function cert_get_dates($str_crt, $decode = true) {
475
	if ($decode)
476
		$str_crt = base64_decode($str_crt);
477
	$crt_details = openssl_x509_parse($str_crt);
478
	if ($crt_details['validFrom_time_t'] > 0)
479
		$start = date('r', $crt_details['validFrom_time_t']);
480
	if ($crt_details['validTo_time_t'] > 0)
481
		$end = date('r', $crt_details['validTo_time_t']);
482
	return array($start, $end);
483
}
484

    
485
function prv_get_modulus($str_crt, $decode = true){
486
	return cert_get_modulus($str_crt, $decode, "prv");
487
}
488

    
489
function is_user_cert($certref) {
490
	global $config;
491
	if (!is_array($config['system']['user']))
492
		return;
493
	foreach ($config['system']['user'] as $user) {
494
		if (!is_array($user['cert']))
495
			continue;
496
		foreach ($user['cert'] as $cert) {
497
			if ($certref == $cert)
498
				return true;
499
		}
500
	}
501
	return false;
502
}
503

    
504
function is_openvpn_server_cert($certref) {
505
	global $config;
506
	if (!is_array($config['openvpn']['openvpn-server']))
507
		return;
508
	foreach ($config['openvpn']['openvpn-server'] as $ovpns) {
509
		if ($ovpns['certref'] == $certref)
510
			return true;
511
	}
512
	return false;
513
}
514

    
515
function is_openvpn_client_cert($certref) {
516
	global $config;
517
	if (!is_array($config['openvpn']['openvpn-client']))
518
		return;
519
	foreach ($config['openvpn']['openvpn-client'] as $ovpnc) {
520
		if ($ovpnc['certref'] == $certref)
521
			return true;
522
	}
523
	return false;
524
}
525

    
526
function is_ipsec_cert($certref) {
527
	global $config;
528
	if (!is_array($config['ipsec']['phase1']))
529
		return;
530
	foreach ($config['ipsec']['phase1'] as $ipsec) {
531
		if ($ipsec['certref'] == $certref)
532
			return true;
533
	}
534
	return false;
535
}
536

    
537
function is_webgui_cert($certref) {
538
	global $config;
539
	if (($config['system']['webgui']['ssl-certref'] == $certref)
540
		&& ($config['system']['webgui']['protocol'] != "http"))
541
		return true;
542
}
543

    
544
function is_captiveportal_cert($certref) {
545
	global $config;
546
	if (!is_array($config['captiveportal']))
547
		return;
548
	foreach ($config['captiveportal'] as $portal) {
549
		if (isset($portal['enable']) && isset($portal['httpslogin']) && ($portal['certref'] == $certref))
550
			return true;
551
	}
552
	return false;
553
}
554

    
555
function cert_in_use($certref) {
556
	return (is_webgui_cert($certref) ||
557
		is_user_cert($certref) ||
558
		is_openvpn_server_cert($certref) ||
559
		is_openvpn_client_cert($certref) ||
560
		is_ipsec_cert($certref) ||
561
		is_captiveportal_cert($certref));
562
}
563

    
564
/*
565
CRL code is a *WORK IN PROGRESS* do not try to use these functions yet.
566

    
567
OpenSSL CRL status code constants.
568
OCSP_REVOKED_STATUS_NOSTATUS
569
OCSP_REVOKED_STATUS_UNSPECIFIED
570
OCSP_REVOKED_STATUS_KEYCOMPROMISE
571
OCSP_REVOKED_STATUS_CACOMPROMISE
572
OCSP_REVOKED_STATUS_AFFILIATIONCHANGED
573
OCSP_REVOKED_STATUS_SUPERSEDED
574
OCSP_REVOKED_STATUS_CESSATIONOFOPERATION
575
OCSP_REVOKED_STATUS_CERTIFICATEHOLD
576
OCSP_REVOKED_STATUS_REMOVEFROMCRL
577
*/
578

    
579
$openssl_crl_status = array(
580
	OCSP_REVOKED_STATUS_NOSTATUS              => "No Status (default)",
581
	OCSP_REVOKED_STATUS_UNSPECIFIED           => "Unspecified",
582
	OCSP_REVOKED_STATUS_KEYCOMPROMISE         => "Key Compromise",
583
	OCSP_REVOKED_STATUS_CACOMPROMISE          => "CA Compromise",
584
	OCSP_REVOKED_STATUS_AFFILIATIONCHANGED    => "Affiliation Changed",
585
	OCSP_REVOKED_STATUS_SUPERSEDED            => "Superseded",
586
	OCSP_REVOKED_STATUS_CESSATIONOFOPERATION  => "Cessation of Operation",
587
	OCSP_REVOKED_STATUS_CERTIFICATEHOLD       => "Certificate Hold"
588
);
589

    
590
function crl_create(& $crl, $caref, $name, $serial=0, $lifetime=9999) {
591
	global $config;
592
	$ca =& lookup_ca($caref);
593
	if (!$ca)
594
		return false;
595
	$crl['descr'] = $name;
596
	$crl['caref'] = $caref;
597
	$crl['serial'] = $serial;
598
	$crl['lifetime'] = $lifetime;
599
	$crl['cert'] = array();
600
	$crl_res = crl_update($crl);
601
	$config['crl'][] = $crl;
602
	return $crl_res;
603
}
604

    
605
function crl_update(& $crl) {
606
	global $config;
607
	$ca =& lookup_ca($crl['caref']);
608
	if (!$ca)
609
		return false;
610
	// If we have text but no certs, it was imported and cannot be updated.
611
	if (($crl["method"] != "internal") && (!empty($crl['text']) && empty($crl['cert'])))
612
		return false;
613
	$crl['serial']++;
614
	$ca_str_crt = base64_decode($ca['crt']);
615
	$ca_str_key = base64_decode($ca['prv']);
616
	$crl_res = openssl_crl_new($ca_str_crt, $crl['serial'], $crl['lifetime']);
617
	if (is_array($crl['cert']) && (count($crl['cert']) > 0)) {
618
		foreach ($crl['cert'] as $cert) {
619
			openssl_crl_revoke_cert($crl_res, base64_decode($cert["crt"]), $cert["revoke_time"], $cert["reason"]);
620
		}
621
	}
622
	openssl_crl_export($crl_res, $crl_text, $ca_str_key);
623
	$crl['text'] = base64_encode($crl_text);
624
	return $crl_res;
625
}
626

    
627
function cert_revoke($cert, & $crl, $reason=OCSP_REVOKED_STATUS_UNSPECIFIED) {
628
	global $config;
629
	if (is_cert_revoked($cert, $crl['refid']))
630
		return true;
631
	// If we have text but no certs, it was imported and cannot be updated.
632
	if (!is_crl_internal($crl))
633
		return false;
634
	$cert["reason"] = $reason;
635
	$cert["revoke_time"] = time();
636
	$crl["cert"][] = $cert;
637
	crl_update($crl);
638
	return true;
639
}
640

    
641
function cert_unrevoke($cert, & $crl) {
642
	global $config;
643
	if (!is_crl_internal($crl))
644
		return false;
645
	foreach ($crl['cert'] as $id => $rcert) {
646
		if (($rcert['refid'] == $cert['refid']) || ($rcert['descr'] == $cert['descr'])) {
647
			unset($crl['cert'][$id]);
648
			if (count($crl['cert']) == 0) {
649
				// Protect against accidentally switching the type to imported, for older CRLs
650
				if (!isset($crl['method']))
651
					$crl['method'] = "internal";
652
				crl_update($crl);
653
			} else
654
				crl_update($crl);
655
			return true;
656
		}
657
	}
658
	return false;
659
}
660

    
661
function is_cert_revoked($cert, $crlref = "") {
662
	global $config;
663
	if (!is_array($config['crl']))
664
		return false;
665

    
666
	if (!empty($crlref)) {
667
		$crl = lookup_crl($crlref);
668
		if (!is_array($crl['cert']))
669
			return false;
670
		foreach ($crl['cert'] as $rcert) {
671
			if (($rcert['refid'] == $cert['refid']) || ($rcert['descr'] == $cert['descr']))
672
				return true;
673
		}
674
	} else {
675
		foreach ($config['crl'] as $crl) {
676
			if (!is_array($crl['cert']))
677
				continue;
678
			foreach ($crl['cert'] as $rcert) {
679
				if (($rcert['refid'] == $cert['refid']) || ($rcert['descr'] == $cert['descr']))
680
					return true;
681
			}
682
		}
683
	}
684
	return false;
685
}
686

    
687
function is_openvpn_server_crl($crlref) {
688
	global $config;
689
	if (!is_array($config['openvpn']['openvpn-server']))
690
		return;
691
	foreach ($config['openvpn']['openvpn-server'] as $ovpns) {
692
		if (!empty($ovpns['crlref']) && ($ovpns['crlref'] == $crlref))
693
			return true;
694
	}
695
	return false;
696
}
697

    
698
// Keep this general to allow for future expansion. See cert_in_use() above.
699
function crl_in_use($crlref) {
700
	return (is_openvpn_server_crl($crlref));
701
}
702

    
703
function is_crl_internal($crl) {
704
	return (!(!empty($crl['text']) && empty($crl['cert'])) || ($crl["method"] == "internal"));
705
}
706

    
707
function cert_get_cn($crt, $isref = false) {
708
	/* If this is a certref, not an actual cert, look up the cert first */
709
	if ($isref) {
710
		$cert = lookup_cert($crt);
711
		/* If it's not a valid cert, bail. */
712
		if (!(is_array($cert) && !empty($cert['crt'])))
713
			return "";
714
		$cert = $cert['crt'];
715
	} else {
716
		$cert = $crt;
717
	}
718
	$sub = cert_get_subject_array($cert);
719
	if (is_array($sub)) {
720
		foreach ($sub as $s) {
721
			if (strtoupper($s['a']) == "CN")
722
				return $s['v'];
723
		}
724
	}
725
	return "";
726
}
727

    
728
?>
(9-9/66)