1 |
8ff5ffcc
|
Matthew Grooms
|
<?php
|
2 |
|
|
/*
|
3 |
ac24dc24
|
Renato Botelho
|
* crypt.inc
|
4 |
|
|
*
|
5 |
|
|
* part of pfSense (https://www.pfsense.org)
|
6 |
81299b5c
|
Renato Botelho
|
* Copyright (c) 2008-2016 Rubicon Communications, LLC (Netgate)
|
7 |
ac24dc24
|
Renato Botelho
|
* Copyright (c) 2008 Shrew Soft Inc. All rights reserved.
|
8 |
|
|
* All rights reserved.
|
9 |
|
|
*
|
10 |
|
|
* originally part of m0n0wall (http://m0n0.ch/wall)
|
11 |
c5d81585
|
Renato Botelho
|
* Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
12 |
ac24dc24
|
Renato Botelho
|
* All rights reserved.
|
13 |
|
|
*
|
14 |
b12ea3fb
|
Renato Botelho
|
* 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 |
ac24dc24
|
Renato Botelho
|
*
|
18 |
b12ea3fb
|
Renato Botelho
|
* http://www.apache.org/licenses/LICENSE-2.0
|
19 |
ac24dc24
|
Renato Botelho
|
*
|
20 |
b12ea3fb
|
Renato Botelho
|
* 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 |
ac24dc24
|
Renato Botelho
|
*/
|
26 |
8ff5ffcc
|
Matthew Grooms
|
|
27 |
e22eca36
|
jim-p
|
function crypt_data($val, $pass, $opt) {
|
28 |
|
|
$file = tempnam("/tmp", "php-encrypt");
|
29 |
3be0dff4
|
jim-p
|
file_put_contents("{$file}.dec", $val);
|
30 |
2ec95f1f
|
Renato Botelho
|
exec("/usr/bin/openssl enc {$opt} -aes-256-cbc -in {$file}.dec -out {$file}.enc -k " . escapeshellarg($pass));
|
31 |
1e0b1727
|
Phil Davis
|
if (file_exists("{$file}.enc")) {
|
32 |
15855fbc
|
jim-p
|
$result = file_get_contents("{$file}.enc");
|
33 |
1e0b1727
|
Phil Davis
|
} else {
|
34 |
15855fbc
|
jim-p
|
$result = "";
|
35 |
d18f3f6e
|
Phil Davis
|
log_error(gettext("Failed to encrypt/decrypt data!"));
|
36 |
15855fbc
|
jim-p
|
}
|
37 |
|
|
@unlink($file);
|
38 |
|
|
@unlink("{$file}.dec");
|
39 |
|
|
@unlink("{$file}.enc");
|
40 |
e22eca36
|
jim-p
|
return $result;
|
41 |
8ff5ffcc
|
Matthew Grooms
|
}
|
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 |
1e0b1727
|
Phil Davis
|
if (($btag_pos === false) || ($etag_pos === false)) {
|
81 |
8ff5ffcc
|
Matthew Grooms
|
return false;
|
82 |
1e0b1727
|
Phil Davis
|
}
|
83 |
8ff5ffcc
|
Matthew Grooms
|
|
84 |
|
|
$body_pos = $btag_pos + $btag_len;
|
85 |
|
|
$body_len = strlen($in);
|
86 |
f5bafe95
|
Ermal
|
$body_len -= $btag_len;
|
87 |
|
|
$body_len -= $etag_len + 1;
|
88 |
8ff5ffcc
|
Matthew Grooms
|
|
89 |
|
|
$out = substr($in, $body_pos, $body_len);
|
90 |
|
|
|
91 |
|
|
return true;
|
92 |
|
|
}
|
93 |
5025a56c
|
Scott Ullrich
|
|
94 |
f5bafe95
|
Ermal
|
?>
|