Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to SHA2, use constant-time comparison #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions modules/cookie-session/Wicked
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ $config = array(
),
'name'=>'data',
'secret_key'=>__FILE__,
'digest_method'=>'md5',
'digest_method'=>'sha256',
'ttl'=>365*24*60*60,
'domain'=>'',
'path'=>'/',
);
);
21 changes: 18 additions & 3 deletions modules/cookie-session/classes/SessionMixin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ static function delete_all() {
unset($_COOKIE[self::$config['name']]);
}

protected static function _timingsafe_cmp($a, $b) {
$a_len = strlen($a);
$b_len = strlen($b);
if ($a_len !== $b_len) {
return FALSE;
}
$x = 0;
for ($i = 0; $i < $a_len; $i++) {
$x |= (ord($a[$i]) ^ ord($b[$i]));
}
return $x === 0;
}

protected static function _wipe_previous_cookie($cookie_name) {
$headers = headers_list();
header_remove();
Expand All @@ -110,8 +123,10 @@ protected static function _fetch_cookie_content() {
} else {
@list($digest, $cookie_json) = explode('|', $cookie, 2);
if (empty($digest) || empty($cookie_json) ||
$digest !== hash_hmac(self::$config['digest_method'], $cookie_json,
self::$config['secret_key'])) {
!self::_timingsafe_cmp($digest,
hash_hmac(self::$config['digest_method'],
$cookie_json,
self::$config['secret_key'])))) {
$cookie_content = new \stdClass();
} else {
$cookie_content = @json_decode($cookie_json);
Expand All @@ -122,4 +137,4 @@ protected static function _fetch_cookie_content() {
}
return $cookie_content;
}
}
}