Project

General

Profile

Download (7.89 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
		Copyright (C) 2008 Shrew Soft Inc
5
		All rights reserved.
6

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

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

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

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

    
28
		DISABLE_PHP_LINT_CHECKING
29
		pfSense_MODULE:	certificate_managaer
30
*/
31

    
32
require_once("functions.inc");
33

    
34
putenv("OPENSSL_CONF=/etc/ssl/openssl.cnf");
35

    
36
function & lookup_ca($refid) {
37
	global $config;
38

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

    
44
	return false;
45
}
46

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

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

    
58
	return false;
59
}
60

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

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

    
69
	return false;
70
}
71

    
72
function ca_chain_array(& $cert) {
73
	if($cert['caref']) {
74
		$chain = array();
75
		$crt =& lookup_ca($cert['caref']);
76
		$chain[] = $crt;
77
		while ($crt) {
78
			$caref = $crt['caref'];
79
			if($caref)
80
				$crt =& lookup_ca($caref);
81
			else
82
				$crt = false;
83
			if($crt)
84
				$chain[] = $crt;
85
		}
86
		return $chain;
87
	}
88
	return false;
89
}
90

    
91
function ca_chain(& $cert) {
92
	if($cert['caref']) {
93
		$ca = "";
94
		$cas = ca_chain_array($cert);
95
		if (is_array($cas))
96
			foreach ($cas as & $ca_cert)
97
			{
98
				$ca .= base64_decode($ca_cert['crt']);
99
				$ca .= "\n";
100
			}
101
		return $ca;
102
	}
103
	return "";
104
}
105

    
106
function ca_import(& $ca, $str) {
107
	global $config;
108

    
109
	$ca['crt'] = base64_encode($str);
110

    
111
	$subject = cert_get_subject($str, false);
112
	$issuer = cert_get_issuer($str, false);
113
	
114
	// Find my issuer unless self-signed
115
	if($issuer <> $subject) {
116
		$issuer_crt =& lookup_ca_by_subject($issuer);
117
		if($issuer_crt)
118
			$ca['caref'] = $issuer_crt['refid'];
119
	}
120

    
121
	/* Correct if child certificate was loaded first */
122
	if (is_array($config['system']['ca']))
123
		foreach ($config['system']['ca'] as & $oca)
124
		{
125
			$issuer = cert_get_issuer($oca['crt']);
126
			if($ca['refid']<>$oca['refid'] && $issuer==$subject)
127
				$oca['caref'] = $ca['refid'];
128
		}
129
	if (is_array($config['system']['cert']))
130
		foreach ($config['system']['cert'] as & $cert)
131
		{
132
			$issuer = cert_get_issuer($cert['crt']);
133
			if($issuer==$subject)
134
				$cert['caref'] = $ca['refid'];
135
		}
136
	return true;
137
}
138

    
139
function ca_create(& $ca, $keylen, $lifetime, $dn) {
140

    
141
	$args = array(
142
		"digest_alg" => "sha1",
143
		"private_key_bits" => (int)$keylen,
144
		"private_key_type" => OPENSSL_KEYTYPE_RSA,
145
		"encrypt_key" => false);
146

    
147
	// generate a new key pair
148
	$res_key = openssl_pkey_new();
149

    
150
	// generate a certificate signing request
151
	$res_csr = openssl_csr_new($dn, $res_key, $args);
152

    
153
	// self sign the certificate
154
	$res_crt = openssl_csr_sign($res_csr, null, $res_key, $lifetime, $args);
155

    
156
	// export our certificate data
157
	openssl_pkey_export($res_key, $str_key);
158
	openssl_x509_export($res_crt, $str_crt);
159

    
160
	// return our ca information
161
	$ca['crt'] = base64_encode($str_crt);
162
	$ca['prv'] = base64_encode($str_key);
163
	$ca['serial'] = 0;
164

    
165
	return true;
166
}
167

    
168
function cert_import(& $cert, $crt_str, $key_str) {
169

    
170
	$cert['crt'] = base64_encode($crt_str);
171
	$cert['prv'] = base64_encode($key_str);
172

    
173
	$subject = cert_get_subject($crt_str, false);
174
	$issuer = cert_get_issuer($crt_str, false);
175
	
176
	// Find my issuer unless self-signed
177
	if($issuer <> $subject) {
178
		$issuer_crt =& lookup_ca_by_subject($issuer);
179
		if($issuer_crt)
180
			$cert['caref'] = $issuer_crt['refid'];
181
	}
182
	return true;
183
}
184

    
185
function cert_create(& $cert, $caref, $keylen, $lifetime, $dn) {
186

    
187
	$ca =& lookup_ca($caref);
188
	if (!$ca)
189
		return false;
190

    
191
	$ca_str_crt = base64_decode($ca['crt']);
192
	$ca_str_key = base64_decode($ca['prv']);
193
	$ca_res_crt = openssl_x509_read($ca_str_crt);
194
	$ca_res_key = openssl_pkey_get_private($ca_str_key);
195
	$ca_serial = $ca['serial']++;
196

    
197
	$args = array(
198
		"digest_alg" => "sha1",
199
		"private_key_bits" => (int)$keylen,
200
		"private_key_type" => OPENSSL_KEYTYPE_RSA,
201
		"encrypt_key" => false);
202

    
203
	// generate a new key pair
204
	$res_key = openssl_pkey_new();
205

    
206
	// generate a certificate signing request
207
	$res_csr = openssl_csr_new($dn, $res_key, $args);
208

    
209
	// self sign the certificate
210
	$res_crt = openssl_csr_sign($res_csr, $ca_res_crt, $ca_res_key, $lifetime,
211
				 $args, $ca_serial);
212

    
213
	// export our certificate data
214
	openssl_pkey_export($res_key, $str_key);
215
	openssl_x509_export($res_crt, $str_crt);
216

    
217
	// return our certificate information
218
	$cert['caref'] = $caref;
219
	$cert['crt'] = base64_encode($str_crt);
220
	$cert['prv'] = base64_encode($str_key);
221

    
222
	return true;
223
}
224

    
225
function csr_generate(& $cert, $keylen, $dn) {
226

    
227
	$args = array(
228
		"digest_alg" => "sha1",
229
		"private_key_bits" => (int)$keylen,
230
		"private_key_type" => OPENSSL_KEYTYPE_RSA,
231
		"encrypt_key" => false);
232

    
233
	// generate a new key pair
234
	$res_key = openssl_pkey_new();
235

    
236
	// generate a certificate signing request
237
	$res_csr = openssl_csr_new($dn, $res_key, $args);
238

    
239
	// export our request data
240
	openssl_pkey_export($res_key, $str_key);
241
	openssl_csr_export($res_csr, $str_csr);
242

    
243
	// return our request information
244
	$cert['csr'] = base64_encode($str_csr);
245
	$cert['prv'] = base64_encode($str_key);
246

    
247
	return true;
248
}
249

    
250
function csr_complete(& $cert, $str_crt) {
251

    
252
	// return our request information
253
	$cert['crt'] = base64_encode($str_crt);
254
	unset($cert['csr']);
255

    
256
	return true;
257
}
258

    
259
function csr_get_subject($str_crt, $decode = true) {
260

    
261
	if ($decode)
262
		$str_crt = base64_decode($str_crt);
263

    
264
	$components = openssl_csr_get_subject($str_crt);
265

    
266
	if (!is_array($components))
267
		return "unknown";
268

    
269
	foreach ($components as $a => $v) {
270
		if (!strlen($subject))
271
			$subject = "{$a}={$v}";
272
		else
273
			$subject = "{$a}={$v}, {$subject}";
274
	}
275

    
276
	return $subject;
277
}
278

    
279
function cert_get_subject($str_crt, $decode = true) {
280

    
281
	if ($decode)
282
		$str_crt = base64_decode($str_crt);
283

    
284
	$inf_crt = openssl_x509_parse($str_crt);
285
	$components = $inf_crt['subject'];
286

    
287
	if (!is_array($components))
288
		return "unknown";
289

    
290
	foreach ($components as $a => $v) {
291
		if (!strlen($subject))
292
			$subject = "{$a}={$v}";
293
		else
294
			$subject = "{$a}={$v}, {$subject}";
295
	}
296

    
297
	return $subject;
298
}
299

    
300
function cert_get_subject_array($crt) {
301
	$str_crt = base64_decode($crt);
302
	$inf_crt = openssl_x509_parse($str_crt);
303
	$components = $inf_crt['subject'];
304
	$subject_array = array();
305

    
306
	foreach($components as $a => $v)
307
		$subject_array[] = array('a' => $a, 'v' => $v);
308

    
309
	return $subject_array;
310
}
311

    
312
function cert_get_issuer($str_crt, $decode = true) {
313

    
314
	if ($decode)
315
		$str_crt = base64_decode($str_crt);
316

    
317
	$inf_crt = openssl_x509_parse($str_crt);
318
	$components = $inf_crt['issuer'];
319
	
320
	if (!is_array($components))
321
		return "unknown";
322
	foreach ($components as $a => $v) {
323
		if (!strlen($issuer))
324
			$issuer = "{$a}={$v}";
325
		else
326
			$issuer = "{$a}={$v}, {$issuer}";
327
	}
328

    
329
	return $issuer;
330
}
331

    
332
?>
(7-7/50)