Project

General

Profile

Download (17.9 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
function & lookup_ca($refid) {
38
	global $config;
39

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

    
45
	return false;
46
}
47

    
48
function & lookup_ca_by_subject($subject) {
49
	global $config;
50

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

    
59
	return false;
60
}
61

    
62
function & lookup_cert($refid) {
63
	global $config;
64

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

    
70
	return false;
71
}
72

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

    
81
function & lookup_crl($refid) {
82
	global $config;
83

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

    
89
	return false;
90
}
91

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

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

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

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

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

    
162
function ca_create(& $ca, $keylen, $lifetime, $dn) {
163

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

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

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

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

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

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

    
193
	return true;
194
}
195

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

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

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

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

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

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

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

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

    
236
	return true;
237
}
238

    
239
function cert_import(& $cert, $crt_str, $key_str) {
240

    
241
	$cert['crt'] = base64_encode($crt_str);
242
	$cert['prv'] = base64_encode($key_str);
243

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

    
256
function cert_create(& $cert, $caref, $keylen, $lifetime, $dn, $type="user") {
257

    
258
	$ca =& lookup_ca($caref);
259
	if (!$ca)
260
		return false;
261

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

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

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

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

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

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

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

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

    
312
	return true;
313
}
314

    
315
function csr_generate(& $cert, $keylen, $dn) {
316

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

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

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

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

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

    
341
	return true;
342
}
343

    
344
function csr_complete(& $cert, $str_crt) {
345

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

    
350
	return true;
351
}
352

    
353
function csr_get_subject($str_crt, $decode = true) {
354

    
355
	if ($decode)
356
		$str_crt = base64_decode($str_crt);
357

    
358
	$components = openssl_csr_get_subject($str_crt);
359

    
360
	if (empty($components) || !is_array($components))
361
		return "unknown";
362

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

    
371
	return $subject;
372
}
373

    
374
function cert_get_subject($str_crt, $decode = true) {
375

    
376
	if ($decode)
377
		$str_crt = base64_decode($str_crt);
378

    
379
	$inf_crt = openssl_x509_parse($str_crt);
380
	$components = $inf_crt['subject'];
381

    
382
	if (empty($components) || !is_array($components))
383
		return "unknown";
384

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

    
399
	return $subject;
400
}
401

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

    
407
	if (!is_array($components))
408
		return;
409

    
410
	$subject_array = array();
411

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

    
415
	return $subject_array;
416
}
417

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

    
424
function cert_get_issuer($str_crt, $decode = true) {
425

    
426
	if ($decode)
427
		$str_crt = base64_decode($str_crt);
428

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

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

    
443
	return $issuer;
444
}
445

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

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

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

    
472
function prv_get_modulus($str_crt, $decode = true){
473
	return cert_get_modulus($str_crt, $decode, "prv");
474
}
475

    
476
function is_user_cert($certref) {
477
	global $config;
478
	if (!is_array($config['system']['user']))
479
		return;
480
	foreach ($config['system']['user'] as $user) {
481
		if (!is_array($user['cert']))
482
			continue;
483
		foreach ($user['cert'] as $cert) {
484
			if ($certref == $cert)
485
				return true;
486
		}
487
	}
488
	return false;
489
}
490

    
491
function is_openvpn_server_cert($certref) {
492
	global $config;
493
	if (!is_array($config['openvpn']['openvpn-server']))
494
		return;
495
	foreach ($config['openvpn']['openvpn-server'] as $ovpns) {
496
		if ($ovpns['certref'] == $certref)
497
			return true;
498
	}
499
	return false;
500
}
501

    
502
function is_openvpn_client_cert($certref) {
503
	global $config;
504
	if (!is_array($config['openvpn']['openvpn-client']))
505
		return;
506
	foreach ($config['openvpn']['openvpn-client'] as $ovpnc) {
507
		if ($ovpnc['certref'] == $certref)
508
			return true;
509
	}
510
	return false;
511
}
512

    
513
function is_ipsec_cert($certref) {
514
	global $config;
515
	if (!is_array($config['ipsec']['phase1']))
516
		return;
517
	foreach ($config['ipsec']['phase1'] as $ipsec) {
518
		if ($ipsec['certref'] == $certref)
519
			return true;
520
	}
521
	return false;
522
}
523

    
524
function is_webgui_cert($certref) {
525
	global $config;
526
	if (($config['system']['webgui']['ssl-certref'] == $certref)
527
		&& ($config['system']['webgui']['protocol'] != "http"))
528
		return true;
529
}
530

    
531
function cert_in_use($certref) {
532
	return (is_webgui_cert($certref) ||
533
		is_user_cert($certref) ||
534
		is_openvpn_server_cert($certref) ||
535
		is_openvpn_client_cert($certref) ||
536
		is_ipsec_cert($certref));
537
}
538

    
539
/*
540
CRL code is a *WORK IN PROGRESS* do not try to use these functions yet.
541

    
542
OpenSSL CRL status code constants.
543
OCSP_REVOKED_STATUS_NOSTATUS
544
OCSP_REVOKED_STATUS_UNSPECIFIED
545
OCSP_REVOKED_STATUS_KEYCOMPROMISE
546
OCSP_REVOKED_STATUS_CACOMPROMISE
547
OCSP_REVOKED_STATUS_AFFILIATIONCHANGED
548
OCSP_REVOKED_STATUS_SUPERSEDED
549
OCSP_REVOKED_STATUS_CESSATIONOFOPERATION
550
OCSP_REVOKED_STATUS_CERTIFICATEHOLD
551
OCSP_REVOKED_STATUS_REMOVEFROMCRL
552
*/
553

    
554
$openssl_crl_status = array(
555
	OCSP_REVOKED_STATUS_NOSTATUS              => "No Status (default)",
556
	OCSP_REVOKED_STATUS_UNSPECIFIED           => "Unspecified",
557
	OCSP_REVOKED_STATUS_KEYCOMPROMISE         => "Key Compromise",
558
	OCSP_REVOKED_STATUS_CACOMPROMISE          => "CA Compromise",
559
	OCSP_REVOKED_STATUS_AFFILIATIONCHANGED    => "Affiliation Changed",
560
	OCSP_REVOKED_STATUS_SUPERSEDED            => "Superseded",
561
	OCSP_REVOKED_STATUS_CESSATIONOFOPERATION  => "Cessation of Operation",
562
	OCSP_REVOKED_STATUS_CERTIFICATEHOLD       => "Certificate Hold"
563
);
564

    
565
function crl_create(& $crl, $caref, $name, $serial=0, $lifetime=9999) {
566
	global $config;
567
	$ca =& lookup_ca($caref);
568
	if (!$ca)
569
		return false;
570
	$crl['descr'] = $name;
571
	$crl['caref'] = $caref;
572
	$crl['serial'] = $serial;
573
	$crl['lifetime'] = $lifetime;
574
	$crl['cert'] = array();
575
	$crl_res = crl_update($crl);
576
	$config['crl'][] = $crl;
577
	return $crl_res;
578
}
579

    
580
function crl_update(& $crl) {
581
	global $config;
582
	$ca =& lookup_ca($crl['caref']);
583
	if (!$ca)
584
		return false;
585
	// If we have text but no certs, it was imported and cannot be updated.
586
	if (($crl["method"] != "internal") && (!empty($crl['text']) && empty($crl['cert'])))
587
		return false;
588
	$crl['serial']++;
589
	$ca_str_crt = base64_decode($ca['crt']);
590
	$ca_str_key = base64_decode($ca['prv']);
591
	$crl_res = openssl_crl_new($ca_str_crt, $crl['serial'], $crl['lifetime']);
592
	if (is_array($crl['cert']) && (count($crl['cert']) > 0)) {
593
		foreach ($crl['cert'] as $cert) {
594
			openssl_crl_revoke_cert($crl_res, base64_decode($cert["crt"]), $cert["revoke_time"], $cert["reason"]);
595
		}
596
	}
597
	openssl_crl_export($crl_res, $crl_text, $ca_str_key);
598
	$crl['text'] = base64_encode($crl_text);
599
	return $crl_res;
600
}
601

    
602
function cert_revoke($cert, & $crl, $reason=OCSP_REVOKED_STATUS_UNSPECIFIED) {
603
	global $config;
604
	if (is_cert_revoked($cert, $crl['refid']))
605
		return true;
606
	// If we have text but no certs, it was imported and cannot be updated.
607
	if (!is_crl_internal($crl))
608
		return false;
609
	$cert["reason"] = $reason;
610
	$cert["revoke_time"] = time();
611
	$crl["cert"][] = $cert;
612
	crl_update($crl);
613
	return true;
614
}
615

    
616
function cert_unrevoke($cert, & $crl) {
617
	global $config;
618
	if (!is_crl_internal($crl))
619
		return false;
620
	foreach ($crl['cert'] as $id => $rcert) {
621
		if (($rcert['refid'] == $cert['refid']) || ($rcert['descr'] == $cert['descr'])) {
622
			unset($crl['cert'][$id]);
623
			if (count($crl['cert']) == 0) {
624
				// Protect against accidentally switching the type to imported, for older CRLs
625
				if (!isset($crl['method']))
626
					$crl['method'] = "internal";
627
				crl_update($crl);
628
			} else
629
				crl_update($crl);
630
			return true;
631
		}
632
	}
633
	return false;
634
}
635

    
636
function is_cert_revoked($cert, $crlref = "") {
637
	global $config;
638
	if (!is_array($config['crl']))
639
		return false;
640

    
641
	if (!empty($crlref)) {
642
		$crl = lookup_crl($crlref);
643
		if (!is_array($crl['cert']))
644
			return false;
645
		foreach ($crl['cert'] as $rcert) {
646
			if (($rcert['refid'] == $cert['refid']) || ($rcert['descr'] == $cert['descr']))
647
				return true;
648
		}
649
	} else {
650
		foreach ($config['crl'] as $crl) {
651
			if (!is_array($crl['cert']))
652
				continue;
653
			foreach ($crl['cert'] as $rcert) {
654
				if (($rcert['refid'] == $cert['refid']) || ($rcert['descr'] == $cert['descr']))
655
					return true;
656
			}
657
		}
658
	}
659
	return false;
660
}
661

    
662
function is_openvpn_server_crl($crlref) {
663
	global $config;
664
	if (!is_array($config['openvpn']['openvpn-server']))
665
		return;
666
	foreach ($config['openvpn']['openvpn-server'] as $ovpns) {
667
		if (!empty($ovpns['crlref']) && ($ovpns['crlref'] == $crlref))
668
			return true;
669
	}
670
	return false;
671
}
672

    
673
// Keep this general to allow for future expansion. See cert_in_use() above.
674
function crl_in_use($crlref) {
675
	return (is_openvpn_server_crl($crlref));
676
}
677

    
678
function is_crl_internal($crl) {
679
	return (!(!empty($crl['text']) && empty($crl['cert'])) || ($crl["method"] == "internal"));
680
}
681

    
682
function cert_get_cn($crt, $isref = false) {
683
	/* If this is a certref, not an actual cert, look up the cert first */
684
	if ($isref) {
685
		$cert = lookup_cert($crt);
686
		/* If it's not a valid cert, bail. */
687
		if (!(is_array($cert) && !empty($cert['crt'])))
688
			return "";
689
		$cert = $cert['crt'];
690
	} else {
691
		$cert = $crt;
692
	}
693
	$sub = cert_get_subject_array($cert);
694
	if (is_array($sub)) {
695
		foreach ($sub as $s) {
696
			if (strtoupper($s['a']) == "CN")
697
				return $s['v'];
698
		}
699
	}
700
	return "";
701
}
702

    
703
?>
(9-9/66)