1 |
8ff5ffcc
|
Matthew Grooms
|
<?php
|
2 |
5025a56c
|
Scott Ullrich
|
|
3 |
8ff5ffcc
|
Matthew Grooms
|
/* $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 |
523855b0
|
Scott Ullrich
|
pfSense_BUILDER_BINARIES: /usr/bin/openssl
|
30 |
|
|
pfSense_MODULE: crypto
|
31 |
|
|
|
32 |
8ff5ffcc
|
Matthew Grooms
|
DISABLE_PHP_LINT_CHECKING
|
33 |
|
|
*/
|
34 |
|
|
|
35 |
|
|
function crypt_data(& $data, $pass, $opt) {
|
36 |
|
|
|
37 |
|
|
$pspec = "/usr/bin/openssl enc {$opt} -aes-256-cbc -k {$pass}";
|
38 |
|
|
$dspec = array( 0 => array("pipe", "r"),
|
39 |
|
|
1 => array("pipe", "w"),
|
40 |
|
|
2 => array("pipe", "e"));
|
41 |
|
|
|
42 |
|
|
$fp = proc_open($pspec, $dspec, $pipes);
|
43 |
|
|
if (!$fp)
|
44 |
|
|
return false;
|
45 |
|
|
|
46 |
|
|
fwrite($pipes[0], $data);
|
47 |
|
|
fclose($pipes[0]);
|
48 |
|
|
|
49 |
|
|
$rslt = stream_get_contents($pipes[1]);
|
50 |
|
|
fclose($pipes[1]);
|
51 |
|
|
|
52 |
|
|
proc_close($fp);
|
53 |
|
|
|
54 |
|
|
return $rslt;
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
function encrypt_data(& $data, $pass) {
|
58 |
|
|
return base64_encode(crypt_data($data, $pass, "-e"));
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
function decrypt_data(& $data, $pass) {
|
62 |
|
|
return crypt_data(base64_decode($data), $pass, "-d");
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
function tagfile_reformat($in, & $out, $tag) {
|
66 |
|
|
|
67 |
|
|
$out = "---- BEGIN {$tag} ----\n";
|
68 |
|
|
|
69 |
|
|
$size = 80;
|
70 |
|
|
$oset = 0;
|
71 |
|
|
while ($size >= 64) {
|
72 |
|
|
$line = substr($in, $oset, 64);
|
73 |
|
|
$out .= $line."\n";
|
74 |
|
|
$size = strlen($line);
|
75 |
|
|
$oset += $size;
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
$out .= "---- END {$tag} ----\n";
|
79 |
|
|
|
80 |
|
|
return true;
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
function tagfile_deformat($in, & $out, $tag) {
|
84 |
|
|
|
85 |
|
|
$btag_val = "---- BEGIN {$tag} ----";
|
86 |
|
|
$etag_val = "---- END {$tag} ----";
|
87 |
|
|
|
88 |
|
|
$btag_len = strlen($btag_val);
|
89 |
|
|
$etag_len = strlen($etag_val);
|
90 |
|
|
|
91 |
|
|
$btag_pos = stripos($in, $btag_val);
|
92 |
|
|
$etag_pos = stripos($in, $etag_val);
|
93 |
|
|
|
94 |
|
|
if (($btag_pos === false) || ($etag_pos === false))
|
95 |
|
|
return false;
|
96 |
|
|
|
97 |
|
|
$body_pos = $btag_pos + $btag_len;
|
98 |
|
|
$body_len = strlen($in);
|
99 |
|
|
$body_len -= strlen($btag_len);
|
100 |
|
|
$body_len -= strlen($etag_len);
|
101 |
|
|
|
102 |
|
|
$out = substr($in, $body_pos, $body_len);
|
103 |
|
|
|
104 |
|
|
return true;
|
105 |
|
|
}
|
106 |
5025a56c
|
Scott Ullrich
|
|
107 |
|
|
?>
|