Bug #19762 [Opn]: splitBody for multipart/signed does not conform to RFC

From: Date: Fri, 28 Dec 2012 18:24:42 +0000
Subject: Bug #19762 [Opn]: splitBody for multipart/signed does not conform to RFC
References: 1  Groups: php.pear.bugs 
Request: Send a blank email to pear-bugs+get-9437@lists.php.net to get a copy of this message
Edit report at https://pear.php.net/bugs/bug.php?id=19762&edit=1 ID: 19762 Updated by: fredcw@gmail.com Reported By: fredcw at gmail dot com Summary: splitBody for multipart/signed does not conform to RFC Status: Open Type: Bug Package: Mail_mimeDecode Operating System: linux Package Version: 1.5.5 -PHP Version: 5.4.3 +PHP Version: Irrelevant Roadmap Versions: New Comment: -PHP Version: 5.4.3 +PHP Version: Irrelevant Previous Comments: ------------------------------------------------------------------------ [2012-12-28 17:47:22] fredcwbr Added #patch bug:19762;patch:comply-rfc2046;revision:1356716842;. ------------------------------------------------------------------------ [2012-12-28 17:43:09] fredcwbr Missing files to the bug to demonstrate the problem. ** $> cat teste05.eml From - Wed Dec 26 17:00:35 2012 X-Mozilla-Status: 0001 X-Mozilla-Status2: 10800000 X-Mozilla-Keys: Message-ID: <50DB4953.6060706@novomundo.com.br> Date: Wed, 26 Dec 2012 17:00:35 -0200 From: Frederico C Wilhelms <fredcw@novomundo.com.br> Organization: Novo Mundo =?ISO-8859-1?Q?M=F3veis_e_Utilidades_Ltda?= User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.6esrpre) Gecko/20120817 Icedove/10.0.6 MIME-Version: 1.0 To: fredcw@novomundo.com.br Subject: teaste X-Enigmail-Version: 1.4.1 OpenPGP: id=6AE2FC05 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enigC56CA05CC37EAE30974B19BD" This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enigC56CA05CC37EAE30974B19BD Content-Type: multipart/mixed; boundary="------------030405000909000706050601" This is a multi-part message in MIME format. --------------030405000909000706050601 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable atsteasetatsestse --=20 [QDNAAT-GS]sates Fred Frederico C Wilhelms Sup. Suporte Tec.=20 Novo Mundo 55-62-3219-3127 fredcw@novomundo.com.br --------------030405000909000706050601 Content-Type: application/pgp-keys; name="0x6AE2FC05.asc" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="0x6AE2FC05.asc" -----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v1.4.12 (GNU/Linux) mQGiBEjSdqsRBACm6yR9TaQf3D0noeVmo2v40YJ2MaVQsv8GC2ebg9smdmt2txY8 4OV5yzeRZzXtpPacTssWw9/UjpgKO+gpizYOyaeY2dZ56OG/1OTnksLIAF2jl+ua PpNxWePfn6T2c9iQPV/5HvSGiSQf08kGg8B+IRvdFAtuTDkrn3IV6z2FSwCglAxO BVkAQKQnTieDZcp71NSIZ3MD/1qraca0s57mLmbe4HTi5lc0J2Hybu3cxQ9GPt2p /XIvJvJ2QnF+uGQF1vSgNrg6ew/KDc8QPF1diT9VLBZOgkSnA32wIkVd/IB+zls6 izT4wwstp5gNZNdkLopEdm9YrvDHnfPXqvjKj9pGfdah9tOEie8V+SQdzixNJpJm rSoCA/oCWT07zO/MndbutRyOTU7DU/3qsq5lp02spf1fFaEeYEIlFnlbPqsfBBxw cHEpDmcxo9+NuI3N9UvmRSDXE2kAVGVoad3blblnYhwcvqsz4Em/0BUHTWwxp4HX ovNBynxeu2fuVVNgMjvPejFgi8MuPy0ErvHEH8+ROl/dYZPO/rR5RnJlZGVyaWNv IENhcmxvcyBXaWxoZWxtcyAoU3VwZXJ2aXNvciBkZSBTdXBvcnRlIFRlY25pY28g LSBOb3ZvIE11bmRvIE1vdmVpcyBlIFV0aWxpZGFkZXMgTHRkYSkgPGZyZWRjd0Bu b3ZvbXVuZG8uY29tLmJyPohgBBMRAgAgBQJI0narAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQswBepGri/AVG6wCffVtxisNwBm/urq7heeOqcA/cKXwAnjKx h79f3ICp40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ffxqJ65yg4fQA7axDmNiS2p3GdNmR eZx+rKgB8n1YhKBCHcsXUyqlTMugtPiuG7JbiLZFJyKCA6H1IsygYbVjZks5Myoq UFceix8/g359sKgPNPaISQQYEQIACQUCSNJ2xAIbDAAKCRCzAF6kauL8BTr9AJwL p5sSc8zi3y3iDje/1eui1qlegACfSUp3qI7hS7nTxTadsdlQtpnCvqg=3D =3DGqiy -----END PGP PUBLIC KEY BLOCK----- --------------030405000909000706050601 Content-Type: text/x-vcard; charset=utf-8; name="fredcw.vcf" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="fredcw.vcf" begin:vcard fn:Frederico C Wilhelms n:Wilhelms;Frederico C org;quoted-printable:Novo Mundo M=3DC3=3DB3veis e Utilidades Ltda;TI - No= vo Mundo adr:Centro;;R. 3, N. 170;Goiania;GO;74037-070;Brasil email;internet:fredcw@novomundo.com.br title;quoted-printable:Supervisor de Suporte T=3DC3=3DA9cnico x-mozilla-html:FALSE url:www.novomundo.com.br version:2.1 end:vcard --------------030405000909000706050601-- --------------enigC56CA05CC37EAE30974B19BD Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAlDbSVMACgkQswBepGri/AWM+gCcCnyatD9sQOG9h4yaQzft7zt6 FyoAni+ebl0wI1PiOX1ovRoNgo0rErrF =ZWMD -----END PGP SIGNATURE----- --------------enigC56CA05CC37EAE30974B19BD-- $>cat teste_e1.php <?PHP require_once 'Mail/mimeDecode.php'; require_once 'Crypt/GPG.php'; $params['include_bodies'] = true; $params['rfc_822bodies'] = false; $params['decode_bodies'] = true; $params['decode_headers'] = false; $fname = getenv('ARQ0'); echo "Reading .eml file ==> " . $fname . "\n"; $input = file_get_contents( $fname ); $decoder = new Mail_mimeDecode($input); $structure = $decoder->decode($params); // temporary parts of the mail to aid debug // // echo "**********************vvvvvvvvvvvv\n"; // print_r( $structure ); echo "\n"; $cnt = 0; foreach( $structure->parts as $ndx => $anexo ) { echo "**********************vvvvvvvvvvvv\n"; print_r( $anexo ); echo "\n"; $fwx = fopen( "tmp_x" . $cnt , "w" ); fwrite( $fwx , $anexo ); ++$cnt; } // if( isset( $structure->parts['msg_body'] ) ) { // patched version --- go ... // do something useful // try { $gnupgres = new Crypt_GPG(); $info = $gnupgres->verify( $structure->parts['msg_body'] , $structure->parts['sig_body'] ); print_r( $info ); echo " Signature : " . ( $info[0]->isValid() ? "Valid" : "Invalid" ) . " \n"; if( $info[0]->isValid() ) { $quem = array( "Fingerprint" => $info[0]->getKeyFingerprint() , "email" => $info[0]->getUserId()->getEmail() ); } } catch( Crypt_GPG_Exception $e ) { echo "Excessao : " . $e->getMessage(); } } if( !isset( $quem ) ) { // nonsense echo "Source origin unverified\n"; return; } $validos = array( array( "Fingerprint" => "109988DAA6857388819F3AC5B3005EA46AE2FC05", "email" => "fredcw@novomundo.com.br" ) ); if( $quem['Fingerprint'] == $validos[0]['Fingerprint'] ) echo "From verified ......\n"; else echo "From mail not valid **** ERROR\n"; ?> ------------------------------------------------------------------------ [2012-12-28 17:36:28] fredcwbr Description: ------------ RFC 2046 --- 5.1.1. Common Syntax This section defines a common syntax for subtypes of "multipart". All subtypes of "multipart" must use this syntax. ..... .... The Content-Type field for multipart entities requires one parameter, "boundary". The boundary delimiter line is then defined as a line consisting entirely of two hyphen characters ("-", decimal value 45) followed by the boundary parameter value from the Content-Type header field, optional linear whitespace, and a terminating CRLF. ** My notes ** The "\n" is part of the boundary, and must be extracted with it. Failure to do so renders the body with an extra "\n" (the boundary added one) Separation of parts in the email happens with a "\n" alone. So the preg_split regex actually leaves 2 "\n" on each part On _boundarySplit() $tmp = preg_split("/--".preg_quote($boundary, '/')."((?=\s)|--)/", $input); does not consume the ending "\n" from the boundary, neither the "\n" that ends the element ... patching to -->> $tmp = preg_split("/\n--".preg_quote($boundary, '/')."(|--)\n/", $input); Test script: --------------- execute as : $> ARQ0=teste05.eml php teste_e1.php Expected result: ---------------- To aid debugging, three files (tmp_x[0,1,2]) with the contents of the email broken in the signed mail body part, signature attributes and armoured gpg signature * A message validating the signed mailbody . Actual result: -------------- Without the patch, it breaks down the signed message, making it impossible to reconstruct the exact original, so gpg can verify. With the following patch, it works fine. *** mimeDecode.php.orig 2012-12-26 14:44:41.000000000 -0200 --- mimeDecode.php 2012-12-28 13:40:32.000000000 -0200 *************** *** 308,317 **** $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding) : $body) : null; break; case 'multipart/parallel': case 'multipart/appledouble': // Appledouble mail case 'multipart/report': // RFC1892 - case 'multipart/signed': // PGP case 'multipart/digest': case 'multipart/alternative': case 'multipart/related': --- 308,326 ---- $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding) : $body) : null; break; + case 'multipart/signed': // PGP + $parts = $this->_boundarySplit($body, $content_type['other']['boundary']); + $return->parts['msg_body'] = $parts[0]; + list($part_header, $part_body) = $this->_splitBodyHeader($parts[1]); + $return->parts['sig_hdr'] = $part_header; + $return->parts['sig_body'] = $part_body; + + break; + + case 'multipart/parallel': case 'multipart/appledouble': // Appledouble mail case 'multipart/report': // RFC1892 case 'multipart/digest': case 'multipart/alternative': case 'multipart/related': *************** *** 658,664 **** if ($boundary == $bs_check) { $boundary = $bs_possible; } ! $tmp = preg_split("/--".preg_quote($boundary, '/')."((?=\s)|--)/", $input); $len = count($tmp) -1; for ($i = 1; $i < $len; $i++) { --- 667,673 ---- if ($boundary == $bs_check) { $boundary = $bs_possible; } ! $tmp = preg_split("/\n--".preg_quote($boundary, '/')."(|--)\n/", $input); $len = count($tmp) -1; for ($i = 1; $i < $len; $i++) { ------------------------------------------------------------------------ -- Edit this bug report at https://pear.php.net/bugs/bug.php?id=19762&edit=1

« previous php.pear.bugs (#9437) next »