-
Notifications
You must be signed in to change notification settings - Fork 2
/
CryptoParser.php
274 lines (243 loc) · 9.35 KB
/
CryptoParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
* CryptoParser allows to do the following operations:
* - generate a header (containing keys' parameters and an encrypted key)
* - fetch a header from message
* - fetch a header and an encrypted message from an ascii-armored cryptogram
*
* PHP version 5
*
* @category Encryption
* @package PageProtectionPlus
* @author Pawel Wilk <pw@gnu.org>
* @copyright 2006, 2007 Pawel Wilk
* @license http://www.gnu.org/licenses/gpl.html General Public License version 2 or higher
* @version 2.3b
* @link http://www.mediawiki.org/wiki/Extension:PPP
*/
define ('BEGIN_KEY_PREFIX', '--- BEGIN KEY:');
define ('BEGIN_KEY_SUFFIX', '---');
define ('END_KEY', '--- END KEY ---');
define ('HEADER_CORRUPTED', 0);
define ('HEADER_NOT_FOUND', 1);
define ('HEADER_NATIVE', 2);
define ('PARAMETER_SEPARATOR', ',');
/**
* Trims white characters and removes separator in the given variable.
*/
function trim_param(&$value)
{
$value = trim( strtr ($value, array(PARAMETER_SEPARATOR=>'') ) );
}
/**
* Makes all characters lower-case in the given variable.
*/
function tolower_value(&$value)
{
$value = strtolower($value);
}
/**
* Provides headers parsing functions.
*/
class CryptoParser {
public $mTextpartCipherName;
public $mTextpartCipherMode;
public $mTextpartKeyData;
public $mTextpartArmoredKeyData;
public $mTextpartKeySize;
public $mTextpartIVSize;
public $mKeypartCipherName;
public $mKeypartCipherKeyID;
public $mHeaderType;
public $mHeadLine;
public $mHeader;
public $mText;
public $mArmoredText;
protected $mRemoveWhite;
protected $mRemoveNotB64;
/**
* Initializes parser.
*/
public function CryptoParser()
{
$this->mTextpartCipherName = false;
$this->mTextpartCipherMode = false;
$this->mTextpartKeyData = false;
$this->mTextpartArmoredKeyData = false;
$this->mTextpartKeySize = false;
$this->mTextpartIVSize = false;
$this->mKeypartCipherName = false;
$this->mKeypartCipherKeyID = false;
$this->mHeadLine = false;
$this->mParameters = false;
$this->mText = false;
$this->mArmoredText = false;
$this->mHeader = false;
$this->mHeaderType = HEADER_NOT_FOUND;
$this->mRemoveWhite = array("\r" => '',
"\t" => '',
" " => '',
"\0" => '');
$this->mRemoveNotB64 = $this->mRemoveWhite;
$this->mRemoveNotB64["\n"] = '';
}
/**
* Husks header from an ascii-armored text and sets the corresponding members.
* @param Text text containing header.
* @return true on success, false on errors and sets mHeaderType member.
*/
public function LoadCryptogram($text)
{
$text = ltrim($text);
$from_headline = strpos($text, BEGIN_KEY_PREFIX);
if ($from_headline !== 0) {
$from_headline = strpos(ltrim($text), '--- BEGIN');
if ($from_headline === 0) {
$this->mHeaderType = HEADER_CORRUPTED;
throw new Exception("Header corrupted: cannot find a proper header in: |" .
substr($text, 0, 64) . "|");
return false;
}
$this->mHeaderType = HEADER_NOT_FOUND;
$this->mArmoredText = $text;
$this->mText = base64_decode(strtr($this->mArmoredText, $this->mRemoveNotB64));
$this->mTextpartArmoredKeyData = false;
$this->mTextpartKeyData = false;
return $this->mHeaderType;
}
$to_headline = strpos($text, BEGIN_KEY_SUFFIX, strlen(BEGIN_KEY_PREFIX)+4);
if ($to_headline === false) {
$this->mHeaderType = HEADER_CORRUPTED;
throw new Exception("Header corrupted: cannot find end of headline in: |" .
substr($text, 0, 64) . "|");
return false;
}
/* get parameters from headline */
$parstart = $from_headline + strlen(BEGIN_KEY_PREFIX);
$parameters = trim(substr($text, $parstart, ($to_headline-$parstart)));
if (strpos($parameters, "\n") !== false) {
$this->mHeaderType = HEADER_CORRUPTED;
throw new Exception("Header corrupted: cannot find key parameters in: |" .
trim(substr($text, $from_headline, ($to_headline-$from_headline))) . "|");
return false;
}
$this->mHeaderType = HEADER_NATIVE;
$this->mHeadLine = trim(substr($text, $from_headline, ($to_headline - $from_headline + strlen(BEGIN_KEY_SUFFIX))));
$parameters = strtolower($parameters);
$this->mParameters = explode(PARAMETER_SEPARATOR, $parameters);
if ( count($this->mParameters) !== 6 ) {
$this->mHeaderType = HEADER_CORRUPTED;
throw new Exception("Header corrupted: field count is " . count($this->mParameters) .
" but 7 was expected in: |" . $parameters . "|");
return false;
}
//array_walk($this->mParameters, 'trim_param');
list ( $this->mKeypartCipherName,
$this->mKeypartCipherKeyID,
$this->mTextpartCipherName,
$this->mTextpartCipherMode,
$this->mTextpartKeySize,
$this->mTextpartIVSize ) = $this->mParameters;
/* get an encrypted key+iv+salt */
$from_key = $to_headline + strlen(BEGIN_KEY_SUFFIX);
$to_key = strpos($text, END_KEY, $from_key);
if ($to_key === false) {
$this->mHeaderType = HEADER_CORRUPTED;
throw new Exception("Header corrupted: cannot find symmetric key in: |" .
substr($text, $from_key, 64) . "|");
return false;
}
$this->mTextpartArmoredKeyData = substr($text, $from_key, ($to_key-$from_key));
$this->mTextpartKeyData = strtr($this->mTextpartArmoredKeyData, $this->mRemoveNotB64);
$this->mTextpartKeyData = base64_decode($this->mTextpartKeyData);
/* get a message body */
if ($this->mTextpartCipherName === 'plain') {
$this->mText = substr($text, $to_key+strlen(END_KEY));
} else {
$this->mArmoredText = substr($text, $to_key+strlen(END_KEY));
$this->mText = base64_decode(strtr($this->mArmoredText, $this->mRemoveNotB64));
}
/* get a header */
$this->mHeader = trim(substr($text, 0, $to_key+strlen(END_KEY)));
return true;
}
/**
* Creates header for a given parameters and sets all corresponding members.
* @param TextpartKeyData ascii-armored, ENCRYPTED symmetric key (in the Base64 format)
* @param KeypartCipherName algorithm identifier used for key encryption
* @param KeypartCipherKeyID identifier of a key used to encrypt key
* @param TextpartCipherName algorithm identifier used for text encryption
* @param TextpartCipherMode mode identifier used for text encryption
* @param TextpartKeySize key size used for text encryption
* @param TextpartIVSize IV size used for text encryption
* @return returns header string on success or false on errors.
*/
public function CreateHeader()
{
$this->mText = false;
$this->mHeader = false;
if (func_num_args() !== 7) {
$this->mHeaderType = HEADER_NOT_FOUND;
throw new Exception("Wrong count of arguments when calling CreateHeader()");
return false;
}
/* fetch parameters */
$this->mParameters = func_get_args();
$this->mTextpartArmoredKeyData = array_shift($this->mParameters);
$this->mTextpartKeyData = strtr($this->mTextpartArmoredKeyData, $this->mRemoveNotB64);
$this->mTextpartKeyData = base64_decode($this->mTextpartArmoredKeyData);
array_walk($this->mParameters, 'trim_param');
array_walk($this->mParameters, 'tolower_value');
list ( $this->mKeypartCipherName,
$this->mKeypartCipherKeyID,
$this->mTextpartCipherName,
$this->mTextpartCipherMode,
$this->mTextpartKeySize,
$this->mTextpartIVSize ) = $this->mParameters;
/* create headline */
$parameters = $this->mParameters;
$parameters[0] = strtoupper($parameters[0]);
$parameters[2] = strtoupper($parameters[2]);
$parameters[3] = strtoupper($parameters[3]);
$this->mHeadLine = BEGIN_KEY_PREFIX . " " .
implode(PARAMETER_SEPARATOR, $parameters) .
" " . BEGIN_KEY_SUFFIX;
/* create header */
$this->mHeader = $this->mHeadLine . "\n\r" .
$this->mTextpartArmoredKeyData . "\n\r" .
END_KEY;
$this->mHeaderType = HEADER_NATIVE;
return $this->mHeader;
}
/**
* Creates header and text in the Base64 for a given parameters and sets all corresponding members.
* @param Text text to be encrypted (not encoded in any way)
* @param TextpartKeyData ENCRYPTED symmetric key[+iv+salt] (binary format)
* @param KeypartCipherName algorithm identifier used for key encryption
* @param KeypartCipherKeyID identifier of a key used to encrypt key
* @param TextpartCipherName algorithm identifier used for text encryption
* @param TextpartCipherMode mode identifier used for text encryption
* @param TextpartKeySize key size used for text encryption
* @param TextpartIVSize IV size used for text encryption
* @return returns ascii-armored text containing a header on success or false on errors.
*/
public function CreateCryptogram($text, $TextpartKeyData, $KeypartCipherName, $KeypartCipherKeyID,
$TextpartCipherName, $TextpartCipherMode, $TextpartKeySize,
$TextpartIVSize)
{
$TextpartArmoredKeyData = '';
if ($TextpartKeyData !== false && $TextpartKeyData !== '') {
$TextpartArmoredKeyData = trim(chunk_split(base64_encode($TextpartKeyData)));
}
$this->CreateHeader ($TextpartArmoredKeyData, $KeypartCipherName, $KeypartCipherKeyID,
$TextpartCipherName, $TextpartCipherMode, $TextpartKeySize,
$TextpartIVSize);
if ($this->mHeaderType !== HEADER_NATIVE) {
return false;
}
$this->mText = $text;
$this->mArmoredText = trim(chunk_split(base64_encode($text)));
return $this->mHeader . "\n\r" . $this->mArmoredText . "\n\r";
}
}
?>