-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathDKIM.php
234 lines (190 loc) · 6.37 KB
/
DKIM.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
<?php
/**
* @see phpseclib/Crypt/RSA
*/
// require_once 'phpseclib/Crypt/RSA.php';
/**
* @see phpseclib/Crypt/Hash
* @link http://phpseclib.sourceforge.net
*/
// require_once 'phpseclib/Crypt/Hash.php';
define('PHPSECLIB_USE_EXCEPTIONS', true);
abstract class DKIM {
/**
*
*
*/
protected $_raw;
/**
*
*
*/
protected $_message;
/**
*
*
*/
protected $_params;
/**
* Initializes required variables and creates/returns a DKIM object
*
* @param string $rawMessage
* @return DKIM
* @throws DKIM_Exception
*/
public function __construct($rawMessage='', $params=array()) {
$this->_raw = $rawMessage;
if (!$this->_raw) {
throw new DKIM_Exception('No message content provided');
}
$this->_params = $params;
// to-do: validate RFC-2822 compatible message string
return $this;
}
/**
* Canonicalizes a header in either "relaxed" or "simple" modes.
* Requires an array of headers (header names are part of array values)
*
* @param array $headers
* @param string $style
* @return string
* @throws DKIM_Exception
*/
protected function _canonicalizeHeader($headers=array(), $style="simple") {
$headers = (array)$headers;
if (sizeof($headers) == 0) {
throw new DKIM_Exception("Attempted to canonicalize empty header array");
}
$cHeader = '';
switch ($style) {
case 'simple':
$cHeader = implode("\r\n", $headers);
break;
case 'relaxed':
default:
$new = array();
foreach ($headers as $header) {
// split off header name
list($name, $val) = explode(':', $header, 2);
// lowercase field name
$name = trim(strtolower($name));
// unfold header values and reduce whitespace
$val = trim(preg_replace('/\s+/s', ' ', $val));
$new[] = "$name:$val";
}
$cHeader = implode("\r\n", $new);
break;
}
return $cHeader;
}
/**
* Canonicalizes a message body in either "relaxed" or "simple" modes.
* Requires a string containing all body content, with an optional byte-length
*
* @param string $body
* @param string $style
* @param int $length
* @return string
* @throws DKIM_Exception
*/
protected function _canonicalizeBody($style='simple', $length=-1) {
$cBody = $this->_getBodyFromRaw();
// trim leading whitespace
if ($cBody == '') {
return "\r\n";
}
// [DG]: mangle newlines
$cBody = str_replace("\r\n","\n",$cBody);
switch ($style) {
case 'relaxed':
default:
// http://tools.ietf.org/html/rfc4871#section-3.4.4
// strip whitespace off end of lines &
// replace whitespace strings with single whitespace
$cBody = preg_replace('/[ \t]+$/m', '', $cBody);
$cBody = preg_replace('/[ \t]+/m', ' ', $cBody);
// also perform rules for "simple" canonicalization
case 'simple':
// http://tools.ietf.org/html/rfc4871#section-3.4.3
// remove any trailing empty lines
$cBody = preg_replace('/\n+$/s', '', $cBody);
break;
}
$cBody = str_replace("\n","\r\n",$cBody);
// Add last trailing CRLF
$cBody .= "\r\n";
return ($length > 0) ? substr($cBody, 0, $length) : $cBody;
}
/**
*
*
*/
protected function _getHeaderFromRaw($headerKey, $style='array') {
$raw = (isset($this->_params['headers'])) ?
str_replace("\r", '', $this->_params['headers'])
: str_replace("\r", '', $this->_raw);
$lines = explode("\n", $raw);
$rawHeaders = array();
$headerVal = array();
$counter = 0;
$on = false;
foreach ($lines as $line) {
if ($on === true) {
if (preg_match('/^\w/', $line) !== 0 || trim($line) == '') {
// new header is starting or end of headers
$on = false;
switch ($style) {
case 'array':
default:
list($key, $val) = explode(':', implode("\r\n", $headerVal), 2);
$rawHeaders[$headerKey][$counter] = trim($val);
break;
case 'string':
$rawHeaders[$counter] = implode("\r\n", $headerVal);
break;
}
$headerVal = array();
$counter++;
} else {
$headerVal[] = $line;
}
}
if (stripos($line, $headerKey) === 0) {
$on = true;
$headerVal[] = $line;
}
if (trim($line) == '') {
break;
}
}
return $rawHeaders;
}
/**
*
*
*/
protected function _getBodyFromRaw($style='string') {
if (isset($this->_params['body'])) {
return (string)$this->_params['body'];
}
$raw = str_replace("\r\n", "\n", $this->_raw);
$body = substr($raw, strpos($raw, "\n\n") + 2);
return $body;
}
/**
*
*
*/
protected static function _hashBody($body, $method='sha1') {
// prefer to use phpseclib
// http://phpseclib.sourceforge.net
if (class_exists('Crypt_Hash')) {
$hash = new Crypt_Hash($method);
return base64_encode($hash->hash($body));
} else {
// try standard PHP hash function
return base64_encode(hash($method, $body, true));
}
}
}
class DKIM_Exception extends Exception { }