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