php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27585 when openssl_pkcs7_verify fails once, it fails next time even if it should not
Submitted: 2004-03-13 07:04 UTC Modified: 2004-03-16 04:59 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: arnaud dot bertrand at apvsys dot org Assigned:
Status: Not a bug Package: OpenSSL related
PHP Version: 4.3.4 OS: win32 & Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: arnaud dot bertrand at apvsys dot org
New email:
PHP Version: OS:

 

 [2004-03-13 07:04 UTC] arnaud dot bertrand at apvsys dot org
Description:
------------
The function openssl_pkcs7_verify has a strange behaviour juster after a verification has report a bad signature.

When the verification reports a good signature, no problem

When it reports a bad signature, it works BUT the next time (if it is a short time) the function is called, the access to the CA certificate failed and it reports a bad signature even if it is a correct one.

Reproduce code:
---------------
Here is the function a use

///////////////// BEGIN

function CheckMailSignature($filename)
{
	global $CertificatDir;
	global $CertificatFile;
	echo("Processing file: $filename<br>\n");
	echo("Certificate: $CertificatDir<br>\n");
        chdir($CertificatDir);
	$tmp_cert = tempnam ("", "crt");
	$res = openssl_pkcs7_verify($filename, 0,$tmp_cert, array($CertificatDir, "$CertificatDir/$CertificatFile"));
	if ($res === false)
		echo("Digital Signature BAD!<br>\n");
	else if ($res === -1)
		echo("Error while verifying digital signature ($res)!<br>\n");
	else {
		echo("Digital Signature OK!<br>\n");

		$cert_info = openssl_x509_parse("file://$tmp_cert");
		echo("Common name: '".$cert_info['subject']['CN']."'<br>\n");
		echo("E-mail: '".$cert_info['subject']['Email']."'<br>\n");

		unlink($tmp_cert);
		return true;
	}
    unlink($tmp_cert);
    return false;
}


////////////////  END



Expected result:
----------------
Processing file: c:/test/abe-0.txt
Certificate: c:/certdir/cert
Digital Signature OK!
Common name: 'Thawte Freemail Member'
E-mail: 'arnaud.bertrand@apvsys.org'

// now check a bad one

Processing file: c:/test/abe-0-bad.txt
Certificate: c:/metadoc-iba/cert
Digital Signature BAD!

// Now check the correct one again

Processing file: c:/test/abe-0.txt
Certificate: c:/certdir/cert
Digital Signature OK!
Common name: 'Thawte Freemail Member'
E-mail: 'arnaud.bertrand@apvsys.org'



Actual result:
--------------
Processing file: c:/test/abe-0.txt
Certificate: c:/certdir/cert
Digital Signature OK!
Common name: 'Thawte Freemail Member'
E-mail: 'arnaud.bertrand@apvsys.org'

// now check a bad one

Processing file: c:/test/abe-0-bad.txt
Certificate: c:/metadoc-iba/cert
Digital Signature BAD!

// Now check the correct one again

Processing file: c:/test/abe-0.txt
Certificate: c:/certdir/cert

Warning: openssl_pkcs7_verify() [function.openssl-pkcs7-verify]: error loading file c:/cert/thawte_freemail.cer in c:\cvswork\ntmetapro\mailsign.php on line 12
Digital Signature BAD!

// Waiting a few minutes or restarting apache:

Processing file: c:/test/abe-0.txt
Certificate: c:/certdir/cert
Digital Signature OK!
Common name: 'Thawte Freemail Member'
E-mail: 'arnaud.bertrand@apvsys.org'



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-14 10:21 UTC] wez@php.net
Are you sure this happens under linux too?
I'd almost expect it under win32 (which has funny
locking semantics).
 [2004-03-14 10:34 UTC] wez@php.net
Also, tell us your openssl version and which sapi (cli, cgi, apache, isapi) you are using to reproduce this.
Does using one or all of the others (that you can try) also cause the problem?
 [2004-03-14 14:10 UTC] arnaud dot bertrand at apvsys dot org
Yes, I'm sure it happens under Linux (Suse8, Kernel 2.4)

The version I used was sapi php4apache.

After multiple tries, I found a version that works without the problem under Win32. This version has openssl 0.9.7.b.

The previous one that failed was 0.9.6.k.

Under Linux, because it is a friend of me who is testing it, I've to be sure of the exact versions.
 [2004-03-15 07:43 UTC] sniper@php.net
