Project

General

Profile

Download (4.07 KB) Statistics
| Branch: | Tag: | Revision:
1 8ff5ffcc Matthew Grooms
<?php
2
/*
3 8acd654a Renato Botelho
 * crypt.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 2a2396a6 Renato Botelho
 * Copyright (c) 2008-2016 Rubicon Communications, LLC (Netgate)
7 8acd654a Renato Botelho
 * Copyright (c) 2008 Shrew Soft Inc. All rights reserved.
8
 * All rights reserved.
9
 *
10
 * originally part of m0n0wall (http://m0n0.ch/wall)
11 aaec5634 Renato Botelho
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
12 8acd654a Renato Botelho
 * All rights reserved.
13
 *
14
 * Redistribution and use in source and binary forms, with or without
15
 * modification, are permitted provided that the following conditions are met:
16
 *
17
 * 1. Redistributions of source code must retain the above copyright notice,
18
 *    this list of conditions and the following disclaimer.
19
 *
20
 * 2. Redistributions in binary form must reproduce the above copyright
21
 *    notice, this list of conditions and the following disclaimer in
22
 *    the documentation and/or other materials provided with the
23
 *    distribution.
24
 *
25
 * 3. All advertising materials mentioning features or use of this software
26
 *    must display the following acknowledgment:
27
 *    "This product includes software developed by the pfSense Project
28
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
29
 *
30
 * 4. The names "pfSense" and "pfSense Project" must not be used to
31
 *    endorse or promote products derived from this software without
32
 *    prior written permission. For written permission, please contact
33
 *    coreteam@pfsense.org.
34
 *
35
 * 5. Products derived from this software may not be called "pfSense"
36
 *    nor may "pfSense" appear in their names without prior written
37
 *    permission of the Electric Sheep Fencing, LLC.
38
 *
39
 * 6. Redistributions of any form whatsoever must retain the following
40
 *    acknowledgment:
41
 *
42
 * "This product includes software developed by the pfSense Project
43
 * for use in the pfSense software distribution (http://www.pfsense.org/).
44
 *
45
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
46
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
49
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
56
 * OF THE POSSIBILITY OF SUCH DAMAGE.
57
 */
58 8ff5ffcc Matthew Grooms
59 e22eca36 jim-p
	function crypt_data($val, $pass, $opt) {
60
		$file = tempnam("/tmp", "php-encrypt");
61 3be0dff4 jim-p
		file_put_contents("{$file}.dec", $val);
62 2ec95f1f Renato Botelho
		exec("/usr/bin/openssl enc {$opt} -aes-256-cbc -in {$file}.dec -out {$file}.enc -k " . escapeshellarg($pass));
63 1e0b1727 Phil Davis
		if (file_exists("{$file}.enc")) {
64 15855fbc jim-p
			$result = file_get_contents("{$file}.enc");
65 1e0b1727 Phil Davis
		} else {
66 15855fbc jim-p
			$result = "";
67 d18f3f6e Phil Davis
			log_error(gettext("Failed to encrypt/decrypt data!"));
68 15855fbc jim-p
		}
69
		@unlink($file);
70
		@unlink("{$file}.dec");
71
		@unlink("{$file}.enc");
72 e22eca36 jim-p
		return $result;
73 8ff5ffcc Matthew Grooms
	}
74
75
	function encrypt_data(& $data, $pass) {
76
		return base64_encode(crypt_data($data, $pass, "-e"));
77
	}
78
79
	function decrypt_data(& $data, $pass) {
80
		return crypt_data(base64_decode($data), $pass, "-d");
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 1e0b1727 Phil Davis
		if (($btag_pos === false) || ($etag_pos === false)) {
113 8ff5ffcc Matthew Grooms
			return false;
114 1e0b1727 Phil Davis
		}
115 8ff5ffcc Matthew Grooms
116
		$body_pos = $btag_pos + $btag_len;
117
		$body_len = strlen($in);
118 f5bafe95 Ermal
		$body_len -= $btag_len;
119
		$body_len -= $etag_len + 1;
120 8ff5ffcc Matthew Grooms
121
		$out = substr($in, $body_pos, $body_len);
122
123
		return true;
124
	}
125 5025a56c Scott Ullrich
126 f5bafe95 Ermal
?>