Project

General

Profile

Download (3.24 KB) Statistics
| Branch: | Tag: | Revision:
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-2020 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
	function crypt_data($val, $pass, $opt, $legacy = false) {
30
		$file = tempnam("/tmp", "php-encrypt");
31
		/* Ensure the files do not already exist */
32
		unlink_if_exists($file);
33
		unlink_if_exists("{$file}.dec");
34
		unlink_if_exists("{$file}.enc");
35

    
36
		file_put_contents("{$file}.dec", $val);
37

    
38
		/* Use PBKDF2 Key Derivation (https://en.wikipedia.org/wiki/PBKDF2)
39
		 *  unless we need to read old data encrypted without it. */
40
		$keyder = ($legacy) ? "" : "-pbkdf2";
41
		$md = ($legacy) ? "md5" : "sha256";
42

    
43
		$output = "";
44
		$exitcode = "";
45
		exec("/usr/bin/openssl enc {$opt} -aes-256-cbc -in {$file}.dec -out {$file}.enc -pass pass:" . escapeshellarg($pass) . " -salt -md ${md} {$keyder} 2> /dev/null", $output, $exitcode);
46

    
47
		if (($exitcode == 0) && file_exists("{$file}.enc") && (filesize("{$file}.enc") > 0)) {
48
			$result = file_get_contents("{$file}.enc");
49
		} elseif ($legacy === false) {
50
			/* Operation failed without new options, try old. */
51
			$result = crypt_data($val, $pass, $opt, true);
52
		} else {
53
			$result = "";
54
			log_error(gettext("Failed to encrypt/decrypt data!"));
55
		}
56

    
57
		/* Cleanup */
58
		unlink_if_exists($file);
59
		unlink_if_exists("{$file}.dec");
60
		unlink_if_exists("{$file}.enc");
61
		return $result;
62
	}
63

    
64
	function encrypt_data(& $data, $pass, $legacy = false) {
65
		return base64_encode(crypt_data($data, $pass, "-e", $legacy));
66
	}
67

    
68
	function decrypt_data(& $data, $pass, $legacy = false) {
69
		return crypt_data(base64_decode($data), $pass, "-d", $legacy);
70
	}
71

    
72
	function tagfile_reformat($in, & $out, $tag) {
73

    
74
		$out = "---- BEGIN {$tag} ----\n";
75

    
76
		$size = 80;
77
		$oset = 0;
78
		while ($size >= 64) {
79
			$line = substr($in, $oset, 64);
80
			$out .= $line."\n";
81
			$size = strlen($line);
82
			$oset += $size;
83
		}
84

    
85
		$out .= "---- END {$tag} ----\n";
86

    
87
		return true;
88
	}
89

    
90
	function tagfile_deformat($in, & $out, $tag) {
91

    
92
		$btag_val = "---- BEGIN {$tag} ----";
93
		$etag_val = "---- END {$tag} ----";
94

    
95
		$btag_len = strlen($btag_val);
96
		$etag_len = strlen($etag_val);
97

    
98
		$btag_pos = stripos($in, $btag_val);
99
		$etag_pos = stripos($in, $etag_val);
100

    
101
		if (($btag_pos === false) || ($etag_pos === false)) {
102
			return false;
103
		}
104

    
105
		$body_pos = $btag_pos + $btag_len;
106
		$body_len = strlen($in);
107
		$body_len -= $btag_len;
108
		$body_len -= $etag_len + 1;
109

    
110
		$out = substr($in, $body_pos, $body_len);
111

    
112
		return true;
113
	}
114

    
115
?>
(14-14/60)