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