Project

General

Profile

Download (2.81 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
/* $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
	pfSense_BUILDER_BINARIES:	/usr/bin/openssl
30
	pfSense_MODULE:	crypto
31

    
32
*/
33

    
34
	function crypt_data($val, $pass, $opt) {
35
		$file = tempnam("/tmp", "php-encrypt");
36
		file_put_contents("{$file}.dec", $val);
37
		exec("/usr/bin/openssl enc {$opt} -aes-256-cbc -in {$file}.dec -out {$file}.enc -k " . escapeshellarg($pass));
38
		if (file_exists("{$file}.enc")) {
39
			$result = file_get_contents("{$file}.enc");
40
		} else {
41
			$result = "";
42
			log_error("Failed to encrypt/decrypt data!");
43
		}
44
		@unlink($file);
45
		@unlink("{$file}.dec");
46
		@unlink("{$file}.enc");
47
		return $result;
48
	}
49

    
50
	function encrypt_data(& $data, $pass) {
51
		return base64_encode(crypt_data($data, $pass, "-e"));
52
	}
53

    
54
	function decrypt_data(& $data, $pass) {
55
		return crypt_data(base64_decode($data), $pass, "-d");
56
	}
57

    
58
	function tagfile_reformat($in, & $out, $tag) {
59

    
60
		$out = "---- BEGIN {$tag} ----\n";
61

    
62
		$size = 80;
63
		$oset = 0;
64
		while ($size >= 64) {
65
			$line = substr($in, $oset, 64);
66
			$out .= $line."\n";
67
			$size = strlen($line);
68
			$oset += $size;
69
		}
70

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

    
73
		return true;
74
	}
75

    
76
	function tagfile_deformat($in, & $out, $tag) {
77

    
78
		$btag_val = "---- BEGIN {$tag} ----";
79
		$etag_val = "---- END {$tag} ----";
80

    
81
		$btag_len = strlen($btag_val);
82
		$etag_len = strlen($etag_val);
83

    
84
		$btag_pos = stripos($in, $btag_val);
85
		$etag_pos = stripos($in, $etag_val);
86

    
87
		if (($btag_pos === false) || ($etag_pos === false)) {
88
			return false;
89
		}
90

    
91
		$body_pos = $btag_pos + $btag_len;
92
		$body_len = strlen($in);
93
		$body_len -= $btag_len;
94
		$body_len -= $etag_len + 1;
95

    
96
		$out = substr($in, $body_pos, $body_len);
97

    
98
		return true;
99
	}
100

    
101
?>
(14-14/68)