Project

General

Profile

Download (3.12 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * crypt.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2008-2019 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2008 Shrew Soft Inc. All rights reserved.
8
 * All rights reserved.
9
 *
10
 * originally part of m0n0wall (http://m0n0.ch/wall)
11
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
12
 * All rights reserved.
13
 *
14
 * 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
 *
18
 * http://www.apache.org/licenses/LICENSE-2.0
19
 *
20
 * 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
 */
26

    
27
	function crypt_data($val, $pass, $opt, $legacy = false) {
28
		$file = tempnam("/tmp", "php-encrypt");
29
		/* Ensure the files do not already exist */
30
		unlink_if_exists($file);
31
		unlink_if_exists("{$file}.dec");
32
		unlink_if_exists("{$file}.enc");
33

    
34
		file_put_contents("{$file}.dec", $val);
35

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

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

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

    
54
		/* Cleanup */
55
		unlink_if_exists($file);
56
		unlink_if_exists("{$file}.dec");
57
		unlink_if_exists("{$file}.enc");
58
		return $result;
59
	}
60

    
61
	function encrypt_data(& $data, $pass, $legacy = false) {
62
		return base64_encode(crypt_data($data, $pass, "-e", $legacy));
63
	}
64

    
65
	function decrypt_data(& $data, $pass, $legacy = false) {
66
		return crypt_data(base64_decode($data), $pass, "-d", $legacy);
67
	}
68

    
69
	function tagfile_reformat($in, & $out, $tag) {
70

    
71
		$out = "---- BEGIN {$tag} ----\n";
72

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

    
82
		$out .= "---- END {$tag} ----\n";
83

    
84
		return true;
85
	}
86

    
87
	function tagfile_deformat($in, & $out, $tag) {
88

    
89
		$btag_val = "---- BEGIN {$tag} ----";
90
		$etag_val = "---- END {$tag} ----";
91

    
92
		$btag_len = strlen($btag_val);
93
		$etag_len = strlen($etag_val);
94

    
95
		$btag_pos = stripos($in, $btag_val);
96
		$etag_pos = stripos($in, $etag_val);
97

    
98
		if (($btag_pos === false) || ($etag_pos === false)) {
99
			return false;
100
		}
101

    
102
		$body_pos = $btag_pos + $btag_len;
103
		$body_len = strlen($in);
104
		$body_len -= $btag_len;
105
		$body_len -= $etag_len + 1;
106

    
107
		$out = substr($in, $body_pos, $body_len);
108

    
109
		return true;
110
	}
111

    
112
?>
(14-14/59)