Let you friend report it then. (I can't reproduce this either on linux or windows)

 [2004-03-16 04:59 UTC] arnaud dot bertrand at apvsys dot org
I tried it on Linux 2.4 with apache 2.0.48 php 5.0.0b4
It fails with openssl 0.9.6l but works good with 0.9.7b

here's the script so you can try to reproduce it
==bug.php===================================================
<?php
function processMail($filename)
{
	$ret = false;
	$tmp_cert = tempnam ("", "crt");

	$res = openssl_pkcs7_verify($filename, 0, $tmp_cert, array(".", "thawte_freemail.cer"));
	if ($res === false)
		echo("Digital Signature BAD!<br>\n");
	else if ($res === -1)
		echo("Error while verifying digital signature ($res)!<br>\n");
	else {
		echo("Digital Signature OK!<br>\n");
		$cert_info = openssl_x509_parse("file://$tmp_cert");
		print_r($cert_info['subject']);
		
		$ret = true;
	}
	unlink($tmp_cert);
	return $ret;
}							

?>
<HTML>
<HEAD>
<TITLE>Signed Mail check</TITLE>
</HEAD>
<BODY>
<pre>
<?php
processMail('mail_ok.txt');
processMail('mail_bad.txt');
processMail('mail_ok.txt');
?>
</pre>
</BODY>
</HTML>
==mail_ok.txt===============================================
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.3) Gecko/20030312
X-Accept-Language: en-us, en
MIME-Version: 1.0
To: xyz@notmyrealaddress.com
Subject: DigiSign
Content-Type: multipart/signed; protocol="application/x-pkcs7-signature"; micalg=sha1; boundary="------------ms020400030006030201090307"
Status: U
This is a cryptographically signed message in MIME format.

--------------ms020400030006030201090307
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

user=jfm
document=test2.txt
version=1.1
checksum=ASH454sdFDD5s4g54b56jhg156qzejh

--------------ms020400030006030201090307
Content-Type: application/x-pkcs7-signature; name="smime.p7s"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="smime.p7s"
Content-Description: S/MIME Cryptographic Signature

MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIIH5TCC
Ak0wggG2oAMCAQICAwvUDjANBgkqhkiG9w0BAQQFADBiMQsw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29uc3VsdGluZyAoUHR5KSBMdGQuMSwwKgYDVQQDEyNUaGF3dGUgUGVyc29uYWwgRnJl
ZW1haWwgSXNzdWluZyBDQQIDC9QOMHoGCyqGSIb3DQEJEAILMWugaTBiMQswCQYDVQQGEwJa
QTElMCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkgTHRkLjEsMCoGA1UEAxMjVGhh
d3RlIFBlcnNvbmFsIEZyZWVtYWlsIElzc3VpbmcgQ0ECAwvUDjANBgkqhkiG9w0BAQEFAASB
gKy6YFFpuMyjCT4BMbqD06pv8x7n9KUXB+jHy3y1T+Cre7ygPJ20W1BDazaZsuykqVO1YX5J
jNThDYP8K57W0K34dCndY+qGMLdqKRMwyZvQoTtkH0pKFdslhzc+hnFK2RXCBapdpK5f0WCQ
Ly2eSSK9SZiDB6wTJnab7Ariuw3fAAAAAAAA
--------------ms020400030006030201090307--
==mail_bad.txt==============================================
just take mail_ok.txt and change something in the message body
==thawte_freemail.cer=======================================
-----BEGIN CERTIFICATE-----
MIIDLTCCApagAwIBAgIBADANBgkqhkiG9w0BAQQFADCB0TELMAkGA1UEBhMCWkEx
FTATBgNVBAgTDFdlc3Rlcm4gQ2FwZTESMBAGA1UEBxMJQ2FwZSBUb3duMRowGAYD
VQQKExFUaGF3dGUgQ29uc3VsdGluZzEoMCYGA1UECxMfQ2VydGlmaWNhdGlvbiBT
ZXJ2aWNlcyBEaXZpc2lvbjEkMCIGA1UEAxMbVGhhd3RlIFBlcnNvbmFsIEZyZWVt
YWlsIENBMSswKQYJKoZIhvcNAQkBFhxwZXJzb25hbC1mcmVlbWFpbEB0aGF3dGUu
Y29tMB4XDTk2MDEwMTAwMDAwMFoXDTIwMTIzMTIzNTk1OVowgdExCzAJBgNVBAYT
AlpBMRUwEwYDVQQIEwxXZXN0ZXJuIENhcGUxEjAQBgNVBAcTCUNhcGUgVG93bjEa
MBgGA1UEChMRVGhhd3RlIENvbnN1bHRpbmcxKDAmBgNVBAsTH0NlcnRpZmljYXRp
b24gU2VydmljZXMgRGl2aXNpb24xJDAiBgNVBAMTG1RoYXd0ZSBQZXJzb25hbCBG
cmVlbWFpbCBDQTErMCkGCSqGSIb3DQEJARYccGVyc29uYWwtZnJlZW1haWxAdGhh
d3RlLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA1GnX1LCUZFtx6UfY
DFG26nKRsIRefS0Nj3sS34UldSh0OkIsYyeflXtL734Zhx2G6qPduc6WZBrCFG5E
rHzmj+hND3EfQDimAKOHePb5lIZererAXnbr2RSjXW56fAylS1V/Bhkpf56aJtVq
uzgkCGqYx7Hao5iR/Xnb5VrEHLkCAwEAAaMTMBEwDwYDVR0TAQH/BAUwAwEB/zAN
BgkqhkiG9w0BAQQFAAOBgQDH7JJ+Tvj1lqVnYiqk8E0RYNBvjWBYYawmu1I1XAjP
MPuoSpaKH2JCI4wXD/S6ZJwXrEcp352YXtJsYHFcoqzceePnbgBHH7UNKOgCneSa
/RP0ptl8sfjcXyMmCZGAc9AUG95DqYMl8uacLxXK/qarigd1iwzdUYRr5PjRznei
gQ==
-----END CERTIFICATE-----
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Nov 28 00:03:13 2021 UTC