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
|
function & lookup_ca($refid) {
|
35
|
global $config;
|
36
|
|
37
|
if (is_array($config['system']['ca']))
|
38
|
foreach ($config['system']['ca'] as & $ca)
|
39
|
if ($ca['refid'] == $refid)
|
40
|
return $ca;
|
41
|
|
42
|
return false;
|
43
|
}
|
44
|
|
45
|
function & lookup_ca_by_subject($subject) {
|
46
|
global $config;
|
47
|
|
48
|
if (is_array($config['system']['ca']))
|
49
|
foreach ($config['system']['ca'] as & $ca)
|
50
|
{
|
51
|
$ca_subject = cert_get_subject($ca['crt']);
|
52
|
if ($ca_subject == $subject)
|
53
|
return $ca;
|
54
|
}
|
55
|
|
56
|
return false;
|
57
|
}
|
58
|
|
59
|
function & lookup_cert($refid) {
|
60
|
global $config;
|
61
|
|
62
|
if (is_array($config['system']['cert']))
|
63
|
foreach ($config['system']['cert'] as & $cert)
|
64
|
if ($cert['refid'] == $refid)
|
65
|
return $cert;
|
66
|
|
67
|
return false;
|
68
|
}
|
69
|
|
70
|
function ca_chain_array(& $cert) {
|
71
|
if($cert['caref']) {
|
72
|
$chain = array();
|
73
|
$crt =& lookup_ca($cert['caref']);
|
74
|
$chain[] = $crt;
|
75
|
while ($crt) {
|
76
|
$caref = $crt['caref'];
|
77
|
if($caref)
|
78
|
$crt =& lookup_ca($caref);
|
79
|
else
|
80
|
$crt = false;
|
81
|
if($crt)
|
82
|
$chain[] = $crt;
|
83
|
}
|
84
|
return $chain;
|
85
|
}
|
86
|
return false;
|
87
|
}
|
88
|
|
89
|
function ca_chain(& $cert) {
|
90
|
if($cert['caref']) {
|
91
|
$ca = "";
|
92
|
$cas = ca_chain_array($cert);
|
93
|
if (is_array($cas))
|
94
|
foreach ($cas as & $ca_cert)
|
95
|
{
|
96
|
$ca .= base64_decode($ca_cert['crt']);
|
97
|
$ca .= "\n";
|
98
|
}
|
99
|
return $ca;
|
100
|
}
|
101
|
return "";
|
102
|
}
|
103
|
|
104
|
function ca_import(& $ca, $str) {
|
105
|
global $config;
|
106
|
|
107
|
$ca['crt'] = base64_encode($str);
|
108
|
|
109
|
$subject = cert_get_subject($str, false);
|
110
|
$issuer = cert_get_issuer($str, false);
|
111
|
|
112
|
// Find my issuer unless self-signed
|
113
|
if($issuer <> $subject) {
|
114
|
$issuer_crt =& lookup_ca_by_subject($issuer);
|
115
|
if($issuer_crt)
|
116
|
$ca['caref'] = $issuer_crt['refid'];
|
117
|
}
|
118
|
|
119
|
/* Correct if child certificate was loaded first */
|
120
|
if (is_array($config['system']['ca']))
|
121
|
foreach ($config['system']['ca'] as & $oca)
|
122
|
{
|
123
|
$issuer = cert_get_issuer($oca['crt']);
|
124
|
if($ca['refid']<>$oca['refid'] && $issuer==$subject)
|
125
|
$oca['caref'] = $ca['refid'];
|
126
|
}
|
127
|
if (is_array($config['system']['cert']))
|
128
|
foreach ($config['system']['cert'] as & $cert)
|
129
|
{
|
130
|
$issuer = cert_get_issuer($cert['crt']);
|
131
|
if($issuer==$subject)
|
132
|
$cert['caref'] = $ca['refid'];
|
133
|
}
|
134
|
return true;
|
135
|
}
|
136
|
|
137
|
function ca_create(& $ca, $keylen, $lifetime, $dn) {
|
138
|
|
139
|
$args = array(
|
140
|
"digest_alg" => "sha1",
|
141
|
"private_key_bits" => (int)$keylen,
|
142
|
"private_key_type" => OPENSSL_KEYTYPE_RSA,
|
143
|
"encrypt_key" => false);
|
144
|
|
145
|
// generate a new key pair
|
146
|
$res_key = openssl_pkey_new();
|
147
|
|
148
|
// generate a certificate signing request
|
149
|
$res_csr = openssl_csr_new($dn, $res_key, $args);
|
150
|
|
151
|
// self sign the certificate
|
152
|
$res_crt = openssl_csr_sign($res_csr, null, $res_key, $lifetime, $args);
|
153
|
|
154
|
// export our certificate data
|
155
|
openssl_pkey_export($res_key, $str_key);
|
156
|
openssl_x509_export($res_crt, $str_crt);
|
157
|
|
158
|
// return our ca information
|
159
|
$ca['crt'] = base64_encode($str_crt);
|
160
|
$ca['prv'] = base64_encode($str_key);
|
161
|
$ca['serial'] = 0;
|
162
|
|
163
|
return true;
|
164
|
}
|
165
|
|
166
|
function cert_import(& $cert, $crt_str, $key_str) {
|
167
|
|
168
|
$cert['crt'] = base64_encode($crt_str);
|
169
|
$cert['prv'] = base64_encode($key_str);
|
170
|
|
171
|
$subject = cert_get_subject($crt_str, false);
|
172
|
$issuer = cert_get_issuer($crt_str, false);
|
173
|
|
174
|
// Find my issuer unless self-signed
|
175
|
if($issuer <> $subject) {
|
176
|
$issuer_crt =& lookup_ca_by_subject($issuer);
|
177
|
if($issuer_crt)
|
178
|
$cert['caref'] = $issuer_crt['refid'];
|
179
|
}
|
180
|
return true;
|
181
|
}
|
182
|
|
183
|
function cert_create(& $cert, $caref, $keylen, $lifetime, $dn) {
|
184
|
|
185
|
$ca =& lookup_ca($caref);
|
186
|
if (!$ca)
|
187
|
return false;
|
188
|
|
189
|
$ca_str_crt = base64_decode($ca['crt']);
|
190
|
$ca_str_key = base64_decode($ca['prv']);
|
191
|
$ca_res_crt = openssl_x509_read($ca_str_crt);
|
192
|
$ca_res_key = openssl_pkey_get_private(array(0 => $ca_str_key, 1 => ""));
|
193
|
$ca_serial = ++$ca['serial'];
|
194
|
|
195
|
$args = array(
|
196
|
"digest_alg" => "sha1",
|
197
|
"private_key_bits" => (int)$keylen,
|
198
|
"private_key_type" => OPENSSL_KEYTYPE_RSA,
|
199
|
"encrypt_key" => false);
|
200
|
|
201
|
// generate a new key pair
|
202
|
$res_key = openssl_pkey_new();
|
203
|
|
204
|
// generate a certificate signing request
|
205
|
$res_csr = openssl_csr_new($dn, $res_key, $args);
|
206
|
|
207
|
// self sign the certificate
|
208
|
$res_crt = openssl_csr_sign($res_csr, $ca_res_crt, $ca_res_key, $lifetime,
|
209
|
$args, $ca_serial);
|
210
|
|
211
|
// export our certificate data
|
212
|
openssl_pkey_export($res_key, $str_key);
|
213
|
openssl_x509_export($res_crt, $str_crt);
|
214
|
|
215
|
// return our certificate information
|
216
|
$cert['caref'] = $caref;
|
217
|
$cert['crt'] = base64_encode($str_crt);
|
218
|
$cert['prv'] = base64_encode($str_key);
|
219
|
|
220
|
return true;
|
221
|
}
|
222
|
|
223
|
function csr_generate(& $cert, $keylen, $dn) {
|
224
|
|
225
|
$args = array(
|
226
|
"digest_alg" => "sha1",
|
227
|
"private_key_bits" => (int)$keylen,
|
228
|
"private_key_type" => OPENSSL_KEYTYPE_RSA,
|
229
|
"encrypt_key" => false);
|
230
|
|
231
|
// generate a new key pair
|
232
|
$res_key = openssl_pkey_new();
|
233
|
|
234
|
// generate a certificate signing request
|
235
|
$res_csr = openssl_csr_new($dn, $res_key, $args);
|
236
|
|
237
|
// export our request data
|
238
|
openssl_pkey_export($res_key, $str_key);
|
239
|
openssl_csr_export($res_csr, $str_csr);
|
240
|
|
241
|
// return our request information
|
242
|
$cert['csr'] = base64_encode($str_csr);
|
243
|
$cert['prv'] = base64_encode($str_key);
|
244
|
|
245
|
return true;
|
246
|
}
|
247
|
|
248
|
function csr_complete(& $cert, $str_crt) {
|
249
|
|
250
|
// return our request information
|
251
|
$cert['crt'] = base64_encode($str_crt);
|
252
|
unset($cert['csr']);
|
253
|
|
254
|
return true;
|
255
|
}
|
256
|
|
257
|
function csr_get_subject($str_crt, $decode = true) {
|
258
|
|
259
|
if ($decode)
|
260
|
$str_crt = base64_decode($str_crt);
|
261
|
|
262
|
$components = openssl_csr_get_subject($str_crt);
|
263
|
|
264
|
if (!is_array($components))
|
265
|
return "unknown";
|
266
|
|
267
|
foreach ($components as $a => $v) {
|
268
|
if (!strlen($subject))
|
269
|
$subject = "{$a}={$v}";
|
270
|
else
|
271
|
$subject = "{$a}={$v}, {$subject}";
|
272
|
}
|
273
|
|
274
|
return $subject;
|
275
|
}
|
276
|
|
277
|
function cert_get_subject($str_crt, $decode = true) {
|
278
|
|
279
|
if ($decode)
|
280
|
$str_crt = base64_decode($str_crt);
|
281
|
|
282
|
$inf_crt = openssl_x509_parse($str_crt);
|
283
|
$components = $inf_crt['subject'];
|
284
|
|
285
|
if (!is_array($components))
|
286
|
return "unknown";
|
287
|
|
288
|
foreach ($components as $a => $v) {
|
289
|
if (!strlen($subject))
|
290
|
$subject = "{$a}={$v}";
|
291
|
else
|
292
|
$subject = "{$a}={$v}, {$subject}";
|
293
|
}
|
294
|
|
295
|
return $subject;
|
296
|
}
|
297
|
|
298
|
function cert_get_subject_array($crt) {
|
299
|
$str_crt = base64_decode($crt);
|
300
|
$inf_crt = openssl_x509_parse($str_crt);
|
301
|
$components = $inf_crt['subject'];
|
302
|
$subject_array = array();
|
303
|
|
304
|
foreach($components as $a => $v)
|
305
|
$subject_array[] = array('a' => $a, 'v' => $v);
|
306
|
|
307
|
return $subject_array;
|
308
|
}
|
309
|
|
310
|
function cert_get_subject_hash($crt) {
|
311
|
$str_crt = base64_decode($crt);
|
312
|
$inf_crt = openssl_x509_parse($str_crt);
|
313
|
return $inf_crt['subject'];
|
314
|
}
|
315
|
|
316
|
function cert_get_issuer($str_crt, $decode = true) {
|
317
|
|
318
|
if ($decode)
|
319
|
$str_crt = base64_decode($str_crt);
|
320
|
|
321
|
$inf_crt = openssl_x509_parse($str_crt);
|
322
|
$components = $inf_crt['issuer'];
|
323
|
|
324
|
if (!is_array($components))
|
325
|
return "unknown";
|
326
|
foreach ($components as $a => $v) {
|
327
|
if (!strlen($issuer))
|
328
|
$issuer = "{$a}={$v}";
|
329
|
else
|
330
|
$issuer = "{$a}={$v}, {$issuer}";
|
331
|
}
|
332
|
|
333
|
return $issuer;
|
334
|
}
|
335
|
|
336
|
?>
|