php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74720 pkcs7_en/decrypt does not work if \x1a is used in content
Submitted: 2017-06-08 23:05 UTC Modified: 2017-06-09 00:04 UTC
From: jan dot slabon at setasign dot com Assigned:
Status: Closed Package: OpenSSL related
PHP Version: 7.2.0alpha1 OS: Windows 10 Pro
Private report: No CVE-ID: None
 [2017-06-08 23:05 UTC] jan dot slabon at setasign dot com
Description:
------------
When passing content, which uses a byte with the value \x1a, to a file which is processed further with openssl_pkcs7_encrypt() and later with openssl_pkcs7_decrypt() the decrypted content is cut at the position of the byte with the value \x1a.

Environment: 7.2.0alpha1 - VC14 x64 Non Thread Safe

Test script:
---------------
<?php

$cert = "-----BEGIN CERTIFICATE-----
MIIDXDCCAkSgAwIBAgIKq/f5U3FzthdKUzANBgkqhkiG9w0BAQUFADBcMRIwEAYD
VQQDEwlzZXRhcGRmLWExCTAHBgNVBAoTADEJMAcGA1UECxMAMSMwIQYJKoZIhvcN
AQkBFhRzdXBwb3J0QHNldGFzaWduLmNvbTELMAkGA1UEBhMCREUwHhcNMTUwOTA4
MDkzNDExWhcNMjAwOTA4MDkzNDExWjBcMRIwEAYDVQQDEwlzZXRhcGRmLWExCTAH
BgNVBAoTADEJMAcGA1UECxMAMSMwIQYJKoZIhvcNAQkBFhRzdXBwb3J0QHNldGFz
aWduLmNvbTELMAkGA1UEBhMCREUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK
AoIBAQCkmTLvUIYfqAKC1CHVgABlemrFIVRm4JGeB0jIGofyrm3yVwR4YcK0eUmt
/0nbFfsFsU0/C9dXKZYD42t5YpLFsj666Z1EoU1CfSIW2bf0HaWVJ+oNT5twS3dv
RTzAcnPM44GxO4y6GUW6un0/bT/MZbFKbb3NI1L0mwY7EoqUXR68XxuHqWETaslm
Sbp1XvbLsJjgV9X3ihi7JC7A9kEzrKh+RBsXEXwlSv5JO2TUdwq9P4EbjqvgiaV1
vFNAArioX5pIUIm9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5Y72WVnZvpTPO0cDw2i1hyaEM6d+
2WR7c6FhRCLxG0DObEOfiO0=
-----END PRIVATE KEY-----
";

$originalEnvelopeData = "any string with \x1a is cut at this point.";
//$originalEnvelopeData = "any string with \xa1 is cut at this point."; // <-- works

$tmpFileIn = tempnam(sys_get_temp_dir(), 'test');
$tmpFileOut = tempnam(sys_get_temp_dir(), 'test');
file_put_contents($tmpFileIn, $originalEnvelopeData);

var_dump(filesize($tmpFileIn) === strlen($originalEnvelopeData));

openssl_pkcs7_encrypt($tmpFileIn, $tmpFileOut, [$cert], array(), PKCS7_BINARY, OPENSSL_CIPHER_AES_128_CBC);

$tmpFileOut2 = tempnam(sys_get_temp_dir(), 'test');
openssl_pkcs7_decrypt($tmpFileOut, $tmpFileOut2, $cert, $pkey);

$envelopeData = file_get_contents($tmpFileOut2);
var_dump($originalEnvelopeData === $envelopeData); // need to be true
var_dump(strlen($originalEnvelopeData), strlen($envelopeData), filesize($tmpFileOut2));

Expected result:
----------------
bool(true)
bool(true)
int(39)
int(39)
int(39)

Actual result:
--------------
bool(true)
bool(false)
int(39)
int(16)
int(16)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-06-09 00:04 UTC] pollita@php.net
This may be a Windows specific issue.  I get expected results on Ubuntu 17.04 w/ OpenSSL 1.0.2g

I believe we're going to see a rebuild of the Windows package using VC15 either soonish, or perhaps with alpha2.  I'll do some asking around.
 [2017-06-09 08:45 UTC] jan dot slabon at setasign dot com
This behaviour is also reproducible in openssl_pkcs7_sign().

Another test script is available here: https://3v4l.org/OnKD9

The correct output is "bool(true)" while on 7.2.0alpha1 (windows) we get "bool(false)" because the content is also cut at \x1a.
 [2017-06-09 15:38 UTC] ab@php.net
Automatic comment on behalf of ab
Revision: http://git.php.net/?p=php-src.git;a=commit;h=cbe73b3cfe5945926c8d91dcc0884ce7524764eb
Log: Fixed bug #74720 pkcs7_en/decrypt does not work if \x1a is used in
 [2017-06-09 15:38 UTC] ab@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 06 01:01:27 2024 UTC