1
|
<?php
|
2
|
/*
|
3
|
* crypt.inc
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2008-2013 BSD Perimeter
|
7
|
* Copyright (c) 2013-2016 Electric Sheep Fencing
|
8
|
* Copyright (c) 2014-2022 Rubicon Communications, LLC (Netgate)
|
9
|
* Copyright (c) 2008 Shrew Soft Inc. All rights reserved.
|
10
|
* All rights reserved.
|
11
|
*
|
12
|
* originally part of m0n0wall (http://m0n0.ch/wall)
|
13
|
* Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
14
|
* All rights reserved.
|
15
|
*
|
16
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
17
|
* you may not use this file except in compliance with the License.
|
18
|
* You may obtain a copy of the License at
|
19
|
*
|
20
|
* http://www.apache.org/licenses/LICENSE-2.0
|
21
|
*
|
22
|
* Unless required by applicable law or agreed to in writing, software
|
23
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
24
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
25
|
* See the License for the specific language governing permissions and
|
26
|
* limitations under the License.
|
27
|
*/
|
28
|
|
29
|
define('PFS_OPENSSL_DEFAULT_ITERATIONS', '500000');
|
30
|
|
31
|
function crypt_cleanup($file) {
|
32
|
unlink_if_exists($file);
|
33
|
unlink_if_exists("{$file}.dec");
|
34
|
unlink_if_exists("{$file}.enc");
|
35
|
}
|
36
|
|
37
|
function crypt_data($val, $pass, $opt, $legacy = false, $iterations = PFS_OPENSSL_DEFAULT_ITERATIONS) {
|
38
|
$file = tempnam("/tmp", "php-encrypt");
|
39
|
/* Ensure the files do not already exist */
|
40
|
|
41
|
crypt_cleanup($file);
|
42
|
file_put_contents("{$file}.dec", $val);
|
43
|
|
44
|
/* Use PBKDF2 Key Derivation (https://en.wikipedia.org/wiki/PBKDF2)
|
45
|
* unless we need to read old data encrypted without it. */
|
46
|
$keyder = ($legacy) ? "" : "-pbkdf2";
|
47
|
$md = ($legacy) ? "md5" : "sha256";
|
48
|
$iter = ($legacy) ? '' : ' -iter ' . escapeshellarg($iterations);
|
49
|
|
50
|
$output = "";
|
51
|
$exitcode = "";
|
52
|
exec("/usr/bin/openssl enc {$opt} -aes-256-cbc -in {$file}.dec -out {$file}.enc -pass pass:" . escapeshellarg($pass) . " -salt -md ${md} {$keyder} {$iter} 2> /dev/null", $output, $exitcode);
|
53
|
|
54
|
if (($exitcode == 0) && file_exists("{$file}.enc") && (filesize("{$file}.enc") > 0)) {
|
55
|
$result = file_get_contents("{$file}.enc");
|
56
|
} elseif (($opt == "-d") && ($legacy === false) && ($iterations == PFS_OPENSSL_DEFAULT_ITERATIONS)) {
|
57
|
/* If it failed with the current default iterations,
|
58
|
* next try with previous default number of iterations. */
|
59
|
crypt_cleanup($file);
|
60
|
$result = crypt_data($val, $pass, $opt, false, '10000');
|
61
|
} elseif (($opt == "-d") && ($legacy === false)) {
|
62
|
/* Operation failed without new options, try old. */
|
63
|
crypt_cleanup($file);
|
64
|
$result = crypt_data($val, $pass, $opt, true);
|
65
|
} else {
|
66
|
$result = "";
|
67
|
log_error(gettext("Failed to encrypt/decrypt data!"));
|
68
|
}
|
69
|
|
70
|
/* Cleanup */
|
71
|
crypt_cleanup($file);
|
72
|
return $result;
|
73
|
}
|
74
|
|
75
|
function encrypt_data(& $data, $pass, $legacy = false) {
|
76
|
return base64_encode(crypt_data($data, $pass, "-e", $legacy));
|
77
|
}
|
78
|
|
79
|
function decrypt_data(& $data, $pass, $legacy = false) {
|
80
|
return crypt_data(base64_decode($data), $pass, "-d", $legacy);
|
81
|
}
|
82
|
|
83
|
function tagfile_reformat($in, & $out, $tag) {
|
84
|
|
85
|
$out = "---- BEGIN {$tag} ----\n";
|
86
|
|
87
|
$size = 80;
|
88
|
$oset = 0;
|
89
|
while ($size >= 64) {
|
90
|
$line = substr($in, $oset, 64);
|
91
|
$out .= $line."\n";
|
92
|
$size = strlen($line);
|
93
|
$oset += $size;
|
94
|
}
|
95
|
|
96
|
$out .= "---- END {$tag} ----\n";
|
97
|
|
98
|
return true;
|
99
|
}
|
100
|
|
101
|
function tagfile_deformat($in, & $out, $tag) {
|
102
|
|
103
|
$btag_val = "---- BEGIN {$tag} ----";
|
104
|
$etag_val = "---- END {$tag} ----";
|
105
|
|
106
|
$btag_len = strlen($btag_val);
|
107
|
$etag_len = strlen($etag_val);
|
108
|
|
109
|
$btag_pos = stripos($in, $btag_val);
|
110
|
$etag_pos = stripos($in, $etag_val);
|
111
|
|
112
|
if (($btag_pos === false) || ($etag_pos === false)) {
|
113
|
return false;
|
114
|
}
|
115
|
|
116
|
$body_pos = $btag_pos + $btag_len;
|
117
|
$body_len = strlen($in);
|
118
|
$body_len -= $btag_len;
|
119
|
$body_len -= $etag_len + 1;
|
120
|
|
121
|
$out = substr($in, $body_pos, $body_len);
|
122
|
|
123
|
return true;
|
124
|
}
|
125
|
|
126
|
?>
|