1
|
<?php
|
2
|
/*
|
3
|
* crypt.inc
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2008-2018 Rubicon Communications, LLC (Netgate)
|
7
|
* Copyright (c) 2008 Shrew Soft Inc. All rights reserved.
|
8
|
* All rights reserved.
|
9
|
*
|
10
|
* originally part of m0n0wall (http://m0n0.ch/wall)
|
11
|
* Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
12
|
* All rights reserved.
|
13
|
*
|
14
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
15
|
* you may not use this file except in compliance with the License.
|
16
|
* You may obtain a copy of the License at
|
17
|
*
|
18
|
* http://www.apache.org/licenses/LICENSE-2.0
|
19
|
*
|
20
|
* Unless required by applicable law or agreed to in writing, software
|
21
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
22
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
23
|
* See the License for the specific language governing permissions and
|
24
|
* limitations under the License.
|
25
|
*/
|
26
|
|
27
|
function crypt_data($val, $pass, $opt) {
|
28
|
$file = tempnam("/tmp", "php-encrypt");
|
29
|
file_put_contents("{$file}.dec", $val);
|
30
|
exec("/usr/bin/openssl enc {$opt} -aes-256-cbc -in {$file}.dec -out {$file}.enc -k " . escapeshellarg($pass));
|
31
|
if (file_exists("{$file}.enc")) {
|
32
|
$result = file_get_contents("{$file}.enc");
|
33
|
} else {
|
34
|
$result = "";
|
35
|
log_error(gettext("Failed to encrypt/decrypt data!"));
|
36
|
}
|
37
|
@unlink($file);
|
38
|
@unlink("{$file}.dec");
|
39
|
@unlink("{$file}.enc");
|
40
|
return $result;
|
41
|
}
|
42
|
|
43
|
function encrypt_data(& $data, $pass) {
|
44
|
return base64_encode(crypt_data($data, $pass, "-e"));
|
45
|
}
|
46
|
|
47
|
function decrypt_data(& $data, $pass) {
|
48
|
return crypt_data(base64_decode($data), $pass, "-d");
|
49
|
}
|
50
|
|
51
|
function tagfile_reformat($in, & $out, $tag) {
|
52
|
|
53
|
$out = "---- BEGIN {$tag} ----\n";
|
54
|
|
55
|
$size = 80;
|
56
|
$oset = 0;
|
57
|
while ($size >= 64) {
|
58
|
$line = substr($in, $oset, 64);
|
59
|
$out .= $line."\n";
|
60
|
$size = strlen($line);
|
61
|
$oset += $size;
|
62
|
}
|
63
|
|
64
|
$out .= "---- END {$tag} ----\n";
|
65
|
|
66
|
return true;
|
67
|
}
|
68
|
|
69
|
function tagfile_deformat($in, & $out, $tag) {
|
70
|
|
71
|
$btag_val = "---- BEGIN {$tag} ----";
|
72
|
$etag_val = "---- END {$tag} ----";
|
73
|
|
74
|
$btag_len = strlen($btag_val);
|
75
|
$etag_len = strlen($etag_val);
|
76
|
|
77
|
$btag_pos = stripos($in, $btag_val);
|
78
|
$etag_pos = stripos($in, $etag_val);
|
79
|
|
80
|
if (($btag_pos === false) || ($etag_pos === false)) {
|
81
|
return false;
|
82
|
}
|
83
|
|
84
|
$body_pos = $btag_pos + $btag_len;
|
85
|
$body_len = strlen($in);
|
86
|
$body_len -= $btag_len;
|
87
|
$body_len -= $etag_len + 1;
|
88
|
|
89
|
$out = substr($in, $body_pos, $body_len);
|
90
|
|
91
|
return true;
|
92
|
}
|
93
|
|
94
|
?>
|