1
|
<?php
|
2
|
|
3
|
/* $Id$ */
|
4
|
/*
|
5
|
Copyright (C) 2008 Shrew Soft Inc
|
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
|
pfSense_BUILDER_BINARIES: /usr/bin/openssl
|
30
|
pfSense_MODULE: crypto
|
31
|
|
32
|
DISABLE_PHP_LINT_CHECKING
|
33
|
*/
|
34
|
|
35
|
function crypt_data($val, $pass, $opt) {
|
36
|
$file = tempnam("/tmp", "php-encrypt");
|
37
|
file_put_contents("{$file}.dec", $val);
|
38
|
exec("/usr/bin/openssl enc {$opt} -aes-256-cbc -in {$file}.dec -out {$file}.enc -k " . escapeshellarg($pass));
|
39
|
if (file_exists("{$file}.enc"))
|
40
|
$result = file_get_contents("{$file}.enc");
|
41
|
else {
|
42
|
$result = "";
|
43
|
log_error("Failed to encrypt/decrypt data!");
|
44
|
}
|
45
|
@unlink($file);
|
46
|
@unlink("{$file}.dec");
|
47
|
@unlink("{$file}.enc");
|
48
|
return $result;
|
49
|
}
|
50
|
|
51
|
function encrypt_data(& $data, $pass) {
|
52
|
return base64_encode(crypt_data($data, $pass, "-e"));
|
53
|
}
|
54
|
|
55
|
function decrypt_data(& $data, $pass) {
|
56
|
return crypt_data(base64_decode($data), $pass, "-d");
|
57
|
}
|
58
|
|
59
|
function tagfile_reformat($in, & $out, $tag) {
|
60
|
|
61
|
$out = "---- BEGIN {$tag} ----\n";
|
62
|
|
63
|
$size = 80;
|
64
|
$oset = 0;
|
65
|
while ($size >= 64) {
|
66
|
$line = substr($in, $oset, 64);
|
67
|
$out .= $line."\n";
|
68
|
$size = strlen($line);
|
69
|
$oset += $size;
|
70
|
}
|
71
|
|
72
|
$out .= "---- END {$tag} ----\n";
|
73
|
|
74
|
return true;
|
75
|
}
|
76
|
|
77
|
function tagfile_deformat($in, & $out, $tag) {
|
78
|
|
79
|
$btag_val = "---- BEGIN {$tag} ----";
|
80
|
$etag_val = "---- END {$tag} ----";
|
81
|
|
82
|
$btag_len = strlen($btag_val);
|
83
|
$etag_len = strlen($etag_val);
|
84
|
|
85
|
$btag_pos = stripos($in, $btag_val);
|
86
|
$etag_pos = stripos($in, $etag_val);
|
87
|
|
88
|
if (($btag_pos === false) || ($etag_pos === false))
|
89
|
return false;
|
90
|
|
91
|
$body_pos = $btag_pos + $btag_len;
|
92
|
$body_len = strlen($in);
|
93
|
$body_len -= $btag_len;
|
94
|
$body_len -= $etag_len + 1;
|
95
|
|
96
|
$out = substr($in, $body_pos, $body_len);
|
97
|
|
98
|
return true;
|
99
|
}
|
100
|
|
101
|
?>
|