-
Notifications
You must be signed in to change notification settings - Fork 31
Merge changes from teonsystems #8
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ccae4d7
0cb1fcf
936f49a
ec3a0a2
d6ca39b
d037aff
19fd3be
ae3272e
1da327d
4348d25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,27 @@ | ||
php-dkim | ||
======== | ||
# PHP DKIM validator | ||
|
||
**Finally, a PHP5 class for not just signing, but _verifying_ DKIM signatures.** | ||
|
||
Requirements | ||
------------ | ||
Currently this package requires PHP 5.1.2 or greater (or PECL `hash` >= 1.1), which provides the `hash()` function. | ||
|
||
Also required, at least one of the following present alongside your PHP installation. | ||
## Installation | ||
|
||
* [openssl](http://us1.php.net/manual/en/openssl.installation.php) | ||
* [phpseclib](http://phpseclib.sourceforge.net/) | ||
``` | ||
composer require teon/dkim | ||
``` | ||
|
||
At least one of those packages must be present in order to compute the RSA signature verification. | ||
|
||
Usage | ||
----- | ||
<pending> | ||
|
||
## Usage | ||
|
||
Changelog | ||
--------- | ||
```php | ||
$msgContent = "..."; | ||
$dkimValidator = new \Teon\DKIM\Validator($msgContent); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Amend the namespace |
||
if (!$dkimValidator->validateBoolean()) { | ||
throw new Exception("DKIM validation FAILED!"); | ||
} | ||
``` | ||
|
||
**v0.02** | ||
_5:36 PM 1/2/2013_ | ||
|
||
* Splitting TODOs into separate file. | ||
* Finally got the header hash to match my expected value, based on debugging output from Mail::DKIM::Validate. | ||
* Removed var_dump() calls | ||
* Still doesn't verify signatures properly - not sure where to go from here. | ||
|
||
**v0.01** | ||
_10:55 AM 12/31/2012_ | ||
Initial commit. Most of the structure is in place, and the body hashes are validating, but I haven't been able to get the signature validation correct just yet. I must have some whitespace issue or some random public key problem. | ||
# Changelog | ||
|
||
See git history :) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,27 @@ | ||
{ | ||
"name": "angrychimp/php-dkim", | ||
"description": "Finally, a PHP5 class for not just signing, but _verifying_ DKIM signatures.", | ||
"name": "teon/dkim", | ||
"description": "Update of angrychimp/php-dkim package.", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "angrychimp", | ||
"email": "rk@angrychimp.net" | ||
}, | ||
{ | ||
"name": "Teon d.o.o. - Bostjan Skufca", | ||
"email": "bostjan@teon.si" | ||
} | ||
], | ||
"require": { | ||
"phpseclib/phpseclib": ">=0.3.6" | ||
"ext-openssl": "*", | ||
"ext-hash": "*" | ||
}, | ||
"suggest": { | ||
"angrychimp/php-dkim": "Original, psr-0 compatible php-dkim package" | ||
}, | ||
"autoload": { | ||
"psr-0": { | ||
"DKIM": "./" | ||
} | ||
"psr-4": { | ||
"Teon\\DKIM\\": "src/Teon/DKIM/" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,12 @@ | ||
<?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); | ||
namespace Teon\DKIM; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. amend namespace |
||
|
||
abstract class DKIM { | ||
|
||
|
||
abstract class AbstractDKIM { | ||
|
||
/** | ||
* | ||
|
@@ -38,13 +31,13 @@ abstract class DKIM { | |
* | ||
* @param string $rawMessage | ||
* @return DKIM | ||
* @throws DKIM_Exception | ||
* @throws Exception | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd really prefer to avoid throwing generic exceptions, since the code could be wrapped in libraries with multiple exceptions, and an precise exception type is very useful |
||
*/ | ||
public function __construct($rawMessage='', $params=array()) { | ||
|
||
$this->_raw = $rawMessage; | ||
if (!$this->_raw) { | ||
throw new DKIM_Exception('No message content provided'); | ||
throw new Exception('No message content provided'); | ||
} | ||
|
||
$this->_params = $params; | ||
|
@@ -61,12 +54,12 @@ public function __construct($rawMessage='', $params=array()) { | |
* @param array $headers | ||
* @param string $style | ||
* @return string | ||
* @throws DKIM_Exception | ||
* @throws 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"); | ||
throw new Exception("Attempted to canonicalize empty header array"); | ||
} | ||
|
||
$cHeader = ''; | ||
|
@@ -106,7 +99,7 @@ protected function _canonicalizeHeader($headers=array(), $style="simple") { | |
* @param string $style | ||
* @param int $length | ||
* @return string | ||
* @throws DKIM_Exception | ||
* @throws Exception | ||
*/ | ||
protected function _canonicalizeBody($style='simple', $length=-1) { | ||
|
||
|
@@ -204,10 +197,15 @@ protected function _getBodyFromRaw($style='string') { | |
return (string)$this->_params['body']; | ||
} | ||
|
||
$lines = explode("\r\n", $this->_raw); | ||
// Do not explode by \r\n, rather do it by \n and strip \r from line endif if it is found - see comment below | ||
$lines = explode("\n", $this->_raw); | ||
// Jump past all the headers | ||
$on = false; | ||
while ($line = array_shift($lines)) { | ||
// Remove trailing carriage-return if present | ||
// It might be present if emails are read from Unix maildirs directly instead of via IMAP/POP3 | ||
$line = preg_replace('/\r$/', '', $line); | ||
|
||
if ($on === true && $line != '') { | ||
break; | ||
} | ||
|
@@ -225,19 +223,8 @@ protected function _getBodyFromRaw($style='string') { | |
* | ||
*/ | ||
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)); | ||
} | ||
|
||
} | ||
} | ||
|
||
return base64_encode(hash($method, $body, true)); | ||
|
||
class DKIM_Exception extends Exception { } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
|
||
|
||
namespace Teon\DKIM; | ||
|
||
|
||
|
||
use \Exception as ParentException; | ||
|
||
|
||
|
||
class Exception | ||
extends ParentException | ||
{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
namespace needs to be amended for the PR