@@ -303,6 +303,7 @@ public function verifyMessage($message, $eml) {
303303 if (!empty ($ userCert )) { // Check MAPI UserStore
304304 file_put_contents ($ tmpUserCert , $ userCert );
305305 }
306+ $ this ->clear_openssl_error ();
306307 $ signed_ok = openssl_pkcs7_verify ($ tmpfname , PKCS7_NOINTERN , $ outcert , explode ('; ' , PLUGIN_SMIME_CACERTS ), $ tmpUserCert );
307308 $ openssl_error_code = $ this ->extract_openssl_error ();
308309 $ this ->validateSignedMessage ($ signed_ok , $ openssl_error_code );
@@ -335,6 +336,7 @@ public function verifyMessage($message, $eml) {
335336 }
336337 else {
337338 // Works. Just leave it.
339+ $ this ->clear_openssl_error ();
338340 $ signed_ok = openssl_pkcs7_verify ($ tmpfname , PKCS7_NOSIGS , $ outcert , explode ('; ' , PLUGIN_SMIME_CACERTS ));
339341 $ openssl_error_code = $ this ->extract_openssl_error ();
340342 $ this ->validateSignedMessage ($ signed_ok , $ openssl_error_code );
@@ -361,6 +363,7 @@ public function verifyMessage($message, $eml) {
361363 // Certificate is newer or not yet imported to the user store and not revoked
362364 // If certificate is from the GAB, then don't import it.
363365 if ($ importMessageCert && !$ fromGAB ) {
366+ $ this ->clear_openssl_error ();
364367 $ signed_ok = openssl_pkcs7_verify ($ tmpfname , PKCS7_NOSIGS , $ outcert , explode ('; ' , PLUGIN_SMIME_CACERTS ));
365368 $ openssl_error_code = $ this ->extract_openssl_error ();
366369 $ this ->validateSignedMessage ($ signed_ok , $ openssl_error_code );
@@ -425,16 +428,19 @@ public function onEncrypted($data) {
425428 // If multiple private certs were decrypted with supplied password
426429 if (!$ certs ['cert ' ] && count ($ certs ) > 0 ) {
427430 foreach ($ certs as $ cert ) {
431+ $ this ->clear_openssl_error ();
428432 $ decryptStatus = openssl_pkcs7_decrypt ($ tmpFile , $ tmpDecrypted , $ cert ['cert ' ], [$ cert ['pkey ' ], $ pass ]);
429433 if ($ decryptStatus !== false ) {
430434 break ;
431435 }
432436 }
433437 }
434438 else {
439+ $ this ->clear_openssl_error ();
435440 $ decryptStatus = openssl_pkcs7_decrypt ($ tmpFile , $ tmpDecrypted , $ certs ['cert ' ], [$ certs ['pkey ' ], $ pass ]);
436441 }
437442
443+ $ ossl_error = $ this ->extract_openssl_error ();
438444 $ content = file_get_contents ($ tmpDecrypted );
439445 // Handle OL empty body Outlook Signed & Encrypted mails.
440446 // The S/MIME plugin has to extract the body from the signed message.
@@ -469,7 +475,7 @@ public function onEncrypted($data) {
469475 $ this ->message ['info ' ] = SMIME_DECRYPT_SUCCESS ;
470476 $ this ->message ['success ' ] = SMIME_STATUS_SUCCESS ;
471477 }
472- elseif ($ this -> extract_openssl_error () === OPENSSL_RECIPIENT_CERTIFICATE_MISMATCH ) {
478+ elseif ($ ossl_error === OPENSSL_RECIPIENT_CERTIFICATE_MISMATCH ) {
473479 error_log ("[smime] Error when decrypting email, openssl error: " . print_r ($ this ->openssl_error , true ));
474480 Log::Write (LOGLEVEL_ERROR , sprintf ("[smime] Error when decrypting email, openssl error: '%s' " , $ this ->openssl_error ));
475481 $ this ->message ['info ' ] = SMIME_DECRYPT_CERT_MISMATCH ;
@@ -982,6 +988,12 @@ public function pubcertExists($emailAddress, $gabUser = false) {
982988 return !empty ($ rows );
983989 }
984990
991+ public function clear_openssl_error ()
992+ {
993+ while (@openssl_error_string () !== false )
994+ /* nothing */ ;
995+ }
996+
985997 /**
986998 * Helper functions which extracts the errors from openssl_error_string()
987999 * Example error from openssl_error_string(): error:21075075:PKCS7 routines:PKCS7_verify:certificate verify error
@@ -990,8 +1002,12 @@ public function pubcertExists($emailAddress, $gabUser = false) {
9901002 * @return string
9911003 */
9921004 public function extract_openssl_error () {
993- // TODO: should catch more errors by using while($error = @openssl_error_string())
994- $ this ->openssl_error = @openssl_error_string ();
1005+ $ this ->openssl_error = "" ;
1006+ while (($ s = @openssl_error_string ()) !== false )
1007+ if (strlen ($ this ->openssl_error ) == 0 )
1008+ $ this ->openssl_error = $ s ;
1009+ else
1010+ $ this ->openssl_error .= "\n" .$ s ;
9951011 $ openssl_error_code = 0 ;
9961012 if ($ this ->openssl_error ) {
9971013 $ openssl_error_list = explode (": " , $ this ->openssl_error );
0 commit comments