Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
84 changes: 84 additions & 0 deletions src/ChecksumList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Drupal\Signify;

class ChecksumList implements \Countable, \Iterator {

// Allowed checksum list verification algorithms and their base64-encoded lengths.
protected $HASH_ALGO_BASE64_LENGTHS = array('SHA256' => 64, 'SHA512' => 128);

protected $checksums = array();

protected $position = 0;

public function __construct($checksum_list_raw, $list_is_trusted)
{
$lines = explode("\n", $checksum_list_raw);
foreach ($lines as $line) {
if (trim($line) == '') {
continue;
}

if (substr($line, 0, 1) === '\\') {
throw new VerifierException('Filenames with problematic characters are not yet supported.');
}

$algo = substr($line, 0, strpos($line, ' '));
if (empty($this->HASH_ALGO_BASE64_LENGTHS[$algo])) {
throw new VerifierException("Algorithm \"$algo\" is unsupported for checksum verification.");
}

$filename_start = strpos($line, '(') + 1;
$bytes_after_filename = $this->HASH_ALGO_BASE64_LENGTHS[$algo] + 4;
$filename = substr($line, $filename_start, -$bytes_after_filename);

$verified_checksum = new VerifierFileChecksum($filename, $algo, substr($line, -$this->HASH_ALGO_BASE64_LENGTHS[$algo]), $list_is_trusted);
$this->checksums[] = $verified_checksum;
}

return $this->checksums;
}

/**
* @inheritDoc
*/
public function current() {
return $this->checksums[$this->position];
}

/**
* @inheritDoc
*/
public function next() {
$this->position += 1;
}

/**
* @inheritDoc
*/
public function key() {
return $this->position;
}

/**
* @inheritDoc
*/
public function valid() {
return isset($this->checksums[$this->position]);
}

/**
* @inheritDoc
*/
public function rewind() {
$this->position = 0;
}

/**
* @inheritDoc
*/
public function count() {
return iterator_count($this);
}

}
34 changes: 1 addition & 33 deletions src/Verifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ class Verifier
const COMMENTHDRLEN = 19;
const COMMENTMAXLEN = 1024;

// Allowed checksum list verification algorithms and their base64-encoded lengths.
protected $HASH_ALGO_BASE64_LENGTHS = array('SHA256' => 64, 'SHA512' => 128);

/**
* @var string
*/
Expand Down Expand Up @@ -173,7 +170,7 @@ public function verifyChecksumList($signed_checksum_list, $working_directory)
}

protected function verifyTrustedChecksumList($checksum_list_raw, $working_directory) {
$checksum_list = $this->parseChecksumList($checksum_list_raw, true);
$checksum_list = new ChecksumList($checksum_list_raw, true);
$verified_count = 0;

/**
Expand Down Expand Up @@ -227,35 +224,6 @@ public function verifyChecksumFile($checksum_file) {
return $this->verifyChecksumList($signed_checksum_list, $working_directory);
}

protected function parseChecksumList($checksum_list_raw, $list_is_trusted)
{
$lines = explode("\n", $checksum_list_raw);
$verified_checksums = array();
foreach ($lines as $line) {
if (trim($line) == '') {
continue;
}

if (substr($line, 0, 1) === '\\') {
throw new VerifierException('Filenames with problematic characters are not yet supported.');
}

$algo = substr($line, 0, strpos($line, ' '));
if (empty($this->HASH_ALGO_BASE64_LENGTHS[$algo])) {
throw new VerifierException("Algorithm \"$algo\" is unsupported for checksum verification.");
}

$filename_start = strpos($line, '(') + 1;
$bytes_after_filename = $this->HASH_ALGO_BASE64_LENGTHS[$algo] + 4;
$filename = substr($line, $filename_start, -$bytes_after_filename);

$verified_checksum = new VerifierFileChecksum($filename, $algo, substr($line, -$this->HASH_ALGO_BASE64_LENGTHS[$algo]), $list_is_trusted);
$verified_checksums[] = $verified_checksum;
}

return $verified_checksums;
}

/**
* Verify a string message signed with CSIG chained-signature extended Signify format.
*
Expand Down