Skip to content

Commit

Permalink
Merge pull request tiamo#34 from robsonala/master
Browse files Browse the repository at this point in the history
Fix verify (binary) + change setBody
  • Loading branch information
tiamo authored Jan 21, 2022
2 parents 73239e8 + 3549832 commit ae566fa
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 8 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"php": "^7.1 || ^7.2 || ^7.3 || ^7.4 || ^8.0",
"ext-openssl": "*",
"ext-zlib": "*",
"ext-ctype": "*",
"guzzlehttp/guzzle": "^6.5 || ^7.0",
"phpseclib/phpseclib": "^3.0.8",
"psr/log": "^1.1"
Expand Down
5 changes: 5 additions & 0 deletions src/CryptoHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public static function sign($data, $cert, $privateKey = null, $headers = [], $mi
public static function verify($data, $caInfo = null, $rootCerts = [])
{
if ($data instanceof MimePart) {
$temp = MimePart::createIfBinaryPart($data);
if ($temp !== null) {
$data = $temp;
}

$data = self::getTempFilename((string) $data);
}

Expand Down
41 changes: 40 additions & 1 deletion src/MimePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,31 @@ public static function fromString($rawMessage, $saveRaw = true)
return new static($payload['headers'], $payload['body'], $saveRaw ? $rawMessage : null);
}

/**
* Recreate message with base64 if part is binary
*
* @param self $message
*
* @return null|self
*/
public static function createIfBinaryPart(self $message): ?self
{
$hasBinary = false;

$temp = new self($message->getHeaders());
foreach ($message->getParts() as $part) {
if (Utils::isBinary($part->getBody())) {
$hasBinary = true;
$recreatedPart = new self($part->getHeaders(), Utils::encodeBase64($part->getBody()));
$temp->addPart($recreatedPart);
} else {
$temp->addPart($part);
}
}

return $hasBinary ? $temp : null;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -321,7 +346,21 @@ public function setBody($body)
array_pop($parts); // remove unecessary last element

foreach ($parts as $part) {
$part = preg_replace('/^\r?\n|\r?\n$/','',$part);
//$part = preg_replace('/^\r?\n|\r?\n$/','',$part);
// Using substr instead of preg_replace as that option is removing multiple break lines instead of only one

// /^\r?\n/
if (substr($part, 0, 2) === "\r\n") {
$part = substr($part, 2);
} elseif (substr($part, 0, 1) === "\n") {
$part = substr($part, 1);
}
// /\r?\n$/
if (substr($part, -2) === "\r\n") {
$part = substr($part, 0, -2);
} elseif (substr($part, -1) === "\n") {
$part = substr($part, 0, -1);
}

$this->addPart($part);
}
Expand Down
14 changes: 14 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,18 @@ public static function fixEncoding($s)
// removes xD800-xDFFF, x110000 and higher
return htmlspecialchars_decode(htmlspecialchars($s, ENT_NOQUOTES | ENT_IGNORE, 'UTF-8'), ENT_NOQUOTES);
}

/**
* Verify if the content is binary
*
* @param mixed $str
*
* @return bool
*/
public static function isBinary($str): bool
{
$str = str_ireplace(["\t","\n","\r"], ["","",""], $str);

return is_string($str) && ctype_print($str) === false;
}
}
17 changes: 12 additions & 5 deletions tests/Unit/CryptoHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@ public function testSign()
self::assertTrue($hasSignature);
}

/**
* TODO: verify binary data.
*/
public function testVerify()
public function testVerifyBase64()
{
// $contents = $this->loadFixture('si_signed.mdn');
$contents = $this->loadFixture('signed-msg.txt');
$payload = MimePart::fromString($contents);

Expand All @@ -54,6 +50,17 @@ public function testVerify()
self::assertTrue(CryptoHelper::verify($payload, $certs['cert']));
}

public function testVerifyBinary()
{
$contents = $this->loadFixture('si_signed.mdn');
$payload = MimePart::fromString($contents);

$certs = $this->getCerts();

self::assertTrue($payload->isSigned());
self::assertTrue(CryptoHelper::verify($payload, $certs['cert']));
}

public function testEncrypt()
{
$payload = $this->initMessage();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testProcessMessage()
self::assertTrue($message->getEncrypted());
self::assertTrue($message->getSigned());
self::assertSame($message->getMic(), 'oVDpnrSnpq+V99dXaarQ9HFyRUaFNsp9tdBBSmRhX4s=, sha256');
self::assertSame((string) $processedPayload, Utils::canonicalize($this->loadFixture('test.edi')));
self::assertSame(trim((string) $processedPayload), Utils::canonicalize($this->loadFixture('test.edi')));
}

public function testProcessMessageBiggerMessage()
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/MimePartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,20 @@ public function testBodyWithoutHeaders(): void
self::assertEmpty($res->getHeaders());
self::assertStringStartsWith('UNB+UNOA', $res->getBody());
}

public function testCreateIfBinaryPartNotBinary(): void
{
$contents = $this->loadFixture('signed-msg.txt');
$payload = MimePart::fromString($contents);

self::assertNull(MimePart::createIfBinaryPart($payload));
}

public function testCreateIfBinaryPartBinary(): void
{
$contents = $this->loadFixture('si_signed.mdn');
$payload = MimePart::fromString($contents);

self::assertInstanceOf(MimePart::class, MimePart::createIfBinaryPart($payload));
}
}
2 changes: 1 addition & 1 deletion tests/fixtures/test.edi
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ PRI+AAA:3.85'
UNS+S'
CNT+2:3'
UNT+26+1'
UNZ+1+5'
UNZ+1+5'

0 comments on commit ae566fa

Please sign in to comment.